Coverage Report

Created: 2023-09-21 18:56

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Lex/PPMacroExpansion.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- PPMacroExpansion.cpp - Top level Macro Expansion -----------------===//
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 top level handling of macro expansion for the
10
// preprocessor.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#include "clang/Basic/AttributeCommonInfo.h"
15
#include "clang/Basic/Attributes.h"
16
#include "clang/Basic/Builtins.h"
17
#include "clang/Basic/FileManager.h"
18
#include "clang/Basic/IdentifierTable.h"
19
#include "clang/Basic/LLVM.h"
20
#include "clang/Basic/LangOptions.h"
21
#include "clang/Basic/ObjCRuntime.h"
22
#include "clang/Basic/SourceLocation.h"
23
#include "clang/Basic/TargetInfo.h"
24
#include "clang/Lex/CodeCompletionHandler.h"
25
#include "clang/Lex/DirectoryLookup.h"
26
#include "clang/Lex/ExternalPreprocessorSource.h"
27
#include "clang/Lex/HeaderSearch.h"
28
#include "clang/Lex/LexDiagnostic.h"
29
#include "clang/Lex/LiteralSupport.h"
30
#include "clang/Lex/MacroArgs.h"
31
#include "clang/Lex/MacroInfo.h"
32
#include "clang/Lex/Preprocessor.h"
33
#include "clang/Lex/PreprocessorLexer.h"
34
#include "clang/Lex/PreprocessorOptions.h"
35
#include "clang/Lex/Token.h"
36
#include "llvm/ADT/ArrayRef.h"
37
#include "llvm/ADT/DenseMap.h"
38
#include "llvm/ADT/DenseSet.h"
39
#include "llvm/ADT/FoldingSet.h"
40
#include "llvm/ADT/STLExtras.h"
41
#include "llvm/ADT/SmallString.h"
42
#include "llvm/ADT/SmallVector.h"
43
#include "llvm/ADT/StringRef.h"
44
#include "llvm/ADT/StringSwitch.h"
45
#include "llvm/Support/Casting.h"
46
#include "llvm/Support/ErrorHandling.h"
47
#include "llvm/Support/Format.h"
48
#include "llvm/Support/Path.h"
49
#include "llvm/Support/raw_ostream.h"
50
#include <algorithm>
51
#include <cassert>
52
#include <cstddef>
53
#include <cstring>
54
#include <ctime>
55
#include <optional>
56
#include <string>
57
#include <tuple>
58
#include <utility>
59
60
using namespace clang;
61
62
MacroDirective *
63
3.20M
Preprocessor::getLocalMacroDirectiveHistory(const IdentifierInfo *II) const {
64
3.20M
  if (!II->hadMacroDefinition())
65
0
    return nullptr;
66
3.20M
  auto Pos = CurSubmoduleState->Macros.find(II);
67
3.20M
  return Pos == CurSubmoduleState->Macros.end() ? 
nullptr294
68
3.20M
                                                : 
Pos->second.getLatest()3.20M
;
69
3.20M
}
70
71
48.0M
void Preprocessor::appendMacroDirective(IdentifierInfo *II, MacroDirective *MD){
72
48.0M
  assert(MD && "MacroDirective should be non-zero!");
73
48.0M
  assert(!MD->getPrevious() && "Already attached to a MacroDirective history.");
74
75
48.0M
  MacroState &StoredMD = CurSubmoduleState->Macros[II];
76
48.0M
  auto *OldMD = StoredMD.getLatest();
77
48.0M
  MD->setPrevious(OldMD);
78
48.0M
  StoredMD.setLatest(MD);
79
48.0M
  StoredMD.overrideActiveModuleMacros(*this, II);
80
81
48.0M
  if (needModuleMacros()) {
82
    // Track that we created a new macro directive, so we know we should
83
    // consider building a ModuleMacro for it when we get to the end of
84
    // the module.
85
242k
    PendingModuleMacroNames.push_back(II);
86
242k
  }
87
88
  // Set up the identifier as having associated macro history.
89
48.0M
  II->setHasMacroDefinition(true);
90
48.0M
  if (!MD->isDefined() && 
!LeafModuleMacros.contains(II)219k
)
91
218k
    II->setHasMacroDefinition(false);
92
48.0M
  if (II->isFromAST())
93
7.82k
    II->setChangedSinceDeserialization();
94
48.0M
}
95
96
void Preprocessor::setLoadedMacroDirective(IdentifierInfo *II,
97
                                           MacroDirective *ED,
98
49.0k
                                           MacroDirective *MD) {
99
  // Normally, when a macro is defined, it goes through appendMacroDirective()
100
  // above, which chains a macro to previous defines, undefs, etc.
101
  // However, in a pch, the whole macro history up to the end of the pch is
102
  // stored, so ASTReader goes through this function instead.
103
  // However, built-in macros are already registered in the Preprocessor
104
  // ctor, and ASTWriter stops writing the macro chain at built-in macros,
105
  // so in that case the chain from the pch needs to be spliced to the existing
106
  // built-in.
107
108
49.0k
  assert(II && MD);
109
49.0k
  MacroState &StoredMD = CurSubmoduleState->Macros[II];
110
111
49.0k
  if (auto *OldMD = StoredMD.getLatest()) {
112
    // shouldIgnoreMacro() in ASTWriter also stops at macros from the
113
    // predefines buffer in module builds. However, in module builds, modules
114
    // are loaded completely before predefines are processed, so StoredMD
115
    // will be nullptr for them when they're loaded. StoredMD should only be
116
    // non-nullptr for builtins read from a pch file.
117
3
    assert(OldMD->getMacroInfo()->isBuiltinMacro() &&
118
3
           "only built-ins should have an entry here");
119
3
    assert(!OldMD->getPrevious() && "builtin should only have a single entry");
120
3
    ED->setPrevious(OldMD);
121
3
    StoredMD.setLatest(MD);
122
49.0k
  } else {
123
49.0k
    StoredMD = MD;
124
49.0k
  }
125
126
  // Setup the identifier as having associated macro history.
127
49.0k
  II->setHasMacroDefinition(true);
128
49.0k
  if (!MD->isDefined() && 
!LeafModuleMacros.contains(II)25
)
129
25
    II->setHasMacroDefinition(false);
130
49.0k
}
131
132
ModuleMacro *Preprocessor::addModuleMacro(Module *Mod, IdentifierInfo *II,
133
                                          MacroInfo *Macro,
134
                                          ArrayRef<ModuleMacro *> Overrides,
135
28.9M
                                          bool &New) {
136
28.9M
  llvm::FoldingSetNodeID ID;
137
28.9M
  ModuleMacro::Profile(ID, Mod, II);
138
139
28.9M
  void *InsertPos;
140
28.9M
  if (auto *MM = ModuleMacros.FindNodeOrInsertPos(ID, InsertPos)) {
141
22.7M
    New = false;
142
22.7M
    return MM;
143
22.7M
  }
144
145
6.19M
  auto *MM = ModuleMacro::create(*this, Mod, II, Macro, Overrides);
146
6.19M
  ModuleMacros.InsertNode(MM, InsertPos);
147
148
  // Each overridden macro is now overridden by one more macro.
149
6.19M
  bool HidAny = false;
150
6.19M
  for (auto *O : Overrides) {
151
4.13k
    HidAny |= (O->NumOverriddenBy == 0);
152
4.13k
    ++O->NumOverriddenBy;
153
4.13k
  }
154
155
  // If we were the first overrider for any macro, it's no longer a leaf.
156
6.19M
  auto &LeafMacros = LeafModuleMacros[II];
157
6.19M
  if (HidAny) {
158
3.89k
    llvm::erase_if(LeafMacros,
159
4.07k
                   [](ModuleMacro *MM) { return MM->NumOverriddenBy != 0; });
160
3.89k
  }
161
162
  // The new macro is always a leaf macro.
163
6.19M
  LeafMacros.push_back(MM);
164
  // The identifier now has defined macros (that may or may not be visible).
165
6.19M
  II->setHasMacroDefinition(true);
166
167
6.19M
  New = true;
168
6.19M
  return MM;
169
28.9M
}
170
171
ModuleMacro *Preprocessor::getModuleMacro(Module *Mod,
172
2.09k
                                          const IdentifierInfo *II) {
173
2.09k
  llvm::FoldingSetNodeID ID;
174
2.09k
  ModuleMacro::Profile(ID, Mod, II);
175
176
2.09k
  void *InsertPos;
177
2.09k
  return ModuleMacros.FindNodeOrInsertPos(ID, InsertPos);
178
2.09k
}
179
180
void Preprocessor::updateModuleMacroInfo(const IdentifierInfo *II,
181
158k
                                         ModuleMacroInfo &Info) {
182
158k
  assert(Info.ActiveModuleMacrosGeneration !=
183
158k
             CurSubmoduleState->VisibleModules.getGeneration() &&
184
158k
         "don't need to update this macro name info");
185
158k
  Info.ActiveModuleMacrosGeneration =
186
158k
      CurSubmoduleState->VisibleModules.getGeneration();
187
188
158k
  auto Leaf = LeafModuleMacros.find(II);
189
158k
  if (Leaf == LeafModuleMacros.end()) {
190
    // No imported macros at all: nothing to do.
191
40.3k
    return;
192
40.3k
  }
193
194
118k
  Info.ActiveModuleMacros.clear();
195
196
  // Every macro that's locally overridden is overridden by a visible macro.
197
118k
  llvm::DenseMap<ModuleMacro *, int> NumHiddenOverrides;
198
118k
  for (auto *O : Info.OverriddenMacros)
199
42
    NumHiddenOverrides[O] = -1;
200
201
  // Collect all macros that are not overridden by a visible macro.
202
118k
  llvm::SmallVector<ModuleMacro *, 16> Worklist;
203
2.10M
  for (auto *LeafMM : Leaf->second) {
204
2.10M
    assert(LeafMM->getNumOverridingMacros() == 0 && "leaf macro overridden");
205
2.10M
    if (NumHiddenOverrides.lookup(LeafMM) == 0)
206
2.10M
      Worklist.push_back(LeafMM);
207
2.10M
  }
208
2.22M
  
while (118k
!Worklist.empty()) {
209
2.10M
    auto *MM = Worklist.pop_back_val();
210
2.10M
    if (CurSubmoduleState->VisibleModules.isVisible(MM->getOwningModule())) {
211
      // We only care about collecting definitions; undefinitions only act
212
      // to override other definitions.
213
842k
      if (MM->getMacroInfo())
214
842k
        Info.ActiveModuleMacros.push_back(MM);
215
1.26M
    } else {
216
1.26M
      for (auto *O : MM->overrides())
217
120
        if ((unsigned)++NumHiddenOverrides[O] == O->getNumOverridingMacros())
218
105
          Worklist.push_back(O);
219
1.26M
    }
220
2.10M
  }
221
  // Our reverse postorder walk found the macros in reverse order.
222
118k
  std::reverse(Info.ActiveModuleMacros.begin(), Info.ActiveModuleMacros.end());
223
224
  // Determine whether the macro name is ambiguous.
225
118k
  MacroInfo *MI = nullptr;
226
118k
  bool IsSystemMacro = true;
227
118k
  bool IsAmbiguous = false;
228
118k
  if (auto *MD = Info.MD) {
229
4.76k
    while (MD && isa<VisibilityMacroDirective>(MD))
230
0
      MD = MD->getPrevious();
231
4.76k
    if (auto *DMD = dyn_cast_or_null<DefMacroDirective>(MD)) {
232
4.74k
      MI = DMD->getInfo();
233
4.74k
      IsSystemMacro &= SourceMgr.isInSystemHeader(DMD->getLocation());
234
4.74k
    }
235
4.76k
  }
236
842k
  for (auto *Active : Info.ActiveModuleMacros) {
237
842k
    auto *NewMI = Active->getMacroInfo();
238
239
    // Before marking the macro as ambiguous, check if this is a case where
240
    // both macros are in system headers. If so, we trust that the system
241
    // did not get it wrong. This also handles cases where Clang's own
242
    // headers have a different spelling of certain system macros:
243
    //   #define LONG_MAX __LONG_MAX__ (clang's limits.h)
244
    //   #define LONG_MAX 0x7fffffffffffffffL (system's limits.h)
245
    //
246
    // FIXME: Remove the defined-in-system-headers check. clang's limits.h
247
    // overrides the system limits.h's macros, so there's no conflict here.
248
842k
    if (MI && 
NewMI != MI736k
&&
249
842k
        
!MI->isIdenticalTo(*NewMI, *this, /*Syntactically=*/true)736k
)
250
71
      IsAmbiguous = true;
251
842k
    IsSystemMacro &= Active->getOwningModule()->IsSystem ||
252
842k
                     
SourceMgr.isInSystemHeader(NewMI->getDefinitionLoc())686
;
253
842k
    MI = NewMI;
254
842k
  }
255
118k
  Info.IsAmbiguous = IsAmbiguous && 
!IsSystemMacro71
;
256
118k
}
257
258
10
void Preprocessor::dumpMacroInfo(const IdentifierInfo *II) {
259
10
  ArrayRef<ModuleMacro*> Leaf;
260
10
  auto LeafIt = LeafModuleMacros.find(II);
261
10
  if (LeafIt != LeafModuleMacros.end())
262
10
    Leaf = LeafIt->second;
263
10
  const MacroState *State = nullptr;
264
10
  auto Pos = CurSubmoduleState->Macros.find(II);
265
10
  if (Pos != CurSubmoduleState->Macros.end())
266
10
    State = &Pos->second;
267
268
10
  llvm::errs() << "MacroState " << State << " " << II->getNameStart();
269
10
  if (State && State->isAmbiguous(*this, II))
270
9
    llvm::errs() << " ambiguous";
271
10
  if (State && !State->getOverriddenMacros().empty()) {
272
0
    llvm::errs() << " overrides";
273
0
    for (auto *O : State->getOverriddenMacros())
274
0
      llvm::errs() << " " << O->getOwningModule()->getFullModuleName();
275
0
  }
276
10
  llvm::errs() << "\n";
277
278
  // Dump local macro directives.
279
10
  for (auto *MD = State ? State->getLatest() : 
nullptr0
; MD;
280
10
       
MD = MD->getPrevious()0
) {
281
0
    llvm::errs() << " ";
282
0
    MD->dump();
283
0
  }
284
285
  // Dump module macros.
286
10
  llvm::DenseSet<ModuleMacro*> Active;
287
10
  for (auto *MM :
288
10
       State ? State->getActiveModuleMacros(*this, II) : 
std::nullopt0
)
289
18
    Active.insert(MM);
290
10
  llvm::DenseSet<ModuleMacro*> Visited;
291
10
  llvm::SmallVector<ModuleMacro *, 16> Worklist(Leaf.begin(), Leaf.end());
292
29
  while (!Worklist.empty()) {
293
19
    auto *MM = Worklist.pop_back_val();
294
19
    llvm::errs() << " ModuleMacro " << MM << " "
295
19
                 << MM->getOwningModule()->getFullModuleName();
296
19
    if (!MM->getMacroInfo())
297
0
      llvm::errs() << " undef";
298
299
19
    if (Active.count(MM))
300
18
      llvm::errs() << " active";
301
1
    else if (!CurSubmoduleState->VisibleModules.isVisible(
302
1
                 MM->getOwningModule()))
303
1
      llvm::errs() << " hidden";
304
0
    else if (MM->getMacroInfo())
305
0
      llvm::errs() << " overridden";
306
307
19
    if (!MM->overrides().empty()) {
308
0
      llvm::errs() << " overrides";
309
0
      for (auto *O : MM->overrides()) {
310
0
        llvm::errs() << " " << O->getOwningModule()->getFullModuleName();
311
0
        if (Visited.insert(O).second)
312
0
          Worklist.push_back(O);
313
0
      }
314
0
    }
315
19
    llvm::errs() << "\n";
316
19
    if (auto *MI = MM->getMacroInfo()) {
317
19
      llvm::errs() << "  ";
318
19
      MI->dump();
319
19
      llvm::errs() << "\n";
320
19
    }
321
19
  }
322
10
}
323
324
/// RegisterBuiltinMacro - Register the specified identifier in the identifier
325
/// table and mark it as a builtin macro to be expanded.
326
2.74M
static IdentifierInfo *RegisterBuiltinMacro(Preprocessor &PP, const char *Name){
327
  // Get the identifier.
328
2.74M
  IdentifierInfo *Id = PP.getIdentifierInfo(Name);
329
330
  // Mark it as being a macro that is builtin.
331
2.74M
  MacroInfo *MI = PP.AllocateMacroInfo(SourceLocation());
332
2.74M
  MI->setIsBuiltinMacro();
333
2.74M
  PP.appendDefMacroDirective(Id, MI);
334
2.74M
  return Id;
335
2.74M
}
336
337
/// RegisterBuiltinMacros - Register builtin macros, such as __LINE__ with the
338
/// identifier table.
339
93.7k
void Preprocessor::RegisterBuiltinMacros() {
340
93.7k
  Ident__LINE__ = RegisterBuiltinMacro(*this, "__LINE__");
341
93.7k
  Ident__FILE__ = RegisterBuiltinMacro(*this, "__FILE__");
342
93.7k
  Ident__DATE__ = RegisterBuiltinMacro(*this, "__DATE__");
343
93.7k
  Ident__TIME__ = RegisterBuiltinMacro(*this, "__TIME__");
344
93.7k
  Ident__COUNTER__ = RegisterBuiltinMacro(*this, "__COUNTER__");
345
93.7k
  Ident_Pragma  = RegisterBuiltinMacro(*this, "_Pragma");
346
93.7k
  Ident__FLT_EVAL_METHOD__ = RegisterBuiltinMacro(*this, "__FLT_EVAL_METHOD__");
347
348
  // C++ Standing Document Extensions.
349
93.7k
  if (getLangOpts().CPlusPlus)
350
70.5k
    Ident__has_cpp_attribute =
351
70.5k
        RegisterBuiltinMacro(*this, "__has_cpp_attribute");
352
23.1k
  else
353
23.1k
    Ident__has_cpp_attribute = nullptr;
354
355
  // GCC Extensions.
356
93.7k
  Ident__BASE_FILE__     = RegisterBuiltinMacro(*this, "__BASE_FILE__");
357
93.7k
  Ident__INCLUDE_LEVEL__ = RegisterBuiltinMacro(*this, "__INCLUDE_LEVEL__");
358
93.7k
  Ident__TIMESTAMP__     = RegisterBuiltinMacro(*this, "__TIMESTAMP__");
359
360
  // Microsoft Extensions.
361
93.7k
  if (getLangOpts().MicrosoftExt) {
362
11.8k
    Ident__identifier = RegisterBuiltinMacro(*this, "__identifier");
363
11.8k
    Ident__pragma = RegisterBuiltinMacro(*this, "__pragma");
364
81.8k
  } else {
365
81.8k
    Ident__identifier = nullptr;
366
81.8k
    Ident__pragma = nullptr;
367
81.8k
  }
368
369
  // Clang Extensions.
370
93.7k
  Ident__FILE_NAME__      = RegisterBuiltinMacro(*this, "__FILE_NAME__");
371
93.7k
  Ident__has_feature      = RegisterBuiltinMacro(*this, "__has_feature");
372
93.7k
  Ident__has_extension    = RegisterBuiltinMacro(*this, "__has_extension");
373
93.7k
  Ident__has_builtin      = RegisterBuiltinMacro(*this, "__has_builtin");
374
93.7k
  Ident__has_constexpr_builtin =
375
93.7k
      RegisterBuiltinMacro(*this, "__has_constexpr_builtin");
376
93.7k
  Ident__has_attribute    = RegisterBuiltinMacro(*this, "__has_attribute");
377
93.7k
  if (!getLangOpts().CPlusPlus)
378
23.1k
    Ident__has_c_attribute = RegisterBuiltinMacro(*this, "__has_c_attribute");
379
70.5k
  else
380
70.5k
    Ident__has_c_attribute = nullptr;
381
382
93.7k
  Ident__has_declspec = RegisterBuiltinMacro(*this, "__has_declspec_attribute");
383
93.7k
  Ident__has_include      = RegisterBuiltinMacro(*this, "__has_include");
384
93.7k
  Ident__has_include_next = RegisterBuiltinMacro(*this, "__has_include_next");
385
93.7k
  Ident__has_warning      = RegisterBuiltinMacro(*this, "__has_warning");
386
93.7k
  Ident__is_identifier    = RegisterBuiltinMacro(*this, "__is_identifier");
387
93.7k
  Ident__is_target_arch   = RegisterBuiltinMacro(*this, "__is_target_arch");
388
93.7k
  Ident__is_target_vendor = RegisterBuiltinMacro(*this, "__is_target_vendor");
389
93.7k
  Ident__is_target_os     = RegisterBuiltinMacro(*this, "__is_target_os");
390
93.7k
  Ident__is_target_environment =
391
93.7k
      RegisterBuiltinMacro(*this, "__is_target_environment");
392
93.7k
  Ident__is_target_variant_os =
393
93.7k
      RegisterBuiltinMacro(*this, "__is_target_variant_os");
394
93.7k
  Ident__is_target_variant_environment =
395
93.7k
      RegisterBuiltinMacro(*this, "__is_target_variant_environment");
396
397
  // Modules.
398
93.7k
  Ident__building_module  = RegisterBuiltinMacro(*this, "__building_module");
399
93.7k
  if (!getLangOpts().CurrentModule.empty())
400
3.34k
    Ident__MODULE__ = RegisterBuiltinMacro(*this, "__MODULE__");
401
90.3k
  else
402
90.3k
    Ident__MODULE__ = nullptr;
403
93.7k
}
404
405
/// isTrivialSingleTokenExpansion - Return true if MI, which has a single token
406
/// in its expansion, currently expands to that token literally.
407
static bool isTrivialSingleTokenExpansion(const MacroInfo *MI,
408
                                          const IdentifierInfo *MacroIdent,
409
10.8M
                                          Preprocessor &PP) {
410
10.8M
  IdentifierInfo *II = MI->getReplacementToken(0).getIdentifierInfo();
411
412
  // If the token isn't an identifier, it's always literally expanded.
413
10.8M
  if (!II) 
return true3.51M
;
414
415
  // If the information about this identifier is out of date, update it from
416
  // the external source.
417
7.35M
  if (II->isOutOfDate())
418
277
    PP.getExternalSource()->updateOutOfDateIdentifier(*II);
419
420
  // If the identifier is a macro, and if that macro is enabled, it may be
421
  // expanded so it's not a trivial expansion.
422
7.35M
  if (auto *ExpansionMI = PP.getMacroInfo(II))
423
1.47M
    if (ExpansionMI->isEnabled() &&
424
        // Fast expanding "#define X X" is ok, because X would be disabled.
425
1.47M
        
II != MacroIdent1.47M
)
426
1.47M
      return false;
427
428
  // If this is an object-like macro invocation, it is safe to trivially expand
429
  // it.
430
5.88M
  if (MI->isObjectLike()) 
return true3.22M
;
431
432
  // If this is a function-like macro invocation, it's safe to trivially expand
433
  // as long as the identifier is not a macro argument.
434
2.66M
  return !llvm::is_contained(MI->params(), II);
435
5.88M
}
436
437
/// isNextPPTokenLParen - Determine whether the next preprocessor token to be
438
/// lexed is a '('.  If so, consume the token and return true, if not, this
439
/// method should have no observable side-effect on the lexed tokens.
440
30.9M
bool Preprocessor::isNextPPTokenLParen() {
441
  // Do some quick tests for rejection cases.
442
30.9M
  unsigned Val;
443
30.9M
  if (CurLexer)
444
3.13M
    Val = CurLexer->isNextPPTokenLParen();
445
27.8M
  else
446
27.8M
    Val = CurTokenLexer->isNextTokenLParen();
447
448
30.9M
  if (Val == 2) {
449
    // We have run off the end.  If it's a source file we don't
450
    // examine enclosing ones (C99 5.1.1.2p4).  Otherwise walk up the
451
    // macro stack.
452
2.36M
    if (CurPPLexer)
453
1
      return false;
454
2.36M
    
for (const IncludeStackInfo &Entry : llvm::reverse(IncludeMacroStack))2.36M
{
455
2.36M
      if (Entry.TheLexer)
456
501
        Val = Entry.TheLexer->isNextPPTokenLParen();
457
2.36M
      else
458
2.36M
        Val = Entry.TheTokenLexer->isNextTokenLParen();
459
460
2.36M
      if (Val != 2)
461
2.36M
        break;
462
463
      // Ran off the end of a source file?
464
12
      if (Entry.ThePPLexer)
465
2
        return false;
466
12
    }
467
2.36M
  }
468
469
  // Okay, if we know that the token is a '(', lex it and return.  Otherwise we
470
  // have found something that isn't a '(' or we found the end of the
471
  // translation unit.  In either case, return false.
472
30.9M
  return Val == 1;
473
30.9M
}
474
475
/// HandleMacroExpandedIdentifier - If an identifier token is read that is to be
476
/// expanded as a macro, handle it and return the next token as 'Identifier'.
477
bool Preprocessor::HandleMacroExpandedIdentifier(Token &Identifier,
478
79.3M
                                                 const MacroDefinition &M) {
479
79.3M
  emitMacroExpansionWarnings(Identifier);
480
481
79.3M
  MacroInfo *MI = M.getMacroInfo();
482
483
  // If this is a macro expansion in the "#if !defined(x)" line for the file,
484
  // then the macro could expand to different things in other contexts, we need
485
  // to disable the optimization in this case.
486
79.3M
  if (CurPPLexer) 
CurPPLexer->MIOpt.ExpandedMacro()41.4M
;
487
488
  // If this is a builtin macro, like __LINE__ or _Pragma, handle it specially.
489
79.3M
  if (MI->isBuiltinMacro()) {
490
1.44M
    if (Callbacks)
491
1.44M
      Callbacks->MacroExpands(Identifier, M, Identifier.getLocation(),
492
1.44M
                              /*Args=*/nullptr);
493
1.44M
    ExpandBuiltinMacro(Identifier);
494
1.44M
    return true;
495
1.44M
  }
496
497
  /// Args - If this is a function-like macro expansion, this contains,
498
  /// for each macro argument, the list of tokens that were provided to the
499
  /// invocation.
500
77.8M
  MacroArgs *Args = nullptr;
501
502
  // Remember where the end of the expansion occurred.  For an object-like
503
  // macro, this is the identifier.  For a function-like macro, this is the ')'.
504
77.8M
  SourceLocation ExpansionEnd = Identifier.getLocation();
505
506
  // If this is a function-like macro, read the arguments.
507
77.8M
  if (MI->isFunctionLike()) {
508
    // Remember that we are now parsing the arguments to a macro invocation.
509
    // Preprocessor directives used inside macro arguments are not portable, and
510
    // this enables the warning.
511
28.6M
    InMacroArgs = true;
512
28.6M
    ArgMacro = &Identifier;
513
514
28.6M
    Args = ReadMacroCallArgumentList(Identifier, MI, ExpansionEnd);
515
516
    // Finished parsing args.
517
28.6M
    InMacroArgs = false;
518
28.6M
    ArgMacro = nullptr;
519
520
    // If there was an error parsing the arguments, bail out.
521
28.6M
    if (!Args) 
return true54
;
522
523
28.6M
    ++NumFnMacroExpanded;
524
49.2M
  } else {
525
49.2M
    ++NumMacroExpanded;
526
49.2M
  }
527
528
  // Notice that this macro has been used.
529
77.8M
  markMacroAsUsed(MI);
530
531
  // Remember where the token is expanded.
532
77.8M
  SourceLocation ExpandLoc = Identifier.getLocation();
533
77.8M
  SourceRange ExpansionRange(ExpandLoc, ExpansionEnd);
534
535
77.8M
  if (Callbacks) {
536
77.8M
    if (InMacroArgs) {
537
      // We can have macro expansion inside a conditional directive while
538
      // reading the function macro arguments. To ensure, in that case, that
539
      // MacroExpands callbacks still happen in source order, queue this
540
      // callback to have it happen after the function macro callback.
541
6
      DelayedMacroExpandsCallbacks.push_back(
542
6
          MacroExpandsInfo(Identifier, M, ExpansionRange));
543
77.8M
    } else {
544
77.8M
      Callbacks->MacroExpands(Identifier, M, ExpansionRange, Args);
545
77.8M
      if (!DelayedMacroExpandsCallbacks.empty()) {
546
4
        for (const MacroExpandsInfo &Info : DelayedMacroExpandsCallbacks) {
547
          // FIXME: We lose macro args info with delayed callback.
548
4
          Callbacks->MacroExpands(Info.Tok, Info.MD, Info.Range,
549
4
                                  /*Args=*/nullptr);
550
4
        }
551
3
        DelayedMacroExpandsCallbacks.clear();
552
3
      }
553
77.8M
    }
554
77.8M
  }
555
556
  // If the macro definition is ambiguous, complain.
557
77.8M
  if (M.isAmbiguous()) {
558
47
    Diag(Identifier, diag::warn_pp_ambiguous_macro)
559
47
      << Identifier.getIdentifierInfo();
560
47
    Diag(MI->getDefinitionLoc(), diag::note_pp_ambiguous_macro_chosen)
561
47
      << Identifier.getIdentifierInfo();
562
94
    M.forAllDefinitions([&](const MacroInfo *OtherMI) {
563
94
      if (OtherMI != MI)
564
47
        Diag(OtherMI->getDefinitionLoc(), diag::note_pp_ambiguous_macro_other)
565
47
          << Identifier.getIdentifierInfo();
566
94
    });
567
47
  }
568
569
  // If we started lexing a macro, enter the macro expansion body.
570
571
  // If this macro expands to no tokens, don't bother to push it onto the
572
  // expansion stack, only to take it right back off.
573
77.8M
  if (MI->getNumTokens() == 0) {
574
    // No need for arg info.
575
1.54M
    if (Args) 
Args->destroy(*this)16.3k
;
576
577
    // Propagate whitespace info as if we had pushed, then popped,
578
    // a macro context.
579
1.54M
    Identifier.setFlag(Token::LeadingEmptyMacro);
580
1.54M
    PropagateLineStartLeadingSpaceInfo(Identifier);
581
1.54M
    ++NumFastMacroExpanded;
582
1.54M
    return false;
583
76.3M
  } else if (MI->getNumTokens() == 1 &&
584
76.3M
             isTrivialSingleTokenExpansion(MI, Identifier.getIdentifierInfo(),
585
10.8M
                                           *this)) {
586
    // Otherwise, if this macro expands into a single trivially-expanded
587
    // token: expand it now.  This handles common cases like
588
    // "#define VAL 42".
589
590
    // No need for arg info.
591
6.73M
    if (Args) 
Args->destroy(*this)97
;
592
593
    // Propagate the isAtStartOfLine/hasLeadingSpace markers of the macro
594
    // identifier to the expanded token.
595
6.73M
    bool isAtStartOfLine = Identifier.isAtStartOfLine();
596
6.73M
    bool hasLeadingSpace = Identifier.hasLeadingSpace();
597
598
    // Replace the result token.
599
6.73M
    Identifier = MI->getReplacementToken(0);
600
601
    // Restore the StartOfLine/LeadingSpace markers.
602
6.73M
    Identifier.setFlagValue(Token::StartOfLine , isAtStartOfLine);
603
6.73M
    Identifier.setFlagValue(Token::LeadingSpace, hasLeadingSpace);
604
605
    // Update the tokens location to include both its expansion and physical
606
    // locations.
607
6.73M
    SourceLocation Loc =
608
6.73M
      SourceMgr.createExpansionLoc(Identifier.getLocation(), ExpandLoc,
609
6.73M
                                   ExpansionEnd,Identifier.getLength());
610
6.73M
    Identifier.setLocation(Loc);
611
612
    // If this is a disabled macro or #define X X, we must mark the result as
613
    // unexpandable.
614
6.73M
    if (IdentifierInfo *NewII = Identifier.getIdentifierInfo()) {
615
3.22M
      if (MacroInfo *NewMI = getMacroInfo(NewII))
616
3.08k
        if (!NewMI->isEnabled() || 
NewMI == MI3.07k
) {
617
3.08k
          Identifier.setFlag(Token::DisableExpand);
618
          // Don't warn for "#define X X" like "#define bool bool" from
619
          // stdbool.h.
620
3.08k
          if (NewMI != MI || 
MI->isFunctionLike()3.07k
)
621
6
            Diag(Identifier, diag::pp_disabled_macro_expansion);
622
3.08k
        }
623
3.22M
    }
624
625
    // Since this is not an identifier token, it can't be macro expanded, so
626
    // we're done.
627
6.73M
    ++NumFastMacroExpanded;
628
6.73M
    return true;
629
6.73M
  }
630
631
  // Start expanding the macro.
632
69.6M
  EnterMacro(Identifier, ExpansionEnd, MI, Args);
633
69.6M
  return false;
634
77.8M
}
635
636
enum Bracket {
637
  Brace,
638
  Paren
639
};
640
641
/// CheckMatchedBrackets - Returns true if the braces and parentheses in the
642
/// token vector are properly nested.
643
48
static bool CheckMatchedBrackets(const SmallVectorImpl<Token> &Tokens) {
644
48
  SmallVector<Bracket, 8> Brackets;
645
48
  for (SmallVectorImpl<Token>::const_iterator I = Tokens.begin(),
646
48
                                              E = Tokens.end();
647
1.01k
       I != E; 
++I969
) {
648
969
    if (I->is(tok::l_paren)) {
649
4
      Brackets.push_back(Paren);
650
965
    } else if (I->is(tok::r_paren)) {
651
4
      if (Brackets.empty() || Brackets.back() == Brace)
652
0
        return false;
653
4
      Brackets.pop_back();
654
961
    } else if (I->is(tok::l_brace)) {
655
101
      Brackets.push_back(Brace);
656
860
    } else if (I->is(tok::r_brace)) {
657
101
      if (Brackets.empty() || Brackets.back() == Paren)
658
0
        return false;
659
101
      Brackets.pop_back();
660
101
    }
661
969
  }
662
48
  return Brackets.empty();
663
48
}
664
665
/// GenerateNewArgTokens - Returns true if OldTokens can be converted to a new
666
/// vector of tokens in NewTokens.  The new number of arguments will be placed
667
/// in NumArgs and the ranges which need to surrounded in parentheses will be
668
/// in ParenHints.
669
/// Returns false if the token stream cannot be changed.  If this is because
670
/// of an initializer list starting a macro argument, the range of those
671
/// initializer lists will be place in InitLists.
672
static bool GenerateNewArgTokens(Preprocessor &PP,
673
                                 SmallVectorImpl<Token> &OldTokens,
674
                                 SmallVectorImpl<Token> &NewTokens,
675
                                 unsigned &NumArgs,
676
                                 SmallVectorImpl<SourceRange> &ParenHints,
677
48
                                 SmallVectorImpl<SourceRange> &InitLists) {
678
48
  if (!CheckMatchedBrackets(OldTokens))
679
0
    return false;
680
681
  // Once it is known that the brackets are matched, only a simple count of the
682
  // braces is needed.
683
48
  unsigned Braces = 0;
684
685
  // First token of a new macro argument.
686
48
  SmallVectorImpl<Token>::iterator ArgStartIterator = OldTokens.begin();
687
688
  // First closing brace in a new macro argument.  Used to generate
689
  // SourceRanges for InitLists.
690
48
  SmallVectorImpl<Token>::iterator ClosingBrace = OldTokens.end();
691
48
  NumArgs = 0;
692
48
  Token TempToken;
693
  // Set to true when a macro separator token is found inside a braced list.
694
  // If true, the fixed argument spans multiple old arguments and ParenHints
695
  // will be updated.
696
48
  bool FoundSeparatorToken = false;
697
48
  for (SmallVectorImpl<Token>::iterator I = OldTokens.begin(),
698
48
                                        E = OldTokens.end();
699
1.01k
       I != E; 
++I969
) {
700
969
    if (I->is(tok::l_brace)) {
701
101
      ++Braces;
702
868
    } else if (I->is(tok::r_brace)) {
703
101
      --Braces;
704
101
      if (Braces == 0 && ClosingBrace == E && 
FoundSeparatorToken65
)
705
61
        ClosingBrace = I;
706
767
    } else if (I->is(tok::eof)) {
707
      // EOF token is used to separate macro arguments
708
326
      if (Braces != 0) {
709
        // Assume comma separator is actually braced list separator and change
710
        // it back to a comma.
711
189
        FoundSeparatorToken = true;
712
189
        I->setKind(tok::comma);
713
189
        I->setLength(1);
714
189
      } else { // Braces == 0
715
        // Separator token still separates arguments.
716
137
        ++NumArgs;
717
718
        // If the argument starts with a brace, it can't be fixed with
719
        // parentheses.  A different diagnostic will be given.
720
137
        if (FoundSeparatorToken && 
ArgStartIterator->is(tok::l_brace)87
) {
721
28
          InitLists.push_back(
722
28
              SourceRange(ArgStartIterator->getLocation(),
723
28
                          PP.getLocForEndOfToken(ClosingBrace->getLocation())));
724
28
          ClosingBrace = E;
725
28
        }
726
727
        // Add left paren
728
137
        if (FoundSeparatorToken) {
729
87
          TempToken.startToken();
730
87
          TempToken.setKind(tok::l_paren);
731
87
          TempToken.setLocation(ArgStartIterator->getLocation());
732
87
          TempToken.setLength(0);
733
87
          NewTokens.push_back(TempToken);
734
87
        }
735
736
        // Copy over argument tokens
737
137
        NewTokens.insert(NewTokens.end(), ArgStartIterator, I);
738
739
        // Add right paren and store the paren locations in ParenHints
740
137
        if (FoundSeparatorToken) {
741
87
          SourceLocation Loc = PP.getLocForEndOfToken((I - 1)->getLocation());
742
87
          TempToken.startToken();
743
87
          TempToken.setKind(tok::r_paren);
744
87
          TempToken.setLocation(Loc);
745
87
          TempToken.setLength(0);
746
87
          NewTokens.push_back(TempToken);
747
87
          ParenHints.push_back(SourceRange(ArgStartIterator->getLocation(),
748
87
                                           Loc));
749
87
        }
750
751
        // Copy separator token
752
137
        NewTokens.push_back(*I);
753
754
        // Reset values
755
137
        ArgStartIterator = I + 1;
756
137
        FoundSeparatorToken = false;
757
137
      }
758
326
    }
759
969
  }
760
761
48
  return !ParenHints.empty() && 
InitLists.empty()39
;
762
48
}
763
764
/// ReadFunctionLikeMacroArgs - After reading "MACRO" and knowing that the next
765
/// token is the '(' of the macro, this method is invoked to read all of the
766
/// actual arguments specified for the macro invocation.  This returns null on
767
/// error.
768
MacroArgs *Preprocessor::ReadMacroCallArgumentList(Token &MacroName,
769
                                                   MacroInfo *MI,
770
28.6M
                                                   SourceLocation &MacroEnd) {
771
  // The number of fixed arguments to parse.
772
28.6M
  unsigned NumFixedArgsLeft = MI->getNumParams();
773
28.6M
  bool isVariadic = MI->isVariadic();
774
775
  // Outer loop, while there are more arguments, keep reading them.
776
28.6M
  Token Tok;
777
778
  // Read arguments as unexpanded tokens.  This avoids issues, e.g., where
779
  // an argument value in a macro could expand to ',' or '(' or ')'.
780
28.6M
  LexUnexpandedToken(Tok);
781
28.6M
  assert(Tok.is(tok::l_paren) && "Error computing l-paren-ness?");
782
783
  // ArgTokens - Build up a list of tokens that make up each argument.  Each
784
  // argument is separated by an EOF token.  Use a SmallVector so we can avoid
785
  // heap allocations in the common case.
786
28.6M
  SmallVector<Token, 64> ArgTokens;
787
28.6M
  bool ContainsCodeCompletionTok = false;
788
28.6M
  bool FoundElidedComma = false;
789
790
28.6M
  SourceLocation TooManyArgsLoc;
791
792
28.6M
  unsigned NumActuals = 0;
793
86.6M
  while (Tok.isNot(tok::r_paren)) {
794
58.3M
    if (ContainsCodeCompletionTok && 
Tok.isOneOf(tok::eof, tok::eod)16
)
795
9
      break;
796
797
58.3M
    assert(Tok.isOneOf(tok::l_paren, tok::comma) &&
798
58.3M
           "only expect argument separators here");
799
800
58.3M
    size_t ArgTokenStart = ArgTokens.size();
801
58.3M
    SourceLocation ArgStartLoc = Tok.getLocation();
802
803
    // C99 6.10.3p11: Keep track of the number of l_parens we have seen.  Note
804
    // that we already consumed the first one.
805
58.3M
    unsigned NumParens = 0;
806
807
191M
    while (true) {
808
      // Read arguments as unexpanded tokens.  This avoids issues, e.g., where
809
      // an argument value in a macro could expand to ',' or '(' or ')'.
810
191M
      LexUnexpandedToken(Tok);
811
812
191M
      if (Tok.isOneOf(tok::eof, tok::eod)) { // "#if f(<eof>" & "#if f(\n"
813
27
        if (!ContainsCodeCompletionTok) {
814
18
          Diag(MacroName, diag::err_unterm_macro_invoc);
815
18
          Diag(MI->getDefinitionLoc(), diag::note_macro_here)
816
18
            << MacroName.getIdentifierInfo();
817
          // Do not lose the EOF/EOD.  Return it to the client.
818
18
          MacroName = Tok;
819
18
          return nullptr;
820
18
        }
821
        // Do not lose the EOF/EOD.
822
9
        auto Toks = std::make_unique<Token[]>(1);
823
9
        Toks[0] = Tok;
824
9
        EnterTokenStream(std::move(Toks), 1, true, /*IsReinject*/ false);
825
9
        break;
826
191M
      } else if (Tok.is(tok::r_paren)) {
827
        // If we found the ) token, the macro arg list is done.
828
45.4M
        if (NumParens-- == 0) {
829
28.6M
          MacroEnd = Tok.getLocation();
830
28.6M
          if (!ArgTokens.empty() &&
831
28.6M
              
ArgTokens.back().commaAfterElided()28.2M
) {
832
1.99k
            FoundElidedComma = true;
833
1.99k
          }
834
28.6M
          break;
835
28.6M
        }
836
145M
      } else if (Tok.is(tok::l_paren)) {
837
16.8M
        ++NumParens;
838
129M
      } else if (Tok.is(tok::comma)) {
839
        // In Microsoft-compatibility mode, single commas from nested macro
840
        // expansions should not be considered as argument separators. We test
841
        // for this with the IgnoredComma token flag.
842
40.9M
        if (Tok.getFlags() & Token::IgnoredComma) {
843
          // However, in MSVC's preprocessor, subsequent expansions do treat
844
          // these commas as argument separators. This leads to a common
845
          // workaround used in macros that need to work in both MSVC and
846
          // compliant preprocessors. Therefore, the IgnoredComma flag can only
847
          // apply once to any given token.
848
3
          Tok.clearFlag(Token::IgnoredComma);
849
40.9M
        } else if (NumParens == 0) {
850
          // Comma ends this argument if there are more fixed arguments
851
          // expected. However, if this is a variadic macro, and this is part of
852
          // the variadic part, then the comma is just an argument token.
853
37.3M
          if (!isVariadic)
854
12.3M
            break;
855
24.9M
          if (NumFixedArgsLeft > 1)
856
17.3M
            break;
857
24.9M
        }
858
88.0M
      } else if (Tok.is(tok::comment) && 
!KeepMacroComments0
) {
859
        // If this is a comment token in the argument list and we're just in
860
        // -C mode (not -CC mode), discard the comment.
861
0
        continue;
862
88.0M
      } else if (!Tok.isAnnotation() && 
Tok.getIdentifierInfo() != nullptr88.0M
) {
863
        // Reading macro arguments can cause macros that we are currently
864
        // expanding from to be popped off the expansion stack.  Doing so causes
865
        // them to be reenabled for expansion.  Here we record whether any
866
        // identifiers we lex as macro arguments correspond to disabled macros.
867
        // If so, we mark the token as noexpand.  This is a subtle aspect of
868
        // C99 6.10.3.4p2.
869
51.9M
        if (MacroInfo *MI = getMacroInfo(Tok.getIdentifierInfo()))
870
20.5M
          if (!MI->isEnabled())
871
1.87k
            Tok.setFlag(Token::DisableExpand);
872
51.9M
      } else 
if (36.1M
Tok.is(tok::code_completion)36.1M
) {
873
25
        ContainsCodeCompletionTok = true;
874
25
        if (CodeComplete)
875
25
          CodeComplete->CodeCompleteMacroArgument(MacroName.getIdentifierInfo(),
876
25
                                                  MI, NumActuals);
877
        // Don't mark that we reached the code-completion point because the
878
        // parser is going to handle the token and there will be another
879
        // code-completion callback.
880
25
      }
881
882
132M
      ArgTokens.push_back(Tok);
883
132M
    }
884
885
    // If this was an empty argument list foo(), don't add this as an empty
886
    // argument.
887
58.3M
    if (ArgTokens.empty() && 
Tok.getKind() == tok::r_paren353k
)
888
353k
      break;
889
890
    // If this is not a variadic macro, and too many args were specified, emit
891
    // an error.
892
58.0M
    if (!isVariadic && 
NumFixedArgsLeft == 035.5M
&&
TooManyArgsLoc.isInvalid()200
) {
893
48
      if (ArgTokens.size() != ArgTokenStart)
894
46
        TooManyArgsLoc = ArgTokens[ArgTokenStart].getLocation();
895
2
      else
896
2
        TooManyArgsLoc = ArgStartLoc;
897
48
    }
898
899
    // Empty arguments are standard in C99 and C++0x, and are supported as an
900
    // extension in other modes.
901
58.0M
    if (ArgTokens.size() == ArgTokenStart && 
!getLangOpts().C9984.9k
)
902
30.2k
      Diag(Tok, getLangOpts().CPlusPlus11
903
30.2k
                    ? 
diag::warn_cxx98_compat_empty_fnmacro_arg30.2k
904
30.2k
                    : 
diag::ext_empty_fnmacro_arg22
);
905
906
    // Add a marker EOF token to the end of the token list for this argument.
907
58.0M
    Token EOFTok;
908
58.0M
    EOFTok.startToken();
909
58.0M
    EOFTok.setKind(tok::eof);
910
58.0M
    EOFTok.setLocation(Tok.getLocation());
911
58.0M
    EOFTok.setLength(0);
912
58.0M
    ArgTokens.push_back(EOFTok);
913
58.0M
    ++NumActuals;
914
58.0M
    if (!ContainsCodeCompletionTok && 
NumFixedArgsLeft != 058.0M
)
915
58.0M
      --NumFixedArgsLeft;
916
58.0M
  }
917
918
  // Okay, we either found the r_paren.  Check to see if we parsed too few
919
  // arguments.
920
28.6M
  unsigned MinArgsExpected = MI->getNumParams();
921
922
  // If this is not a variadic macro, and too many args were specified, emit
923
  // an error.
924
28.6M
  if (!isVariadic && 
NumActuals > MinArgsExpected23.5M
&&
925
28.6M
      
!ContainsCodeCompletionTok49
) {
926
    // Emit the diagnostic at the macro name in case there is a missing ).
927
    // Emitting it at the , could be far away from the macro name.
928
48
    Diag(TooManyArgsLoc, diag::err_too_many_args_in_macro_invoc);
929
48
    Diag(MI->getDefinitionLoc(), diag::note_macro_here)
930
48
      << MacroName.getIdentifierInfo();
931
932
    // Commas from braced initializer lists will be treated as argument
933
    // separators inside macros.  Attempt to correct for this with parentheses.
934
    // TODO: See if this can be generalized to angle brackets for templates
935
    // inside macro arguments.
936
937
48
    SmallVector<Token, 4> FixedArgTokens;
938
48
    unsigned FixedNumArgs = 0;
939
48
    SmallVector<SourceRange, 4> ParenHints, InitLists;
940
48
    if (!GenerateNewArgTokens(*this, ArgTokens, FixedArgTokens, FixedNumArgs,
941
48
                              ParenHints, InitLists)) {
942
15
      if (!InitLists.empty()) {
943
6
        DiagnosticBuilder DB =
944
6
            Diag(MacroName,
945
6
                 diag::note_init_list_at_beginning_of_macro_argument);
946
6
        for (SourceRange Range : InitLists)
947
28
          DB << Range;
948
6
      }
949
15
      return nullptr;
950
15
    }
951
33
    if (FixedNumArgs != MinArgsExpected)
952
0
      return nullptr;
953
954
33
    DiagnosticBuilder DB = Diag(MacroName, diag::note_suggest_parens_for_macro);
955
59
    for (SourceRange ParenLocation : ParenHints) {
956
59
      DB << FixItHint::CreateInsertion(ParenLocation.getBegin(), "(");
957
59
      DB << FixItHint::CreateInsertion(ParenLocation.getEnd(), ")");
958
59
    }
959
33
    ArgTokens.swap(FixedArgTokens);
960
33
    NumActuals = FixedNumArgs;
961
33
  }
962
963
  // See MacroArgs instance var for description of this.
964
28.6M
  bool isVarargsElided = false;
965
966
28.6M
  if (ContainsCodeCompletionTok) {
967
    // Recover from not-fully-formed macro invocation during code-completion.
968
25
    Token EOFTok;
969
25
    EOFTok.startToken();
970
25
    EOFTok.setKind(tok::eof);
971
25
    EOFTok.setLocation(Tok.getLocation());
972
25
    EOFTok.setLength(0);
973
35
    for (; NumActuals < MinArgsExpected; 
++NumActuals10
)
974
10
      ArgTokens.push_back(EOFTok);
975
25
  }
976
977
28.6M
  if (NumActuals < MinArgsExpected) {
978
    // There are several cases where too few arguments is ok, handle them now.
979
460k
    if (NumActuals == 0 && 
MinArgsExpected == 1350k
) {
980
      // #define A(X)  or  #define A(...)   ---> A()
981
982
      // If there is exactly one argument, and that argument is missing,
983
      // then we have an empty "()" argument empty list.  This is fine, even if
984
      // the macro expects one argument (the argument is just empty).
985
350k
      isVarargsElided = MI->isVariadic();
986
350k
    } else 
if (110k
(110k
FoundElidedComma110k
||
MI->isVariadic()110k
) &&
987
110k
               
(110k
NumActuals+1 == MinArgsExpected110k
|| // A(x, ...) -> A(X)
988
110k
                
(30
NumActuals == 030
&&
MinArgsExpected == 230
))) {// A(x,...) -> A()
989
      // Varargs where the named vararg parameter is missing: OK as extension.
990
      //   #define A(x, ...)
991
      //   A("blah")
992
      //
993
      // If the macro contains the comma pasting extension, the diagnostic
994
      // is suppressed; we know we'll get another diagnostic later.
995
110k
      if (!MI->hasCommaPasting()) {
996
        // C++20 allows this construct, but standards before C++20 and all C
997
        // standards do not allow the construct (we allow it as an extension).
998
100k
        Diag(Tok, getLangOpts().CPlusPlus20
999
100k
                      ? 
diag::warn_cxx17_compat_missing_varargs_arg15
1000
100k
                      : 
diag::ext_missing_varargs_arg100k
);
1001
100k
        Diag(MI->getDefinitionLoc(), diag::note_macro_here)
1002
100k
          << MacroName.getIdentifierInfo();
1003
100k
      }
1004
1005
      // Remember this occurred, allowing us to elide the comma when used for
1006
      // cases like:
1007
      //   #define A(x, foo...) blah(a, ## foo)
1008
      //   #define B(x, ...) blah(a, ## __VA_ARGS__)
1009
      //   #define C(...) blah(a, ## __VA_ARGS__)
1010
      //  A(x) B(x) C()
1011
110k
      isVarargsElided = true;
1012
110k
    } else 
if (21
!ContainsCodeCompletionTok21
) {
1013
      // Otherwise, emit the error.
1014
21
      Diag(Tok, diag::err_too_few_args_in_macro_invoc);
1015
21
      Diag(MI->getDefinitionLoc(), diag::note_macro_here)
1016
21
        << MacroName.getIdentifierInfo();
1017
21
      return nullptr;
1018
21
    }
1019
1020
    // Add a marker EOF token to the end of the token list for this argument.
1021
460k
    SourceLocation EndLoc = Tok.getLocation();
1022
460k
    Tok.startToken();
1023
460k
    Tok.setKind(tok::eof);
1024
460k
    Tok.setLocation(EndLoc);
1025
460k
    Tok.setLength(0);
1026
460k
    ArgTokens.push_back(Tok);
1027
1028
    // If we expect two arguments, add both as empty.
1029
460k
    if (NumActuals == 0 && 
MinArgsExpected == 2350k
)
1030
30
      ArgTokens.push_back(Tok);
1031
1032
28.1M
  } else if (NumActuals > MinArgsExpected && 
!MI->isVariadic()1
&&
1033
28.1M
             
!ContainsCodeCompletionTok1
) {
1034
    // Emit the diagnostic at the macro name in case there is a missing ).
1035
    // Emitting it at the , could be far away from the macro name.
1036
0
    Diag(MacroName, diag::err_too_many_args_in_macro_invoc);
1037
0
    Diag(MI->getDefinitionLoc(), diag::note_macro_here)
1038
0
      << MacroName.getIdentifierInfo();
1039
0
    return nullptr;
1040
0
  }
1041
1042
28.6M
  return MacroArgs::create(MI, ArgTokens, isVarargsElided, *this);
1043
28.6M
}
1044
1045
/// Keeps macro expanded tokens for TokenLexers.
1046
//
1047
/// Works like a stack; a TokenLexer adds the macro expanded tokens that is
1048
/// going to lex in the cache and when it finishes the tokens are removed
1049
/// from the end of the cache.
1050
Token *Preprocessor::cacheMacroExpandedTokens(TokenLexer *tokLexer,
1051
28.5M
                                              ArrayRef<Token> tokens) {
1052
28.5M
  assert(tokLexer);
1053
28.5M
  if (tokens.empty())
1054
262k
    return nullptr;
1055
1056
28.2M
  size_t newIndex = MacroExpandedTokens.size();
1057
28.2M
  bool cacheNeedsToGrow = tokens.size() >
1058
28.2M
                      MacroExpandedTokens.capacity()-MacroExpandedTokens.size();
1059
28.2M
  MacroExpandedTokens.append(tokens.begin(), tokens.end());
1060
1061
28.2M
  if (cacheNeedsToGrow) {
1062
    // Go through all the TokenLexers whose 'Tokens' pointer points in the
1063
    // buffer and update the pointers to the (potential) new buffer array.
1064
13.6k
    for (const auto &Lexer : MacroExpandingLexersStack) {
1065
12.4k
      TokenLexer *prevLexer;
1066
12.4k
      size_t tokIndex;
1067
12.4k
      std::tie(prevLexer, tokIndex) = Lexer;
1068
12.4k
      prevLexer->Tokens = MacroExpandedTokens.data() + tokIndex;
1069
12.4k
    }
1070
13.6k
  }
1071
1072
28.2M
  MacroExpandingLexersStack.push_back(std::make_pair(tokLexer, newIndex));
1073
28.2M
  return MacroExpandedTokens.data() + newIndex;
1074
28.5M
}
1075
1076
28.2M
void Preprocessor::removeCachedMacroExpandedTokensOfLastLexer() {
1077
28.2M
  assert(!MacroExpandingLexersStack.empty());
1078
28.2M
  size_t tokIndex = MacroExpandingLexersStack.back().second;
1079
28.2M
  assert(tokIndex < MacroExpandedTokens.size());
1080
  // Pop the cached macro expanded tokens from the end.
1081
28.2M
  MacroExpandedTokens.resize(tokIndex);
1082
28.2M
  MacroExpandingLexersStack.pop_back();
1083
28.2M
}
1084
1085
/// ComputeDATE_TIME - Compute the current time, enter it into the specified
1086
/// scratch buffer, then return DATELoc/TIMELoc locations with the position of
1087
/// the identifier tokens inserted.
1088
static void ComputeDATE_TIME(SourceLocation &DATELoc, SourceLocation &TIMELoc,
1089
10
                             Preprocessor &PP) {
1090
10
  time_t TT;
1091
10
  std::tm *TM;
1092
10
  if (PP.getPreprocessorOpts().SourceDateEpoch) {
1093
3
    TT = *PP.getPreprocessorOpts().SourceDateEpoch;
1094
3
    TM = std::gmtime(&TT);
1095
7
  } else {
1096
7
    TT = std::time(nullptr);
1097
7
    TM = std::localtime(&TT);
1098
7
  }
1099
1100
10
  static const char * const Months[] = {
1101
10
    "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
1102
10
  };
1103
1104
10
  {
1105
10
    SmallString<32> TmpBuffer;
1106
10
    llvm::raw_svector_ostream TmpStream(TmpBuffer);
1107
10
    if (TM)
1108
10
      TmpStream << llvm::format("\"%s %2d %4d\"", Months[TM->tm_mon],
1109
10
                                TM->tm_mday, TM->tm_year + 1900);
1110
0
    else
1111
0
      TmpStream << "??? ?? ????";
1112
10
    Token TmpTok;
1113
10
    TmpTok.startToken();
1114
10
    PP.CreateString(TmpStream.str(), TmpTok);
1115
10
    DATELoc = TmpTok.getLocation();
1116
10
  }
1117
1118
10
  {
1119
10
    SmallString<32> TmpBuffer;
1120
10
    llvm::raw_svector_ostream TmpStream(TmpBuffer);
1121
10
    if (TM)
1122
10
      TmpStream << llvm::format("\"%02d:%02d:%02d\"", TM->tm_hour, TM->tm_min,
1123
10
                                TM->tm_sec);
1124
0
    else
1125
0
      TmpStream << "??:??:??";
1126
10
    Token TmpTok;
1127
10
    TmpTok.startToken();
1128
10
    PP.CreateString(TmpStream.str(), TmpTok);
1129
10
    TIMELoc = TmpTok.getLocation();
1130
10
  }
1131
10
}
1132
1133
/// HasFeature - Return true if we recognize and implement the feature
1134
/// specified by the identifier as a standard language feature.
1135
530k
static bool HasFeature(const Preprocessor &PP, StringRef Feature) {
1136
530k
  const LangOptions &LangOpts = PP.getLangOpts();
1137
1138
  // Normalize the feature name, __foo__ becomes foo.
1139
530k
  if (Feature.startswith("__") && 
Feature.endswith("__")7
&&
Feature.size() >= 46
)
1140
6
    Feature = Feature.substr(2, Feature.size() - 4);
1141
1142
90.7M
#define FEATURE(Name, Predicate) .Case(#Name, 
Predicate4.66M
)
1143
530k
  return llvm::StringSwitch<bool>(Feature)
