Coverage Report

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