Coverage Report

Created: 2023-11-11 10:31

/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.23M
Preprocessor::getLocalMacroDirectiveHistory(const IdentifierInfo *II) const {
64
3.23M
  if (!II->hadMacroDefinition())
65
0
    return nullptr;
66
3.23M
  auto Pos = CurSubmoduleState->Macros.find(II);
67
3.23M
  return Pos == CurSubmoduleState->Macros.end() ? 
nullptr175
68
3.23M
                                                : 
Pos->second.getLatest()3.23M
;
69
3.23M
}
70
71
49.3M
void Preprocessor::appendMacroDirective(IdentifierInfo *II, MacroDirective *MD){
72
49.3M
  assert(MD && "MacroDirective should be non-zero!");
73
49.3M
  assert(!MD->getPrevious() && "Already attached to a MacroDirective history.");
74
75
49.3M
  MacroState &StoredMD = CurSubmoduleState->Macros[II];
76
49.3M
  auto *OldMD = StoredMD.getLatest();
77
49.3M
  MD->setPrevious(OldMD);
78
49.3M
  StoredMD.setLatest(MD);
79
49.3M
  StoredMD.overrideActiveModuleMacros(*this, II);
80
81
49.3M
  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
243k
    PendingModuleMacroNames.push_back(II);
86
243k
  }
87
88
  // Set up the identifier as having associated macro history.
89
49.3M
  II->setHasMacroDefinition(true);
90
49.3M
  if (!MD->isDefined() && 
!LeafModuleMacros.contains(II)223k
)
91
223k
    II->setHasMacroDefinition(false);
92
49.3M
  if (II->isFromAST())
93
7.88k
    II->setChangedSinceDeserialization();
94
49.3M
}
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
20.0M
                                          bool &New) {
136
20.0M
  llvm::FoldingSetNodeID ID;
137
20.0M
  ModuleMacro::Profile(ID, Mod, II);
138
139
20.0M
  void *InsertPos;
140
20.0M
  if (auto *MM = ModuleMacros.FindNodeOrInsertPos(ID, InsertPos)) {
141
14.1M
    New = false;
142
14.1M
    return MM;
143
14.1M
  }
144
145
5.89M
  auto *MM = ModuleMacro::create(*this, Mod, II, Macro, Overrides);
146
5.89M
  ModuleMacros.InsertNode(MM, InsertPos);
147
148
  // Each overridden macro is now overridden by one more macro.
149
5.89M
  bool HidAny = false;
150
5.89M
  for (auto *O : Overrides) {
151
4.00k
    HidAny |= (O->NumOverriddenBy == 0);
152
4.00k
    ++O->NumOverriddenBy;
153
4.00k
  }
154
155
  // If we were the first overrider for any macro, it's no longer a leaf.
156
5.89M
  auto &LeafMacros = LeafModuleMacros[II];
157
5.89M
  if (HidAny) {
158
3.80k
    llvm::erase_if(LeafMacros,
159
3.91k
                   [](ModuleMacro *MM) { return MM->NumOverriddenBy != 0; });
160
3.80k
  }
161
162
  // The new macro is always a leaf macro.
163
5.89M
  LeafMacros.push_back(MM);
164
  // The identifier now has defined macros (that may or may not be visible).
165
5.89M
  II->setHasMacroDefinition(true);
166
167
5.89M
  New = true;
168
5.89M
  return MM;
169
20.0M
}
170
171
ModuleMacro *Preprocessor::getModuleMacro(Module *Mod,
172
1.99k
                                          const IdentifierInfo *II) {
173
1.99k
  llvm::FoldingSetNodeID ID;
174
1.99k
  ModuleMacro::Profile(ID, Mod, II);
175
176
1.99k
  void *InsertPos;
177
1.99k
  return ModuleMacros.FindNodeOrInsertPos(ID, InsertPos);
178
1.99k
}
179
180
void Preprocessor::updateModuleMacroInfo(const IdentifierInfo *II,
181
159k
                                         ModuleMacroInfo &Info) {
182
159k
  assert(Info.ActiveModuleMacrosGeneration !=
183
159k
             CurSubmoduleState->VisibleModules.getGeneration() &&
184
159k
         "don't need to update this macro name info");
185
159k
  Info.ActiveModuleMacrosGeneration =
186
159k
      CurSubmoduleState->VisibleModules.getGeneration();
187
188
159k
  auto Leaf = LeafModuleMacros.find(II);
189
159k
  if (Leaf == LeafModuleMacros.end()) {
190
    // No imported macros at all: nothing to do.
191
41.0k
    return;
192
41.0k
  }
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.21M
  for (auto *LeafMM : Leaf->second) {
204
2.21M
    assert(LeafMM->getNumOverridingMacros() == 0 && "leaf macro overridden");
205
2.21M
    if (NumHiddenOverrides.lookup(LeafMM) == 0)
206
2.21M
      Worklist.push_back(LeafMM);
207
2.21M
  }
208
2.33M
  
while (118k
!Worklist.empty()) {
209
2.21M
    auto *MM = Worklist.pop_back_val();
210
2.21M
    if (CurSubmoduleState->VisibleModules.isVisible(MM->getOwningModule())) {
211
      // We only care about collecting definitions; undefinitions only act
212
      // to override other definitions.
213
901k
      if (MM->getMacroInfo())
214
901k
        Info.ActiveModuleMacros.push_back(MM);
215
1.31M
    } else {
216
1.31M
      for (auto *O : MM->overrides())
217
90
        if ((unsigned)++NumHiddenOverrides[O] == O->getNumOverridingMacros())
218
83
          Worklist.push_back(O);
219
1.31M
    }
220
2.21M
  }
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
5.02k
    while (MD && isa<VisibilityMacroDirective>(MD))
230
0
      MD = MD->getPrevious();
231
5.02k
    if (auto *DMD = dyn_cast_or_null<DefMacroDirective>(MD)) {
232
4.99k
      MI = DMD->getInfo();
233
4.99k
      IsSystemMacro &= SourceMgr.isInSystemHeader(DMD->getLocation());
234
4.99k
    }
235
5.02k
  }
236
901k
  for (auto *Active : Info.ActiveModuleMacros) {
237
901k
    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
901k
    if (MI && 
NewMI != MI795k
&&
249
901k
        
!MI->isIdenticalTo(*NewMI, *this, /*Syntactically=*/true)795k
)
250
265
      IsAmbiguous = true;
251
901k
    IsSystemMacro &= Active->getOwningModule()->IsSystem ||
252
901k
                     
SourceMgr.isInSystemHeader(NewMI->getDefinitionLoc())686
;
253
901k
    MI = NewMI;
254
901k
  }
255
118k
  Info.IsAmbiguous = IsAmbiguous && 
