Coverage Report

Created: 2024-01-17 10:31

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