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