Coverage Report

Created: 2022-01-18 06:27

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Frontend/InitPreprocessor.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- InitPreprocessor.cpp - PP initialization code. ---------*- C++ -*-===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// This file implements the clang::InitializePreprocessor function.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "clang/Basic/FileManager.h"
14
#include "clang/Basic/MacroBuilder.h"
15
#include "clang/Basic/SourceManager.h"
16
#include "clang/Basic/SyncScope.h"
17
#include "clang/Basic/TargetInfo.h"
18
#include "clang/Basic/Version.h"
19
#include "clang/Frontend/FrontendDiagnostic.h"
20
#include "clang/Frontend/FrontendOptions.h"
21
#include "clang/Frontend/Utils.h"
22
#include "clang/Lex/HeaderSearch.h"
23
#include "clang/Lex/Preprocessor.h"
24
#include "clang/Lex/PreprocessorOptions.h"
25
#include "clang/Serialization/ASTReader.h"
26
#include "llvm/ADT/APFloat.h"
27
#include "llvm/IR/DataLayout.h"
28
using namespace clang;
29
30
37.0k
static bool MacroBodyEndsInBackslash(StringRef MacroBody) {
31
37.0k
  while (!MacroBody.empty() && 
isWhitespace(MacroBody.back())36.7k
)
32
0
    MacroBody = MacroBody.drop_back();
33
37.0k
  return !MacroBody.empty() && 
MacroBody.back() == '\\'36.7k
;
34
37.0k
}
35
36
// Append a #define line to Buf for Macro.  Macro should be of the form XXX,
37
// in which case we emit "#define XXX 1" or "XXX=Y z W" in which case we emit
38
// "#define XXX Y z W".  To get a #define with no value, use "XXX=".
39
static void DefineBuiltinMacro(MacroBuilder &Builder, StringRef Macro,
40
348k
                               DiagnosticsEngine &Diags) {
41
348k
  std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
42
348k
  StringRef MacroName = MacroPair.first;
43
348k
  StringRef MacroBody = MacroPair.second;
44
348k
  if (MacroName.size() != Macro.size()) {
45
    // Per GCC -D semantics, the macro ends at \n if it exists.
46
37.0k
    StringRef::size_type End = MacroBody.find_first_of("\n\r");
47
37.0k
    if (End != StringRef::npos)
48
3
      Diags.Report(diag::warn_fe_macro_contains_embedded_newline)
49
3
        << MacroName;
50
37.0k
    MacroBody = MacroBody.substr(0, End);
51
    // We handle macro bodies which end in a backslash by appending an extra
52
    // backslash+newline.  This makes sure we don't accidentally treat the
53
    // backslash as a line continuation marker.
54
37.0k
    if (MacroBodyEndsInBackslash(MacroBody))
55
1
      Builder.defineMacro(MacroName, Twine(MacroBody) + "\\\n");
56
37.0k
    else
57
37.0k
      Builder.defineMacro(MacroName, MacroBody);
58
311k
  } else {
59
    // Push "macroname 1".
60
311k
    Builder.defineMacro(Macro);
61
311k
  }
62
348k
}
63
64
/// AddImplicitInclude - Add an implicit \#include of the specified file to the
65
/// predefines buffer.
66
/// As these includes are generated by -include arguments the header search
67
/// logic is going to search relatively to the current working directory.
68
7.21k
static void AddImplicitInclude(MacroBuilder &Builder, StringRef File) {
69
7.21k
  Builder.append(Twine("#include \"") + File + "\"");
70
7.21k
}
71
72
2
static void AddImplicitIncludeMacros(MacroBuilder &Builder, StringRef File) {
73
2
  Builder.append(Twine("#__include_macros \"") + File + "\"");
74
  // Marker token to stop the __include_macros fetch loop.
75
2
  Builder.append("##"); // ##?
76
2
}
77
78
/// Add an implicit \#include using the original file used to generate
79
/// a PCH file.
80
static void AddImplicitIncludePCH(MacroBuilder &Builder, Preprocessor &PP,
81
                                  const PCHContainerReader &PCHContainerRdr,
82
3.66k
                                  StringRef ImplicitIncludePCH) {
83
3.66k
  std::string OriginalFile = ASTReader::getOriginalSourceFile(
84
3.66k
      std::string(ImplicitIncludePCH), PP.getFileManager(), PCHContainerRdr,
85
3.66k
      PP.getDiagnostics());
86
3.66k
  if (OriginalFile.empty())
87
1
    return;
88
89
3.66k
  AddImplicitInclude(Builder, OriginalFile);
90
3.66k
}
91
92
/// PickFP - This is used to pick a value based on the FP semantics of the
93
/// specified FP model.
94
template <typename T>
95
static T PickFP(const llvm::fltSemantics *Sem, T IEEEHalfVal, T IEEESingleVal,
96
                T IEEEDoubleVal, T X87DoubleExtendedVal, T PPCDoubleDoubleVal,
97
2.95M
                T IEEEQuadVal) {
98
2.95M
  if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEhalf())
99
58.3k
    return IEEEHalfVal;
100
2.89M
  if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEsingle())
101
965k
    return IEEESingleVal;
102
1.93M
  if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEdouble())
103
1.08M
    return IEEEDoubleVal;
104
843k
  if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::x87DoubleExtended())
105
754k
    return X87DoubleExtendedVal;
106
89.1k
  if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::PPCDoubleDouble())
107
41.7k
    return PPCDoubleDoubleVal;
108
47.3k
  assert(Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEquad());
109
0
  return IEEEQuadVal;
110
89.1k
}
InitPreprocessor.cpp:char const* PickFP<char const*>(llvm::fltSemantics const*, char const*, char const*, char const*, char const*, char const*, char const*)
Line
Count
Source
97
1.07M
                T IEEEQuadVal) {
98
1.07M
  if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEhalf())
99
21.2k
    return IEEEHalfVal;
100
1.05M
  if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEsingle())
101
351k
    return IEEESingleVal;
102
701k
  if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEdouble())
103
395k
    return IEEEDoubleVal;
104
306k
  if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::x87DoubleExtended())
105
274k
    return X87DoubleExtendedVal;
106
32.4k
  if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::PPCDoubleDouble())
107
15.1k
    return PPCDoubleDoubleVal;
108
17.2k
  assert(Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEquad());
109
0
  return IEEEQuadVal;
110
32.4k
}
InitPreprocessor.cpp:int PickFP<int>(llvm::fltSemantics const*, int, int, int, int, int, int)
Line
Count
Source
97
1.87M
                T IEEEQuadVal) {
98
1.87M
  if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEhalf())
99
37.1k
    return IEEEHalfVal;
100
1.84M
  if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEsingle())
101
614k
    return IEEESingleVal;
102
1.22M
  if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEdouble())
103
691k
    return IEEEDoubleVal;
104
536k
  if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::x87DoubleExtended())
105
480k
    return X87DoubleExtendedVal;
106
56.7k
  if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::PPCDoubleDouble())
107
26.5k
    return PPCDoubleDoubleVal;
108
30.1k
  assert(Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEquad());
109
0
  return IEEEQuadVal;
