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