!IsSystemMacro265
;
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.76M
static IdentifierInfo *RegisterBuiltinMacro(Preprocessor &PP, const char *Name){
327
  // Get the identifier.
328
2.76M
  IdentifierInfo *Id = PP.getIdentifierInfo(Name);
329
330
  // Mark it as being a macro that is builtin.
331
2.76M
  MacroInfo *MI = PP.AllocateMacroInfo(SourceLocation());
332
2.76M
  MI->setIsBuiltinMacro();
333
2.76M
  PP.appendDefMacroDirective(Id, MI);
334
2.76M
  return Id;
335
2.76M
}
336
337
/// RegisterBuiltinMacros - Register builtin macros, such as __LINE__ with the
338
/// identifier table.
339
94.4k
void Preprocessor::RegisterBuiltinMacros() {
340
94.4k
  Ident__LINE__ = RegisterBuiltinMacro(*this, "__LINE__");
341
94.4k
  Ident__FILE__ = RegisterBuiltinMacro(*this, "__FILE__");
342
94.4k
  Ident__DATE__ = RegisterBuiltinMacro(*this, "__DATE__");
343
94.4k
  Ident__TIME__ = RegisterBuiltinMacro(*this, "__TIME__");
344
94.4k
  Ident__COUNTER__ = RegisterBuiltinMacro(*this, "__COUNTER__");
345
94.4k
  Ident_Pragma  = RegisterBuiltinMacro(*this, "_Pragma");
346
94.4k
  Ident__FLT_EVAL_METHOD__ = RegisterBuiltinMacro(*this, "__FLT_EVAL_METHOD__");
347
348
  // C++ Standing Document Extensions.
349
94.4k
  if (getLangOpts().CPlusPlus)
350
70.9k
    Ident__has_cpp_attribute =
351
70.9k
        RegisterBuiltinMacro(*this, "__has_cpp_attribute");
352
23.4k
  else
353
23.4k
    Ident__has_cpp_attribute = nullptr;
354
355
  // GCC Extensions.
356
94.4k
  Ident__BASE_FILE__     = RegisterBuiltinMacro(*this, "__BASE_FILE__");
357
94.4k
  Ident__INCLUDE_LEVEL__ = RegisterBuiltinMacro(*this, "__INCLUDE_LEVEL__");
358
94.4k
  Ident__TIMESTAMP__     = RegisterBuiltinMacro(*this, "__TIMESTAMP__");
359
360
  // Microsoft Extensions.
361
94.4k
  if (getLangOpts().MicrosoftExt) {
362
11.9k
    Ident__identifier = RegisterBuiltinMacro(*this, "__identifier");
363
11.9k
    Ident__pragma = RegisterBuiltinMacro(*this, "__pragma");
364
82.5k
  } else {
365
82.5k
    Ident__identifier = nullptr;
366
82.5k
    Ident__pragma = nullptr;
367
82.5k
  }
368
369
  // Clang Extensions.
370
94.4k
  Ident__FILE_NAME__      = RegisterBuiltinMacro(*this, "__FILE_NAME__");
371
94.4k
  Ident__has_feature      = RegisterBuiltinMacro(*this, "__has_feature");
372
94.4k
  Ident__has_extension    = RegisterBuiltinMacro(*this, "__has_extension");
373
94.4k
  Ident__has_builtin      = RegisterBuiltinMacro(*this, "__has_builtin");
374
94.4k
  Ident__has_constexpr_builtin =
375
94.4k
      RegisterBuiltinMacro(*this, "__has_constexpr_builtin");
376
94.4k
  Ident__has_attribute    = RegisterBuiltinMacro(*this, "__has_attribute");
377
94.4k
  if (!getLangOpts().CPlusPlus)
378
23.4k
    Ident__has_c_attribute = RegisterBuiltinMacro(*this, "__has_c_attribute");
379
70.9k
  else
380
70.9k
    Ident__has_c_attribute = nullptr;
381
382
94.4k
  Ident__has_declspec = RegisterBuiltinMacro(*this, "__has_declspec_attribute");
383
94.4k
  Ident__has_include      = RegisterBuiltinMacro(*this, "__has_include");
384
94.4k
  Ident__has_include_next = RegisterBuiltinMacro(*this, "__has_include_next");
385
94.4k
  Ident__has_warning      = RegisterBuiltinMacro(*this, "__has_warning");
386
94.4k
  Ident__is_identifier    = RegisterBuiltinMacro(*this, "__is_identifier");
387
94.4k
  Ident__is_target_arch   = RegisterBuiltinMacro(*this, "__is_target_arch");
388
94.4k
  Ident__is_target_vendor = RegisterBuiltinMacro(*this, "__is_target_vendor");
389
94.4k
  Ident__is_target_os     = RegisterBuiltinMacro(*this, "__is_target_os");
390
94.4k
  Ident__is_target_environment =
391
94.4k
      RegisterBuiltinMacro(*this, "__is_target_environment");
392
94.4k
  Ident__is_target_variant_os =
393
94.4k
      RegisterBuiltinMacro(*this, "__is_target_variant_os");
394
94.4k
  Ident__is_target_variant_environment =
395
94.4k
      RegisterBuiltinMacro(*this, "__is_target_variant_environment");
396
397
  // Modules.
398
94.4k
  Ident__building_module  = RegisterBuiltinMacro(*this, "__building_module");
399
94.4k
  if (!getLangOpts().CurrentModule.empty())
400
3.41k
    Ident__MODULE__ = RegisterBuiltinMacro(*this, "__MODULE__");
401
91.0k
  else
402
91.0k
    Ident__MODULE__ = nullptr;
403
94.4k
}
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
14.4M
                                          Preprocessor &PP) {
410
14.4M
  IdentifierInfo *II = MI->getReplacementToken(0).getIdentifierInfo();
411
412
  // If the token isn't an identifier, it's always literally expanded.
413
14.4M
  if (!II) 
return true3.52M
;
414
415
  // If the information about this identifier is out of date, update it from
416
  // the external source.
417
10.8M
  if (II->isOutOfDate())
418
274
    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
10.8M
  if (auto *ExpansionMI = PP.getMacroInfo(II))
423
1.46M
    if (ExpansionMI->isEnabled() &&
424
        // Fast expanding "#define X X" is ok, because X would be disabled.
425
1.46M
        
II != MacroIdent1.46M
)
426
1.45M
      return false;
427
428
  // If this is an object-like macro invocation, it is safe to trivially expand
429
  // it.
430
9.42M
  if (MI->isObjectLike()) 
return true6.76M
;
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
9.42M
}
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
34.5M
bool Preprocessor::isNextPPTokenLParen() {
441
  // Do some quick tests for rejection cases.
442
34.5M
  unsigned Val;
443
34.5M
  if (CurLexer)
444
3.14M
    Val = CurLexer->isNextPPTokenLParen();
445
31.4M
  else
446
31.4M
    Val = CurTokenLexer->isNextTokenLParen();
447
448
34.5M
  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
1.36k
        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
1.74k
      if (Entry.ThePPLexer)
465
2
        return false;
466
1.74k
    }
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
34.5M
  return Val == 1;
473
34.5M
}
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
90.1M
                                                 const MacroDefinition &M) {
479
90.1M
  emitMacroExpansionWarnings(Identifier);
480
481
90.1M
  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
90.1M
  if (CurPPLexer) 
CurPPLexer->MIOpt.ExpandedMacro()45.1M
;
487
488
  // If this is a builtin macro, like __LINE__ or _Pragma, handle it specially.
489
90.1M
  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
88.7M
  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
88.7M
  SourceLocation ExpansionEnd = Identifier.getLocation();
505
506
  // If this is a function-like macro, read the arguments.
507
88.7M
  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
32.2M
    InMacroArgs = true;
512
32.2M
    ArgMacro = &Identifier;
513
514
32.2M
    Args = ReadMacroCallArgumentList(Identifier, MI, ExpansionEnd);
515
516
    // Finished parsing args.
517
32.2M
    InMacroArgs = false;
518
32.2M
    ArgMacro = nullptr;
519
520
    // If there was an error parsing the arguments, bail out.
521
32.2M
    if (!Args) 
return true54
;
522
523
32.2M
    ++NumFnMacroExpanded;
524
56.4M
  } else {
525
56.4M
    ++NumMacroExpanded;
526
56.4M
  }
527
528
  // Notice that this macro has been used.
529
88.7M
  markMacroAsUsed(MI);
530
531
  // Remember where the token is expanded.
532
88.7M
  SourceLocation ExpandLoc = Identifier.getLocation();
533
88.7M
  SourceRange ExpansionRange(ExpandLoc, ExpansionEnd);
534
535
88.7M
  if (Callbacks) {
536
88.6M
    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
88.6M
    } else {
544
88.6M
      Callbacks->MacroExpands(Identifier, M, ExpansionRange, Args);
545
88.6M
      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
88.6M
    }
554
88.6M
  }
555
556
  // If the macro definition is ambiguous, complain.
557
88.7M
  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
