Coverage Report

Created: 2021-08-24 07:12

/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/Attributes.h"
15
#include "clang/Basic/Builtins.h"
16
#include "clang/Basic/FileManager.h"
17
#include "clang/Basic/IdentifierTable.h"
18
#include "clang/Basic/LLVM.h"
19
#include "clang/Basic/LangOptions.h"
20
#include "clang/Basic/ObjCRuntime.h"
21
#include "clang/Basic/SourceLocation.h"
22
#include "clang/Basic/TargetInfo.h"
23
#include "clang/Lex/CodeCompletionHandler.h"
24
#include "clang/Lex/DirectoryLookup.h"
25
#include "clang/Lex/ExternalPreprocessorSource.h"
26
#include "clang/Lex/HeaderSearch.h"
27
#include "clang/Lex/LexDiagnostic.h"
28
#include "clang/Lex/LiteralSupport.h"
29
#include "clang/Lex/MacroArgs.h"
30
#include "clang/Lex/MacroInfo.h"
31
#include "clang/Lex/Preprocessor.h"
32
#include "clang/Lex/PreprocessorLexer.h"
33
#include "clang/Lex/PreprocessorOptions.h"
34
#include "clang/Lex/Token.h"
35
#include "llvm/ADT/ArrayRef.h"
36
#include "llvm/ADT/DenseMap.h"
37
#include "llvm/ADT/DenseSet.h"
38
#include "llvm/ADT/FoldingSet.h"
39
#include "llvm/ADT/None.h"
40
#include "llvm/ADT/Optional.h"
41
#include "llvm/ADT/STLExtras.h"
42
#include "llvm/ADT/SmallString.h"
43
#include "llvm/ADT/SmallVector.h"
44
#include "llvm/ADT/StringRef.h"
45
#include "llvm/ADT/StringSwitch.h"
46
#include "llvm/Support/Casting.h"
47
#include "llvm/Support/ErrorHandling.h"
48
#include "llvm/Support/Format.h"
49
#include "llvm/Support/Path.h"
50
#include "llvm/Support/raw_ostream.h"
51
#include <algorithm>
52
#include <cassert>
53
#include <cstddef>
54
#include <cstring>
55
#include <ctime>
56
#include <string>
57
#include <tuple>
58
#include <utility>
59
60
using namespace clang;
61
62
MacroDirective *
63
2.46M
Preprocessor::getLocalMacroDirectiveHistory(const IdentifierInfo *II) const {
64
2.46M
  if (!II->hadMacroDefinition())
65
0
    return nullptr;
66
2.46M
  auto Pos = CurSubmoduleState->Macros.find(II);
67
2.46M
  return Pos == CurSubmoduleState->Macros.end() ? 
nullptr287
68
2.46M
                                                : 
Pos->second.getLatest()2.46M
;
69
2.46M
}
70
71
51.1M
void Preprocessor::appendMacroDirective(IdentifierInfo *II, MacroDirective *MD){
72
51.1M
  assert(MD && "MacroDirective should be non-zero!");
73
0
  assert(!MD->getPrevious() && "Already attached to a MacroDirective history.");
74
75
0
  MacroState &StoredMD = CurSubmoduleState->Macros[II];
76
51.1M
  auto *OldMD = StoredMD.getLatest();
77
51.1M
  MD->setPrevious(OldMD);
78
51.1M
  StoredMD.setLatest(MD);
79
51.1M
  StoredMD.overrideActiveModuleMacros(*this, II);
80
81
51.1M
  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
324k
    PendingModuleMacroNames.push_back(II);
86
324k
  }
87
88
  // Set up the identifier as having associated macro history.
89
51.1M
  II->setHasMacroDefinition(true);
90
51.1M
  if (!MD->isDefined() && 
LeafModuleMacros.find(II) == LeafModuleMacros.end()72.3k
)
91
71.3k
    II->setHasMacroDefinition(false);
92
51.1M
  if (II->isFromAST())
93
1.27k
    II->setChangedSinceDeserialization();
94
51.1M
}
95
96
void Preprocessor::setLoadedMacroDirective(IdentifierInfo *II,
97
                                           MacroDirective *ED,
98
43.4k
                                           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
43.4k
  assert(II && MD);
109
0
  MacroState &StoredMD = CurSubmoduleState->Macros[II];
110
111
43.4k
  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
0
    assert(!OldMD->getPrevious() && "builtin should only have a single entry");
120
0
    ED->setPrevious(OldMD);
121
3
    StoredMD.setLatest(MD);
122
43.4k
  } else {
123
43.4k
    StoredMD = MD;
124
43.4k
  }
125
126
  // Setup the identifier as having associated macro history.
127
0
  II->setHasMacroDefinition(true);
128
43.4k
  if (!MD->isDefined() && 
LeafModuleMacros.find(II) == LeafModuleMacros.end()25
)
129
25
    II->setHasMacroDefinition(false);
