Coverage Report

Created: 2020-02-25 14:32

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