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