130
43.4k
}
131
132
ModuleMacro *Preprocessor::addModuleMacro(Module *Mod, IdentifierInfo *II,
133
                                          MacroInfo *Macro,
134
                                          ArrayRef<ModuleMacro *> Overrides,
135
1.99M
                                          bool &New) {
136
1.99M
  llvm::FoldingSetNodeID ID;
137
1.99M
  ModuleMacro::Profile(ID, Mod, II);
138
139
1.99M
  void *InsertPos;
140
1.99M
  if (auto *MM = ModuleMacros.FindNodeOrInsertPos(ID, InsertPos)) {
141
1.84k
    New = false;
142
1.84k
    return MM;
143
1.84k
  }
144
145
1.99M
  auto *MM = ModuleMacro::create(*this, Mod, II, Macro, Overrides);
146
1.99M
  ModuleMacros.InsertNode(MM, InsertPos);
147
148
  // Each overridden macro is now overridden by one more macro.
149
1.99M
  bool HidAny = false;
150
1.99M
  for (auto *O : Overrides) {
151
9.94k
    HidAny |= (O->NumOverriddenBy == 0);
152
9.94k
    ++O->NumOverriddenBy;
153
9.94k
  }
154
155
  // If we were the first overrider for any macro, it's no longer a leaf.
156
1.99M
  auto &LeafMacros = LeafModuleMacros[II];
157
1.99M
  if (HidAny) {
158
9.70k
    LeafMacros.erase(std::remove_if(LeafMacros.begin(), LeafMacros.end(),
159
9.78k
                                    [](ModuleMacro *MM) {
160
9.78k
                                      return MM->NumOverriddenBy != 0;
161
9.78k
                                    }),
162
9.70k
                     LeafMacros.end());
163
9.70k
  }
164
165
  // The new macro is always a leaf macro.
166
1.99M
  LeafMacros.push_back(MM);
167
  // The identifier now has defined macros (that may or may not be visible).
168
1.99M
  II->setHasMacroDefinition(true);
169
170
1.99M
  New = true;
171
1.99M
  return MM;
172
1.99M
}
173
174
ModuleMacro *Preprocessor::getModuleMacro(Module *Mod,
175
6.60k
                                          const IdentifierInfo *II) {
176
6.60k
  llvm::FoldingSetNodeID ID;
177
6.60k
  ModuleMacro::Profile(ID, Mod, II);
178
179
6.60k
  void *InsertPos;
180
6.60k
  return ModuleMacros.FindNodeOrInsertPos(ID, InsertPos);
181
6.60k
}
182
183
void Preprocessor::updateModuleMacroInfo(const IdentifierInfo *II,
184
325k
                                         ModuleMacroInfo &Info) {
185
325k
  assert(Info.ActiveModuleMacrosGeneration !=
186
325k
             CurSubmoduleState->VisibleModules.getGeneration() &&
187
325k
         "don't need to update this macro name info");
188
0
  Info.ActiveModuleMacrosGeneration =
189
325k
      CurSubmoduleState->VisibleModules.getGeneration();
190
191
325k
  auto Leaf = LeafModuleMacros.find(II);
192
325k
  if (Leaf == LeafModuleMacros.end()) {
193
    // No imported macros at all: nothing to do.
194
83.3k
    return;
195
83.3k
  }
196
197
242k
  Info.ActiveModuleMacros.clear();
198
199
  // Every macro that's locally overridden is overridden by a visible macro.
200
242k
  llvm::DenseMap<ModuleMacro *, int> NumHiddenOverrides;
201
242k
  for (auto *O : Info.OverriddenMacros)
202
18
    NumHiddenOverrides[O] = -1;
203
204
  // Collect all macros that are not overridden by a visible macro.
205
242k
  llvm::SmallVector<ModuleMacro *, 16> Worklist;
206
243k
  for (auto *LeafMM : Leaf->second) {
207
243k
    assert(LeafMM->getNumOverridingMacros() == 0 && "leaf macro overridden");
208
243k
    if (NumHiddenOverrides.lookup(LeafMM) == 0)
209
243k
      Worklist.push_back(LeafMM);
210
243k
  }
211
485k
  while (!Worklist.empty()) {
212
243k
    auto *MM = Worklist.pop_back_val();
213
243k
    if (CurSubmoduleState->VisibleModules.isVisible(MM->getOwningModule())) {
214
      // We only care about collecting definitions; undefinitions only act
215
      // to override other definitions.
216
241k
      if (MM->getMacroInfo())
217
240k
        Info.ActiveModuleMacros.push_back(MM);
218
241k
    } else {
219
2.16k
      for (auto *O : MM->overrides())
220
173
        if ((unsigned)++NumHiddenOverrides[O] == O->getNumOverridingMacros())
221
159
          Worklist.push_back(O);
222
2.16k
    }
223
243k
  }
224
  // Our reverse postorder walk found the macros in reverse order.
225
242k
  std::reverse(Info.ActiveModuleMacros.begin(), Info.ActiveModuleMacros.end());
226
227
  // Determine whether the macro name is ambiguous.
228
242k
  MacroInfo *MI = nullptr;
229
242k
  bool IsSystemMacro = true;
230
242k
  bool IsAmbiguous = false;
231
242k
  if (auto *MD = Info.MD) {
232
133
    while (MD && isa<VisibilityMacroDirective>(MD))
233
0
      MD = MD->getPrevious();
234
133
    if (auto *DMD = dyn_cast_or_null<DefMacroDirective>(MD)) {
235
133
      MI = DMD->getInfo();
236
133
      IsSystemMacro &= SourceMgr.isInSystemHeader(DMD->getLocation());
237
133
    }
238
133
  }
239
242k
  for (auto *Active : Info.ActiveModuleMacros) {
240
240k
    auto *NewMI = Active->getMacroInfo();
241
242
    // Before marking the macro as ambiguous, check if this is a case where
243
    // both macros are in system headers. If so, we trust that the system
244
    // did not get it wrong. This also handles cases where Clang's own
245
    // headers have a different spelling of certain system macros:
246
    //   #define LONG_MAX __LONG_MAX__ (clang's limits.h)
247
    //   #define LONG_MAX 0x7fffffffffffffffL (system's limits.h)
248
    //
249
    // FIXME: Remove the defined-in-system-headers check. clang's limits.h
250
    // overrides the system limits.h's macros, so there's no conflict here.
251
240k
    if (MI && 
NewMI != MI384
&&
252
240k
        
!MI->isIdenticalTo(*NewMI, *this, /*Syntactically=*/true)383
)
253
70
      IsAmbiguous = true;
254
240k
    IsSystemMacro &= Active->getOwningModule()->IsSystem ||
255
240k
                     
SourceMgr.isInSystemHeader(NewMI->getDefinitionLoc())678
;
256
240k
    MI = NewMI;
257
240k
  }
258
242k
  Info.IsAmbiguous = IsAmbiguous && 
!IsSystemMacro70
;
259
242k
}
260
261
10
void Preprocessor::dumpMacroInfo(const IdentifierInfo *II) {
262
10
  ArrayRef<ModuleMacro*> Leaf;
263
10
  auto LeafIt = LeafModuleMacros.find(II);
264
10
  if (LeafIt != LeafModuleMacros.end())
265
10
    Leaf = LeafIt->second;
266
10
  const MacroState *State = nullptr;
267
10
  auto Pos = CurSubmoduleState->Macros.find(II);
268
10
  if (Pos != CurSubmoduleState->Macros.end())
269
10
    State = &Pos->second;
270
271
10
  llvm::errs() << "MacroState " << State << " " << II->getNameStart();
272
10
  if (State && State->isAmbiguous(*this, II))
273
9
    llvm::errs() << " ambiguous";
274
10
  if (State && !State->getOverriddenMacros().empty()) {
275
0
    llvm::errs() << " overrides";
276
0
    for (auto *O : State->getOverriddenMacros())
277
0
      llvm::errs() << " " << O->getOwningModule()->getFullModuleName();
278
0
  }
279
10
  llvm::errs() << "\n";
280
281
  // Dump local macro directives.
282
10
  for (auto *MD = State ? State->getLatest() : 
nullptr0
; MD;
283
10
       
MD = MD->getPrevious()0
) {
284
0
    llvm::errs() << " ";
285
0
    MD->dump();
286
0
  }
287
288
  // Dump module macros.
289
10
  llvm::DenseSet<ModuleMacro*> Active;
290
10
  for (auto *MM : State ? State->getActiveModuleMacros(*this, II) : 
None0
)
291
18
    Active.insert(MM);
292
10
  llvm::DenseSet<ModuleMacro*> Visited;
293
10
  llvm::SmallVector<ModuleMacro *, 16> Worklist(Leaf.begin(), Leaf.end());
294
29
  while (!Worklist.empty()) {
295
19
    auto *MM = Worklist.pop_back_val();
296
19
    llvm::errs() << " ModuleMacro " << MM << " "
297
19
                 << MM->getOwningModule()->getFullModuleName();
298
19
    if (!MM->getMacroInfo())
299
0
      llvm::errs() << " undef";
300
301
19
    if (Active.count(MM))
302
18
      llvm::errs() << " active";
303
1
    else if (!CurSubmoduleState->VisibleModules.isVisible(
304
1
                 MM->getOwningModule()))
305
1
      llvm::errs() << " hidden";
306
0
    else if (MM->getMacroInfo())
307
0
      llvm::errs() << " overridden";
308
309
19
    if (!MM->overrides().empty()) {
310
0
      llvm::errs() << " overrides";
311
0
      for (auto *O : MM->overrides()) {
312
0
        llvm::errs() << " " << O->getOwningModule()->getFullModuleName();
313
0
        if (Visited.insert(O).second)
314
0
          Worklist.push_back(O);
315
0
      }
316
0
    }
317
19
    llvm::errs() << "\n";
318
19
    if (auto *MI = MM->getMacroInfo()) {
319
19
      llvm::errs() << "  ";
320
19
      MI->dump();
321
19
      llvm::errs() << "\n";
322
19
    }
323
19
  }
324
10
}
325
326
/// RegisterBuiltinMacro - Register the specified identifier in the identifier
327
/// table and mark it as a builtin macro to be expanded.
328
2.54M
static IdentifierInfo *RegisterBuiltinMacro(Preprocessor &PP, const char *Name){
329
  // Get the identifier.
330
2.54M
  IdentifierInfo *Id = PP.getIdentifierInfo(Name);
331
332
  // Mark it as being a macro that is builtin.
333
2.54M
  MacroInfo *MI = PP.AllocateMacroInfo(SourceLocation());
334
2.54M
  MI->setIsBuiltinMacro();
335
2.54M
  PP.appendDefMacroDirective(Id, MI);
336
2.54M
  return Id;
337
2.54M
}
338
339
/// RegisterBuiltinMacros - Register builtin macros, such as __LINE__ with the
340
/// identifier table.
341
97.0k
void Preprocessor::RegisterBuiltinMacros() {
342
97.0k
  Ident__LINE__ = RegisterBuiltinMacro(*this, "__LINE__");
343
97.0k
  Ident__FILE__ = RegisterBuiltinMacro(*this, "__FILE__");
344
97.0k
  Ident__DATE__ = RegisterBuiltinMacro(*this, "__DATE__");
345
97.0k
  Ident__TIME__ = RegisterBuiltinMacro(*this, "__TIME__");
346
97.0k
  Ident__COUNTER__ = RegisterBuiltinMacro(*this, "__COUNTER__");
347
97.0k
  Ident_Pragma  = RegisterBuiltinMacro(*this, "_Pragma");
348
97.0k
  if (PPOpts->LexExpandSpecialBuiltins)
349
    // Suppress macro expansion if compiler stops before semantic analysis,
350
    // the macro identifier will appear in the preprocessed output.
351
94.6k
    Ident__FLT_EVAL_METHOD__ =
352
94.6k
        RegisterBuiltinMacro(*this, "__FLT_EVAL_METHOD__");
353
354
  // C++ Standing Document Extensions.
355
97.0k
  if (getLangOpts().CPlusPlus)
356
75.8k
    Ident__has_cpp_attribute =
357
75.8k
        RegisterBuiltinMacro(*this, "__has_cpp_attribute");
358
21.1k
  else
359
21.1k
    Ident__has_cpp_attribute = nullptr;
360
361
  // GCC Extensions.
362
97.0k
  Ident__BASE_FILE__     = RegisterBuiltinMacro(*this, "__BASE_FILE__");
363
97.0k
  Ident__INCLUDE_LEVEL__ = RegisterBuiltinMacro(*this, "__INCLUDE_LEVEL__");
364
97.0k
  Ident__TIMESTAMP__     = RegisterBuiltinMacro(*this, "__TIMESTAMP__");
365
366
  // Microsoft Extensions.
367
97.0k
  if (getLangOpts().MicrosoftExt) {
368
10.6k
    Ident__identifier = RegisterBuiltinMacro(*this, "__identifier");
369
10.6k
    Ident__pragma = RegisterBuiltinMacro(*this, "__pragma");
370
86.3k
  } else {
371
86.3k
    Ident__identifier = nullptr;
372
86.3k
    Ident__pragma = nullptr;
373
86.3k
  }
374
375
  // Clang Extensions.
376
97.0k
  Ident__FILE_NAME__      = RegisterBuiltinMacro(*this, "__FILE_NAME__");
377
97.0k
  Ident__has_feature      = RegisterBuiltinMacro(*this, "__has_feature");
378
97.0k
  Ident__has_extension    = RegisterBuiltinMacro(*this, "__has_extension");
379
97.0k
  Ident__has_builtin      = RegisterBuiltinMacro(*this, "__has_builtin");
380
97.0k
  Ident__has_attribute    = RegisterBuiltinMacro(*this, "__has_attribute");
381
97.0k
  if (!getLangOpts().CPlusPlus)
382
21.1k
    Ident__has_c_attribute = RegisterBuiltinMacro(*this, "__has_c_attribute");
383
75.8k
  else
384
75.8k
    Ident__has_c_attribute = nullptr;
385
386
97.0k
  Ident__has_declspec = RegisterBuiltinMacro(*this, "__has_declspec_attribute");
387
97.0k
  Ident__has_include      = RegisterBuiltinMacro(*this, "__has_include");
388
97.0k
  Ident__has_include_next = RegisterBuiltinMacro(*this, "__has_include_next");
389
97.0k
  Ident__has_warning      = RegisterBuiltinMacro(*this, "__has_warning");
390
97.0k
  Ident__is_identifier    = RegisterBuiltinMacro(*this, "__is_identifier");
391
97.0k
  Ident__is_target_arch   = RegisterBuiltinMacro(*this, "__is_target_arch");
392
97.0k
  Ident__is_target_vendor = RegisterBuiltinMacro(*this, "__is_target_vendor");
393
97.0k
  Ident__is_target_os     = RegisterBuiltinMacro(*this, "__is_target_os");
394
97.0k
  Ident__is_target_environment =
395
97.0k
      RegisterBuiltinMacro(*this, "__is_target_environment");
396
397
  // Modules.
398
97.0k
  Ident__building_module  = RegisterBuiltinMacro(*this, "__building_module");
399
97.0k
  if (!getLangOpts().CurrentModule.empty())
400
1.98k
    Ident__MODULE__ = RegisterBuiltinMacro(*this, "__MODULE__");
401
95.0k
  else
402
95.0k
    Ident__MODULE__ = nullptr;
403
97.0k
}
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
7.92M
                                          Preprocessor &PP) {
410
7.92M
  IdentifierInfo *II = MI->getReplacementToken(0).getIdentifierInfo();
411
412
  // If the token isn't an identifier, it's always literally expanded.
413
7.92M
  if (!II) 
return true1.65M
;
414
415
  // If the information about this identifier is out of date, update it from
416
  // the external source.
417
6.26M
  if (II->isOutOfDate())
418
625
    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
6.26M
  if (auto *ExpansionMI = PP.getMacroInfo(II))
423
1.42M
    if (ExpansionMI->isEnabled() &&
424
        // Fast expanding "#define X X" is ok, because X would be disabled.
425
1.42M
        
II != MacroIdent1.42M
)
426
1.42M
      return false;
427
428
  // If this is an object-like macro invocation, it is safe to trivially expand
429
  // it.
430
4.84M
  if (MI->isObjectLike()) 
return true2.29M
;
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.55M
  return std::find(MI->param_begin(), MI->param_end(), II) == MI->param_end();
435
4.84M
}
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
21.8M
bool Preprocessor::isNextPPTokenLParen() {
441
  // Do some quick tests for rejection cases.
442
21.8M
  unsigned Val;
443
21.8M
  if (CurLexer)
444
3.00M
    Val = CurLexer->isNextPPTokenLParen();
445
18.7M
  else
446
18.7M
    Val = CurTokenLexer->isNextTokenLParen();
447
448
21.8M
  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.26M
    if (CurPPLexer)
453
1
      return false;
454
2.26M
    
for (const IncludeStackInfo &Entry : llvm::reverse(IncludeMacroStack))2.26M
{
455
2.26M
      if (Entry.TheLexer)
456
1.34k
        Val = Entry.TheLexer->isNextPPTokenLParen();
457
2.26M
      else
458
2.26M
        Val = Entry.TheTokenLexer->isNextTokenLParen();
459
460
2.26M
      if (Val != 2)
461
2.26M
        break;
462
463
      // Ran off the end of a source file?
464
12
      if (Entry.ThePPLexer)
465
2
        return false;
466
12
    }
467
2.26M
  }
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
21.8M
  return Val == 1;