1144
530k
#include "clang/Basic/Features.def"
1145
530k
      .Default(false);
1146
530k
#undef FEATURE
1147
530k
}
1148
1149
/// HasExtension - Return true if we recognize and implement the feature
1150
/// specified by the identifier, either as an extension or a standard language
1151
/// feature.
1152
19.3k
static bool HasExtension(const Preprocessor &PP, StringRef Extension) {
1153
19.3k
  if (HasFeature(PP, Extension))
1154
10.5k
    return true;
1155
1156
  // If the use of an extension results in an error diagnostic, extensions are
1157
  // effectively unavailable, so just return false here.
1158
8.83k
  if (PP.getDiagnostics().getExtensionHandlingBehavior() >=
1159
8.83k
      diag::Severity::Error)
1160
7
    return false;
1161
1162
8.82k
  const LangOptions &LangOpts = PP.getLangOpts();
1163
1164
  // Normalize the extension name, __foo__ becomes foo.
1165
8.82k
  if (Extension.startswith("__") && 
Extension.endswith("__")1
&&
1166
8.82k
      
Extension.size() >= 41
)
1167
1
    Extension = Extension.substr(2, Extension.size() - 4);
1168
1169
    // Because we inherit the feature list from HasFeature, this string switch
1170
    // must be less restrictive than HasFeature's.
1171
317k
#define EXTENSION(Name, Predicate) .Case(#Name, Predicate)
1172
8.82k
  return llvm::StringSwitch<bool>(Extension)
1173
8.82k
#include "clang/Basic/Features.def"
1174
8.82k
      .Default(false);
1175
8.83k
#undef EXTENSION
1176
8.83k
}
1177
1178
/// EvaluateHasIncludeCommon - Process a '__has_include("path")'
1179
/// or '__has_include_next("path")' expression.
1180
/// Returns true if successful.
1181
static bool EvaluateHasIncludeCommon(Token &Tok, IdentifierInfo *II,
1182
                                     Preprocessor &PP,
1183
                                     ConstSearchDirIterator LookupFrom,
1184
24.8k
                                     const FileEntry *LookupFromFile) {
1185
  // Save the location of the current token.  If a '(' is later found, use
1186
  // that location.  If not, use the end of this location instead.
1187
24.8k
  SourceLocation LParenLoc = Tok.getLocation();
1188
1189
  // These expressions are only allowed within a preprocessor directive.
1190
24.8k
  if (!PP.isParsingIfOrElifDirective()) {
1191
8
    PP.Diag(LParenLoc, diag::err_pp_directive_required) << II;
1192
    // Return a valid identifier token.
1193
8
    assert(Tok.is(tok::identifier));
1194
8
    Tok.setIdentifierInfo(II);
1195
8
    return false;
1196
8
  }
1197
1198
  // Get '('. If we don't have a '(', try to form a header-name token.
1199
24.8k
  do {
1200
24.8k
    if (PP.LexHeaderName(Tok))
1201
0
      return false;
1202
24.8k
  } while (Tok.getKind() == tok::comment);
1203
1204
  // Ensure we have a '('.
1205
24.8k
  if (Tok.isNot(tok::l_paren)) {
1206
    // No '(', use end of last token.
1207
6
    LParenLoc = PP.getLocForEndOfToken(LParenLoc);
1208
6
    PP.Diag(LParenLoc, diag::err_pp_expected_after) << II << tok::l_paren;
1209
    // If the next token looks like a filename or the start of one,
1210
    // assume it is and process it as such.
1211
6
    if (Tok.isNot(tok::header_name))
1212
4
      return false;
1213
24.8k
  } else {
1214
    // Save '(' location for possible missing ')' message.
1215
24.8k
    LParenLoc = Tok.getLocation();
1216
24.8k
    if (PP.LexHeaderName(Tok))
1217
3
      return false;
1218
24.8k
  }
1219
1220
24.8k
  if (Tok.isNot(tok::header_name)) {
1221
9
    PP.Diag(Tok.getLocation(), diag::err_pp_expects_filename);
1222
9
    return false;
1223
9
  }
1224
1225
  // Reserve a buffer to get the spelling.
1226
24.8k
  SmallString<128> FilenameBuffer;
1227
24.8k
  bool Invalid = false;
1228
24.8k
  StringRef Filename = PP.getSpelling(Tok, FilenameBuffer, &Invalid);
1229
24.8k
  if (Invalid)
1230
0
    return false;
1231
1232
24.8k
  SourceLocation FilenameLoc = Tok.getLocation();
1233
1234
  // Get ')'.
1235
24.8k
  PP.LexNonComment(Tok);
1236
1237
  // Ensure we have a trailing ).
1238
24.8k
  if (Tok.isNot(tok::r_paren)) {
1239
2
    PP.Diag(PP.getLocForEndOfToken(FilenameLoc), diag::err_pp_expected_after)
1240
2
        << II << tok::r_paren;
1241
2
    PP.Diag(LParenLoc, diag::note_matching) << tok::l_paren;
1242
2
    return false;
1243
2
  }
1244
1245
24.8k
  bool isAngled = PP.GetIncludeFilenameSpelling(Tok.getLocation(), Filename);
1246
  // If GetIncludeFilenameSpelling set the start ptr to null, there was an
1247
  // error.
1248
24.8k
  if (Filename.empty())
1249
1
    return false;
1250
1251
  // Search include directories.
1252
24.8k
  OptionalFileEntryRef File =
1253
24.8k
      PP.LookupFile(FilenameLoc, Filename, isAngled, LookupFrom, LookupFromFile,
1254
24.8k
                    nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);
1255
1256
24.8k
  if (PPCallbacks *Callbacks = PP.getPPCallbacks()) {
1257
24.8k
    SrcMgr::CharacteristicKind FileType = SrcMgr::C_User;
1258
24.8k
    if (File)
1259
15.4k
      FileType =
1260
15.4k
          PP.getHeaderSearchInfo().getFileDirFlavor(&File->getFileEntry());
1261
24.8k
    Callbacks->HasInclude(FilenameLoc, Filename, isAngled, File, FileType);
1262
24.8k
  }
1263
1264
  // Get the result value.  A result of true means the file exists.
1265
24.8k
  return File.has_value();
1266
24.8k
}
1267
1268
12.8k
bool Preprocessor::EvaluateHasInclude(Token &Tok, IdentifierInfo *II) {
1269
12.8k
  return EvaluateHasIncludeCommon(Tok, II, *this, nullptr, nullptr);
1270
12.8k
}
1271
1272
12.0k
bool Preprocessor::EvaluateHasIncludeNext(Token &Tok, IdentifierInfo *II) {
1273
12.0k
  ConstSearchDirIterator Lookup = nullptr;
1274
12.0k
  const FileEntry *LookupFromFile;
1275
12.0k
  std::tie(Lookup, LookupFromFile) = getIncludeNextStart(Tok);
1276
1277
12.0k
  return EvaluateHasIncludeCommon(Tok, II, *this, Lookup, LookupFromFile);
1278
12.0k
}
1279
1280
/// Process single-argument builtin feature-like macros that return
1281
/// integer values.
1282
static void EvaluateFeatureLikeBuiltinMacro(llvm::raw_svector_ostream& OS,
1283
                                            Token &Tok, IdentifierInfo *II,
1284
                                            Preprocessor &PP, bool ExpandArgs,
1285
                                            llvm::function_ref<
1286
                                              int(Token &Tok,
1287
712k
                                                  bool &HasLexedNextTok)> Op) {
1288
  // Parse the initial '('.
1289
712k
  PP.LexUnexpandedToken(Tok);
1290
712k
  if (Tok.isNot(tok::l_paren)) {
1291
6
    PP.Diag(Tok.getLocation(), diag::err_pp_expected_after) << II
1292
6
                                                            << tok::l_paren;
1293
1294
    // Provide a dummy '0' value on output stream to elide further errors.
1295
6
    if (!Tok.isOneOf(tok::eof, tok::eod)) {
1296
2
      OS << 0;
1297
2
      Tok.setKind(tok::numeric_constant);
1298
2
    }
1299
6
    return;
1300
6
  }
1301
1302
712k
  unsigned ParenDepth = 1;
1303
712k
  SourceLocation LParenLoc = Tok.getLocation();
1304
712k
  std::optional<int> Result;
1305
1306
712k
  Token ResultTok;
1307
712k
  bool SuppressDiagnostic = false;
1308
1.41M
  while (true) {
1309
    // Parse next token.
1310
1.41M
    if (ExpandArgs)
1311
173k
      PP.Lex(Tok);
1312
1.24M
    else
1313
1.24M
      PP.LexUnexpandedToken(Tok);
1314
1315
1.42M
already_lexed:
1316
1.42M
    switch (Tok.getKind()) {
1317
1
      case tok::eof:
1318
4
      case tok::eod:
1319
        // Don't provide even a dummy value if the eod or eof marker is
1320
        // reached.  Simply provide a diagnostic.
1321
4
        PP.Diag(Tok.getLocation(), diag::err_unterm_macro_invoc);
1322
4
        return;
1323
1324
4
      case tok::comma:
1325
4
        if (!SuppressDiagnostic) {
1326
4
          PP.Diag(Tok.getLocation(), diag::err_too_many_args_in_macro_invoc);
1327
4
          SuppressDiagnostic = true;
1328
4
        }
1329
4
        continue;
1330
1331
6
      case tok::l_paren:
1332
6
        ++ParenDepth;
1333
6
        if (Result)
1334
2
          break;
1335
4
        if (!SuppressDiagnostic) {
1336
2
          PP.Diag(Tok.getLocation(), diag::err_pp_nested_paren) << II;
1337
2
          SuppressDiagnostic = true;
1338
2
        }
1339
4
        continue;
1340
1341
712k
      case tok::r_paren:
1342
712k
        if (--ParenDepth > 0)
1343
6
          continue;
1344
1345
        // The last ')' has been reached; return the value if one found or
1346
        // a diagnostic and a dummy value.
1347
712k
        if (Result) {
1348
712k
          OS << *Result;
1349
          // For strict conformance to __has_cpp_attribute rules, use 'L'
1350
          // suffix for dated literals.
1351
712k
          if (*Result > 1)
1352
7.51k
            OS << 'L';
1353
712k
        } else {
1354
5
          OS << 0;
1355
5
          if (!SuppressDiagnostic)
1356
3
            PP.Diag(Tok.getLocation(), diag::err_too_few_args_in_macro_invoc);
1357
5
        }
1358
712k
        Tok.setKind(tok::numeric_constant);
1359
712k
        return;
1360
1361
712k
      default: {
1362
        // Parse the macro argument, if one not found so far.
1363
712k
        if (Result)
1364
9
          break;
1365
1366
712k
        bool HasLexedNextToken = false;
1367
712k
        Result = Op(Tok, HasLexedNextToken);
1368
712k
        ResultTok = Tok;
1369
712k
        if (HasLexedNextToken)
1370
5.51k
          goto already_lexed;
1371
706k
        continue;
1372
712k
      }
1373
1.42M
    }
1374
1375
    // Diagnose missing ')'.
1376
11
    if (!SuppressDiagnostic) {
1377
8
      if (auto Diag = PP.Diag(Tok.getLocation(), diag::err_pp_expected_after)) {
1378
8
        if (IdentifierInfo *LastII = ResultTok.getIdentifierInfo())
1379
4
          Diag << LastII;
1380
4
        else
1381
4
          Diag << ResultTok.getKind();
1382
8
        Diag << tok::r_paren << ResultTok.getLocation();
1383
8
      }
1384
8
      PP.Diag(LParenLoc, diag::note_matching) << tok::l_paren;
1385
8
      SuppressDiagnostic = true;
1386
8
    }
1387
11
  }
1388
712k
}
1389
1390
/// Helper function to return the IdentifierInfo structure of a Token
1391
/// or generate a diagnostic if none available.
1392
static IdentifierInfo *ExpectFeatureIdentifierInfo(Token &Tok,
1393
                                                   Preprocessor &PP,
1394
712k
                                                   signed DiagID) {
1395
712k
  IdentifierInfo *II;
1396
712k
  if (!Tok.isAnnotation() && (II = Tok.getIdentifierInfo()))
1397
712k
    return II;
1398
1399
6
  PP.Diag(Tok.getLocation(), DiagID);
1400
6
  return nullptr;
1401
712k
}
1402
1403
/// Implements the __is_target_arch builtin macro.
1404
6.47k
static bool isTargetArch(const TargetInfo &TI, const IdentifierInfo *II) {
1405
6.47k
  std::string ArchName = II->getName().lower() + "--";
1406
6.47k
  llvm::Triple Arch(ArchName);
1407
6.47k
  const llvm::Triple &TT = TI.getTriple();
1408
6.47k
  if (TT.isThumb()) {
1409
    // arm matches thumb or thumbv7. armv7 matches thumbv7.
1410
11
    if ((Arch.getSubArch() == llvm::Triple::NoSubArch ||
1411
11
         
Arch.getSubArch() == TT.getSubArch()7
) &&
1412
11
        
(8
(8
TT.getArch() == llvm::Triple::thumb8
&&
1413
8
          Arch.getArch() == llvm::Triple::arm) ||
1414
8
         
(6
TT.getArch() == llvm::Triple::thumbeb6
&&
1415
6
          
Arch.getArch() == llvm::Triple::armeb0
)))
1416
2
      return true;
1417
11
  }
1418
  // Check the parsed arch when it has no sub arch to allow Clang to
1419
  // match thumb to thumbv7 but to prohibit matching thumbv6 to thumbv7.
1420
6.46k
  return (Arch.getSubArch() == llvm::Triple::NoSubArch ||
1421
6.46k
          
Arch.getSubArch() == TT.getSubArch()1.72k
) &&
1422
6.46k
         
Arch.getArch() == TT.getArch()4.75k
;
1423
6.47k
}
1424
1425
/// Implements the __is_target_vendor builtin macro.
1426
4.73k
static bool isTargetVendor(const TargetInfo &TI, const IdentifierInfo *II) {
1427
4.73k
  StringRef VendorName = TI.getTriple().getVendorName();
1428
4.73k
  if (VendorName.empty())
1429
0
    VendorName = "unknown";
1430
4.73k
  return VendorName.equals_insensitive(II->getName());
1431
4.73k
}
1432
1433
/// Implements the __is_target_os builtin macro.
1434
4.75k
static bool isTargetOS(const TargetInfo &TI, const IdentifierInfo *II) {
1435
4.75k
  std::string OSName =
1436
4.75k
      (llvm::Twine("unknown-unknown-") + II->getName().lower()).str();
1437
4.75k
  llvm::Triple OS(OSName);
1438
4.75k
  if (OS.getOS() == llvm::Triple::Darwin) {
1439
    // Darwin matches macos, ios, etc.
1440
6
    return TI.getTriple().isOSDarwin();
1441
6
  }
1442
4.74k
  return TI.getTriple().getOS() == OS.getOS();
1443
4.75k
}
1444
1445
/// Implements the __is_target_environment builtin macro.
1446
static bool isTargetEnvironment(const TargetInfo &TI,
1447
4.31k
                                const IdentifierInfo *II) {
1448
4.31k
  std::string EnvName = (llvm::Twine("---") + II->getName().lower()).str();
1449
4.31k
  llvm::Triple Env(EnvName);
1450
  // The unknown environment is matched only if
1451
  // '__is_target_environment(unknown)' is used.
1452
4.31k
  if (Env.getEnvironment() == llvm::Triple::UnknownEnvironment &&
1453
4.31k
      
EnvName != "---unknown"6
)
1454
2
    return false;
1455
4.31k
  return TI.getTriple().getEnvironment() == Env.getEnvironment();
1456
4.31k
}
1457
1458
/// Implements the __is_target_variant_os builtin macro.
1459
3.45k
static bool isTargetVariantOS(const TargetInfo &TI, const IdentifierInfo *II) {
1460
3.45k
  if (TI.getTriple().isOSDarwin()) {
1461
3.45k
    const llvm::Triple *VariantTriple = TI.getDarwinTargetVariantTriple();
1462
3.45k
    if (!VariantTriple)
1463
3.45k
      return false;
1464
1465
3
    std::string OSName =
1466
3
        (llvm::Twine("unknown-unknown-") + II->getName().lower()).str();
1467
3
    llvm::Triple OS(OSName);
1468
3
    if (OS.getOS() == llvm::Triple::Darwin) {
1469
      // Darwin matches macos, ios, etc.
1470
1
      return VariantTriple->isOSDarwin();
1471
1
    }
1472
2
    return VariantTriple->getOS() == OS.getOS();
1473
3
  }
1474
2
  return false;
1475
3.45k
}
1476
1477
/// Implements the __is_target_variant_environment builtin macro.
1478
static bool isTargetVariantEnvironment(const TargetInfo &TI,
1479
3.45k
                                const IdentifierInfo *II) {
1480
3.45k
  if (TI.getTriple().isOSDarwin()) {
1481
3.44k
    const llvm::Triple *VariantTriple = TI.getDarwinTargetVariantTriple();
1482
3.44k
    if (!VariantTriple)
1483
3.44k
      return false;
1484
1
    std::string EnvName = (llvm::Twine("---") + II->getName().lower()).str();
1485
1
    llvm::Triple Env(EnvName);
1486
1
    return VariantTriple->getEnvironment() == Env.getEnvironment();
1487
3.44k
  }
1488
2
  return false;
1489
3.45k
}
1490
1491
/// ExpandBuiltinMacro - If an identifier token is read that is to be expanded
1492
/// as a builtin macro, handle it and return the next token as 'Tok'.
1493
1.44M
void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
1494
  // Figure out which token this is.
