Coverage Report

Created: 2022-05-14 11:35

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