88.7M
  if (MI->getNumTokens() == 0) {
574
    // No need for arg info.
575
1.54M
    if (Args) 
Args->destroy(*this)16.9k
;
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
87.1M
  } else if (MI->getNumTokens() == 1 &&
584
87.1M
             isTrivialSingleTokenExpansion(MI, Identifier.getIdentifierInfo(),
585
14.4M
                                           *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
10.2M
    if (Args) 
Args->destroy(*this)98
;
592
593
    // Propagate the isAtStartOfLine/hasLeadingSpace markers of the macro
594
    // identifier to the expanded token.
595
10.2M
    bool isAtStartOfLine = Identifier.isAtStartOfLine();
596
10.2M
    bool hasLeadingSpace = Identifier.hasLeadingSpace();
597
598
    // Replace the result token.
599
10.2M
    Identifier = MI->getReplacementToken(0);
600
601
    // Restore the StartOfLine/LeadingSpace markers.
602
10.2M
    Identifier.setFlagValue(Token::StartOfLine , isAtStartOfLine);
603
10.2M
    Identifier.setFlagValue(Token::LeadingSpace, hasLeadingSpace);
604
605
    // Update the tokens location to include both its expansion and physical
606
    // locations.
607
10.2M
    SourceLocation Loc =
608
10.2M
      SourceMgr.createExpansionLoc(Identifier.getLocation(), ExpandLoc,
609
10.2M
                                   ExpansionEnd,Identifier.getLength());
610
10.2M
    Identifier.setLocation(Loc);
611
612
    // If this is a disabled macro or #define X X, we must mark the result as
613
    // unexpandable.
614
10.2M
    if (IdentifierInfo *NewII = Identifier.getIdentifierInfo()) {
615
6.76M
      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
6.76M
    }
624
625
    // Since this is not an identifier token, it can't be macro expanded, so
626
    // we're done.
627
10.2M
    ++NumFastMacroExpanded;
628
10.2M
    return true;
629
10.2M
  }
630
631
  // Start expanding the macro.
632
76.8M
  EnterMacro(Identifier, ExpansionEnd, MI, Args);
633
76.8M
  return false;
634
88.7M
}
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
32.2M
                                                   SourceLocation &MacroEnd) {
771
  // The number of fixed arguments to parse.
772
32.2M
  unsigned NumFixedArgsLeft = MI->getNumParams();
773
32.2M
  bool isVariadic = MI->isVariadic();
774
775
  // Outer loop, while there are more arguments, keep reading them.
776
32.2M
  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
32.2M
  LexUnexpandedToken(Tok);
781
32.2M
  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
32.2M
  SmallVector<Token, 64> ArgTokens;
787
32.2M
  bool ContainsCodeCompletionTok = false;
788
32.2M
  bool FoundElidedComma = false;
789
790
32.2M
  SourceLocation TooManyArgsLoc;
791
792
32.2M
  unsigned NumActuals = 0;
793
97.3M
  while (Tok.isNot(tok::r_paren)) {
794
65.5M
    if (ContainsCodeCompletionTok && 
Tok.isOneOf(tok::eof, tok::eod)16
)
795
9
      break;
796
797
65.5M
    assert(Tok.isOneOf(tok::l_paren, tok::comma) &&
798
65.5M
           "only expect argument separators here");
799
800
65.5M
    size_t ArgTokenStart = ArgTokens.size();
801
65.5M
    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
65.5M
    unsigned NumParens = 0;
806
807
214M
    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
214M
      LexUnexpandedToken(Tok);
811
812
214M
      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
214M
      } else if (Tok.is(tok::r_paren)) {
827
        // If we found the ) token, the macro arg list is done.
828
50.8M
        if (NumParens-- == 0) {
829
32.2M
          MacroEnd = Tok.getLocation();
830
32.2M
          if (!ArgTokens.empty() &&
831
32.2M
              
ArgTokens.back().commaAfterElided()31.8M
) {
832
1.99k
            FoundElidedComma = true;
833
1.99k
          }
834
32.2M
          break;
835
32.2M
        }
836
163M
      } else if (Tok.is(tok::l_paren)) {
837
18.6M
        ++NumParens;
838
145M
      } 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
46.3M
        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
46.3M
        } 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
40.9M
          if (!isVariadic)
854
15.9M
            break;
855
24.9M
          if (NumFixedArgsLeft > 1)
856
17.3M
            break;
857
24.9M
        }
858
98.7M
      } 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
98.7M
      } else if (!Tok.isAnnotation() && 
Tok.getIdentifierInfo() != nullptr98.7M
) {
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
62.5M
        if (MacroInfo *MI = getMacroInfo(Tok.getIdentifierInfo()))
870
29.3M
          if (!MI->isEnabled())
871
1.89k
            Tok.setFlag(Token::DisableExpand);
872
62.5M
      } 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
148M
      ArgTokens.push_back(Tok);
883
148M
    }
884
885
    // If this was an empty argument list foo(), don't add this as an empty
886
    // argument.
887
65.5M
    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
65.1M
    if (!isVariadic && 
NumFixedArgsLeft == 042.7M
&&
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
65.1M
    if (ArgTokens.size() == ArgTokenStart && 
!getLangOpts().C9985.3k
)
902
30.5k
      Diag(Tok, getLangOpts().CPlusPlus11
903
30.5k
                    ? 
diag::warn_cxx98_compat_empty_fnmacro_arg30.4k
904
30.5k
                    : 
diag::ext_empty_fnmacro_arg22
);
905
906
    // Add a marker EOF token to the end of the token list for this argument.
907
65.1M
    Token EOFTok;
908
65.1M
    EOFTok.startToken();
909
65.1M
    EOFTok.setKind(tok::eof);
910
65.1M
    EOFTok.setLocation(Tok.getLocation());
911
65.1M
    EOFTok.setLength(0);
912
65.1M
    ArgTokens.push_back(EOFTok);
913
65.1M
    ++NumActuals;
914
65.1M
    if (!ContainsCodeCompletionTok && 
NumFixedArgsLeft != 065.1M
)
915
65.1M
      --NumFixedArgsLeft;
916
65.1M
  }
917
918
  // Okay, we either found the r_paren.  Check to see if we parsed too few
919
  // arguments.
920
32.2M
  unsigned MinArgsExpected = MI->getNumParams();
921
922
  // If this is not a variadic macro, and too many args were specified, emit
923
  // an error.
924
32.2M
  if (!isVariadic && 
NumActuals > MinArgsExpected27.1M
&&
925
32.2M
      
!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
32.2M
  bool isVarargsElided = false;
965
966
32.2M
  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
32.2M
  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
31.7M
  } else if (NumActuals > MinArgsExpected && 
!MI->isVariadic()1
&&
1033
31.7M
             
!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
32.2M
  return MacroArgs::create(MI, ArgTokens, isVarargsElided, *this);
1043
32.2M
}
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
32.1M
                                              ArrayRef<Token> tokens) {
1052
32.1M
  assert(tokLexer);
1053
32.1M
  if (tokens.empty())
1054
262k
    return nullptr;
1055
1056
31.8M
  size_t newIndex = MacroExpandedTokens.size();
1057
31.8M
  bool cacheNeedsToGrow = tokens.size() >
1058
31.8M
                      MacroExpandedTokens.capacity()-MacroExpandedTokens.size();
1059
31.8M
  MacroExpandedTokens.append(tokens.begin(), tokens.end());
1060
1061
31.8M
  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.8k
    for (const auto &Lexer : MacroExpandingLexersStack) {
1065
12.6k
      TokenLexer *prevLexer;
1066
12.6k
      size_t tokIndex;
1067
12.6k
      std::tie(prevLexer, tokIndex) = Lexer;
1068
12.6k
      prevLexer->Tokens = MacroExpandedTokens.data() + tokIndex;
1069
12.6k
    }
1070
13.8k
  }
1071
1072
31.8M
  MacroExpandingLexersStack.push_back(std::make_pair(tokLexer, newIndex));
1073
31.8M
  return MacroExpandedTokens.data() + newIndex;
1074
32.1M
}
1075
1076
31.8M
void Preprocessor::removeCachedMacroExpandedTokensOfLastLexer() {
1077
31.8M
  assert(!MacroExpandingLexersStack.empty());
1078
31.8M
  size_t tokIndex = MacroExpandingLexersStack.back().second;
1079
31.8M
  assert(tokIndex < MacroExpandedTokens.size());
1080
  // Pop the cached macro expanded tokens from the end.
1081
31.8M
  MacroExpandedTokens.resize(tokIndex);
1082
31.8M
  MacroExpandingLexersStack.pop_back();
1083
31.8M
}
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
517k
static bool HasFeature(const Preprocessor &PP, StringRef Feature) {
1136
517k
  const LangOptions &LangOpts = PP.getLangOpts();
1137
1138
  // Normalize the feature name, __foo__ becomes foo.
1139
517k
  if (Feature.startswith("__") && 
Feature.endswith("__")7
&&
Feature.size() >= 46
)
1140
6
    Feature = Feature.substr(2, Feature.size() - 4);
1141
1142
89.0M
#define FEATURE(Name, Predicate) .Case(#Name, 
Predicate4.55M
)
1143
517k
  return llvm::StringSwitch<bool>(Feature)
1144
517k
#include "clang/Basic/Features.def"
1145
517k
      .Default(false);
1146
517k
#undef FEATURE
1147
517k
}
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.86k
  if (PP.getDiagnostics().getExtensionHandlingBehavior() >=
1159
8.86k
      diag::Severity::Error)
1160
7
    return false;
1161
1162
8.85k
  const LangOptions &LangOpts = PP.getLangOpts();
1163
1164
  // Normalize the extension name, __foo__ becomes foo.
1165
8.85k
  if (Extension.startswith("__") && 
Extension.endswith("__")1
&&
1166
8.85k
      
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
327k
#define EXTENSION(Name, Predicate) .Case(#Name, 
Predicate8.85k
)
1172
8.85k
  return llvm::StringSwitch<bool>(Extension)
1173
8.85k
#include "clang/Basic/Features.def"
1174
8.85k
      .Default(false);
1175
8.86k
#undef EXTENSION
1176
8.86k
}
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.3k
                                     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.3k
  SourceLocation LParenLoc = Tok.getLocation();
1188
1189
  // These expressions are only allowed within a preprocessor directive.
