clang  16.0.0git
PPMacroExpansion.cpp
Go to the documentation of this file.
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 
15 #include "clang/Basic/Attributes.h"
16 #include "clang/Basic/Builtins.h"
19 #include "clang/Basic/LLVM.h"
23 #include "clang/Basic/TargetInfo.h"
27 #include "clang/Lex/HeaderSearch.h"
30 #include "clang/Lex/MacroArgs.h"
31 #include "clang/Lex/MacroInfo.h"
32 #include "clang/Lex/Preprocessor.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/None.h"
41 #include "llvm/ADT/Optional.h"
42 #include "llvm/ADT/STLExtras.h"
43 #include "llvm/ADT/SmallString.h"
44 #include "llvm/ADT/SmallVector.h"
45 #include "llvm/ADT/StringRef.h"
46 #include "llvm/ADT/StringSwitch.h"
47 #include "llvm/Support/Casting.h"
48 #include "llvm/Support/ErrorHandling.h"
49 #include "llvm/Support/Format.h"
50 #include "llvm/Support/Path.h"
51 #include "llvm/Support/raw_ostream.h"
52 #include <algorithm>
53 #include <cassert>
54 #include <cstddef>
55 #include <cstring>
56 #include <ctime>
57 #include <string>
58 #include <tuple>
59 #include <utility>
60 
61 using namespace clang;
62 
65  if (!II->hadMacroDefinition())
66  return nullptr;
67  auto Pos = CurSubmoduleState->Macros.find(II);
68  return Pos == CurSubmoduleState->Macros.end() ? nullptr
69  : Pos->second.getLatest();
70 }
71 
73  assert(MD && "MacroDirective should be non-zero!");
74  assert(!MD->getPrevious() && "Already attached to a MacroDirective history.");
75 
76  MacroState &StoredMD = CurSubmoduleState->Macros[II];
77  auto *OldMD = StoredMD.getLatest();
78  MD->setPrevious(OldMD);
79  StoredMD.setLatest(MD);
80  StoredMD.overrideActiveModuleMacros(*this, II);
81 
82  if (needModuleMacros()) {
83  // Track that we created a new macro directive, so we know we should
84  // consider building a ModuleMacro for it when we get to the end of
85  // the module.
86  PendingModuleMacroNames.push_back(II);
87  }
88 
89  // Set up the identifier as having associated macro history.
90  II->setHasMacroDefinition(true);
91  if (!MD->isDefined() && LeafModuleMacros.find(II) == LeafModuleMacros.end())
92  II->setHasMacroDefinition(false);
93  if (II->isFromAST())
95 }
96 
98  MacroDirective *ED,
99  MacroDirective *MD) {
100  // Normally, when a macro is defined, it goes through appendMacroDirective()
101  // above, which chains a macro to previous defines, undefs, etc.
102  // However, in a pch, the whole macro history up to the end of the pch is
103  // stored, so ASTReader goes through this function instead.
104  // However, built-in macros are already registered in the Preprocessor
105  // ctor, and ASTWriter stops writing the macro chain at built-in macros,
106  // so in that case the chain from the pch needs to be spliced to the existing
107  // built-in.
108 
109  assert(II && MD);
110  MacroState &StoredMD = CurSubmoduleState->Macros[II];
111 
112  if (auto *OldMD = StoredMD.getLatest()) {
113  // shouldIgnoreMacro() in ASTWriter also stops at macros from the
114  // predefines buffer in module builds. However, in module builds, modules
115  // are loaded completely before predefines are processed, so StoredMD
116  // will be nullptr for them when they're loaded. StoredMD should only be
117  // non-nullptr for builtins read from a pch file.
118  assert(OldMD->getMacroInfo()->isBuiltinMacro() &&
119  "only built-ins should have an entry here");
120  assert(!OldMD->getPrevious() && "builtin should only have a single entry");
121  ED->setPrevious(OldMD);
122  StoredMD.setLatest(MD);
123  } else {
124  StoredMD = MD;
125  }
126 
127  // Setup the identifier as having associated macro history.
128  II->setHasMacroDefinition(true);
129  if (!MD->isDefined() && LeafModuleMacros.find(II) == LeafModuleMacros.end())
130  II->setHasMacroDefinition(false);
131 }
132 
134  MacroInfo *Macro,
135  ArrayRef<ModuleMacro *> Overrides,
136  bool &New) {
137  llvm::FoldingSetNodeID ID;
138  ModuleMacro::Profile(ID, Mod, II);
139 
140  void *InsertPos;
141  if (auto *MM = ModuleMacros.FindNodeOrInsertPos(ID, InsertPos)) {
142  New = false;
143  return MM;
144  }
145 
146  auto *MM = ModuleMacro::create(*this, Mod, II, Macro, Overrides);
147  ModuleMacros.InsertNode(MM, InsertPos);
148 
149  // Each overridden macro is now overridden by one more macro.
150  bool HidAny = false;
151  for (auto *O : Overrides) {
152  HidAny |= (O->NumOverriddenBy == 0);
153  ++O->NumOverriddenBy;
154  }
155 
156  // If we were the first overrider for any macro, it's no longer a leaf.
157  auto &LeafMacros = LeafModuleMacros[II];
158  if (HidAny) {
159  llvm::erase_if(LeafMacros,
160  [](ModuleMacro *MM) { return MM->NumOverriddenBy != 0; });
161  }
162 
163  // The new macro is always a leaf macro.
164  LeafMacros.push_back(MM);
165  // The identifier now has defined macros (that may or may not be visible).
166  II->setHasMacroDefinition(true);
167 
168  New = true;
169  return MM;
170 }
171 
173  const IdentifierInfo *II) {
174  llvm::FoldingSetNodeID ID;
175  ModuleMacro::Profile(ID, Mod, II);
176 
177  void *InsertPos;
178  return ModuleMacros.FindNodeOrInsertPos(ID, InsertPos);
179 }
180 
181 void Preprocessor::updateModuleMacroInfo(const IdentifierInfo *II,
182  ModuleMacroInfo &Info) {
183  assert(Info.ActiveModuleMacrosGeneration !=
184  CurSubmoduleState->VisibleModules.getGeneration() &&
185  "don't need to update this macro name info");
186  Info.ActiveModuleMacrosGeneration =
187  CurSubmoduleState->VisibleModules.getGeneration();
188 
189  auto Leaf = LeafModuleMacros.find(II);
190  if (Leaf == LeafModuleMacros.end()) {
191  // No imported macros at all: nothing to do.
192  return;
193  }
194 
195  Info.ActiveModuleMacros.clear();
196 
197  // Every macro that's locally overridden is overridden by a visible macro.
198  llvm::DenseMap<ModuleMacro *, int> NumHiddenOverrides;
199  for (auto *O : Info.OverriddenMacros)
200  NumHiddenOverrides[O] = -1;
201 
202  // Collect all macros that are not overridden by a visible macro.
204  for (auto *LeafMM : Leaf->second) {
205  assert(LeafMM->getNumOverridingMacros() == 0 && "leaf macro overridden");
206  if (NumHiddenOverrides.lookup(LeafMM) == 0)
207  Worklist.push_back(LeafMM);
208  }
209  while (!Worklist.empty()) {
210  auto *MM = Worklist.pop_back_val();
211  if (CurSubmoduleState->VisibleModules.isVisible(MM->getOwningModule())) {
212  // We only care about collecting definitions; undefinitions only act
213  // to override other definitions.
214  if (MM->getMacroInfo())
215  Info.ActiveModuleMacros.push_back(MM);
216  } else {
217  for (auto *O : MM->overrides())
218  if ((unsigned)++NumHiddenOverrides[O] == O->getNumOverridingMacros())
219  Worklist.push_back(O);
220  }
221  }
222  // Our reverse postorder walk found the macros in reverse order.
223  std::reverse(Info.ActiveModuleMacros.begin(), Info.ActiveModuleMacros.end());
224 
225  // Determine whether the macro name is ambiguous.
226  MacroInfo *MI = nullptr;
227  bool IsSystemMacro = true;
228  bool IsAmbiguous = false;
229  if (auto *MD = Info.MD) {
230  while (MD && isa<VisibilityMacroDirective>(MD))
231  MD = MD->getPrevious();
232  if (auto *DMD = dyn_cast_or_null<DefMacroDirective>(MD)) {
233  MI = DMD->getInfo();
234  IsSystemMacro &= SourceMgr.isInSystemHeader(DMD->getLocation());
235  }
236  }
237  for (auto *Active : Info.ActiveModuleMacros) {
238  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  if (MI && NewMI != MI &&
250  !MI->isIdenticalTo(*NewMI, *this, /*Syntactically=*/true))
251  IsAmbiguous = true;
252  IsSystemMacro &= Active->getOwningModule()->IsSystem ||
253  SourceMgr.isInSystemHeader(NewMI->getDefinitionLoc());
254  MI = NewMI;
255  }
256  Info.IsAmbiguous = IsAmbiguous && !IsSystemMacro;
257 }
258 
261  auto LeafIt = LeafModuleMacros.find(II);
262  if (LeafIt != LeafModuleMacros.end())
263  Leaf = LeafIt->second;
264  const MacroState *State = nullptr;
265  auto Pos = CurSubmoduleState->Macros.find(II);
266  if (Pos != CurSubmoduleState->Macros.end())
267  State = &Pos->second;
268 
269  llvm::errs() << "MacroState " << State << " " << II->getNameStart();
270  if (State && State->isAmbiguous(*this, II))
271  llvm::errs() << " ambiguous";
272  if (State && !State->getOverriddenMacros().empty()) {
273  llvm::errs() << " overrides";
274  for (auto *O : State->getOverriddenMacros())
275  llvm::errs() << " " << O->getOwningModule()->getFullModuleName();
276  }
277  llvm::errs() << "\n";
278 
279  // Dump local macro directives.
280  for (auto *MD = State ? State->getLatest() : nullptr; MD;
281  MD = MD->getPrevious()) {
282  llvm::errs() << " ";
283  MD->dump();
284  }
285 
286  // Dump module macros.
288  for (auto *MM : State ? State->getActiveModuleMacros(*this, II) : None)
289  Active.insert(MM);
291  llvm::SmallVector<ModuleMacro *, 16> Worklist(Leaf.begin(), Leaf.end());
292  while (!Worklist.empty()) {
293  auto *MM = Worklist.pop_back_val();
294  llvm::errs() << " ModuleMacro " << MM << " "
295  << MM->getOwningModule()->getFullModuleName();
296  if (!MM->getMacroInfo())
297  llvm::errs() << " undef";
298 
299  if (Active.count(MM))
300  llvm::errs() << " active";
301  else if (!CurSubmoduleState->VisibleModules.isVisible(
302  MM->getOwningModule()))
303  llvm::errs() << " hidden";
304  else if (MM->getMacroInfo())
305  llvm::errs() << " overridden";
306 
307  if (!MM->overrides().empty()) {
308  llvm::errs() << " overrides";
309  for (auto *O : MM->overrides()) {
310  llvm::errs() << " " << O->getOwningModule()->getFullModuleName();
311  if (Visited.insert(O).second)
312  Worklist.push_back(O);
313  }
314  }
315  llvm::errs() << "\n";
316  if (auto *MI = MM->getMacroInfo()) {
317  llvm::errs() << " ";
318  MI->dump();
319  llvm::errs() << "\n";
320  }
321  }
322 }
323 
324 /// RegisterBuiltinMacro - Register the specified identifier in the identifier
325 /// table and mark it as a builtin macro to be expanded.
326 static IdentifierInfo *RegisterBuiltinMacro(Preprocessor &PP, const char *Name){
327  // Get the identifier.
328  IdentifierInfo *Id = PP.getIdentifierInfo(Name);
329 
330  // Mark it as being a macro that is builtin.
332  MI->setIsBuiltinMacro();
333  PP.appendDefMacroDirective(Id, MI);
334  return Id;
335 }
336 
337 /// RegisterBuiltinMacros - Register builtin macros, such as __LINE__ with the
338 /// identifier table.
339 void Preprocessor::RegisterBuiltinMacros() {
340  Ident__LINE__ = RegisterBuiltinMacro(*this, "__LINE__");
341  Ident__FILE__ = RegisterBuiltinMacro(*this, "__FILE__");
342  Ident__DATE__ = RegisterBuiltinMacro(*this, "__DATE__");
343  Ident__TIME__ = RegisterBuiltinMacro(*this, "__TIME__");
344  Ident__COUNTER__ = RegisterBuiltinMacro(*this, "__COUNTER__");
345  Ident_Pragma = RegisterBuiltinMacro(*this, "_Pragma");
346  Ident__FLT_EVAL_METHOD__ = RegisterBuiltinMacro(*this, "__FLT_EVAL_METHOD__");
347 
348  // C++ Standing Document Extensions.
349  if (getLangOpts().CPlusPlus)
350  Ident__has_cpp_attribute =
351  RegisterBuiltinMacro(*this, "__has_cpp_attribute");
352  else
353  Ident__has_cpp_attribute = nullptr;
354 
355  // GCC Extensions.
356  Ident__BASE_FILE__ = RegisterBuiltinMacro(*this, "__BASE_FILE__");
357  Ident__INCLUDE_LEVEL__ = RegisterBuiltinMacro(*this, "__INCLUDE_LEVEL__");
358  Ident__TIMESTAMP__ = RegisterBuiltinMacro(*this, "__TIMESTAMP__");
359 
360  // Microsoft Extensions.
361  if (getLangOpts().MicrosoftExt) {
362  Ident__identifier = RegisterBuiltinMacro(*this, "__identifier");
363  Ident__pragma = RegisterBuiltinMacro(*this, "__pragma");
364  } else {
365  Ident__identifier = nullptr;
366  Ident__pragma = nullptr;
367  }
368 
369  // Clang Extensions.
370  Ident__FILE_NAME__ = RegisterBuiltinMacro(*this, "__FILE_NAME__");
371  Ident__has_feature = RegisterBuiltinMacro(*this, "__has_feature");
372  Ident__has_extension = RegisterBuiltinMacro(*this, "__has_extension");
373  Ident__has_builtin = RegisterBuiltinMacro(*this, "__has_builtin");
374  Ident__has_constexpr_builtin =
375  RegisterBuiltinMacro(*this, "__has_constexpr_builtin");
376  Ident__has_attribute = RegisterBuiltinMacro(*this, "__has_attribute");
377  if (!getLangOpts().CPlusPlus)
378  Ident__has_c_attribute = RegisterBuiltinMacro(*this, "__has_c_attribute");
379  else
380  Ident__has_c_attribute = nullptr;
381 
382  Ident__has_declspec = RegisterBuiltinMacro(*this, "__has_declspec_attribute");
383  Ident__has_include = RegisterBuiltinMacro(*this, "__has_include");
384  Ident__has_include_next = RegisterBuiltinMacro(*this, "__has_include_next");
385  Ident__has_warning = RegisterBuiltinMacro(*this, "__has_warning");
386  Ident__is_identifier = RegisterBuiltinMacro(*this, "__is_identifier");
387  Ident__is_target_arch = RegisterBuiltinMacro(*this, "__is_target_arch");
388  Ident__is_target_vendor = RegisterBuiltinMacro(*this, "__is_target_vendor");
389  Ident__is_target_os = RegisterBuiltinMacro(*this, "__is_target_os");
390  Ident__is_target_environment =
391  RegisterBuiltinMacro(*this, "__is_target_environment");
392  Ident__is_target_variant_os =
393  RegisterBuiltinMacro(*this, "__is_target_variant_os");
394  Ident__is_target_variant_environment =
395  RegisterBuiltinMacro(*this, "__is_target_variant_environment");
396 
397  // Modules.
398  Ident__building_module = RegisterBuiltinMacro(*this, "__building_module");
399  if (!getLangOpts().CurrentModule.empty())
400  Ident__MODULE__ = RegisterBuiltinMacro(*this, "__MODULE__");
401  else
402  Ident__MODULE__ = nullptr;
403 }
404 
405 /// isTrivialSingleTokenExpansion - Return true if MI, which has a single token
406 /// in its expansion, currently expands to that token literally.
408  const IdentifierInfo *MacroIdent,
409  Preprocessor &PP) {
411 
412  // If the token isn't an identifier, it's always literally expanded.
413  if (!II) return true;
414 
415  // If the information about this identifier is out of date, update it from
416  // the external source.
417  if (II->isOutOfDate())
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  if (auto *ExpansionMI = PP.getMacroInfo(II))
423  if (ExpansionMI->isEnabled() &&
424  // Fast expanding "#define X X" is ok, because X would be disabled.
425  II != MacroIdent)
426  return false;
427 
428  // If this is an object-like macro invocation, it is safe to trivially expand
429  // it.
430  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  return !llvm::is_contained(MI->params(), II);
435 }
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 bool Preprocessor::isNextPPTokenLParen() {
441  // Do some quick tests for rejection cases.
442  unsigned Val;
443  if (CurLexer)
444  Val = CurLexer->isNextPPTokenLParen();
445  else
446  Val = CurTokenLexer->isNextTokenLParen();
447 
448  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  if (CurPPLexer)
453  return false;
454  for (const IncludeStackInfo &Entry : llvm::reverse(IncludeMacroStack)) {
455  if (Entry.TheLexer)
456  Val = Entry.TheLexer->isNextPPTokenLParen();
457  else
458  Val = Entry.TheTokenLexer->isNextTokenLParen();
459 
460  if (Val != 2)
461  break;
462 
463  // Ran off the end of a source file?
464  if (Entry.ThePPLexer)
465  return false;
466  }
467  }
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  return Val == 1;
473 }
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  const MacroDefinition &M) {
480 
481  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  if (CurPPLexer) CurPPLexer->MIOpt.ExpandedMacro();
487 
488  // If this is a builtin macro, like __LINE__ or _Pragma, handle it specially.
489  if (MI->isBuiltinMacro()) {
490  if (Callbacks)
491  Callbacks->MacroExpands(Identifier, M, Identifier.getLocation(),
492  /*Args=*/nullptr);
493  ExpandBuiltinMacro(Identifier);
494  return true;
495  }
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  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  SourceLocation ExpansionEnd = Identifier.getLocation();
505 
506  // If this is a function-like macro, read the arguments.
507  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  InMacroArgs = true;
512  ArgMacro = &Identifier;
513 
514  Args = ReadMacroCallArgumentList(Identifier, MI, ExpansionEnd);
515 
516  // Finished parsing args.
517  InMacroArgs = false;
518  ArgMacro = nullptr;
519 
520  // If there was an error parsing the arguments, bail out.
521  if (!Args) return true;
522 
523  ++NumFnMacroExpanded;
524  } else {
525  ++NumMacroExpanded;
526  }
527 
528  // Notice that this macro has been used.
529  markMacroAsUsed(MI);
530 
531  // Remember where the token is expanded.
532  SourceLocation ExpandLoc = Identifier.getLocation();
533  SourceRange ExpansionRange(ExpandLoc, ExpansionEnd);
534 
535  if (Callbacks) {
536  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  DelayedMacroExpandsCallbacks.push_back(
542  MacroExpandsInfo(Identifier, M, ExpansionRange));
543  } else {
544  Callbacks->MacroExpands(Identifier, M, ExpansionRange, Args);
545  if (!DelayedMacroExpandsCallbacks.empty()) {
546  for (const MacroExpandsInfo &Info : DelayedMacroExpandsCallbacks) {
547  // FIXME: We lose macro args info with delayed callback.
548  Callbacks->MacroExpands(Info.Tok, Info.MD, Info.Range,
549  /*Args=*/nullptr);
550  }
551  DelayedMacroExpandsCallbacks.clear();
552  }
553  }
554  }
555 
556  // If the macro definition is ambiguous, complain.
557  if (M.isAmbiguous()) {
558  Diag(Identifier, diag::warn_pp_ambiguous_macro)
559  << Identifier.getIdentifierInfo();
560  Diag(MI->getDefinitionLoc(), diag::note_pp_ambiguous_macro_chosen)
561  << Identifier.getIdentifierInfo();
562  M.forAllDefinitions([&](const MacroInfo *OtherMI) {
563  if (OtherMI != MI)
564  Diag(OtherMI->getDefinitionLoc(), diag::note_pp_ambiguous_macro_other)
565  << Identifier.getIdentifierInfo();
566  });
567  }
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  if (MI->getNumTokens() == 0) {
574  // No need for arg info.
575  if (Args) Args->destroy(*this);
576 
577  // Propagate whitespace info as if we had pushed, then popped,
578  // a macro context.
580  PropagateLineStartLeadingSpaceInfo(Identifier);
581  ++NumFastMacroExpanded;
582  return false;
583  } else if (MI->getNumTokens() == 1 &&
584  isTrivialSingleTokenExpansion(MI, Identifier.getIdentifierInfo(),
585  *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  if (Args) Args->destroy(*this);
592 
593  // Propagate the isAtStartOfLine/hasLeadingSpace markers of the macro
594  // identifier to the expanded token.
595  bool isAtStartOfLine = Identifier.isAtStartOfLine();
596  bool hasLeadingSpace = Identifier.hasLeadingSpace();
597 
598  // Replace the result token.
600 
601  // Restore the StartOfLine/LeadingSpace markers.
602  Identifier.setFlagValue(Token::StartOfLine , isAtStartOfLine);
603  Identifier.setFlagValue(Token::LeadingSpace, hasLeadingSpace);
604 
605  // Update the tokens location to include both its expansion and physical
606  // locations.
607  SourceLocation Loc =
608  SourceMgr.createExpansionLoc(Identifier.getLocation(), ExpandLoc,
609  ExpansionEnd,Identifier.getLength());
610  Identifier.setLocation(Loc);
611 
612  // If this is a disabled macro or #define X X, we must mark the result as
613  // unexpandable.
614  if (IdentifierInfo *NewII = Identifier.getIdentifierInfo()) {
615  if (MacroInfo *NewMI = getMacroInfo(NewII))
616  if (!NewMI->isEnabled() || NewMI == MI) {
618  // Don't warn for "#define X X" like "#define bool bool" from
619  // stdbool.h.
620  if (NewMI != MI || MI->isFunctionLike())
621  Diag(Identifier, diag::pp_disabled_macro_expansion);
622  }
623  }
624 
625  // Since this is not an identifier token, it can't be macro expanded, so
626  // we're done.
627  ++NumFastMacroExpanded;
628  return true;
629  }
630 
631  // Start expanding the macro.
632  EnterMacro(Identifier, ExpansionEnd, MI, Args);
633  return false;
634 }
635 
636 enum Bracket {
639 };
640 
641 /// CheckMatchedBrackets - Returns true if the braces and parentheses in the
642 /// token vector are properly nested.
643 static bool CheckMatchedBrackets(const SmallVectorImpl<Token> &Tokens) {
644  SmallVector<Bracket, 8> Brackets;
645  for (SmallVectorImpl<Token>::const_iterator I = Tokens.begin(),
646  E = Tokens.end();
647  I != E; ++I) {
648  if (I->is(tok::l_paren)) {
649  Brackets.push_back(Paren);
650  } else if (I->is(tok::r_paren)) {
651  if (Brackets.empty() || Brackets.back() == Brace)
652  return false;
653  Brackets.pop_back();
654  } else if (I->is(tok::l_brace)) {
655  Brackets.push_back(Brace);
656  } else if (I->is(tok::r_brace)) {
657  if (Brackets.empty() || Brackets.back() == Paren)
658  return false;
659  Brackets.pop_back();
660  }
661  }
662  return Brackets.empty();
663 }
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.
673  SmallVectorImpl<Token> &OldTokens,
674  SmallVectorImpl<Token> &NewTokens,
675  unsigned &NumArgs,
676  SmallVectorImpl<SourceRange> &ParenHints,
677  SmallVectorImpl<SourceRange> &InitLists) {
678  if (!CheckMatchedBrackets(OldTokens))
679  return false;
680 
681  // Once it is known that the brackets are matched, only a simple count of the
682  // braces is needed.
683  unsigned Braces = 0;
684 
685  // First token of a new macro argument.
686  SmallVectorImpl<Token>::iterator ArgStartIterator = OldTokens.begin();
687 
688  // First closing brace in a new macro argument. Used to generate
689  // SourceRanges for InitLists.
690  SmallVectorImpl<Token>::iterator ClosingBrace = OldTokens.end();
691  NumArgs = 0;
692  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  bool FoundSeparatorToken = false;
697  for (SmallVectorImpl<Token>::iterator I = OldTokens.begin(),
698  E = OldTokens.end();
699  I != E; ++I) {
700  if (I->is(tok::l_brace)) {
701  ++Braces;
702  } else if (I->is(tok::r_brace)) {
703  --Braces;
704  if (Braces == 0 && ClosingBrace == E && FoundSeparatorToken)
705  ClosingBrace = I;
706  } else if (I->is(tok::eof)) {
707  // EOF token is used to separate macro arguments
708  if (Braces != 0) {
709  // Assume comma separator is actually braced list separator and change
710  // it back to a comma.
711  FoundSeparatorToken = true;
712  I->setKind(tok::comma);
713  I->setLength(1);
714  } else { // Braces == 0
715  // Separator token still separates arguments.
716  ++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  if (FoundSeparatorToken && ArgStartIterator->is(tok::l_brace)) {
721  InitLists.push_back(
722  SourceRange(ArgStartIterator->getLocation(),
723  PP.getLocForEndOfToken(ClosingBrace->getLocation())));
724  ClosingBrace = E;
725  }
726 
727  // Add left paren
728  if (FoundSeparatorToken) {
729  TempToken.startToken();
730  TempToken.setKind(tok::l_paren);
731  TempToken.setLocation(ArgStartIterator->getLocation());
732  TempToken.setLength(0);
733  NewTokens.push_back(TempToken);
734  }
735 
736  // Copy over argument tokens
737  NewTokens.insert(NewTokens.end(), ArgStartIterator, I);
738 
739  // Add right paren and store the paren locations in ParenHints
740  if (FoundSeparatorToken) {
741  SourceLocation Loc = PP.getLocForEndOfToken((I - 1)->getLocation());
742  TempToken.startToken();
743  TempToken.setKind(tok::r_paren);
744  TempToken.setLocation(Loc);
745  TempToken.setLength(0);
746  NewTokens.push_back(TempToken);
747  ParenHints.push_back(SourceRange(ArgStartIterator->getLocation(),
748  Loc));
749  }
750 
751  // Copy separator token
752  NewTokens.push_back(*I);
753 
754  // Reset values
755  ArgStartIterator = I + 1;
756  FoundSeparatorToken = false;
757  }
758  }
759  }
760 
761  return !ParenHints.empty() && InitLists.empty();
762 }
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  SourceLocation &MacroEnd) {
771  // The number of fixed arguments to parse.
772  unsigned NumFixedArgsLeft = MI->getNumParams();
773  bool isVariadic = MI->isVariadic();
774 
775  // Outer loop, while there are more arguments, keep reading them.
776  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  LexUnexpandedToken(Tok);
781  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  SmallVector<Token, 64> ArgTokens;
787  bool ContainsCodeCompletionTok = false;
788  bool FoundElidedComma = false;
789 
790  SourceLocation TooManyArgsLoc;
791 
792  unsigned NumActuals = 0;
793  while (Tok.isNot(tok::r_paren)) {
794  if (ContainsCodeCompletionTok && Tok.isOneOf(tok::eof, tok::eod))
795  break;
796 
797  assert(Tok.isOneOf(tok::l_paren, tok::comma) &&
798  "only expect argument separators here");
799 
800  size_t ArgTokenStart = ArgTokens.size();
801  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  unsigned NumParens = 0;
806 
807  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  LexUnexpandedToken(Tok);
811 
812  if (Tok.isOneOf(tok::eof, tok::eod)) { // "#if f(<eof>" & "#if f(\n"
813  if (!ContainsCodeCompletionTok) {
814  Diag(MacroName, diag::err_unterm_macro_invoc);
815  Diag(MI->getDefinitionLoc(), diag::note_macro_here)
816  << MacroName.getIdentifierInfo();
817  // Do not lose the EOF/EOD. Return it to the client.
818  MacroName = Tok;
819  return nullptr;
820  }
821  // Do not lose the EOF/EOD.
822  auto Toks = std::make_unique<Token[]>(1);
823  Toks[0] = Tok;
824  EnterTokenStream(std::move(Toks), 1, true, /*IsReinject*/ false);
825  break;
826  } else if (Tok.is(tok::r_paren)) {
827  // If we found the ) token, the macro arg list is done.
828  if (NumParens-- == 0) {
829  MacroEnd = Tok.getLocation();
830  if (!ArgTokens.empty() &&
831  ArgTokens.back().commaAfterElided()) {
832  FoundElidedComma = true;
833  }
834  break;
835  }
836  } else if (Tok.is(tok::l_paren)) {
837  ++NumParens;
838  } 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  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.
849  } 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  if (!isVariadic)
854  break;
855  if (NumFixedArgsLeft > 1)
856  break;
857  }
858  } 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  continue;
862  } 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  if (MacroInfo *MI = getMacroInfo(Tok.getIdentifierInfo()))
870  if (!MI->isEnabled())
872  } else if (Tok.is(tok::code_completion)) {
873  ContainsCodeCompletionTok = true;
874  if (CodeComplete)
875  CodeComplete->CodeCompleteMacroArgument(MacroName.getIdentifierInfo(),
876  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  }
881 
882  ArgTokens.push_back(Tok);
883  }
884 
885  // If this was an empty argument list foo(), don't add this as an empty
886  // argument.
887  if (ArgTokens.empty() && Tok.getKind() == tok::r_paren)
888  break;
889 
890  // If this is not a variadic macro, and too many args were specified, emit
891  // an error.
892  if (!isVariadic && NumFixedArgsLeft == 0 && TooManyArgsLoc.isInvalid()) {
893  if (ArgTokens.size() != ArgTokenStart)
894  TooManyArgsLoc = ArgTokens[ArgTokenStart].getLocation();
895  else
896  TooManyArgsLoc = ArgStartLoc;
897  }
898 
899  // Empty arguments are standard in C99 and C++0x, and are supported as an
900  // extension in other modes.
901  if (ArgTokens.size() == ArgTokenStart && !getLangOpts().C99)
903  ? diag::warn_cxx98_compat_empty_fnmacro_arg
904  : diag::ext_empty_fnmacro_arg);
905 
906  // Add a marker EOF token to the end of the token list for this argument.
907  Token EOFTok;
908  EOFTok.startToken();
909  EOFTok.setKind(tok::eof);
910  EOFTok.setLocation(Tok.getLocation());
911  EOFTok.setLength(0);
912  ArgTokens.push_back(EOFTok);
913  ++NumActuals;
914  if (!ContainsCodeCompletionTok && NumFixedArgsLeft != 0)
915  --NumFixedArgsLeft;
916  }
917 
918  // Okay, we either found the r_paren. Check to see if we parsed too few
919  // arguments.
920  unsigned MinArgsExpected = MI->getNumParams();
921 
922  // If this is not a variadic macro, and too many args were specified, emit
923  // an error.
924  if (!isVariadic && NumActuals > MinArgsExpected &&
925  !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  Diag(TooManyArgsLoc, diag::err_too_many_args_in_macro_invoc);
929  Diag(MI->getDefinitionLoc(), diag::note_macro_here)
930  << 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  SmallVector<Token, 4> FixedArgTokens;
938  unsigned FixedNumArgs = 0;
939  SmallVector<SourceRange, 4> ParenHints, InitLists;
940  if (!GenerateNewArgTokens(*this, ArgTokens, FixedArgTokens, FixedNumArgs,
941  ParenHints, InitLists)) {
942  if (!InitLists.empty()) {
943  DiagnosticBuilder DB =
944  Diag(MacroName,
945  diag::note_init_list_at_beginning_of_macro_argument);
946  for (SourceRange Range : InitLists)
947  DB << Range;
948  }
949  return nullptr;
950  }
951  if (FixedNumArgs != MinArgsExpected)
952  return nullptr;
953 
954  DiagnosticBuilder DB = Diag(MacroName, diag::note_suggest_parens_for_macro);
955  for (SourceRange ParenLocation : ParenHints) {
956  DB << FixItHint::CreateInsertion(ParenLocation.getBegin(), "(");
957  DB << FixItHint::CreateInsertion(ParenLocation.getEnd(), ")");
958  }
959  ArgTokens.swap(FixedArgTokens);
960  NumActuals = FixedNumArgs;
961  }
962 
963  // See MacroArgs instance var for description of this.
964  bool isVarargsElided = false;
965 
966  if (ContainsCodeCompletionTok) {
967  // Recover from not-fully-formed macro invocation during code-completion.
968  Token EOFTok;
969  EOFTok.startToken();
970  EOFTok.setKind(tok::eof);
971  EOFTok.setLocation(Tok.getLocation());
972  EOFTok.setLength(0);
973  for (; NumActuals < MinArgsExpected; ++NumActuals)
974  ArgTokens.push_back(EOFTok);
975  }
976 
977  if (NumActuals < MinArgsExpected) {
978  // There are several cases where too few arguments is ok, handle them now.
979  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  isVarargsElided = MI->isVariadic();
986  } else if ((FoundElidedComma || MI->isVariadic()) &&
987  (NumActuals+1 == MinArgsExpected || // A(x, ...) -> A(X)
988  (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  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).
999  ? diag::warn_cxx17_compat_missing_varargs_arg
1000  : diag::ext_missing_varargs_arg);
1001  Diag(MI->getDefinitionLoc(), diag::note_macro_here)
1002  << MacroName.getIdentifierInfo();
1003  }
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  isVarargsElided = true;
1012  } else if (!ContainsCodeCompletionTok) {
1013  // Otherwise, emit the error.
1014  Diag(Tok, diag::err_too_few_args_in_macro_invoc);
1015  Diag(MI->getDefinitionLoc(), diag::note_macro_here)
1016  << MacroName.getIdentifierInfo();
1017  return nullptr;
1018  }
1019 
1020  // Add a marker EOF token to the end of the token list for this argument.
1021  SourceLocation EndLoc = Tok.getLocation();
1022  Tok.startToken();
1023  Tok.setKind(tok::eof);
1024  Tok.setLocation(EndLoc);
1025  Tok.setLength(0);
1026  ArgTokens.push_back(Tok);
1027 
1028  // If we expect two arguments, add both as empty.
1029  if (NumActuals == 0 && MinArgsExpected == 2)
1030  ArgTokens.push_back(Tok);
1031 
1032  } else if (NumActuals > MinArgsExpected && !MI->isVariadic() &&
1033  !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  Diag(MacroName, diag::err_too_many_args_in_macro_invoc);
1037  Diag(MI->getDefinitionLoc(), diag::note_macro_here)
1038  << MacroName.getIdentifierInfo();
1039  return nullptr;
1040  }
1041 
1042  return MacroArgs::create(MI, ArgTokens, isVarargsElided, *this);
1043 }
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  ArrayRef<Token> tokens) {
1052  assert(tokLexer);
1053  if (tokens.empty())
1054  return nullptr;
1055 
1056  size_t newIndex = MacroExpandedTokens.size();
1057  bool cacheNeedsToGrow = tokens.size() >
1058  MacroExpandedTokens.capacity()-MacroExpandedTokens.size();
1059  MacroExpandedTokens.append(tokens.begin(), tokens.end());
1060 
1061  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  for (const auto &Lexer : MacroExpandingLexersStack) {
1065  TokenLexer *prevLexer;
1066  size_t tokIndex;
1067  std::tie(prevLexer, tokIndex) = Lexer;
1068  prevLexer->Tokens = MacroExpandedTokens.data() + tokIndex;
1069  }
1070  }
1071 
1072  MacroExpandingLexersStack.push_back(std::make_pair(tokLexer, newIndex));
1073  return MacroExpandedTokens.data() + newIndex;
1074 }
1075 
1076 void Preprocessor::removeCachedMacroExpandedTokensOfLastLexer() {
1077  assert(!MacroExpandingLexersStack.empty());
1078  size_t tokIndex = MacroExpandingLexersStack.back().second;
1079  assert(tokIndex < MacroExpandedTokens.size());
1080  // Pop the cached macro expanded tokens from the end.
1081  MacroExpandedTokens.resize(tokIndex);
1082  MacroExpandingLexersStack.pop_back();
1083 }
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  Preprocessor &PP) {
1090  time_t TT;
1091  std::tm *TM;
1094  TM = std::gmtime(&TT);
1095  } else {
1096  TT = std::time(nullptr);
1097  TM = std::localtime(&TT);
1098  }
1099 
1100  static const char * const Months[] = {
1101  "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
1102  };
1103 
1104  {
1105  SmallString<32> TmpBuffer;
1106  llvm::raw_svector_ostream TmpStream(TmpBuffer);
1107  if (TM)
1108  TmpStream << llvm::format("\"%s %2d %4d\"", Months[TM->tm_mon],
1109  TM->tm_mday, TM->tm_year + 1900);
1110  else
1111  TmpStream << "??? ?? ????";
1112  Token TmpTok;
1113  TmpTok.startToken();
1114  PP.CreateString(TmpStream.str(), TmpTok);
1115  DATELoc = TmpTok.getLocation();
1116  }
1117 
1118  {
1119  SmallString<32> TmpBuffer;
1120  llvm::raw_svector_ostream TmpStream(TmpBuffer);
1121  if (TM)
1122  TmpStream << llvm::format("\"%02d:%02d:%02d\"", TM->tm_hour, TM->tm_min,
1123  TM->tm_sec);
1124  else
1125  TmpStream << "??:??:??";
1126  Token TmpTok;
1127  TmpTok.startToken();
1128  PP.CreateString(TmpStream.str(), TmpTok);
1129  TIMELoc = TmpTok.getLocation();
1130  }
1131 }
1132 
1133 /// HasFeature - Return true if we recognize and implement the feature
1134 /// specified by the identifier as a standard language feature.
1135 static bool HasFeature(const Preprocessor &PP, StringRef Feature) {
1136  const LangOptions &LangOpts = PP.getLangOpts();
1137 
1138  // Normalize the feature name, __foo__ becomes foo.
1139  if (Feature.startswith("__") && Feature.endswith("__") && Feature.size() >= 4)
1140  Feature = Feature.substr(2, Feature.size() - 4);
1141 
1142 #define FEATURE(Name, Predicate) .Case(#Name, Predicate)
1143  return llvm::StringSwitch<bool>(Feature)
1144 #include "clang/Basic/Features.def"
1145  .Default(false);
1146 #undef FEATURE
1147 }
1148 
1149 /// HasExtension - Return true if we recognize and implement the feature
1150 /// specified by the identifier, either as an extension or a standard language
1151 /// feature.
1152 static bool HasExtension(const Preprocessor &PP, StringRef Extension) {
1153  if (HasFeature(PP, Extension))
1154  return true;
1155 
1156  // If the use of an extension results in an error diagnostic, extensions are
1157  // effectively unavailable, so just return false here.
1160  return false;
1161 
1162  const LangOptions &LangOpts = PP.getLangOpts();
1163 
1164  // Normalize the extension name, __foo__ becomes foo.
1165  if (Extension.startswith("__") && Extension.endswith("__") &&
1166  Extension.size() >= 4)
1167  Extension = Extension.substr(2, Extension.size() - 4);
1168 
1169  // Because we inherit the feature list from HasFeature, this string switch
1170  // must be less restrictive than HasFeature's.
1171 #define EXTENSION(Name, Predicate) .Case(#Name, Predicate)
1172  return llvm::StringSwitch<bool>(Extension)
1173 #include "clang/Basic/Features.def"
1174  .Default(false);
1175 #undef EXTENSION
1176 }
1177 
1178 /// EvaluateHasIncludeCommon - Process a '__has_include("path")'
1179 /// or '__has_include_next("path")' expression.
1180 /// Returns true if successful.
1182  Preprocessor &PP,
1183  ConstSearchDirIterator LookupFrom,
1184  const FileEntry *LookupFromFile) {
1185  // Save the location of the current token. If a '(' is later found, use
1186  // that location. If not, use the end of this location instead.
1187  SourceLocation LParenLoc = Tok.getLocation();
1188 
1189  // These expressions are only allowed within a preprocessor directive.
1190  if (!PP.isParsingIfOrElifDirective()) {
1191  PP.Diag(LParenLoc, diag::err_pp_directive_required) << II;
1192  // Return a valid identifier token.
1193  assert(Tok.is(tok::identifier));
1194  Tok.setIdentifierInfo(II);
1195  return false;
1196  }
1197 
1198  // Get '('. If we don't have a '(', try to form a header-name token.
1199  do {
1200  if (PP.LexHeaderName(Tok))
1201  return false;
1202  } while (Tok.getKind() == tok::comment);
1203 
1204  // Ensure we have a '('.
1205  if (Tok.isNot(tok::l_paren)) {
1206  // No '(', use end of last token.
1207  LParenLoc = PP.getLocForEndOfToken(LParenLoc);
1208  PP.Diag(LParenLoc, diag::err_pp_expected_after) << II << tok::l_paren;
1209  // If the next token looks like a filename or the start of one,
1210  // assume it is and process it as such.
1211  if (Tok.isNot(tok::header_name))
1212  return false;
1213  } else {
1214  // Save '(' location for possible missing ')' message.
1215  LParenLoc = Tok.getLocation();
1216  if (PP.LexHeaderName(Tok))
1217  return false;
1218  }
1219 
1220  if (Tok.isNot(tok::header_name)) {
1221  PP.Diag(Tok.getLocation(), diag::err_pp_expects_filename);
1222  return false;
1223  }
1224 
1225  // Reserve a buffer to get the spelling.
1226  SmallString<128> FilenameBuffer;
1227  bool Invalid = false;
1228  StringRef Filename = PP.getSpelling(Tok, FilenameBuffer, &Invalid);
1229  if (Invalid)
1230  return false;
1231 
1232  SourceLocation FilenameLoc = Tok.getLocation();
1233 
1234  // Get ')'.
1235  PP.LexNonComment(Tok);
1236 
1237  // Ensure we have a trailing ).
1238  if (Tok.isNot(tok::r_paren)) {
1239  PP.Diag(PP.getLocForEndOfToken(FilenameLoc), diag::err_pp_expected_after)
1240  << II << tok::r_paren;
1241  PP.Diag(LParenLoc, diag::note_matching) << tok::l_paren;
1242  return false;
1243  }
1244 
1245  bool isAngled = PP.GetIncludeFilenameSpelling(Tok.getLocation(), Filename);
1246  // If GetIncludeFilenameSpelling set the start ptr to null, there was an
1247  // error.
1248  if (Filename.empty())
1249  return false;
1250 
1251  // Search include directories.
1253  PP.LookupFile(FilenameLoc, Filename, isAngled, LookupFrom, LookupFromFile,
1254  nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);
1255 
1256  if (PPCallbacks *Callbacks = PP.getPPCallbacks()) {
1258  if (File)
1259  FileType =
1260  PP.getHeaderSearchInfo().getFileDirFlavor(&File->getFileEntry());
1261  Callbacks->HasInclude(FilenameLoc, Filename, isAngled, File, FileType);
1262  }
1263 
1264  // Get the result value. A result of true means the file exists.
1265  return File.has_value();
1266 }
1267 
1268 bool Preprocessor::EvaluateHasInclude(Token &Tok, IdentifierInfo *II) {
1269  return EvaluateHasIncludeCommon(Tok, II, *this, nullptr, nullptr);
1270 }
1271 
1272 bool Preprocessor::EvaluateHasIncludeNext(Token &Tok, IdentifierInfo *II) {
1273  ConstSearchDirIterator Lookup = nullptr;
1274  const FileEntry *LookupFromFile;
1275  std::tie(Lookup, LookupFromFile) = getIncludeNextStart(Tok);
1276 
1277  return EvaluateHasIncludeCommon(Tok, II, *this, Lookup, LookupFromFile);
1278 }
1279 
1280 /// Process single-argument builtin feature-like macros that return
1281 /// integer values.
1282 static void EvaluateFeatureLikeBuiltinMacro(llvm::raw_svector_ostream& OS,
1283  Token &Tok, IdentifierInfo *II,
1284  Preprocessor &PP, bool ExpandArgs,
1285  llvm::function_ref<
1286  int(Token &Tok,
1287  bool &HasLexedNextTok)> Op) {
1288  // Parse the initial '('.
1289  PP.LexUnexpandedToken(Tok);
1290  if (Tok.isNot(tok::l_paren)) {
1291  PP.Diag(Tok.getLocation(), diag::err_pp_expected_after) << II
1292  << tok::l_paren;
1293 
1294  // Provide a dummy '0' value on output stream to elide further errors.
1295  if (!Tok.isOneOf(tok::eof, tok::eod)) {
1296  OS << 0;
1297  Tok.setKind(tok::numeric_constant);
1298  }
1299  return;
1300  }
1301 
1302  unsigned ParenDepth = 1;
1303  SourceLocation LParenLoc = Tok.getLocation();
1304  llvm::Optional<int> Result;
1305 
1306  Token ResultTok;
1307  bool SuppressDiagnostic = false;
1308  while (true) {
1309  // Parse next token.
1310  if (ExpandArgs)
1311  PP.Lex(Tok);
1312  else
1313  PP.LexUnexpandedToken(Tok);
1314 
1315 already_lexed:
1316  switch (Tok.getKind()) {
1317  case tok::eof:
1318  case tok::eod:
1319  // Don't provide even a dummy value if the eod or eof marker is
1320  // reached. Simply provide a diagnostic.
1321  PP.Diag(Tok.getLocation(), diag::err_unterm_macro_invoc);
1322  return;
1323 
1324  case tok::comma:
1325  if (!SuppressDiagnostic) {
1326  PP.Diag(Tok.getLocation(), diag::err_too_many_args_in_macro_invoc);
1327  SuppressDiagnostic = true;
1328  }
1329  continue;
1330 
1331  case tok::l_paren:
1332  ++ParenDepth;
1333  if (Result)
1334  break;
1335  if (!SuppressDiagnostic) {
1336  PP.Diag(Tok.getLocation(), diag::err_pp_nested_paren) << II;
1337  SuppressDiagnostic = true;
1338  }
1339  continue;
1340 
1341  case tok::r_paren:
1342  if (--ParenDepth > 0)
1343  continue;
1344 
1345  // The last ')' has been reached; return the value if one found or
1346  // a diagnostic and a dummy value.
1347  if (Result) {
1348  OS << Result.value();
1349  // For strict conformance to __has_cpp_attribute rules, use 'L'
1350  // suffix for dated literals.
1351  if (Result.value() > 1)
1352  OS << 'L';
1353  } else {
1354  OS << 0;
1355  if (!SuppressDiagnostic)
1356  PP.Diag(Tok.getLocation(), diag::err_too_few_args_in_macro_invoc);
1357  }
1358  Tok.setKind(tok::numeric_constant);
1359  return;
1360 
1361  default: {
1362  // Parse the macro argument, if one not found so far.
1363  if (Result)
1364  break;
1365 
1366  bool HasLexedNextToken = false;
1367  Result = Op(Tok, HasLexedNextToken);
1368  ResultTok = Tok;
1369  if (HasLexedNextToken)
1370  goto already_lexed;
1371  continue;
1372  }
1373  }
1374 
1375  // Diagnose missing ')'.
1376  if (!SuppressDiagnostic) {
1377  if (auto Diag = PP.Diag(Tok.getLocation(), diag::err_pp_expected_after)) {
1378  if (IdentifierInfo *LastII = ResultTok.getIdentifierInfo())
1379  Diag << LastII;
1380  else
1381  Diag << ResultTok.getKind();
1382  Diag << tok::r_paren << ResultTok.getLocation();
1383  }
1384  PP.Diag(LParenLoc, diag::note_matching) << tok::l_paren;
1385  SuppressDiagnostic = true;
1386  }
1387  }
1388 }
1389 
1390 /// Helper function to return the IdentifierInfo structure of a Token
1391 /// or generate a diagnostic if none available.
1393  Preprocessor &PP,
1394  signed DiagID) {
1395  IdentifierInfo *II;
1396  if (!Tok.isAnnotation() && (II = Tok.getIdentifierInfo()))
1397  return II;
1398 
1399  PP.Diag(Tok.getLocation(), DiagID);
1400  return nullptr;
1401 }
1402 
1403 /// Implements the __is_target_arch builtin macro.
1404 static bool isTargetArch(const TargetInfo &TI, const IdentifierInfo *II) {
1405  std::string ArchName = II->getName().lower() + "--";
1406  llvm::Triple Arch(ArchName);
1407  const llvm::Triple &TT = TI.getTriple();
1408  if (TT.isThumb()) {
1409  // arm matches thumb or thumbv7. armv7 matches thumbv7.
1410  if ((Arch.getSubArch() == llvm::Triple::NoSubArch ||
1411  Arch.getSubArch() == TT.getSubArch()) &&
1412  ((TT.getArch() == llvm::Triple::thumb &&
1413  Arch.getArch() == llvm::Triple::arm) ||
1414  (TT.getArch() == llvm::Triple::thumbeb &&
1415  Arch.getArch() == llvm::Triple::armeb)))
1416  return true;
1417  }
1418  // Check the parsed arch when it has no sub arch to allow Clang to
1419  // match thumb to thumbv7 but to prohibit matching thumbv6 to thumbv7.
1420  return (Arch.getSubArch() == llvm::Triple::NoSubArch ||
1421  Arch.getSubArch() == TT.getSubArch()) &&
1422  Arch.getArch() == TT.getArch();
1423 }
1424 
1425 /// Implements the __is_target_vendor builtin macro.
1426 static bool isTargetVendor(const TargetInfo &TI, const IdentifierInfo *II) {
1427  StringRef VendorName = TI.getTriple().getVendorName();
1428  if (VendorName.empty())
1429  VendorName = "unknown";
1430  return VendorName.equals_insensitive(II->getName());
1431 }
1432 
1433 /// Implements the __is_target_os builtin macro.
1434 static bool isTargetOS(const TargetInfo &TI, const IdentifierInfo *II) {
1435  std::string OSName =
1436  (llvm::Twine("unknown-unknown-") + II->getName().lower()).str();
1437  llvm::Triple OS(OSName);
1438  if (OS.getOS() == llvm::Triple::Darwin) {
1439  // Darwin matches macos, ios, etc.
1440  return TI.getTriple().isOSDarwin();
1441  }
1442  return TI.getTriple().getOS() == OS.getOS();
1443 }
1444 
1445 /// Implements the __is_target_environment builtin macro.
1446 static bool isTargetEnvironment(const TargetInfo &TI,
1447  const IdentifierInfo *II) {
1448  std::string EnvName = (llvm::Twine("---") + II->getName().lower()).str();
1449  llvm::Triple Env(EnvName);
1450  // The unknown environment is matched only if
1451  // '__is_target_environment(unknown)' is used.
1452  if (Env.getEnvironment() == llvm::Triple::UnknownEnvironment &&
1453  EnvName != "---unknown")
1454  return false;
1455  return TI.getTriple().getEnvironment() == Env.getEnvironment();
1456 }
1457 
1458 /// Implements the __is_target_variant_os builtin macro.
1459 static bool isTargetVariantOS(const TargetInfo &TI, const IdentifierInfo *II) {
1460  if (TI.getTriple().isOSDarwin()) {
1461  const llvm::Triple *VariantTriple = TI.getDarwinTargetVariantTriple();
1462  if (!VariantTriple)
1463  return false;
1464 
1465  std::string OSName =
1466  (llvm::Twine("unknown-unknown-") + II->getName().lower()).str();
1467  llvm::Triple OS(OSName);
1468  if (OS.getOS() == llvm::Triple::Darwin) {
1469  // Darwin matches macos, ios, etc.
1470  return VariantTriple->isOSDarwin();
1471  }
1472  return VariantTriple->getOS() == OS.getOS();
1473  }
1474  return false;
1475 }
1476 
1477 /// Implements the __is_target_variant_environment builtin macro.
1479  const IdentifierInfo *II) {
1480  if (TI.getTriple().isOSDarwin()) {
1481  const llvm::Triple *VariantTriple = TI.getDarwinTargetVariantTriple();
1482  if (!VariantTriple)
1483  return false;
1484  std::string EnvName = (llvm::Twine("---") + II->getName().lower()).str();
1485  llvm::Triple Env(EnvName);
1486  return VariantTriple->getEnvironment() == Env.getEnvironment();
1487  }
1488  return false;
1489 }
1490 
1491 /// ExpandBuiltinMacro - If an identifier token is read that is to be expanded
1492 /// as a builtin macro, handle it and return the next token as 'Tok'.
1493 void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
1494  // Figure out which token this is.
1495  IdentifierInfo *II = Tok.getIdentifierInfo();
1496  assert(II && "Can't be a macro without id info!");
1497 
1498  // If this is an _Pragma or Microsoft __pragma directive, expand it,
1499  // invoke the pragma handler, then lex the token after it.
1500  if (II == Ident_Pragma)
1501  return Handle_Pragma(Tok);
1502  else if (II == Ident__pragma) // in non-MS mode this is null
1503  return HandleMicrosoft__pragma(Tok);
1504 
1505  ++NumBuiltinMacroExpanded;
1506 
1507  SmallString<128> TmpBuffer;
1508  llvm::raw_svector_ostream OS(TmpBuffer);
1509 
1510  // Set up the return result.
1511  Tok.setIdentifierInfo(nullptr);
1513  bool IsAtStartOfLine = Tok.isAtStartOfLine();
1514  bool HasLeadingSpace = Tok.hasLeadingSpace();
1515 
1516  if (II == Ident__LINE__) {
1517  // C99 6.10.8: "__LINE__: The presumed line number (within the current
1518  // source file) of the current source line (an integer constant)". This can
1519  // be affected by #line.
1520  SourceLocation Loc = Tok.getLocation();
1521 
1522  // Advance to the location of the first _, this might not be the first byte
1523  // of the token if it starts with an escaped newline.
1524  Loc = AdvanceToTokenCharacter(Loc, 0);
1525 
1526  // One wrinkle here is that GCC expands __LINE__ to location of the *end* of
1527  // a macro expansion. This doesn't matter for object-like macros, but
1528  // can matter for a function-like macro that expands to contain __LINE__.
1529  // Skip down through expansion points until we find a file loc for the
1530  // end of the expansion history.
1531  Loc = SourceMgr.getExpansionRange(Loc).getEnd();
1532  PresumedLoc PLoc = SourceMgr.getPresumedLoc(Loc);
1533 
1534  // __LINE__ expands to a simple numeric value.
1535  OS << (PLoc.isValid()? PLoc.getLine() : 1);
1536  Tok.setKind(tok::numeric_constant);
1537  } else if (II == Ident__FILE__ || II == Ident__BASE_FILE__ ||
1538  II == Ident__FILE_NAME__) {
1539  // C99 6.10.8: "__FILE__: The presumed name of the current source file (a
1540  // character string literal)". This can be affected by #line.
1541  PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
1542 
1543  // __BASE_FILE__ is a GNU extension that returns the top of the presumed
1544  // #include stack instead of the current file.
1545  if (II == Ident__BASE_FILE__ && PLoc.isValid()) {
1546  SourceLocation NextLoc = PLoc.getIncludeLoc();
1547  while (NextLoc.isValid()) {
1548  PLoc = SourceMgr.getPresumedLoc(NextLoc);
1549  if (PLoc.isInvalid())
1550  break;
1551 
1552  NextLoc = PLoc.getIncludeLoc();
1553  }
1554  }
1555 
1556  // Escape this filename. Turn '\' -> '\\' '"' -> '\"'
1557  SmallString<256> FN;
1558  if (PLoc.isValid()) {
1559  // __FILE_NAME__ is a Clang-specific extension that expands to the
1560  // the last part of __FILE__.
1561  if (II == Ident__FILE_NAME__) {
1562  // Try to get the last path component, failing that return the original
1563  // presumed location.
1564  StringRef PLFileName = llvm::sys::path::filename(PLoc.getFilename());
1565  if (PLFileName != "")
1566  FN += PLFileName;
1567  else
1568  FN += PLoc.getFilename();
1569  } else {
1570  FN += PLoc.getFilename();
1571  }
1573  Lexer::Stringify(FN);
1574  OS << '"' << FN << '"';
1575  }
1576  Tok.setKind(tok::string_literal);
1577  } else if (II == Ident__DATE__) {
1578  Diag(Tok.getLocation(), diag::warn_pp_date_time);
1579  if (!DATELoc.isValid())
1580  ComputeDATE_TIME(DATELoc, TIMELoc, *this);
1581  Tok.setKind(tok::string_literal);
1582  Tok.setLength(strlen("\"Mmm dd yyyy\""));
1583  Tok.setLocation(SourceMgr.createExpansionLoc(DATELoc, Tok.getLocation(),
1584  Tok.getLocation(),
1585  Tok.getLength()));
1586  return;
1587  } else if (II == Ident__TIME__) {
1588  Diag(Tok.getLocation(), diag::warn_pp_date_time);
1589  if (!TIMELoc.isValid())
1590  ComputeDATE_TIME(DATELoc, TIMELoc, *this);
1591  Tok.setKind(tok::string_literal);
1592  Tok.setLength(strlen("\"hh:mm:ss\""));
1593  Tok.setLocation(SourceMgr.createExpansionLoc(TIMELoc, Tok.getLocation(),
1594  Tok.getLocation(),
1595  Tok.getLength()));
1596  return;
1597  } else if (II == Ident__INCLUDE_LEVEL__) {
1598  // Compute the presumed include depth of this token. This can be affected
1599  // by GNU line markers.
1600  unsigned Depth = 0;
1601 
1602  PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
1603  if (PLoc.isValid()) {
1604  PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc());
1605  for (; PLoc.isValid(); ++Depth)
1606  PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc());
1607  }
1608 
1609  // __INCLUDE_LEVEL__ expands to a simple numeric value.
1610  OS << Depth;
1611  Tok.setKind(tok::numeric_constant);
1612  } else if (II == Ident__TIMESTAMP__) {
1613  Diag(Tok.getLocation(), diag::warn_pp_date_time);
1614  // MSVC, ICC, GCC, VisualAge C++ extension. The generated string should be
1615  // of the form "Ddd Mmm dd hh::mm::ss yyyy", which is returned by asctime.
1616  const char *Result;
1617  if (getPreprocessorOpts().SourceDateEpoch) {
1618  time_t TT = *getPreprocessorOpts().SourceDateEpoch;
1619  std::tm *TM = std::gmtime(&TT);
1620  Result = asctime(TM);
1621  } else {
1622  // Get the file that we are lexing out of. If we're currently lexing from
1623  // a macro, dig into the include stack.
1624  const FileEntry *CurFile = nullptr;
1625  if (PreprocessorLexer *TheLexer = getCurrentFileLexer())
1626  CurFile = SourceMgr.getFileEntryForID(TheLexer->getFileID());
1627  if (CurFile) {
1628  time_t TT = CurFile->getModificationTime();
1629  struct tm *TM = localtime(&TT);
1630  Result = asctime(TM);
1631  } else {
1632  Result = "??? ??? ?? ??:??:?? ????\n";
1633  }
1634  }
1635  // Surround the string with " and strip the trailing newline.
1636  OS << '"' << StringRef(Result).drop_back() << '"';
1637  Tok.setKind(tok::string_literal);
1638  } else if (II == Ident__FLT_EVAL_METHOD__) {
1639  // __FLT_EVAL_METHOD__ is set to the default value.
1640  if (getTUFPEvalMethod() ==
1641  LangOptions::FPEvalMethodKind::FEM_Indeterminable) {
1642  // This is possible if `AllowFPReassoc` or `AllowReciprocal` is enabled.
1643  // These modes can be triggered via the command line option `-ffast-math`
1644  // or via a `pragam float_control`.
1645  // __FLT_EVAL_METHOD__ expands to -1.
1646  // The `minus` operator is the next token we read from the stream.
1647  auto Toks = std::make_unique<Token[]>(1);
1648  OS << "-";
1649  Tok.setKind(tok::minus);
1650  // Push the token `1` to the stream.
1651  Token NumberToken;
1652  NumberToken.startToken();
1653  NumberToken.setKind(tok::numeric_constant);
1654  NumberToken.setLiteralData("1");
1655  NumberToken.setLength(1);
1656  Toks[0] = NumberToken;
1657  EnterTokenStream(std::move(Toks), 1, /*DisableMacroExpansion*/ false,
1658  /*IsReinject*/ false);
1659  } else {
1660  OS << getTUFPEvalMethod();
1661  // __FLT_EVAL_METHOD__ expands to a simple numeric value.
1662  Tok.setKind(tok::numeric_constant);
1663  if (getLastFPEvalPragmaLocation().isValid()) {
1664  // The program is ill-formed. The value of __FLT_EVAL_METHOD__ is
1665  // altered by the pragma.
1666  Diag(Tok, diag::err_illegal_use_of_flt_eval_macro);
1667  Diag(getLastFPEvalPragmaLocation(), diag::note_pragma_entered_here);
1668  }
1669  }
1670  } else if (II == Ident__COUNTER__) {
1671  // __COUNTER__ expands to a simple numeric value.
1672  OS << CounterValue++;
1673  Tok.setKind(tok::numeric_constant);
1674  } else if (II == Ident__has_feature) {
1675  EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, false,
1676  [this](Token &Tok, bool &HasLexedNextToken) -> int {
1678  diag::err_feature_check_malformed);
1679  return II && HasFeature(*this, II->getName());
1680  });
1681  } else if (II == Ident__has_extension) {
1682  EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, false,
1683  [this](Token &Tok, bool &HasLexedNextToken) -> int {
1685  diag::err_feature_check_malformed);
1686  return II && HasExtension(*this, II->getName());
1687  });
1688  } else if (II == Ident__has_builtin) {
1689  EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, false,
1690  [this](Token &Tok, bool &HasLexedNextToken) -> int {
1692  diag::err_feature_check_malformed);
1693  if (!II)
1694  return false;
1695  else if (II->getBuiltinID() != 0) {
1696  switch (II->getBuiltinID()) {
1697  case Builtin::BI__builtin_operator_new:
1698  case Builtin::BI__builtin_operator_delete:
1699  // denotes date of behavior change to support calling arbitrary
1700  // usual allocation and deallocation functions. Required by libc++
1701  return 201802;
1702  default:
1704  getBuiltinInfo().getRequiredFeatures(II->getBuiltinID()),
1705  getTargetInfo().getTargetOpts().FeatureMap);
1706  }
1707  return true;
1708  } else if (II->getTokenID() != tok::identifier ||
1710  // Treat all keywords that introduce a custom syntax of the form
1711  //
1712  // '__some_keyword' '(' [...] ')'
1713  //
1714  // as being "builtin functions", even if the syntax isn't a valid
1715  // function call (for example, because the builtin takes a type
1716  // argument).
1717  if (II->getName().startswith("__builtin_") ||
1718  II->getName().startswith("__is_") ||
1719  II->getName().startswith("__has_"))
1720  return true;
1721  return llvm::StringSwitch<bool>(II->getName())
1722  .Case("__array_rank", true)
1723  .Case("__array_extent", true)
1724  .Case("__reference_binds_to_temporary", true)
1725 #define TRANSFORM_TYPE_TRAIT_DEF(_, Trait) .Case("__" #Trait, true)
1726 #include "clang/Basic/TransformTypeTraits.def"
1727  .Default(false);
1728  } else {
1729  return llvm::StringSwitch<bool>(II->getName())
1730  // Report builtin templates as being builtins.
1731  .Case("__make_integer_seq", getLangOpts().CPlusPlus)
1732  .Case("__type_pack_element", getLangOpts().CPlusPlus)
1733  // Likewise for some builtin preprocessor macros.
1734  // FIXME: This is inconsistent; we usually suggest detecting
1735  // builtin macros via #ifdef. Don't add more cases here.
1736  .Case("__is_target_arch", true)
1737  .Case("__is_target_vendor", true)
1738  .Case("__is_target_os", true)
1739  .Case("__is_target_environment", true)
1740  .Case("__is_target_variant_os", true)
1741  .Case("__is_target_variant_environment", true)
1742  .Default(false);
1743  }
1744  });
1745  } else if (II == Ident__has_constexpr_builtin) {
1747  OS, Tok, II, *this, false,
1748  [this](Token &Tok, bool &HasLexedNextToken) -> int {
1750  Tok, *this, diag::err_feature_check_malformed);
1751  if (!II)
1752  return false;
1753  unsigned BuiltinOp = II->getBuiltinID();
1754  return BuiltinOp != 0 &&
1755  this->getBuiltinInfo().isConstantEvaluated(BuiltinOp);
1756  });
1757  } else if (II == Ident__is_identifier) {
1758  EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, false,
1759  [](Token &Tok, bool &HasLexedNextToken) -> int {
1760  return Tok.is(tok::identifier);
1761  });
1762  } else if (II == Ident__has_attribute) {
1763  EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, true,
1764  [this](Token &Tok, bool &HasLexedNextToken) -> int {
1766  diag::err_feature_check_malformed);
1767  return II ? hasAttribute(AttributeCommonInfo::Syntax::AS_GNU, nullptr,
1768  II, getTargetInfo(), getLangOpts())
1769  : 0;
1770  });
1771  } else if (II == Ident__has_declspec) {
1772  EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, true,
1773  [this](Token &Tok, bool &HasLexedNextToken) -> int {
1775  diag::err_feature_check_malformed);
1776  if (II) {
1777  const LangOptions &LangOpts = getLangOpts();
1778  return LangOpts.DeclSpecKeyword &&
1779  hasAttribute(AttributeCommonInfo::Syntax::AS_Declspec, nullptr,
1780  II, getTargetInfo(), LangOpts);
1781  }
1782 
1783  return false;
1784  });
1785  } else if (II == Ident__has_cpp_attribute ||
1786  II == Ident__has_c_attribute) {
1787  bool IsCXX = II == Ident__has_cpp_attribute;
1788  EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, true,
1789  [&](Token &Tok, bool &HasLexedNextToken) -> int {
1790  IdentifierInfo *ScopeII = nullptr;
1792  Tok, *this, diag::err_feature_check_malformed);
1793  if (!II)
1794  return false;
1795 
1796  // It is possible to receive a scope token. Read the "::", if it is
1797  // available, and the subsequent identifier.
1798  LexUnexpandedToken(Tok);
1799  if (Tok.isNot(tok::coloncolon))
1800  HasLexedNextToken = true;
1801  else {
1802  ScopeII = II;
1803  // Lex an expanded token for the attribute name.
1804  Lex(Tok);
1805  II = ExpectFeatureIdentifierInfo(Tok, *this,
1806  diag::err_feature_check_malformed);
1807  }
1808 
1810  IsCXX ? AttributeCommonInfo::Syntax::AS_CXX11
1811  : AttributeCommonInfo::Syntax::AS_C2x;
1812  return II ? hasAttribute(Syntax, ScopeII, II, getTargetInfo(),
1813  getLangOpts())
1814  : 0;
1815  });
1816  } else if (II == Ident__has_include ||
1817  II == Ident__has_include_next) {
1818  // The argument to these two builtins should be a parenthesized
1819  // file name string literal using angle brackets (<>) or
1820  // double-quotes ("").
1821  bool Value;
1822  if (II == Ident__has_include)
1823  Value = EvaluateHasInclude(Tok, II);
1824  else
1825  Value = EvaluateHasIncludeNext(Tok, II);
1826 
1827  if (Tok.isNot(tok::r_paren))
1828  return;
1829  OS << (int)Value;
1830  Tok.setKind(tok::numeric_constant);
1831  } else if (II == Ident__has_warning) {
1832  // The argument should be a parenthesized string literal.
1833  EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, false,
1834  [this](Token &Tok, bool &HasLexedNextToken) -> int {
1835  std::string WarningName;
1836  SourceLocation StrStartLoc = Tok.getLocation();
1837 
1838  HasLexedNextToken = Tok.is(tok::string_literal);
1839  if (!FinishLexStringLiteral(Tok, WarningName, "'__has_warning'",
1840  /*AllowMacroExpansion=*/false))
1841  return false;
1842 
1843  // FIXME: Should we accept "-R..." flags here, or should that be
1844  // handled by a separate __has_remark?
1845  if (WarningName.size() < 3 || WarningName[0] != '-' ||
1846  WarningName[1] != 'W') {
1847  Diag(StrStartLoc, diag::warn_has_warning_invalid_option);
1848  return false;
1849  }
1850 
1851  // Finally, check if the warning flags maps to a diagnostic group.
1852  // We construct a SmallVector here to talk to getDiagnosticIDs().
1853  // Although we don't use the result, this isn't a hot path, and not
1854  // worth special casing.
1856  return !getDiagnostics().getDiagnosticIDs()->
1858  WarningName.substr(2), Diags);
1859  });
1860  } else if (II == Ident__building_module) {
1861  // The argument to this builtin should be an identifier. The
1862  // builtin evaluates to 1 when that identifier names the module we are
1863  // currently building.
1864  EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, false,
1865  [this](Token &Tok, bool &HasLexedNextToken) -> int {
1867  diag::err_expected_id_building_module);
1868  return getLangOpts().isCompilingModule() && II &&
1869  (II->getName() == getLangOpts().CurrentModule);
1870  });
1871  } else if (II == Ident__MODULE__) {
1872  // The current module as an identifier.
1874  IdentifierInfo *ModuleII = getIdentifierInfo(getLangOpts().CurrentModule);
1875  Tok.setIdentifierInfo(ModuleII);
1876  Tok.setKind(ModuleII->getTokenID());
1877  } else if (II == Ident__identifier) {
1878  SourceLocation Loc = Tok.getLocation();
1879 
1880  // We're expecting '__identifier' '(' identifier ')'. Try to recover
1881  // if the parens are missing.
1882  LexNonComment(Tok);
1883  if (Tok.isNot(tok::l_paren)) {
1884  // No '(', use end of last token.
1885  Diag(getLocForEndOfToken(Loc), diag::err_pp_expected_after)
1886  << II << tok::l_paren;
1887  // If the next token isn't valid as our argument, we can't recover.
1888  if (!Tok.isAnnotation() && Tok.getIdentifierInfo())
1889  Tok.setKind(tok::identifier);
1890  return;
1891  }
1892 
1893  SourceLocation LParenLoc = Tok.getLocation();
1894  LexNonComment(Tok);
1895 
1896  if (!Tok.isAnnotation() && Tok.getIdentifierInfo())
1897  Tok.setKind(tok::identifier);
1898  else if (Tok.is(tok::string_literal) && !Tok.hasUDSuffix()) {
1899  StringLiteralParser Literal(Tok, *this);
1900  if (Literal.hadError)
1901  return;
1902 
1903  Tok.setIdentifierInfo(getIdentifierInfo(Literal.GetString()));
1904  Tok.setKind(tok::identifier);
1905  } else {
1906  Diag(Tok.getLocation(), diag::err_pp_identifier_arg_not_identifier)
1907  << Tok.getKind();
1908  // Don't walk past anything that's not a real token.
1909  if (Tok.isOneOf(tok::eof, tok::eod) || Tok.isAnnotation())
1910  return;
1911  }
1912 
1913  // Discard the ')', preserving 'Tok' as our result.
1914  Token RParen;
1915  LexNonComment(RParen);
1916  if (RParen.isNot(tok::r_paren)) {
1917  Diag(getLocForEndOfToken(Tok.getLocation()), diag::err_pp_expected_after)
1918  << Tok.getKind() << tok::r_paren;
1919  Diag(LParenLoc, diag::note_matching) << tok::l_paren;
1920  }
1921  return;
1922  } else if (II == Ident__is_target_arch) {
1924  OS, Tok, II, *this, false,
1925  [this](Token &Tok, bool &HasLexedNextToken) -> int {
1927  Tok, *this, diag::err_feature_check_malformed);
1928  return II && isTargetArch(getTargetInfo(), II);
1929  });
1930  } else if (II == Ident__is_target_vendor) {
1932  OS, Tok, II, *this, false,
1933  [this](Token &Tok, bool &HasLexedNextToken) -> int {
1935  Tok, *this, diag::err_feature_check_malformed);
1936  return II && isTargetVendor(getTargetInfo(), II);
1937  });
1938  } else if (II == Ident__is_target_os) {
1940  OS, Tok, II, *this, false,
1941  [this](Token &Tok, bool &HasLexedNextToken) -> int {
1943  Tok, *this, diag::err_feature_check_malformed);
1944  return II && isTargetOS(getTargetInfo(), II);
1945  });
1946  } else if (II == Ident__is_target_environment) {
1948  OS, Tok, II, *this, false,
1949  [this](Token &Tok, bool &HasLexedNextToken) -> int {
1951  Tok, *this, diag::err_feature_check_malformed);
1952  return II && isTargetEnvironment(getTargetInfo(), II);
1953  });
1954  } else if (II == Ident__is_target_variant_os) {
1956  OS, Tok, II, *this, false,
1957  [this](Token &Tok, bool &HasLexedNextToken) -> int {
1959  Tok, *this, diag::err_feature_check_malformed);
1960  return II && isTargetVariantOS(getTargetInfo(), II);
1961  });
1962  } else if (II == Ident__is_target_variant_environment) {
1964  OS, Tok, II, *this, false,
1965  [this](Token &Tok, bool &HasLexedNextToken) -> int {
1967  Tok, *this, diag::err_feature_check_malformed);
1968  return II && isTargetVariantEnvironment(getTargetInfo(), II);
1969  });
1970  } else {
1971  llvm_unreachable("Unknown identifier!");
1972  }
1973  CreateString(OS.str(), Tok, Tok.getLocation(), Tok.getLocation());
1974  Tok.setFlagValue(Token::StartOfLine, IsAtStartOfLine);
1975  Tok.setFlagValue(Token::LeadingSpace, HasLeadingSpace);
1976 }
1977 
1979  // If the 'used' status changed, and the macro requires 'unused' warning,
1980  // remove its SourceLocation from the warn-for-unused-macro locations.
1981  if (MI->isWarnIfUnused() && !MI->isUsed())
1982  WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());
1983  MI->setIsUsed(true);
1984 }
1985 
1987  const LangOptions &LangOpts,
1988  const TargetInfo &TI) {
1989  LangOpts.remapPathPrefix(Path);
1990  if (LangOpts.UseTargetPathSeparator) {
1991  if (TI.getTriple().isOSWindows())
1992  llvm::sys::path::remove_dots(Path, false,
1993  llvm::sys::path::Style::windows_backslash);
1994  else
1995  llvm::sys::path::remove_dots(Path, false, llvm::sys::path::Style::posix);
1996  }
1997 }
Brace
@ Brace
Definition: PPMacroExpansion.cpp:637
clang::Preprocessor::isParsingIfOrElifDirective
bool isParsingIfOrElifDirective() const
True if we are currently preprocessing a if or #elif directive.
Definition: Preprocessor.h:1100
clang::Token::startToken
void startToken()
Reset all flags to cleared.
Definition: Token.h:171
clang::MacroInfo::getNumTokens
unsigned getNumTokens() const
Return the number of tokens that this macro expands to.
Definition: MacroInfo.h:235
PreprocessorLexer.h
Builtins.h
clang::LangOptions::CurrentModule
std::string CurrentModule
The name of the current module, of which the main source file is a part.
Definition: LangOptions.h:434
clang::MacroInfo::getReplacementToken
const Token & getReplacementToken(unsigned Tok) const
Definition: MacroInfo.h:237
clang::Preprocessor::CreateString
void CreateString(StringRef Str, Token &Tok, SourceLocation ExpansionLocStart=SourceLocation(), SourceLocation ExpansionLocEnd=SourceLocation())
Plop the specified string into a scratch buffer and set the specified token's location and length to ...
Definition: Preprocessor.cpp:488
clang::Token::IgnoredComma
@ IgnoredComma
Definition: Token.h:83
clang::Preprocessor::AdvanceToTokenCharacter
SourceLocation AdvanceToTokenCharacter(SourceLocation TokStart, unsigned Char) const
Given a location that specifies the start of a token, return a new location that specifies a characte...
Definition: Preprocessor.h:2056
clang::Token::hasLeadingSpace
bool hasLeadingSpace() const
Return true if this token has whitespace before it.
Definition: Token.h:274
GenerateNewArgTokens
static bool GenerateNewArgTokens(Preprocessor &PP, SmallVectorImpl< Token > &OldTokens, SmallVectorImpl< Token > &NewTokens, unsigned &NumArgs, SmallVectorImpl< SourceRange > &ParenHints, SmallVectorImpl< SourceRange > &InitLists)
GenerateNewArgTokens - Returns true if OldTokens can be converted to a new vector of tokens in NewTok...
Definition: PPMacroExpansion.cpp:672
clang::DiagnosticBuilder
A little helper class used to produce diagnostics.
Definition: Diagnostic.h:1266
clang::diag::Severity::Error
@ Error
Present this diagnostic as an error.
ObjCRuntime.h
CheckMatchedBrackets
static bool CheckMatchedBrackets(const SmallVectorImpl< Token > &Tokens)
CheckMatchedBrackets - Returns true if the braces and parentheses in the token vector are properly ne...
Definition: PPMacroExpansion.cpp:643
clang::Preprocessor::FinishLexStringLiteral
bool FinishLexStringLiteral(Token &Result, std::string &String, const char *DiagnosticTag, bool AllowMacroExpansion)
Complete the lexing of a string literal where the first token has already been lexed (see LexStringLi...
Definition: Preprocessor.cpp:1333
clang::IdentifierInfo::setChangedSinceDeserialization
void setChangedSinceDeserialization()
Note that this identifier has changed since it was loaded from an AST file.
Definition: IdentifierTable.h:397
clang::DeclaratorContext::File
@ File
AttributeCommonInfo.h
clang::SourceRange
A trivial tuple used to represent a source range.
Definition: SourceLocation.h:210
string
string(SUBSTRING ${CMAKE_CURRENT_BINARY_DIR} 0 ${PATH_LIB_START} PATH_HEAD) string(SUBSTRING $
Definition: CMakeLists.txt:22
clang::PresumedLoc::getLine
unsigned getLine() const
Return the presumed line number of this location.
Definition: SourceLocation.h:337
clang::Preprocessor::getModuleMacro
ModuleMacro * getModuleMacro(Module *Mod, const IdentifierInfo *II)
Definition: PPMacroExpansion.cpp:172
clang::Preprocessor::addModuleMacro
ModuleMacro * addModuleMacro(Module *Mod, IdentifierInfo *II, MacroInfo *Macro, ArrayRef< ModuleMacro * > Overrides, bool &IsNew)
Register an exported macro for a module and identifier.
Definition: PPMacroExpansion.cpp:133
clang::Preprocessor::getPPCallbacks
PPCallbacks * getPPCallbacks() const
Definition: Preprocessor.h:1162
clang::FixItHint::CreateInsertion
static FixItHint CreateInsertion(SourceLocation InsertionLoc, StringRef Code, bool BeforePreviousInsertions=false)
Create a code modification hint that inserts the given code string at a specific location.
Definition: Diagnostic.h:97
clang::Preprocessor::Lex
void Lex(Token &Result)
Lex the next token for this preprocessor.
Definition: Preprocessor.cpp:881
clang::Preprocessor::getLocalMacroDirectiveHistory
MacroDirective * getLocalMacroDirectiveHistory(const IdentifierInfo *II) const
Given an identifier, return the latest non-imported macro directive for that identifier.
Definition: PPMacroExpansion.cpp:64
Diag
static DiagnosticBuilder Diag(DiagnosticsEngine *Diags, const LangOptions &Features, FullSourceLoc TokLoc, const char *TokBegin, const char *TokRangeBegin, const char *TokRangeEnd, unsigned DiagID)
Produce a diagnostic highlighting some portion of a literal.
Definition: LiteralSupport.cpp:79
clang::MacroInfo::isObjectLike
bool isObjectLike() const
Definition: MacroInfo.h:202
clang::HeaderSearch::getFileDirFlavor
SrcMgr::CharacteristicKind getFileDirFlavor(const FileEntry *File)
Return whether the specified file is a normal header, a system header, or a C++ friendly system heade...
Definition: HeaderSearch.h:520
clang::Preprocessor::emitMacroExpansionWarnings
void emitMacroExpansionWarnings(const Token &Identifier) const
Definition: Preprocessor.h:2676
clang::MacroDefinition
A description of the current definition of a macro.
Definition: MacroInfo.h:587
clang::Preprocessor::LexHeaderName
bool LexHeaderName(Token &Result, bool AllowMacroExpansion=true)
Lex a token, forming a header-name token if possible.
Definition: Preprocessor.cpp:997
llvm::SmallVector
Definition: LLVM.h:38
clang::SourceLocation
Encodes a location in the source.
Definition: SourceLocation.h:86
clang::LangOptions::isCompilingModule
bool isCompilingModule() const
Are we compiling a module?
Definition: LangOptions.h:511
clang::MacroInfo::isVariadic
bool isVariadic() const
Definition: MacroInfo.h:209
clang::PreprocessorOptions::SourceDateEpoch
Optional< uint64_t > SourceDateEpoch
If set, the UNIX timestamp specified by SOURCE_DATE_EPOCH.
Definition: PreprocessorOptions.h:224
clang::PPCallbacks
This interface provides a way to observe the actions of the preprocessor as it does its thing.
Definition: PPCallbacks.h:35
ComputeDATE_TIME
static void ComputeDATE_TIME(SourceLocation &DATELoc, SourceLocation &TIMELoc, Preprocessor &PP)
ComputeDATE_TIME - Compute the current time, enter it into the specified scratch buffer,...
Definition: PPMacroExpansion.cpp:1088
TargetInfo.h
clang::PreprocessorLexer
Definition: PreprocessorLexer.h:30
clang::IdentifierInfo::setHasMacroDefinition
void setHasMacroDefinition(bool Val)
Definition: IdentifierTable.h:205
clang::MacroInfo::dump
void dump() const
Definition: MacroInfo.cpp:152
clang::Token::getIdentifierInfo
IdentifierInfo * getIdentifierInfo() const
Definition: Token.h:181
clang::MacroInfo::isIdenticalTo
bool isIdenticalTo(const MacroInfo &Other, Preprocessor &PP, bool Syntactically) const
Return true if the specified macro definition is equal to this macro in spelling, arguments,...
Definition: MacroInfo.cpp:94
clang::MacroArgs
MacroArgs - An instance of this class captures information about the formal arguments specified to a ...
Definition: MacroArgs.h:29
Filename
StringRef Filename
Definition: Format.cpp:2715
clang::PreprocessorLexer::MIOpt
MultipleIncludeOpt MIOpt
A state machine that detects the #ifndef-wrapping a file idiom for the multiple-include optimization.
Definition: PreprocessorLexer.h:72
clang::TargetInfo
Exposes information about the current target.
Definition: TargetInfo.h:205
int
__device__ int
Definition: __clang_hip_libdevice_declares.h:63
clang::Token::isAtStartOfLine
bool isAtStartOfLine() const
isAtStartOfLine - Return true if this token is at the start of a line.
Definition: Token.h:270
clang::IdentifierInfo::isOutOfDate
bool isOutOfDate() const
Determine whether the information for this identifier is out of date with respect to the external sou...
Definition: IdentifierTable.h:415
clang::C99
@ C99
Definition: LangStandard.h:49
clang::SourceManager::getExpansionRange
CharSourceRange getExpansionRange(SourceLocation Loc) const
Given a SourceLocation object, return the range of tokens covered by the expansion in the ultimate fi...
Definition: SourceManager.cpp:1044
llvm::Optional< FileEntryRef >
clang::PresumedLoc::getIncludeLoc
SourceLocation getIncludeLoc() const
Return the presumed include location of this location.
Definition: SourceLocation.h:353
clang::MacroInfo
Encapsulates the data about a macro definition (e.g.
Definition: MacroInfo.h:39
clang::Token::setIdentifierInfo
void setIdentifierInfo(IdentifierInfo *II)
Definition: Token.h:190
clang::SourceManager::getPresumedLoc
PresumedLoc getPresumedLoc(SourceLocation Loc, bool UseLineDirectives=true) const
Returns the "presumed" location of a SourceLocation specifies.
Definition: SourceManager.cpp:1523
clang::SrcMgr::CharacteristicKind
CharacteristicKind
Indicates whether a file or directory holds normal user code, system code, or system code which is im...
Definition: SourceManager.h:79
clang::IdentifierInfo::isFromAST
bool isFromAST() const
Return true if the identifier in its current state was loaded from an AST file.
Definition: IdentifierTable.h:385
clang::Preprocessor::getCurrentFileLexer
PreprocessorLexer * getCurrentFileLexer() const
Return the current file lexer being lexed from.
Definition: PPLexerChange.cpp:49
clang::MacroDefinition::isAmbiguous
bool isAmbiguous() const
true if the definition is ambiguous, false otherwise.
Definition: MacroInfo.h:612
Identifier
StringRef Identifier
Definition: Format.cpp:2723
isTargetVariantEnvironment
static bool isTargetVariantEnvironment(const TargetInfo &TI, const IdentifierInfo *II)
Implements the __is_target_variant_environment builtin macro.
Definition: PPMacroExpansion.cpp:1478
clang::IdentifierInfo::getTokenID
tok::TokenKind getTokenID() const
If this is a source-language token (e.g.
Definition: IdentifierTable.h:262
DirectoryLookup.h
clang::Token
Token - This structure provides full information about a lexed token.
Definition: Token.h:35
LiteralSupport.h
clang::Builtin::Context::isConstantEvaluated
bool isConstantEvaluated(unsigned ID) const
Return true if this function can be constant evaluated by Clang frontend.
Definition: Builtins.h:267
clang::AttributeCommonInfo::Syntax
Syntax
The style used to specify an attribute.
Definition: AttributeCommonInfo.h:25
clang::MultipleIncludeOpt::ExpandedMacro
void ExpandedMacro()
ExpandedMacro - When a macro is expanded with this lexer as the current buffer, this method is called...
Definition: MultipleIncludeOpt.h:113
isTargetVariantOS
static bool isTargetVariantOS(const TargetInfo &TI, const IdentifierInfo *II)
Implements the __is_target_variant_os builtin macro.
Definition: PPMacroExpansion.cpp:1459
clang::MacroInfo::hasCommaPasting
bool hasCommaPasting() const
Definition: MacroInfo.h:219
Paren
@ Paren
Definition: PPMacroExpansion.cpp:638
clang::Lexer::Stringify
static std::string Stringify(StringRef Str, bool Charify=false)
Stringify - Convert the specified string into a C string by i) escaping '\' and " characters and ii) ...
Definition: Lexer.cpp:262
clang::Preprocessor::processPathForFileMacro
static void processPathForFileMacro(SmallVectorImpl< char > &Path, const LangOptions &LangOpts, const TargetInfo &TI)
Definition: PPMacroExpansion.cpp:1986
clang::Preprocessor::getLangOpts
const LangOptions & getLangOpts() const
Definition: Preprocessor.h:1065
EvaluateFeatureLikeBuiltinMacro
static void EvaluateFeatureLikeBuiltinMacro(llvm::raw_svector_ostream &OS, Token &Tok, IdentifierInfo *II, Preprocessor &PP, bool ExpandArgs, llvm::function_ref< int(Token &Tok, bool &HasLexedNextTok)> Op)
Process single-argument builtin feature-like macros that return integer values.
Definition: PPMacroExpansion.cpp:1282
Preprocessor.h
clang::Lexer
Lexer - This provides a simple interface that turns a text buffer into a stream of tokens.
Definition: Lexer.h:78
isTargetVendor
static bool isTargetVendor(const TargetInfo &TI, const IdentifierInfo *II)
Implements the __is_target_vendor builtin macro.
Definition: PPMacroExpansion.cpp:1426
EvaluateHasIncludeCommon
static bool EvaluateHasIncludeCommon(Token &Tok, IdentifierInfo *II, Preprocessor &PP, ConstSearchDirIterator LookupFrom, const FileEntry *LookupFromFile)
EvaluateHasIncludeCommon - Process a '__has_include("path")' or '__has_include_next("path")' expressi...
Definition: PPMacroExpansion.cpp:1181
clang::MacroInfo::isUsed
bool isUsed() const
Return false if this macro is defined in the main file and has not yet been used.
Definition: MacroInfo.h:224
clang::Preprocessor::GetIncludeFilenameSpelling
bool GetIncludeFilenameSpelling(SourceLocation Loc, StringRef &Buffer)
Turn the specified lexer token into a fully checked and spelled filename, e.g.
Definition: PPDirectives.cpp:1756
clang::MacroDirective::getPrevious
const MacroDirective * getPrevious() const
Get previous definition of the macro with the same name.
Definition: MacroInfo.h:351
clang::dataflow::Literal
uint32_t Literal
Literals are represented as positive integers.
Definition: WatchedLiteralsSolver.cpp:55
clang::Preprocessor::AllocateMacroInfo
MacroInfo * AllocateMacroInfo(SourceLocation L)
Allocate a new MacroInfo object with the provided SourceLocation.
Definition: PPDirectives.cpp:59
clang::Module
Describes a module or submodule.
Definition: Module.h:98
clang::diag::Flavor::WarningOrError
@ WarningOrError
A diagnostic that indicates a problem or potential problem.
RegisterBuiltinMacro
static IdentifierInfo * RegisterBuiltinMacro(Preprocessor &PP, const char *Name)
RegisterBuiltinMacro - Register the specified identifier in the identifier table and mark it as a bui...
Definition: PPMacroExpansion.cpp:326
clang::Preprocessor::setLoadedMacroDirective
void setLoadedMacroDirective(IdentifierInfo *II, MacroDirective *ED, MacroDirective *MD)
Set a MacroDirective that was loaded from a PCH file.
Definition: PPMacroExpansion.cpp:97
Id
int Id
Definition: ASTDiff.cpp:189
getDiagnosticsInGroup
static bool getDiagnosticsInGroup(diag::Flavor Flavor, const WarningOption *Group, SmallVectorImpl< diag::kind > &Diags)
Return true if any diagnostics were found in this group, even if they were filtered out due to having...
Definition: DiagnosticIDs.cpp:675
clang::MacroDefinition::getMacroInfo
MacroInfo * getMacroInfo() const
Get the MacroInfo that should be used for this definition.
Definition: MacroInfo.h:603
clang::PresumedLoc::getFilename
const char * getFilename() const
Return the presumed filename of this location.
Definition: SourceLocation.h:324
clang::Token::setFlagValue
void setFlagValue(TokenFlags Flag, bool Val)
Set a flag to either true or false.
Definition: Token.h:261
clang::Token::getKind
tok::TokenKind getKind() const
Definition: Token.h:93
clang::Preprocessor::getLastFPEvalPragmaLocation
SourceLocation getLastFPEvalPragmaLocation() const
Definition: Preprocessor.h:2195
clang::CPlusPlus
@ CPlusPlus
Definition: LangStandard.h:53
isTrivialSingleTokenExpansion
static bool isTrivialSingleTokenExpansion(const MacroInfo *MI, const IdentifierInfo *MacroIdent, Preprocessor &PP)
isTrivialSingleTokenExpansion - Return true if MI, which has a single token in its expansion,...
Definition: PPMacroExpansion.cpp:407
LangOptions.h
clang::MacroInfo::setIsUsed
void setIsUsed(bool Val)
Set the value of the IsUsed flag.
Definition: MacroInfo.h:154
LexDiagnostic.h
Depth
int Depth
Definition: ASTDiff.cpp:189
clang::Preprocessor::Diag
DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID) const
Forwarding function for diagnostics.
Definition: Preprocessor.h:1899
clang::Token::is
bool is(tok::TokenKind K) const
is/isNot - Predicates to check if this token is a specific kind, as in "if (Tok.is(tok::l_brace)) {....
Definition: Token.h:98
clang::Preprocessor::getTargetInfo
const TargetInfo & getTargetInfo() const
Definition: Preprocessor.h:1066
clang::Token::DisableExpand
@ DisableExpand
Definition: Token.h:78
IdentifierTable.h
clang::CPlusPlus20
@ CPlusPlus20
Definition: LangStandard.h:57
llvm::SmallString< 32 >
clang::Token::LeadingEmptyMacro
@ LeadingEmptyMacro
Definition: Token.h:80
HasFeature
static bool HasFeature(const Preprocessor &PP, StringRef Feature)
HasFeature - Return true if we recognize and implement the feature specified by the identifier as a s...
Definition: PPMacroExpansion.cpp:1135
llvm::DenseSet
Definition: Sema.h:77
clang::Preprocessor::getMacroInfo
const MacroInfo * getMacroInfo(const IdentifierInfo *II) const
Definition: Preprocessor.h:1258
isTargetOS
static bool isTargetOS(const TargetInfo &TI, const IdentifierInfo *II)
Implements the __is_target_os builtin macro.
Definition: PPMacroExpansion.cpp:1434
clang::SrcMgr::C_User
@ C_User
Definition: SourceManager.h:80
clang::MacroArgs::create
static MacroArgs * create(const MacroInfo *MI, ArrayRef< Token > UnexpArgTokens, bool VarargsElided, Preprocessor &PP)
MacroArgs ctor function - Create a new MacroArgs object with the specified macro and argument info.
Definition: MacroArgs.cpp:24
clang::IdentifierInfo::getBuiltinID
unsigned getBuiltinID() const
Return a value indicating whether this is a builtin function.
Definition: IdentifierTable.h:303
clang::Token::isNot
bool isNot(tok::TokenKind K) const
Definition: Token.h:99
HasExtension
static bool HasExtension(const Preprocessor &PP, StringRef Extension)
HasExtension - Return true if we recognize and implement the feature specified by the identifier,...
Definition: PPMacroExpansion.cpp:1152
clang::hasAttribute
int hasAttribute(AttributeCommonInfo::Syntax Syntax, const IdentifierInfo *Scope, const IdentifierInfo *Attr, const TargetInfo &Target, const LangOptions &LangOpts)
Return the version number associated with the attribute if we recognize and implement the attribute s...
Definition: Attributes.cpp:7
clang::FileEntry
Cached information about one file (either on disk or in the virtual file system).
Definition: FileEntry.h:366
clang::MacroInfo::isEnabled
bool isEnabled() const
Return true if this macro is enabled.
Definition: MacroInfo.h:281
clang::TargetInfo::getDarwinTargetVariantTriple
const llvm::Triple * getDarwinTargetVariantTriple() const
Returns the darwin target variant triple, the variant of the deployment target for which the code is ...
Definition: TargetInfo.h:1680
clang::Builtin::evaluateRequiredTargetFeatures
bool evaluateRequiredTargetFeatures(llvm::StringRef RequiredFatures, const llvm::StringMap< bool > &TargetFetureMap)
Returns true if the required target features of a builtin function are enabled.
clang::Token::getLength
unsigned getLength() const
Definition: Token.h:129
clang::Token::LeadingSpace
@ LeadingSpace
Definition: Token.h:76
clang::transformer::EditKind::Range
@ Range
clang::TargetInfo::getTriple
const llvm::Triple & getTriple() const
Returns the target triple of the primary target.
Definition: TargetInfo.h:1197
clang::MacroDirective::isDefined
bool isDefined() const
Definition: MacroInfo.h:407
clang::MacroArgs::destroy
void destroy(Preprocessor &PP)
destroy - Destroy and deallocate the memory for this object.
Definition: MacroArgs.cpp:78
clang::PresumedLoc::isInvalid
bool isInvalid() const
Return true if this object is invalid or uninitialized.
Definition: SourceLocation.h:318
clang::Preprocessor::getSpelling
StringRef getSpelling(SourceLocation loc, SmallVectorImpl< char > &buffer, bool *invalid=nullptr) const
Return the 'spelling' of the token at the given location; does not go up to the spelling location or ...
Definition: Preprocessor.h:1914
MacroArgs.h
clang::LangOptions::UseTargetPathSeparator
bool UseTargetPathSeparator
Indicates whether the FILE macro should use the target's platform-specific file separator or whether ...
Definition: LangOptions.h:485
SourceLocation.h
clang::Token::setLiteralData
void setLiteralData(const char *Ptr)
Definition: Token.h:223
clang::detail::SearchDirIteratorImpl< true >
clang::MacroInfo::params
ArrayRef< const IdentifierInfo * > params() const
Definition: MacroInfo.h:185
clang::Token::getFlags
unsigned getFlags() const
Return the internal represtation of the flags.
Definition: Token.h:256
clang::Preprocessor::getBuiltinInfo
Builtin::Context & getBuiltinInfo()
Definition: Preprocessor.h:1075
clang::MacroDefinition::forAllDefinitions
void forAllDefinitions(Fn F) const
Definition: MacroInfo.h:623
clang::Preprocessor::dumpMacroInfo
void dumpMacroInfo(const IdentifierInfo *II)
Definition: PPMacroExpansion.cpp:259
llvm::ArrayRef
Definition: LLVM.h:34
Value
Value
Definition: UninitializedValues.cpp:103
HeaderSearch.h
clang::Preprocessor::appendDefMacroDirective
DefMacroDirective * appendDefMacroDirective(IdentifierInfo *II, MacroInfo *MI, SourceLocation Loc)
Definition: Preprocessor.h:1279
LLVM.h
clang::Preprocessor::markMacroAsUsed
void markMacroAsUsed(MacroInfo *MI)
A macro is used, update information about macros that need unused warnings.
Definition: PPMacroExpansion.cpp:1978
clang::Token::StartOfLine
@ StartOfLine
Definition: Token.h:74
clang::TokenLexer
TokenLexer - This implements a lexer that returns tokens from a macro body or token stream instead of...
Definition: TokenLexer.h:30
State
LineState State
Definition: UnwrappedLineFormatter.cpp:1147
clang::DiagnosticsEngine::getExtensionHandlingBehavior
diag::Severity getExtensionHandlingBehavior() const
Definition: Diagnostic.h:776
clang::CharSourceRange::getEnd
SourceLocation getEnd() const
Definition: SourceLocation.h:284
clang::IdentifierInfo
One of these records is kept for each identifier that is lexed.
Definition: IdentifierTable.h:85
clang::Token::clearFlag
void clearFlag(TokenFlags Flag)
Unset the specified flag.
Definition: Token.h:248
clang::Token::getLocation
SourceLocation getLocation() const
Return a source location identifier for the specified offset in the current file.
Definition: Token.h:126
isTargetArch
static bool isTargetArch(const TargetInfo &TI, const IdentifierInfo *II)
Implements the __is_target_arch builtin macro.
Definition: PPMacroExpansion.cpp:1404
Attributes.h
clang::LangOptions
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:81
CodeCompletionHandler.h
clang::IdentifierInfo::getNameStart
const char * getNameStart() const
Return the beginning of the actual null-terminated string for this identifier.
Definition: IdentifierTable.h:190
clang::Token::isAnnotation
bool isAnnotation() const
Return true if this is any of tok::annot_* kind tokens.
Definition: Token.h:120
clang::Preprocessor::getPreprocessorOpts
PreprocessorOptions & getPreprocessorOpts() const
Retrieve the preprocessor options used to initialize this preprocessor.
Definition: Preprocessor.h:1060
clang::Token::setLocation
void setLocation(SourceLocation L)
Definition: Token.h:134
clang::Preprocessor::EnterMacro
void EnterMacro(Token &Tok, SourceLocation ILEnd, MacroInfo *Macro, MacroArgs *Args)
Add a Macro to the top of the include stack and start lexing tokens from it instead of the current bu...
Definition: PPLexerChange.cpp:150
clang::PresumedLoc::isValid
bool isValid() const
Definition: SourceLocation.h:319
clang::IdentifierInfo::getName
StringRef getName() const
Return the actual identifier string.
Definition: IdentifierTable.h:196
clang::Builtin::ID
ID
Definition: Builtins.h:52
clang::MacroInfo::getNumParams
unsigned getNumParams() const
Definition: MacroInfo.h:184
clang::SourceLocation::isInvalid
bool isInvalid() const
Definition: SourceLocation.h:111
clang::MacroInfo::isBuiltinMacro
bool isBuiltinMacro() const
Return true if this macro requires processing before expansion.
Definition: MacroInfo.h:217
clang
Definition: CalledOnceCheck.h:17
clang::ExternalPreprocessorSource::updateOutOfDateIdentifier
virtual void updateOutOfDateIdentifier(IdentifierInfo &II)=0
Update an out-of-date identifier.
Bracket
Bracket
Definition: PPMacroExpansion.cpp:636
clang::FileEntry::getModificationTime
time_t getModificationTime() const
Definition: FileEntry.h:404
clang::IdentifierInfo::hadMacroDefinition
bool hadMacroDefinition() const
Returns true if this identifier was #defined to some value at any moment.
Definition: IdentifierTable.h:227
MacroInfo.h
clang::MacroDirective
Encapsulates changes to the "macros namespace" (the location where the macro name became active,...
Definition: MacroInfo.h:313
clang::MacroDirective::setPrevious
void setPrevious(MacroDirective *Prev)
Set previous definition of the macro with the same name.
Definition: MacroInfo.h:348
clang::Preprocessor::getDiagnostics
DiagnosticsEngine & getDiagnostics() const
Definition: Preprocessor.h:1062
clang::SourceManager::isInSystemHeader
bool isInSystemHeader(SourceLocation Loc) const
Returns if a SourceLocation is in a system header.
Definition: SourceManager.h:1504
clang::ModuleMacro::Profile
void Profile(llvm::FoldingSetNodeID &ID) const
Definition: MacroInfo.h:542
clang::SourceLocation::isValid
bool isValid() const
Return true if this is a valid SourceLocation object.
Definition: SourceLocation.h:110
clang::Token::setLength
void setLength(unsigned Len)
Definition: Token.h:135
clang::SourceManager::getFileEntryForID
const FileEntry * getFileEntryForID(FileID FID) const
Returns the FileEntry record for the provided FileID.
Definition: SourceManager.h:1043
clang::Preprocessor::LookupFile
Optional< FileEntryRef > LookupFile(SourceLocation FilenameLoc, StringRef Filename, bool isAngled, ConstSearchDirIterator FromDir, const FileEntry *FromFile, ConstSearchDirIterator *CurDir, SmallVectorImpl< char > *SearchPath, SmallVectorImpl< char > *RelativePath, ModuleMap::KnownHeader *SuggestedModule, bool *IsMapped, bool *IsFrameworkFound, bool SkipCache=false, bool OpenFile=true, bool CacheFailures=true)
Given a "foo" or <foo> reference, look up the indicated file.
Definition: PPDirectives.cpp:951
clang::Preprocessor::getLocForEndOfToken
SourceLocation getLocForEndOfToken(SourceLocation Loc, unsigned Offset=0)
Computes the source location just past the end of the token at this source location.
Definition: Preprocessor.h:2023
clang::Token::setKind
void setKind(tok::TokenKind K)
Definition: Token.h:94
clang::MacroInfo::isWarnIfUnused
bool isWarnIfUnused() const
Return true if we should emit a warning if the macro is unused.
Definition: MacroInfo.h:232
clang::Token::NeedsCleaning
@ NeedsCleaning
Definition: Token.h:79
ExpectFeatureIdentifierInfo
static IdentifierInfo * ExpectFeatureIdentifierInfo(Token &Tok, Preprocessor &PP, signed DiagID)
Helper function to return the IdentifierInfo structure of a Token or generate a diagnostic if none av...
Definition: PPMacroExpansion.cpp:1392
clang::Token::isOneOf
bool isOneOf(tok::TokenKind K1, tok::TokenKind K2) const
Definition: Token.h:100
clang::CPlusPlus11
@ CPlusPlus11
Definition: LangStandard.h:54
clang::LangOptions::remapPathPrefix
void remapPathPrefix(SmallVectorImpl< char > &Path) const
Remap path prefix according to -fmacro-prefix-path option.
Definition: LangOptions.cpp:65
clang::Token::setFlag
void setFlag(TokenFlags Flag)
Set the specified flag.
Definition: Token.h:238
clang::comments::tok::eof
@ eof
Definition: CommentLexer.h:33
clang::PresumedLoc
Represents an unpacked "presumed" location which can be presented to the user.
Definition: SourceLocation.h:302
llvm::SmallVectorImpl
Definition: Randstruct.h:18
clang::MacroInfo::isFunctionLike
bool isFunctionLike() const
Definition: MacroInfo.h:201
FileManager.h
clang::Preprocessor::LexUnexpandedToken
void LexUnexpandedToken(Token &Result)
Just like Lex, but disables macro expansion of identifier tokens.
Definition: Preprocessor.h:1632
PreprocessorOptions.h
clang::IdentifierInfo::hasRevertedTokenIDToIdentifier
bool hasRevertedTokenIDToIdentifier() const
True if revertTokenIDToIdentifier() was called.
Definition: IdentifierTable.h:265
clang::Preprocessor
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Definition: Preprocessor.h:129
clang::Preprocessor::appendMacroDirective
void appendMacroDirective(IdentifierInfo *II, MacroDirective *MD)
Add a directive to the macro directive history for this identifier.
Definition: PPMacroExpansion.cpp:72
clang::Preprocessor::getHeaderSearchInfo
HeaderSearch & getHeaderSearchInfo() const
Definition: Preprocessor.h:1070
Token.h
clang::StringLiteralParser
StringLiteralParser - This decodes string escape characters and performs wide string analysis and Tra...
Definition: LiteralSupport.h:218
clang::MacroInfo::setIsBuiltinMacro
void setIsBuiltinMacro(bool Val=true)
Set or clear the isBuiltinMacro flag.
Definition: MacroInfo.h:151
clang::ModuleMacro::create
static ModuleMacro * create(Preprocessor &PP, Module *OwningModule, IdentifierInfo *II, MacroInfo *Macro, ArrayRef< ModuleMacro * > Overrides)
Definition: MacroInfo.cpp:259
clang::Preprocessor::getExternalSource
ExternalPreprocessorSource * getExternalSource() const
Definition: Preprocessor.h:1082
clang::Preprocessor::LexNonComment
void LexNonComment(Token &Result)
Lex a token.
Definition: Preprocessor.h:1625
isTargetEnvironment
static bool isTargetEnvironment(const TargetInfo &TI, const IdentifierInfo *II)
Implements the __is_target_environment builtin macro.
Definition: PPMacroExpansion.cpp:1446
clang::SourceManager::createExpansionLoc
SourceLocation createExpansionLoc(SourceLocation SpellingLoc, SourceLocation ExpansionLocStart, SourceLocation ExpansionLocEnd, unsigned Length, bool ExpansionIsTokenRange=true, int LoadedID=0, SourceLocation::UIntTy LoadedOffset=0)
Creates an expansion SLocEntry for a macro use.
Definition: SourceManager.cpp:642
clang::ModuleMacro
Represents a macro directive exported by a module.
Definition: MacroInfo.h:511
clang::Preprocessor::getIdentifierInfo
IdentifierInfo * getIdentifierInfo(StringRef Name) const
Return information about the specified preprocessor identifier token.
Definition: Preprocessor.h:1383
clang::CodeCompletionHandler::CodeCompleteMacroArgument
virtual void CodeCompleteMacroArgument(IdentifierInfo *Macro, MacroInfo *MacroInfo, unsigned ArgumentIndex)
Callback invoked when performing code completion inside a function-like macro argument.
Definition: CodeCompletionHandler.h:60
ExternalPreprocessorSource.h
clang::Token::hasUDSuffix
bool hasUDSuffix() const
Return true if this token is a string or character literal which has a ud-suffix.
Definition: Token.h:295
clang::ento::ObjKind::OS
@ OS
Indicates that the tracking object is a descendant of a referenced-counted OSObject,...
clang::Preprocessor::getTUFPEvalMethod
LangOptions::FPEvalMethodKind getTUFPEvalMethod() const
Definition: Preprocessor.h:2191
clang::MacroInfo::getDefinitionLoc
SourceLocation getDefinitionLoc() const
Return the location that the macro was defined at.
Definition: MacroInfo.h:125