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