1190
24.3k
  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.3k
  do {
1200
24.3k
    if (PP.LexHeaderName(Tok))
1201
0
      return false;
1202
24.3k
  } while (Tok.getKind() == tok::comment);
1203
1204
  // Ensure we have a '('.
1205
24.3k
  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.3k
  } else {
1214
    // Save '(' location for possible missing ')' message.
1215
24.3k
    LParenLoc = Tok.getLocation();
1216
24.3k
    if (PP.LexHeaderName(Tok))
1217
3
      return false;
1218
24.3k
  }
1219
1220
24.3k
  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.3k
  SmallString<128> FilenameBuffer;
1227
24.3k
  bool Invalid = false;
1228
24.3k
  StringRef Filename = PP.getSpelling(Tok, FilenameBuffer, &Invalid);
1229
24.3k
  if (Invalid)
1230
0
    return false;
1231
1232
24.3k
  SourceLocation FilenameLoc = Tok.getLocation();
1233
1234
  // Get ')'.
1235
24.3k
  PP.LexNonComment(Tok);
1236
1237
  // Ensure we have a trailing ).
1238
24.3k
  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.3k
  bool isAngled = PP.GetIncludeFilenameSpelling(Tok.getLocation(), Filename);
1246
  // If GetIncludeFilenameSpelling set the start ptr to null, there was an
1247
  // error.
1248
24.3k
  if (Filename.empty())
1249
1
    return false;
1250
1251
  // Passing this to LookupFile forces header search to check whether the found
1252
  // file belongs to a module. Skipping that check could incorrectly mark
1253
  // modular header as textual, causing issues down the line.
1254
24.3k
  ModuleMap::KnownHeader KH;
1255
1256
  // Search include directories.
1257
24.3k
  OptionalFileEntryRef File =
1258
24.3k
      PP.LookupFile(FilenameLoc, Filename, isAngled, LookupFrom, LookupFromFile,
1259
24.3k
                    nullptr, nullptr, nullptr, &KH, nullptr, nullptr);
1260
1261
24.3k
  if (PPCallbacks *Callbacks = PP.getPPCallbacks()) {
1262
24.3k
    SrcMgr::CharacteristicKind FileType = SrcMgr::C_User;
1263
24.3k
    if (File)
1264
14.8k
      FileType = PP.getHeaderSearchInfo().getFileDirFlavor(*File);
1265
24.3k
    Callbacks->HasInclude(FilenameLoc, Filename, isAngled, File, FileType);
1266
24.3k
  }
1267
1268
  // Get the result value.  A result of true means the file exists.
1269
24.3k
  return File.has_value();
1270
24.3k
}
1271
1272
12.8k
bool Preprocessor::EvaluateHasInclude(Token &Tok, IdentifierInfo *II) {
1273
12.8k
  return EvaluateHasIncludeCommon(Tok, II, *this, nullptr, nullptr);
1274
12.8k
}
1275
1276
11.5k
bool Preprocessor::EvaluateHasIncludeNext(Token &Tok, IdentifierInfo *II) {
1277
11.5k
  ConstSearchDirIterator Lookup = nullptr;
1278
11.5k
  const FileEntry *LookupFromFile;
1279
11.5k
  std::tie(Lookup, LookupFromFile) = getIncludeNextStart(Tok);
1280
1281
11.5k
  return EvaluateHasIncludeCommon(Tok, II, *this, Lookup, LookupFromFile);
1282
11.5k
}
1283
1284
/// Process single-argument builtin feature-like macros that return
1285
/// integer values.
1286
static void EvaluateFeatureLikeBuiltinMacro(llvm::raw_svector_ostream& OS,
1287
                                            Token &Tok, IdentifierInfo *II,
1288
                                            Preprocessor &PP, bool ExpandArgs,
1289
                                            llvm::function_ref<
1290
                                              int(Token &Tok,
1291
699k
                                                  bool &HasLexedNextTok)> Op) {
1292
  // Parse the initial '('.
1293
699k
  PP.LexUnexpandedToken(Tok);
1294
699k
  if (Tok.isNot(tok::l_paren)) {
1295
6
    PP.Diag(Tok.getLocation(), diag::err_pp_expected_after) << II
1296
6
                                                            << tok::l_paren;
1297
1298
    // Provide a dummy '0' value on output stream to elide further errors.
1299
6
    if (!Tok.isOneOf(tok::eof, tok::eod)) {
1300
2
      OS << 0;
1301
2
      Tok.setKind(tok::numeric_constant);
1302
2
    }
1303
6
    return;
1304
6
  }
1305
1306
699k
  unsigned ParenDepth = 1;
1307
699k
  SourceLocation LParenLoc = Tok.getLocation();
1308
699k
  std::optional<int> Result;
1309
1310
699k
  Token ResultTok;
1311
699k
  bool SuppressDiagnostic = false;
1312
1.39M
  while (true) {
1313
    // Parse next token.
1314
1.39M
    if (ExpandArgs)
1315
173k
      PP.Lex(Tok);
1316
1.22M
    else
1317
1.22M
      PP.LexUnexpandedToken(Tok);
1318
1319
1.39M
already_lexed:
1320
1.39M
    switch (Tok.getKind()) {
1321
1
      case tok::eof:
1322
4
      case tok::eod:
1323
        // Don't provide even a dummy value if the eod or eof marker is
1324
        // reached.  Simply provide a diagnostic.
1325
4
        PP.Diag(Tok.getLocation(), diag::err_unterm_macro_invoc);
1326
4
        return;
1327
1328
4
      case tok::comma:
1329
4
        if (!SuppressDiagnostic) {
1330
4
          PP.Diag(Tok.getLocation(), diag::err_too_many_args_in_macro_invoc);
1331
4
          SuppressDiagnostic = true;
1332
4
        }
1333
4
        continue;
1334
1335
6
      case tok::l_paren:
1336
6
        ++ParenDepth;
1337
6
        if (Result)
1338
2
          break;
1339
4
        if (!SuppressDiagnostic) {
1340
2
          PP.Diag(Tok.getLocation(), diag::err_pp_nested_paren) << II;
1341
2
          SuppressDiagnostic = true;
1342
2
        }
1343
4
        continue;
1344
1345
699k
      case tok::r_paren:
1346
699k
        if (--ParenDepth > 0)
1347
6
          continue;
1348
1349
        // The last ')' has been reached; return the value if one found or
1350
        // a diagnostic and a dummy value.
1351
699k
        if (Result) {
1352
699k
          OS << *Result;
1353
          // For strict conformance to __has_cpp_attribute rules, use 'L'
1354
          // suffix for dated literals.
1355
699k
          if (*Result > 1)
1356
7.47k
            OS << 'L';
1357
699k
        } else {
1358
5
          OS << 0;
1359
5
          if (!SuppressDiagnostic)
1360
3
            PP.Diag(Tok.getLocation(), diag::err_too_few_args_in_macro_invoc);
1361
5
        }
1362
699k
        Tok.setKind(tok::numeric_constant);
1363
699k
        return;
1364
1365
699k
      default: {
1366
        // Parse the macro argument, if one not found so far.
1367
699k
        if (Result)
1368
9
          break;
1369
1370
699k
        bool HasLexedNextToken = false;
1371
699k
        Result = Op(Tok, HasLexedNextToken);
1372
699k
        ResultTok = Tok;
1373
699k
        if (HasLexedNextToken)
1374
5.50k
          goto already_lexed;
1375
693k
        continue;
1376
699k
      }
1377
1.39M
    }
1378
1379
    // Diagnose missing ')'.
1380
11
    if (!SuppressDiagnostic) {
1381
8
      if (auto Diag = PP.Diag(Tok.getLocation(), diag::err_pp_expected_after)) {
1382
8
        if (IdentifierInfo *LastII = ResultTok.getIdentifierInfo())
1383
4
          Diag << LastII;
1384
4
        else
1385
4
          Diag << ResultTok.getKind();
1386
8
        Diag << tok::r_paren << ResultTok.getLocation();
1387
8
      }
1388
8
      PP.Diag(LParenLoc, diag::note_matching) << tok::l_paren;
1389
8
      SuppressDiagnostic = true;
1390
8
    }
1391
11
  }
1392
699k
}
1393
1394
/// Helper function to return the IdentifierInfo structure of a Token
1395
/// or generate a diagnostic if none available.
1396
static IdentifierInfo *ExpectFeatureIdentifierInfo(Token &Tok,
1397
                                                   Preprocessor &PP,
1398
699k
                                                   signed DiagID) {
1399
699k
  IdentifierInfo *II;
1400
699k
  if (!Tok.isAnnotation() && (II = Tok.getIdentifierInfo()))
1401
699k
    return II;
1402
1403
6
  PP.Diag(Tok.getLocation(), DiagID);
1404
6
  return nullptr;
1405
699k
}
1406
1407
/// Implements the __is_target_arch builtin macro.
1408
6.47k
static bool isTargetArch(const TargetInfo &TI, const IdentifierInfo *II) {
1409
6.47k
  std::string ArchName = II->getName().lower() + "--";
1410
6.47k
  llvm::Triple Arch(ArchName);
1411
6.47k
  const llvm::Triple &TT = TI.getTriple();
1412
6.47k
  if (TT.isThumb()) {
1413
    // arm matches thumb or thumbv7. armv7 matches thumbv7.
1414
11
    if ((Arch.getSubArch() == llvm::Triple::NoSubArch ||
1415
11
         
Arch.getSubArch() == TT.getSubArch()7
) &&
1416
11
        
(8
(8
TT.getArch() == llvm::Triple::thumb8
&&
1417
8
          Arch.getArch() == llvm::Triple::arm) ||
1418
8
         
(6
TT.getArch() == llvm::Triple::thumbeb6
&&
1419
6
          
Arch.getArch() == llvm::Triple::armeb0
)))
1420
2
      return true;
1421
11
  }
1422
  // Check the parsed arch when it has no sub arch to allow Clang to
1423
  // match thumb to thumbv7 but to prohibit matching thumbv6 to thumbv7.
1424
6.47k
  return (Arch.getSubArch() == llvm::Triple::NoSubArch ||
1425
6.47k
          
Arch.getSubArch() == TT.getSubArch()1.72k
) &&
1426
6.47k
         
Arch.getArch() == TT.getArch()4.75k
;
1427
6.47k
}
1428
1429
/// Implements the __is_target_vendor builtin macro.
1430
4.74k
static bool isTargetVendor(const TargetInfo &TI, const IdentifierInfo *II) {
1431
4.74k
  StringRef VendorName = TI.getTriple().getVendorName();
1432
4.74k
  if (VendorName.empty())
1433
0
    VendorName = "unknown";
1434
4.74k
  return VendorName.equals_insensitive(II->getName());
1435
4.74k
}
1436
1437
/// Implements the __is_target_os builtin macro.
1438
4.75k
static bool isTargetOS(const TargetInfo &TI, const IdentifierInfo *II) {
1439
4.75k
  std::string OSName =
1440
4.75k
      (llvm::Twine("unknown-unknown-") + II->getName().lower()).str();
1441
4.75k
  llvm::Triple OS(OSName);
1442
4.75k
  if (OS.getOS() == llvm::Triple::Darwin) {
1443
    // Darwin matches macos, ios, etc.
1444
6
    return TI.getTriple().isOSDarwin();
1445
6
  }
1446
4.75k
  return TI.getTriple().getOS() == OS.getOS();
1447
4.75k
}
1448
1449
/// Implements the __is_target_environment builtin macro.
1450
static bool isTargetEnvironment(const TargetInfo &TI,
1451
4.32k
                                const IdentifierInfo *II) {
1452
4.32k
  std::string EnvName = (llvm::Twine("---") + II->getName().lower()).str();
1453
4.32k
  llvm::Triple Env(EnvName);
1454
  // The unknown environment is matched only if
1455
  // '__is_target_environment(unknown)' is used.
1456
4.32k
  if (Env.getEnvironment() == llvm::Triple::UnknownEnvironment &&
1457
4.32k
      
EnvName != "---unknown"6
)
1458
2
    return false;
1459
4.31k
  return TI.getTriple().getEnvironment() == Env.getEnvironment();
1460
4.32k
}
1461
1462
/// Implements the __is_target_variant_os builtin macro.
1463
3.46k
static bool isTargetVariantOS(const TargetInfo &TI, const IdentifierInfo *II) {
1464
3.46k
  if (TI.getTriple().isOSDarwin()) {
1465
3.46k
    const llvm::Triple *VariantTriple = TI.getDarwinTargetVariantTriple();
1466
3.46k
    if (!VariantTriple)
1467
3.45k
      return false;
1468
1469
3
    std::string OSName =
1470
3
        (llvm::Twine("unknown-unknown-") + II->getName().lower()).str();
1471
3
    llvm::Triple OS(OSName);
1472
3
    if (OS.getOS() == llvm::Triple::Darwin) {
1473
      // Darwin matches macos, ios, etc.
1474
1
      return VariantTriple->isOSDarwin();
1475
1
    }
1476
2
    return VariantTriple->getOS() == OS.getOS();
1477
3
  }
1478
2
  return false;
1479
3.46k
}
1480
1481
/// Implements the __is_target_variant_environment builtin macro.
1482
static bool isTargetVariantEnvironment(const TargetInfo &TI,
1483
3.45k
                                const IdentifierInfo *II) {
1484
3.45k
  if (TI.getTriple().isOSDarwin()) {
1485
3.45k
    const llvm::Triple *VariantTriple = TI.getDarwinTargetVariantTriple();
1486
3.45k
    if (!VariantTriple)
1487
3.45k
      return false;
1488
1
    std::string EnvName = (llvm::Twine("---") + II->getName().lower()).str();
1489
1
    llvm::Triple Env(EnvName);
1490
1
    return VariantTriple->getEnvironment() == Env.getEnvironment();
1491
3.45k
  }
1492
2
  return false;
1493
3.45k
}
1494
1495
/// ExpandBuiltinMacro - If an identifier token is read that is to be expanded
1496
/// as a builtin macro, handle it and return the next token as 'Tok'.
1497
1.44M
void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
1498
  // Figure out which token this is.
