Coverage Report

Created: 2019-07-24 05:18

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