110
56.7k
}
111
112
static void DefineFloatMacros(MacroBuilder &Builder, StringRef Prefix,
113
268k
                              const llvm::fltSemantics *Sem, StringRef Ext) {
114
268k
  const char *DenormMin, *Epsilon, *Max, *Min;
115
268k
  DenormMin = PickFP(Sem, "5.9604644775390625e-8", "1.40129846e-45",
116
268k
                     "4.9406564584124654e-324", "3.64519953188247460253e-4951",
117
268k
                     "4.94065645841246544176568792868221e-324",
118
268k
                     "6.47517511943802511092443895822764655e-4966");
119
268k
  int Digits = PickFP(Sem, 3, 6, 15, 18, 31, 33);
120
268k
  int DecimalDigits = PickFP(Sem, 5, 9, 17, 21, 33, 36);
121
268k
  Epsilon = PickFP(Sem, "9.765625e-4", "1.19209290e-7",
122
268k
                   "2.2204460492503131e-16", "1.08420217248550443401e-19",
123
268k
                   "4.94065645841246544176568792868221e-324",
124
268k
                   "1.92592994438723585305597794258492732e-34");
125
268k
  int MantissaDigits = PickFP(Sem, 11, 24, 53, 64, 106, 113);
126
268k
  int Min10Exp = PickFP(Sem, -4, -37, -307, -4931, -291, -4931);
127
268k
  int Max10Exp = PickFP(Sem, 4, 38, 308, 4932, 308, 4932);
128
268k
  int MinExp = PickFP(Sem, -13, -125, -1021, -16381, -968, -16381);
129
268k
  int MaxExp = PickFP(Sem, 16, 128, 1024, 16384, 1024, 16384);
130
268k
  Min = PickFP(Sem, "6.103515625e-5", "1.17549435e-38", "2.2250738585072014e-308",
131
268k
               "3.36210314311209350626e-4932",
132
268k
               "2.00416836000897277799610805135016e-292",
133
268k
               "3.36210314311209350626267781732175260e-4932");
134
268k
  Max = PickFP(Sem, "6.5504e+4", "3.40282347e+38", "1.7976931348623157e+308",
135
268k
               "1.18973149535723176502e+4932",
136
268k
               "1.79769313486231580793728971405301e+308",
137
268k
               "1.18973149535723176508575932662800702e+4932");
138
139
268k
  SmallString<32> DefPrefix;
140
268k
  DefPrefix = "__";
141
268k
  DefPrefix += Prefix;
142
268k
  DefPrefix += "_";
143
144
268k
  Builder.defineMacro(DefPrefix + "DENORM_MIN__", Twine(DenormMin)+Ext);
145
268k
  Builder.defineMacro(DefPrefix + "HAS_DENORM__");
146
268k
  Builder.defineMacro(DefPrefix + "DIG__", Twine(Digits));
147
268k
  Builder.defineMacro(DefPrefix + "DECIMAL_DIG__", Twine(DecimalDigits));
148
268k
  Builder.defineMacro(DefPrefix + "EPSILON__", Twine(Epsilon)+Ext);
149
268k
  Builder.defineMacro(DefPrefix + "HAS_INFINITY__");
150
268k
  Builder.defineMacro(DefPrefix + "HAS_QUIET_NAN__");
151
268k
  Builder.defineMacro(DefPrefix + "MANT_DIG__", Twine(MantissaDigits));
152
153
268k
  Builder.defineMacro(DefPrefix + "MAX_10_EXP__", Twine(Max10Exp));
154
268k
  Builder.defineMacro(DefPrefix + "MAX_EXP__", Twine(MaxExp));
155
268k
  Builder.defineMacro(DefPrefix + "MAX__", Twine(Max)+Ext);
156
157
268k
  Builder.defineMacro(DefPrefix + "MIN_10_EXP__","("+Twine(Min10Exp)+")");
158
268k
  Builder.defineMacro(DefPrefix + "MIN_EXP__", "("+Twine(MinExp)+")");
159
268k
  Builder.defineMacro(DefPrefix + "MIN__", Twine(Min)+Ext);
160
268k
}
161
162
163
/// DefineTypeSize - Emit a macro to the predefines buffer that declares a macro
164
/// named MacroName with the max value for a type with width 'TypeWidth' a
165
/// signedness of 'isSigned' and with a value suffix of 'ValSuffix' (e.g. LL).
166
static void DefineTypeSize(const Twine &MacroName, unsigned TypeWidth,
167
                           StringRef ValSuffix, bool isSigned,
168
3.33M
                           MacroBuilder &Builder) {
169
3.33M
  llvm::APInt MaxVal = isSigned ? 
llvm::APInt::getSignedMaxValue(TypeWidth)1.98M
170
3.33M
                                : 
llvm::APInt::getMaxValue(TypeWidth)1.34M
;
171
3.33M
  Builder.defineMacro(MacroName, toString(MaxVal, 10, isSigned) + ValSuffix);
172
3.33M
}
173
174
/// DefineTypeSize - An overloaded helper that uses TargetInfo to determine
175
/// the width, suffix, and signedness of the given type
176
static void DefineTypeSize(const Twine &MacroName, TargetInfo::IntType Ty,
177
3.33M
                           const TargetInfo &TI, MacroBuilder &Builder) {
178
3.33M
  DefineTypeSize(MacroName, TI.getTypeWidth(Ty), TI.getTypeConstantSuffix(Ty),
179
3.33M
                 TI.isTypeSigned(Ty), Builder);
180
3.33M
}
181
182
static void DefineFmt(const Twine &Prefix, TargetInfo::IntType Ty,
183
2.63M
                      const TargetInfo &TI, MacroBuilder &Builder) {
184
2.63M
  bool IsSigned = TI.isTypeSigned(Ty);
185
2.63M
  StringRef FmtModifier = TI.getTypeFormatModifier(Ty);
186
10.5M
  for (const char *Fmt = 
IsSigned2.63M
?
"di"1.31M
:
"ouxX"1.31M
; *Fmt;
++Fmt7.90M
) {
187
7.90M
    Builder.defineMacro(Prefix + "_FMT" + Twine(*Fmt) + "__",
188
7.90M
                        Twine("\"") + FmtModifier + Twine(*Fmt) + "\"");
189
7.90M
  }
190
2.63M
}
191
192
static void DefineType(const Twine &MacroName, TargetInfo::IntType Ty,
193
2.98M
                       MacroBuilder &Builder) {
194
2.98M
  Builder.defineMacro(MacroName, TargetInfo::getTypeName(Ty));
195
2.98M
}
196
197
static void DefineTypeWidth(const Twine &MacroName, TargetInfo::IntType Ty,
198
1.49M
                            const TargetInfo &TI, MacroBuilder &Builder) {
199
1.49M
  Builder.defineMacro(MacroName, Twine(TI.getTypeWidth(Ty)));
200
1.49M
}
201
202
static void DefineTypeSizeof(StringRef MacroName, unsigned BitWidth,
203
1.12M
                             const TargetInfo &TI, MacroBuilder &Builder) {
204
1.12M
  Builder.defineMacro(MacroName,
205
1.12M
                      Twine(BitWidth / TI.getCharWidth()));
206
1.12M
}
207
208
// This will generate a macro based on the prefix with `_MAX__` as the suffix
209
// for the max value representable for the type, and a macro with a `_WIDTH__`
210
// suffix for the width of the type.
211
static void DefineTypeSizeAndWidth(const Twine &Prefix, TargetInfo::IntType Ty,
212
                                   const TargetInfo &TI,
213
1.49M
                                   MacroBuilder &Builder) {
214
1.49M
  DefineTypeSize(Prefix + "_MAX__", Ty, TI, Builder);
215
1.49M
  DefineTypeWidth(Prefix + "_WIDTH__", Ty, TI, Builder);
216
1.49M
}
217
218
static void DefineExactWidthIntType(TargetInfo::IntType Ty,
219
                                    const TargetInfo &TI,
220
703k
                                    MacroBuilder &Builder) {
221
703k
  int TypeWidth = TI.getTypeWidth(Ty);
222
703k
  bool IsSigned = TI.isTypeSigned(Ty);
223
224
  // Use the target specified int64 type, when appropriate, so that [u]int64_t
225
  // ends up being defined in terms of the correct type.
226
703k
  if (TypeWidth == 64)
227
175k
    Ty = IsSigned ? 
TI.getInt64Type()87.7k
:
TI.getUInt64Type()87.7k
;
228
229
  // Use the target specified int16 type when appropriate. Some MCU targets
230
  // (such as AVR) have definition of [u]int16_t to [un]signed int.
231
703k
  if (TypeWidth == 16)
232
175k
    Ty = IsSigned ? 
TI.getInt16Type()87.7k
:
TI.getUInt16Type()87.7k
;
233
234
703k
  const char *Prefix = IsSigned ? 
"__INT"351k
:
"__UINT"351k
;
235
236
703k
  DefineType(Prefix + Twine(TypeWidth) + "_TYPE__", Ty, Builder);
237
703k
  DefineFmt(Prefix + Twine(TypeWidth), Ty, TI, Builder);
238
239
703k
  StringRef ConstSuffix(TI.getTypeConstantSuffix(Ty));
240
703k
  Builder.defineMacro(Prefix + Twine(TypeWidth) + "_C_SUFFIX__", ConstSuffix);
241
703k
}
242
243
static void DefineExactWidthIntTypeSize(TargetInfo::IntType Ty,
244
                                        const TargetInfo &TI,
245
703k
                                        MacroBuilder &Builder) {
246
703k
  int TypeWidth = TI.getTypeWidth(Ty);
247
703k
  bool IsSigned = TI.isTypeSigned(Ty);
248
249
  // Use the target specified int64 type, when appropriate, so that [u]int64_t
250
  // ends up being defined in terms of the correct type.
251
703k
  if (TypeWidth == 64)
252
175k
    Ty = IsSigned ? 
TI.getInt64Type()87.7k
:
TI.getUInt64Type()87.7k
;
253
254
  // We don't need to define a _WIDTH macro for the exact-width types because
255
  // we already know the width.
256
703k
  const char *Prefix = IsSigned ? 
"__INT"351k
:
"__UINT"351k
;
257
703k
  DefineTypeSize(Prefix + Twine(TypeWidth) + "_MAX__", Ty, TI, Builder);
258
703k
}
259
260
static void DefineLeastWidthIntType(unsigned TypeWidth, bool IsSigned,
261
                                    const TargetInfo &TI,
262
702k
                                    MacroBuilder &Builder) {
263
702k
  TargetInfo::IntType Ty = TI.getLeastIntTypeByWidth(TypeWidth, IsSigned);
264
702k
  if (Ty == TargetInfo::NoInt)
265
14
    return;
266
267
702k
  const char *Prefix = IsSigned ? 
"__INT_LEAST"351k
:
"__UINT_LEAST"351k
;
268
702k
  DefineType(Prefix + Twine(TypeWidth) + "_TYPE__", Ty, Builder);
269
  // We only want the *_WIDTH macro for the signed types to avoid too many
270
  // predefined macros (the unsigned width and the signed width are identical.)
271
702k
  if (IsSigned)
272
351k
    DefineTypeSizeAndWidth(Prefix + Twine(TypeWidth), Ty, TI, Builder);
273
351k
  else
274
351k
    DefineTypeSize(Prefix + Twine(TypeWidth) + "_MAX__", Ty, TI, Builder);
275
702k
  DefineFmt(Prefix + Twine(TypeWidth), Ty, TI, Builder);
276
702k
}
277
278
static void DefineFastIntType(unsigned TypeWidth, bool IsSigned,
279
702k
                              const TargetInfo &TI, MacroBuilder &Builder) {
280
  // stdint.h currently defines the fast int types as equivalent to the least
281
  // types.
282
702k
  TargetInfo::IntType Ty = TI.getLeastIntTypeByWidth(TypeWidth, IsSigned);
283
702k
  if (Ty == TargetInfo::NoInt)
284
14
    return;
285
286
702k
  const char *Prefix = IsSigned ? 
"__INT_FAST"351k
:
"__UINT_FAST"351k
;
287
702k
  DefineType(Prefix + Twine(TypeWidth) + "_TYPE__", Ty, Builder);
288
  // We only want the *_WIDTH macro for the signed types to avoid too many
289
  // predefined macros (the unsigned width and the signed width are identical.)
290
702k
  if (IsSigned)
291
351k
    DefineTypeSizeAndWidth(Prefix + Twine(TypeWidth), Ty, TI, Builder);
292
351k
  else
293
351k
    DefineTypeSize(Prefix + Twine(TypeWidth) + "_MAX__", Ty, TI, Builder);
294
702k
  DefineFmt(Prefix + Twine(TypeWidth), Ty, TI, Builder);
295
702k
}
296
297
298
/// Get the value the ATOMIC_*_LOCK_FREE macro should have for a type with
299
/// the specified properties.
300
static const char *getLockFreeValue(unsigned TypeWidth, unsigned TypeAlign,
301
1.15M
                                    unsigned InlineWidth) {
302
  // Fully-aligned, power-of-2 sizes no larger than the inline
303
  // width will be inlined as lock-free operations.
304
1.15M
  if (TypeWidth == TypeAlign && 
(TypeWidth & (TypeWidth - 1)) == 01.13M
&&
305
1.15M
      
TypeWidth <= InlineWidth1.13M
)
306
1.12M
    return "2"; // "always lock free"
307
  // We cannot be certain what operations the lib calls might be
308
  // able to implement as lock-free on future processors.
309
36.5k
  return "1"; // "sometimes lock free"
310
1.15M
}
311
312
/// Add definitions required for a smooth interaction between
313
/// Objective-C++ automated reference counting and libstdc++ (4.2).
314
static void AddObjCXXARCLibstdcxxDefines(const LangOptions &LangOpts,
315
1
                                         MacroBuilder &Builder) {
316
1
  Builder.defineMacro("_GLIBCXX_PREDEFINED_OBJC_ARC_IS_SCALAR");
317
318
1
  std::string Result;
319
1
  {
320
    // Provide specializations for the __is_scalar type trait so that
321
    // lifetime-qualified objects are not considered "scalar" types, which
322
    // libstdc++ uses as an indicator of the presence of trivial copy, assign,
323
    // default-construct, and destruct semantics (none of which hold for
324
    // lifetime-qualified objects in ARC).
325
1
    llvm::raw_string_ostream Out(Result);
326
327
1
    Out << "namespace std {\n"
328
1
        << "\n"
329
1
        << "struct __true_type;\n"
330
1
        << "struct __false_type;\n"
331
1
        << "\n";
332
333
1
    Out << "template<typename _Tp> struct __is_scalar;\n"
334
1
        << "\n";
335
336
1
    if (LangOpts.ObjCAutoRefCount) {
337
1
      Out << "template<typename _Tp>\n"
338
1
          << "struct __is_scalar<__attribute__((objc_ownership(strong))) _Tp> {\n"
339
1
          << "  enum { __value = 0 };\n"
340
1
          << "  typedef __false_type __type;\n"
341
1
          << "};\n"
342
1
          << "\n";
343
1
    }
344
345
1
    if (LangOpts.ObjCWeak) {
346
1
      Out << "template<typename _Tp>\n"
347
1
          << "struct __is_scalar<__attribute__((objc_ownership(weak))) _Tp> {\n"
348
1
          << "  enum { __value = 0 };\n"
349
1
          << "  typedef __false_type __type;\n"
350
1
          << "};\n"
351
1
          << "\n";
352
1
    }
353
354
1
    if (LangOpts.ObjCAutoRefCount) {
355
1
      Out << "template<typename _Tp>\n"
356
1
          << "struct __is_scalar<__attribute__((objc_ownership(autoreleasing)))"
357
1
          << " _Tp> {\n"
358
1
          << "  enum { __value = 0 };\n"
359
1
          << "  typedef __false_type __type;\n"
360
1
          << "};\n"
361
1
          << "\n";
362
1
    }
363
364
1
    Out << "}\n";
365
1
  }
366
1
  Builder.append(Result);
367
1
}
368
369
static void InitializeStandardPredefinedMacros(const TargetInfo &TI,
370
                                               const LangOptions &LangOpts,
371
                                               const FrontendOptions &FEOpts,
372
87.6k
                                               MacroBuilder &Builder) {
373
  // C++ [cpp.predefined]p1:
374
  //   The following macro names shall be defined by the implementation:
375
376
  //   -- __STDC__
377
  //      [C++] Whether __STDC__ is predefined and if so, what its value is,
378
  //      are implementation-defined.
379
  // (Removed in C++20.)
380
87.6k
  if (!LangOpts.MSVCCompat && 
!LangOpts.TraditionalCPP77.1k
)
381
77.1k
    Builder.defineMacro("__STDC__");
382
  //   -- __STDC_HOSTED__
383
  //      The integer literal 1 if the implementation is a hosted
384
  //      implementation or the integer literal 0 if it is not.
385
87.6k
  if (LangOpts.Freestanding)
386
827
    Builder.defineMacro("__STDC_HOSTED__", "0");
387
86.8k
  else
388
86.8k
    Builder.defineMacro("__STDC_HOSTED__");
389
390
  //   -- __STDC_VERSION__
391
  //      [C++] Whether __STDC_VERSION__ is predefined and if so, what its
392
  //      value is, are implementation-defined.
393
  // (Removed in C++20.)
394
87.6k
  if (!LangOpts.CPlusPlus) {
395
    // FIXME: Use correct value for C23.
396
20.4k
    if (LangOpts.C2x)
397
42
      Builder.defineMacro("__STDC_VERSION__", "202000L");
398
20.3k
    else if (LangOpts.C17)
399
11.8k
      Builder.defineMacro("__STDC_VERSION__", "201710L");
400
8.53k
    else if (LangOpts.C11)
401
5.58k
      Builder.defineMacro("__STDC_VERSION__", "201112L");
402
2.94k
    else if (LangOpts.C99)
403
1.96k
      Builder.defineMacro("__STDC_VERSION__", "199901L");
404
975
    else if (!LangOpts.GNUMode && 
LangOpts.Digraphs922
)
405
5
      Builder.defineMacro("__STDC_VERSION__", "199409L");
406
67.2k
  } else {
407
    //   -- __cplusplus
408
    // FIXME: Use correct value for C++23.
409
67.2k
    if (LangOpts.CPlusPlus2b)
410
44
      Builder.defineMacro("__cplusplus", "202101L");
411
    //      [C++20] The integer literal 202002L.
412
67.1k
    else if (LangOpts.CPlusPlus20)
413
3.57k
      Builder.defineMacro("__cplusplus", "202002L");
414
    //      [C++17] The integer literal 201703L.
415
63.6k
    else if (LangOpts.CPlusPlus17)
416
3.94k
      Builder.defineMacro("__cplusplus", "201703L");
417
    //      [C++14] The name __cplusplus is defined to the value 201402L when
418
    //      compiling a C++ translation unit.
419
59.6k
    else if (LangOpts.CPlusPlus14)
420
20.7k
      Builder.defineMacro("__cplusplus", "201402L");
421
    //      [C++11] The name __cplusplus is defined to the value 201103L when
422
    //      compiling a C++ translation unit.
423
38.9k
    else if (LangOpts.CPlusPlus11)
424
31.9k
      Builder.defineMacro("__cplusplus", "201103L");
425
    //      [C++03] The name __cplusplus is defined to the value 199711L when
426
    //      compiling a C++ translation unit.
427
7.03k
    else
428
7.03k
      Builder.defineMacro("__cplusplus", "199711L");
429
430
    //   -- __STDCPP_DEFAULT_NEW_ALIGNMENT__
431
    //      [C++17] An integer literal of type std::size_t whose value is the
432
    //      alignment guaranteed by a call to operator new(std::size_t)
433
    //
434
    // We provide this in all language modes, since it seems generally useful.
435
67.2k
    Builder.defineMacro("__STDCPP_DEFAULT_NEW_ALIGNMENT__",
436
67.2k
                        Twine(TI.getNewAlign() / TI.getCharWidth()) +
437
67.2k
                            TI.getTypeConstantSuffix(TI.getSizeType()));
438
439
    //   -- __STDCPP_­THREADS__
440
    //      Defined, and has the value integer literal 1, if and only if a
441
    //      program can have more than one thread of execution.
442
67.2k
    if (LangOpts.getThreadModel() == LangOptions::ThreadModelKind::POSIX)
443
67.2k
      Builder.defineMacro("__STDCPP_THREADS__", "1");
444
67.2k
  }
445
446
  // In C11 these are environment macros. In C++11 they are only defined
447
  // as part of <cuchar>. To prevent breakage when mixing C and C++
448
  // code, define these macros unconditionally. We can define them
449
  // unconditionally, as Clang always uses UTF-16 and UTF-32 for 16-bit
450
  // and 32-bit character literals.
451
87.6k
  Builder.defineMacro("__STDC_UTF_16__", "1");
452
87.6k
  Builder.defineMacro("__STDC_UTF_32__", "1");
453
454
87.6k
  if (LangOpts.ObjC)
455
22.6k
    Builder.defineMacro("__OBJC__");
456
457
  // OpenCL v1.0/1.1 s6.9, v1.2/2.0 s6.10: Preprocessor Directives and Macros.
458
87.6k
  if (LangOpts.OpenCL) {
459
643
    if (LangOpts.CPlusPlus) {
460
149
      switch (LangOpts.OpenCLCPlusPlusVersion) {
461
104
      case 100:
462
104
        Builder.defineMacro("__OPENCL_CPP_VERSION__", "100");
463
104
        break;
464
45
      case 202100:
465
45
        Builder.defineMacro("__OPENCL_CPP_VERSION__", "202100");
466
45
        break;
467
0
      default:
468
0
        llvm_unreachable("Unsupported C++ version for OpenCL");
469
149
      }
470
149
      Builder.defineMacro("__CL_CPP_VERSION_1_0__", "100");
471
149
      Builder.defineMacro("__CL_CPP_VERSION_2021__", "202100");
472
494
    } else {
473
      // OpenCL v1.0 and v1.1 do not have a predefined macro to indicate the
474
      // language standard with which the program is compiled. __OPENCL_VERSION__
475
      // is for the OpenCL version supported by the OpenCL device, which is not
476
      // necessarily the language standard with which the program is compiled.
477
      // A shared OpenCL header file requires a macro to indicate the language
478
      // standard. As a workaround, __OPENCL_C_VERSION__ is defined for
479
      // OpenCL v1.0 and v1.1.
480
494
      switch (LangOpts.OpenCLVersion) {
481
26
      case 100:
482
26
        Builder.defineMacro("__OPENCL_C_VERSION__", "100");
483
26
        break;
484
16
      case 110:
485
16
        Builder.defineMacro("__OPENCL_C_VERSION__", "110");
486
16
        break;
487
237
      case 120:
488
237
        Builder.defineMacro("__OPENCL_C_VERSION__", "120");
489
237
        break;
490
149
      case 200:
491
149
        Builder.defineMacro("__OPENCL_C_VERSION__", "200");
492
149
        break;
493
66
      case 300:
494
66
        Builder.defineMacro("__OPENCL_C_VERSION__", "300");
495
66
        break;
496
0
      default:
497
0
        llvm_unreachable("Unsupported OpenCL version");
498
494
      }
499
494
    }
500
643
    Builder.defineMacro("CL_VERSION_1_0", "100");
501
643
    Builder.defineMacro("CL_VERSION_1_1", "110");
502
643
    Builder.defineMacro("CL_VERSION_1_2", "120");
503
643
    Builder.defineMacro("CL_VERSION_2_0", "200");
504
643
    Builder.defineMacro("CL_VERSION_3_0", "300");
505
506
643
    if (TI.isLittleEndian())
507
643
      Builder.defineMacro("__ENDIAN_LITTLE__");
508
509
643
    if (LangOpts.FastRelaxedMath)
510
3
      Builder.defineMacro("__FAST_RELAXED_MATH__");
511
643
  }
512
513
87.6k
  if (LangOpts.SYCLIsDevice || 
LangOpts.SYCLIsHost87.6k
) {
514
    // SYCL Version is set to a value when building SYCL applications
515
37
    if (LangOpts.getSYCLVersion() == LangOptions::SYCL_2017)
516
3
      Builder.defineMacro("CL_SYCL_LANGUAGE_VERSION", "121");
517
34
    else if (LangOpts.getSYCLVersion() == LangOptions::SYCL_2020)
518
34
      Builder.defineMacro("SYCL_LANGUAGE_VERSION", "202001");
519
37
  }
520
521
  // Not "standard" per se, but available even with the -undef flag.
522
87.6k
  if (LangOpts.AsmPreprocessor)
523
41
    Builder.defineMacro("__ASSEMBLER__");
524
87.6k
  if (LangOpts.CUDA) {
525
308
    if (LangOpts.GPURelocatableDeviceCode)
526
15
      Builder.defineMacro("__CLANG_RDC__");
527
308
    if (!LangOpts.HIP)
528
229
      Builder.defineMacro("__CUDA__");
529
308
  }
530
87.6k
  if (LangOpts.HIP) {
531
79
    Builder.defineMacro("__HIP__");
532
79
    Builder.defineMacro("__HIPCC__");
533
79
    Builder.defineMacro("__HIP_MEMORY_SCOPE_SINGLETHREAD", "1");
534
79
    Builder.defineMacro("__HIP_MEMORY_SCOPE_WAVEFRONT", "2");
535
79
    Builder.defineMacro("__HIP_MEMORY_SCOPE_WORKGROUP", "3");
536
79
    Builder.defineMacro("__HIP_MEMORY_SCOPE_AGENT", "4");
537
79
    Builder.defineMacro("__HIP_MEMORY_SCOPE_SYSTEM", "5");
538
79
    if (LangOpts.CUDAIsDevice)
539
40
      Builder.defineMacro("__HIP_DEVICE_COMPILE__");
540
79
  }
541
87.6k
}
542
543
/// Initialize the predefined C++ language feature test macros defined in
544
/// ISO/IEC JTC1/SC22/WG21 (C++) SD-6: "SG10 Feature Test Recommendations".
545
static void InitializeCPlusPlusFeatureTestMacros(const LangOptions &LangOpts,
546
67.3k
                                                 MacroBuilder &Builder) {
547
  // C++98 features.
548
67.3k
  if (LangOpts.RTTI)
549
67.2k
    Builder.defineMacro("__cpp_rtti", "199711L");
550
67.3k
  if (LangOpts.CXXExceptions)
551
30.1k
    Builder.defineMacro("__cpp_exceptions", "199711L");
552
553
  // C++11 features.
554
67.3k
  if (LangOpts.CPlusPlus11) {
555
60.3k
    Builder.defineMacro("__cpp_unicode_characters", "200704L");
556
60.3k
    Builder.defineMacro("__cpp_raw_strings", "200710L");
557
60.3k
    Builder.defineMacro("__cpp_unicode_literals", "200710L");
558
60.3k
    Builder.defineMacro("__cpp_user_defined_literals", "200809L");
559
60.3k
    Builder.defineMacro("__cpp_lambdas", "200907L");
560
60.3k
    Builder.defineMacro("__cpp_constexpr",
561
60.3k
                        LangOpts.CPlusPlus20 ? 
"201907L"3.61k
:
562
60.3k
                        
LangOpts.CPlusPlus1756.6k
?
"201603L"3.94k
:
563
56.6k
                        
LangOpts.CPlusPlus1452.7k
?
"201304L"20.8k
:
"200704"31.9k
);
564
60.3k
    Builder.defineMacro("__cpp_constexpr_in_decltype", "201711L");
565
60.3k
    Builder.defineMacro("__cpp_range_based_for",
566
60.3k
                        LangOpts.CPlusPlus17 ? 
"201603L"7.55k
:
"200907"52.7k
);
567
60.3k
    Builder.defineMacro("__cpp_static_assert",
568
60.3k
                        LangOpts.CPlusPlus17 ? 
"201411L"7.55k
:
"200410"52.7k
);
569
60.3k
    Builder.defineMacro("__cpp_decltype", "200707L");
570
60.3k
    Builder.defineMacro("__cpp_attributes", "200809L");
571
60.3k
    Builder.defineMacro("__cpp_rvalue_references", "200610L");
572
60.3k
    Builder.defineMacro("__cpp_variadic_templates", "200704L");
573
60.3k
    Builder.defineMacro("__cpp_initializer_lists", "200806L");
574
60.3k
    Builder.defineMacro("__cpp_delegating_constructors", "200604L");
575
60.3k
    Builder.defineMacro("__cpp_nsdmi", "200809L");
576
60.3k
    Builder.defineMacro("__cpp_inheriting_constructors", "201511L");
577
60.3k
    Builder.defineMacro("__cpp_ref_qualifiers", "200710L");
578
60.3k
    Builder.defineMacro("__cpp_alias_templates", "200704L");
579
60.3k
  }
580
67.3k
  if (LangOpts.ThreadsafeStatics)
581
51.8k
    Builder.defineMacro("__cpp_threadsafe_static_init", "200806L");
582
583
  // C++14 features.
584
67.3k
  if (LangOpts.CPlusPlus14) {
585
28.3k
    Builder.defineMacro("__cpp_binary_literals", "201304L");
586
28.3k
    Builder.defineMacro("__cpp_digit_separators", "201309L");
587
28.3k
    Builder.defineMacro("__cpp_init_captures",
588
28.3k
                        LangOpts.CPlusPlus20 ? 
"201803L"3.61k
:
"201304L"24.7k
);
589
28.3k
    Builder.defineMacro("__cpp_generic_lambdas",
590
28.3k
                        LangOpts.CPlusPlus20 ? 
"201707L"3.61k
:
"201304L"24.7k
);
591
28.3k
    Builder.defineMacro("__cpp_decltype_auto", "201304L");
592
28.3k
    Builder.defineMacro("__cpp_return_type_deduction", "201304L");
593
28.3k
    Builder.defineMacro("__cpp_aggregate_nsdmi", "201304L");
594
28.3k
    Builder.defineMacro("__cpp_variable_templates", "201304L");
595
28.3k
  }
596
67.3k
  if (LangOpts.SizedDeallocation)
597
39
    Builder.defineMacro("__cpp_sized_deallocation", "201309L");
598
599
  // C++17 features.
600
67.3k
  if (LangOpts.CPlusPlus17) {
601
7.55k
    Builder.defineMacro("__cpp_hex_float", "201603L");
602
7.55k
    Builder.defineMacro("__cpp_inline_variables", "201606L");
603
7.55k
    Builder.defineMacro("__cpp_noexcept_function_type", "201510L");
604
7.55k
    Builder.defineMacro("__cpp_capture_star_this", "201603L");
605
7.55k
    Builder.defineMacro("__cpp_if_constexpr", "201606L");
606
7.55k
    Builder.defineMacro("__cpp_deduction_guides", "201703L"); // (not latest)
607
7.55k
    Builder.defineMacro("__cpp_template_auto", "201606L"); // (old name)
608
7.55k
    Builder.defineMacro("__cpp_namespace_attributes", "201411L");
609
7.55k
    Builder.defineMacro("__cpp_enumerator_attributes", "201411L");
610
7.55k
    Builder.defineMacro("__cpp_nested_namespace_definitions", "201411L");
611
7.55k
    Builder.defineMacro("__cpp_variadic_using", "201611L");
612
7.55k
    Builder.defineMacro("__cpp_aggregate_bases", "201603L");
613
7.55k
    Builder.defineMacro("__cpp_structured_bindings", "201606L");
614
7.55k
    Builder.defineMacro("__cpp_nontype_template_args",
615
7.55k
                        "201411L"); // (not latest)
616
7.55k
    Builder.defineMacro("__cpp_fold_expressions", "201603L");
617
7.55k
    Builder.defineMacro("__cpp_guaranteed_copy_elision", "201606L");
618
7.55k
    Builder.defineMacro("__cpp_nontype_template_parameter_auto", "201606L");
619
7.55k
  }
620
67.3k
  if (LangOpts.AlignedAllocation && 
!LangOpts.AlignedAllocationUnavailable7.56k
)
621
7.55k
    Builder.defineMacro("__cpp_aligned_new", "201606L");
622
67.3k
  if (LangOpts.RelaxedTemplateTemplateArgs)
623
3
    Builder.defineMacro("__cpp_template_template_args", "201611L");
624
625
  // C++20 features.
626
67.3k
  if (LangOpts.CPlusPlus20) {
627
    //Builder.defineMacro("__cpp_aggregate_paren_init", "201902L");
628
3.61k
    Builder.defineMacro("__cpp_concepts", "201907L");
629
3.61k
    Builder.defineMacro("__cpp_conditional_explicit", "201806L");
630
    //Builder.defineMacro("__cpp_consteval", "201811L");
631
3.61k
    Builder.defineMacro("__cpp_constexpr_dynamic_alloc", "201907L");
632
3.61k
    Builder.defineMacro("__cpp_constinit", "201907L");
633
3.61k
    Builder.defineMacro("__cpp_impl_coroutine", "201902L");
634
3.61k
    Builder.defineMacro("__cpp_designated_initializers", "201707L");
635
3.61k
    Builder.defineMacro("__cpp_impl_three_way_comparison", "201907L");
636
    //Builder.defineMacro("__cpp_modules", "201907L");
637
3.61k
    Builder.defineMacro("__cpp_using_enum", "201907L");
638
3.61k
  }
639
  // C++2b features.
640
67.3k
  if (LangOpts.CPlusPlus2b) {
641
44
    Builder.defineMacro("__cpp_implicit_move", "202011L");
642
44
    Builder.defineMacro("__cpp_size_t_suffix", "202011L");
643
44
    Builder.defineMacro("__cpp_if_consteval", "202106L");
644
44
  }
645
67.3k
  if (LangOpts.Char8)
646
3.62k
    Builder.defineMacro("__cpp_char8_t", "201811L");
647
67.3k
  Builder.defineMacro("__cpp_impl_destroying_delete", "201806L");
648
649
  // TS features.
650
67.3k
  if (LangOpts.Coroutines)
651
3.67k
    Builder.defineMacro("__cpp_coroutines", "201703L");
652
67.3k
}
653
654
/// InitializeOpenCLFeatureTestMacros - Define OpenCL macros based on target
655
/// settings and language version
656
void InitializeOpenCLFeatureTestMacros(const TargetInfo &TI,
657
                                       const LangOptions &Opts,
658
643
                                       MacroBuilder &Builder) {
659
643
  const llvm::StringMap<bool> &OpenCLFeaturesMap = TI.getSupportedOpenCLOpts();
660
  // FIXME: OpenCL options which affect language semantics/syntax
661
  // should be moved into LangOptions.
662
27.0k
  auto defineOpenCLExtMacro = [&](llvm::StringRef Name, auto... OptArgs) {
663
    // Check if extension is supported by target and is available in this
664
    // OpenCL version
665
27.0k
    if (TI.hasFeatureEnabled(OpenCLFeaturesMap, Name) &&
666
27.0k
        
OpenCLOptions::isOpenCLOptionAvailableIn(Opts, OptArgs...)24.5k
)
667
17.9k
      Builder.defineMacro(Name);
668
27.0k
  };
InitPreprocessor.cpp:auto InitializeOpenCLFeatureTestMacros(clang::TargetInfo const&, clang::LangOptions const&, clang::MacroBuilder&)::$_0::operator()<bool, int, clang::(anonymous namespace)::OpenCLVersionID, unsigned int>(llvm::StringRef, bool, int, clang::(anonymous namespace)::OpenCLVersionID, unsigned int) const
Line
Count
Source
662
3.21k
  auto defineOpenCLExtMacro = [&](llvm::StringRef Name, auto... OptArgs) {
663
    // Check if extension is supported by target and is available in this
664
    // OpenCL version
665
3.21k
    if (TI.hasFeatureEnabled(OpenCLFeaturesMap, Name) &&
666
3.21k
        
OpenCLOptions::isOpenCLOptionAvailableIn(Opts, OptArgs...)3.10k
)
667
3.10k
      Builder.defineMacro(Name);
668
3.21k
  };
InitPreprocessor.cpp:auto InitializeOpenCLFeatureTestMacros(clang::TargetInfo const&, clang::LangOptions const&, clang::MacroBuilder&)::$_0::operator()<bool, int, unsigned int, clang::(anonymous namespace)::OpenCLVersionID>(llvm::StringRef, bool, int, unsigned int, clang::(anonymous namespace)::OpenCLVersionID) const
Line
Count
Source
662
7.71k
  auto defineOpenCLExtMacro = [&](llvm::StringRef Name, auto... OptArgs) {
663
    // Check if extension is supported by target and is available in this
664
    // OpenCL version
665
7.71k
    if (TI.hasFeatureEnabled(OpenCLFeaturesMap, Name) &&
666
7.71k
        
OpenCLOptions::isOpenCLOptionAvailableIn(Opts, OptArgs...)6.75k
)
667
1.56k
      Builder.defineMacro(Name);
668
7.71k
  };
InitPreprocessor.cpp:auto InitializeOpenCLFeatureTestMacros(clang::TargetInfo const&, clang::LangOptions const&, clang::MacroBuilder&)::$_0::operator()<bool, int, unsigned int, unsigned int>(llvm::StringRef, bool, int, unsigned int, unsigned int) const
Line
Count
Source
662
15.4k
  auto defineOpenCLExtMacro = [&](llvm::StringRef Name, auto... OptArgs) {
663
    // Check if extension is supported by target and is available in this
664
    // OpenCL version
665
15.4k
    if (TI.hasFeatureEnabled(OpenCLFeaturesMap, Name) &&
666
15.4k
        
OpenCLOptions::isOpenCLOptionAvailableIn(Opts, OptArgs...)14.0k
)
667
12.6k
      Builder.defineMacro(Name);
668
15.4k
  };
InitPreprocessor.cpp:auto InitializeOpenCLFeatureTestMacros(clang::TargetInfo const&, clang::LangOptions const&, clang::MacroBuilder&)::$_0::operator()<bool, int, clang::(anonymous namespace)::OpenCLVersionID, clang::(anonymous namespace)::OpenCLVersionID>(llvm::StringRef, bool, int, clang::(anonymous namespace)::OpenCLVersionID, clang::(anonymous namespace)::OpenCLVersionID) const
Line
Count
Source
662
643
  auto defineOpenCLExtMacro = [&](llvm::StringRef Name, auto... OptArgs) {
663
    // Check if extension is supported by target and is available in this
664
    // OpenCL version
665
643
    if (TI.hasFeatureEnabled(OpenCLFeaturesMap, Name) &&
666
643
        
OpenCLOptions::isOpenCLOptionAvailableIn(Opts, OptArgs...)610
)
667
610
      Builder.defineMacro(Name);
668
643
  };
669
643
#define OPENCL_GENERIC_EXTENSION(Ext, ...)                                     \
670
27.0k
  defineOpenCLExtMacro(#Ext, __VA_ARGS__);
671
643
#include "clang/Basic/OpenCLExtensions.def"
672
673
  // Assume compiling for FULL profile
674
643
  Builder.defineMacro("__opencl_c_int64");
675
643
}
676
677
static void InitializePredefinedMacros(const TargetInfo &TI,
678
                                       const LangOptions &LangOpts,
679
                                       const FrontendOptions &FEOpts,
680
                                       const PreprocessorOptions &PPOpts,
681
87.7k
                                       MacroBuilder &Builder) {
682
  // Compiler version introspection macros.
683
87.7k
  Builder.defineMacro("__llvm__");  // LLVM Backend
684
87.7k
  Builder.defineMacro("__clang__"); // Clang Frontend
685
263k
#define TOSTR2(X) #X
686
263k
#define TOSTR(X) TOSTR2(X)
687
87.7k
  Builder.defineMacro("__clang_major__", TOSTR(CLANG_VERSION_MAJOR));
688
87.7k
  Builder.defineMacro("__clang_minor__", TOSTR(CLANG_VERSION_MINOR));
689
87.7k
  Builder.defineMacro("__clang_patchlevel__", TOSTR(CLANG_VERSION_PATCHLEVEL));
690
87.7k
#undef TOSTR
691
87.7k
#undef TOSTR2
692
87.7k
  Builder.defineMacro("__clang_version__",
693
87.7k
                      "\"" CLANG_VERSION_STRING " "
694
87.7k
                      + getClangFullRepositoryVersion() + "\"");
695
696
87.7k
  if (LangOpts.GNUCVersion != 0) {
697
    // Major, minor, patch, are given two decimal places each, so 4.2.1 becomes
698
    // 40201.
699
27.3k
    unsigned GNUCMajor = LangOpts.GNUCVersion / 100 / 100;
700
27.3k
    unsigned GNUCMinor = LangOpts.GNUCVersion / 100 % 100;
701
27.3k
    unsigned GNUCPatch = LangOpts.GNUCVersion % 100;
702
27.3k
    Builder.defineMacro("__GNUC__", Twine(GNUCMajor));
703
27.3k
    Builder.defineMacro("__GNUC_MINOR__", Twine(GNUCMinor));
704
27.3k
    Builder.defineMacro("__GNUC_PATCHLEVEL__", Twine(GNUCPatch));
705
27.3k
    Builder.defineMacro("__GXX_ABI_VERSION", "1002");
706
707
27.3k
    if (LangOpts.CPlusPlus) {
708
20.3k
      Builder.defineMacro("__GNUG__", Twine(GNUCMajor));
709
20.3k
      Builder.defineMacro("__GXX_WEAK__");
710
20.3k
    }
711
27.3k
  }
712
713
  // Define macros for the C11 / C++11 memory orderings
714
87.7k
  Builder.defineMacro("__ATOMIC_RELAXED", "0");
715
87.7k
  Builder.defineMacro("__ATOMIC_CONSUME", "1");
716
87.7k
  Builder.defineMacro("__ATOMIC_ACQUIRE", "2");
717
87.7k
  Builder.defineMacro("__ATOMIC_RELEASE", "3");
718
87.7k
  Builder.defineMacro("__ATOMIC_ACQ_REL", "4");
719
87.7k
  Builder.defineMacro("__ATOMIC_SEQ_CST", "5");
720
721
  // Define macros for the OpenCL memory scope.
722
  // The values should match AtomicScopeOpenCLModel::ID enum.
723
87.7k
  static_assert(
724
87.7k
      static_cast<unsigned>(AtomicScopeOpenCLModel::WorkGroup) == 1 &&
725
87.7k
          static_cast<unsigned>(AtomicScopeOpenCLModel::Device) == 2 &&
726
87.7k
          static_cast<unsigned>(AtomicScopeOpenCLModel::AllSVMDevices) == 3 &&
727
87.7k
          static_cast<unsigned>(AtomicScopeOpenCLModel::SubGroup) == 4,
728
87.7k
      "Invalid OpenCL memory scope enum definition");
729
87.7k
  Builder.defineMacro("__OPENCL_MEMORY_SCOPE_WORK_ITEM", "0");
730
87.7k
  Builder.defineMacro("__OPENCL_MEMORY_SCOPE_WORK_GROUP", "1");
731
87.7k
  Builder.defineMacro("__OPENCL_MEMORY_SCOPE_DEVICE", "2");
732
87.7k
  Builder.defineMacro("__OPENCL_MEMORY_SCOPE_ALL_SVM_DEVICES", "3");
733
87.7k
  Builder.defineMacro("__OPENCL_MEMORY_SCOPE_SUB_GROUP", "4");
734
735
  // Support for #pragma redefine_extname (Sun compatibility)
736
87.7k
  Builder.defineMacro("__PRAGMA_REDEFINE_EXTNAME", "1");
737
738
  // Previously this macro was set to a string aiming to achieve compatibility
739
  // with GCC 4.2.1. Now, just return the full Clang version
740
87.7k
  Builder.defineMacro("__VERSION__", "\"" +
741
87.7k
                      Twine(getClangFullCPPVersion()) + "\"");
742
743
  // Initialize language-specific preprocessor defines.
744
745
  // Standard conforming mode?
746
87.7k
  if (!LangOpts.GNUMode && 
!LangOpts.MSVCCompat38.6k
)
747
28.3k
    Builder.defineMacro("__STRICT_ANSI__");
748
749
87.7k
  if (LangOpts.GNUCVersion && 
LangOpts.CPlusPlus1127.3k
)
750
17.2k
    Builder.defineMacro("__GXX_EXPERIMENTAL_CXX0X__");
751
752
87.7k
  if (LangOpts.ObjC) {
753
22.6k
    if (LangOpts.ObjCRuntime.isNonFragile()) {
754
22.3k
      Builder.defineMacro("__OBJC2__");
755
756
22.3k
      if (LangOpts.ObjCExceptions)
757
4.36k
        Builder.defineMacro("OBJC_ZEROCOST_EXCEPTIONS");
758
22.3k
    }
759
760
22.6k
    if (LangOpts.getGC() != LangOptions::NonGC)
761
92
      Builder.defineMacro("__OBJC_GC__");
762
763
22.6k
    if (LangOpts.ObjCRuntime.isNeXTFamily())
764
21.7k
      Builder.defineMacro("__NEXT_RUNTIME__");
765
766
22.6k
    if (LangOpts.ObjCRuntime.getKind() == ObjCRuntime::GNUstep) {
767
803
      auto version = LangOpts.ObjCRuntime.getVersion();
768
803
      std::string versionString = "1";
769
      // Don't rely on the tuple argument, because we can be asked to target
770
      // later ABIs than we actually support, so clamp these values to those
771
      // currently supported
772
803
      if (version >= VersionTuple(2, 0))
773
14
        Builder.defineMacro("__OBJC_GNUSTEP_RUNTIME_ABI__", "20");
774
789
      else
775
789
        Builder.defineMacro("__OBJC_GNUSTEP_RUNTIME_ABI__",
776
789
            "1" + Twine(std::min(8U, version.getMinor().getValueOr(0))));
777
803
    }
778
779
22.6k
    if (LangOpts.ObjCRuntime.getKind() == ObjCRuntime::ObjFW) {
780
21
      VersionTuple tuple = LangOpts.ObjCRuntime.getVersion();
781
782
21
      unsigned minor = 0;
783
21
      if (tuple.getMinor().hasValue())
784
21
        minor = tuple.getMinor().getValue();
785
786
21
      unsigned subminor = 0;
787
21
      if (tuple.getSubminor().hasValue())
788
0
        subminor = tuple.getSubminor().getValue();
789
790
21
      Builder.defineMacro("__OBJFW_RUNTIME_ABI__",
791
21
                          Twine(tuple.getMajor() * 10000 + minor * 100 +
792
21
                                subminor));
793
21
    }
794
795
22.6k
    Builder.defineMacro("IBOutlet", "__attribute__((iboutlet))");
796
22.6k
    Builder.defineMacro("IBOutletCollection(ClassName)",
797
22.6k
                        "__attribute__((iboutletcollection(ClassName)))");
798
22.6k
    Builder.defineMacro("IBAction", "void)__attribute__((ibaction)");
799
22.6k
    Builder.defineMacro("IBInspectable", "");
800
22.6k
    Builder.defineMacro("IB_DESIGNABLE", "");
801
22.6k
  }
802
803
  // Define a macro that describes the Objective-C boolean type even for C
804
  // and C++ since BOOL can be used from non Objective-C code.
805
87.7k
  Builder.defineMacro("__OBJC_BOOL_IS_BOOL",
806
87.7k
                      Twine(TI.useSignedCharForObjCBool() ? 
"0"87.2k
:
"1"465
));
807
808
87.7k
  if (LangOpts.CPlusPlus)
809
67.3k
    InitializeCPlusPlusFeatureTestMacros(LangOpts, Builder);
810
811
  // darwin_constant_cfstrings controls this. This is also dependent
812
  // on other things like the runtime I believe.  This is set even for C code.
813
87.7k
  if (!LangOpts.NoConstantCFStrings)
814
87.7k
      Builder.defineMacro("__CONSTANT_CFSTRINGS__");
815
816
87.7k
  if (LangOpts.ObjC)
817
22.6k
    Builder.defineMacro("OBJC_NEW_PROPERTIES");
818
819
87.7k
  if (LangOpts.PascalStrings)
820
8
    Builder.defineMacro("__PASCAL_STRINGS__");
821
822
87.7k
  if (LangOpts.Blocks) {
823
34.3k
    Builder.defineMacro("__block", "__attribute__((__blocks__(byref)))");
824
34.3k
    Builder.defineMacro("__BLOCKS__");
825
34.3k
  }
826
827
87.7k
  if (!LangOpts.MSVCCompat && 
LangOpts.Exceptions77.2k
)
828
24.2k
    Builder.defineMacro("__EXCEPTIONS");
829
87.7k
  if (LangOpts.GNUCVersion && 
LangOpts.RTTI27.3k
)
830
20.3k
    Builder.defineMacro("__GXX_RTTI");
831
832
87.7k
  if (LangOpts.hasSjLjExceptions())
833
34
    Builder.defineMacro("__USING_SJLJ_EXCEPTIONS__");
834
87.7k
  else if (LangOpts.hasSEHExceptions())
835
25
    Builder.defineMacro("__SEH__");
836
87.7k
  else if (LangOpts.hasDWARFExceptions() &&
837
87.7k
           
(32
TI.getTriple().isThumb()32
||
TI.getTriple().isARM()25
))
838
12
    Builder.defineMacro("__ARM_DWARF_EH__");
839
840
87.7k
  if (LangOpts.Deprecated)
841
28.7k
    Builder.defineMacro("__DEPRECATED");
842
843
87.7k
  if (!LangOpts.MSVCCompat && 
LangOpts.CPlusPlus77.2k
)
844
57.9k
    Builder.defineMacro("__private_extern__", "extern");
845
846
87.7k
  if (LangOpts.MicrosoftExt) {
847
11.1k
    if (LangOpts.WChar) {
848
      // wchar_t supported as a keyword.
849
9.83k
      Builder.defineMacro("_WCHAR_T_DEFINED");
850
9.83k
      Builder.defineMacro("_NATIVE_WCHAR_T_DEFINED");
851
9.83k
    }
852
11.1k
  }
853
854
  // Macros to help identify the narrow and wide character sets
855
  // FIXME: clang currently ignores -fexec-charset=. If this changes,
856
  // then this may need to be updated.
857
87.7k
  Builder.defineMacro("__clang_literal_encoding__", "\"UTF-8\"");
858
87.7k
  if (TI.getTypeWidth(TI.getWCharType()) >= 32) {
859
    // FIXME: 32-bit wchar_t signals UTF-32. This may change
860
    // if -fwide-exec-charset= is ever supported.
861
78.5k
    Builder.defineMacro("__clang_wide_literal_encoding__", "\"UTF-32\"");
862
78.5k
  } else {
863
    // FIXME: Less-than 32-bit wchar_t generally means UTF-16
864
    // (e.g., Windows, 32-bit IBM). This may need to be
865
    // updated if -fwide-exec-charset= is ever supported.
866
9.18k
    Builder.defineMacro("__clang_wide_literal_encoding__", "\"UTF-16\"");
867
9.18k
  }
868
869
87.7k
  if (LangOpts.Optimize)
870
3.47k
    Builder.defineMacro("__OPTIMIZE__");
871
87.7k
  if (LangOpts.OptimizeSize)
872
84
    Builder.defineMacro("__OPTIMIZE_SIZE__");
873
874
87.7k
  if (LangOpts.FastMath)
875
48
    Builder.defineMacro("__FAST_MATH__");
876
877
  // Initialize target-specific preprocessor defines.
878
879
  // __BYTE_ORDER__ was added in GCC 4.6. It's analogous
880
  // to the macro __BYTE_ORDER (no trailing underscores)
881
  // from glibc's <endian.h> header.
882
  // We don't support the PDP-11 as a target, but include
883
  // the define so it can still be compared against.
884
87.7k
  Builder.defineMacro("__ORDER_LITTLE_ENDIAN__", "1234");
885
87.7k
  Builder.defineMacro("__ORDER_BIG_ENDIAN__",    "4321");
886
87.7k
  Builder.defineMacro("__ORDER_PDP_ENDIAN__",    "3412");
887
87.7k
  if (TI.isBigEndian()) {
888
864
    Builder.defineMacro("__BYTE_ORDER__", "__ORDER_BIG_ENDIAN__");
889
864
    Builder.defineMacro("__BIG_ENDIAN__");
890
86.8k
  } else {
891
86.8k
    Builder.defineMacro("__BYTE_ORDER__", "__ORDER_LITTLE_ENDIAN__");
892
86.8k
    Builder.defineMacro("__LITTLE_ENDIAN__");
893
86.8k
  }
894
895
87.7k
  if (TI.getPointerWidth(0) == 64 && 
TI.getLongWidth() == 6470.7k
896
87.7k
      && 
TI.getIntWidth() == 3262.6k
) {
897
62.6k
    Builder.defineMacro("_LP64");
898
62.6k
    Builder.defineMacro("__LP64__");
899
62.6k
  }
900
901
87.7k
  if (TI.getPointerWidth(0) == 32 && 
TI.getLongWidth() == 3216.9k
902
87.7k
      && 
TI.getIntWidth() == 3216.6k
) {
903
16.6k
    Builder.defineMacro("_ILP32");
904
16.6k
    Builder.defineMacro("__ILP32__");
905
16.6k
  }
906
907
  // Define type sizing macros based on the target properties.
908
87.7k
  assert(TI.getCharWidth() == 8 && "Only support 8-bit char so far");
909
0
  Builder.defineMacro("__CHAR_BIT__", Twine(TI.getCharWidth()));
910
911
87.7k
  Builder.defineMacro("__BOOL_WIDTH__", Twine(TI.getBoolWidth()));
912
87.7k
  Builder.defineMacro("__SHRT_WIDTH__", Twine(TI.getShortWidth()));
913
87.7k
  Builder.defineMacro("__INT_WIDTH__", Twine(TI.getIntWidth()));
914
87.7k
  Builder.defineMacro("__LONG_WIDTH__", Twine(TI.getLongWidth()));
915
87.7k
  Builder.defineMacro("__LLONG_WIDTH__", Twine(TI.getLongLongWidth()));
916
917
87.7k
  DefineTypeSize("__SCHAR_MAX__", TargetInfo::SignedChar, TI, Builder);
918
87.7k
  DefineTypeSize("__SHRT_MAX__", TargetInfo::SignedShort, TI, Builder);
919
87.7k
  DefineTypeSize("__INT_MAX__", TargetInfo::SignedInt, TI, Builder);
920
87.7k
  DefineTypeSize("__LONG_MAX__", TargetInfo::SignedLong, TI, Builder);
921
87.7k
  DefineTypeSize("__LONG_LONG_MAX__", TargetInfo::SignedLongLong, TI, Builder);
922
87.7k
  DefineTypeSizeAndWidth("__WCHAR", TI.getWCharType(), TI, Builder);
923
87.7k
  DefineTypeSizeAndWidth("__WINT", TI.getWIntType(), TI, Builder);
924
87.7k
  DefineTypeSizeAndWidth("__INTMAX", TI.getIntMaxType(), TI, Builder);
925
87.7k
  DefineTypeSizeAndWidth("__SIZE", TI.getSizeType(), TI, Builder);
926
927
87.7k
  DefineTypeSizeAndWidth("__UINTMAX", TI.getUIntMaxType(), TI, Builder);
928
87.7k
  DefineTypeSizeAndWidth("__PTRDIFF", TI.getPtrDiffType(0), TI, Builder);
929
87.7k
  DefineTypeSizeAndWidth("__INTPTR", TI.getIntPtrType(), TI, Builder);
930
87.7k
  DefineTypeSizeAndWidth("__UINTPTR", TI.getUIntPtrType(), TI, Builder);
931
932
87.7k
  DefineTypeSizeof("__SIZEOF_DOUBLE__", TI.getDoubleWidth(), TI, Builder);
933
87.7k
  DefineTypeSizeof("__SIZEOF_FLOAT__", TI.getFloatWidth(), TI, Builder);
934
87.7k
  DefineTypeSizeof("__SIZEOF_INT__", TI.getIntWidth(), TI, Builder);
935
87.7k
  DefineTypeSizeof("__SIZEOF_LONG__", TI.getLongWidth(), TI, Builder);
936
87.7k
  DefineTypeSizeof("__SIZEOF_LONG_DOUBLE__",TI.getLongDoubleWidth(),TI,Builder);
937
87.7k
  DefineTypeSizeof("__SIZEOF_LONG_LONG__", TI.getLongLongWidth(), TI, Builder);
938
87.7k
  DefineTypeSizeof("__SIZEOF_POINTER__", TI.getPointerWidth(0), TI, Builder);
939
87.7k
  DefineTypeSizeof("__SIZEOF_SHORT__", TI.getShortWidth(), TI, Builder);
940
87.7k
  DefineTypeSizeof("__SIZEOF_PTRDIFF_T__",
941
87.7k
                   TI.getTypeWidth(TI.getPtrDiffType(0)), TI, Builder);
942
87.7k
  DefineTypeSizeof("__SIZEOF_SIZE_T__",
943
87.7k
                   TI.getTypeWidth(TI.getSizeType()), TI, Builder);
944
87.7k
  DefineTypeSizeof("__SIZEOF_WCHAR_T__",
945
87.7k
                   TI.getTypeWidth(TI.getWCharType()), TI, Builder);
946
87.7k
  DefineTypeSizeof("__SIZEOF_WINT_T__",
947
87.7k
                   TI.getTypeWidth(TI.getWIntType()), TI, Builder);
948
87.7k
  if (TI.hasInt128Type())
949
70.8k
    DefineTypeSizeof("__SIZEOF_INT128__", 128, TI, Builder);
950
951
87.7k
  DefineType("__INTMAX_TYPE__", TI.getIntMaxType(), Builder);
952
87.7k
  DefineFmt("__INTMAX", TI.getIntMaxType(), TI, Builder);
953
87.7k
  Builder.defineMacro("__INTMAX_C_SUFFIX__",
954
87.7k
                      TI.getTypeConstantSuffix(TI.getIntMaxType()));
955
87.7k
  DefineType("__UINTMAX_TYPE__", TI.getUIntMaxType(), Builder);
956
87.7k
  DefineFmt("__UINTMAX", TI.getUIntMaxType(), TI, Builder);
957
87.7k
  Builder.defineMacro("__UINTMAX_C_SUFFIX__",
958
87.7k
                      TI.getTypeConstantSuffix(TI.getUIntMaxType()));
959
87.7k
  DefineType("__PTRDIFF_TYPE__", TI.getPtrDiffType(0), Builder);
960
87.7k
  DefineFmt("__PTRDIFF", TI.getPtrDiffType(0), TI, Builder);
961
87.7k
  DefineType("__INTPTR_TYPE__", TI.getIntPtrType(), Builder);
962
87.7k
  DefineFmt("__INTPTR", TI.getIntPtrType(), TI, Builder);
963
87.7k
  DefineType("__SIZE_TYPE__", TI.getSizeType(), Builder);
964
87.7k
  DefineFmt("__SIZE", TI.getSizeType(), TI, Builder);
965
87.7k
  DefineType("__WCHAR_TYPE__", TI.getWCharType(), Builder);
966
87.7k
  DefineType("__WINT_TYPE__", TI.getWIntType(), Builder);
967
87.7k
  DefineTypeSizeAndWidth("__SIG_ATOMIC", TI.getSigAtomicType(), TI, Builder);
968
87.7k
  DefineType("__CHAR16_TYPE__", TI.getChar16Type(), Builder);
969
87.7k
  DefineType("__CHAR32_TYPE__", TI.getChar32Type(), Builder);
970
971
87.7k
  DefineType("__UINTPTR_TYPE__", TI.getUIntPtrType(), Builder);
972
87.7k
  DefineFmt("__UINTPTR", TI.getUIntPtrType(), TI, Builder);
973
974
  // The C standard requires the width of uintptr_t and intptr_t to be the same,
975
  // per 7.20.2.4p1. Same for intmax_t and uintmax_t, per 7.20.2.5p1.
976
87.7k
  assert(TI.getTypeWidth(TI.getUIntPtrType()) ==
977
87.7k
             TI.getTypeWidth(TI.getIntPtrType()) &&
978
87.7k
         "uintptr_t and intptr_t have different widths?");
979
0
  assert(TI.getTypeWidth(TI.getUIntMaxType()) ==
980
87.7k
             TI.getTypeWidth(TI.getIntMaxType()) &&
981
87.7k
         "uintmax_t and intmax_t have different widths?");
982
983
87.7k
  if (TI.hasFloat16Type())
984
5.30k
    DefineFloatMacros(Builder, "FLT16", &TI.getHalfFormat(), "F16");
985
87.7k
  DefineFloatMacros(Builder, "FLT", &TI.getFloatFormat(), "F");
986
87.7k
  DefineFloatMacros(Builder, "DBL", &TI.getDoubleFormat(), "");
987
87.7k
  DefineFloatMacros(Builder, "LDBL", &TI.getLongDoubleFormat(), "L");
988
989
  // Define a __POINTER_WIDTH__ macro for stdint.h.
990
87.7k
  Builder.defineMacro("__POINTER_WIDTH__",
991
87.7k
                      Twine((int)TI.getPointerWidth(0)));
992
993
  // Define __BIGGEST_ALIGNMENT__ to be compatible with gcc.
994
87.7k
  Builder.defineMacro("__BIGGEST_ALIGNMENT__",
995
87.7k
                      Twine(TI.getSuitableAlign() / TI.getCharWidth()) );
996
997
87.7k
  if (!LangOpts.CharIsSigned)
998
996
    Builder.defineMacro("__CHAR_UNSIGNED__");
999
1000
87.7k
  if (!TargetInfo::isTypeSigned(TI.getWCharType()))
1001
13.3k
    Builder.defineMacro("__WCHAR_UNSIGNED__");
1002
1003
87.7k
  if (!TargetInfo::isTypeSigned(TI.getWIntType()))
1004
16.5k
    Builder.defineMacro("__WINT_UNSIGNED__");
1005
1006
  // Define exact-width integer types for stdint.h
1007
87.7k
  DefineExactWidthIntType(TargetInfo::SignedChar, TI, Builder);
1008
1009
87.7k
  if (TI.getShortWidth() > TI.getCharWidth())
1010
87.7k
    DefineExactWidthIntType(TargetInfo::SignedShort, TI, Builder);
1011
1012
87.7k
  if (TI.getIntWidth() > TI.getShortWidth())
1013
87.7k
    DefineExactWidthIntType(TargetInfo::SignedInt, TI, Builder);
1014
1015
87.7k
  if (TI.getLongWidth() > TI.getIntWidth())
1016
62.9k
    DefineExactWidthIntType(TargetInfo::SignedLong, TI, Builder);
1017
1018
87.7k
  if (TI.getLongLongWidth() > TI.getLongWidth())
1019
25.4k
    DefineExactWidthIntType(TargetInfo::SignedLongLong, TI, Builder);
1020
1021
87.7k
  DefineExactWidthIntType(TargetInfo::UnsignedChar, TI, Builder);
1022
87.7k
  DefineExactWidthIntTypeSize(TargetInfo::UnsignedChar, TI, Builder);
1023
87.7k
  DefineExactWidthIntTypeSize(TargetInfo::SignedChar, TI, Builder);
1024
1025
87.7k
  if (
TI.getShortWidth() > TI.getCharWidth()87.7k
) {
1026
87.7k
    DefineExactWidthIntType(TargetInfo::UnsignedShort, TI, Builder);
1027
87.7k
    DefineExactWidthIntTypeSize(TargetInfo::UnsignedShort, TI, Builder);
1028
87.7k
    DefineExactWidthIntTypeSize(TargetInfo::SignedShort, TI, Builder);
1029
87.7k
  }
1030
1031
87.7k
  if (TI.getIntWidth() > TI.getShortWidth()) {
1032
87.7k
    DefineExactWidthIntType(TargetInfo::UnsignedInt, TI, Builder);
1033
87.7k
    DefineExactWidthIntTypeSize(TargetInfo::UnsignedInt, TI, Builder);
1034
87.7k
    DefineExactWidthIntTypeSize(TargetInfo::SignedInt, TI, Builder);
1035
87.7k
  }
1036
1037
87.7k
  if (TI.getLongWidth() > TI.getIntWidth()) {
1038
62.9k
    DefineExactWidthIntType(TargetInfo::UnsignedLong, TI, Builder);
1039
62.9k
    DefineExactWidthIntTypeSize(TargetInfo::UnsignedLong, TI, Builder);
1040
62.9k
    DefineExactWidthIntTypeSize(TargetInfo::SignedLong, TI, Builder);
1041
62.9k
  }
1042
1043
87.7k
  if (TI.getLongLongWidth() > TI.getLongWidth()) {
1044
25.4k
    DefineExactWidthIntType(TargetInfo::UnsignedLongLong, TI, Builder);
1045
25.4k
    DefineExactWidthIntTypeSize(TargetInfo::UnsignedLongLong, TI, Builder);
1046
25.4k
    DefineExactWidthIntTypeSize(TargetInfo::SignedLongLong, TI, Builder);
1047
25.4k
  }
1048
1049
87.7k
  DefineLeastWidthIntType(8, true, TI, Builder);
1050
87.7k
  DefineLeastWidthIntType(8, false, TI, Builder);
1051
87.7k
  DefineLeastWidthIntType(16, true, TI, Builder);
1052
87.7k
  DefineLeastWidthIntType(16, false, TI, Builder);
1053
87.7k
  DefineLeastWidthIntType(32, true, TI, Builder);
1054
87.7k
  DefineLeastWidthIntType(32, false, TI, Builder);
1055
87.7k
  DefineLeastWidthIntType(64, true, TI, Builder);
1056
87.7k
  DefineLeastWidthIntType(64, false, TI, Builder);
1057
1058
87.7k
  DefineFastIntType(8, true, TI, Builder);
1059
87.7k
  DefineFastIntType(8, false, TI, Builder);
1060
87.7k
  DefineFastIntType(16, true, TI, Builder);
1061
87.7k
  DefineFastIntType(16, false, TI, Builder);
1062
87.7k
  DefineFastIntType(32, true, TI, Builder);
1063
87.7k
  DefineFastIntType(32, false, TI, Builder);
1064
87.7k
  DefineFastIntType(64, true, TI, Builder);
1065
87.7k
  DefineFastIntType(64, false, TI, Builder);
1066
1067
87.7k
  Builder.defineMacro("__USER_LABEL_PREFIX__", TI.getUserLabelPrefix());
1068
1069
87.7k
  if (!LangOpts.MathErrno)
1070
52.5k
    Builder.defineMacro("__NO_MATH_ERRNO__");
1071
1072
87.7k
  if (LangOpts.FastMath || 
LangOpts.FiniteMathOnly87.7k
)
1073
53
    Builder.defineMacro("__FINITE_MATH_ONLY__", "1");
1074
87.7k
  else
1075
87.7k
    Builder.defineMacro("__FINITE_MATH_ONLY__", "0");
1076
1077
87.7k
  if (LangOpts.GNUCVersion) {
1078
27.3k
    if (LangOpts.GNUInline || 
LangOpts.CPlusPlus26.9k
)
1079
20.8k
      Builder.defineMacro("__GNUC_GNU_INLINE__");
1080
6.54k
    else
1081
6.54k
      Builder.defineMacro("__GNUC_STDC_INLINE__");
1082
1083
    // The value written by __atomic_test_and_set.
1084
    // FIXME: This is target-dependent.
1085
27.3k
    Builder.defineMacro("__GCC_ATOMIC_TEST_AND_SET_TRUEVAL", "1");
1086
27.3k
  }
1087
1088
115k
  auto addLockFreeMacros = [&](const llvm::Twine &Prefix) {
1089
    // Used by libc++ and libstdc++ to implement ATOMIC_<foo>_LOCK_FREE.
1090
115k
    unsigned InlineWidthBits = TI.getMaxAtomicInlineWidth();
1091
115k
#define DEFINE_LOCK_FREE_MACRO(TYPE, Type)                                     \
1092
115k
  Builder.defineMacro(Prefix + #TYPE "_LOCK_FREE",                             \
1093
5.24k
                      getLockFreeValue(TI.get##Type##Width(),                  \
1094
5.24k
                                       TI.get##Type##Align(),                  \
1095
5.24k
                                       InlineWidthBits));
1096
115k
    DEFINE_LOCK_FREE_MACRO(BOOL, Bool);
1097
115k
    DEFINE_LOCK_FREE_MACRO(CHAR, Char);
1098
115k
    if (LangOpts.Char8)
1099
5.24k
      DEFINE_LOCK_FREE_MACRO(CHAR8_T, Char); // Treat char8_t like char.
1100
115k
    DEFINE_LOCK_FREE_MACRO(CHAR16_T, Char16);
1101
115k
    DEFINE_LOCK_FREE_MACRO(CHAR32_T, Char32);
1102
115k
    DEFINE_LOCK_FREE_MACRO(WCHAR_T, WChar);
1103
115k
    DEFINE_LOCK_FREE_MACRO(SHORT, Short);
1104
115k
    DEFINE_LOCK_FREE_MACRO(INT, Int);
1105
115k
    DEFINE_LOCK_FREE_MACRO(LONG, Long);
1106
115k
    DEFINE_LOCK_FREE_MACRO(LLONG, LongLong);
1107
115k
    Builder.defineMacro(Prefix + "POINTER_LOCK_FREE",
1108
115k
                        getLockFreeValue(TI.getPointerWidth(0),
1109
115k
                                         TI.getPointerAlign(0),
1110
115k
                                         InlineWidthBits));
1111
115k
#undef DEFINE_LOCK_FREE_MACRO
1112
115k
  };
1113
87.7k
  addLockFreeMacros("__CLANG_ATOMIC_");
1114
87.7k
  if (LangOpts.GNUCVersion)
1115
27.3k
    addLockFreeMacros("__GCC_ATOMIC_");
1116
1117
87.7k
  if (LangOpts.NoInlineDefine)
1118
68.9k
    Builder.defineMacro("__NO_INLINE__");
1119
1120
87.7k
  if (unsigned PICLevel = LangOpts.PICLevel) {
1121
24.4k
    Builder.defineMacro("__PIC__", Twine(PICLevel));
1122
24.4k
    Builder.defineMacro("__pic__", Twine(PICLevel));
1123
24.4k
    if (LangOpts.PIE) {
1124
55
      Builder.defineMacro("__PIE__", Twine(PICLevel));
1125
55
      Builder.defineMacro("__pie__", Twine(PICLevel));
1126
55
    }
1127
24.4k
  }
1128
1129
  // Macros to control C99 numerics and <float.h>
1130
87.7k
  Builder.defineMacro("__FLT_EVAL_METHOD__", Twine(TI.getFloatEvalMethod()));
1131
87.7k
  Builder.defineMacro("__FLT_RADIX__", "2");
1132
87.7k
  Builder.defineMacro("__DECIMAL_DIG__", "__LDBL_DECIMAL_DIG__");
1133
1134
87.7k
  if (LangOpts.getStackProtector() == LangOptions::SSPOn)
1135
16.8k
    Builder.defineMacro("__SSP__");
1136
70.9k
  else if (LangOpts.getStackProtector() == LangOptions::SSPStrong)
1137
164
    Builder.defineMacro("__SSP_STRONG__", "2");
1138
70.7k
  else if (LangOpts.getStackProtector() == LangOptions::SSPReq)
1139
2
    Builder.defineMacro("__SSP_ALL__", "3");
1140
1141
87.7k
  if (PPOpts.SetUpStaticAnalyzer)
1142
1.37k
    Builder.defineMacro("__clang_analyzer__");
1143
1144
87.7k
  if (LangOpts.FastRelaxedMath)
1145
3
    Builder.defineMacro("__FAST_RELAXED_MATH__");
1146
1147
87.7k
  if (FEOpts.ProgramAction == frontend::RewriteObjC ||
1148
87.7k
      
LangOpts.getGC() != LangOptions::NonGC87.5k
) {
1149
255
    Builder.defineMacro("__weak", "__attribute__((objc_gc(weak)))");
1150
255
    Builder.defineMacro("__strong", "__attribute__((objc_gc(strong)))");
1151
255
    Builder.defineMacro("__autoreleasing", "");
1152
255
    Builder.defineMacro("__unsafe_unretained", "");
1153
87.5k
  } else if (LangOpts.ObjC) {
1154
22.3k
    Builder.defineMacro("__weak", "__attribute__((objc_ownership(weak)))");
1155
22.3k
    Builder.defineMacro("__strong", "__attribute__((objc_ownership(strong)))");
1156
22.3k
    Builder.defineMacro("__autoreleasing",
1157
22.3k
                        "__attribute__((objc_ownership(autoreleasing)))");
1158
22.3k
    Builder.defineMacro("__unsafe_unretained",
1159
22.3k
                        "__attribute__((objc_ownership(none)))");
1160
22.3k
  }
1161
1162
  // On Darwin, there are __double_underscored variants of the type
1163
  // nullability qualifiers.
1164
87.7k
  if (TI.getTriple().isOSDarwin()) {
1165
50.7k
    Builder.defineMacro("__nonnull", "_Nonnull");
1166
50.7k
    Builder.defineMacro("__null_unspecified", "_Null_unspecified");
1167
50.7k
    Builder.defineMacro("__nullable", "_Nullable");
1168
50.7k
  }
1169
1170
  // Add a macro to differentiate between regular iOS/tvOS/watchOS targets and
1171
  // the corresponding simulator targets.
1172
87.7k
  if (TI.getTriple().isOSDarwin() && 
TI.getTriple().isSimulatorEnvironment()50.7k
)
1173
44
    Builder.defineMacro("__APPLE_EMBEDDED_SIMULATOR__", "1");
1174
1175
  // OpenMP definition
1176
  // OpenMP 2.2:
1177
  //   In implementations that support a preprocessor, the _OPENMP
1178
  //   macro name is defined to have the decimal value yyyymm where
1179
  //   yyyy and mm are the year and the month designations of the
1180
  //   version of the OpenMP API that the implementation support.
1181
87.7k
  if (!LangOpts.OpenMPSimd) {
1182
82.7k
    switch (LangOpts.OpenMP) {
1183
75.8k
    case 0:
1184
75.8k
      break;
1185
2
    case 31:
1186
2
      Builder.defineMacro("_OPENMP", "201107");
1187
2
      break;
1188
12
    case 40:
1189
12
      Builder.defineMacro("_OPENMP", "201307");
1190
12
      break;
1191
1.24k
    case 45:
1192
1.24k
      Builder.defineMacro("_OPENMP", "201511");
1193
1.24k
      break;
1194
365
    case 51:
1195
365
      Builder.defineMacro("_OPENMP", "202011");
1196
365
      break;
1197
1
    case 52:
1198
1
      Builder.defineMacro("_OPENMP", "202111");
1199
1
      break;
1200
5.33k
    default:
1201
      // Default version is OpenMP 5.0
1202
5.33k
      Builder.defineMacro("_OPENMP", "201811");
1203
5.33k
      break;
1204
82.7k
    }
1205
82.7k
  }
1206
1207
  // CUDA device path compilaton
1208
87.7k
  if (LangOpts.CUDAIsDevice && 
!LangOpts.HIP182
) {
1209
    // The CUDA_ARCH value is set for the GPU target specified in the NVPTX
1210
    // backend's target defines.
1211
138
    Builder.defineMacro("__CUDA_ARCH__");
1212
138
  }
1213
1214
  // We need to communicate this to our CUDA header wrapper, which in turn
1215
  // informs the proper CUDA headers of this choice.
1216
87.7k
  if (LangOpts.CUDADeviceApproxTranscendentals || 
LangOpts.FastMath87.7k
) {
1217
50
    Builder.defineMacro("__CLANG_CUDA_APPROX_TRANSCENDENTALS__");
1218
50
  }
1219
1220
  // Define a macro indicating that the source file is being compiled with a
1221
  // SYCL device compiler which doesn't produce host binary.
1222
87.7k
  if (LangOpts.SYCLIsDevice) {
1223
37
    Builder.defineMacro("__SYCL_DEVICE_ONLY__", "1");
1224
37
  }
1225
1226
  // OpenCL definitions.
1227
87.7k
  if (LangOpts.OpenCL) {
1228
643
    InitializeOpenCLFeatureTestMacros(TI, LangOpts, Builder);
1229
1230
643
    if (TI.getTriple().isSPIR() || 
TI.getTriple().isSPIRV()394
)
1231
254
      Builder.defineMacro("__IMAGE_SUPPORT__");
1232
643
  }
1233
1234
87.7k
  if (TI.hasInt128Type() && 
LangOpts.CPlusPlus70.8k
&&
LangOpts.GNUMode54.7k
) {
1235
    // For each extended integer type, g++ defines a macro mapping the
1236
    // index of the type (0 in this case) in some list of extended types
1237
    // to the type.
1238
28.8k
    Builder.defineMacro("__GLIBCXX_TYPE_INT_N_0", "__int128");
1239
28.8k
    Builder.defineMacro("__GLIBCXX_BITSIZE_INT_N_0", "128");
1240
28.8k
  }
1241
1242
  // Get other target #defines.
1243
87.7k
  TI.getTargetDefines(LangOpts, Builder);
1244
87.7k
}
1245
1246
/// InitializePreprocessor - Initialize the preprocessor getting it and the
1247
/// environment ready to process a single file. This returns true on error.
1248
///
1249
void clang::InitializePreprocessor(
1250
    Preprocessor &PP, const PreprocessorOptions &InitOpts,
1251
    const PCHContainerReader &PCHContainerRdr,
1252
87.6k
    const FrontendOptions &FEOpts) {
1253
87.6k
  const LangOptions &LangOpts = PP.getLangOpts();
1254
87.6k
  std::string PredefineBuffer;
1255
87.6k
  PredefineBuffer.reserve(4080);
1256
87.6k
  llvm::raw_string_ostream Predefines(PredefineBuffer);
1257
87.6k
  MacroBuilder Builder(Predefines);
1258
1259
  // Emit line markers for various builtin sections of the file.  We don't do
1260
  // this in asm preprocessor mode, because "# 4" is not a line marker directive
1261
  // in this mode.
1262
87.6k
  if (!PP.getLangOpts().AsmPreprocessor)
1263
87.6k
    Builder.append("# 1 \"<built-in>\" 3");
1264
1265
  // Install things like __POWERPC__, __GNUC__, etc into the macro table.
1266
87.6k
  if (InitOpts.UsePredefines) {
1267
    // FIXME: This will create multiple definitions for most of the predefined
1268
    // macros. This is not the right way to handle this.
1269
87.6k
    if ((LangOpts.CUDA || 
LangOpts.OpenMPIsDevice87.3k
||
LangOpts.SYCLIsDevice86.6k
) &&
1270
87.6k
        
PP.getAuxTargetInfo()1.03k
)
1271
116
      InitializePredefinedMacros(*PP.getAuxTargetInfo(), LangOpts, FEOpts,
1272
116
                                 PP.getPreprocessorOpts(), Builder);
1273
1274
87.6k
    InitializePredefinedMacros(PP.getTargetInfo(), LangOpts, FEOpts,
1275
87.6k
                               PP.getPreprocessorOpts(), Builder);
1276
1277
    // Install definitions to make Objective-C++ ARC work well with various
1278
    // C++ Standard Library implementations.
1279
87.6k
    if (LangOpts.ObjC && 
LangOpts.CPlusPlus22.6k
&&
1280
87.6k
        
(17.1k
LangOpts.ObjCAutoRefCount17.1k
||
LangOpts.ObjCWeak15.6k
)) {
1281
1.57k
      switch (InitOpts.ObjCXXARCStandardLibrary) {
1282
142
      case ARCXX_nolib:
1283
1.57k
      case ARCXX_libcxx:
1284
1.57k
        break;
1285
1286
1
      case ARCXX_libstdcxx:
1287
1
        AddObjCXXARCLibstdcxxDefines(LangOpts, Builder);
1288
1
        break;
1289
1.57k
      }
1290
1.57k
    }
1291
87.6k
  }
1292
1293
  // Even with predefines off, some macros are still predefined.
1294
  // These should all be defined in the preprocessor according to the
1295
  // current language configuration.
1296
87.6k
  InitializeStandardPredefinedMacros(PP.getTargetInfo(), PP.getLangOpts(),
1297
87.6k
                                     FEOpts, Builder);
1298
1299
  // Add on the predefines from the driver.  Wrap in a #line directive to report
1300
  // that they come from the command line.
1301
87.6k
  if (!PP.getLangOpts().AsmPreprocessor)
1302
87.5k
    Builder.append("# 1 \"<command line>\" 1");
1303
1304
  // Process #define's and #undef's in the order they are given.
1305
436k
  for (unsigned i = 0, e = InitOpts.Macros.size(); i != e; 
++i348k
) {
1306
348k
    if (InitOpts.Macros[i].second)  // isUndef
1307
49
      Builder.undefineMacro(InitOpts.Macros[i].first);
1308
348k
    else
1309
348k
      DefineBuiltinMacro(Builder, InitOpts.Macros[i].first,
1310
348k
                         PP.getDiagnostics());
1311
348k
  }
1312
1313
  // Exit the command line and go back to <built-in> (2 is LC_LEAVE).
1314
87.6k
  if (!PP.getLangOpts().AsmPreprocessor)
1315
87.6k
    Builder.append("# 1 \"<built-in>\" 2");
1316
1317
  // If -imacros are specified, include them now.  These are processed before
1318
  // any -include directives.
1319
87.6k
  for (unsigned i = 0, e = InitOpts.MacroIncludes.size(); i != e; 
++i2
)
1320
2
    AddImplicitIncludeMacros(Builder, InitOpts.MacroIncludes[i]);
1321
1322
  // Process -include-pch/-include-pth directives.
1323
87.6k
  if (!InitOpts.ImplicitPCHInclude.empty())
1324
3.66k
    AddImplicitIncludePCH(Builder, PP, PCHContainerRdr,
1325
3.66k
                          InitOpts.ImplicitPCHInclude);
1326
1327
  // Process -include directives.
1328
91.2k
  for (unsigned i = 0, e = InitOpts.Includes.size(); i != e; 
++i3.55k
) {
1329
3.55k
    const std::string &Path = InitOpts.Includes[i];
1330
3.55k
    AddImplicitInclude(Builder, Path);
1331
3.55k
  }
1332
1333
  // Instruct the preprocessor to skip the preamble.
1334
87.6k
  PP.setSkipMainFilePreamble(InitOpts.PrecompiledPreambleBytes.first,
1335
87.6k
                             InitOpts.PrecompiledPreambleBytes.second);
1336
1337
  // Copy PredefinedBuffer into the Preprocessor.
1338
87.6k
  PP.setPredefines(Predefines.str());
1339
87.6k
}