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