1499
1.44M
  IdentifierInfo *II = Tok.getIdentifierInfo();
1500
1.44M
  assert(II && "Can't be a macro without id info!");
1501
1502
  // If this is an _Pragma or Microsoft __pragma directive, expand it,
1503
  // invoke the pragma handler, then lex the token after it.
1504
1.44M
  if (II == Ident_Pragma)
1505
719k
    return Handle_Pragma(Tok);
1506
729k
  else if (II == Ident__pragma) // in non-MS mode this is null
1507
47
    return HandleMicrosoft__pragma(Tok);
1508
1509
729k
  ++NumBuiltinMacroExpanded;
1510
1511
729k
  SmallString<128> TmpBuffer;
1512
729k
  llvm::raw_svector_ostream OS(TmpBuffer);
1513
1514
  // Set up the return result.
1515
729k
  Tok.setIdentifierInfo(nullptr);
1516
729k
  Tok.clearFlag(Token::NeedsCleaning);
1517
729k
  bool IsAtStartOfLine = Tok.isAtStartOfLine();
1518
729k
  bool HasLeadingSpace = Tok.hasLeadingSpace();
1519
1520
729k
  if (II == Ident__LINE__) {
1521
    // C99 6.10.8: "__LINE__: The presumed line number (within the current
1522
    // source file) of the current source line (an integer constant)".  This can
1523
    // be affected by #line.
1524
3.58k
    SourceLocation Loc = Tok.getLocation();
1525
1526
    // Advance to the location of the first _, this might not be the first byte
1527
    // of the token if it starts with an escaped newline.
1528
3.58k
    Loc = AdvanceToTokenCharacter(Loc, 0);
1529
1530
    // One wrinkle here is that GCC expands __LINE__ to location of the *end* of
1531
    // a macro expansion.  This doesn't matter for object-like macros, but
1532
    // can matter for a function-like macro that expands to contain __LINE__.
1533
    // Skip down through expansion points until we find a file loc for the
1534
    // end of the expansion history.
1535
3.58k
    Loc = SourceMgr.getExpansionRange(Loc).getEnd();
1536
3.58k
    PresumedLoc PLoc = SourceMgr.getPresumedLoc(Loc);
1537
1538
    // __LINE__ expands to a simple numeric value.
1539
3.58k
    OS << (PLoc.isValid()? PLoc.getLine() : 
10
);
1540
3.58k
    Tok.setKind(tok::numeric_constant);
1541
726k
  } else if (II == Ident__FILE__ || 
II == Ident__BASE_FILE__725k
||
1542
726k
             
II == Ident__FILE_NAME__725k
) {
1543
    // C99 6.10.8: "__FILE__: The presumed name of the current source file (a
1544
    // character string literal)". This can be affected by #line.
1545
504
    PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
1546
1547
    // __BASE_FILE__ is a GNU extension that returns the top of the presumed
1548
    // #include stack instead of the current file.
1549
504
    if (II == Ident__BASE_FILE__ && 
PLoc.isValid()10
) {
1550
10
      SourceLocation NextLoc = PLoc.getIncludeLoc();
1551
20
      while (NextLoc.isValid()) {
1552
10
        PLoc = SourceMgr.getPresumedLoc(NextLoc);
1553
10
        if (PLoc.isInvalid())
1554
0
          break;
1555
1556
10
        NextLoc = PLoc.getIncludeLoc();
1557
10
      }
1558
10
    }
1559
1560
    // Escape this filename.  Turn '\' -> '\\' '"' -> '\"'
1561
504
    SmallString<256> FN;
1562
504
    if (PLoc.isValid()) {
1563
      // __FILE_NAME__ is a Clang-specific extension that expands to the
1564
      // the last part of __FILE__.
1565
504
      if (II == Ident__FILE_NAME__) {
1566
83
        processPathToFileName(FN, PLoc, getLangOpts(), getTargetInfo());
1567
421
      } else {
1568
421
        FN += PLoc.getFilename();
1569
421
        processPathForFileMacro(FN, getLangOpts(), getTargetInfo());
1570
421
      }
1571
504
      Lexer::Stringify(FN);
1572
504
      OS << '"' << FN << '"';
1573
504
    }
1574
504
    Tok.setKind(tok::string_literal);
1575
725k
  } else if (II == Ident__DATE__) {
1576
10
    Diag(Tok.getLocation(), diag::warn_pp_date_time);
1577
10
    if (!DATELoc.isValid())
1578
7
      ComputeDATE_TIME(DATELoc, TIMELoc, *this);
1579
10
    Tok.setKind(tok::string_literal);
1580
10
    Tok.setLength(strlen("\"Mmm dd yyyy\""));
1581
10
    Tok.setLocation(SourceMgr.createExpansionLoc(DATELoc, Tok.getLocation(),
1582
10
                                                 Tok.getLocation(),
1583
10
                                                 Tok.getLength()));
1584
10
    return;
1585
725k
  } else if (II == Ident__TIME__) {
1586
9
    Diag(Tok.getLocation(), diag::warn_pp_date_time);
1587
9
    if (!TIMELoc.isValid())
1588
3
      ComputeDATE_TIME(DATELoc, TIMELoc, *this);
1589
9
    Tok.setKind(tok::string_literal);
1590
9
    Tok.setLength(strlen("\"hh:mm:ss\""));
1591
9
    Tok.setLocation(SourceMgr.createExpansionLoc(TIMELoc, Tok.getLocation(),
1592
9
                                                 Tok.getLocation(),
1593
9
                                                 Tok.getLength()));
1594
9
    return;
1595
725k
  } else if (II == Ident__INCLUDE_LEVEL__) {
1596
    // Compute the presumed include depth of this token.  This can be affected
1597
    // by GNU line markers.
1598
9
    unsigned Depth = 0;
1599
1600
9
    PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
1601
9
    if (PLoc.isValid()) {
1602
9
      PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc());
1603
15
      for (; PLoc.isValid(); 
++Depth6
)
1604
6
        PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc());