1495
1.44M
  IdentifierInfo *II = Tok.getIdentifierInfo();
1496
1.44M
  assert(II && "Can't be a macro without id info!");
1497
1498
  // If this is an _Pragma or Microsoft __pragma directive, expand it,
1499
  // invoke the pragma handler, then lex the token after it.
1500
1.44M
  if (II == Ident_Pragma)
1501
699k
    return Handle_Pragma(Tok);
1502
743k
  else if (II == Ident__pragma) // in non-MS mode this is null
1503
47
    return HandleMicrosoft__pragma(Tok);
1504
1505
743k
  ++NumBuiltinMacroExpanded;
1506
1507
743k
  SmallString<128> TmpBuffer;
1508
743k
  llvm::raw_svector_ostream OS(TmpBuffer);
1509
1510
  // Set up the return result.
1511
743k
  Tok.setIdentifierInfo(nullptr);
1512
743k
  Tok.clearFlag(Token::NeedsCleaning);
1513
743k
  bool IsAtStartOfLine = Tok.isAtStartOfLine();
1514
743k
  bool HasLeadingSpace = Tok.hasLeadingSpace();
1515
1516
743k
  if (II == Ident__LINE__) {
1517
    // C99 6.10.8: "__LINE__: The presumed line number (within the current
1518
    // source file) of the current source line (an integer constant)".  This can
1519
    // be affected by #line.
1520
3.58k
    SourceLocation Loc = Tok.getLocation();
1521
1522
    // Advance to the location of the first _, this might not be the first byte
1523
    // of the token if it starts with an escaped newline.
1524
3.58k
    Loc = AdvanceToTokenCharacter(Loc, 0);
1525
1526
    // One wrinkle here is that GCC expands __LINE__ to location of the *end* of
1527
    // a macro expansion.  This doesn't matter for object-like macros, but
1528
    // can matter for a function-like macro that expands to contain __LINE__.
1529
    // Skip down through expansion points until we find a file loc for the
1530
    // end of the expansion history.
1531
3.58k
    Loc = SourceMgr.getExpansionRange(Loc).getEnd();
1532
3.58k
    PresumedLoc PLoc = SourceMgr.getPresumedLoc(Loc);
1533
1534
    // __LINE__ expands to a simple numeric value.
1535
3.58k
    OS << (PLoc.isValid()? PLoc.getLine() : 
10
);
1536
3.58k
    Tok.setKind(tok::numeric_constant);
1537
739k
  } else if (II == Ident__FILE__ || 
II == Ident__BASE_FILE__739k
||
1538
739k
             
II == Ident__FILE_NAME__739k
) {
1539
    // C99 6.10.8: "__FILE__: The presumed name of the current source file (a
1540
    // character string literal)". This can be affected by #line.
1541
504
    PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
1542
1543
    // __BASE_FILE__ is a GNU extension that returns the top of the presumed
1544
    // #include stack instead of the current file.
1545
504
    if (II == Ident__BASE_FILE__ && 
PLoc.isValid()10
) {
1546
10
      SourceLocation NextLoc = PLoc.getIncludeLoc();
1547
20
      while (NextLoc.isValid()) {
1548
10
        PLoc = SourceMgr.getPresumedLoc(NextLoc);
1549
10
        if (PLoc.isInvalid())
1550
0
          break;
1551
1552
10
        NextLoc = PLoc.getIncludeLoc();
1553
10
      }
1554
10
    }
1555
1556
    // Escape this filename.  Turn '\' -> '\\' '"' -> '\"'
1557
504
    SmallString<256> FN;
1558
504
    if (PLoc.isValid()) {
1559
      // __FILE_NAME__ is a Clang-specific extension that expands to the
1560
      // the last part of __FILE__.
1561
504
      if (II == Ident__FILE_NAME__) {
1562
83
        processPathToFileName(FN, PLoc, getLangOpts(), getTargetInfo());
1563
421
      } else {
1564
421
        FN += PLoc.getFilename();
1565
421
        processPathForFileMacro(FN, getLangOpts(), getTargetInfo());
1566
421
      }
1567
504
      Lexer::Stringify(FN);
1568
504
      OS << '"' << FN << '"';
1569
504
    }
1570
504
    Tok.setKind(tok::string_literal);
1571
738k
  } else if (II == Ident__DATE__) {
1572
10
    Diag(Tok.getLocation(), diag::warn_pp_date_time);
1573
10
    if (!DATELoc.isValid())
1574
7
      ComputeDATE_TIME(DATELoc, TIMELoc, *this);
1575
10
    Tok.setKind(tok::string_literal);
1576
10
    Tok.setLength(strlen("\"Mmm dd yyyy\""));
1577
10
    Tok.setLocation(SourceMgr.createExpansionLoc(DATELoc, Tok.getLocation(),
1578
10
                                                 Tok.getLocation(),
1579
10
                                                 Tok.getLength()));
1580
10
    return;
1581
738k
  } else if (II == Ident__TIME__) {
1582
9
    Diag(Tok.getLocation(), diag::warn_pp_date_time);
1583
9
    if (!TIMELoc.isValid())
1584
3
      ComputeDATE_TIME(DATELoc, TIMELoc, *this);
1585
9
    Tok.setKind(tok::string_literal);
1586
9
    Tok.setLength(strlen("\"hh:mm:ss\""));
1587
9
    Tok.setLocation(SourceMgr.createExpansionLoc(TIMELoc, Tok.getLocation(),
1588
9
                                                 Tok.getLocation(),
1589
9
                                                 Tok.getLength()));
1590
9
    return;
1591
738k
  } else if (II == Ident__INCLUDE_LEVEL__) {
1592
    // Compute the presumed include depth of this token.  This can be affected
1593
    // by GNU line markers.
1594
9
    unsigned Depth = 0;
1595
1596
9
    PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
1597
9
    if (PLoc.isValid()) {
1598
9
      PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc());
1599
15
      for (; PLoc.isValid(); 
++Depth6
)
1600
6
        PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc());