473
21.8M
}
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
62.4M
                                                 const MacroDefinition &M) {
479
62.4M
  emitMacroExpansionWarnings(Identifier);
480
481
62.4M
  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
62.4M
  if (CurPPLexer) 
CurPPLexer->MIOpt.ExpandedMacro()38.7M
;
487
488
  // If this is a builtin macro, like __LINE__ or _Pragma, handle it specially.
489
62.4M
  if (MI->isBuiltinMacro()) {
490
1.21M
    if (Callbacks)
491
1.21M
      Callbacks->MacroExpands(Identifier, M, Identifier.getLocation(),
492
1.21M
                              /*Args=*/nullptr);
493
1.21M
    ExpandBuiltinMacro(Identifier);
494
1.21M
    return true;
495
1.21M
  }
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
61.2M
  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
61.2M
  SourceLocation ExpansionEnd = Identifier.getLocation();
505
506
  // If this is a function-like macro, read the arguments.
507
61.2M
  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
19.5M
    InMacroArgs = true;
512
19.5M
    ArgMacro = &Identifier;
513
514
19.5M
    Args = ReadMacroCallArgumentList(Identifier, MI, ExpansionEnd);
515
516
    // Finished parsing args.
517
19.5M
    InMacroArgs = false;
518
19.5M
    ArgMacro = nullptr;
519
520
    // If there was an error parsing the arguments, bail out.
521
19.5M
    if (!Args) 
return true54
;
522
523
19.5M
    ++NumFnMacroExpanded;
524
41.6M
  } else {
525
41.6M
    ++NumMacroExpanded;
526
41.6M
  }
527
528
  // Notice that this macro has been used.
529
61.2M
  markMacroAsUsed(MI);
530
531
  // Remember where the token is expanded.
532
61.2M
  SourceLocation ExpandLoc = Identifier.getLocation();
533
61.2M
  SourceRange ExpansionRange(ExpandLoc, ExpansionEnd);
534
535
61.2M
  if (Callbacks) {
536
61.2M
    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
61.2M
    } else {
544
61.2M
      Callbacks->MacroExpands(Identifier, M, ExpansionRange, Args);
545
61.2M
      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
61.2M
    }
554
61.2M
  }
555
556
  // If the macro definition is ambiguous, complain.
557
61.2M
  if (M.isAmbiguous()) {
558
45
    Diag(Identifier, diag::warn_pp_ambiguous_macro)
559
45
      << Identifier.getIdentifierInfo();
560
45
    Diag(MI->getDefinitionLoc(), diag::note_pp_ambiguous_macro_chosen)
561
45
      << Identifier.getIdentifierInfo();
562
90
    M.forAllDefinitions([&](const MacroInfo *OtherMI) {
563
90
      if (OtherMI != MI)
564
45
        Diag(OtherMI->getDefinitionLoc(), diag::note_pp_ambiguous_macro_other)
565
45
          << Identifier.getIdentifierInfo();
566
90
    });
567
45
  }
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
61.2M
  if (MI->getNumTokens() == 0) {
574
    // No need for arg info.
575
870k
    if (Args) 
Args->destroy(*this)12.3k
;
576
577
    // Propagate whitespace info as if we had pushed, then popped,
578
    // a macro context.
579
870k
    Identifier.setFlag(Token::LeadingEmptyMacro);
580
870k
    PropagateLineStartLeadingSpaceInfo(Identifier);
581
870k
    ++NumFastMacroExpanded;
582
870k
    return false;
583
60.3M
  } else if (MI->getNumTokens() == 1 &&
584
60.3M
             isTrivialSingleTokenExpansion(MI, Identifier.getIdentifierInfo(),
585
7.92M
                                           *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
3.94M
    if (Args) 
Args->destroy(*this)101
;
592
593
    // Propagate the isAtStartOfLine/hasLeadingSpace markers of the macro
594
    // identifier to the expanded token.
595
3.94M
    bool isAtStartOfLine = Identifier.isAtStartOfLine();
596
3.94M
    bool hasLeadingSpace = Identifier.hasLeadingSpace();
597
598
    // Replace the result token.
599
3.94M
    Identifier = MI->getReplacementToken(0);
600
601
    // Restore the StartOfLine/LeadingSpace markers.
602
3.94M
    Identifier.setFlagValue(Token::StartOfLine , isAtStartOfLine);
603
3.94M
    Identifier.setFlagValue(Token::LeadingSpace, hasLeadingSpace);
604
605
    // Update the tokens location to include both its expansion and physical
606
    // locations.
607
3.94M
    SourceLocation Loc =
608
3.94M
      SourceMgr.createExpansionLoc(Identifier.getLocation(), ExpandLoc,
609
3.94M
                                   ExpansionEnd,Identifier.getLength());
610
3.94M
    Identifier.setLocation(Loc);
611
612
    // If this is a disabled macro or #define X X, we must mark the result as
613
    // unexpandable.
614
3.94M
    if (IdentifierInfo *NewII = Identifier.getIdentifierInfo()) {
615
2.29M
      if (MacroInfo *NewMI = getMacroInfo(NewII))
616
3.05k
        if (!NewMI->isEnabled() || 
NewMI == MI3.04k
) {
617
3.05k
          Identifier.setFlag(Token::DisableExpand);
618
          // Don't warn for "#define X X" like "#define bool bool" from
619
          // stdbool.h.
620
3.05k
          if (NewMI != MI || 
MI->isFunctionLike()3.04k
)
621
6
            Diag(Identifier, diag::pp_disabled_macro_expansion);
622
3.05k
        }
623
2.29M
    }
624
625
    // Since this is not an identifier token, it can't be macro expanded, so
626
    // we're done.
627
3.94M
    ++NumFastMacroExpanded;
628
3.94M
    return true;
629
3.94M
  }
630
631
  // Start expanding the macro.
632
56.3M
  EnterMacro(Identifier, ExpansionEnd, MI, Args);
633
56.3M
  return false;
634
61.2M
}
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
19.5M
                                                   SourceLocation &MacroEnd) {
771
  // The number of fixed arguments to parse.
772
19.5M
  unsigned NumFixedArgsLeft = MI->getNumParams();
773
19.5M
  bool isVariadic = MI->isVariadic();
774
775
  // Outer loop, while there are more arguments, keep reading them.
776
19.5M
  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
19.5M
  LexUnexpandedToken(Tok);
781
19.5M
  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
0
  SmallVector<Token, 64> ArgTokens;
787
19.5M
  bool ContainsCodeCompletionTok = false;
788
19.5M
  bool FoundElidedComma = false;
789
790
19.5M
  SourceLocation TooManyArgsLoc;
791
792
19.5M
  unsigned NumActuals = 0;
793
64.1M
  while (Tok.isNot(tok::r_paren)) {
794
44.9M
    if (ContainsCodeCompletionTok && 
Tok.isOneOf(tok::eof, tok::eod)16
)
795
9
      break;
796
797
44.9M
    assert(Tok.isOneOf(tok::l_paren, tok::comma) &&
798
44.9M
           "only expect argument separators here");
799
800
0
    size_t ArgTokenStart = ArgTokens.size();
801
44.9M
    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
44.9M
    unsigned NumParens = 0;
806
807
157M
    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
157M
      LexUnexpandedToken(Tok);
811
812
157M
      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
157M
      } else if (Tok.is(tok::r_paren)) {
827
        // If we found the ) token, the macro arg list is done.
828
35.2M
        if (NumParens-- == 0) {
829
19.5M
          MacroEnd = Tok.getLocation();
830
19.5M
          if (!ArgTokens.empty() &&
831
19.5M
              
ArgTokens.back().commaAfterElided()19.1M
) {
832
853
            FoundElidedComma = true;
833
853
          }
834
19.5M
          break;
835
19.5M
        }
836
122M
      } else if (Tok.is(tok::l_paren)) {
837
15.7M
        ++NumParens;
838
106M
      } 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
35.0M
        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
35.0M
        } 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
32.4M
          if (!isVariadic)
854
8.88M
            break;
855
23.6M
          if (NumFixedArgsLeft > 1)
856
16.5M
            break;
857
23.6M
        }
858
71.5M
      } 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
