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