clang  8.0.0svn
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 /// Implements # directive processing for the Preprocessor.
12 ///
13 //===----------------------------------------------------------------------===//
14 
15 #include "clang/Basic/CharInfo.h"
19 #include "clang/Basic/Module.h"
22 #include "clang/Basic/TokenKinds.h"
24 #include "clang/Lex/HeaderSearch.h"
27 #include "clang/Lex/MacroInfo.h"
28 #include "clang/Lex/ModuleLoader.h"
29 #include "clang/Lex/ModuleMap.h"
30 #include "clang/Lex/PPCallbacks.h"
31 #include "clang/Lex/Pragma.h"
32 #include "clang/Lex/Preprocessor.h"
34 #include "clang/Lex/Token.h"
36 #include "llvm/ADT/ArrayRef.h"
37 #include "llvm/ADT/SmallString.h"
38 #include "llvm/ADT/SmallVector.h"
39 #include "llvm/ADT/STLExtras.h"
40 #include "llvm/ADT/StringSwitch.h"
41 #include "llvm/ADT/StringRef.h"
42 #include "llvm/Support/AlignOf.h"
43 #include "llvm/Support/ErrorHandling.h"
44 #include "llvm/Support/Path.h"
45 #include <algorithm>
46 #include <cassert>
47 #include <cstring>
48 #include <new>
49 #include <string>
50 #include <utility>
51 
52 using namespace clang;
53 
54 //===----------------------------------------------------------------------===//
55 // Utility Methods for Preprocessor Directive Handling.
56 //===----------------------------------------------------------------------===//
57 
59  auto *MIChain = new (BP) MacroInfoChain{L, MIChainHead};
60  MIChainHead = MIChain;
61  return &MIChain->MI;
62 }
63 
64 DefMacroDirective *Preprocessor::AllocateDefMacroDirective(MacroInfo *MI,
65  SourceLocation Loc) {
66  return new (BP) DefMacroDirective(MI, Loc);
67 }
68 
70 Preprocessor::AllocateUndefMacroDirective(SourceLocation UndefLoc) {
71  return new (BP) UndefMacroDirective(UndefLoc);
72 }
73 
75 Preprocessor::AllocateVisibilityMacroDirective(SourceLocation Loc,
76  bool isPublic) {
77  return new (BP) VisibilityMacroDirective(Loc, isPublic);
78 }
79 
80 /// Read and discard all tokens remaining on the current line until
81 /// the tok::eod token is found.
83  Token Tmp;
84  do {
85  LexUnexpandedToken(Tmp);
86  assert(Tmp.isNot(tok::eof) && "EOF seen while discarding directive tokens");
87  } while (Tmp.isNot(tok::eod));
88 }
89 
90 /// Enumerates possible cases of #define/#undef a reserved identifier.
91 enum MacroDiag {
92  MD_NoWarn, //> Not a reserved identifier
93  MD_KeywordDef, //> Macro hides keyword, enabled by default
94  MD_ReservedMacro //> #define of #undef reserved id, disabled by default
95 };
96 
97 /// Checks if the specified identifier is reserved in the specified
98 /// language.
99 /// This function does not check if the identifier is a keyword.
100 static bool isReservedId(StringRef Text, const LangOptions &Lang) {
101  // C++ [macro.names], C11 7.1.3:
102  // All identifiers that begin with an underscore and either an uppercase
103  // letter or another underscore are always reserved for any use.
104  if (Text.size() >= 2 && Text[0] == '_' &&
105  (isUppercase(Text[1]) || Text[1] == '_'))
106  return true;
107  // C++ [global.names]
108  // Each name that contains a double underscore ... is reserved to the
109  // implementation for any use.
110  if (Lang.CPlusPlus) {
111  if (Text.find("__") != StringRef::npos)
112  return true;
113  }
114  return false;
115 }
116 
117 // The -fmodule-name option tells the compiler to textually include headers in
118 // the specified module, meaning clang won't build the specified module. This is
119 // useful in a number of situations, for instance, when building a library that
120 // vends a module map, one might want to avoid hitting intermediate build
121 // products containimg the the module map or avoid finding the system installed
122 // modulemap for that library.
123 static bool isForModuleBuilding(Module *M, StringRef CurrentModule,
124  StringRef ModuleName) {
125  StringRef TopLevelName = M->getTopLevelModuleName();
126 
127  // When building framework Foo, we wanna make sure that Foo *and* Foo_Private
128  // are textually included and no modules are built for both.
129  if (M->getTopLevelModule()->IsFramework && CurrentModule == ModuleName &&
130  !CurrentModule.endswith("_Private") && TopLevelName.endswith("_Private"))
131  TopLevelName = TopLevelName.drop_back(8);
132 
133  return TopLevelName == CurrentModule;
134 }
135 
137  const LangOptions &Lang = PP.getLangOpts();
138  StringRef Text = II->getName();
139  if (isReservedId(Text, Lang))
140  return MD_ReservedMacro;
141  if (II->isKeyword(Lang))
142  return MD_KeywordDef;
143  if (Lang.CPlusPlus11 && (Text.equals("override") || Text.equals("final")))
144  return MD_KeywordDef;
145  return MD_NoWarn;
146 }
147 
149  const LangOptions &Lang = PP.getLangOpts();
150  StringRef Text = II->getName();
151  // Do not warn on keyword undef. It is generally harmless and widely used.
152  if (isReservedId(Text, Lang))
153  return MD_ReservedMacro;
154  return MD_NoWarn;
155 }
156 
157 // Return true if we want to issue a diagnostic by default if we
158 // encounter this name in a #include with the wrong case. For now,
159 // this includes the standard C and C++ headers, Posix headers,
160 // and Boost headers. Improper case for these #includes is a
161 // potential portability issue.
162 static bool warnByDefaultOnWrongCase(StringRef Include) {
163  // If the first component of the path is "boost", treat this like a standard header
164  // for the purposes of diagnostics.
165  if (::llvm::sys::path::begin(Include)->equals_lower("boost"))
166  return true;
167 
168  // "condition_variable" is the longest standard header name at 18 characters.
169  // If the include file name is longer than that, it can't be a standard header.
170  static const size_t MaxStdHeaderNameLen = 18u;
171  if (Include.size() > MaxStdHeaderNameLen)
172  return false;
173 
174  // Lowercase and normalize the search string.
175  SmallString<32> LowerInclude{Include};
176  for (char &Ch : LowerInclude) {
177  // In the ASCII range?
178  if (static_cast<unsigned char>(Ch) > 0x7f)
179  return false; // Can't be a standard header
180  // ASCII lowercase:
181  if (Ch >= 'A' && Ch <= 'Z')
182  Ch += 'a' - 'A';
183  // Normalize path separators for comparison purposes.
184  else if (::llvm::sys::path::is_separator(Ch))
185  Ch = '/';
186  }
187 
188  // The standard C/C++ and Posix headers
189  return llvm::StringSwitch<bool>(LowerInclude)
190  // C library headers
191  .Cases("assert.h", "complex.h", "ctype.h", "errno.h", "fenv.h", true)
192  .Cases("float.h", "inttypes.h", "iso646.h", "limits.h", "locale.h", true)
193  .Cases("math.h", "setjmp.h", "signal.h", "stdalign.h", "stdarg.h", true)
194  .Cases("stdatomic.h", "stdbool.h", "stddef.h", "stdint.h", "stdio.h", true)
195  .Cases("stdlib.h", "stdnoreturn.h", "string.h", "tgmath.h", "threads.h", true)
196  .Cases("time.h", "uchar.h", "wchar.h", "wctype.h", true)
197 
198  // C++ headers for C library facilities
199  .Cases("cassert", "ccomplex", "cctype", "cerrno", "cfenv", true)
200  .Cases("cfloat", "cinttypes", "ciso646", "climits", "clocale", true)
201  .Cases("cmath", "csetjmp", "csignal", "cstdalign", "cstdarg", true)
202  .Cases("cstdbool", "cstddef", "cstdint", "cstdio", "cstdlib", true)
203  .Cases("cstring", "ctgmath", "ctime", "cuchar", "cwchar", true)
204  .Case("cwctype", true)
205 
206  // C++ library headers
207  .Cases("algorithm", "fstream", "list", "regex", "thread", true)
208  .Cases("array", "functional", "locale", "scoped_allocator", "tuple", true)
209  .Cases("atomic", "future", "map", "set", "type_traits", true)
210  .Cases("bitset", "initializer_list", "memory", "shared_mutex", "typeindex", true)
211  .Cases("chrono", "iomanip", "mutex", "sstream", "typeinfo", true)
212  .Cases("codecvt", "ios", "new", "stack", "unordered_map", true)
213  .Cases("complex", "iosfwd", "numeric", "stdexcept", "unordered_set", true)
214  .Cases("condition_variable", "iostream", "ostream", "streambuf", "utility", true)
215  .Cases("deque", "istream", "queue", "string", "valarray", true)
216  .Cases("exception", "iterator", "random", "strstream", "vector", true)
217  .Cases("forward_list", "limits", "ratio", "system_error", true)
218 
219  // POSIX headers (which aren't also C headers)
220  .Cases("aio.h", "arpa/inet.h", "cpio.h", "dirent.h", "dlfcn.h", true)
221  .Cases("fcntl.h", "fmtmsg.h", "fnmatch.h", "ftw.h", "glob.h", true)
222  .Cases("grp.h", "iconv.h", "langinfo.h", "libgen.h", "monetary.h", true)
223  .Cases("mqueue.h", "ndbm.h", "net/if.h", "netdb.h", "netinet/in.h", true)
224  .Cases("netinet/tcp.h", "nl_types.h", "poll.h", "pthread.h", "pwd.h", true)
225  .Cases("regex.h", "sched.h", "search.h", "semaphore.h", "spawn.h", true)
226  .Cases("strings.h", "stropts.h", "sys/ipc.h", "sys/mman.h", "sys/msg.h", true)
227  .Cases("sys/resource.h", "sys/select.h", "sys/sem.h", "sys/shm.h", "sys/socket.h", true)
228  .Cases("sys/stat.h", "sys/statvfs.h", "sys/time.h", "sys/times.h", "sys/types.h", true)
229  .Cases("sys/uio.h", "sys/un.h", "sys/utsname.h", "sys/wait.h", "syslog.h", true)
230  .Cases("tar.h", "termios.h", "trace.h", "ulimit.h", true)
231  .Cases("unistd.h", "utime.h", "utmpx.h", "wordexp.h", true)
232  .Default(false);
233 }
234 
235 bool Preprocessor::CheckMacroName(Token &MacroNameTok, MacroUse isDefineUndef,
236  bool *ShadowFlag) {
237  // Missing macro name?
238  if (MacroNameTok.is(tok::eod))
239  return Diag(MacroNameTok, diag::err_pp_missing_macro_name);
240 
241  IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
242  if (!II)
243  return Diag(MacroNameTok, diag::err_pp_macro_not_identifier);
244 
245  if (II->isCPlusPlusOperatorKeyword()) {
246  // C++ 2.5p2: Alternative tokens behave the same as its primary token
247  // except for their spellings.
248  Diag(MacroNameTok, getLangOpts().MicrosoftExt
249  ? diag::ext_pp_operator_used_as_macro_name
250  : diag::err_pp_operator_used_as_macro_name)
251  << II << MacroNameTok.getKind();
252  // Allow #defining |and| and friends for Microsoft compatibility or
253  // recovery when legacy C headers are included in C++.
254  }
255 
256  if ((isDefineUndef != MU_Other) && II->getPPKeywordID() == tok::pp_defined) {
257  // Error if defining "defined": C99 6.10.8/4, C++ [cpp.predefined]p4.
258  return Diag(MacroNameTok, diag::err_defined_macro_name);
259  }
260 
261  if (isDefineUndef == MU_Undef) {
262  auto *MI = getMacroInfo(II);
263  if (MI && MI->isBuiltinMacro()) {
264  // Warn if undefining "__LINE__" and other builtins, per C99 6.10.8/4
265  // and C++ [cpp.predefined]p4], but allow it as an extension.
266  Diag(MacroNameTok, diag::ext_pp_undef_builtin_macro);
267  }
268  }
269 
270  // If defining/undefining reserved identifier or a keyword, we need to issue
271  // a warning.
272  SourceLocation MacroNameLoc = MacroNameTok.getLocation();
273  if (ShadowFlag)
274  *ShadowFlag = false;
275  if (!SourceMgr.isInSystemHeader(MacroNameLoc) &&
276  (SourceMgr.getBufferName(MacroNameLoc) != "<built-in>")) {
277  MacroDiag D = MD_NoWarn;
278  if (isDefineUndef == MU_Define) {
279  D = shouldWarnOnMacroDef(*this, II);
280  }
281  else if (isDefineUndef == MU_Undef)
282  D = shouldWarnOnMacroUndef(*this, II);
283  if (D == MD_KeywordDef) {
284  // We do not want to warn on some patterns widely used in configuration
285  // scripts. This requires analyzing next tokens, so do not issue warnings
286  // now, only inform caller.
287  if (ShadowFlag)
288  *ShadowFlag = true;
289  }
290  if (D == MD_ReservedMacro)
291  Diag(MacroNameTok, diag::warn_pp_macro_is_reserved_id);
292  }
293 
294  // Okay, we got a good identifier.
295  return false;
296 }
297 
298 /// Lex and validate a macro name, which occurs after a
299 /// \#define or \#undef.
300 ///
301 /// This sets the token kind to eod and discards the rest of the macro line if
302 /// the macro name is invalid.
303 ///
304 /// \param MacroNameTok Token that is expected to be a macro name.
305 /// \param isDefineUndef Context in which macro is used.
306 /// \param ShadowFlag Points to a flag that is set if macro shadows a keyword.
307 void Preprocessor::ReadMacroName(Token &MacroNameTok, MacroUse isDefineUndef,
308  bool *ShadowFlag) {
309  // Read the token, don't allow macro expansion on it.
310  LexUnexpandedToken(MacroNameTok);
311 
312  if (MacroNameTok.is(tok::code_completion)) {
313  if (CodeComplete)
314  CodeComplete->CodeCompleteMacroName(isDefineUndef == MU_Define);
316  LexUnexpandedToken(MacroNameTok);
317  }
318 
319  if (!CheckMacroName(MacroNameTok, isDefineUndef, ShadowFlag))
320  return;
321 
322  // Invalid macro name, read and discard the rest of the line and set the
323  // token kind to tok::eod if necessary.
324  if (MacroNameTok.isNot(tok::eod)) {
325  MacroNameTok.setKind(tok::eod);
327  }
328 }
329 
330 /// Ensure that the next token is a tok::eod token.
331 ///
332 /// If not, emit a diagnostic and consume up until the eod. If EnableMacros is
333 /// true, then we consider macros that expand to zero tokens as being ok.
334 void Preprocessor::CheckEndOfDirective(const char *DirType, bool EnableMacros) {
335  Token Tmp;
336  // Lex unexpanded tokens for most directives: macros might expand to zero
337  // tokens, causing us to miss diagnosing invalid lines. Some directives (like
338  // #line) allow empty macros.
339  if (EnableMacros)
340  Lex(Tmp);
341  else
342  LexUnexpandedToken(Tmp);
343 
344  // There should be no tokens after the directive, but we allow them as an
345  // extension.
346  while (Tmp.is(tok::comment)) // Skip comments in -C mode.
347  LexUnexpandedToken(Tmp);
348 
349  if (Tmp.isNot(tok::eod)) {
350  // Add a fixit in GNU/C99/C++ mode. Don't offer a fixit for strict-C89,
351  // or if this is a macro-style preprocessing directive, because it is more
352  // trouble than it is worth to insert /**/ and check that there is no /**/
353  // in the range also.
354  FixItHint Hint;
355  if ((LangOpts.GNUMode || LangOpts.C99 || LangOpts.CPlusPlus) &&
356  !CurTokenLexer)
357  Hint = FixItHint::CreateInsertion(Tmp.getLocation(),"//");
358  Diag(Tmp, diag::ext_pp_extra_tokens_at_eol) << DirType << Hint;
360  }
361 }
362 
363 /// SkipExcludedConditionalBlock - We just read a \#if or related directive and
364 /// decided that the subsequent tokens are in the \#if'd out portion of the
365 /// file. Lex the rest of the file, until we see an \#endif. If
366 /// FoundNonSkipPortion is true, then we have already emitted code for part of
367 /// this \#if directive, so \#else/\#elif blocks should never be entered.
368 /// If ElseOk is true, then \#else directives are ok, if not, then we have
369 /// already seen one so a \#else directive is a duplicate. When this returns,
370 /// the caller can lex the first valid token.
371 void Preprocessor::SkipExcludedConditionalBlock(SourceLocation HashTokenLoc,
372  SourceLocation IfTokenLoc,
373  bool FoundNonSkipPortion,
374  bool FoundElse,
375  SourceLocation ElseLoc) {
376  ++NumSkipped;
377  assert(!CurTokenLexer && CurPPLexer && "Lexing a macro, not a file?");
378 
379  if (PreambleConditionalStack.reachedEOFWhileSkipping())
380  PreambleConditionalStack.clearSkipInfo();
381  else
382  CurPPLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/ false,
383  FoundNonSkipPortion, FoundElse);
384 
385  // Enter raw mode to disable identifier lookup (and thus macro expansion),
386  // disabling warnings, etc.
387  CurPPLexer->LexingRawMode = true;
388  Token Tok;
389  while (true) {
390  CurLexer->Lex(Tok);
391 
392  if (Tok.is(tok::code_completion)) {
393  if (CodeComplete)
394  CodeComplete->CodeCompleteInConditionalExclusion();
396  continue;
397  }
398 
399  // If this is the end of the buffer, we have an error.
400  if (Tok.is(tok::eof)) {
401  // We don't emit errors for unterminated conditionals here,
402  // Lexer::LexEndOfFile can do that properly.
403  // Just return and let the caller lex after this #include.
404  if (PreambleConditionalStack.isRecording())
405  PreambleConditionalStack.SkipInfo.emplace(
406  HashTokenLoc, IfTokenLoc, FoundNonSkipPortion, FoundElse, ElseLoc);
407  break;
408  }
409 
410  // If this token is not a preprocessor directive, just skip it.
411  if (Tok.isNot(tok::hash) || !Tok.isAtStartOfLine())
412  continue;
413 
414  // We just parsed a # character at the start of a line, so we're in
415  // directive mode. Tell the lexer this so any newlines we see will be
416  // converted into an EOD token (this terminates the macro).
417  CurPPLexer->ParsingPreprocessorDirective = true;
418  if (CurLexer) CurLexer->SetKeepWhitespaceMode(false);
419 
420 
421  // Read the next token, the directive flavor.
422  LexUnexpandedToken(Tok);
423 
424  // If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or
425  // something bogus), skip it.
426  if (Tok.isNot(tok::raw_identifier)) {
427  CurPPLexer->ParsingPreprocessorDirective = false;
428  // Restore comment saving mode.
429  if (CurLexer) CurLexer->resetExtendedTokenMode();
430  continue;
431  }
432 
433  // If the first letter isn't i or e, it isn't intesting to us. We know that
434  // this is safe in the face of spelling differences, because there is no way
435  // to spell an i/e in a strange way that is another letter. Skipping this
436  // allows us to avoid looking up the identifier info for #define/#undef and
437  // other common directives.
438  StringRef RI = Tok.getRawIdentifier();
439 
440  char FirstChar = RI[0];
441  if (FirstChar >= 'a' && FirstChar <= 'z' &&
442  FirstChar != 'i' && FirstChar != 'e') {
443  CurPPLexer->ParsingPreprocessorDirective = false;
444  // Restore comment saving mode.
445  if (CurLexer) CurLexer->resetExtendedTokenMode();
446  continue;
447  }
448 
449  // Get the identifier name without trigraphs or embedded newlines. Note
450  // that we can't use Tok.getIdentifierInfo() because its lookup is disabled
451  // when skipping.
452  char DirectiveBuf[20];
453  StringRef Directive;
454  if (!Tok.needsCleaning() && RI.size() < 20) {
455  Directive = RI;
456  } else {
457  std::string DirectiveStr = getSpelling(Tok);
458  size_t IdLen = DirectiveStr.size();
459  if (IdLen >= 20) {
460  CurPPLexer->ParsingPreprocessorDirective = false;
461  // Restore comment saving mode.
462  if (CurLexer) CurLexer->resetExtendedTokenMode();
463  continue;
464  }
465  memcpy(DirectiveBuf, &DirectiveStr[0], IdLen);
466  Directive = StringRef(DirectiveBuf, IdLen);
467  }
468 
469  if (Directive.startswith("if")) {
470  StringRef Sub = Directive.substr(2);
471  if (Sub.empty() || // "if"
472  Sub == "def" || // "ifdef"
473  Sub == "ndef") { // "ifndef"
474  // We know the entire #if/#ifdef/#ifndef block will be skipped, don't
475  // bother parsing the condition.
477  CurPPLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true,
478  /*foundnonskip*/false,
479  /*foundelse*/false);
480  }
481  } else if (Directive[0] == 'e') {
482  StringRef Sub = Directive.substr(1);
483  if (Sub == "ndif") { // "endif"
484  PPConditionalInfo CondInfo;
485  CondInfo.WasSkipping = true; // Silence bogus warning.
486  bool InCond = CurPPLexer->popConditionalLevel(CondInfo);
487  (void)InCond; // Silence warning in no-asserts mode.
488  assert(!InCond && "Can't be skipping if not in a conditional!");
489 
490  // If we popped the outermost skipping block, we're done skipping!
491  if (!CondInfo.WasSkipping) {
492  // Restore the value of LexingRawMode so that trailing comments
493  // are handled correctly, if we've reached the outermost block.
494  CurPPLexer->LexingRawMode = false;
495  CheckEndOfDirective("endif");
496  CurPPLexer->LexingRawMode = true;
497  if (Callbacks)
498  Callbacks->Endif(Tok.getLocation(), CondInfo.IfLoc);
499  break;
500  } else {
502  }
503  } else if (Sub == "lse") { // "else".
504  // #else directive in a skipping conditional. If not in some other
505  // skipping conditional, and if #else hasn't already been seen, enter it
506  // as a non-skipping conditional.
507  PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel();
508 
509  // If this is a #else with a #else before it, report the error.
510  if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_else_after_else);
511 
512  // Note that we've seen a #else in this conditional.
513  CondInfo.FoundElse = true;
514 
515  // If the conditional is at the top level, and the #if block wasn't
516  // entered, enter the #else block now.
517  if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) {
518  CondInfo.FoundNonSkip = true;
519  // Restore the value of LexingRawMode so that trailing comments
520  // are handled correctly.
521  CurPPLexer->LexingRawMode = false;
522  CheckEndOfDirective("else");
523  CurPPLexer->LexingRawMode = true;
524  if (Callbacks)
525  Callbacks->Else(Tok.getLocation(), CondInfo.IfLoc);
526  break;
527  } else {
528  DiscardUntilEndOfDirective(); // C99 6.10p4.
529  }
530  } else if (Sub == "lif") { // "elif".
531  PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel();
532 
533  // If this is a #elif with a #else before it, report the error.
534  if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_elif_after_else);
535 
536  // If this is in a skipping block or if we're already handled this #if
537  // block, don't bother parsing the condition.
538  if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) {
540  } else {
541  const SourceLocation CondBegin = CurPPLexer->getSourceLocation();
542  // Restore the value of LexingRawMode so that identifiers are
543  // looked up, etc, inside the #elif expression.
544  assert(CurPPLexer->LexingRawMode && "We have to be skipping here!");
545  CurPPLexer->LexingRawMode = false;
546  IdentifierInfo *IfNDefMacro = nullptr;
547  const bool CondValue = EvaluateDirectiveExpression(IfNDefMacro).Conditional;
548  CurPPLexer->LexingRawMode = true;
549  if (Callbacks) {
550  const SourceLocation CondEnd = CurPPLexer->getSourceLocation();
551  Callbacks->Elif(Tok.getLocation(),
552  SourceRange(CondBegin, CondEnd),
553  (CondValue ? PPCallbacks::CVK_True : PPCallbacks::CVK_False), CondInfo.IfLoc);
554  }
555  // If this condition is true, enter it!
556  if (CondValue) {
557  CondInfo.FoundNonSkip = true;
558  break;
559  }
560  }
561  }
562  }
563 
564  CurPPLexer->ParsingPreprocessorDirective = false;
565  // Restore comment saving mode.
566  if (CurLexer) CurLexer->resetExtendedTokenMode();
567  }
568 
569  // Finally, if we are out of the conditional (saw an #endif or ran off the end
570  // of the file, just stop skipping and return to lexing whatever came after
571  // the #if block.
572  CurPPLexer->LexingRawMode = false;
573 
574  // The last skipped range isn't actually skipped yet if it's truncated
575  // by the end of the preamble; we'll resume parsing after the preamble.
576  if (Callbacks && (Tok.isNot(tok::eof) || !isRecordingPreamble()))
577  Callbacks->SourceRangeSkipped(
578  SourceRange(HashTokenLoc, CurPPLexer->getSourceLocation()),
579  Tok.getLocation());
580 }
581 
583  if (!SourceMgr.isInMainFile(Loc)) {
584  // Try to determine the module of the include directive.
585  // FIXME: Look into directly passing the FileEntry from LookupFile instead.
586  FileID IDOfIncl = SourceMgr.getFileID(SourceMgr.getExpansionLoc(Loc));
587  if (const FileEntry *EntryOfIncl = SourceMgr.getFileEntryForID(IDOfIncl)) {
588  // The include comes from an included file.
589  return HeaderInfo.getModuleMap()
590  .findModuleForHeader(EntryOfIncl)
591  .getModule();
592  }
593  }
594 
595  // This is either in the main file or not in a file at all. It belongs
596  // to the current module, if there is one.
597  return getLangOpts().CurrentModule.empty()
598  ? nullptr
599  : HeaderInfo.lookupModule(getLangOpts().CurrentModule);
600 }
601 
602 const FileEntry *
604  Module *M,
605  SourceLocation Loc) {
606  assert(M && "no module to include");
607 
608  // If we have a module import syntax, we shouldn't include a header to
609  // make a particular module visible.
610  if (getLangOpts().ObjC)
611  return nullptr;
612 
613  Module *TopM = M->getTopLevelModule();
614  Module *IncM = getModuleForLocation(IncLoc);
615 
616  // Walk up through the include stack, looking through textual headers of M
617  // until we hit a non-textual header that we can #include. (We assume textual
618  // headers of a module with non-textual headers aren't meant to be used to
619  // import entities from the module.)
620  auto &SM = getSourceManager();
621  while (!Loc.isInvalid() && !SM.isInMainFile(Loc)) {
622  auto ID = SM.getFileID(SM.getExpansionLoc(Loc));
623  auto *FE = SM.getFileEntryForID(ID);
624  if (!FE)
625  break;
626 
627  bool InTextualHeader = false;
628  for (auto Header : HeaderInfo.getModuleMap().findAllModulesForHeader(FE)) {
629  if (!Header.getModule()->isSubModuleOf(TopM))
630  continue;
631 
632  if (!(Header.getRole() & ModuleMap::TextualHeader)) {
633  // If this is an accessible, non-textual header of M's top-level module
634  // that transitively includes the given location and makes the
635  // corresponding module visible, this is the thing to #include.
636  if (Header.isAccessibleFrom(IncM))
637  return FE;
638 
639  // It's in a private header; we can't #include it.
640  // FIXME: If there's a public header in some module that re-exports it,
641  // then we could suggest including that, but it's not clear that's the
642  // expected way to make this entity visible.
643  continue;
644  }
645 
646  InTextualHeader = true;
647  }
648 
649  if (!InTextualHeader)
650  break;
651 
652  Loc = SM.getIncludeLoc(ID);
653  }
654 
655  return nullptr;
656 }
657 
659  SourceLocation FilenameLoc, StringRef Filename, bool isAngled,
660  const DirectoryLookup *FromDir, const FileEntry *FromFile,
661  const DirectoryLookup *&CurDir, SmallVectorImpl<char> *SearchPath,
662  SmallVectorImpl<char> *RelativePath,
663  ModuleMap::KnownHeader *SuggestedModule, bool *IsMapped, bool SkipCache) {
664  Module *RequestingModule = getModuleForLocation(FilenameLoc);
665  bool RequestingModuleIsModuleInterface = !SourceMgr.isInMainFile(FilenameLoc);
666 
667  // If the header lookup mechanism may be relative to the current inclusion
668  // stack, record the parent #includes.
670  Includers;
671  bool BuildSystemModule = false;
672  if (!FromDir && !FromFile) {
674  const FileEntry *FileEnt = SourceMgr.getFileEntryForID(FID);
675 
676  // If there is no file entry associated with this file, it must be the
677  // predefines buffer or the module includes buffer. Any other file is not
678  // lexed with a normal lexer, so it won't be scanned for preprocessor
679  // directives.
680  //
681  // If we have the predefines buffer, resolve #include references (which come
682  // from the -include command line argument) from the current working
683  // directory instead of relative to the main file.
684  //
685  // If we have the module includes buffer, resolve #include references (which
686  // come from header declarations in the module map) relative to the module
687  // map file.
688  if (!FileEnt) {
689  if (FID == SourceMgr.getMainFileID() && MainFileDir) {
690  Includers.push_back(std::make_pair(nullptr, MainFileDir));
691  BuildSystemModule = getCurrentModule()->IsSystem;
692  } else if ((FileEnt =
693  SourceMgr.getFileEntryForID(SourceMgr.getMainFileID())))
694  Includers.push_back(std::make_pair(FileEnt, FileMgr.getDirectory(".")));
695  } else {
696  Includers.push_back(std::make_pair(FileEnt, FileEnt->getDir()));
697  }
698 
699  // MSVC searches the current include stack from top to bottom for
700  // headers included by quoted include directives.
701  // See: http://msdn.microsoft.com/en-us/library/36k2cdd4.aspx
702  if (LangOpts.MSVCCompat && !isAngled) {
703  for (IncludeStackInfo &ISEntry : llvm::reverse(IncludeMacroStack)) {
704  if (IsFileLexer(ISEntry))
705  if ((FileEnt = ISEntry.ThePPLexer->getFileEntry()))
706  Includers.push_back(std::make_pair(FileEnt, FileEnt->getDir()));
707  }
708  }
709  }
710 
711  CurDir = CurDirLookup;
712 
713  if (FromFile) {
714  // We're supposed to start looking from after a particular file. Search
715  // the include path until we find that file or run out of files.
716  const DirectoryLookup *TmpCurDir = CurDir;
717  const DirectoryLookup *TmpFromDir = nullptr;
718  while (const FileEntry *FE = HeaderInfo.LookupFile(
719  Filename, FilenameLoc, isAngled, TmpFromDir, TmpCurDir,
720  Includers, SearchPath, RelativePath, RequestingModule,
721  SuggestedModule, /*IsMapped=*/nullptr, SkipCache)) {
722  // Keep looking as if this file did a #include_next.
723  TmpFromDir = TmpCurDir;
724  ++TmpFromDir;
725  if (FE == FromFile) {
726  // Found it.
727  FromDir = TmpFromDir;
728  CurDir = TmpCurDir;
729  break;
730  }
731  }
732  }
733 
734  // Do a standard file entry lookup.
735  const FileEntry *FE = HeaderInfo.LookupFile(
736  Filename, FilenameLoc, isAngled, FromDir, CurDir, Includers, SearchPath,
737  RelativePath, RequestingModule, SuggestedModule, IsMapped, SkipCache,
738  BuildSystemModule);
739  if (FE) {
740  if (SuggestedModule && !LangOpts.AsmPreprocessor)
742  RequestingModule, RequestingModuleIsModuleInterface, FilenameLoc,
743  Filename, FE);
744  return FE;
745  }
746 
747  const FileEntry *CurFileEnt;
748  // Otherwise, see if this is a subframework header. If so, this is relative
749  // to one of the headers on the #include stack. Walk the list of the current
750  // headers on the #include stack and pass them to HeaderInfo.
751  if (IsFileLexer()) {
752  if ((CurFileEnt = CurPPLexer->getFileEntry())) {
753  if ((FE = HeaderInfo.LookupSubframeworkHeader(Filename, CurFileEnt,
754  SearchPath, RelativePath,
755  RequestingModule,
756  SuggestedModule))) {
757  if (SuggestedModule && !LangOpts.AsmPreprocessor)
759  RequestingModule, RequestingModuleIsModuleInterface, FilenameLoc,
760  Filename, FE);
761  return FE;
762  }
763  }
764  }
765 
766  for (IncludeStackInfo &ISEntry : llvm::reverse(IncludeMacroStack)) {
767  if (IsFileLexer(ISEntry)) {
768  if ((CurFileEnt = ISEntry.ThePPLexer->getFileEntry())) {
769  if ((FE = HeaderInfo.LookupSubframeworkHeader(
770  Filename, CurFileEnt, SearchPath, RelativePath,
771  RequestingModule, SuggestedModule))) {
772  if (SuggestedModule && !LangOpts.AsmPreprocessor)
774  RequestingModule, RequestingModuleIsModuleInterface,
775  FilenameLoc, Filename, FE);
776  return FE;
777  }
778  }
779  }
780  }
781 
782  // Otherwise, we really couldn't find the file.
783  return nullptr;
784 }
785 
786 //===----------------------------------------------------------------------===//
787 // Preprocessor Directive Handling.
788 //===----------------------------------------------------------------------===//
789 
791 public:
793  : PP(pp), save(pp->DisableMacroExpansion) {
794  if (pp->MacroExpansionInDirectivesOverride)
795  pp->DisableMacroExpansion = false;
796  }
797 
799  PP->DisableMacroExpansion = save;
800  }
801 
802 private:
803  Preprocessor *PP;
804  bool save;
805 };
806 
807 /// Process a directive while looking for the through header or a #pragma
808 /// hdrstop. The following directives are handled:
809 /// #include (to check if it is the through header)
810 /// #define (to warn about macros that don't match the PCH)
811 /// #pragma (to check for pragma hdrstop).
812 /// All other directives are completely discarded.
814  SourceLocation HashLoc) {
815  if (const IdentifierInfo *II = Result.getIdentifierInfo()) {
816  if (II->getPPKeywordID() == tok::pp_define) {
817  return HandleDefineDirective(Result,
818  /*ImmediatelyAfterHeaderGuard=*/false);
819  }
820  if (SkippingUntilPCHThroughHeader &&
821  II->getPPKeywordID() == tok::pp_include) {
822  return HandleIncludeDirective(HashLoc, Result);
823  }
824  if (SkippingUntilPragmaHdrStop && II->getPPKeywordID() == tok::pp_pragma) {
825  Token P = LookAhead(0);
826  auto *II = P.getIdentifierInfo();
827  if (II && II->getName() == "hdrstop")
828  return HandlePragmaDirective(HashLoc, PIK_HashPragma);
829  }
830  }
832 }
833 
834 /// HandleDirective - This callback is invoked when the lexer sees a # token
835 /// at the start of a line. This consumes the directive, modifies the
836 /// lexer/preprocessor state, and advances the lexer(s) so that the next token
837 /// read is the correct one.
839  // FIXME: Traditional: # with whitespace before it not recognized by K&R?
840 
841  // We just parsed a # character at the start of a line, so we're in directive
842  // mode. Tell the lexer this so any newlines we see will be converted into an
843  // EOD token (which terminates the directive).
844  CurPPLexer->ParsingPreprocessorDirective = true;
845  if (CurLexer) CurLexer->SetKeepWhitespaceMode(false);
846 
847  bool ImmediatelyAfterTopLevelIfndef =
848  CurPPLexer->MIOpt.getImmediatelyAfterTopLevelIfndef();
849  CurPPLexer->MIOpt.resetImmediatelyAfterTopLevelIfndef();
850 
851  ++NumDirectives;
852 
853  // We are about to read a token. For the multiple-include optimization FA to
854  // work, we have to remember if we had read any tokens *before* this
855  // pp-directive.
856  bool ReadAnyTokensBeforeDirective =CurPPLexer->MIOpt.getHasReadAnyTokensVal();
857 
858  // Save the '#' token in case we need to return it later.
859  Token SavedHash = Result;
860 
861  // Read the next token, the directive flavor. This isn't expanded due to
862  // C99 6.10.3p8.
863  LexUnexpandedToken(Result);
864 
865  // C99 6.10.3p11: Is this preprocessor directive in macro invocation? e.g.:
866  // #define A(x) #x
867  // A(abc
868  // #warning blah
869  // def)
870  // If so, the user is relying on undefined behavior, emit a diagnostic. Do
871  // not support this for #include-like directives, since that can result in
872  // terrible diagnostics, and does not work in GCC.
873  if (InMacroArgs) {
874  if (IdentifierInfo *II = Result.getIdentifierInfo()) {
875  switch (II->getPPKeywordID()) {
876  case tok::pp_include:
877  case tok::pp_import:
878  case tok::pp_include_next:
879  case tok::pp___include_macros:
880  case tok::pp_pragma:
881  Diag(Result, diag::err_embedded_directive) << II->getName();
883  return;
884  default:
885  break;
886  }
887  }
888  Diag(Result, diag::ext_embedded_directive);
889  }
890 
891  // Temporarily enable macro expansion if set so
892  // and reset to previous state when returning from this function.
893  ResetMacroExpansionHelper helper(this);
894 
895  if (SkippingUntilPCHThroughHeader || SkippingUntilPragmaHdrStop)
896  return HandleSkippedDirectiveWhileUsingPCH(Result, SavedHash.getLocation());
897 
898  switch (Result.getKind()) {
899  case tok::eod:
900  return; // null directive.
901  case tok::code_completion:
902  if (CodeComplete)
903  CodeComplete->CodeCompleteDirective(
904  CurPPLexer->getConditionalStackDepth() > 0);
906  return;
907  case tok::numeric_constant: // # 7 GNU line marker directive.
908  if (getLangOpts().AsmPreprocessor)
909  break; // # 4 is not a preprocessor directive in .S files.
910  return HandleDigitDirective(Result);
911  default:
912  IdentifierInfo *II = Result.getIdentifierInfo();
913  if (!II) break; // Not an identifier.
914 
915  // Ask what the preprocessor keyword ID is.
916  switch (II->getPPKeywordID()) {
917  default: break;
918  // C99 6.10.1 - Conditional Inclusion.
919  case tok::pp_if:
920  return HandleIfDirective(Result, SavedHash, ReadAnyTokensBeforeDirective);
921  case tok::pp_ifdef:
922  return HandleIfdefDirective(Result, SavedHash, false,
923  true /*not valid for miopt*/);
924  case tok::pp_ifndef:
925  return HandleIfdefDirective(Result, SavedHash, true,
926  ReadAnyTokensBeforeDirective);
927  case tok::pp_elif:
928  return HandleElifDirective(Result, SavedHash);
929  case tok::pp_else:
930  return HandleElseDirective(Result, SavedHash);
931  case tok::pp_endif:
932  return HandleEndifDirective(Result);
933 
934  // C99 6.10.2 - Source File Inclusion.
935  case tok::pp_include:
936  // Handle #include.
937  return HandleIncludeDirective(SavedHash.getLocation(), Result);
938  case tok::pp___include_macros:
939  // Handle -imacros.
940  return HandleIncludeMacrosDirective(SavedHash.getLocation(), Result);
941 
942  // C99 6.10.3 - Macro Replacement.
943  case tok::pp_define:
944  return HandleDefineDirective(Result, ImmediatelyAfterTopLevelIfndef);
945  case tok::pp_undef:
946  return HandleUndefDirective();
947 
948  // C99 6.10.4 - Line Control.
949  case tok::pp_line:
950  return HandleLineDirective();
951 
952  // C99 6.10.5 - Error Directive.
953  case tok::pp_error:
954  return HandleUserDiagnosticDirective(Result, false);
955 
956  // C99 6.10.6 - Pragma Directive.
957  case tok::pp_pragma:
958  return HandlePragmaDirective(SavedHash.getLocation(), PIK_HashPragma);
959 
960  // GNU Extensions.
961  case tok::pp_import:
962  return HandleImportDirective(SavedHash.getLocation(), Result);
963  case tok::pp_include_next:
964  return HandleIncludeNextDirective(SavedHash.getLocation(), Result);
965 
966  case tok::pp_warning:
967  Diag(Result, diag::ext_pp_warning_directive);
968  return HandleUserDiagnosticDirective(Result, true);
969  case tok::pp_ident:
970  return HandleIdentSCCSDirective(Result);
971  case tok::pp_sccs:
972  return HandleIdentSCCSDirective(Result);
973  case tok::pp_assert:
974  //isExtension = true; // FIXME: implement #assert
975  break;
976  case tok::pp_unassert:
977  //isExtension = true; // FIXME: implement #unassert
978  break;
979 
980  case tok::pp___public_macro:
981  if (getLangOpts().Modules)
982  return HandleMacroPublicDirective(Result);
983  break;
984 
985  case tok::pp___private_macro:
986  if (getLangOpts().Modules)
987  return HandleMacroPrivateDirective();
988  break;
989  }
990  break;
991  }
992 
993  // If this is a .S file, treat unknown # directives as non-preprocessor
994  // directives. This is important because # may be a comment or introduce
995  // various pseudo-ops. Just return the # token and push back the following
996  // token to be lexed next time.
997  if (getLangOpts().AsmPreprocessor) {
998  auto Toks = llvm::make_unique<Token[]>(2);
999  // Return the # and the token after it.
1000  Toks[0] = SavedHash;
1001  Toks[1] = Result;
1002 
1003  // If the second token is a hashhash token, then we need to translate it to
1004  // unknown so the token lexer doesn't try to perform token pasting.
1005  if (Result.is(tok::hashhash))
1006  Toks[1].setKind(tok::unknown);
1007 
1008  // Enter this token stream so that we re-lex the tokens. Make sure to
1009  // enable macro expansion, in case the token after the # is an identifier
1010  // that is expanded.
1011  EnterTokenStream(std::move(Toks), 2, false);
1012  return;
1013  }
1014 
1015  // If we reached here, the preprocessing token is not valid!
1016  Diag(Result, diag::err_pp_invalid_directive);
1017 
1018  // Read the rest of the PP line.
1020 
1021  // Okay, we're done parsing the directive.
1022 }
1023 
1024 /// GetLineValue - Convert a numeric token into an unsigned value, emitting
1025 /// Diagnostic DiagID if it is invalid, and returning the value in Val.
1026 static bool GetLineValue(Token &DigitTok, unsigned &Val,
1027  unsigned DiagID, Preprocessor &PP,
1028  bool IsGNULineDirective=false) {
1029  if (DigitTok.isNot(tok::numeric_constant)) {
1030  PP.Diag(DigitTok, DiagID);
1031 
1032  if (DigitTok.isNot(tok::eod))
1034  return true;
1035  }
1036 
1037  SmallString<64> IntegerBuffer;
1038  IntegerBuffer.resize(DigitTok.getLength());
1039  const char *DigitTokBegin = &IntegerBuffer[0];
1040  bool Invalid = false;
1041  unsigned ActualLength = PP.getSpelling(DigitTok, DigitTokBegin, &Invalid);
1042  if (Invalid)
1043  return true;
1044 
1045  // Verify that we have a simple digit-sequence, and compute the value. This
1046  // is always a simple digit string computed in decimal, so we do this manually
1047  // here.
1048  Val = 0;
1049  for (unsigned i = 0; i != ActualLength; ++i) {
1050  // C++1y [lex.fcon]p1:
1051  // Optional separating single quotes in a digit-sequence are ignored
1052  if (DigitTokBegin[i] == '\'')
1053  continue;
1054 
1055  if (!isDigit(DigitTokBegin[i])) {
1056  PP.Diag(PP.AdvanceToTokenCharacter(DigitTok.getLocation(), i),
1057  diag::err_pp_line_digit_sequence) << IsGNULineDirective;
1059  return true;
1060  }
1061 
1062  unsigned NextVal = Val*10+(DigitTokBegin[i]-'0');
1063  if (NextVal < Val) { // overflow.
1064  PP.Diag(DigitTok, DiagID);
1066  return true;
1067  }
1068  Val = NextVal;
1069  }
1070 
1071  if (DigitTokBegin[0] == '0' && Val)
1072  PP.Diag(DigitTok.getLocation(), diag::warn_pp_line_decimal)
1073  << IsGNULineDirective;
1074 
1075  return false;
1076 }
1077 
1078 /// Handle a \#line directive: C99 6.10.4.
1079 ///
1080 /// The two acceptable forms are:
1081 /// \verbatim
1082 /// # line digit-sequence
1083 /// # line digit-sequence "s-char-sequence"
1084 /// \endverbatim
1085 void Preprocessor::HandleLineDirective() {
1086  // Read the line # and string argument. Per C99 6.10.4p5, these tokens are
1087  // expanded.
1088  Token DigitTok;
1089  Lex(DigitTok);
1090 
1091  // Validate the number and convert it to an unsigned.
1092  unsigned LineNo;
1093  if (GetLineValue(DigitTok, LineNo, diag::err_pp_line_requires_integer,*this))
1094  return;
1095 
1096  if (LineNo == 0)
1097  Diag(DigitTok, diag::ext_pp_line_zero);
1098 
1099  // Enforce C99 6.10.4p3: "The digit sequence shall not specify ... a
1100  // number greater than 2147483647". C90 requires that the line # be <= 32767.
1101  unsigned LineLimit = 32768U;
1102  if (LangOpts.C99 || LangOpts.CPlusPlus11)
1103  LineLimit = 2147483648U;
1104  if (LineNo >= LineLimit)
1105  Diag(DigitTok, diag::ext_pp_line_too_big) << LineLimit;
1106  else if (LangOpts.CPlusPlus11 && LineNo >= 32768U)
1107  Diag(DigitTok, diag::warn_cxx98_compat_pp_line_too_big);
1108 
1109  int FilenameID = -1;
1110  Token StrTok;
1111  Lex(StrTok);
1112 
1113  // If the StrTok is "eod", then it wasn't present. Otherwise, it must be a
1114  // string followed by eod.
1115  if (StrTok.is(tok::eod))
1116  ; // ok
1117  else if (StrTok.isNot(tok::string_literal)) {
1118  Diag(StrTok, diag::err_pp_line_invalid_filename);
1119  return DiscardUntilEndOfDirective();
1120  } else if (StrTok.hasUDSuffix()) {
1121  Diag(StrTok, diag::err_invalid_string_udl);
1122  return DiscardUntilEndOfDirective();
1123  } else {
1124  // Parse and validate the string, converting it into a unique ID.
1125  StringLiteralParser Literal(StrTok, *this);
1126  assert(Literal.isAscii() && "Didn't allow wide strings in");
1127  if (Literal.hadError)
1128  return DiscardUntilEndOfDirective();
1129  if (Literal.Pascal) {
1130  Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
1131  return DiscardUntilEndOfDirective();
1132  }
1133  FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString());
1134 
1135  // Verify that there is nothing after the string, other than EOD. Because
1136  // of C99 6.10.4p5, macros that expand to empty tokens are ok.
1137  CheckEndOfDirective("line", true);
1138  }
1139 
1140  // Take the file kind of the file containing the #line directive. #line
1141  // directives are often used for generated sources from the same codebase, so
1142  // the new file should generally be classified the same way as the current
1143  // file. This is visible in GCC's pre-processed output, which rewrites #line
1144  // to GNU line markers.
1145  SrcMgr::CharacteristicKind FileKind =
1146  SourceMgr.getFileCharacteristic(DigitTok.getLocation());
1147 
1148  SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID, false,
1149  false, FileKind);
1150 
1151  if (Callbacks)
1152  Callbacks->FileChanged(CurPPLexer->getSourceLocation(),
1153  PPCallbacks::RenameFile, FileKind);
1154 }
1155 
1156 /// ReadLineMarkerFlags - Parse and validate any flags at the end of a GNU line
1157 /// marker directive.
1158 static bool ReadLineMarkerFlags(bool &IsFileEntry, bool &IsFileExit,
1159  SrcMgr::CharacteristicKind &FileKind,
1160  Preprocessor &PP) {
1161  unsigned FlagVal;
1162  Token FlagTok;
1163  PP.Lex(FlagTok);
1164  if (FlagTok.is(tok::eod)) return false;
1165  if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP))
1166  return true;
1167 
1168  if (FlagVal == 1) {
1169  IsFileEntry = true;
1170 
1171  PP.Lex(FlagTok);
1172  if (FlagTok.is(tok::eod)) return false;
1173  if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP))
1174  return true;
1175  } else if (FlagVal == 2) {
1176  IsFileExit = true;
1177 
1179  // If we are leaving the current presumed file, check to make sure the
1180  // presumed include stack isn't empty!
1181  FileID CurFileID =
1182  SM.getDecomposedExpansionLoc(FlagTok.getLocation()).first;
1183  PresumedLoc PLoc = SM.getPresumedLoc(FlagTok.getLocation());
1184  if (PLoc.isInvalid())
1185  return true;
1186 
1187  // If there is no include loc (main file) or if the include loc is in a
1188  // different physical file, then we aren't in a "1" line marker flag region.
1189  SourceLocation IncLoc = PLoc.getIncludeLoc();
1190  if (IncLoc.isInvalid() ||
1191  SM.getDecomposedExpansionLoc(IncLoc).first != CurFileID) {
1192  PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_pop);
1194  return true;
1195  }
1196 
1197  PP.Lex(FlagTok);
1198  if (FlagTok.is(tok::eod)) return false;
1199  if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP))
1200  return true;
1201  }
1202 
1203  // We must have 3 if there are still flags.
1204  if (FlagVal != 3) {
1205  PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
1207  return true;
1208  }
1209 
1210  FileKind = SrcMgr::C_System;
1211 
1212  PP.Lex(FlagTok);
1213  if (FlagTok.is(tok::eod)) return false;
1214  if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP))
1215  return true;
1216 
1217  // We must have 4 if there is yet another flag.
1218  if (FlagVal != 4) {
1219  PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
1221  return true;
1222  }
1223 
1224  FileKind = SrcMgr::C_ExternCSystem;
1225 
1226  PP.Lex(FlagTok);
1227  if (FlagTok.is(tok::eod)) return false;
1228 
1229  // There are no more valid flags here.
1230  PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
1232  return true;
1233 }
1234 
1235 /// HandleDigitDirective - Handle a GNU line marker directive, whose syntax is
1236 /// one of the following forms:
1237 ///
1238 /// # 42
1239 /// # 42 "file" ('1' | '2')?
1240 /// # 42 "file" ('1' | '2')? '3' '4'?
1241 ///
1242 void Preprocessor::HandleDigitDirective(Token &DigitTok) {
1243  // Validate the number and convert it to an unsigned. GNU does not have a
1244  // line # limit other than it fit in 32-bits.
1245  unsigned LineNo;
1246  if (GetLineValue(DigitTok, LineNo, diag::err_pp_linemarker_requires_integer,
1247  *this, true))
1248  return;
1249 
1250  Token StrTok;
1251  Lex(StrTok);
1252 
1253  bool IsFileEntry = false, IsFileExit = false;
1254  int FilenameID = -1;
1256 
1257  // If the StrTok is "eod", then it wasn't present. Otherwise, it must be a
1258  // string followed by eod.
1259  if (StrTok.is(tok::eod)) {
1260  // Treat this like "#line NN", which doesn't change file characteristics.
1261  FileKind = SourceMgr.getFileCharacteristic(DigitTok.getLocation());
1262  } else if (StrTok.isNot(tok::string_literal)) {
1263  Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
1264  return DiscardUntilEndOfDirective();
1265  } else if (StrTok.hasUDSuffix()) {
1266  Diag(StrTok, diag::err_invalid_string_udl);
1267  return DiscardUntilEndOfDirective();
1268  } else {
1269  // Parse and validate the string, converting it into a unique ID.
1270  StringLiteralParser Literal(StrTok, *this);
1271  assert(Literal.isAscii() && "Didn't allow wide strings in");
1272  if (Literal.hadError)
1273  return DiscardUntilEndOfDirective();
1274  if (Literal.Pascal) {
1275  Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
1276  return DiscardUntilEndOfDirective();
1277  }
1278  FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString());
1279 
1280  // If a filename was present, read any flags that are present.
1281  if (ReadLineMarkerFlags(IsFileEntry, IsFileExit, FileKind, *this))
1282  return;
1283  }
1284 
1285  // Create a line note with this information.
1286  SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID, IsFileEntry,
1287  IsFileExit, FileKind);
1288 
1289  // If the preprocessor has callbacks installed, notify them of the #line
1290  // change. This is used so that the line marker comes out in -E mode for
1291  // example.
1292  if (Callbacks) {
1294  if (IsFileEntry)
1295  Reason = PPCallbacks::EnterFile;
1296  else if (IsFileExit)
1297  Reason = PPCallbacks::ExitFile;
1298 
1299  Callbacks->FileChanged(CurPPLexer->getSourceLocation(), Reason, FileKind);
1300  }
1301 }
1302 
1303 /// HandleUserDiagnosticDirective - Handle a #warning or #error directive.
1304 ///
1305 void Preprocessor::HandleUserDiagnosticDirective(Token &Tok,
1306  bool isWarning) {
1307  // Read the rest of the line raw. We do this because we don't want macros
1308  // to be expanded and we don't require that the tokens be valid preprocessing
1309  // tokens. For example, this is allowed: "#warning ` 'foo". GCC does
1310  // collapse multiple consecutive white space between tokens, but this isn't
1311  // specified by the standard.
1312  SmallString<128> Message;
1313  CurLexer->ReadToEndOfLine(&Message);
1314 
1315  // Find the first non-whitespace character, so that we can make the
1316  // diagnostic more succinct.
1317  StringRef Msg = StringRef(Message).ltrim(' ');
1318 
1319  if (isWarning)
1320  Diag(Tok, diag::pp_hash_warning) << Msg;
1321  else
1322  Diag(Tok, diag::err_pp_hash_error) << Msg;
1323 }
1324 
1325 /// HandleIdentSCCSDirective - Handle a #ident/#sccs directive.
1326 ///
1327 void Preprocessor::HandleIdentSCCSDirective(Token &Tok) {
1328  // Yes, this directive is an extension.
1329  Diag(Tok, diag::ext_pp_ident_directive);
1330 
1331  // Read the string argument.
1332  Token StrTok;
1333  Lex(StrTok);
1334 
1335  // If the token kind isn't a string, it's a malformed directive.
1336  if (StrTok.isNot(tok::string_literal) &&
1337  StrTok.isNot(tok::wide_string_literal)) {
1338  Diag(StrTok, diag::err_pp_malformed_ident);
1339  if (StrTok.isNot(tok::eod))
1341  return;
1342  }
1343 
1344  if (StrTok.hasUDSuffix()) {
1345  Diag(StrTok, diag::err_invalid_string_udl);
1346  return DiscardUntilEndOfDirective();
1347  }
1348 
1349  // Verify that there is nothing after the string, other than EOD.
1350  CheckEndOfDirective("ident");
1351 
1352  if (Callbacks) {
1353  bool Invalid = false;
1354  std::string Str = getSpelling(StrTok, &Invalid);
1355  if (!Invalid)
1356  Callbacks->Ident(Tok.getLocation(), Str);
1357  }
1358 }
1359 
1360 /// Handle a #public directive.
1361 void Preprocessor::HandleMacroPublicDirective(Token &Tok) {
1362  Token MacroNameTok;
1363  ReadMacroName(MacroNameTok, MU_Undef);
1364 
1365  // Error reading macro name? If so, diagnostic already issued.
1366  if (MacroNameTok.is(tok::eod))
1367  return;
1368 
1369  // Check to see if this is the last token on the #__public_macro line.
1370  CheckEndOfDirective("__public_macro");
1371 
1372  IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
1373  // Okay, we finally have a valid identifier to undef.
1375 
1376  // If the macro is not defined, this is an error.
1377  if (!MD) {
1378  Diag(MacroNameTok, diag::err_pp_visibility_non_macro) << II;
1379  return;
1380  }
1381 
1382  // Note that this macro has now been exported.
1383  appendMacroDirective(II, AllocateVisibilityMacroDirective(
1384  MacroNameTok.getLocation(), /*IsPublic=*/true));
1385 }
1386 
1387 /// Handle a #private directive.
1388 void Preprocessor::HandleMacroPrivateDirective() {
1389  Token MacroNameTok;
1390  ReadMacroName(MacroNameTok, MU_Undef);
1391 
1392  // Error reading macro name? If so, diagnostic already issued.
1393  if (MacroNameTok.is(tok::eod))
1394  return;
1395 
1396  // Check to see if this is the last token on the #__private_macro line.
1397  CheckEndOfDirective("__private_macro");
1398 
1399  IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
1400  // Okay, we finally have a valid identifier to undef.
1402 
1403  // If the macro is not defined, this is an error.
1404  if (!MD) {
1405  Diag(MacroNameTok, diag::err_pp_visibility_non_macro) << II;
1406  return;
1407  }
1408 
1409  // Note that this macro has now been marked private.
1410  appendMacroDirective(II, AllocateVisibilityMacroDirective(
1411  MacroNameTok.getLocation(), /*IsPublic=*/false));
1412 }
1413 
1414 //===----------------------------------------------------------------------===//
1415 // Preprocessor Include Directive Handling.
1416 //===----------------------------------------------------------------------===//
1417 
1418 /// GetIncludeFilenameSpelling - Turn the specified lexer token into a fully
1419 /// checked and spelled filename, e.g. as an operand of \#include. This returns
1420 /// true if the input filename was in <>'s or false if it were in ""'s. The
1421 /// caller is expected to provide a buffer that is large enough to hold the
1422 /// spelling of the filename, but is also expected to handle the case when
1423 /// this method decides to use a different buffer.
1425  StringRef &Buffer) {
1426  // Get the text form of the filename.
1427  assert(!Buffer.empty() && "Can't have tokens with empty spellings!");
1428 
1429  // Make sure the filename is <x> or "x".
1430  bool isAngled;
1431  if (Buffer[0] == '<') {
1432  if (Buffer.back() != '>') {
1433  Diag(Loc, diag::err_pp_expects_filename);
1434  Buffer = StringRef();
1435  return true;
1436  }
1437  isAngled = true;
1438  } else if (Buffer[0] == '"') {
1439  if (Buffer.back() != '"') {
1440  Diag(Loc, diag::err_pp_expects_filename);
1441  Buffer = StringRef();
1442  return true;
1443  }
1444  isAngled = false;
1445  } else {
1446  Diag(Loc, diag::err_pp_expects_filename);
1447  Buffer = StringRef();
1448  return true;
1449  }
1450 
1451  // Diagnose #include "" as invalid.
1452  if (Buffer.size() <= 2) {
1453  Diag(Loc, diag::err_pp_empty_filename);
1454  Buffer = StringRef();
1455  return true;
1456  }
1457 
1458  // Skip the brackets.
1459  Buffer = Buffer.substr(1, Buffer.size()-2);
1460  return isAngled;
1461 }
1462 
1463 // Handle cases where the \#include name is expanded from a macro
1464 // as multiple tokens, which need to be glued together.
1465 //
1466 // This occurs for code like:
1467 // \code
1468 // \#define FOO <a/b.h>
1469 // \#include FOO
1470 // \endcode
1471 // because in this case, "<a/b.h>" is returned as 7 tokens, not one.
1472 //
1473 // This code concatenates and consumes tokens up to the '>' token. It returns
1474 // false if the > was found, otherwise it returns true if it finds and consumes
1475 // the EOD marker.
1477  SourceLocation &End) {
1478  Token CurTok;
1479 
1480  Lex(CurTok);
1481  while (CurTok.isNot(tok::eod)) {
1482  End = CurTok.getLocation();
1483 
1484  // FIXME: Provide code completion for #includes.
1485  if (CurTok.is(tok::code_completion)) {
1487  Lex(CurTok);
1488  continue;
1489  }
1490 
1491  // Append the spelling of this token to the buffer. If there was a space
1492  // before it, add it now.
1493  if (CurTok.hasLeadingSpace())
1494  FilenameBuffer.push_back(' ');
1495 
1496  // Get the spelling of the token, directly into FilenameBuffer if possible.
1497  size_t PreAppendSize = FilenameBuffer.size();
1498  FilenameBuffer.resize(PreAppendSize+CurTok.getLength());
1499 
1500  const char *BufPtr = &FilenameBuffer[PreAppendSize];
1501  unsigned ActualLen = getSpelling(CurTok, BufPtr);
1502 
1503  // If the token was spelled somewhere else, copy it into FilenameBuffer.
1504  if (BufPtr != &FilenameBuffer[PreAppendSize])
1505  memcpy(&FilenameBuffer[PreAppendSize], BufPtr, ActualLen);
1506 
1507  // Resize FilenameBuffer to the correct size.
1508  if (CurTok.getLength() != ActualLen)
1509  FilenameBuffer.resize(PreAppendSize+ActualLen);
1510 
1511  // If we found the '>' marker, return success.
1512  if (CurTok.is(tok::greater))
1513  return false;
1514 
1515  Lex(CurTok);
1516  }
1517 
1518  // If we hit the eod marker, emit an error and return true so that the caller
1519  // knows the EOD has been read.
1520  Diag(CurTok.getLocation(), diag::err_pp_expects_filename);
1521  return true;
1522 }
1523 
1524 /// Push a token onto the token stream containing an annotation.
1527  void *AnnotationVal) {
1528  // FIXME: Produce this as the current token directly, rather than
1529  // allocating a new token for it.
1530  auto Tok = llvm::make_unique<Token[]>(1);
1531  Tok[0].startToken();
1532  Tok[0].setKind(Kind);
1533  Tok[0].setLocation(Range.getBegin());
1534  Tok[0].setAnnotationEndLoc(Range.getEnd());
1535  Tok[0].setAnnotationValue(AnnotationVal);
1536  EnterTokenStream(std::move(Tok), 1, true);
1537 }
1538 
1539 /// Produce a diagnostic informing the user that a #include or similar
1540 /// was implicitly treated as a module import.
1542  Preprocessor &PP, SourceLocation HashLoc, Token &IncludeTok,
1543  ArrayRef<std::pair<IdentifierInfo *, SourceLocation>> Path,
1544  SourceLocation PathEnd) {
1545  assert(PP.getLangOpts().ObjC && "no import syntax available");
1546 
1547  SmallString<128> PathString;
1548  for (size_t I = 0, N = Path.size(); I != N; ++I) {
1549  if (I)
1550  PathString += '.';
1551  PathString += Path[I].first->getName();
1552  }
1553  int IncludeKind = 0;
1554 
1555  switch (IncludeTok.getIdentifierInfo()->getPPKeywordID()) {
1556  case tok::pp_include:
1557  IncludeKind = 0;
1558  break;
1559 
1560  case tok::pp_import:
1561  IncludeKind = 1;
1562  break;
1563 
1564  case tok::pp_include_next:
1565  IncludeKind = 2;
1566  break;
1567 
1568  case tok::pp___include_macros:
1569  IncludeKind = 3;
1570  break;
1571 
1572  default:
1573  llvm_unreachable("unknown include directive kind");
1574  }
1575 
1576  CharSourceRange ReplaceRange(SourceRange(HashLoc, PathEnd),
1577  /*IsTokenRange=*/false);
1578  PP.Diag(HashLoc, diag::warn_auto_module_import)
1579  << IncludeKind << PathString
1580  << FixItHint::CreateReplacement(ReplaceRange,
1581  ("@import " + PathString + ";").str());
1582 }
1583 
1584 // Given a vector of path components and a string containing the real
1585 // path to the file, build a properly-cased replacement in the vector,
1586 // and return true if the replacement should be suggested.
1588  StringRef RealPathName) {
1589  auto RealPathComponentIter = llvm::sys::path::rbegin(RealPathName);
1590  auto RealPathComponentEnd = llvm::sys::path::rend(RealPathName);
1591  int Cnt = 0;
1592  bool SuggestReplacement = false;
1593  // Below is a best-effort to handle ".." in paths. It is admittedly
1594  // not 100% correct in the presence of symlinks.
1595  for (auto &Component : llvm::reverse(Components)) {
1596  if ("." == Component) {
1597  } else if (".." == Component) {
1598  ++Cnt;
1599  } else if (Cnt) {
1600  --Cnt;
1601  } else if (RealPathComponentIter != RealPathComponentEnd) {
1602  if (Component != *RealPathComponentIter) {
1603  // If these path components differ by more than just case, then we
1604  // may be looking at symlinked paths. Bail on this diagnostic to avoid
1605  // noisy false positives.
1606  SuggestReplacement = RealPathComponentIter->equals_lower(Component);
1607  if (!SuggestReplacement)
1608  break;
1609  Component = *RealPathComponentIter;
1610  }
1611  ++RealPathComponentIter;
1612  }
1613  }
1614  return SuggestReplacement;
1615 }
1616 
1618  const TargetInfo &TargetInfo,
1619  DiagnosticsEngine &Diags, Module *M) {
1620  Module::Requirement Requirement;
1621  Module::UnresolvedHeaderDirective MissingHeader;
1622  Module *ShadowingModule = nullptr;
1623  if (M->isAvailable(LangOpts, TargetInfo, Requirement, MissingHeader,
1624  ShadowingModule))
1625  return false;
1626 
1627  if (MissingHeader.FileNameLoc.isValid()) {
1628  Diags.Report(MissingHeader.FileNameLoc, diag::err_module_header_missing)
1629  << MissingHeader.IsUmbrella << MissingHeader.FileName;
1630  } else if (ShadowingModule) {
1631  Diags.Report(M->DefinitionLoc, diag::err_module_shadowed) << M->Name;
1632  Diags.Report(ShadowingModule->DefinitionLoc,
1633  diag::note_previous_definition);
1634  } else {
1635  // FIXME: Track the location at which the requirement was specified, and
1636  // use it here.
1637  Diags.Report(M->DefinitionLoc, diag::err_module_unavailable)
1638  << M->getFullModuleName() << Requirement.second << Requirement.first;
1639  }
1640  return true;
1641 }
1642 
1643 /// HandleIncludeDirective - The "\#include" tokens have just been read, read
1644 /// the file to be included from the lexer, then include it! This is a common
1645 /// routine with functionality shared between \#include, \#include_next and
1646 /// \#import. LookupFrom is set when this is a \#include_next directive, it
1647 /// specifies the file to start searching from.
1648 void Preprocessor::HandleIncludeDirective(SourceLocation HashLoc,
1649  Token &IncludeTok,
1650  const DirectoryLookup *LookupFrom,
1651  const FileEntry *LookupFromFile,
1652  bool isImport) {
1653  Token FilenameTok;
1654  CurPPLexer->LexIncludeFilename(FilenameTok);
1655 
1656  // Reserve a buffer to get the spelling.
1657  SmallString<128> FilenameBuffer;
1658  StringRef Filename;
1660  SourceLocation CharEnd; // the end of this directive, in characters
1661 
1662  switch (FilenameTok.getKind()) {
1663  case tok::eod:
1664  // If the token kind is EOD, the error has already been diagnosed.
1665  return;
1666 
1667  case tok::angle_string_literal:
1668  case tok::string_literal:
1669  Filename = getSpelling(FilenameTok, FilenameBuffer);
1670  End = FilenameTok.getLocation();
1671  CharEnd = End.getLocWithOffset(FilenameTok.getLength());
1672  break;
1673 
1674  case tok::less:
1675  // This could be a <foo/bar.h> file coming from a macro expansion. In this
1676  // case, glue the tokens together into FilenameBuffer and interpret those.
1677  FilenameBuffer.push_back('<');
1678  if (ConcatenateIncludeName(FilenameBuffer, End))
1679  return; // Found <eod> but no ">"? Diagnostic already emitted.
1680  Filename = FilenameBuffer;
1681  CharEnd = End.getLocWithOffset(1);
1682  break;
1683  default:
1684  Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
1686  return;
1687  }
1688 
1689  CharSourceRange FilenameRange
1690  = CharSourceRange::getCharRange(FilenameTok.getLocation(), CharEnd);
1691  StringRef OriginalFilename = Filename;
1692  bool isAngled =
1694  // If GetIncludeFilenameSpelling set the start ptr to null, there was an
1695  // error.
1696  if (Filename.empty()) {
1698  return;
1699  }
1700 
1701  // Verify that there is nothing after the filename, other than EOD. Note that
1702  // we allow macros that expand to nothing after the filename, because this
1703  // falls into the category of "#include pp-tokens new-line" specified in
1704  // C99 6.10.2p4.
1705  CheckEndOfDirective(IncludeTok.getIdentifierInfo()->getNameStart(), true);
1706 
1707  // Check that we don't have infinite #include recursion.
1708  if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1) {
1709  Diag(FilenameTok, diag::err_pp_include_too_deep);
1710  HasReachedMaxIncludeDepth = true;
1711  return;
1712  }
1713 
1714  // Complain about attempts to #include files in an audit pragma.
1715  if (PragmaARCCFCodeAuditedLoc.isValid()) {
1716  Diag(HashLoc, diag::err_pp_include_in_arc_cf_code_audited);
1717  Diag(PragmaARCCFCodeAuditedLoc, diag::note_pragma_entered_here);
1718 
1719  // Immediately leave the pragma.
1720  PragmaARCCFCodeAuditedLoc = SourceLocation();
1721  }
1722 
1723  // Complain about attempts to #include files in an assume-nonnull pragma.
1724  if (PragmaAssumeNonNullLoc.isValid()) {
1725  Diag(HashLoc, diag::err_pp_include_in_assume_nonnull);
1726  Diag(PragmaAssumeNonNullLoc, diag::note_pragma_entered_here);
1727 
1728  // Immediately leave the pragma.
1729  PragmaAssumeNonNullLoc = SourceLocation();
1730  }
1731 
1732  if (HeaderInfo.HasIncludeAliasMap()) {
1733  // Map the filename with the brackets still attached. If the name doesn't
1734  // map to anything, fall back on the filename we've already gotten the
1735  // spelling for.
1736  StringRef NewName = HeaderInfo.MapHeaderToIncludeAlias(OriginalFilename);
1737  if (!NewName.empty())
1738  Filename = NewName;
1739  }
1740 
1741  // Search include directories.
1742  bool IsMapped = false;
1743  const DirectoryLookup *CurDir;
1744  SmallString<1024> SearchPath;
1745  SmallString<1024> RelativePath;
1746  // We get the raw path only if we have 'Callbacks' to which we later pass
1747  // the path.
1748  ModuleMap::KnownHeader SuggestedModule;
1749  SourceLocation FilenameLoc = FilenameTok.getLocation();
1750  SmallString<128> NormalizedPath;
1751  if (LangOpts.MSVCCompat) {
1752  NormalizedPath = Filename.str();
1753 #ifndef _WIN32
1754  llvm::sys::path::native(NormalizedPath);
1755 #endif
1756  }
1757  const FileEntry *File = LookupFile(
1758  FilenameLoc, LangOpts.MSVCCompat ? NormalizedPath.c_str() : Filename,
1759  isAngled, LookupFrom, LookupFromFile, CurDir,
1760  Callbacks ? &SearchPath : nullptr, Callbacks ? &RelativePath : nullptr,
1761  &SuggestedModule, &IsMapped);
1762 
1763  if (!File) {
1764  if (Callbacks) {
1765  // Give the clients a chance to recover.
1766  SmallString<128> RecoveryPath;
1767  if (Callbacks->FileNotFound(Filename, RecoveryPath)) {
1768  if (const DirectoryEntry *DE = FileMgr.getDirectory(RecoveryPath)) {
1769  // Add the recovery path to the list of search paths.
1770  DirectoryLookup DL(DE, SrcMgr::C_User, false);
1771  HeaderInfo.AddSearchPath(DL, isAngled);
1772 
1773  // Try the lookup again, skipping the cache.
1774  File = LookupFile(
1775  FilenameLoc,
1776  LangOpts.MSVCCompat ? NormalizedPath.c_str() : Filename, isAngled,
1777  LookupFrom, LookupFromFile, CurDir, nullptr, nullptr,
1778  &SuggestedModule, &IsMapped, /*SkipCache*/ true);
1779  }
1780  }
1781  }
1782 
1783  if (!SuppressIncludeNotFoundError) {
1784  // If the file could not be located and it was included via angle
1785  // brackets, we can attempt a lookup as though it were a quoted path to
1786  // provide the user with a possible fixit.
1787  if (isAngled) {
1788  File = LookupFile(
1789  FilenameLoc,
1790  LangOpts.MSVCCompat ? NormalizedPath.c_str() : Filename, false,
1791  LookupFrom, LookupFromFile, CurDir,
1792  Callbacks ? &SearchPath : nullptr,
1793  Callbacks ? &RelativePath : nullptr, &SuggestedModule, &IsMapped);
1794  if (File) {
1795  SourceRange Range(FilenameTok.getLocation(), CharEnd);
1796  Diag(FilenameTok, diag::err_pp_file_not_found_angled_include_not_fatal) <<
1797  Filename <<
1798  FixItHint::CreateReplacement(Range, "\"" + Filename.str() + "\"");
1799  }
1800  }
1801 
1802  // Check for likely typos due to leading or trailing non-isAlphanumeric
1803  // characters
1804  StringRef OriginalFilename = Filename;
1805  if (LangOpts.SpellChecking && !File) {
1806  // A heuristic to correct a typo file name by removing leading and
1807  // trailing non-isAlphanumeric characters.
1808  auto CorrectTypoFilename = [](llvm::StringRef Filename) {
1809  Filename = Filename.drop_until(isAlphanumeric);
1810  while (!Filename.empty() && !isAlphanumeric(Filename.back())) {
1811  Filename = Filename.drop_back();
1812  }
1813  return Filename;
1814  };
1815  StringRef TypoCorrectionName = CorrectTypoFilename(Filename);
1816  File = LookupFile(
1817  FilenameLoc,
1818  LangOpts.MSVCCompat ? NormalizedPath.c_str() : TypoCorrectionName,
1819  isAngled, LookupFrom, LookupFromFile, CurDir,
1820  Callbacks ? &SearchPath : nullptr,
1821  Callbacks ? &RelativePath : nullptr, &SuggestedModule, &IsMapped);
1822  if (File) {
1823  SourceRange Range(FilenameTok.getLocation(), CharEnd);
1824  auto Hint = isAngled
1826  Range, "<" + TypoCorrectionName.str() + ">")
1828  Range, "\"" + TypoCorrectionName.str() + "\"");
1829  Diag(FilenameTok, diag::err_pp_file_not_found_typo_not_fatal)
1830  << OriginalFilename << TypoCorrectionName << Hint;
1831  // We found the file, so set the Filename to the name after typo
1832  // correction.
1833  Filename = TypoCorrectionName;
1834  }
1835  }
1836 
1837  // If the file is still not found, just go with the vanilla diagnostic
1838  if (!File)
1839  Diag(FilenameTok, diag::err_pp_file_not_found) << OriginalFilename
1840  << FilenameRange;
1841  }
1842  }
1843 
1844  if (usingPCHWithThroughHeader() && SkippingUntilPCHThroughHeader) {
1845  if (isPCHThroughHeader(File))
1846  SkippingUntilPCHThroughHeader = false;
1847  return;
1848  }
1849 
1850  // Should we enter the source file? Set to false if either the source file is
1851  // known to have no effect beyond its effect on module visibility -- that is,
1852  // if it's got an include guard that is already defined or is a modular header
1853  // we've imported or already built.
1854  bool ShouldEnter = true;
1855 
1856  if (PPOpts->SingleFileParseMode)
1857  ShouldEnter = false;
1858 
1859  // If we've reached the max allowed include depth, it is usually due to an
1860  // include cycle. Don't enter already processed files again as it can lead to
1861  // reaching the max allowed include depth again.
1862  if (ShouldEnter && HasReachedMaxIncludeDepth && File &&
1863  HeaderInfo.getFileInfo(File).NumIncludes)
1864  ShouldEnter = false;
1865 
1866  // Determine whether we should try to import the module for this #include, if
1867  // there is one. Don't do so if precompiled module support is disabled or we
1868  // are processing this module textually (because we're building the module).
1869  if (ShouldEnter && File && SuggestedModule && getLangOpts().Modules &&
1870  !isForModuleBuilding(SuggestedModule.getModule(),
1872  getLangOpts().ModuleName)) {
1873  // If this include corresponds to a module but that module is
1874  // unavailable, diagnose the situation and bail out.
1875  // FIXME: Remove this; loadModule does the same check (but produces
1876  // slightly worse diagnostics).
1878  SuggestedModule.getModule())) {
1879  Diag(FilenameTok.getLocation(),
1880  diag::note_implicit_top_level_module_import_here)
1881  << SuggestedModule.getModule()->getTopLevelModuleName();
1882  return;
1883  }
1884 
1885  // Compute the module access path corresponding to this module.
1886  // FIXME: Should we have a second loadModule() overload to avoid this
1887  // extra lookup step?
1889  for (Module *Mod = SuggestedModule.getModule(); Mod; Mod = Mod->Parent)
1890  Path.push_back(std::make_pair(getIdentifierInfo(Mod->Name),
1891  FilenameTok.getLocation()));
1892  std::reverse(Path.begin(), Path.end());
1893 
1894  // Warn that we're replacing the include/import with a module import.
1895  // We only do this in Objective-C, where we have a module-import syntax.
1896  if (getLangOpts().ObjC)
1897  diagnoseAutoModuleImport(*this, HashLoc, IncludeTok, Path, CharEnd);
1898 
1899  // Load the module to import its macros. We'll make the declarations
1900  // visible when the parser gets here.
1901  // FIXME: Pass SuggestedModule in here rather than converting it to a path
1902  // and making the module loader convert it back again.
1903  ModuleLoadResult Imported = TheModuleLoader.loadModule(
1904  IncludeTok.getLocation(), Path, Module::Hidden,
1905  /*IsIncludeDirective=*/true);
1906  assert((Imported == nullptr || Imported == SuggestedModule.getModule()) &&
1907  "the imported module is different than the suggested one");
1908 
1909  if (Imported)
1910  ShouldEnter = false;
1911  else if (Imported.isMissingExpected()) {
1912  // We failed to find a submodule that we assumed would exist (because it
1913  // was in the directory of an umbrella header, for instance), but no
1914  // actual module containing it exists (because the umbrella header is
1915  // incomplete). Treat this as a textual inclusion.
1916  SuggestedModule = ModuleMap::KnownHeader();
1917  } else if (Imported.isConfigMismatch()) {
1918  // On a configuration mismatch, enter the header textually. We still know
1919  // that it's part of the corresponding module.
1920  } else {
1921  // We hit an error processing the import. Bail out.
1923  // With a fatal failure in the module loader, we abort parsing.
1924  Token &Result = IncludeTok;
1925  assert(CurLexer && "#include but no current lexer set!");
1926  Result.startToken();
1927  CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd, tok::eof);
1928  CurLexer->cutOffLexing();
1929  }
1930  return;
1931  }
1932  }
1933 
1934  // The #included file will be considered to be a system header if either it is
1935  // in a system include directory, or if the #includer is a system include
1936  // header.
1937  SrcMgr::CharacteristicKind FileCharacter =
1938  SourceMgr.getFileCharacteristic(FilenameTok.getLocation());
1939  if (File)
1940  FileCharacter = std::max(HeaderInfo.getFileDirFlavor(File), FileCharacter);
1941 
1942  // Ask HeaderInfo if we should enter this #include file. If not, #including
1943  // this file will have no effect.
1944  bool SkipHeader = false;
1945  if (ShouldEnter && File &&
1946  !HeaderInfo.ShouldEnterIncludeFile(*this, File, isImport,
1947  getLangOpts().Modules,
1948  SuggestedModule.getModule())) {
1949  ShouldEnter = false;
1950  SkipHeader = true;
1951  }
1952 
1953  if (Callbacks) {
1954  // Notify the callback object that we've seen an inclusion directive.
1955  Callbacks->InclusionDirective(
1956  HashLoc, IncludeTok,
1957  LangOpts.MSVCCompat ? NormalizedPath.c_str() : Filename, isAngled,
1958  FilenameRange, File, SearchPath, RelativePath,
1959  ShouldEnter ? nullptr : SuggestedModule.getModule(), FileCharacter);
1960  if (SkipHeader && !SuggestedModule.getModule())
1961  Callbacks->FileSkipped(*File, FilenameTok, FileCharacter);
1962  }
1963 
1964  if (!File)
1965  return;
1966 
1967  // FIXME: If we have a suggested module, and we've already visited this file,
1968  // don't bother entering it again. We know it has no further effect.
1969 
1970  // Issue a diagnostic if the name of the file on disk has a different case
1971  // than the one we're about to open.
1972  const bool CheckIncludePathPortability =
1973  !IsMapped && File && !File->tryGetRealPathName().empty();
1974 
1975  if (CheckIncludePathPortability) {
1976  StringRef Name = LangOpts.MSVCCompat ? NormalizedPath.str() : Filename;
1977  StringRef RealPathName = File->tryGetRealPathName();
1978  SmallVector<StringRef, 16> Components(llvm::sys::path::begin(Name),
1979  llvm::sys::path::end(Name));
1980 
1981  if (trySimplifyPath(Components, RealPathName)) {
1982  SmallString<128> Path;
1983  Path.reserve(Name.size()+2);
1984  Path.push_back(isAngled ? '<' : '"');
1985  bool isLeadingSeparator = llvm::sys::path::is_absolute(Name);
1986  for (auto Component : Components) {
1987  if (isLeadingSeparator)
1988  isLeadingSeparator = false;
1989  else
1990  Path.append(Component);
1991  // Append the separator the user used, or the close quote
1992  Path.push_back(
1993  Path.size() <= Filename.size() ? Filename[Path.size()-1] :
1994  (isAngled ? '>' : '"'));
1995  }
1996  // For user files and known standard headers, by default we issue a diagnostic.
1997  // For other system headers, we don't. They can be controlled separately.
1998  auto DiagId = (FileCharacter == SrcMgr::C_User || warnByDefaultOnWrongCase(Name)) ?
1999  diag::pp_nonportable_path : diag::pp_nonportable_system_path;
2000  SourceRange Range(FilenameTok.getLocation(), CharEnd);
2001  Diag(FilenameTok, DiagId) << Path <<
2002  FixItHint::CreateReplacement(Range, Path);
2003  }
2004  }
2005 
2006  // If we don't need to enter the file, stop now.
2007  if (!ShouldEnter) {
2008  // If this is a module import, make it visible if needed.
2009  if (auto *M = SuggestedModule.getModule()) {
2010  // When building a pch, -fmodule-name tells the compiler to textually
2011  // include headers in the specified module. But it is possible that
2012  // ShouldEnter is false because we are skipping the header. In that
2013  // case, We are not importing the specified module.
2014  if (SkipHeader && getLangOpts().CompilingPCH &&
2015  isForModuleBuilding(M, getLangOpts().CurrentModule,
2016  getLangOpts().ModuleName))
2017  return;
2018 
2019  makeModuleVisible(M, HashLoc);
2020 
2021  if (IncludeTok.getIdentifierInfo()->getPPKeywordID() !=
2022  tok::pp___include_macros)
2023  EnterAnnotationToken(SourceRange(HashLoc, End),
2024  tok::annot_module_include, M);
2025  }
2026  return;
2027  }
2028 
2029  // Look up the file, create a File ID for it.
2030  SourceLocation IncludePos = End;
2031  // If the filename string was the result of macro expansions, set the include
2032  // position on the file where it will be included and after the expansions.
2033  if (IncludePos.isMacroID())
2034  IncludePos = SourceMgr.getExpansionRange(IncludePos).getEnd();
2035  FileID FID = SourceMgr.createFileID(File, IncludePos, FileCharacter);
2036  assert(FID.isValid() && "Expected valid file ID");
2037 
2038  // If all is good, enter the new file!
2039  if (EnterSourceFile(FID, CurDir, FilenameTok.getLocation()))
2040  return;
2041 
2042  // Determine if we're switching to building a new submodule, and which one.
2043  if (auto *M = SuggestedModule.getModule()) {
2044  if (M->getTopLevelModule()->ShadowingModule) {
2045  // We are building a submodule that belongs to a shadowed module. This
2046  // means we find header files in the shadowed module.
2047  Diag(M->DefinitionLoc, diag::err_module_build_shadowed_submodule)
2048  << M->getFullModuleName();
2049  Diag(M->getTopLevelModule()->ShadowingModule->DefinitionLoc,
2050  diag::note_previous_definition);
2051  return;
2052  }
2053  // When building a pch, -fmodule-name tells the compiler to textually
2054  // include headers in the specified module. We are not building the
2055  // specified module.
2056  if (getLangOpts().CompilingPCH &&
2057  isForModuleBuilding(M, getLangOpts().CurrentModule,
2058  getLangOpts().ModuleName))
2059  return;
2060 
2061  assert(!CurLexerSubmodule && "should not have marked this as a module yet");
2062  CurLexerSubmodule = M;
2063 
2064  // Let the macro handling code know that any future macros are within
2065  // the new submodule.
2066  EnterSubmodule(M, HashLoc, /*ForPragma*/false);
2067 
2068  // Let the parser know that any future declarations are within the new
2069  // submodule.
2070  // FIXME: There's no point doing this if we're handling a #__include_macros
2071  // directive.
2072  EnterAnnotationToken(SourceRange(HashLoc, End), tok::annot_module_begin, M);
2073  }
2074 }
2075 
2076 /// HandleIncludeNextDirective - Implements \#include_next.
2077 ///
2078 void Preprocessor::HandleIncludeNextDirective(SourceLocation HashLoc,
2079  Token &IncludeNextTok) {
2080  Diag(IncludeNextTok, diag::ext_pp_include_next_directive);
2081 
2082  // #include_next is like #include, except that we start searching after
2083  // the current found directory. If we can't do this, issue a
2084  // diagnostic.
2085  const DirectoryLookup *Lookup = CurDirLookup;
2086  const FileEntry *LookupFromFile = nullptr;
2087  if (isInPrimaryFile() && LangOpts.IsHeaderFile) {
2088  // If the main file is a header, then it's either for PCH/AST generation,
2089  // or libclang opened it. Either way, handle it as a normal include below
2090  // and do not complain about include_next.
2091  } else if (isInPrimaryFile()) {
2092  Lookup = nullptr;
2093  Diag(IncludeNextTok, diag::pp_include_next_in_primary);
2094  } else if (CurLexerSubmodule) {
2095  // Start looking up in the directory *after* the one in which the current
2096  // file would be found, if any.
2097  assert(CurPPLexer && "#include_next directive in macro?");
2098  LookupFromFile = CurPPLexer->getFileEntry();
2099  Lookup = nullptr;
2100  } else if (!Lookup) {
2101  Diag(IncludeNextTok, diag::pp_include_next_absolute_path);
2102  } else {
2103  // Start looking up in the next directory.
2104  ++Lookup;
2105  }
2106 
2107  return HandleIncludeDirective(HashLoc, IncludeNextTok, Lookup,
2108  LookupFromFile);
2109 }
2110 
2111 /// HandleMicrosoftImportDirective - Implements \#import for Microsoft Mode
2112 void Preprocessor::HandleMicrosoftImportDirective(Token &Tok) {
2113  // The Microsoft #import directive takes a type library and generates header
2114  // files from it, and includes those. This is beyond the scope of what clang
2115  // does, so we ignore it and error out. However, #import can optionally have
2116  // trailing attributes that span multiple lines. We're going to eat those
2117  // so we can continue processing from there.
2118  Diag(Tok, diag::err_pp_import_directive_ms );
2119 
2120  // Read tokens until we get to the end of the directive. Note that the
2121  // directive can be split over multiple lines using the backslash character.
2123 }
2124 
2125 /// HandleImportDirective - Implements \#import.
2126 ///
2127 void Preprocessor::HandleImportDirective(SourceLocation HashLoc,
2128  Token &ImportTok) {
2129  if (!LangOpts.ObjC) { // #import is standard for ObjC.
2130  if (LangOpts.MSVCCompat)
2131  return HandleMicrosoftImportDirective(ImportTok);
2132  Diag(ImportTok, diag::ext_pp_import_directive);
2133  }
2134  return HandleIncludeDirective(HashLoc, ImportTok, nullptr, nullptr, true);
2135 }
2136 
2137 /// HandleIncludeMacrosDirective - The -imacros command line option turns into a
2138 /// pseudo directive in the predefines buffer. This handles it by sucking all
2139 /// tokens through the preprocessor and discarding them (only keeping the side
2140 /// effects on the preprocessor).
2141 void Preprocessor::HandleIncludeMacrosDirective(SourceLocation HashLoc,
2142  Token &IncludeMacrosTok) {
2143  // This directive should only occur in the predefines buffer. If not, emit an
2144  // error and reject it.
2145  SourceLocation Loc = IncludeMacrosTok.getLocation();
2146  if (SourceMgr.getBufferName(Loc) != "<built-in>") {
2147  Diag(IncludeMacrosTok.getLocation(),
2148  diag::pp_include_macros_out_of_predefines);
2150  return;
2151  }
2152 
2153  // Treat this as a normal #include for checking purposes. If this is
2154  // successful, it will push a new lexer onto the include stack.
2155  HandleIncludeDirective(HashLoc, IncludeMacrosTok);
2156 
2157  Token TmpTok;
2158  do {
2159  Lex(TmpTok);
2160  assert(TmpTok.isNot(tok::eof) && "Didn't find end of -imacros!");
2161  } while (TmpTok.isNot(tok::hashhash));
2162 }
2163 
2164 //===----------------------------------------------------------------------===//
2165 // Preprocessor Macro Directive Handling.
2166 //===----------------------------------------------------------------------===//
2167 
2168 /// ReadMacroParameterList - The ( starting a parameter list of a macro
2169 /// definition has just been read. Lex the rest of the parameters and the
2170 /// closing ), updating MI with what we learn. Return true if an error occurs
2171 /// parsing the param list.
2172 bool Preprocessor::ReadMacroParameterList(MacroInfo *MI, Token &Tok) {
2174 
2175  while (true) {
2176  LexUnexpandedToken(Tok);
2177  switch (Tok.getKind()) {
2178  case tok::r_paren:
2179  // Found the end of the parameter list.
2180  if (Parameters.empty()) // #define FOO()
2181  return false;
2182  // Otherwise we have #define FOO(A,)
2183  Diag(Tok, diag::err_pp_expected_ident_in_arg_list);
2184  return true;
2185  case tok::ellipsis: // #define X(... -> C99 varargs
2186  if (!LangOpts.C99)
2187  Diag(Tok, LangOpts.CPlusPlus11 ?
2188  diag::warn_cxx98_compat_variadic_macro :
2189  diag::ext_variadic_macro);
2190 
2191  // OpenCL v1.2 s6.9.e: variadic macros are not supported.
2192  if (LangOpts.OpenCL) {
2193  Diag(Tok, diag::err_pp_opencl_variadic_macros);
2194  return true;
2195  }
2196 
2197  // Lex the token after the identifier.
2198  LexUnexpandedToken(Tok);
2199  if (Tok.isNot(tok::r_paren)) {
2200  Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
2201  return true;
2202  }
2203  // Add the __VA_ARGS__ identifier as a parameter.
2204  Parameters.push_back(Ident__VA_ARGS__);
2205  MI->setIsC99Varargs();
2206  MI->setParameterList(Parameters, BP);
2207  return false;
2208  case tok::eod: // #define X(
2209  Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
2210  return true;
2211  default:
2212  // Handle keywords and identifiers here to accept things like
2213  // #define Foo(for) for.
2214  IdentifierInfo *II = Tok.getIdentifierInfo();
2215  if (!II) {
2216  // #define X(1
2217  Diag(Tok, diag::err_pp_invalid_tok_in_arg_list);
2218  return true;
2219  }
2220 
2221  // If this is already used as a parameter, it is used multiple times (e.g.
2222  // #define X(A,A.
2223  if (std::find(Parameters.begin(), Parameters.end(), II) !=
2224  Parameters.end()) { // C99 6.10.3p6
2225  Diag(Tok, diag::err_pp_duplicate_name_in_arg_list) << II;
2226  return true;
2227  }
2228 
2229  // Add the parameter to the macro info.
2230  Parameters.push_back(II);
2231 
2232  // Lex the token after the identifier.
2233  LexUnexpandedToken(Tok);
2234 
2235  switch (Tok.getKind()) {
2236  default: // #define X(A B
2237  Diag(Tok, diag::err_pp_expected_comma_in_arg_list);
2238  return true;
2239  case tok::r_paren: // #define X(A)
2240  MI->setParameterList(Parameters, BP);
2241  return false;
2242  case tok::comma: // #define X(A,
2243  break;
2244  case tok::ellipsis: // #define X(A... -> GCC extension
2245  // Diagnose extension.
2246  Diag(Tok, diag::ext_named_variadic_macro);
2247 
2248  // Lex the token after the identifier.
2249  LexUnexpandedToken(Tok);
2250  if (Tok.isNot(tok::r_paren)) {
2251  Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
2252  return true;
2253  }
2254 
2255  MI->setIsGNUVarargs();
2256  MI->setParameterList(Parameters, BP);
2257  return false;
2258  }
2259  }
2260  }
2261 }
2262 
2263 static bool isConfigurationPattern(Token &MacroName, MacroInfo *MI,
2264  const LangOptions &LOptions) {
2265  if (MI->getNumTokens() == 1) {
2266  const Token &Value = MI->getReplacementToken(0);
2267 
2268  // Macro that is identity, like '#define inline inline' is a valid pattern.
2269  if (MacroName.getKind() == Value.getKind())
2270  return true;
2271 
2272  // Macro that maps a keyword to the same keyword decorated with leading/
2273  // trailing underscores is a valid pattern:
2274  // #define inline __inline
2275  // #define inline __inline__
2276  // #define inline _inline (in MS compatibility mode)
2277  StringRef MacroText = MacroName.getIdentifierInfo()->getName();
2278  if (IdentifierInfo *II = Value.getIdentifierInfo()) {
2279  if (!II->isKeyword(LOptions))
2280  return false;
2281  StringRef ValueText = II->getName();
2282  StringRef TrimmedValue = ValueText;
2283  if (!ValueText.startswith("__")) {
2284  if (ValueText.startswith("_"))
2285  TrimmedValue = TrimmedValue.drop_front(1);
2286  else
2287  return false;
2288  } else {
2289  TrimmedValue = TrimmedValue.drop_front(2);
2290  if (TrimmedValue.endswith("__"))
2291  TrimmedValue = TrimmedValue.drop_back(2);
2292  }
2293  return TrimmedValue.equals(MacroText);
2294  } else {
2295  return false;
2296  }
2297  }
2298 
2299  // #define inline
2300  return MacroName.isOneOf(tok::kw_extern, tok::kw_inline, tok::kw_static,
2301  tok::kw_const) &&
2302  MI->getNumTokens() == 0;
2303 }
2304 
2305 // ReadOptionalMacroParameterListAndBody - This consumes all (i.e. the
2306 // entire line) of the macro's tokens and adds them to MacroInfo, and while
2307 // doing so performs certain validity checks including (but not limited to):
2308 // - # (stringization) is followed by a macro parameter
2309 //
2310 // Returns a nullptr if an invalid sequence of tokens is encountered or returns
2311 // a pointer to a MacroInfo object.
2312 
2313 MacroInfo *Preprocessor::ReadOptionalMacroParameterListAndBody(
2314  const Token &MacroNameTok, const bool ImmediatelyAfterHeaderGuard) {
2315 
2316  Token LastTok = MacroNameTok;
2317  // Create the new macro.
2318  MacroInfo *const MI = AllocateMacroInfo(MacroNameTok.getLocation());
2319 
2320  Token Tok;
2321  LexUnexpandedToken(Tok);
2322 
2323  // Used to un-poison and then re-poison identifiers of the __VA_ARGS__ ilk
2324  // within their appropriate context.
2326 
2327  // If this is a function-like macro definition, parse the argument list,
2328  // marking each of the identifiers as being used as macro arguments. Also,
2329  // check other constraints on the first token of the macro body.
2330  if (Tok.is(tok::eod)) {
2331  if (ImmediatelyAfterHeaderGuard) {
2332  // Save this macro information since it may part of a header guard.
2333  CurPPLexer->MIOpt.SetDefinedMacro(MacroNameTok.getIdentifierInfo(),
2334  MacroNameTok.getLocation());
2335  }
2336  // If there is no body to this macro, we have no special handling here.
2337  } else if (Tok.hasLeadingSpace()) {
2338  // This is a normal token with leading space. Clear the leading space
2339  // marker on the first token to get proper expansion.
2340  Tok.clearFlag(Token::LeadingSpace);
2341  } else if (Tok.is(tok::l_paren)) {
2342  // This is a function-like macro definition. Read the argument list.
2343  MI->setIsFunctionLike();
2344  if (ReadMacroParameterList(MI, LastTok)) {
2345  // Throw away the rest of the line.
2346  if (CurPPLexer->ParsingPreprocessorDirective)
2348  return nullptr;
2349  }
2350 
2351  // If this is a definition of an ISO C/C++ variadic function-like macro (not
2352  // using the GNU named varargs extension) inform our variadic scope guard
2353  // which un-poisons and re-poisons certain identifiers (e.g. __VA_ARGS__)
2354  // allowed only within the definition of a variadic macro.
2355 
2356  if (MI->isC99Varargs()) {
2357  VariadicMacroScopeGuard.enterScope();
2358  }
2359 
2360  // Read the first token after the arg list for down below.
2361  LexUnexpandedToken(Tok);
2362  } else if (LangOpts.C99 || LangOpts.CPlusPlus11) {
2363  // C99 requires whitespace between the macro definition and the body. Emit
2364  // a diagnostic for something like "#define X+".
2365  Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name);
2366  } else {
2367  // C90 6.8 TC1 says: "In the definition of an object-like macro, if the
2368  // first character of a replacement list is not a character required by
2369  // subclause 5.2.1, then there shall be white-space separation between the
2370  // identifier and the replacement list.". 5.2.1 lists this set:
2371  // "A-Za-z0-9!"#%&'()*+,_./:;<=>?[\]^_{|}~" as well as whitespace, which
2372  // is irrelevant here.
2373  bool isInvalid = false;
2374  if (Tok.is(tok::at)) // @ is not in the list above.
2375  isInvalid = true;
2376  else if (Tok.is(tok::unknown)) {
2377  // If we have an unknown token, it is something strange like "`". Since
2378  // all of valid characters would have lexed into a single character
2379  // token of some sort, we know this is not a valid case.
2380  isInvalid = true;
2381  }
2382  if (isInvalid)
2383  Diag(Tok, diag::ext_missing_whitespace_after_macro_name);
2384  else
2385  Diag(Tok, diag::warn_missing_whitespace_after_macro_name);
2386  }
2387 
2388  if (!Tok.is(tok::eod))
2389  LastTok = Tok;
2390 
2391  // Read the rest of the macro body.
2392  if (MI->isObjectLike()) {
2393  // Object-like macros are very simple, just read their body.
2394  while (Tok.isNot(tok::eod)) {
2395  LastTok = Tok;
2396  MI->AddTokenToBody(Tok);
2397  // Get the next token of the macro.
2398  LexUnexpandedToken(Tok);
2399  }
2400  } else {
2401  // Otherwise, read the body of a function-like macro. While we are at it,
2402  // check C99 6.10.3.2p1: ensure that # operators are followed by macro
2403  // parameters in function-like macro expansions.
2404 
2405  VAOptDefinitionContext VAOCtx(*this);
2406 
2407  while (Tok.isNot(tok::eod)) {
2408  LastTok = Tok;
2409 
2410  if (!Tok.isOneOf(tok::hash, tok::hashat, tok::hashhash)) {
2411  MI->AddTokenToBody(Tok);
2412 
2413  if (VAOCtx.isVAOptToken(Tok)) {
2414  // If we're already within a VAOPT, emit an error.
2415  if (VAOCtx.isInVAOpt()) {
2416  Diag(Tok, diag::err_pp_vaopt_nested_use);
2417  return nullptr;
2418  }
2419  // Ensure VAOPT is followed by a '(' .
2420  LexUnexpandedToken(Tok);
2421  if (Tok.isNot(tok::l_paren)) {
2422  Diag(Tok, diag::err_pp_missing_lparen_in_vaopt_use);
2423  return nullptr;
2424  }
2425  MI->AddTokenToBody(Tok);
2426  VAOCtx.sawVAOptFollowedByOpeningParens(Tok.getLocation());
2427  LexUnexpandedToken(Tok);
2428  if (Tok.is(tok::hashhash)) {
2429  Diag(Tok, diag::err_vaopt_paste_at_start);
2430  return nullptr;
2431  }
2432  continue;
2433  } else if (VAOCtx.isInVAOpt()) {
2434  if (Tok.is(tok::r_paren)) {
2435  if (VAOCtx.sawClosingParen()) {
2436  const unsigned NumTokens = MI->getNumTokens();
2437  assert(NumTokens >= 3 && "Must have seen at least __VA_OPT__( "
2438  "and a subsequent tok::r_paren");
2439  if (MI->getReplacementToken(NumTokens - 2).is(tok::hashhash)) {
2440  Diag(Tok, diag::err_vaopt_paste_at_end);
2441  return nullptr;
2442  }
2443  }
2444  } else if (Tok.is(tok::l_paren)) {
2445  VAOCtx.sawOpeningParen(Tok.getLocation());
2446  }
2447  }
2448  // Get the next token of the macro.
2449  LexUnexpandedToken(Tok);
2450  continue;
2451  }
2452 
2453  // If we're in -traditional mode, then we should ignore stringification
2454  // and token pasting. Mark the tokens as unknown so as not to confuse
2455  // things.
2456  if (getLangOpts().TraditionalCPP) {
2457  Tok.setKind(tok::unknown);
2458  MI->AddTokenToBody(Tok);
2459 
2460  // Get the next token of the macro.
2461  LexUnexpandedToken(Tok);
2462  continue;
2463  }
2464 
2465  if (Tok.is(tok::hashhash)) {
2466  // If we see token pasting, check if it looks like the gcc comma
2467  // pasting extension. We'll use this information to suppress
2468  // diagnostics later on.
2469 
2470  // Get the next token of the macro.
2471  LexUnexpandedToken(Tok);
2472 
2473  if (Tok.is(tok::eod)) {
2474  MI->AddTokenToBody(LastTok);
2475  break;
2476  }
2477 
2478  unsigned NumTokens = MI->getNumTokens();
2479  if (NumTokens && Tok.getIdentifierInfo() == Ident__VA_ARGS__ &&
2480  MI->getReplacementToken(NumTokens-1).is(tok::comma))
2481  MI->setHasCommaPasting();
2482 
2483  // Things look ok, add the '##' token to the macro.
2484  MI->AddTokenToBody(LastTok);
2485  continue;
2486  }
2487 
2488  // Our Token is a stringization operator.
2489  // Get the next token of the macro.
2490  LexUnexpandedToken(Tok);
2491 
2492  // Check for a valid macro arg identifier or __VA_OPT__.
2493  if (!VAOCtx.isVAOptToken(Tok) &&
2494  (Tok.getIdentifierInfo() == nullptr ||
2495  MI->getParameterNum(Tok.getIdentifierInfo()) == -1)) {
2496 
2497  // If this is assembler-with-cpp mode, we accept random gibberish after
2498  // the '#' because '#' is often a comment character. However, change
2499  // the kind of the token to tok::unknown so that the preprocessor isn't
2500  // confused.
2501  if (getLangOpts().AsmPreprocessor && Tok.isNot(tok::eod)) {
2502  LastTok.setKind(tok::unknown);
2503  MI->AddTokenToBody(LastTok);
2504  continue;
2505  } else {
2506  Diag(Tok, diag::err_pp_stringize_not_parameter)
2507  << LastTok.is(tok::hashat);
2508  return nullptr;
2509  }
2510  }
2511 
2512  // Things look ok, add the '#' and param name tokens to the macro.
2513  MI->AddTokenToBody(LastTok);
2514 
2515  // If the token following '#' is VAOPT, let the next iteration handle it
2516  // and check it for correctness, otherwise add the token and prime the
2517  // loop with the next one.
2518  if (!VAOCtx.isVAOptToken(Tok)) {
2519  MI->AddTokenToBody(Tok);
2520  LastTok = Tok;
2521 
2522  // Get the next token of the macro.
2523  LexUnexpandedToken(Tok);
2524  }
2525  }
2526  if (VAOCtx.isInVAOpt()) {
2527  assert(Tok.is(tok::eod) && "Must be at End Of preprocessing Directive");
2528  Diag(Tok, diag::err_pp_expected_after)
2529  << LastTok.getKind() << tok::r_paren;
2530  Diag(VAOCtx.getUnmatchedOpeningParenLoc(), diag::note_matching) << tok::l_paren;
2531  return nullptr;
2532  }
2533  }
2534  MI->setDefinitionEndLoc(LastTok.getLocation());
2535  return MI;
2536 }
2537 /// HandleDefineDirective - Implements \#define. This consumes the entire macro
2538 /// line then lets the caller lex the next real token.
2539 void Preprocessor::HandleDefineDirective(
2540  Token &DefineTok, const bool ImmediatelyAfterHeaderGuard) {
2541  ++NumDefined;
2542 
2543  Token MacroNameTok;
2544  bool MacroShadowsKeyword;
2545  ReadMacroName(MacroNameTok, MU_Define, &MacroShadowsKeyword);
2546 
2547  // Error reading macro name? If so, diagnostic already issued.
2548  if (MacroNameTok.is(tok::eod))
2549  return;
2550 
2551  // If we are supposed to keep comments in #defines, reenable comment saving
2552  // mode.
2553  if (CurLexer) CurLexer->SetCommentRetentionState(KeepMacroComments);
2554 
2555  MacroInfo *const MI = ReadOptionalMacroParameterListAndBody(
2556  MacroNameTok, ImmediatelyAfterHeaderGuard);
2557 
2558  if (!MI) return;
2559 
2560  if (MacroShadowsKeyword &&
2561  !isConfigurationPattern(MacroNameTok, MI, getLangOpts())) {
2562  Diag(MacroNameTok, diag::warn_pp_macro_hides_keyword);
2563  }
2564  // Check that there is no paste (##) operator at the beginning or end of the
2565  // replacement list.
2566  unsigned NumTokens = MI->getNumTokens();
2567  if (NumTokens != 0) {
2568  if (MI->getReplacementToken(0).is(tok::hashhash)) {
2569  Diag(MI->getReplacementToken(0), diag::err_paste_at_start);
2570  return;
2571  }
2572  if (MI->getReplacementToken(NumTokens-1).is(tok::hashhash)) {
2573  Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end);
2574  return;
2575  }
2576  }
2577 
2578  // When skipping just warn about macros that do not match.
2579  if (SkippingUntilPCHThroughHeader) {
2580  const MacroInfo *OtherMI = getMacroInfo(MacroNameTok.getIdentifierInfo());
2581  if (!OtherMI || !MI->isIdenticalTo(*OtherMI, *this,
2582  /*Syntactic=*/LangOpts.MicrosoftExt))
2583  Diag(MI->getDefinitionLoc(), diag::warn_pp_macro_def_mismatch_with_pch)
2584  << MacroNameTok.getIdentifierInfo();
2585  return;
2586  }
2587 
2588  // Finally, if this identifier already had a macro defined for it, verify that
2589  // the macro bodies are identical, and issue diagnostics if they are not.
2590  if (const MacroInfo *OtherMI=getMacroInfo(MacroNameTok.getIdentifierInfo())) {
2591  // In Objective-C, ignore attempts to directly redefine the builtin
2592  // definitions of the ownership qualifiers. It's still possible to
2593  // #undef them.
2594  auto isObjCProtectedMacro = [](const IdentifierInfo *II) -> bool {
2595  return II->isStr("__strong") ||
2596  II->isStr("__weak") ||
2597  II->isStr("__unsafe_unretained") ||
2598  II->isStr("__autoreleasing");
2599  };
2600  if (getLangOpts().ObjC &&
2601  SourceMgr.getFileID(OtherMI->getDefinitionLoc())
2602  == getPredefinesFileID() &&
2603  isObjCProtectedMacro(MacroNameTok.getIdentifierInfo())) {
2604  // Warn if it changes the tokens.
2606  !SourceMgr.isInSystemHeader(DefineTok.getLocation())) &&
2607  !MI->isIdenticalTo(*OtherMI, *this,
2608  /*Syntactic=*/LangOpts.MicrosoftExt)) {
2609  Diag(MI->getDefinitionLoc(), diag::warn_pp_objc_macro_redef_ignored);
2610  }
2611  assert(!OtherMI->isWarnIfUnused());
2612  return;
2613  }
2614 
2615  // It is very common for system headers to have tons of macro redefinitions
2616  // and for warnings to be disabled in system headers. If this is the case,
2617  // then don't bother calling MacroInfo::isIdenticalTo.
2619  !SourceMgr.isInSystemHeader(DefineTok.getLocation())) {
2620  if (!OtherMI->isUsed() && OtherMI->isWarnIfUnused())
2621  Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used);
2622 
2623  // Warn if defining "__LINE__" and other builtins, per C99 6.10.8/4 and
2624  // C++ [cpp.predefined]p4, but allow it as an extension.
2625  if (OtherMI->isBuiltinMacro())
2626  Diag(MacroNameTok, diag::ext_pp_redef_builtin_macro);
2627  // Macros must be identical. This means all tokens and whitespace
2628  // separation must be the same. C99 6.10.3p2.
2629  else if (!OtherMI->isAllowRedefinitionsWithoutWarning() &&
2630  !MI->isIdenticalTo(*OtherMI, *this, /*Syntactic=*/LangOpts.MicrosoftExt)) {
2631  Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef)
2632  << MacroNameTok.getIdentifierInfo();
2633  Diag(OtherMI->getDefinitionLoc(), diag::note_previous_definition);
2634  }
2635  }
2636  if (OtherMI->isWarnIfUnused())
2637  WarnUnusedMacroLocs.erase(OtherMI->getDefinitionLoc());
2638  }
2639 
2640  DefMacroDirective *MD =
2641  appendDefMacroDirective(MacroNameTok.getIdentifierInfo(), MI);
2642 
2643  assert(!MI->isUsed());
2644  // If we need warning for not using the macro, add its location in the
2645  // warn-because-unused-macro set. If it gets used it will be removed from set.
2647  !Diags->isIgnored(diag::pp_macro_not_used, MI->getDefinitionLoc())) {
2648  MI->setIsWarnIfUnused(true);
2649  WarnUnusedMacroLocs.insert(MI->getDefinitionLoc());
2650  }
2651 
2652  // If the callbacks want to know, tell them about the macro definition.
2653  if (Callbacks)
2654  Callbacks->MacroDefined(MacroNameTok, MD);
2655 }
2656 
2657 /// HandleUndefDirective - Implements \#undef.
2658 ///
2659 void Preprocessor::HandleUndefDirective() {
2660  ++NumUndefined;
2661 
2662  Token MacroNameTok;
2663  ReadMacroName(MacroNameTok, MU_Undef);
2664 
2665  // Error reading macro name? If so, diagnostic already issued.
2666  if (MacroNameTok.is(tok::eod))
2667  return;
2668 
2669  // Check to see if this is the last token on the #undef line.
2670  CheckEndOfDirective("undef");
2671 
2672  // Okay, we have a valid identifier to undef.
2673  auto *II = MacroNameTok.getIdentifierInfo();
2674  auto MD = getMacroDefinition(II);
2675  UndefMacroDirective *Undef = nullptr;
2676 
2677  // If the macro is not defined, this is a noop undef.
2678  if (const MacroInfo *MI = MD.getMacroInfo()) {
2679  if (!MI->isUsed() && MI->isWarnIfUnused())
2680  Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used);
2681 
2682  if (MI->isWarnIfUnused())
2683  WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());
2684 
2685  Undef = AllocateUndefMacroDirective(MacroNameTok.getLocation());
2686  }
2687 
2688  // If the callbacks want to know, tell them about the macro #undef.
2689  // Note: no matter if the macro was defined or not.
2690  if (Callbacks)
2691  Callbacks->MacroUndefined(MacroNameTok, MD, Undef);
2692 
2693  if (Undef)
2694  appendMacroDirective(II, Undef);
2695 }
2696 
2697 //===----------------------------------------------------------------------===//
2698 // Preprocessor Conditional Directive Handling.
2699 //===----------------------------------------------------------------------===//
2700 
2701 /// HandleIfdefDirective - Implements the \#ifdef/\#ifndef directive. isIfndef
2702 /// is true when this is a \#ifndef directive. ReadAnyTokensBeforeDirective is
2703 /// true if any tokens have been returned or pp-directives activated before this
2704 /// \#ifndef has been lexed.
2705 ///
2706 void Preprocessor::HandleIfdefDirective(Token &Result,
2707  const Token &HashToken,
2708  bool isIfndef,
2709  bool ReadAnyTokensBeforeDirective) {
2710  ++NumIf;
2711  Token DirectiveTok = Result;
2712 
2713  Token MacroNameTok;
2714  ReadMacroName(MacroNameTok);
2715 
2716  // Error reading macro name? If so, diagnostic already issued.
2717  if (MacroNameTok.is(tok::eod)) {
2718  // Skip code until we get to #endif. This helps with recovery by not
2719  // emitting an error when the #endif is reached.
2720  SkipExcludedConditionalBlock(HashToken.getLocation(),
2721  DirectiveTok.getLocation(),
2722  /*Foundnonskip*/ false, /*FoundElse*/ false);
2723  return;
2724  }
2725 
2726  // Check to see if this is the last token on the #if[n]def line.
2727  CheckEndOfDirective(isIfndef ? "ifndef" : "ifdef");
2728 
2729  IdentifierInfo *MII = MacroNameTok.getIdentifierInfo();
2730  auto MD = getMacroDefinition(MII);
2731  MacroInfo *MI = MD.getMacroInfo();
2732 
2733  if (CurPPLexer->getConditionalStackDepth() == 0) {
2734  // If the start of a top-level #ifdef and if the macro is not defined,
2735  // inform MIOpt that this might be the start of a proper include guard.
2736  // Otherwise it is some other form of unknown conditional which we can't
2737  // handle.
2738  if (!ReadAnyTokensBeforeDirective && !MI) {
2739  assert(isIfndef && "#ifdef shouldn't reach here");
2740  CurPPLexer->MIOpt.EnterTopLevelIfndef(MII, MacroNameTok.getLocation());
2741  } else
2742  CurPPLexer->MIOpt.EnterTopLevelConditional();
2743  }
2744 
2745  // If there is a macro, process it.
2746  if (MI) // Mark it used.
2747  markMacroAsUsed(MI);
2748 
2749  if (Callbacks) {
2750  if (isIfndef)
2751  Callbacks->Ifndef(DirectiveTok.getLocation(), MacroNameTok, MD);
2752  else
2753  Callbacks->Ifdef(DirectiveTok.getLocation(), MacroNameTok, MD);
2754  }
2755 
2756  // Should we include the stuff contained by this directive?
2757  if (PPOpts->SingleFileParseMode && !MI) {
2758  // In 'single-file-parse mode' undefined identifiers trigger parsing of all
2759  // the directive blocks.
2760  CurPPLexer->pushConditionalLevel(DirectiveTok.getLocation(),
2761  /*wasskip*/false, /*foundnonskip*/false,
2762  /*foundelse*/false);
2763  } else if (!MI == isIfndef) {
2764  // Yes, remember that we are inside a conditional, then lex the next token.
2765  CurPPLexer->pushConditionalLevel(DirectiveTok.getLocation(),
2766  /*wasskip*/false, /*foundnonskip*/true,
2767  /*foundelse*/false);
2768  } else {
2769  // No, skip the contents of this block.
2770  SkipExcludedConditionalBlock(HashToken.getLocation(),
2771  DirectiveTok.getLocation(),
2772  /*Foundnonskip*/ false,
2773  /*FoundElse*/ false);
2774  }
2775 }
2776 
2777 /// HandleIfDirective - Implements the \#if directive.
2778 ///
2779 void Preprocessor::HandleIfDirective(Token &IfToken,
2780  const Token &HashToken,
2781  bool ReadAnyTokensBeforeDirective) {
2782  ++NumIf;
2783 
2784  // Parse and evaluate the conditional expression.
2785  IdentifierInfo *IfNDefMacro = nullptr;
2786  const SourceLocation ConditionalBegin = CurPPLexer->getSourceLocation();
2787  const DirectiveEvalResult DER = EvaluateDirectiveExpression(IfNDefMacro);
2788  const bool ConditionalTrue = DER.Conditional;
2789  const SourceLocation ConditionalEnd = CurPPLexer->getSourceLocation();
2790 
2791  // If this condition is equivalent to #ifndef X, and if this is the first
2792  // directive seen, handle it for the multiple-include optimization.
2793  if (CurPPLexer->getConditionalStackDepth() == 0) {
2794  if (!ReadAnyTokensBeforeDirective && IfNDefMacro && ConditionalTrue)
2795  // FIXME: Pass in the location of the macro name, not the 'if' token.
2796  CurPPLexer->MIOpt.EnterTopLevelIfndef(IfNDefMacro, IfToken.getLocation());
2797  else
2798  CurPPLexer->MIOpt.EnterTopLevelConditional();
2799  }
2800 
2801  if (Callbacks)
2802  Callbacks->If(IfToken.getLocation(),
2803  SourceRange(ConditionalBegin, ConditionalEnd),
2804  (ConditionalTrue ? PPCallbacks::CVK_True : PPCallbacks::CVK_False));
2805 
2806  // Should we include the stuff contained by this directive?
2807  if (PPOpts->SingleFileParseMode && DER.IncludedUndefinedIds) {
2808  // In 'single-file-parse mode' undefined identifiers trigger parsing of all
2809  // the directive blocks.
2810  CurPPLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
2811  /*foundnonskip*/false, /*foundelse*/false);
2812  } else if (ConditionalTrue) {
2813  // Yes, remember that we are inside a conditional, then lex the next token.
2814  CurPPLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
2815  /*foundnonskip*/true, /*foundelse*/false);
2816  } else {
2817  // No, skip the contents of this block.
2818  SkipExcludedConditionalBlock(HashToken.getLocation(), IfToken.getLocation(),
2819  /*Foundnonskip*/ false,
2820  /*FoundElse*/ false);
2821  }
2822 }
2823 
2824 /// HandleEndifDirective - Implements the \#endif directive.
2825 ///
2826 void Preprocessor::HandleEndifDirective(Token &EndifToken) {
2827  ++NumEndif;
2828 
2829  // Check that this is the whole directive.
2830  CheckEndOfDirective("endif");
2831 
2832  PPConditionalInfo CondInfo;
2833  if (CurPPLexer->popConditionalLevel(CondInfo)) {
2834  // No conditionals on the stack: this is an #endif without an #if.
2835  Diag(EndifToken, diag::err_pp_endif_without_if);
2836  return;
2837  }
2838 
2839  // If this the end of a top-level #endif, inform MIOpt.
2840  if (CurPPLexer->getConditionalStackDepth() == 0)
2841  CurPPLexer->MIOpt.ExitTopLevelConditional();
2842 
2843  assert(!CondInfo.WasSkipping && !CurPPLexer->LexingRawMode &&
2844  "This code should only be reachable in the non-skipping case!");
2845 
2846  if (Callbacks)
2847  Callbacks->Endif(EndifToken.getLocation(), CondInfo.IfLoc);
2848 }
2849 
2850 /// HandleElseDirective - Implements the \#else directive.
2851 ///
2852 void Preprocessor::HandleElseDirective(Token &Result, const Token &HashToken) {
2853  ++NumElse;
2854 
2855  // #else directive in a non-skipping conditional... start skipping.
2856  CheckEndOfDirective("else");
2857 
2858  PPConditionalInfo CI;
2859  if (CurPPLexer->popConditionalLevel(CI)) {
2860  Diag(Result, diag::pp_err_else_without_if);
2861  return;
2862  }
2863 
2864  // If this is a top-level #else, inform the MIOpt.
2865  if (CurPPLexer->getConditionalStackDepth() == 0)
2866  CurPPLexer->MIOpt.EnterTopLevelConditional();
2867 
2868  // If this is a #else with a #else before it, report the error.
2869  if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else);
2870 
2871  if (Callbacks)
2872  Callbacks->Else(Result.getLocation(), CI.IfLoc);
2873 
2874  if (PPOpts->SingleFileParseMode && !CI.FoundNonSkip) {
2875  // In 'single-file-parse mode' undefined identifiers trigger parsing of all
2876  // the directive blocks.
2877  CurPPLexer->pushConditionalLevel(CI.IfLoc, /*wasskip*/false,
2878  /*foundnonskip*/false, /*foundelse*/true);
2879  return;
2880  }
2881 
2882  // Finally, skip the rest of the contents of this block.
2883  SkipExcludedConditionalBlock(HashToken.getLocation(), CI.IfLoc,
2884  /*Foundnonskip*/ true,
2885  /*FoundElse*/ true, Result.getLocation());
2886 }
2887 
2888 /// HandleElifDirective - Implements the \#elif directive.
2889 ///
2890 void Preprocessor::HandleElifDirective(Token &ElifToken,
2891  const Token &HashToken) {
2892  ++NumElse;
2893 
2894  // #elif directive in a non-skipping conditional... start skipping.
2895  // We don't care what the condition is, because we will always skip it (since
2896  // the block immediately before it was included).
2897  const SourceLocation ConditionalBegin = CurPPLexer->getSourceLocation();
2899  const SourceLocation ConditionalEnd = CurPPLexer->getSourceLocation();
2900 
2901  PPConditionalInfo CI;
2902  if (CurPPLexer->popConditionalLevel(CI)) {
2903  Diag(ElifToken, diag::pp_err_elif_without_if);
2904  return;
2905  }
2906 
2907  // If this is a top-level #elif, inform the MIOpt.
2908  if (CurPPLexer->getConditionalStackDepth() == 0)
2909  CurPPLexer->MIOpt.EnterTopLevelConditional();
2910 
2911  // If this is a #elif with a #else before it, report the error.
2912  if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else);
2913 
2914  if (Callbacks)
2915  Callbacks->Elif(ElifToken.getLocation(),
2916  SourceRange(ConditionalBegin, ConditionalEnd),
2918 
2919  if (PPOpts->SingleFileParseMode && !CI.FoundNonSkip) {
2920  // In 'single-file-parse mode' undefined identifiers trigger parsing of all
2921  // the directive blocks.
2922  CurPPLexer->pushConditionalLevel(ElifToken.getLocation(), /*wasskip*/false,
2923  /*foundnonskip*/false, /*foundelse*/false);
2924  return;
2925  }
2926 
2927  // Finally, skip the rest of the contents of this block.
2928  SkipExcludedConditionalBlock(
2929  HashToken.getLocation(), CI.IfLoc, /*Foundnonskip*/ true,
2930  /*FoundElse*/ CI.FoundElse, ElifToken.getLocation());
2931 }
StringRef tryGetRealPathName() const
Definition: FileManager.h:87
std::string Name
The name of this module.
Definition: Module.h:68
Module * getModuleForLocation(SourceLocation Loc)
Find the module that owns the source or header file that Loc points to.
SourceLocation getLocWithOffset(int Offset) const
Return a source location with the specified offset from this SourceLocation.
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:249
void markMacroAsUsed(MacroInfo *MI)
A macro is used, update information about macros that need unused warnings.
void sawOpeningParen(SourceLocation LParenLoc)
Call this function each time an lparen is seen.
IdentifierInfo * getIdentifierInfo(StringRef Name) const
Return information about the specified preprocessor identifier token.
Defines the clang::FileManager interface and associated types.
static bool ReadLineMarkerFlags(bool &IsFileEntry, bool &IsFileExit, SrcMgr::CharacteristicKind &FileKind, Preprocessor &PP)
ReadLineMarkerFlags - Parse and validate any flags at the end of a GNU line marker directive...
This header is part of the module (for layering purposes) but should be textually included...
Definition: ModuleMap.h:131
const Token & LookAhead(unsigned N)
Peeks ahead N tokens and returns that token without consuming any tokens.
PPConditionalInfo & peekConditionalLevel()
Return the top of the conditional stack.
StringRef getBufferName(SourceLocation Loc, bool *Invalid=nullptr) const
Return the filename or buffer identifier of the buffer the location is in.
static bool isReservedId(StringRef Text, const LangOptions &Lang)
Checks if the specified identifier is reserved in the specified language.
bool is(tok::TokenKind K) const
is/isNot - Predicates to check if this token is a specific kind, as in "if (Tok.is(tok::l_brace)) {...
Definition: Token.h:95
Module * getTopLevelModule()
Retrieve the top-level module for this (sub)module, which may be this module.
Definition: Module.h:451
bool sawClosingParen()
Call this function each time an rparen is seen.
Defines the SourceManager interface.
Defines the clang::Module class, which describes a module in the source code.
Module * getCurrentModule()
Retrieves the module that we&#39;re currently building, if any.
void pushConditionalLevel(SourceLocation DirectiveStart, bool WasSkipping, bool FoundNonSkip, bool FoundElse)
pushConditionalLevel - When we enter a #if directive, this keeps track of what we are currently in fo...
unsigned IsFramework
Whether this is a framework module.
Definition: Module.h:219
bool isInPrimaryFile() const
Return true if we&#39;re in the top-level file, not in a #include.
StringRef P
Defines the clang::MacroInfo and clang::MacroDirective classes.
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
Definition: Diagnostic.h:1292
A directive for an undefined macro.
Definition: MacroInfo.h:430
bool getSuppressSystemWarnings() const
Definition: Diagnostic.h:623
void setCodeCompletionReached()
Note that we hit the code-completion point.
bool hadModuleLoaderFatalFailure() const
Definition: Preprocessor.h:840
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 *IsMapped, bool SkipCache=false)
Given a "foo" or <foo> reference, look up the indicated file.
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:655
void setIsWarnIfUnused(bool val)
Set the value of the IsWarnIfUnused flag.
Definition: MacroInfo.h:161
tok::TokenKind getKind() const
Definition: Token.h:90
static MacroDiag shouldWarnOnMacroDef(Preprocessor &PP, IdentifierInfo *II)
void setIsGNUVarargs()
Definition: MacroInfo.h:205
DefMacroDirective * appendDefMacroDirective(IdentifierInfo *II, MacroInfo *MI, SourceLocation Loc)
One of these records is kept for each identifier that is lexed.
A directive for a defined macro or a macro imported from a module.
Definition: MacroInfo.h:407
bool ParsingPreprocessorDirective
True when parsing #XXX; turns &#39;\n&#39; into a tok::eod token.
bool isStr(const char(&Str)[StrLen]) const
Return true if this is the identifier for the specified string.
bool CheckMacroName(Token &MacroNameTok, MacroUse isDefineUndef, bool *ShadowFlag=nullptr)
void EnterSubmodule(Module *M, SourceLocation ImportLoc, bool ForPragma)
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 isCPlusPlusOperatorKeyword() const
const FileEntry * LookupSubframeworkHeader(StringRef Filename, const FileEntry *ContextFileEnt, SmallVectorImpl< char > *SearchPath, SmallVectorImpl< char > *RelativePath, Module *RequestingModule, ModuleMap::KnownHeader *SuggestedModule)
Look up a subframework for the specified #include file.
const TargetInfo & getTargetInfo() const
Definition: Preprocessor.h:816
Token - This structure provides full information about a lexed token.
Definition: Token.h:35
static bool trySimplifyPath(SmallVectorImpl< StringRef > &Components, StringRef RealPathName)
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:50
const LangOptions & getLangOpts() const
Definition: Preprocessor.h:815
Describes a module or submodule.
Definition: Module.h:65
bool popConditionalLevel(PPConditionalInfo &CI)
popConditionalLevel - Remove an entry off the top of the conditional stack, returning information abo...
A directive for setting the module visibility of a macro.
Definition: MacroInfo.h:445
StringRef getTopLevelModuleName() const
Retrieve the name of the top-level module.
Definition: Module.h:461
std::pair< FileID, unsigned > getDecomposedExpansionLoc(SourceLocation Loc) const
Decompose the specified location into a raw FileID + Offset pair.
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...
MacroUse
Context in which macro name is used.
Definition: Preprocessor.h:108
std::string getFullModuleName(bool AllowStringLiterals=false) const
Retrieve the full name of this module, including the path from its top-level module.
Definition: Module.cpp:214
StringRef getSpelling(SourceLocation loc, SmallVectorImpl< char > &buffer, bool *invalid=nullptr) const
Return the &#39;spelling&#39; of the token at the given location; does not go up to the spelling location or ...
const Token & getReplacementToken(unsigned Tok) const
Definition: MacroInfo.h:236
bool hasUDSuffix() const
Return true if this token is a string or character literal which has a ud-suffix. ...
Definition: Token.h:291
const FormatToken & Tok
SourceLocation getExpansionLoc(SourceLocation Loc) const
Given a SourceLocation object Loc, return the expansion location referenced by the ID...
void HandleDirective(Token &Result)
Callback invoked when the lexer sees a # token at the start of a line.
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:149
bool GetIncludeFilenameSpelling(SourceLocation Loc, StringRef &Buffer)
Turn the specified lexer token into a fully checked and spelled filename, e.g.
Module * Parent
The parent of this module.
Definition: Module.h:91
bool isUsed() const
Return false if this macro is defined in the main file and has not yet been used. ...
Definition: MacroInfo.h:223
bool FoundNonSkip
True if we have emitted tokens already, and now we&#39;re in an #else block or something.
Definition: Token.h:323
LLVM_READONLY bool isUppercase(unsigned char c)
Return true if this character is an uppercase ASCII letter: [A-Z].
Definition: CharInfo.h:106
void appendMacroDirective(IdentifierInfo *II, MacroDirective *MD)
Add a directive to the macro directive history for this identifier.
void LexUnexpandedToken(Token &Result)
Just like Lex, but disables macro expansion of identifier tokens.
MacroDiag
Enumerates possible cases of #define/#undef a reserved identifier.
unsigned IsSystem
Whether this is a "system" module (which assumes that all headers in it are system headers)...
Definition: Module.h:226
return Out str()
void HandleSkippedDirectiveWhileUsingPCH(Token &Result, SourceLocation HashLoc)
Process directives while skipping until the through header or #pragma hdrstop is found.
std::string CurrentModule
The name of the current module, of which the main source file is a part.
Definition: LangOptions.h:219
const DirectoryEntry * getDirectory(StringRef DirName, bool CacheFailure=true)
Lookup, cache, and verify the specified directory (real or virtual).
Describes the result of attempting to load a module.
Definition: ModuleLoader.h:36
StringRef Filename
Definition: Format.cpp:1629
bool isValid() const
Exposes information about the current target.
Definition: TargetInfo.h:54
void setAnnotationValue(void *val)
Definition: Token.h:228
void diagnoseHeaderInclusion(Module *RequestingModule, bool RequestingModuleIsModuleInterface, SourceLocation FilenameLoc, StringRef Filename, const FileEntry *File)
Reports errors if a module must not include a specific file.
Definition: ModuleMap.cpp:464
Defines the clang::LangOptions interface.
bool LexingRawMode
True if in raw mode.
SourceLocation End
Represents a character-granular source range.
void setHasCommaPasting()
Definition: MacroInfo.h:219
bool isInvalid() const
Return true if this object is invalid or uninitialized.
void makeModuleVisible(Module *M, SourceLocation Loc)
bool isInSystemHeader(SourceLocation Loc) const
Returns if a SourceLocation is in a system header.
const FileEntry * getFileEntryForID(FileID FID) const
Returns the FileEntry record for the provided FileID.
bool usingPCHWithThroughHeader()
True if using a PCH with a through header.
bool isPCHThroughHeader(const FileEntry *FE)
Returns true if the FileEntry is the PCH through header.
A class for tracking whether we&#39;re inside a VA_OPT during a traversal of the tokens of a variadic mac...
static bool warnByDefaultOnWrongCase(StringRef Include)
SourceLocation getLocation() const
Return a source location identifier for the specified offset in the current file. ...
Definition: Token.h:124
Defines the clang::Preprocessor interface.
LLVM_READONLY bool isAlphanumeric(unsigned char c)
Return true if this character is an ASCII letter or digit: [a-zA-Z0-9].
Definition: CharInfo.h:118
void enterScope()
Client code should call this function just before the Preprocessor is about to Lex tokens from the de...
static bool checkModuleIsAvailable(const LangOptions &LangOpts, const TargetInfo &TargetInfo, DiagnosticsEngine &Diags, Module *M)
Check that the given module is available, producing a diagnostic if not.
const MacroInfo * getMacroInfo(const IdentifierInfo *II) const
Definition: Preprocessor.h:981
Defines the clang::IdentifierInfo, clang::IdentifierTable, and clang::Selector interfaces.
SourceLocation getUnmatchedOpeningParenLoc() const
KnownHeader findModuleForHeader(const FileEntry *File, bool AllowTextual=false)
Retrieve the module that owns the given header file, if any.
Definition: ModuleMap.cpp:560
Information about the conditional stack (#if directives) currently active.
Definition: Token.h:313
Represents an unpacked "presumed" location which can be presented to the user.
SourceLocation getEnd() const
bool isAvailable() const
Determine whether this module is available for use within the current translation unit...
Definition: Module.h:379
bool isObjectLike() const
Definition: MacroInfo.h:201
The result type of a method or function.
StringRef GetString() const
const SourceManager & SM
Definition: Format.cpp:1490
DirectoryLookup - This class represents one entry in the search list that specifies the search order ...
bool isKeyword(const LangOptions &LangOpts) const
Return true if this token is a keyword in the specified language.
bool isRecordingPreamble() const
static CharSourceRange getCharRange(SourceRange R)
SourceManager & getSourceManager() const
Definition: Preprocessor.h:819
virtual SourceLocation getSourceLocation()=0
Return the source location for the next observable location.
MacroDirective * getLocalMacroDirective(const IdentifierInfo *II) const
Given an identifier, return its latest non-imported MacroDirective if it is #define&#39;d and not #undef&#39;...
Definition: Preprocessor.h:970
const DirectoryEntry * getDir() const
Return the directory the file lives in.
Definition: FileManager.h:96
bool isVAOptToken(const Token &T) const
void setIsFunctionLike()
Function/Object-likeness.
Definition: MacroInfo.h:199
Encapsulates changes to the "macros namespace" (the location where the macro name became active...
Definition: MacroInfo.h:291
Kind
bool WasSkipping
True if this was contained in a skipping directive, e.g., in a "\#if 0" block.
Definition: Token.h:319
Encodes a location in the source.
bool isConfigMismatch() const
Determines whether the module failed to load due to a configuration mismatch with an explicitly-named...
Definition: ModuleLoader.h:66
friend class VariadicMacroScopeGuard
Definition: Preprocessor.h:127
MacroDefinition getMacroDefinition(const IdentifierInfo *II)
Definition: Preprocessor.h:940
All of the names in this module are hidden.
Definition: Module.h:269
IdentifierInfo * getIdentifierInfo() const
Definition: Token.h:177
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:60
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:204
bool isInVAOpt() const
Returns true if we have seen the VA_OPT and &#39;(&#39; but before having seen the matching &#39;)&#39;...
void Lex(Token &Result)
Lex the next token for this preprocessor.
const char * getNameStart() const
Return the beginning of the actual null-terminated string for this identifier.
void setDefinitionEndLoc(SourceLocation EndLoc)
Set the location of the last token in the macro.
Definition: MacroInfo.h:127
bool isAtStartOfLine() const
isAtStartOfLine - Return true if this token is at the start of a line.
Definition: Token.h:266
TokenKind
Provides a simple uniform namespace for tokens from all C languages.
Definition: TokenKinds.h:25
bool isInMainFile(SourceLocation Loc) const
Returns whether the PresumedLoc for a given SourceLocation is in the main file.
std::string ModuleName
The module currently being compiled as speficied by -fmodule-name.
Definition: LangOptions.h:212
const FileEntry * LookupFile(StringRef Filename, SourceLocation IncludeLoc, bool isAngled, const DirectoryLookup *FromDir, const DirectoryLookup *&CurDir, ArrayRef< std::pair< const FileEntry *, const DirectoryEntry *>> Includers, SmallVectorImpl< char > *SearchPath, SmallVectorImpl< char > *RelativePath, Module *RequestingModule, ModuleMap::KnownHeader *SuggestedModule, bool *IsMapped, bool SkipCache=false, bool BuildSystemModule=false)
Given a "foo" or <foo> reference, look up the indicated file, return null on failure.
std::pair< std::string, bool > Requirement
An individual requirement: a feature name and a flag indicating the required state of that feature...
Definition: Module.h:192
bool isC99Varargs() const
Definition: MacroInfo.h:206
__DEVICE__ void * memcpy(void *__a, const void *__b, size_t __c)
StringRef getName() const
Return the actual identifier string.
SourceLocation IfLoc
Location where the conditional started.
Definition: Token.h:315
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
bool isNot(tok::TokenKind K) const
Definition: Token.h:96
static bool isInvalid(LocType Loc, bool *Invalid)
bool EnterSourceFile(FileID FID, 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...
Dataflow Directional Tag Classes.
bool isWarnIfUnused() const
Return true if we should emit a warning if the macro is unused.
Definition: MacroInfo.h:231
bool isValid() const
Return true if this is a valid SourceLocation object.
The pragma was introduced via #pragma.
Definition: Pragma.h:36
unsigned getNumTokens() const
Return the number of tokens that this macro expands to.
Definition: MacroInfo.h:234
Stored information about a header directive that was found in the module map file but has not been re...
Definition: Module.h:172
const FileEntry * getFileEntry() const
getFileEntry - Return the FileEntry corresponding to this FileID.
static bool isConfigurationPattern(Token &MacroName, MacroInfo *MI, const LangOptions &LOptions)
ArrayRef< KnownHeader > findAllModulesForHeader(const FileEntry *File) const
Retrieve all the modules that contain the given header file.
Definition: ModuleMap.cpp:658
SourceLocation getIncludeLoc(FileID FID) const
Returns the include location if FID is a #include&#39;d file otherwise it returns an invalid location...
FileID getMainFileID() const
Returns the FileID of the main source file.
bool isMissingExpected() const
Determines whether the module, which failed to load, was actually a submodule that we expected to see...
Definition: ModuleLoader.h:62
static bool isForModuleBuilding(Module *M, StringRef CurrentModule, StringRef ModuleName)
bool isOneOf(tok::TokenKind K1, tok::TokenKind K2) const
Definition: Token.h:97
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.
static MacroDiag shouldWarnOnMacroUndef(Preprocessor &PP, IdentifierInfo *II)
unsigned getLength() const
Definition: Token.h:127
Encapsulates the data about a macro definition (e.g.
Definition: MacroInfo.h:40
SourceLocation DefinitionLoc
The location of the module definition.
Definition: Module.h:71
LLVM_READONLY bool isDigit(unsigned char c)
Return true if this character is an ASCII digit: [0-9].
Definition: CharInfo.h:94
FileID getFileID(SourceLocation SpellingLoc) const
Return the FileID for a SourceLocation.
int getParameterNum(const IdentifierInfo *Arg) const
Return the parameter number of the specified identifier, or -1 if the identifier is not a formal para...
Definition: MacroInfo.h:190
Module * lookupModule(StringRef ModuleName, bool AllowSearch=true, bool AllowExtraModuleMapSearch=false)
Lookup a module Search for a module with the given name.
static FixItHint CreateInsertion(SourceLocation InsertionLoc, StringRef Code, bool BeforePreviousInsertions=false)
Create a code modification hint that inserts the given code string at a specific location.
Definition: Diagnostic.h:92
SourceLocation getDefinitionLoc() const
Return the location that the macro was defined at.
Definition: MacroInfo.h:124
void CheckEndOfDirective(const char *DirType, bool EnableMacros=false)
Ensure that the next token is a tok::eod token.
Cached information about one directory (either on disk or in the virtual file system).
Definition: FileManager.h:46
Defines the PPCallbacks interface.
Defines the clang::TokenKind enum and support functions.
static bool GetLineValue(Token &DigitTok, unsigned &Val, unsigned DiagID, Preprocessor &PP, bool IsGNULineDirective=false)
GetLineValue - Convert a numeric token into an unsigned value, emitting Diagnostic DiagID if it is in...
virtual void CodeCompleteMacroName(bool IsDefinition)
Callback invoked when performing code completion in a context where the name of a macro is expected...
Defines the clang::SourceLocation class and associated facilities.
FileID getPredefinesFileID() const
Returns the FileID for the preprocessor predefines.
Definition: Preprocessor.h:900
DiagnosticsEngine & getDiagnostics() const
Definition: Preprocessor.h:812
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:75
StringRef getRawIdentifier() const
getRawIdentifier - For a raw identifier token (i.e., an identifier lexed in raw mode), returns a reference to the text substring in the buffer if known.
Definition: Token.h:203
Module * getModule() const
Retrieve the module the header is stored in.
Definition: ModuleMap.h:164
bool isBuiltinMacro() const
Return true if this macro requires processing before expansion.
Definition: MacroInfo.h:216
StringLiteralParser - This decodes string escape characters and performs wide string analysis and Tra...
static FixItHint CreateReplacement(CharSourceRange RemoveRange, StringRef Code)
Create a code modification hint that replaces the given source range with the given code string...
Definition: Diagnostic.h:129
void sawVAOptFollowedByOpeningParens(const SourceLocation LParenLoc)
Call this function as soon as you see VA_OPT and &#39;(&#39;.
const FileEntry * getModuleHeaderToIncludeForDiagnostics(SourceLocation IncLoc, Module *M, SourceLocation MLoc)
We want to produce a diagnostic at location IncLoc concerning a missing module import.
bool needsCleaning() const
Return true if this token has trigraphs or escaped newlines in it.
Definition: Token.h:283
__DEVICE__ int max(int __a, int __b)
void DiscardUntilEndOfDirective()
Read and discard all tokens remaining on the current line until the tok::eod token is found...
Annotates a diagnostic with some code that should be inserted, removed, or replaced to fix the proble...
Definition: Diagnostic.h:66
StringRef Text
Definition: Format.cpp:1630
void setLocation(SourceLocation L)
Definition: Token.h:132
A trivial tuple used to represent a source range.
void setParameterList(ArrayRef< IdentifierInfo *> List, llvm::BumpPtrAllocator &PPAllocator)
Set the specified list of identifiers as the parameter list for this macro.
Definition: MacroInfo.h:165
PreprocessorLexer * getCurrentFileLexer() const
Return the current file lexer being lexed from.
void EnterAnnotationToken(SourceRange Range, tok::TokenKind Kind, void *AnnotationVal)
Enter an annotation token into the token stream.
SourceLocation getIncludeLoc() const
Return the presumed include location of this location.
bool FoundElse
True if we&#39;ve seen a #else in this block.
Definition: Token.h:327
A header that is known to reside within a given module, whether it was included or excluded...
Definition: ModuleMap.h:149
SourceLocation getBegin() const
bool hasLeadingSpace() const
Return true if this token has whitespace before it.
Definition: Token.h:270
DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID) const
Forwarding function for diagnostics.
VerifyDiagnosticConsumer::Directive Directive
This class handles loading and caching of source files into memory.
void startToken()
Reset all flags to cleared.
Definition: Token.h:169
An RAII class that tracks when the Preprocessor starts and stops lexing the definition of a (ISO C/C+...
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Definition: Preprocessor.h:125