1601
9
    }
1602
1603
    // __INCLUDE_LEVEL__ expands to a simple numeric value.
1604
9
    OS << Depth;
1605
9
    Tok.setKind(tok::numeric_constant);
1606
738k
  } else if (II == Ident__TIMESTAMP__) {
1607
6
    Diag(Tok.getLocation(), diag::warn_pp_date_time);
1608
    // MSVC, ICC, GCC, VisualAge C++ extension.  The generated string should be
1609
    // of the form "Ddd Mmm dd hh::mm::ss yyyy", which is returned by asctime.
1610
6
    const char *Result;
1611
6
    if (getPreprocessorOpts().SourceDateEpoch) {
1612
3
      time_t TT = *getPreprocessorOpts().SourceDateEpoch;
1613
3
      std::tm *TM = std::gmtime(&TT);
1614
3
      Result = asctime(TM);
1615
3
    } else {
1616
      // Get the file that we are lexing out of.  If we're currently lexing from
1617
      // a macro, dig into the include stack.
1618
3
      const FileEntry *CurFile = nullptr;
1619
3
      if (PreprocessorLexer *TheLexer = getCurrentFileLexer())
1620
3
        CurFile = SourceMgr.getFileEntryForID(TheLexer->getFileID());
1621
3
      if (CurFile) {
1622
3
        time_t TT = CurFile->getModificationTime();
1623
3
        struct tm *TM = localtime(&TT);
1624
3
        Result = asctime(TM);
1625
3
      } else {
1626
0
        Result = "??? ??? ?? ??:??:?? ????\n";
1627
0
      }
1628
3
    }
1629
    // Surround the string with " and strip the trailing newline.
1630
6
    OS << '"' << StringRef(Result).drop_back() << '"';
1631
6
    Tok.setKind(tok::string_literal);
1632
738k
  } else if (II == Ident__FLT_EVAL_METHOD__) {
1633
    // __FLT_EVAL_METHOD__ is set to the default value.
1634
882
    OS << getTUFPEvalMethod();
1635
    // __FLT_EVAL_METHOD__ expands to a simple numeric value.
1636
882
    Tok.setKind(tok::numeric_constant);
1637
882
    if (getLastFPEvalPragmaLocation().isValid()) {
1638
      // The program is ill-formed. The value of __FLT_EVAL_METHOD__ is altered
1639
      // by the pragma.
1640
18
      Diag(Tok, diag::err_illegal_use_of_flt_eval_macro);
1641
18
      Diag(getLastFPEvalPragmaLocation(), diag::note_pragma_entered_here);
1642
18
    }
1643
738k
  } else if (II == Ident__COUNTER__) {
1644
    // __COUNTER__ expands to a simple numeric value.
1645
772
    OS << CounterValue++;
1646
772
    Tok.setKind(tok::numeric_constant);
1647
737k
  } else if (II == Ident__has_feature) {
1648
511k
    EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, false,
1649
511k
      [this](Token &Tok, bool &HasLexedNextToken) -> int {
1650
511k
        IdentifierInfo *II = ExpectFeatureIdentifierInfo(Tok, *this,
1651
511k
                                           diag::err_feature_check_malformed);
1652
511k
        return II && 
HasFeature(*this, II->getName())511k
;
1653
511k
      });
