[DFAJumpThreading] Remove incoming StartBlock from all phis when unfolding select...
[llvm-project.git] / clang / lib / Lex / PPDirectives.cpp
blobd97a103833c2fa637462b19bdf2e250f0a7a3543
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 //===----------------------------------------------------------------------===//
14 #include "clang/Basic/CharInfo.h"
15 #include "clang/Basic/DirectoryEntry.h"
16 #include "clang/Basic/FileManager.h"
17 #include "clang/Basic/IdentifierTable.h"
18 #include "clang/Basic/LangOptions.h"
19 #include "clang/Basic/Module.h"
20 #include "clang/Basic/SourceLocation.h"
21 #include "clang/Basic/SourceManager.h"
22 #include "clang/Basic/TokenKinds.h"
23 #include "clang/Lex/CodeCompletionHandler.h"
24 #include "clang/Lex/HeaderSearch.h"
25 #include "clang/Lex/HeaderSearchOptions.h"
26 #include "clang/Lex/LexDiagnostic.h"
27 #include "clang/Lex/LiteralSupport.h"
28 #include "clang/Lex/MacroInfo.h"
29 #include "clang/Lex/ModuleLoader.h"
30 #include "clang/Lex/ModuleMap.h"
31 #include "clang/Lex/PPCallbacks.h"
32 #include "clang/Lex/Pragma.h"
33 #include "clang/Lex/Preprocessor.h"
34 #include "clang/Lex/PreprocessorOptions.h"
35 #include "clang/Lex/Token.h"
36 #include "clang/Lex/VariadicMacroSupport.h"
37 #include "llvm/ADT/ArrayRef.h"
38 #include "llvm/ADT/STLExtras.h"
39 #include "llvm/ADT/ScopeExit.h"
40 #include "llvm/ADT/SmallString.h"
41 #include "llvm/ADT/SmallVector.h"
42 #include "llvm/ADT/StringRef.h"
43 #include "llvm/ADT/StringSwitch.h"
44 #include "llvm/Support/AlignOf.h"
45 #include "llvm/Support/ErrorHandling.h"
46 #include "llvm/Support/Path.h"
47 #include "llvm/Support/SaveAndRestore.h"
48 #include <algorithm>
49 #include <cassert>
50 #include <cstring>
51 #include <new>
52 #include <optional>
53 #include <string>
54 #include <utility>
56 using namespace clang;
58 //===----------------------------------------------------------------------===//
59 // Utility Methods for Preprocessor Directive Handling.
60 //===----------------------------------------------------------------------===//
62 MacroInfo *Preprocessor::AllocateMacroInfo(SourceLocation L) {
63 static_assert(std::is_trivially_destructible_v<MacroInfo>, "");
64 return new (BP) MacroInfo(L);
67 DefMacroDirective *Preprocessor::AllocateDefMacroDirective(MacroInfo *MI,
68 SourceLocation Loc) {
69 return new (BP) DefMacroDirective(MI, Loc);
72 UndefMacroDirective *
73 Preprocessor::AllocateUndefMacroDirective(SourceLocation UndefLoc) {
74 return new (BP) UndefMacroDirective(UndefLoc);
77 VisibilityMacroDirective *
78 Preprocessor::AllocateVisibilityMacroDirective(SourceLocation Loc,
79 bool isPublic) {
80 return new (BP) VisibilityMacroDirective(Loc, isPublic);
83 /// Read and discard all tokens remaining on the current line until
84 /// the tok::eod token is found.
85 SourceRange Preprocessor::DiscardUntilEndOfDirective() {
86 Token Tmp;
87 SourceRange Res;
89 LexUnexpandedToken(Tmp);
90 Res.setBegin(Tmp.getLocation());
91 while (Tmp.isNot(tok::eod)) {
92 assert(Tmp.isNot(tok::eof) && "EOF seen while discarding directive tokens");
93 LexUnexpandedToken(Tmp);
95 Res.setEnd(Tmp.getLocation());
96 return Res;
99 /// Enumerates possible cases of #define/#undef a reserved identifier.
100 enum MacroDiag {
101 MD_NoWarn, //> Not a reserved identifier
102 MD_KeywordDef, //> Macro hides keyword, enabled by default
103 MD_ReservedMacro //> #define of #undef reserved id, disabled by default
106 /// Enumerates possible %select values for the pp_err_elif_after_else and
107 /// pp_err_elif_without_if diagnostics.
108 enum PPElifDiag {
109 PED_Elif,
110 PED_Elifdef,
111 PED_Elifndef
114 static bool isFeatureTestMacro(StringRef MacroName) {
115 // list from:
116 // * https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_macros.html
117 // * https://docs.microsoft.com/en-us/cpp/c-runtime-library/security-features-in-the-crt?view=msvc-160
118 // * man 7 feature_test_macros
119 // The list must be sorted for correct binary search.
120 static constexpr StringRef ReservedMacro[] = {
121 "_ATFILE_SOURCE",
122 "_BSD_SOURCE",
123 "_CRT_NONSTDC_NO_WARNINGS",
124 "_CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES",
125 "_CRT_SECURE_NO_WARNINGS",
126 "_FILE_OFFSET_BITS",
127 "_FORTIFY_SOURCE",
128 "_GLIBCXX_ASSERTIONS",
129 "_GLIBCXX_CONCEPT_CHECKS",
130 "_GLIBCXX_DEBUG",
131 "_GLIBCXX_DEBUG_PEDANTIC",
132 "_GLIBCXX_PARALLEL",
133 "_GLIBCXX_PARALLEL_ASSERTIONS",
134 "_GLIBCXX_SANITIZE_VECTOR",
135 "_GLIBCXX_USE_CXX11_ABI",
136 "_GLIBCXX_USE_DEPRECATED",
137 "_GNU_SOURCE",
138 "_ISOC11_SOURCE",
139 "_ISOC95_SOURCE",
140 "_ISOC99_SOURCE",
141 "_LARGEFILE64_SOURCE",
142 "_POSIX_C_SOURCE",
143 "_REENTRANT",
144 "_SVID_SOURCE",
145 "_THREAD_SAFE",
146 "_XOPEN_SOURCE",
147 "_XOPEN_SOURCE_EXTENDED",
148 "__STDCPP_WANT_MATH_SPEC_FUNCS__",
149 "__STDC_FORMAT_MACROS",
151 return std::binary_search(std::begin(ReservedMacro), std::end(ReservedMacro),
152 MacroName);
155 static bool isLanguageDefinedBuiltin(const SourceManager &SourceMgr,
156 const MacroInfo *MI,
157 const StringRef MacroName) {
158 // If this is a macro with special handling (like __LINE__) then it's language
159 // defined.
160 if (MI->isBuiltinMacro())
161 return true;
162 // Builtin macros are defined in the builtin file
163 if (!SourceMgr.isWrittenInBuiltinFile(MI->getDefinitionLoc()))
164 return false;
165 // C defines macros starting with __STDC, and C++ defines macros starting with
166 // __STDCPP
167 if (MacroName.startswith("__STDC"))
168 return true;
169 // C++ defines the __cplusplus macro
170 if (MacroName == "__cplusplus")
171 return true;
172 // C++ defines various feature-test macros starting with __cpp
173 if (MacroName.startswith("__cpp"))
174 return true;
175 // Anything else isn't language-defined
176 return false;
179 static MacroDiag shouldWarnOnMacroDef(Preprocessor &PP, IdentifierInfo *II) {
180 const LangOptions &Lang = PP.getLangOpts();
181 StringRef Text = II->getName();
182 if (isReservedInAllContexts(II->isReserved(Lang)))
183 return isFeatureTestMacro(Text) ? MD_NoWarn : MD_ReservedMacro;
184 if (II->isKeyword(Lang))
185 return MD_KeywordDef;
186 if (Lang.CPlusPlus11 && (Text.equals("override") || Text.equals("final")))
187 return MD_KeywordDef;
188 return MD_NoWarn;
191 static MacroDiag shouldWarnOnMacroUndef(Preprocessor &PP, IdentifierInfo *II) {
192 const LangOptions &Lang = PP.getLangOpts();
193 // Do not warn on keyword undef. It is generally harmless and widely used.
194 if (isReservedInAllContexts(II->isReserved(Lang)))
195 return MD_ReservedMacro;
196 return MD_NoWarn;
199 // Return true if we want to issue a diagnostic by default if we
200 // encounter this name in a #include with the wrong case. For now,
201 // this includes the standard C and C++ headers, Posix headers,
202 // and Boost headers. Improper case for these #includes is a
203 // potential portability issue.
204 static bool warnByDefaultOnWrongCase(StringRef Include) {
205 // If the first component of the path is "boost", treat this like a standard header
206 // for the purposes of diagnostics.
207 if (::llvm::sys::path::begin(Include)->equals_insensitive("boost"))
208 return true;
210 // "condition_variable" is the longest standard header name at 18 characters.
211 // If the include file name is longer than that, it can't be a standard header.
212 static const size_t MaxStdHeaderNameLen = 18u;
213 if (Include.size() > MaxStdHeaderNameLen)
214 return false;
216 // Lowercase and normalize the search string.
217 SmallString<32> LowerInclude{Include};
218 for (char &Ch : LowerInclude) {
219 // In the ASCII range?
220 if (static_cast<unsigned char>(Ch) > 0x7f)
221 return false; // Can't be a standard header
222 // ASCII lowercase:
223 if (Ch >= 'A' && Ch <= 'Z')
224 Ch += 'a' - 'A';
225 // Normalize path separators for comparison purposes.
226 else if (::llvm::sys::path::is_separator(Ch))
227 Ch = '/';
230 // The standard C/C++ and Posix headers
231 return llvm::StringSwitch<bool>(LowerInclude)
232 // C library headers
233 .Cases("assert.h", "complex.h", "ctype.h", "errno.h", "fenv.h", true)
234 .Cases("float.h", "inttypes.h", "iso646.h", "limits.h", "locale.h", true)
235 .Cases("math.h", "setjmp.h", "signal.h", "stdalign.h", "stdarg.h", true)
236 .Cases("stdatomic.h", "stdbool.h", "stdckdint.h", "stddef.h", true)
237 .Cases("stdint.h", "stdio.h", "stdlib.h", "stdnoreturn.h", true)
238 .Cases("string.h", "tgmath.h", "threads.h", "time.h", "uchar.h", true)
239 .Cases("wchar.h", "wctype.h", true)
241 // C++ headers for C library facilities
242 .Cases("cassert", "ccomplex", "cctype", "cerrno", "cfenv", true)
243 .Cases("cfloat", "cinttypes", "ciso646", "climits", "clocale", true)
244 .Cases("cmath", "csetjmp", "csignal", "cstdalign", "cstdarg", true)
245 .Cases("cstdbool", "cstddef", "cstdint", "cstdio", "cstdlib", true)
246 .Cases("cstring", "ctgmath", "ctime", "cuchar", "cwchar", true)
247 .Case("cwctype", true)
249 // C++ library headers
250 .Cases("algorithm", "fstream", "list", "regex", "thread", true)
251 .Cases("array", "functional", "locale", "scoped_allocator", "tuple", true)
252 .Cases("atomic", "future", "map", "set", "type_traits", true)
253 .Cases("bitset", "initializer_list", "memory", "shared_mutex", "typeindex", true)
254 .Cases("chrono", "iomanip", "mutex", "sstream", "typeinfo", true)
255 .Cases("codecvt", "ios", "new", "stack", "unordered_map", true)
256 .Cases("complex", "iosfwd", "numeric", "stdexcept", "unordered_set", true)
257 .Cases("condition_variable", "iostream", "ostream", "streambuf", "utility", true)
258 .Cases("deque", "istream", "queue", "string", "valarray", true)
259 .Cases("exception", "iterator", "random", "strstream", "vector", true)
260 .Cases("forward_list", "limits", "ratio", "system_error", true)
262 // POSIX headers (which aren't also C headers)
263 .Cases("aio.h", "arpa/inet.h", "cpio.h", "dirent.h", "dlfcn.h", true)
264 .Cases("fcntl.h", "fmtmsg.h", "fnmatch.h", "ftw.h", "glob.h", true)
265 .Cases("grp.h", "iconv.h", "langinfo.h", "libgen.h", "monetary.h", true)
266 .Cases("mqueue.h", "ndbm.h", "net/if.h", "netdb.h", "netinet/in.h", true)
267 .Cases("netinet/tcp.h", "nl_types.h", "poll.h", "pthread.h", "pwd.h", true)
268 .Cases("regex.h", "sched.h", "search.h", "semaphore.h", "spawn.h", true)
269 .Cases("strings.h", "stropts.h", "sys/ipc.h", "sys/mman.h", "sys/msg.h", true)
270 .Cases("sys/resource.h", "sys/select.h", "sys/sem.h", "sys/shm.h", "sys/socket.h", true)
271 .Cases("sys/stat.h", "sys/statvfs.h", "sys/time.h", "sys/times.h", "sys/types.h", true)
272 .Cases("sys/uio.h", "sys/un.h", "sys/utsname.h", "sys/wait.h", "syslog.h", true)
273 .Cases("tar.h", "termios.h", "trace.h", "ulimit.h", true)
274 .Cases("unistd.h", "utime.h", "utmpx.h", "wordexp.h", true)
275 .Default(false);
278 /// Find a similar string in `Candidates`.
280 /// \param LHS a string for a similar string in `Candidates`
282 /// \param Candidates the candidates to find a similar string.
284 /// \returns a similar string if exists. If no similar string exists,
285 /// returns std::nullopt.
286 static std::optional<StringRef>
287 findSimilarStr(StringRef LHS, const std::vector<StringRef> &Candidates) {
288 // We need to check if `Candidates` has the exact case-insensitive string
289 // because the Levenshtein distance match does not care about it.
290 for (StringRef C : Candidates) {
291 if (LHS.equals_insensitive(C)) {
292 return C;
296 // Keep going with the Levenshtein distance match.
297 // If the LHS size is less than 3, use the LHS size minus 1 and if not,
298 // use the LHS size divided by 3.
299 size_t Length = LHS.size();
300 size_t MaxDist = Length < 3 ? Length - 1 : Length / 3;
302 std::optional<std::pair<StringRef, size_t>> SimilarStr;
303 for (StringRef C : Candidates) {
304 size_t CurDist = LHS.edit_distance(C, true);
305 if (CurDist <= MaxDist) {
306 if (!SimilarStr) {
307 // The first similar string found.
308 SimilarStr = {C, CurDist};
309 } else if (CurDist < SimilarStr->second) {
310 // More similar string found.
311 SimilarStr = {C, CurDist};
316 if (SimilarStr) {
317 return SimilarStr->first;
318 } else {
319 return std::nullopt;
323 bool Preprocessor::CheckMacroName(Token &MacroNameTok, MacroUse isDefineUndef,
324 bool *ShadowFlag) {
325 // Missing macro name?
326 if (MacroNameTok.is(tok::eod))
327 return Diag(MacroNameTok, diag::err_pp_missing_macro_name);
329 IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
330 if (!II)
331 return Diag(MacroNameTok, diag::err_pp_macro_not_identifier);
333 if (II->isCPlusPlusOperatorKeyword()) {
334 // C++ 2.5p2: Alternative tokens behave the same as its primary token
335 // except for their spellings.
336 Diag(MacroNameTok, getLangOpts().MicrosoftExt
337 ? diag::ext_pp_operator_used_as_macro_name
338 : diag::err_pp_operator_used_as_macro_name)
339 << II << MacroNameTok.getKind();
340 // Allow #defining |and| and friends for Microsoft compatibility or
341 // recovery when legacy C headers are included in C++.
344 if ((isDefineUndef != MU_Other) && II->getPPKeywordID() == tok::pp_defined) {
345 // Error if defining "defined": C99 6.10.8/4, C++ [cpp.predefined]p4.
346 return Diag(MacroNameTok, diag::err_defined_macro_name);
349 // If defining/undefining reserved identifier or a keyword, we need to issue
350 // a warning.
351 SourceLocation MacroNameLoc = MacroNameTok.getLocation();
352 if (ShadowFlag)
353 *ShadowFlag = false;
354 if (!SourceMgr.isInSystemHeader(MacroNameLoc) &&
355 (SourceMgr.getBufferName(MacroNameLoc) != "<built-in>")) {
356 MacroDiag D = MD_NoWarn;
357 if (isDefineUndef == MU_Define) {
358 D = shouldWarnOnMacroDef(*this, II);
360 else if (isDefineUndef == MU_Undef)
361 D = shouldWarnOnMacroUndef(*this, II);
362 if (D == MD_KeywordDef) {
363 // We do not want to warn on some patterns widely used in configuration
364 // scripts. This requires analyzing next tokens, so do not issue warnings
365 // now, only inform caller.
366 if (ShadowFlag)
367 *ShadowFlag = true;
369 if (D == MD_ReservedMacro)
370 Diag(MacroNameTok, diag::warn_pp_macro_is_reserved_id);
373 // Okay, we got a good identifier.
374 return false;
377 /// Lex and validate a macro name, which occurs after a
378 /// \#define or \#undef.
380 /// This sets the token kind to eod and discards the rest of the macro line if
381 /// the macro name is invalid.
383 /// \param MacroNameTok Token that is expected to be a macro name.
384 /// \param isDefineUndef Context in which macro is used.
385 /// \param ShadowFlag Points to a flag that is set if macro shadows a keyword.
386 void Preprocessor::ReadMacroName(Token &MacroNameTok, MacroUse isDefineUndef,
387 bool *ShadowFlag) {
388 // Read the token, don't allow macro expansion on it.
389 LexUnexpandedToken(MacroNameTok);
391 if (MacroNameTok.is(tok::code_completion)) {
392 if (CodeComplete)
393 CodeComplete->CodeCompleteMacroName(isDefineUndef == MU_Define);
394 setCodeCompletionReached();
395 LexUnexpandedToken(MacroNameTok);
398 if (!CheckMacroName(MacroNameTok, isDefineUndef, ShadowFlag))
399 return;
401 // Invalid macro name, read and discard the rest of the line and set the
402 // token kind to tok::eod if necessary.
403 if (MacroNameTok.isNot(tok::eod)) {
404 MacroNameTok.setKind(tok::eod);
405 DiscardUntilEndOfDirective();
409 /// Ensure that the next token is a tok::eod token.
411 /// If not, emit a diagnostic and consume up until the eod. If EnableMacros is
412 /// true, then we consider macros that expand to zero tokens as being ok.
414 /// Returns the location of the end of the directive.
415 SourceLocation Preprocessor::CheckEndOfDirective(const char *DirType,
416 bool EnableMacros) {
417 Token Tmp;
418 // Lex unexpanded tokens for most directives: macros might expand to zero
419 // tokens, causing us to miss diagnosing invalid lines. Some directives (like
420 // #line) allow empty macros.
421 if (EnableMacros)
422 Lex(Tmp);
423 else
424 LexUnexpandedToken(Tmp);
426 // There should be no tokens after the directive, but we allow them as an
427 // extension.
428 while (Tmp.is(tok::comment)) // Skip comments in -C mode.
429 LexUnexpandedToken(Tmp);
431 if (Tmp.is(tok::eod))
432 return Tmp.getLocation();
434 // Add a fixit in GNU/C99/C++ mode. Don't offer a fixit for strict-C89,
435 // or if this is a macro-style preprocessing directive, because it is more
436 // trouble than it is worth to insert /**/ and check that there is no /**/
437 // in the range also.
438 FixItHint Hint;
439 if ((LangOpts.GNUMode || LangOpts.C99 || LangOpts.CPlusPlus) &&
440 !CurTokenLexer)
441 Hint = FixItHint::CreateInsertion(Tmp.getLocation(),"//");
442 Diag(Tmp, diag::ext_pp_extra_tokens_at_eol) << DirType << Hint;
443 return DiscardUntilEndOfDirective().getEnd();
446 void Preprocessor::SuggestTypoedDirective(const Token &Tok,
447 StringRef Directive) const {
448 // If this is a `.S` file, treat unknown # directives as non-preprocessor
449 // directives.
450 if (getLangOpts().AsmPreprocessor) return;
452 std::vector<StringRef> Candidates = {
453 "if", "ifdef", "ifndef", "elif", "else", "endif"
455 if (LangOpts.C23 || LangOpts.CPlusPlus23)
456 Candidates.insert(Candidates.end(), {"elifdef", "elifndef"});
458 if (std::optional<StringRef> Sugg = findSimilarStr(Directive, Candidates)) {
459 // Directive cannot be coming from macro.
460 assert(Tok.getLocation().isFileID());
461 CharSourceRange DirectiveRange = CharSourceRange::getCharRange(
462 Tok.getLocation(),
463 Tok.getLocation().getLocWithOffset(Directive.size()));
464 StringRef SuggValue = *Sugg;
466 auto Hint = FixItHint::CreateReplacement(DirectiveRange, SuggValue);
467 Diag(Tok, diag::warn_pp_invalid_directive) << 1 << SuggValue << Hint;
471 /// SkipExcludedConditionalBlock - We just read a \#if or related directive and
472 /// decided that the subsequent tokens are in the \#if'd out portion of the
473 /// file. Lex the rest of the file, until we see an \#endif. If
474 /// FoundNonSkipPortion is true, then we have already emitted code for part of
475 /// this \#if directive, so \#else/\#elif blocks should never be entered.
476 /// If ElseOk is true, then \#else directives are ok, if not, then we have
477 /// already seen one so a \#else directive is a duplicate. When this returns,
478 /// the caller can lex the first valid token.
479 void Preprocessor::SkipExcludedConditionalBlock(SourceLocation HashTokenLoc,
480 SourceLocation IfTokenLoc,
481 bool FoundNonSkipPortion,
482 bool FoundElse,
483 SourceLocation ElseLoc) {
484 // In SkippingRangeStateTy we are depending on SkipExcludedConditionalBlock()
485 // not getting called recursively by storing the RecordedSkippedRanges
486 // DenseMap lookup pointer (field SkipRangePtr). SkippingRangeStateTy expects
487 // that RecordedSkippedRanges won't get modified and SkipRangePtr won't be
488 // invalidated. If this changes and there is a need to call
489 // SkipExcludedConditionalBlock() recursively, SkippingRangeStateTy should
490 // change to do a second lookup in endLexPass function instead of reusing the
491 // lookup pointer.
492 assert(!SkippingExcludedConditionalBlock &&
493 "calling SkipExcludedConditionalBlock recursively");
494 llvm::SaveAndRestore SARSkipping(SkippingExcludedConditionalBlock, true);
496 ++NumSkipped;
497 assert(!CurTokenLexer && "Conditional PP block cannot appear in a macro!");
498 assert(CurPPLexer && "Conditional PP block must be in a file!");
499 assert(CurLexer && "Conditional PP block but no current lexer set!");
501 if (PreambleConditionalStack.reachedEOFWhileSkipping())
502 PreambleConditionalStack.clearSkipInfo();
503 else
504 CurPPLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/ false,
505 FoundNonSkipPortion, FoundElse);
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;
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;
518 const char *BeginPtr = nullptr;
519 unsigned *SkipRangePtr = nullptr;
521 SkippingRangeStateTy(Preprocessor &PP) : PP(PP) {}
523 void beginLexPass() {
524 if (BeginPtr)
525 return; // continue skipping a block.
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);
537 void endLexPass(const char *Hashptr) {
538 if (!BeginPtr) {
539 // Not doing normal lexing.
540 assert(PP.CurLexer->isDependencyDirectivesLexer());
541 return;
544 // Finished skipping a block, record the range if it's first time visited.
545 if (!*SkipRangePtr) {
546 *SkipRangePtr = Hashptr - BeginPtr;
548 assert(*SkipRangePtr == Hashptr - BeginPtr);
549 BeginPtr = nullptr;
550 SkipRangePtr = nullptr;
552 } SkippingRangeState(*this);
554 while (true) {
555 if (CurLexer->isDependencyDirectivesLexer()) {
556 CurLexer->LexDependencyDirectiveTokenWhileSkipping(Tok);
557 } else {
558 SkippingRangeState.beginLexPass();
559 while (true) {
560 CurLexer->Lex(Tok);
562 if (Tok.is(tok::code_completion)) {
563 setCodeCompletionReached();
564 if (CodeComplete)
565 CodeComplete->CodeCompleteInConditionalExclusion();
566 continue;
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;
581 // If this token is not a preprocessor directive, just skip it.
582 if (Tok.isNot(tok::hash) || !Tok.isAtStartOfLine())
583 continue;
585 break;
588 if (Tok.is(tok::eof))
589 break;
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);
597 assert(Tok.is(tok::hash));
598 const char *Hashptr = CurLexer->getBufferLocation() - Tok.getLength();
599 assert(CurLexer->getSourceLocation(Hashptr) == Tok.getLocation());
601 // Read the next token, the directive flavor.
602 LexUnexpandedToken(Tok);
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;
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();
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;
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;
645 memcpy(DirectiveBuf, &DirectiveStr[0], IdLen);
646 Directive = StringRef(DirectiveBuf, IdLen);
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.
656 DiscardUntilEndOfDirective();
657 CurPPLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true,
658 /*foundnonskip*/false,
659 /*foundelse*/false);
660 } else {
661 SuggestTypoedDirective(Tok, Directive);
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!");
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 {
684 DiscardUntilEndOfDirective();
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();
692 if (!CondInfo.WasSkipping)
693 SkippingRangeState.endLexPass(Hashptr);
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);
699 // Note that we've seen a #else in this conditional.
700 CondInfo.FoundElse = true;
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.
717 } else if (Sub == "lif") { // "elif".
718 PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel();
720 if (!CondInfo.WasSkipping)
721 SkippingRangeState.endLexPass(Hashptr);
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;
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.
734 DiscardUntilEndOfDirective();
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,
750 (CondValue ? PPCallbacks::CVK_True : PPCallbacks::CVK_False),
751 CondInfo.IfLoc);
753 // If this condition is true, enter it!
754 if (CondValue) {
755 CondInfo.FoundNonSkip = true;
756 break;
759 } else if (Sub == "lifdef" || // "elifdef"
760 Sub == "lifndef") { // "elifndef"
761 bool IsElifDef = Sub == "lifdef";
762 PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel();
763 Token DirectiveToken = Tok;
765 if (!CondInfo.WasSkipping)
766 SkippingRangeState.endLexPass(Hashptr);
768 // Warn if using `#elifdef` & `#elifndef` in not C23 & C++23 mode even
769 // if this branch is in a skipping block.
770 unsigned DiagID;
771 if (LangOpts.CPlusPlus)
772 DiagID = LangOpts.CPlusPlus23 ? diag::warn_cxx23_compat_pp_directive
773 : diag::ext_cxx23_pp_directive;
774 else
775 DiagID = LangOpts.C23 ? diag::warn_c23_compat_pp_directive
776 : diag::ext_c23_pp_directive;
777 Diag(Tok, DiagID) << (IsElifDef ? PED_Elifdef : PED_Elifndef);
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);
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.
791 DiscardUntilEndOfDirective();
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;
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;
809 emitMacroExpansionWarnings(MacroNameTok);
811 CheckEndOfDirective(IsElifDef ? "elifdef" : "elifndef");
813 IdentifierInfo *MII = MacroNameTok.getIdentifierInfo();
814 auto MD = getMacroDefinition(MII);
815 MacroInfo *MI = MD.getMacroInfo();
817 if (Callbacks) {
818 if (IsElifDef) {
819 Callbacks->Elifdef(DirectiveToken.getLocation(), MacroNameTok,
820 MD);
821 } else {
822 Callbacks->Elifndef(DirectiveToken.getLocation(), MacroNameTok,
823 MD);
826 // If this condition is true, enter it!
827 if (static_cast<bool>(MI) == IsElifDef) {
828 CondInfo.FoundNonSkip = true;
829 break;
832 } else {
833 SuggestTypoedDirective(Tok, Directive);
835 } else {
836 SuggestTypoedDirective(Tok, Directive);
839 CurPPLexer->ParsingPreprocessorDirective = false;
840 // Restore comment saving mode.
841 if (CurLexer) CurLexer->resetExtendedTokenMode();
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;
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());
859 Module *Preprocessor::getModuleForLocation(SourceLocation Loc,
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 (auto EntryOfIncl = SourceMgr.getFileEntryRefForID(IDOfIncl)) {
866 // The include comes from an included file.
867 return HeaderInfo.getModuleMap()
868 .findModuleForHeader(*EntryOfIncl, AllowTextual)
869 .getModule();
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);
880 OptionalFileEntryRef
881 Preprocessor::getHeaderToIncludeForDiagnostics(SourceLocation IncLoc,
882 SourceLocation Loc) {
883 Module *IncM = getModuleForLocation(
884 IncLoc, LangOpts.ModulesValidateTextualHeaderIncludes);
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.getFileEntryRefForID(ID);
894 if (!FE)
895 break;
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));
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;
913 // Don't suggest explicitly excluded headers.
914 if (Header.getRole() == ModuleMap::ExcludedHeader)
915 continue;
917 // We'll suggest including textual headers below if they're
918 // include-guarded.
919 if (Header.getRole() & ModuleMap::TextualHeader)
920 continue;
922 // If we have a module import syntax, we shouldn't include a header to
923 // make a particular module visible. Let the caller know they should
924 // suggest an import instead.
925 if (getLangOpts().ObjC || getLangOpts().CPlusPlusModules)
926 return std::nullopt;
928 // If this is an accessible, non-textual header of M's top-level module
929 // that transitively includes the given location and makes the
930 // corresponding module visible, this is the thing to #include.
931 return *FE;
934 // FIXME: If we're bailing out due to a private header, we shouldn't suggest
935 // an import either.
936 if (InPrivateHeader)
937 return std::nullopt;
939 // If the header is includable and has an include guard, assume the
940 // intended way to expose its contents is by #include, not by importing a
941 // module that transitively includes it.
942 if (getHeaderSearchInfo().isFileMultipleIncludeGuarded(*FE))
943 return *FE;
945 Loc = SM.getIncludeLoc(ID);
948 return std::nullopt;
951 OptionalFileEntryRef Preprocessor::LookupFile(
952 SourceLocation FilenameLoc, StringRef Filename, bool isAngled,
953 ConstSearchDirIterator FromDir, const FileEntry *FromFile,
954 ConstSearchDirIterator *CurDirArg, SmallVectorImpl<char> *SearchPath,
955 SmallVectorImpl<char> *RelativePath,
956 ModuleMap::KnownHeader *SuggestedModule, bool *IsMapped,
957 bool *IsFrameworkFound, bool SkipCache, bool OpenFile, bool CacheFailures) {
958 ConstSearchDirIterator CurDirLocal = nullptr;
959 ConstSearchDirIterator &CurDir = CurDirArg ? *CurDirArg : CurDirLocal;
961 Module *RequestingModule = getModuleForLocation(
962 FilenameLoc, LangOpts.ModulesValidateTextualHeaderIncludes);
963 bool RequestingModuleIsModuleInterface = !SourceMgr.isInMainFile(FilenameLoc);
965 // If the header lookup mechanism may be relative to the current inclusion
966 // stack, record the parent #includes.
967 SmallVector<std::pair<OptionalFileEntryRef, DirectoryEntryRef>, 16> Includers;
968 bool BuildSystemModule = false;
969 if (!FromDir && !FromFile) {
970 FileID FID = getCurrentFileLexer()->getFileID();
971 OptionalFileEntryRef FileEnt = SourceMgr.getFileEntryRefForID(FID);
973 // If there is no file entry associated with this file, it must be the
974 // predefines buffer or the module includes buffer. Any other file is not
975 // lexed with a normal lexer, so it won't be scanned for preprocessor
976 // directives.
978 // If we have the predefines buffer, resolve #include references (which come
979 // from the -include command line argument) from the current working
980 // directory instead of relative to the main file.
982 // If we have the module includes buffer, resolve #include references (which
983 // come from header declarations in the module map) relative to the module
984 // map file.
985 if (!FileEnt) {
986 if (FID == SourceMgr.getMainFileID() && MainFileDir) {
987 auto IncludeDir =
988 HeaderInfo.getModuleMap().shouldImportRelativeToBuiltinIncludeDir(
989 Filename, getCurrentModule())
990 ? HeaderInfo.getModuleMap().getBuiltinDir()
991 : MainFileDir;
992 Includers.push_back(std::make_pair(std::nullopt, *IncludeDir));
993 BuildSystemModule = getCurrentModule()->IsSystem;
994 } else if ((FileEnt = SourceMgr.getFileEntryRefForID(
995 SourceMgr.getMainFileID()))) {
996 auto CWD = FileMgr.getOptionalDirectoryRef(".");
997 Includers.push_back(std::make_pair(*FileEnt, *CWD));
999 } else {
1000 Includers.push_back(std::make_pair(*FileEnt, FileEnt->getDir()));
1003 // MSVC searches the current include stack from top to bottom for
1004 // headers included by quoted include directives.
1005 // See: http://msdn.microsoft.com/en-us/library/36k2cdd4.aspx
1006 if (LangOpts.MSVCCompat && !isAngled) {
1007 for (IncludeStackInfo &ISEntry : llvm::reverse(IncludeMacroStack)) {
1008 if (IsFileLexer(ISEntry))
1009 if ((FileEnt = ISEntry.ThePPLexer->getFileEntry()))
1010 Includers.push_back(std::make_pair(*FileEnt, FileEnt->getDir()));
1015 CurDir = CurDirLookup;
1017 if (FromFile) {
1018 // We're supposed to start looking from after a particular file. Search
1019 // the include path until we find that file or run out of files.
1020 ConstSearchDirIterator TmpCurDir = CurDir;
1021 ConstSearchDirIterator TmpFromDir = nullptr;
1022 while (OptionalFileEntryRef FE = HeaderInfo.LookupFile(
1023 Filename, FilenameLoc, isAngled, TmpFromDir, &TmpCurDir,
1024 Includers, SearchPath, RelativePath, RequestingModule,
1025 SuggestedModule, /*IsMapped=*/nullptr,
1026 /*IsFrameworkFound=*/nullptr, SkipCache)) {
1027 // Keep looking as if this file did a #include_next.
1028 TmpFromDir = TmpCurDir;
1029 ++TmpFromDir;
1030 if (&FE->getFileEntry() == FromFile) {
1031 // Found it.
1032 FromDir = TmpFromDir;
1033 CurDir = TmpCurDir;
1034 break;
1039 // Do a standard file entry lookup.
1040 OptionalFileEntryRef FE = HeaderInfo.LookupFile(
1041 Filename, FilenameLoc, isAngled, FromDir, &CurDir, Includers, SearchPath,
1042 RelativePath, RequestingModule, SuggestedModule, IsMapped,
1043 IsFrameworkFound, SkipCache, BuildSystemModule, OpenFile, CacheFailures);
1044 if (FE) {
1045 if (SuggestedModule && !LangOpts.AsmPreprocessor)
1046 HeaderInfo.getModuleMap().diagnoseHeaderInclusion(
1047 RequestingModule, RequestingModuleIsModuleInterface, FilenameLoc,
1048 Filename, *FE);
1049 return FE;
1052 OptionalFileEntryRef CurFileEnt;
1053 // Otherwise, see if this is a subframework header. If so, this is relative
1054 // to one of the headers on the #include stack. Walk the list of the current
1055 // headers on the #include stack and pass them to HeaderInfo.
1056 if (IsFileLexer()) {
1057 if ((CurFileEnt = CurPPLexer->getFileEntry())) {
1058 if (OptionalFileEntryRef FE = HeaderInfo.LookupSubframeworkHeader(
1059 Filename, *CurFileEnt, SearchPath, RelativePath, RequestingModule,
1060 SuggestedModule)) {
1061 if (SuggestedModule && !LangOpts.AsmPreprocessor)
1062 HeaderInfo.getModuleMap().diagnoseHeaderInclusion(
1063 RequestingModule, RequestingModuleIsModuleInterface, FilenameLoc,
1064 Filename, *FE);
1065 return FE;
1070 for (IncludeStackInfo &ISEntry : llvm::reverse(IncludeMacroStack)) {
1071 if (IsFileLexer(ISEntry)) {
1072 if ((CurFileEnt = ISEntry.ThePPLexer->getFileEntry())) {
1073 if (OptionalFileEntryRef FE = HeaderInfo.LookupSubframeworkHeader(
1074 Filename, *CurFileEnt, SearchPath, RelativePath,
1075 RequestingModule, SuggestedModule)) {
1076 if (SuggestedModule && !LangOpts.AsmPreprocessor)
1077 HeaderInfo.getModuleMap().diagnoseHeaderInclusion(
1078 RequestingModule, RequestingModuleIsModuleInterface,
1079 FilenameLoc, Filename, *FE);
1080 return FE;
1086 // Otherwise, we really couldn't find the file.
1087 return std::nullopt;
1090 //===----------------------------------------------------------------------===//
1091 // Preprocessor Directive Handling.
1092 //===----------------------------------------------------------------------===//
1094 class Preprocessor::ResetMacroExpansionHelper {
1095 public:
1096 ResetMacroExpansionHelper(Preprocessor *pp)
1097 : PP(pp), save(pp->DisableMacroExpansion) {
1098 if (pp->MacroExpansionInDirectivesOverride)
1099 pp->DisableMacroExpansion = false;
1102 ~ResetMacroExpansionHelper() {
1103 PP->DisableMacroExpansion = save;
1106 private:
1107 Preprocessor *PP;
1108 bool save;
1111 /// Process a directive while looking for the through header or a #pragma
1112 /// hdrstop. The following directives are handled:
1113 /// #include (to check if it is the through header)
1114 /// #define (to warn about macros that don't match the PCH)
1115 /// #pragma (to check for pragma hdrstop).
1116 /// All other directives are completely discarded.
1117 void Preprocessor::HandleSkippedDirectiveWhileUsingPCH(Token &Result,
1118 SourceLocation HashLoc) {
1119 if (const IdentifierInfo *II = Result.getIdentifierInfo()) {
1120 if (II->getPPKeywordID() == tok::pp_define) {
1121 return HandleDefineDirective(Result,
1122 /*ImmediatelyAfterHeaderGuard=*/false);
1124 if (SkippingUntilPCHThroughHeader &&
1125 II->getPPKeywordID() == tok::pp_include) {
1126 return HandleIncludeDirective(HashLoc, Result);
1128 if (SkippingUntilPragmaHdrStop && II->getPPKeywordID() == tok::pp_pragma) {
1129 Lex(Result);
1130 auto *II = Result.getIdentifierInfo();
1131 if (II && II->getName() == "hdrstop")
1132 return HandlePragmaHdrstop(Result);
1135 DiscardUntilEndOfDirective();
1138 /// HandleDirective - This callback is invoked when the lexer sees a # token
1139 /// at the start of a line. This consumes the directive, modifies the
1140 /// lexer/preprocessor state, and advances the lexer(s) so that the next token
1141 /// read is the correct one.
1142 void Preprocessor::HandleDirective(Token &Result) {
1143 // FIXME: Traditional: # with whitespace before it not recognized by K&R?
1145 // We just parsed a # character at the start of a line, so we're in directive
1146 // mode. Tell the lexer this so any newlines we see will be converted into an
1147 // EOD token (which terminates the directive).
1148 CurPPLexer->ParsingPreprocessorDirective = true;
1149 if (CurLexer) CurLexer->SetKeepWhitespaceMode(false);
1151 bool ImmediatelyAfterTopLevelIfndef =
1152 CurPPLexer->MIOpt.getImmediatelyAfterTopLevelIfndef();
1153 CurPPLexer->MIOpt.resetImmediatelyAfterTopLevelIfndef();
1155 ++NumDirectives;
1157 // We are about to read a token. For the multiple-include optimization FA to
1158 // work, we have to remember if we had read any tokens *before* this
1159 // pp-directive.
1160 bool ReadAnyTokensBeforeDirective =CurPPLexer->MIOpt.getHasReadAnyTokensVal();
1162 // Save the '#' token in case we need to return it later.
1163 Token SavedHash = Result;
1165 // Read the next token, the directive flavor. This isn't expanded due to
1166 // C99 6.10.3p8.
1167 LexUnexpandedToken(Result);
1169 // C99 6.10.3p11: Is this preprocessor directive in macro invocation? e.g.:
1170 // #define A(x) #x
1171 // A(abc
1172 // #warning blah
1173 // def)
1174 // If so, the user is relying on undefined behavior, emit a diagnostic. Do
1175 // not support this for #include-like directives, since that can result in
1176 // terrible diagnostics, and does not work in GCC.
1177 if (InMacroArgs) {
1178 if (IdentifierInfo *II = Result.getIdentifierInfo()) {
1179 switch (II->getPPKeywordID()) {
1180 case tok::pp_include:
1181 case tok::pp_import:
1182 case tok::pp_include_next:
1183 case tok::pp___include_macros:
1184 case tok::pp_pragma:
1185 Diag(Result, diag::err_embedded_directive) << II->getName();
1186 Diag(*ArgMacro, diag::note_macro_expansion_here)
1187 << ArgMacro->getIdentifierInfo();
1188 DiscardUntilEndOfDirective();
1189 return;
1190 default:
1191 break;
1194 Diag(Result, diag::ext_embedded_directive);
1197 // Temporarily enable macro expansion if set so
1198 // and reset to previous state when returning from this function.
1199 ResetMacroExpansionHelper helper(this);
1201 if (SkippingUntilPCHThroughHeader || SkippingUntilPragmaHdrStop)
1202 return HandleSkippedDirectiveWhileUsingPCH(Result, SavedHash.getLocation());
1204 switch (Result.getKind()) {
1205 case tok::eod:
1206 // Ignore the null directive with regards to the multiple-include
1207 // optimization, i.e. allow the null directive to appear outside of the
1208 // include guard and still enable the multiple-include optimization.
1209 CurPPLexer->MIOpt.SetReadToken(ReadAnyTokensBeforeDirective);
1210 return; // null directive.
1211 case tok::code_completion:
1212 setCodeCompletionReached();
1213 if (CodeComplete)
1214 CodeComplete->CodeCompleteDirective(
1215 CurPPLexer->getConditionalStackDepth() > 0);
1216 return;
1217 case tok::numeric_constant: // # 7 GNU line marker directive.
1218 // In a .S file "# 4" may be a comment so don't treat it as a preprocessor
1219 // directive. However do permit it in the predefines file, as we use line
1220 // markers to mark the builtin macros as being in a system header.
1221 if (getLangOpts().AsmPreprocessor &&
1222 SourceMgr.getFileID(SavedHash.getLocation()) != getPredefinesFileID())
1223 break;
1224 return HandleDigitDirective(Result);
1225 default:
1226 IdentifierInfo *II = Result.getIdentifierInfo();
1227 if (!II) break; // Not an identifier.
1229 // Ask what the preprocessor keyword ID is.
1230 switch (II->getPPKeywordID()) {
1231 default: break;
1232 // C99 6.10.1 - Conditional Inclusion.
1233 case tok::pp_if:
1234 return HandleIfDirective(Result, SavedHash, ReadAnyTokensBeforeDirective);
1235 case tok::pp_ifdef:
1236 return HandleIfdefDirective(Result, SavedHash, false,
1237 true /*not valid for miopt*/);
1238 case tok::pp_ifndef:
1239 return HandleIfdefDirective(Result, SavedHash, true,
1240 ReadAnyTokensBeforeDirective);
1241 case tok::pp_elif:
1242 case tok::pp_elifdef:
1243 case tok::pp_elifndef:
1244 return HandleElifFamilyDirective(Result, SavedHash, II->getPPKeywordID());
1246 case tok::pp_else:
1247 return HandleElseDirective(Result, SavedHash);
1248 case tok::pp_endif:
1249 return HandleEndifDirective(Result);
1251 // C99 6.10.2 - Source File Inclusion.
1252 case tok::pp_include:
1253 // Handle #include.
1254 return HandleIncludeDirective(SavedHash.getLocation(), Result);
1255 case tok::pp___include_macros:
1256 // Handle -imacros.
1257 return HandleIncludeMacrosDirective(SavedHash.getLocation(), Result);
1259 // C99 6.10.3 - Macro Replacement.
1260 case tok::pp_define:
1261 return HandleDefineDirective(Result, ImmediatelyAfterTopLevelIfndef);
1262 case tok::pp_undef:
1263 return HandleUndefDirective();
1265 // C99 6.10.4 - Line Control.
1266 case tok::pp_line:
1267 return HandleLineDirective();
1269 // C99 6.10.5 - Error Directive.
1270 case tok::pp_error:
1271 return HandleUserDiagnosticDirective(Result, false);
1273 // C99 6.10.6 - Pragma Directive.
1274 case tok::pp_pragma:
1275 return HandlePragmaDirective({PIK_HashPragma, SavedHash.getLocation()});
1277 // GNU Extensions.
1278 case tok::pp_import:
1279 return HandleImportDirective(SavedHash.getLocation(), Result);
1280 case tok::pp_include_next:
1281 return HandleIncludeNextDirective(SavedHash.getLocation(), Result);
1283 case tok::pp_warning:
1284 if (LangOpts.CPlusPlus)
1285 Diag(Result, LangOpts.CPlusPlus23
1286 ? diag::warn_cxx23_compat_warning_directive
1287 : diag::ext_pp_warning_directive)
1288 << /*C++23*/ 1;
1289 else
1290 Diag(Result, LangOpts.C23 ? diag::warn_c23_compat_warning_directive
1291 : diag::ext_pp_warning_directive)
1292 << /*C23*/ 0;
1294 return HandleUserDiagnosticDirective(Result, true);
1295 case tok::pp_ident:
1296 return HandleIdentSCCSDirective(Result);
1297 case tok::pp_sccs:
1298 return HandleIdentSCCSDirective(Result);
1299 case tok::pp_assert:
1300 //isExtension = true; // FIXME: implement #assert
1301 break;
1302 case tok::pp_unassert:
1303 //isExtension = true; // FIXME: implement #unassert
1304 break;
1306 case tok::pp___public_macro:
1307 if (getLangOpts().Modules || getLangOpts().ModulesLocalVisibility)
1308 return HandleMacroPublicDirective(Result);
1309 break;
1311 case tok::pp___private_macro:
1312 if (getLangOpts().Modules || getLangOpts().ModulesLocalVisibility)
1313 return HandleMacroPrivateDirective();
1314 break;
1316 break;
1319 // If this is a .S file, treat unknown # directives as non-preprocessor
1320 // directives. This is important because # may be a comment or introduce
1321 // various pseudo-ops. Just return the # token and push back the following
1322 // token to be lexed next time.
1323 if (getLangOpts().AsmPreprocessor) {
1324 auto Toks = std::make_unique<Token[]>(2);
1325 // Return the # and the token after it.
1326 Toks[0] = SavedHash;
1327 Toks[1] = Result;
1329 // If the second token is a hashhash token, then we need to translate it to
1330 // unknown so the token lexer doesn't try to perform token pasting.
1331 if (Result.is(tok::hashhash))
1332 Toks[1].setKind(tok::unknown);
1334 // Enter this token stream so that we re-lex the tokens. Make sure to
1335 // enable macro expansion, in case the token after the # is an identifier
1336 // that is expanded.
1337 EnterTokenStream(std::move(Toks), 2, false, /*IsReinject*/false);
1338 return;
1341 // If we reached here, the preprocessing token is not valid!
1342 // Start suggesting if a similar directive found.
1343 Diag(Result, diag::err_pp_invalid_directive) << 0;
1345 // Read the rest of the PP line.
1346 DiscardUntilEndOfDirective();
1348 // Okay, we're done parsing the directive.
1351 /// GetLineValue - Convert a numeric token into an unsigned value, emitting
1352 /// Diagnostic DiagID if it is invalid, and returning the value in Val.
1353 static bool GetLineValue(Token &DigitTok, unsigned &Val,
1354 unsigned DiagID, Preprocessor &PP,
1355 bool IsGNULineDirective=false) {
1356 if (DigitTok.isNot(tok::numeric_constant)) {
1357 PP.Diag(DigitTok, DiagID);
1359 if (DigitTok.isNot(tok::eod))
1360 PP.DiscardUntilEndOfDirective();
1361 return true;
1364 SmallString<64> IntegerBuffer;
1365 IntegerBuffer.resize(DigitTok.getLength());
1366 const char *DigitTokBegin = &IntegerBuffer[0];
1367 bool Invalid = false;
1368 unsigned ActualLength = PP.getSpelling(DigitTok, DigitTokBegin, &Invalid);
1369 if (Invalid)
1370 return true;
1372 // Verify that we have a simple digit-sequence, and compute the value. This
1373 // is always a simple digit string computed in decimal, so we do this manually
1374 // here.
1375 Val = 0;
1376 for (unsigned i = 0; i != ActualLength; ++i) {
1377 // C++1y [lex.fcon]p1:
1378 // Optional separating single quotes in a digit-sequence are ignored
1379 if (DigitTokBegin[i] == '\'')
1380 continue;
1382 if (!isDigit(DigitTokBegin[i])) {
1383 PP.Diag(PP.AdvanceToTokenCharacter(DigitTok.getLocation(), i),
1384 diag::err_pp_line_digit_sequence) << IsGNULineDirective;
1385 PP.DiscardUntilEndOfDirective();
1386 return true;
1389 unsigned NextVal = Val*10+(DigitTokBegin[i]-'0');
1390 if (NextVal < Val) { // overflow.
1391 PP.Diag(DigitTok, DiagID);
1392 PP.DiscardUntilEndOfDirective();
1393 return true;
1395 Val = NextVal;
1398 if (DigitTokBegin[0] == '0' && Val)
1399 PP.Diag(DigitTok.getLocation(), diag::warn_pp_line_decimal)
1400 << IsGNULineDirective;
1402 return false;
1405 /// Handle a \#line directive: C99 6.10.4.
1407 /// The two acceptable forms are:
1408 /// \verbatim
1409 /// # line digit-sequence
1410 /// # line digit-sequence "s-char-sequence"
1411 /// \endverbatim
1412 void Preprocessor::HandleLineDirective() {
1413 // Read the line # and string argument. Per C99 6.10.4p5, these tokens are
1414 // expanded.
1415 Token DigitTok;
1416 Lex(DigitTok);
1418 // Validate the number and convert it to an unsigned.
1419 unsigned LineNo;
1420 if (GetLineValue(DigitTok, LineNo, diag::err_pp_line_requires_integer,*this))
1421 return;
1423 if (LineNo == 0)
1424 Diag(DigitTok, diag::ext_pp_line_zero);
1426 // Enforce C99 6.10.4p3: "The digit sequence shall not specify ... a
1427 // number greater than 2147483647". C90 requires that the line # be <= 32767.
1428 unsigned LineLimit = 32768U;
1429 if (LangOpts.C99 || LangOpts.CPlusPlus11)
1430 LineLimit = 2147483648U;
1431 if (LineNo >= LineLimit)
1432 Diag(DigitTok, diag::ext_pp_line_too_big) << LineLimit;
1433 else if (LangOpts.CPlusPlus11 && LineNo >= 32768U)
1434 Diag(DigitTok, diag::warn_cxx98_compat_pp_line_too_big);
1436 int FilenameID = -1;
1437 Token StrTok;
1438 Lex(StrTok);
1440 // If the StrTok is "eod", then it wasn't present. Otherwise, it must be a
1441 // string followed by eod.
1442 if (StrTok.is(tok::eod))
1443 ; // ok
1444 else if (StrTok.isNot(tok::string_literal)) {
1445 Diag(StrTok, diag::err_pp_line_invalid_filename);
1446 DiscardUntilEndOfDirective();
1447 return;
1448 } else if (StrTok.hasUDSuffix()) {
1449 Diag(StrTok, diag::err_invalid_string_udl);
1450 DiscardUntilEndOfDirective();
1451 return;
1452 } else {
1453 // Parse and validate the string, converting it into a unique ID.
1454 StringLiteralParser Literal(StrTok, *this);
1455 assert(Literal.isOrdinary() && "Didn't allow wide strings in");
1456 if (Literal.hadError) {
1457 DiscardUntilEndOfDirective();
1458 return;
1460 if (Literal.Pascal) {
1461 Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
1462 DiscardUntilEndOfDirective();
1463 return;
1465 FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString());
1467 // Verify that there is nothing after the string, other than EOD. Because
1468 // of C99 6.10.4p5, macros that expand to empty tokens are ok.
1469 CheckEndOfDirective("line", true);
1472 // Take the file kind of the file containing the #line directive. #line
1473 // directives are often used for generated sources from the same codebase, so
1474 // the new file should generally be classified the same way as the current
1475 // file. This is visible in GCC's pre-processed output, which rewrites #line
1476 // to GNU line markers.
1477 SrcMgr::CharacteristicKind FileKind =
1478 SourceMgr.getFileCharacteristic(DigitTok.getLocation());
1480 SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID, false,
1481 false, FileKind);
1483 if (Callbacks)
1484 Callbacks->FileChanged(CurPPLexer->getSourceLocation(),
1485 PPCallbacks::RenameFile, FileKind);
1488 /// ReadLineMarkerFlags - Parse and validate any flags at the end of a GNU line
1489 /// marker directive.
1490 static bool ReadLineMarkerFlags(bool &IsFileEntry, bool &IsFileExit,
1491 SrcMgr::CharacteristicKind &FileKind,
1492 Preprocessor &PP) {
1493 unsigned FlagVal;
1494 Token FlagTok;
1495 PP.Lex(FlagTok);
1496 if (FlagTok.is(tok::eod)) return false;
1497 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP))
1498 return true;
1500 if (FlagVal == 1) {
1501 IsFileEntry = true;
1503 PP.Lex(FlagTok);
1504 if (FlagTok.is(tok::eod)) return false;
1505 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP))
1506 return true;
1507 } else if (FlagVal == 2) {
1508 IsFileExit = true;
1510 SourceManager &SM = PP.getSourceManager();
1511 // If we are leaving the current presumed file, check to make sure the
1512 // presumed include stack isn't empty!
1513 FileID CurFileID =
1514 SM.getDecomposedExpansionLoc(FlagTok.getLocation()).first;
1515 PresumedLoc PLoc = SM.getPresumedLoc(FlagTok.getLocation());
1516 if (PLoc.isInvalid())
1517 return true;
1519 // If there is no include loc (main file) or if the include loc is in a
1520 // different physical file, then we aren't in a "1" line marker flag region.
1521 SourceLocation IncLoc = PLoc.getIncludeLoc();
1522 if (IncLoc.isInvalid() ||
1523 SM.getDecomposedExpansionLoc(IncLoc).first != CurFileID) {
1524 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_pop);
1525 PP.DiscardUntilEndOfDirective();
1526 return true;
1529 PP.Lex(FlagTok);
1530 if (FlagTok.is(tok::eod)) return false;
1531 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP))
1532 return true;
1535 // We must have 3 if there are still flags.
1536 if (FlagVal != 3) {
1537 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
1538 PP.DiscardUntilEndOfDirective();
1539 return true;
1542 FileKind = SrcMgr::C_System;
1544 PP.Lex(FlagTok);
1545 if (FlagTok.is(tok::eod)) return false;
1546 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP))
1547 return true;
1549 // We must have 4 if there is yet another flag.
1550 if (FlagVal != 4) {
1551 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
1552 PP.DiscardUntilEndOfDirective();
1553 return true;
1556 FileKind = SrcMgr::C_ExternCSystem;
1558 PP.Lex(FlagTok);
1559 if (FlagTok.is(tok::eod)) return false;
1561 // There are no more valid flags here.
1562 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
1563 PP.DiscardUntilEndOfDirective();
1564 return true;
1567 /// HandleDigitDirective - Handle a GNU line marker directive, whose syntax is
1568 /// one of the following forms:
1570 /// # 42
1571 /// # 42 "file" ('1' | '2')?
1572 /// # 42 "file" ('1' | '2')? '3' '4'?
1574 void Preprocessor::HandleDigitDirective(Token &DigitTok) {
1575 // Validate the number and convert it to an unsigned. GNU does not have a
1576 // line # limit other than it fit in 32-bits.
1577 unsigned LineNo;
1578 if (GetLineValue(DigitTok, LineNo, diag::err_pp_linemarker_requires_integer,
1579 *this, true))
1580 return;
1582 Token StrTok;
1583 Lex(StrTok);
1585 bool IsFileEntry = false, IsFileExit = false;
1586 int FilenameID = -1;
1587 SrcMgr::CharacteristicKind FileKind = SrcMgr::C_User;
1589 // If the StrTok is "eod", then it wasn't present. Otherwise, it must be a
1590 // string followed by eod.
1591 if (StrTok.is(tok::eod)) {
1592 Diag(StrTok, diag::ext_pp_gnu_line_directive);
1593 // Treat this like "#line NN", which doesn't change file characteristics.
1594 FileKind = SourceMgr.getFileCharacteristic(DigitTok.getLocation());
1595 } else if (StrTok.isNot(tok::string_literal)) {
1596 Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
1597 DiscardUntilEndOfDirective();
1598 return;
1599 } else if (StrTok.hasUDSuffix()) {
1600 Diag(StrTok, diag::err_invalid_string_udl);
1601 DiscardUntilEndOfDirective();
1602 return;
1603 } else {
1604 // Parse and validate the string, converting it into a unique ID.
1605 StringLiteralParser Literal(StrTok, *this);
1606 assert(Literal.isOrdinary() && "Didn't allow wide strings in");
1607 if (Literal.hadError) {
1608 DiscardUntilEndOfDirective();
1609 return;
1611 if (Literal.Pascal) {
1612 Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
1613 DiscardUntilEndOfDirective();
1614 return;
1617 // If a filename was present, read any flags that are present.
1618 if (ReadLineMarkerFlags(IsFileEntry, IsFileExit, FileKind, *this))
1619 return;
1620 if (!SourceMgr.isWrittenInBuiltinFile(DigitTok.getLocation()) &&
1621 !SourceMgr.isWrittenInCommandLineFile(DigitTok.getLocation()))
1622 Diag(StrTok, diag::ext_pp_gnu_line_directive);
1624 // Exiting to an empty string means pop to the including file, so leave
1625 // FilenameID as -1 in that case.
1626 if (!(IsFileExit && Literal.GetString().empty()))
1627 FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString());
1630 // Create a line note with this information.
1631 SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID, IsFileEntry,
1632 IsFileExit, FileKind);
1634 // If the preprocessor has callbacks installed, notify them of the #line
1635 // change. This is used so that the line marker comes out in -E mode for
1636 // example.
1637 if (Callbacks) {
1638 PPCallbacks::FileChangeReason Reason = PPCallbacks::RenameFile;
1639 if (IsFileEntry)
1640 Reason = PPCallbacks::EnterFile;
1641 else if (IsFileExit)
1642 Reason = PPCallbacks::ExitFile;
1644 Callbacks->FileChanged(CurPPLexer->getSourceLocation(), Reason, FileKind);
1648 /// HandleUserDiagnosticDirective - Handle a #warning or #error directive.
1650 void Preprocessor::HandleUserDiagnosticDirective(Token &Tok,
1651 bool isWarning) {
1652 // Read the rest of the line raw. We do this because we don't want macros
1653 // to be expanded and we don't require that the tokens be valid preprocessing
1654 // tokens. For example, this is allowed: "#warning ` 'foo". GCC does
1655 // collapse multiple consecutive white space between tokens, but this isn't
1656 // specified by the standard.
1657 SmallString<128> Message;
1658 CurLexer->ReadToEndOfLine(&Message);
1660 // Find the first non-whitespace character, so that we can make the
1661 // diagnostic more succinct.
1662 StringRef Msg = Message.str().ltrim(' ');
1664 if (isWarning)
1665 Diag(Tok, diag::pp_hash_warning) << Msg;
1666 else
1667 Diag(Tok, diag::err_pp_hash_error) << Msg;
1670 /// HandleIdentSCCSDirective - Handle a #ident/#sccs directive.
1672 void Preprocessor::HandleIdentSCCSDirective(Token &Tok) {
1673 // Yes, this directive is an extension.
1674 Diag(Tok, diag::ext_pp_ident_directive);
1676 // Read the string argument.
1677 Token StrTok;
1678 Lex(StrTok);
1680 // If the token kind isn't a string, it's a malformed directive.
1681 if (StrTok.isNot(tok::string_literal) &&
1682 StrTok.isNot(tok::wide_string_literal)) {
1683 Diag(StrTok, diag::err_pp_malformed_ident);
1684 if (StrTok.isNot(tok::eod))
1685 DiscardUntilEndOfDirective();
1686 return;
1689 if (StrTok.hasUDSuffix()) {
1690 Diag(StrTok, diag::err_invalid_string_udl);
1691 DiscardUntilEndOfDirective();
1692 return;
1695 // Verify that there is nothing after the string, other than EOD.
1696 CheckEndOfDirective("ident");
1698 if (Callbacks) {
1699 bool Invalid = false;
1700 std::string Str = getSpelling(StrTok, &Invalid);
1701 if (!Invalid)
1702 Callbacks->Ident(Tok.getLocation(), Str);
1706 /// Handle a #public directive.
1707 void Preprocessor::HandleMacroPublicDirective(Token &Tok) {
1708 Token MacroNameTok;
1709 ReadMacroName(MacroNameTok, MU_Undef);
1711 // Error reading macro name? If so, diagnostic already issued.
1712 if (MacroNameTok.is(tok::eod))
1713 return;
1715 // Check to see if this is the last token on the #__public_macro line.
1716 CheckEndOfDirective("__public_macro");
1718 IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
1719 // Okay, we finally have a valid identifier to undef.
1720 MacroDirective *MD = getLocalMacroDirective(II);
1722 // If the macro is not defined, this is an error.
1723 if (!MD) {
1724 Diag(MacroNameTok, diag::err_pp_visibility_non_macro) << II;
1725 return;
1728 // Note that this macro has now been exported.
1729 appendMacroDirective(II, AllocateVisibilityMacroDirective(
1730 MacroNameTok.getLocation(), /*isPublic=*/true));
1733 /// Handle a #private directive.
1734 void Preprocessor::HandleMacroPrivateDirective() {
1735 Token MacroNameTok;
1736 ReadMacroName(MacroNameTok, MU_Undef);
1738 // Error reading macro name? If so, diagnostic already issued.
1739 if (MacroNameTok.is(tok::eod))
1740 return;
1742 // Check to see if this is the last token on the #__private_macro line.
1743 CheckEndOfDirective("__private_macro");
1745 IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
1746 // Okay, we finally have a valid identifier to undef.
1747 MacroDirective *MD = getLocalMacroDirective(II);
1749 // If the macro is not defined, this is an error.
1750 if (!MD) {
1751 Diag(MacroNameTok, diag::err_pp_visibility_non_macro) << II;
1752 return;
1755 // Note that this macro has now been marked private.
1756 appendMacroDirective(II, AllocateVisibilityMacroDirective(
1757 MacroNameTok.getLocation(), /*isPublic=*/false));
1760 //===----------------------------------------------------------------------===//
1761 // Preprocessor Include Directive Handling.
1762 //===----------------------------------------------------------------------===//
1764 /// GetIncludeFilenameSpelling - Turn the specified lexer token into a fully
1765 /// checked and spelled filename, e.g. as an operand of \#include. This returns
1766 /// true if the input filename was in <>'s or false if it were in ""'s. The
1767 /// caller is expected to provide a buffer that is large enough to hold the
1768 /// spelling of the filename, but is also expected to handle the case when
1769 /// this method decides to use a different buffer.
1770 bool Preprocessor::GetIncludeFilenameSpelling(SourceLocation Loc,
1771 StringRef &Buffer) {
1772 // Get the text form of the filename.
1773 assert(!Buffer.empty() && "Can't have tokens with empty spellings!");
1775 // FIXME: Consider warning on some of the cases described in C11 6.4.7/3 and
1776 // C++20 [lex.header]/2:
1778 // If `"`, `'`, `\`, `/*`, or `//` appears in a header-name, then
1779 // in C: behavior is undefined
1780 // in C++: program is conditionally-supported with implementation-defined
1781 // semantics
1783 // Make sure the filename is <x> or "x".
1784 bool isAngled;
1785 if (Buffer[0] == '<') {
1786 if (Buffer.back() != '>') {
1787 Diag(Loc, diag::err_pp_expects_filename);
1788 Buffer = StringRef();
1789 return true;
1791 isAngled = true;
1792 } else if (Buffer[0] == '"') {
1793 if (Buffer.back() != '"') {
1794 Diag(Loc, diag::err_pp_expects_filename);
1795 Buffer = StringRef();
1796 return true;
1798 isAngled = false;
1799 } else {
1800 Diag(Loc, diag::err_pp_expects_filename);
1801 Buffer = StringRef();
1802 return true;
1805 // Diagnose #include "" as invalid.
1806 if (Buffer.size() <= 2) {
1807 Diag(Loc, diag::err_pp_empty_filename);
1808 Buffer = StringRef();
1809 return true;
1812 // Skip the brackets.
1813 Buffer = Buffer.substr(1, Buffer.size()-2);
1814 return isAngled;
1817 /// Push a token onto the token stream containing an annotation.
1818 void Preprocessor::EnterAnnotationToken(SourceRange Range,
1819 tok::TokenKind Kind,
1820 void *AnnotationVal) {
1821 // FIXME: Produce this as the current token directly, rather than
1822 // allocating a new token for it.
1823 auto Tok = std::make_unique<Token[]>(1);
1824 Tok[0].startToken();
1825 Tok[0].setKind(Kind);
1826 Tok[0].setLocation(Range.getBegin());
1827 Tok[0].setAnnotationEndLoc(Range.getEnd());
1828 Tok[0].setAnnotationValue(AnnotationVal);
1829 EnterTokenStream(std::move(Tok), 1, true, /*IsReinject*/ false);
1832 /// Produce a diagnostic informing the user that a #include or similar
1833 /// was implicitly treated as a module import.
1834 static void diagnoseAutoModuleImport(
1835 Preprocessor &PP, SourceLocation HashLoc, Token &IncludeTok,
1836 ArrayRef<std::pair<IdentifierInfo *, SourceLocation>> Path,
1837 SourceLocation PathEnd) {
1838 SmallString<128> PathString;
1839 for (size_t I = 0, N = Path.size(); I != N; ++I) {
1840 if (I)
1841 PathString += '.';
1842 PathString += Path[I].first->getName();
1845 int IncludeKind = 0;
1846 switch (IncludeTok.getIdentifierInfo()->getPPKeywordID()) {
1847 case tok::pp_include:
1848 IncludeKind = 0;
1849 break;
1851 case tok::pp_import:
1852 IncludeKind = 1;
1853 break;
1855 case tok::pp_include_next:
1856 IncludeKind = 2;
1857 break;
1859 case tok::pp___include_macros:
1860 IncludeKind = 3;
1861 break;
1863 default:
1864 llvm_unreachable("unknown include directive kind");
1867 PP.Diag(HashLoc, diag::remark_pp_include_directive_modular_translation)
1868 << IncludeKind << PathString;
1871 // Given a vector of path components and a string containing the real
1872 // path to the file, build a properly-cased replacement in the vector,
1873 // and return true if the replacement should be suggested.
1874 static bool trySimplifyPath(SmallVectorImpl<StringRef> &Components,
1875 StringRef RealPathName) {
1876 auto RealPathComponentIter = llvm::sys::path::rbegin(RealPathName);
1877 auto RealPathComponentEnd = llvm::sys::path::rend(RealPathName);
1878 int Cnt = 0;
1879 bool SuggestReplacement = false;
1880 // Below is a best-effort to handle ".." in paths. It is admittedly
1881 // not 100% correct in the presence of symlinks.
1882 for (auto &Component : llvm::reverse(Components)) {
1883 if ("." == Component) {
1884 } else if (".." == Component) {
1885 ++Cnt;
1886 } else if (Cnt) {
1887 --Cnt;
1888 } else if (RealPathComponentIter != RealPathComponentEnd) {
1889 if (Component != *RealPathComponentIter) {
1890 // If these path components differ by more than just case, then we
1891 // may be looking at symlinked paths. Bail on this diagnostic to avoid
1892 // noisy false positives.
1893 SuggestReplacement =
1894 RealPathComponentIter->equals_insensitive(Component);
1895 if (!SuggestReplacement)
1896 break;
1897 Component = *RealPathComponentIter;
1899 ++RealPathComponentIter;
1902 return SuggestReplacement;
1905 bool Preprocessor::checkModuleIsAvailable(const LangOptions &LangOpts,
1906 const TargetInfo &TargetInfo,
1907 const Module &M,
1908 DiagnosticsEngine &Diags) {
1909 Module::Requirement Requirement;
1910 Module::UnresolvedHeaderDirective MissingHeader;
1911 Module *ShadowingModule = nullptr;
1912 if (M.isAvailable(LangOpts, TargetInfo, Requirement, MissingHeader,
1913 ShadowingModule))
1914 return false;
1916 if (MissingHeader.FileNameLoc.isValid()) {
1917 Diags.Report(MissingHeader.FileNameLoc, diag::err_module_header_missing)
1918 << MissingHeader.IsUmbrella << MissingHeader.FileName;
1919 } else if (ShadowingModule) {
1920 Diags.Report(M.DefinitionLoc, diag::err_module_shadowed) << M.Name;
1921 Diags.Report(ShadowingModule->DefinitionLoc,
1922 diag::note_previous_definition);
1923 } else {
1924 // FIXME: Track the location at which the requirement was specified, and
1925 // use it here.
1926 Diags.Report(M.DefinitionLoc, diag::err_module_unavailable)
1927 << M.getFullModuleName() << Requirement.second << Requirement.first;
1929 return true;
1932 std::pair<ConstSearchDirIterator, const FileEntry *>
1933 Preprocessor::getIncludeNextStart(const Token &IncludeNextTok) const {
1934 // #include_next is like #include, except that we start searching after
1935 // the current found directory. If we can't do this, issue a
1936 // diagnostic.
1937 ConstSearchDirIterator Lookup = CurDirLookup;
1938 const FileEntry *LookupFromFile = nullptr;
1940 if (isInPrimaryFile() && LangOpts.IsHeaderFile) {
1941 // If the main file is a header, then it's either for PCH/AST generation,
1942 // or libclang opened it. Either way, handle it as a normal include below
1943 // and do not complain about include_next.
1944 } else if (isInPrimaryFile()) {
1945 Lookup = nullptr;
1946 Diag(IncludeNextTok, diag::pp_include_next_in_primary);
1947 } else if (CurLexerSubmodule) {
1948 // Start looking up in the directory *after* the one in which the current
1949 // file would be found, if any.
1950 assert(CurPPLexer && "#include_next directive in macro?");
1951 LookupFromFile = CurPPLexer->getFileEntry();
1952 Lookup = nullptr;
1953 } else if (!Lookup) {
1954 // The current file was not found by walking the include path. Either it
1955 // is the primary file (handled above), or it was found by absolute path,
1956 // or it was found relative to such a file.
1957 // FIXME: Track enough information so we know which case we're in.
1958 Diag(IncludeNextTok, diag::pp_include_next_absolute_path);
1959 } else {
1960 // Start looking up in the next directory.
1961 ++Lookup;
1964 return {Lookup, LookupFromFile};
1967 /// HandleIncludeDirective - The "\#include" tokens have just been read, read
1968 /// the file to be included from the lexer, then include it! This is a common
1969 /// routine with functionality shared between \#include, \#include_next and
1970 /// \#import. LookupFrom is set when this is a \#include_next directive, it
1971 /// specifies the file to start searching from.
1972 void Preprocessor::HandleIncludeDirective(SourceLocation HashLoc,
1973 Token &IncludeTok,
1974 ConstSearchDirIterator LookupFrom,
1975 const FileEntry *LookupFromFile) {
1976 Token FilenameTok;
1977 if (LexHeaderName(FilenameTok))
1978 return;
1980 if (FilenameTok.isNot(tok::header_name)) {
1981 Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
1982 if (FilenameTok.isNot(tok::eod))
1983 DiscardUntilEndOfDirective();
1984 return;
1987 // Verify that there is nothing after the filename, other than EOD. Note
1988 // that we allow macros that expand to nothing after the filename, because
1989 // this falls into the category of "#include pp-tokens new-line" specified
1990 // in C99 6.10.2p4.
1991 SourceLocation EndLoc =
1992 CheckEndOfDirective(IncludeTok.getIdentifierInfo()->getNameStart(), true);
1994 auto Action = HandleHeaderIncludeOrImport(HashLoc, IncludeTok, FilenameTok,
1995 EndLoc, LookupFrom, LookupFromFile);
1996 switch (Action.Kind) {
1997 case ImportAction::None:
1998 case ImportAction::SkippedModuleImport:
1999 break;
2000 case ImportAction::ModuleBegin:
2001 EnterAnnotationToken(SourceRange(HashLoc, EndLoc),
2002 tok::annot_module_begin, Action.ModuleForHeader);
2003 break;
2004 case ImportAction::HeaderUnitImport:
2005 EnterAnnotationToken(SourceRange(HashLoc, EndLoc), tok::annot_header_unit,
2006 Action.ModuleForHeader);
2007 break;
2008 case ImportAction::ModuleImport:
2009 EnterAnnotationToken(SourceRange(HashLoc, EndLoc),
2010 tok::annot_module_include, Action.ModuleForHeader);
2011 break;
2012 case ImportAction::Failure:
2013 assert(TheModuleLoader.HadFatalFailure &&
2014 "This should be an early exit only to a fatal error");
2015 TheModuleLoader.HadFatalFailure = true;
2016 IncludeTok.setKind(tok::eof);
2017 CurLexer->cutOffLexing();
2018 return;
2022 OptionalFileEntryRef Preprocessor::LookupHeaderIncludeOrImport(
2023 ConstSearchDirIterator *CurDir, StringRef &Filename,
2024 SourceLocation FilenameLoc, CharSourceRange FilenameRange,
2025 const Token &FilenameTok, bool &IsFrameworkFound, bool IsImportDecl,
2026 bool &IsMapped, ConstSearchDirIterator LookupFrom,
2027 const FileEntry *LookupFromFile, StringRef &LookupFilename,
2028 SmallVectorImpl<char> &RelativePath, SmallVectorImpl<char> &SearchPath,
2029 ModuleMap::KnownHeader &SuggestedModule, bool isAngled) {
2030 OptionalFileEntryRef File = LookupFile(
2031 FilenameLoc, LookupFilename, isAngled, LookupFrom, LookupFromFile, CurDir,
2032 Callbacks ? &SearchPath : nullptr, Callbacks ? &RelativePath : nullptr,
2033 &SuggestedModule, &IsMapped, &IsFrameworkFound);
2034 if (File)
2035 return File;
2037 // Give the clients a chance to silently skip this include.
2038 if (Callbacks && Callbacks->FileNotFound(Filename))
2039 return std::nullopt;
2041 if (SuppressIncludeNotFoundError)
2042 return std::nullopt;
2044 // If the file could not be located and it was included via angle
2045 // brackets, we can attempt a lookup as though it were a quoted path to
2046 // provide the user with a possible fixit.
2047 if (isAngled) {
2048 OptionalFileEntryRef File = LookupFile(
2049 FilenameLoc, LookupFilename, false, LookupFrom, LookupFromFile, CurDir,
2050 Callbacks ? &SearchPath : nullptr, Callbacks ? &RelativePath : nullptr,
2051 &SuggestedModule, &IsMapped,
2052 /*IsFrameworkFound=*/nullptr);
2053 if (File) {
2054 Diag(FilenameTok, diag::err_pp_file_not_found_angled_include_not_fatal)
2055 << Filename << IsImportDecl
2056 << FixItHint::CreateReplacement(FilenameRange,
2057 "\"" + Filename.str() + "\"");
2058 return File;
2062 // Check for likely typos due to leading or trailing non-isAlphanumeric
2063 // characters
2064 StringRef OriginalFilename = Filename;
2065 if (LangOpts.SpellChecking) {
2066 // A heuristic to correct a typo file name by removing leading and
2067 // trailing non-isAlphanumeric characters.
2068 auto CorrectTypoFilename = [](llvm::StringRef Filename) {
2069 Filename = Filename.drop_until(isAlphanumeric);
2070 while (!Filename.empty() && !isAlphanumeric(Filename.back())) {
2071 Filename = Filename.drop_back();
2073 return Filename;
2075 StringRef TypoCorrectionName = CorrectTypoFilename(Filename);
2076 StringRef TypoCorrectionLookupName = CorrectTypoFilename(LookupFilename);
2078 OptionalFileEntryRef File = LookupFile(
2079 FilenameLoc, TypoCorrectionLookupName, isAngled, LookupFrom,
2080 LookupFromFile, CurDir, Callbacks ? &SearchPath : nullptr,
2081 Callbacks ? &RelativePath : nullptr, &SuggestedModule, &IsMapped,
2082 /*IsFrameworkFound=*/nullptr);
2083 if (File) {
2084 auto Hint =
2085 isAngled ? FixItHint::CreateReplacement(
2086 FilenameRange, "<" + TypoCorrectionName.str() + ">")
2087 : FixItHint::CreateReplacement(
2088 FilenameRange, "\"" + TypoCorrectionName.str() + "\"");
2089 Diag(FilenameTok, diag::err_pp_file_not_found_typo_not_fatal)
2090 << OriginalFilename << TypoCorrectionName << Hint;
2091 // We found the file, so set the Filename to the name after typo
2092 // correction.
2093 Filename = TypoCorrectionName;
2094 LookupFilename = TypoCorrectionLookupName;
2095 return File;
2099 // If the file is still not found, just go with the vanilla diagnostic
2100 assert(!File && "expected missing file");
2101 Diag(FilenameTok, diag::err_pp_file_not_found)
2102 << OriginalFilename << FilenameRange;
2103 if (IsFrameworkFound) {
2104 size_t SlashPos = OriginalFilename.find('/');
2105 assert(SlashPos != StringRef::npos &&
2106 "Include with framework name should have '/' in the filename");
2107 StringRef FrameworkName = OriginalFilename.substr(0, SlashPos);
2108 FrameworkCacheEntry &CacheEntry =
2109 HeaderInfo.LookupFrameworkCache(FrameworkName);
2110 assert(CacheEntry.Directory && "Found framework should be in cache");
2111 Diag(FilenameTok, diag::note_pp_framework_without_header)
2112 << OriginalFilename.substr(SlashPos + 1) << FrameworkName
2113 << CacheEntry.Directory->getName();
2116 return std::nullopt;
2119 /// Handle either a #include-like directive or an import declaration that names
2120 /// a header file.
2122 /// \param HashLoc The location of the '#' token for an include, or
2123 /// SourceLocation() for an import declaration.
2124 /// \param IncludeTok The include / include_next / import token.
2125 /// \param FilenameTok The header-name token.
2126 /// \param EndLoc The location at which any imported macros become visible.
2127 /// \param LookupFrom For #include_next, the starting directory for the
2128 /// directory lookup.
2129 /// \param LookupFromFile For #include_next, the starting file for the directory
2130 /// lookup.
2131 Preprocessor::ImportAction Preprocessor::HandleHeaderIncludeOrImport(
2132 SourceLocation HashLoc, Token &IncludeTok, Token &FilenameTok,
2133 SourceLocation EndLoc, ConstSearchDirIterator LookupFrom,
2134 const FileEntry *LookupFromFile) {
2135 SmallString<128> FilenameBuffer;
2136 StringRef Filename = getSpelling(FilenameTok, FilenameBuffer);
2137 SourceLocation CharEnd = FilenameTok.getEndLoc();
2139 CharSourceRange FilenameRange
2140 = CharSourceRange::getCharRange(FilenameTok.getLocation(), CharEnd);
2141 StringRef OriginalFilename = Filename;
2142 bool isAngled =
2143 GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
2145 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
2146 // error.
2147 if (Filename.empty())
2148 return {ImportAction::None};
2150 bool IsImportDecl = HashLoc.isInvalid();
2151 SourceLocation StartLoc = IsImportDecl ? IncludeTok.getLocation() : HashLoc;
2153 // Complain about attempts to #include files in an audit pragma.
2154 if (PragmaARCCFCodeAuditedInfo.second.isValid()) {
2155 Diag(StartLoc, diag::err_pp_include_in_arc_cf_code_audited) << IsImportDecl;
2156 Diag(PragmaARCCFCodeAuditedInfo.second, diag::note_pragma_entered_here);
2158 // Immediately leave the pragma.
2159 PragmaARCCFCodeAuditedInfo = {nullptr, SourceLocation()};
2162 // Complain about attempts to #include files in an assume-nonnull pragma.
2163 if (PragmaAssumeNonNullLoc.isValid()) {
2164 Diag(StartLoc, diag::err_pp_include_in_assume_nonnull) << IsImportDecl;
2165 Diag(PragmaAssumeNonNullLoc, diag::note_pragma_entered_here);
2167 // Immediately leave the pragma.
2168 PragmaAssumeNonNullLoc = SourceLocation();
2171 if (HeaderInfo.HasIncludeAliasMap()) {
2172 // Map the filename with the brackets still attached. If the name doesn't
2173 // map to anything, fall back on the filename we've already gotten the
2174 // spelling for.
2175 StringRef NewName = HeaderInfo.MapHeaderToIncludeAlias(OriginalFilename);
2176 if (!NewName.empty())
2177 Filename = NewName;
2180 // Search include directories.
2181 bool IsMapped = false;
2182 bool IsFrameworkFound = false;
2183 ConstSearchDirIterator CurDir = nullptr;
2184 SmallString<1024> SearchPath;
2185 SmallString<1024> RelativePath;
2186 // We get the raw path only if we have 'Callbacks' to which we later pass
2187 // the path.
2188 ModuleMap::KnownHeader SuggestedModule;
2189 SourceLocation FilenameLoc = FilenameTok.getLocation();
2190 StringRef LookupFilename = Filename;
2192 // Normalize slashes when compiling with -fms-extensions on non-Windows. This
2193 // is unnecessary on Windows since the filesystem there handles backslashes.
2194 SmallString<128> NormalizedPath;
2195 llvm::sys::path::Style BackslashStyle = llvm::sys::path::Style::native;
2196 if (is_style_posix(BackslashStyle) && LangOpts.MicrosoftExt) {
2197 NormalizedPath = Filename.str();
2198 llvm::sys::path::native(NormalizedPath);
2199 LookupFilename = NormalizedPath;
2200 BackslashStyle = llvm::sys::path::Style::windows;
2203 OptionalFileEntryRef File = LookupHeaderIncludeOrImport(
2204 &CurDir, Filename, FilenameLoc, FilenameRange, FilenameTok,
2205 IsFrameworkFound, IsImportDecl, IsMapped, LookupFrom, LookupFromFile,
2206 LookupFilename, RelativePath, SearchPath, SuggestedModule, isAngled);
2208 if (usingPCHWithThroughHeader() && SkippingUntilPCHThroughHeader) {
2209 if (File && isPCHThroughHeader(&File->getFileEntry()))
2210 SkippingUntilPCHThroughHeader = false;
2211 return {ImportAction::None};
2214 // Should we enter the source file? Set to Skip if either the source file is
2215 // known to have no effect beyond its effect on module visibility -- that is,
2216 // if it's got an include guard that is already defined, set to Import if it
2217 // is a modular header we've already built and should import.
2219 // For C++20 Modules
2220 // [cpp.include]/7 If the header identified by the header-name denotes an
2221 // importable header, it is implementation-defined whether the #include
2222 // preprocessing directive is instead replaced by an import directive.
2223 // For this implementation, the translation is permitted when we are parsing
2224 // the Global Module Fragment, and not otherwise (the cases where it would be
2225 // valid to replace an include with an import are highly constrained once in
2226 // named module purview; this choice avoids considerable complexity in
2227 // determining valid cases).
2229 enum { Enter, Import, Skip, IncludeLimitReached } Action = Enter;
2231 if (PPOpts->SingleFileParseMode)
2232 Action = IncludeLimitReached;
2234 // If we've reached the max allowed include depth, it is usually due to an
2235 // include cycle. Don't enter already processed files again as it can lead to
2236 // reaching the max allowed include depth again.
2237 if (Action == Enter && HasReachedMaxIncludeDepth && File &&
2238 alreadyIncluded(*File))
2239 Action = IncludeLimitReached;
2241 // FIXME: We do not have a good way to disambiguate C++ clang modules from
2242 // C++ standard modules (other than use/non-use of Header Units).
2243 Module *SM = SuggestedModule.getModule();
2245 bool MaybeTranslateInclude =
2246 Action == Enter && File && SM && !SM->isForBuilding(getLangOpts());
2248 // Maybe a usable Header Unit
2249 bool UsableHeaderUnit = false;
2250 if (getLangOpts().CPlusPlusModules && SM && SM->isHeaderUnit()) {
2251 if (TrackGMFState.inGMF() || IsImportDecl)
2252 UsableHeaderUnit = true;
2253 else if (!IsImportDecl) {
2254 // This is a Header Unit that we do not include-translate
2255 SuggestedModule = ModuleMap::KnownHeader();
2256 SM = nullptr;
2259 // Maybe a usable clang header module.
2260 bool UsableClangHeaderModule =
2261 (getLangOpts().CPlusPlusModules || getLangOpts().Modules) && SM &&
2262 !SM->isHeaderUnit();
2264 // Determine whether we should try to import the module for this #include, if
2265 // there is one. Don't do so if precompiled module support is disabled or we
2266 // are processing this module textually (because we're building the module).
2267 if (MaybeTranslateInclude && (UsableHeaderUnit || UsableClangHeaderModule)) {
2268 // If this include corresponds to a module but that module is
2269 // unavailable, diagnose the situation and bail out.
2270 // FIXME: Remove this; loadModule does the same check (but produces
2271 // slightly worse diagnostics).
2272 if (checkModuleIsAvailable(getLangOpts(), getTargetInfo(),
2273 *SuggestedModule.getModule(),
2274 getDiagnostics())) {
2275 Diag(FilenameTok.getLocation(),
2276 diag::note_implicit_top_level_module_import_here)
2277 << SuggestedModule.getModule()->getTopLevelModuleName();
2278 return {ImportAction::None};
2281 // Compute the module access path corresponding to this module.
2282 // FIXME: Should we have a second loadModule() overload to avoid this
2283 // extra lookup step?
2284 SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Path;
2285 for (Module *Mod = SM; Mod; Mod = Mod->Parent)
2286 Path.push_back(std::make_pair(getIdentifierInfo(Mod->Name),
2287 FilenameTok.getLocation()));
2288 std::reverse(Path.begin(), Path.end());
2290 // Warn that we're replacing the include/import with a module import.
2291 if (!IsImportDecl)
2292 diagnoseAutoModuleImport(*this, StartLoc, IncludeTok, Path, CharEnd);
2294 // Load the module to import its macros. We'll make the declarations
2295 // visible when the parser gets here.
2296 // FIXME: Pass SuggestedModule in here rather than converting it to a path
2297 // and making the module loader convert it back again.
2298 ModuleLoadResult Imported = TheModuleLoader.loadModule(
2299 IncludeTok.getLocation(), Path, Module::Hidden,
2300 /*IsInclusionDirective=*/true);
2301 assert((Imported == nullptr || Imported == SuggestedModule.getModule()) &&
2302 "the imported module is different than the suggested one");
2304 if (Imported) {
2305 Action = Import;
2306 } else if (Imported.isMissingExpected()) {
2307 markClangModuleAsAffecting(
2308 static_cast<Module *>(Imported)->getTopLevelModule());
2309 // We failed to find a submodule that we assumed would exist (because it
2310 // was in the directory of an umbrella header, for instance), but no
2311 // actual module containing it exists (because the umbrella header is
2312 // incomplete). Treat this as a textual inclusion.
2313 SuggestedModule = ModuleMap::KnownHeader();
2314 SM = nullptr;
2315 } else if (Imported.isConfigMismatch()) {
2316 // On a configuration mismatch, enter the header textually. We still know
2317 // that it's part of the corresponding module.
2318 } else {
2319 // We hit an error processing the import. Bail out.
2320 if (hadModuleLoaderFatalFailure()) {
2321 // With a fatal failure in the module loader, we abort parsing.
2322 Token &Result = IncludeTok;
2323 assert(CurLexer && "#include but no current lexer set!");
2324 Result.startToken();
2325 CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd, tok::eof);
2326 CurLexer->cutOffLexing();
2328 return {ImportAction::None};
2332 // The #included file will be considered to be a system header if either it is
2333 // in a system include directory, or if the #includer is a system include
2334 // header.
2335 SrcMgr::CharacteristicKind FileCharacter =
2336 SourceMgr.getFileCharacteristic(FilenameTok.getLocation());
2337 if (File)
2338 FileCharacter = std::max(HeaderInfo.getFileDirFlavor(*File), FileCharacter);
2340 // If this is a '#import' or an import-declaration, don't re-enter the file.
2342 // FIXME: If we have a suggested module for a '#include', and we've already
2343 // visited this file, don't bother entering it again. We know it has no
2344 // further effect.
2345 bool EnterOnce =
2346 IsImportDecl ||
2347 IncludeTok.getIdentifierInfo()->getPPKeywordID() == tok::pp_import;
2349 bool IsFirstIncludeOfFile = false;
2351 // Ask HeaderInfo if we should enter this #include file. If not, #including
2352 // this file will have no effect.
2353 if (Action == Enter && File &&
2354 !HeaderInfo.ShouldEnterIncludeFile(*this, *File, EnterOnce,
2355 getLangOpts().Modules, SM,
2356 IsFirstIncludeOfFile)) {
2357 // C++ standard modules:
2358 // If we are not in the GMF, then we textually include only
2359 // clang modules:
2360 // Even if we've already preprocessed this header once and know that we
2361 // don't need to see its contents again, we still need to import it if it's
2362 // modular because we might not have imported it from this submodule before.
2364 // FIXME: We don't do this when compiling a PCH because the AST
2365 // serialization layer can't cope with it. This means we get local
2366 // submodule visibility semantics wrong in that case.
2367 if (UsableHeaderUnit && !getLangOpts().CompilingPCH)
2368 Action = TrackGMFState.inGMF() ? Import : Skip;
2369 else
2370 Action = (SuggestedModule && !getLangOpts().CompilingPCH) ? Import : Skip;
2373 // Check for circular inclusion of the main file.
2374 // We can't generate a consistent preamble with regard to the conditional
2375 // stack if the main file is included again as due to the preamble bounds
2376 // some directives (e.g. #endif of a header guard) will never be seen.
2377 // Since this will lead to confusing errors, avoid the inclusion.
2378 if (Action == Enter && File && PreambleConditionalStack.isRecording() &&
2379 SourceMgr.isMainFile(File->getFileEntry())) {
2380 Diag(FilenameTok.getLocation(),
2381 diag::err_pp_including_mainfile_in_preamble);
2382 return {ImportAction::None};
2385 if (Callbacks && !IsImportDecl) {
2386 // Notify the callback object that we've seen an inclusion directive.
2387 // FIXME: Use a different callback for a pp-import?
2388 Callbacks->InclusionDirective(HashLoc, IncludeTok, LookupFilename, isAngled,
2389 FilenameRange, File, SearchPath, RelativePath,
2390 Action == Import ? SuggestedModule.getModule()
2391 : nullptr,
2392 FileCharacter);
2393 if (Action == Skip && File)
2394 Callbacks->FileSkipped(*File, FilenameTok, FileCharacter);
2397 if (!File)
2398 return {ImportAction::None};
2400 // If this is a C++20 pp-import declaration, diagnose if we didn't find any
2401 // module corresponding to the named header.
2402 if (IsImportDecl && !SuggestedModule) {
2403 Diag(FilenameTok, diag::err_header_import_not_header_unit)
2404 << OriginalFilename << File->getName();
2405 return {ImportAction::None};
2408 // Issue a diagnostic if the name of the file on disk has a different case
2409 // than the one we're about to open.
2410 const bool CheckIncludePathPortability =
2411 !IsMapped && !File->getFileEntry().tryGetRealPathName().empty();
2413 if (CheckIncludePathPortability) {
2414 StringRef Name = LookupFilename;
2415 StringRef NameWithoriginalSlashes = Filename;
2416 #if defined(_WIN32)
2417 // Skip UNC prefix if present. (tryGetRealPathName() always
2418 // returns a path with the prefix skipped.)
2419 bool NameWasUNC = Name.consume_front("\\\\?\\");
2420 NameWithoriginalSlashes.consume_front("\\\\?\\");
2421 #endif
2422 StringRef RealPathName = File->getFileEntry().tryGetRealPathName();
2423 SmallVector<StringRef, 16> Components(llvm::sys::path::begin(Name),
2424 llvm::sys::path::end(Name));
2425 #if defined(_WIN32)
2426 // -Wnonportable-include-path is designed to diagnose includes using
2427 // case even on systems with a case-insensitive file system.
2428 // On Windows, RealPathName always starts with an upper-case drive
2429 // letter for absolute paths, but Name might start with either
2430 // case depending on if `cd c:\foo` or `cd C:\foo` was used in the shell.
2431 // ("foo" will always have on-disk case, no matter which case was
2432 // used in the cd command). To not emit this warning solely for
2433 // the drive letter, whose case is dependent on if `cd` is used
2434 // with upper- or lower-case drive letters, always consider the
2435 // given drive letter case as correct for the purpose of this warning.
2436 SmallString<128> FixedDriveRealPath;
2437 if (llvm::sys::path::is_absolute(Name) &&
2438 llvm::sys::path::is_absolute(RealPathName) &&
2439 toLowercase(Name[0]) == toLowercase(RealPathName[0]) &&
2440 isLowercase(Name[0]) != isLowercase(RealPathName[0])) {
2441 assert(Components.size() >= 3 && "should have drive, backslash, name");
2442 assert(Components[0].size() == 2 && "should start with drive");
2443 assert(Components[0][1] == ':' && "should have colon");
2444 FixedDriveRealPath = (Name.substr(0, 1) + RealPathName.substr(1)).str();
2445 RealPathName = FixedDriveRealPath;
2447 #endif
2449 if (trySimplifyPath(Components, RealPathName)) {
2450 SmallString<128> Path;
2451 Path.reserve(Name.size()+2);
2452 Path.push_back(isAngled ? '<' : '"');
2454 const auto IsSep = [BackslashStyle](char c) {
2455 return llvm::sys::path::is_separator(c, BackslashStyle);
2458 for (auto Component : Components) {
2459 // On POSIX, Components will contain a single '/' as first element
2460 // exactly if Name is an absolute path.
2461 // On Windows, it will contain "C:" followed by '\' for absolute paths.
2462 // The drive letter is optional for absolute paths on Windows, but
2463 // clang currently cannot process absolute paths in #include lines that
2464 // don't have a drive.
2465 // If the first entry in Components is a directory separator,
2466 // then the code at the bottom of this loop that keeps the original
2467 // directory separator style copies it. If the second entry is
2468 // a directory separator (the C:\ case), then that separator already
2469 // got copied when the C: was processed and we want to skip that entry.
2470 if (!(Component.size() == 1 && IsSep(Component[0])))
2471 Path.append(Component);
2472 else if (!Path.empty())
2473 continue;
2475 // Append the separator(s) the user used, or the close quote
2476 if (Path.size() > NameWithoriginalSlashes.size()) {
2477 Path.push_back(isAngled ? '>' : '"');
2478 continue;
2480 assert(IsSep(NameWithoriginalSlashes[Path.size()-1]));
2482 Path.push_back(NameWithoriginalSlashes[Path.size()-1]);
2483 while (Path.size() <= NameWithoriginalSlashes.size() &&
2484 IsSep(NameWithoriginalSlashes[Path.size()-1]));
2487 #if defined(_WIN32)
2488 // Restore UNC prefix if it was there.
2489 if (NameWasUNC)
2490 Path = (Path.substr(0, 1) + "\\\\?\\" + Path.substr(1)).str();
2491 #endif
2493 // For user files and known standard headers, issue a diagnostic.
2494 // For other system headers, don't. They can be controlled separately.
2495 auto DiagId =
2496 (FileCharacter == SrcMgr::C_User || warnByDefaultOnWrongCase(Name))
2497 ? diag::pp_nonportable_path
2498 : diag::pp_nonportable_system_path;
2499 Diag(FilenameTok, DiagId) << Path <<
2500 FixItHint::CreateReplacement(FilenameRange, Path);
2504 switch (Action) {
2505 case Skip:
2506 // If we don't need to enter the file, stop now.
2507 if (SM)
2508 return {ImportAction::SkippedModuleImport, SM};
2509 return {ImportAction::None};
2511 case IncludeLimitReached:
2512 // If we reached our include limit and don't want to enter any more files,
2513 // don't go any further.
2514 return {ImportAction::None};
2516 case Import: {
2517 // If this is a module import, make it visible if needed.
2518 assert(SM && "no module to import");
2520 makeModuleVisible(SM, EndLoc);
2522 if (IncludeTok.getIdentifierInfo()->getPPKeywordID() ==
2523 tok::pp___include_macros)
2524 return {ImportAction::None};
2526 return {ImportAction::ModuleImport, SM};
2529 case Enter:
2530 break;
2533 // Check that we don't have infinite #include recursion.
2534 if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1) {
2535 Diag(FilenameTok, diag::err_pp_include_too_deep);
2536 HasReachedMaxIncludeDepth = true;
2537 return {ImportAction::None};
2540 if (isAngled && isInNamedModule())
2541 Diag(FilenameTok, diag::warn_pp_include_angled_in_module_purview)
2542 << getNamedModuleName();
2544 // Look up the file, create a File ID for it.
2545 SourceLocation IncludePos = FilenameTok.getLocation();
2546 // If the filename string was the result of macro expansions, set the include
2547 // position on the file where it will be included and after the expansions.
2548 if (IncludePos.isMacroID())
2549 IncludePos = SourceMgr.getExpansionRange(IncludePos).getEnd();
2550 FileID FID = SourceMgr.createFileID(*File, IncludePos, FileCharacter);
2551 if (!FID.isValid()) {
2552 TheModuleLoader.HadFatalFailure = true;
2553 return ImportAction::Failure;
2556 // If all is good, enter the new file!
2557 if (EnterSourceFile(FID, CurDir, FilenameTok.getLocation(),
2558 IsFirstIncludeOfFile))
2559 return {ImportAction::None};
2561 // Determine if we're switching to building a new submodule, and which one.
2562 // This does not apply for C++20 modules header units.
2563 if (SM && !SM->isHeaderUnit()) {
2564 if (SM->getTopLevelModule()->ShadowingModule) {
2565 // We are building a submodule that belongs to a shadowed module. This
2566 // means we find header files in the shadowed module.
2567 Diag(SM->DefinitionLoc, diag::err_module_build_shadowed_submodule)
2568 << SM->getFullModuleName();
2569 Diag(SM->getTopLevelModule()->ShadowingModule->DefinitionLoc,
2570 diag::note_previous_definition);
2571 return {ImportAction::None};
2573 // When building a pch, -fmodule-name tells the compiler to textually
2574 // include headers in the specified module. We are not building the
2575 // specified module.
2577 // FIXME: This is the wrong way to handle this. We should produce a PCH
2578 // that behaves the same as the header would behave in a compilation using
2579 // that PCH, which means we should enter the submodule. We need to teach
2580 // the AST serialization layer to deal with the resulting AST.
2581 if (getLangOpts().CompilingPCH && SM->isForBuilding(getLangOpts()))
2582 return {ImportAction::None};
2584 assert(!CurLexerSubmodule && "should not have marked this as a module yet");
2585 CurLexerSubmodule = SM;
2587 // Let the macro handling code know that any future macros are within
2588 // the new submodule.
2589 EnterSubmodule(SM, EndLoc, /*ForPragma*/ false);
2591 // Let the parser know that any future declarations are within the new
2592 // submodule.
2593 // FIXME: There's no point doing this if we're handling a #__include_macros
2594 // directive.
2595 return {ImportAction::ModuleBegin, SM};
2598 assert(!IsImportDecl && "failed to diagnose missing module for import decl");
2599 return {ImportAction::None};
2602 /// HandleIncludeNextDirective - Implements \#include_next.
2604 void Preprocessor::HandleIncludeNextDirective(SourceLocation HashLoc,
2605 Token &IncludeNextTok) {
2606 Diag(IncludeNextTok, diag::ext_pp_include_next_directive);
2608 ConstSearchDirIterator Lookup = nullptr;
2609 const FileEntry *LookupFromFile;
2610 std::tie(Lookup, LookupFromFile) = getIncludeNextStart(IncludeNextTok);
2612 return HandleIncludeDirective(HashLoc, IncludeNextTok, Lookup,
2613 LookupFromFile);
2616 /// HandleMicrosoftImportDirective - Implements \#import for Microsoft Mode
2617 void Preprocessor::HandleMicrosoftImportDirective(Token &Tok) {
2618 // The Microsoft #import directive takes a type library and generates header
2619 // files from it, and includes those. This is beyond the scope of what clang
2620 // does, so we ignore it and error out. However, #import can optionally have
2621 // trailing attributes that span multiple lines. We're going to eat those
2622 // so we can continue processing from there.
2623 Diag(Tok, diag::err_pp_import_directive_ms );
2625 // Read tokens until we get to the end of the directive. Note that the
2626 // directive can be split over multiple lines using the backslash character.
2627 DiscardUntilEndOfDirective();
2630 /// HandleImportDirective - Implements \#import.
2632 void Preprocessor::HandleImportDirective(SourceLocation HashLoc,
2633 Token &ImportTok) {
2634 if (!LangOpts.ObjC) { // #import is standard for ObjC.
2635 if (LangOpts.MSVCCompat)
2636 return HandleMicrosoftImportDirective(ImportTok);
2637 Diag(ImportTok, diag::ext_pp_import_directive);
2639 return HandleIncludeDirective(HashLoc, ImportTok);
2642 /// HandleIncludeMacrosDirective - The -imacros command line option turns into a
2643 /// pseudo directive in the predefines buffer. This handles it by sucking all
2644 /// tokens through the preprocessor and discarding them (only keeping the side
2645 /// effects on the preprocessor).
2646 void Preprocessor::HandleIncludeMacrosDirective(SourceLocation HashLoc,
2647 Token &IncludeMacrosTok) {
2648 // This directive should only occur in the predefines buffer. If not, emit an
2649 // error and reject it.
2650 SourceLocation Loc = IncludeMacrosTok.getLocation();
2651 if (SourceMgr.getBufferName(Loc) != "<built-in>") {
2652 Diag(IncludeMacrosTok.getLocation(),
2653 diag::pp_include_macros_out_of_predefines);
2654 DiscardUntilEndOfDirective();
2655 return;
2658 // Treat this as a normal #include for checking purposes. If this is
2659 // successful, it will push a new lexer onto the include stack.
2660 HandleIncludeDirective(HashLoc, IncludeMacrosTok);
2662 Token TmpTok;
2663 do {
2664 Lex(TmpTok);
2665 assert(TmpTok.isNot(tok::eof) && "Didn't find end of -imacros!");
2666 } while (TmpTok.isNot(tok::hashhash));
2669 //===----------------------------------------------------------------------===//
2670 // Preprocessor Macro Directive Handling.
2671 //===----------------------------------------------------------------------===//
2673 /// ReadMacroParameterList - The ( starting a parameter list of a macro
2674 /// definition has just been read. Lex the rest of the parameters and the
2675 /// closing ), updating MI with what we learn. Return true if an error occurs
2676 /// parsing the param list.
2677 bool Preprocessor::ReadMacroParameterList(MacroInfo *MI, Token &Tok) {
2678 SmallVector<IdentifierInfo*, 32> Parameters;
2680 while (true) {
2681 LexUnexpandedNonComment(Tok);
2682 switch (Tok.getKind()) {
2683 case tok::r_paren:
2684 // Found the end of the parameter list.
2685 if (Parameters.empty()) // #define FOO()
2686 return false;
2687 // Otherwise we have #define FOO(A,)
2688 Diag(Tok, diag::err_pp_expected_ident_in_arg_list);
2689 return true;
2690 case tok::ellipsis: // #define X(... -> C99 varargs
2691 if (!LangOpts.C99)
2692 Diag(Tok, LangOpts.CPlusPlus11 ?
2693 diag::warn_cxx98_compat_variadic_macro :
2694 diag::ext_variadic_macro);
2696 // OpenCL v1.2 s6.9.e: variadic macros are not supported.
2697 if (LangOpts.OpenCL && !LangOpts.OpenCLCPlusPlus) {
2698 Diag(Tok, diag::ext_pp_opencl_variadic_macros);
2701 // Lex the token after the identifier.
2702 LexUnexpandedNonComment(Tok);
2703 if (Tok.isNot(tok::r_paren)) {
2704 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
2705 return true;
2707 // Add the __VA_ARGS__ identifier as a parameter.
2708 Parameters.push_back(Ident__VA_ARGS__);
2709 MI->setIsC99Varargs();
2710 MI->setParameterList(Parameters, BP);
2711 return false;
2712 case tok::eod: // #define X(
2713 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
2714 return true;
2715 default:
2716 // Handle keywords and identifiers here to accept things like
2717 // #define Foo(for) for.
2718 IdentifierInfo *II = Tok.getIdentifierInfo();
2719 if (!II) {
2720 // #define X(1
2721 Diag(Tok, diag::err_pp_invalid_tok_in_arg_list);
2722 return true;
2725 // If this is already used as a parameter, it is used multiple times (e.g.
2726 // #define X(A,A.
2727 if (llvm::is_contained(Parameters, II)) { // C99 6.10.3p6
2728 Diag(Tok, diag::err_pp_duplicate_name_in_arg_list) << II;
2729 return true;
2732 // Add the parameter to the macro info.
2733 Parameters.push_back(II);
2735 // Lex the token after the identifier.
2736 LexUnexpandedNonComment(Tok);
2738 switch (Tok.getKind()) {
2739 default: // #define X(A B
2740 Diag(Tok, diag::err_pp_expected_comma_in_arg_list);
2741 return true;
2742 case tok::r_paren: // #define X(A)
2743 MI->setParameterList(Parameters, BP);
2744 return false;
2745 case tok::comma: // #define X(A,
2746 break;
2747 case tok::ellipsis: // #define X(A... -> GCC extension
2748 // Diagnose extension.
2749 Diag(Tok, diag::ext_named_variadic_macro);
2751 // Lex the token after the identifier.
2752 LexUnexpandedNonComment(Tok);
2753 if (Tok.isNot(tok::r_paren)) {
2754 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
2755 return true;
2758 MI->setIsGNUVarargs();
2759 MI->setParameterList(Parameters, BP);
2760 return false;
2766 static bool isConfigurationPattern(Token &MacroName, MacroInfo *MI,
2767 const LangOptions &LOptions) {
2768 if (MI->getNumTokens() == 1) {
2769 const Token &Value = MI->getReplacementToken(0);
2771 // Macro that is identity, like '#define inline inline' is a valid pattern.
2772 if (MacroName.getKind() == Value.getKind())
2773 return true;
2775 // Macro that maps a keyword to the same keyword decorated with leading/
2776 // trailing underscores is a valid pattern:
2777 // #define inline __inline
2778 // #define inline __inline__
2779 // #define inline _inline (in MS compatibility mode)
2780 StringRef MacroText = MacroName.getIdentifierInfo()->getName();
2781 if (IdentifierInfo *II = Value.getIdentifierInfo()) {
2782 if (!II->isKeyword(LOptions))
2783 return false;
2784 StringRef ValueText = II->getName();
2785 StringRef TrimmedValue = ValueText;
2786 if (!ValueText.startswith("__")) {
2787 if (ValueText.startswith("_"))
2788 TrimmedValue = TrimmedValue.drop_front(1);
2789 else
2790 return false;
2791 } else {
2792 TrimmedValue = TrimmedValue.drop_front(2);
2793 if (TrimmedValue.endswith("__"))
2794 TrimmedValue = TrimmedValue.drop_back(2);
2796 return TrimmedValue.equals(MacroText);
2797 } else {
2798 return false;
2802 // #define inline
2803 return MacroName.isOneOf(tok::kw_extern, tok::kw_inline, tok::kw_static,
2804 tok::kw_const) &&
2805 MI->getNumTokens() == 0;
2808 // ReadOptionalMacroParameterListAndBody - This consumes all (i.e. the
2809 // entire line) of the macro's tokens and adds them to MacroInfo, and while
2810 // doing so performs certain validity checks including (but not limited to):
2811 // - # (stringization) is followed by a macro parameter
2813 // Returns a nullptr if an invalid sequence of tokens is encountered or returns
2814 // a pointer to a MacroInfo object.
2816 MacroInfo *Preprocessor::ReadOptionalMacroParameterListAndBody(
2817 const Token &MacroNameTok, const bool ImmediatelyAfterHeaderGuard) {
2819 Token LastTok = MacroNameTok;
2820 // Create the new macro.
2821 MacroInfo *const MI = AllocateMacroInfo(MacroNameTok.getLocation());
2823 Token Tok;
2824 LexUnexpandedToken(Tok);
2826 // Ensure we consume the rest of the macro body if errors occur.
2827 auto _ = llvm::make_scope_exit([&]() {
2828 // The flag indicates if we are still waiting for 'eod'.
2829 if (CurLexer->ParsingPreprocessorDirective)
2830 DiscardUntilEndOfDirective();
2833 // Used to un-poison and then re-poison identifiers of the __VA_ARGS__ ilk
2834 // within their appropriate context.
2835 VariadicMacroScopeGuard VariadicMacroScopeGuard(*this);
2837 // If this is a function-like macro definition, parse the argument list,
2838 // marking each of the identifiers as being used as macro arguments. Also,
2839 // check other constraints on the first token of the macro body.
2840 if (Tok.is(tok::eod)) {
2841 if (ImmediatelyAfterHeaderGuard) {
2842 // Save this macro information since it may part of a header guard.
2843 CurPPLexer->MIOpt.SetDefinedMacro(MacroNameTok.getIdentifierInfo(),
2844 MacroNameTok.getLocation());
2846 // If there is no body to this macro, we have no special handling here.
2847 } else if (Tok.hasLeadingSpace()) {
2848 // This is a normal token with leading space. Clear the leading space
2849 // marker on the first token to get proper expansion.
2850 Tok.clearFlag(Token::LeadingSpace);
2851 } else if (Tok.is(tok::l_paren)) {
2852 // This is a function-like macro definition. Read the argument list.
2853 MI->setIsFunctionLike();
2854 if (ReadMacroParameterList(MI, LastTok))
2855 return nullptr;
2857 // If this is a definition of an ISO C/C++ variadic function-like macro (not
2858 // using the GNU named varargs extension) inform our variadic scope guard
2859 // which un-poisons and re-poisons certain identifiers (e.g. __VA_ARGS__)
2860 // allowed only within the definition of a variadic macro.
2862 if (MI->isC99Varargs()) {
2863 VariadicMacroScopeGuard.enterScope();
2866 // Read the first token after the arg list for down below.
2867 LexUnexpandedToken(Tok);
2868 } else if (LangOpts.C99 || LangOpts.CPlusPlus11) {
2869 // C99 requires whitespace between the macro definition and the body. Emit
2870 // a diagnostic for something like "#define X+".
2871 Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name);
2872 } else {
2873 // C90 6.8 TC1 says: "In the definition of an object-like macro, if the
2874 // first character of a replacement list is not a character required by
2875 // subclause 5.2.1, then there shall be white-space separation between the
2876 // identifier and the replacement list.". 5.2.1 lists this set:
2877 // "A-Za-z0-9!"#%&'()*+,_./:;<=>?[\]^_{|}~" as well as whitespace, which
2878 // is irrelevant here.
2879 bool isInvalid = false;
2880 if (Tok.is(tok::at)) // @ is not in the list above.
2881 isInvalid = true;
2882 else if (Tok.is(tok::unknown)) {
2883 // If we have an unknown token, it is something strange like "`". Since
2884 // all of valid characters would have lexed into a single character
2885 // token of some sort, we know this is not a valid case.
2886 isInvalid = true;
2888 if (isInvalid)
2889 Diag(Tok, diag::ext_missing_whitespace_after_macro_name);
2890 else
2891 Diag(Tok, diag::warn_missing_whitespace_after_macro_name);
2894 if (!Tok.is(tok::eod))
2895 LastTok = Tok;
2897 SmallVector<Token, 16> Tokens;
2899 // Read the rest of the macro body.
2900 if (MI->isObjectLike()) {
2901 // Object-like macros are very simple, just read their body.
2902 while (Tok.isNot(tok::eod)) {
2903 LastTok = Tok;
2904 Tokens.push_back(Tok);
2905 // Get the next token of the macro.
2906 LexUnexpandedToken(Tok);
2908 } else {
2909 // Otherwise, read the body of a function-like macro. While we are at it,
2910 // check C99 6.10.3.2p1: ensure that # operators are followed by macro
2911 // parameters in function-like macro expansions.
2913 VAOptDefinitionContext VAOCtx(*this);
2915 while (Tok.isNot(tok::eod)) {
2916 LastTok = Tok;
2918 if (!Tok.isOneOf(tok::hash, tok::hashat, tok::hashhash)) {
2919 Tokens.push_back(Tok);
2921 if (VAOCtx.isVAOptToken(Tok)) {
2922 // If we're already within a VAOPT, emit an error.
2923 if (VAOCtx.isInVAOpt()) {
2924 Diag(Tok, diag::err_pp_vaopt_nested_use);
2925 return nullptr;
2927 // Ensure VAOPT is followed by a '(' .
2928 LexUnexpandedToken(Tok);
2929 if (Tok.isNot(tok::l_paren)) {
2930 Diag(Tok, diag::err_pp_missing_lparen_in_vaopt_use);
2931 return nullptr;
2933 Tokens.push_back(Tok);
2934 VAOCtx.sawVAOptFollowedByOpeningParens(Tok.getLocation());
2935 LexUnexpandedToken(Tok);
2936 if (Tok.is(tok::hashhash)) {
2937 Diag(Tok, diag::err_vaopt_paste_at_start);
2938 return nullptr;
2940 continue;
2941 } else if (VAOCtx.isInVAOpt()) {
2942 if (Tok.is(tok::r_paren)) {
2943 if (VAOCtx.sawClosingParen()) {
2944 assert(Tokens.size() >= 3 &&
2945 "Must have seen at least __VA_OPT__( "
2946 "and a subsequent tok::r_paren");
2947 if (Tokens[Tokens.size() - 2].is(tok::hashhash)) {
2948 Diag(Tok, diag::err_vaopt_paste_at_end);
2949 return nullptr;
2952 } else if (Tok.is(tok::l_paren)) {
2953 VAOCtx.sawOpeningParen(Tok.getLocation());
2956 // Get the next token of the macro.
2957 LexUnexpandedToken(Tok);
2958 continue;
2961 // If we're in -traditional mode, then we should ignore stringification
2962 // and token pasting. Mark the tokens as unknown so as not to confuse
2963 // things.
2964 if (getLangOpts().TraditionalCPP) {
2965 Tok.setKind(tok::unknown);
2966 Tokens.push_back(Tok);
2968 // Get the next token of the macro.
2969 LexUnexpandedToken(Tok);
2970 continue;
2973 if (Tok.is(tok::hashhash)) {
2974 // If we see token pasting, check if it looks like the gcc comma
2975 // pasting extension. We'll use this information to suppress
2976 // diagnostics later on.
2978 // Get the next token of the macro.
2979 LexUnexpandedToken(Tok);
2981 if (Tok.is(tok::eod)) {
2982 Tokens.push_back(LastTok);
2983 break;
2986 if (!Tokens.empty() && Tok.getIdentifierInfo() == Ident__VA_ARGS__ &&
2987 Tokens[Tokens.size() - 1].is(tok::comma))
2988 MI->setHasCommaPasting();
2990 // Things look ok, add the '##' token to the macro.
2991 Tokens.push_back(LastTok);
2992 continue;
2995 // Our Token is a stringization operator.
2996 // Get the next token of the macro.
2997 LexUnexpandedToken(Tok);
2999 // Check for a valid macro arg identifier or __VA_OPT__.
3000 if (!VAOCtx.isVAOptToken(Tok) &&
3001 (Tok.getIdentifierInfo() == nullptr ||
3002 MI->getParameterNum(Tok.getIdentifierInfo()) == -1)) {
3004 // If this is assembler-with-cpp mode, we accept random gibberish after
3005 // the '#' because '#' is often a comment character. However, change
3006 // the kind of the token to tok::unknown so that the preprocessor isn't
3007 // confused.
3008 if (getLangOpts().AsmPreprocessor && Tok.isNot(tok::eod)) {
3009 LastTok.setKind(tok::unknown);
3010 Tokens.push_back(LastTok);
3011 continue;
3012 } else {
3013 Diag(Tok, diag::err_pp_stringize_not_parameter)
3014 << LastTok.is(tok::hashat);
3015 return nullptr;
3019 // Things look ok, add the '#' and param name tokens to the macro.
3020 Tokens.push_back(LastTok);
3022 // If the token following '#' is VAOPT, let the next iteration handle it
3023 // and check it for correctness, otherwise add the token and prime the
3024 // loop with the next one.
3025 if (!VAOCtx.isVAOptToken(Tok)) {
3026 Tokens.push_back(Tok);
3027 LastTok = Tok;
3029 // Get the next token of the macro.
3030 LexUnexpandedToken(Tok);
3033 if (VAOCtx.isInVAOpt()) {
3034 assert(Tok.is(tok::eod) && "Must be at End Of preprocessing Directive");
3035 Diag(Tok, diag::err_pp_expected_after)
3036 << LastTok.getKind() << tok::r_paren;
3037 Diag(VAOCtx.getUnmatchedOpeningParenLoc(), diag::note_matching) << tok::l_paren;
3038 return nullptr;
3041 MI->setDefinitionEndLoc(LastTok.getLocation());
3043 MI->setTokens(Tokens, BP);
3044 return MI;
3047 static bool isObjCProtectedMacro(const IdentifierInfo *II) {
3048 return II->isStr("__strong") || II->isStr("__weak") ||
3049 II->isStr("__unsafe_unretained") || II->isStr("__autoreleasing");
3052 /// HandleDefineDirective - Implements \#define. This consumes the entire macro
3053 /// line then lets the caller lex the next real token.
3054 void Preprocessor::HandleDefineDirective(
3055 Token &DefineTok, const bool ImmediatelyAfterHeaderGuard) {
3056 ++NumDefined;
3058 Token MacroNameTok;
3059 bool MacroShadowsKeyword;
3060 ReadMacroName(MacroNameTok, MU_Define, &MacroShadowsKeyword);
3062 // Error reading macro name? If so, diagnostic already issued.
3063 if (MacroNameTok.is(tok::eod))
3064 return;
3066 IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
3067 // Issue a final pragma warning if we're defining a macro that was has been
3068 // undefined and is being redefined.
3069 if (!II->hasMacroDefinition() && II->hadMacroDefinition() && II->isFinal())
3070 emitFinalMacroWarning(MacroNameTok, /*IsUndef=*/false);
3072 // If we are supposed to keep comments in #defines, reenable comment saving
3073 // mode.
3074 if (CurLexer) CurLexer->SetCommentRetentionState(KeepMacroComments);
3076 MacroInfo *const MI = ReadOptionalMacroParameterListAndBody(
3077 MacroNameTok, ImmediatelyAfterHeaderGuard);
3079 if (!MI) return;
3081 if (MacroShadowsKeyword &&
3082 !isConfigurationPattern(MacroNameTok, MI, getLangOpts())) {
3083 Diag(MacroNameTok, diag::warn_pp_macro_hides_keyword);
3085 // Check that there is no paste (##) operator at the beginning or end of the
3086 // replacement list.
3087 unsigned NumTokens = MI->getNumTokens();
3088 if (NumTokens != 0) {
3089 if (MI->getReplacementToken(0).is(tok::hashhash)) {
3090 Diag(MI->getReplacementToken(0), diag::err_paste_at_start);
3091 return;
3093 if (MI->getReplacementToken(NumTokens-1).is(tok::hashhash)) {
3094 Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end);
3095 return;
3099 // When skipping just warn about macros that do not match.
3100 if (SkippingUntilPCHThroughHeader) {
3101 const MacroInfo *OtherMI = getMacroInfo(MacroNameTok.getIdentifierInfo());
3102 if (!OtherMI || !MI->isIdenticalTo(*OtherMI, *this,
3103 /*Syntactic=*/LangOpts.MicrosoftExt))
3104 Diag(MI->getDefinitionLoc(), diag::warn_pp_macro_def_mismatch_with_pch)
3105 << MacroNameTok.getIdentifierInfo();
3106 // Issue the diagnostic but allow the change if msvc extensions are enabled
3107 if (!LangOpts.MicrosoftExt)
3108 return;
3111 // Finally, if this identifier already had a macro defined for it, verify that
3112 // the macro bodies are identical, and issue diagnostics if they are not.
3113 if (const MacroInfo *OtherMI=getMacroInfo(MacroNameTok.getIdentifierInfo())) {
3114 // Final macros are hard-mode: they always warn. Even if the bodies are
3115 // identical. Even if they are in system headers. Even if they are things we
3116 // would silently allow in the past.
3117 if (MacroNameTok.getIdentifierInfo()->isFinal())
3118 emitFinalMacroWarning(MacroNameTok, /*IsUndef=*/false);
3120 // In Objective-C, ignore attempts to directly redefine the builtin
3121 // definitions of the ownership qualifiers. It's still possible to
3122 // #undef them.
3123 if (getLangOpts().ObjC &&
3124 SourceMgr.getFileID(OtherMI->getDefinitionLoc()) ==
3125 getPredefinesFileID() &&
3126 isObjCProtectedMacro(MacroNameTok.getIdentifierInfo())) {
3127 // Warn if it changes the tokens.
3128 if ((!getDiagnostics().getSuppressSystemWarnings() ||
3129 !SourceMgr.isInSystemHeader(DefineTok.getLocation())) &&
3130 !MI->isIdenticalTo(*OtherMI, *this,
3131 /*Syntactic=*/LangOpts.MicrosoftExt)) {
3132 Diag(MI->getDefinitionLoc(), diag::warn_pp_objc_macro_redef_ignored);
3134 assert(!OtherMI->isWarnIfUnused());
3135 return;
3138 // It is very common for system headers to have tons of macro redefinitions
3139 // and for warnings to be disabled in system headers. If this is the case,
3140 // then don't bother calling MacroInfo::isIdenticalTo.
3141 if (!getDiagnostics().getSuppressSystemWarnings() ||
3142 !SourceMgr.isInSystemHeader(DefineTok.getLocation())) {
3144 if (!OtherMI->isUsed() && OtherMI->isWarnIfUnused())
3145 Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used);
3147 // Warn if defining "__LINE__" and other builtins, per C99 6.10.8/4 and
3148 // C++ [cpp.predefined]p4, but allow it as an extension.
3149 if (isLanguageDefinedBuiltin(SourceMgr, OtherMI, II->getName()))
3150 Diag(MacroNameTok, diag::ext_pp_redef_builtin_macro);
3151 // Macros must be identical. This means all tokens and whitespace
3152 // separation must be the same. C99 6.10.3p2.
3153 else if (!OtherMI->isAllowRedefinitionsWithoutWarning() &&
3154 !MI->isIdenticalTo(*OtherMI, *this, /*Syntactic=*/LangOpts.MicrosoftExt)) {
3155 Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef)
3156 << MacroNameTok.getIdentifierInfo();
3157 Diag(OtherMI->getDefinitionLoc(), diag::note_previous_definition);
3160 if (OtherMI->isWarnIfUnused())
3161 WarnUnusedMacroLocs.erase(OtherMI->getDefinitionLoc());
3164 DefMacroDirective *MD =
3165 appendDefMacroDirective(MacroNameTok.getIdentifierInfo(), MI);
3167 assert(!MI->isUsed());
3168 // If we need warning for not using the macro, add its location in the
3169 // warn-because-unused-macro set. If it gets used it will be removed from set.
3170 if (getSourceManager().isInMainFile(MI->getDefinitionLoc()) &&
3171 !Diags->isIgnored(diag::pp_macro_not_used, MI->getDefinitionLoc()) &&
3172 !MacroExpansionInDirectivesOverride &&
3173 getSourceManager().getFileID(MI->getDefinitionLoc()) !=
3174 getPredefinesFileID()) {
3175 MI->setIsWarnIfUnused(true);
3176 WarnUnusedMacroLocs.insert(MI->getDefinitionLoc());
3179 // If the callbacks want to know, tell them about the macro definition.
3180 if (Callbacks)
3181 Callbacks->MacroDefined(MacroNameTok, MD);
3183 // If we're in MS compatibility mode and the macro being defined is the
3184 // assert macro, implicitly add a macro definition for static_assert to work
3185 // around their broken assert.h header file in C. Only do so if there isn't
3186 // already a static_assert macro defined.
3187 if (!getLangOpts().CPlusPlus && getLangOpts().MSVCCompat &&
3188 MacroNameTok.getIdentifierInfo()->isStr("assert") &&
3189 !isMacroDefined("static_assert")) {
3190 MacroInfo *MI = AllocateMacroInfo(SourceLocation());
3192 Token Tok;
3193 Tok.startToken();
3194 Tok.setKind(tok::kw__Static_assert);
3195 Tok.setIdentifierInfo(getIdentifierInfo("_Static_assert"));
3196 MI->setTokens({Tok}, BP);
3197 (void)appendDefMacroDirective(getIdentifierInfo("static_assert"), MI);
3201 /// HandleUndefDirective - Implements \#undef.
3203 void Preprocessor::HandleUndefDirective() {
3204 ++NumUndefined;
3206 Token MacroNameTok;
3207 ReadMacroName(MacroNameTok, MU_Undef);
3209 // Error reading macro name? If so, diagnostic already issued.
3210 if (MacroNameTok.is(tok::eod))
3211 return;
3213 // Check to see if this is the last token on the #undef line.
3214 CheckEndOfDirective("undef");
3216 // Okay, we have a valid identifier to undef.
3217 auto *II = MacroNameTok.getIdentifierInfo();
3218 auto MD = getMacroDefinition(II);
3219 UndefMacroDirective *Undef = nullptr;
3221 if (II->isFinal())
3222 emitFinalMacroWarning(MacroNameTok, /*IsUndef=*/true);
3224 // If the macro is not defined, this is a noop undef.
3225 if (const MacroInfo *MI = MD.getMacroInfo()) {
3226 if (!MI->isUsed() && MI->isWarnIfUnused())
3227 Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used);
3229 // Warn if undefining "__LINE__" and other builtins, per C99 6.10.8/4 and
3230 // C++ [cpp.predefined]p4, but allow it as an extension.
3231 if (isLanguageDefinedBuiltin(SourceMgr, MI, II->getName()))
3232 Diag(MacroNameTok, diag::ext_pp_undef_builtin_macro);
3234 if (MI->isWarnIfUnused())
3235 WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());
3237 Undef = AllocateUndefMacroDirective(MacroNameTok.getLocation());
3240 // If the callbacks want to know, tell them about the macro #undef.
3241 // Note: no matter if the macro was defined or not.
3242 if (Callbacks)
3243 Callbacks->MacroUndefined(MacroNameTok, MD, Undef);
3245 if (Undef)
3246 appendMacroDirective(II, Undef);
3249 //===----------------------------------------------------------------------===//
3250 // Preprocessor Conditional Directive Handling.
3251 //===----------------------------------------------------------------------===//
3253 /// HandleIfdefDirective - Implements the \#ifdef/\#ifndef directive. isIfndef
3254 /// is true when this is a \#ifndef directive. ReadAnyTokensBeforeDirective is
3255 /// true if any tokens have been returned or pp-directives activated before this
3256 /// \#ifndef has been lexed.
3258 void Preprocessor::HandleIfdefDirective(Token &Result,
3259 const Token &HashToken,
3260 bool isIfndef,
3261 bool ReadAnyTokensBeforeDirective) {
3262 ++NumIf;
3263 Token DirectiveTok = Result;
3265 Token MacroNameTok;
3266 ReadMacroName(MacroNameTok);
3268 // Error reading macro name? If so, diagnostic already issued.
3269 if (MacroNameTok.is(tok::eod)) {
3270 // Skip code until we get to #endif. This helps with recovery by not
3271 // emitting an error when the #endif is reached.
3272 SkipExcludedConditionalBlock(HashToken.getLocation(),
3273 DirectiveTok.getLocation(),
3274 /*Foundnonskip*/ false, /*FoundElse*/ false);
3275 return;
3278 emitMacroExpansionWarnings(MacroNameTok);
3280 // Check to see if this is the last token on the #if[n]def line.
3281 CheckEndOfDirective(isIfndef ? "ifndef" : "ifdef");
3283 IdentifierInfo *MII = MacroNameTok.getIdentifierInfo();
3284 auto MD = getMacroDefinition(MII);
3285 MacroInfo *MI = MD.getMacroInfo();
3287 if (CurPPLexer->getConditionalStackDepth() == 0) {
3288 // If the start of a top-level #ifdef and if the macro is not defined,
3289 // inform MIOpt that this might be the start of a proper include guard.
3290 // Otherwise it is some other form of unknown conditional which we can't
3291 // handle.
3292 if (!ReadAnyTokensBeforeDirective && !MI) {
3293 assert(isIfndef && "#ifdef shouldn't reach here");
3294 CurPPLexer->MIOpt.EnterTopLevelIfndef(MII, MacroNameTok.getLocation());
3295 } else
3296 CurPPLexer->MIOpt.EnterTopLevelConditional();
3299 // If there is a macro, process it.
3300 if (MI) // Mark it used.
3301 markMacroAsUsed(MI);
3303 if (Callbacks) {
3304 if (isIfndef)
3305 Callbacks->Ifndef(DirectiveTok.getLocation(), MacroNameTok, MD);
3306 else
3307 Callbacks->Ifdef(DirectiveTok.getLocation(), MacroNameTok, MD);
3310 bool RetainExcludedCB = PPOpts->RetainExcludedConditionalBlocks &&
3311 getSourceManager().isInMainFile(DirectiveTok.getLocation());
3313 // Should we include the stuff contained by this directive?
3314 if (PPOpts->SingleFileParseMode && !MI) {
3315 // In 'single-file-parse mode' undefined identifiers trigger parsing of all
3316 // the directive blocks.
3317 CurPPLexer->pushConditionalLevel(DirectiveTok.getLocation(),
3318 /*wasskip*/false, /*foundnonskip*/false,
3319 /*foundelse*/false);
3320 } else if (!MI == isIfndef || RetainExcludedCB) {
3321 // Yes, remember that we are inside a conditional, then lex the next token.
3322 CurPPLexer->pushConditionalLevel(DirectiveTok.getLocation(),
3323 /*wasskip*/false, /*foundnonskip*/true,
3324 /*foundelse*/false);
3325 } else {
3326 // No, skip the contents of this block.
3327 SkipExcludedConditionalBlock(HashToken.getLocation(),
3328 DirectiveTok.getLocation(),
3329 /*Foundnonskip*/ false,
3330 /*FoundElse*/ false);
3334 /// HandleIfDirective - Implements the \#if directive.
3336 void Preprocessor::HandleIfDirective(Token &IfToken,
3337 const Token &HashToken,
3338 bool ReadAnyTokensBeforeDirective) {
3339 ++NumIf;
3341 // Parse and evaluate the conditional expression.
3342 IdentifierInfo *IfNDefMacro = nullptr;
3343 const DirectiveEvalResult DER = EvaluateDirectiveExpression(IfNDefMacro);
3344 const bool ConditionalTrue = DER.Conditional;
3345 // Lexer might become invalid if we hit code completion point while evaluating
3346 // expression.
3347 if (!CurPPLexer)
3348 return;
3350 // If this condition is equivalent to #ifndef X, and if this is the first
3351 // directive seen, handle it for the multiple-include optimization.
3352 if (CurPPLexer->getConditionalStackDepth() == 0) {
3353 if (!ReadAnyTokensBeforeDirective && IfNDefMacro && ConditionalTrue)
3354 // FIXME: Pass in the location of the macro name, not the 'if' token.
3355 CurPPLexer->MIOpt.EnterTopLevelIfndef(IfNDefMacro, IfToken.getLocation());
3356 else
3357 CurPPLexer->MIOpt.EnterTopLevelConditional();
3360 if (Callbacks)
3361 Callbacks->If(
3362 IfToken.getLocation(), DER.ExprRange,
3363 (ConditionalTrue ? PPCallbacks::CVK_True : PPCallbacks::CVK_False));
3365 bool RetainExcludedCB = PPOpts->RetainExcludedConditionalBlocks &&
3366 getSourceManager().isInMainFile(IfToken.getLocation());
3368 // Should we include the stuff contained by this directive?
3369 if (PPOpts->SingleFileParseMode && DER.IncludedUndefinedIds) {
3370 // In 'single-file-parse mode' undefined identifiers trigger parsing of all
3371 // the directive blocks.
3372 CurPPLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
3373 /*foundnonskip*/false, /*foundelse*/false);
3374 } else if (ConditionalTrue || RetainExcludedCB) {
3375 // Yes, remember that we are inside a conditional, then lex the next token.
3376 CurPPLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
3377 /*foundnonskip*/true, /*foundelse*/false);
3378 } else {
3379 // No, skip the contents of this block.
3380 SkipExcludedConditionalBlock(HashToken.getLocation(), IfToken.getLocation(),
3381 /*Foundnonskip*/ false,
3382 /*FoundElse*/ false);
3386 /// HandleEndifDirective - Implements the \#endif directive.
3388 void Preprocessor::HandleEndifDirective(Token &EndifToken) {
3389 ++NumEndif;
3391 // Check that this is the whole directive.
3392 CheckEndOfDirective("endif");
3394 PPConditionalInfo CondInfo;
3395 if (CurPPLexer->popConditionalLevel(CondInfo)) {
3396 // No conditionals on the stack: this is an #endif without an #if.
3397 Diag(EndifToken, diag::err_pp_endif_without_if);
3398 return;
3401 // If this the end of a top-level #endif, inform MIOpt.
3402 if (CurPPLexer->getConditionalStackDepth() == 0)
3403 CurPPLexer->MIOpt.ExitTopLevelConditional();
3405 assert(!CondInfo.WasSkipping && !CurPPLexer->LexingRawMode &&
3406 "This code should only be reachable in the non-skipping case!");
3408 if (Callbacks)
3409 Callbacks->Endif(EndifToken.getLocation(), CondInfo.IfLoc);
3412 /// HandleElseDirective - Implements the \#else directive.
3414 void Preprocessor::HandleElseDirective(Token &Result, const Token &HashToken) {
3415 ++NumElse;
3417 // #else directive in a non-skipping conditional... start skipping.
3418 CheckEndOfDirective("else");
3420 PPConditionalInfo CI;
3421 if (CurPPLexer->popConditionalLevel(CI)) {
3422 Diag(Result, diag::pp_err_else_without_if);
3423 return;
3426 // If this is a top-level #else, inform the MIOpt.
3427 if (CurPPLexer->getConditionalStackDepth() == 0)
3428 CurPPLexer->MIOpt.EnterTopLevelConditional();
3430 // If this is a #else with a #else before it, report the error.
3431 if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else);
3433 if (Callbacks)
3434 Callbacks->Else(Result.getLocation(), CI.IfLoc);
3436 bool RetainExcludedCB = PPOpts->RetainExcludedConditionalBlocks &&
3437 getSourceManager().isInMainFile(Result.getLocation());
3439 if ((PPOpts->SingleFileParseMode && !CI.FoundNonSkip) || RetainExcludedCB) {
3440 // In 'single-file-parse mode' undefined identifiers trigger parsing of all
3441 // the directive blocks.
3442 CurPPLexer->pushConditionalLevel(CI.IfLoc, /*wasskip*/false,
3443 /*foundnonskip*/false, /*foundelse*/true);
3444 return;
3447 // Finally, skip the rest of the contents of this block.
3448 SkipExcludedConditionalBlock(HashToken.getLocation(), CI.IfLoc,
3449 /*Foundnonskip*/ true,
3450 /*FoundElse*/ true, Result.getLocation());
3453 /// Implements the \#elif, \#elifdef, and \#elifndef directives.
3454 void Preprocessor::HandleElifFamilyDirective(Token &ElifToken,
3455 const Token &HashToken,
3456 tok::PPKeywordKind Kind) {
3457 PPElifDiag DirKind = Kind == tok::pp_elif ? PED_Elif
3458 : Kind == tok::pp_elifdef ? PED_Elifdef
3459 : PED_Elifndef;
3460 ++NumElse;
3462 // Warn if using `#elifdef` & `#elifndef` in not C23 & C++23 mode.
3463 switch (DirKind) {
3464 case PED_Elifdef:
3465 case PED_Elifndef:
3466 unsigned DiagID;
3467 if (LangOpts.CPlusPlus)
3468 DiagID = LangOpts.CPlusPlus23 ? diag::warn_cxx23_compat_pp_directive
3469 : diag::ext_cxx23_pp_directive;
3470 else
3471 DiagID = LangOpts.C23 ? diag::warn_c23_compat_pp_directive
3472 : diag::ext_c23_pp_directive;
3473 Diag(ElifToken, DiagID) << DirKind;
3474 break;
3475 default:
3476 break;
3479 // #elif directive in a non-skipping conditional... start skipping.
3480 // We don't care what the condition is, because we will always skip it (since
3481 // the block immediately before it was included).
3482 SourceRange ConditionRange = DiscardUntilEndOfDirective();
3484 PPConditionalInfo CI;
3485 if (CurPPLexer->popConditionalLevel(CI)) {
3486 Diag(ElifToken, diag::pp_err_elif_without_if) << DirKind;
3487 return;
3490 // If this is a top-level #elif, inform the MIOpt.
3491 if (CurPPLexer->getConditionalStackDepth() == 0)
3492 CurPPLexer->MIOpt.EnterTopLevelConditional();
3494 // If this is a #elif with a #else before it, report the error.
3495 if (CI.FoundElse)
3496 Diag(ElifToken, diag::pp_err_elif_after_else) << DirKind;
3498 if (Callbacks) {
3499 switch (Kind) {
3500 case tok::pp_elif:
3501 Callbacks->Elif(ElifToken.getLocation(), ConditionRange,
3502 PPCallbacks::CVK_NotEvaluated, CI.IfLoc);
3503 break;
3504 case tok::pp_elifdef:
3505 Callbacks->Elifdef(ElifToken.getLocation(), ConditionRange, CI.IfLoc);
3506 break;
3507 case tok::pp_elifndef:
3508 Callbacks->Elifndef(ElifToken.getLocation(), ConditionRange, CI.IfLoc);
3509 break;
3510 default:
3511 assert(false && "unexpected directive kind");
3512 break;
3516 bool RetainExcludedCB = PPOpts->RetainExcludedConditionalBlocks &&
3517 getSourceManager().isInMainFile(ElifToken.getLocation());
3519 if ((PPOpts->SingleFileParseMode && !CI.FoundNonSkip) || RetainExcludedCB) {
3520 // In 'single-file-parse mode' undefined identifiers trigger parsing of all
3521 // the directive blocks.
3522 CurPPLexer->pushConditionalLevel(ElifToken.getLocation(), /*wasskip*/false,
3523 /*foundnonskip*/false, /*foundelse*/false);
3524 return;
3527 // Finally, skip the rest of the contents of this block.
3528 SkipExcludedConditionalBlock(
3529 HashToken.getLocation(), CI.IfLoc, /*Foundnonskip*/ true,
3530 /*FoundElse*/ CI.FoundElse, ElifToken.getLocation());