clang  8.0.0
PPDirectives.cpp
Go to the documentation of this file.
1 //===--- PPDirectives.cpp - Directive Handling for Preprocessor -----------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 ///
10 /// \file
11 /// 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  SmallString<128> NormalizedTypoCorrectionPath;
1817  if (LangOpts.MSVCCompat) {
1818  NormalizedTypoCorrectionPath = TypoCorrectionName.str();
1819 #ifndef _WIN32
1820  llvm::sys::path::native(NormalizedTypoCorrectionPath);
1821 #endif
1822  }
1823  File = LookupFile(
1824  FilenameLoc,
1825  LangOpts.MSVCCompat ? NormalizedTypoCorrectionPath.c_str()
1826  : TypoCorrectionName,
1827  isAngled, LookupFrom, LookupFromFile, CurDir,
1828  Callbacks ? &SearchPath : nullptr,
1829  Callbacks ? &RelativePath : nullptr, &SuggestedModule, &IsMapped);
1830  if (File) {
1831  SourceRange Range(FilenameTok.getLocation(), CharEnd);
1832  auto Hint = isAngled
1834  Range, "<" + TypoCorrectionName.str() + ">")
1836  Range, "\"" + TypoCorrectionName.str() + "\"");
1837  Diag(FilenameTok, diag::err_pp_file_not_found_typo_not_fatal)
1838  << OriginalFilename << TypoCorrectionName << Hint;
1839  // We found the file, so set the Filename to the name after typo
1840  // correction.
1841  Filename = TypoCorrectionName;
1842  }
1843  }
1844 
1845  // If the file is still not found, just go with the vanilla diagnostic
1846  if (!File)
1847  Diag(FilenameTok, diag::err_pp_file_not_found) << OriginalFilename
1848  << FilenameRange;
1849  }
1850  }
1851 
1852  if (usingPCHWithThroughHeader() && SkippingUntilPCHThroughHeader) {
1853  if (isPCHThroughHeader(File))
1854  SkippingUntilPCHThroughHeader = false;
1855  return;
1856  }
1857 
1858  // Should we enter the source file? Set to false if either the source file is
1859  // known to have no effect beyond its effect on module visibility -- that is,
1860  // if it's got an include guard that is already defined or is a modular header
1861  // we've imported or already built.
1862  bool ShouldEnter = true;
1863 
1864  if (PPOpts->SingleFileParseMode)
1865  ShouldEnter = false;
1866 
1867  // If we've reached the max allowed include depth, it is usually due to an
1868  // include cycle. Don't enter already processed files again as it can lead to
1869  // reaching the max allowed include depth again.
1870  if (ShouldEnter && HasReachedMaxIncludeDepth && File &&
1871  HeaderInfo.getFileInfo(File).NumIncludes)
1872  ShouldEnter = false;
1873 
1874  // Determine whether we should try to import the module for this #include, if
1875  // there is one. Don't do so if precompiled module support is disabled or we
1876  // are processing this module textually (because we're building the module).
1877  if (ShouldEnter && File && SuggestedModule && getLangOpts().Modules &&
1878  !isForModuleBuilding(SuggestedModule.getModule(),
1880  getLangOpts().ModuleName)) {
1881  // If this include corresponds to a module but that module is
1882  // unavailable, diagnose the situation and bail out.
1883  // FIXME: Remove this; loadModule does the same check (but produces
1884  // slightly worse diagnostics).
1886  SuggestedModule.getModule())) {
1887  Diag(FilenameTok.getLocation(),
1888  diag::note_implicit_top_level_module_import_here)
1889  << SuggestedModule.getModule()->getTopLevelModuleName();
1890  return;
1891  }
1892 
1893  // Compute the module access path corresponding to this module.
1894  // FIXME: Should we have a second loadModule() overload to avoid this
1895  // extra lookup step?
1897  for (Module *Mod = SuggestedModule.getModule(); Mod; Mod = Mod->Parent)
1898  Path.push_back(std::make_pair(getIdentifierInfo(Mod->Name),
1899  FilenameTok.getLocation()));
1900  std::reverse(Path.begin(), Path.end());
1901 
1902  // Warn that we're replacing the include/import with a module import.
1903  // We only do this in Objective-C, where we have a module-import syntax.
1904  if (getLangOpts().ObjC)
1905  diagnoseAutoModuleImport(*this, HashLoc, IncludeTok, Path, CharEnd);
1906 
1907  // Load the module to import its macros. We'll make the declarations
1908  // visible when the parser gets here.
1909  // FIXME: Pass SuggestedModule in here rather than converting it to a path
1910  // and making the module loader convert it back again.
1911  ModuleLoadResult Imported = TheModuleLoader.loadModule(
1912  IncludeTok.getLocation(), Path, Module::Hidden,
1913  /*IsIncludeDirective=*/true);
1914  assert((Imported == nullptr || Imported == SuggestedModule.getModule()) &&
1915  "the imported module is different than the suggested one");
1916 
1917  if (Imported)
1918  ShouldEnter = false;
1919  else if (Imported.isMissingExpected()) {
1920  // We failed to find a submodule that we assumed would exist (because it
1921  // was in the directory of an umbrella header, for instance), but no
1922  // actual module containing it exists (because the umbrella header is
1923  // incomplete). Treat this as a textual inclusion.
1924  SuggestedModule = ModuleMap::KnownHeader();
1925  } else if (Imported.isConfigMismatch()) {
1926  // On a configuration mismatch, enter the header textually. We still know
1927  // that it's part of the corresponding module.
1928  } else {
1929  // We hit an error processing the import. Bail out.
1931  // With a fatal failure in the module loader, we abort parsing.
1932  Token &Result = IncludeTok;
1933  assert(CurLexer && "#include but no current lexer set!");
1934  Result.startToken();
1935  CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd, tok::eof);
1936  CurLexer->cutOffLexing();
1937  }
1938  return;
1939  }
1940  }
1941 
1942  // The #included file will be considered to be a system header if either it is
1943  // in a system include directory, or if the #includer is a system include
1944  // header.
1945  SrcMgr::CharacteristicKind FileCharacter =
1946  SourceMgr.getFileCharacteristic(FilenameTok.getLocation());
1947  if (File)
1948  FileCharacter = std::max(HeaderInfo.getFileDirFlavor(File), FileCharacter);
1949 
1950  // Ask HeaderInfo if we should enter this #include file. If not, #including
1951  // this file will have no effect.
1952  bool SkipHeader = false;
1953  if (ShouldEnter && File &&
1954  !HeaderInfo.ShouldEnterIncludeFile(*this, File, isImport,
1955  getLangOpts().Modules,
1956  SuggestedModule.getModule())) {
1957  ShouldEnter = false;
1958  SkipHeader = true;
1959  }
1960 
1961  if (Callbacks) {
1962  // Notify the callback object that we've seen an inclusion directive.
1963  Callbacks->InclusionDirective(
1964  HashLoc, IncludeTok,
1965  LangOpts.MSVCCompat ? NormalizedPath.c_str() : Filename, isAngled,
1966  FilenameRange, File, SearchPath, RelativePath,
1967  ShouldEnter ? nullptr : SuggestedModule.getModule(), FileCharacter);
1968  if (SkipHeader && !SuggestedModule.getModule())
1969  Callbacks->FileSkipped(*File, FilenameTok, FileCharacter);
1970  }
1971 
1972  if (!File)
1973  return;
1974 
1975  // FIXME: If we have a suggested module, and we've already visited this file,
1976  // don't bother entering it again. We know it has no further effect.
1977 
1978  // Issue a diagnostic if the name of the file on disk has a different case
1979  // than the one we're about to open.
1980  const bool CheckIncludePathPortability =
1981  !IsMapped && File && !File->tryGetRealPathName().empty();
1982 
1983  if (CheckIncludePathPortability) {
1984  StringRef Name = LangOpts.MSVCCompat ? NormalizedPath.str() : Filename;
1985  StringRef RealPathName = File->tryGetRealPathName();
1986  SmallVector<StringRef, 16> Components(llvm::sys::path::begin(Name),
1987  llvm::sys::path::end(Name));
1988 
1989  if (trySimplifyPath(Components, RealPathName)) {
1990  SmallString<128> Path;
1991  Path.reserve(Name.size()+2);
1992  Path.push_back(isAngled ? '<' : '"');
1993  bool isLeadingSeparator = llvm::sys::path::is_absolute(Name);
1994  for (auto Component : Components) {
1995  if (isLeadingSeparator)
1996  isLeadingSeparator = false;
1997  else
1998  Path.append(Component);
1999  // Append the separator the user used, or the close quote
2000  Path.push_back(
2001  Path.size() <= Filename.size() ? Filename[Path.size()-1] :
2002  (isAngled ? '>' : '"'));
2003  }
2004  // For user files and known standard headers, by default we issue a diagnostic.
2005  // For other system headers, we don't. They can be controlled separately.
2006  auto DiagId = (FileCharacter == SrcMgr::C_User || warnByDefaultOnWrongCase(Name)) ?
2007  diag::pp_nonportable_path : diag::pp_nonportable_system_path;
2008  SourceRange Range(FilenameTok.getLocation(), CharEnd);
2009  Diag(FilenameTok, DiagId) << Path <<
2010  FixItHint::CreateReplacement(Range, Path);
2011  }
2012  }
2013 
2014  // If we don't need to enter the file, stop now.
2015  if (!ShouldEnter) {
2016  // If this is a module import, make it visible if needed.
2017  if (auto *M = SuggestedModule.getModule()) {
2018  // When building a pch, -fmodule-name tells the compiler to textually
2019  // include headers in the specified module. But it is possible that
2020  // ShouldEnter is false because we are skipping the header. In that
2021  // case, We are not importing the specified module.
2022  if (SkipHeader && getLangOpts().CompilingPCH &&
2023  isForModuleBuilding(M, getLangOpts().CurrentModule,
2024  getLangOpts().ModuleName))
2025  return;
2026 
2027  makeModuleVisible(M, HashLoc);
2028 
2029  if (IncludeTok.getIdentifierInfo()->getPPKeywordID() !=
2030  tok::pp___include_macros)
2031  EnterAnnotationToken(SourceRange(HashLoc, End),
2032  tok::annot_module_include, M);
2033  }
2034  return;
2035  }
2036 
2037  // Look up the file, create a File ID for it.
2038  SourceLocation IncludePos = End;
2039  // If the filename string was the result of macro expansions, set the include
2040  // position on the file where it will be included and after the expansions.
2041  if (IncludePos.isMacroID())
2042  IncludePos = SourceMgr.getExpansionRange(IncludePos).getEnd();
2043  FileID FID = SourceMgr.createFileID(File, IncludePos, FileCharacter);
2044  assert(FID.isValid() && "Expected valid file ID");
2045 
2046  // If all is good, enter the new file!
2047  if (EnterSourceFile(FID, CurDir, FilenameTok.getLocation()))
2048  return;
2049 
2050  // Determine if we're switching to building a new submodule, and which one.
2051  if (auto *M = SuggestedModule.getModule()) {
2052  if (M->getTopLevelModule()->ShadowingModule) {
2053  // We are building a submodule that belongs to a shadowed module. This
2054  // means we find header files in the shadowed module.
2055  Diag(M->DefinitionLoc, diag::err_module_build_shadowed_submodule)
2056  << M->getFullModuleName();
2057  Diag(M->getTopLevelModule()->ShadowingModule->DefinitionLoc,
2058  diag::note_previous_definition);
2059  return;
2060  }
2061  // When building a pch, -fmodule-name tells the compiler to textually
2062  // include headers in the specified module. We are not building the
2063  // specified module.
2064  if (getLangOpts().CompilingPCH &&
2065  isForModuleBuilding(M, getLangOpts().CurrentModule,
2066  getLangOpts().ModuleName))
2067  return;
2068 
2069  assert(!CurLexerSubmodule && "should not have marked this as a module yet");
2070  CurLexerSubmodule = M;
2071 
2072  // Let the macro handling code know that any future macros are within
2073  // the new submodule.
2074  EnterSubmodule(M, HashLoc, /*ForPragma*/false);
2075 
2076  // Let the parser know that any future declarations are within the new
2077  // submodule.
2078  // FIXME: There's no point doing this if we're handling a #__include_macros
2079  // directive.
2080  EnterAnnotationToken(SourceRange(HashLoc, End), tok::annot_module_begin, M);
2081  }
2082 }
2083 
2084 /// HandleIncludeNextDirective - Implements \#include_next.
2085 ///
2086 void Preprocessor::HandleIncludeNextDirective(SourceLocation HashLoc,
2087  Token &IncludeNextTok) {
2088  Diag(IncludeNextTok, diag::ext_pp_include_next_directive);
2089 
2090  // #include_next is like #include, except that we start searching after
2091  // the current found directory. If we can't do this, issue a
2092  // diagnostic.
2093  const DirectoryLookup *Lookup = CurDirLookup;
2094  const FileEntry *LookupFromFile = nullptr;
2095  if (isInPrimaryFile() && LangOpts.IsHeaderFile) {
2096  // If the main file is a header, then it's either for PCH/AST generation,
2097  // or libclang opened it. Either way, handle it as a normal include below
2098  // and do not complain about include_next.
2099  } else if (isInPrimaryFile()) {
2100  Lookup = nullptr;
2101  Diag(IncludeNextTok, diag::pp_include_next_in_primary);
2102  } else if (CurLexerSubmodule) {
2103  // Start looking up in the directory *after* the one in which the current
2104  // file would be found, if any.
2105  assert(CurPPLexer && "#include_next directive in macro?");
2106  LookupFromFile = CurPPLexer->getFileEntry();
2107  Lookup = nullptr;
2108  } else if (!Lookup) {
2109  Diag(IncludeNextTok, diag::pp_include_next_absolute_path);
2110  } else {
2111  // Start looking up in the next directory.
2112  ++Lookup;
2113  }
2114 
2115  return HandleIncludeDirective(HashLoc, IncludeNextTok, Lookup,
2116  LookupFromFile);
2117 }
2118 
2119 /// HandleMicrosoftImportDirective - Implements \#import for Microsoft Mode
2120 void Preprocessor::HandleMicrosoftImportDirective(Token &Tok) {
2121  // The Microsoft #import directive takes a type library and generates header
2122  // files from it, and includes those. This is beyond the scope of what clang
2123  // does, so we ignore it and error out. However, #import can optionally have
2124  // trailing attributes that span multiple lines. We're going to eat those
2125  // so we can continue processing from there.
2126  Diag(Tok, diag::err_pp_import_directive_ms );
2127 
2128  // Read tokens until we get to the end of the directive. Note that the
2129  // directive can be split over multiple lines using the backslash character.
2131 }
2132 
2133 /// HandleImportDirective - Implements \#import.
2134 ///
2135 void Preprocessor::HandleImportDirective(SourceLocation HashLoc,
2136  Token &ImportTok) {
2137  if (!LangOpts.ObjC) { // #import is standard for ObjC.
2138  if (LangOpts.MSVCCompat)
2139  return HandleMicrosoftImportDirective(ImportTok);
2140  Diag(ImportTok, diag::ext_pp_import_directive);
2141  }
2142  return HandleIncludeDirective(HashLoc, ImportTok, nullptr, nullptr, true);
2143 }
2144 
2145 /// HandleIncludeMacrosDirective - The -imacros command line option turns into a
2146 /// pseudo directive in the predefines buffer. This handles it by sucking all
2147 /// tokens through the preprocessor and discarding them (only keeping the side
2148 /// effects on the preprocessor).
2149 void Preprocessor::HandleIncludeMacrosDirective(SourceLocation HashLoc,
2150  Token &IncludeMacrosTok) {
2151  // This directive should only occur in the predefines buffer. If not, emit an
2152  // error and reject it.
2153  SourceLocation Loc = IncludeMacrosTok.getLocation();
2154  if (SourceMgr.getBufferName(Loc) != "<built-in>") {
2155  Diag(IncludeMacrosTok.getLocation(),
2156  diag::pp_include_macros_out_of_predefines);
2158  return;
2159  }
2160 
2161  // Treat this as a normal #include for checking purposes. If this is
2162  // successful, it will push a new lexer onto the include stack.
2163  HandleIncludeDirective(HashLoc, IncludeMacrosTok);
2164 
2165  Token TmpTok;
2166  do {
2167  Lex(TmpTok);
2168  assert(TmpTok.isNot(tok::eof) && "Didn't find end of -imacros!");
2169  } while (TmpTok.isNot(tok::hashhash));
2170 }
2171 
2172 //===----------------------------------------------------------------------===//
2173 // Preprocessor Macro Directive Handling.
2174 //===----------------------------------------------------------------------===//
2175 
2176 /// ReadMacroParameterList - The ( starting a parameter list of a macro
2177 /// definition has just been read. Lex the rest of the parameters and the
2178 /// closing ), updating MI with what we learn. Return true if an error occurs
2179 /// parsing the param list.
2180 bool Preprocessor::ReadMacroParameterList(MacroInfo *MI, Token &Tok) {
2182 
2183  while (true) {
2184  LexUnexpandedToken(Tok);
2185  switch (Tok.getKind()) {
2186  case tok::r_paren:
2187  // Found the end of the parameter list.
2188  if (Parameters.empty()) // #define FOO()
2189  return false;
2190  // Otherwise we have #define FOO(A,)
2191  Diag(Tok, diag::err_pp_expected_ident_in_arg_list);
2192  return true;
2193  case tok::ellipsis: // #define X(... -> C99 varargs
2194  if (!LangOpts.C99)
2195  Diag(Tok, LangOpts.CPlusPlus11 ?
2196  diag::warn_cxx98_compat_variadic_macro :
2197  diag::ext_variadic_macro);
2198 
2199  // OpenCL v1.2 s6.9.e: variadic macros are not supported.
2200  if (LangOpts.OpenCL) {
2201  Diag(Tok, diag::err_pp_opencl_variadic_macros);
2202  return true;
2203  }
2204 
2205  // Lex the token after the identifier.
2206  LexUnexpandedToken(Tok);
2207  if (Tok.isNot(tok::r_paren)) {
2208  Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
2209  return true;
2210  }
2211  // Add the __VA_ARGS__ identifier as a parameter.
2212  Parameters.push_back(Ident__VA_ARGS__);
2213  MI->setIsC99Varargs();
2214  MI->setParameterList(Parameters, BP);
2215  return false;
2216  case tok::eod: // #define X(
2217  Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
2218  return true;
2219  default:
2220  // Handle keywords and identifiers here to accept things like
2221  // #define Foo(for) for.
2222  IdentifierInfo *II = Tok.getIdentifierInfo();
2223  if (!II) {
2224  // #define X(1
2225  Diag(Tok, diag::err_pp_invalid_tok_in_arg_list);
2226  return true;
2227  }
2228 
2229  // If this is already used as a parameter, it is used multiple times (e.g.
2230  // #define X(A,A.
2231  if (std::find(Parameters.begin(), Parameters.end(), II) !=
2232  Parameters.end()) { // C99 6.10.3p6
2233  Diag(Tok, diag::err_pp_duplicate_name_in_arg_list) << II;
2234  return true;
2235  }
2236 
2237  // Add the parameter to the macro info.
2238  Parameters.push_back(II);
2239 
2240  // Lex the token after the identifier.
2241  LexUnexpandedToken(Tok);
2242 
2243  switch (Tok.getKind()) {
2244  default: // #define X(A B
2245  Diag(Tok, diag::err_pp_expected_comma_in_arg_list);
2246  return true;
2247  case tok::r_paren: // #define X(A)
2248  MI->setParameterList(Parameters, BP);
2249  return false;
2250  case tok::comma: // #define X(A,
2251  break;
2252  case tok::ellipsis: // #define X(A... -> GCC extension
2253  // Diagnose extension.
2254  Diag(Tok, diag::ext_named_variadic_macro);
2255 
2256  // Lex the token after the identifier.
2257  LexUnexpandedToken(Tok);
2258  if (Tok.isNot(tok::r_paren)) {
2259  Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
2260  return true;
2261  }
2262 
2263  MI->setIsGNUVarargs();
2264  MI->setParameterList(Parameters, BP);
2265  return false;
2266  }
2267  }
2268  }
2269 }
2270 
2271 static bool isConfigurationPattern(Token &MacroName, MacroInfo *MI,
2272  const LangOptions &LOptions) {
2273  if (MI->getNumTokens() == 1) {
2274  const Token &Value = MI->getReplacementToken(0);
2275 
2276  // Macro that is identity, like '#define inline inline' is a valid pattern.
2277  if (MacroName.getKind() == Value.getKind())
2278  return true;
2279 
2280  // Macro that maps a keyword to the same keyword decorated with leading/
2281  // trailing underscores is a valid pattern:
2282  // #define inline __inline
2283  // #define inline __inline__
2284  // #define inline _inline (in MS compatibility mode)
2285  StringRef MacroText = MacroName.getIdentifierInfo()->getName();
2286  if (IdentifierInfo *II = Value.getIdentifierInfo()) {
2287  if (!II->isKeyword(LOptions))
2288  return false;
2289  StringRef ValueText = II->getName();
2290  StringRef TrimmedValue = ValueText;
2291  if (!ValueText.startswith("__")) {
2292  if (ValueText.startswith("_"))
2293  TrimmedValue = TrimmedValue.drop_front(1);
2294  else
2295  return false;
2296  } else {
2297  TrimmedValue = TrimmedValue.drop_front(2);
2298  if (TrimmedValue.endswith("__"))
2299  TrimmedValue = TrimmedValue.drop_back(2);
2300  }
2301  return TrimmedValue.equals(MacroText);
2302  } else {
2303  return false;
2304  }
2305  }
2306 
2307  // #define inline
2308  return MacroName.isOneOf(tok::kw_extern, tok::kw_inline, tok::kw_static,
2309  tok::kw_const) &&
2310  MI->getNumTokens() == 0;
2311 }
2312 
2313 // ReadOptionalMacroParameterListAndBody - This consumes all (i.e. the
2314 // entire line) of the macro's tokens and adds them to MacroInfo, and while
2315 // doing so performs certain validity checks including (but not limited to):
2316 // - # (stringization) is followed by a macro parameter
2317 //
2318 // Returns a nullptr if an invalid sequence of tokens is encountered or returns
2319 // a pointer to a MacroInfo object.
2320 
2321 MacroInfo *Preprocessor::ReadOptionalMacroParameterListAndBody(
2322  const Token &MacroNameTok, const bool ImmediatelyAfterHeaderGuard) {
2323 
2324  Token LastTok = MacroNameTok;
2325  // Create the new macro.
2326  MacroInfo *const MI = AllocateMacroInfo(MacroNameTok.getLocation());
2327 
2328  Token Tok;
2329  LexUnexpandedToken(Tok);
2330 
2331  // Used to un-poison and then re-poison identifiers of the __VA_ARGS__ ilk
2332  // within their appropriate context.
2334 
2335  // If this is a function-like macro definition, parse the argument list,
2336  // marking each of the identifiers as being used as macro arguments. Also,
2337  // check other constraints on the first token of the macro body.
2338  if (Tok.is(tok::eod)) {
2339  if (ImmediatelyAfterHeaderGuard) {
2340  // Save this macro information since it may part of a header guard.
2341  CurPPLexer->MIOpt.SetDefinedMacro(MacroNameTok.getIdentifierInfo(),
2342  MacroNameTok.getLocation());
2343  }
2344  // If there is no body to this macro, we have no special handling here.
2345  } else if (Tok.hasLeadingSpace()) {
2346  // This is a normal token with leading space. Clear the leading space
2347  // marker on the first token to get proper expansion.
2348  Tok.clearFlag(Token::LeadingSpace);
2349  } else if (Tok.is(tok::l_paren)) {
2350  // This is a function-like macro definition. Read the argument list.
2351  MI->setIsFunctionLike();
2352  if (ReadMacroParameterList(MI, LastTok)) {
2353  // Throw away the rest of the line.
2354  if (CurPPLexer->ParsingPreprocessorDirective)
2356  return nullptr;
2357  }
2358 
2359  // If this is a definition of an ISO C/C++ variadic function-like macro (not
2360  // using the GNU named varargs extension) inform our variadic scope guard
2361  // which un-poisons and re-poisons certain identifiers (e.g. __VA_ARGS__)
2362  // allowed only within the definition of a variadic macro.
2363 
2364  if (MI->isC99Varargs()) {
2365  VariadicMacroScopeGuard.enterScope();
2366  }
2367 
2368  // Read the first token after the arg list for down below.
2369  LexUnexpandedToken(Tok);
2370  } else if (LangOpts.C99 || LangOpts.CPlusPlus11) {
2371  // C99 requires whitespace between the macro definition and the body. Emit
2372  // a diagnostic for something like "#define X+".
2373  Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name);
2374  } else {
2375  // C90 6.8 TC1 says: "In the definition of an object-like macro, if the
2376  // first character of a replacement list is not a character required by
2377  // subclause 5.2.1, then there shall be white-space separation between the
2378  // identifier and the replacement list.". 5.2.1 lists this set:
2379  // "A-Za-z0-9!"#%&'()*+,_./:;<=>?[\]^_{|}~" as well as whitespace, which
2380  // is irrelevant here.
2381  bool isInvalid = false;
2382  if (Tok.is(tok::at)) // @ is not in the list above.
2383  isInvalid = true;
2384  else if (Tok.is(tok::unknown)) {
2385  // If we have an unknown token, it is something strange like "`". Since
2386  // all of valid characters would have lexed into a single character
2387  // token of some sort, we know this is not a valid case.
2388  isInvalid = true;
2389  }
2390  if (isInvalid)
2391  Diag(Tok, diag::ext_missing_whitespace_after_macro_name);
2392  else
2393  Diag(Tok, diag::warn_missing_whitespace_after_macro_name);
2394  }
2395 
2396  if (!Tok.is(tok::eod))
2397  LastTok = Tok;
2398 
2399  // Read the rest of the macro body.
2400  if (MI->isObjectLike()) {
2401  // Object-like macros are very simple, just read their body.
2402  while (Tok.isNot(tok::eod)) {
2403  LastTok = Tok;
2404  MI->AddTokenToBody(Tok);
2405  // Get the next token of the macro.
2406  LexUnexpandedToken(Tok);
2407  }
2408  } else {
2409  // Otherwise, read the body of a function-like macro. While we are at it,
2410  // check C99 6.10.3.2p1: ensure that # operators are followed by macro
2411  // parameters in function-like macro expansions.
2412 
2413  VAOptDefinitionContext VAOCtx(*this);
2414 
2415  while (Tok.isNot(tok::eod)) {
2416  LastTok = Tok;
2417 
2418  if (!Tok.isOneOf(tok::hash, tok::hashat, tok::hashhash)) {
2419  MI->AddTokenToBody(Tok);
2420 
2421  if (VAOCtx.isVAOptToken(Tok)) {
2422  // If we're already within a VAOPT, emit an error.
2423  if (VAOCtx.isInVAOpt()) {
2424  Diag(Tok, diag::err_pp_vaopt_nested_use);
2425  return nullptr;
2426  }
2427  // Ensure VAOPT is followed by a '(' .
2428  LexUnexpandedToken(Tok);
2429  if (Tok.isNot(tok::l_paren)) {
2430  Diag(Tok, diag::err_pp_missing_lparen_in_vaopt_use);
2431  return nullptr;
2432  }
2433  MI->AddTokenToBody(Tok);
2434  VAOCtx.sawVAOptFollowedByOpeningParens(Tok.getLocation());
2435  LexUnexpandedToken(Tok);
2436  if (Tok.is(tok::hashhash)) {
2437  Diag(Tok, diag::err_vaopt_paste_at_start);
2438  return nullptr;
2439  }
2440  continue;
2441  } else if (VAOCtx.isInVAOpt()) {
2442  if (Tok.is(tok::r_paren)) {
2443  if (VAOCtx.sawClosingParen()) {
2444  const unsigned NumTokens = MI->getNumTokens();
2445  assert(NumTokens >= 3 && "Must have seen at least __VA_OPT__( "
2446  "and a subsequent tok::r_paren");
2447  if (MI->getReplacementToken(NumTokens - 2).is(tok::hashhash)) {
2448  Diag(Tok, diag::err_vaopt_paste_at_end);
2449  return nullptr;
2450  }
2451  }
2452  } else if (Tok.is(tok::l_paren)) {
2453  VAOCtx.sawOpeningParen(Tok.getLocation());
2454  }
2455  }
2456  // Get the next token of the macro.
2457  LexUnexpandedToken(Tok);
2458  continue;
2459  }
2460 
2461  // If we're in -traditional mode, then we should ignore stringification
2462  // and token pasting. Mark the tokens as unknown so as not to confuse
2463  // things.
2464  if (getLangOpts().TraditionalCPP) {
2465  Tok.setKind(tok::unknown);
2466  MI->AddTokenToBody(Tok);
2467 
2468  // Get the next token of the macro.
2469  LexUnexpandedToken(Tok);
2470  continue;
2471  }
2472 
2473  if (Tok.is(tok::hashhash)) {
2474  // If we see token pasting, check if it looks like the gcc comma
2475  // pasting extension. We'll use this information to suppress
2476  // diagnostics later on.
2477 
2478  // Get the next token of the macro.
2479  LexUnexpandedToken(Tok);
2480 
2481  if (Tok.is(tok::eod)) {
2482  MI->AddTokenToBody(LastTok);
2483  break;
2484  }
2485 
2486  unsigned NumTokens = MI->getNumTokens();
2487  if (NumTokens && Tok.getIdentifierInfo() == Ident__VA_ARGS__ &&
2488  MI->getReplacementToken(NumTokens-1).is(tok::comma))
2489  MI->setHasCommaPasting();
2490 
2491  // Things look ok, add the '##' token to the macro.
2492  MI->AddTokenToBody(LastTok);
2493  continue;
2494  }
2495 
2496  // Our Token is a stringization operator.
2497  // Get the next token of the macro.
2498  LexUnexpandedToken(Tok);
2499 
2500  // Check for a valid macro arg identifier or __VA_OPT__.
2501  if (!VAOCtx.isVAOptToken(Tok) &&
2502  (Tok.getIdentifierInfo() == nullptr ||
2503  MI->getParameterNum(Tok.getIdentifierInfo()) == -1)) {
2504 
2505  // If this is assembler-with-cpp mode, we accept random gibberish after
2506  // the '#' because '#' is often a comment character. However, change
2507  // the kind of the token to tok::unknown so that the preprocessor isn't
2508  // confused.
2509  if (getLangOpts().AsmPreprocessor && Tok.isNot(tok::eod)) {
2510  LastTok.setKind(tok::unknown);
2511  MI->AddTokenToBody(LastTok);
2512  continue;
2513  } else {
2514  Diag(Tok, diag::err_pp_stringize_not_parameter)
2515  << LastTok.is(tok::hashat);
2516  return nullptr;
2517  }
2518  }
2519 
2520  // Things look ok, add the '#' and param name tokens to the macro.
2521  MI->AddTokenToBody(LastTok);
2522 
2523  // If the token following '#' is VAOPT, let the next iteration handle it
2524  // and check it for correctness, otherwise add the token and prime the
2525  // loop with the next one.
2526  if (!VAOCtx.isVAOptToken(Tok)) {
2527  MI->AddTokenToBody(Tok);
2528  LastTok = Tok;
2529 
2530  // Get the next token of the macro.
2531  LexUnexpandedToken(Tok);
2532  }
2533  }
2534  if (VAOCtx.isInVAOpt()) {
2535  assert(Tok.is(tok::eod) && "Must be at End Of preprocessing Directive");
2536  Diag(Tok, diag::err_pp_expected_after)
2537  << LastTok.getKind() << tok::r_paren;
2538  Diag(VAOCtx.getUnmatchedOpeningParenLoc(), diag::note_matching) << tok::l_paren;
2539  return nullptr;
2540  }
2541  }
2542  MI->setDefinitionEndLoc(LastTok.getLocation());
2543  return MI;
2544 }
2545 /// HandleDefineDirective - Implements \#define. This consumes the entire macro
2546 /// line then lets the caller lex the next real token.
2547 void Preprocessor::HandleDefineDirective(
2548  Token &DefineTok, const bool ImmediatelyAfterHeaderGuard) {
2549  ++NumDefined;
2550 
2551  Token MacroNameTok;
2552  bool MacroShadowsKeyword;
2553  ReadMacroName(MacroNameTok, MU_Define, &MacroShadowsKeyword);
2554 
2555  // Error reading macro name? If so, diagnostic already issued.
2556  if (MacroNameTok.is(tok::eod))
2557  return;
2558 
2559  // If we are supposed to keep comments in #defines, reenable comment saving
2560  // mode.
2561  if (CurLexer) CurLexer->SetCommentRetentionState(KeepMacroComments);
2562 
2563  MacroInfo *const MI = ReadOptionalMacroParameterListAndBody(
2564  MacroNameTok, ImmediatelyAfterHeaderGuard);
2565 
2566  if (!MI) return;
2567 
2568  if (MacroShadowsKeyword &&
2569  !isConfigurationPattern(MacroNameTok, MI, getLangOpts())) {
2570  Diag(MacroNameTok, diag::warn_pp_macro_hides_keyword);
2571  }
2572  // Check that there is no paste (##) operator at the beginning or end of the
2573  // replacement list.
2574  unsigned NumTokens = MI->getNumTokens();
2575  if (NumTokens != 0) {
2576  if (MI->getReplacementToken(0).is(tok::hashhash)) {
2577  Diag(MI->getReplacementToken(0), diag::err_paste_at_start);
2578  return;
2579  }
2580  if (MI->getReplacementToken(NumTokens-1).is(tok::hashhash)) {
2581  Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end);
2582  return;
2583  }
2584  }
2585 
2586  // When skipping just warn about macros that do not match.
2587  if (SkippingUntilPCHThroughHeader) {
2588  const MacroInfo *OtherMI = getMacroInfo(MacroNameTok.getIdentifierInfo());
2589  if (!OtherMI || !MI->isIdenticalTo(*OtherMI, *this,
2590  /*Syntactic=*/LangOpts.MicrosoftExt))
2591  Diag(MI->getDefinitionLoc(), diag::warn_pp_macro_def_mismatch_with_pch)
2592  << MacroNameTok.getIdentifierInfo();
2593  return;
2594  }
2595 
2596  // Finally, if this identifier already had a macro defined for it, verify that
2597  // the macro bodies are identical, and issue diagnostics if they are not.
2598  if (const MacroInfo *OtherMI=getMacroInfo(MacroNameTok.getIdentifierInfo())) {
2599  // In Objective-C, ignore attempts to directly redefine the builtin
2600  // definitions of the ownership qualifiers. It's still possible to
2601  // #undef them.
2602  auto isObjCProtectedMacro = [](const IdentifierInfo *II) -> bool {
2603  return II->isStr("__strong") ||
2604  II->isStr("__weak") ||
2605  II->isStr("__unsafe_unretained") ||
2606  II->isStr("__autoreleasing");
2607  };
2608  if (getLangOpts().ObjC &&
2609  SourceMgr.getFileID(OtherMI->getDefinitionLoc())
2610  == getPredefinesFileID() &&
2611  isObjCProtectedMacro(MacroNameTok.getIdentifierInfo())) {
2612  // Warn if it changes the tokens.
2614  !SourceMgr.isInSystemHeader(DefineTok.getLocation())) &&
2615  !MI->isIdenticalTo(*OtherMI, *this,
2616  /*Syntactic=*/LangOpts.MicrosoftExt)) {
2617  Diag(MI->getDefinitionLoc(), diag::warn_pp_objc_macro_redef_ignored);
2618  }
2619  assert(!OtherMI->isWarnIfUnused());
2620  return;
2621  }
2622 
2623  // It is very common for system headers to have tons of macro redefinitions
2624  // and for warnings to be disabled in system headers. If this is the case,
2625  // then don't bother calling MacroInfo::isIdenticalTo.
2627  !SourceMgr.isInSystemHeader(DefineTok.getLocation())) {
2628  if (!OtherMI->isUsed() && OtherMI->isWarnIfUnused())
2629  Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used);
2630 
2631  // Warn if defining "__LINE__" and other builtins, per C99 6.10.8/4 and
2632  // C++ [cpp.predefined]p4, but allow it as an extension.
2633  if (OtherMI->isBuiltinMacro())
2634  Diag(MacroNameTok, diag::ext_pp_redef_builtin_macro);
2635  // Macros must be identical. This means all tokens and whitespace
2636  // separation must be the same. C99 6.10.3p2.
2637  else if (!OtherMI->isAllowRedefinitionsWithoutWarning() &&
2638  !MI->isIdenticalTo(*OtherMI, *this, /*Syntactic=*/LangOpts.MicrosoftExt)) {
2639  Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef)
2640  << MacroNameTok.getIdentifierInfo();
2641  Diag(OtherMI->getDefinitionLoc(), diag::note_previous_definition);
2642  }
2643  }
2644  if (OtherMI->isWarnIfUnused())
2645  WarnUnusedMacroLocs.erase(OtherMI->getDefinitionLoc());
2646  }
2647 
2648  DefMacroDirective *MD =
2649  appendDefMacroDirective(MacroNameTok.getIdentifierInfo(), MI);
2650 
2651  assert(!MI->isUsed());
2652  // If we need warning for not using the macro, add its location in the
2653  // warn-because-unused-macro set. If it gets used it will be removed from set.
2655  !Diags->isIgnored(diag::pp_macro_not_used, MI->getDefinitionLoc())) {
2656  MI->setIsWarnIfUnused(true);
2657  WarnUnusedMacroLocs.insert(MI->getDefinitionLoc());
2658  }
2659 
2660  // If the callbacks want to know, tell them about the macro definition.
2661  if (Callbacks)
2662  Callbacks->MacroDefined(MacroNameTok, MD);
2663 }
2664 
2665 /// HandleUndefDirective - Implements \#undef.
2666 ///
2667 void Preprocessor::HandleUndefDirective() {
2668  ++NumUndefined;
2669 
2670  Token MacroNameTok;
2671  ReadMacroName(MacroNameTok, MU_Undef);
2672 
2673  // Error reading macro name? If so, diagnostic already issued.
2674  if (MacroNameTok.is(tok::eod))
2675  return;
2676 
2677  // Check to see if this is the last token on the #undef line.
2678  CheckEndOfDirective("undef");
2679 
2680  // Okay, we have a valid identifier to undef.
2681  auto *II = MacroNameTok.getIdentifierInfo();
2682  auto MD = getMacroDefinition(II);
2683  UndefMacroDirective *Undef = nullptr;
2684 
2685  // If the macro is not defined, this is a noop undef.
2686  if (const MacroInfo *MI = MD.getMacroInfo()) {
2687  if (!MI->isUsed() && MI->isWarnIfUnused())
2688  Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used);
2689 
2690  if (MI->isWarnIfUnused())
2691  WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());
2692 
2693  Undef = AllocateUndefMacroDirective(MacroNameTok.getLocation());
2694  }
2695 
2696  // If the callbacks want to know, tell them about the macro #undef.
2697  // Note: no matter if the macro was defined or not.
2698  if (Callbacks)
2699  Callbacks->MacroUndefined(MacroNameTok, MD, Undef);
2700 
2701  if (Undef)
2702  appendMacroDirective(II, Undef);
2703 }
2704 
2705 //===----------------------------------------------------------------------===//
2706 // Preprocessor Conditional Directive Handling.
2707 //===----------------------------------------------------------------------===//
2708 
2709 /// HandleIfdefDirective - Implements the \#ifdef/\#ifndef directive. isIfndef
2710 /// is true when this is a \#ifndef directive. ReadAnyTokensBeforeDirective is
2711 /// true if any tokens have been returned or pp-directives activated before this
2712 /// \#ifndef has been lexed.
2713 ///
2714 void Preprocessor::HandleIfdefDirective(Token &Result,
2715  const Token &HashToken,
2716  bool isIfndef,
2717  bool ReadAnyTokensBeforeDirective) {
2718  ++NumIf;
2719  Token DirectiveTok = Result;
2720 
2721  Token MacroNameTok;
2722  ReadMacroName(MacroNameTok);
2723 
2724  // Error reading macro name? If so, diagnostic already issued.
2725  if (MacroNameTok.is(tok::eod)) {
2726  // Skip code until we get to #endif. This helps with recovery by not
2727  // emitting an error when the #endif is reached.
2728  SkipExcludedConditionalBlock(HashToken.getLocation(),
2729  DirectiveTok.getLocation(),
2730  /*Foundnonskip*/ false, /*FoundElse*/ false);
2731  return;
2732  }
2733 
2734  // Check to see if this is the last token on the #if[n]def line.
2735  CheckEndOfDirective(isIfndef ? "ifndef" : "ifdef");
2736 
2737  IdentifierInfo *MII = MacroNameTok.getIdentifierInfo();
2738  auto MD = getMacroDefinition(MII);
2739  MacroInfo *MI = MD.getMacroInfo();
2740 
2741  if (CurPPLexer->getConditionalStackDepth() == 0) {
2742  // If the start of a top-level #ifdef and if the macro is not defined,
2743  // inform MIOpt that this might be the start of a proper include guard.
2744  // Otherwise it is some other form of unknown conditional which we can't
2745  // handle.
2746  if (!ReadAnyTokensBeforeDirective && !MI) {
2747  assert(isIfndef && "#ifdef shouldn't reach here");
2748  CurPPLexer->MIOpt.EnterTopLevelIfndef(MII, MacroNameTok.getLocation());
2749  } else
2750  CurPPLexer->MIOpt.EnterTopLevelConditional();
2751  }
2752 
2753  // If there is a macro, process it.
2754  if (MI) // Mark it used.
2755  markMacroAsUsed(MI);
2756 
2757  if (Callbacks) {
2758  if (isIfndef)
2759  Callbacks->Ifndef(DirectiveTok.getLocation(), MacroNameTok, MD);
2760  else
2761  Callbacks->Ifdef(DirectiveTok.getLocation(), MacroNameTok, MD);
2762  }
2763 
2764  // Should we include the stuff contained by this directive?
2765  if (PPOpts->SingleFileParseMode && !MI) {
2766  // In 'single-file-parse mode' undefined identifiers trigger parsing of all
2767  // the directive blocks.
2768  CurPPLexer->pushConditionalLevel(DirectiveTok.getLocation(),
2769  /*wasskip*/false, /*foundnonskip*/false,
2770  /*foundelse*/false);
2771  } else if (!MI == isIfndef) {
2772  // Yes, remember that we are inside a conditional, then lex the next token.
2773  CurPPLexer->pushConditionalLevel(DirectiveTok.getLocation(),
2774  /*wasskip*/false, /*foundnonskip*/true,
2775  /*foundelse*/false);
2776  } else {
2777  // No, skip the contents of this block.
2778  SkipExcludedConditionalBlock(HashToken.getLocation(),
2779  DirectiveTok.getLocation(),
2780  /*Foundnonskip*/ false,
2781  /*FoundElse*/ false);
2782  }
2783 }
2784 
2785 /// HandleIfDirective - Implements the \#if directive.
2786 ///
2787 void Preprocessor::HandleIfDirective(Token &IfToken,
2788  const Token &HashToken,
2789  bool ReadAnyTokensBeforeDirective) {
2790  ++NumIf;
2791 
2792  // Parse and evaluate the conditional expression.
2793  IdentifierInfo *IfNDefMacro = nullptr;
2794  const SourceLocation ConditionalBegin = CurPPLexer->getSourceLocation();
2795  const DirectiveEvalResult DER = EvaluateDirectiveExpression(IfNDefMacro);
2796  const bool ConditionalTrue = DER.Conditional;
2797  const SourceLocation ConditionalEnd = CurPPLexer->getSourceLocation();
2798 
2799  // If this condition is equivalent to #ifndef X, and if this is the first
2800  // directive seen, handle it for the multiple-include optimization.
2801  if (CurPPLexer->getConditionalStackDepth() == 0) {
2802  if (!ReadAnyTokensBeforeDirective && IfNDefMacro && ConditionalTrue)
2803  // FIXME: Pass in the location of the macro name, not the 'if' token.
2804  CurPPLexer->MIOpt.EnterTopLevelIfndef(IfNDefMacro, IfToken.getLocation());
2805  else
2806  CurPPLexer->MIOpt.EnterTopLevelConditional();
2807  }
2808 
2809  if (Callbacks)
2810  Callbacks->If(IfToken.getLocation(),
2811  SourceRange(ConditionalBegin, ConditionalEnd),
2812  (ConditionalTrue ? PPCallbacks::CVK_True : PPCallbacks::CVK_False));
2813 
2814  // Should we include the stuff contained by this directive?
2815  if (PPOpts->SingleFileParseMode && DER.IncludedUndefinedIds) {
2816  // In 'single-file-parse mode' undefined identifiers trigger parsing of all
2817  // the directive blocks.
2818  CurPPLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
2819  /*foundnonskip*/false, /*foundelse*/false);
2820  } else if (ConditionalTrue) {
2821  // Yes, remember that we are inside a conditional, then lex the next token.
2822  CurPPLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
2823  /*foundnonskip*/true, /*foundelse*/false);
2824  } else {
2825  // No, skip the contents of this block.
2826  SkipExcludedConditionalBlock(HashToken.getLocation(), IfToken.getLocation(),
2827  /*Foundnonskip*/ false,
2828  /*FoundElse*/ false);
2829  }
2830 }
2831 
2832 /// HandleEndifDirective - Implements the \#endif directive.
2833 ///
2834 void Preprocessor::HandleEndifDirective(Token &EndifToken) {
2835  ++NumEndif;
2836 
2837  // Check that this is the whole directive.
2838  CheckEndOfDirective("endif");
2839 
2840  PPConditionalInfo CondInfo;
2841  if (CurPPLexer->popConditionalLevel(CondInfo)) {
2842  // No conditionals on the stack: this is an #endif without an #if.
2843  Diag(EndifToken, diag::err_pp_endif_without_if);
2844  return;
2845  }
2846 
2847  // If this the end of a top-level #endif, inform MIOpt.
2848  if (CurPPLexer->getConditionalStackDepth() == 0)
2849  CurPPLexer->MIOpt.ExitTopLevelConditional();
2850 
2851  assert(!CondInfo.WasSkipping && !CurPPLexer->LexingRawMode &&
2852  "This code should only be reachable in the non-skipping case!");
2853 
2854  if (Callbacks)
2855  Callbacks->Endif(EndifToken.getLocation(), CondInfo.IfLoc);
2856 }
2857 
2858 /// HandleElseDirective - Implements the \#else directive.
2859 ///
2860 void Preprocessor::HandleElseDirective(Token &Result, const Token &HashToken) {
2861  ++NumElse;
2862 
2863  // #else directive in a non-skipping conditional... start skipping.
2864  CheckEndOfDirective("else");
2865 
2866  PPConditionalInfo CI;
2867  if (CurPPLexer->popConditionalLevel(CI)) {
2868  Diag(Result, diag::pp_err_else_without_if);
2869  return;
2870  }
2871 
2872  // If this is a top-level #else, inform the MIOpt.
2873  if (CurPPLexer->getConditionalStackDepth() == 0)
2874  CurPPLexer->MIOpt.EnterTopLevelConditional();
2875 
2876  // If this is a #else with a #else before it, report the error.
2877  if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else);
2878 
2879  if (Callbacks)
2880  Callbacks->Else(Result.getLocation(), CI.IfLoc);
2881 
2882  if (PPOpts->SingleFileParseMode && !CI.FoundNonSkip) {
2883  // In 'single-file-parse mode' undefined identifiers trigger parsing of all
2884  // the directive blocks.
2885  CurPPLexer->pushConditionalLevel(CI.IfLoc, /*wasskip*/false,
2886  /*foundnonskip*/false, /*foundelse*/true);
2887  return;
2888  }
2889 
2890  // Finally, skip the rest of the contents of this block.
2891  SkipExcludedConditionalBlock(HashToken.getLocation(), CI.IfLoc,
2892  /*Foundnonskip*/ true,
2893  /*FoundElse*/ true, Result.getLocation());
2894 }
2895 
2896 /// HandleElifDirective - Implements the \#elif directive.
2897 ///
2898 void Preprocessor::HandleElifDirective(Token &ElifToken,
2899  const Token &HashToken) {
2900  ++NumElse;
2901 
2902  // #elif directive in a non-skipping conditional... start skipping.
2903  // We don't care what the condition is, because we will always skip it (since
2904  // the block immediately before it was included).
2905  const SourceLocation ConditionalBegin = CurPPLexer->getSourceLocation();
2907  const SourceLocation ConditionalEnd = CurPPLexer->getSourceLocation();
2908 
2909  PPConditionalInfo CI;
2910  if (CurPPLexer->popConditionalLevel(CI)) {
2911  Diag(ElifToken, diag::pp_err_elif_without_if);
2912  return;
2913  }
2914 
2915  // If this is a top-level #elif, inform the MIOpt.
2916  if (CurPPLexer->getConditionalStackDepth() == 0)
2917  CurPPLexer->MIOpt.EnterTopLevelConditional();
2918 
2919  // If this is a #elif with a #else before it, report the error.
2920  if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else);
2921 
2922  if (Callbacks)
2923  Callbacks->Elif(ElifToken.getLocation(),
2924  SourceRange(ConditionalBegin, ConditionalEnd),
2926 
2927  if (PPOpts->SingleFileParseMode && !CI.FoundNonSkip) {
2928  // In 'single-file-parse mode' undefined identifiers trigger parsing of all
2929  // the directive blocks.
2930  CurPPLexer->pushConditionalLevel(ElifToken.getLocation(), /*wasskip*/false,
2931  /*foundnonskip*/false, /*foundelse*/false);
2932  return;
2933  }
2934 
2935  // Finally, skip the rest of the contents of this block.
2936  SkipExcludedConditionalBlock(
2937  HashToken.getLocation(), CI.IfLoc, /*Foundnonskip*/ true,
2938  /*FoundElse*/ CI.FoundElse, ElifToken.getLocation());
2939 }
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:133
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:1295
A directive for an undefined macro.
Definition: MacroInfo.h:430
bool getSuppressSystemWarnings() const
Definition: Diagnostic.h:626
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:227
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:466
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:562
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:220
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:660
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:166
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:151
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