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