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