Coverage Report

Created: 2020-02-15 09:57

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