1654
511k
  } else 
if (226k
II == Ident__has_extension226k
) {
1655
19.3k
    EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, false,
1656
19.3k
      [this](Token &Tok, bool &HasLexedNextToken) -> int {
1657
19.3k
        IdentifierInfo *II = ExpectFeatureIdentifierInfo(Tok, *this,
1658
19.3k
                                           diag::err_feature_check_malformed);
1659
19.3k
        return II && HasExtension(*this, II->getName());
1660
19.3k
      });
1661
206k
  } else if (II == Ident__has_builtin) {
1662
59.1k
    EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, false,
1663
59.1k
      [this](Token &Tok, bool &HasLexedNextToken) -> int {
1664
59.1k
        IdentifierInfo *II = ExpectFeatureIdentifierInfo(Tok, *this,
1665
59.1k
                                           diag::err_feature_check_malformed);
1666
59.1k
        if (!II)
1667
0
          return false;
1668
59.1k
        else if (II->getBuiltinID() != 0) {
1669
13.6k
          switch (II->getBuiltinID()) {
1670
994
          case Builtin::BI__builtin_operator_new:
1671
1.98k
          case Builtin::BI__builtin_operator_delete:
1672
            // denotes date of behavior change to support calling arbitrary
1673
            // usual allocation and deallocation functions. Required by libc++
1674
1.98k
            return 201802;
1675
11.6k
          default:
1676
11.6k
            return Builtin::evaluateRequiredTargetFeatures(
1677
11.6k
                getBuiltinInfo().getRequiredFeatures(II->getBuiltinID()),
1678
11.6k
                getTargetInfo().getTargetOpts().FeatureMap);
1679
13.6k
          }
1680
0
          return true;
1681
45.4k
        } else if (II->getTokenID() != tok::identifier ||
1682
45.4k
                   
II->hasRevertedTokenIDToIdentifier()24.2k
) {
1683
          // Treat all keywords that introduce a custom syntax of the form
1684
          //
1685
          //   '__some_keyword' '(' [...] ')'
1686
          //
1687
          // as being "builtin functions", even if the syntax isn't a valid
1688
          // function call (for example, because the builtin takes a type
1689
          // argument).
1690
21.2k
          if (II->getName().startswith("__builtin_") ||
1691
21.2k
              
II->getName().startswith("__is_")21.1k
||
1692
21.2k
              
II->getName().startswith("__has_")8.50k
)
1693
12.7k
            return true;
1694
8.50k
          return llvm::StringSwitch<bool>(II->getName())
1695
8.50k
              .Case("__array_rank", true)
1696
8.50k
              .Case("__array_extent", true)
1697
8.50k
              .Case("__reference_binds_to_temporary", true)
1698
8.50k
              .Case("__reference_constructs_from_temporary", true)
1699
136k
#define TRANSFORM_TYPE_TRAIT_DEF(_, Trait) .Case("__" #Trait, true)
1700
8.50k
#include "clang/Basic/TransformTypeTraits.def"
1701
8.50k
              .Default(false);
1702
24.2k
        } else {
1703
24.2k
          return llvm::StringSwitch<bool>(II->getName())
1704
              // Report builtin templates as being builtins.
1705
24.2k
              .Case("__make_integer_seq", getLangOpts().CPlusPlus)
1706
24.2k
              .Case("__type_pack_element", getLangOpts().CPlusPlus)
1707
              // Likewise for some builtin preprocessor macros.
1708
              // FIXME: This is inconsistent; we usually suggest detecting
1709
              // builtin macros via #ifdef. Don't add more cases here.
1710
24.2k
              .Case("__is_target_arch", true)
1711
24.2k
              .Case("__is_target_vendor", true)
1712
24.2k
              .Case("__is_target_os", true)
1713
24.2k
              .Case("__is_target_environment", true)
1714
24.2k
              .Case("__is_target_variant_os", true)
1715
24.2k
              .Case("__is_target_variant_environment", true)
1716
24.2k
              .Default(false);
1717
24.2k
        }
1718
59.1k
      });
