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