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