clang 23.0.0git
PPDirectives.cpp
Go to the documentation of this file.
1//===--- PPDirectives.cpp - Directive Handling for Preprocessor -----------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// Implements # directive processing for the Preprocessor.
11///
12//===----------------------------------------------------------------------===//
13
21#include "clang/Basic/Module.h"
30#include "clang/Lex/MacroInfo.h"
32#include "clang/Lex/ModuleMap.h"
34#include "clang/Lex/Pragma.h"
37#include "clang/Lex/Token.h"
39#include "llvm/ADT/ArrayRef.h"
40#include "llvm/ADT/STLExtras.h"
41#include "llvm/ADT/ScopeExit.h"
42#include "llvm/ADT/SmallVector.h"
43#include "llvm/ADT/StringExtras.h"
44#include "llvm/ADT/StringRef.h"
45#include "llvm/ADT/StringSwitch.h"
46#include "llvm/Support/ErrorHandling.h"
47#include "llvm/Support/Path.h"
48#include "llvm/Support/SaveAndRestore.h"
49#include <algorithm>
50#include <cassert>
51#include <cstddef>
52#include <cstring>
53#include <optional>
54#include <string>
55#include <utility>
56
57using namespace clang;
58
59//===----------------------------------------------------------------------===//
60// Utility Methods for Preprocessor Directive Handling.
61//===----------------------------------------------------------------------===//
62
64 static_assert(std::is_trivially_destructible_v<MacroInfo>, "");
65 return new (BP) MacroInfo(L);
66}
67
68DefMacroDirective *Preprocessor::AllocateDefMacroDirective(MacroInfo *MI,
69 SourceLocation Loc) {
70 return new (BP) DefMacroDirective(MI, Loc);
71}
72
74Preprocessor::AllocateUndefMacroDirective(SourceLocation UndefLoc) {
75 return new (BP) UndefMacroDirective(UndefLoc);
76}
77
79Preprocessor::AllocateVisibilityMacroDirective(SourceLocation Loc,
80 bool isPublic) {
81 return new (BP) VisibilityMacroDirective(Loc, isPublic);
82}
83
84/// Read and discard all tokens remaining on the current line until
85/// the tok::eod token is found.
87 Token &Tmp, SmallVectorImpl<Token> *DiscardedToks) {
88 SourceRange Res;
89 auto ReadNextTok = [&]() {
91 if (DiscardedToks && Tmp.isNot(tok::eod))
92 DiscardedToks->push_back(Tmp);
93 };
94 ReadNextTok();
95 Res.setBegin(Tmp.getLocation());
96 while (Tmp.isNot(tok::eod)) {
97 assert(Tmp.isNot(tok::eof) && "EOF seen while discarding directive tokens");
98 ReadNextTok();
99 }
100 Res.setEnd(Tmp.getLocation());
101 return Res;
102}
103
104/// Enumerates possible cases of #define/#undef a reserved identifier.
106 MD_NoWarn, //> Not a reserved identifier
107 MD_KeywordDef, //> Macro hides keyword, enabled by default
108 MD_ReservedMacro, //> #define of #undef reserved id, disabled by default
110};
111
112/// Enumerates possible %select values for the pp_err_elif_after_else and
113/// pp_err_elif_without_if diagnostics.
119
120static bool isFeatureTestMacro(StringRef MacroName) {
121 // list from:
122 // * https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_macros.html
123 // * https://docs.microsoft.com/en-us/cpp/c-runtime-library/security-features-in-the-crt?view=msvc-160
124 // * man 7 feature_test_macros
125 // The list must be sorted for correct binary search.
126 static constexpr StringRef ReservedMacro[] = {
127 "_ATFILE_SOURCE",
128 "_BSD_SOURCE",
129 "_CRT_NONSTDC_NO_WARNINGS",
130 "_CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES",
131 "_CRT_SECURE_NO_WARNINGS",
132 "_FILE_OFFSET_BITS",
133 "_FORTIFY_SOURCE",
134 "_GLIBCXX_ASSERTIONS",
135 "_GLIBCXX_CONCEPT_CHECKS",
136 "_GLIBCXX_DEBUG",
137 "_GLIBCXX_DEBUG_PEDANTIC",
138 "_GLIBCXX_PARALLEL",
139 "_GLIBCXX_PARALLEL_ASSERTIONS",
140 "_GLIBCXX_SANITIZE_VECTOR",
141 "_GLIBCXX_USE_CXX11_ABI",
142 "_GLIBCXX_USE_DEPRECATED",
143 "_GNU_SOURCE",
144 "_ISOC11_SOURCE",
145 "_ISOC95_SOURCE",
146 "_ISOC99_SOURCE",
147 "_LARGEFILE64_SOURCE",
148 "_POSIX_C_SOURCE",
149 "_REENTRANT",
150 "_SVID_SOURCE",
151 "_THREAD_SAFE",
152 "_XOPEN_SOURCE",
153 "_XOPEN_SOURCE_EXTENDED",
154 "__STDCPP_WANT_MATH_SPEC_FUNCS__",
155 "__STDC_FORMAT_MACROS",
156 };
157 return llvm::binary_search(ReservedMacro, MacroName);
158}
159
160static bool isLanguageDefinedBuiltin(const SourceManager &SourceMgr,
161 const MacroInfo *MI,
162 const StringRef MacroName) {
163 // If this is a macro with special handling (like __LINE__) then it's language
164 // defined.
165 if (MI->isBuiltinMacro())
166 return true;
167 // Builtin macros are defined in the builtin file
168 if (!SourceMgr.isWrittenInBuiltinFile(MI->getDefinitionLoc()))
169 return false;
170 // C defines macros starting with __STDC, and C++ defines macros starting with
171 // __STDCPP
172 if (MacroName.starts_with("__STDC"))
173 return true;
174 // C++ defines the __cplusplus macro
175 if (MacroName == "__cplusplus")
176 return true;
177 // C++ defines various feature-test macros starting with __cpp
178 if (MacroName.starts_with("__cpp"))
179 return true;
180 // Anything else isn't language-defined
181 return false;
182}
183
185 const LangOptions &Lang = PP.getLangOpts();
186 if (Lang.CPlusPlus &&
187 hasAttribute(AttributeCommonInfo::AS_CXX11, /* Scope*/ nullptr, II,
188 PP.getTargetInfo(), Lang, /*CheckPlugins*/ false) > 0) {
192 return PP.isNextPPTokenOneOf(tok::l_paren);
193
194 return !PP.isNextPPTokenOneOf(tok::l_paren) ||
196 }
197 return false;
198}
199
201 const LangOptions &Lang = PP.getLangOpts();
202 StringRef Text = II->getName();
203 if (isReservedInAllContexts(II->isReserved(Lang)))
205 if (II->isKeyword(Lang))
206 return MD_KeywordDef;
207 if (Lang.CPlusPlus11 && (Text == "override" || Text == "final"))
208 return MD_KeywordDef;
209 if (isReservedCXXAttributeName(PP, II))
211 return MD_NoWarn;
212}
213
215 const LangOptions &Lang = PP.getLangOpts();
216 // Do not warn on keyword undef. It is generally harmless and widely used.
217 if (isReservedInAllContexts(II->isReserved(Lang)))
218 return MD_ReservedMacro;
219 if (isReservedCXXAttributeName(PP, II))
221 return MD_NoWarn;
222}
223
224// Return true if we want to issue a diagnostic by default if we
225// encounter this name in a #include with the wrong case. For now,
226// this includes the standard C and C++ headers, Posix headers,
227// and Boost headers. Improper case for these #includes is a
228// potential portability issue.
229static bool warnByDefaultOnWrongCase(StringRef Include) {
230 // If the first component of the path is "boost", treat this like a standard header
231 // for the purposes of diagnostics.
232 if (::llvm::sys::path::begin(Include)->equals_insensitive("boost"))
233 return true;
234
235 // "condition_variable" is the longest standard header name at 18 characters.
236 // If the include file name is longer than that, it can't be a standard header.
237 static const size_t MaxStdHeaderNameLen = 18u;
238 if (Include.size() > MaxStdHeaderNameLen)
239 return false;
240
241 // Lowercase and normalize the search string.
242 SmallString<32> LowerInclude{Include};
243 for (char &Ch : LowerInclude) {
244 // In the ASCII range?
245 if (static_cast<unsigned char>(Ch) > 0x7f)
246 return false; // Can't be a standard header
247 // ASCII lowercase:
248 if (Ch >= 'A' && Ch <= 'Z')
249 Ch += 'a' - 'A';
250 // Normalize path separators for comparison purposes.
251 else if (::llvm::sys::path::is_separator(Ch))
252 Ch = '/';
253 }
254
255 // The standard C/C++ and Posix headers
256 return llvm::StringSwitch<bool>(LowerInclude)
257 // C library headers
258 .Cases({"assert.h", "complex.h", "ctype.h", "errno.h", "fenv.h"}, true)
259 .Cases({"float.h", "inttypes.h", "iso646.h", "limits.h", "locale.h"},
260 true)
261 .Cases({"math.h", "setjmp.h", "signal.h", "stdalign.h", "stdarg.h"}, true)
262 .Cases({"stdatomic.h", "stdbool.h", "stdckdint.h", "stdcountof.h"}, true)
263 .Cases({"stddef.h", "stdint.h", "stdio.h", "stdlib.h", "stdnoreturn.h"},
264 true)
265 .Cases({"string.h", "tgmath.h", "threads.h", "time.h", "uchar.h"}, true)
266 .Cases({"wchar.h", "wctype.h"}, true)
267
268 // C++ headers for C library facilities
269 .Cases({"cassert", "ccomplex", "cctype", "cerrno", "cfenv"}, true)
270 .Cases({"cfloat", "cinttypes", "ciso646", "climits", "clocale"}, true)
271 .Cases({"cmath", "csetjmp", "csignal", "cstdalign", "cstdarg"}, true)
272 .Cases({"cstdbool", "cstddef", "cstdint", "cstdio", "cstdlib"}, true)
273 .Cases({"cstring", "ctgmath", "ctime", "cuchar", "cwchar"}, true)
274 .Case("cwctype", true)
275
276 // C++ library headers
277 .Cases({"algorithm", "fstream", "list", "regex", "thread"}, true)
278 .Cases({"array", "functional", "locale", "scoped_allocator", "tuple"},
279 true)
280 .Cases({"atomic", "future", "map", "set", "type_traits"}, true)
281 .Cases(
282 {"bitset", "initializer_list", "memory", "shared_mutex", "typeindex"},
283 true)
284 .Cases({"chrono", "iomanip", "mutex", "sstream", "typeinfo"}, true)
285 .Cases({"codecvt", "ios", "new", "stack", "unordered_map"}, true)
286 .Cases({"complex", "iosfwd", "numeric", "stdexcept", "unordered_set"},
287 true)
288 .Cases(
289 {"condition_variable", "iostream", "ostream", "streambuf", "utility"},
290 true)
291 .Cases({"deque", "istream", "queue", "string", "valarray"}, true)
292 .Cases({"exception", "iterator", "random", "strstream", "vector"}, true)
293 .Cases({"forward_list", "limits", "ratio", "system_error"}, true)
294
295 // POSIX headers (which aren't also C headers)
296 .Cases({"aio.h", "arpa/inet.h", "cpio.h", "dirent.h", "dlfcn.h"}, true)
297 .Cases({"fcntl.h", "fmtmsg.h", "fnmatch.h", "ftw.h", "glob.h"}, true)
298 .Cases({"grp.h", "iconv.h", "langinfo.h", "libgen.h", "monetary.h"}, true)
299 .Cases({"mqueue.h", "ndbm.h", "net/if.h", "netdb.h", "netinet/in.h"},
300 true)
301 .Cases({"netinet/tcp.h", "nl_types.h", "poll.h", "pthread.h", "pwd.h"},
302 true)
303 .Cases({"regex.h", "sched.h", "search.h", "semaphore.h", "spawn.h"}, true)
304 .Cases({"strings.h", "stropts.h", "sys/ipc.h", "sys/mman.h", "sys/msg.h"},
305 true)
306 .Cases({"sys/resource.h", "sys/select.h", "sys/sem.h", "sys/shm.h",
307 "sys/socket.h"},
308 true)
309 .Cases({"sys/stat.h", "sys/statvfs.h", "sys/time.h", "sys/times.h",
310 "sys/types.h"},
311 true)
312 .Cases(
313 {"sys/uio.h", "sys/un.h", "sys/utsname.h", "sys/wait.h", "syslog.h"},
314 true)
315 .Cases({"tar.h", "termios.h", "trace.h", "ulimit.h"}, true)
316 .Cases({"unistd.h", "utime.h", "utmpx.h", "wordexp.h"}, true)
317 .Default(false);
318}
319
320/// Find a similar string in `Candidates`.
321///
322/// \param LHS a string for a similar string in `Candidates`
323///
324/// \param Candidates the candidates to find a similar string.
325///
326/// \returns a similar string if exists. If no similar string exists,
327/// returns std::nullopt.
328static std::optional<StringRef>
329findSimilarStr(StringRef LHS, const std::vector<StringRef> &Candidates) {
330 // We need to check if `Candidates` has the exact case-insensitive string
331 // because the Levenshtein distance match does not care about it.
332 for (StringRef C : Candidates) {
333 if (LHS.equals_insensitive(C)) {
334 return C;
335 }
336 }
337
338 // Keep going with the Levenshtein distance match.
339 // If the LHS size is less than 3, use the LHS size minus 1 and if not,
340 // use the LHS size divided by 3.
341 size_t Length = LHS.size();
342 size_t MaxDist = Length < 3 ? Length - 1 : Length / 3;
343
344 std::optional<std::pair<StringRef, size_t>> SimilarStr;
345 for (StringRef C : Candidates) {
346 size_t CurDist = LHS.edit_distance(C, true);
347 if (CurDist <= MaxDist) {
348 if (!SimilarStr) {
349 // The first similar string found.
350 SimilarStr = {C, CurDist};
351 } else if (CurDist < SimilarStr->second) {
352 // More similar string found.
353 SimilarStr = {C, CurDist};
354 }
355 }
356 }
357
358 if (SimilarStr) {
359 return SimilarStr->first;
360 } else {
361 return std::nullopt;
362 }
363}
364
365bool Preprocessor::CheckMacroName(Token &MacroNameTok, MacroUse isDefineUndef,
366 bool *ShadowFlag) {
367 // Missing macro name?
368 if (MacroNameTok.is(tok::eod))
369 return Diag(MacroNameTok, diag::err_pp_missing_macro_name);
370
371 IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
372 if (!II)
373 return Diag(MacroNameTok, diag::err_pp_macro_not_identifier);
374
375 if (II->isCPlusPlusOperatorKeyword()) {
376 // C++ 2.5p2: Alternative tokens behave the same as its primary token
377 // except for their spellings.
378 Diag(MacroNameTok, getLangOpts().MicrosoftExt
379 ? diag::ext_pp_operator_used_as_macro_name
380 : diag::err_pp_operator_used_as_macro_name)
381 << II << MacroNameTok.getKind();
382 // Allow #defining |and| and friends for Microsoft compatibility or
383 // recovery when legacy C headers are included in C++.
384 }
385
386 if ((isDefineUndef != MU_Other) && II->getPPKeywordID() == tok::pp_defined) {
387 // Error if defining "defined": C99 6.10.8/4, C++ [cpp.predefined]p4.
388 return Diag(MacroNameTok, diag::err_defined_macro_name);
389 }
390
391 // If defining/undefining reserved identifier or a keyword, we need to issue
392 // a warning.
393 SourceLocation MacroNameLoc = MacroNameTok.getLocation();
394 if (ShadowFlag)
395 *ShadowFlag = false;
396 // Macro names with reserved identifiers are accepted if built-in or passed
397 // through the command line (the later may be present if -dD was used to
398 // generate the preprocessed file).
399 // NB: isInPredefinedFile() is relatively expensive, so keep it at the end
400 // of the condition.
401 if (!SourceMgr.isInSystemHeader(MacroNameLoc) &&
402 !SourceMgr.isInPredefinedFile(MacroNameLoc)) {
404 if (isDefineUndef == MU_Define) {
405 D = shouldWarnOnMacroDef(*this, II);
406 }
407 else if (isDefineUndef == MU_Undef)
408 D = shouldWarnOnMacroUndef(*this, II);
409 if (D == MD_KeywordDef) {
410 // We do not want to warn on some patterns widely used in configuration
411 // scripts. This requires analyzing next tokens, so do not issue warnings
412 // now, only inform caller.
413 if (ShadowFlag)
414 *ShadowFlag = true;
415 }
416 if (D == MD_ReservedMacro)
417 Diag(MacroNameTok, diag::warn_pp_macro_is_reserved_id);
419 Diag(MacroNameTok, diag::warn_pp_macro_is_reserved_attribute_id)
420 << II->getName();
421 }
422
423 // Okay, we got a good identifier.
424 return false;
425}
426
427/// Lex and validate a macro name, which occurs after a
428/// \#define or \#undef.
429///
430/// This sets the token kind to eod and discards the rest of the macro line if
431/// the macro name is invalid.
432///
433/// \param MacroNameTok Token that is expected to be a macro name.
434/// \param isDefineUndef Context in which macro is used.
435/// \param ShadowFlag Points to a flag that is set if macro shadows a keyword.
436void Preprocessor::ReadMacroName(Token &MacroNameTok, MacroUse isDefineUndef,
437 bool *ShadowFlag) {
438 // Read the token, don't allow macro expansion on it.
439 LexUnexpandedToken(MacroNameTok);
440
441 if (MacroNameTok.is(tok::code_completion)) {
442 if (CodeComplete)
443 CodeComplete->CodeCompleteMacroName(isDefineUndef == MU_Define);
445 LexUnexpandedToken(MacroNameTok);
446 }
447
448 if (!CheckMacroName(MacroNameTok, isDefineUndef, ShadowFlag))
449 return;
450
451 // Invalid macro name, read and discard the rest of the line and set the
452 // token kind to tok::eod if necessary.
453 if (MacroNameTok.isNot(tok::eod)) {
454 MacroNameTok.setKind(tok::eod);
456 }
457}
458
459/// Ensure that the next token is a tok::eod token.
460///
461/// If not, emit a diagnostic and consume up until the eod. If EnableMacros is
462/// true, then we consider macros that expand to zero tokens as being ok.
463///
464/// Returns the location of the end of the directive.
466Preprocessor::CheckEndOfDirective(StringRef DirType, bool EnableMacros,
467 SmallVectorImpl<Token> *ExtraToks) {
468 Token Tmp;
469 // Avoid use-of-uninitialized-memory for edge case(s) where there is no extra
470 // token to be parsed.
471 Tmp.startToken();
472 auto ReadNextTok = [this, ExtraToks, &Tmp](auto &&LexFn) {
473 std::invoke(LexFn, this, Tmp);
474 if (ExtraToks && Tmp.isNot(tok::eod))
475 ExtraToks->push_back(Tmp);
476 };
477 // Lex unexpanded tokens for most directives: macros might expand to zero
478 // tokens, causing us to miss diagnosing invalid lines. Some directives (like
479 // #line) allow empty macros.
480 if (EnableMacros)
481 ReadNextTok(&Preprocessor::Lex);
482 else
484
485 // There should be no tokens after the directive, but we allow them as an
486 // extension.
487 while (Tmp.is(tok::comment)) // Skip comments in -C mode.
489
490 if (Tmp.is(tok::eod))
491 return Tmp.getLocation();
492
493 // Add a fixit in GNU/C99/C++ mode. Don't offer a fixit for strict-C89,
494 // or if this is a macro-style preprocessing directive, because it is more
495 // trouble than it is worth to insert /**/ and check that there is no /**/
496 // in the range also.
497 FixItHint Hint;
498 if ((LangOpts.GNUMode || LangOpts.C99 || LangOpts.CPlusPlus) &&
499 !CurTokenLexer)
500 Hint = FixItHint::CreateInsertion(Tmp.getLocation(),"//");
501
502 unsigned DiagID = diag::ext_pp_extra_tokens_at_eol;
503 // C++20 import or module directive has no '#' prefix.
504 if (getLangOpts().CPlusPlusModules &&
505 (DirType == "import" || DirType == "module"))
506 DiagID = diag::warn_pp_extra_tokens_at_module_directive_eol;
507
508 Diag(Tmp, DiagID) << DirType << Hint;
509 return DiscardUntilEndOfDirective(ExtraToks).getEnd();
510}
511
512void Preprocessor::SuggestTypoedDirective(const Token &Tok,
513 StringRef Directive) const {
514 // If this is a `.S` file, treat unknown # directives as non-preprocessor
515 // directives.
516 if (getLangOpts().AsmPreprocessor) return;
517
518 std::vector<StringRef> Candidates = {
519 "if", "ifdef", "ifndef", "elif", "else", "endif"
520 };
521 if (LangOpts.C23 || LangOpts.CPlusPlus23)
522 Candidates.insert(Candidates.end(), {"elifdef", "elifndef"});
523
524 if (std::optional<StringRef> Sugg = findSimilarStr(Directive, Candidates)) {
525 // Directive cannot be coming from macro.
526 assert(Tok.getLocation().isFileID());
528 Tok.getLocation(),
529 Tok.getLocation().getLocWithOffset(Directive.size()));
530 StringRef SuggValue = *Sugg;
531
532 auto Hint = FixItHint::CreateReplacement(DirectiveRange, SuggValue);
533 Diag(Tok, diag::warn_pp_invalid_directive) << 1 << SuggValue << Hint;
534 }
535}
536
537/// SkipExcludedConditionalBlock - We just read a \#if or related directive and
538/// decided that the subsequent tokens are in the \#if'd out portion of the
539/// file. Lex the rest of the file, until we see an \#endif. If
540/// FoundNonSkipPortion is true, then we have already emitted code for part of
541/// this \#if directive, so \#else/\#elif blocks should never be entered.
542/// If ElseOk is true, then \#else directives are ok, if not, then we have
543/// already seen one so a \#else directive is a duplicate. When this returns,
544/// the caller can lex the first valid token.
545void Preprocessor::SkipExcludedConditionalBlock(SourceLocation HashTokenLoc,
546 SourceLocation IfTokenLoc,
547 bool FoundNonSkipPortion,
548 bool FoundElse,
549 SourceLocation ElseLoc) {
550 // In SkippingRangeStateTy we are depending on SkipExcludedConditionalBlock()
551 // not getting called recursively by storing the RecordedSkippedRanges
552 // DenseMap lookup pointer (field SkipRangePtr). SkippingRangeStateTy expects
553 // that RecordedSkippedRanges won't get modified and SkipRangePtr won't be
554 // invalidated. If this changes and there is a need to call
555 // SkipExcludedConditionalBlock() recursively, SkippingRangeStateTy should
556 // change to do a second lookup in endLexPass function instead of reusing the
557 // lookup pointer.
558 assert(!SkippingExcludedConditionalBlock &&
559 "calling SkipExcludedConditionalBlock recursively");
560 llvm::SaveAndRestore SARSkipping(SkippingExcludedConditionalBlock, true);
561
562 ++NumSkipped;
563 assert(!CurTokenLexer && "Conditional PP block cannot appear in a macro!");
564 assert(CurPPLexer && "Conditional PP block must be in a file!");
565 assert(CurLexer && "Conditional PP block but no current lexer set!");
566
567 if (PreambleConditionalStack.reachedEOFWhileSkipping())
568 PreambleConditionalStack.clearSkipInfo();
569 else
570 CurPPLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/ false,
571 FoundNonSkipPortion, FoundElse);
572
573 // Enter raw mode to disable identifier lookup (and thus macro expansion),
574 // disabling warnings, etc.
575 CurPPLexer->LexingRawMode = true;
576 Token Tok;
577 SourceLocation endLoc;
578
579 /// Keeps track and caches skipped ranges and also retrieves a prior skipped
580 /// range if the same block is re-visited.
581 struct SkippingRangeStateTy {
582 Preprocessor &PP;
583
584 const char *BeginPtr = nullptr;
585 unsigned *SkipRangePtr = nullptr;
586
587 SkippingRangeStateTy(Preprocessor &PP) : PP(PP) {}
588
589 void beginLexPass() {
590 if (BeginPtr)
591 return; // continue skipping a block.
592
593 // Initiate a skipping block and adjust the lexer if we already skipped it
594 // before.
595 BeginPtr = PP.CurLexer->getBufferLocation();
596 SkipRangePtr = &PP.RecordedSkippedRanges[BeginPtr];
597 if (*SkipRangePtr) {
598 PP.CurLexer->seek(PP.CurLexer->getCurrentBufferOffset() + *SkipRangePtr,
599 /*IsAtStartOfLine*/ true);
600 }
601 }
602
603 void endLexPass(const char *Hashptr) {
604 if (!BeginPtr) {
605 // Not doing normal lexing.
606 assert(PP.CurLexer->isDependencyDirectivesLexer());
607 return;
608 }
609
610 // Finished skipping a block, record the range if it's first time visited.
611 if (!*SkipRangePtr) {
612 *SkipRangePtr = Hashptr - BeginPtr;
613 }
614 assert(*SkipRangePtr == unsigned(Hashptr - BeginPtr));
615 BeginPtr = nullptr;
616 SkipRangePtr = nullptr;
617 }
618 } SkippingRangeState(*this);
619
620 while (true) {
621 if (CurLexer->isDependencyDirectivesLexer()) {
622 CurLexer->LexDependencyDirectiveTokenWhileSkipping(Tok);
623 } else {
624 SkippingRangeState.beginLexPass();
625 while (true) {
626 CurLexer->Lex(Tok);
627
628 if (Tok.is(tok::code_completion)) {
630 if (CodeComplete)
631 CodeComplete->CodeCompleteInConditionalExclusion();
632 continue;
633 }
634
635 // There is actually no "skipped block" in the above because the module
636 // directive is not a text-line (https://wg21.link/cpp.pre#2) nor
637 // anything else that is allowed in a group
638 // (https://eel.is/c++draft/cpp.pre#nt:group-part).
639 //
640 // A preprocessor diagnostic (effective with -E) that triggers whenever
641 // a module directive is encountered where a control-line or a text-line
642 // is required.
643 if (getLangOpts().CPlusPlusModules && Tok.isAtStartOfLine() &&
644 Tok.is(tok::raw_identifier) &&
645 (Tok.getRawIdentifier() == "export" ||
646 Tok.getRawIdentifier() == "module")) {
647 llvm::SaveAndRestore ModuleDirectiveSkipping(LastExportKeyword);
648 LastExportKeyword.startToken();
650 IdentifierInfo *II = Tok.getIdentifierInfo();
651
652 if (II->getName()[0] == 'e') { // export
654 CurLexer->Lex(Tok);
655 if (Tok.is(tok::raw_identifier)) {
657 II = Tok.getIdentifierInfo();
658 }
659 }
660
661 if (II->getName()[0] == 'm') { // module
662 // HandleModuleContextualKeyword changes the lexer state, so we need
663 // to save RawLexingMode
664 llvm::SaveAndRestore RestoreLexingRawMode(CurPPLexer->LexingRawMode,
665 false);
667 // We just parsed a # character at the start of a line, so we're
668 // in directive mode. Tell the lexer this so any newlines we see
669 // will be converted into an EOD token (this terminates the
670 // macro).
671 CurPPLexer->ParsingPreprocessorDirective = true;
672 SourceLocation StartLoc = Tok.getLocation();
673 SourceLocation End = DiscardUntilEndOfDirective().getEnd();
674 Diag(StartLoc, diag::err_pp_cond_span_module_decl)
675 << SourceRange(StartLoc, End);
676 CurPPLexer->ParsingPreprocessorDirective = false;
677 // Restore comment saving mode.
678 if (CurLexer)
679 CurLexer->resetExtendedTokenMode();
680 continue;
681 }
682 }
683 }
684
685 // If this is the end of the buffer, we have an error.
686 if (Tok.is(tok::eof)) {
687 // We don't emit errors for unterminated conditionals here,
688 // Lexer::LexEndOfFile can do that properly.
689 // Just return and let the caller lex after this #include.
690 if (PreambleConditionalStack.isRecording())
691 PreambleConditionalStack.SkipInfo.emplace(HashTokenLoc, IfTokenLoc,
692 FoundNonSkipPortion,
693 FoundElse, ElseLoc);
694 break;
695 }
696
697 // If this token is not a preprocessor directive, just skip it.
698 if (Tok.isNot(tok::hash) || !Tok.isAtStartOfLine())
699 continue;
700
701 break;
702 }
703 }
704 if (Tok.is(tok::eof))
705 break;
706
707 // We just parsed a # character at the start of a line, so we're in
708 // directive mode. Tell the lexer this so any newlines we see will be
709 // converted into an EOD token (this terminates the macro).
710 CurPPLexer->ParsingPreprocessorDirective = true;
711 if (CurLexer) CurLexer->SetKeepWhitespaceMode(false);
712
713 assert(Tok.is(tok::hash));
714 const char *Hashptr = CurLexer->getBufferLocation() - Tok.getLength();
715 assert(CurLexer->getSourceLocation(Hashptr) == Tok.getLocation());
716
717 // Read the next token, the directive flavor.
719
720 // If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or
721 // something bogus), skip it.
722 if (Tok.isNot(tok::raw_identifier)) {
723 CurPPLexer->ParsingPreprocessorDirective = false;
724 // Restore comment saving mode.
725 if (CurLexer) CurLexer->resetExtendedTokenMode();
726 continue;
727 }
728
729 // If the first letter isn't i or e, it isn't intesting to us. We know that
730 // this is safe in the face of spelling differences, because there is no way
731 // to spell an i/e in a strange way that is another letter. Skipping this
732 // allows us to avoid looking up the identifier info for #define/#undef and
733 // other common directives.
734 StringRef RI = Tok.getRawIdentifier();
735
736 char FirstChar = RI[0];
737 if (FirstChar >= 'a' && FirstChar <= 'z' &&
738 FirstChar != 'i' && FirstChar != 'e') {
739 CurPPLexer->ParsingPreprocessorDirective = false;
740 // Restore comment saving mode.
741 if (CurLexer) CurLexer->resetExtendedTokenMode();
742 continue;
743 }
744
745 // Get the identifier name without trigraphs or embedded newlines. Note
746 // that we can't use Tok.getIdentifierInfo() because its lookup is disabled
747 // when skipping.
748 char DirectiveBuf[20];
749 StringRef Directive;
750 if (!Tok.needsCleaning() && RI.size() < 20) {
751 Directive = RI;
752 } else {
753 std::string DirectiveStr = getSpelling(Tok);
754 size_t IdLen = DirectiveStr.size();
755 if (IdLen >= 20) {
756 CurPPLexer->ParsingPreprocessorDirective = false;
757 // Restore comment saving mode.
758 if (CurLexer) CurLexer->resetExtendedTokenMode();
759 continue;
760 }
761 memcpy(DirectiveBuf, &DirectiveStr[0], IdLen);
762 Directive = StringRef(DirectiveBuf, IdLen);
763 }
764
765 if (Directive.starts_with("if")) {
766 StringRef Sub = Directive.substr(2);
767 if (Sub.empty() || // "if"
768 Sub == "def" || // "ifdef"
769 Sub == "ndef") { // "ifndef"
770 // We know the entire #if/#ifdef/#ifndef block will be skipped, don't
771 // bother parsing the condition.
773 CurPPLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true,
774 /*foundnonskip*/false,
775 /*foundelse*/false);
776 } else {
777 SuggestTypoedDirective(Tok, Directive);
778 }
779 } else if (Directive[0] == 'e') {
780 StringRef Sub = Directive.substr(1);
781 if (Sub == "ndif") { // "endif"
782 PPConditionalInfo CondInfo;
783 CondInfo.WasSkipping = true; // Silence bogus warning.
784 bool InCond = CurPPLexer->popConditionalLevel(CondInfo);
785 (void)InCond; // Silence warning in no-asserts mode.
786 assert(!InCond && "Can't be skipping if not in a conditional!");
787
788 // If we popped the outermost skipping block, we're done skipping!
789 if (!CondInfo.WasSkipping) {
790 SkippingRangeState.endLexPass(Hashptr);
791 // Restore the value of LexingRawMode so that trailing comments
792 // are handled correctly, if we've reached the outermost block.
793 CurPPLexer->LexingRawMode = false;
794 endLoc = CheckEndOfDirective("endif");
795 CurPPLexer->LexingRawMode = true;
796 if (Callbacks)
797 Callbacks->Endif(Tok.getLocation(), CondInfo.IfLoc);
798 break;
799 } else {
801 }
802 } else if (Sub == "lse") { // "else".
803 // #else directive in a skipping conditional. If not in some other
804 // skipping conditional, and if #else hasn't already been seen, enter it
805 // as a non-skipping conditional.
806 PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel();
807
808 if (!CondInfo.WasSkipping)
809 SkippingRangeState.endLexPass(Hashptr);
810
811 // If this is a #else with a #else before it, report the error.
812 if (CondInfo.FoundElse)
813 Diag(Tok, diag::pp_err_else_after_else);
814
815 // Note that we've seen a #else in this conditional.
816 CondInfo.FoundElse = true;
817
818 // If the conditional is at the top level, and the #if block wasn't
819 // entered, enter the #else block now.
820 if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) {
821 CondInfo.FoundNonSkip = true;
822 // Restore the value of LexingRawMode so that trailing comments
823 // are handled correctly.
824 CurPPLexer->LexingRawMode = false;
825 endLoc = CheckEndOfDirective("else");
826 CurPPLexer->LexingRawMode = true;
827 if (Callbacks)
828 Callbacks->Else(Tok.getLocation(), CondInfo.IfLoc);
829 break;
830 } else {
831 DiscardUntilEndOfDirective(); // C99 6.10p4.
832 }
833 } else if (Sub == "lif") { // "elif".
834 PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel();
835
836 if (!CondInfo.WasSkipping)
837 SkippingRangeState.endLexPass(Hashptr);
838
839 // If this is a #elif with a #else before it, report the error.
840 if (CondInfo.FoundElse)
841 Diag(Tok, diag::pp_err_elif_after_else) << PED_Elif;
842
843 // If this is in a skipping block or if we're already handled this #if
844 // block, don't bother parsing the condition.
845 if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) {
846 // FIXME: We should probably do at least some minimal parsing of the
847 // condition to verify that it is well-formed. The current state
848 // allows #elif* directives with completely malformed (or missing)
849 // conditions.
851 } else {
852 // Restore the value of LexingRawMode so that identifiers are
853 // looked up, etc, inside the #elif expression.
854 assert(CurPPLexer->LexingRawMode && "We have to be skipping here!");
855 CurPPLexer->LexingRawMode = false;
856 IdentifierInfo *IfNDefMacro = nullptr;
857 DirectiveEvalResult DER = EvaluateDirectiveExpression(IfNDefMacro);
858 // Stop if Lexer became invalid after hitting code completion token.
859 if (!CurPPLexer)
860 return;
861 const bool CondValue = DER.Conditional;
862 CurPPLexer->LexingRawMode = true;
863 if (Callbacks) {
864 Callbacks->Elif(
865 Tok.getLocation(), DER.ExprRange,
867 CondInfo.IfLoc);
868 }
869 // If this condition is true, enter it!
870 if (CondValue) {
871 CondInfo.FoundNonSkip = true;
872 break;
873 }
874 }
875 } else if (Sub == "lifdef" || // "elifdef"
876 Sub == "lifndef") { // "elifndef"
877 bool IsElifDef = Sub == "lifdef";
878 PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel();
879 Token DirectiveToken = Tok;
880
881 if (!CondInfo.WasSkipping)
882 SkippingRangeState.endLexPass(Hashptr);
883
884 // Warn if using `#elifdef` & `#elifndef` in not C23 & C++23 mode even
885 // if this branch is in a skipping block.
886 unsigned DiagID;
887 if (LangOpts.CPlusPlus)
888 DiagID = LangOpts.CPlusPlus23 ? diag::warn_cxx23_compat_pp_directive
889 : diag::ext_cxx23_pp_directive;
890 else
891 DiagID = LangOpts.C23 ? diag::warn_c23_compat_pp_directive
892 : diag::ext_c23_pp_directive;
893 Diag(Tok, DiagID) << (IsElifDef ? PED_Elifdef : PED_Elifndef);
894
895 // If this is a #elif with a #else before it, report the error.
896 if (CondInfo.FoundElse)
897 Diag(Tok, diag::pp_err_elif_after_else)
898 << (IsElifDef ? PED_Elifdef : PED_Elifndef);
899
900 // If this is in a skipping block or if we're already handled this #if
901 // block, don't bother parsing the condition.
902 if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) {
903 // FIXME: We should probably do at least some minimal parsing of the
904 // condition to verify that it is well-formed. The current state
905 // allows #elif* directives with completely malformed (or missing)
906 // conditions.
908 } else {
909 // Restore the value of LexingRawMode so that identifiers are
910 // looked up, etc, inside the #elif[n]def expression.
911 assert(CurPPLexer->LexingRawMode && "We have to be skipping here!");
912 CurPPLexer->LexingRawMode = false;
913 Token MacroNameTok;
914 ReadMacroName(MacroNameTok);
915 CurPPLexer->LexingRawMode = true;
916
917 // If the macro name token is tok::eod, there was an error that was
918 // already reported.
919 if (MacroNameTok.is(tok::eod)) {
920 // Skip code until we get to #endif. This helps with recovery by
921 // not emitting an error when the #endif is reached.
922 continue;
923 }
924
925 emitMacroExpansionWarnings(MacroNameTok);
926
927 CheckEndOfDirective(IsElifDef ? "elifdef" : "elifndef");
928
929 IdentifierInfo *MII = MacroNameTok.getIdentifierInfo();
930 auto MD = getMacroDefinition(MII);
931 MacroInfo *MI = MD.getMacroInfo();
932
933 if (Callbacks) {
934 if (IsElifDef) {
935 Callbacks->Elifdef(DirectiveToken.getLocation(), MacroNameTok,
936 MD);
937 } else {
938 Callbacks->Elifndef(DirectiveToken.getLocation(), MacroNameTok,
939 MD);
940 }
941 }
942 // If this condition is true, enter it!
943 if (static_cast<bool>(MI) == IsElifDef) {
944 CondInfo.FoundNonSkip = true;
945 break;
946 }
947 }
948 } else {
949 SuggestTypoedDirective(Tok, Directive);
950 }
951 } else {
952 SuggestTypoedDirective(Tok, Directive);
953 }
954
955 CurPPLexer->ParsingPreprocessorDirective = false;
956 // Restore comment saving mode.
957 if (CurLexer) CurLexer->resetExtendedTokenMode();
958 }
959
960 // Finally, if we are out of the conditional (saw an #endif or ran off the end
961 // of the file, just stop skipping and return to lexing whatever came after
962 // the #if block.
963 CurPPLexer->LexingRawMode = false;
964
965 // The last skipped range isn't actually skipped yet if it's truncated
966 // by the end of the preamble; we'll resume parsing after the preamble.
967 if (Callbacks && (Tok.isNot(tok::eof) || !isRecordingPreamble()))
968 Callbacks->SourceRangeSkipped(
969 SourceRange(HashTokenLoc, endLoc.isValid()
970 ? endLoc
971 : CurPPLexer->getSourceLocation()),
972 Tok.getLocation());
973}
974
976 bool AllowTextual) {
977 if (!SourceMgr.isInMainFile(Loc)) {
978 // Try to determine the module of the include directive.
979 // FIXME: Look into directly passing the FileEntry from LookupFile instead.
980 FileID IDOfIncl = SourceMgr.getFileID(SourceMgr.getExpansionLoc(Loc));
981 if (auto EntryOfIncl = SourceMgr.getFileEntryRefForID(IDOfIncl)) {
982 // The include comes from an included file.
983 return HeaderInfo.getModuleMap()
984 .findModuleForHeader(*EntryOfIncl, AllowTextual)
985 .getModule();
986 }
987 }
988
989 // This is either in the main file or not in a file at all. It belongs
990 // to the current module, if there is one.
991 return getLangOpts().CurrentModule.empty()
992 ? nullptr
993 : HeaderInfo.lookupModule(getLangOpts().CurrentModule, Loc);
994}
995
998 SourceLocation Loc) {
1000 IncLoc, LangOpts.ModulesValidateTextualHeaderIncludes);
1001
1002 // Walk up through the include stack, looking through textual headers of M
1003 // until we hit a non-textual header that we can #include. (We assume textual
1004 // headers of a module with non-textual headers aren't meant to be used to
1005 // import entities from the module.)
1006 auto &SM = getSourceManager();
1007 while (!Loc.isInvalid() && !SM.isInMainFile(Loc)) {
1008 auto ID = SM.getFileID(SM.getExpansionLoc(Loc));
1009 auto FE = SM.getFileEntryRefForID(ID);
1010 if (!FE)
1011 break;
1012
1013 // We want to find all possible modules that might contain this header, so
1014 // search all enclosing directories for module maps and load them.
1015 HeaderInfo.hasModuleMap(FE->getName(), /*Root*/ nullptr,
1016 SourceMgr.isInSystemHeader(Loc));
1017
1018 bool InPrivateHeader = false;
1019 for (auto Header : HeaderInfo.findAllModulesForHeader(*FE)) {
1020 if (!Header.isAccessibleFrom(IncM)) {
1021 // It's in a private header; we can't #include it.
1022 // FIXME: If there's a public header in some module that re-exports it,
1023 // then we could suggest including that, but it's not clear that's the
1024 // expected way to make this entity visible.
1025 InPrivateHeader = true;
1026 continue;
1027 }
1028
1029 // Don't suggest explicitly excluded headers.
1030 if (Header.getRole() == ModuleMap::ExcludedHeader)
1031 continue;
1032
1033 // We'll suggest including textual headers below if they're
1034 // include-guarded.
1035 if (Header.getRole() & ModuleMap::TextualHeader)
1036 continue;
1037
1038 // If we have a module import syntax, we shouldn't include a header to
1039 // make a particular module visible. Let the caller know they should
1040 // suggest an import instead.
1041 if (getLangOpts().ObjC || getLangOpts().CPlusPlusModules)
1042 return std::nullopt;
1043
1044 // If this is an accessible, non-textual header of M's top-level module
1045 // that transitively includes the given location and makes the
1046 // corresponding module visible, this is the thing to #include.
1047 return *FE;
1048 }
1049
1050 // FIXME: If we're bailing out due to a private header, we shouldn't suggest
1051 // an import either.
1052 if (InPrivateHeader)
1053 return std::nullopt;
1054
1055 // If the header is includable and has an include guard, assume the
1056 // intended way to expose its contents is by #include, not by importing a
1057 // module that transitively includes it.
1058 if (getHeaderSearchInfo().isFileMultipleIncludeGuarded(*FE))
1059 return *FE;
1060
1061 Loc = SM.getIncludeLoc(ID);
1062 }
1063
1064 return std::nullopt;
1065}
1066
1068 SourceLocation FilenameLoc, StringRef Filename, bool isAngled,
1069 ConstSearchDirIterator FromDir, const FileEntry *FromFile,
1070 ConstSearchDirIterator *CurDirArg, SmallVectorImpl<char> *SearchPath,
1071 SmallVectorImpl<char> *RelativePath,
1072 ModuleMap::KnownHeader *SuggestedModule, bool *IsMapped,
1073 bool *IsFrameworkFound, bool SkipCache, bool OpenFile, bool CacheFailures) {
1074 ConstSearchDirIterator CurDirLocal = nullptr;
1075 ConstSearchDirIterator &CurDir = CurDirArg ? *CurDirArg : CurDirLocal;
1076
1077 Module *RequestingModule = getModuleForLocation(
1078 FilenameLoc, LangOpts.ModulesValidateTextualHeaderIncludes);
1079
1080 // If the header lookup mechanism may be relative to the current inclusion
1081 // stack, record the parent #includes.
1083 bool BuildSystemModule = false;
1084 if (!FromDir && !FromFile) {
1086 OptionalFileEntryRef FileEnt = SourceMgr.getFileEntryRefForID(FID);
1087
1088 // If there is no file entry associated with this file, it must be the
1089 // predefines buffer or the module includes buffer. Any other file is not
1090 // lexed with a normal lexer, so it won't be scanned for preprocessor
1091 // directives.
1092 //
1093 // If we have the predefines buffer, resolve #include references (which come
1094 // from the -include command line argument) from the current working
1095 // directory instead of relative to the main file.
1096 //
1097 // If we have the module includes buffer, resolve #include references (which
1098 // come from header declarations in the module map) relative to the module
1099 // map file.
1100 if (!FileEnt) {
1101 if (FID == SourceMgr.getMainFileID() && MainFileDir) {
1102 auto IncludeDir =
1103 HeaderInfo.getModuleMap().shouldImportRelativeToBuiltinIncludeDir(
1104 Filename, getCurrentModule())
1105 ? HeaderInfo.getModuleMap().getBuiltinDir()
1106 : MainFileDir;
1107 Includers.push_back(std::make_pair(std::nullopt, *IncludeDir));
1108 BuildSystemModule = getCurrentModule()->IsSystem;
1109 } else if ((FileEnt = SourceMgr.getFileEntryRefForID(
1110 SourceMgr.getMainFileID()))) {
1111 auto CWD = FileMgr.getOptionalDirectoryRef(".");
1112 Includers.push_back(std::make_pair(*FileEnt, *CWD));
1113 }
1114 } else {
1115 Includers.push_back(std::make_pair(*FileEnt, FileEnt->getDir()));
1116 }
1117
1118 // MSVC searches the current include stack from top to bottom for
1119 // headers included by quoted include directives.
1120 // See: http://msdn.microsoft.com/en-us/library/36k2cdd4.aspx
1121 if (LangOpts.MSVCCompat && !isAngled) {
1122 for (IncludeStackInfo &ISEntry : llvm::reverse(IncludeMacroStack)) {
1123 if (IsFileLexer(ISEntry))
1124 if ((FileEnt = ISEntry.ThePPLexer->getFileEntry()))
1125 Includers.push_back(std::make_pair(*FileEnt, FileEnt->getDir()));
1126 }
1127 }
1128 }
1129
1130 CurDir = CurDirLookup;
1131
1132 if (FromFile) {
1133 // We're supposed to start looking from after a particular file. Search
1134 // the include path until we find that file or run out of files.
1135 ConstSearchDirIterator TmpCurDir = CurDir;
1136 ConstSearchDirIterator TmpFromDir = nullptr;
1137 while (OptionalFileEntryRef FE = HeaderInfo.LookupFile(
1138 Filename, FilenameLoc, isAngled, TmpFromDir, &TmpCurDir,
1139 Includers, SearchPath, RelativePath, RequestingModule,
1140 SuggestedModule, /*IsMapped=*/nullptr,
1141 /*IsFrameworkFound=*/nullptr, SkipCache)) {
1142 // Keep looking as if this file did a #include_next.
1143 TmpFromDir = TmpCurDir;
1144 ++TmpFromDir;
1145 if (&FE->getFileEntry() == FromFile) {
1146 // Found it.
1147 FromDir = TmpFromDir;
1148 CurDir = TmpCurDir;
1149 break;
1150 }
1151 }
1152 }
1153
1154 // Do a standard file entry lookup.
1155 OptionalFileEntryRef FE = HeaderInfo.LookupFile(
1156 Filename, FilenameLoc, isAngled, FromDir, &CurDir, Includers, SearchPath,
1157 RelativePath, RequestingModule, SuggestedModule, IsMapped,
1158 IsFrameworkFound, SkipCache, BuildSystemModule, OpenFile, CacheFailures);
1159 if (FE)
1160 return FE;
1161
1162 OptionalFileEntryRef CurFileEnt;
1163 // Otherwise, see if this is a subframework header. If so, this is relative
1164 // to one of the headers on the #include stack. Walk the list of the current
1165 // headers on the #include stack and pass them to HeaderInfo.
1166 if (IsFileLexer()) {
1167 if ((CurFileEnt = CurPPLexer->getFileEntry())) {
1168 if (OptionalFileEntryRef FE = HeaderInfo.LookupSubframeworkHeader(
1169 Filename, *CurFileEnt, SearchPath, RelativePath, RequestingModule,
1170 SuggestedModule)) {
1171 return FE;
1172 }
1173 }
1174 }
1175
1176 for (IncludeStackInfo &ISEntry : llvm::reverse(IncludeMacroStack)) {
1177 if (IsFileLexer(ISEntry)) {
1178 if ((CurFileEnt = ISEntry.ThePPLexer->getFileEntry())) {
1179 if (OptionalFileEntryRef FE = HeaderInfo.LookupSubframeworkHeader(
1180 Filename, *CurFileEnt, SearchPath, RelativePath,
1181 RequestingModule, SuggestedModule)) {
1182 return FE;
1183 }
1184 }
1185 }
1186 }
1187
1188 // Otherwise, we really couldn't find the file.
1189 return std::nullopt;
1190}
1191
1193 bool isAngled,
1194 bool OpenFile) {
1195 FileManager &FM = this->getFileManager();
1196 if (llvm::sys::path::is_absolute(Filename)) {
1197 // lookup path or immediately fail
1198 return FM.getOptionalFileRef(Filename, OpenFile, /*CacheFailure=*/true,
1199 /*IsText=*/false);
1200 }
1201
1202 auto SeparateComponents = [](SmallVectorImpl<char> &LookupPath,
1203 StringRef StartingFrom, StringRef FileName,
1204 bool RemoveInitialFileComponentFromLookupPath) {
1205 llvm::sys::path::native(StartingFrom, LookupPath);
1206 if (RemoveInitialFileComponentFromLookupPath)
1207 llvm::sys::path::remove_filename(LookupPath);
1208 if (!LookupPath.empty() &&
1209 !llvm::sys::path::is_separator(LookupPath.back())) {
1210 LookupPath.push_back(llvm::sys::path::get_separator().front());
1211 }
1212 LookupPath.append(FileName.begin(), FileName.end());
1213 };
1214
1215 // Otherwise, it's search time!
1216 SmallString<512> LookupPath;
1217 // Non-angled lookup
1218 if (!isAngled) {
1220 if (LookupFromFile) {
1221 // Use file-based lookup.
1222 SmallString<1024> TmpDir;
1223 TmpDir = LookupFromFile->getDir().getName();
1224 llvm::sys::path::append(TmpDir, Filename);
1225 if (!TmpDir.empty()) {
1226 OptionalFileEntryRef ShouldBeEntry = FM.getOptionalFileRef(
1227 TmpDir, OpenFile, /*CacheFailure=*/true, /*IsText=*/false);
1228 if (ShouldBeEntry)
1229 return ShouldBeEntry;
1230 }
1231 }
1232
1233 // Otherwise, do working directory lookup.
1234 LookupPath.clear();
1235 auto MaybeWorkingDirEntry = FM.getOptionalDirectoryRef(".");
1236 if (MaybeWorkingDirEntry) {
1237 DirectoryEntryRef WorkingDirEntry = *MaybeWorkingDirEntry;
1238 StringRef WorkingDir = WorkingDirEntry.getName();
1239 if (!WorkingDir.empty()) {
1240 SeparateComponents(LookupPath, WorkingDir, Filename, false);
1241 OptionalFileEntryRef ShouldBeEntry = FM.getOptionalFileRef(
1242 LookupPath, OpenFile, /*CacheFailure=*/true, /*IsText=*/false);
1243 if (ShouldBeEntry)
1244 return ShouldBeEntry;
1245 }
1246 }
1247 }
1248
1249 for (const auto &Entry : PPOpts.EmbedEntries) {
1250 LookupPath.clear();
1251 SeparateComponents(LookupPath, Entry, Filename, false);
1252 OptionalFileEntryRef ShouldBeEntry = FM.getOptionalFileRef(
1253 LookupPath, OpenFile, /*CacheFailure=*/true, /*IsText=*/false);
1254 if (ShouldBeEntry)
1255 return ShouldBeEntry;
1256 }
1257 return std::nullopt;
1258}
1259
1260//===----------------------------------------------------------------------===//
1261// Preprocessor Directive Handling.
1262//===----------------------------------------------------------------------===//
1263
1265public:
1267 : PP(pp), save(pp->DisableMacroExpansion) {
1268 if (pp->MacroExpansionInDirectivesOverride)
1269 pp->DisableMacroExpansion = false;
1270 }
1271
1273 PP->DisableMacroExpansion = save;
1274 }
1275
1276private:
1277 Preprocessor *PP;
1278 bool save;
1279};
1280
1281/// Process a directive while looking for the through header or a #pragma
1282/// hdrstop. The following directives are handled:
1283/// #include (to check if it is the through header)
1284/// #define (to warn about macros that don't match the PCH)
1285/// #pragma (to check for pragma hdrstop).
1286/// All other directives are completely discarded.
1288 SourceLocation HashLoc) {
1289 if (const IdentifierInfo *II = Result.getIdentifierInfo()) {
1290 if (II->getPPKeywordID() == tok::pp_define) {
1291 return HandleDefineDirective(Result,
1292 /*ImmediatelyAfterHeaderGuard=*/false);
1293 }
1294 if (SkippingUntilPCHThroughHeader &&
1295 II->getPPKeywordID() == tok::pp_include) {
1296 return HandleIncludeDirective(HashLoc, Result);
1297 }
1298 if (SkippingUntilPragmaHdrStop && II->getPPKeywordID() == tok::pp_pragma) {
1299 Lex(Result);
1300 auto *II = Result.getIdentifierInfo();
1301 if (II && II->getName() == "hdrstop")
1303 }
1304 }
1306}
1307
1308/// HandleDirective - This callback is invoked when the lexer sees a # token
1309/// at the start of a line. This consumes the directive, modifies the
1310/// lexer/preprocessor state, and advances the lexer(s) so that the next token
1311/// read is the correct one.
1313 // FIXME: Traditional: # with whitespace before it not recognized by K&R?
1314
1315 // We just parsed a # or @ character at the start of a line, so we're in
1316 // directive mode. Tell the lexer this so any newlines we see will be
1317 // converted into an EOD token (which terminates the directive).
1318 CurPPLexer->ParsingPreprocessorDirective = true;
1319 if (CurLexer) CurLexer->SetKeepWhitespaceMode(false);
1320
1321 bool ImmediatelyAfterTopLevelIfndef =
1322 CurPPLexer->MIOpt.getImmediatelyAfterTopLevelIfndef();
1323 CurPPLexer->MIOpt.resetImmediatelyAfterTopLevelIfndef();
1324
1325 ++NumDirectives;
1326
1327 // We are about to read a token. For the multiple-include optimization FA to
1328 // work, we have to remember if we had read any tokens *before* this
1329 // pp-directive.
1330 bool ReadAnyTokensBeforeDirective =CurPPLexer->MIOpt.getHasReadAnyTokensVal();
1331
1332 // Save the directive-introducing token ('#', '@', or import/module in C++20)
1333 // in case we need to return it later.
1334 Token Introducer = Result;
1335
1336 // Read the next token, the directive flavor. This isn't expanded due to
1337 // C99 6.10.3p8.
1338 if (Introducer.isOneOf(tok::hash, tok::at))
1340
1341 // C99 6.10.3p11: Is this preprocessor directive in macro invocation? e.g.:
1342 // #define A(x) #x
1343 // A(abc
1344 // #warning blah
1345 // def)
1346 // If so, the user is relying on undefined behavior, emit a diagnostic. Do
1347 // not support this for #include-like directives, since that can result in
1348 // terrible diagnostics, and does not work in GCC.
1349 if (InMacroArgs) {
1350 if (IdentifierInfo *II = Result.getIdentifierInfo()) {
1351 switch (II->getPPKeywordID()) {
1352 case tok::pp_include:
1353 case tok::pp_import:
1354 case tok::pp_include_next:
1355 case tok::pp___include_macros:
1356 case tok::pp_pragma:
1357 case tok::pp_embed:
1358 case tok::pp_module:
1359 case tok::pp___preprocessed_module:
1360 case tok::pp___preprocessed_import:
1361 Diag(Result, diag::err_embedded_directive)
1362 << Introducer.is(tok::hash) << II->getName();
1363 Diag(*ArgMacro, diag::note_macro_expansion_here)
1364 << ArgMacro->getIdentifierInfo();
1366 return;
1367 default:
1368 break;
1369 }
1370 }
1371 Diag(Result, diag::ext_embedded_directive);
1372 }
1373
1374 // Temporarily enable macro expansion if set so
1375 // and reset to previous state when returning from this function.
1376 ResetMacroExpansionHelper helper(this);
1377
1378 if (SkippingUntilPCHThroughHeader || SkippingUntilPragmaHdrStop)
1380 Introducer.getLocation());
1381
1382 switch (Result.getKind()) {
1383 case tok::eod:
1384 // Ignore the null directive with regards to the multiple-include
1385 // optimization, i.e. allow the null directive to appear outside of the
1386 // include guard and still enable the multiple-include optimization.
1387 CurPPLexer->MIOpt.SetReadToken(ReadAnyTokensBeforeDirective);
1388 return; // null directive.
1389 case tok::code_completion:
1391 if (CodeComplete)
1392 CodeComplete->CodeCompleteDirective(
1393 CurPPLexer->getConditionalStackDepth() > 0);
1394 return;
1395 case tok::numeric_constant: // # 7 GNU line marker directive.
1396 // In a .S file "# 4" may be a comment so don't treat it as a preprocessor
1397 // directive. However do permit it in the predefines file, as we use line
1398 // markers to mark the builtin macros as being in a system header.
1399 if (getLangOpts().AsmPreprocessor &&
1400 SourceMgr.getFileID(Introducer.getLocation()) != getPredefinesFileID())
1401 break;
1402 return HandleDigitDirective(Result);
1403 default:
1404 IdentifierInfo *II = Result.getIdentifierInfo();
1405 if (!II) break; // Not an identifier.
1406
1407 // Ask what the preprocessor keyword ID is.
1408 switch (II->getPPKeywordID()) {
1409 default: break;
1410 // C99 6.10.1 - Conditional Inclusion.
1411 case tok::pp_if:
1412 return HandleIfDirective(Result, Introducer,
1413 ReadAnyTokensBeforeDirective);
1414 case tok::pp_ifdef:
1415 return HandleIfdefDirective(Result, Introducer, false,
1416 true /*not valid for miopt*/);
1417 case tok::pp_ifndef:
1418 return HandleIfdefDirective(Result, Introducer, true,
1419 ReadAnyTokensBeforeDirective);
1420 case tok::pp_elif:
1421 case tok::pp_elifdef:
1422 case tok::pp_elifndef:
1423 return HandleElifFamilyDirective(Result, Introducer,
1424 II->getPPKeywordID());
1425
1426 case tok::pp_else:
1427 return HandleElseDirective(Result, Introducer);
1428 case tok::pp_endif:
1429 return HandleEndifDirective(Result);
1430
1431 // C99 6.10.2 - Source File Inclusion.
1432 case tok::pp_include:
1433 // Handle #include.
1434 return HandleIncludeDirective(Introducer.getLocation(), Result);
1435 case tok::pp___include_macros:
1436 // Handle -imacros.
1437 return HandleIncludeMacrosDirective(Introducer.getLocation(), Result);
1438
1439 // C99 6.10.3 - Macro Replacement.
1440 case tok::pp_define:
1441 return HandleDefineDirective(Result, ImmediatelyAfterTopLevelIfndef);
1442 case tok::pp_undef:
1443 return HandleUndefDirective();
1444
1445 // C99 6.10.4 - Line Control.
1446 case tok::pp_line:
1447 return HandleLineDirective();
1448
1449 // C99 6.10.5 - Error Directive.
1450 case tok::pp_error:
1451 return HandleUserDiagnosticDirective(Result, false);
1452
1453 // C99 6.10.6 - Pragma Directive.
1454 case tok::pp_pragma:
1455 return HandlePragmaDirective({PIK_HashPragma, Introducer.getLocation()});
1456 case tok::pp_module:
1457 case tok::pp___preprocessed_module:
1459 case tok::pp___preprocessed_import:
1461 // GNU Extensions.
1462 case tok::pp_import:
1463 switch (Introducer.getKind()) {
1464 case tok::hash:
1465 return HandleImportDirective(Introducer.getLocation(), Result);
1466 case tok::at:
1467 return HandleObjCImportDirective(Introducer, Result);
1468 case tok::kw_import:
1470 default:
1471 llvm_unreachable("not a valid import directive");
1472 }
1473 case tok::pp_include_next:
1474 return HandleIncludeNextDirective(Introducer.getLocation(), Result);
1475
1476 case tok::pp_warning:
1477 if (LangOpts.CPlusPlus)
1478 Diag(Result, LangOpts.CPlusPlus23
1479 ? diag::warn_cxx23_compat_warning_directive
1480 : diag::ext_pp_warning_directive)
1481 << /*C++23*/ 1;
1482 else
1483 Diag(Result, LangOpts.C23 ? diag::warn_c23_compat_warning_directive
1484 : diag::ext_pp_warning_directive)
1485 << /*C23*/ 0;
1486
1487 return HandleUserDiagnosticDirective(Result, true);
1488 case tok::pp_ident:
1489 return HandleIdentSCCSDirective(Result);
1490 case tok::pp_sccs:
1491 return HandleIdentSCCSDirective(Result);
1492 case tok::pp_embed:
1493 return HandleEmbedDirective(Introducer.getLocation(), Result);
1494 case tok::pp_assert:
1495 //isExtension = true; // FIXME: implement #assert
1496 break;
1497 case tok::pp_unassert:
1498 //isExtension = true; // FIXME: implement #unassert
1499 break;
1500
1501 case tok::pp___public_macro:
1502 if (getLangOpts().Modules || getLangOpts().ModulesLocalVisibility)
1503 return HandleMacroPublicDirective(Result);
1504 break;
1505
1506 case tok::pp___private_macro:
1507 if (getLangOpts().Modules || getLangOpts().ModulesLocalVisibility)
1508 return HandleMacroPrivateDirective();
1509 break;
1510 }
1511 break;
1512 }
1513
1514 // If this is a .S file, treat unknown # directives as non-preprocessor
1515 // directives. This is important because # may be a comment or introduce
1516 // various pseudo-ops. Just return the # token and push back the following
1517 // token to be lexed next time.
1518 if (getLangOpts().AsmPreprocessor) {
1519 auto Toks = std::make_unique<Token[]>(2);
1520 // Return the # and the token after it.
1521 Toks[0] = Introducer;
1522 Toks[1] = Result;
1523
1524 // If the second token is a hashhash token, then we need to translate it to
1525 // unknown so the token lexer doesn't try to perform token pasting.
1526 if (Result.is(tok::hashhash))
1527 Toks[1].setKind(tok::unknown);
1528
1529 // Enter this token stream so that we re-lex the tokens. Make sure to
1530 // enable macro expansion, in case the token after the # is an identifier
1531 // that is expanded.
1532 EnterTokenStream(std::move(Toks), 2, false, /*IsReinject*/false);
1533 return;
1534 }
1535
1536 // If we reached here, the preprocessing token is not valid!
1537 // Start suggesting if a similar directive found.
1538 Diag(Result, diag::err_pp_invalid_directive) << 0;
1539
1540 // Read the rest of the PP line.
1542
1543 // Okay, we're done parsing the directive.
1544}
1545
1546/// GetLineValue - Convert a numeric token into an unsigned value, emitting
1547/// Diagnostic DiagID if it is invalid, and returning the value in Val.
1548static bool GetLineValue(Token &DigitTok, unsigned &Val,
1549 unsigned DiagID, Preprocessor &PP,
1550 bool IsGNULineDirective=false) {
1551 if (DigitTok.isNot(tok::numeric_constant)) {
1552 PP.Diag(DigitTok, DiagID);
1553
1554 if (DigitTok.isNot(tok::eod))
1556 return true;
1557 }
1558
1559 SmallString<64> IntegerBuffer;
1560 IntegerBuffer.resize(DigitTok.getLength());
1561 const char *DigitTokBegin = &IntegerBuffer[0];
1562 bool Invalid = false;
1563 unsigned ActualLength = PP.getSpelling(DigitTok, DigitTokBegin, &Invalid);
1564 if (Invalid)
1565 return true;
1566
1567 // Verify that we have a simple digit-sequence, and compute the value. This
1568 // is always a simple digit string computed in decimal, so we do this manually
1569 // here.
1570 Val = 0;
1571 for (unsigned i = 0; i != ActualLength; ++i) {
1572 // C++1y [lex.fcon]p1:
1573 // Optional separating single quotes in a digit-sequence are ignored
1574 if (DigitTokBegin[i] == '\'')
1575 continue;
1576
1577 if (!isDigit(DigitTokBegin[i])) {
1578 PP.Diag(PP.AdvanceToTokenCharacter(DigitTok.getLocation(), i),
1579 diag::err_pp_line_digit_sequence) << IsGNULineDirective;
1581 return true;
1582 }
1583
1584 unsigned NextVal = Val*10+(DigitTokBegin[i]-'0');
1585 if (NextVal < Val) { // overflow.
1586 PP.Diag(DigitTok, DiagID);
1588 return true;
1589 }
1590 Val = NextVal;
1591 }
1592
1593 if (DigitTokBegin[0] == '0' && Val)
1594 PP.Diag(DigitTok.getLocation(), diag::warn_pp_line_decimal)
1595 << IsGNULineDirective;
1596
1597 return false;
1598}
1599
1600/// Handle a \#line directive: C99 6.10.4.
1601///
1602/// The two acceptable forms are:
1603/// \verbatim
1604/// # line digit-sequence
1605/// # line digit-sequence "s-char-sequence"
1606/// \endverbatim
1607void Preprocessor::HandleLineDirective() {
1608 // Read the line # and string argument. Per C99 6.10.4p5, these tokens are
1609 // expanded.
1610 Token DigitTok;
1611 Lex(DigitTok);
1612
1613 // Validate the number and convert it to an unsigned.
1614 unsigned LineNo;
1615 if (GetLineValue(DigitTok, LineNo, diag::err_pp_line_requires_integer,*this))
1616 return;
1617
1618 if (LineNo == 0)
1619 Diag(DigitTok, diag::ext_pp_line_zero);
1620
1621 // Enforce C99 6.10.4p3: "The digit sequence shall not specify ... a
1622 // number greater than 2147483647". C90 requires that the line # be <= 32767.
1623 unsigned LineLimit = 32768U;
1624 if (LangOpts.C99 || LangOpts.CPlusPlus11)
1625 LineLimit = 2147483648U;
1626 if (LineNo >= LineLimit)
1627 Diag(DigitTok, diag::ext_pp_line_too_big) << LineLimit;
1628 else if (LangOpts.CPlusPlus11 && LineNo >= 32768U)
1629 Diag(DigitTok, diag::warn_cxx98_compat_pp_line_too_big);
1630
1631 int FilenameID = -1;
1632 Token StrTok;
1633 Lex(StrTok);
1634
1635 // If the StrTok is "eod", then it wasn't present. Otherwise, it must be a
1636 // string followed by eod.
1637 if (StrTok.is(tok::eod))
1638 ; // ok
1639 else if (StrTok.isNot(tok::string_literal)) {
1640 Diag(StrTok, diag::err_pp_line_invalid_filename);
1642 return;
1643 } else if (StrTok.hasUDSuffix()) {
1644 Diag(StrTok, diag::err_invalid_string_udl);
1646 return;
1647 } else {
1648 // Parse and validate the string, converting it into a unique ID.
1649 StringLiteralParser Literal(StrTok, *this);
1650 assert(Literal.isOrdinary() && "Didn't allow wide strings in");
1651 if (Literal.hadError) {
1653 return;
1654 }
1655 if (Literal.Pascal) {
1656 Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
1658 return;
1659 }
1660 FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString());
1661
1662 // Verify that there is nothing after the string, other than EOD. Because
1663 // of C99 6.10.4p5, macros that expand to empty tokens are ok.
1664 CheckEndOfDirective("line", true);
1665 }
1666
1667 // Take the file kind of the file containing the #line directive. #line
1668 // directives are often used for generated sources from the same codebase, so
1669 // the new file should generally be classified the same way as the current
1670 // file. This is visible in GCC's pre-processed output, which rewrites #line
1671 // to GNU line markers.
1673 SourceMgr.getFileCharacteristic(DigitTok.getLocation());
1674
1675 SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID, false,
1676 false, FileKind);
1677
1678 if (Callbacks)
1679 Callbacks->FileChanged(CurPPLexer->getSourceLocation(),
1680 PPCallbacks::RenameFile, FileKind);
1681}
1682
1683/// ReadLineMarkerFlags - Parse and validate any flags at the end of a GNU line
1684/// marker directive.
1685static bool ReadLineMarkerFlags(bool &IsFileEntry, bool &IsFileExit,
1687 Preprocessor &PP) {
1688 unsigned FlagVal;
1689 Token FlagTok;
1690 PP.Lex(FlagTok);
1691 if (FlagTok.is(tok::eod)) return false;
1692 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP))
1693 return true;
1694
1695 if (FlagVal == 1) {
1696 IsFileEntry = true;
1697
1698 PP.Lex(FlagTok);
1699 if (FlagTok.is(tok::eod)) return false;
1700 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP))
1701 return true;
1702 } else if (FlagVal == 2) {
1703 IsFileExit = true;
1704
1706 // If we are leaving the current presumed file, check to make sure the
1707 // presumed include stack isn't empty!
1708 FileID CurFileID =
1709 SM.getDecomposedExpansionLoc(FlagTok.getLocation()).first;
1710 PresumedLoc PLoc = SM.getPresumedLoc(FlagTok.getLocation());
1711 if (PLoc.isInvalid())
1712 return true;
1713
1714 // If there is no include loc (main file) or if the include loc is in a
1715 // different physical file, then we aren't in a "1" line marker flag region.
1716 SourceLocation IncLoc = PLoc.getIncludeLoc();
1717 if (IncLoc.isInvalid() ||
1718 SM.getDecomposedExpansionLoc(IncLoc).first != CurFileID) {
1719 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_pop);
1721 return true;
1722 }
1723
1724 PP.Lex(FlagTok);
1725 if (FlagTok.is(tok::eod)) return false;
1726 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP))
1727 return true;
1728 }
1729
1730 // We must have 3 if there are still flags.
1731 if (FlagVal != 3) {
1732 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
1734 return true;
1735 }
1736
1737 FileKind = SrcMgr::C_System;
1738
1739 PP.Lex(FlagTok);
1740 if (FlagTok.is(tok::eod)) return false;
1741 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP))
1742 return true;
1743
1744 // We must have 4 if there is yet another flag.
1745 if (FlagVal != 4) {
1746 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
1748 return true;
1749 }
1750
1751 FileKind = SrcMgr::C_ExternCSystem;
1752
1753 PP.Lex(FlagTok);
1754 if (FlagTok.is(tok::eod)) return false;
1755
1756 // There are no more valid flags here.
1757 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
1759 return true;
1760}
1761
1762/// HandleDigitDirective - Handle a GNU line marker directive, whose syntax is
1763/// one of the following forms:
1764///
1765/// # 42
1766/// # 42 "file" ('1' | '2')?
1767/// # 42 "file" ('1' | '2')? '3' '4'?
1768///
1769void Preprocessor::HandleDigitDirective(Token &DigitTok) {
1770 // Validate the number and convert it to an unsigned. GNU does not have a
1771 // line # limit other than it fit in 32-bits.
1772 unsigned LineNo;
1773 if (GetLineValue(DigitTok, LineNo, diag::err_pp_linemarker_requires_integer,
1774 *this, true))
1775 return;
1776
1777 Token StrTok;
1778 Lex(StrTok);
1779
1780 bool IsFileEntry = false, IsFileExit = false;
1781 int FilenameID = -1;
1783
1784 // If the StrTok is "eod", then it wasn't present. Otherwise, it must be a
1785 // string followed by eod.
1786 if (StrTok.is(tok::eod)) {
1787 Diag(StrTok, diag::ext_pp_gnu_line_directive);
1788 // Treat this like "#line NN", which doesn't change file characteristics.
1789 FileKind = SourceMgr.getFileCharacteristic(DigitTok.getLocation());
1790 } else if (StrTok.isNot(tok::string_literal)) {
1791 Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
1793 return;
1794 } else if (StrTok.hasUDSuffix()) {
1795 Diag(StrTok, diag::err_invalid_string_udl);
1797 return;
1798 } else {
1799 // Parse and validate the string, converting it into a unique ID.
1800 StringLiteralParser Literal(StrTok, *this);
1801 assert(Literal.isOrdinary() && "Didn't allow wide strings in");
1802 if (Literal.hadError) {
1804 return;
1805 }
1806 if (Literal.Pascal) {
1807 Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
1809 return;
1810 }
1811
1812 // If a filename was present, read any flags that are present.
1813 if (ReadLineMarkerFlags(IsFileEntry, IsFileExit, FileKind, *this))
1814 return;
1815 if (!SourceMgr.isInPredefinedFile(DigitTok.getLocation()))
1816 Diag(StrTok, diag::ext_pp_gnu_line_directive);
1817
1818 // Exiting to an empty string means pop to the including file, so leave
1819 // FilenameID as -1 in that case.
1820 if (!(IsFileExit && Literal.GetString().empty()))
1821 FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString());
1822 }
1823
1824 // Create a line note with this information.
1825 SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID, IsFileEntry,
1826 IsFileExit, FileKind);
1827
1828 // If the preprocessor has callbacks installed, notify them of the #line
1829 // change. This is used so that the line marker comes out in -E mode for
1830 // example.
1831 if (Callbacks) {
1833 if (IsFileEntry)
1834 Reason = PPCallbacks::EnterFile;
1835 else if (IsFileExit)
1836 Reason = PPCallbacks::ExitFile;
1837
1838 Callbacks->FileChanged(CurPPLexer->getSourceLocation(), Reason, FileKind);
1839 }
1840}
1841
1842/// HandleUserDiagnosticDirective - Handle a #warning or #error directive.
1843///
1844void Preprocessor::HandleUserDiagnosticDirective(Token &Tok,
1845 bool isWarning) {
1846 // Read the rest of the line raw. We do this because we don't want macros
1847 // to be expanded and we don't require that the tokens be valid preprocessing
1848 // tokens. For example, this is allowed: "#warning ` 'foo". GCC does
1849 // collapse multiple consecutive white space between tokens, but this isn't
1850 // specified by the standard.
1851 SmallString<128> Message;
1852 CurLexer->ReadToEndOfLine(&Message);
1853
1854 // Find the first non-whitespace character, so that we can make the
1855 // diagnostic more succinct.
1856 StringRef Msg = Message.str().ltrim(' ');
1857
1858 if (isWarning)
1859 Diag(Tok, diag::pp_hash_warning) << Msg;
1860 else
1861 Diag(Tok, diag::err_pp_hash_error) << Msg;
1862}
1863
1864/// HandleIdentSCCSDirective - Handle a #ident/#sccs directive.
1865///
1866void Preprocessor::HandleIdentSCCSDirective(Token &Tok) {
1867 // Yes, this directive is an extension.
1868 Diag(Tok, diag::ext_pp_ident_directive);
1869
1870 // Read the string argument.
1871 Token StrTok;
1872 Lex(StrTok);
1873
1874 // If the token kind isn't a string, it's a malformed directive.
1875 if (StrTok.isNot(tok::string_literal) &&
1876 StrTok.isNot(tok::wide_string_literal)) {
1877 Diag(StrTok, diag::err_pp_malformed_ident);
1878 if (StrTok.isNot(tok::eod))
1880 return;
1881 }
1882
1883 if (StrTok.hasUDSuffix()) {
1884 Diag(StrTok, diag::err_invalid_string_udl);
1886 return;
1887 }
1888
1889 // Verify that there is nothing after the string, other than EOD.
1890 CheckEndOfDirective("ident");
1891
1892 if (Callbacks) {
1893 bool Invalid = false;
1894 std::string Str = getSpelling(StrTok, &Invalid);
1895 if (!Invalid)
1896 Callbacks->Ident(Tok.getLocation(), Str);
1897 }
1898}
1899
1900/// Handle a #public directive.
1901void Preprocessor::HandleMacroPublicDirective(Token &Tok) {
1902 Token MacroNameTok;
1903 ReadMacroName(MacroNameTok, MU_Undef);
1904
1905 // Error reading macro name? If so, diagnostic already issued.
1906 if (MacroNameTok.is(tok::eod))
1907 return;
1908
1909 // Check to see if this is the last token on the #__public_macro line.
1910 CheckEndOfDirective("__public_macro");
1911
1912 IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
1913 // Okay, we finally have a valid identifier to undef.
1914 MacroDirective *MD = getLocalMacroDirective(II);
1915
1916 // If the macro is not defined, this is an error.
1917 if (!MD) {
1918 Diag(MacroNameTok, diag::err_pp_visibility_non_macro) << II;
1919 return;
1920 }
1921
1922 // Note that this macro has now been exported.
1923 appendMacroDirective(II, AllocateVisibilityMacroDirective(
1924 MacroNameTok.getLocation(), /*isPublic=*/true));
1925}
1926
1927/// Handle a #private directive.
1928void Preprocessor::HandleMacroPrivateDirective() {
1929 Token MacroNameTok;
1930 ReadMacroName(MacroNameTok, MU_Undef);
1931
1932 // Error reading macro name? If so, diagnostic already issued.
1933 if (MacroNameTok.is(tok::eod))
1934 return;
1935
1936 // Check to see if this is the last token on the #__private_macro line.
1937 CheckEndOfDirective("__private_macro");
1938
1939 IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
1940 // Okay, we finally have a valid identifier to undef.
1941 MacroDirective *MD = getLocalMacroDirective(II);
1942
1943 // If the macro is not defined, this is an error.
1944 if (!MD) {
1945 Diag(MacroNameTok, diag::err_pp_visibility_non_macro) << II;
1946 return;
1947 }
1948
1949 // Note that this macro has now been marked private.
1950 appendMacroDirective(II, AllocateVisibilityMacroDirective(
1951 MacroNameTok.getLocation(), /*isPublic=*/false));
1952}
1953
1954//===----------------------------------------------------------------------===//
1955// Preprocessor Include Directive Handling.
1956//===----------------------------------------------------------------------===//
1957
1958/// GetIncludeFilenameSpelling - Turn the specified lexer token into a fully
1959/// checked and spelled filename, e.g. as an operand of \#include. This returns
1960/// true if the input filename was in <>'s or false if it were in ""'s. The
1961/// caller is expected to provide a buffer that is large enough to hold the
1962/// spelling of the filename, but is also expected to handle the case when
1963/// this method decides to use a different buffer.
1965 StringRef &Buffer) {
1966 // Get the text form of the filename.
1967 assert(!Buffer.empty() && "Can't have tokens with empty spellings!");
1968
1969 // FIXME: Consider warning on some of the cases described in C11 6.4.7/3 and
1970 // C++20 [lex.header]/2:
1971 //
1972 // If `"`, `'`, `\`, `/*`, or `//` appears in a header-name, then
1973 // in C: behavior is undefined
1974 // in C++: program is conditionally-supported with implementation-defined
1975 // semantics
1976
1977 // Make sure the filename is <x> or "x".
1978 bool isAngled;
1979 if (Buffer[0] == '<') {
1980 if (Buffer.back() != '>') {
1981 Diag(Loc, diag::err_pp_expects_filename);
1982 Buffer = StringRef();
1983 return true;
1984 }
1985 isAngled = true;
1986 } else if (Buffer[0] == '"') {
1987 if (Buffer.back() != '"') {
1988 Diag(Loc, diag::err_pp_expects_filename);
1989 Buffer = StringRef();
1990 return true;
1991 }
1992 isAngled = false;
1993 } else {
1994 Diag(Loc, diag::err_pp_expects_filename);
1995 Buffer = StringRef();
1996 return true;
1997 }
1998
1999 // Diagnose #include "" as invalid.
2000 if (Buffer.size() <= 2) {
2001 Diag(Loc, diag::err_pp_empty_filename);
2002 Buffer = StringRef();
2003 return true;
2004 }
2005
2006 // Skip the brackets.
2007 Buffer = Buffer.substr(1, Buffer.size()-2);
2008 return isAngled;
2009}
2010
2011/// Push a token onto the token stream containing an annotation.
2013 tok::TokenKind Kind,
2014 void *AnnotationVal) {
2015 // FIXME: Produce this as the current token directly, rather than
2016 // allocating a new token for it.
2017 auto Tok = std::make_unique<Token[]>(1);
2018 Tok[0].startToken();
2019 Tok[0].setKind(Kind);
2020 Tok[0].setLocation(Range.getBegin());
2021 Tok[0].setAnnotationEndLoc(Range.getEnd());
2022 Tok[0].setAnnotationValue(AnnotationVal);
2023 EnterTokenStream(std::move(Tok), 1, true, /*IsReinject*/ false);
2024}
2025
2026/// Produce a diagnostic informing the user that a #include or similar
2027/// was implicitly treated as a module import.
2029 Token &IncludeTok,
2031 SourceLocation PathEnd) {
2032 SmallString<128> PathString;
2033 for (size_t I = 0, N = Path.size(); I != N; ++I) {
2034 if (I)
2035 PathString += '.';
2036 PathString += Path[I].getIdentifierInfo()->getName();
2037 }
2038
2039 int IncludeKind = 0;
2040 switch (IncludeTok.getIdentifierInfo()->getPPKeywordID()) {
2041 case tok::pp_include:
2042 IncludeKind = 0;
2043 break;
2044
2045 case tok::pp_import:
2046 IncludeKind = 1;
2047 break;
2048
2049 case tok::pp_include_next:
2050 IncludeKind = 2;
2051 break;
2052
2053 case tok::pp___include_macros:
2054 IncludeKind = 3;
2055 break;
2056
2057 default:
2058 llvm_unreachable("unknown include directive kind");
2059 }
2060
2061 PP.Diag(HashLoc, diag::remark_pp_include_directive_modular_translation)
2062 << IncludeKind << PathString;
2063}
2064
2065// Given a vector of path components and a string containing the real
2066// path to the file, build a properly-cased replacement in the vector,
2067// and return true if the replacement should be suggested.
2069 StringRef RealPathName,
2070 llvm::sys::path::Style Separator) {
2071 auto RealPathComponentIter = llvm::sys::path::rbegin(RealPathName);
2072 auto RealPathComponentEnd = llvm::sys::path::rend(RealPathName);
2073 int Cnt = 0;
2074 bool SuggestReplacement = false;
2075
2076 auto IsSep = [Separator](StringRef Component) {
2077 return Component.size() == 1 &&
2078 llvm::sys::path::is_separator(Component[0], Separator);
2079 };
2080
2081 // Below is a best-effort to handle ".." in paths. It is admittedly
2082 // not 100% correct in the presence of symlinks.
2083 for (auto &Component : llvm::reverse(Components)) {
2084 if ("." == Component) {
2085 } else if (".." == Component) {
2086 ++Cnt;
2087 } else if (Cnt) {
2088 --Cnt;
2089 } else if (RealPathComponentIter != RealPathComponentEnd) {
2090 if (!IsSep(Component) && !IsSep(*RealPathComponentIter) &&
2091 Component != *RealPathComponentIter) {
2092 // If these non-separator path components differ by more than just case,
2093 // then we may be looking at symlinked paths. Bail on this diagnostic to
2094 // avoid noisy false positives.
2095 SuggestReplacement =
2096 RealPathComponentIter->equals_insensitive(Component);
2097 if (!SuggestReplacement)
2098 break;
2099 Component = *RealPathComponentIter;
2100 }
2101 ++RealPathComponentIter;
2102 }
2103 }
2104 return SuggestReplacement;
2105}
2106
2108 const TargetInfo &TargetInfo,
2109 const Module &M,
2110 DiagnosticsEngine &Diags) {
2111 Module::Requirement Requirement;
2113 Module *ShadowingModule = nullptr;
2114 if (M.isAvailable(LangOpts, TargetInfo, Requirement, MissingHeader,
2115 ShadowingModule))
2116 return false;
2117
2118 if (MissingHeader.FileNameLoc.isValid()) {
2119 Diags.Report(MissingHeader.FileNameLoc, diag::err_module_header_missing)
2120 << MissingHeader.IsUmbrella << MissingHeader.FileName;
2121 } else if (ShadowingModule) {
2122 Diags.Report(M.DefinitionLoc, diag::err_module_shadowed) << M.Name;
2123 Diags.Report(ShadowingModule->DefinitionLoc,
2124 diag::note_previous_definition);
2125 } else {
2126 // FIXME: Track the location at which the requirement was specified, and
2127 // use it here.
2128 Diags.Report(M.DefinitionLoc, diag::err_module_unavailable)
2129 << M.getFullModuleName() << Requirement.RequiredState
2130 << Requirement.FeatureName;
2131 }
2132 return true;
2133}
2134
2135std::pair<ConstSearchDirIterator, const FileEntry *>
2136Preprocessor::getIncludeNextStart(const Token &IncludeNextTok) const {
2137 // #include_next is like #include, except that we start searching after
2138 // the current found directory. If we can't do this, issue a
2139 // diagnostic.
2140 ConstSearchDirIterator Lookup = CurDirLookup;
2141 const FileEntry *LookupFromFile = nullptr;
2142
2143 if (isInPrimaryFile() && LangOpts.IsHeaderFile) {
2144 // If the main file is a header, then it's either for PCH/AST generation,
2145 // or libclang opened it. Either way, handle it as a normal include below
2146 // and do not complain about include_next.
2147 } else if (isInPrimaryFile()) {
2148 Lookup = nullptr;
2149 Diag(IncludeNextTok, diag::pp_include_next_in_primary);
2150 } else if (CurLexerSubmodule) {
2151 // Start looking up in the directory *after* the one in which the current
2152 // file would be found, if any.
2153 assert(CurPPLexer && "#include_next directive in macro?");
2154 if (auto FE = CurPPLexer->getFileEntry())
2155 LookupFromFile = *FE;
2156 Lookup = nullptr;
2157 } else if (!Lookup) {
2158 // The current file was not found by walking the include path. Either it
2159 // is the primary file (handled above), or it was found by absolute path,
2160 // or it was found relative to such a file.
2161 // FIXME: Track enough information so we know which case we're in.
2162 Diag(IncludeNextTok, diag::pp_include_next_absolute_path);
2163 } else {
2164 // Start looking up in the next directory.
2165 ++Lookup;
2166 }
2167
2168 return {Lookup, LookupFromFile};
2169}
2170
2171/// HandleIncludeDirective - The "\#include" tokens have just been read, read
2172/// the file to be included from the lexer, then include it! This is a common
2173/// routine with functionality shared between \#include, \#include_next and
2174/// \#import. LookupFrom is set when this is a \#include_next directive, it
2175/// specifies the file to start searching from.
2176void Preprocessor::HandleIncludeDirective(SourceLocation HashLoc,
2177 Token &IncludeTok,
2178 ConstSearchDirIterator LookupFrom,
2179 const FileEntry *LookupFromFile) {
2180 Token FilenameTok;
2181 if (LexHeaderName(FilenameTok))
2182 return;
2183
2184 if (FilenameTok.isNot(tok::header_name)) {
2185 if (FilenameTok.is(tok::identifier) &&
2186 (PPOpts.SingleFileParseMode || PPOpts.SingleModuleParseMode)) {
2187 // If we saw #include IDENTIFIER and lexing didn't turn in into a header
2188 // name, it was undefined. In 'single-{file,module}-parse' mode, just skip
2189 // the directive without emitting diagnostics - the identifier might be
2190 // normally defined in previously-skipped include directive.
2192 return;
2193 }
2194
2195 Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
2196 if (FilenameTok.isNot(tok::eod))
2198 return;
2199 }
2200
2201 // Verify that there is nothing after the filename, other than EOD. Note
2202 // that we allow macros that expand to nothing after the filename, because
2203 // this falls into the category of "#include pp-tokens new-line" specified
2204 // in C99 6.10.2p4.
2205 SourceLocation EndLoc =
2206 CheckEndOfDirective(IncludeTok.getIdentifierInfo()->getNameStart(), true);
2207
2208 auto Action = HandleHeaderIncludeOrImport(HashLoc, IncludeTok, FilenameTok,
2209 EndLoc, LookupFrom, LookupFromFile);
2210 switch (Action.Kind) {
2211 case ImportAction::None:
2212 case ImportAction::SkippedModuleImport:
2213 break;
2214 case ImportAction::ModuleBegin:
2215 EnterAnnotationToken(SourceRange(HashLoc, EndLoc),
2216 tok::annot_module_begin, Action.ModuleForHeader);
2217 break;
2218 case ImportAction::HeaderUnitImport:
2219 EnterAnnotationToken(SourceRange(HashLoc, EndLoc), tok::annot_header_unit,
2220 Action.ModuleForHeader);
2221 break;
2222 case ImportAction::ModuleImport:
2223 EnterAnnotationToken(SourceRange(HashLoc, EndLoc),
2224 tok::annot_module_include, Action.ModuleForHeader);
2225 break;
2226 case ImportAction::Failure:
2227 assert(TheModuleLoader.HadFatalFailure &&
2228 "This should be an early exit only to a fatal error");
2229 TheModuleLoader.HadFatalFailure = true;
2230 IncludeTok.setKind(tok::eof);
2231 CurLexer->cutOffLexing();
2232 return;
2233 }
2234}
2235
2236OptionalFileEntryRef Preprocessor::LookupHeaderIncludeOrImport(
2237 ConstSearchDirIterator *CurDir, StringRef &Filename,
2238 SourceLocation FilenameLoc, CharSourceRange FilenameRange,
2239 const Token &FilenameTok, bool &IsFrameworkFound, bool IsImportDecl,
2240 bool &IsMapped, ConstSearchDirIterator LookupFrom,
2241 const FileEntry *LookupFromFile, StringRef &LookupFilename,
2242 SmallVectorImpl<char> &RelativePath, SmallVectorImpl<char> &SearchPath,
2243 ModuleMap::KnownHeader &SuggestedModule, bool isAngled) {
2244 auto DiagnoseHeaderInclusion = [&](FileEntryRef FE) {
2245 if (LangOpts.AsmPreprocessor)
2246 return;
2247
2248 Module *RequestingModule = getModuleForLocation(
2249 FilenameLoc, LangOpts.ModulesValidateTextualHeaderIncludes);
2250 bool RequestingModuleIsModuleInterface =
2251 !SourceMgr.isInMainFile(FilenameLoc);
2252
2253 HeaderInfo.getModuleMap().diagnoseHeaderInclusion(
2254 RequestingModule, RequestingModuleIsModuleInterface, FilenameLoc,
2255 Filename, FE);
2256 };
2257
2259 FilenameLoc, LookupFilename, isAngled, LookupFrom, LookupFromFile, CurDir,
2260 Callbacks ? &SearchPath : nullptr, Callbacks ? &RelativePath : nullptr,
2261 &SuggestedModule, &IsMapped, &IsFrameworkFound);
2262 if (File) {
2263 DiagnoseHeaderInclusion(*File);
2264 return File;
2265 }
2266
2267 // Give the clients a chance to silently skip this include.
2268 if (Callbacks && Callbacks->FileNotFound(Filename))
2269 return std::nullopt;
2270
2271 if (SuppressIncludeNotFoundError)
2272 return std::nullopt;
2273
2274 // If the file could not be located and it was included via angle
2275 // brackets, we can attempt a lookup as though it were a quoted path to
2276 // provide the user with a possible fixit.
2277 if (isAngled) {
2279 FilenameLoc, LookupFilename, false, LookupFrom, LookupFromFile, CurDir,
2280 Callbacks ? &SearchPath : nullptr, Callbacks ? &RelativePath : nullptr,
2281 &SuggestedModule, &IsMapped,
2282 /*IsFrameworkFound=*/nullptr);
2283 if (File) {
2284 DiagnoseHeaderInclusion(*File);
2285 Diag(FilenameTok, diag::err_pp_file_not_found_angled_include_not_fatal)
2286 << Filename << IsImportDecl
2287 << FixItHint::CreateReplacement(FilenameRange,
2288 "\"" + Filename.str() + "\"");
2289 return File;
2290 }
2291 }
2292
2293 // Check for likely typos due to leading or trailing non-isAlphanumeric
2294 // characters
2295 StringRef OriginalFilename = Filename;
2296 if (LangOpts.SpellChecking) {
2297 // A heuristic to correct a typo file name by removing leading and
2298 // trailing non-isAlphanumeric characters.
2299 auto CorrectTypoFilename = [](llvm::StringRef Filename) {
2300 Filename = Filename.drop_until(isAlphanumeric);
2301 while (!Filename.empty() && !isAlphanumeric(Filename.back())) {
2302 Filename = Filename.drop_back();
2303 }
2304 return Filename;
2305 };
2306 StringRef TypoCorrectionName = CorrectTypoFilename(Filename);
2307 StringRef TypoCorrectionLookupName = CorrectTypoFilename(LookupFilename);
2308
2310 FilenameLoc, TypoCorrectionLookupName, isAngled, LookupFrom,
2311 LookupFromFile, CurDir, Callbacks ? &SearchPath : nullptr,
2312 Callbacks ? &RelativePath : nullptr, &SuggestedModule, &IsMapped,
2313 /*IsFrameworkFound=*/nullptr);
2314 if (File) {
2315 DiagnoseHeaderInclusion(*File);
2316 auto Hint =
2318 FilenameRange, "<" + TypoCorrectionName.str() + ">")
2319 : FixItHint::CreateReplacement(
2320 FilenameRange, "\"" + TypoCorrectionName.str() + "\"");
2321 Diag(FilenameTok, diag::err_pp_file_not_found_typo_not_fatal)
2322 << OriginalFilename << TypoCorrectionName << Hint;
2323 // We found the file, so set the Filename to the name after typo
2324 // correction.
2325 Filename = TypoCorrectionName;
2326 LookupFilename = TypoCorrectionLookupName;
2327 return File;
2328 }
2329 }
2330
2331 // If the file is still not found, just go with the vanilla diagnostic
2332 assert(!File && "expected missing file");
2333 Diag(FilenameTok, diag::err_pp_file_not_found)
2334 << OriginalFilename << FilenameRange;
2335 if (IsFrameworkFound) {
2336 size_t SlashPos = OriginalFilename.find('/');
2337 assert(SlashPos != StringRef::npos &&
2338 "Include with framework name should have '/' in the filename");
2339 StringRef FrameworkName = OriginalFilename.substr(0, SlashPos);
2340 FrameworkCacheEntry &CacheEntry =
2341 HeaderInfo.LookupFrameworkCache(FrameworkName);
2342 assert(CacheEntry.Directory && "Found framework should be in cache");
2343 Diag(FilenameTok, diag::note_pp_framework_without_header)
2344 << OriginalFilename.substr(SlashPos + 1) << FrameworkName
2345 << CacheEntry.Directory->getName();
2346 }
2347
2348 return std::nullopt;
2349}
2350
2351/// Handle either a #include-like directive or an import declaration that names
2352/// a header file.
2353///
2354/// \param HashLoc The location of the '#' token for an include, or
2355/// SourceLocation() for an import declaration.
2356/// \param IncludeTok The include / include_next / import token.
2357/// \param FilenameTok The header-name token.
2358/// \param EndLoc The location at which any imported macros become visible.
2359/// \param LookupFrom For #include_next, the starting directory for the
2360/// directory lookup.
2361/// \param LookupFromFile For #include_next, the starting file for the directory
2362/// lookup.
2363Preprocessor::ImportAction Preprocessor::HandleHeaderIncludeOrImport(
2364 SourceLocation HashLoc, Token &IncludeTok, Token &FilenameTok,
2365 SourceLocation EndLoc, ConstSearchDirIterator LookupFrom,
2366 const FileEntry *LookupFromFile) {
2367 SmallString<128> FilenameBuffer;
2368 StringRef Filename = getSpelling(FilenameTok, FilenameBuffer);
2369 SourceLocation CharEnd = FilenameTok.getEndLoc();
2370
2371 CharSourceRange FilenameRange
2372 = CharSourceRange::getCharRange(FilenameTok.getLocation(), CharEnd);
2373 StringRef OriginalFilename = Filename;
2374 bool isAngled =
2375 GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
2376
2377 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
2378 // error.
2379 if (Filename.empty())
2380 return {ImportAction::None};
2381 if (Filename.ends_with(' ') || Filename.ends_with('.')) {
2382 unsigned Selection = Filename.ends_with('.') ? 1 : 0;
2383 Diag(FilenameTok, diag::pp_nonportable_path_trailing)
2384 << Filename << Selection;
2385 }
2386
2387 bool IsImportDecl = HashLoc.isInvalid();
2388 SourceLocation StartLoc = IsImportDecl ? IncludeTok.getLocation() : HashLoc;
2389
2390 // Complain about attempts to #include files in an audit pragma.
2391 if (PragmaARCCFCodeAuditedInfo.getLoc().isValid()) {
2392 Diag(StartLoc, diag::err_pp_include_in_arc_cf_code_audited) << IsImportDecl;
2393 Diag(PragmaARCCFCodeAuditedInfo.getLoc(), diag::note_pragma_entered_here);
2394
2395 // Immediately leave the pragma.
2396 PragmaARCCFCodeAuditedInfo = IdentifierLoc();
2397 }
2398
2399 // Complain about attempts to #include files in an assume-nonnull pragma.
2400 if (PragmaAssumeNonNullLoc.isValid()) {
2401 Diag(StartLoc, diag::err_pp_include_in_assume_nonnull) << IsImportDecl;
2402 Diag(PragmaAssumeNonNullLoc, diag::note_pragma_entered_here);
2403
2404 // Immediately leave the pragma.
2405 PragmaAssumeNonNullLoc = SourceLocation();
2406 }
2407
2408 if (HeaderInfo.HasIncludeAliasMap()) {
2409 // Map the filename with the brackets still attached. If the name doesn't
2410 // map to anything, fall back on the filename we've already gotten the
2411 // spelling for.
2412 StringRef NewName = HeaderInfo.MapHeaderToIncludeAlias(OriginalFilename);
2413 if (!NewName.empty())
2414 Filename = NewName;
2415 }
2416
2417 // Search include directories.
2418 bool IsMapped = false;
2419 bool IsFrameworkFound = false;
2420 ConstSearchDirIterator CurDir = nullptr;
2421 SmallString<1024> SearchPath;
2422 SmallString<1024> RelativePath;
2423 // We get the raw path only if we have 'Callbacks' to which we later pass
2424 // the path.
2425 ModuleMap::KnownHeader SuggestedModule;
2426 SourceLocation FilenameLoc = FilenameTok.getLocation();
2427 StringRef LookupFilename = Filename;
2428
2429 // Normalize slashes when compiling with -fms-extensions on non-Windows. This
2430 // is unnecessary on Windows since the filesystem there handles backslashes.
2431 SmallString<128> NormalizedPath;
2432 llvm::sys::path::Style BackslashStyle = llvm::sys::path::Style::native;
2433 if (is_style_posix(BackslashStyle) && LangOpts.MicrosoftExt) {
2434 NormalizedPath = Filename.str();
2435 llvm::sys::path::native(NormalizedPath);
2436 LookupFilename = NormalizedPath;
2437 BackslashStyle = llvm::sys::path::Style::windows;
2438 }
2439
2440 OptionalFileEntryRef File = LookupHeaderIncludeOrImport(
2441 &CurDir, Filename, FilenameLoc, FilenameRange, FilenameTok,
2442 IsFrameworkFound, IsImportDecl, IsMapped, LookupFrom, LookupFromFile,
2443 LookupFilename, RelativePath, SearchPath, SuggestedModule, isAngled);
2444
2445 if (usingPCHWithThroughHeader() && SkippingUntilPCHThroughHeader) {
2446 if (File && isPCHThroughHeader(&File->getFileEntry()))
2447 SkippingUntilPCHThroughHeader = false;
2448 return {ImportAction::None};
2449 }
2450
2451 // Should we enter the source file? Set to Skip if either the source file is
2452 // known to have no effect beyond its effect on module visibility -- that is,
2453 // if it's got an include guard that is already defined, set to Import if it
2454 // is a modular header we've already built and should import.
2455
2456 // For C++20 Modules
2457 // [cpp.include]/7 If the header identified by the header-name denotes an
2458 // importable header, it is implementation-defined whether the #include
2459 // preprocessing directive is instead replaced by an import directive.
2460 // For this implementation, the translation is permitted when we are parsing
2461 // the Global Module Fragment, and not otherwise (the cases where it would be
2462 // valid to replace an include with an import are highly constrained once in
2463 // named module purview; this choice avoids considerable complexity in
2464 // determining valid cases).
2465
2466 enum { Enter, Import, Skip, IncludeLimitReached } Action = Enter;
2467
2468 if (PPOpts.SingleFileParseMode)
2469 Action = IncludeLimitReached;
2470
2471 // If we've reached the max allowed include depth, it is usually due to an
2472 // include cycle. Don't enter already processed files again as it can lead to
2473 // reaching the max allowed include depth again.
2474 if (Action == Enter && HasReachedMaxIncludeDepth && File &&
2476 Action = IncludeLimitReached;
2477
2478 // FIXME: We do not have a good way to disambiguate C++ clang modules from
2479 // C++ standard modules (other than use/non-use of Header Units).
2480
2481 Module *ModuleToImport = SuggestedModule.getModule();
2482
2483 bool MaybeTranslateInclude = Action == Enter && File && ModuleToImport &&
2484 !ModuleToImport->isForBuilding(getLangOpts());
2485
2486 // Maybe a usable Header Unit
2487 bool UsableHeaderUnit = false;
2488 if (getLangOpts().CPlusPlusModules && ModuleToImport &&
2489 ModuleToImport->isHeaderUnit()) {
2490 if (TrackGMFState.inGMF() || IsImportDecl)
2491 UsableHeaderUnit = true;
2492 else if (!IsImportDecl) {
2493 // This is a Header Unit that we do not include-translate
2494 ModuleToImport = nullptr;
2495 }
2496 }
2497 // Maybe a usable clang header module.
2498 bool UsableClangHeaderModule =
2499 (getLangOpts().CPlusPlusModules || getLangOpts().Modules) &&
2500 ModuleToImport && !ModuleToImport->isHeaderUnit();
2501
2502 // Determine whether we should try to import the module for this #include, if
2503 // there is one. Don't do so if precompiled module support is disabled or we
2504 // are processing this module textually (because we're building the module).
2505 if (MaybeTranslateInclude && (UsableHeaderUnit || UsableClangHeaderModule)) {
2506 // If this include corresponds to a module but that module is
2507 // unavailable, diagnose the situation and bail out.
2508 // FIXME: Remove this; loadModule does the same check (but produces
2509 // slightly worse diagnostics).
2510 if (checkModuleIsAvailable(getLangOpts(), getTargetInfo(), *ModuleToImport,
2511 getDiagnostics())) {
2512 Diag(FilenameTok.getLocation(),
2513 diag::note_implicit_top_level_module_import_here)
2514 << ModuleToImport->getTopLevelModuleName();
2515 return {ImportAction::None};
2516 }
2517
2518 // Compute the module access path corresponding to this module.
2519 // FIXME: Should we have a second loadModule() overload to avoid this
2520 // extra lookup step?
2521 SmallVector<IdentifierLoc, 2> Path;
2522 for (Module *Mod = ModuleToImport; Mod; Mod = Mod->Parent)
2523 Path.emplace_back(FilenameTok.getLocation(),
2524 getIdentifierInfo(Mod->Name));
2525 std::reverse(Path.begin(), Path.end());
2526
2527 // Warn that we're replacing the include/import with a module import.
2528 if (!IsImportDecl)
2529 diagnoseAutoModuleImport(*this, StartLoc, IncludeTok, Path, CharEnd);
2530
2531 // Load the module to import its macros. We'll make the declarations
2532 // visible when the parser gets here.
2533 // FIXME: Pass ModuleToImport in here rather than converting it to a path
2534 // and making the module loader convert it back again.
2535 ModuleLoadResult Imported = TheModuleLoader.loadModule(
2536 IncludeTok.getLocation(), Path, Module::Hidden,
2537 /*IsInclusionDirective=*/true);
2538 assert((Imported == nullptr || Imported == ModuleToImport) &&
2539 "the imported module is different than the suggested one");
2540
2541 if (Imported) {
2542 Action = Import;
2543 } else if (Imported.isMissingExpected()) {
2545 static_cast<Module *>(Imported)->getTopLevelModule());
2546 // We failed to find a submodule that we assumed would exist (because it
2547 // was in the directory of an umbrella header, for instance), but no
2548 // actual module containing it exists (because the umbrella header is
2549 // incomplete). Treat this as a textual inclusion.
2550 ModuleToImport = nullptr;
2551 } else if (Imported.isConfigMismatch()) {
2552 // On a configuration mismatch, enter the header textually. We still know
2553 // that it's part of the corresponding module.
2554 } else {
2555 // We hit an error processing the import. Bail out.
2557 // With a fatal failure in the module loader, we abort parsing.
2558 Token &Result = IncludeTok;
2559 assert(CurLexer && "#include but no current lexer set!");
2560 Result.startToken();
2561 CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd, tok::eof);
2562 CurLexer->cutOffLexing();
2563 }
2564 return {ImportAction::None};
2565 }
2566 }
2567
2568 // The #included file will be considered to be a system header if either it is
2569 // in a system include directory, or if the #includer is a system include
2570 // header.
2571 SrcMgr::CharacteristicKind FileCharacter =
2572 SourceMgr.getFileCharacteristic(FilenameTok.getLocation());
2573 if (File)
2574 FileCharacter = std::max(HeaderInfo.getFileDirFlavor(*File), FileCharacter);
2575
2576 // If this is a '#import' or an import-declaration, don't re-enter the file.
2577 //
2578 // FIXME: If we have a suggested module for a '#include', and we've already
2579 // visited this file, don't bother entering it again. We know it has no
2580 // further effect.
2581 bool EnterOnce =
2582 IsImportDecl ||
2583 IncludeTok.getIdentifierInfo()->getPPKeywordID() == tok::pp_import;
2584
2585 bool IsFirstIncludeOfFile = false;
2586
2587 // Ask HeaderInfo if we should enter this #include file. If not, #including
2588 // this file will have no effect.
2589 if (Action == Enter && File &&
2590 !HeaderInfo.ShouldEnterIncludeFile(*this, *File, EnterOnce,
2591 getLangOpts().Modules, ModuleToImport,
2592 IsFirstIncludeOfFile)) {
2593 // C++ standard modules:
2594 // If we are not in the GMF, then we textually include only
2595 // clang modules:
2596 // Even if we've already preprocessed this header once and know that we
2597 // don't need to see its contents again, we still need to import it if it's
2598 // modular because we might not have imported it from this submodule before.
2599 //
2600 // FIXME: We don't do this when compiling a PCH because the AST
2601 // serialization layer can't cope with it. This means we get local
2602 // submodule visibility semantics wrong in that case.
2603 if (UsableHeaderUnit && !getLangOpts().CompilingPCH)
2604 Action = TrackGMFState.inGMF() ? Import : Skip;
2605 else
2606 Action = (ModuleToImport && !getLangOpts().CompilingPCH) ? Import : Skip;
2607 }
2608
2609 // Check for circular inclusion of the main file.
2610 // We can't generate a consistent preamble with regard to the conditional
2611 // stack if the main file is included again as due to the preamble bounds
2612 // some directives (e.g. #endif of a header guard) will never be seen.
2613 // Since this will lead to confusing errors, avoid the inclusion.
2614 if (Action == Enter && File && PreambleConditionalStack.isRecording() &&
2615 SourceMgr.isMainFile(File->getFileEntry())) {
2616 Diag(FilenameTok.getLocation(),
2617 diag::err_pp_including_mainfile_in_preamble);
2618 return {ImportAction::None};
2619 }
2620
2621 if (Callbacks && !IsImportDecl) {
2622 // Notify the callback object that we've seen an inclusion directive.
2623 // FIXME: Use a different callback for a pp-import?
2624 Callbacks->InclusionDirective(HashLoc, IncludeTok, LookupFilename, isAngled,
2625 FilenameRange, File, SearchPath, RelativePath,
2626 SuggestedModule.getModule(), Action == Import,
2627 FileCharacter);
2628 if (Action == Skip && File)
2629 Callbacks->FileSkipped(*File, FilenameTok, FileCharacter);
2630 }
2631
2632 if (!File)
2633 return {ImportAction::None};
2634
2635 // If this is a C++20 pp-import declaration, diagnose if we didn't find any
2636 // module corresponding to the named header.
2637 if (IsImportDecl && !ModuleToImport) {
2638 Diag(FilenameTok, diag::err_header_import_not_header_unit)
2639 << OriginalFilename << File->getName();
2640 return {ImportAction::None};
2641 }
2642
2643 // Issue a diagnostic if the name of the file on disk has a different case
2644 // than the one we're about to open.
2645 const bool CheckIncludePathPortability =
2646 !IsMapped && !File->getFileEntry().tryGetRealPathName().empty();
2647
2648 if (CheckIncludePathPortability) {
2649 StringRef Name = LookupFilename;
2650 StringRef NameWithoriginalSlashes = Filename;
2651#if defined(_WIN32)
2652 // Skip UNC prefix if present. (tryGetRealPathName() always
2653 // returns a path with the prefix skipped.)
2654 bool NameWasUNC = Name.consume_front("\\\\?\\");
2655 NameWithoriginalSlashes.consume_front("\\\\?\\");
2656#endif
2657 StringRef RealPathName = File->getFileEntry().tryGetRealPathName();
2658 SmallVector<StringRef, 16> Components(llvm::sys::path::begin(Name),
2659 llvm::sys::path::end(Name));
2660#if defined(_WIN32)
2661 // -Wnonportable-include-path is designed to diagnose includes using
2662 // case even on systems with a case-insensitive file system.
2663 // On Windows, RealPathName always starts with an upper-case drive
2664 // letter for absolute paths, but Name might start with either
2665 // case depending on if `cd c:\foo` or `cd C:\foo` was used in the shell.
2666 // ("foo" will always have on-disk case, no matter which case was
2667 // used in the cd command). To not emit this warning solely for
2668 // the drive letter, whose case is dependent on if `cd` is used
2669 // with upper- or lower-case drive letters, always consider the
2670 // given drive letter case as correct for the purpose of this warning.
2671 SmallString<128> FixedDriveRealPath;
2672 if (llvm::sys::path::is_absolute(Name) &&
2673 llvm::sys::path::is_absolute(RealPathName) &&
2674 toLowercase(Name[0]) == toLowercase(RealPathName[0]) &&
2675 isLowercase(Name[0]) != isLowercase(RealPathName[0])) {
2676 assert(Components.size() >= 3 && "should have drive, backslash, name");
2677 assert(Components[0].size() == 2 && "should start with drive");
2678 assert(Components[0][1] == ':' && "should have colon");
2679 FixedDriveRealPath = (Name.substr(0, 1) + RealPathName.substr(1)).str();
2680 RealPathName = FixedDriveRealPath;
2681 }
2682#endif
2683
2684 if (trySimplifyPath(Components, RealPathName, BackslashStyle)) {
2685 SmallString<128> Path;
2686 Path.reserve(Name.size()+2);
2687 Path.push_back(isAngled ? '<' : '"');
2688
2689 const auto IsSep = [BackslashStyle](char c) {
2690 return llvm::sys::path::is_separator(c, BackslashStyle);
2691 };
2692
2693 for (auto Component : Components) {
2694 // On POSIX, Components will contain a single '/' as first element
2695 // exactly if Name is an absolute path.
2696 // On Windows, it will contain "C:" followed by '\' for absolute paths.
2697 // The drive letter is optional for absolute paths on Windows, but
2698 // clang currently cannot process absolute paths in #include lines that
2699 // don't have a drive.
2700 // If the first entry in Components is a directory separator,
2701 // then the code at the bottom of this loop that keeps the original
2702 // directory separator style copies it. If the second entry is
2703 // a directory separator (the C:\ case), then that separator already
2704 // got copied when the C: was processed and we want to skip that entry.
2705 if (!(Component.size() == 1 && IsSep(Component[0])))
2706 Path.append(Component);
2707 else if (Path.size() != 1)
2708 continue;
2709
2710 // Append the separator(s) the user used, or the close quote
2711 if (Path.size() > NameWithoriginalSlashes.size()) {
2712 Path.push_back(isAngled ? '>' : '"');
2713 continue;
2714 }
2715 assert(IsSep(NameWithoriginalSlashes[Path.size()-1]));
2716 do
2717 Path.push_back(NameWithoriginalSlashes[Path.size()-1]);
2718 while (Path.size() <= NameWithoriginalSlashes.size() &&
2719 IsSep(NameWithoriginalSlashes[Path.size()-1]));
2720 }
2721
2722#if defined(_WIN32)
2723 // Restore UNC prefix if it was there.
2724 if (NameWasUNC)
2725 Path = (Path.substr(0, 1) + "\\\\?\\" + Path.substr(1)).str();
2726#endif
2727
2728 // For user files and known standard headers, issue a diagnostic.
2729 // For other system headers, don't. They can be controlled separately.
2730 auto DiagId =
2731 (FileCharacter == SrcMgr::C_User || warnByDefaultOnWrongCase(Name))
2732 ? diag::pp_nonportable_path
2733 : diag::pp_nonportable_system_path;
2734 Diag(FilenameTok, DiagId) << Path <<
2735 FixItHint::CreateReplacement(FilenameRange, Path);
2736 }
2737
2738 bool SuppressBackslashDiag =
2739 // The diagnostic logic is expensive, so only run it if it's enabled...
2740 Diags->isIgnored(diag::pp_nonportable_path_separator, FilenameLoc) ||
2741 // ...and try to only trigger on paths that appear in source.
2742 FilenameLoc.isMacroID() ||
2743 SourceMgr.isWrittenInBuiltinFile(FilenameLoc) ||
2744 SourceMgr.isWrittenInModuleIncludes(FilenameLoc);
2745 if (!SuppressBackslashDiag && OriginalFilename.contains('\\')) {
2746 std::string SuggestedPath = OriginalFilename.str();
2747 llvm::replace(SuggestedPath, '\\', '/');
2748 Diag(FilenameTok, diag::pp_nonportable_path_separator)
2749 << Name << FixItHint::CreateReplacement(FilenameRange, SuggestedPath);
2750 }
2751 }
2752
2753 switch (Action) {
2754 case Skip:
2755 // If we don't need to enter the file, stop now.
2756 if (ModuleToImport)
2757 return {ImportAction::SkippedModuleImport, ModuleToImport};
2758 return {ImportAction::None};
2759
2760 case IncludeLimitReached:
2761 // If we reached our include limit and don't want to enter any more files,
2762 // don't go any further.
2763 return {ImportAction::None};
2764
2765 case Import: {
2766 // If this is a module import, make it visible if needed.
2767 assert(ModuleToImport && "no module to import");
2768
2769 makeModuleVisible(ModuleToImport, EndLoc);
2770
2771 if (IncludeTok.getIdentifierInfo()->getPPKeywordID() ==
2772 tok::pp___include_macros)
2773 return {ImportAction::None};
2774
2775 return {ImportAction::ModuleImport, ModuleToImport};
2776 }
2777
2778 case Enter:
2779 break;
2780 }
2781
2782 // Check that we don't have infinite #include recursion.
2783 if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1) {
2784 Diag(FilenameTok, diag::err_pp_include_too_deep);
2785 HasReachedMaxIncludeDepth = true;
2786 return {ImportAction::None};
2787 }
2788
2789 if (isAngled && isInNamedModule())
2790 Diag(FilenameTok, diag::warn_pp_include_angled_in_module_purview)
2791 << getNamedModuleName();
2792
2793 // Look up the file, create a File ID for it.
2794 SourceLocation IncludePos = FilenameTok.getLocation();
2795 // If the filename string was the result of macro expansions, set the include
2796 // position on the file where it will be included and after the expansions.
2797 if (IncludePos.isMacroID())
2798 IncludePos = SourceMgr.getExpansionRange(IncludePos).getEnd();
2799 FileID FID = SourceMgr.createFileID(*File, IncludePos, FileCharacter);
2800 if (!FID.isValid()) {
2801 TheModuleLoader.HadFatalFailure = true;
2802 return ImportAction::Failure;
2803 }
2804
2805 // If all is good, enter the new file!
2806 if (EnterSourceFile(FID, CurDir, FilenameTok.getLocation(),
2807 IsFirstIncludeOfFile))
2808 return {ImportAction::None};
2809
2810 // Determine if we're switching to building a new submodule, and which one.
2811 // This does not apply for C++20 modules header units.
2812 if (ModuleToImport && !ModuleToImport->isHeaderUnit()) {
2813 if (ModuleToImport->getTopLevelModule()->ShadowingModule) {
2814 // We are building a submodule that belongs to a shadowed module. This
2815 // means we find header files in the shadowed module.
2816 Diag(ModuleToImport->DefinitionLoc,
2817 diag::err_module_build_shadowed_submodule)
2818 << ModuleToImport->getFullModuleName();
2820 diag::note_previous_definition);
2821 return {ImportAction::None};
2822 }
2823 // When building a pch, -fmodule-name tells the compiler to textually
2824 // include headers in the specified module. We are not building the
2825 // specified module.
2826 //
2827 // FIXME: This is the wrong way to handle this. We should produce a PCH
2828 // that behaves the same as the header would behave in a compilation using
2829 // that PCH, which means we should enter the submodule. We need to teach
2830 // the AST serialization layer to deal with the resulting AST.
2831 if (getLangOpts().CompilingPCH &&
2832 ModuleToImport->isForBuilding(getLangOpts()))
2833 return {ImportAction::None};
2834
2835 assert(!CurLexerSubmodule && "should not have marked this as a module yet");
2836 CurLexerSubmodule = ModuleToImport;
2837
2838 // Let the macro handling code know that any future macros are within
2839 // the new submodule.
2840 EnterSubmodule(ModuleToImport, EndLoc, /*ForPragma*/ false);
2841
2842 // Let the parser know that any future declarations are within the new
2843 // submodule.
2844 // FIXME: There's no point doing this if we're handling a #__include_macros
2845 // directive.
2846 return {ImportAction::ModuleBegin, ModuleToImport};
2847 }
2848
2849 assert(!IsImportDecl && "failed to diagnose missing module for import decl");
2850 return {ImportAction::None};
2851}
2852
2853/// HandleIncludeNextDirective - Implements \#include_next.
2854///
2855void Preprocessor::HandleIncludeNextDirective(SourceLocation HashLoc,
2856 Token &IncludeNextTok) {
2857 Diag(IncludeNextTok, diag::ext_pp_include_next_directive);
2858
2859 ConstSearchDirIterator Lookup = nullptr;
2860 const FileEntry *LookupFromFile;
2861 std::tie(Lookup, LookupFromFile) = getIncludeNextStart(IncludeNextTok);
2862
2863 return HandleIncludeDirective(HashLoc, IncludeNextTok, Lookup,
2864 LookupFromFile);
2865}
2866
2867/// HandleMicrosoftImportDirective - Implements \#import for Microsoft Mode
2868void Preprocessor::HandleMicrosoftImportDirective(Token &Tok) {
2869 // The Microsoft #import directive takes a type library and generates header
2870 // files from it, and includes those. This is beyond the scope of what clang
2871 // does, so we ignore it and error out. However, #import can optionally have
2872 // trailing attributes that span multiple lines. We're going to eat those
2873 // so we can continue processing from there.
2874 Diag(Tok, diag::err_pp_import_directive_ms );
2875
2876 // Read tokens until we get to the end of the directive. Note that the
2877 // directive can be split over multiple lines using the backslash character.
2879}
2880
2881/// HandleImportDirective - Implements \#import.
2882///
2883void Preprocessor::HandleImportDirective(SourceLocation HashLoc,
2884 Token &ImportTok) {
2885 if (!LangOpts.ObjC) { // #import is standard for ObjC.
2886 if (LangOpts.MSVCCompat)
2887 return HandleMicrosoftImportDirective(ImportTok);
2888 Diag(ImportTok, diag::ext_pp_import_directive);
2889 }
2890 return HandleIncludeDirective(HashLoc, ImportTok);
2891}
2892
2893/// HandleIncludeMacrosDirective - The -imacros command line option turns into a
2894/// pseudo directive in the predefines buffer. This handles it by sucking all
2895/// tokens through the preprocessor and discarding them (only keeping the side
2896/// effects on the preprocessor).
2897void Preprocessor::HandleIncludeMacrosDirective(SourceLocation HashLoc,
2898 Token &IncludeMacrosTok) {
2899 // This directive should only occur in the predefines buffer. If not, emit an
2900 // error and reject it.
2901 SourceLocation Loc = IncludeMacrosTok.getLocation();
2902 if (SourceMgr.getBufferName(Loc) != "<built-in>") {
2903 Diag(IncludeMacrosTok.getLocation(),
2904 diag::pp_include_macros_out_of_predefines);
2906 return;
2907 }
2908
2909 // Treat this as a normal #include for checking purposes. If this is
2910 // successful, it will push a new lexer onto the include stack.
2911 HandleIncludeDirective(HashLoc, IncludeMacrosTok);
2912
2913 Token TmpTok;
2914 do {
2915 Lex(TmpTok);
2916 assert(TmpTok.isNot(tok::eof) && "Didn't find end of -imacros!");
2917 } while (TmpTok.isNot(tok::hashhash));
2918}
2919
2920//===----------------------------------------------------------------------===//
2921// Preprocessor Macro Directive Handling.
2922//===----------------------------------------------------------------------===//
2923
2924/// ReadMacroParameterList - The ( starting a parameter list of a macro
2925/// definition has just been read. Lex the rest of the parameters and the
2926/// closing ), updating MI with what we learn. Return true if an error occurs
2927/// parsing the param list.
2928bool Preprocessor::ReadMacroParameterList(MacroInfo *MI, Token &Tok) {
2929 SmallVector<IdentifierInfo*, 32> Parameters;
2930
2931 while (true) {
2933 switch (Tok.getKind()) {
2934 case tok::r_paren:
2935 // Found the end of the parameter list.
2936 if (Parameters.empty()) // #define FOO()
2937 return false;
2938 // Otherwise we have #define FOO(A,)
2939 Diag(Tok, diag::err_pp_expected_ident_in_arg_list);
2940 return true;
2941 case tok::ellipsis: // #define X(... -> C99 varargs
2942 if (!LangOpts.C99)
2943 Diag(Tok, LangOpts.CPlusPlus11 ?
2944 diag::warn_cxx98_compat_variadic_macro :
2945 diag::ext_variadic_macro);
2946
2947 // OpenCL v1.2 s6.9.e: variadic macros are not supported.
2948 if (LangOpts.OpenCL && !LangOpts.OpenCLCPlusPlus) {
2949 Diag(Tok, diag::ext_pp_opencl_variadic_macros);
2950 }
2951
2952 // Lex the token after the identifier.
2954 if (Tok.isNot(tok::r_paren)) {
2955 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
2956 return true;
2957 }
2958 // Add the __VA_ARGS__ identifier as a parameter.
2959 Parameters.push_back(Ident__VA_ARGS__);
2960 MI->setIsC99Varargs();
2961 MI->setParameterList(Parameters, BP);
2962 return false;
2963 case tok::eod: // #define X(
2964 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
2965 return true;
2966 default:
2967 // Handle keywords and identifiers here to accept things like
2968 // #define Foo(for) for.
2969 IdentifierInfo *II = Tok.getIdentifierInfo();
2970 if (!II) {
2971 // #define X(1
2972 Diag(Tok, diag::err_pp_invalid_tok_in_arg_list);
2973 return true;
2974 }
2975
2976 // If this is already used as a parameter, it is used multiple times (e.g.
2977 // #define X(A,A.
2978 if (llvm::is_contained(Parameters, II)) { // C99 6.10.3p6
2979 Diag(Tok, diag::err_pp_duplicate_name_in_arg_list) << II;
2980 return true;
2981 }
2982
2983 // Add the parameter to the macro info.
2984 Parameters.push_back(II);
2985
2986 // Lex the token after the identifier.
2988
2989 switch (Tok.getKind()) {
2990 default: // #define X(A B
2991 Diag(Tok, diag::err_pp_expected_comma_in_arg_list);
2992 return true;
2993 case tok::r_paren: // #define X(A)
2994 MI->setParameterList(Parameters, BP);
2995 return false;
2996 case tok::comma: // #define X(A,
2997 break;
2998 case tok::ellipsis: // #define X(A... -> GCC extension
2999 // Diagnose extension.
3000 Diag(Tok, diag::ext_named_variadic_macro);
3001
3002 // Lex the token after the identifier.
3004 if (Tok.isNot(tok::r_paren)) {
3005 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
3006 return true;
3007 }
3008
3009 MI->setIsGNUVarargs();
3010 MI->setParameterList(Parameters, BP);
3011 return false;
3012 }
3013 }
3014 }
3015}
3016
3017static bool isConfigurationPattern(Token &MacroName, MacroInfo *MI,
3018 const LangOptions &LOptions) {
3019 if (MI->getNumTokens() == 1) {
3020 const Token &Value = MI->getReplacementToken(0);
3021
3022 // Macro that is identity, like '#define inline inline' is a valid pattern.
3023 if (MacroName.getKind() == Value.getKind())
3024 return true;
3025
3026 // Macro that maps a keyword to the same keyword decorated with leading/
3027 // trailing underscores is a valid pattern:
3028 // #define inline __inline
3029 // #define inline __inline__
3030 // #define inline _inline (in MS compatibility mode)
3031 StringRef MacroText = MacroName.getIdentifierInfo()->getName();
3032 if (IdentifierInfo *II = Value.getIdentifierInfo()) {
3033 if (!II->isKeyword(LOptions))
3034 return false;
3035 StringRef ValueText = II->getName();
3036 StringRef TrimmedValue = ValueText;
3037 if (!ValueText.starts_with("__")) {
3038 if (ValueText.starts_with("_"))
3039 TrimmedValue = TrimmedValue.drop_front(1);
3040 else
3041 return false;
3042 } else {
3043 TrimmedValue = TrimmedValue.drop_front(2);
3044 if (TrimmedValue.ends_with("__"))
3045 TrimmedValue = TrimmedValue.drop_back(2);
3046 }
3047 return TrimmedValue == MacroText;
3048 } else {
3049 return false;
3050 }
3051 }
3052
3053 // #define inline
3054 return MacroName.isOneOf(tok::kw_extern, tok::kw_inline, tok::kw_static,
3055 tok::kw_const) &&
3056 MI->getNumTokens() == 0;
3057}
3058
3059// ReadOptionalMacroParameterListAndBody - This consumes all (i.e. the
3060// entire line) of the macro's tokens and adds them to MacroInfo, and while
3061// doing so performs certain validity checks including (but not limited to):
3062// - # (stringization) is followed by a macro parameter
3063//
3064// Returns a nullptr if an invalid sequence of tokens is encountered or returns
3065// a pointer to a MacroInfo object.
3066
3067MacroInfo *Preprocessor::ReadOptionalMacroParameterListAndBody(
3068 const Token &MacroNameTok, const bool ImmediatelyAfterHeaderGuard) {
3069
3070 Token LastTok = MacroNameTok;
3071 // Create the new macro.
3072 MacroInfo *const MI = AllocateMacroInfo(MacroNameTok.getLocation());
3073
3074 Token Tok;
3076
3077 // Ensure we consume the rest of the macro body if errors occur.
3078 llvm::scope_exit _([&]() {
3079 // The flag indicates if we are still waiting for 'eod'.
3080 if (CurLexer->ParsingPreprocessorDirective)
3082 });
3083
3084 // Used to un-poison and then re-poison identifiers of the __VA_ARGS__ ilk
3085 // within their appropriate context.
3087
3088 // If this is a function-like macro definition, parse the argument list,
3089 // marking each of the identifiers as being used as macro arguments. Also,
3090 // check other constraints on the first token of the macro body.
3091 if (Tok.is(tok::eod)) {
3092 if (ImmediatelyAfterHeaderGuard) {
3093 // Save this macro information since it may part of a header guard.
3094 CurPPLexer->MIOpt.SetDefinedMacro(MacroNameTok.getIdentifierInfo(),
3095 MacroNameTok.getLocation());
3096 }
3097 // If there is no body to this macro, we have no special handling here.
3098 } else if (Tok.hasLeadingSpace()) {
3099 // This is a normal token with leading space. Clear the leading space
3100 // marker on the first token to get proper expansion.
3102 } else if (Tok.is(tok::l_paren)) {
3103 // This is a function-like macro definition. Read the argument list.
3104 MI->setIsFunctionLike();
3105 if (ReadMacroParameterList(MI, LastTok))
3106 return nullptr;
3107
3108 // If this is a definition of an ISO C/C++ variadic function-like macro (not
3109 // using the GNU named varargs extension) inform our variadic scope guard
3110 // which un-poisons and re-poisons certain identifiers (e.g. __VA_ARGS__)
3111 // allowed only within the definition of a variadic macro.
3112
3113 if (MI->isC99Varargs()) {
3114 VariadicMacroScopeGuard.enterScope();
3115 }
3116
3117 // Read the first token after the arg list for down below.
3119 } else if (LangOpts.C99 || LangOpts.CPlusPlus11) {
3120 // C99 requires whitespace between the macro definition and the body. Emit
3121 // a diagnostic for something like "#define X+".
3122 Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name);
3123 } else {
3124 // C90 6.8 TC1 says: "In the definition of an object-like macro, if the
3125 // first character of a replacement list is not a character required by
3126 // subclause 5.2.1, then there shall be white-space separation between the
3127 // identifier and the replacement list.". 5.2.1 lists this set:
3128 // "A-Za-z0-9!"#%&'()*+,_./:;<=>?[\]^_{|}~" as well as whitespace, which
3129 // is irrelevant here.
3130 bool isInvalid = false;
3131 if (Tok.is(tok::at)) // @ is not in the list above.
3132 isInvalid = true;
3133 else if (Tok.is(tok::unknown)) {
3134 // If we have an unknown token, it is something strange like "`". Since
3135 // all of valid characters would have lexed into a single character
3136 // token of some sort, we know this is not a valid case.
3137 isInvalid = true;
3138 }
3139 if (isInvalid)
3140 Diag(Tok, diag::ext_missing_whitespace_after_macro_name);
3141 else
3142 Diag(Tok, diag::warn_missing_whitespace_after_macro_name);
3143 }
3144
3145 if (!Tok.is(tok::eod))
3146 LastTok = Tok;
3147
3148 SmallVector<Token, 16> Tokens;
3149
3150 // Read the rest of the macro body.
3151 if (MI->isObjectLike()) {
3152 // Object-like macros are very simple, just read their body.
3153 while (Tok.isNot(tok::eod)) {
3154 LastTok = Tok;
3155 Tokens.push_back(Tok);
3156 // Get the next token of the macro.
3158 }
3159 } else {
3160 // Otherwise, read the body of a function-like macro. While we are at it,
3161 // check C99 6.10.3.2p1: ensure that # operators are followed by macro
3162 // parameters in function-like macro expansions.
3163
3164 VAOptDefinitionContext VAOCtx(*this);
3165
3166 while (Tok.isNot(tok::eod)) {
3167 LastTok = Tok;
3168
3169 if (!Tok.isOneOf(tok::hash, tok::hashat, tok::hashhash)) {
3170 Tokens.push_back(Tok);
3171
3172 if (VAOCtx.isVAOptToken(Tok)) {
3173 // If we're already within a VAOPT, emit an error.
3174 if (VAOCtx.isInVAOpt()) {
3175 Diag(Tok, diag::err_pp_vaopt_nested_use);
3176 return nullptr;
3177 }
3178 // Ensure VAOPT is followed by a '(' .
3180 if (Tok.isNot(tok::l_paren)) {
3181 Diag(Tok, diag::err_pp_missing_lparen_in_vaopt_use);
3182 return nullptr;
3183 }
3184 Tokens.push_back(Tok);
3185 VAOCtx.sawVAOptFollowedByOpeningParens(Tok.getLocation());
3187 if (Tok.is(tok::hashhash)) {
3188 Diag(Tok, diag::err_vaopt_paste_at_start);
3189 return nullptr;
3190 }
3191 continue;
3192 } else if (VAOCtx.isInVAOpt()) {
3193 if (Tok.is(tok::r_paren)) {
3194 if (VAOCtx.sawClosingParen()) {
3195 assert(Tokens.size() >= 3 &&
3196 "Must have seen at least __VA_OPT__( "
3197 "and a subsequent tok::r_paren");
3198 if (Tokens[Tokens.size() - 2].is(tok::hashhash)) {
3199 Diag(Tok, diag::err_vaopt_paste_at_end);
3200 return nullptr;
3201 }
3202 }
3203 } else if (Tok.is(tok::l_paren)) {
3204 VAOCtx.sawOpeningParen(Tok.getLocation());
3205 }
3206 }
3207 // Get the next token of the macro.
3209 continue;
3210 }
3211
3212 // If we're in -traditional mode, then we should ignore stringification
3213 // and token pasting. Mark the tokens as unknown so as not to confuse
3214 // things.
3215 if (getLangOpts().TraditionalCPP) {
3216 Tok.setKind(tok::unknown);
3217 Tokens.push_back(Tok);
3218
3219 // Get the next token of the macro.
3221 continue;
3222 }
3223
3224 if (Tok.is(tok::hashhash)) {
3225 // If we see token pasting, check if it looks like the gcc comma
3226 // pasting extension. We'll use this information to suppress
3227 // diagnostics later on.
3228
3229 // Get the next token of the macro.
3231
3232 if (Tok.is(tok::eod)) {
3233 Tokens.push_back(LastTok);
3234 break;
3235 }
3236
3237 if (!Tokens.empty() && Tok.getIdentifierInfo() == Ident__VA_ARGS__ &&
3238 Tokens[Tokens.size() - 1].is(tok::comma))
3239 MI->setHasCommaPasting();
3240
3241 // Things look ok, add the '##' token to the macro.
3242 Tokens.push_back(LastTok);
3243 continue;
3244 }
3245
3246 // Our Token is a stringization operator.
3247 // Get the next token of the macro.
3249
3250 // Check for a valid macro arg identifier or __VA_OPT__.
3251 if (!VAOCtx.isVAOptToken(Tok) &&
3252 (Tok.getIdentifierInfo() == nullptr ||
3253 MI->getParameterNum(Tok.getIdentifierInfo()) == -1)) {
3254
3255 // If this is assembler-with-cpp mode, we accept random gibberish after
3256 // the '#' because '#' is often a comment character. However, change
3257 // the kind of the token to tok::unknown so that the preprocessor isn't
3258 // confused.
3259 if (getLangOpts().AsmPreprocessor && Tok.isNot(tok::eod)) {
3260 LastTok.setKind(tok::unknown);
3261 Tokens.push_back(LastTok);
3262 continue;
3263 } else {
3264 Diag(Tok, diag::err_pp_stringize_not_parameter)
3265 << LastTok.is(tok::hashat);
3266 return nullptr;
3267 }
3268 }
3269
3270 // Things look ok, add the '#' and param name tokens to the macro.
3271 Tokens.push_back(LastTok);
3272
3273 // If the token following '#' is VAOPT, let the next iteration handle it
3274 // and check it for correctness, otherwise add the token and prime the
3275 // loop with the next one.
3276 if (!VAOCtx.isVAOptToken(Tok)) {
3277 Tokens.push_back(Tok);
3278 LastTok = Tok;
3279
3280 // Get the next token of the macro.
3282 }
3283 }
3284 if (VAOCtx.isInVAOpt()) {
3285 assert(Tok.is(tok::eod) && "Must be at End Of preprocessing Directive");
3286 Diag(Tok, diag::err_pp_expected_after)
3287 << LastTok.getKind() << tok::r_paren;
3288 Diag(VAOCtx.getUnmatchedOpeningParenLoc(), diag::note_matching) << tok::l_paren;
3289 return nullptr;
3290 }
3291 }
3292 MI->setDefinitionEndLoc(LastTok.getLocation());
3293
3294 MI->setTokens(Tokens, BP);
3295 return MI;
3296}
3297
3298static bool isObjCProtectedMacro(const IdentifierInfo *II) {
3299 return II->isStr("__strong") || II->isStr("__weak") ||
3300 II->isStr("__unsafe_unretained") || II->isStr("__autoreleasing");
3301}
3302
3303/// HandleDefineDirective - Implements \#define. This consumes the entire macro
3304/// line then lets the caller lex the next real token.
3305void Preprocessor::HandleDefineDirective(
3306 Token &DefineTok, const bool ImmediatelyAfterHeaderGuard) {
3307 ++NumDefined;
3308
3309 Token MacroNameTok;
3310 bool MacroShadowsKeyword;
3311 ReadMacroName(MacroNameTok, MU_Define, &MacroShadowsKeyword);
3312
3313 // Error reading macro name? If so, diagnostic already issued.
3314 if (MacroNameTok.is(tok::eod))
3315 return;
3316
3317 IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
3318 // Issue a final pragma warning if we're defining a macro that was has been
3319 // undefined and is being redefined.
3320 if (!II->hasMacroDefinition() && II->hadMacroDefinition() && II->isFinal())
3321 emitFinalMacroWarning(MacroNameTok, /*IsUndef=*/false);
3322
3323 // If we are supposed to keep comments in #defines, reenable comment saving
3324 // mode.
3325 if (CurLexer) CurLexer->SetCommentRetentionState(KeepMacroComments);
3326
3327 MacroInfo *const MI = ReadOptionalMacroParameterListAndBody(
3328 MacroNameTok, ImmediatelyAfterHeaderGuard);
3329
3330 if (!MI) return;
3331
3332 if (MacroShadowsKeyword &&
3333 !isConfigurationPattern(MacroNameTok, MI, getLangOpts())) {
3334 Diag(MacroNameTok, diag::warn_pp_macro_hides_keyword);
3335 }
3336 // Check that there is no paste (##) operator at the beginning or end of the
3337 // replacement list.
3338 unsigned NumTokens = MI->getNumTokens();
3339 if (NumTokens != 0) {
3340 if (MI->getReplacementToken(0).is(tok::hashhash)) {
3341 Diag(MI->getReplacementToken(0), diag::err_paste_at_start);
3342 return;
3343 }
3344 if (MI->getReplacementToken(NumTokens-1).is(tok::hashhash)) {
3345 Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end);
3346 return;
3347 }
3348 }
3349
3350 // When skipping just warn about macros that do not match.
3351 if (SkippingUntilPCHThroughHeader) {
3352 const MacroInfo *OtherMI = getMacroInfo(MacroNameTok.getIdentifierInfo());
3353 if (!OtherMI || !MI->isIdenticalTo(*OtherMI, *this,
3354 /*Syntactic=*/LangOpts.MicrosoftExt))
3355 Diag(MI->getDefinitionLoc(), diag::warn_pp_macro_def_mismatch_with_pch)
3356 << MacroNameTok.getIdentifierInfo();
3357 // Issue the diagnostic but allow the change if msvc extensions are enabled
3358 if (!LangOpts.MicrosoftExt)
3359 return;
3360 }
3361
3362 // Finally, if this identifier already had a macro defined for it, verify that
3363 // the macro bodies are identical, and issue diagnostics if they are not.
3364 if (const MacroInfo *OtherMI=getMacroInfo(MacroNameTok.getIdentifierInfo())) {
3365 // Final macros are hard-mode: they always warn. Even if the bodies are
3366 // identical. Even if they are in system headers. Even if they are things we
3367 // would silently allow in the past.
3368 if (MacroNameTok.getIdentifierInfo()->isFinal())
3369 emitFinalMacroWarning(MacroNameTok, /*IsUndef=*/false);
3370
3371 // In Objective-C, ignore attempts to directly redefine the builtin
3372 // definitions of the ownership qualifiers. It's still possible to
3373 // #undef them.
3374 if (getLangOpts().ObjC &&
3375 SourceMgr.getFileID(OtherMI->getDefinitionLoc()) ==
3377 isObjCProtectedMacro(MacroNameTok.getIdentifierInfo())) {
3378 // Warn if it changes the tokens.
3379 if ((!getDiagnostics().getSuppressSystemWarnings() ||
3380 !SourceMgr.isInSystemHeader(DefineTok.getLocation())) &&
3381 !MI->isIdenticalTo(*OtherMI, *this,
3382 /*Syntactic=*/LangOpts.MicrosoftExt)) {
3383 Diag(MI->getDefinitionLoc(), diag::warn_pp_objc_macro_redef_ignored);
3384 }
3385 assert(!OtherMI->isWarnIfUnused());
3386 return;
3387 }
3388
3389 // It is very common for system headers to have tons of macro redefinitions
3390 // and for warnings to be disabled in system headers. If this is the case,
3391 // then don't bother calling MacroInfo::isIdenticalTo.
3392 if (!getDiagnostics().getSuppressSystemWarnings() ||
3393 !SourceMgr.isInSystemHeader(DefineTok.getLocation())) {
3394
3395 if (!OtherMI->isUsed() && OtherMI->isWarnIfUnused())
3396 Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used);
3397
3398 // Warn if defining "__LINE__" and other builtins, per C99 6.10.8/4 and
3399 // C++ [cpp.predefined]p4, but allow it as an extension.
3400 if (isLanguageDefinedBuiltin(SourceMgr, OtherMI, II->getName()))
3401 Diag(MacroNameTok, diag::ext_pp_redef_builtin_macro);
3402 // Macros must be identical. This means all tokens and whitespace
3403 // separation must be the same. C99 6.10.3p2.
3404 else if (!OtherMI->isAllowRedefinitionsWithoutWarning() &&
3405 !MI->isIdenticalTo(*OtherMI, *this, /*Syntactic=*/LangOpts.MicrosoftExt)) {
3406 Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef)
3407 << MacroNameTok.getIdentifierInfo();
3408 Diag(OtherMI->getDefinitionLoc(), diag::note_previous_definition);
3409 }
3410 }
3411 if (OtherMI->isWarnIfUnused())
3412 WarnUnusedMacroLocs.erase(OtherMI->getDefinitionLoc());
3413 }
3414
3415 DefMacroDirective *MD =
3416 appendDefMacroDirective(MacroNameTok.getIdentifierInfo(), MI);
3417
3418 assert(!MI->isUsed());
3419 // If we need warning for not using the macro, add its location in the
3420 // warn-because-unused-macro set. If it gets used it will be removed from set.
3422 !Diags->isIgnored(diag::pp_macro_not_used, MI->getDefinitionLoc()) &&
3423 !MacroExpansionInDirectivesOverride &&
3424 getSourceManager().getFileID(MI->getDefinitionLoc()) !=
3426 MI->setIsWarnIfUnused(true);
3427 WarnUnusedMacroLocs.insert(MI->getDefinitionLoc());
3428 }
3429
3430 // If the callbacks want to know, tell them about the macro definition.
3431 if (Callbacks)
3432 Callbacks->MacroDefined(MacroNameTok, MD);
3433}
3434
3435/// HandleUndefDirective - Implements \#undef.
3436///
3437void Preprocessor::HandleUndefDirective() {
3438 ++NumUndefined;
3439
3440 Token MacroNameTok;
3441 ReadMacroName(MacroNameTok, MU_Undef);
3442
3443 // Error reading macro name? If so, diagnostic already issued.
3444 if (MacroNameTok.is(tok::eod))
3445 return;
3446
3447 // Check to see if this is the last token on the #undef line.
3448 CheckEndOfDirective("undef");
3449
3450 // Okay, we have a valid identifier to undef.
3451 auto *II = MacroNameTok.getIdentifierInfo();
3452 auto MD = getMacroDefinition(II);
3453 UndefMacroDirective *Undef = nullptr;
3454
3455 if (II->isFinal())
3456 emitFinalMacroWarning(MacroNameTok, /*IsUndef=*/true);
3457
3458 // If the macro is not defined, this is a noop undef.
3459 if (const MacroInfo *MI = MD.getMacroInfo()) {
3460 if (!MI->isUsed() && MI->isWarnIfUnused())
3461 Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used);
3462
3463 // Warn if undefining "__LINE__" and other builtins, per C99 6.10.8/4 and
3464 // C++ [cpp.predefined]p4, but allow it as an extension.
3465 if (isLanguageDefinedBuiltin(SourceMgr, MI, II->getName()))
3466 Diag(MacroNameTok, diag::ext_pp_undef_builtin_macro);
3467
3468 if (MI->isWarnIfUnused())
3469 WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());
3470
3471 Undef = AllocateUndefMacroDirective(MacroNameTok.getLocation());
3472 }
3473
3474 // If the callbacks want to know, tell them about the macro #undef.
3475 // Note: no matter if the macro was defined or not.
3476 if (Callbacks)
3477 Callbacks->MacroUndefined(MacroNameTok, MD, Undef);
3478
3479 if (Undef)
3480 appendMacroDirective(II, Undef);
3481}
3482
3483//===----------------------------------------------------------------------===//
3484// Preprocessor Conditional Directive Handling.
3485//===----------------------------------------------------------------------===//
3486
3487/// HandleIfdefDirective - Implements the \#ifdef/\#ifndef directive. isIfndef
3488/// is true when this is a \#ifndef directive. ReadAnyTokensBeforeDirective is
3489/// true if any tokens have been returned or pp-directives activated before this
3490/// \#ifndef has been lexed.
3491///
3492void Preprocessor::HandleIfdefDirective(Token &Result,
3493 const Token &HashToken,
3494 bool isIfndef,
3495 bool ReadAnyTokensBeforeDirective) {
3496 ++NumIf;
3497 Token DirectiveTok = Result;
3498
3499 Token MacroNameTok;
3500 ReadMacroName(MacroNameTok);
3501
3502 // Error reading macro name? If so, diagnostic already issued.
3503 if (MacroNameTok.is(tok::eod)) {
3504 // Skip code until we get to #endif. This helps with recovery by not
3505 // emitting an error when the #endif is reached.
3506 SkipExcludedConditionalBlock(HashToken.getLocation(),
3507 DirectiveTok.getLocation(),
3508 /*Foundnonskip*/ false, /*FoundElse*/ false);
3509 return;
3510 }
3511
3512 emitMacroExpansionWarnings(MacroNameTok, /*IsIfnDef=*/true);
3513
3514 // Check to see if this is the last token on the #if[n]def line.
3515 CheckEndOfDirective(isIfndef ? "ifndef" : "ifdef");
3516
3517 IdentifierInfo *MII = MacroNameTok.getIdentifierInfo();
3518 auto MD = getMacroDefinition(MII);
3519 MacroInfo *MI = MD.getMacroInfo();
3520
3521 if (CurPPLexer->getConditionalStackDepth() == 0) {
3522 // If the start of a top-level #ifdef and if the macro is not defined,
3523 // inform MIOpt that this might be the start of a proper include guard.
3524 // Otherwise it is some other form of unknown conditional which we can't
3525 // handle.
3526 if (!ReadAnyTokensBeforeDirective && !MI) {
3527 assert(isIfndef && "#ifdef shouldn't reach here");
3528 CurPPLexer->MIOpt.EnterTopLevelIfndef(MII, MacroNameTok.getLocation());
3529 } else
3530 CurPPLexer->MIOpt.EnterTopLevelConditional();
3531 }
3532
3533 // If there is a macro, process it.
3534 if (MI) // Mark it used.
3535 markMacroAsUsed(MI);
3536
3537 if (Callbacks) {
3538 if (isIfndef)
3539 Callbacks->Ifndef(DirectiveTok.getLocation(), MacroNameTok, MD);
3540 else
3541 Callbacks->Ifdef(DirectiveTok.getLocation(), MacroNameTok, MD);
3542 }
3543
3544 bool RetainExcludedCB = PPOpts.RetainExcludedConditionalBlocks &&
3545 getSourceManager().isInMainFile(DirectiveTok.getLocation());
3546
3547 // Should we include the stuff contained by this directive?
3548 if (PPOpts.SingleFileParseMode && !MI) {
3549 // In 'single-file-parse mode' undefined identifiers trigger parsing of all
3550 // the directive blocks.
3551 CurPPLexer->pushConditionalLevel(DirectiveTok.getLocation(),
3552 /*wasskip*/false, /*foundnonskip*/false,
3553 /*foundelse*/false);
3554 } else if (PPOpts.SingleModuleParseMode && !MI) {
3555 // In 'single-module-parse mode' undefined identifiers trigger skipping of
3556 // all the directive blocks. We lie here and set FoundNonSkipPortion so that
3557 // even any \#else blocks get skipped.
3558 SkipExcludedConditionalBlock(
3559 HashToken.getLocation(), DirectiveTok.getLocation(),
3560 /*FoundNonSkipPortion=*/true, /*FoundElse=*/false);
3561 } else if (!MI == isIfndef || RetainExcludedCB) {
3562 // Yes, remember that we are inside a conditional, then lex the next token.
3563 CurPPLexer->pushConditionalLevel(DirectiveTok.getLocation(),
3564 /*wasskip*/false, /*foundnonskip*/true,
3565 /*foundelse*/false);
3566 } else {
3567 // No, skip the contents of this block.
3568 SkipExcludedConditionalBlock(HashToken.getLocation(),
3569 DirectiveTok.getLocation(),
3570 /*Foundnonskip*/ false,
3571 /*FoundElse*/ false);
3572 }
3573}
3574
3575/// HandleIfDirective - Implements the \#if directive.
3576///
3577void Preprocessor::HandleIfDirective(Token &IfToken,
3578 const Token &HashToken,
3579 bool ReadAnyTokensBeforeDirective) {
3580 ++NumIf;
3581
3582 // Parse and evaluate the conditional expression.
3583 IdentifierInfo *IfNDefMacro = nullptr;
3584 const DirectiveEvalResult DER = EvaluateDirectiveExpression(IfNDefMacro);
3585 const bool ConditionalTrue = DER.Conditional;
3586 // Lexer might become invalid if we hit code completion point while evaluating
3587 // expression.
3588 if (!CurPPLexer)
3589 return;
3590
3591 // If this condition is equivalent to #ifndef X, and if this is the first
3592 // directive seen, handle it for the multiple-include optimization.
3593 if (CurPPLexer->getConditionalStackDepth() == 0) {
3594 if (!ReadAnyTokensBeforeDirective && IfNDefMacro && ConditionalTrue)
3595 // FIXME: Pass in the location of the macro name, not the 'if' token.
3596 CurPPLexer->MIOpt.EnterTopLevelIfndef(IfNDefMacro, IfToken.getLocation());
3597 else
3598 CurPPLexer->MIOpt.EnterTopLevelConditional();
3599 }
3600
3601 if (Callbacks)
3602 Callbacks->If(
3603 IfToken.getLocation(), DER.ExprRange,
3604 (ConditionalTrue ? PPCallbacks::CVK_True : PPCallbacks::CVK_False));
3605
3606 bool RetainExcludedCB = PPOpts.RetainExcludedConditionalBlocks &&
3608
3609 // Should we include the stuff contained by this directive?
3610 if (PPOpts.SingleFileParseMode && DER.IncludedUndefinedIds) {
3611 // In 'single-file-parse mode' undefined identifiers trigger parsing of all
3612 // the directive blocks.
3613 CurPPLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
3614 /*foundnonskip*/false, /*foundelse*/false);
3615 } else if (PPOpts.SingleModuleParseMode && DER.IncludedUndefinedIds) {
3616 // In 'single-module-parse mode' undefined identifiers trigger skipping of
3617 // all the directive blocks. We lie here and set FoundNonSkipPortion so that
3618 // even any \#else blocks get skipped.
3619 SkipExcludedConditionalBlock(HashToken.getLocation(), IfToken.getLocation(),
3620 /*FoundNonSkipPortion=*/true,
3621 /*FoundElse=*/false);
3622 } else if (ConditionalTrue || RetainExcludedCB) {
3623 // Yes, remember that we are inside a conditional, then lex the next token.
3624 CurPPLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
3625 /*foundnonskip*/true, /*foundelse*/false);
3626 } else {
3627 // No, skip the contents of this block.
3628 SkipExcludedConditionalBlock(HashToken.getLocation(), IfToken.getLocation(),
3629 /*Foundnonskip*/ false,
3630 /*FoundElse*/ false);
3631 }
3632}
3633
3634/// HandleEndifDirective - Implements the \#endif directive.
3635///
3636void Preprocessor::HandleEndifDirective(Token &EndifToken) {
3637 ++NumEndif;
3638
3639 // Check that this is the whole directive.
3640 CheckEndOfDirective("endif");
3641
3642 PPConditionalInfo CondInfo;
3643 if (CurPPLexer->popConditionalLevel(CondInfo)) {
3644 // No conditionals on the stack: this is an #endif without an #if.
3645 Diag(EndifToken, diag::err_pp_endif_without_if);
3646 return;
3647 }
3648
3649 // If this the end of a top-level #endif, inform MIOpt.
3650 if (CurPPLexer->getConditionalStackDepth() == 0)
3651 CurPPLexer->MIOpt.ExitTopLevelConditional();
3652
3653 assert(!CondInfo.WasSkipping && !CurPPLexer->LexingRawMode &&
3654 "This code should only be reachable in the non-skipping case!");
3655
3656 if (Callbacks)
3657 Callbacks->Endif(EndifToken.getLocation(), CondInfo.IfLoc);
3658}
3659
3660/// HandleElseDirective - Implements the \#else directive.
3661///
3662void Preprocessor::HandleElseDirective(Token &Result, const Token &HashToken) {
3663 ++NumElse;
3664
3665 // #else directive in a non-skipping conditional... start skipping.
3666 CheckEndOfDirective("else");
3667
3668 PPConditionalInfo CI;
3669 if (CurPPLexer->popConditionalLevel(CI)) {
3670 Diag(Result, diag::pp_err_else_without_if);
3671 return;
3672 }
3673
3674 // If this is a top-level #else, inform the MIOpt.
3675 if (CurPPLexer->getConditionalStackDepth() == 0)
3676 CurPPLexer->MIOpt.EnterTopLevelConditional();
3677
3678 // If this is a #else with a #else before it, report the error.
3679 if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else);
3680
3681 if (Callbacks)
3682 Callbacks->Else(Result.getLocation(), CI.IfLoc);
3683
3684 bool RetainExcludedCB = PPOpts.RetainExcludedConditionalBlocks &&
3685 getSourceManager().isInMainFile(Result.getLocation());
3686
3687 if ((PPOpts.SingleFileParseMode && !CI.FoundNonSkip) || RetainExcludedCB) {
3688 // In 'single-file-parse mode' undefined identifiers trigger parsing of all
3689 // the directive blocks.
3690 CurPPLexer->pushConditionalLevel(CI.IfLoc, /*wasskip*/false,
3691 /*foundnonskip*/false, /*foundelse*/true);
3692 return;
3693 }
3694
3695 // Finally, skip the rest of the contents of this block.
3696 SkipExcludedConditionalBlock(HashToken.getLocation(), CI.IfLoc,
3697 /*Foundnonskip*/ true,
3698 /*FoundElse*/ true, Result.getLocation());
3699}
3700
3701/// Implements the \#elif, \#elifdef, and \#elifndef directives.
3702void Preprocessor::HandleElifFamilyDirective(Token &ElifToken,
3703 const Token &HashToken,
3704 tok::PPKeywordKind Kind) {
3705 PPElifDiag DirKind = Kind == tok::pp_elif ? PED_Elif
3706 : Kind == tok::pp_elifdef ? PED_Elifdef
3707 : PED_Elifndef;
3708 ++NumElse;
3709
3710 // Warn if using `#elifdef` & `#elifndef` in not C23 & C++23 mode.
3711 switch (DirKind) {
3712 case PED_Elifdef:
3713 case PED_Elifndef:
3714 unsigned DiagID;
3715 if (LangOpts.CPlusPlus)
3716 DiagID = LangOpts.CPlusPlus23 ? diag::warn_cxx23_compat_pp_directive
3717 : diag::ext_cxx23_pp_directive;
3718 else
3719 DiagID = LangOpts.C23 ? diag::warn_c23_compat_pp_directive
3720 : diag::ext_c23_pp_directive;
3721 Diag(ElifToken, DiagID) << DirKind;
3722 break;
3723 default:
3724 break;
3725 }
3726
3727 // #elif directive in a non-skipping conditional... start skipping.
3728 // We don't care what the condition is, because we will always skip it (since
3729 // the block immediately before it was included).
3730 SourceRange ConditionRange = DiscardUntilEndOfDirective();
3731
3732 PPConditionalInfo CI;
3733 if (CurPPLexer->popConditionalLevel(CI)) {
3734 Diag(ElifToken, diag::pp_err_elif_without_if) << DirKind;
3735 return;
3736 }
3737
3738 // If this is a top-level #elif, inform the MIOpt.
3739 if (CurPPLexer->getConditionalStackDepth() == 0)
3740 CurPPLexer->MIOpt.EnterTopLevelConditional();
3741
3742 // If this is a #elif with a #else before it, report the error.
3743 if (CI.FoundElse)
3744 Diag(ElifToken, diag::pp_err_elif_after_else) << DirKind;
3745
3746 if (Callbacks) {
3747 switch (Kind) {
3748 case tok::pp_elif:
3749 Callbacks->Elif(ElifToken.getLocation(), ConditionRange,
3751 break;
3752 case tok::pp_elifdef:
3753 Callbacks->Elifdef(ElifToken.getLocation(), ConditionRange, CI.IfLoc);
3754 break;
3755 case tok::pp_elifndef:
3756 Callbacks->Elifndef(ElifToken.getLocation(), ConditionRange, CI.IfLoc);
3757 break;
3758 default:
3759 assert(false && "unexpected directive kind");
3760 break;
3761 }
3762 }
3763
3764 bool RetainExcludedCB = PPOpts.RetainExcludedConditionalBlocks &&
3766
3767 if ((PPOpts.SingleFileParseMode && !CI.FoundNonSkip) || RetainExcludedCB) {
3768 // In 'single-file-parse mode' undefined identifiers trigger parsing of all
3769 // the directive blocks.
3770 CurPPLexer->pushConditionalLevel(ElifToken.getLocation(), /*wasskip*/false,
3771 /*foundnonskip*/false, /*foundelse*/false);
3772 return;
3773 }
3774
3775 // Finally, skip the rest of the contents of this block.
3776 SkipExcludedConditionalBlock(
3777 HashToken.getLocation(), CI.IfLoc, /*Foundnonskip*/ true,
3778 /*FoundElse*/ CI.FoundElse, ElifToken.getLocation());
3779}
3780
3781std::optional<LexEmbedParametersResult>
3782Preprocessor::LexEmbedParameters(Token &CurTok, bool ForHasEmbed) {
3784 tok::TokenKind EndTokenKind = ForHasEmbed ? tok::r_paren : tok::eod;
3785
3786 auto DiagMismatchedBracesAndSkipToEOD =
3788 std::pair<tok::TokenKind, SourceLocation> Matches) {
3789 Diag(CurTok, diag::err_expected) << Expected;
3790 Diag(Matches.second, diag::note_matching) << Matches.first;
3791 if (CurTok.isNot(tok::eod))
3793 };
3794
3795 auto ExpectOrDiagAndSkipToEOD = [&](tok::TokenKind Kind) {
3796 if (CurTok.isNot(Kind)) {
3797 Diag(CurTok, diag::err_expected) << Kind;
3798 if (CurTok.isNot(tok::eod))
3800 return false;
3801 }
3802 return true;
3803 };
3804
3805 // C23 6.10:
3806 // pp-parameter-name:
3807 // pp-standard-parameter
3808 // pp-prefixed-parameter
3809 //
3810 // pp-standard-parameter:
3811 // identifier
3812 //
3813 // pp-prefixed-parameter:
3814 // identifier :: identifier
3815 auto LexPPParameterName = [&]() -> std::optional<std::string> {
3816 // We expect the current token to be an identifier; if it's not, things
3817 // have gone wrong.
3818 if (!ExpectOrDiagAndSkipToEOD(tok::identifier))
3819 return std::nullopt;
3820
3821 const IdentifierInfo *Prefix = CurTok.getIdentifierInfo();
3822
3823 // Lex another token; it is either a :: or we're done with the parameter
3824 // name.
3825 LexNonComment(CurTok);
3826 if (CurTok.is(tok::coloncolon)) {
3827 // We found a ::, so lex another identifier token.
3828 LexNonComment(CurTok);
3829 if (!ExpectOrDiagAndSkipToEOD(tok::identifier))
3830 return std::nullopt;
3831
3832 const IdentifierInfo *Suffix = CurTok.getIdentifierInfo();
3833
3834 // Lex another token so we're past the name.
3835 LexNonComment(CurTok);
3836 return (llvm::Twine(Prefix->getName()) + "::" + Suffix->getName()).str();
3837 }
3838 return Prefix->getName().str();
3839 };
3840
3841 // C23 6.10p5: In all aspects, a preprocessor standard parameter specified by
3842 // this document as an identifier pp_param and an identifier of the form
3843 // __pp_param__ shall behave the same when used as a preprocessor parameter,
3844 // except for the spelling.
3845 auto NormalizeParameterName = [](StringRef Name) {
3846 if (Name.size() > 4 && Name.starts_with("__") && Name.ends_with("__"))
3847 return Name.substr(2, Name.size() - 4);
3848 return Name;
3849 };
3850
3851 auto LexParenthesizedIntegerExpr = [&]() -> std::optional<size_t> {
3852 // we have a limit parameter and its internals are processed using
3853 // evaluation rules from #if.
3854 if (!ExpectOrDiagAndSkipToEOD(tok::l_paren))
3855 return std::nullopt;
3856
3857 // We do not consume the ( because EvaluateDirectiveExpression will lex
3858 // the next token for us.
3859 IdentifierInfo *ParameterIfNDef = nullptr;
3860 bool EvaluatedDefined;
3861 DirectiveEvalResult LimitEvalResult = EvaluateDirectiveExpression(
3862 ParameterIfNDef, CurTok, EvaluatedDefined, /*CheckForEOD=*/false);
3863
3864 if (!LimitEvalResult.Value) {
3865 // If there was an error evaluating the directive expression, we expect
3866 // to be at the end of directive token.
3867 assert(CurTok.is(tok::eod) && "expect to be at the end of directive");
3868 return std::nullopt;
3869 }
3870
3871 if (!ExpectOrDiagAndSkipToEOD(tok::r_paren))
3872 return std::nullopt;
3873
3874 // Eat the ).
3875 LexNonComment(CurTok);
3876
3877 // C23 6.10.3.2p2: The token defined shall not appear within the constant
3878 // expression.
3879 if (EvaluatedDefined) {
3880 Diag(CurTok, diag::err_defined_in_pp_embed);
3881 return std::nullopt;
3882 }
3883
3884 if (LimitEvalResult.Value) {
3885 const llvm::APSInt &Result = *LimitEvalResult.Value;
3886 if (Result.isNegative()) {
3887 Diag(CurTok, diag::err_requires_positive_value)
3888 << toString(Result, 10) << /*positive*/ 0;
3889 if (CurTok.isNot(EndTokenKind))
3891 return std::nullopt;
3892 }
3893 return Result.getLimitedValue();
3894 }
3895 return std::nullopt;
3896 };
3897
3898 auto GetMatchingCloseBracket = [](tok::TokenKind Kind) {
3899 switch (Kind) {
3900 case tok::l_paren:
3901 return tok::r_paren;
3902 case tok::l_brace:
3903 return tok::r_brace;
3904 case tok::l_square:
3905 return tok::r_square;
3906 default:
3907 llvm_unreachable("should not get here");
3908 }
3909 };
3910
3911 auto LexParenthesizedBalancedTokenSoup =
3912 [&](llvm::SmallVectorImpl<Token> &Tokens) {
3913 std::vector<std::pair<tok::TokenKind, SourceLocation>> BracketStack;
3914
3915 // We expect the current token to be a left paren.
3916 if (!ExpectOrDiagAndSkipToEOD(tok::l_paren))
3917 return false;
3918 LexNonComment(CurTok); // Eat the (
3919
3920 bool WaitingForInnerCloseParen = false;
3921 while (CurTok.isNot(tok::eod) &&
3922 (WaitingForInnerCloseParen || CurTok.isNot(tok::r_paren))) {
3923 switch (CurTok.getKind()) {
3924 default: // Shutting up diagnostics about not fully-covered switch.
3925 break;
3926 case tok::l_paren:
3927 WaitingForInnerCloseParen = true;
3928 [[fallthrough]];
3929 case tok::l_brace:
3930 case tok::l_square:
3931 BracketStack.push_back({CurTok.getKind(), CurTok.getLocation()});
3932 break;
3933 case tok::r_paren:
3934 WaitingForInnerCloseParen = false;
3935 [[fallthrough]];
3936 case tok::r_brace:
3937 case tok::r_square: {
3938 if (BracketStack.empty()) {
3939 ExpectOrDiagAndSkipToEOD(tok::r_paren);
3940 return false;
3941 }
3942 tok::TokenKind Matching =
3943 GetMatchingCloseBracket(BracketStack.back().first);
3944 if (CurTok.getKind() != Matching) {
3945 DiagMismatchedBracesAndSkipToEOD(Matching, BracketStack.back());
3946 return false;
3947 }
3948 BracketStack.pop_back();
3949 } break;
3950 }
3951 Tokens.push_back(CurTok);
3952 LexNonComment(CurTok);
3953 }
3954
3955 // When we're done, we want to eat the closing paren.
3956 if (!ExpectOrDiagAndSkipToEOD(tok::r_paren))
3957 return false;
3958
3959 LexNonComment(CurTok); // Eat the )
3960 return true;
3961 };
3962
3963 LexNonComment(CurTok); // Prime the pump.
3964 while (!CurTok.isOneOf(EndTokenKind, tok::eod)) {
3965 SourceLocation ParamStartLoc = CurTok.getLocation();
3966 std::optional<std::string> ParamName = LexPPParameterName();
3967 if (!ParamName)
3968 return std::nullopt;
3969 StringRef Parameter = NormalizeParameterName(*ParamName);
3970
3971 // Lex the parameters (dependent on the parameter type we want!).
3972 //
3973 // C23 6.10.3.Xp1: The X standard embed parameter may appear zero times or
3974 // one time in the embed parameter sequence.
3975 if (Parameter == "limit") {
3976 if (Result.MaybeLimitParam)
3977 Diag(CurTok, diag::err_pp_embed_dup_params) << Parameter;
3978
3979 std::optional<size_t> Limit = LexParenthesizedIntegerExpr();
3980 if (!Limit)
3981 return std::nullopt;
3982 Result.MaybeLimitParam =
3983 PPEmbedParameterLimit{*Limit, {ParamStartLoc, CurTok.getLocation()}};
3984 } else if (Parameter == "clang::offset") {
3985 if (Result.MaybeOffsetParam)
3986 Diag(CurTok, diag::err_pp_embed_dup_params) << Parameter;
3987
3988 std::optional<size_t> Offset = LexParenthesizedIntegerExpr();
3989 if (!Offset)
3990 return std::nullopt;
3991 Result.MaybeOffsetParam = PPEmbedParameterOffset{
3992 *Offset, {ParamStartLoc, CurTok.getLocation()}};
3993 } else if (Parameter == "prefix") {
3994 if (Result.MaybePrefixParam)
3995 Diag(CurTok, diag::err_pp_embed_dup_params) << Parameter;
3996
3998 if (!LexParenthesizedBalancedTokenSoup(Soup))
3999 return std::nullopt;
4000 Result.MaybePrefixParam = PPEmbedParameterPrefix{
4001 std::move(Soup), {ParamStartLoc, CurTok.getLocation()}};
4002 } else if (Parameter == "suffix") {
4003 if (Result.MaybeSuffixParam)
4004 Diag(CurTok, diag::err_pp_embed_dup_params) << Parameter;
4005
4007 if (!LexParenthesizedBalancedTokenSoup(Soup))
4008 return std::nullopt;
4009 Result.MaybeSuffixParam = PPEmbedParameterSuffix{
4010 std::move(Soup), {ParamStartLoc, CurTok.getLocation()}};
4011 } else if (Parameter == "if_empty") {
4012 if (Result.MaybeIfEmptyParam)
4013 Diag(CurTok, diag::err_pp_embed_dup_params) << Parameter;
4014
4016 if (!LexParenthesizedBalancedTokenSoup(Soup))
4017 return std::nullopt;
4018 Result.MaybeIfEmptyParam = PPEmbedParameterIfEmpty{
4019 std::move(Soup), {ParamStartLoc, CurTok.getLocation()}};
4020 } else {
4021 ++Result.UnrecognizedParams;
4022
4023 // If there's a left paren, we need to parse a balanced token sequence
4024 // and just eat those tokens.
4025 if (CurTok.is(tok::l_paren)) {
4027 if (!LexParenthesizedBalancedTokenSoup(Soup))
4028 return std::nullopt;
4029 }
4030 if (!ForHasEmbed) {
4031 Diag(ParamStartLoc, diag::err_pp_unknown_parameter) << 1 << Parameter;
4032 if (CurTok.isNot(EndTokenKind))
4034 return std::nullopt;
4035 }
4036 }
4037 }
4038 return Result;
4039}
4040
4041void Preprocessor::HandleEmbedDirectiveImpl(
4042 SourceLocation HashLoc, const LexEmbedParametersResult &Params,
4043 StringRef BinaryContents, StringRef FileName) {
4044 if (BinaryContents.empty()) {
4045 // If we have no binary contents, the only thing we need to emit are the
4046 // if_empty tokens, if any.
4047 // FIXME: this loses AST fidelity; nothing in the compiler will see that
4048 // these tokens came from #embed. We have to hack around this when printing
4049 // preprocessed output. The same is true for prefix and suffix tokens.
4050 if (Params.MaybeIfEmptyParam) {
4051 ArrayRef<Token> Toks = Params.MaybeIfEmptyParam->Tokens;
4052 size_t TokCount = Toks.size();
4053 auto NewToks = std::make_unique<Token[]>(TokCount);
4054 llvm::copy(Toks, NewToks.get());
4055 EnterTokenStream(std::move(NewToks), TokCount, true, true);
4056 }
4057 return;
4058 }
4059
4060 size_t NumPrefixToks = Params.PrefixTokenCount(),
4061 NumSuffixToks = Params.SuffixTokenCount();
4062 size_t TotalNumToks = 1 + NumPrefixToks + NumSuffixToks;
4063 size_t CurIdx = 0;
4064 auto Toks = std::make_unique<Token[]>(TotalNumToks);
4065
4066 // Add the prefix tokens, if any.
4067 if (Params.MaybePrefixParam) {
4068 llvm::copy(Params.MaybePrefixParam->Tokens, &Toks[CurIdx]);
4069 CurIdx += NumPrefixToks;
4070 }
4071
4072 EmbedAnnotationData *Data = new (BP) EmbedAnnotationData;
4073 Data->BinaryData = BinaryContents;
4074 Data->FileName = FileName;
4075
4076 Toks[CurIdx].startToken();
4077 Toks[CurIdx].setKind(tok::annot_embed);
4078 Toks[CurIdx].setAnnotationRange(HashLoc);
4079 Toks[CurIdx++].setAnnotationValue(Data);
4080
4081 // Now add the suffix tokens, if any.
4082 if (Params.MaybeSuffixParam) {
4083 llvm::copy(Params.MaybeSuffixParam->Tokens, &Toks[CurIdx]);
4084 CurIdx += NumSuffixToks;
4085 }
4086
4087 assert(CurIdx == TotalNumToks && "Calculated the incorrect number of tokens");
4088 EnterTokenStream(std::move(Toks), TotalNumToks, true, true);
4089}
4090
4091void Preprocessor::HandleEmbedDirective(SourceLocation HashLoc,
4092 Token &EmbedTok) {
4093 // Give the usual extension/compatibility warnings.
4094 if (LangOpts.C23)
4095 Diag(EmbedTok, diag::warn_compat_pp_embed_directive);
4096 else
4097 Diag(EmbedTok, diag::ext_pp_embed_directive)
4098 << (LangOpts.CPlusPlus ? /*Clang*/ 1 : /*C23*/ 0);
4099
4100 // Parse the filename header
4101 Token FilenameTok;
4102 if (LexHeaderName(FilenameTok))
4103 return;
4104
4105 if (FilenameTok.isNot(tok::header_name)) {
4106 Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
4107 if (FilenameTok.isNot(tok::eod))
4109 return;
4110 }
4111
4112 // Parse the optional sequence of
4113 // directive-parameters:
4114 // identifier parameter-name-list[opt] directive-argument-list[opt]
4115 // directive-argument-list:
4116 // '(' balanced-token-sequence ')'
4117 // parameter-name-list:
4118 // '::' identifier parameter-name-list[opt]
4119 Token CurTok;
4120 std::optional<LexEmbedParametersResult> Params =
4121 LexEmbedParameters(CurTok, /*ForHasEmbed=*/false);
4122
4123 assert((Params || CurTok.is(tok::eod)) &&
4124 "expected success or to be at the end of the directive");
4125 if (!Params)
4126 return;
4127
4128 // Now, splat the data out!
4129 SmallString<128> FilenameBuffer;
4130 StringRef Filename = getSpelling(FilenameTok, FilenameBuffer);
4131 StringRef OriginalFilename = Filename;
4132 bool isAngled =
4133 GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
4134
4135 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
4136 // error.
4137 if (Filename.empty())
4138 return;
4139
4140 OptionalFileEntryRef MaybeFileRef =
4141 this->LookupEmbedFile(Filename, isAngled, /*OpenFile=*/true);
4142 if (!MaybeFileRef) {
4143 // could not find file
4144 if (Callbacks && Callbacks->EmbedFileNotFound(Filename)) {
4145 return;
4146 }
4147 Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
4148 return;
4149 }
4150
4151 if (MaybeFileRef->isDeviceFile()) {
4152 Diag(FilenameTok, diag::err_pp_embed_device_file) << Filename;
4153 return;
4154 }
4155
4156 std::optional<llvm::MemoryBufferRef> MaybeFile =
4158 if (!MaybeFile) {
4159 // could not find file
4160 Diag(FilenameTok, diag::err_cannot_open_file)
4161 << Filename << "a buffer to the contents could not be created";
4162 return;
4163 }
4164 StringRef BinaryContents = MaybeFile->getBuffer();
4165
4166 // The order is important between 'offset' and 'limit'; we want to offset
4167 // first and then limit second; otherwise we may reduce the notional resource
4168 // size to something too small to offset into.
4169 if (Params->MaybeOffsetParam) {
4170 // FIXME: just like with the limit() and if_empty() parameters, this loses
4171 // source fidelity in the AST; it has no idea that there was an offset
4172 // involved.
4173 // offsets all the way to the end of the file make for an empty file.
4174 BinaryContents = BinaryContents.substr(Params->MaybeOffsetParam->Offset);
4175 }
4176
4177 if (Params->MaybeLimitParam) {
4178 // FIXME: just like with the clang::offset() and if_empty() parameters,
4179 // this loses source fidelity in the AST; it has no idea there was a limit
4180 // involved.
4181 BinaryContents = BinaryContents.substr(0, Params->MaybeLimitParam->Limit);
4182 }
4183
4184 if (Callbacks)
4185 Callbacks->EmbedDirective(HashLoc, Filename, isAngled, MaybeFileRef,
4186 *Params);
4187 // getSpelling() may return a buffer from the token itself or it may use the
4188 // SmallString buffer we provided. getSpelling() may also return a string that
4189 // is actually longer than FilenameTok.getLength(), so we first pass a
4190 // locally created buffer to getSpelling() to get the string of real length
4191 // and then we allocate a long living buffer because the buffer we used
4192 // previously will only live till the end of this function and we need
4193 // filename info to live longer.
4194 void *Mem = BP.Allocate(OriginalFilename.size(), alignof(char *));
4195 memcpy(Mem, OriginalFilename.data(), OriginalFilename.size());
4196 StringRef FilenameToGo =
4197 StringRef(static_cast<char *>(Mem), OriginalFilename.size());
4198 HandleEmbedDirectiveImpl(HashLoc, *Params, BinaryContents, FilenameToGo);
4199}
4200
4201/// HandleCXXImportDirective - Handle the C++ modules import directives
4202///
4203/// pp-import:
4204/// export[opt] import header-name pp-tokens[opt] ; new-line
4205/// export[opt] import header-name-tokens pp-tokens[opt] ; new-line
4206/// export[opt] import pp-tokens ; new-line
4207///
4208/// The header importing are replaced by annot_header_unit token, and the
4209/// lexed module name are replaced by annot_module_name token.
4211 assert(getLangOpts().CPlusPlusModules && ImportTok.is(tok::kw_import));
4212 llvm::SaveAndRestore<bool> SaveImportingCXXModules(
4213 this->ImportingCXXNamedModules, true);
4214
4215 Token Tok;
4216 if (LexHeaderName(Tok)) {
4217 if (Tok.isNot(tok::eod))
4219 return;
4220 }
4221
4222 SourceLocation UseLoc = ImportTok.getLocation();
4223 SmallVector<Token, 4> DirToks{ImportTok};
4225 bool ImportingHeader = false;
4226 bool IsPartition = false;
4227
4228 switch (Tok.getKind()) {
4229 case tok::header_name:
4230 ImportingHeader = true;
4231 DirToks.push_back(Tok);
4232 Lex(DirToks.emplace_back());
4233 break;
4234 case tok::colon:
4235 IsPartition = true;
4236 DirToks.push_back(Tok);
4237 UseLoc = Tok.getLocation();
4238 Lex(Tok);
4239 [[fallthrough]];
4240 case tok::identifier: {
4241 if (HandleModuleName(ImportTok.getIdentifierInfo()->getName(), UseLoc, Tok,
4242 Path, DirToks, /*AllowMacroExpansion=*/true,
4243 IsPartition))
4244 return;
4245
4246 std::string FlatName;
4247 bool IsValid =
4248 (IsPartition && ModuleDeclState.isNamedModule()) || !IsPartition;
4249 if (Callbacks && IsValid) {
4250 if (IsPartition && ModuleDeclState.isNamedModule()) {
4251 FlatName += ModuleDeclState.getPrimaryName();
4252 FlatName += ":";
4253 }
4254
4255 FlatName += ModuleLoader::getFlatNameFromPath(Path);
4256 SourceLocation StartLoc = IsPartition ? UseLoc : Path[0].getLoc();
4257 IdentifierLoc FlatNameLoc(StartLoc, getIdentifierInfo(FlatName));
4258
4259 // We don't/shouldn't load the standard c++20 modules when preprocessing.
4260 // so the imported module is nullptr.
4261 Callbacks->moduleImport(ImportTok.getLocation(),
4262 ModuleIdPath(FlatNameLoc),
4263 /*Imported=*/nullptr);
4264 }
4265 break;
4266 }
4267 default:
4268 DirToks.push_back(Tok);
4269 break;
4270 }
4271
4272 // Consume the pp-import-suffix and expand any macros in it now, if we're not
4273 // at the semicolon already.
4274 if (!DirToks.back().isOneOf(tok::semi, tok::eod))
4275 CollectPPImportSuffix(DirToks);
4276
4277 if (DirToks.back().isNot(tok::eod))
4279 else
4280 DirToks.pop_back();
4281
4282 // This is not a pp-import after all.
4283 if (DirToks.back().isNot(tok::semi)) {
4285 return;
4286 }
4287
4288 if (ImportingHeader) {
4289 // C++2a [cpp.module]p1:
4290 // The ';' preprocessing-token terminating a pp-import shall not have
4291 // been produced by macro replacement.
4292 SourceLocation SemiLoc = DirToks.back().getLocation();
4293 if (SemiLoc.isMacroID())
4294 Diag(SemiLoc, diag::err_header_import_semi_in_macro);
4295
4296 auto Action = HandleHeaderIncludeOrImport(
4297 /*HashLoc*/ SourceLocation(), ImportTok, Tok, SemiLoc);
4298 switch (Action.Kind) {
4299 case ImportAction::None:
4300 break;
4301
4302 case ImportAction::ModuleBegin:
4303 // Let the parser know we're textually entering the module.
4304 DirToks.emplace_back();
4305 DirToks.back().startToken();
4306 DirToks.back().setKind(tok::annot_module_begin);
4307 DirToks.back().setLocation(SemiLoc);
4308 DirToks.back().setAnnotationEndLoc(SemiLoc);
4309 DirToks.back().setAnnotationValue(Action.ModuleForHeader);
4310 [[fallthrough]];
4311
4312 case ImportAction::ModuleImport:
4313 case ImportAction::HeaderUnitImport:
4314 case ImportAction::SkippedModuleImport:
4315 // We chose to import (or textually enter) the file. Convert the
4316 // header-name token into a header unit annotation token.
4317 DirToks[1].setKind(tok::annot_header_unit);
4318 DirToks[1].setAnnotationEndLoc(DirToks[0].getLocation());
4319 DirToks[1].setAnnotationValue(Action.ModuleForHeader);
4320 // FIXME: Call the moduleImport callback?
4321 break;
4322 case ImportAction::Failure:
4323 assert(TheModuleLoader.HadFatalFailure &&
4324 "This should be an early exit only to a fatal error");
4325 CurLexer->cutOffLexing();
4326 return;
4327 }
4328 }
4329
4331}
4332
4333/// HandleCXXModuleDirective - Handle C++ module declaration directives.
4334///
4335/// pp-module:
4336/// export[opt] module pp-tokens[opt] ; new-line
4337///
4338/// pp-module-name:
4339/// pp-module-name-qualifier[opt] identifier
4340/// pp-module-partition:
4341/// : pp-module-name-qualifier[opt] identifier
4342/// pp-module-name-qualifier:
4343/// identifier .
4344/// pp-module-name-qualifier identifier .
4345///
4346/// global-module-fragment:
4347/// module-keyword ; declaration-seq[opt]
4348///
4349/// private-module-fragment:
4350/// module-keyword : private ; declaration-seq[opt]
4351///
4352/// The lexed module name are replaced by annot_module_name token.
4354 assert(getLangOpts().CPlusPlusModules && ModuleTok.is(tok::kw_module));
4355 SourceLocation StartLoc = ModuleTok.getLocation();
4356
4357 Token Tok;
4358 SourceLocation UseLoc = ModuleTok.getLocation();
4359 SmallVector<Token, 4> DirToks{ModuleTok};
4360 SmallVector<IdentifierLoc, 2> Path, Partition;
4362
4363 switch (Tok.getKind()) {
4364 // Global Module Fragment.
4365 case tok::semi:
4366 DirToks.push_back(Tok);
4367 break;
4368 case tok::colon:
4369 DirToks.push_back(Tok);
4371 if (Tok.isNot(tok::kw_private)) {
4372 if (Tok.isNot(tok::eod))
4374 /*EnableMacros=*/false, &DirToks);
4376 return;
4377 }
4378 DirToks.push_back(Tok);
4379 break;
4380 case tok::identifier: {
4381 if (HandleModuleName(ModuleTok.getIdentifierInfo()->getName(), UseLoc, Tok,
4382 Path, DirToks, /*AllowMacroExpansion=*/false,
4383 /*IsPartition=*/false))
4384 return;
4385
4386 // C++20 [cpp.module]p
4387 // The pp-tokens, if any, of a pp-module shall be of the form:
4388 // pp-module-name pp-module-partition[opt] pp-tokens[opt]
4389 if (Tok.is(tok::colon)) {
4391 if (HandleModuleName(ModuleTok.getIdentifierInfo()->getName(), UseLoc,
4392 Tok, Partition, DirToks,
4393 /*AllowMacroExpansion=*/false, /*IsPartition=*/true))
4394 return;
4395 }
4396
4397 // If the current token is a macro definition, put it back to token stream
4398 // and expand any macros in it later.
4399 //
4400 // export module M ATTR(some_attr); // -D'ATTR(x)=[[x]]'
4401 //
4402 // Current token is `ATTR`.
4403 if (Tok.is(tok::identifier) &&
4404 getMacroDefinition(Tok.getIdentifierInfo())) {
4405 std::unique_ptr<Token[]> TokCopy = std::make_unique<Token[]>(1);
4406 TokCopy[0] = Tok;
4407 EnterTokenStream(std::move(TokCopy), /*NumToks=*/1,
4408 /*DisableMacroExpansion=*/false, /*IsReinject=*/false);
4409 Lex(Tok);
4410 DirToks.back() = Tok;
4411 }
4412 break;
4413 }
4414 default:
4415 DirToks.push_back(Tok);
4416 break;
4417 }
4418
4419 // Consume the pp-import-suffix and expand any macros in it now, if we're not
4420 // at the semicolon already.
4421 std::optional<Token> NextPPTok =
4422 DirToks.back().is(tok::eod) ? peekNextPPToken() : DirToks.back();
4423
4424 // Only ';' and '[' are allowed after module name.
4425 // We also check 'private' because the previous is not a module name.
4426 if (NextPPTok) {
4427 if (NextPPTok->is(tok::raw_identifier))
4428 LookUpIdentifierInfo(*NextPPTok);
4429 if (!NextPPTok->isOneOf(tok::semi, tok::eod, tok::l_square,
4430 tok::kw_private))
4431 Diag(*NextPPTok, diag::err_pp_unexpected_tok_after_module_name)
4432 << getSpelling(*NextPPTok);
4433 }
4434
4435 if (!DirToks.back().isOneOf(tok::semi, tok::eod)) {
4436 // Consume the pp-import-suffix and expand any macros in it now. We'll add
4437 // it back into the token stream later.
4438 CollectPPImportSuffix(DirToks);
4439 }
4440
4441 SourceLocation End =
4442 DirToks.back().isNot(tok::eod)
4444 /*EnableMacros=*/false, &DirToks)
4445
4446 : DirToks.pop_back_val().getLocation();
4447
4448 if (!IncludeMacroStack.empty()) {
4449 Diag(StartLoc, diag::err_pp_module_decl_in_header)
4450 << SourceRange(StartLoc, End);
4451 }
4452
4453 if (CurPPLexer->getConditionalStackDepth() != 0) {
4454 Diag(StartLoc, diag::err_pp_cond_span_module_decl)
4455 << SourceRange(StartLoc, End);
4456 }
4458}
4459
4460/// Lex a token following the 'import' contextual keyword.
4461///
4462/// pp-import:
4463/// [ObjC] @ import module-name ;
4464///
4465/// module-name:
4466/// module-name-qualifier[opt] identifier
4467///
4468/// module-name-qualifier
4469/// module-name-qualifier[opt] identifier .
4470///
4471/// We respond to a pp-import by importing macros from the named module.
4472void Preprocessor::HandleObjCImportDirective(Token &AtTok, Token &ImportTok) {
4473 assert(getLangOpts().ObjC && AtTok.is(tok::at) &&
4474 ImportTok.isObjCAtKeyword(tok::objc_import));
4475 ImportTok.setKind(tok::kw_import);
4476 SmallVector<Token, 32> DirToks{AtTok, ImportTok};
4478 SourceLocation UseLoc = ImportTok.getLocation();
4479 ModuleImportLoc = ImportTok.getLocation();
4480 Token Tok;
4481 Lex(Tok);
4482 if (HandleModuleName(ImportTok.getIdentifierInfo()->getName(), UseLoc, Tok,
4483 Path, DirToks,
4484 /*AllowMacroExpansion=*/true,
4485 /*IsPartition=*/false))
4486 return;
4487
4488 // Consume the pp-import-suffix and expand any macros in it now, if we're not
4489 // at the semicolon already.
4490 if (!DirToks.back().isOneOf(tok::semi, tok::eod))
4491 CollectPPImportSuffix(DirToks);
4492
4493 SourceLocation End =
4494 DirToks.back().isNot(tok::eod)
4496 /*EnableMacros=*/false, &DirToks)
4497
4498 : DirToks.pop_back_val().getLocation();
4499
4500 Module *Imported = nullptr;
4501 if (getLangOpts().Modules) {
4502 Imported = TheModuleLoader.loadModule(ModuleImportLoc, Path, Module::Hidden,
4503 /*IsInclusionDirective=*/false);
4504 if (Imported)
4505 makeModuleVisible(Imported, End);
4506 }
4507
4508 if (Callbacks)
4509 Callbacks->moduleImport(ModuleImportLoc, Path, Imported);
4510
4512}
static bool isInMainFile(const clang::Diagnostic &D)
Definition ASTUnit.cpp:595
Defines interfaces for clang::DirectoryEntry and clang::DirectoryEntryRef.
Defines the clang::FileManager interface and associated types.
Token Tok
The Token.
Defines the clang::IdentifierInfo, clang::IdentifierTable, and clang::Selector interfaces.
Defines the clang::LangOptions interface.
Defines the clang::MacroInfo and clang::MacroDirective classes.
Defines the clang::Module class, which describes a module in the source code.
#define SM(sm)
Defines the PPCallbacks interface.
static bool ReadLineMarkerFlags(bool &IsFileEntry, bool &IsFileExit, SrcMgr::CharacteristicKind &FileKind, Preprocessor &PP)
ReadLineMarkerFlags - Parse and validate any flags at the end of a GNU line marker directive.
static bool isConfigurationPattern(Token &MacroName, MacroInfo *MI, const LangOptions &LOptions)
static void diagnoseAutoModuleImport(Preprocessor &PP, SourceLocation HashLoc, Token &IncludeTok, ArrayRef< IdentifierLoc > Path, SourceLocation PathEnd)
Produce a diagnostic informing the user that a include or similar was implicitly treated as a module ...
static std::optional< StringRef > findSimilarStr(StringRef LHS, const std::vector< StringRef > &Candidates)
Find a similar string in Candidates.
static bool isLanguageDefinedBuiltin(const SourceManager &SourceMgr, const MacroInfo *MI, const StringRef MacroName)
static bool trySimplifyPath(SmallVectorImpl< StringRef > &Components, StringRef RealPathName, llvm::sys::path::Style Separator)
static bool warnByDefaultOnWrongCase(StringRef Include)
MacroDiag
Enumerates possible cases of define/undef a reserved identifier.
@ MD_ReservedMacro
@ MD_ReservedAttributeIdentifier
@ MD_NoWarn
@ MD_KeywordDef
static bool isFeatureTestMacro(StringRef MacroName)
static bool GetLineValue(Token &DigitTok, unsigned &Val, unsigned DiagID, Preprocessor &PP, bool IsGNULineDirective=false)
GetLineValue - Convert a numeric token into an unsigned value, emitting Diagnostic DiagID if it is in...
PPElifDiag
Enumerates possible select values for the pp_err_elif_after_else and pp_err_elif_without_if diagnosti...
@ PED_Elifndef
@ PED_Elifdef
@ PED_Elif
static bool isReservedCXXAttributeName(Preprocessor &PP, IdentifierInfo *II)
static MacroDiag shouldWarnOnMacroUndef(Preprocessor &PP, IdentifierInfo *II)
static bool isObjCProtectedMacro(const IdentifierInfo *II)
static MacroDiag shouldWarnOnMacroDef(Preprocessor &PP, IdentifierInfo *II)
Defines the clang::Preprocessor interface.
static std::string toString(const clang::SanitizerSet &Sanitizers)
Produce a string containing comma-separated names of sanitizers in Sanitizers set.
Defines the clang::SourceLocation class and associated facilities.
static bool isInvalid(LocType Loc, bool *Invalid)
Defines the SourceManager interface.
Defines the clang::TokenKind enum and support functions.
VerifyDiagnosticConsumer::Directive Directive
__DEVICE__ void * memcpy(void *__a, const void *__b, size_t __c)
__device__ __2f16 float c
static AttrArgsInfo getCXX11AttrArgsInfo(const IdentifierInfo *Name)
Represents a byte-granular source range.
static CharSourceRange getCharRange(SourceRange R)
virtual void CodeCompleteMacroName(bool IsDefinition)
Callback invoked when performing code completion in a context where the name of a macro is expected.
A directive for a defined macro or a macro imported from a module.
Definition MacroInfo.h:433
Concrete class used by the front-end to report problems and issues.
Definition Diagnostic.h:233
A reference to a DirectoryEntry that includes the name of the directory as it was accessed by the Fil...
StringRef getName() const
bool isDeviceFile() const
Definition FileEntry.h:359
const FileEntry & getFileEntry() const
Definition FileEntry.h:70
DirectoryEntryRef getDir() const
Definition FileEntry.h:78
Cached information about one file (either on disk or in the virtual file system).
Definition FileEntry.h:302
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
bool isValid() const
Implements support for file system lookup, file system caching, and directory search management.
Definition FileManager.h:54
OptionalFileEntryRef getOptionalFileRef(StringRef Filename, bool OpenFile=false, bool CacheFailure=true, bool IsText=true)
Get a FileEntryRef if it exists, without doing anything on error.
OptionalDirectoryEntryRef getOptionalDirectoryRef(StringRef DirName, bool CacheFailure=true)
Get a DirectoryEntryRef if it exists, without doing anything on error.
Annotates a diagnostic with some code that should be inserted, removed, or replaced to fix the proble...
Definition Diagnostic.h:80
static FixItHint CreateReplacement(CharSourceRange RemoveRange, StringRef Code)
Create a code modification hint that replaces the given source range with the given code string.
Definition Diagnostic.h:141
static FixItHint CreateInsertion(SourceLocation InsertionLoc, StringRef Code, bool BeforePreviousInsertions=false)
Create a code modification hint that inserts the given code string at a specific location.
Definition Diagnostic.h:104
One of these records is kept for each identifier that is lexed.
tok::PPKeywordKind getPPKeywordID() const
Return the preprocessor keyword ID for this identifier.
bool isCPlusPlusOperatorKeyword() const
bool hadMacroDefinition() const
Returns true if this identifier was #defined to some value at any moment.
bool hasMacroDefinition() const
Return true if this identifier is #defined to some other value.
const char * getNameStart() const
Return the beginning of the actual null-terminated string for this identifier.
bool isKeyword(const LangOptions &LangOpts) const
Return true if this token is a keyword in the specified language.
ReservedIdentifierStatus isReserved(const LangOptions &LangOpts) const
Determine whether this is a name reserved for the implementation (C99 7.1.3, C++ [lib....
bool isStr(const char(&Str)[StrLen]) const
Return true if this is the identifier for the specified string.
StringRef getName() const
Return the actual identifier string.
A simple pair of identifier info and location.
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
bool IsHeaderFile
Indicates whether the front-end is explicitly told that the input is a header file (i....
std::string CurrentModule
The name of the current module, of which the main source file is a part.
const MacroInfo * getMacroInfo() const
Definition MacroInfo.h:417
Encapsulates the data about a macro definition (e.g.
Definition MacroInfo.h:40
bool isIdenticalTo(const MacroInfo &Other, Preprocessor &PP, bool Syntactically) const
Return true if the specified macro definition is equal to this macro in spelling, arguments,...
Definition MacroInfo.cpp:89
bool isUsed() const
Return false if this macro is defined in the main file and has not yet been used.
Definition MacroInfo.h:225
bool isC99Varargs() const
Definition MacroInfo.h:208
bool isAllowRedefinitionsWithoutWarning() const
Return true if this macro can be redefined without warning.
Definition MacroInfo.h:228
void setHasCommaPasting()
Definition MacroInfo.h:221
unsigned getNumTokens() const
Return the number of tokens that this macro expands to.
Definition MacroInfo.h:236
const Token & getReplacementToken(unsigned Tok) const
Definition MacroInfo.h:238
void setDefinitionEndLoc(SourceLocation EndLoc)
Set the location of the last token in the macro.
Definition MacroInfo.h:129
bool isBuiltinMacro() const
Return true if this macro requires processing before expansion.
Definition MacroInfo.h:218
void setTokens(ArrayRef< Token > Tokens, llvm::BumpPtrAllocator &PPAllocator)
Definition MacroInfo.h:264
void setParameterList(ArrayRef< IdentifierInfo * > List, llvm::BumpPtrAllocator &PPAllocator)
Set the specified list of identifiers as the parameter list for this macro.
Definition MacroInfo.h:167
SourceLocation getDefinitionLoc() const
Return the location that the macro was defined at.
Definition MacroInfo.h:126
void setIsFunctionLike()
Function/Object-likeness.
Definition MacroInfo.h:201
bool isObjectLike() const
Definition MacroInfo.h:203
void setIsWarnIfUnused(bool val)
Set the value of the IsWarnIfUnused flag.
Definition MacroInfo.h:163
int getParameterNum(const IdentifierInfo *Arg) const
Return the parameter number of the specified identifier, or -1 if the identifier is not a formal para...
Definition MacroInfo.h:192
void setIsGNUVarargs()
Definition MacroInfo.h:207
bool isWarnIfUnused() const
Return true if we should emit a warning if the macro is unused.
Definition MacroInfo.h:233
void setIsC99Varargs()
Varargs querying methods. This can only be set for function-like macros.
Definition MacroInfo.h:206
bool isMissingExpected() const
Determines whether the module, which failed to load, was actually a submodule that we expected to see...
bool isConfigMismatch() const
Determines whether the module failed to load due to a configuration mismatch with an explicitly-named...
virtual ModuleLoadResult loadModule(SourceLocation ImportLoc, ModuleIdPath Path, Module::NameVisibilityKind Visibility, bool IsInclusionDirective)=0
Attempt to load the given module.
static std::string getFlatNameFromPath(ModuleIdPath Path)
A header that is known to reside within a given module, whether it was included or excluded.
Definition ModuleMap.h:158
Module * getModule() const
Retrieve the module the header is stored in.
Definition ModuleMap.h:173
@ ExcludedHeader
This header is explicitly excluded from the module.
Definition ModuleMap.h:138
@ TextualHeader
This header is part of the module (for layering purposes) but should be textually included.
Definition ModuleMap.h:135
Describes a module or submodule.
Definition Module.h:340
StringRef getTopLevelModuleName() const
Retrieve the name of the top-level module.
Definition Module.h:950
bool isForBuilding(const LangOptions &LangOpts) const
Determine whether this module can be built in this compilation.
Definition Module.cpp:155
@ Hidden
All of the names in this module are hidden.
Definition Module.h:645
SourceLocation DefinitionLoc
The location of the module definition.
Definition Module.h:346
unsigned IsSystem
Whether this is a "system" module (which assumes that all headers in it are system headers).
Definition Module.h:589
std::string Name
The name of this module.
Definition Module.h:343
bool isAvailable() const
Determine whether this module is available for use within the current translation unit.
Definition Module.h:786
bool isHeaderUnit() const
Is this module a header unit.
Definition Module.h:887
Module * ShadowingModule
A module with the same name that shadows this module.
Definition Module.h:555
std::string getFullModuleName(bool AllowStringLiterals=false) const
Retrieve the full name of this module, including the path from its top-level module.
Definition Module.cpp:239
Module * getTopLevelModule()
Retrieve the top-level module for this (sub)module, which may be this module.
Definition Module.h:940
Preprocessor standard embed parameter "if_empty" if_empty( balanced-token-seq )
Preprocessor standard embed parameter "limit" limit( constant-expression )
Preprocessor extension embed parameter "clang::offset" clang::offset( constant-expression )
Preprocessor standard embed parameter "prefix" prefix( balanced-token-seq )
Preprocessor standard embed parameter "suffix" suffix( balanced-token-seq )
OptionalFileEntryRef getFileEntry() const
getFileEntry - Return the FileEntry corresponding to this FileID.
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
MacroDirective * getLocalMacroDirective(const IdentifierInfo *II) const
Given an identifier, return its latest non-imported MacroDirective if it is #define'd and not #undef'...
void EnterModuleSuffixTokenStream(ArrayRef< Token > Toks)
void markClangModuleAsAffecting(Module *M)
Mark the given clang module as affecting the current clang module or translation unit.
void HandleCXXImportDirective(Token Import)
HandleCXXImportDirective - Handle the C++ modules import directives.
SourceRange DiscardUntilEndOfDirective(SmallVectorImpl< Token > *DiscardedToks=nullptr)
Read and discard all tokens remaining on the current line until the tok::eod token is found.
const MacroInfo * getMacroInfo(const IdentifierInfo *II) const
bool isRecordingPreamble() const
void HandleSkippedDirectiveWhileUsingPCH(Token &Result, SourceLocation HashLoc)
Process directives while skipping until the through header or pragma hdrstop is found.
bool isInPrimaryFile() const
Return true if we're in the top-level file, not in a #include.
void markMacroAsUsed(MacroInfo *MI)
A macro is used, update information about macros that need unused warnings.
void EnterSubmodule(Module *M, SourceLocation ImportLoc, bool ForPragma)
IdentifierInfo * LookUpIdentifierInfo(Token &Identifier) const
Given a tok::raw_identifier token, look up the identifier information for the token and install it in...
void setCodeCompletionReached()
Note that we hit the code-completion point.
StringRef getNamedModuleName() const
Get the named module name we're preprocessing.
void Lex(Token &Result)
Lex the next token for this preprocessor.
bool EnterSourceFile(FileID FID, ConstSearchDirIterator Dir, SourceLocation Loc, bool IsFirstIncludeOfFile=true)
Add a source file to the top of the include stack and start lexing tokens from it instead of the curr...
void LexNonComment(Token &Result)
Lex a token.
friend class VAOptDefinitionContext
IdentifierInfo * getIdentifierInfo(StringRef Name) const
Return information about the specified preprocessor identifier token.
SourceManager & getSourceManager() const
MacroDefinition getMacroDefinition(const IdentifierInfo *II)
bool CheckMacroName(Token &MacroNameTok, MacroUse isDefineUndef, bool *ShadowFlag=nullptr)
SourceLocation CheckEndOfDirective(StringRef DirType, bool EnableMacros=false, SmallVectorImpl< Token > *ExtraToks=nullptr)
Ensure that the next token is a tok::eod token.
static bool checkModuleIsAvailable(const LangOptions &LangOpts, const TargetInfo &TargetInfo, const Module &M, DiagnosticsEngine &Diags)
Check that the given module is available, producing a diagnostic if not.
SourceLocation AdvanceToTokenCharacter(SourceLocation TokStart, unsigned Char) const
Given a location that specifies the start of a token, return a new location that specifies a characte...
Module * getCurrentModule()
Retrieves the module that we're currently building, if any.
OptionalFileEntryRef LookupEmbedFile(StringRef Filename, bool isAngled, bool OpenFile)
Given a "Filename" or <Filename> reference, look up the indicated embed resource.
void makeModuleVisible(Module *M, SourceLocation Loc, bool IncludeExports=true)
bool hadModuleLoaderFatalFailure() const
bool HandleModuleContextualKeyword(Token &Result)
Callback invoked when the lexer sees one of export, import or module token at the start of a line.
const TargetInfo & getTargetInfo() const
FileManager & getFileManager() const
bool LexHeaderName(Token &Result, bool AllowMacroExpansion=true)
Lex a token, forming a header-name token if possible.
bool isPCHThroughHeader(const FileEntry *FE)
Returns true if the FileEntry is the PCH through header.
friend class VariadicMacroScopeGuard
MacroInfo * AllocateMacroInfo(SourceLocation L)
Allocate a new MacroInfo object with the provided SourceLocation.
void LexUnexpandedToken(Token &Result)
Just like Lex, but disables macro expansion of identifier tokens.
bool alreadyIncluded(FileEntryRef File) const
Return true if this header has already been included.
FileID getPredefinesFileID() const
Returns the FileID for the preprocessor predefines.
void LexUnexpandedNonComment(Token &Result)
Like LexNonComment, but this disables macro expansion of identifier tokens.
StringRef getSpelling(SourceLocation loc, SmallVectorImpl< char > &buffer, bool *invalid=nullptr) const
Return the 'spelling' of the token at the given location; does not go up to the spelling location or ...
bool GetIncludeFilenameSpelling(SourceLocation Loc, StringRef &Buffer)
Turn the specified lexer token into a fully checked and spelled filename, e.g.
PreprocessorLexer * getCurrentFileLexer() const
Return the current file lexer being lexed from.
HeaderSearch & getHeaderSearchInfo() const
void emitMacroExpansionWarnings(const Token &Identifier, bool IsIfnDef=false) const
void HandleDirective(Token &Result)
Callback invoked when the lexer sees a # token at the start of a line.
void EnterAnnotationToken(SourceRange Range, tok::TokenKind Kind, void *AnnotationVal)
Enter an annotation token into the token stream.
OptionalFileEntryRef LookupFile(SourceLocation FilenameLoc, StringRef Filename, bool isAngled, ConstSearchDirIterator FromDir, const FileEntry *FromFile, ConstSearchDirIterator *CurDir, SmallVectorImpl< char > *SearchPath, SmallVectorImpl< char > *RelativePath, ModuleMap::KnownHeader *SuggestedModule, bool *IsMapped, bool *IsFrameworkFound, bool SkipCache=false, bool OpenFile=true, bool CacheFailures=true)
Given a "foo" or <foo> reference, look up the indicated file.
const LangOptions & getLangOpts() const
bool isInNamedModule() const
If we are preprocessing a named module.
OptionalFileEntryRef getHeaderToIncludeForDiagnostics(SourceLocation IncLoc, SourceLocation MLoc)
We want to produce a diagnostic at location IncLoc concerning an unreachable effect at location MLoc ...
bool isNextPPTokenOneOf(Ts... Ks) const
isNextPPTokenOneOf - Check whether the next pp-token is one of the specificed token kind.
DefMacroDirective * appendDefMacroDirective(IdentifierInfo *II, MacroInfo *MI, SourceLocation Loc)
void CollectPPImportSuffix(SmallVectorImpl< Token > &Toks, bool StopUntilEOD=false)
Collect the tokens of a C++20 pp-import-suffix.
void HandlePragmaHdrstop(Token &Tok)
Definition Pragma.cpp:885
DiagnosticsEngine & getDiagnostics() const
void HandleCXXModuleDirective(Token Module)
HandleCXXModuleDirective - Handle C++ module declaration directives.
std::optional< LexEmbedParametersResult > LexEmbedParameters(Token &Current, bool ForHasEmbed)
Lex the parameters for an embed directive, returns nullopt on error.
Module * getModuleForLocation(SourceLocation Loc, bool AllowTextual)
Find the module that owns the source or header file that Loc points to.
bool HandleModuleName(StringRef DirType, SourceLocation UseLoc, Token &Tok, SmallVectorImpl< IdentifierLoc > &Path, SmallVectorImpl< Token > &DirToks, bool AllowMacroExpansion, bool IsPartition)
DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID) const
Forwarding function for diagnostics.
bool usingPCHWithThroughHeader()
True if using a PCH with a through header.
Preprocessor(const PreprocessorOptions &PPOpts, DiagnosticsEngine &diags, const LangOptions &LangOpts, SourceManager &SM, HeaderSearch &Headers, ModuleLoader &TheModuleLoader, IdentifierInfoLookup *IILookup=nullptr, bool OwnsHeaderSearch=false, TranslationUnitKind TUKind=TU_Complete)
void appendMacroDirective(IdentifierInfo *II, MacroDirective *MD)
Add a directive to the macro directive history for this identifier.
Represents an unpacked "presumed" location which can be presented to the user.
bool isInvalid() const
Return true if this object is invalid or uninitialized.
SourceLocation getIncludeLoc() const
Return the presumed include location of this location.
Encodes a location in the source.
bool isValid() const
Return true if this is a valid SourceLocation object.
This class handles loading and caching of source files into memory.
bool isInMainFile(SourceLocation Loc) const
Returns whether the PresumedLoc for a given SourceLocation is in the main file.
std::optional< llvm::MemoryBufferRef > getMemoryBufferForFileOrNone(FileEntryRef File)
Retrieve the memory buffer associated with the given file.
A trivial tuple used to represent a source range.
void setBegin(SourceLocation b)
SourceLocation getEnd() const
void setEnd(SourceLocation e)
Exposes information about the current target.
Definition TargetInfo.h:227
Token - This structure provides full information about a lexed token.
Definition Token.h:36
IdentifierInfo * getIdentifierInfo() const
Definition Token.h:197
SourceLocation getEndLoc() const
Definition Token.h:169
void clearFlag(TokenFlags Flag)
Unset the specified flag.
Definition Token.h:264
SourceLocation getLocation() const
Return a source location identifier for the specified offset in the current file.
Definition Token.h:142
unsigned getLength() const
Definition Token.h:145
void setKind(tok::TokenKind K)
Definition Token.h:100
bool is(tok::TokenKind K) const
is/isNot - Predicates to check if this token is a specific kind, as in "if (Tok.is(tok::l_brace)) {....
Definition Token.h:104
tok::TokenKind getKind() const
Definition Token.h:99
bool isAtStartOfLine() const
isAtStartOfLine - Return true if this token is at the start of a line.
Definition Token.h:286
bool isOneOf(Ts... Ks) const
Definition Token.h:105
@ LeadingSpace
Definition Token.h:77
bool hasLeadingSpace() const
Return true if this token has whitespace before it.
Definition Token.h:294
bool isNot(tok::TokenKind K) const
Definition Token.h:111
bool hasUDSuffix() const
Return true if this token is a string or character literal which has a ud-suffix.
Definition Token.h:321
bool isObjCAtKeyword(tok::ObjCKeywordKind objcKey) const
Return true if we have an ObjC keyword identifier.
Definition Lexer.cpp:60
void startToken()
Reset all flags to cleared.
Definition Token.h:187
bool needsCleaning() const
Return true if this token has trigraphs or escaped newlines in it.
Definition Token.h:313
StringRef getRawIdentifier() const
getRawIdentifier - For a raw identifier token (i.e., an identifier lexed in raw mode),...
Definition Token.h:223
A directive for an undefined macro.
Definition MacroInfo.h:456
Kind getKind() const
Definition Value.h:137
A directive for setting the module visibility of a macro.
Definition MacroInfo.h:471
Defines the clang::TargetInfo interface.
CharacteristicKind
Indicates whether a file or directory holds normal user code, system code, or system code which is im...
uint32_t Literal
Literals are represented as positive integers.
Definition CNFFormula.h:35
bool Sub(InterpState &S, CodePtr OpPC)
Definition Interp.h:417
TokenKind
Provides a simple uniform namespace for tokens from all C languages.
Definition TokenKinds.h:25
PPKeywordKind
Provides a namespace for preprocessor keywords which start with a '#' at the beginning of the line.
Definition TokenKinds.h:33
The JSON file list parser is used to communicate input to InstallAPI.
CustomizableOptional< FileEntryRef > OptionalFileEntryRef
Definition FileEntry.h:208
bool isReservedInAllContexts(ReservedIdentifierStatus Status)
Determine whether an identifier is reserved in all contexts.
int hasAttribute(AttributeCommonInfo::Syntax Syntax, llvm::StringRef ScopeName, llvm::StringRef AttrName, const TargetInfo &Target, const LangOptions &LangOpts, bool CheckPlugins)
Return the version number associated with the attribute if we recognize and implement the attribute s...
ArrayRef< IdentifierLoc > ModuleIdPath
A sequence of identifier/location pairs used to describe a particular module or submodule,...
LLVM_READONLY char toLowercase(char c)
Converts the given ASCII character to its lowercase equivalent.
Definition CharInfo.h:224
detail::SearchDirIteratorImpl< true > ConstSearchDirIterator
nullptr
This class represents a compute construct, representing a 'Kind' of ‘parallel’, 'serial',...
MacroUse
Context in which macro name is used.
LLVM_READONLY bool isAlphanumeric(unsigned char c)
Return true if this character is an ASCII letter or digit: [a-zA-Z0-9].
Definition CharInfo.h:138
@ Module
Module linkage, which indicates that the entity can be referred to from other translation units withi...
Definition Linkage.h:54
@ Parameter
The parameter type of a method or function.
Definition TypeBase.h:908
@ Result
The result type of a method or function.
Definition TypeBase.h:905
LLVM_READONLY bool isDigit(unsigned char c)
Return true if this character is an ASCII digit: [0-9].
Definition CharInfo.h:114
LLVM_READONLY bool isLowercase(unsigned char c)
Return true if this character is a lowercase ASCII letter: [a-z].
Definition CharInfo.h:120
@ PIK_HashPragma
The pragma was introduced via #pragma.
Definition Pragma.h:36
OptionalDirectoryEntryRef Directory
The directory entry which should be used for the cached framework.
std::optional< PPEmbedParameterIfEmpty > MaybeIfEmptyParam
std::optional< PPEmbedParameterSuffix > MaybeSuffixParam
std::optional< PPEmbedParameterPrefix > MaybePrefixParam
std::string FeatureName
Definition Module.h:544
Stored information about a header directive that was found in the module map file but has not been re...
Definition Module.h:525
bool FoundNonSkip
True if we have emitted tokens already, and now we're in an #else block or something.
Definition Token.h:357
SourceLocation IfLoc
Location where the conditional started.
Definition Token.h:349
bool WasSkipping
True if this was contained in a skipping directive, e.g., in a "\#if 0" block.
Definition Token.h:353
bool FoundElse
True if we've seen a #else in this block.
Definition Token.h:361