71.5M
      } else if (!Tok.isAnnotation() && 
Tok.getIdentifierInfo() != nullptr71.5M
) {
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
41.9M
        if (MacroInfo *MI = getMacroInfo(Tok.getIdentifierInfo()))
870
16.6M
          if (!MI->isEnabled())
871
1.70k
            Tok.setFlag(Token::DisableExpand);
872
41.9M
      } else 
if (29.6M
Tok.is(tok::code_completion)29.6M
) {
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
112M
      ArgTokens.push_back(Tok);
883
112M
    }
884
885
    // If this was an empty argument list foo(), don't add this as an empty
886
    // argument.
887
44.9M
    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
44.6M
    if (!isVariadic && 
NumFixedArgsLeft == 023.2M
&&
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
44.6M
    if (ArgTokens.size() == ArgTokenStart && 
!getLangOpts().C9987.1k
)
902
23.2k
      Diag(Tok, getLangOpts().CPlusPlus11
903
23.2k
                    ? 
diag::warn_cxx98_compat_empty_fnmacro_arg23.2k
904
23.2k
                    : 
diag::ext_empty_fnmacro_arg22
);
905
906
    // Add a marker EOF token to the end of the token list for this argument.
907
44.6M
    Token EOFTok;
908
44.6M
    EOFTok.startToken();
909
44.6M
    EOFTok.setKind(tok::eof);
910
44.6M
    EOFTok.setLocation(Tok.getLocation());
911
44.6M
    EOFTok.setLength(0);
912
44.6M
    ArgTokens.push_back(EOFTok);
913
44.6M
    ++NumActuals;
914
44.6M
    if (!ContainsCodeCompletionTok && 
NumFixedArgsLeft != 044.6M
)
915
44.6M
      --NumFixedArgsLeft;
916
44.6M
  }
917
918
  // Okay, we either found the r_paren.  Check to see if we parsed too few
919
  // arguments.
920
19.5M
  unsigned MinArgsExpected = MI->getNumParams();
921
922
  // If this is not a variadic macro, and too many args were specified, emit
923
  // an error.
