clang  3.7.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  // If the header lookup mechanism may be relative to the current inclusion
615  // stack, record the parent #includes.
617  Includers;
618  if (!FromDir && !FromFile) {
620  const FileEntry *FileEnt = SourceMgr.getFileEntryForID(FID);
621 
622  // If there is no file entry associated with this file, it must be the
623  // predefines buffer or the module includes buffer. Any other file is not
624  // lexed with a normal lexer, so it won't be scanned for preprocessor
625  // directives.
626  //
627  // If we have the predefines buffer, resolve #include references (which come
628  // from the -include command line argument) from the current working
629  // directory instead of relative to the main file.
630  //
631  // If we have the module includes buffer, resolve #include references (which
632  // come from header declarations in the module map) relative to the module
633  // map file.
634  if (!FileEnt) {
635  if (FID == SourceMgr.getMainFileID() && MainFileDir)
636  Includers.push_back(std::make_pair(nullptr, MainFileDir));
637  else if ((FileEnt =
638  SourceMgr.getFileEntryForID(SourceMgr.getMainFileID())))
639  Includers.push_back(std::make_pair(FileEnt, FileMgr.getDirectory(".")));
640  } else {
641  Includers.push_back(std::make_pair(FileEnt, FileEnt->getDir()));
642  }
643 
644  // MSVC searches the current include stack from top to bottom for
645  // headers included by quoted include directives.
646  // See: http://msdn.microsoft.com/en-us/library/36k2cdd4.aspx
647  if (LangOpts.MSVCCompat && !isAngled) {
648  for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) {
649  IncludeStackInfo &ISEntry = IncludeMacroStack[e - i - 1];
650  if (IsFileLexer(ISEntry))
651  if ((FileEnt = SourceMgr.getFileEntryForID(
652  ISEntry.ThePPLexer->getFileID())))
653  Includers.push_back(std::make_pair(FileEnt, FileEnt->getDir()));
654  }
655  }
656  }
657 
658  CurDir = CurDirLookup;
659 
660  if (FromFile) {
661  // We're supposed to start looking from after a particular file. Search
662  // the include path until we find that file or run out of files.
663  const DirectoryLookup *TmpCurDir = CurDir;
664  const DirectoryLookup *TmpFromDir = nullptr;
665  while (const FileEntry *FE = HeaderInfo.LookupFile(
666  Filename, FilenameLoc, isAngled, TmpFromDir, TmpCurDir,
667  Includers, SearchPath, RelativePath, SuggestedModule,
668  SkipCache)) {
669  // Keep looking as if this file did a #include_next.
670  TmpFromDir = TmpCurDir;
671  ++TmpFromDir;
672  if (FE == FromFile) {
673  // Found it.
674  FromDir = TmpFromDir;
675  CurDir = TmpCurDir;
676  break;
677  }
678  }
679  }
680 
681  // Do a standard file entry lookup.
682  const FileEntry *FE = HeaderInfo.LookupFile(
683  Filename, FilenameLoc, isAngled, FromDir, CurDir, Includers, SearchPath,
684  RelativePath, SuggestedModule, SkipCache);
685  if (FE) {
686  if (SuggestedModule && !LangOpts.AsmPreprocessor)
688  getModuleForLocation(FilenameLoc), FilenameLoc, Filename, FE);
689  return FE;
690  }
691 
692  const FileEntry *CurFileEnt;
693  // Otherwise, see if this is a subframework header. If so, this is relative
694  // to one of the headers on the #include stack. Walk the list of the current
695  // headers on the #include stack and pass them to HeaderInfo.
696  if (IsFileLexer()) {
697  if ((CurFileEnt = SourceMgr.getFileEntryForID(CurPPLexer->getFileID()))) {
698  if ((FE = HeaderInfo.LookupSubframeworkHeader(Filename, CurFileEnt,
699  SearchPath, RelativePath,
700  SuggestedModule))) {
701  if (SuggestedModule && !LangOpts.AsmPreprocessor)
703  getModuleForLocation(FilenameLoc), FilenameLoc, Filename, FE);
704  return FE;
705  }
706  }
707  }
708 
709  for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) {
710  IncludeStackInfo &ISEntry = IncludeMacroStack[e-i-1];
711  if (IsFileLexer(ISEntry)) {
712  if ((CurFileEnt =
713  SourceMgr.getFileEntryForID(ISEntry.ThePPLexer->getFileID()))) {
714  if ((FE = HeaderInfo.LookupSubframeworkHeader(
715  Filename, CurFileEnt, SearchPath, RelativePath,
716  SuggestedModule))) {
717  if (SuggestedModule && !LangOpts.AsmPreprocessor)
719  getModuleForLocation(FilenameLoc), FilenameLoc, Filename, FE);
720  return FE;
721  }
722  }
723  }
724  }
725 
726  // Otherwise, we really couldn't find the file.
727  return nullptr;
728 }
729 
730 
731 //===----------------------------------------------------------------------===//
732 // Preprocessor Directive Handling.
733 //===----------------------------------------------------------------------===//
734 
736 public:
738  : PP(pp), save(pp->DisableMacroExpansion) {
739  if (pp->MacroExpansionInDirectivesOverride)
740  pp->DisableMacroExpansion = false;
741  }
743  PP->DisableMacroExpansion = save;
744  }
745 private:
746  Preprocessor *PP;
747  bool save;
748 };
749 
750 /// HandleDirective - This callback is invoked when the lexer sees a # token
751 /// at the start of a line. This consumes the directive, modifies the
752 /// lexer/preprocessor state, and advances the lexer(s) so that the next token
753 /// read is the correct one.
755  // FIXME: Traditional: # with whitespace before it not recognized by K&R?
756 
757  // We just parsed a # character at the start of a line, so we're in directive
758  // mode. Tell the lexer this so any newlines we see will be converted into an
759  // EOD token (which terminates the directive).
760  CurPPLexer->ParsingPreprocessorDirective = true;
761  if (CurLexer) CurLexer->SetKeepWhitespaceMode(false);
762 
763  bool ImmediatelyAfterTopLevelIfndef =
766 
767  ++NumDirectives;
768 
769  // We are about to read a token. For the multiple-include optimization FA to
770  // work, we have to remember if we had read any tokens *before* this
771  // pp-directive.
772  bool ReadAnyTokensBeforeDirective =CurPPLexer->MIOpt.getHasReadAnyTokensVal();
773 
774  // Save the '#' token in case we need to return it later.
775  Token SavedHash = Result;
776 
777  // Read the next token, the directive flavor. This isn't expanded due to
778  // C99 6.10.3p8.
779  LexUnexpandedToken(Result);
780 
781  // C99 6.10.3p11: Is this preprocessor directive in macro invocation? e.g.:
782  // #define A(x) #x
783  // A(abc
784  // #warning blah
785  // def)
786  // If so, the user is relying on undefined behavior, emit a diagnostic. Do
787  // not support this for #include-like directives, since that can result in
788  // terrible diagnostics, and does not work in GCC.
789  if (InMacroArgs) {
790  if (IdentifierInfo *II = Result.getIdentifierInfo()) {
791  switch (II->getPPKeywordID()) {
792  case tok::pp_include:
793  case tok::pp_import:
794  case tok::pp_include_next:
795  case tok::pp___include_macros:
796  case tok::pp_pragma:
797  Diag(Result, diag::err_embedded_directive) << II->getName();
799  return;
800  default:
801  break;
802  }
803  }
804  Diag(Result, diag::ext_embedded_directive);
805  }
806 
807  // Temporarily enable macro expansion if set so
808  // and reset to previous state when returning from this function.
809  ResetMacroExpansionHelper helper(this);
810 
811  switch (Result.getKind()) {
812  case tok::eod:
813  return; // null directive.
814  case tok::code_completion:
815  if (CodeComplete)
816  CodeComplete->CodeCompleteDirective(
817  CurPPLexer->getConditionalStackDepth() > 0);
819  return;
820  case tok::numeric_constant: // # 7 GNU line marker directive.
821  if (getLangOpts().AsmPreprocessor)
822  break; // # 4 is not a preprocessor directive in .S files.
823  return HandleDigitDirective(Result);
824  default:
825  IdentifierInfo *II = Result.getIdentifierInfo();
826  if (!II) break; // Not an identifier.
827 
828  // Ask what the preprocessor keyword ID is.
829  switch (II->getPPKeywordID()) {
830  default: break;
831  // C99 6.10.1 - Conditional Inclusion.
832  case tok::pp_if:
833  return HandleIfDirective(Result, ReadAnyTokensBeforeDirective);
834  case tok::pp_ifdef:
835  return HandleIfdefDirective(Result, false, true/*not valid for miopt*/);
836  case tok::pp_ifndef:
837  return HandleIfdefDirective(Result, true, ReadAnyTokensBeforeDirective);
838  case tok::pp_elif:
839  return HandleElifDirective(Result);
840  case tok::pp_else:
841  return HandleElseDirective(Result);
842  case tok::pp_endif:
843  return HandleEndifDirective(Result);
844 
845  // C99 6.10.2 - Source File Inclusion.
846  case tok::pp_include:
847  // Handle #include.
848  return HandleIncludeDirective(SavedHash.getLocation(), Result);
849  case tok::pp___include_macros:
850  // Handle -imacros.
851  return HandleIncludeMacrosDirective(SavedHash.getLocation(), Result);
852 
853  // C99 6.10.3 - Macro Replacement.
854  case tok::pp_define:
855  return HandleDefineDirective(Result, ImmediatelyAfterTopLevelIfndef);
856  case tok::pp_undef:
857  return HandleUndefDirective(Result);
858 
859  // C99 6.10.4 - Line Control.
860  case tok::pp_line:
861  return HandleLineDirective(Result);
862 
863  // C99 6.10.5 - Error Directive.
864  case tok::pp_error:
865  return HandleUserDiagnosticDirective(Result, false);
866 
867  // C99 6.10.6 - Pragma Directive.
868  case tok::pp_pragma:
869  return HandlePragmaDirective(SavedHash.getLocation(), PIK_HashPragma);
870 
871  // GNU Extensions.
872  case tok::pp_import:
873  return HandleImportDirective(SavedHash.getLocation(), Result);
874  case tok::pp_include_next:
875  return HandleIncludeNextDirective(SavedHash.getLocation(), Result);
876 
877  case tok::pp_warning:
878  Diag(Result, diag::ext_pp_warning_directive);
879  return HandleUserDiagnosticDirective(Result, true);
880  case tok::pp_ident:
881  return HandleIdentSCCSDirective(Result);
882  case tok::pp_sccs:
883  return HandleIdentSCCSDirective(Result);
884  case tok::pp_assert:
885  //isExtension = true; // FIXME: implement #assert
886  break;
887  case tok::pp_unassert:
888  //isExtension = true; // FIXME: implement #unassert
889  break;
890 
891  case tok::pp___public_macro:
892  if (getLangOpts().Modules)
893  return HandleMacroPublicDirective(Result);
894  break;
895 
896  case tok::pp___private_macro:
897  if (getLangOpts().Modules)
898  return HandleMacroPrivateDirective(Result);
899  break;
900  }
901  break;
902  }
903 
904  // If this is a .S file, treat unknown # directives as non-preprocessor
905  // directives. This is important because # may be a comment or introduce
906  // various pseudo-ops. Just return the # token and push back the following
907  // token to be lexed next time.
908  if (getLangOpts().AsmPreprocessor) {
909  Token *Toks = new Token[2];
910  // Return the # and the token after it.
911  Toks[0] = SavedHash;
912  Toks[1] = Result;
913 
914  // If the second token is a hashhash token, then we need to translate it to
915  // unknown so the token lexer doesn't try to perform token pasting.
916  if (Result.is(tok::hashhash))
917  Toks[1].setKind(tok::unknown);
918 
919  // Enter this token stream so that we re-lex the tokens. Make sure to
920  // enable macro expansion, in case the token after the # is an identifier
921  // that is expanded.
922  EnterTokenStream(Toks, 2, false, true);
923  return;
924  }
925 
926  // If we reached here, the preprocessing token is not valid!
927  Diag(Result, diag::err_pp_invalid_directive);
928 
929  // Read the rest of the PP line.
931 
932  // Okay, we're done parsing the directive.
933 }
934 
935 /// GetLineValue - Convert a numeric token into an unsigned value, emitting
936 /// Diagnostic DiagID if it is invalid, and returning the value in Val.
937 static bool GetLineValue(Token &DigitTok, unsigned &Val,
938  unsigned DiagID, Preprocessor &PP,
939  bool IsGNULineDirective=false) {
940  if (DigitTok.isNot(tok::numeric_constant)) {
941  PP.Diag(DigitTok, DiagID);
942 
943  if (DigitTok.isNot(tok::eod))
945  return true;
946  }
947 
948  SmallString<64> IntegerBuffer;
949  IntegerBuffer.resize(DigitTok.getLength());
950  const char *DigitTokBegin = &IntegerBuffer[0];
951  bool Invalid = false;
952  unsigned ActualLength = PP.getSpelling(DigitTok, DigitTokBegin, &Invalid);
953  if (Invalid)
954  return true;
955 
956  // Verify that we have a simple digit-sequence, and compute the value. This
957  // is always a simple digit string computed in decimal, so we do this manually
958  // here.
959  Val = 0;
960  for (unsigned i = 0; i != ActualLength; ++i) {
961  // C++1y [lex.fcon]p1:
962  // Optional separating single quotes in a digit-sequence are ignored
963  if (DigitTokBegin[i] == '\'')
964  continue;
965 
966  if (!isDigit(DigitTokBegin[i])) {
967  PP.Diag(PP.AdvanceToTokenCharacter(DigitTok.getLocation(), i),
968  diag::err_pp_line_digit_sequence) << IsGNULineDirective;
970  return true;
971  }
972 
973  unsigned NextVal = Val*10+(DigitTokBegin[i]-'0');
974  if (NextVal < Val) { // overflow.
975  PP.Diag(DigitTok, DiagID);
977  return true;
978  }
979  Val = NextVal;
980  }
981 
982  if (DigitTokBegin[0] == '0' && Val)
983  PP.Diag(DigitTok.getLocation(), diag::warn_pp_line_decimal)
984  << IsGNULineDirective;
985 
986  return false;
987 }
988 
989 /// \brief Handle a \#line directive: C99 6.10.4.
990 ///
991 /// The two acceptable forms are:
992 /// \verbatim
993 /// # line digit-sequence
994 /// # line digit-sequence "s-char-sequence"
995 /// \endverbatim
996 void Preprocessor::HandleLineDirective(Token &Tok) {
997  // Read the line # and string argument. Per C99 6.10.4p5, these tokens are
998  // expanded.
999  Token DigitTok;
1000  Lex(DigitTok);
1001 
1002  // Validate the number and convert it to an unsigned.
1003  unsigned LineNo;
1004  if (GetLineValue(DigitTok, LineNo, diag::err_pp_line_requires_integer,*this))
1005  return;
1006 
1007  if (LineNo == 0)
1008  Diag(DigitTok, diag::ext_pp_line_zero);
1009 
1010  // Enforce C99 6.10.4p3: "The digit sequence shall not specify ... a
1011  // number greater than 2147483647". C90 requires that the line # be <= 32767.
1012  unsigned LineLimit = 32768U;
1013  if (LangOpts.C99 || LangOpts.CPlusPlus11)
1014  LineLimit = 2147483648U;
1015  if (LineNo >= LineLimit)
1016  Diag(DigitTok, diag::ext_pp_line_too_big) << LineLimit;
1017  else if (LangOpts.CPlusPlus11 && LineNo >= 32768U)
1018  Diag(DigitTok, diag::warn_cxx98_compat_pp_line_too_big);
1019 
1020  int FilenameID = -1;
1021  Token StrTok;
1022  Lex(StrTok);
1023 
1024  // If the StrTok is "eod", then it wasn't present. Otherwise, it must be a
1025  // string followed by eod.
1026  if (StrTok.is(tok::eod))
1027  ; // ok
1028  else if (StrTok.isNot(tok::string_literal)) {
1029  Diag(StrTok, diag::err_pp_line_invalid_filename);
1030  return DiscardUntilEndOfDirective();
1031  } else if (StrTok.hasUDSuffix()) {
1032  Diag(StrTok, diag::err_invalid_string_udl);
1033  return DiscardUntilEndOfDirective();
1034  } else {
1035  // Parse and validate the string, converting it into a unique ID.
1036  StringLiteralParser Literal(StrTok, *this);
1037  assert(Literal.isAscii() && "Didn't allow wide strings in");
1038  if (Literal.hadError)
1039  return DiscardUntilEndOfDirective();
1040  if (Literal.Pascal) {
1041  Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
1042  return DiscardUntilEndOfDirective();
1043  }
1044  FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString());
1045 
1046  // Verify that there is nothing after the string, other than EOD. Because
1047  // of C99 6.10.4p5, macros that expand to empty tokens are ok.
1048  CheckEndOfDirective("line", true);
1049  }
1050 
1051  SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID);
1052 
1053  if (Callbacks)
1054  Callbacks->FileChanged(CurPPLexer->getSourceLocation(),
1056  SrcMgr::C_User);
1057 }
1058 
1059 /// ReadLineMarkerFlags - Parse and validate any flags at the end of a GNU line
1060 /// marker directive.
1061 static bool ReadLineMarkerFlags(bool &IsFileEntry, bool &IsFileExit,
1062  bool &IsSystemHeader, bool &IsExternCHeader,
1063  Preprocessor &PP) {
1064  unsigned FlagVal;
1065  Token FlagTok;
1066  PP.Lex(FlagTok);
1067  if (FlagTok.is(tok::eod)) return false;
1068  if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP))
1069  return true;
1070 
1071  if (FlagVal == 1) {
1072  IsFileEntry = true;
1073 
1074  PP.Lex(FlagTok);
1075  if (FlagTok.is(tok::eod)) return false;
1076  if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP))
1077  return true;
1078  } else if (FlagVal == 2) {
1079  IsFileExit = true;
1080 
1082  // If we are leaving the current presumed file, check to make sure the
1083  // presumed include stack isn't empty!
1084  FileID CurFileID =
1085  SM.getDecomposedExpansionLoc(FlagTok.getLocation()).first;
1086  PresumedLoc PLoc = SM.getPresumedLoc(FlagTok.getLocation());
1087  if (PLoc.isInvalid())
1088  return true;
1089 
1090  // If there is no include loc (main file) or if the include loc is in a
1091  // different physical file, then we aren't in a "1" line marker flag region.
1092  SourceLocation IncLoc = PLoc.getIncludeLoc();
1093  if (IncLoc.isInvalid() ||
1094  SM.getDecomposedExpansionLoc(IncLoc).first != CurFileID) {
1095  PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_pop);
1097  return true;
1098  }
1099 
1100  PP.Lex(FlagTok);
1101  if (FlagTok.is(tok::eod)) return false;
1102  if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP))
1103  return true;
1104  }
1105 
1106  // We must have 3 if there are still flags.
1107  if (FlagVal != 3) {
1108  PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
1110  return true;
1111  }
1112 
1113  IsSystemHeader = true;
1114 
1115  PP.Lex(FlagTok);
1116  if (FlagTok.is(tok::eod)) return false;
1117  if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP))
1118  return true;
1119 
1120  // We must have 4 if there is yet another flag.
1121  if (FlagVal != 4) {
1122  PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
1124  return true;
1125  }
1126 
1127  IsExternCHeader = true;
1128 
1129  PP.Lex(FlagTok);
1130  if (FlagTok.is(tok::eod)) return false;
1131 
1132  // There are no more valid flags here.
1133  PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
1135  return true;
1136 }
1137 
1138 /// HandleDigitDirective - Handle a GNU line marker directive, whose syntax is
1139 /// one of the following forms:
1140 ///
1141 /// # 42
1142 /// # 42 "file" ('1' | '2')?
1143 /// # 42 "file" ('1' | '2')? '3' '4'?
1144 ///
1145 void Preprocessor::HandleDigitDirective(Token &DigitTok) {
1146  // Validate the number and convert it to an unsigned. GNU does not have a
1147  // line # limit other than it fit in 32-bits.
1148  unsigned LineNo;
1149  if (GetLineValue(DigitTok, LineNo, diag::err_pp_linemarker_requires_integer,
1150  *this, true))
1151  return;
1152 
1153  Token StrTok;
1154  Lex(StrTok);
1155 
1156  bool IsFileEntry = false, IsFileExit = false;
1157  bool IsSystemHeader = false, IsExternCHeader = false;
1158  int FilenameID = -1;
1159 
1160  // If the StrTok is "eod", then it wasn't present. Otherwise, it must be a
1161  // string followed by eod.
1162  if (StrTok.is(tok::eod))
1163  ; // ok
1164  else if (StrTok.isNot(tok::string_literal)) {
1165  Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
1166  return DiscardUntilEndOfDirective();
1167  } else if (StrTok.hasUDSuffix()) {
1168  Diag(StrTok, diag::err_invalid_string_udl);
1169  return DiscardUntilEndOfDirective();
1170  } else {
1171  // Parse and validate the string, converting it into a unique ID.
1172  StringLiteralParser Literal(StrTok, *this);
1173  assert(Literal.isAscii() && "Didn't allow wide strings in");
1174  if (Literal.hadError)
1175  return DiscardUntilEndOfDirective();
1176  if (Literal.Pascal) {
1177  Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
1178  return DiscardUntilEndOfDirective();
1179  }
1180  FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString());
1181 
1182  // If a filename was present, read any flags that are present.
1183  if (ReadLineMarkerFlags(IsFileEntry, IsFileExit,
1184  IsSystemHeader, IsExternCHeader, *this))
1185  return;
1186  }
1187 
1188  // Create a line note with this information.
1189  SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID,
1190  IsFileEntry, IsFileExit,
1191  IsSystemHeader, IsExternCHeader);
1192 
1193  // If the preprocessor has callbacks installed, notify them of the #line
1194  // change. This is used so that the line marker comes out in -E mode for
1195  // example.
1196  if (Callbacks) {
1198  if (IsFileEntry)
1199  Reason = PPCallbacks::EnterFile;
1200  else if (IsFileExit)
1201  Reason = PPCallbacks::ExitFile;
1203  if (IsExternCHeader)
1204  FileKind = SrcMgr::C_ExternCSystem;
1205  else if (IsSystemHeader)
1206  FileKind = SrcMgr::C_System;
1207 
1208  Callbacks->FileChanged(CurPPLexer->getSourceLocation(), Reason, FileKind);
1209  }
1210 }
1211 
1212 
1213 /// HandleUserDiagnosticDirective - Handle a #warning or #error directive.
1214 ///
1215 void Preprocessor::HandleUserDiagnosticDirective(Token &Tok,
1216  bool isWarning) {
1217  // PTH doesn't emit #warning or #error directives.
1218  if (CurPTHLexer)
1219  return CurPTHLexer->DiscardToEndOfLine();
1220 
1221  // Read the rest of the line raw. We do this because we don't want macros
1222  // to be expanded and we don't require that the tokens be valid preprocessing
1223  // tokens. For example, this is allowed: "#warning ` 'foo". GCC does
1224  // collapse multiple consequtive white space between tokens, but this isn't
1225  // specified by the standard.
1226  SmallString<128> Message;
1227  CurLexer->ReadToEndOfLine(&Message);
1228 
1229  // Find the first non-whitespace character, so that we can make the
1230  // diagnostic more succinct.
1231  StringRef Msg = StringRef(Message).ltrim(" ");
1232 
1233  if (isWarning)
1234  Diag(Tok, diag::pp_hash_warning) << Msg;
1235  else
1236  Diag(Tok, diag::err_pp_hash_error) << Msg;
1237 }
1238 
1239 /// HandleIdentSCCSDirective - Handle a #ident/#sccs directive.
1240 ///
1241 void Preprocessor::HandleIdentSCCSDirective(Token &Tok) {
1242  // Yes, this directive is an extension.
1243  Diag(Tok, diag::ext_pp_ident_directive);
1244 
1245  // Read the string argument.
1246  Token StrTok;
1247  Lex(StrTok);
1248 
1249  // If the token kind isn't a string, it's a malformed directive.
1250  if (StrTok.isNot(tok::string_literal) &&
1251  StrTok.isNot(tok::wide_string_literal)) {
1252  Diag(StrTok, diag::err_pp_malformed_ident);
1253  if (StrTok.isNot(tok::eod))
1255  return;
1256  }
1257 
1258  if (StrTok.hasUDSuffix()) {
1259  Diag(StrTok, diag::err_invalid_string_udl);
1260  return DiscardUntilEndOfDirective();
1261  }
1262 
1263  // Verify that there is nothing after the string, other than EOD.
1264  CheckEndOfDirective("ident");
1265 
1266  if (Callbacks) {
1267  bool Invalid = false;
1268  std::string Str = getSpelling(StrTok, &Invalid);
1269  if (!Invalid)
1270  Callbacks->Ident(Tok.getLocation(), Str);
1271  }
1272 }
1273 
1274 /// \brief Handle a #public directive.
1275 void Preprocessor::HandleMacroPublicDirective(Token &Tok) {
1276  Token MacroNameTok;
1277  ReadMacroName(MacroNameTok, MU_Undef);
1278 
1279  // Error reading macro name? If so, diagnostic already issued.
1280  if (MacroNameTok.is(tok::eod))
1281  return;
1282 
1283  // Check to see if this is the last token on the #__public_macro line.
1284  CheckEndOfDirective("__public_macro");
1285 
1286  IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
1287  // Okay, we finally have a valid identifier to undef.
1289 
1290  // If the macro is not defined, this is an error.
1291  if (!MD) {
1292  Diag(MacroNameTok, diag::err_pp_visibility_non_macro) << II;
1293  return;
1294  }
1295 
1296  // Note that this macro has now been exported.
1297  appendMacroDirective(II, AllocateVisibilityMacroDirective(
1298  MacroNameTok.getLocation(), /*IsPublic=*/true));
1299 }
1300 
1301 /// \brief Handle a #private directive.
1302 void Preprocessor::HandleMacroPrivateDirective(Token &Tok) {
1303  Token MacroNameTok;
1304  ReadMacroName(MacroNameTok, MU_Undef);
1305 
1306  // Error reading macro name? If so, diagnostic already issued.
1307  if (MacroNameTok.is(tok::eod))
1308  return;
1309 
1310  // Check to see if this is the last token on the #__private_macro line.
1311  CheckEndOfDirective("__private_macro");
1312 
1313  IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
1314  // Okay, we finally have a valid identifier to undef.
1316 
1317  // If the macro is not defined, this is an error.
1318  if (!MD) {
1319  Diag(MacroNameTok, diag::err_pp_visibility_non_macro) << II;
1320  return;
1321  }
1322 
1323  // Note that this macro has now been marked private.
1324  appendMacroDirective(II, AllocateVisibilityMacroDirective(
1325  MacroNameTok.getLocation(), /*IsPublic=*/false));
1326 }
1327 
1328 //===----------------------------------------------------------------------===//
1329 // Preprocessor Include Directive Handling.
1330 //===----------------------------------------------------------------------===//
1331 
1332 /// GetIncludeFilenameSpelling - Turn the specified lexer token into a fully
1333 /// checked and spelled filename, e.g. as an operand of \#include. This returns
1334 /// true if the input filename was in <>'s or false if it were in ""'s. The
1335 /// caller is expected to provide a buffer that is large enough to hold the
1336 /// spelling of the filename, but is also expected to handle the case when
1337 /// this method decides to use a different buffer.
1339  StringRef &Buffer) {
1340  // Get the text form of the filename.
1341  assert(!Buffer.empty() && "Can't have tokens with empty spellings!");
1342 
1343  // Make sure the filename is <x> or "x".
1344  bool isAngled;
1345  if (Buffer[0] == '<') {
1346  if (Buffer.back() != '>') {
1347  Diag(Loc, diag::err_pp_expects_filename);
1348  Buffer = StringRef();
1349  return true;
1350  }
1351  isAngled = true;
1352  } else if (Buffer[0] == '"') {
1353  if (Buffer.back() != '"') {
1354  Diag(Loc, diag::err_pp_expects_filename);
1355  Buffer = StringRef();
1356  return true;
1357  }
1358  isAngled = false;
1359  } else {
1360  Diag(Loc, diag::err_pp_expects_filename);
1361  Buffer = StringRef();
1362  return true;
1363  }
1364 
1365  // Diagnose #include "" as invalid.
1366  if (Buffer.size() <= 2) {
1367  Diag(Loc, diag::err_pp_empty_filename);
1368  Buffer = StringRef();
1369  return true;
1370  }
1371 
1372  // Skip the brackets.
1373  Buffer = Buffer.substr(1, Buffer.size()-2);
1374  return isAngled;
1375 }
1376 
1377 // \brief Handle cases where the \#include name is expanded from a macro
1378 // as multiple tokens, which need to be glued together.
1379 //
1380 // This occurs for code like:
1381 // \code
1382 // \#define FOO <a/b.h>
1383 // \#include FOO
1384 // \endcode
1385 // because in this case, "<a/b.h>" is returned as 7 tokens, not one.
1386 //
1387 // This code concatenates and consumes tokens up to the '>' token. It returns
1388 // false if the > was found, otherwise it returns true if it finds and consumes
1389 // the EOD marker.
1391  SourceLocation &End) {
1392  Token CurTok;
1393 
1394  Lex(CurTok);
1395  while (CurTok.isNot(tok::eod)) {
1396  End = CurTok.getLocation();
1397 
1398  // FIXME: Provide code completion for #includes.
1399  if (CurTok.is(tok::code_completion)) {
1401  Lex(CurTok);
1402  continue;
1403  }
1404 
1405  // Append the spelling of this token to the buffer. If there was a space
1406  // before it, add it now.
1407  if (CurTok.hasLeadingSpace())
1408  FilenameBuffer.push_back(' ');
1409 
1410  // Get the spelling of the token, directly into FilenameBuffer if possible.
1411  unsigned PreAppendSize = FilenameBuffer.size();
1412  FilenameBuffer.resize(PreAppendSize+CurTok.getLength());
1413 
1414  const char *BufPtr = &FilenameBuffer[PreAppendSize];
1415  unsigned ActualLen = getSpelling(CurTok, BufPtr);
1416 
1417  // If the token was spelled somewhere else, copy it into FilenameBuffer.
1418  if (BufPtr != &FilenameBuffer[PreAppendSize])
1419  memcpy(&FilenameBuffer[PreAppendSize], BufPtr, ActualLen);
1420 
1421  // Resize FilenameBuffer to the correct size.
1422  if (CurTok.getLength() != ActualLen)
1423  FilenameBuffer.resize(PreAppendSize+ActualLen);
1424 
1425  // If we found the '>' marker, return success.
1426  if (CurTok.is(tok::greater))
1427  return false;
1428 
1429  Lex(CurTok);
1430  }
1431 
1432  // If we hit the eod marker, emit an error and return true so that the caller
1433  // knows the EOD has been read.
1434  Diag(CurTok.getLocation(), diag::err_pp_expects_filename);
1435  return true;
1436 }
1437 
1438 /// \brief Push a token onto the token stream containing an annotation.
1441  tok::TokenKind Kind, void *AnnotationVal) {
1442  // FIXME: Produce this as the current token directly, rather than
1443  // allocating a new token for it.
1444  Token *Tok = new Token[1];
1445  Tok[0].startToken();
1446  Tok[0].setKind(Kind);
1447  Tok[0].setLocation(Begin);
1448  Tok[0].setAnnotationEndLoc(End);
1449  Tok[0].setAnnotationValue(AnnotationVal);
1450  PP.EnterTokenStream(Tok, 1, true, true);
1451 }
1452 
1453 /// \brief Produce a diagnostic informing the user that a #include or similar
1454 /// was implicitly treated as a module import.
1456  Preprocessor &PP, SourceLocation HashLoc, Token &IncludeTok,
1457  ArrayRef<std::pair<IdentifierInfo *, SourceLocation>> Path,
1458  SourceLocation PathEnd) {
1459  assert(PP.getLangOpts().ObjC2 && "no import syntax available");
1460 
1461  SmallString<128> PathString;
1462  for (unsigned I = 0, N = Path.size(); I != N; ++I) {
1463  if (I)
1464  PathString += '.';
1465  PathString += Path[I].first->getName();
1466  }
1467  int IncludeKind = 0;
1468 
1469  switch (IncludeTok.getIdentifierInfo()->getPPKeywordID()) {
1470  case tok::pp_include:
1471  IncludeKind = 0;
1472  break;
1473 
1474  case tok::pp_import:
1475  IncludeKind = 1;
1476  break;
1477 
1478  case tok::pp_include_next:
1479  IncludeKind = 2;
1480  break;
1481 
1482  case tok::pp___include_macros:
1483  IncludeKind = 3;
1484  break;
1485 
1486  default:
1487  llvm_unreachable("unknown include directive kind");
1488  }
1489 
1490  CharSourceRange ReplaceRange(SourceRange(HashLoc, PathEnd),
1491  /*IsTokenRange=*/false);
1492  PP.Diag(HashLoc, diag::warn_auto_module_import)
1493  << IncludeKind << PathString
1494  << FixItHint::CreateReplacement(ReplaceRange,
1495  ("@import " + PathString + ";").str());
1496 }
1497 
1498 /// HandleIncludeDirective - The "\#include" tokens have just been read, read
1499 /// the file to be included from the lexer, then include it! This is a common
1500 /// routine with functionality shared between \#include, \#include_next and
1501 /// \#import. LookupFrom is set when this is a \#include_next directive, it
1502 /// specifies the file to start searching from.
1503 void Preprocessor::HandleIncludeDirective(SourceLocation HashLoc,
1504  Token &IncludeTok,
1505  const DirectoryLookup *LookupFrom,
1506  const FileEntry *LookupFromFile,
1507  bool isImport) {
1508 
1509  Token FilenameTok;
1510  CurPPLexer->LexIncludeFilename(FilenameTok);
1511 
1512  // Reserve a buffer to get the spelling.
1513  SmallString<128> FilenameBuffer;
1514  StringRef Filename;
1516  SourceLocation CharEnd; // the end of this directive, in characters
1517 
1518  switch (FilenameTok.getKind()) {
1519  case tok::eod:
1520  // If the token kind is EOD, the error has already been diagnosed.
1521  return;
1522 
1523  case tok::angle_string_literal:
1524  case tok::string_literal:
1525  Filename = getSpelling(FilenameTok, FilenameBuffer);
1526  End = FilenameTok.getLocation();
1527  CharEnd = End.getLocWithOffset(FilenameTok.getLength());
1528  break;
1529 
1530  case tok::less:
1531  // This could be a <foo/bar.h> file coming from a macro expansion. In this
1532  // case, glue the tokens together into FilenameBuffer and interpret those.
1533  FilenameBuffer.push_back('<');
1534  if (ConcatenateIncludeName(FilenameBuffer, End))
1535  return; // Found <eod> but no ">"? Diagnostic already emitted.
1536  Filename = FilenameBuffer;
1537  CharEnd = End.getLocWithOffset(1);
1538  break;
1539  default:
1540  Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
1542  return;
1543  }
1544 
1545  CharSourceRange FilenameRange
1546  = CharSourceRange::getCharRange(FilenameTok.getLocation(), CharEnd);
1547  StringRef OriginalFilename = Filename;
1548  bool isAngled =
1549  GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
1550  // If GetIncludeFilenameSpelling set the start ptr to null, there was an
1551  // error.
1552  if (Filename.empty()) {
1554  return;
1555  }
1556 
1557  // Verify that there is nothing after the filename, other than EOD. Note that
1558  // we allow macros that expand to nothing after the filename, because this
1559  // falls into the category of "#include pp-tokens new-line" specified in
1560  // C99 6.10.2p4.
1561  CheckEndOfDirective(IncludeTok.getIdentifierInfo()->getNameStart(), true);
1562 
1563  // Check that we don't have infinite #include recursion.
1564  if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1) {
1565  Diag(FilenameTok, diag::err_pp_include_too_deep);
1566  return;
1567  }
1568 
1569  // Complain about attempts to #include files in an audit pragma.
1570  if (PragmaARCCFCodeAuditedLoc.isValid()) {
1571  Diag(HashLoc, diag::err_pp_include_in_arc_cf_code_audited);
1572  Diag(PragmaARCCFCodeAuditedLoc, diag::note_pragma_entered_here);
1573 
1574  // Immediately leave the pragma.
1575  PragmaARCCFCodeAuditedLoc = SourceLocation();
1576  }
1577 
1578  // Complain about attempts to #include files in an assume-nonnull pragma.
1579  if (PragmaAssumeNonNullLoc.isValid()) {
1580  Diag(HashLoc, diag::err_pp_include_in_assume_nonnull);
1581  Diag(PragmaAssumeNonNullLoc, diag::note_pragma_entered_here);
1582 
1583  // Immediately leave the pragma.
1584  PragmaAssumeNonNullLoc = SourceLocation();
1585  }
1586 
1587  if (HeaderInfo.HasIncludeAliasMap()) {
1588  // Map the filename with the brackets still attached. If the name doesn't
1589  // map to anything, fall back on the filename we've already gotten the
1590  // spelling for.
1591  StringRef NewName = HeaderInfo.MapHeaderToIncludeAlias(OriginalFilename);
1592  if (!NewName.empty())
1593  Filename = NewName;
1594  }
1595 
1596  // Search include directories.
1597  const DirectoryLookup *CurDir;
1598  SmallString<1024> SearchPath;
1599  SmallString<1024> RelativePath;
1600  // We get the raw path only if we have 'Callbacks' to which we later pass
1601  // the path.
1602  ModuleMap::KnownHeader SuggestedModule;
1603  SourceLocation FilenameLoc = FilenameTok.getLocation();
1604  SmallString<128> NormalizedPath;
1605  if (LangOpts.MSVCCompat) {
1606  NormalizedPath = Filename.str();
1607 #ifndef LLVM_ON_WIN32
1608  llvm::sys::path::native(NormalizedPath);
1609 #endif
1610  }
1611  const FileEntry *File = LookupFile(
1612  FilenameLoc, LangOpts.MSVCCompat ? NormalizedPath.c_str() : Filename,
1613  isAngled, LookupFrom, LookupFromFile, CurDir,
1614  Callbacks ? &SearchPath : nullptr, Callbacks ? &RelativePath : nullptr,
1615  &SuggestedModule);
1616 
1617  if (!File) {
1618  if (Callbacks) {
1619  // Give the clients a chance to recover.
1620  SmallString<128> RecoveryPath;
1621  if (Callbacks->FileNotFound(Filename, RecoveryPath)) {
1622  if (const DirectoryEntry *DE = FileMgr.getDirectory(RecoveryPath)) {
1623  // Add the recovery path to the list of search paths.
1624  DirectoryLookup DL(DE, SrcMgr::C_User, false);
1625  HeaderInfo.AddSearchPath(DL, isAngled);
1626 
1627  // Try the lookup again, skipping the cache.
1628  File = LookupFile(
1629  FilenameLoc,
1630  LangOpts.MSVCCompat ? NormalizedPath.c_str() : Filename, isAngled,
1631  LookupFrom, LookupFromFile, CurDir, nullptr, nullptr,
1632  &SuggestedModule, /*SkipCache*/ true);
1633  }
1634  }
1635  }
1636 
1637  if (!SuppressIncludeNotFoundError) {
1638  // If the file could not be located and it was included via angle
1639  // brackets, we can attempt a lookup as though it were a quoted path to
1640  // provide the user with a possible fixit.
1641  if (isAngled) {
1642  File = LookupFile(
1643  FilenameLoc,
1644  LangOpts.MSVCCompat ? NormalizedPath.c_str() : Filename, false,
1645  LookupFrom, LookupFromFile, CurDir,
1646  Callbacks ? &SearchPath : nullptr,
1647  Callbacks ? &RelativePath : nullptr,
1648  &SuggestedModule);
1649  if (File) {
1650  SourceRange Range(FilenameTok.getLocation(), CharEnd);
1651  Diag(FilenameTok, diag::err_pp_file_not_found_not_fatal) <<
1652  Filename <<
1653  FixItHint::CreateReplacement(Range, "\"" + Filename.str() + "\"");
1654  }
1655  }
1656 
1657  // If the file is still not found, just go with the vanilla diagnostic
1658  if (!File)
1659  Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
1660  }
1661  }
1662 
1663  // Should we enter the source file? Set to false if either the source file is
1664  // known to have no effect beyond its effect on module visibility -- that is,
1665  // if it's got an include guard that is already defined or is a modular header
1666  // we've imported or already built.
1667  bool ShouldEnter = true;
1668 
1669  // Determine whether we should try to import the module for this #include, if
1670  // there is one. Don't do so if precompiled module support is disabled or we
1671  // are processing this module textually (because we're building the module).
1672  if (File && SuggestedModule && getLangOpts().Modules &&
1673  SuggestedModule.getModule()->getTopLevelModuleName() !=
1675  SuggestedModule.getModule()->getTopLevelModuleName() !=
1677  // Compute the module access path corresponding to this module.
1678  // FIXME: Should we have a second loadModule() overload to avoid this
1679  // extra lookup step?
1681  for (Module *Mod = SuggestedModule.getModule(); Mod; Mod = Mod->Parent)
1682  Path.push_back(std::make_pair(getIdentifierInfo(Mod->Name),
1683  FilenameTok.getLocation()));
1684  std::reverse(Path.begin(), Path.end());
1685 
1686  // Warn that we're replacing the include/import with a module import.
1687  // We only do this in Objective-C, where we have a module-import syntax.
1688  if (getLangOpts().ObjC2)
1689  diagnoseAutoModuleImport(*this, HashLoc, IncludeTok, Path, CharEnd);
1690 
1691  // Load the module to import its macros. We'll make the declarations
1692  // visible when the parser gets here.
1693  // FIXME: Pass SuggestedModule in here rather than converting it to a path
1694  // and making the module loader convert it back again.
1695  ModuleLoadResult Imported = TheModuleLoader.loadModule(
1696  IncludeTok.getLocation(), Path, Module::Hidden,
1697  /*IsIncludeDirective=*/true);
1698  assert((Imported == nullptr || Imported == SuggestedModule.getModule()) &&
1699  "the imported module is different than the suggested one");
1700 
1701  if (Imported)
1702  ShouldEnter = false;
1703  else if (Imported.isMissingExpected()) {
1704  // We failed to find a submodule that we assumed would exist (because it
1705  // was in the directory of an umbrella header, for instance), but no
1706  // actual module exists for it (because the umbrella header is
1707  // incomplete). Treat this as a textual inclusion.
1708  SuggestedModule = ModuleMap::KnownHeader();
1709  } else {
1710  // We hit an error processing the import. Bail out.
1712  // With a fatal failure in the module loader, we abort parsing.
1713  Token &Result = IncludeTok;
1714  if (CurLexer) {
1715  Result.startToken();
1716  CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd, tok::eof);
1717  CurLexer->cutOffLexing();
1718  } else {
1719  assert(CurPTHLexer && "#include but no current lexer set!");
1720  CurPTHLexer->getEOF(Result);
1721  }
1722  }
1723  return;
1724  }
1725  }
1726 
1727  if (Callbacks) {
1728  // Notify the callback object that we've seen an inclusion directive.
1729  Callbacks->InclusionDirective(
1730  HashLoc, IncludeTok,
1731  LangOpts.MSVCCompat ? NormalizedPath.c_str() : Filename, isAngled,
1732  FilenameRange, File, SearchPath, RelativePath,
1733  ShouldEnter ? nullptr : SuggestedModule.getModule());
1734  }
1735 
1736  if (!File)
1737  return;
1738 
1739  // The #included file will be considered to be a system header if either it is
1740  // in a system include directory, or if the #includer is a system include
1741  // header.
1742  SrcMgr::CharacteristicKind FileCharacter =
1743  std::max(HeaderInfo.getFileDirFlavor(File),
1744  SourceMgr.getFileCharacteristic(FilenameTok.getLocation()));
1745 
1746  // FIXME: If we have a suggested module, and we've already visited this file,
1747  // don't bother entering it again. We know it has no further effect.
1748 
1749  // Ask HeaderInfo if we should enter this #include file. If not, #including
1750  // this file will have no effect.
1751  if (ShouldEnter &&
1752  !HeaderInfo.ShouldEnterIncludeFile(*this, File, isImport,
1753  SuggestedModule.getModule())) {
1754  ShouldEnter = false;
1755  if (Callbacks)
1756  Callbacks->FileSkipped(*File, FilenameTok, FileCharacter);
1757  }
1758 
1759  // If we don't need to enter the file, stop now.
1760  if (!ShouldEnter) {
1761  // If this is a module import, make it visible if needed.
1762  if (auto *M = SuggestedModule.getModule()) {
1763  makeModuleVisible(M, HashLoc);
1764 
1765  if (IncludeTok.getIdentifierInfo()->getPPKeywordID() !=
1766  tok::pp___include_macros)
1767  EnterAnnotationToken(*this, HashLoc, End, tok::annot_module_include, M);
1768  }
1769  return;
1770  }
1771 
1772  // Look up the file, create a File ID for it.
1773  SourceLocation IncludePos = End;
1774  // If the filename string was the result of macro expansions, set the include
1775  // position on the file where it will be included and after the expansions.
1776  if (IncludePos.isMacroID())
1777  IncludePos = SourceMgr.getExpansionRange(IncludePos).second;
1778  FileID FID = SourceMgr.createFileID(File, IncludePos, FileCharacter);
1779  assert(!FID.isInvalid() && "Expected valid file ID");
1780 
1781  // If all is good, enter the new file!
1782  if (EnterSourceFile(FID, CurDir, FilenameTok.getLocation()))
1783  return;
1784 
1785  // Determine if we're switching to building a new submodule, and which one.
1786  if (auto *M = SuggestedModule.getModule()) {
1787  assert(!CurSubmodule && "should not have marked this as a module yet");
1788  CurSubmodule = M;
1789 
1790  // Let the macro handling code know that any future macros are within
1791  // the new submodule.
1792  EnterSubmodule(M, HashLoc);
1793 
1794  // Let the parser know that any future declarations are within the new
1795  // submodule.
1796  // FIXME: There's no point doing this if we're handling a #__include_macros
1797  // directive.
1798  EnterAnnotationToken(*this, HashLoc, End, tok::annot_module_begin, M);
1799  }
1800 }
1801 
1802 /// HandleIncludeNextDirective - Implements \#include_next.
1803 ///
1804 void Preprocessor::HandleIncludeNextDirective(SourceLocation HashLoc,
1805  Token &IncludeNextTok) {
1806  Diag(IncludeNextTok, diag::ext_pp_include_next_directive);
1807 
1808  // #include_next is like #include, except that we start searching after
1809  // the current found directory. If we can't do this, issue a
1810  // diagnostic.
1811  const DirectoryLookup *Lookup = CurDirLookup;
1812  const FileEntry *LookupFromFile = nullptr;
1813  if (isInPrimaryFile()) {
1814  Lookup = nullptr;
1815  Diag(IncludeNextTok, diag::pp_include_next_in_primary);
1816  } else if (CurSubmodule) {
1817  // Start looking up in the directory *after* the one in which the current
1818  // file would be found, if any.
1819  assert(CurPPLexer && "#include_next directive in macro?");
1820  LookupFromFile = CurPPLexer->getFileEntry();
1821  Lookup = nullptr;
1822  } else if (!Lookup) {
1823  Diag(IncludeNextTok, diag::pp_include_next_absolute_path);
1824  } else {
1825  // Start looking up in the next directory.
1826  ++Lookup;
1827  }
1828 
1829  return HandleIncludeDirective(HashLoc, IncludeNextTok, Lookup,
1830  LookupFromFile);
1831 }
1832 
1833 /// HandleMicrosoftImportDirective - Implements \#import for Microsoft Mode
1834 void Preprocessor::HandleMicrosoftImportDirective(Token &Tok) {
1835  // The Microsoft #import directive takes a type library and generates header
1836  // files from it, and includes those. This is beyond the scope of what clang
1837  // does, so we ignore it and error out. However, #import can optionally have
1838  // trailing attributes that span multiple lines. We're going to eat those
1839  // so we can continue processing from there.
1840  Diag(Tok, diag::err_pp_import_directive_ms );
1841 
1842  // Read tokens until we get to the end of the directive. Note that the
1843  // directive can be split over multiple lines using the backslash character.
1845 }
1846 
1847 /// HandleImportDirective - Implements \#import.
1848 ///
1849 void Preprocessor::HandleImportDirective(SourceLocation HashLoc,
1850  Token &ImportTok) {
1851  if (!LangOpts.ObjC1) { // #import is standard for ObjC.
1852  if (LangOpts.MSVCCompat)
1853  return HandleMicrosoftImportDirective(ImportTok);
1854  Diag(ImportTok, diag::ext_pp_import_directive);
1855  }
1856  return HandleIncludeDirective(HashLoc, ImportTok, nullptr, nullptr, true);
1857 }
1858 
1859 /// HandleIncludeMacrosDirective - The -imacros command line option turns into a
1860 /// pseudo directive in the predefines buffer. This handles it by sucking all
1861 /// tokens through the preprocessor and discarding them (only keeping the side
1862 /// effects on the preprocessor).
1863 void Preprocessor::HandleIncludeMacrosDirective(SourceLocation HashLoc,
1864  Token &IncludeMacrosTok) {
1865  // This directive should only occur in the predefines buffer. If not, emit an
1866  // error and reject it.
1867  SourceLocation Loc = IncludeMacrosTok.getLocation();
1868  if (strcmp(SourceMgr.getBufferName(Loc), "<built-in>") != 0) {
1869  Diag(IncludeMacrosTok.getLocation(),
1870  diag::pp_include_macros_out_of_predefines);
1872  return;
1873  }
1874 
1875  // Treat this as a normal #include for checking purposes. If this is
1876  // successful, it will push a new lexer onto the include stack.
1877  HandleIncludeDirective(HashLoc, IncludeMacrosTok);
1878 
1879  Token TmpTok;
1880  do {
1881  Lex(TmpTok);
1882  assert(TmpTok.isNot(tok::eof) && "Didn't find end of -imacros!");
1883  } while (TmpTok.isNot(tok::hashhash));
1884 }
1885 
1886 //===----------------------------------------------------------------------===//
1887 // Preprocessor Macro Directive Handling.
1888 //===----------------------------------------------------------------------===//
1889 
1890 /// ReadMacroDefinitionArgList - The ( starting an argument list of a macro
1891 /// definition has just been read. Lex the rest of the arguments and the
1892 /// closing ), updating MI with what we learn. Return true if an error occurs
1893 /// parsing the arg list.
1894 bool Preprocessor::ReadMacroDefinitionArgList(MacroInfo *MI, Token &Tok) {
1896 
1897  while (1) {
1898  LexUnexpandedToken(Tok);
1899  switch (Tok.getKind()) {
1900  case tok::r_paren:
1901  // Found the end of the argument list.
1902  if (Arguments.empty()) // #define FOO()
1903  return false;
1904  // Otherwise we have #define FOO(A,)
1905  Diag(Tok, diag::err_pp_expected_ident_in_arg_list);
1906  return true;
1907  case tok::ellipsis: // #define X(... -> C99 varargs
1908  if (!LangOpts.C99)
1909  Diag(Tok, LangOpts.CPlusPlus11 ?
1910  diag::warn_cxx98_compat_variadic_macro :
1911  diag::ext_variadic_macro);
1912 
1913  // OpenCL v1.2 s6.9.e: variadic macros are not supported.
1914  if (LangOpts.OpenCL) {
1915  Diag(Tok, diag::err_pp_opencl_variadic_macros);
1916  return true;
1917  }
1918 
1919  // Lex the token after the identifier.
1920  LexUnexpandedToken(Tok);
1921  if (Tok.isNot(tok::r_paren)) {
1922  Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
1923  return true;
1924  }
1925  // Add the __VA_ARGS__ identifier as an argument.
1926  Arguments.push_back(Ident__VA_ARGS__);
1927  MI->setIsC99Varargs();
1928  MI->setArgumentList(&Arguments[0], Arguments.size(), BP);
1929  return false;
1930  case tok::eod: // #define X(
1931  Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
1932  return true;
1933  default:
1934  // Handle keywords and identifiers here to accept things like
1935  // #define Foo(for) for.
1936  IdentifierInfo *II = Tok.getIdentifierInfo();
1937  if (!II) {
1938  // #define X(1
1939  Diag(Tok, diag::err_pp_invalid_tok_in_arg_list);
1940  return true;
1941  }
1942 
1943  // If this is already used as an argument, it is used multiple times (e.g.
1944  // #define X(A,A.
1945  if (std::find(Arguments.begin(), Arguments.end(), II) !=
1946  Arguments.end()) { // C99 6.10.3p6
1947  Diag(Tok, diag::err_pp_duplicate_name_in_arg_list) << II;
1948  return true;
1949  }
1950 
1951  // Add the argument to the macro info.
1952  Arguments.push_back(II);
1953 
1954  // Lex the token after the identifier.
1955  LexUnexpandedToken(Tok);
1956 
1957  switch (Tok.getKind()) {
1958  default: // #define X(A B
1959  Diag(Tok, diag::err_pp_expected_comma_in_arg_list);
1960  return true;
1961  case tok::r_paren: // #define X(A)
1962  MI->setArgumentList(&Arguments[0], Arguments.size(), BP);
1963  return false;
1964  case tok::comma: // #define X(A,
1965  break;
1966  case tok::ellipsis: // #define X(A... -> GCC extension
1967  // Diagnose extension.
1968  Diag(Tok, diag::ext_named_variadic_macro);
1969 
1970  // Lex the token after the identifier.
1971  LexUnexpandedToken(Tok);
1972  if (Tok.isNot(tok::r_paren)) {
1973  Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
1974  return true;
1975  }
1976 
1977  MI->setIsGNUVarargs();
1978  MI->setArgumentList(&Arguments[0], Arguments.size(), BP);
1979  return false;
1980  }
1981  }
1982  }
1983 }
1984 
1985 static bool isConfigurationPattern(Token &MacroName, MacroInfo *MI,
1986  const LangOptions &LOptions) {
1987  if (MI->getNumTokens() == 1) {
1988  const Token &Value = MI->getReplacementToken(0);
1989 
1990  // Macro that is identity, like '#define inline inline' is a valid pattern.
1991  if (MacroName.getKind() == Value.getKind())
1992  return true;
1993 
1994  // Macro that maps a keyword to the same keyword decorated with leading/
1995  // trailing underscores is a valid pattern:
1996  // #define inline __inline
1997  // #define inline __inline__
1998  // #define inline _inline (in MS compatibility mode)
1999  StringRef MacroText = MacroName.getIdentifierInfo()->getName();
2000  if (IdentifierInfo *II = Value.getIdentifierInfo()) {
2001  if (!II->isKeyword(LOptions))
2002  return false;
2003  StringRef ValueText = II->getName();
2004  StringRef TrimmedValue = ValueText;
2005  if (!ValueText.startswith("__")) {
2006  if (ValueText.startswith("_"))
2007  TrimmedValue = TrimmedValue.drop_front(1);
2008  else
2009  return false;
2010  } else {
2011  TrimmedValue = TrimmedValue.drop_front(2);
2012  if (TrimmedValue.endswith("__"))
2013  TrimmedValue = TrimmedValue.drop_back(2);
2014  }
2015  return TrimmedValue.equals(MacroText);
2016  } else {
2017  return false;
2018  }
2019  }
2020 
2021  // #define inline
2022  if (MacroName.isOneOf(tok::kw_extern, tok::kw_inline, tok::kw_static,
2023  tok::kw_const) &&
2024  MI->getNumTokens() == 0) {
2025  return true;
2026  }
2027 
2028  return false;
2029 }
2030 
2031 /// HandleDefineDirective - Implements \#define. This consumes the entire macro
2032 /// line then lets the caller lex the next real token.
2033 void Preprocessor::HandleDefineDirective(Token &DefineTok,
2034  bool ImmediatelyAfterHeaderGuard) {
2035  ++NumDefined;
2036 
2037  Token MacroNameTok;
2038  bool MacroShadowsKeyword;
2039  ReadMacroName(MacroNameTok, MU_Define, &MacroShadowsKeyword);
2040 
2041  // Error reading macro name? If so, diagnostic already issued.
2042  if (MacroNameTok.is(tok::eod))
2043  return;
2044 
2045  Token LastTok = MacroNameTok;
2046 
2047  // If we are supposed to keep comments in #defines, reenable comment saving
2048  // mode.
2049  if (CurLexer) CurLexer->SetCommentRetentionState(KeepMacroComments);
2050 
2051  // Create the new macro.
2052  MacroInfo *MI = AllocateMacroInfo(MacroNameTok.getLocation());
2053 
2054  Token Tok;
2055  LexUnexpandedToken(Tok);
2056 
2057  // If this is a function-like macro definition, parse the argument list,
2058  // marking each of the identifiers as being used as macro arguments. Also,
2059  // check other constraints on the first token of the macro body.
2060  if (Tok.is(tok::eod)) {
2061  if (ImmediatelyAfterHeaderGuard) {
2062  // Save this macro information since it may part of a header guard.
2063  CurPPLexer->MIOpt.SetDefinedMacro(MacroNameTok.getIdentifierInfo(),
2064  MacroNameTok.getLocation());
2065  }
2066  // If there is no body to this macro, we have no special handling here.
2067  } else if (Tok.hasLeadingSpace()) {
2068  // This is a normal token with leading space. Clear the leading space
2069  // marker on the first token to get proper expansion.
2070  Tok.clearFlag(Token::LeadingSpace);
2071  } else if (Tok.is(tok::l_paren)) {
2072  // This is a function-like macro definition. Read the argument list.
2073  MI->setIsFunctionLike();
2074  if (ReadMacroDefinitionArgList(MI, LastTok)) {
2075  // Throw away the rest of the line.
2076  if (CurPPLexer->ParsingPreprocessorDirective)
2078  return;
2079  }
2080 
2081  // If this is a definition of a variadic C99 function-like macro, not using
2082  // the GNU named varargs extension, enabled __VA_ARGS__.
2083 
2084  // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro.
2085  // This gets unpoisoned where it is allowed.
2086  assert(Ident__VA_ARGS__->isPoisoned() && "__VA_ARGS__ should be poisoned!");
2087  if (MI->isC99Varargs())
2088  Ident__VA_ARGS__->setIsPoisoned(false);
2089 
2090  // Read the first token after the arg list for down below.
2091  LexUnexpandedToken(Tok);
2092  } else if (LangOpts.C99 || LangOpts.CPlusPlus11) {
2093  // C99 requires whitespace between the macro definition and the body. Emit
2094  // a diagnostic for something like "#define X+".
2095  Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name);
2096  } else {
2097  // C90 6.8 TC1 says: "In the definition of an object-like macro, if the
2098  // first character of a replacement list is not a character required by
2099  // subclause 5.2.1, then there shall be white-space separation between the
2100  // identifier and the replacement list.". 5.2.1 lists this set:
2101  // "A-Za-z0-9!"#%&'()*+,_./:;<=>?[\]^_{|}~" as well as whitespace, which
2102  // is irrelevant here.
2103  bool isInvalid = false;
2104  if (Tok.is(tok::at)) // @ is not in the list above.
2105  isInvalid = true;
2106  else if (Tok.is(tok::unknown)) {
2107  // If we have an unknown token, it is something strange like "`". Since
2108  // all of valid characters would have lexed into a single character
2109  // token of some sort, we know this is not a valid case.
2110  isInvalid = true;
2111  }
2112  if (isInvalid)
2113  Diag(Tok, diag::ext_missing_whitespace_after_macro_name);
2114  else
2115  Diag(Tok, diag::warn_missing_whitespace_after_macro_name);
2116  }
2117 
2118  if (!Tok.is(tok::eod))
2119  LastTok = Tok;
2120 
2121  // Read the rest of the macro body.
2122  if (MI->isObjectLike()) {
2123  // Object-like macros are very simple, just read their body.
2124  while (Tok.isNot(tok::eod)) {
2125  LastTok = Tok;
2126  MI->AddTokenToBody(Tok);
2127  // Get the next token of the macro.
2128  LexUnexpandedToken(Tok);
2129  }
2130 
2131  } else {
2132  // Otherwise, read the body of a function-like macro. While we are at it,
2133  // check C99 6.10.3.2p1: ensure that # operators are followed by macro
2134  // parameters in function-like macro expansions.
2135  while (Tok.isNot(tok::eod)) {
2136  LastTok = Tok;
2137 
2138  if (Tok.isNot(tok::hash) && Tok.isNot(tok::hashhash)) {
2139  MI->AddTokenToBody(Tok);
2140 
2141  // Get the next token of the macro.
2142  LexUnexpandedToken(Tok);
2143  continue;
2144  }
2145 
2146  // If we're in -traditional mode, then we should ignore stringification
2147  // and token pasting. Mark the tokens as unknown so as not to confuse
2148  // things.
2149  if (getLangOpts().TraditionalCPP) {
2150  Tok.setKind(tok::unknown);
2151  MI->AddTokenToBody(Tok);
2152 
2153  // Get the next token of the macro.
2154  LexUnexpandedToken(Tok);
2155  continue;
2156  }
2157 
2158  if (Tok.is(tok::hashhash)) {
2159 
2160  // If we see token pasting, check if it looks like the gcc comma
2161  // pasting extension. We'll use this information to suppress
2162  // diagnostics later on.
2163 
2164  // Get the next token of the macro.
2165  LexUnexpandedToken(Tok);
2166 
2167  if (Tok.is(tok::eod)) {
2168  MI->AddTokenToBody(LastTok);
2169  break;
2170  }
2171 
2172  unsigned NumTokens = MI->getNumTokens();
2173  if (NumTokens && Tok.getIdentifierInfo() == Ident__VA_ARGS__ &&
2174  MI->getReplacementToken(NumTokens-1).is(tok::comma))
2175  MI->setHasCommaPasting();
2176 
2177  // Things look ok, add the '##' token to the macro.
2178  MI->AddTokenToBody(LastTok);
2179  continue;
2180  }
2181 
2182  // Get the next token of the macro.
2183  LexUnexpandedToken(Tok);
2184 
2185  // Check for a valid macro arg identifier.
2186  if (Tok.getIdentifierInfo() == nullptr ||
2187  MI->getArgumentNum(Tok.getIdentifierInfo()) == -1) {
2188 
2189  // If this is assembler-with-cpp mode, we accept random gibberish after
2190  // the '#' because '#' is often a comment character. However, change
2191  // the kind of the token to tok::unknown so that the preprocessor isn't
2192  // confused.
2193  if (getLangOpts().AsmPreprocessor && Tok.isNot(tok::eod)) {
2194  LastTok.setKind(tok::unknown);
2195  MI->AddTokenToBody(LastTok);
2196  continue;
2197  } else {
2198  Diag(Tok, diag::err_pp_stringize_not_parameter);
2199 
2200  // Disable __VA_ARGS__ again.
2201  Ident__VA_ARGS__->setIsPoisoned(true);
2202  return;
2203  }
2204  }
2205 
2206  // Things look ok, add the '#' and param name tokens to the macro.
2207  MI->AddTokenToBody(LastTok);
2208  MI->AddTokenToBody(Tok);
2209  LastTok = Tok;
2210 
2211  // Get the next token of the macro.
2212  LexUnexpandedToken(Tok);
2213  }
2214  }
2215 
2216  if (MacroShadowsKeyword &&
2217  !isConfigurationPattern(MacroNameTok, MI, getLangOpts())) {
2218  Diag(MacroNameTok, diag::warn_pp_macro_hides_keyword);
2219  }
2220 
2221  // Disable __VA_ARGS__ again.
2222  Ident__VA_ARGS__->setIsPoisoned(true);
2223 
2224  // Check that there is no paste (##) operator at the beginning or end of the
2225  // replacement list.
2226  unsigned NumTokens = MI->getNumTokens();
2227  if (NumTokens != 0) {
2228  if (MI->getReplacementToken(0).is(tok::hashhash)) {
2229  Diag(MI->getReplacementToken(0), diag::err_paste_at_start);
2230  return;
2231  }
2232  if (MI->getReplacementToken(NumTokens-1).is(tok::hashhash)) {
2233  Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end);
2234  return;
2235  }
2236  }
2237 
2238  MI->setDefinitionEndLoc(LastTok.getLocation());
2239 
2240  // Finally, if this identifier already had a macro defined for it, verify that
2241  // the macro bodies are identical, and issue diagnostics if they are not.
2242  if (const MacroInfo *OtherMI=getMacroInfo(MacroNameTok.getIdentifierInfo())) {
2243  // It is very common for system headers to have tons of macro redefinitions
2244  // and for warnings to be disabled in system headers. If this is the case,
2245  // then don't bother calling MacroInfo::isIdenticalTo.
2247  !SourceMgr.isInSystemHeader(DefineTok.getLocation())) {
2248  if (!OtherMI->isUsed() && OtherMI->isWarnIfUnused())
2249  Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used);
2250 
2251  // Warn if defining "__LINE__" and other builtins, per C99 6.10.8/4 and
2252  // C++ [cpp.predefined]p4, but allow it as an extension.
2253  if (OtherMI->isBuiltinMacro())
2254  Diag(MacroNameTok, diag::ext_pp_redef_builtin_macro);
2255  // Macros must be identical. This means all tokens and whitespace
2256  // separation must be the same. C99 6.10.3p2.
2257  else if (!OtherMI->isAllowRedefinitionsWithoutWarning() &&
2258  !MI->isIdenticalTo(*OtherMI, *this, /*Syntactic=*/LangOpts.MicrosoftExt)) {
2259  Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef)
2260  << MacroNameTok.getIdentifierInfo();
2261  Diag(OtherMI->getDefinitionLoc(), diag::note_previous_definition);
2262  }
2263  }
2264  if (OtherMI->isWarnIfUnused())
2265  WarnUnusedMacroLocs.erase(OtherMI->getDefinitionLoc());
2266  }
2267 
2268  DefMacroDirective *MD =
2269  appendDefMacroDirective(MacroNameTok.getIdentifierInfo(), MI);
2270 
2271  assert(!MI->isUsed());
2272  // If we need warning for not using the macro, add its location in the
2273  // warn-because-unused-macro set. If it gets used it will be removed from set.
2275  !Diags->isIgnored(diag::pp_macro_not_used, MI->getDefinitionLoc())) {
2276  MI->setIsWarnIfUnused(true);
2277  WarnUnusedMacroLocs.insert(MI->getDefinitionLoc());
2278  }
2279 
2280  // If the callbacks want to know, tell them about the macro definition.
2281  if (Callbacks)
2282  Callbacks->MacroDefined(MacroNameTok, MD);
2283 }
2284 
2285 /// HandleUndefDirective - Implements \#undef.
2286 ///
2287 void Preprocessor::HandleUndefDirective(Token &UndefTok) {
2288  ++NumUndefined;
2289 
2290  Token MacroNameTok;
2291  ReadMacroName(MacroNameTok, MU_Undef);
2292 
2293  // Error reading macro name? If so, diagnostic already issued.
2294  if (MacroNameTok.is(tok::eod))
2295  return;
2296 
2297  // Check to see if this is the last token on the #undef line.
2298  CheckEndOfDirective("undef");
2299 
2300  // Okay, we have a valid identifier to undef.
2301  auto *II = MacroNameTok.getIdentifierInfo();
2302  auto MD = getMacroDefinition(II);
2303 
2304  // If the callbacks want to know, tell them about the macro #undef.
2305  // Note: no matter if the macro was defined or not.
2306  if (Callbacks)
2307  Callbacks->MacroUndefined(MacroNameTok, MD);
2308 
2309  // If the macro is not defined, this is a noop undef, just return.
2310  const MacroInfo *MI = MD.getMacroInfo();
2311  if (!MI)
2312  return;
2313 
2314  if (!MI->isUsed() && MI->isWarnIfUnused())
2315  Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used);
2316 
2317  if (MI->isWarnIfUnused())
2318  WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());
2319 
2320  appendMacroDirective(MacroNameTok.getIdentifierInfo(),
2321  AllocateUndefMacroDirective(MacroNameTok.getLocation()));
2322 }
2323 
2324 
2325 //===----------------------------------------------------------------------===//
2326 // Preprocessor Conditional Directive Handling.
2327 //===----------------------------------------------------------------------===//
2328 
2329 /// HandleIfdefDirective - Implements the \#ifdef/\#ifndef directive. isIfndef
2330 /// is true when this is a \#ifndef directive. ReadAnyTokensBeforeDirective is
2331 /// true if any tokens have been returned or pp-directives activated before this
2332 /// \#ifndef has been lexed.
2333 ///
2334 void Preprocessor::HandleIfdefDirective(Token &Result, bool isIfndef,
2335  bool ReadAnyTokensBeforeDirective) {
2336  ++NumIf;
2337  Token DirectiveTok = Result;
2338 
2339  Token MacroNameTok;
2340  ReadMacroName(MacroNameTok);
2341 
2342  // Error reading macro name? If so, diagnostic already issued.
2343  if (MacroNameTok.is(tok::eod)) {
2344  // Skip code until we get to #endif. This helps with recovery by not
2345  // emitting an error when the #endif is reached.
2346  SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
2347  /*Foundnonskip*/false, /*FoundElse*/false);
2348  return;
2349  }
2350 
2351  // Check to see if this is the last token on the #if[n]def line.
2352  CheckEndOfDirective(isIfndef ? "ifndef" : "ifdef");
2353 
2354  IdentifierInfo *MII = MacroNameTok.getIdentifierInfo();
2355  auto MD = getMacroDefinition(MII);
2356  MacroInfo *MI = MD.getMacroInfo();
2357 
2358  if (CurPPLexer->getConditionalStackDepth() == 0) {
2359  // If the start of a top-level #ifdef and if the macro is not defined,
2360  // inform MIOpt that this might be the start of a proper include guard.
2361  // Otherwise it is some other form of unknown conditional which we can't
2362  // handle.
2363  if (!ReadAnyTokensBeforeDirective && !MI) {
2364  assert(isIfndef && "#ifdef shouldn't reach here");
2365  CurPPLexer->MIOpt.EnterTopLevelIfndef(MII, MacroNameTok.getLocation());
2366  } else
2367  CurPPLexer->MIOpt.EnterTopLevelConditional();
2368  }
2369 
2370  // If there is a macro, process it.
2371  if (MI) // Mark it used.
2372  markMacroAsUsed(MI);
2373 
2374  if (Callbacks) {
2375  if (isIfndef)
2376  Callbacks->Ifndef(DirectiveTok.getLocation(), MacroNameTok, MD);
2377  else
2378  Callbacks->Ifdef(DirectiveTok.getLocation(), MacroNameTok, MD);
2379  }
2380 
2381  // Should we include the stuff contained by this directive?
2382  if (!MI == isIfndef) {
2383  // Yes, remember that we are inside a conditional, then lex the next token.
2384  CurPPLexer->pushConditionalLevel(DirectiveTok.getLocation(),
2385  /*wasskip*/false, /*foundnonskip*/true,
2386  /*foundelse*/false);
2387  } else {
2388  // No, skip the contents of this block.
2389  SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
2390  /*Foundnonskip*/false,
2391  /*FoundElse*/false);
2392  }
2393 }
2394 
2395 /// HandleIfDirective - Implements the \#if directive.
2396 ///
2397 void Preprocessor::HandleIfDirective(Token &IfToken,
2398  bool ReadAnyTokensBeforeDirective) {
2399  ++NumIf;
2400 
2401  // Parse and evaluate the conditional expression.
2402  IdentifierInfo *IfNDefMacro = nullptr;
2403  const SourceLocation ConditionalBegin = CurPPLexer->getSourceLocation();
2404  const bool ConditionalTrue = EvaluateDirectiveExpression(IfNDefMacro);
2405  const SourceLocation ConditionalEnd = CurPPLexer->getSourceLocation();
2406 
2407  // If this condition is equivalent to #ifndef X, and if this is the first
2408  // directive seen, handle it for the multiple-include optimization.
2409  if (CurPPLexer->getConditionalStackDepth() == 0) {
2410  if (!ReadAnyTokensBeforeDirective && IfNDefMacro && ConditionalTrue)
2411  // FIXME: Pass in the location of the macro name, not the 'if' token.
2412  CurPPLexer->MIOpt.EnterTopLevelIfndef(IfNDefMacro, IfToken.getLocation());
2413  else
2414  CurPPLexer->MIOpt.EnterTopLevelConditional();
2415  }
2416 
2417  if (Callbacks)
2418  Callbacks->If(IfToken.getLocation(),
2419  SourceRange(ConditionalBegin, ConditionalEnd),
2420  (ConditionalTrue ? PPCallbacks::CVK_True : PPCallbacks::CVK_False));
2421 
2422  // Should we include the stuff contained by this directive?
2423  if (ConditionalTrue) {
2424  // Yes, remember that we are inside a conditional, then lex the next token.
2425  CurPPLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
2426  /*foundnonskip*/true, /*foundelse*/false);
2427  } else {
2428  // No, skip the contents of this block.
2429  SkipExcludedConditionalBlock(IfToken.getLocation(), /*Foundnonskip*/false,
2430  /*FoundElse*/false);
2431  }
2432 }
2433 
2434 /// HandleEndifDirective - Implements the \#endif directive.
2435 ///
2436 void Preprocessor::HandleEndifDirective(Token &EndifToken) {
2437  ++NumEndif;
2438 
2439  // Check that this is the whole directive.
2440  CheckEndOfDirective("endif");
2441 
2442  PPConditionalInfo CondInfo;
2443  if (CurPPLexer->popConditionalLevel(CondInfo)) {
2444  // No conditionals on the stack: this is an #endif without an #if.
2445  Diag(EndifToken, diag::err_pp_endif_without_if);
2446  return;
2447  }
2448 
2449  // If this the end of a top-level #endif, inform MIOpt.
2450  if (CurPPLexer->getConditionalStackDepth() == 0)
2451  CurPPLexer->MIOpt.ExitTopLevelConditional();
2452 
2453  assert(!CondInfo.WasSkipping && !CurPPLexer->LexingRawMode &&
2454  "This code should only be reachable in the non-skipping case!");
2455 
2456  if (Callbacks)
2457  Callbacks->Endif(EndifToken.getLocation(), CondInfo.IfLoc);
2458 }
2459 
2460 /// HandleElseDirective - Implements the \#else directive.
2461 ///
2462 void Preprocessor::HandleElseDirective(Token &Result) {
2463  ++NumElse;
2464 
2465  // #else directive in a non-skipping conditional... start skipping.
2466  CheckEndOfDirective("else");
2467 
2468  PPConditionalInfo CI;
2469  if (CurPPLexer->popConditionalLevel(CI)) {
2470  Diag(Result, diag::pp_err_else_without_if);
2471  return;
2472  }
2473 
2474  // If this is a top-level #else, inform the MIOpt.
2475  if (CurPPLexer->getConditionalStackDepth() == 0)
2476  CurPPLexer->MIOpt.EnterTopLevelConditional();
2477 
2478  // If this is a #else with a #else before it, report the error.
2479  if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else);
2480 
2481  if (Callbacks)
2482  Callbacks->Else(Result.getLocation(), CI.IfLoc);
2483 
2484  // Finally, skip the rest of the contents of this block.
2485  SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
2486  /*FoundElse*/true, Result.getLocation());
2487 }
2488 
2489 /// HandleElifDirective - Implements the \#elif directive.
2490 ///
2491 void Preprocessor::HandleElifDirective(Token &ElifToken) {
2492  ++NumElse;
2493 
2494  // #elif directive in a non-skipping conditional... start skipping.
2495  // We don't care what the condition is, because we will always skip it (since
2496  // the block immediately before it was included).
2497  const SourceLocation ConditionalBegin = CurPPLexer->getSourceLocation();
2499  const SourceLocation ConditionalEnd = CurPPLexer->getSourceLocation();
2500 
2501  PPConditionalInfo CI;
2502  if (CurPPLexer->popConditionalLevel(CI)) {
2503  Diag(ElifToken, diag::pp_err_elif_without_if);
2504  return;
2505  }
2506 
2507  // If this is a top-level #elif, inform the MIOpt.
2508  if (CurPPLexer->getConditionalStackDepth() == 0)
2509  CurPPLexer->MIOpt.EnterTopLevelConditional();
2510 
2511  // If this is a #elif with a #else before it, report the error.
2512  if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else);
2513 
2514  if (Callbacks)
2515  Callbacks->Elif(ElifToken.getLocation(),
2516  SourceRange(ConditionalBegin, ConditionalEnd),
2518 
2519  // Finally, skip the rest of the contents of this block.
2520  SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
2521  /*FoundElse*/CI.FoundElse,
2522  ElifToken.getLocation());
2523 }
bool isAtStartOfLine() const
Definition: Token.h:261
Module * getModule() const
Retrieve the module the header is stored in.
Definition: ModuleMap.h:98
SourceManager & getSourceManager() const
Definition: Preprocessor.h:682
bool isPoisoned() const
Return true if this token has been poisoned.
bool getHasReadAnyTokensVal() const
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. If the location is in a file ...
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:246
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.
void setArgumentList(IdentifierInfo *const *List, unsigned NumArgs, llvm::BumpPtrAllocator &PPAllocator)
Set the specified list of identifiers as the argument list for this macro.
Definition: MacroInfo.h:161
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. This function does not chec...
Defines the SourceManager interface.
IdentifierInfo * getIdentifierInfo(StringRef Name) const
Definition: Preprocessor.h:922
static bool ReadLineMarkerFlags(bool &IsFileEntry, bool &IsFileExit, bool &IsSystemHeader, bool &IsExternCHeader, Preprocessor &PP)
Module * getCurrentModule()
Retrieves the module that we're currently building, if any.
void pushConditionalLevel(SourceLocation DirectiveStart, bool WasSkipping, bool FoundNonSkip, bool FoundElse)
bool isObjectLike() const
Definition: MacroInfo.h:198
Defines the clang::MacroInfo and clang::MacroDirective classes.
bool hasLeadingSpace() const
Return true if this token has whitespace before it.
Definition: Token.h:265
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:441
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
CharacteristicKind
Indicates whether a file or directory holds normal user code, system code, or system code which is im...
Definition: SourceManager.h:78
ModuleMap & getModuleMap()
Retrieve the module map.
Definition: HeaderSearch.h:585
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
static MacroDiag shouldWarnOnMacroDef(Preprocessor &PP, IdentifierInfo *II)
void setIsGNUVarargs()
Definition: MacroInfo.h:202
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:867
A directive for a defined macro or a macro imported from a module.
Definition: MacroInfo.h:419
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:306
bool getImmediatelyAfterTopLevelIfndef() const
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:846
const LangOptions & getLangOpts() const
Definition: Preprocessor.h:679
virtual void CodeCompleteDirective(bool InConditional)
Callback invoked when performing code completion for a preprocessor directive.
bool isWarnIfUnused() const
Return true if we should emit a warning if the macro is unused.
Definition: MacroInfo.h:228
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:49
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)
A directive for setting the module visibility of a macro.
Definition: MacroInfo.h:455
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.
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. This will be NULL for the top-level module.
Definition: Basic/Module.h:59
bool hadModuleLoaderFatalFailure() const
Definition: Preprocessor.h:706
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. Only useful in Skipping blocks.
Definition: Token.h:314
const FileEntry * getFileEntry() const
DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID) const
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:245
StringRef getRawIdentifier() const
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
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:216
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)
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:187
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:232
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.
The result type of a method or function.
bool isC99Varargs() const
Definition: MacroInfo.h:203
StringRef getTopLevelModuleName() const
Retrieve the name of the top-level module.
Definition: Basic/Module.h:372
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:430
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()
Definition: MacroInfo.h:196
Encapsulates changes to the "macros namespace" (the location where the macro name became active...
Definition: MacroInfo.h:308
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. The SourceManager can decode this to get at the full include stack...
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:805
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:835
All of the names in this module are hidden.
Definition: Basic/Module.h:207
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:201
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:234
FileID getMainFileID() const
Returns the FileID of the main source file.
bool is(tok::TokenKind K) const
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:676
SourceLocation IfLoc
Location where the conditional started.
Definition: Token.h:306
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
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:894
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:404
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. Prevents semantic imports...
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. its tokens).
Definition: MacroInfo.h:34
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. as an operand of #incl...
bool isInvalid() const
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
void AddSearchPath(const DirectoryLookup &dir, bool isAngled)
Add an additional search path.
Definition: HeaderSearch.h:292
bool isBuiltinMacro() const
Return true if this macro requires processing before expansion.
Definition: MacroInfo.h:213
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:337
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)
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...
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.
const FileEntry * LookupSubframeworkHeader(StringRef Filename, const FileEntry *RelativeFileEnt, SmallVectorImpl< char > *SearchPath, SmallVectorImpl< char > *RelativePath, ModuleMap::KnownHeader *SuggestedModule)
Look up a subframework for the specified #include file.
A SourceLocation and its associated SourceManager.
StringRef MapHeaderToIncludeAlias(StringRef Source)
Definition: HeaderSearch.h:322
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:60
Annotates a diagnostic with some code that should be inserted, removed, or replaced to fix the proble...
Definition: Diagnostic.h:52
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...
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. If so, #elif/#else directives are not allowed.
Definition: Token.h:318
A header that is known to reside within a given module, whether it was included or excluded...
Definition: ModuleMap.h:90
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, ModuleMap::KnownHeader *SuggestedModule, bool SkipCache=false)
Given a "foo" or <foo> reference, look up the indicated file, return null on failure.
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:220
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.