1605
9
    }
1606
1607
    // __INCLUDE_LEVEL__ expands to a simple numeric value.
1608
9
    OS << Depth;
1609
9
    Tok.setKind(tok::numeric_constant);
1610
725k
  } else if (II == Ident__TIMESTAMP__) {
1611
6
    Diag(Tok.getLocation(), diag::warn_pp_date_time);
1612
    // MSVC, ICC, GCC, VisualAge C++ extension.  The generated string should be
1613
    // of the form "Ddd Mmm dd hh::mm::ss yyyy", which is returned by asctime.
1614
6
    const char *Result;
1615
6
    if (getPreprocessorOpts().SourceDateEpoch) {
1616
3
      time_t TT = *getPreprocessorOpts().SourceDateEpoch;
1617
3
      std::tm *TM = std::gmtime(&TT);
1618
3
      Result = asctime(TM);
1619
3
    } else {
1620
      // Get the file that we are lexing out of.  If we're currently lexing from
1621
      // a macro, dig into the include stack.
1622
3
      const FileEntry *CurFile = nullptr;
1623
3
      if (PreprocessorLexer *TheLexer = getCurrentFileLexer())
1624
3
        CurFile = SourceMgr.getFileEntryForID(TheLexer->getFileID());
1625
3
      if (CurFile) {
1626
3
        time_t TT = CurFile->getModificationTime();
1627
3
        struct tm *TM = localtime(&TT);
1628
3
        Result = asctime(TM);
1629
3
      } else {
1630
0
        Result = "??? ??? ?? ??:??:?? ????\n";
1631
0
      }
1632
3
    }
1633
    // Surround the string with " and strip the trailing newline.
1634
6
    OS << '"' << StringRef(Result).drop_back() << '"';
1635
6
    Tok.setKind(tok::string_literal);
1636
725k
  } else if (II == Ident__FLT_EVAL_METHOD__) {
1637
    // __FLT_EVAL_METHOD__ is set to the default value.
1638
884
    OS << getTUFPEvalMethod();
1639
    // __FLT_EVAL_METHOD__ expands to a simple numeric value.
1640
884
    Tok.setKind(tok::numeric_constant);
1641
884
    if (getLastFPEvalPragmaLocation().isValid()) {
1642
      // The program is ill-formed. The value of __FLT_EVAL_METHOD__ is altered
1643
      // by the pragma.
1644
18
      Diag(Tok, diag::err_illegal_use_of_flt_eval_macro);
1645
18
      Diag(getLastFPEvalPragmaLocation(), diag::note_pragma_entered_here);
1646
18
    }
1647
724k
  } else if (II == Ident__COUNTER__) {
1648
    // __COUNTER__ expands to a simple numeric value.
1649
772
    OS << CounterValue++;
1650
772
    Tok.setKind(tok::numeric_constant);
1651
723k
  } else if (II == Ident__has_feature) {
1652
498k
    EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, false,
1653
498k
      [this](Token &Tok, bool &HasLexedNextToken) -> int {
1654
498k
        IdentifierInfo *II = ExpectFeatureIdentifierInfo(Tok, *this,
1655
498k
                                           diag::err_feature_check_malformed);
1656
498k
        return II && 
HasFeature(*this, II->getName())498k
;
1657
498k
      });
1658
498k
  } else 
if (225k
II == Ident__has_extension225k
) {
1659
19.3k
    EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, false,
1660
19.3k
      [this](Token &Tok, bool &HasLexedNextToken) -> int {
1661
19.3k
        IdentifierInfo *II = ExpectFeatureIdentifierInfo(Tok, *this,
1662
19.3k
                                           diag::err_feature_check_malformed);
1663
19.3k
        return II && HasExtension(*this, II->getName());
1664
19.3k
      });
1665
206k
  } else if (II == Ident__has_builtin) {
1666
59.2k
    EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, false,
1667
59.2k
      [this](Token &Tok, bool &HasLexedNextToken) -> int {
1668
59.2k
        IdentifierInfo *II = ExpectFeatureIdentifierInfo(Tok, *this,
1669
59.2k
                                           diag::err_feature_check_malformed);
1670
59.2k
        if (!II)
1671
0
          return false;
1672
59.2k
        else if (II->getBuiltinID() != 0) {
1673
13.7k
          switch (II->getBuiltinID()) {
1674
998
          case Builtin::BI__builtin_operator_new:
1675
1.98k
          case Builtin::BI__builtin_operator_delete:
1676
            // denotes date of behavior change to support calling arbitrary
1677
            // usual allocation and deallocation functions. Required by libc++
1678
1.98k
            return 201802;
1679
11.7k
          default:
1680
11.7k
            return Builtin::evaluateRequiredTargetFeatures(
1681
11.7k
                getBuiltinInfo().getRequiredFeatures(II->getBuiltinID()),
1682
11.7k
                getTargetInfo().getTargetOpts().FeatureMap);
1683
13.7k
          }
1684
0
          return true;
1685
45.5k
        } else if (II->getTokenID() != tok::identifier ||
1686
45.5k
                   
II->hasRevertedTokenIDToIdentifier()24.2k
) {
1687
          // Treat all keywords that introduce a custom syntax of the form
1688
          //
1689
          //   '__some_keyword' '(' [...] ')'
1690
          //
1691
          // as being "builtin functions", even if the syntax isn't a valid
1692
          // function call (for example, because the builtin takes a type
1693
          // argument).
1694
21.3k
          if (II->getName().startswith("__builtin_") ||
1695
21.3k
              
II->getName().startswith("__is_")21.2k
||
1696
21.3k
              
II->getName().startswith("__has_")8.53k
)
1697
12.7k
            return true;
1698
8.53k
          return llvm::StringSwitch<bool>(II->getName())
1699
8.53k
              .Case("__array_rank", true)
1700
8.53k
              .Case("__array_extent", true)
1701
8.53k
              .Case("__reference_binds_to_temporary", true)
1702
8.53k
              .Case("__reference_constructs_from_temporary", true)
1703
136k
#define TRANSFORM_TYPE_TRAIT_DEF(_, Trait) .Case("__" #Trait, true)
1704
8.53k
#include "clang/Basic/TransformTypeTraits.def"
1705
8.53k
              .Default(false);
1706
24.2k
        } else {
1707
24.2k
          return llvm::StringSwitch<bool>(II->getName())
1708
              // Report builtin templates as being builtins.
1709
24.2k
              .Case("__make_integer_seq", getLangOpts().CPlusPlus)
1710
24.2k
              .Case("__type_pack_element", getLangOpts().CPlusPlus)
1711
              // Likewise for some builtin preprocessor macros.
1712
              // FIXME: This is inconsistent; we usually suggest detecting
1713
              // builtin macros via #ifdef. Don't add more cases here.
1714
24.2k
              .Case("__is_target_arch", true)
1715
24.2k
              .Case("__is_target_vendor", true)
1716
24.2k
              .Case("__is_target_os", true)
1717
24.2k
              .Case("__is_target_environment", true)
1718
24.2k
              .Case("__is_target_variant_os", true)
1719
24.2k
              .Case("__is_target_variant_environment", true)
1720
24.2k
              .Default(false);
1721
24.2k
        }
1722
59.2k
      });