924
19.5M
  if (!isVariadic && 
NumActuals > MinArgsExpected14.6M
&&
925
19.5M
      
!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
19.5M
  bool isVarargsElided = false;
965
966
19.5M
  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
19.5M
  if (NumActuals < MinArgsExpected) {
978
    // There are several cases where too few arguments is ok, handle them now.
979
461k
    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 (111k
(111k
FoundElidedComma111k
||
MI->isVariadic()111k
) &&
987
111k
               
(111k
NumActuals+1 == MinArgsExpected111k
|| // A(x, ...) -> A(X)
988
111k
                
(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
111k
      if (!MI->hasCommaPasting()) {
996
99.9k
        Diag(Tok, diag::ext_missing_varargs_arg);
997
99.9k
        Diag(MI->getDefinitionLoc(), diag::note_macro_here)
998
99.9k
          << MacroName.getIdentifierInfo();
999
99.9k
      }
1000
1001
      // Remember this occurred, allowing us to elide the comma when used for
1002
      // cases like:
1003
      //   #define A(x, foo...) blah(a, ## foo)
1004
      //   #define B(x, ...) blah(a, ## __VA_ARGS__)
1005
      //   #define C(...) blah(a, ## __VA_ARGS__)
1006
      //  A(x) B(x) C()
1007
111k
      isVarargsElided = true;
1008
111k
    } else 
if (21
!ContainsCodeCompletionTok21
) {
1009
      // Otherwise, emit the error.
1010
21
      Diag(Tok, diag::err_too_few_args_in_macro_invoc);
1011
21
      Diag(MI->getDefinitionLoc(), diag::note_macro_here)
1012
21
        << MacroName.getIdentifierInfo();
1013
21
      return nullptr;
1014
21
    }
1015
1016
    // Add a marker EOF token to the end of the token list for this argument.
1017
461k
    SourceLocation EndLoc = Tok.getLocation();
1018
461k
    Tok.startToken();
1019
461k
    Tok.setKind(tok::eof);
1020
461k
    Tok.setLocation(EndLoc);
1021
461k
    Tok.setLength(0);
1022
461k
    ArgTokens.push_back(Tok);
1023
1024
    // If we expect two arguments, add both as empty.
1025
461k
    if (NumActuals == 0 && 
MinArgsExpected == 2350k
)
1026
30
      ArgTokens.push_back(Tok);
1027
1028
19.0M
  } else if (NumActuals > MinArgsExpected && 
!MI->isVariadic()1
&&
1029
19.0M
             
!ContainsCodeCompletionTok1
) {
1030
    // Emit the diagnostic at the macro name in case there is a missing ).
1031
    // Emitting it at the , could be far away from the macro name.
1032
0
    Diag(MacroName, diag::err_too_many_args_in_macro_invoc);
1033
0
    Diag(MI->getDefinitionLoc(), diag::note_macro_here)
1034
0
      << MacroName.getIdentifierInfo();
1035
0
    return nullptr;
1036
0
  }
1037
1038
19.5M
  return MacroArgs::create(MI, ArgTokens, isVarargsElided, *this);
1039
19.5M
}
1040
1041
/// Keeps macro expanded tokens for TokenLexers.
1042
//
1043
/// Works like a stack; a TokenLexer adds the macro expanded tokens that is
1044
/// going to lex in the cache and when it finishes the tokens are removed
1045
/// from the end of the cache.
1046
Token *Preprocessor::cacheMacroExpandedTokens(TokenLexer *tokLexer,
1047
19.4M
                                              ArrayRef<Token> tokens) {
1048
19.4M
  assert(tokLexer);
1049
19.4M
  if (tokens.empty())
1050
262k
    return nullptr;
1051
1052
19.2M
  size_t newIndex = MacroExpandedTokens.size();
1053
19.2M
  bool cacheNeedsToGrow = tokens.size() >
1054
19.2M
                      MacroExpandedTokens.capacity()-MacroExpandedTokens.size();
1055
19.2M
  MacroExpandedTokens.append(tokens.begin(), tokens.end());
1056
1057
19.2M
  if (cacheNeedsToGrow) {
1058
    // Go through all the TokenLexers whose 'Tokens' pointer points in the
1059
    // buffer and update the pointers to the (potential) new buffer array.
1060
13.5k
    for (const auto &Lexer : MacroExpandingLexersStack) {
1061
12.0k
      TokenLexer *prevLexer;
1062
12.0k
      size_t tokIndex;
1063
12.0k
      std::tie(prevLexer, tokIndex) = Lexer;
1064
12.0k
      prevLexer->Tokens = MacroExpandedTokens.data() + tokIndex;
1065
12.0k
    }
1066
13.5k
  }
1067
1068
19.2M
  MacroExpandingLexersStack.push_back(std::make_pair(tokLexer, newIndex));
1069
19.2M
  return MacroExpandedTokens.data() + newIndex;
1070
19.4M
}
1071
1072
19.2M
void Preprocessor::removeCachedMacroExpandedTokensOfLastLexer() {
1073
19.2M
  assert(!MacroExpandingLexersStack.empty());
1074
0
  size_t tokIndex = MacroExpandingLexersStack.back().second;
1075
19.2M
  assert(tokIndex < MacroExpandedTokens.size());
1076
  // Pop the cached macro expanded tokens from the end.
1077
0
  MacroExpandedTokens.resize(tokIndex);
1078
19.2M
  MacroExpandingLexersStack.pop_back();
1079
19.2M
}
1080
1081
/// ComputeDATE_TIME - Compute the current time, enter it into the specified
1082
/// scratch buffer, then return DATELoc/TIMELoc locations with the position of
1083
/// the identifier tokens inserted.
1084
static void ComputeDATE_TIME(SourceLocation &DATELoc, SourceLocation &TIMELoc,
1085
7
                             Preprocessor &PP) {
1086
7
  time_t TT = time(nullptr);
1087
7
  struct tm *TM = localtime(&TT);
1088
1089
7
  static const char * const Months[] = {
1090
7
    "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
1091
7
  };
1092
1093
7
  {
1094
7
    SmallString<32> TmpBuffer;
1095
7
    llvm::raw_svector_ostream TmpStream(TmpBuffer);
1096
7
    TmpStream << llvm::format("\"%s %2d %4d\"", Months[TM->tm_mon],
1097
7
                              TM->tm_mday, TM->tm_year + 1900);
1098
7
    Token TmpTok;
1099
7
    TmpTok.startToken();
1100
7
    PP.CreateString(TmpStream.str(), TmpTok);
1101
7
    DATELoc = TmpTok.getLocation();
1102
7
  }
1103
1104
7
  {
1105
7
    SmallString<32> TmpBuffer;
1106
7
    llvm::raw_svector_ostream TmpStream(TmpBuffer);
1107
7
    TmpStream << llvm::format("\"%02d:%02d:%02d\"",
1108
7
                              TM->tm_hour, TM->tm_min, TM->tm_sec);
1109
7
    Token TmpTok;
1110
7
    TmpTok.startToken();
1111
7
    PP.CreateString(TmpStream.str(), TmpTok);
1112
7
    TIMELoc = TmpTok.getLocation();
1113
7
  }
1114
7
}
1115
1116
/// HasFeature - Return true if we recognize and implement the feature
1117
/// specified by the identifier as a standard language feature.
1118
478k
static bool HasFeature(const Preprocessor &PP, StringRef Feature) {
1119
478k
  const LangOptions &LangOpts = PP.getLangOpts();
1120
1121
  // Normalize the feature name, __foo__ becomes foo.
1122
478k
  if (Feature.startswith("__") && 
Feature.endswith("__")5
&&
Feature.size() >= 44
)
1123
4
    Feature = Feature.substr(2, Feature.size() - 4);
1124
1125
79.0M
#define FEATURE(Name, Predicate) .Case(#Name, 
Predicate4.22M
)
1126
478k
  return llvm::StringSwitch<bool>(Feature)
1127
478k
#include "clang/Basic/Features.def"
1128
478k
      .Default(false);
1129
478k
#undef FEATURE
1130
478k
}
1131
1132
/// HasExtension - Return true if we recognize and implement the feature
1133
/// specified by the identifier, either as an extension or a standard language
1134
/// feature.
1135
16.4k
static bool HasExtension(const Preprocessor &PP, StringRef Extension) {
1136
16.4k
  if (HasFeature(PP, Extension))
1137
9.23k
    return true;
1138
1139
  // If the use of an extension results in an error diagnostic, extensions are
1140
  // effectively unavailable, so just return false here.
1141
7.24k
  if (PP.getDiagnostics().getExtensionHandlingBehavior() >=
1142
7.24k
      diag::Severity::Error)
1143
6
    return false;
1144
1145
7.24k
  const LangOptions &LangOpts = PP.getLangOpts();
1146
1147
  // Normalize the extension name, __foo__ becomes foo.
1148
7.24k
  if (Extension.startswith("__") && 
Extension.endswith("__")1
&&
1149
7.24k
      
Extension.size() >= 41
)
1150
1
    Extension = Extension.substr(2, Extension.size() - 4);
1151
1152
    // Because we inherit the feature list from HasFeature, this string switch
1153
    // must be less restrictive than HasFeature's.
1154
224k
#define EXTENSION(Name, Predicate) .Case(#Name, Predicate)
1155
7.24k
  return llvm::StringSwitch<bool>(Extension)
1156
7.24k
#include "clang/Basic/Features.def"
1157
7.24k
      .Default(false);
1158
7.24k
#undef EXTENSION
1159
7.24k
}
1160
1161
/// EvaluateHasIncludeCommon - Process a '__has_include("path")'
1162
/// or '__has_include_next("path")' expression.
1163
/// Returns true if successful.
1164
static bool EvaluateHasIncludeCommon(Token &Tok,
1165
                                     IdentifierInfo *II, Preprocessor &PP,
1166
                                     const DirectoryLookup *LookupFrom,
1167
16.5k
                                     const FileEntry *LookupFromFile) {
1168
  // Save the location of the current token.  If a '(' is later found, use
1169
  // that location.  If not, use the end of this location instead.
1170
16.5k
  SourceLocation LParenLoc = Tok.getLocation();
1171
1172
  // These expressions are only allowed within a preprocessor directive.
1173
16.5k
  if (!PP.isParsingIfOrElifDirective()) {
1174
8
    PP.Diag(LParenLoc, diag::err_pp_directive_required) << II;
1175
    // Return a valid identifier token.
1176
8
    assert(Tok.is(tok::identifier));
1177
0
    Tok.setIdentifierInfo(II);
1178
8
    return false;
1179
8
  }
1180
1181
  // Get '('. If we don't have a '(', try to form a header-name token.
1182
16.5k
  do {
1183
16.5k
    if (PP.LexHeaderName(Tok))
1184
0
      return false;
1185
16.5k
  } while (Tok.getKind() == tok::comment);
1186
1187
  // Ensure we have a '('.
1188
16.5k
  if (Tok.isNot(tok::l_paren)) {
1189
    // No '(', use end of last token.
1190
6
    LParenLoc = PP.getLocForEndOfToken(LParenLoc);
1191
6
    PP.Diag(LParenLoc, diag::err_pp_expected_after) << II << tok::l_paren;
1192
    // If the next token looks like a filename or the start of one,
1193
    // assume it is and process it as such.
1194
6
    if (Tok.isNot(tok::header_name))
1195
4
      return false;
1196
16.5k
  } else {
1197
    // Save '(' location for possible missing ')' message.
1198
16.5k
    LParenLoc = Tok.getLocation();
1199
16.5k
    if (PP.LexHeaderName(Tok))
1200
2
      return false;
1201
16.5k
  }
1202
1203
16.5k
  if (Tok.isNot(tok::header_name)) {
1204
9
    PP.Diag(Tok.getLocation(), diag::err_pp_expects_filename);
1205
9
    return false;
1206
9
  }
1207
1208
  // Reserve a buffer to get the spelling.
1209
16.5k
  SmallString<128> FilenameBuffer;
1210
16.5k
  bool Invalid = false;
1211
16.5k
  StringRef Filename = PP.getSpelling(Tok, FilenameBuffer, &Invalid);
1212
16.5k
  if (Invalid)
1213
0
    return false;
1214
1215
16.5k
  SourceLocation FilenameLoc = Tok.getLocation();
1216
1217
  // Get ')'.
1218
16.5k
  PP.LexNonComment(Tok);
1219
1220
  // Ensure we have a trailing ).
1221
16.5k
  if (Tok.isNot(tok::r_paren)) {
1222
2
    PP.Diag(PP.getLocForEndOfToken(FilenameLoc), diag::err_pp_expected_after)
1223
2
        << II << tok::r_paren;
1224
2
    PP.Diag(LParenLoc, diag::note_matching) << tok::l_paren;
1225
2
    return false;
1226
2
  }
1227
1228
16.5k
  bool isAngled = PP.GetIncludeFilenameSpelling(Tok.getLocation(), Filename);
1229
  // If GetIncludeFilenameSpelling set the start ptr to null, there was an
1230
  // error.
1231
16.5k
  if (Filename.empty())
1232
1
    return false;
1233
1234
  // Search include directories.
1235
16.5k
  const DirectoryLookup *CurDir;
1236
16.5k
  Optional<FileEntryRef> File =
1237
16.5k
      PP.LookupFile(FilenameLoc, Filename, isAngled, LookupFrom, LookupFromFile,
1238
16.5k
                    CurDir, nullptr, nullptr, nullptr, nullptr, nullptr);
1239
1240
16.5k
  if (PPCallbacks *Callbacks = PP.getPPCallbacks()) {
1241
16.5k
    SrcMgr::CharacteristicKind FileType = SrcMgr::C_User;
1242
16.5k
    if (File)
1243
8.27k
      FileType =
1244
8.27k
          PP.getHeaderSearchInfo().getFileDirFlavor(&File->getFileEntry());
1245
16.5k
    Callbacks->HasInclude(FilenameLoc, Filename, isAngled, File, FileType);
1246
16.5k
  }
1247
1248
  // Get the result value.  A result of true means the file exists.
1249
16.5k
  return File.hasValue();
1250
16.5k
}
1251
1252
/// EvaluateHasInclude - Process a '__has_include("path")' expression.
1253
/// Returns true if successful.
1254
static bool EvaluateHasInclude(Token &Tok, IdentifierInfo *II,
1255
10.9k
                               Preprocessor &PP) {
1256
10.9k
  return EvaluateHasIncludeCommon(Tok, II, PP, nullptr, nullptr);
1257
10.9k
}
1258
1259
/// EvaluateHasIncludeNext - Process '__has_include_next("path")' expression.
1260
/// Returns true if successful.
1261
static bool EvaluateHasIncludeNext(Token &Tok,
1262
5.63k
                                   IdentifierInfo *II, Preprocessor &PP) {
1263
  // __has_include_next is like __has_include, except that we start
1264
  // searching after the current found directory.  If we can't do this,
1265
  // issue a diagnostic.
1266
  // FIXME: Factor out duplication with
1267
  // Preprocessor::HandleIncludeNextDirective.
1268
5.63k
  const DirectoryLookup *Lookup = PP.GetCurDirLookup();
1269
5.63k
  const FileEntry *LookupFromFile = nullptr;
1270
5.63k
  if (PP.isInPrimaryFile() && 
PP.getLangOpts().IsHeaderFile7
) {
1271
    // If the main file is a header, then it's either for PCH/AST generation,
1272
    // or libclang opened it. Either way, handle it as a normal include below
1273
    // and do not complain about __has_include_next.
1274
5.63k
  } else if (PP.isInPrimaryFile()) {
1275
6
    Lookup = nullptr;
1276
6
    PP.Diag(Tok, diag::pp_include_next_in_primary);
1277
5.62k
  } else if (PP.getCurrentLexerSubmodule()) {
1278
    // Start looking up in the directory *after* the one in which the current
1279
    // file would be found, if any.
1280
166
    assert(PP.getCurrentLexer() && "#include_next directive in macro?");
1281
0
    LookupFromFile = PP.getCurrentLexer()->getFileEntry();
1282
166
    Lookup = nullptr;
1283
5.46k
  } else if (!Lookup) {
1284
0
    PP.Diag(Tok, diag::pp_include_next_absolute_path);
1285
5.46k
  } else {
1286
    // Start looking up in the next directory.
1287
5.46k
    ++Lookup;
1288
5.46k
  }
1289
1290
0
  return EvaluateHasIncludeCommon(Tok, II, PP, Lookup, LookupFromFile);
1291
5.63k
}
1292
1293
/// Process single-argument builtin feature-like macros that return
1294
/// integer values.
1295
static void EvaluateFeatureLikeBuiltinMacro(llvm::raw_svector_ostream& OS,
1296
                                            Token &Tok, IdentifierInfo *II,
1297
                                            Preprocessor &PP,
1298
                                            llvm::function_ref<
1299
                                              int(Token &Tok,
1300
594k
                                                  bool &HasLexedNextTok)> Op) {
1301
  // Parse the initial '('.
1302
594k
  PP.LexUnexpandedToken(Tok);
1303
594k
  if (Tok.isNot(tok::l_paren)) {
1304
3
    PP.Diag(Tok.getLocation(), diag::err_pp_expected_after) << II
1305
3
                                                            << tok::l_paren;
1306
1307
    // Provide a dummy '0' value on output stream to elide further errors.
1308
3
    if (!Tok.isOneOf(tok::eof, tok::eod)) {
1309
2
      OS << 0;
1310
2
      Tok.setKind(tok::numeric_constant);
1311
2
    }
1312
3
    return;
1313
3
  }
1314
1315
594k
  unsigned ParenDepth = 1;
1316
594k
  SourceLocation LParenLoc = Tok.getLocation();
1317
594k
  llvm::Optional<int> Result;
1318
1319
594k
  Token ResultTok;
1320
594k
  bool SuppressDiagnostic = false;
1321
1.18M
  while (true) {
1322
    // Parse next token.
1323
1.18M
    PP.LexUnexpandedToken(Tok);
1324
1325
1.18M
already_lexed:
1326
1.18M
    switch (Tok.getKind()) {
1327
1
      case tok::eof:
1328
3
      case tok::eod:
1329
        // Don't provide even a dummy value if the eod or eof marker is
1330
        // reached.  Simply provide a diagnostic.
1331
3
        PP.Diag(Tok.getLocation(), diag::err_unterm_macro_invoc);
1332
3
        return;
1333
1334
1
      case tok::comma:
1335
1
        if (!SuppressDiagnostic) {
1336
1
          PP.Diag(Tok.getLocation(), diag::err_too_many_args_in_macro_invoc);
1337
1
          SuppressDiagnostic = true;
1338
1
        }
1339
1
        continue;
1340
1341
3
      case tok::l_paren:
1342
3
        ++ParenDepth;
1343
3
        if (Result.hasValue())
1344
1
          break;
1345
2
        if (!SuppressDiagnostic) {
1346
1
          PP.Diag(Tok.getLocation(), diag::err_pp_nested_paren) << II;
1347
1
          SuppressDiagnostic = true;
1348
1
        }
1349
2
        continue;
1350
1351
594k
      case tok::r_paren:
1352
594k
        if (--ParenDepth > 0)
1353
3
          continue;
1354
1355
        // The last ')' has been reached; return the value if one found or
1356
        // a diagnostic and a dummy value.
1357
594k
        if (Result.hasValue()) {
1358
594k
          OS << Result.getValue();
1359
          // For strict conformance to __has_cpp_attribute rules, use 'L'
1360
          // suffix for dated literals.
1361
594k
          if (Result.getValue() > 1)
1362
4.46k
            OS << 'L';
1363
594k
        } else {
1364
3
          OS << 0;
1365
3
          if (!SuppressDiagnostic)
1366
2
            PP.Diag(Tok.getLocation(), diag::err_too_few_args_in_macro_invoc);
1367
3
        }
1368
594k
        Tok.setKind(tok::numeric_constant);
1369
594k
        return;
1370
1371
594k
      default: {
1372
        // Parse the macro argument, if one not found so far.
1373
594k
        if (Result.hasValue())
1374
3
          break;
1375
1376
594k
        bool HasLexedNextToken = false;
1377
594k
        Result = Op(Tok, HasLexedNextToken);
1378
594k
        ResultTok = Tok;
1379
594k
        if (HasLexedNextToken)
1380
1.84k
          goto already_lexed;
1381
592k
        continue;
1382
594k
      }
1383
1.18M
    }
1384
1385
    // Diagnose missing ')'.
1386
4
    if (!SuppressDiagnostic) {
1387
4
      if (auto Diag = PP.Diag(Tok.getLocation(), diag::err_pp_expected_after)) {
1388
4
        if (IdentifierInfo *LastII = ResultTok.getIdentifierInfo())
1389
2
          Diag << LastII;
1390
2
        else
1391
2
          Diag << ResultTok.getKind();
1392
4
        Diag << tok::r_paren << ResultTok.getLocation();
1393
4
      }
1394
4
      PP.Diag(LParenLoc, diag::note_matching) << tok::l_paren;
1395
4
      SuppressDiagnostic = true;
1396
4
    }
1397
4
  }
1398
594k
}
1399
1400
/// Helper function to return the IdentifierInfo structure of a Token
1401
/// or generate a diagnostic if none available.
1402
static IdentifierInfo *ExpectFeatureIdentifierInfo(Token &Tok,
1403
                                                   Preprocessor &PP,
1404
579k
                                                   signed DiagID) {
1405
579k
  IdentifierInfo *II;
1406
579k
  if (!Tok.isAnnotation() && (II = Tok.getIdentifierInfo()))
1407
579k
    return II;
1408
1409
2
  PP.Diag(Tok.getLocation(), DiagID);
1410
2
  return nullptr;
1411
579k
}
1412
1413
/// Implements the __is_target_arch builtin macro.
1414
1.32k
static bool isTargetArch(const TargetInfo &TI, const IdentifierInfo *II) {
1415
1.32k
  std::string ArchName = II->getName().lower() + "--";
1416
1.32k
  llvm::Triple Arch(ArchName);
1417
1.32k
  const llvm::Triple &TT = TI.getTriple();
1418
1.32k
  if (TT.isThumb()) {
1419
    // arm matches thumb or thumbv7. armv7 matches thumbv7.
1420
11
    if ((Arch.getSubArch() == llvm::Triple::NoSubArch ||
1421
11
         
Arch.getSubArch() == TT.getSubArch()7
) &&
1422
11
        
(8
(8
TT.getArch() == llvm::Triple::thumb8
&&
1423
8
          Arch.getArch() == llvm::Triple::arm) ||
1424
8
         
(6
TT.getArch() == llvm::Triple::thumbeb6
&&
1425
6
          
Arch.getArch() == llvm::Triple::armeb0
)))
1426
2
      return true;
1427
11
  }
1428
  // Check the parsed arch when it has no sub arch to allow Clang to
1429
  // match thumb to thumbv7 but to prohibit matching thumbv6 to thumbv7.
1430
1.32k
  return (Arch.getSubArch() == llvm::Triple::NoSubArch ||
1431
1.32k
          
Arch.getSubArch() == TT.getSubArch()15
) &&
1432
1.32k
         
Arch.getArch() == TT.getArch()1.31k
;
1433
1.32k
}
1434
1435
/// Implements the __is_target_vendor builtin macro.
1436
1.29k
static bool isTargetVendor(const TargetInfo &TI, const IdentifierInfo *II) {
1437
1.29k
  StringRef VendorName = TI.getTriple().getVendorName();
1438
1.29k
  if (VendorName.empty())
1439
0
    VendorName = "unknown";
1440
1.29k
  return VendorName.equals_insensitive(II->getName());
1441
1.29k
}
1442
1443
/// Implements the __is_target_os builtin macro.
1444
1.31k
static bool isTargetOS(const TargetInfo &TI, const IdentifierInfo *II) {
1445
1.31k
  std::string OSName =
1446
1.31k
      (llvm::Twine("unknown-unknown-") + II->getName().lower()).str();
1447
1.31k
  llvm::Triple OS(OSName);
1448
1.31k
  if (OS.getOS() == llvm::Triple::Darwin) {
1449
    // Darwin matches macos, ios, etc.
1450
6
    return TI.getTriple().isOSDarwin();
1451
6
  }
1452
1.30k
  return TI.getTriple().getOS() == OS.getOS();
1453
1.31k
}
1454
1455
/// Implements the __is_target_environment builtin macro.
1456
static bool isTargetEnvironment(const TargetInfo &TI,
1457
869
                                const IdentifierInfo *II) {
1458
869
  std::string EnvName = (llvm::Twine("---") + II->getName().lower()).str();
1459
869
  llvm::Triple Env(EnvName);
1460
869
  return TI.getTriple().getEnvironment() == Env.getEnvironment();
1461
869
}
1462
1463
/// ExpandBuiltinMacro - If an identifier token is read that is to be expanded
1464
/// as a builtin macro, handle it and return the next token as 'Tok'.
1465
1.21M
void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
1466
  // Figure out which token this is.
1467
1.21M
  IdentifierInfo *II = Tok.getIdentifierInfo();
1468
1.21M
  assert(II && "Can't be a macro without id info!");
1469
1470
  // If this is an _Pragma or Microsoft __pragma directive, expand it,
1471
  // invoke the pragma handler, then lex the token after it.
1472
1.21M
  if (II == Ident_Pragma)
1473
601k
    return Handle_Pragma(Tok);
1474
615k
  else if (II == Ident__pragma) // in non-MS mode this is null
1475
32
    return HandleMicrosoft__pragma(Tok);
1476
1477
615k
  ++NumBuiltinMacroExpanded;
1478
1479
615k
  SmallString<128> TmpBuffer;
1480
615k
  llvm::raw_svector_ostream OS(TmpBuffer);
1481
1482
  // Set up the return result.
1483
615k
  Tok.setIdentifierInfo(nullptr);
1484
615k
  Tok.clearFlag(Token::NeedsCleaning);
1485
615k
  bool IsAtStartOfLine = Tok.isAtStartOfLine();
1486
615k
  bool HasLeadingSpace = Tok.hasLeadingSpace();
1487
1488
615k
  if (II == Ident__LINE__) {
1489
    // C99 6.10.8: "__LINE__: The presumed line number (within the current
1490
    // source file) of the current source line (an integer constant)".  This can
1491
    // be affected by #line.
1492
2.88k
    SourceLocation Loc = Tok.getLocation();
1493
1494
    // Advance to the location of the first _, this might not be the first byte
1495
    // of the token if it starts with an escaped newline.
1496
2.88k
    Loc = AdvanceToTokenCharacter(Loc, 0);
1497
1498
    // One wrinkle here is that GCC expands __LINE__ to location of the *end* of
1499
    // a macro expansion.  This doesn't matter for object-like macros, but
1500
    // can matter for a function-like macro that expands to contain __LINE__.
1501
    // Skip down through expansion points until we find a file loc for the
1502
    // end of the expansion history.
1503
2.88k
    Loc = SourceMgr.getExpansionRange(Loc).getEnd();
1504
2.88k
    PresumedLoc PLoc = SourceMgr.getPresumedLoc(Loc);
1505
1506
    // __LINE__ expands to a simple numeric value.
1507
2.88k
    OS << (PLoc.isValid()? PLoc.getLine() : 
10
);
1508
2.88k
    Tok.setKind(tok::numeric_constant);
1509
612k
  } else if (II == Ident__FILE__ || 
II == Ident__BASE_FILE__612k
||
1510
612k
             
II == Ident__FILE_NAME__612k
) {
1511
    // C99 6.10.8: "__FILE__: The presumed name of the current source file (a
1512
    // character string literal)". This can be affected by #line.
1513
306
    PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
1514
1515
    // __BASE_FILE__ is a GNU extension that returns the top of the presumed
1516
    // #include stack instead of the current file.
1517
306
    if (II == Ident__BASE_FILE__ && 
PLoc.isValid()4
) {
1518
4
      SourceLocation NextLoc = PLoc.getIncludeLoc();
1519
8
      while (NextLoc.isValid()) {
1520
4
        PLoc = SourceMgr.getPresumedLoc(NextLoc);
1521
4
        if (PLoc.isInvalid())
1522
0
          break;
1523
1524
4
        NextLoc = PLoc.getIncludeLoc();
1525
4
      }
1526
4
    }
1527
1528
    // Escape this filename.  Turn '\' -> '\\' '"' -> '\"'
1529
306
    SmallString<256> FN;
1530
306
    if (PLoc.isValid()) {
1531
      // __FILE_NAME__ is a Clang-specific extension that expands to the
1532
      // the last part of __FILE__.
1533
306
      if (II == Ident__FILE_NAME__) {
1534
        // Try to get the last path component, failing that return the original
1535
        // presumed location.
1536
11
        StringRef PLFileName = llvm::sys::path::filename(PLoc.getFilename());
1537
11
        if (PLFileName != "")
1538
11
          FN += PLFileName;
1539
0
        else
1540
0
          FN += PLoc.getFilename();
1541
295
      } else {
1542
295
        FN += PLoc.getFilename();
1543
295
      }
1544
306
      getLangOpts().remapPathPrefix(FN);
1545
306
      Lexer::Stringify(FN);
1546
306
      OS << '"' << FN << '"';
1547
306
    }
1548
306
    Tok.setKind(tok::string_literal);
1549
612k
  } else if (II == Ident__DATE__) {
1550
7
    Diag(Tok.getLocation(), diag::warn_pp_date_time);
1551
7
    if (!DATELoc.isValid())
1552
4
      ComputeDATE_TIME(DATELoc, TIMELoc, *this);
1553
7
    Tok.setKind(tok::string_literal);
1554
7
    Tok.setLength(strlen("\"Mmm dd yyyy\""));
1555
7
    Tok.setLocation(SourceMgr.createExpansionLoc(DATELoc, Tok.getLocation(),
1556
7
                                                 Tok.getLocation(),
1557
7
                                                 Tok.getLength()));
1558
7
    return;
1559
612k
  } else if (II == Ident__TIME__) {
1560
6
    Diag(Tok.getLocation(), diag::warn_pp_date_time);
1561
6
    if (!TIMELoc.isValid())
1562
3
      ComputeDATE_TIME(DATELoc, TIMELoc, *this);
1563
6
    Tok.setKind(tok::string_literal);
1564
6
    Tok.setLength(strlen("\"hh:mm:ss\""));
1565
6
    Tok.setLocation(SourceMgr.createExpansionLoc(TIMELoc, Tok.getLocation(),
1566
6
                                                 Tok.getLocation(),
1567
6
                                                 Tok.getLength()));
1568
6
    return;
1569
612k
  } else if (II == Ident__INCLUDE_LEVEL__) {
1570
    // Compute the presumed include depth of this token.  This can be affected
1571
    // by GNU line markers.
1572
9
    unsigned Depth = 0;
1573
1574
9
    PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
1575
9
    if (PLoc.isValid()) {
1576
9
      PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc());
1577
15
      for (; PLoc.isValid(); 
++Depth6
)
1578
6
        PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc());
1579
9
    }
