clang  3.8.0
PPDirectives.cpp
Go to the documentation of this file.
1 //===--- PPDirectives.cpp - Directive Handling for Preprocessor -----------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 ///
10 /// \file
11 /// \brief Implements # directive processing for the Preprocessor.
12 ///
13 //===----------------------------------------------------------------------===//
14 
15 #include "clang/Lex/Preprocessor.h"
19 #include "clang/Lex/HeaderSearch.h"
23 #include "clang/Lex/MacroInfo.h"
24 #include "clang/Lex/ModuleLoader.h"
25 #include "clang/Lex/Pragma.h"
26 #include "llvm/ADT/APInt.h"
27 #include "llvm/Support/ErrorHandling.h"
28 #include "llvm/Support/Path.h"
29 #include "llvm/Support/SaveAndRestore.h"
30 using namespace clang;
31 
32 //===----------------------------------------------------------------------===//
33 // Utility Methods for Preprocessor Directive Handling.
34 //===----------------------------------------------------------------------===//
35 
36 MacroInfo *Preprocessor::AllocateMacroInfo() {
37  MacroInfoChain *MIChain = BP.Allocate<MacroInfoChain>();
38  MIChain->Next = MIChainHead;
39  MIChainHead = MIChain;
40  return &MIChain->MI;
41 }
42 
43 MacroInfo *Preprocessor::AllocateMacroInfo(SourceLocation L) {
45  new (MI) MacroInfo(L);
46  return MI;
47 }
48 
50  unsigned SubModuleID) {
51  static_assert(llvm::AlignOf<MacroInfo>::Alignment >= sizeof(SubModuleID),
52  "alignment for MacroInfo is less than the ID");
53  DeserializedMacroInfoChain *MIChain =
54  BP.Allocate<DeserializedMacroInfoChain>();
55  MIChain->Next = DeserialMIChainHead;
56  DeserialMIChainHead = MIChain;
57 
58  MacroInfo *MI = &MIChain->MI;
59  new (MI) MacroInfo(L);
60  MI->FromASTFile = true;
61  MI->setOwningModuleID(SubModuleID);
62  return MI;
63 }
64 
65 DefMacroDirective *Preprocessor::AllocateDefMacroDirective(MacroInfo *MI,
66  SourceLocation Loc) {
67  return new (BP) DefMacroDirective(MI, Loc);
68 }
69 
71 Preprocessor::AllocateUndefMacroDirective(SourceLocation UndefLoc) {
72  return new (BP) UndefMacroDirective(UndefLoc);
73 }
74 
76 Preprocessor::AllocateVisibilityMacroDirective(SourceLocation Loc,
77  bool isPublic) {
78  return new (BP) VisibilityMacroDirective(Loc, isPublic);
79 }
80 
81 /// \brief Read and discard all tokens remaining on the current line until
82 /// the tok::eod token is found.
84  Token Tmp;
85  do {
86  LexUnexpandedToken(Tmp);
87  assert(Tmp.isNot(tok::eof) && "EOF seen while discarding directive tokens");
88  } while (Tmp.isNot(tok::eod));
89 }
90 
91 /// \brief Enumerates possible cases of #define/#undef a reserved identifier.
92 enum MacroDiag {
93  MD_NoWarn, //> Not a reserved identifier
94  MD_KeywordDef, //> Macro hides keyword, enabled by default
95  MD_ReservedMacro //> #define of #undef reserved id, disabled by default
96 };
97 
98 /// \brief Checks if the specified identifier is reserved in the specified
99 /// language.
100 /// This function does not check if the identifier is a keyword.
101 static bool isReservedId(StringRef Text, const LangOptions &Lang) {
102  // C++ [macro.names], C11 7.1.3:
103  // All identifiers that begin with an underscore and either an uppercase
104  // letter or another underscore are always reserved for any use.
105  if (Text.size() >= 2 && Text[0] == '_' &&
106  (isUppercase(Text[1]) || Text[1] == '_'))
107  return true;
108  // C++ [global.names]
109  // Each name that contains a double underscore ... is reserved to the
110  // implementation for any use.
111  if (Lang.CPlusPlus) {
112  if (Text.find("__") != StringRef::npos)
113  return true;
114  }
115  return false;
116 }
117 
119  const LangOptions &Lang = PP.getLangOpts();
120  StringRef Text = II->getName();
121  if (isReservedId(Text, Lang))
122  return MD_ReservedMacro;
123  if (II->isKeyword(Lang))
124  return MD_KeywordDef;
125  if (Lang.CPlusPlus11 && (Text.equals("override") || Text.equals("final")))
126  return MD_KeywordDef;
127  return MD_NoWarn;
128 }
129 
131  const LangOptions &Lang = PP.getLangOpts();
132  StringRef Text = II->getName();
133  // Do not warn on keyword undef. It is generally harmless and widely used.
134  if (isReservedId(Text, Lang))
135  return MD_ReservedMacro;
136  return MD_NoWarn;
137 }
138 
139 bool Preprocessor::CheckMacroName(Token &MacroNameTok, MacroUse isDefineUndef,
140  bool *ShadowFlag) {
141  // Missing macro name?
142  if (MacroNameTok.is(tok::eod))
143  return Diag(MacroNameTok, diag::err_pp_missing_macro_name);
144 
145  IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
146  if (!II) {
147  bool Invalid = false;
148  std::string Spelling = getSpelling(MacroNameTok, &Invalid);
149  if (Invalid)
150  return Diag(MacroNameTok, diag::err_pp_macro_not_identifier);
151  II = getIdentifierInfo(Spelling);
152 
153  if (!II->isCPlusPlusOperatorKeyword())
154  return Diag(MacroNameTok, diag::err_pp_macro_not_identifier);
155 
156  // C++ 2.5p2: Alternative tokens behave the same as its primary token
157  // except for their spellings.
158  Diag(MacroNameTok, getLangOpts().MicrosoftExt
159  ? diag::ext_pp_operator_used_as_macro_name
160  : diag::err_pp_operator_used_as_macro_name)
161  << II << MacroNameTok.getKind();
162 
163  // Allow #defining |and| and friends for Microsoft compatibility or
164  // recovery when legacy C headers are included in C++.
165  MacroNameTok.setIdentifierInfo(II);
166  }
167 
168  if ((isDefineUndef != MU_Other) && II->getPPKeywordID() == tok::pp_defined) {
169  // Error if defining "defined": C99 6.10.8/4, C++ [cpp.predefined]p4.
170  return Diag(MacroNameTok, diag::err_defined_macro_name);
171  }
172 
173  if (isDefineUndef == MU_Undef) {
174  auto *MI = getMacroInfo(II);
175  if (MI && MI->isBuiltinMacro()) {
176  // Warn if undefining "__LINE__" and other builtins, per C99 6.10.8/4
177  // and C++ [cpp.predefined]p4], but allow it as an extension.
178  Diag(MacroNameTok, diag::ext_pp_undef_builtin_macro);
179  }
180  }
181 
182  // If defining/undefining reserved identifier or a keyword, we need to issue
183  // a warning.
184  SourceLocation MacroNameLoc = MacroNameTok.getLocation();
185  if (ShadowFlag)
186  *ShadowFlag = false;
187  if (!SourceMgr.isInSystemHeader(MacroNameLoc) &&
188  (strcmp(SourceMgr.getBufferName(MacroNameLoc), "<built-in>") != 0)) {
189  MacroDiag D = MD_NoWarn;
190  if (isDefineUndef == MU_Define) {
191  D = shouldWarnOnMacroDef(*this, II);
192  }
193  else if (isDefineUndef == MU_Undef)
194  D = shouldWarnOnMacroUndef(*this, II);
195  if (D == MD_KeywordDef) {
196  // We do not want to warn on some patterns widely used in configuration
197  // scripts. This requires analyzing next tokens, so do not issue warnings
198  // now, only inform caller.
199  if (ShadowFlag)
200  *ShadowFlag = true;
201  }
202  if (D == MD_ReservedMacro)
203  Diag(MacroNameTok, diag::warn_pp_macro_is_reserved_id);
204  }
205 
206  // Okay, we got a good identifier.
207  return false;
208 }
209 
210 /// \brief Lex and validate a macro name, which occurs after a
211 /// \#define or \#undef.
212 ///
213 /// This sets the token kind to eod and discards the rest of the macro line if
214 /// the macro name is invalid.
215 ///
216 /// \param MacroNameTok Token that is expected to be a macro name.
217 /// \param isDefineUndef Context in which macro is used.
218 /// \param ShadowFlag Points to a flag that is set if macro shadows a keyword.
219 void Preprocessor::ReadMacroName(Token &MacroNameTok, MacroUse isDefineUndef,
220  bool *ShadowFlag) {
221  // Read the token, don't allow macro expansion on it.
222  LexUnexpandedToken(MacroNameTok);
223 
224  if (MacroNameTok.is(tok::code_completion)) {
225  if (CodeComplete)
226  CodeComplete->CodeCompleteMacroName(isDefineUndef == MU_Define);
228  LexUnexpandedToken(MacroNameTok);
229  }
230 
231  if (!CheckMacroName(MacroNameTok, isDefineUndef, ShadowFlag))
232  return;
233 
234  // Invalid macro name, read and discard the rest of the line and set the
235  // token kind to tok::eod if necessary.
236  if (MacroNameTok.isNot(tok::eod)) {
237  MacroNameTok.setKind(tok::eod);
239  }
240 }
241 
242 /// \brief Ensure that the next token is a tok::eod token.
243 ///
244 /// If not, emit a diagnostic and consume up until the eod. If EnableMacros is
245 /// true, then we consider macros that expand to zero tokens as being ok.
246 void Preprocessor::CheckEndOfDirective(const char *DirType, bool EnableMacros) {
247  Token Tmp;
248  // Lex unexpanded tokens for most directives: macros might expand to zero
249  // tokens, causing us to miss diagnosing invalid lines. Some directives (like
250  // #line) allow empty macros.
251  if (EnableMacros)
252  Lex(Tmp);
253  else
254  LexUnexpandedToken(Tmp);
255 
256  // There should be no tokens after the directive, but we allow them as an
257  // extension.
258  while (Tmp.is(tok::comment)) // Skip comments in -C mode.
259  LexUnexpandedToken(Tmp);
260 
261  if (Tmp.isNot(tok::eod)) {
262  // Add a fixit in GNU/C99/C++ mode. Don't offer a fixit for strict-C89,
263  // or if this is a macro-style preprocessing directive, because it is more
264  // trouble than it is worth to insert /**/ and check that there is no /**/
265  // in the range also.
266  FixItHint Hint;
267  if ((LangOpts.GNUMode || LangOpts.C99 || LangOpts.CPlusPlus) &&
268  !CurTokenLexer)
269  Hint = FixItHint::CreateInsertion(Tmp.getLocation(),"//");
270  Diag(Tmp, diag::ext_pp_extra_tokens_at_eol) << DirType << Hint;
272  }
273 }
274 
275 
276 
277 /// SkipExcludedConditionalBlock - We just read a \#if or related directive and
278 /// decided that the subsequent tokens are in the \#if'd out portion of the
279 /// file. Lex the rest of the file, until we see an \#endif. If
280 /// FoundNonSkipPortion is true, then we have already emitted code for part of
281 /// this \#if directive, so \#else/\#elif blocks should never be entered.
282 /// If ElseOk is true, then \#else directives are ok, if not, then we have
283 /// already seen one so a \#else directive is a duplicate. When this returns,
284 /// the caller can lex the first valid token.
285 void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc,
286  bool FoundNonSkipPortion,
287  bool FoundElse,
288  SourceLocation ElseLoc) {
289  ++NumSkipped;
290  assert(!CurTokenLexer && CurPPLexer && "Lexing a macro, not a file?");
291 
292  CurPPLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/false,
293  FoundNonSkipPortion, FoundElse);
294 
295  if (CurPTHLexer) {
296  PTHSkipExcludedConditionalBlock();
297  return;
298  }
299 
300  // Enter raw mode to disable identifier lookup (and thus macro expansion),
301  // disabling warnings, etc.
302  CurPPLexer->LexingRawMode = true;
303  Token Tok;
304  while (1) {
305  CurLexer->Lex(Tok);
306 
307  if (Tok.is(tok::code_completion)) {
308  if (CodeComplete)
309  CodeComplete->CodeCompleteInConditionalExclusion();
311  continue;
312  }
313 
314  // If this is the end of the buffer, we have an error.
315  if (Tok.is(tok::eof)) {
316  // Emit errors for each unterminated conditional on the stack, including
317  // the current one.
318  while (!CurPPLexer->ConditionalStack.empty()) {
319  if (CurLexer->getFileLoc() != CodeCompletionFileLoc)
320  Diag(CurPPLexer->ConditionalStack.back().IfLoc,
321  diag::err_pp_unterminated_conditional);
322  CurPPLexer->ConditionalStack.pop_back();
323  }
324 
325  // Just return and let the caller lex after this #include.
326  break;
327  }
328 
329  // If this token is not a preprocessor directive, just skip it.
330  if (Tok.isNot(tok::hash) || !Tok.isAtStartOfLine())
331  continue;
332 
333  // We just parsed a # character at the start of a line, so we're in
334  // directive mode. Tell the lexer this so any newlines we see will be
335  // converted into an EOD token (this terminates the macro).
336  CurPPLexer->ParsingPreprocessorDirective = true;
337  if (CurLexer) CurLexer->SetKeepWhitespaceMode(false);
338 
339 
340  // Read the next token, the directive flavor.
341  LexUnexpandedToken(Tok);
342 
343  // If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or
344  // something bogus), skip it.
345  if (Tok.isNot(tok::raw_identifier)) {
346  CurPPLexer->ParsingPreprocessorDirective = false;
347  // Restore comment saving mode.
348  if (CurLexer) CurLexer->resetExtendedTokenMode();
349  continue;
350  }
351 
352  // If the first letter isn't i or e, it isn't intesting to us. We know that
353  // this is safe in the face of spelling differences, because there is no way
354  // to spell an i/e in a strange way that is another letter. Skipping this
355  // allows us to avoid looking up the identifier info for #define/#undef and
356  // other common directives.
357  StringRef RI = Tok.getRawIdentifier();
358 
359  char FirstChar = RI[0];
360  if (FirstChar >= 'a' && FirstChar <= 'z' &&
361  FirstChar != 'i' && FirstChar != 'e') {
362  CurPPLexer->ParsingPreprocessorDirective = false;
363  // Restore comment saving mode.
364  if (CurLexer) CurLexer->resetExtendedTokenMode();
365  continue;
366  }
367 
368  // Get the identifier name without trigraphs or embedded newlines. Note
369  // that we can't use Tok.getIdentifierInfo() because its lookup is disabled
370  // when skipping.
371  char DirectiveBuf[20];
372  StringRef Directive;
373  if (!Tok.needsCleaning() && RI.size() < 20) {
374  Directive = RI;
375  } else {
376  std::string DirectiveStr = getSpelling(Tok);
377  unsigned IdLen = DirectiveStr.size();
378  if (IdLen >= 20) {
379  CurPPLexer->ParsingPreprocessorDirective = false;
380  // Restore comment saving mode.
381  if (CurLexer) CurLexer->resetExtendedTokenMode();
382  continue;
383  }
384  memcpy(DirectiveBuf, &DirectiveStr[0], IdLen);
385  Directive = StringRef(DirectiveBuf, IdLen);
386  }
387 
388  if (Directive.startswith("if")) {
389  StringRef Sub = Directive.substr(2);
390  if (Sub.empty() || // "if"
391  Sub == "def" || // "ifdef"
392  Sub == "ndef") { // "ifndef"
393  // We know the entire #if/#ifdef/#ifndef block will be skipped, don't
394  // bother parsing the condition.
396  CurPPLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true,
397  /*foundnonskip*/false,
398  /*foundelse*/false);
399  }
400  } else if (Directive[0] == 'e') {
401  StringRef Sub = Directive.substr(1);
402  if (Sub == "ndif") { // "endif"
403  PPConditionalInfo CondInfo;
404  CondInfo.WasSkipping = true; // Silence bogus warning.
405  bool InCond = CurPPLexer->popConditionalLevel(CondInfo);
406  (void)InCond; // Silence warning in no-asserts mode.
407  assert(!InCond && "Can't be skipping if not in a conditional!");
408 
409  // If we popped the outermost skipping block, we're done skipping!
410  if (!CondInfo.WasSkipping) {
411  // Restore the value of LexingRawMode so that trailing comments
412  // are handled correctly, if we've reached the outermost block.
413  CurPPLexer->LexingRawMode = false;
414  CheckEndOfDirective("endif");
415  CurPPLexer->LexingRawMode = true;
416  if (Callbacks)
417  Callbacks->Endif(Tok.getLocation(), CondInfo.IfLoc);
418  break;
419  } else {
421  }
422  } else if (Sub == "lse") { // "else".
423  // #else directive in a skipping conditional. If not in some other
424  // skipping conditional, and if #else hasn't already been seen, enter it
425  // as a non-skipping conditional.
426  PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel();
427 
428  // If this is a #else with a #else before it, report the error.
429  if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_else_after_else);
430 
431  // Note that we've seen a #else in this conditional.
432  CondInfo.FoundElse = true;
433 
434  // If the conditional is at the top level, and the #if block wasn't
435  // entered, enter the #else block now.
436  if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) {
437  CondInfo.FoundNonSkip = true;
438  // Restore the value of LexingRawMode so that trailing comments
439  // are handled correctly.
440  CurPPLexer->LexingRawMode = false;
441  CheckEndOfDirective("else");
442  CurPPLexer->LexingRawMode = true;
443  if (Callbacks)
444  Callbacks->Else(Tok.getLocation(), CondInfo.IfLoc);
445  break;
446  } else {
447  DiscardUntilEndOfDirective(); // C99 6.10p4.
448  }
449  } else if (Sub == "lif") { // "elif".
450  PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel();
451 
452  // If this is a #elif with a #else before it, report the error.
453  if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_elif_after_else);
454 
455  // If this is in a skipping block or if we're already handled this #if
456  // block, don't bother parsing the condition.
457  if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) {
459  } else {
460  const SourceLocation CondBegin = CurPPLexer->getSourceLocation();
461  // Restore the value of LexingRawMode so that identifiers are
462  // looked up, etc, inside the #elif expression.
463  assert(CurPPLexer->LexingRawMode && "We have to be skipping here!");
464  CurPPLexer->LexingRawMode = false;
465  IdentifierInfo *IfNDefMacro = nullptr;
466  const bool CondValue = EvaluateDirectiveExpression(IfNDefMacro);
467  CurPPLexer->LexingRawMode = true;
468  if (Callbacks) {
469  const SourceLocation CondEnd = CurPPLexer->getSourceLocation();
470  Callbacks->Elif(Tok.getLocation(),
471  SourceRange(CondBegin, CondEnd),
472  (CondValue ? PPCallbacks::CVK_True : PPCallbacks::CVK_False), CondInfo.IfLoc);
473  }
474  // If this condition is true, enter it!
475  if (CondValue) {
476  CondInfo.FoundNonSkip = true;
477  break;
478  }
479  }
480  }
481  }
482 
483  CurPPLexer->ParsingPreprocessorDirective = false;
484  // Restore comment saving mode.
485  if (CurLexer) CurLexer->resetExtendedTokenMode();
486  }
487 
488  // Finally, if we are out of the conditional (saw an #endif or ran off the end
489  // of the file, just stop skipping and return to lexing whatever came after
490  // the #if block.
491  CurPPLexer->LexingRawMode = false;
492 
493  if (Callbacks) {
494  SourceLocation BeginLoc = ElseLoc.isValid() ? ElseLoc : IfTokenLoc;
495  Callbacks->SourceRangeSkipped(SourceRange(BeginLoc, Tok.getLocation()));
496  }
497 }
498 
499 void Preprocessor::PTHSkipExcludedConditionalBlock() {
500 
501  while (1) {
502  assert(CurPTHLexer);
503  assert(CurPTHLexer->LexingRawMode == false);
504 
505  // Skip to the next '#else', '#elif', or #endif.
506  if (CurPTHLexer->SkipBlock()) {
507  // We have reached an #endif. Both the '#' and 'endif' tokens
508  // have been consumed by the PTHLexer. Just pop off the condition level.
509  PPConditionalInfo CondInfo;
510  bool InCond = CurPTHLexer->popConditionalLevel(CondInfo);
511  (void)InCond; // Silence warning in no-asserts mode.
512  assert(!InCond && "Can't be skipping if not in a conditional!");
513  break;
514  }
515 
516  // We have reached a '#else' or '#elif'. Lex the next token to get
517  // the directive flavor.
518  Token Tok;
519  LexUnexpandedToken(Tok);
520 
521  // We can actually look up the IdentifierInfo here since we aren't in
522  // raw mode.
524 
525  if (K == tok::pp_else) {
526  // #else: Enter the else condition. We aren't in a nested condition
527  // since we skip those. We're always in the one matching the last
528  // blocked we skipped.
529  PPConditionalInfo &CondInfo = CurPTHLexer->peekConditionalLevel();
530  // Note that we've seen a #else in this conditional.
531  CondInfo.FoundElse = true;
532 
533  // If the #if block wasn't entered then enter the #else block now.
534  if (!CondInfo.FoundNonSkip) {
535  CondInfo.FoundNonSkip = true;
536 
537  // Scan until the eod token.
538  CurPTHLexer->ParsingPreprocessorDirective = true;
540  CurPTHLexer->ParsingPreprocessorDirective = false;
541 
542  break;
543  }
544 
545  // Otherwise skip this block.
546  continue;
547  }
548 
549  assert(K == tok::pp_elif);
550  PPConditionalInfo &CondInfo = CurPTHLexer->peekConditionalLevel();
551 
552  // If this is a #elif with a #else before it, report the error.
553  if (CondInfo.FoundElse)
554  Diag(Tok, diag::pp_err_elif_after_else);
555 
556  // If this is in a skipping block or if we're already handled this #if
557  // block, don't bother parsing the condition. We just skip this block.
558  if (CondInfo.FoundNonSkip)
559  continue;
560 
561  // Evaluate the condition of the #elif.
562  IdentifierInfo *IfNDefMacro = nullptr;
563  CurPTHLexer->ParsingPreprocessorDirective = true;
564  bool ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro);
565  CurPTHLexer->ParsingPreprocessorDirective = false;
566 
567  // If this condition is true, enter it!
568  if (ShouldEnter) {
569  CondInfo.FoundNonSkip = true;
570  break;
571  }
572 
573  // Otherwise, skip this block and go to the next one.
574  continue;
575  }
576 }
577 
579  ModuleMap &ModMap = HeaderInfo.getModuleMap();
580  if (SourceMgr.isInMainFile(Loc)) {
581  if (Module *CurMod = getCurrentModule())
582  return CurMod; // Compiling a module.
583  return HeaderInfo.getModuleMap().SourceModule; // Compiling a source.
584  }
585  // Try to determine the module of the include directive.
586  // FIXME: Look into directly passing the FileEntry from LookupFile instead.
587  FileID IDOfIncl = SourceMgr.getFileID(SourceMgr.getExpansionLoc(Loc));
588  if (const FileEntry *EntryOfIncl = SourceMgr.getFileEntryForID(IDOfIncl)) {
589  // The include comes from a file.
590  return ModMap.findModuleForHeader(EntryOfIncl).getModule();
591  } else {
592  // The include does not come from a file,
593  // so it is probably a module compilation.
594  return getCurrentModule();
595  }
596 }
597 
599  return HeaderInfo.getModuleMap().inferModuleFromLocation(
600  FullSourceLoc(Loc, SourceMgr));
601 }
602 
604  SourceLocation FilenameLoc,
605  StringRef Filename,
606  bool isAngled,
607  const DirectoryLookup *FromDir,
608  const FileEntry *FromFile,
609  const DirectoryLookup *&CurDir,
610  SmallVectorImpl<char> *SearchPath,
611  SmallVectorImpl<char> *RelativePath,
612  ModuleMap::KnownHeader *SuggestedModule,
613  bool SkipCache) {
614  Module *RequestingModule = getModuleForLocation(FilenameLoc);
615 
616  // If the header lookup mechanism may be relative to the current inclusion
617  // stack, record the parent #includes.
619  Includers;
620  if (!FromDir && !FromFile) {
622  const FileEntry *FileEnt = SourceMgr.getFileEntryForID(FID);
623 
624  // If there is no file entry associated with this file, it must be the
625  // predefines buffer or the module includes buffer. Any other file is not
626  // lexed with a normal lexer, so it won't be scanned for preprocessor
627  // directives.
628  //
629  // If we have the predefines buffer, resolve #include references (which come
630  // from the -include command line argument) from the current working
631  // directory instead of relative to the main file.
632  //
633  // If we have the module includes buffer, resolve #include references (which
634  // come from header declarations in the module map) relative to the module
635  // map file.
636  if (!FileEnt) {
637  if (FID == SourceMgr.getMainFileID() && MainFileDir)
638  Includers.push_back(std::make_pair(nullptr, MainFileDir));
639  else if ((FileEnt =
640  SourceMgr.getFileEntryForID(SourceMgr.getMainFileID())))
641  Includers.push_back(std::make_pair(FileEnt, FileMgr.getDirectory(".")));
642  } else {
643  Includers.push_back(std::make_pair(FileEnt, FileEnt->getDir()));
644  }
645 
646  // MSVC searches the current include stack from top to bottom for
647  // headers included by quoted include directives.
648  // See: http://msdn.microsoft.com/en-us/library/36k2cdd4.aspx
649  if (LangOpts.MSVCCompat && !isAngled) {
650  for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) {
651  IncludeStackInfo &ISEntry = IncludeMacroStack[e - i - 1];
652  if (IsFileLexer(ISEntry))
653  if ((FileEnt = ISEntry.ThePPLexer->getFileEntry()))
654  Includers.push_back(std::make_pair(FileEnt, FileEnt->getDir()));
655  }
656  }
657  }
658 
659  CurDir = CurDirLookup;
660 
661  if (FromFile) {
662  // We're supposed to start looking from after a particular file. Search
663  // the include path until we find that file or run out of files.
664  const DirectoryLookup *TmpCurDir = CurDir;
665  const DirectoryLookup *TmpFromDir = nullptr;
666  while (const FileEntry *FE = HeaderInfo.LookupFile(
667  Filename, FilenameLoc, isAngled, TmpFromDir, TmpCurDir,
668  Includers, SearchPath, RelativePath, RequestingModule,
669  SuggestedModule, SkipCache)) {
670  // Keep looking as if this file did a #include_next.
671  TmpFromDir = TmpCurDir;
672  ++TmpFromDir;
673  if (FE == FromFile) {
674  // Found it.
675  FromDir = TmpFromDir;
676  CurDir = TmpCurDir;
677  break;
678  }
679  }
680  }
681 
682  // Do a standard file entry lookup.
683  const FileEntry *FE = HeaderInfo.LookupFile(
684  Filename, FilenameLoc, isAngled, FromDir, CurDir, Includers, SearchPath,
685  RelativePath, RequestingModule, SuggestedModule, SkipCache);
686  if (FE) {
687  if (SuggestedModule && !LangOpts.AsmPreprocessor)
689  RequestingModule, FilenameLoc, Filename, FE);
690  return FE;
691  }
692 
693  const FileEntry *CurFileEnt;
694  // Otherwise, see if this is a subframework header. If so, this is relative
695  // to one of the headers on the #include stack. Walk the list of the current
696  // headers on the #include stack and pass them to HeaderInfo.
697  if (IsFileLexer()) {
698  if ((CurFileEnt = CurPPLexer->getFileEntry())) {
699  if ((FE = HeaderInfo.LookupSubframeworkHeader(Filename, CurFileEnt,
700  SearchPath, RelativePath,
701  RequestingModule,
702  SuggestedModule))) {
703  if (SuggestedModule && !LangOpts.AsmPreprocessor)
705  RequestingModule, FilenameLoc, Filename, FE);
706  return FE;
707  }
708  }
709  }
710 
711  for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) {
712  IncludeStackInfo &ISEntry = IncludeMacroStack[e-i-1];
713  if (IsFileLexer(ISEntry)) {
714  if ((CurFileEnt = ISEntry.ThePPLexer->getFileEntry())) {
715  if ((FE = HeaderInfo.LookupSubframeworkHeader(
716  Filename, CurFileEnt, SearchPath, RelativePath,
717  RequestingModule, SuggestedModule))) {
718  if (SuggestedModule && !LangOpts.AsmPreprocessor)
720  RequestingModule, FilenameLoc, Filename, FE);
721  return FE;
722  }
723  }
724  }
725  }
726 
727  // Otherwise, we really couldn't find the file.
728  return nullptr;
729 }
730 
731 
732 //===----------------------------------------------------------------------===//
733 // Preprocessor Directive Handling.
734 //===----------------------------------------------------------------------===//
735 
737 public:
739  : PP(pp), save(pp->DisableMacroExpansion) {
740  if (pp->MacroExpansionInDirectivesOverride)
741  pp->DisableMacroExpansion = false;
742  }
744  PP->DisableMacroExpansion = save;
745  }
746 private:
747  Preprocessor *PP;
748  bool save;
749 };
750 
751 /// HandleDirective - This callback is invoked when the lexer sees a # token
752 /// at the start of a line. This consumes the directive, modifies the
753 /// lexer/preprocessor state, and advances the lexer(s) so that the next token
754 /// read is the correct one.
756  // FIXME: Traditional: # with whitespace before it not recognized by K&R?
757 
758  // We just parsed a # character at the start of a line, so we're in directive
759  // mode. Tell the lexer this so any newlines we see will be converted into an
760  // EOD token (which terminates the directive).
761  CurPPLexer->ParsingPreprocessorDirective = true;
762  if (CurLexer) CurLexer->SetKeepWhitespaceMode(false);
763 
764  bool ImmediatelyAfterTopLevelIfndef =
767 
768  ++NumDirectives;
769 
770  // We are about to read a token. For the multiple-include optimization FA to
771  // work, we have to remember if we had read any tokens *before* this
772  // pp-directive.
773  bool ReadAnyTokensBeforeDirective =CurPPLexer->MIOpt.getHasReadAnyTokensVal();
774 
775  // Save the '#' token in case we need to return it later.
776  Token SavedHash = Result;
777 
778  // Read the next token, the directive flavor. This isn't expanded due to
779  // C99 6.10.3p8.
780  LexUnexpandedToken(Result);
781 
782  // C99 6.10.3p11: Is this preprocessor directive in macro invocation? e.g.:
783  // #define A(x) #x
784  // A(abc
785  // #warning blah
786  // def)
787  // If so, the user is relying on undefined behavior, emit a diagnostic. Do
788  // not support this for #include-like directives, since that can result in
789  // terrible diagnostics, and does not work in GCC.
790  if (InMacroArgs) {
791  if (IdentifierInfo *II = Result.getIdentifierInfo()) {
792  switch (II->getPPKeywordID()) {
793  case tok::pp_include:
794  case tok::pp_import:
795  case tok::pp_include_next:
796  case tok::pp___include_macros:
797  case tok::pp_pragma:
798  Diag(Result, diag::err_embedded_directive) << II->getName();
800  return;
801  default:
802  break;
803  }
804  }
805  Diag(Result, diag::ext_embedded_directive);
806  }
807 
808  // Temporarily enable macro expansion if set so
809  // and reset to previous state when returning from this function.
810  ResetMacroExpansionHelper helper(this);
811 
812  switch (Result.getKind()) {
813  case tok::eod:
814  return; // null directive.
815  case tok::code_completion:
816  if (CodeComplete)
817  CodeComplete->CodeCompleteDirective(
818  CurPPLexer->getConditionalStackDepth() > 0);
820  return;
821  case tok::numeric_constant: // # 7 GNU line marker directive.
822  if (getLangOpts().AsmPreprocessor)
823  break; // # 4 is not a preprocessor directive in .S files.
824  return HandleDigitDirective(Result);
825  default:
826  IdentifierInfo *II = Result.getIdentifierInfo();
827  if (!II) break; // Not an identifier.
828 
829  // Ask what the preprocessor keyword ID is.
830  switch (II->getPPKeywordID()) {
831  default: break;
832  // C99 6.10.1 - Conditional Inclusion.
833  case tok::pp_if:
834  return HandleIfDirective(Result, ReadAnyTokensBeforeDirective);
835  case tok::pp_ifdef:
836  return HandleIfdefDirective(Result, false, true/*not valid for miopt*/);
837  case tok::pp_ifndef:
838  return HandleIfdefDirective(Result, true, ReadAnyTokensBeforeDirective);
839  case tok::pp_elif:
840  return HandleElifDirective(Result);
841  case tok::pp_else:
842  return HandleElseDirective(Result);
843  case tok::pp_endif:
844  return HandleEndifDirective(Result);
845 
846  // C99 6.10.2 - Source File Inclusion.
847  case tok::pp_include:
848  // Handle #include.
849  return HandleIncludeDirective(SavedHash.getLocation(), Result);
850  case tok::pp___include_macros:
851  // Handle -imacros.
852  return HandleIncludeMacrosDirective(SavedHash.getLocation(), Result);
853 
854  // C99 6.10.3 - Macro Replacement.
855  case tok::pp_define:
856  return HandleDefineDirective(Result, ImmediatelyAfterTopLevelIfndef);
857  case tok::pp_undef:
858  return HandleUndefDirective(Result);
859 
860  // C99 6.10.4 - Line Control.
861  case tok::pp_line:
862  return HandleLineDirective(Result);
863 
864  // C99 6.10.5 - Error Directive.
865  case tok::pp_error:
866  return HandleUserDiagnosticDirective(Result, false);
867 
868  // C99 6.10.6 - Pragma Directive.
869  case tok::pp_pragma:
870  return HandlePragmaDirective(SavedHash.getLocation(), PIK_HashPragma);
871 
872  // GNU Extensions.
873  case tok::pp_import:
874  return HandleImportDirective(SavedHash.getLocation(), Result);
875  case tok::pp_include_next:
876  return HandleIncludeNextDirective(SavedHash.getLocation(), Result);
877 
878  case tok::pp_warning:
879  Diag(Result, diag::ext_pp_warning_directive);
880  return HandleUserDiagnosticDirective(Result, true);
881  case tok::pp_ident:
882  return HandleIdentSCCSDirective(Result);
883  case tok::pp_sccs:
884  return HandleIdentSCCSDirective(Result);
885  case tok::pp_assert:
886  //isExtension = true; // FIXME: implement #assert
887  break;
888  case tok::pp_unassert:
889  //isExtension = true; // FIXME: implement #unassert
890  break;
891 
892  case tok::pp___public_macro:
893  if (getLangOpts().Modules)
894  return HandleMacroPublicDirective(Result);
895  break;
896 
897  case tok::pp___private_macro:
898  if (getLangOpts().Modules)
899  return HandleMacroPrivateDirective(Result);
900  break;
901  }
902  break;
903  }
904 
905  // If this is a .S file, treat unknown # directives as non-preprocessor
906  // directives. This is important because # may be a comment or introduce
907  // various pseudo-ops. Just return the # token and push back the following
908  // token to be lexed next time.
909  if (getLangOpts().AsmPreprocessor) {
910  Token *Toks = new Token[2];
911  // Return the # and the token after it.
912  Toks[0] = SavedHash;
913  Toks[1] = Result;
914 
915  // If the second token is a hashhash token, then we need to translate it to
916  // unknown so the token lexer doesn't try to perform token pasting.
917  if (Result.is(tok::hashhash))
918  Toks[1].setKind(tok::unknown);
919 
920  // Enter this token stream so that we re-lex the tokens. Make sure to
921  // enable macro expansion, in case the token after the # is an identifier
922  // that is expanded.
923  EnterTokenStream(Toks, 2, false, true);
924  return;
925  }
926 
927  // If we reached here, the preprocessing token is not valid!
928  Diag(Result, diag::err_pp_invalid_directive);
929 
930  // Read the rest of the PP line.
932 
933  // Okay, we're done parsing the directive.
934 }
935 
936 /// GetLineValue - Convert a numeric token into an unsigned value, emitting
937 /// Diagnostic DiagID if it is invalid, and returning the value in Val.
938 static bool GetLineValue(Token &DigitTok, unsigned &Val,
939  unsigned DiagID, Preprocessor &PP,
940  bool IsGNULineDirective=false) {
941  if (DigitTok.isNot(tok::numeric_constant)) {
942  PP.Diag(DigitTok, DiagID);
943 
944  if (DigitTok.isNot(tok::eod))
946  return true;
947  }
948 
949  SmallString<64> IntegerBuffer;
950  IntegerBuffer.resize(DigitTok.getLength());
951  const char *DigitTokBegin = &IntegerBuffer[0];
952  bool Invalid = false;
953  unsigned ActualLength = PP.getSpelling(DigitTok, DigitTokBegin, &Invalid);
954  if (Invalid)
955  return true;
956 
957  // Verify that we have a simple digit-sequence, and compute the value. This
958  // is always a simple digit string computed in decimal, so we do this manually
959  // here.
960  Val = 0;
961  for (unsigned i = 0; i != ActualLength; ++i) {
962  // C++1y [lex.fcon]p1:
963  // Optional separating single quotes in a digit-sequence are ignored
964  if (DigitTokBegin[i] == '\'')
965  continue;
966 
967  if (!isDigit(DigitTokBegin[i])) {
968  PP.Diag(PP.AdvanceToTokenCharacter(DigitTok.getLocation(), i),
969  diag::err_pp_line_digit_sequence) << IsGNULineDirective;
971  return true;
972  }
973 
974  unsigned NextVal = Val*10+(DigitTokBegin[i]-'0');
975  if (NextVal < Val) { // overflow.
976  PP.Diag(DigitTok, DiagID);
978  return true;
979  }
980  Val = NextVal;
981  }
982 
983  if (DigitTokBegin[0] == '0' && Val)
984  PP.Diag(DigitTok.getLocation(), diag::warn_pp_line_decimal)
985  << IsGNULineDirective;
986 
987  return false;
988 }
989 
990 /// \brief Handle a \#line directive: C99 6.10.4.
991 ///
992 /// The two acceptable forms are:
993 /// \verbatim
994 /// # line digit-sequence
995 /// # line digit-sequence "s-char-sequence"
996 /// \endverbatim
997 void Preprocessor::HandleLineDirective(Token &Tok) {
998  // Read the line # and string argument. Per C99 6.10.4p5, these tokens are
999  // expanded.
1000  Token DigitTok;
1001  Lex(DigitTok);
1002 
1003  // Validate the number and convert it to an unsigned.
1004  unsigned LineNo;
1005  if (GetLineValue(DigitTok, LineNo, diag::err_pp_line_requires_integer,*this))
1006  return;
1007 
1008  if (LineNo == 0)
1009  Diag(DigitTok, diag::ext_pp_line_zero);
1010 
1011  // Enforce C99 6.10.4p3: "The digit sequence shall not specify ... a
1012  // number greater than 2147483647". C90 requires that the line # be <= 32767.
1013  unsigned LineLimit = 32768U;
1014  if (LangOpts.C99 || LangOpts.CPlusPlus11)
1015  LineLimit = 2147483648U;
1016  if (LineNo >= LineLimit)
1017  Diag(DigitTok, diag::ext_pp_line_too_big) << LineLimit;
1018  else if (LangOpts.CPlusPlus11 && LineNo >= 32768U)
1019  Diag(DigitTok, diag::warn_cxx98_compat_pp_line_too_big);
1020 
1021  int FilenameID = -1;
1022  Token StrTok;
1023  Lex(StrTok);
1024 
1025  // If the StrTok is "eod", then it wasn't present. Otherwise, it must be a
1026  // string followed by eod.
1027  if (StrTok.is(tok::eod))
1028  ; // ok
1029  else if (StrTok.isNot(tok::string_literal)) {
1030  Diag(StrTok, diag::err_pp_line_invalid_filename);
1031  return DiscardUntilEndOfDirective();
1032  } else if (StrTok.hasUDSuffix()) {
1033  Diag(StrTok, diag::err_invalid_string_udl);
1034  return DiscardUntilEndOfDirective();
1035  } else {
1036  // Parse and validate the string, converting it into a unique ID.
1037  StringLiteralParser Literal(StrTok, *this);
1038  assert(Literal.isAscii() && "Didn't allow wide strings in");
1039  if (Literal.hadError)
1040  return DiscardUntilEndOfDirective();
1041  if (Literal.Pascal) {
1042  Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
1043  return DiscardUntilEndOfDirective();
1044  }
1045  FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString());
1046 
1047  // Verify that there is nothing after the string, other than EOD. Because
1048  // of C99 6.10.4p5, macros that expand to empty tokens are ok.
1049  CheckEndOfDirective("line", true);
1050  }
1051 
1052  SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID);
1053 
1054  if (Callbacks)
1055  Callbacks->FileChanged(CurPPLexer->getSourceLocation(),
1057  SrcMgr::C_User);
1058 }
1059 
1060 /// ReadLineMarkerFlags - Parse and validate any flags at the end of a GNU line
1061 /// marker directive.
1062 static bool ReadLineMarkerFlags(bool &IsFileEntry, bool &IsFileExit,
1063  bool &IsSystemHeader, bool &IsExternCHeader,
1064  Preprocessor &PP) {
1065  unsigned FlagVal;
1066  Token FlagTok;
1067  PP.Lex(FlagTok);
1068  if (FlagTok.is(tok::eod)) return false;
1069  if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP))
1070  return true;
1071 
1072  if (FlagVal == 1) {
1073  IsFileEntry = true;
1074 
1075  PP.Lex(FlagTok);
1076  if (FlagTok.is(tok::eod)) return false;
1077  if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP))
1078  return true;
1079  } else if (FlagVal == 2) {
1080  IsFileExit = true;
1081 
1083  // If we are leaving the current presumed file, check to make sure the
1084  // presumed include stack isn't empty!
1085  FileID CurFileID =
1086  SM.getDecomposedExpansionLoc(FlagTok.getLocation()).first;
1087  PresumedLoc PLoc = SM.getPresumedLoc(FlagTok.getLocation());
1088  if (PLoc.isInvalid())
1089  return true;
1090 
1091  // If there is no include loc (main file) or if the include loc is in a
1092  // different physical file, then we aren't in a "1" line marker flag region.
1093  SourceLocation IncLoc = PLoc.getIncludeLoc();
1094  if (IncLoc.isInvalid() ||
1095  SM.getDecomposedExpansionLoc(IncLoc).first != CurFileID) {
1096  PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_pop);
1098  return true;
1099  }
1100 
1101  PP.Lex(FlagTok);
1102  if (FlagTok.is(tok::eod)) return false;
1103  if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP))
1104  return true;
1105  }
1106 
1107  // We must have 3 if there are still flags.
1108  if (FlagVal != 3) {
1109  PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
1111  return true;
1112  }
1113 
1114  IsSystemHeader = true;
1115 
1116  PP.Lex(FlagTok);
1117  if (FlagTok.is(tok::eod)) return false;
1118  if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP))
1119  return true;
1120 
1121  // We must have 4 if there is yet another flag.
1122  if (FlagVal != 4) {
1123  PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
1125  return true;
1126  }
1127 
1128  IsExternCHeader = true;
1129 
1130  PP.Lex(FlagTok);
1131  if (FlagTok.is(tok::eod)) return false;
1132 
1133  // There are no more valid flags here.
1134  PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
1136  return true;
1137 }
1138 
1139 /// HandleDigitDirective - Handle a GNU line marker directive, whose syntax is
1140 /// one of the following forms:
1141 ///
1142 /// # 42
1143 /// # 42 "file" ('1' | '2')?
1144 /// # 42 "file" ('1' | '2')? '3' '4'?
1145 ///
1146 void Preprocessor::HandleDigitDirective(Token &DigitTok) {
1147  // Validate the number and convert it to an unsigned. GNU does not have a
1148  // line # limit other than it fit in 32-bits.
1149  unsigned LineNo;
1150  if (GetLineValue(DigitTok, LineNo, diag::err_pp_linemarker_requires_integer,
1151  *this, true))
1152  return;
1153 
1154  Token StrTok;
1155  Lex(StrTok);
1156 
1157  bool IsFileEntry = false, IsFileExit = false;
1158  bool IsSystemHeader = false, IsExternCHeader = false;
1159  int FilenameID = -1;
1160 
1161  // If the StrTok is "eod", then it wasn't present. Otherwise, it must be a
1162  // string followed by eod.
1163  if (StrTok.is(tok::eod))
1164  ; // ok
1165  else if (StrTok.isNot(tok::string_literal)) {
1166  Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
1167  return DiscardUntilEndOfDirective();
1168  } else if (StrTok.hasUDSuffix()) {
1169  Diag(StrTok, diag::err_invalid_string_udl);
1170  return DiscardUntilEndOfDirective();
1171  } else {
1172  // Parse and validate the string, converting it into a unique ID.
1173  StringLiteralParser Literal(StrTok, *this);
1174  assert(Literal.isAscii() && "Didn't allow wide strings in");
1175  if (Literal.hadError)
1176  return DiscardUntilEndOfDirective();
1177  if (Literal.Pascal) {
1178  Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
1179  return DiscardUntilEndOfDirective();
1180  }
1181  FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString());
1182 
1183  // If a filename was present, read any flags that are present.
1184  if (ReadLineMarkerFlags(IsFileEntry, IsFileExit,
1185  IsSystemHeader, IsExternCHeader, *this))
1186  return;
1187  }
1188 
1189  // Create a line note with this information.
1190  SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID,
1191  IsFileEntry, IsFileExit,
1192  IsSystemHeader, IsExternCHeader);
1193 
1194  // If the preprocessor has callbacks installed, notify them of the #line
1195  // change. This is used so that the line marker comes out in -E mode for
1196  // example.
1197  if (Callbacks) {
1199  if (IsFileEntry)
1200  Reason = PPCallbacks::EnterFile;
1201  else if (IsFileExit)
1202  Reason = PPCallbacks::ExitFile;
1204  if (IsExternCHeader)
1205  FileKind = SrcMgr::C_ExternCSystem;
1206  else if (IsSystemHeader)
1207  FileKind = SrcMgr::C_System;
1208 
1209  Callbacks->FileChanged(CurPPLexer->getSourceLocation(), Reason, FileKind);
1210  }
1211 }
1212 
1213 
1214 /// HandleUserDiagnosticDirective - Handle a #warning or #error directive.
1215 ///
1216 void Preprocessor::HandleUserDiagnosticDirective(Token &Tok,
1217  bool isWarning) {
1218  // PTH doesn't emit #warning or #error directives.
1219  if (CurPTHLexer)
1220  return CurPTHLexer->DiscardToEndOfLine();
1221 
1222  // Read the rest of the line raw. We do this because we don't want macros
1223  // to be expanded and we don't require that the tokens be valid preprocessing
1224  // tokens. For example, this is allowed: "#warning ` 'foo". GCC does
1225  // collapse multiple consequtive white space between tokens, but this isn't
1226  // specified by the standard.
1227  SmallString<128> Message;
1228  CurLexer->ReadToEndOfLine(&Message);
1229 
1230  // Find the first non-whitespace character, so that we can make the
1231  // diagnostic more succinct.
1232  StringRef Msg = StringRef(Message).ltrim(" ");
1233 
1234  if (isWarning)
1235  Diag(Tok, diag::pp_hash_warning) << Msg;
1236  else
1237  Diag(Tok, diag::err_pp_hash_error) << Msg;
1238 }
1239 
1240 /// HandleIdentSCCSDirective - Handle a #ident/#sccs directive.
1241 ///
1242 void Preprocessor::HandleIdentSCCSDirective(Token &Tok) {
1243  // Yes, this directive is an extension.
1244  Diag(Tok, diag::ext_pp_ident_directive);
1245 
1246  // Read the string argument.
1247  Token StrTok;
1248  Lex(StrTok);
1249 
1250  // If the token kind isn't a string, it's a malformed directive.
1251  if (StrTok.isNot(tok::string_literal) &&
1252  StrTok.isNot(tok::wide_string_literal)) {
1253  Diag(StrTok, diag::err_pp_malformed_ident);
1254  if (StrTok.isNot(tok::eod))
1256  return;
1257  }
1258 
1259  if (StrTok.hasUDSuffix()) {
1260  Diag(StrTok, diag::err_invalid_string_udl);
1261  return DiscardUntilEndOfDirective();
1262  }
1263 
1264  // Verify that there is nothing after the string, other than EOD.
1265  CheckEndOfDirective("ident");
1266 
1267  if (Callbacks) {
1268  bool Invalid = false;
1269  std::string Str = getSpelling(StrTok, &Invalid);
1270  if (!Invalid)
1271  Callbacks->Ident(Tok.getLocation(), Str);
1272  }
1273 }
1274 
1275 /// \brief Handle a #public directive.
1276 void Preprocessor::HandleMacroPublicDirective(Token &Tok) {
1277  Token MacroNameTok;
1278  ReadMacroName(MacroNameTok, MU_Undef);
1279 
1280  // Error reading macro name? If so, diagnostic already issued.
1281  if (MacroNameTok.is(tok::eod))
1282  return;
1283 
1284  // Check to see if this is the last token on the #__public_macro line.
1285  CheckEndOfDirective("__public_macro");
1286 
1287  IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
1288  // Okay, we finally have a valid identifier to undef.
1290 
1291  // If the macro is not defined, this is an error.
1292  if (!MD) {
1293  Diag(MacroNameTok, diag::err_pp_visibility_non_macro) << II;
1294  return;
1295  }
1296 
1297  // Note that this macro has now been exported.
1298  appendMacroDirective(II, AllocateVisibilityMacroDirective(
1299  MacroNameTok.getLocation(), /*IsPublic=*/true));
1300 }
1301 
1302 /// \brief Handle a #private directive.
1303 void Preprocessor::HandleMacroPrivateDirective(Token &Tok) {
1304  Token MacroNameTok;
1305  ReadMacroName(MacroNameTok, MU_Undef);
1306 
1307  // Error reading macro name? If so, diagnostic already issued.
1308  if (MacroNameTok.is(tok::eod))
1309  return;
1310 
1311  // Check to see if this is the last token on the #__private_macro line.
1312  CheckEndOfDirective("__private_macro");
1313 
1314  IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
1315  // Okay, we finally have a valid identifier to undef.
1317 
1318  // If the macro is not defined, this is an error.
1319  if (!MD) {
1320  Diag(MacroNameTok, diag::err_pp_visibility_non_macro) << II;
1321  return;
1322  }
1323 
1324  // Note that this macro has now been marked private.
1325  appendMacroDirective(II, AllocateVisibilityMacroDirective(
1326  MacroNameTok.getLocation(), /*IsPublic=*/false));
1327 }
1328 
1329 //===----------------------------------------------------------------------===//
1330 // Preprocessor Include Directive Handling.
1331 //===----------------------------------------------------------------------===//
1332 
1333 /// GetIncludeFilenameSpelling - Turn the specified lexer token into a fully
1334 /// checked and spelled filename, e.g. as an operand of \#include. This returns
1335 /// true if the input filename was in <>'s or false if it were in ""'s. The
1336 /// caller is expected to provide a buffer that is large enough to hold the
1337 /// spelling of the filename, but is also expected to handle the case when
1338 /// this method decides to use a different buffer.
1340  StringRef &Buffer) {
1341  // Get the text form of the filename.
1342  assert(!Buffer.empty() && "Can't have tokens with empty spellings!");
1343 
1344  // Make sure the filename is <x> or "x".
1345  bool isAngled;
1346  if (Buffer[0] == '<') {
1347  if (Buffer.back() != '>') {
1348  Diag(Loc, diag::err_pp_expects_filename);
1349  Buffer = StringRef();
1350  return true;
1351  }
1352  isAngled = true;
1353  } else if (Buffer[0] == '"') {
1354  if (Buffer.back() != '"') {
1355  Diag(Loc, diag::err_pp_expects_filename);
1356  Buffer = StringRef();
1357  return true;
1358  }
1359  isAngled = false;
1360  } else {
1361  Diag(Loc, diag::err_pp_expects_filename);
1362  Buffer = StringRef();
1363  return true;
1364  }
1365 
1366  // Diagnose #include "" as invalid.
1367  if (Buffer.size() <= 2) {
1368  Diag(Loc, diag::err_pp_empty_filename);
1369  Buffer = StringRef();
1370  return true;
1371  }
1372 
1373  // Skip the brackets.
1374  Buffer = Buffer.substr(1, Buffer.size()-2);
1375  return isAngled;
1376 }
1377 
1378 // \brief Handle cases where the \#include name is expanded from a macro
1379 // as multiple tokens, which need to be glued together.
1380 //
1381 // This occurs for code like:
1382 // \code
1383 // \#define FOO <a/b.h>
1384 // \#include FOO
1385 // \endcode
1386 // because in this case, "<a/b.h>" is returned as 7 tokens, not one.
1387 //
1388 // This code concatenates and consumes tokens up to the '>' token. It returns
1389 // false if the > was found, otherwise it returns true if it finds and consumes
1390 // the EOD marker.
1392  SourceLocation &End) {
1393  Token CurTok;
1394 
1395  Lex(CurTok);
1396  while (CurTok.isNot(tok::eod)) {
1397  End = CurTok.getLocation();
1398 
1399  // FIXME: Provide code completion for #includes.
1400  if (CurTok.is(tok::code_completion)) {
1402  Lex(CurTok);
1403  continue;
1404  }
1405 
1406  // Append the spelling of this token to the buffer. If there was a space
1407  // before it, add it now.
1408  if (CurTok.hasLeadingSpace())
1409  FilenameBuffer.push_back(' ');
1410 
1411  // Get the spelling of the token, directly into FilenameBuffer if possible.
1412  unsigned PreAppendSize = FilenameBuffer.size();
1413  FilenameBuffer.resize(PreAppendSize+CurTok.getLength());
1414 
1415  const char *BufPtr = &FilenameBuffer[PreAppendSize];
1416  unsigned ActualLen = getSpelling(CurTok, BufPtr);
1417 
1418  // If the token was spelled somewhere else, copy it into FilenameBuffer.
1419  if (BufPtr != &FilenameBuffer[PreAppendSize])
1420  memcpy(&FilenameBuffer[PreAppendSize], BufPtr, ActualLen);
1421 
1422  // Resize FilenameBuffer to the correct size.
1423  if (CurTok.getLength() != ActualLen)
1424  FilenameBuffer.resize(PreAppendSize+ActualLen);
1425 
1426  // If we found the '>' marker, return success.
1427  if (CurTok.is(tok::greater))
1428  return false;
1429 
1430  Lex(CurTok);
1431  }
1432 
1433  // If we hit the eod marker, emit an error and return true so that the caller
1434  // knows the EOD has been read.
1435  Diag(CurTok.getLocation(), diag::err_pp_expects_filename);
1436  return true;
1437 }
1438 
1439 /// \brief Push a token onto the token stream containing an annotation.
1442  tok::TokenKind Kind, void *AnnotationVal) {
1443  // FIXME: Produce this as the current token directly, rather than
1444  // allocating a new token for it.
1445  Token *Tok = new Token[1];
1446  Tok[0].startToken();
1447  Tok[0].setKind(Kind);
1448  Tok[0].setLocation(Begin);
1449  Tok[0].setAnnotationEndLoc(End);
1450  Tok[0].setAnnotationValue(AnnotationVal);
1451  PP.EnterTokenStream(Tok, 1, true, true);
1452 }
1453 
1454 /// \brief Produce a diagnostic informing the user that a #include or similar
1455 /// was implicitly treated as a module import.
1457  Preprocessor &PP, SourceLocation HashLoc, Token &IncludeTok,
1458  ArrayRef<std::pair<IdentifierInfo *, SourceLocation>> Path,
1459  SourceLocation PathEnd) {
1460  assert(PP.getLangOpts().ObjC2 && "no import syntax available");
1461 
1462  SmallString<128> PathString;
1463  for (unsigned I = 0, N = Path.size(); I != N; ++I) {
1464  if (I)
1465  PathString += '.';
1466  PathString += Path[I].first->getName();
1467  }
1468  int IncludeKind = 0;
1469 
1470  switch (IncludeTok.getIdentifierInfo()->getPPKeywordID()) {
1471  case tok::pp_include:
1472  IncludeKind = 0;
1473  break;
1474 
1475  case tok::pp_import:
1476  IncludeKind = 1;
1477  break;
1478 
1479  case tok::pp_include_next:
1480  IncludeKind = 2;
1481  break;
1482 
1483  case tok::pp___include_macros:
1484  IncludeKind = 3;
1485  break;
1486 
1487  default:
1488  llvm_unreachable("unknown include directive kind");
1489  }
1490 
1491  CharSourceRange ReplaceRange(SourceRange(HashLoc, PathEnd),
1492  /*IsTokenRange=*/false);
1493  PP.Diag(HashLoc, diag::warn_auto_module_import)
1494  << IncludeKind << PathString
1495  << FixItHint::CreateReplacement(ReplaceRange,
1496  ("@import " + PathString + ";").str());
1497 }
1498 
1499 /// HandleIncludeDirective - The "\#include" tokens have just been read, read
1500 /// the file to be included from the lexer, then include it! This is a common
1501 /// routine with functionality shared between \#include, \#include_next and
1502 /// \#import. LookupFrom is set when this is a \#include_next directive, it
1503 /// specifies the file to start searching from.
1504 void Preprocessor::HandleIncludeDirective(SourceLocation HashLoc,
1505  Token &IncludeTok,
1506  const DirectoryLookup *LookupFrom,
1507  const FileEntry *LookupFromFile,
1508  bool isImport) {
1509 
1510  Token FilenameTok;
1511  CurPPLexer->LexIncludeFilename(FilenameTok);
1512 
1513  // Reserve a buffer to get the spelling.
1514  SmallString<128> FilenameBuffer;
1515  StringRef Filename;
1517  SourceLocation CharEnd; // the end of this directive, in characters
1518 
1519  switch (FilenameTok.getKind()) {
1520  case tok::eod:
1521  // If the token kind is EOD, the error has already been diagnosed.
1522  return;
1523 
1524  case tok::angle_string_literal:
1525  case tok::string_literal:
1526  Filename = getSpelling(FilenameTok, FilenameBuffer);
1527  End = FilenameTok.getLocation();
1528  CharEnd = End.getLocWithOffset(FilenameTok.getLength());
1529  break;
1530 
1531  case tok::less:
1532  // This could be a <foo/bar.h> file coming from a macro expansion. In this
1533  // case, glue the tokens together into FilenameBuffer and interpret those.
1534  FilenameBuffer.push_back('<');
1535  if (ConcatenateIncludeName(FilenameBuffer, End))
1536  return; // Found <eod> but no ">"? Diagnostic already emitted.
1537  Filename = FilenameBuffer;
1538  CharEnd = End.getLocWithOffset(1);
1539  break;
1540  default:
1541  Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
1543  return;
1544  }
1545 
1546  CharSourceRange FilenameRange
1547  = CharSourceRange::getCharRange(FilenameTok.getLocation(), CharEnd);
1548  StringRef OriginalFilename = Filename;
1549  bool isAngled =
1551  // If GetIncludeFilenameSpelling set the start ptr to null, there was an
1552  // error.
1553  if (Filename.empty()) {
1555  return;
1556  }
1557 
1558  // Verify that there is nothing after the filename, other than EOD. Note that
1559  // we allow macros that expand to nothing after the filename, because this
1560  // falls into the category of "#include pp-tokens new-line" specified in
1561  // C99 6.10.2p4.
1562  CheckEndOfDirective(IncludeTok.getIdentifierInfo()->getNameStart(), true);
1563 
1564  // Check that we don't have infinite #include recursion.
1565  if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1) {
1566  Diag(FilenameTok, diag::err_pp_include_too_deep);
1567  return;
1568  }
1569 
1570  // Complain about attempts to #include files in an audit pragma.
1571  if (PragmaARCCFCodeAuditedLoc.isValid()) {
1572  Diag(HashLoc, diag::err_pp_include_in_arc_cf_code_audited);
1573  Diag(PragmaARCCFCodeAuditedLoc, diag::note_pragma_entered_here);
1574 
1575  // Immediately leave the pragma.
1576  PragmaARCCFCodeAuditedLoc = SourceLocation();
1577  }
1578 
1579  // Complain about attempts to #include files in an assume-nonnull pragma.
1580  if (PragmaAssumeNonNullLoc.isValid()) {
1581  Diag(HashLoc, diag::err_pp_include_in_assume_nonnull);
1582  Diag(PragmaAssumeNonNullLoc, diag::note_pragma_entered_here);
1583 
1584  // Immediately leave the pragma.
1585  PragmaAssumeNonNullLoc = SourceLocation();
1586  }
1587 
1588  if (HeaderInfo.HasIncludeAliasMap()) {
1589  // Map the filename with the brackets still attached. If the name doesn't
1590  // map to anything, fall back on the filename we've already gotten the
1591  // spelling for.
1592  StringRef NewName = HeaderInfo.MapHeaderToIncludeAlias(OriginalFilename);
1593  if (!NewName.empty())
1594  Filename = NewName;
1595  }
1596 
1597  // Search include directories.
1598  const DirectoryLookup *CurDir;
1599  SmallString<1024> SearchPath;
1600  SmallString<1024> RelativePath;
1601  // We get the raw path only if we have 'Callbacks' to which we later pass
1602  // the path.
1603  ModuleMap::KnownHeader SuggestedModule;
1604  SourceLocation FilenameLoc = FilenameTok.getLocation();
1605  SmallString<128> NormalizedPath;
1606  if (LangOpts.MSVCCompat) {
1607  NormalizedPath = Filename.str();
1608 #ifndef LLVM_ON_WIN32
1609  llvm::sys::path::native(NormalizedPath);
1610 #endif
1611  }
1612  const FileEntry *File = LookupFile(
1613  FilenameLoc, LangOpts.MSVCCompat ? NormalizedPath.c_str() : Filename,
1614  isAngled, LookupFrom, LookupFromFile, CurDir,
1615  Callbacks ? &SearchPath : nullptr, Callbacks ? &RelativePath : nullptr,
1616  &SuggestedModule);
1617 
1618  if (!File) {
1619  if (Callbacks) {
1620  // Give the clients a chance to recover.
1621  SmallString<128> RecoveryPath;
1622  if (Callbacks->FileNotFound(Filename, RecoveryPath)) {
1623  if (const DirectoryEntry *DE = FileMgr.getDirectory(RecoveryPath)) {
1624  // Add the recovery path to the list of search paths.
1625  DirectoryLookup DL(DE, SrcMgr::C_User, false);
1626  HeaderInfo.AddSearchPath(DL, isAngled);
1627 
1628  // Try the lookup again, skipping the cache.
1629  File = LookupFile(
1630  FilenameLoc,
1631  LangOpts.MSVCCompat ? NormalizedPath.c_str() : Filename, isAngled,
1632  LookupFrom, LookupFromFile, CurDir, nullptr, nullptr,
1633  &SuggestedModule, /*SkipCache*/ true);
1634  }
1635  }
1636  }
1637 
1638  if (!SuppressIncludeNotFoundError) {
1639  // If the file could not be located and it was included via angle
1640  // brackets, we can attempt a lookup as though it were a quoted path to
1641  // provide the user with a possible fixit.
1642  if (isAngled) {
1643  File = LookupFile(
1644  FilenameLoc,
1645  LangOpts.MSVCCompat ? NormalizedPath.c_str() : Filename, false,
1646  LookupFrom, LookupFromFile, CurDir,
1647  Callbacks ? &SearchPath : nullptr,
1648  Callbacks ? &RelativePath : nullptr,
1649  &SuggestedModule);
1650  if (File) {
1651  SourceRange Range(FilenameTok.getLocation(), CharEnd);
1652  Diag(FilenameTok, diag::err_pp_file_not_found_not_fatal) <<
1653  Filename <<
1654  FixItHint::CreateReplacement(Range, "\"" + Filename.str() + "\"");
1655  }
1656  }
1657 
1658  // If the file is still not found, just go with the vanilla diagnostic
1659  if (!File)
1660  Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
1661  }
1662  }
1663 
1664  // Should we enter the source file? Set to false if either the source file is
1665  // known to have no effect beyond its effect on module visibility -- that is,
1666  // if it's got an include guard that is already defined or is a modular header
1667  // we've imported or already built.
1668  bool ShouldEnter = true;
1669 
1670  // Determine whether we should try to import the module for this #include, if
1671  // there is one. Don't do so if precompiled module support is disabled or we
1672  // are processing this module textually (because we're building the module).
1673  if (File && SuggestedModule && getLangOpts().Modules &&
1674  SuggestedModule.getModule()->getTopLevelModuleName() !=
1676  SuggestedModule.getModule()->getTopLevelModuleName() !=
1678 
1679  // If this include corresponds to a module but that module is
1680  // unavailable, diagnose the situation and bail out.
1681  if (!SuggestedModule.getModule()->isAvailable()) {
1682  clang::Module::Requirement Requirement;
1684  Module *M = SuggestedModule.getModule();
1685  // Identify the cause.
1686  (void)M->isAvailable(getLangOpts(), getTargetInfo(), Requirement,
1687  MissingHeader);
1688  if (MissingHeader.FileNameLoc.isValid()) {
1689  Diag(MissingHeader.FileNameLoc, diag::err_module_header_missing)
1690  << MissingHeader.IsUmbrella << MissingHeader.FileName;
1691  } else {
1692  Diag(M->DefinitionLoc, diag::err_module_unavailable)
1693  << M->getFullModuleName() << Requirement.second << Requirement.first;
1694  }
1695  Diag(FilenameTok.getLocation(),
1696  diag::note_implicit_top_level_module_import_here)
1697  << M->getTopLevelModuleName();
1698  return;
1699  }
1700 
1701  // Compute the module access path corresponding to this module.
1702  // FIXME: Should we have a second loadModule() overload to avoid this
1703  // extra lookup step?
1705  for (Module *Mod = SuggestedModule.getModule(); Mod; Mod = Mod->Parent)
1706  Path.push_back(std::make_pair(getIdentifierInfo(Mod->Name),
1707  FilenameTok.getLocation()));
1708  std::reverse(Path.begin(), Path.end());
1709 
1710  // Warn that we're replacing the include/import with a module import.
1711  // We only do this in Objective-C, where we have a module-import syntax.
1712  if (getLangOpts().ObjC2)
1713  diagnoseAutoModuleImport(*this, HashLoc, IncludeTok, Path, CharEnd);
1714 
1715  // Load the module to import its macros. We'll make the declarations
1716  // visible when the parser gets here.
1717  // FIXME: Pass SuggestedModule in here rather than converting it to a path
1718  // and making the module loader convert it back again.
1719  ModuleLoadResult Imported = TheModuleLoader.loadModule(
1720  IncludeTok.getLocation(), Path, Module::Hidden,
1721  /*IsIncludeDirective=*/true);
1722  assert((Imported == nullptr || Imported == SuggestedModule.getModule()) &&
1723  "the imported module is different than the suggested one");
1724 
1725  if (Imported)
1726  ShouldEnter = false;
1727  else if (Imported.isMissingExpected()) {
1728  // We failed to find a submodule that we assumed would exist (because it
1729  // was in the directory of an umbrella header, for instance), but no
1730  // actual module exists for it (because the umbrella header is
1731  // incomplete). Treat this as a textual inclusion.
1732  SuggestedModule = ModuleMap::KnownHeader();
1733  } else {
1734  // We hit an error processing the import. Bail out.
1736  // With a fatal failure in the module loader, we abort parsing.
1737  Token &Result = IncludeTok;
1738  if (CurLexer) {
1739  Result.startToken();
1740  CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd, tok::eof);
1741  CurLexer->cutOffLexing();
1742  } else {
1743  assert(CurPTHLexer && "#include but no current lexer set!");
1744  CurPTHLexer->getEOF(Result);
1745  }
1746  }
1747  return;
1748  }
1749  }
1750 
1751  if (Callbacks) {
1752  // Notify the callback object that we've seen an inclusion directive.
1753  Callbacks->InclusionDirective(
1754  HashLoc, IncludeTok,
1755  LangOpts.MSVCCompat ? NormalizedPath.c_str() : Filename, isAngled,
1756  FilenameRange, File, SearchPath, RelativePath,
1757  ShouldEnter ? nullptr : SuggestedModule.getModule());
1758  }
1759 
1760  if (!File)
1761  return;
1762 
1763  // The #included file will be considered to be a system header if either it is
1764  // in a system include directory, or if the #includer is a system include
1765  // header.
1766  SrcMgr::CharacteristicKind FileCharacter =
1767  std::max(HeaderInfo.getFileDirFlavor(File),
1768  SourceMgr.getFileCharacteristic(FilenameTok.getLocation()));
1769 
1770  // FIXME: If we have a suggested module, and we've already visited this file,
1771  // don't bother entering it again. We know it has no further effect.
1772 
1773  // Ask HeaderInfo if we should enter this #include file. If not, #including
1774  // this file will have no effect.
1775  if (ShouldEnter &&
1776  !HeaderInfo.ShouldEnterIncludeFile(*this, File, isImport,
1777  SuggestedModule.getModule())) {
1778  ShouldEnter = false;
1779  if (Callbacks)
1780  Callbacks->FileSkipped(*File, FilenameTok, FileCharacter);
1781  }
1782 
1783  // If we don't need to enter the file, stop now.
1784  if (!ShouldEnter) {
1785  // If this is a module import, make it visible if needed.
1786  if (auto *M = SuggestedModule.getModule()) {
1787  makeModuleVisible(M, HashLoc);
1788 
1789  if (IncludeTok.getIdentifierInfo()->getPPKeywordID() !=
1790  tok::pp___include_macros)
1791  EnterAnnotationToken(*this, HashLoc, End, tok::annot_module_include, M);
1792  }
1793  return;
1794  }
1795 
1796  // Look up the file, create a File ID for it.
1797  SourceLocation IncludePos = End;
1798  // If the filename string was the result of macro expansions, set the include
1799  // position on the file where it will be included and after the expansions.
1800  if (IncludePos.isMacroID())
1801  IncludePos = SourceMgr.getExpansionRange(IncludePos).second;
1802  FileID FID = SourceMgr.createFileID(File, IncludePos, FileCharacter);
1803  assert(FID.isValid() && "Expected valid file ID");
1804 
1805  // If all is good, enter the new file!
1806  if (EnterSourceFile(FID, CurDir, FilenameTok.getLocation()))
1807  return;
1808 
1809  // Determine if we're switching to building a new submodule, and which one.
1810  if (auto *M = SuggestedModule.getModule()) {
1811  assert(!CurSubmodule && "should not have marked this as a module yet");
1812  CurSubmodule = M;
1813 
1814  // Let the macro handling code know that any future macros are within
1815  // the new submodule.
1816  EnterSubmodule(M, HashLoc);
1817 
1818  // Let the parser know that any future declarations are within the new
1819  // submodule.
1820  // FIXME: There's no point doing this if we're handling a #__include_macros
1821  // directive.
1822  EnterAnnotationToken(*this, HashLoc, End, tok::annot_module_begin, M);
1823  }
1824 }
1825 
1826 /// HandleIncludeNextDirective - Implements \#include_next.
1827 ///
1828 void Preprocessor::HandleIncludeNextDirective(SourceLocation HashLoc,
1829  Token &IncludeNextTok) {
1830  Diag(IncludeNextTok, diag::ext_pp_include_next_directive);
1831 
1832  // #include_next is like #include, except that we start searching after
1833  // the current found directory. If we can't do this, issue a
1834  // diagnostic.
1835  const DirectoryLookup *Lookup = CurDirLookup;
1836  const FileEntry *LookupFromFile = nullptr;
1837  if (isInPrimaryFile()) {
1838  Lookup = nullptr;
1839  Diag(IncludeNextTok, diag::pp_include_next_in_primary);
1840  } else if (CurSubmodule) {
1841  // Start looking up in the directory *after* the one in which the current
1842  // file would be found, if any.
1843  assert(CurPPLexer && "#include_next directive in macro?");
1844  LookupFromFile = CurPPLexer->getFileEntry();
1845  Lookup = nullptr;
1846  } else if (!Lookup) {
1847  Diag(IncludeNextTok, diag::pp_include_next_absolute_path);
1848  } else {
1849  // Start looking up in the next directory.
1850  ++Lookup;
1851  }
1852 
1853  return HandleIncludeDirective(HashLoc, IncludeNextTok, Lookup,
1854  LookupFromFile);
1855 }
1856 
1857 /// HandleMicrosoftImportDirective - Implements \#import for Microsoft Mode
1858 void Preprocessor::HandleMicrosoftImportDirective(Token &Tok) {
1859  // The Microsoft #import directive takes a type library and generates header
1860  // files from it, and includes those. This is beyond the scope of what clang
1861  // does, so we ignore it and error out. However, #import can optionally have
1862  // trailing attributes that span multiple lines. We're going to eat those
1863  // so we can continue processing from there.
1864  Diag(Tok, diag::err_pp_import_directive_ms );
1865 
1866  // Read tokens until we get to the end of the directive. Note that the
1867  // directive can be split over multiple lines using the backslash character.
1869 }
1870 
1871 /// HandleImportDirective - Implements \#import.
1872 ///
1873 void Preprocessor::HandleImportDirective(SourceLocation HashLoc,
1874  Token &ImportTok) {
1875  if (!LangOpts.ObjC1) { // #import is standard for ObjC.
1876  if (LangOpts.MSVCCompat)
1877  return HandleMicrosoftImportDirective(ImportTok);
1878  Diag(ImportTok, diag::ext_pp_import_directive);
1879  }
1880  return HandleIncludeDirective(HashLoc, ImportTok, nullptr, nullptr, true);
1881 }
1882 
1883 /// HandleIncludeMacrosDirective - The -imacros command line option turns into a
1884 /// pseudo directive in the predefines buffer. This handles it by sucking all
1885 /// tokens through the preprocessor and discarding them (only keeping the side
1886 /// effects on the preprocessor).
1887 void Preprocessor::HandleIncludeMacrosDirective(SourceLocation HashLoc,
1888  Token &IncludeMacrosTok) {
1889  // This directive should only occur in the predefines buffer. If not, emit an
1890  // error and reject it.
1891  SourceLocation Loc = IncludeMacrosTok.getLocation();
1892  if (strcmp(SourceMgr.getBufferName(Loc), "<built-in>") != 0) {
1893  Diag(IncludeMacrosTok.getLocation(),
1894  diag::pp_include_macros_out_of_predefines);
1896  return;
1897  }
1898 
1899  // Treat this as a normal #include for checking purposes. If this is
1900  // successful, it will push a new lexer onto the include stack.
1901  HandleIncludeDirective(HashLoc, IncludeMacrosTok);
1902 
1903  Token TmpTok;
1904  do {
1905  Lex(TmpTok);
1906  assert(TmpTok.isNot(tok::eof) && "Didn't find end of -imacros!");
1907  } while (TmpTok.isNot(tok::hashhash));
1908 }
1909 
1910 //===----------------------------------------------------------------------===//
1911 // Preprocessor Macro Directive Handling.
1912 //===----------------------------------------------------------------------===//
1913 
1914 /// ReadMacroDefinitionArgList - The ( starting an argument list of a macro
1915 /// definition has just been read. Lex the rest of the arguments and the
1916 /// closing ), updating MI with what we learn. Return true if an error occurs
1917 /// parsing the arg list.
1918 bool Preprocessor::ReadMacroDefinitionArgList(MacroInfo *MI, Token &Tok) {
1920 
1921  while (1) {
1922  LexUnexpandedToken(Tok);
1923  switch (Tok.getKind()) {
1924  case tok::r_paren:
1925  // Found the end of the argument list.
1926  if (Arguments.empty()) // #define FOO()
1927  return false;
1928  // Otherwise we have #define FOO(A,)
1929  Diag(Tok, diag::err_pp_expected_ident_in_arg_list);
1930  return true;
1931  case tok::ellipsis: // #define X(... -> C99 varargs
1932  if (!LangOpts.C99)
1933  Diag(Tok, LangOpts.CPlusPlus11 ?
1934  diag::warn_cxx98_compat_variadic_macro :
1935  diag::ext_variadic_macro);
1936 
1937  // OpenCL v1.2 s6.9.e: variadic macros are not supported.
1938  if (LangOpts.OpenCL) {
1939  Diag(Tok, diag::err_pp_opencl_variadic_macros);
1940  return true;
1941  }
1942 
1943  // Lex the token after the identifier.
1944  LexUnexpandedToken(Tok);
1945  if (Tok.isNot(tok::r_paren)) {
1946  Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
1947  return true;
1948  }
1949  // Add the __VA_ARGS__ identifier as an argument.
1950  Arguments.push_back(Ident__VA_ARGS__);
1951  MI->setIsC99Varargs();
1952  MI->setArgumentList(Arguments, BP);
1953  return false;
1954  case tok::eod: // #define X(
1955  Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
1956  return true;
1957  default:
1958  // Handle keywords and identifiers here to accept things like
1959  // #define Foo(for) for.
1960  IdentifierInfo *II = Tok.getIdentifierInfo();
1961  if (!II) {
1962  // #define X(1
1963  Diag(Tok, diag::err_pp_invalid_tok_in_arg_list);
1964  return true;
1965  }
1966 
1967  // If this is already used as an argument, it is used multiple times (e.g.
1968  // #define X(A,A.
1969  if (std::find(Arguments.begin(), Arguments.end(), II) !=
1970  Arguments.end()) { // C99 6.10.3p6
1971  Diag(Tok, diag::err_pp_duplicate_name_in_arg_list) << II;
1972  return true;
1973  }
1974 
1975  // Add the argument to the macro info.
1976  Arguments.push_back(II);
1977 
1978  // Lex the token after the identifier.
1979  LexUnexpandedToken(Tok);
1980 
1981  switch (Tok.getKind()) {
1982  default: // #define X(A B
1983  Diag(Tok, diag::err_pp_expected_comma_in_arg_list);
1984  return true;
1985  case tok::r_paren: // #define X(A)
1986  MI->setArgumentList(Arguments, BP);
1987  return false;
1988  case tok::comma: // #define X(A,
1989  break;
1990  case tok::ellipsis: // #define X(A... -> GCC extension
1991  // Diagnose extension.
1992  Diag(Tok, diag::ext_named_variadic_macro);
1993 
1994  // Lex the token after the identifier.
1995  LexUnexpandedToken(Tok);
1996  if (Tok.isNot(tok::r_paren)) {
1997  Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
1998  return true;
1999  }
2000 
2001  MI->setIsGNUVarargs();
2002  MI->setArgumentList(Arguments, BP);
2003  return false;
2004  }
2005  }
2006  }
2007 }
2008 
2009 static bool isConfigurationPattern(Token &MacroName, MacroInfo *MI,
2010  const LangOptions &LOptions) {
2011  if (MI->getNumTokens() == 1) {
2012  const Token &Value = MI->getReplacementToken(0);
2013 
2014  // Macro that is identity, like '#define inline inline' is a valid pattern.
2015  if (MacroName.getKind() == Value.getKind())
2016  return true;
2017 
2018  // Macro that maps a keyword to the same keyword decorated with leading/
2019  // trailing underscores is a valid pattern:
2020  // #define inline __inline
2021  // #define inline __inline__
2022  // #define inline _inline (in MS compatibility mode)
2023  StringRef MacroText = MacroName.getIdentifierInfo()->getName();
2024  if (IdentifierInfo *II = Value.getIdentifierInfo()) {
2025  if (!II->isKeyword(LOptions))
2026  return false;
2027  StringRef ValueText = II->getName();
2028  StringRef TrimmedValue = ValueText;
2029  if (!ValueText.startswith("__")) {
2030  if (ValueText.startswith("_"))
2031  TrimmedValue = TrimmedValue.drop_front(1);
2032  else
2033  return false;
2034  } else {
2035  TrimmedValue = TrimmedValue.drop_front(2);
2036  if (TrimmedValue.endswith("__"))
2037  TrimmedValue = TrimmedValue.drop_back(2);
2038  }
2039  return TrimmedValue.equals(MacroText);
2040  } else {
2041  return false;
2042  }
2043  }
2044 
2045  // #define inline
2046  return MacroName.isOneOf(tok::kw_extern, tok::kw_inline, tok::kw_static,
2047  tok::kw_const) &&
2048  MI->getNumTokens() == 0;
2049 }
2050 
2051 /// HandleDefineDirective - Implements \#define. This consumes the entire macro
2052 /// line then lets the caller lex the next real token.
2053 void Preprocessor::HandleDefineDirective(Token &DefineTok,
2054  bool ImmediatelyAfterHeaderGuard) {
2055  ++NumDefined;
2056 
2057  Token MacroNameTok;
2058  bool MacroShadowsKeyword;
2059  ReadMacroName(MacroNameTok, MU_Define, &MacroShadowsKeyword);
2060 
2061  // Error reading macro name? If so, diagnostic already issued.
2062  if (MacroNameTok.is(tok::eod))
2063  return;
2064 
2065  Token LastTok = MacroNameTok;
2066 
2067  // If we are supposed to keep comments in #defines, reenable comment saving
2068  // mode.
2069  if (CurLexer) CurLexer->SetCommentRetentionState(KeepMacroComments);
2070 
2071  // Create the new macro.
2072  MacroInfo *MI = AllocateMacroInfo(MacroNameTok.getLocation());
2073 
2074  Token Tok;
2075  LexUnexpandedToken(Tok);
2076 
2077  // If this is a function-like macro definition, parse the argument list,
2078  // marking each of the identifiers as being used as macro arguments. Also,
2079  // check other constraints on the first token of the macro body.
2080  if (Tok.is(tok::eod)) {
2081  if (ImmediatelyAfterHeaderGuard) {
2082  // Save this macro information since it may part of a header guard.
2083  CurPPLexer->MIOpt.SetDefinedMacro(MacroNameTok.getIdentifierInfo(),
2084  MacroNameTok.getLocation());
2085  }
2086  // If there is no body to this macro, we have no special handling here.
2087  } else if (Tok.hasLeadingSpace()) {
2088  // This is a normal token with leading space. Clear the leading space
2089  // marker on the first token to get proper expansion.
2090  Tok.clearFlag(Token::LeadingSpace);
2091  } else if (Tok.is(tok::l_paren)) {
2092  // This is a function-like macro definition. Read the argument list.
2093  MI->setIsFunctionLike();
2094  if (ReadMacroDefinitionArgList(MI, LastTok)) {
2095  // Throw away the rest of the line.
2096  if (CurPPLexer->ParsingPreprocessorDirective)
2098  return;
2099  }
2100 
2101  // If this is a definition of a variadic C99 function-like macro, not using
2102  // the GNU named varargs extension, enabled __VA_ARGS__.
2103 
2104  // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro.
2105  // This gets unpoisoned where it is allowed.
2106  assert(Ident__VA_ARGS__->isPoisoned() && "__VA_ARGS__ should be poisoned!");
2107  if (MI->isC99Varargs())
2108  Ident__VA_ARGS__->setIsPoisoned(false);
2109 
2110  // Read the first token after the arg list for down below.
2111  LexUnexpandedToken(Tok);
2112  } else if (LangOpts.C99 || LangOpts.CPlusPlus11) {
2113  // C99 requires whitespace between the macro definition and the body. Emit
2114  // a diagnostic for something like "#define X+".
2115  Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name);
2116  } else {
2117  // C90 6.8 TC1 says: "In the definition of an object-like macro, if the
2118  // first character of a replacement list is not a character required by
2119  // subclause 5.2.1, then there shall be white-space separation between the
2120  // identifier and the replacement list.". 5.2.1 lists this set:
2121  // "A-Za-z0-9!"#%&'()*+,_./:;<=>?[\]^_{|}~" as well as whitespace, which
2122  // is irrelevant here.
2123  bool isInvalid = false;
2124  if (Tok.is(tok::at)) // @ is not in the list above.
2125  isInvalid = true;
2126  else if (Tok.is(tok::unknown)) {
2127  // If we have an unknown token, it is something strange like "`". Since
2128  // all of valid characters would have lexed into a single character
2129  // token of some sort, we know this is not a valid case.
2130  isInvalid = true;
2131  }
2132  if (isInvalid)
2133  Diag(Tok, diag::ext_missing_whitespace_after_macro_name);
2134  else
2135  Diag(Tok, diag::warn_missing_whitespace_after_macro_name);
2136  }
2137 
2138  if (!Tok.is(tok::eod))
2139  LastTok = Tok;
2140 
2141  // Read the rest of the macro body.
2142  if (MI->isObjectLike()) {
2143  // Object-like macros are very simple, just read their body.
2144  while (Tok.isNot(tok::eod)) {
2145  LastTok = Tok;
2146  MI->AddTokenToBody(Tok);
2147  // Get the next token of the macro.
2148  LexUnexpandedToken(Tok);
2149  }
2150 
2151  } else {
2152  // Otherwise, read the body of a function-like macro. While we are at it,
2153  // check C99 6.10.3.2p1: ensure that # operators are followed by macro
2154  // parameters in function-like macro expansions.
2155  while (Tok.isNot(tok::eod)) {
2156  LastTok = Tok;
2157 
2158  if (Tok.isNot(tok::hash) && Tok.isNot(tok::hashhash)) {
2159  MI->AddTokenToBody(Tok);
2160 
2161  // Get the next token of the macro.
2162  LexUnexpandedToken(Tok);
2163  continue;
2164  }
2165 
2166  // If we're in -traditional mode, then we should ignore stringification
2167  // and token pasting. Mark the tokens as unknown so as not to confuse
2168  // things.
2169  if (getLangOpts().TraditionalCPP) {
2170  Tok.setKind(tok::unknown);
2171  MI->AddTokenToBody(Tok);
2172 
2173  // Get the next token of the macro.
2174  LexUnexpandedToken(Tok);
2175  continue;
2176  }
2177 
2178  if (Tok.is(tok::hashhash)) {
2179 
2180  // If we see token pasting, check if it looks like the gcc comma
2181  // pasting extension. We'll use this information to suppress
2182  // diagnostics later on.
2183 
2184  // Get the next token of the macro.
2185  LexUnexpandedToken(Tok);
2186 
2187  if (Tok.is(tok::eod)) {
2188  MI->AddTokenToBody(LastTok);
2189  break;
2190  }
2191 
2192  unsigned NumTokens = MI->getNumTokens();
2193  if (NumTokens && Tok.getIdentifierInfo() == Ident__VA_ARGS__ &&
2194  MI->getReplacementToken(NumTokens-1).is(tok::comma))
2195  MI->setHasCommaPasting();
2196 
2197  // Things look ok, add the '##' token to the macro.
2198  MI->AddTokenToBody(LastTok);
2199  continue;
2200  }
2201 
2202  // Get the next token of the macro.
2203  LexUnexpandedToken(Tok);
2204 
2205  // Check for a valid macro arg identifier.
2206  if (Tok.getIdentifierInfo() == nullptr ||
2207  MI->getArgumentNum(Tok.getIdentifierInfo()) == -1) {
2208 
2209  // If this is assembler-with-cpp mode, we accept random gibberish after
2210  // the '#' because '#' is often a comment character. However, change
2211  // the kind of the token to tok::unknown so that the preprocessor isn't
2212  // confused.
2213  if (getLangOpts().AsmPreprocessor && Tok.isNot(tok::eod)) {
2214  LastTok.setKind(tok::unknown);
2215  MI->AddTokenToBody(LastTok);
2216  continue;
2217  } else {
2218  Diag(Tok, diag::err_pp_stringize_not_parameter);
2219 
2220  // Disable __VA_ARGS__ again.
2221  Ident__VA_ARGS__->setIsPoisoned(true);
2222  return;
2223  }
2224  }
2225 
2226  // Things look ok, add the '#' and param name tokens to the macro.
2227  MI->AddTokenToBody(LastTok);
2228  MI->AddTokenToBody(Tok);
2229  LastTok = Tok;
2230 
2231  // Get the next token of the macro.
2232  LexUnexpandedToken(Tok);
2233  }
2234  }
2235 
2236  if (MacroShadowsKeyword &&
2237  !isConfigurationPattern(MacroNameTok, MI, getLangOpts())) {
2238  Diag(MacroNameTok, diag::warn_pp_macro_hides_keyword);
2239  }
2240 
2241  // Disable __VA_ARGS__ again.
2242  Ident__VA_ARGS__->setIsPoisoned(true);
2243 
2244  // Check that there is no paste (##) operator at the beginning or end of the
2245  // replacement list.
2246  unsigned NumTokens = MI->getNumTokens();
2247  if (NumTokens != 0) {
2248  if (MI->getReplacementToken(0).is(tok::hashhash)) {
2249  Diag(MI->getReplacementToken(0), diag::err_paste_at_start);
2250  return;
2251  }
2252  if (MI->getReplacementToken(NumTokens-1).is(tok::hashhash)) {
2253  Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end);
2254  return;
2255  }
2256  }
2257 
2258  MI->setDefinitionEndLoc(LastTok.getLocation());
2259 
2260  // Finally, if this identifier already had a macro defined for it, verify that
2261  // the macro bodies are identical, and issue diagnostics if they are not.
2262  if (const MacroInfo *OtherMI=getMacroInfo(MacroNameTok.getIdentifierInfo())) {
2263  // In Objective-C, ignore attempts to directly redefine the builtin
2264  // definitions of the ownership qualifiers. It's still possible to
2265  // #undef them.
2266  auto isObjCProtectedMacro = [](const IdentifierInfo *II) -> bool {
2267  return II->isStr("__strong") ||
2268  II->isStr("__weak") ||
2269  II->isStr("__unsafe_unretained") ||
2270  II->isStr("__autoreleasing");
2271  };
2272  if (getLangOpts().ObjC1 &&
2273  SourceMgr.getFileID(OtherMI->getDefinitionLoc())
2274  == getPredefinesFileID() &&
2275  isObjCProtectedMacro(MacroNameTok.getIdentifierInfo())) {
2276  // Warn if it changes the tokens.
2278  !SourceMgr.isInSystemHeader(DefineTok.getLocation())) &&
2279  !MI->isIdenticalTo(*OtherMI, *this,
2280  /*Syntactic=*/LangOpts.MicrosoftExt)) {
2281  Diag(MI->getDefinitionLoc(), diag::warn_pp_objc_macro_redef_ignored);
2282  }
2283  assert(!OtherMI->isWarnIfUnused());
2284  return;
2285  }
2286 
2287  // It is very common for system headers to have tons of macro redefinitions
2288  // and for warnings to be disabled in system headers. If this is the case,
2289  // then don't bother calling MacroInfo::isIdenticalTo.
2291  !SourceMgr.isInSystemHeader(DefineTok.getLocation())) {
2292  if (!OtherMI->isUsed() && OtherMI->isWarnIfUnused())
2293  Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used);
2294 
2295  // Warn if defining "__LINE__" and other builtins, per C99 6.10.8/4 and
2296  // C++ [cpp.predefined]p4, but allow it as an extension.
2297  if (OtherMI->isBuiltinMacro())
2298  Diag(MacroNameTok, diag::ext_pp_redef_builtin_macro);
2299  // Macros must be identical. This means all tokens and whitespace
2300  // separation must be the same. C99 6.10.3p2.
2301  else if (!OtherMI->isAllowRedefinitionsWithoutWarning() &&
2302  !MI->isIdenticalTo(*OtherMI, *this, /*Syntactic=*/LangOpts.MicrosoftExt)) {
2303  Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef)
2304  << MacroNameTok.getIdentifierInfo();
2305  Diag(OtherMI->getDefinitionLoc(), diag::note_previous_definition);
2306  }
2307  }
2308  if (OtherMI->isWarnIfUnused())
2309  WarnUnusedMacroLocs.erase(OtherMI->getDefinitionLoc());
2310  }
2311 
2312  DefMacroDirective *MD =
2313  appendDefMacroDirective(MacroNameTok.getIdentifierInfo(), MI);
2314 
2315  assert(!MI->isUsed());
2316  // If we need warning for not using the macro, add its location in the
2317  // warn-because-unused-macro set. If it gets used it will be removed from set.
2319  !Diags->isIgnored(diag::pp_macro_not_used, MI->getDefinitionLoc())) {
2320  MI->setIsWarnIfUnused(true);
2321  WarnUnusedMacroLocs.insert(MI->getDefinitionLoc());
2322  }
2323 
2324  // If the callbacks want to know, tell them about the macro definition.
2325  if (Callbacks)
2326  Callbacks->MacroDefined(MacroNameTok, MD);
2327 }
2328 
2329 /// HandleUndefDirective - Implements \#undef.
2330 ///
2331 void Preprocessor::HandleUndefDirective(Token &UndefTok) {
2332  ++NumUndefined;
2333 
2334  Token MacroNameTok;
2335  ReadMacroName(MacroNameTok, MU_Undef);
2336 
2337  // Error reading macro name? If so, diagnostic already issued.
2338  if (MacroNameTok.is(tok::eod))
2339  return;
2340 
2341  // Check to see if this is the last token on the #undef line.
2342  CheckEndOfDirective("undef");
2343 
2344  // Okay, we have a valid identifier to undef.
2345  auto *II = MacroNameTok.getIdentifierInfo();
2346  auto MD = getMacroDefinition(II);
2347 
2348  // If the callbacks want to know, tell them about the macro #undef.
2349  // Note: no matter if the macro was defined or not.
2350  if (Callbacks)
2351  Callbacks->MacroUndefined(MacroNameTok, MD);
2352 
2353  // If the macro is not defined, this is a noop undef, just return.
2354  const MacroInfo *MI = MD.getMacroInfo();
2355  if (!MI)
2356  return;
2357 
2358  if (!MI->isUsed() && MI->isWarnIfUnused())
2359  Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used);
2360 
2361  if (MI->isWarnIfUnused())
2362  WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());
2363 
2364  appendMacroDirective(MacroNameTok.getIdentifierInfo(),
2365  AllocateUndefMacroDirective(MacroNameTok.getLocation()));
2366 }
2367 
2368 
2369 //===----------------------------------------------------------------------===//
2370 // Preprocessor Conditional Directive Handling.
2371 //===----------------------------------------------------------------------===//
2372 
2373 /// HandleIfdefDirective - Implements the \#ifdef/\#ifndef directive. isIfndef
2374 /// is true when this is a \#ifndef directive. ReadAnyTokensBeforeDirective is
2375 /// true if any tokens have been returned or pp-directives activated before this
2376 /// \#ifndef has been lexed.
2377 ///
2378 void Preprocessor::HandleIfdefDirective(Token &Result, bool isIfndef,
2379  bool ReadAnyTokensBeforeDirective) {
2380  ++NumIf;
2381  Token DirectiveTok = Result;
2382 
2383  Token MacroNameTok;
2384  ReadMacroName(MacroNameTok);
2385 
2386  // Error reading macro name? If so, diagnostic already issued.
2387  if (MacroNameTok.is(tok::eod)) {
2388  // Skip code until we get to #endif. This helps with recovery by not
2389  // emitting an error when the #endif is reached.
2390  SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
2391  /*Foundnonskip*/false, /*FoundElse*/false);
2392  return;
2393  }
2394 
2395  // Check to see if this is the last token on the #if[n]def line.
2396  CheckEndOfDirective(isIfndef ? "ifndef" : "ifdef");
2397 
2398  IdentifierInfo *MII = MacroNameTok.getIdentifierInfo();
2399  auto MD = getMacroDefinition(MII);
2400  MacroInfo *MI = MD.getMacroInfo();
2401 
2402  if (CurPPLexer->getConditionalStackDepth() == 0) {
2403  // If the start of a top-level #ifdef and if the macro is not defined,
2404  // inform MIOpt that this might be the start of a proper include guard.
2405  // Otherwise it is some other form of unknown conditional which we can't
2406  // handle.
2407  if (!ReadAnyTokensBeforeDirective && !MI) {
2408  assert(isIfndef && "#ifdef shouldn't reach here");
2409  CurPPLexer->MIOpt.EnterTopLevelIfndef(MII, MacroNameTok.getLocation());
2410  } else
2411  CurPPLexer->MIOpt.EnterTopLevelConditional();
2412  }
2413 
2414  // If there is a macro, process it.
2415  if (MI) // Mark it used.
2416  markMacroAsUsed(MI);
2417 
2418  if (Callbacks) {
2419  if (isIfndef)
2420  Callbacks->Ifndef(DirectiveTok.getLocation(), MacroNameTok, MD);
2421  else
2422  Callbacks->Ifdef(DirectiveTok.getLocation(), MacroNameTok, MD);
2423  }
2424 
2425  // Should we include the stuff contained by this directive?
2426  if (!MI == isIfndef) {
2427  // Yes, remember that we are inside a conditional, then lex the next token.
2428  CurPPLexer->pushConditionalLevel(DirectiveTok.getLocation(),
2429  /*wasskip*/false, /*foundnonskip*/true,
2430  /*foundelse*/false);
2431  } else {
2432  // No, skip the contents of this block.
2433  SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
2434  /*Foundnonskip*/false,
2435  /*FoundElse*/false);
2436  }
2437 }
2438 
2439 /// HandleIfDirective - Implements the \#if directive.
2440 ///
2441 void Preprocessor::HandleIfDirective(Token &IfToken,
2442  bool ReadAnyTokensBeforeDirective) {
2443  ++NumIf;
2444 
2445  // Parse and evaluate the conditional expression.
2446  IdentifierInfo *IfNDefMacro = nullptr;
2447  const SourceLocation ConditionalBegin = CurPPLexer->getSourceLocation();
2448  const bool ConditionalTrue = EvaluateDirectiveExpression(IfNDefMacro);
2449  const SourceLocation ConditionalEnd = CurPPLexer->getSourceLocation();
2450 
2451  // If this condition is equivalent to #ifndef X, and if this is the first
2452  // directive seen, handle it for the multiple-include optimization.
2453  if (CurPPLexer->getConditionalStackDepth() == 0) {
2454  if (!ReadAnyTokensBeforeDirective && IfNDefMacro && ConditionalTrue)
2455  // FIXME: Pass in the location of the macro name, not the 'if' token.
2456  CurPPLexer->MIOpt.EnterTopLevelIfndef(IfNDefMacro, IfToken.getLocation());
2457  else
2458  CurPPLexer->MIOpt.EnterTopLevelConditional();
2459  }
2460 
2461  if (Callbacks)
2462  Callbacks->If(IfToken.getLocation(),
2463  SourceRange(ConditionalBegin, ConditionalEnd),
2464  (ConditionalTrue ? PPCallbacks::CVK_True : PPCallbacks::CVK_False));
2465 
2466  // Should we include the stuff contained by this directive?
2467  if (ConditionalTrue) {
2468  // Yes, remember that we are inside a conditional, then lex the next token.
2469  CurPPLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
2470  /*foundnonskip*/true, /*foundelse*/false);
2471  } else {
2472  // No, skip the contents of this block.
2473  SkipExcludedConditionalBlock(IfToken.getLocation(), /*Foundnonskip*/false,
2474  /*FoundElse*/false);
2475  }
2476 }
2477 
2478 /// HandleEndifDirective - Implements the \#endif directive.
2479 ///
2480 void Preprocessor::HandleEndifDirective(Token &EndifToken) {
2481  ++NumEndif;
2482 
2483  // Check that this is the whole directive.
2484  CheckEndOfDirective("endif");
2485 
2486  PPConditionalInfo CondInfo;
2487  if (CurPPLexer->popConditionalLevel(CondInfo)) {
2488  // No conditionals on the stack: this is an #endif without an #if.
2489  Diag(EndifToken, diag::err_pp_endif_without_if);
2490  return;
2491  }
2492 
2493  // If this the end of a top-level #endif, inform MIOpt.
2494  if (CurPPLexer->getConditionalStackDepth() == 0)
2495  CurPPLexer->MIOpt.ExitTopLevelConditional();
2496 
2497  assert(!CondInfo.WasSkipping && !CurPPLexer->LexingRawMode &&
2498  "This code should only be reachable in the non-skipping case!");
2499 
2500  if (Callbacks)
2501  Callbacks->Endif(EndifToken.getLocation(), CondInfo.IfLoc);
2502 }
2503 
2504 /// HandleElseDirective - Implements the \#else directive.
2505 ///
2506 void Preprocessor::HandleElseDirective(Token &Result) {
2507  ++NumElse;
2508 
2509  // #else directive in a non-skipping conditional... start skipping.
2510  CheckEndOfDirective("else");
2511 
2512  PPConditionalInfo CI;
2513  if (CurPPLexer->popConditionalLevel(CI)) {
2514  Diag(Result, diag::pp_err_else_without_if);
2515  return;
2516  }
2517 
2518  // If this is a top-level #else, inform the MIOpt.
2519  if (CurPPLexer->getConditionalStackDepth() == 0)
2520  CurPPLexer->MIOpt.EnterTopLevelConditional();
2521 
2522  // If this is a #else with a #else before it, report the error.
2523  if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else);
2524 
2525  if (Callbacks)
2526  Callbacks->Else(Result.getLocation(), CI.IfLoc);
2527 
2528  // Finally, skip the rest of the contents of this block.
2529  SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
2530  /*FoundElse*/true, Result.getLocation());
2531 }
2532 
2533 /// HandleElifDirective - Implements the \#elif directive.
2534 ///
2535 void Preprocessor::HandleElifDirective(Token &ElifToken) {
2536  ++NumElse;
2537 
2538  // #elif directive in a non-skipping conditional... start skipping.
2539  // We don't care what the condition is, because we will always skip it (since
2540  // the block immediately before it was included).
2541  const SourceLocation ConditionalBegin = CurPPLexer->getSourceLocation();
2543  const SourceLocation ConditionalEnd = CurPPLexer->getSourceLocation();
2544 
2545  PPConditionalInfo CI;
2546  if (CurPPLexer->popConditionalLevel(CI)) {
2547  Diag(ElifToken, diag::pp_err_elif_without_if);
2548  return;
2549  }
2550 
2551  // If this is a top-level #elif, inform the MIOpt.
2552  if (CurPPLexer->getConditionalStackDepth() == 0)
2553  CurPPLexer->MIOpt.EnterTopLevelConditional();
2554 
2555  // If this is a #elif with a #else before it, report the error.
2556  if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else);
2557 
2558  if (Callbacks)
2559  Callbacks->Elif(ElifToken.getLocation(),
2560  SourceRange(ConditionalBegin, ConditionalEnd),
2562 
2563  // Finally, skip the rest of the contents of this block.
2564  SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
2565  /*FoundElse*/CI.FoundElse,
2566  ElifToken.getLocation());
2567 }
bool isAtStartOfLine() const
isAtStartOfLine - Return true if this token is at the start of a line.
Definition: Token.h:261
Module * getModule() const
Retrieve the module the header is stored in.
Definition: ModuleMap.h:123
SourceManager & getSourceManager() const
Definition: Preprocessor.h:687
bool isPoisoned() const
Return true if this token has been poisoned.
bool getHasReadAnyTokensVal() const
getHasReadAnyTokensVal - This is used for the #ifndef hande-shake at the top of the file when reading...
static LLVM_READONLY bool isDigit(unsigned char c)
Return true if this character is an ASCII digit: [0-9].
Definition: CharInfo.h:94
Module * getModuleForLocation(SourceLocation Loc)
Find the module that owns the source or header file that Loc points to.
MacroInfo * AllocateMacroInfo(SourceLocation L)
Allocate a new MacroInfo object with the provided SourceLocation.
bool ConcatenateIncludeName(SmallString< 128 > &FilenameBuffer, SourceLocation &End)
Handle cases where the #include name is expanded from a macro as multiple tokens, which need to be gl...
void AddTokenToBody(const Token &Tok)
Add the specified token to the replacement text for the macro.
Definition: MacroInfo.h:245
bool isMacroID() const
std::pair< FileID, unsigned > getDecomposedExpansionLoc(SourceLocation Loc) const
Decompose the specified location into a raw FileID + Offset pair.
void markMacroAsUsed(MacroInfo *MI)
A macro is used, update information about macros that need unused warnings.
Defines the clang::FileManager interface and associated types.
PPConditionalInfo & peekConditionalLevel()
Return the top of the conditional stack.
bool isInvalid() const
Return true if this object is invalid or uninitialized.
static LLVM_READONLY bool isUppercase(unsigned char c)
Return true if this character is an uppercase ASCII letter: [A-Z].
Definition: CharInfo.h:106
static bool isReservedId(StringRef Text, const LangOptions &Lang)
Checks if the specified identifier is reserved in the specified language.
Defines the SourceManager interface.
IdentifierInfo * getIdentifierInfo(StringRef Name) const
Return information about the specified preprocessor identifier token.
Definition: Preprocessor.h:927
static bool ReadLineMarkerFlags(bool &IsFileEntry, bool &IsFileExit, bool &IsSystemHeader, bool &IsExternCHeader, Preprocessor &PP)
ReadLineMarkerFlags - Parse and validate any flags at the end of a GNU line marker directive...
Module * getCurrentModule()
Retrieves the module that we're currently building, if any.
void pushConditionalLevel(SourceLocation DirectiveStart, bool WasSkipping, bool FoundNonSkip, bool FoundElse)
pushConditionalLevel - When we enter a #if directive, this keeps track of what we are currently in fo...
bool isObjectLike() const
Definition: MacroInfo.h:197
Defines the clang::MacroInfo and clang::MacroDirective classes.
bool hasLeadingSpace() const
Return true if this token has whitespace before it.
Definition: Token.h:265
std::unique_ptr< llvm::MemoryBuffer > Buffer
void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID)
Add a line note to the line table for the FileID and offset specified by Loc.
A directive for an undefined macro.
Definition: MacroInfo.h:440
bool needsCleaning() const
Return true if this token has trigraphs or escaped newlines in it.
Definition: Token.h:280
void setCodeCompletionReached()
Note that we hit the code-completion point.
static void EnterAnnotationToken(Preprocessor &PP, SourceLocation Begin, SourceLocation End, tok::TokenKind Kind, void *AnnotationVal)
Push a token onto the token stream containing an annotation.
SourceLocation getDefinitionLoc() const
Return the location that the macro was defined at.
Definition: MacroInfo.h:120
const FileEntry * LookupFile(StringRef Filename, SourceLocation IncludeLoc, bool isAngled, const DirectoryLookup *FromDir, const DirectoryLookup *&CurDir, ArrayRef< std::pair< const FileEntry *, const DirectoryEntry * >> Includers, SmallVectorImpl< char > *SearchPath, SmallVectorImpl< char > *RelativePath, Module *RequestingModule, ModuleMap::KnownHeader *SuggestedModule, bool SkipCache=false)
Given a "foo" or <foo> reference, look up the indicated file, return null on failure.
CharacteristicKind
Indicates whether a file or directory holds normal user code, system code, or system code which is im...
Definition: SourceManager.h:79
ModuleMap & getModuleMap()
Retrieve the module map.
Definition: HeaderSearch.h:591
void setIsWarnIfUnused(bool val)
Set the value of the IsWarnIfUnused flag.
Definition: MacroInfo.h:157
bool isInPrimaryFile() const
Return true if we're in the top-level file, not in a #include.
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 ...
static MacroDiag shouldWarnOnMacroDef(Preprocessor &PP, IdentifierInfo *II)
void setIsGNUVarargs()
Definition: MacroInfo.h:201
std::string getFullModuleName() const
Retrieve the full name of this module, including the path from its top-level module.
Module * getModuleContainingLocation(SourceLocation Loc)
Find the module that contains the specified location, either directly or indirectly.
DefMacroDirective * appendDefMacroDirective(IdentifierInfo *II, MacroInfo *MI, SourceLocation Loc)
Definition: Preprocessor.h:872
One of these records is kept for each identifier that is lexed.
A directive for a defined macro or a macro imported from a module.
Definition: MacroInfo.h:418
bool ParsingPreprocessorDirective
True when parsing #XXX; turns '\n' into a tok::eod token.
std::pair< SourceLocation, SourceLocation > getExpansionRange(SourceLocation Loc) const
Given a SourceLocation object, return the range of tokens covered by the expansion in the ultimate fi...
bool CheckMacroName(Token &MacroNameTok, MacroUse isDefineUndef, bool *ShadowFlag=nullptr)
static void diagnoseAutoModuleImport(Preprocessor &PP, SourceLocation HashLoc, Token &IncludeTok, ArrayRef< std::pair< IdentifierInfo *, SourceLocation >> Path, SourceLocation PathEnd)
Produce a diagnostic informing the user that a #include or similar was implicitly treated as a module...
bool HasIncludeAliasMap() const
Checks whether the map exists or not.
Definition: HeaderSearch.h:292
bool getImmediatelyAfterTopLevelIfndef() const
getImmediatelyAfterTopLevelIfndef - returns true if the last directive was an #ifndef at the beginnin...
const FileEntry * LookupSubframeworkHeader(StringRef Filename, const FileEntry *RelativeFileEnt, SmallVectorImpl< char > *SearchPath, SmallVectorImpl< char > *RelativePath, Module *RequestingModule, ModuleMap::KnownHeader *SuggestedModule)
Look up a subframework for the specified #include file.
SmallVector< PPConditionalInfo, 4 > ConditionalStack
Information about the set of #if/#ifdef/#ifndef blocks we are currently in.
const MacroInfo * getMacroInfo(const IdentifierInfo *II) const
Definition: Preprocessor.h:851
const LangOptions & getLangOpts() const
Definition: Preprocessor.h:683
virtual void CodeCompleteDirective(bool InConditional)
Callback invoked when performing code completion for a preprocessor directive.
Token - This structure provides full information about a lexed token.
Definition: Token.h:37
bool isWarnIfUnused() const
Return true if we should emit a warning if the macro is unused.
Definition: MacroInfo.h:227
void setKind(tok::TokenKind K)
Definition: Token.h:91
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:48
Describes a module or submodule.
Definition: Basic/Module.h:47
bool isMissingExpected() const
Determines whether the module, which failed to load, was actually a submodule that we expected to see...
Definition: ModuleLoader.h:48
VerifyDiagnosticConsumer::Directive Directive
bool popConditionalLevel(PPConditionalInfo &CI)
popConditionalLevel - Remove an entry off the top of the conditional stack, returning information abo...
A directive for setting the module visibility of a macro.
Definition: MacroInfo.h:454
void CheckEndOfDirective(const char *Directive, bool EnableMacros=false)
Ensure that the next token is a tok::eod token.
MacroUse
Context in which macro name is used.
Definition: Preprocessor.h:84
SourceLocation getLocWithOffset(int Offset) const
Return a source location with the specified offset from this SourceLocation.
bool isAvailable() const
Determine whether this module is available for use within the current translation unit...
Definition: Basic/Module.h:314
void HandleDirective(Token &Result)
Callback invoked when the lexer sees a # token at the start of a line.
Module * Parent
The parent of this module.
Definition: Basic/Module.h:57
bool hadModuleLoaderFatalFailure() const
Definition: Preprocessor.h:711
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...
tok::TokenKind getKind() const
Definition: Token.h:90
bool FoundNonSkip
True if we have emitted tokens already, and now we're in an #else block or something.
Definition: Token.h:314
const TargetInfo & getTargetInfo() const
Definition: Preprocessor.h:684
const FileEntry * getFileEntry() const
getFileEntry - Return the FileEntry corresponding to this FileID.
detail::InMemoryDirectory::const_iterator I
DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID) const
Forwarding function for diagnostics.
SourceLocation getIncludeLoc() const
Return the presumed include location of this location.
void appendMacroDirective(IdentifierInfo *II, MacroDirective *MD)
Add a directive to the macro directive history for this identifier.
bool isInvalid() const
void LexUnexpandedToken(Token &Result)
Just like Lex, but disables macro expansion of identifier tokens.
MacroDiag
Enumerates possible cases of #define/#undef a reserved identifier.
bool isCPlusPlusOperatorKeyword() const
void diagnoseHeaderInclusion(Module *RequestingModule, SourceLocation FilenameLoc, StringRef Filename, const FileEntry *File)
Reports errors if a module must not include a specific file.
Definition: ModuleMap.cpp:243
StringRef getRawIdentifier() const
getRawIdentifier - For a raw identifier token (i.e., an identifier lexed in raw mode), returns a reference to the text substring in the buffer if known.
Definition: Token.h:203
const FileEntry * getFileEntryForID(FileID FID) const
Returns the FileEntry record for the provided FileID.
std::string CurrentModule
The name of the current module.
Definition: LangOptions.h:96
const DirectoryEntry * getDirectory(StringRef DirName, bool CacheFailure=true)
Lookup, cache, and verify the specified directory (real or virtual).
FileID getFileID(SourceLocation SpellingLoc) const
Return the FileID for a SourceLocation.
Describes the result of attempting to load a module.
Definition: ModuleLoader.h:33
StringRef Filename
Definition: Format.cpp:1723
const SmallVectorImpl< AnnotatedLine * >::const_iterator End
SourceManager & SM
void setAnnotationValue(void *val)
Definition: Token.h:228
bool LexingRawMode
True if in raw mode.
StringRef getName() const
Return the actual identifier string.
Represents a character-granular source range.
void setHasCommaPasting()
Definition: MacroInfo.h:215
void makeModuleVisible(Module *M, SourceLocation Loc)
FileID createFileID(const FileEntry *SourceFile, SourceLocation IncludePos, SrcMgr::CharacteristicKind FileCharacter, int LoadedID=0, unsigned LoadedOffset=0)
Create a new FileID that represents the specified file being #included from the specified IncludePosi...
Defines the clang::Preprocessor interface.
const char * getBufferName(SourceLocation Loc, bool *Invalid=nullptr) const
Return the filename or buffer identifier of the buffer the location is in.
bool hasUDSuffix() const
Return true if this token is a string or character literal which has a ud-suffix. ...
Definition: Token.h:290
PPKeywordKind
Provides a namespace for preprocessor keywords which start with a '#' at the beginning of the line...
Definition: TokenKinds.h:33
void setIsPoisoned(bool Value=true)
setIsPoisoned - Mark this identifier as poisoned.
int getArgumentNum(const IdentifierInfo *Arg) const
Return the argument number of the specified identifier, or -1 if the identifier is not a formal argum...
Definition: MacroInfo.h:186
MultipleIncludeOpt MIOpt
A state machine that detects the #ifndef-wrapping a file idiom for the multiple-include optimization...
void SetDefinedMacro(IdentifierInfo *M, SourceLocation Loc)
bool isInSystemHeader(SourceLocation Loc) const
Returns if a SourceLocation is in a system header.
SourceLocation getLocation() const
Return a source location identifier for the specified offset in the current file. ...
Definition: Token.h:124
bool isNot(tok::TokenKind K) const
Definition: Token.h:96
unsigned getNumTokens() const
Return the number of tokens that this macro expands to.
Definition: MacroInfo.h:231
Information about the conditional stack (#if directives) currently active.
Definition: Token.h:304
Represents an unpacked "presumed" location which can be presented to the user.
void setArgumentList(ArrayRef< IdentifierInfo * > List, llvm::BumpPtrAllocator &PPAllocator)
Set the specified list of identifiers as the argument list for this macro.
Definition: MacroInfo.h:161
The result type of a method or function.
DirectoryLookup - This class represents one entry in the search list that specifies the search order ...
bool isC99Varargs() const
Definition: MacroInfo.h:202
StringRef getTopLevelModuleName() const
Retrieve the name of the top-level module.
Definition: Basic/Module.h:379
static CharSourceRange getCharRange(SourceRange R)
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:415
bool getSuppressSystemWarnings() const
Definition: Diagnostic.h:460
virtual SourceLocation getSourceLocation()=0
Return the source location for the next observable location.
bool isInMainFile(SourceLocation Loc) const
Returns whether the PresumedLoc for a given SourceLocation is in the main file.
MacroInfo * AllocateDeserializedMacroInfo(SourceLocation L, unsigned SubModuleID)
Allocate a new MacroInfo object loaded from an AST file.
void setIsFunctionLike()
Function/Object-likeness.
Definition: MacroInfo.h:195
Encapsulates changes to the "macros namespace" (the location where the macro name became active...
Definition: MacroInfo.h:307
Kind
bool WasSkipping
True if this was contained in a skipping directive, e.g., in a "\#if 0" block.
Definition: Token.h:310
Encodes a location in the source.
const char * getNameStart() const
Return the beginning of the actual null-terminated string for this identifier.
bool isValid() const
Return true if this is a valid SourceLocation object.
MacroDefinition getMacroDefinition(const IdentifierInfo *II)
Definition: Preprocessor.h:810
MacroDirective * getLocalMacroDirective(const IdentifierInfo *II) const
Given an identifier, return its latest non-imported MacroDirective if it is #define'd and not #undef'...
Definition: Preprocessor.h:840
All of the names in this module are hidden.
Definition: Basic/Module.h:208
void setAnnotationEndLoc(SourceLocation L)
Definition: Token.h:142
Cached information about one file (either on disk or in the virtual file system). ...
Definition: FileManager.h:53
void EnterTokenStream(const Token *Toks, unsigned NumToks, bool DisableMacroExpansion, bool OwnsTokens)
Add a "macro" context to the top of the include stack, which will cause the lexer to start returning ...
virtual void CodeCompleteInConditionalExclusion()
Callback invoked when performing code completion within a block of code that was excluded due to prep...
void setIsC99Varargs()
Varargs querying methods. This can only be set for function-like macros.
Definition: MacroInfo.h:200
void setIdentifierInfo(IdentifierInfo *II)
Definition: Token.h:186
void Lex(Token &Result)
Lex the next token for this preprocessor.
bool isKeyword(const LangOptions &LangOpts)
Return true if this token is a keyword in the specified language.
void setDefinitionEndLoc(SourceLocation EndLoc)
Set the location of the last token in the macro.
Definition: MacroInfo.h:123
TokenKind
Provides a simple uniform namespace for tokens from all C languages.
Definition: TokenKinds.h:25
const Token & getReplacementToken(unsigned Tok) const
Definition: MacroInfo.h:233
FileID getMainFileID() const
Returns the FileID of the main source file.
bool is(tok::TokenKind K) const
is/isNot - Predicates to check if this token is a specific kind, as in "if (Tok.is(tok::l_brace)) {...
Definition: Token.h:95
bool isIgnored(unsigned DiagID, SourceLocation Loc) const
Determine whether the diagnostic is known to be ignored.
Definition: Diagnostic.h:645
unsigned getConditionalStackDepth() const
DiagnosticsEngine & getDiagnostics() const
Definition: Preprocessor.h:680
FileID getPredefinesFileID() const
Returns the FileID for the preprocessor predefines.
Definition: Preprocessor.h:770
SourceLocation IfLoc
Location where the conditional started.
Definition: Token.h:306
bool isStr(const char(&Str)[StrLen]) const
Return true if this is the identifier for the specified string.
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
The pragma was introduced via #pragma.
Definition: Pragma.h:36
Stored information about a header directive that was found in the module map file but has not been re...
Definition: Basic/Module.h:130
void ExitTopLevelConditional()
Called when the lexer exits the top-level conditional.
Module * inferModuleFromLocation(FullSourceLoc Loc)
Infers the (sub)module based on the given source location and source manager.
Definition: ModuleMap.cpp:919
static bool isConfigurationPattern(Token &MacroName, MacroInfo *MI, const LangOptions &LOptions)
SrcMgr::CharacteristicKind getFileCharacteristic(SourceLocation Loc) const
Return the file characteristic of the specified source location, indicating whether this is a normal ...
unsigned getLineTableFilenameID(StringRef Str)
Return the uniqued ID for the specified filename.
const MacroInfo * getMacroInfo() const
Definition: MacroInfo.h:403
virtual ModuleLoadResult loadModule(SourceLocation ImportLoc, ModuleIdPath Path, Module::NameVisibilityKind Visibility, bool IsInclusionDirective)=0
Attempt to load the given module.
static MacroDiag shouldWarnOnMacroUndef(Preprocessor &PP, IdentifierInfo *II)
PreprocessorLexer * getCurrentFileLexer() const
Return the current file lexer being lexed from.
std::string ImplementationOfModule
The name of the module that the translation unit is an implementation of.
Definition: LangOptions.h:101
void EnterTopLevelConditional()
Invoked when a top level conditional (except #ifndef) is found.
Encapsulates the data about a macro definition (e.g.
Definition: MacroInfo.h:34
SourceLocation DefinitionLoc
The location of the module definition.
Definition: Basic/Module.h:53
bool isOneOf(tok::TokenKind K1, tok::TokenKind K2) const
Definition: Token.h:97
bool GetIncludeFilenameSpelling(SourceLocation Loc, StringRef &Filename)
Turn the specified lexer token into a fully checked and spelled filename, e.g.
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:72
std::pair< std::string, bool > Requirement
An individual requirement: a feature name and a flag indicating the required state of that feature...
Definition: Basic/Module.h:142
void AddSearchPath(const DirectoryLookup &dir, bool isAngled)
Add an additional search path.
Definition: HeaderSearch.h:278
bool isBuiltinMacro() const
Return true if this macro requires processing before expansion.
Definition: MacroInfo.h:212
static bool isInvalid(SourceLocation Loc, bool *Invalid)
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:78
KnownHeader findModuleForHeader(const FileEntry *File)
Retrieve the module that owns the given header file, if any.
Definition: ModuleMap.cpp:334
void LexIncludeFilename(Token &Result)
After the preprocessor has parsed a #include, lex and (potentially) macro expand the filename...
Cached information about one directory (either on disk or in the virtual file system).
Definition: FileManager.h:40
static bool GetLineValue(Token &DigitTok, unsigned &Val, unsigned DiagID, Preprocessor &PP, bool IsGNULineDirective=false)
GetLineValue - Convert a numeric token into an unsigned value, emitting Diagnostic DiagID if it is in...
virtual void CodeCompleteMacroName(bool IsDefinition)
Callback invoked when performing code completion in a context where the name of a macro is expected...
const FileEntry * LookupFile(SourceLocation FilenameLoc, StringRef Filename, bool isAngled, const DirectoryLookup *FromDir, const FileEntry *FromFile, const DirectoryLookup *&CurDir, SmallVectorImpl< char > *SearchPath, SmallVectorImpl< char > *RelativePath, ModuleMap::KnownHeader *SuggestedModule, bool SkipCache=false)
Given a "foo" or <foo> reference, look up the indicated file.
void EnterTopLevelIfndef(const IdentifierInfo *M, SourceLocation Loc)
Called when entering a top-level #ifndef directive (or the "\#if !defined" equivalent) without any pr...
StringLiteralParser - This decodes string escape characters and performs wide string analysis and Tra...
static FixItHint CreateReplacement(CharSourceRange RemoveRange, StringRef Code)
Create a code modification hint that replaces the given source range with the given code string...
Definition: Diagnostic.h:115
bool ShouldEnterIncludeFile(Preprocessor &PP, const FileEntry *File, bool isImport, Module *CorrespondingModule)
Mark the specified file as a target of of a #include, #include_next, or #import directive.
A SourceLocation and its associated SourceManager.
StringRef MapHeaderToIncludeAlias(StringRef Source)
MapHeaderToIncludeAlias - Maps one header file name to a different header file name, for use with the include_alias pragma.
Definition: HeaderSearch.h:308
void DiscardUntilEndOfDirective()
Read and discard all tokens remaining on the current line until the tok::eod token is found...
unsigned getLength() const
Definition: Token.h:127
Module * SourceModule
Definition: ModuleMap.h:78
Annotates a diagnostic with some code that should be inserted, removed, or replaced to fix the proble...
Definition: Diagnostic.h:52
StringRef Text
Definition: Format.cpp:1724
void setLocation(SourceLocation L)
Definition: Token.h:132
A trivial tuple used to represent a source range.
SourceLocation getExpansionLoc(SourceLocation Loc) const
Given a SourceLocation object Loc, return the expansion location referenced by the ID...
bool isValid() const
const DirectoryEntry * getDir() const
Return the directory the file lives in.
Definition: FileManager.h:93
bool FoundElse
True if we've seen a #else in this block.
Definition: Token.h:318
A header that is known to reside within a given module, whether it was included or excluded...
Definition: ModuleMap.h:108
This class handles loading and caching of source files into memory.
void startToken()
Reset all flags to cleared.
Definition: Token.h:169
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Definition: Preprocessor.h:96
bool isUsed() const
Return false if this macro is defined in the main file and has not yet been used. ...
Definition: MacroInfo.h:219
bool EnterSourceFile(FileID CurFileID, const DirectoryLookup *Dir, SourceLocation Loc)
Add a source file to the top of the include stack and start lexing tokens from it instead of the curr...
IdentifierInfo * getIdentifierInfo() const
Definition: Token.h:177
tok::PPKeywordKind getPPKeywordID() const
Return the preprocessor keyword ID for this identifier.
PresumedLoc getPresumedLoc(SourceLocation Loc, bool UseLineDirectives=true) const
Returns the "presumed" location of a SourceLocation specifies.