1723
147k
  } else if (II == Ident__has_constexpr_builtin) {
1724
2.46k
    EvaluateFeatureLikeBuiltinMacro(
1725
2.46k
        OS, Tok, II, *this, false,
1726
2.46k
        [this](Token &Tok, bool &HasLexedNextToken) -> int {
1727
2.46k
          IdentifierInfo *II = ExpectFeatureIdentifierInfo(
1728
2.46k
              Tok, *this, diag::err_feature_check_malformed);
1729
2.46k
          if (!II)
1730
2
            return false;
1731
2.46k
          unsigned BuiltinOp = II->getBuiltinID();
1732
2.46k
          return BuiltinOp != 0 &&
1733
2.46k
                 
this->getBuiltinInfo().isConstantEvaluated(BuiltinOp)2.45k
;
1734
2.46k
        });
1735
144k
  } else if (II == Ident__is_identifier) {
1736
2.98k
    EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, false,
1737
2.98k
      [](Token &Tok, bool &HasLexedNextToken) -> int {
1738
2.97k
        return Tok.is(tok::identifier);
1739
2.97k
      });
1740
141k
  } else if (II == Ident__has_attribute) {
1741
78.9k
    EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, true,
1742
78.9k
      [this](Token &Tok, bool &HasLexedNextToken) -> int {
1743
78.9k
        IdentifierInfo *II = ExpectFeatureIdentifierInfo(Tok, *this,
1744
78.9k
                                           diag::err_feature_check_malformed);
1745
78.9k
        return II ? hasAttribute(AttributeCommonInfo::Syntax::AS_GNU, nullptr,
1746
78.9k
                                 II, getTargetInfo(), getLangOpts())
1747
78.9k
                  : 
00
;
1748
78.9k
      });
1749
78.9k
  } else 
if (62.7k
II == Ident__has_declspec62.7k
) {
1750
1.66k
    EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, true,
1751
1.66k
      [this](Token &Tok, bool &HasLexedNextToken) -> int {
1752
1.66k
        IdentifierInfo *II = ExpectFeatureIdentifierInfo(Tok, *this,
1753
1.66k
                                           diag::err_feature_check_malformed);
1754
1.66k
        if (II) {
1755
1.66k
          const LangOptions &LangOpts = getLangOpts();
1756
1.66k
          return LangOpts.DeclSpecKeyword &&
1757
1.66k
                 hasAttribute(AttributeCommonInfo::Syntax::AS_Declspec, nullptr,
1758
8
                              II, getTargetInfo(), LangOpts);
1759
1.66k
        }
1760
1761
0
        return false;
1762
1.66k
      });
1763
61.1k
  } else if (II == Ident__has_cpp_attribute ||
1764
61.1k
             
II == Ident__has_c_attribute52.3k
) {
1765
8.88k
    bool IsCXX = II == Ident__has_cpp_attribute;
1766
8.88k
    EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, true,
1767
8.88k
        [&](Token &Tok, bool &HasLexedNextToken) -> int {
1768
8.88k
          IdentifierInfo *ScopeII = nullptr;
1769
8.88k
          IdentifierInfo *II = ExpectFeatureIdentifierInfo(
1770
8.88k
              Tok, *this, diag::err_feature_check_malformed);
1771
8.88k
          if (!II)
1772
1
            return false;
1773
1774
          // It is possible to receive a scope token.  Read the "::", if it is
1775
          // available, and the subsequent identifier.
1776
8.87k
          LexUnexpandedToken(Tok);
1777
8.87k
          if (Tok.isNot(tok::coloncolon))
1778
5.50k
            HasLexedNextToken = true;
1779
3.37k
          else {
1780
3.37k
            ScopeII = II;
1781
            // Lex an expanded token for the attribute name.
1782
3.37k
            Lex(Tok);
1783
3.37k
            II = ExpectFeatureIdentifierInfo(Tok, *this,
1784
3.37k
                                             diag::err_feature_check_malformed);
1785
3.37k
          }
1786
1787
8.87k
          AttributeCommonInfo::Syntax Syntax =
1788
8.87k
              IsCXX ? 
AttributeCommonInfo::Syntax::AS_CXX118.79k
1789
8.87k
                    : 
AttributeCommonInfo::Syntax::AS_C2380
;
1790
8.87k
          return II ? hasAttribute(Syntax, ScopeII, II, getTargetInfo(),
1791
8.87k
                                   getLangOpts())
1792
8.87k
                    : 
00
;
1793
8.88k
        });
1794
52.2k
  } else if (II == Ident__has_include ||
1795
52.2k
             
II == Ident__has_include_next39.4k
) {
1796
    // The argument to these two builtins should be a parenthesized
1797
    // file name string literal using angle brackets (<>) or
1798
    // double-quotes ("").
1799
24.3k
    bool Value;
1800
24.3k
    if (II == Ident__has_include)
1801
12.8k
      Value = EvaluateHasInclude(Tok, II);
1802
11.5k
    else
1803
11.5k
      Value = EvaluateHasIncludeNext(Tok, II);
1804
1805
24.3k
    if (Tok.isNot(tok::r_paren))
1806
24
      return;
1807
24.3k
    OS << (int)Value;
1808
24.3k
    Tok.setKind(tok::numeric_constant);
1809
27.8k
  } else if (II == Ident__has_warning) {
1810
    // The argument should be a parenthesized string literal.
1811
10
    EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, false,
1812
10
      [this](Token &Tok, bool &HasLexedNextToken) -> int {
1813
7
        std::string WarningName;
1814
7
        SourceLocation StrStartLoc = Tok.getLocation();
1815
1816
7
        HasLexedNextToken = Tok.is(tok::string_literal);
1817
7
        if (!FinishLexStringLiteral(Tok, WarningName, "'__has_warning'",
1818
7
                                    /*AllowMacroExpansion=*/false))
1819
2
          return false;
1820
1821
        // FIXME: Should we accept "-R..." flags here, or should that be
1822
        // handled by a separate __has_remark?
1823
5
        if (WarningName.size() < 3 || WarningName[0] != '-' ||
1824
5
            
WarningName[1] != 'W'4
) {
1825
1
          Diag(StrStartLoc, diag::warn_has_warning_invalid_option);
1826
1
          return false;
1827
1
        }
1828
1829
        // Finally, check if the warning flags maps to a diagnostic group.
1830
        // We construct a SmallVector here to talk to getDiagnosticIDs().
1831
        // Although we don't use the result, this isn't a hot path, and not
1832
        // worth special casing.
1833
4
        SmallVector<diag::kind, 10> Diags;
1834
4
        return !getDiagnostics().getDiagnosticIDs()->
1835
4
                getDiagnosticsInGroup(diag::Flavor::WarningOrError,
1836
4
                                      WarningName.substr(2), Diags);
1837
5
      });
1838
27.8k
  } else if (II == Ident__building_module) {
1839
    // The argument to this builtin should be an identifier. The
1840
    // builtin evaluates to 1 when that identifier names the module we are
1841
    // currently building.
1842
610
    EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, false,
1843
610
      [this](Token &Tok, bool &HasLexedNextToken) -> int {
1844
610
        IdentifierInfo *II = ExpectFeatureIdentifierInfo(Tok, *this,
1845
610
                                       diag::err_expected_id_building_module);
1846
610
        return getLangOpts().isCompilingModule() && 
II26
&&
1847
610
               
(II->getName() == getLangOpts().CurrentModule)26
;
1848
610
      });