1580
1581
    // __INCLUDE_LEVEL__ expands to a simple numeric value.
1582
9
    OS << Depth;
1583
9
    Tok.setKind(tok::numeric_constant);
1584
612k
  } else if (II == Ident__TIMESTAMP__) {
1585
3
    Diag(Tok.getLocation(), diag::warn_pp_date_time);
1586
    // MSVC, ICC, GCC, VisualAge C++ extension.  The generated string should be
1587
    // of the form "Ddd Mmm dd hh::mm::ss yyyy", which is returned by asctime.
1588
1589
    // Get the file that we are lexing out of.  If we're currently lexing from
1590
    // a macro, dig into the include stack.
1591
3
    const FileEntry *CurFile = nullptr;
1592
3
    PreprocessorLexer *TheLexer = getCurrentFileLexer();
1593
1594
3
    if (TheLexer)
1595
3
      CurFile = SourceMgr.getFileEntryForID(TheLexer->getFileID());
1596
1597
3
    const char *Result;
1598
3
    if (CurFile) {
1599
3
      time_t TT = CurFile->getModificationTime();
1600
3
      struct tm *TM = localtime(&TT);
1601
3
      Result = asctime(TM);
1602
3
    } else {
1603
0
      Result = "??? ??? ?? ??:??:?? ????\n";
1604
0
    }
1605
    // Surround the string with " and strip the trailing newline.
1606
3
    OS << '"' << StringRef(Result).drop_back() << '"';
1607
3
    Tok.setKind(tok::string_literal);
1608
612k
  } else if (II == Ident__FLT_EVAL_METHOD__) {
1609
    // __FLT_EVAL_METHOD__ expands to a simple numeric value.
1610
265
    OS << getCurrentFPEvalMethod();
1611
265
    Tok.setKind(tok::numeric_constant);
1612
611k
  } else if (II == Ident__COUNTER__) {
1613
    // __COUNTER__ expands to a simple numeric value.
1614
1.14k
    OS << CounterValue++;
1615
1.14k
    Tok.setKind(tok::numeric_constant);
1616
610k
  } else if (II == Ident__has_feature) {
1617
462k
    EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this,
1618
462k
      [this](Token &Tok, bool &HasLexedNextToken) -> int {
1619
462k
        IdentifierInfo *II = ExpectFeatureIdentifierInfo(Tok, *this,
1620
462k
                                           diag::err_feature_check_malformed);
1621
462k
        return II && 
HasFeature(*this, II->getName())462k
;
1622
462k
      });
