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