1849
27.2k
  } else if (II == Ident__MODULE__) {
1850
    // The current module as an identifier.
1851
7
    OS << getLangOpts().CurrentModule;
1852
7
    IdentifierInfo *ModuleII = getIdentifierInfo(getLangOpts().CurrentModule);
1853
7
    Tok.setIdentifierInfo(ModuleII);
1854
7
    Tok.setKind(ModuleII->getTokenID());
1855
27.2k
  } else if (II == Ident__identifier) {
1856
24
    SourceLocation Loc = Tok.getLocation();
1857
1858
    // We're expecting '__identifier' '(' identifier ')'. Try to recover
1859
    // if the parens are missing.
1860
24
    LexNonComment(Tok);
1861
24
    if (Tok.isNot(tok::l_paren)) {
1862
      // No '(', use end of last token.
1863
1
      Diag(getLocForEndOfToken(Loc), diag::err_pp_expected_after)
1864
1
        << II << tok::l_paren;
1865
      // If the next token isn't valid as our argument, we can't recover.
1866
1
      if (!Tok.isAnnotation() && Tok.getIdentifierInfo())
1867
1
        Tok.setKind(tok::identifier);
1868
1
      return;
1869
1
    }
1870
1871
23
    SourceLocation LParenLoc = Tok.getLocation();
1872
23
    LexNonComment(Tok);
1873
1874
23
    if (!Tok.isAnnotation() && Tok.getIdentifierInfo())
1875
13
      Tok.setKind(tok::identifier);
1876
10
    else if (Tok.is(tok::string_literal) && 
!Tok.hasUDSuffix()5
) {
1877
5
      StringLiteralParser Literal(Tok, *this,
1878
5
                                  StringLiteralEvalMethod::Unevaluated);
1879
5
      if (Literal.hadError)
1880
0
        return;
1881
1882
5
      Tok.setIdentifierInfo(getIdentifierInfo(Literal.GetString()));
1883
5
      Tok.setKind(tok::identifier);
1884
5
    } else {
1885
5
      Diag(Tok.getLocation(), diag::err_pp_identifier_arg_not_identifier)
1886
5
        << Tok.getKind();
1887
      // Don't walk past anything that's not a real token.
1888
5
      if (Tok.isOneOf(tok::eof, tok::eod) || Tok.isAnnotation())
1889
0
        return;
1890
5
    }
1891
1892
    // Discard the ')', preserving 'Tok' as our result.
1893
23
    Token RParen;
1894
23
    LexNonComment(RParen);
1895
23
    if (RParen.isNot(tok::r_paren)) {
1896
1
      Diag(getLocForEndOfToken(Tok.getLocation()), diag::err_pp_expected_after)
1897
1
        << Tok.getKind() << tok::r_paren;
1898
1
      Diag(LParenLoc, diag::note_matching) << tok::l_paren;
1899
1
    }
1900
23
    return;
1901
27.2k
  } else if (II == Ident__is_target_arch) {
1902
6.47k
    EvaluateFeatureLikeBuiltinMacro(
1903
6.47k
        OS, Tok, II, *this, false,
1904
6.47k
        [this](Token &Tok, bool &HasLexedNextToken) -> int {
1905
6.47k
          IdentifierInfo *II = ExpectFeatureIdentifierInfo(
1906
6.47k
              Tok, *this, diag::err_feature_check_malformed);
1907
6.47k
          return II && 
isTargetArch(getTargetInfo(), II)6.47k
;
1908
6.47k
        });
1909
20.7k
  } else if (II == Ident__is_target_vendor) {
1910
4.74k
    EvaluateFeatureLikeBuiltinMacro(
1911
4.74k
        OS, Tok, II, *this, false,
1912
4.74k
        [this](Token &Tok, bool &HasLexedNextToken) -> int {
1913
4.74k
          IdentifierInfo *II = ExpectFeatureIdentifierInfo(
1914
4.74k
              Tok, *this, diag::err_feature_check_malformed);
1915
4.74k
          return II && isTargetVendor(getTargetInfo(), II);
1916
4.74k
        });
1917
15.9k
  } else if (II == Ident__is_target_os) {
1918
4.75k
    EvaluateFeatureLikeBuiltinMacro(
1919
4.75k
        OS, Tok, II, *this, false,
1920
4.75k
        [this](Token &Tok, bool &HasLexedNextToken) -> int {
1921
4.75k
          IdentifierInfo *II = ExpectFeatureIdentifierInfo(
1922
4.75k
              Tok, *this, diag::err_feature_check_malformed);
1923
4.75k
          return II && isTargetOS(getTargetInfo(), II);
1924
4.75k
        });
1925
11.2k
  } else if (II == Ident__is_target_environment) {
1926
4.32k
    EvaluateFeatureLikeBuiltinMacro(
1927
4.32k
        OS, Tok, II, *this, false,
1928
4.32k
        [this](Token &Tok, bool &HasLexedNextToken) -> int {
1929
4.32k
          IdentifierInfo *II = ExpectFeatureIdentifierInfo(
1930
4.32k
              Tok, *this, diag::err_feature_check_malformed);
1931
4.32k
          return II && isTargetEnvironment(getTargetInfo(), II);
1932
4.32k
        });
1933
6.92k
  } else if (II == Ident__is_target_variant_os) {
1934
3.46k
    EvaluateFeatureLikeBuiltinMacro(
1935
3.46k
        OS, Tok, II, *this, false,
1936
3.46k
        [this](Token &Tok, bool &HasLexedNextToken) -> int {
1937
3.46k
          IdentifierInfo *II = ExpectFeatureIdentifierInfo(
1938
3.46k
              Tok, *this, diag::err_feature_check_malformed);
1939
3.46k
          return II && isTargetVariantOS(getTargetInfo(), II);
1940
3.46k
        });
1941
3.46k
  } else 
if (3.45k
II == Ident__is_target_variant_environment3.45k
) {
1942
3.45k
    EvaluateFeatureLikeBuiltinMacro(
1943
3.45k
        OS, Tok, II, *this, false,
1944
3.45k
        [this](Token &Tok, bool &HasLexedNextToken) -> int {
1945
3.45k
          IdentifierInfo *II = ExpectFeatureIdentifierInfo(
1946
3.45k
              Tok, *this, diag::err_feature_check_malformed);
1947
3.45k
          return II && isTargetVariantEnvironment(getTargetInfo(), II);
1948
3.45k
        });
1949
3.45k
  } else {
1950
0
    llvm_unreachable("Unknown identifier!");
1951
0
  }
1952
729k
  CreateString(OS.str(), Tok, Tok.getLocation(), Tok.getLocation());
1953
729k
  Tok.setFlagValue(Token::StartOfLine, IsAtStartOfLine);
1954
729k
  Tok.setFlagValue(Token::LeadingSpace, HasLeadingSpace);
1955
729k
}
1956
1957
90.3M
void Preprocessor::markMacroAsUsed(MacroInfo *MI) {
1958
  // If the 'used' status changed, and the macro requires 'unused' warning,
1959
  // remove its SourceLocation from the warn-for-unused-macro locations.
1960
90.3M
  if (MI->isWarnIfUnused() && 
!MI->isUsed()4
)
1961
4
    WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());
1962
90.3M
  MI->setIsUsed(true);
1963
90.3M
}
1964
1965
void Preprocessor::processPathForFileMacro(SmallVectorImpl<char> &Path,
1966
                                           const LangOptions &LangOpts,
1967
2.09k
                                           const TargetInfo &TI) {
1968
2.09k
  LangOpts.remapPathPrefix(Path);
1969
2.09k
  if (LangOpts.UseTargetPathSeparator) {
1970
24
    if (TI.getTriple().isOSWindows())
1971
9
      llvm::sys::path::remove_dots(Path, false,
1972
9
                                   llvm::sys::path::Style::windows_backslash);
1973
15
    else
1974
15
      llvm::sys::path::remove_dots(Path, false, llvm::sys::path::Style::posix);
1975
24
  }
1976
2.09k
}
1977
1978
void Preprocessor::processPathToFileName(SmallVectorImpl<char> &FileName,
1979
                                         const PresumedLoc &PLoc,
1980
                                         const LangOptions &LangOpts,
1981
324
                                         const TargetInfo &TI) {
1982
  // Try to get the last path component, failing that return the original
1983
  // presumed location.
1984
324
  StringRef PLFileName = llvm::sys::path::filename(PLoc.getFilename());
1985
324
  if (PLFileName.empty())
1986
0
    PLFileName = PLoc.getFilename();
1987
324
  FileName.append(PLFileName.begin(), PLFileName.end());
1988
324
  processPathForFileMacro(FileName, LangOpts, TI);
1989
324
}