1623
462k
  } else 
if (148k
II == Ident__has_extension148k
) {
1624
16.4k
    EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this,
1625
16.4k
      [this](Token &Tok, bool &HasLexedNextToken) -> int {
1626
16.4k
        IdentifierInfo *II = ExpectFeatureIdentifierInfo(Tok, *this,
1627
16.4k
                                           diag::err_feature_check_malformed);
1628
16.4k
        return II && HasExtension(*this, II->getName());
1629
16.4k
      });
1630
131k
  } else if (II == Ident__has_builtin) {
1631
32.0k
    EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this,
1632
32.0k
      [this](Token &Tok, bool &HasLexedNextToken) -> int {
1633
32.0k
        IdentifierInfo *II = ExpectFeatureIdentifierInfo(Tok, *this,
1634
32.0k
                                           diag::err_feature_check_malformed);
1635
32.0k
        if (!II)
1636
0
          return false;
1637
32.0k
        else if (II->getBuiltinID() != 0) {
1638
12.9k
          switch (II->getBuiltinID()) {
1639
864
          case Builtin::BI__builtin_operator_new:
1640
1.72k
          case Builtin::BI__builtin_operator_delete:
1641
            // denotes date of behavior change to support calling arbitrary
1642
            // usual allocation and deallocation functions. Required by libc++
1643
1.72k
            return 201802;
1644
11.2k
          default:
1645
11.2k
            return true;
1646
12.9k
          }
1647
0
          return true;
1648
19.0k
        } else if (II->getTokenID() != tok::identifier ||
1649
19.0k
                   
II->hasRevertedTokenIDToIdentifier()19.0k
) {
1650
          // Treat all keywords that introduce a custom syntax of the form
1651
          //
1652
          //   '__some_keyword' '(' [...] ')'
1653
          //
1654
          // as being "builtin functions", even if the syntax isn't a valid
1655
          // function call (for example, because the builtin takes a type
1656
          // argument).
1657
22
          if (II->getName().startswith("__builtin_") ||
1658
22
              
II->getName().startswith("__is_")5
||
1659
22
              
II->getName().startswith("__has_")3
)
1660
20
            return true;
1661
2
          return llvm::StringSwitch<bool>(II->getName())
1662
2
              .Case("__array_rank", true)
1663
2
              .Case("__array_extent", true)
1664
2
              .Case("__reference_binds_to_temporary", true)
1665
2
              .Case("__underlying_type", true)
1666
2
              .Default(false);
1667
19.0k
        } else {
1668
19.0k
          return llvm::StringSwitch<bool>(II->getName())
1669
              // Report builtin templates as being builtins.
1670
19.0k
              .Case("__make_integer_seq", getLangOpts().CPlusPlus)
1671
19.0k
              .Case("__type_pack_element", getLangOpts().CPlusPlus)
1672
              // Likewise for some builtin preprocessor macros.
1673
              // FIXME: This is inconsistent; we usually suggest detecting
1674
              // builtin macros via #ifdef. Don't add more cases here.
1675
19.0k
              .Case("__is_target_arch", true)
1676
19.0k
              .Case("__is_target_vendor", true)
1677
19.0k
              .Case("__is_target_os", true)
1678
19.0k
              .Case("__is_target_environment", true)
1679
19.0k
              .Default(false);
1680
19.0k
        }
1681
32.0k
      });
1682
99.6k
  } else if (II == Ident__is_identifier) {
1683
15.1k
    EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this,
1684
15.1k
      [](Token &Tok, bool &HasLexedNextToken) -> int {
1685
15.1k
        return Tok.is(tok::identifier);
1686
15.1k
      });
1687
84.5k
  } else if (II == Ident__has_attribute) {
1688
58.8k
    EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this,
1689
58.8k
      [this](Token &Tok, bool &HasLexedNextToken) -> int {
1690
58.8k
        IdentifierInfo *II = ExpectFeatureIdentifierInfo(Tok, *this,
1691
58.8k
                                           diag::err_feature_check_malformed);
1692
58.8k
        return II ? hasAttribute(AttrSyntax::GNU, nullptr, II,
1693
58.8k
                                 getTargetInfo(), getLangOpts()) : 
00
;
1694
58.8k
      });
1695
58.8k
  } else 
if (25.6k
II == Ident__has_declspec25.6k
) {
1696
906
    EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this,
1697
906
      [this](Token &Tok, bool &HasLexedNextToken) -> int {
1698
906
        IdentifierInfo *II = ExpectFeatureIdentifierInfo(Tok, *this,
1699
906
                                           diag::err_feature_check_malformed);
1700
906
        if (II) {
1701
906
          const LangOptions &LangOpts = getLangOpts();
1702
906
          return LangOpts.DeclSpecKeyword &&
1703
906
                 hasAttribute(AttrSyntax::Declspec, nullptr, II,
1704
8
                              getTargetInfo(), LangOpts);
1705
906
        }
1706
1707
0
        return false;
1708
906
      });