1719
147k
  } else if (II == Ident__has_constexpr_builtin) {
1720
2.45k
    EvaluateFeatureLikeBuiltinMacro(
1721
2.45k
        OS, Tok, II, *this, false,
1722
2.45k
        [this](Token &Tok, bool &HasLexedNextToken) -> int {
1723
2.45k
          IdentifierInfo *II = ExpectFeatureIdentifierInfo(
1724
2.45k
              Tok, *this, diag::err_feature_check_malformed);
1725
2.45k
          if (!II)
1726
2
            return false;
1727
2.45k
          unsigned BuiltinOp = II->getBuiltinID();
1728
2.45k
          return BuiltinOp != 0 &&
1729
2.45k
                 
this->getBuiltinInfo().isConstantEvaluated(BuiltinOp)2.44k
;
1730
2.45k
        });
1731
145k
  } else if (II == Ident__is_identifier) {
1732
2.99k
    EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, false,
1733
2.99k
      [](Token &Tok, bool &HasLexedNextToken) -> int {
1734
2.98k
        return Tok.is(tok::identifier);
1735
2.98k
      });
1736
142k
  } else if (II == Ident__has_attribute) {
1737
78.9k
    EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, true,
1738
78.9k
      [this](Token &Tok, bool &HasLexedNextToken) -> int {
1739
78.9k
        IdentifierInfo *II = ExpectFeatureIdentifierInfo(Tok, *this,
1740
78.9k
                                           diag::err_feature_check_malformed);
1741
78.9k
        return II ? hasAttribute(AttributeCommonInfo::Syntax::AS_GNU, nullptr,
1742
78.9k
                                 II, getTargetInfo(), getLangOpts())
1743
78.9k
                  : 
00
;
1744
78.9k
      });
