Coverage Report

Created: 2020-09-22 08:39

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