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