1745
78.9k
  } else 
if (63.2k
II == Ident__has_declspec63.2k
) {
1746
1.66k
    EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, true,
1747
1.66k
      [this](Token &Tok, bool &HasLexedNextToken) -> int {
1748
1.66k
        IdentifierInfo *II = ExpectFeatureIdentifierInfo(Tok, *this,
1749
1.66k
                                           diag::err_feature_check_malformed);
1750
1.66k
        if (II) {
1751
1.66k
          const LangOptions &LangOpts = getLangOpts();
1752
1.66k
          return LangOpts.DeclSpecKeyword &&
1753
1.66k
                 hasAttribute(AttributeCommonInfo::Syntax::AS_Declspec, nullptr,
1754
8
                              II, getTargetInfo(), LangOpts);
1755
1.66k
        }
1756
1757
0
        return false;
1758
1.66k
      });
1759
61.5k
  } else if (II == Ident__has_cpp_attribute ||
1760
61.5k
             
II == Ident__has_c_attribute52.7k
) {
1761
8.89k
    bool IsCXX = II == Ident__has_cpp_attribute;
1762
8.89k
    EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, true,
1763
8.89k
        [&](Token &Tok, bool &HasLexedNextToken) -> int {
1764
8.89k
          IdentifierInfo *ScopeII = nullptr;
1765
8.89k
          IdentifierInfo *II = ExpectFeatureIdentifierInfo(
1766
8.89k
              Tok, *this, diag::err_feature_check_malformed);
1767
8.89k
          if (!II)
1768
1
            return false;
1769
1770
          // It is possible to receive a scope token.  Read the "::", if it is
1771
          // available, and the subsequent identifier.
1772
8.89k
          LexUnexpandedToken(Tok);
1773
8.89k
          if (Tok.isNot(tok::coloncolon))
1774
5.51k
            HasLexedNextToken = true;
1775
3.38k
          else {
1776
3.38k
            ScopeII = II;
1777
            // Lex an expanded token for the attribute name.
1778
3.38k
            Lex(Tok);
1779
3.38k
            II = ExpectFeatureIdentifierInfo(Tok, *this,
1780
3.38k
                                             diag::err_feature_check_malformed);
1781
3.38k
          }
1782
1783
8.89k
          AttributeCommonInfo::Syntax Syntax =
1784
8.89k
              IsCXX ? 
AttributeCommonInfo::Syntax::AS_CXX118.81k
1785
8.89k
                    : 
AttributeCommonInfo::Syntax::AS_C2374
;
1786
8.89k
          return II ? hasAttribute(Syntax, ScopeII, II, getTargetInfo(),
1787
8.89k
                                   getLangOpts())
1788
8.89k
                    : 
00
;
1789
8.89k
        });