1709
24.7k
  } else if (II == Ident__has_cpp_attribute || 
II == Ident__has_c_attribute21.9k
) {
1710
2.78k
    bool IsCXX = II == Ident__has_cpp_attribute;
1711
2.78k
    EvaluateFeatureLikeBuiltinMacro(
1712
2.78k
        OS, Tok, II, *this, [&](Token &Tok, bool &HasLexedNextToken) -> int {
1713
2.78k
          IdentifierInfo *ScopeII = nullptr;
1714
2.78k
          IdentifierInfo *II = ExpectFeatureIdentifierInfo(
1715
2.78k
              Tok, *this, diag::err_feature_check_malformed);
1716
2.78k
          if (!II)
1717
0
            return false;
1718
1719
          // It is possible to receive a scope token.  Read the "::", if it is
1720
          // available, and the subsequent identifier.
1721
2.78k
          LexUnexpandedToken(Tok);
1722
2.78k
          if (Tok.isNot(tok::coloncolon))
1723
1.84k
            HasLexedNextToken = true;
1724
938
          else {
1725
938
            ScopeII = II;
1726
938
            LexUnexpandedToken(Tok);
1727
938
            II = ExpectFeatureIdentifierInfo(Tok, *this,
1728
938
                                             diag::err_feature_check_malformed);
1729
938
          }
1730
1731
2.78k
          AttrSyntax Syntax = IsCXX ? 
AttrSyntax::CXX2.74k
:
AttrSyntax::C31
;
1732
2.78k
          return II ? hasAttribute(Syntax, ScopeII, II, getTargetInfo(),
1733
2.78k
                                   getLangOpts())
1734
2.78k
                    : 
00
;
1735
2.78k
        });
1736
21.9k
  } else if (II == Ident__has_include || 
II == Ident__has_include_next11.0k
) {
1737
    // The argument to these two builtins should be a parenthesized
1738
    // file name string literal using angle brackets (<>) or
1739
    // double-quotes ("").
1740
16.5k
    bool Value;
1741
16.5k
    if (II == Ident__has_include)
1742
10.9k
      Value = EvaluateHasInclude(Tok, II, *this);
1743
5.63k
    else
1744
5.63k
      Value = EvaluateHasIncludeNext(Tok, II, *this);
1745
1746
16.5k
    if (Tok.isNot(tok::r_paren))
1747
23
      return;
1748
16.5k
    OS << (int)Value;
1749
16.5k
    Tok.setKind(tok::numeric_constant);
1750
16.5k
  } else 
if (5.37k
II == Ident__has_warning5.37k
) {
1751
    // The argument should be a parenthesized string literal.
1752
10
    EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this,
1753
10
      [this](Token &Tok, bool &HasLexedNextToken) -> int {
1754
7
        std::string WarningName;
1755
7
        SourceLocation StrStartLoc = Tok.getLocation();
1756
1757
7
        HasLexedNextToken = Tok.is(tok::string_literal);
1758
7
        if (!FinishLexStringLiteral(Tok, WarningName, "'__has_warning'",
1759
7
                                    /*AllowMacroExpansion=*/false))
1760
2
          return false;
1761
1762
        // FIXME: Should we accept "-R..." flags here, or should that be
1763
        // handled by a separate __has_remark?
1764
5
        if (WarningName.size() < 3 || WarningName[0] != '-' ||
1765
5
            
WarningName[1] != 'W'4
) {
1766
1
          Diag(StrStartLoc, diag::warn_has_warning_invalid_option);
1767
1
          return false;
1768
1
        }
1769
1770
        // Finally, check if the warning flags maps to a diagnostic group.
1771
        // We construct a SmallVector here to talk to getDiagnosticIDs().
1772
        // Although we don't use the result, this isn't a hot path, and not
1773
        // worth special casing.
1774
4
        SmallVector<diag::kind, 10> Diags;
1775
4
        return !getDiagnostics().getDiagnosticIDs()->
1776
4
                getDiagnosticsInGroup(diag::Flavor::WarningOrError,
1777
4
                                      WarningName.substr(2), Diags);
1778
5
      });
1779
5.36k
  } else if (II == Ident__building_module) {
1780
    // The argument to this builtin should be an identifier. The
1781
    // builtin evaluates to 1 when that identifier names the module we are
1782
    // currently building.
1783
523
    EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this,
1784
523
      [this](Token &Tok, bool &HasLexedNextToken) -> int {
1785
523
        IdentifierInfo *II = ExpectFeatureIdentifierInfo(Tok, *this,
1786
523
                                       diag::err_expected_id_building_module);
1787
523
        return getLangOpts().isCompilingModule() && 
II27
&&
1788
523
               
(II->getName() == getLangOpts().CurrentModule)27
;
1789
523
      });
1790
4.83k
  } else if (II == Ident__MODULE__) {
1791
    // The current module as an identifier.
1792
7
    OS << getLangOpts().CurrentModule;
1793
7
    IdentifierInfo *ModuleII = getIdentifierInfo(getLangOpts().CurrentModule);
1794
7
    Tok.setIdentifierInfo(ModuleII);
1795
7
    Tok.setKind(ModuleII->getTokenID());
1796
4.83k
  } else if (II == Ident__identifier) {
1797
24
    SourceLocation Loc = Tok.getLocation();
1798
1799
    // We're expecting '__identifier' '(' identifier ')'. Try to recover
1800
    // if the parens are missing.
1801
24
    LexNonComment(Tok);
1802
24
    if (Tok.isNot(tok::l_paren)) {
1803
      // No '(', use end of last token.
1804
1
      Diag(getLocForEndOfToken(Loc), diag::err_pp_expected_after)
1805
1
        << II << tok::l_paren;
1806
      // If the next token isn't valid as our argument, we can't recover.
1807
1
      if (!Tok.isAnnotation() && Tok.getIdentifierInfo())
1808
1
        Tok.setKind(tok::identifier);
1809
1
      return;
1810
1
    }
1811
1812
23
    SourceLocation LParenLoc = Tok.getLocation();
1813
23
    LexNonComment(Tok);
1814
1815
23
    if (!Tok.isAnnotation() && Tok.getIdentifierInfo())
1816
13
      Tok.setKind(tok::identifier);
1817
10
    else if (Tok.is(tok::string_literal) && 
!Tok.hasUDSuffix()5
) {
1818
5
      StringLiteralParser Literal(Tok, *this);
1819
5
      if (Literal.hadError)
1820
0
        return;
1821
1822
5
      Tok.setIdentifierInfo(getIdentifierInfo(Literal.GetString()));
1823
5
      Tok.setKind(tok::identifier);
1824
5
    } else {
1825
5
      Diag(Tok.getLocation(), diag::err_pp_identifier_arg_not_identifier)
1826
5
        << Tok.getKind();
1827
      // Don't walk past anything that's not a real token.
1828
5
      if (Tok.isOneOf(tok::eof, tok::eod) || Tok.isAnnotation())
1829
0
        return;
1830
5
    }
1831
1832
    // Discard the ')', preserving 'Tok' as our result.
1833
23
    Token RParen;
1834
23
    LexNonComment(RParen);
1835
23
    if (RParen.isNot(tok::r_paren)) {
1836
1
      Diag(getLocForEndOfToken(Tok.getLocation()), diag::err_pp_expected_after)
1837
1
        << Tok.getKind() << tok::r_paren;
1838
1
      Diag(LParenLoc, diag::note_matching) << tok::l_paren;
1839
1
    }
1840
23
    return;
1841
4.80k
  } else if (II == Ident__is_target_arch) {
1842
1.32k
    EvaluateFeatureLikeBuiltinMacro(
1843
1.32k
        OS, Tok, II, *this, [this](Token &Tok, bool &HasLexedNextToken) -> int {
1844
1.32k
          IdentifierInfo *II = ExpectFeatureIdentifierInfo(
1845
1.32k
              Tok, *this, diag::err_feature_check_malformed);
1846
1.32k
          return II && 
isTargetArch(getTargetInfo(), II)1.32k
;
1847
1.32k
        });
1848
3.48k
  } else if (II == Ident__is_target_vendor) {
1849
1.29k
    EvaluateFeatureLikeBuiltinMacro(
1850
1.29k
        OS, Tok, II, *this, [this](Token &Tok, bool &HasLexedNextToken) -> int {
1851
1.29k
          IdentifierInfo *II = ExpectFeatureIdentifierInfo(
1852
1.29k
              Tok, *this, diag::err_feature_check_malformed);
1853
1.29k
          return II && isTargetVendor(getTargetInfo(), II);
1854
1.29k
        });
1855
2.18k
  } else if (II == Ident__is_target_os) {
1856
1.31k
    EvaluateFeatureLikeBuiltinMacro(
1857
1.31k
        OS, Tok, II, *this, [this](Token &Tok, bool &HasLexedNextToken) -> int {
1858
1.31k
          IdentifierInfo *II = ExpectFeatureIdentifierInfo(
1859
1.31k
              Tok, *this, diag::err_feature_check_malformed);
1860
1.31k
          return II && isTargetOS(getTargetInfo(), II);
1861
1.31k
        });
1862
1.31k
  } else 
if (869
II == Ident__is_target_environment869
) {
1863
869
    EvaluateFeatureLikeBuiltinMacro(
1864
869
        OS, Tok, II, *this, [this](Token &Tok, bool &HasLexedNextToken) -> int {
1865
869
          IdentifierInfo *II = ExpectFeatureIdentifierInfo(
1866
869
              Tok, *this, diag::err_feature_check_malformed);
1867
869
          return II && isTargetEnvironment(getTargetInfo(), II);
1868
869
        });
1869
869
  } else {
1870
0
    llvm_unreachable("Unknown identifier!");
1871
0
  }
1872
615k
  CreateString(OS.str(), Tok, Tok.getLocation(), Tok.getLocation());
1873
615k
  Tok.setFlagValue(Token::StartOfLine, IsAtStartOfLine);
1874
615k
  Tok.setFlagValue(Token::LeadingSpace, HasLeadingSpace);
1875
615k
}
1876
1877
62.6M
void Preprocessor::markMacroAsUsed(MacroInfo *MI) {
1878
  // If the 'used' status changed, and the macro requires 'unused' warning,
1879
  // remove its SourceLocation from the warn-for-unused-macro locations.
1880
62.6M
  if (MI->isWarnIfUnused() && 
!MI->isUsed()4
)
1881
4
    WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());
1882
62.6M
  MI->setIsUsed(true);
1883
62.6M
}