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