Coverage Report

Created: 2021-08-24 07:12

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