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