1790
52.6k
  } else if (II == Ident__has_include ||
1791
52.6k
             
II == Ident__has_include_next39.8k
) {
1792
    // The argument to these two builtins should be a parenthesized
1793
    // file name string literal using angle brackets (<>) or
1794
    // double-quotes ("").
1795
24.8k
    bool Value;
1796
24.8k
    if (II == Ident__has_include)
1797
12.8k
      Value = EvaluateHasInclude(Tok, II);
1798
12.0k
    else
1799
12.0k
      Value = EvaluateHasIncludeNext(Tok, II);
1800
1801
24.8k
    if (Tok.isNot(tok::r_paren))
1802
24
      return;
1803
24.8k
    OS << (int)Value;
1804
24.8k
    Tok.setKind(tok::numeric_constant);
1805
27.8k
  } else if (II == Ident__has_warning) {
1806
    // The argument should be a parenthesized string literal.
1807
10
    EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, false,
1808
10
      [this](Token &Tok, bool &HasLexedNextToken) -> int {
1809
7
        std::string WarningName;
1810
7
        SourceLocation StrStartLoc = Tok.getLocation();
1811
1812
7
        HasLexedNextToken = Tok.is(tok::string_literal);
1813
7
        if (!FinishLexStringLiteral(Tok, WarningName, "'__has_warning'",
1814
7
                                    /*AllowMacroExpansion=*/false))
1815
2
          return false;
1816
1817
        // FIXME: Should we accept "-R..." flags here, or should that be
1818
        // handled by a separate __has_remark?
1819
5
        if (WarningName.size() < 3 || WarningName[0] != '-' ||
1820
5
            
WarningName[1] != 'W'4
) {
1821
1
          Diag(StrStartLoc, diag::warn_has_warning_invalid_option);
1822
1
          return false;
1823
1
        }
1824
1825
        // Finally, check if the warning flags maps to a diagnostic group.
1826
        // We construct a SmallVector here to talk to getDiagnosticIDs().
1827
        // Although we don't use the result, this isn't a hot path, and not
1828
        // worth special casing.
1829
4
        SmallVector<diag::kind, 10> Diags;
1830
4
        return !getDiagnostics().getDiagnosticIDs()->
1831
4
                getDiagnosticsInGroup(diag::Flavor::WarningOrError,
1832
4
                                      WarningName.substr(2), Diags);
1833
5
      });
1834
27.8k
  } else if (II == Ident__building_module) {
1835
    // The argument to this builtin should be an identifier. The
1836
    // builtin evaluates to 1 when that identifier names the module we are
1837
    // currently building.
1838
612
    EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, false,
1839
612
      [this](Token &Tok, bool &HasLexedNextToken) -> int {
1840
612
        IdentifierInfo *II = ExpectFeatureIdentifierInfo(Tok, *this,
1841
612
                                       diag::err_expected_id_building_module);
1842
612
        return getLangOpts().isCompilingModule() && 
II33
&&
1843
612
               
(II->getName() == getLangOpts().CurrentModule)33
;
1844
612
      });
1845
27.2k
  } else if (II == Ident__MODULE__) {
1846
    // The current module as an identifier.
1847
7
    OS << getLangOpts().CurrentModule;
1848
7
    IdentifierInfo *ModuleII = getIdentifierInfo(getLangOpts().CurrentModule);
1849
7
    Tok.setIdentifierInfo(ModuleII);
1850
7
    Tok.setKind(ModuleII->getTokenID());
1851
27.2k
  } else if (II == Ident__identifier) {
1852
24
    SourceLocation Loc = Tok.getLocation();
1853
1854
    // We're expecting '__identifier' '(' identifier ')'. Try to recover
1855
    // if the parens are missing.
1856
24
    LexNonComment(Tok);
1857
24
    if (Tok.isNot(tok::l_paren)) {
1858
      // No '(', use end of last token.
1859
1
      Diag(getLocForEndOfToken(Loc), diag::err_pp_expected_after)
1860
1
        << II << tok::l_paren;
1861
      // If the next token isn't valid as our argument, we can't recover.
1862
1
      if (!Tok.isAnnotation() && Tok.getIdentifierInfo())
1863
1
        Tok.setKind(tok::identifier);
1864
1
      return;
1865
1
    }
1866
1867
23
    SourceLocation LParenLoc = Tok.getLocation();
1868
23
    LexNonComment(Tok);
1869
1870
23
    if (!Tok.isAnnotation() && Tok.getIdentifierInfo())
1871
13
      Tok.setKind(tok::identifier);
1872
10
    else if (Tok.is(tok::string_literal) && 
!Tok.hasUDSuffix()5
) {
1873
5
      StringLiteralParser Literal(Tok, *this,
1874
5
                                  StringLiteralEvalMethod::Unevaluated);
1875
5
      if (Literal.hadError)
1876
0
        return;
1877
1878
5
      Tok.setIdentifierInfo(getIdentifierInfo(Literal.GetString()));
1879
5
      Tok.setKind(tok::identifier);
1880
5
    } else {
1881
5
      Diag(Tok.getLocation(), diag::err_pp_identifier_arg_not_identifier)
1882
5
        << Tok.getKind();
1883
      // Don't walk past anything that's not a real token.
1884
5
      if (Tok.isOneOf(tok::eof, tok::eod) || Tok.isAnnotation())
1885
0
        return;
1886
5
    }
1887
1888
    // Discard the ')', preserving 'Tok' as our result.
1889
23
    Token RParen;
1890
23
    LexNonComment(RParen);
1891
23
    if (RParen.isNot(tok::r_paren)) {
1892
1
      Diag(getLocForEndOfToken(Tok.getLocation()), diag::err_pp_expected_after)
1893
1
        << Tok.getKind() << tok::r_paren;
1894
1
      Diag(LParenLoc, diag::note_matching) << tok::l_paren;
1895
1
    }
1896
23
    return;
1897
27.1k
  } else if (II == Ident__is_target_arch) {
1898
6.47k
    EvaluateFeatureLikeBuiltinMacro(
1899
6.47k
        OS, Tok, II, *this, false,
1900
6.47k
        [this](Token &Tok, bool &HasLexedNextToken) -> int {
1901
6.47k
          IdentifierInfo *II = ExpectFeatureIdentifierInfo(
1902
6.47k
              Tok, *this, diag::err_feature_check_malformed);
1903
6.47k
          return II && 
isTargetArch(getTargetInfo(), II)6.47k
;
1904
6.47k
        });
1905
20.7k
  } else if (II == Ident__is_target_vendor) {
1906
4.73k
    EvaluateFeatureLikeBuiltinMacro(
1907
4.73k
        OS, Tok, II, *this, false,
1908
4.73k
        [this](Token &Tok, bool &HasLexedNextToken) -> int {
1909
4.73k
          IdentifierInfo *II = ExpectFeatureIdentifierInfo(
1910
4.73k
              Tok, *this, diag::err_feature_check_malformed);
1911
4.73k
          return II && isTargetVendor(getTargetInfo(), II);
1912
4.73k
        });
1913
15.9k
  } else if (II == Ident__is_target_os) {
1914
4.75k
    EvaluateFeatureLikeBuiltinMacro(
1915
4.75k
        OS, Tok, II, *this, false,
1916
4.75k
        [this](Token &Tok, bool &HasLexedNextToken) -> int {
1917
4.75k
          IdentifierInfo *II = ExpectFeatureIdentifierInfo(
1918
4.75k
              Tok, *this, diag::err_feature_check_malformed);
1919
4.75k
          return II && isTargetOS(getTargetInfo(), II);
1920
4.75k
        });
1921
11.2k
  } else if (II == Ident__is_target_environment) {
1922
4.31k
    EvaluateFeatureLikeBuiltinMacro(
1923
4.31k
        OS, Tok, II, *this, false,
1924
4.31k
        [this](Token &Tok, bool &HasLexedNextToken) -> int {
1925
4.31k
          IdentifierInfo *II = ExpectFeatureIdentifierInfo(
1926
4.31k
              Tok, *this, diag::err_feature_check_malformed);
1927
4.31k
          return II && isTargetEnvironment(getTargetInfo(), II);
1928
4.31k
        });
1929
6.90k
  } else if (II == Ident__is_target_variant_os) {
1930
3.45k
    EvaluateFeatureLikeBuiltinMacro(
1931
3.45k
        OS, Tok, II, *this, false,
1932
3.45k
        [this](Token &Tok, bool &HasLexedNextToken) -> int {
1933
3.45k
          IdentifierInfo *II = ExpectFeatureIdentifierInfo(
1934
3.45k
              Tok, *this, diag::err_feature_check_malformed);
1935
3.45k
          return II && isTargetVariantOS(getTargetInfo(), II);
1936
3.45k
        });
1937
3.45k
  } else 
if (3.45k
II == Ident__is_target_variant_environment3.45k
) {
1938
3.45k
    EvaluateFeatureLikeBuiltinMacro(
1939
3.45k
        OS, Tok, II, *this, false,
1940
3.45k
        [this](Token &Tok, bool &HasLexedNextToken) -> int {
1941
3.45k
          IdentifierInfo *II = ExpectFeatureIdentifierInfo(
1942
3.45k
              Tok, *this, diag::err_feature_check_malformed);
1943
3.45k
          return II && isTargetVariantEnvironment(getTargetInfo(), II);
1944
3.45k
        });
1945
3.45k
  } else {
1946
0
    llvm_unreachable("Unknown identifier!");
1947
0
  }
1948
743k
  CreateString(OS.str(), Tok, Tok.getLocation(), Tok.getLocation());
1949
743k
  Tok.setFlagValue(Token::StartOfLine, IsAtStartOfLine);
1950
743k
  Tok.setFlagValue(Token::LeadingSpace, HasLeadingSpace);
1951
743k
}
1952
1953
79.5M
void Preprocessor::markMacroAsUsed(MacroInfo *MI) {
1954
  // If the 'used' status changed, and the macro requires 'unused' warning,
1955
  // remove its SourceLocation from the warn-for-unused-macro locations.
1956
79.5M
  if (MI->isWarnIfUnused() && 
!MI->isUsed()4
)
1957
4
    WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());
1958
79.5M
  MI->setIsUsed(true);
1959
79.5M
}
1960
1961
void Preprocessor::processPathForFileMacro(SmallVectorImpl<char> &Path,
1962
                                           const LangOptions &LangOpts,
1963
2.09k
                                           const TargetInfo &TI) {
1964
2.09k
  LangOpts.remapPathPrefix(Path);
1965
2.09k
  if (LangOpts.UseTargetPathSeparator) {
1966
24
    if (TI.getTriple().isOSWindows())
1967
9
      llvm::sys::path::remove_dots(Path, false,
1968
9
                                   llvm::sys::path::Style::windows_backslash);
1969
15
    else
1970
15
      llvm::sys::path::remove_dots(Path, false, llvm::sys::path::Style::posix);
1971
24
  }
1972
2.09k
}
1973
1974
void Preprocessor::processPathToFileName(SmallVectorImpl<char> &FileName,
1975
                                         const PresumedLoc &PLoc,
1976
                                         const LangOptions &LangOpts,
1977
324
                                         const TargetInfo &TI) {
1978
  // Try to get the last path component, failing that return the original
1979
  // presumed location.
1980
324
  StringRef PLFileName = llvm::sys::path::filename(PLoc.getFilename());
1981
324
  if (PLFileName.empty())
1982
0
    PLFileName = PLoc.getFilename();
1983
324
  FileName.append(PLFileName.begin(), PLFileName.end());
1984
324
  processPathForFileMacro(FileName, LangOpts, TI);
1985
324
}