clang 18.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,
68 SourceLocation Loc) {
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.startswith("__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.startswith("__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.equals("override") || Text.equals("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.startswith("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
882 SourceLocation Loc) {
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());
1813 Tok[0].setAnnotationEndLoc(Range.getEnd());
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 auto RealPathComponentIter = llvm::sys::path::rbegin(RealPathName);
1863 auto RealPathComponentEnd = llvm::sys::path::rend(RealPathName);
1864 int Cnt = 0;
1865 bool SuggestReplacement = false;
1866 // Below is a best-effort to handle ".." in paths. It is admittedly
1867 // not 100% correct in the presence of symlinks.
1868 for (auto &Component : llvm::reverse(Components)) {
1869 if ("." == Component) {
1870 } else if (".." == Component) {
1871 ++Cnt;
1872 } else if (Cnt) {
1873 --Cnt;
1874 } else if (RealPathComponentIter != RealPathComponentEnd) {
1875 if (Component != *RealPathComponentIter) {
1876 // If these path components differ by more than just case, then we
1877 // may be looking at symlinked paths. Bail on this diagnostic to avoid
1878 // noisy false positives.
1879 SuggestReplacement =
1880 RealPathComponentIter->equals_insensitive(Component);
1881 if (!SuggestReplacement)
1882 break;
1883 Component = *RealPathComponentIter;
1884 }
1885 ++RealPathComponentIter;
1886 }
1887 }
1888 return SuggestReplacement;
1889}
1890
1892 const TargetInfo &TargetInfo,
1893 const Module &M,
1894 DiagnosticsEngine &Diags) {
1895 Module::Requirement Requirement;
1897 Module *ShadowingModule = nullptr;
1898 if (M.isAvailable(LangOpts, TargetInfo, Requirement, MissingHeader,
1899 ShadowingModule))
1900 return false;
1901
1902 if (MissingHeader.FileNameLoc.isValid()) {
1903 Diags.Report(MissingHeader.FileNameLoc, diag::err_module_header_missing)
1904 << MissingHeader.IsUmbrella << MissingHeader.FileName;
1905 } else if (ShadowingModule) {
1906 Diags.Report(M.DefinitionLoc, diag::err_module_shadowed) << M.Name;
1907 Diags.Report(ShadowingModule->DefinitionLoc,
1908 diag::note_previous_definition);
1909 } else {
1910 // FIXME: Track the location at which the requirement was specified, and
1911 // use it here.
1912 Diags.Report(M.DefinitionLoc, diag::err_module_unavailable)
1913 << M.getFullModuleName() << Requirement.second << Requirement.first;
1914 }
1915 return true;
1916}
1917
1918std::pair<ConstSearchDirIterator, const FileEntry *>
1919Preprocessor::getIncludeNextStart(const Token &IncludeNextTok) const {
1920 // #include_next is like #include, except that we start searching after
1921 // the current found directory. If we can't do this, issue a
1922 // diagnostic.
1923 ConstSearchDirIterator Lookup = CurDirLookup;
1924 const FileEntry *LookupFromFile = nullptr;
1925
1926 if (isInPrimaryFile() && LangOpts.IsHeaderFile) {
1927 // If the main file is a header, then it's either for PCH/AST generation,
1928 // or libclang opened it. Either way, handle it as a normal include below
1929 // and do not complain about include_next.
1930 } else if (isInPrimaryFile()) {
1931 Lookup = nullptr;
1932 Diag(IncludeNextTok, diag::pp_include_next_in_primary);
1933 } else if (CurLexerSubmodule) {
1934 // Start looking up in the directory *after* the one in which the current
1935 // file would be found, if any.
1936 assert(CurPPLexer && "#include_next directive in macro?");
1937 LookupFromFile = CurPPLexer->getFileEntry();
1938 Lookup = nullptr;
1939 } else if (!Lookup) {
1940 // The current file was not found by walking the include path. Either it
1941 // is the primary file (handled above), or it was found by absolute path,
1942 // or it was found relative to such a file.
1943 // FIXME: Track enough information so we know which case we're in.
1944 Diag(IncludeNextTok, diag::pp_include_next_absolute_path);
1945 } else {
1946 // Start looking up in the next directory.
1947 ++Lookup;
1948 }
1949
1950 return {Lookup, LookupFromFile};
1951}
1952
1953/// HandleIncludeDirective - The "\#include" tokens have just been read, read
1954/// the file to be included from the lexer, then include it! This is a common
1955/// routine with functionality shared between \#include, \#include_next and
1956/// \#import. LookupFrom is set when this is a \#include_next directive, it
1957/// specifies the file to start searching from.
1958void Preprocessor::HandleIncludeDirective(SourceLocation HashLoc,
1959 Token &IncludeTok,
1960 ConstSearchDirIterator LookupFrom,
1961 const FileEntry *LookupFromFile) {
1962 Token FilenameTok;
1963 if (LexHeaderName(FilenameTok))
1964 return;
1965
1966 if (FilenameTok.isNot(tok::header_name)) {
1967 Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
1968 if (FilenameTok.isNot(tok::eod))
1970 return;
1971 }
1972
1973 // Verify that there is nothing after the filename, other than EOD. Note
1974 // that we allow macros that expand to nothing after the filename, because
1975 // this falls into the category of "#include pp-tokens new-line" specified
1976 // in C99 6.10.2p4.
1977 SourceLocation EndLoc =
1978 CheckEndOfDirective(IncludeTok.getIdentifierInfo()->getNameStart(), true);
1979
1980 auto Action = HandleHeaderIncludeOrImport(HashLoc, IncludeTok, FilenameTok,
1981 EndLoc, LookupFrom, LookupFromFile);
1982 switch (Action.Kind) {
1983 case ImportAction::None:
1984 case ImportAction::SkippedModuleImport:
1985 break;
1986 case ImportAction::ModuleBegin:
1987 EnterAnnotationToken(SourceRange(HashLoc, EndLoc),
1988 tok::annot_module_begin, Action.ModuleForHeader);
1989 break;
1990 case ImportAction::HeaderUnitImport:
1991 EnterAnnotationToken(SourceRange(HashLoc, EndLoc), tok::annot_header_unit,
1992 Action.ModuleForHeader);
1993 break;
1994 case ImportAction::ModuleImport:
1995 EnterAnnotationToken(SourceRange(HashLoc, EndLoc),
1996 tok::annot_module_include, Action.ModuleForHeader);
1997 break;
1998 case ImportAction::Failure:
1999 assert(TheModuleLoader.HadFatalFailure &&
2000 "This should be an early exit only to a fatal error");
2001 TheModuleLoader.HadFatalFailure = true;
2002 IncludeTok.setKind(tok::eof);
2003 CurLexer->cutOffLexing();
2004 return;
2005 }
2006}
2007
2008OptionalFileEntryRef Preprocessor::LookupHeaderIncludeOrImport(
2009 ConstSearchDirIterator *CurDir, StringRef &Filename,
2010 SourceLocation FilenameLoc, CharSourceRange FilenameRange,
2011 const Token &FilenameTok, bool &IsFrameworkFound, bool IsImportDecl,
2012 bool &IsMapped, ConstSearchDirIterator LookupFrom,
2013 const FileEntry *LookupFromFile, StringRef &LookupFilename,
2014 SmallVectorImpl<char> &RelativePath, SmallVectorImpl<char> &SearchPath,
2015 ModuleMap::KnownHeader &SuggestedModule, bool isAngled) {
2016 auto DiagnoseHeaderInclusion = [&](FileEntryRef FE) {
2017 if (LangOpts.AsmPreprocessor)
2018 return;
2019
2020 Module *RequestingModule = getModuleForLocation(
2021 FilenameLoc, LangOpts.ModulesValidateTextualHeaderIncludes);
2022 bool RequestingModuleIsModuleInterface =
2023 !SourceMgr.isInMainFile(FilenameLoc);
2024
2026 RequestingModule, RequestingModuleIsModuleInterface, FilenameLoc,
2027 Filename, FE);
2028 };
2029
2031 FilenameLoc, LookupFilename, isAngled, LookupFrom, LookupFromFile, CurDir,
2032 Callbacks ? &SearchPath : nullptr, Callbacks ? &RelativePath : nullptr,
2033 &SuggestedModule, &IsMapped, &IsFrameworkFound);
2034 if (File) {
2035 DiagnoseHeaderInclusion(*File);
2036 return File;
2037 }
2038
2039 // Give the clients a chance to silently skip this include.
2040 if (Callbacks && Callbacks->FileNotFound(Filename))
2041 return std::nullopt;
2042
2043 if (SuppressIncludeNotFoundError)
2044 return std::nullopt;
2045
2046 // If the file could not be located and it was included via angle
2047 // brackets, we can attempt a lookup as though it were a quoted path to
2048 // provide the user with a possible fixit.
2049 if (isAngled) {
2051 FilenameLoc, LookupFilename, false, LookupFrom, LookupFromFile, CurDir,
2052 Callbacks ? &SearchPath : nullptr, Callbacks ? &RelativePath : nullptr,
2053 &SuggestedModule, &IsMapped,
2054 /*IsFrameworkFound=*/nullptr);
2055 if (File) {
2056 DiagnoseHeaderInclusion(*File);
2057 Diag(FilenameTok, diag::err_pp_file_not_found_angled_include_not_fatal)
2058 << Filename << IsImportDecl
2059 << FixItHint::CreateReplacement(FilenameRange,
2060 "\"" + Filename.str() + "\"");
2061 return File;
2062 }
2063 }
2064
2065 // Check for likely typos due to leading or trailing non-isAlphanumeric
2066 // characters
2067 StringRef OriginalFilename = Filename;
2068 if (LangOpts.SpellChecking) {
2069 // A heuristic to correct a typo file name by removing leading and
2070 // trailing non-isAlphanumeric characters.
2071 auto CorrectTypoFilename = [](llvm::StringRef Filename) {
2072 Filename = Filename.drop_until(isAlphanumeric);
2073 while (!Filename.empty() && !isAlphanumeric(Filename.back())) {
2074 Filename = Filename.drop_back();
2075 }
2076 return Filename;
2077 };
2078 StringRef TypoCorrectionName = CorrectTypoFilename(Filename);
2079 StringRef TypoCorrectionLookupName = CorrectTypoFilename(LookupFilename);
2080
2082 FilenameLoc, TypoCorrectionLookupName, isAngled, LookupFrom,
2083 LookupFromFile, CurDir, Callbacks ? &SearchPath : nullptr,
2084 Callbacks ? &RelativePath : nullptr, &SuggestedModule, &IsMapped,
2085 /*IsFrameworkFound=*/nullptr);
2086 if (File) {
2087 DiagnoseHeaderInclusion(*File);
2088 auto Hint =
2090 FilenameRange, "<" + TypoCorrectionName.str() + ">")
2091 : FixItHint::CreateReplacement(
2092 FilenameRange, "\"" + TypoCorrectionName.str() + "\"");
2093 Diag(FilenameTok, diag::err_pp_file_not_found_typo_not_fatal)
2094 << OriginalFilename << TypoCorrectionName << Hint;
2095 // We found the file, so set the Filename to the name after typo
2096 // correction.
2097 Filename = TypoCorrectionName;
2098 LookupFilename = TypoCorrectionLookupName;
2099 return File;
2100 }
2101 }
2102
2103 // If the file is still not found, just go with the vanilla diagnostic
2104 assert(!File && "expected missing file");
2105 Diag(FilenameTok, diag::err_pp_file_not_found)
2106 << OriginalFilename << FilenameRange;
2107 if (IsFrameworkFound) {
2108 size_t SlashPos = OriginalFilename.find('/');
2109 assert(SlashPos != StringRef::npos &&
2110 "Include with framework name should have '/' in the filename");
2111 StringRef FrameworkName = OriginalFilename.substr(0, SlashPos);
2112 FrameworkCacheEntry &CacheEntry =
2113 HeaderInfo.LookupFrameworkCache(FrameworkName);
2114 assert(CacheEntry.Directory && "Found framework should be in cache");
2115 Diag(FilenameTok, diag::note_pp_framework_without_header)
2116 << OriginalFilename.substr(SlashPos + 1) << FrameworkName
2117 << CacheEntry.Directory->getName();
2118 }
2119
2120 return std::nullopt;
2121}
2122
2123/// Handle either a #include-like directive or an import declaration that names
2124/// a header file.
2125///
2126/// \param HashLoc The location of the '#' token for an include, or
2127/// SourceLocation() for an import declaration.
2128/// \param IncludeTok The include / include_next / import token.
2129/// \param FilenameTok The header-name token.
2130/// \param EndLoc The location at which any imported macros become visible.
2131/// \param LookupFrom For #include_next, the starting directory for the
2132/// directory lookup.
2133/// \param LookupFromFile For #include_next, the starting file for the directory
2134/// lookup.
2135Preprocessor::ImportAction Preprocessor::HandleHeaderIncludeOrImport(
2136 SourceLocation HashLoc, Token &IncludeTok, Token &FilenameTok,
2137 SourceLocation EndLoc, ConstSearchDirIterator LookupFrom,
2138 const FileEntry *LookupFromFile) {
2139 SmallString<128> FilenameBuffer;
2140 StringRef Filename = getSpelling(FilenameTok, FilenameBuffer);
2141 SourceLocation CharEnd = FilenameTok.getEndLoc();
2142
2143 CharSourceRange FilenameRange
2144 = CharSourceRange::getCharRange(FilenameTok.getLocation(), CharEnd);
2145 StringRef OriginalFilename = Filename;
2146 bool isAngled =
2148
2149 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
2150 // error.
2151 if (Filename.empty())
2152 return {ImportAction::None};
2153
2154 bool IsImportDecl = HashLoc.isInvalid();
2155 SourceLocation StartLoc = IsImportDecl ? IncludeTok.getLocation() : HashLoc;
2156
2157 // Complain about attempts to #include files in an audit pragma.
2158 if (PragmaARCCFCodeAuditedInfo.second.isValid()) {
2159 Diag(StartLoc, diag::err_pp_include_in_arc_cf_code_audited) << IsImportDecl;
2160 Diag(PragmaARCCFCodeAuditedInfo.second, diag::note_pragma_entered_here);
2161
2162 // Immediately leave the pragma.
2163 PragmaARCCFCodeAuditedInfo = {nullptr, SourceLocation()};
2164 }
2165
2166 // Complain about attempts to #include files in an assume-nonnull pragma.
2167 if (PragmaAssumeNonNullLoc.isValid()) {
2168 Diag(StartLoc, diag::err_pp_include_in_assume_nonnull) << IsImportDecl;
2169 Diag(PragmaAssumeNonNullLoc, diag::note_pragma_entered_here);
2170
2171 // Immediately leave the pragma.
2172 PragmaAssumeNonNullLoc = SourceLocation();
2173 }
2174
2175 if (HeaderInfo.HasIncludeAliasMap()) {
2176 // Map the filename with the brackets still attached. If the name doesn't
2177 // map to anything, fall back on the filename we've already gotten the
2178 // spelling for.
2179 StringRef NewName = HeaderInfo.MapHeaderToIncludeAlias(OriginalFilename);
2180 if (!NewName.empty())
2181 Filename = NewName;
2182 }
2183
2184 // Search include directories.
2185 bool IsMapped = false;
2186 bool IsFrameworkFound = false;
2187 ConstSearchDirIterator CurDir = nullptr;
2188 SmallString<1024> SearchPath;
2189 SmallString<1024> RelativePath;
2190 // We get the raw path only if we have 'Callbacks' to which we later pass
2191 // the path.
2192 ModuleMap::KnownHeader SuggestedModule;
2193 SourceLocation FilenameLoc = FilenameTok.getLocation();
2194 StringRef LookupFilename = Filename;
2195
2196 // Normalize slashes when compiling with -fms-extensions on non-Windows. This
2197 // is unnecessary on Windows since the filesystem there handles backslashes.
2198 SmallString<128> NormalizedPath;
2199 llvm::sys::path::Style BackslashStyle = llvm::sys::path::Style::native;
2200 if (is_style_posix(BackslashStyle) && LangOpts.MicrosoftExt) {
2201 NormalizedPath = Filename.str();
2202 llvm::sys::path::native(NormalizedPath);
2203 LookupFilename = NormalizedPath;
2204 BackslashStyle = llvm::sys::path::Style::windows;
2205 }
2206
2207 OptionalFileEntryRef File = LookupHeaderIncludeOrImport(
2208 &CurDir, Filename, FilenameLoc, FilenameRange, FilenameTok,
2209 IsFrameworkFound, IsImportDecl, IsMapped, LookupFrom, LookupFromFile,
2210 LookupFilename, RelativePath, SearchPath, SuggestedModule, isAngled);
2211
2212 if (usingPCHWithThroughHeader() && SkippingUntilPCHThroughHeader) {
2213 if (File && isPCHThroughHeader(&File->getFileEntry()))
2214 SkippingUntilPCHThroughHeader = false;
2215 return {ImportAction::None};
2216 }
2217
2218 // Should we enter the source file? Set to Skip if either the source file is
2219 // known to have no effect beyond its effect on module visibility -- that is,
2220 // if it's got an include guard that is already defined, set to Import if it
2221 // is a modular header we've already built and should import.
2222
2223 // For C++20 Modules
2224 // [cpp.include]/7 If the header identified by the header-name denotes an
2225 // importable header, it is implementation-defined whether the #include
2226 // preprocessing directive is instead replaced by an import directive.
2227 // For this implementation, the translation is permitted when we are parsing
2228 // the Global Module Fragment, and not otherwise (the cases where it would be
2229 // valid to replace an include with an import are highly constrained once in
2230 // named module purview; this choice avoids considerable complexity in
2231 // determining valid cases).
2232
2233 enum { Enter, Import, Skip, IncludeLimitReached } Action = Enter;
2234
2235 if (PPOpts->SingleFileParseMode)
2236 Action = IncludeLimitReached;
2237
2238 // If we've reached the max allowed include depth, it is usually due to an
2239 // include cycle. Don't enter already processed files again as it can lead to
2240 // reaching the max allowed include depth again.
2241 if (Action == Enter && HasReachedMaxIncludeDepth && File &&
2243 Action = IncludeLimitReached;
2244
2245 // FIXME: We do not have a good way to disambiguate C++ clang modules from
2246 // C++ standard modules (other than use/non-use of Header Units).
2247 Module *SM = SuggestedModule.getModule();
2248
2249 bool MaybeTranslateInclude =
2250 Action == Enter && File && SM && !SM->isForBuilding(getLangOpts());
2251
2252 // Maybe a usable Header Unit
2253 bool UsableHeaderUnit = false;
2254 if (getLangOpts().CPlusPlusModules && SM && SM->isHeaderUnit()) {
2255 if (TrackGMFState.inGMF() || IsImportDecl)
2256 UsableHeaderUnit = true;
2257 else if (!IsImportDecl) {
2258 // This is a Header Unit that we do not include-translate
2259 SuggestedModule = ModuleMap::KnownHeader();
2260 SM = nullptr;
2261 }
2262 }
2263 // Maybe a usable clang header module.
2264 bool UsableClangHeaderModule =
2265 (getLangOpts().CPlusPlusModules || getLangOpts().Modules) && SM &&
2266 !SM->isHeaderUnit();
2267
2268 // Determine whether we should try to import the module for this #include, if
2269 // there is one. Don't do so if precompiled module support is disabled or we
2270 // are processing this module textually (because we're building the module).
2271 if (MaybeTranslateInclude && (UsableHeaderUnit || UsableClangHeaderModule)) {
2272 // If this include corresponds to a module but that module is
2273 // unavailable, diagnose the situation and bail out.
2274 // FIXME: Remove this; loadModule does the same check (but produces
2275 // slightly worse diagnostics).
2277 *SuggestedModule.getModule(),
2278 getDiagnostics())) {
2279 Diag(FilenameTok.getLocation(),
2280 diag::note_implicit_top_level_module_import_here)
2281 << SuggestedModule.getModule()->getTopLevelModuleName();
2282 return {ImportAction::None};
2283 }
2284
2285 // Compute the module access path corresponding to this module.
2286 // FIXME: Should we have a second loadModule() overload to avoid this
2287 // extra lookup step?
2289 for (Module *Mod = SM; Mod; Mod = Mod->Parent)
2290 Path.push_back(std::make_pair(getIdentifierInfo(Mod->Name),
2291 FilenameTok.getLocation()));
2292 std::reverse(Path.begin(), Path.end());
2293
2294 // Warn that we're replacing the include/import with a module import.
2295 if (!IsImportDecl)
2296 diagnoseAutoModuleImport(*this, StartLoc, IncludeTok, Path, CharEnd);
2297
2298 // Load the module to import its macros. We'll make the declarations
2299 // visible when the parser gets here.
2300 // FIXME: Pass SuggestedModule in here rather than converting it to a path
2301 // and making the module loader convert it back again.
2302 ModuleLoadResult Imported = TheModuleLoader.loadModule(
2303 IncludeTok.getLocation(), Path, Module::Hidden,
2304 /*IsInclusionDirective=*/true);
2305 assert((Imported == nullptr || Imported == SuggestedModule.getModule()) &&
2306 "the imported module is different than the suggested one");
2307
2308 if (Imported) {
2309 Action = Import;
2310 } else if (Imported.isMissingExpected()) {
2312 static_cast<Module *>(Imported)->getTopLevelModule());
2313 // We failed to find a submodule that we assumed would exist (because it
2314 // was in the directory of an umbrella header, for instance), but no
2315 // actual module containing it exists (because the umbrella header is
2316 // incomplete). Treat this as a textual inclusion.
2317 SuggestedModule = ModuleMap::KnownHeader();
2318 SM = nullptr;
2319 } else if (Imported.isConfigMismatch()) {
2320 // On a configuration mismatch, enter the header textually. We still know
2321 // that it's part of the corresponding module.
2322 } else {
2323 // We hit an error processing the import. Bail out.
2325 // With a fatal failure in the module loader, we abort parsing.
2326 Token &Result = IncludeTok;
2327 assert(CurLexer && "#include but no current lexer set!");
2328 Result.startToken();
2329 CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd, tok::eof);
2330 CurLexer->cutOffLexing();
2331 }
2332 return {ImportAction::None};
2333 }
2334 }
2335
2336 // The #included file will be considered to be a system header if either it is
2337 // in a system include directory, or if the #includer is a system include
2338 // header.
2339 SrcMgr::CharacteristicKind FileCharacter =
2340 SourceMgr.getFileCharacteristic(FilenameTok.getLocation());
2341 if (File)
2342 FileCharacter = std::max(HeaderInfo.getFileDirFlavor(*File), FileCharacter);
2343
2344 // If this is a '#import' or an import-declaration, don't re-enter the file.
2345 //
2346 // FIXME: If we have a suggested module for a '#include', and we've already
2347 // visited this file, don't bother entering it again. We know it has no
2348 // further effect.
2349 bool EnterOnce =
2350 IsImportDecl ||
2351 IncludeTok.getIdentifierInfo()->getPPKeywordID() == tok::pp_import;
2352
2353 bool IsFirstIncludeOfFile = false;
2354
2355 // Ask HeaderInfo if we should enter this #include file. If not, #including
2356 // this file will have no effect.
2357 if (Action == Enter && File &&
2358 !HeaderInfo.ShouldEnterIncludeFile(*this, *File, EnterOnce,
2359 getLangOpts().Modules, SM,
2360 IsFirstIncludeOfFile)) {
2361 // C++ standard modules:
2362 // If we are not in the GMF, then we textually include only
2363 // clang modules:
2364 // Even if we've already preprocessed this header once and know that we
2365 // don't need to see its contents again, we still need to import it if it's
2366 // modular because we might not have imported it from this submodule before.
2367 //
2368 // FIXME: We don't do this when compiling a PCH because the AST
2369 // serialization layer can't cope with it. This means we get local
2370 // submodule visibility semantics wrong in that case.
2371 if (UsableHeaderUnit && !getLangOpts().CompilingPCH)
2372 Action = TrackGMFState.inGMF() ? Import : Skip;
2373 else
2374 Action = (SuggestedModule && !getLangOpts().CompilingPCH) ? Import : Skip;
2375 }
2376
2377 // Check for circular inclusion of the main file.
2378 // We can't generate a consistent preamble with regard to the conditional
2379 // stack if the main file is included again as due to the preamble bounds
2380 // some directives (e.g. #endif of a header guard) will never be seen.
2381 // Since this will lead to confusing errors, avoid the inclusion.
2382 if (Action == Enter && File && PreambleConditionalStack.isRecording() &&
2383 SourceMgr.isMainFile(File->getFileEntry())) {
2384 Diag(FilenameTok.getLocation(),
2385 diag::err_pp_including_mainfile_in_preamble);
2386 return {ImportAction::None};
2387 }
2388
2389 if (Callbacks && !IsImportDecl) {
2390 // Notify the callback object that we've seen an inclusion directive.
2391 // FIXME: Use a different callback for a pp-import?
2392 Callbacks->InclusionDirective(HashLoc, IncludeTok, LookupFilename, isAngled,
2393 FilenameRange, File, SearchPath, RelativePath,
2394 Action == Import ? SuggestedModule.getModule()
2395 : nullptr,
2396 FileCharacter);
2397 if (Action == Skip && File)
2398 Callbacks->FileSkipped(*File, FilenameTok, FileCharacter);
2399 }
2400
2401 if (!File)
2402 return {ImportAction::None};
2403
2404 // If this is a C++20 pp-import declaration, diagnose if we didn't find any
2405 // module corresponding to the named header.
2406 if (IsImportDecl && !SuggestedModule) {
2407 Diag(FilenameTok, diag::err_header_import_not_header_unit)
2408 << OriginalFilename << File->getName();
2409 return {ImportAction::None};
2410 }
2411
2412 // Issue a diagnostic if the name of the file on disk has a different case
2413 // than the one we're about to open.
2414 const bool CheckIncludePathPortability =
2415 !IsMapped && !File->getFileEntry().tryGetRealPathName().empty();
2416
2417 if (CheckIncludePathPortability) {
2418 StringRef Name = LookupFilename;
2419 StringRef NameWithoriginalSlashes = Filename;
2420#if defined(_WIN32)
2421 // Skip UNC prefix if present. (tryGetRealPathName() always
2422 // returns a path with the prefix skipped.)
2423 bool NameWasUNC = Name.consume_front("\\\\?\\");
2424 NameWithoriginalSlashes.consume_front("\\\\?\\");
2425#endif
2426 StringRef RealPathName = File->getFileEntry().tryGetRealPathName();
2427 SmallVector<StringRef, 16> Components(llvm::sys::path::begin(Name),
2428 llvm::sys::path::end(Name));
2429#if defined(_WIN32)
2430 // -Wnonportable-include-path is designed to diagnose includes using
2431 // case even on systems with a case-insensitive file system.
2432 // On Windows, RealPathName always starts with an upper-case drive
2433 // letter for absolute paths, but Name might start with either
2434 // case depending on if `cd c:\foo` or `cd C:\foo` was used in the shell.
2435 // ("foo" will always have on-disk case, no matter which case was
2436 // used in the cd command). To not emit this warning solely for
2437 // the drive letter, whose case is dependent on if `cd` is used
2438 // with upper- or lower-case drive letters, always consider the
2439 // given drive letter case as correct for the purpose of this warning.
2440 SmallString<128> FixedDriveRealPath;
2441 if (llvm::sys::path::is_absolute(Name) &&
2442 llvm::sys::path::is_absolute(RealPathName) &&
2443 toLowercase(Name[0]) == toLowercase(RealPathName[0]) &&
2444 isLowercase(Name[0]) != isLowercase(RealPathName[0])) {
2445 assert(Components.size() >= 3 && "should have drive, backslash, name");
2446 assert(Components[0].size() == 2 && "should start with drive");
2447 assert(Components[0][1] == ':' && "should have colon");
2448 FixedDriveRealPath = (Name.substr(0, 1) + RealPathName.substr(1)).str();
2449 RealPathName = FixedDriveRealPath;
2450 }
2451#endif
2452
2453 if (trySimplifyPath(Components, RealPathName)) {
2454 SmallString<128> Path;
2455 Path.reserve(Name.size()+2);
2456 Path.push_back(isAngled ? '<' : '"');
2457
2458 const auto IsSep = [BackslashStyle](char c) {
2459 return llvm::sys::path::is_separator(c, BackslashStyle);
2460 };
2461
2462 for (auto Component : Components) {
2463 // On POSIX, Components will contain a single '/' as first element
2464 // exactly if Name is an absolute path.
2465 // On Windows, it will contain "C:" followed by '\' for absolute paths.
2466 // The drive letter is optional for absolute paths on Windows, but
2467 // clang currently cannot process absolute paths in #include lines that
2468 // don't have a drive.
2469 // If the first entry in Components is a directory separator,
2470 // then the code at the bottom of this loop that keeps the original
2471 // directory separator style copies it. If the second entry is
2472 // a directory separator (the C:\ case), then that separator already
2473 // got copied when the C: was processed and we want to skip that entry.
2474 if (!(Component.size() == 1 && IsSep(Component[0])))
2475 Path.append(Component);
2476 else if (!Path.empty())
2477 continue;
2478
2479 // Append the separator(s) the user used, or the close quote
2480 if (Path.size() > NameWithoriginalSlashes.size()) {
2481 Path.push_back(isAngled ? '>' : '"');
2482 continue;
2483 }
2484 assert(IsSep(NameWithoriginalSlashes[Path.size()-1]));
2485 do
2486 Path.push_back(NameWithoriginalSlashes[Path.size()-1]);
2487 while (Path.size() <= NameWithoriginalSlashes.size() &&
2488 IsSep(NameWithoriginalSlashes[Path.size()-1]));
2489 }
2490
2491#if defined(_WIN32)
2492 // Restore UNC prefix if it was there.
2493 if (NameWasUNC)
2494 Path = (Path.substr(0, 1) + "\\\\?\\" + Path.substr(1)).str();
2495#endif
2496
2497 // For user files and known standard headers, issue a diagnostic.
2498 // For other system headers, don't. They can be controlled separately.
2499 auto DiagId =
2500 (FileCharacter == SrcMgr::C_User || warnByDefaultOnWrongCase(Name))
2501 ? diag::pp_nonportable_path
2502 : diag::pp_nonportable_system_path;
2503 Diag(FilenameTok, DiagId) << Path <<
2504 FixItHint::CreateReplacement(FilenameRange, Path);
2505 }
2506 }
2507
2508 switch (Action) {
2509 case Skip:
2510 // If we don't need to enter the file, stop now.
2511 if (SM)
2512 return {ImportAction::SkippedModuleImport, SM};
2513 return {ImportAction::None};
2514
2515 case IncludeLimitReached:
2516 // If we reached our include limit and don't want to enter any more files,
2517 // don't go any further.
2518 return {ImportAction::None};
2519
2520 case Import: {
2521 // If this is a module import, make it visible if needed.
2522 assert(SM && "no module to import");
2523
2524 makeModuleVisible(SM, EndLoc);
2525
2526 if (IncludeTok.getIdentifierInfo()->getPPKeywordID() ==
2527 tok::pp___include_macros)
2528 return {ImportAction::None};
2529
2530 return {ImportAction::ModuleImport, SM};
2531 }
2532
2533 case Enter:
2534 break;
2535 }
2536
2537 // Check that we don't have infinite #include recursion.
2538 if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1) {
2539 Diag(FilenameTok, diag::err_pp_include_too_deep);
2540 HasReachedMaxIncludeDepth = true;
2541 return {ImportAction::None};
2542 }
2543
2544 if (isAngled && isInNamedModule())
2545 Diag(FilenameTok, diag::warn_pp_include_angled_in_module_purview)
2546 << getNamedModuleName();
2547
2548 // Look up the file, create a File ID for it.
2549 SourceLocation IncludePos = FilenameTok.getLocation();
2550 // If the filename string was the result of macro expansions, set the include
2551 // position on the file where it will be included and after the expansions.
2552 if (IncludePos.isMacroID())
2553 IncludePos = SourceMgr.getExpansionRange(IncludePos).getEnd();
2554 FileID FID = SourceMgr.createFileID(*File, IncludePos, FileCharacter);
2555 if (!FID.isValid()) {
2556 TheModuleLoader.HadFatalFailure = true;
2557 return ImportAction::Failure;
2558 }
2559
2560 // If all is good, enter the new file!
2561 if (EnterSourceFile(FID, CurDir, FilenameTok.getLocation(),
2562 IsFirstIncludeOfFile))
2563 return {ImportAction::None};
2564
2565 // Determine if we're switching to building a new submodule, and which one.
2566 // This does not apply for C++20 modules header units.
2567 if (SM && !SM->isHeaderUnit()) {
2568 if (SM->getTopLevelModule()->ShadowingModule) {
2569 // We are building a submodule that belongs to a shadowed module. This
2570 // means we find header files in the shadowed module.
2571 Diag(SM->DefinitionLoc, diag::err_module_build_shadowed_submodule)
2572 << SM->getFullModuleName();
2573 Diag(SM->getTopLevelModule()->ShadowingModule->DefinitionLoc,
2574 diag::note_previous_definition);
2575 return {ImportAction::None};
2576 }
2577 // When building a pch, -fmodule-name tells the compiler to textually
2578 // include headers in the specified module. We are not building the
2579 // specified module.
2580 //
2581 // FIXME: This is the wrong way to handle this. We should produce a PCH
2582 // that behaves the same as the header would behave in a compilation using
2583 // that PCH, which means we should enter the submodule. We need to teach
2584 // the AST serialization layer to deal with the resulting AST.
2585 if (getLangOpts().CompilingPCH && SM->isForBuilding(getLangOpts()))
2586 return {ImportAction::None};
2587
2588 assert(!CurLexerSubmodule && "should not have marked this as a module yet");
2589 CurLexerSubmodule = SM;
2590
2591 // Let the macro handling code know that any future macros are within
2592 // the new submodule.
2593 EnterSubmodule(SM, EndLoc, /*ForPragma*/ false);
2594
2595 // Let the parser know that any future declarations are within the new
2596 // submodule.
2597 // FIXME: There's no point doing this if we're handling a #__include_macros
2598 // directive.
2599 return {ImportAction::ModuleBegin, SM};
2600 }
2601
2602 assert(!IsImportDecl && "failed to diagnose missing module for import decl");
2603 return {ImportAction::None};
2604}
2605
2606/// HandleIncludeNextDirective - Implements \#include_next.
2607///
2608void Preprocessor::HandleIncludeNextDirective(SourceLocation HashLoc,
2609 Token &IncludeNextTok) {
2610 Diag(IncludeNextTok, diag::ext_pp_include_next_directive);
2611
2612 ConstSearchDirIterator Lookup = nullptr;
2613 const FileEntry *LookupFromFile;
2614 std::tie(Lookup, LookupFromFile) = getIncludeNextStart(IncludeNextTok);
2615
2616 return HandleIncludeDirective(HashLoc, IncludeNextTok, Lookup,
2617 LookupFromFile);
2618}
2619
2620/// HandleMicrosoftImportDirective - Implements \#import for Microsoft Mode
2621void Preprocessor::HandleMicrosoftImportDirective(Token &Tok) {
2622 // The Microsoft #import directive takes a type library and generates header
2623 // files from it, and includes those. This is beyond the scope of what clang
2624 // does, so we ignore it and error out. However, #import can optionally have
2625 // trailing attributes that span multiple lines. We're going to eat those
2626 // so we can continue processing from there.
2627 Diag(Tok, diag::err_pp_import_directive_ms );
2628
2629 // Read tokens until we get to the end of the directive. Note that the
2630 // directive can be split over multiple lines using the backslash character.
2632}
2633
2634/// HandleImportDirective - Implements \#import.
2635///
2636void Preprocessor::HandleImportDirective(SourceLocation HashLoc,
2637 Token &ImportTok) {
2638 if (!LangOpts.ObjC) { // #import is standard for ObjC.
2639 if (LangOpts.MSVCCompat)
2640 return HandleMicrosoftImportDirective(ImportTok);
2641 Diag(ImportTok, diag::ext_pp_import_directive);
2642 }
2643 return HandleIncludeDirective(HashLoc, ImportTok);
2644}
2645
2646/// HandleIncludeMacrosDirective - The -imacros command line option turns into a
2647/// pseudo directive in the predefines buffer. This handles it by sucking all
2648/// tokens through the preprocessor and discarding them (only keeping the side
2649/// effects on the preprocessor).
2650void Preprocessor::HandleIncludeMacrosDirective(SourceLocation HashLoc,
2651 Token &IncludeMacrosTok) {
2652 // This directive should only occur in the predefines buffer. If not, emit an
2653 // error and reject it.
2654 SourceLocation Loc = IncludeMacrosTok.getLocation();
2655 if (SourceMgr.getBufferName(Loc) != "<built-in>") {
2656 Diag(IncludeMacrosTok.getLocation(),
2657 diag::pp_include_macros_out_of_predefines);
2659 return;
2660 }
2661
2662 // Treat this as a normal #include for checking purposes. If this is
2663 // successful, it will push a new lexer onto the include stack.
2664 HandleIncludeDirective(HashLoc, IncludeMacrosTok);
2665
2666 Token TmpTok;
2667 do {
2668 Lex(TmpTok);
2669 assert(TmpTok.isNot(tok::eof) && "Didn't find end of -imacros!");
2670 } while (TmpTok.isNot(tok::hashhash));
2671}
2672
2673//===----------------------------------------------------------------------===//
2674// Preprocessor Macro Directive Handling.
2675//===----------------------------------------------------------------------===//
2676
2677/// ReadMacroParameterList - The ( starting a parameter list of a macro
2678/// definition has just been read. Lex the rest of the parameters and the
2679/// closing ), updating MI with what we learn. Return true if an error occurs
2680/// parsing the param list.
2681bool Preprocessor::ReadMacroParameterList(MacroInfo *MI, Token &Tok) {
2683
2684 while (true) {
2686 switch (Tok.getKind()) {
2687 case tok::r_paren:
2688 // Found the end of the parameter list.
2689 if (Parameters.empty()) // #define FOO()
2690 return false;
2691 // Otherwise we have #define FOO(A,)
2692 Diag(Tok, diag::err_pp_expected_ident_in_arg_list);
2693 return true;
2694 case tok::ellipsis: // #define X(... -> C99 varargs
2695 if (!LangOpts.C99)
2696 Diag(Tok, LangOpts.CPlusPlus11 ?
2697 diag::warn_cxx98_compat_variadic_macro :
2698 diag::ext_variadic_macro);
2699
2700 // OpenCL v1.2 s6.9.e: variadic macros are not supported.
2701 if (LangOpts.OpenCL && !LangOpts.OpenCLCPlusPlus) {
2702 Diag(Tok, diag::ext_pp_opencl_variadic_macros);
2703 }
2704
2705 // Lex the token after the identifier.
2707 if (Tok.isNot(tok::r_paren)) {
2708 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
2709 return true;
2710 }
2711 // Add the __VA_ARGS__ identifier as a parameter.
2712 Parameters.push_back(Ident__VA_ARGS__);
2713 MI->setIsC99Varargs();
2714 MI->setParameterList(Parameters, BP);
2715 return false;
2716 case tok::eod: // #define X(
2717 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
2718 return true;
2719 default:
2720 // Handle keywords and identifiers here to accept things like
2721 // #define Foo(for) for.
2723 if (!II) {
2724 // #define X(1
2725 Diag(Tok, diag::err_pp_invalid_tok_in_arg_list);
2726 return true;
2727 }
2728
2729 // If this is already used as a parameter, it is used multiple times (e.g.
2730 // #define X(A,A.
2731 if (llvm::is_contained(Parameters, II)) { // C99 6.10.3p6
2732 Diag(Tok, diag::err_pp_duplicate_name_in_arg_list) << II;
2733 return true;
2734 }
2735
2736 // Add the parameter to the macro info.
2737 Parameters.push_back(II);
2738
2739 // Lex the token after the identifier.
2741
2742 switch (Tok.getKind()) {
2743 default: // #define X(A B
2744 Diag(Tok, diag::err_pp_expected_comma_in_arg_list);
2745 return true;
2746 case tok::r_paren: // #define X(A)
2747 MI->setParameterList(Parameters, BP);
2748 return false;
2749 case tok::comma: // #define X(A,
2750 break;
2751 case tok::ellipsis: // #define X(A... -> GCC extension
2752 // Diagnose extension.
2753 Diag(Tok, diag::ext_named_variadic_macro);
2754
2755 // Lex the token after the identifier.
2757 if (Tok.isNot(tok::r_paren)) {
2758 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
2759 return true;
2760 }
2761
2762 MI->setIsGNUVarargs();
2763 MI->setParameterList(Parameters, BP);
2764 return false;
2765 }
2766 }
2767 }
2768}
2769
2770static bool isConfigurationPattern(Token &MacroName, MacroInfo *MI,
2771 const LangOptions &LOptions) {
2772 if (MI->getNumTokens() == 1) {
2773 const Token &Value = MI->getReplacementToken(0);
2774
2775 // Macro that is identity, like '#define inline inline' is a valid pattern.
2776 if (MacroName.getKind() == Value.getKind())
2777 return true;
2778
2779 // Macro that maps a keyword to the same keyword decorated with leading/
2780 // trailing underscores is a valid pattern:
2781 // #define inline __inline
2782 // #define inline __inline__
2783 // #define inline _inline (in MS compatibility mode)
2784 StringRef MacroText = MacroName.getIdentifierInfo()->getName();
2785 if (IdentifierInfo *II = Value.getIdentifierInfo()) {
2786 if (!II->isKeyword(LOptions))
2787 return false;
2788 StringRef ValueText = II->getName();
2789 StringRef TrimmedValue = ValueText;
2790 if (!ValueText.startswith("__")) {
2791 if (ValueText.startswith("_"))
2792 TrimmedValue = TrimmedValue.drop_front(1);
2793 else
2794 return false;
2795 } else {
2796 TrimmedValue = TrimmedValue.drop_front(2);
2797 if (TrimmedValue.endswith("__"))
2798 TrimmedValue = TrimmedValue.drop_back(2);
2799 }
2800 return TrimmedValue.equals(MacroText);
2801 } else {
2802 return false;
2803 }
2804 }
2805
2806 // #define inline
2807 return MacroName.isOneOf(tok::kw_extern, tok::kw_inline, tok::kw_static,
2808 tok::kw_const) &&
2809 MI->getNumTokens() == 0;
2810}
2811
2812// ReadOptionalMacroParameterListAndBody - This consumes all (i.e. the
2813// entire line) of the macro's tokens and adds them to MacroInfo, and while
2814// doing so performs certain validity checks including (but not limited to):
2815// - # (stringization) is followed by a macro parameter
2816//
2817// Returns a nullptr if an invalid sequence of tokens is encountered or returns
2818// a pointer to a MacroInfo object.
2819
2820MacroInfo *Preprocessor::ReadOptionalMacroParameterListAndBody(
2821 const Token &MacroNameTok, const bool ImmediatelyAfterHeaderGuard) {
2822
2823 Token LastTok = MacroNameTok;
2824 // Create the new macro.
2825 MacroInfo *const MI = AllocateMacroInfo(MacroNameTok.getLocation());
2826
2827 Token Tok;
2828 LexUnexpandedToken(Tok);
2829
2830 // Ensure we consume the rest of the macro body if errors occur.
2831 auto _ = llvm::make_scope_exit([&]() {
2832 // The flag indicates if we are still waiting for 'eod'.
2833 if (CurLexer->ParsingPreprocessorDirective)
2835 });
2836
2837 // Used to un-poison and then re-poison identifiers of the __VA_ARGS__ ilk
2838 // within their appropriate context.
2840
2841 // If this is a function-like macro definition, parse the argument list,
2842 // marking each of the identifiers as being used as macro arguments. Also,
2843 // check other constraints on the first token of the macro body.
2844 if (Tok.is(tok::eod)) {
2845 if (ImmediatelyAfterHeaderGuard) {
2846 // Save this macro information since it may part of a header guard.
2847 CurPPLexer->MIOpt.SetDefinedMacro(MacroNameTok.getIdentifierInfo(),
2848 MacroNameTok.getLocation());
2849 }
2850 // If there is no body to this macro, we have no special handling here.
2851 } else if (Tok.hasLeadingSpace()) {
2852 // This is a normal token with leading space. Clear the leading space
2853 // marker on the first token to get proper expansion.
2855 } else if (Tok.is(tok::l_paren)) {
2856 // This is a function-like macro definition. Read the argument list.
2857 MI->setIsFunctionLike();
2858 if (ReadMacroParameterList(MI, LastTok))
2859 return nullptr;
2860
2861 // If this is a definition of an ISO C/C++ variadic function-like macro (not
2862 // using the GNU named varargs extension) inform our variadic scope guard
2863 // which un-poisons and re-poisons certain identifiers (e.g. __VA_ARGS__)
2864 // allowed only within the definition of a variadic macro.
2865
2866 if (MI->isC99Varargs()) {
2868 }
2869
2870 // Read the first token after the arg list for down below.
2871 LexUnexpandedToken(Tok);
2872 } else if (LangOpts.C99 || LangOpts.CPlusPlus11) {
2873 // C99 requires whitespace between the macro definition and the body. Emit
2874 // a diagnostic for something like "#define X+".
2875 Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name);
2876 } else {
2877 // C90 6.8 TC1 says: "In the definition of an object-like macro, if the
2878 // first character of a replacement list is not a character required by
2879 // subclause 5.2.1, then there shall be white-space separation between the
2880 // identifier and the replacement list.". 5.2.1 lists this set:
2881 // "A-Za-z0-9!"#%&'()*+,_./:;<=>?[\]^_{|}~" as well as whitespace, which
2882 // is irrelevant here.
2883 bool isInvalid = false;
2884 if (Tok.is(tok::at)) // @ is not in the list above.
2885 isInvalid = true;
2886 else if (Tok.is(tok::unknown)) {
2887 // If we have an unknown token, it is something strange like "`". Since
2888 // all of valid characters would have lexed into a single character
2889 // token of some sort, we know this is not a valid case.
2890 isInvalid = true;
2891 }
2892 if (isInvalid)
2893 Diag(Tok, diag::ext_missing_whitespace_after_macro_name);
2894 else
2895 Diag(Tok, diag::warn_missing_whitespace_after_macro_name);
2896 }
2897
2898 if (!Tok.is(tok::eod))
2899 LastTok = Tok;
2900
2902
2903 // Read the rest of the macro body.
2904 if (MI->isObjectLike()) {
2905 // Object-like macros are very simple, just read their body.
2906 while (Tok.isNot(tok::eod)) {
2907 LastTok = Tok;
2908 Tokens.push_back(Tok);
2909 // Get the next token of the macro.
2910 LexUnexpandedToken(Tok);
2911 }
2912 } else {
2913 // Otherwise, read the body of a function-like macro. While we are at it,
2914 // check C99 6.10.3.2p1: ensure that # operators are followed by macro
2915 // parameters in function-like macro expansions.
2916
2917 VAOptDefinitionContext VAOCtx(*this);
2918
2919 while (Tok.isNot(tok::eod)) {
2920 LastTok = Tok;
2921
2922 if (!Tok.isOneOf(tok::hash, tok::hashat, tok::hashhash)) {
2923 Tokens.push_back(Tok);
2924
2925 if (VAOCtx.isVAOptToken(Tok)) {
2926 // If we're already within a VAOPT, emit an error.
2927 if (VAOCtx.isInVAOpt()) {
2928 Diag(Tok, diag::err_pp_vaopt_nested_use);
2929 return nullptr;
2930 }
2931 // Ensure VAOPT is followed by a '(' .
2932 LexUnexpandedToken(Tok);
2933 if (Tok.isNot(tok::l_paren)) {
2934 Diag(Tok, diag::err_pp_missing_lparen_in_vaopt_use);
2935 return nullptr;
2936 }
2937 Tokens.push_back(Tok);
2938 VAOCtx.sawVAOptFollowedByOpeningParens(Tok.getLocation());
2939 LexUnexpandedToken(Tok);
2940 if (Tok.is(tok::hashhash)) {
2941 Diag(Tok, diag::err_vaopt_paste_at_start);
2942 return nullptr;
2943 }
2944 continue;
2945 } else if (VAOCtx.isInVAOpt()) {
2946 if (Tok.is(tok::r_paren)) {
2947 if (VAOCtx.sawClosingParen()) {
2948 assert(Tokens.size() >= 3 &&
2949 "Must have seen at least __VA_OPT__( "
2950 "and a subsequent tok::r_paren");
2951 if (Tokens[Tokens.size() - 2].is(tok::hashhash)) {
2952 Diag(Tok, diag::err_vaopt_paste_at_end);
2953 return nullptr;
2954 }
2955 }
2956 } else if (Tok.is(tok::l_paren)) {
2957 VAOCtx.sawOpeningParen(Tok.getLocation());
2958 }
2959 }
2960 // Get the next token of the macro.
2961 LexUnexpandedToken(Tok);
2962 continue;
2963 }
2964
2965 // If we're in -traditional mode, then we should ignore stringification
2966 // and token pasting. Mark the tokens as unknown so as not to confuse
2967 // things.
2968 if (getLangOpts().TraditionalCPP) {
2969 Tok.setKind(tok::unknown);
2970 Tokens.push_back(Tok);
2971
2972 // Get the next token of the macro.
2973 LexUnexpandedToken(Tok);
2974 continue;
2975 }
2976
2977 if (Tok.is(tok::hashhash)) {
2978 // If we see token pasting, check if it looks like the gcc comma
2979 // pasting extension. We'll use this information to suppress
2980 // diagnostics later on.
2981
2982 // Get the next token of the macro.
2983 LexUnexpandedToken(Tok);
2984
2985 if (Tok.is(tok::eod)) {
2986 Tokens.push_back(LastTok);
2987 break;
2988 }
2989
2990 if (!Tokens.empty() && Tok.getIdentifierInfo() == Ident__VA_ARGS__ &&
2991 Tokens[Tokens.size() - 1].is(tok::comma))
2992 MI->setHasCommaPasting();
2993
2994 // Things look ok, add the '##' token to the macro.
2995 Tokens.push_back(LastTok);
2996 continue;
2997 }
2998
2999 // Our Token is a stringization operator.
3000 // Get the next token of the macro.
3001 LexUnexpandedToken(Tok);
3002
3003 // Check for a valid macro arg identifier or __VA_OPT__.
3004 if (!VAOCtx.isVAOptToken(Tok) &&
3005 (Tok.getIdentifierInfo() == nullptr ||
3006 MI->getParameterNum(Tok.getIdentifierInfo()) == -1)) {
3007
3008 // If this is assembler-with-cpp mode, we accept random gibberish after
3009 // the '#' because '#' is often a comment character. However, change
3010 // the kind of the token to tok::unknown so that the preprocessor isn't
3011 // confused.
3012 if (getLangOpts().AsmPreprocessor && Tok.isNot(tok::eod)) {
3013 LastTok.setKind(tok::unknown);
3014 Tokens.push_back(LastTok);
3015 continue;
3016 } else {
3017 Diag(Tok, diag::err_pp_stringize_not_parameter)
3018 << LastTok.is(tok::hashat);
3019 return nullptr;
3020 }
3021 }
3022
3023 // Things look ok, add the '#' and param name tokens to the macro.
3024 Tokens.push_back(LastTok);
3025
3026 // If the token following '#' is VAOPT, let the next iteration handle it
3027 // and check it for correctness, otherwise add the token and prime the
3028 // loop with the next one.
3029 if (!VAOCtx.isVAOptToken(Tok)) {
3030 Tokens.push_back(Tok);
3031 LastTok = Tok;
3032
3033 // Get the next token of the macro.
3034 LexUnexpandedToken(Tok);
3035 }
3036 }
3037 if (VAOCtx.isInVAOpt()) {
3038 assert(Tok.is(tok::eod) && "Must be at End Of preprocessing Directive");
3039 Diag(Tok, diag::err_pp_expected_after)
3040 << LastTok.getKind() << tok::r_paren;
3041 Diag(VAOCtx.getUnmatchedOpeningParenLoc(), diag::note_matching) << tok::l_paren;
3042 return nullptr;
3043 }
3044 }
3045 MI->setDefinitionEndLoc(LastTok.getLocation());
3046
3047 MI->setTokens(Tokens, BP);
3048 return MI;
3049}
3050
3051static bool isObjCProtectedMacro(const IdentifierInfo *II) {
3052 return II->isStr("__strong") || II->isStr("__weak") ||
3053 II->isStr("__unsafe_unretained") || II->isStr("__autoreleasing");
3054}
3055
3056/// HandleDefineDirective - Implements \#define. This consumes the entire macro
3057/// line then lets the caller lex the next real token.
3058void Preprocessor::HandleDefineDirective(
3059 Token &DefineTok, const bool ImmediatelyAfterHeaderGuard) {
3060 ++NumDefined;
3061
3062 Token MacroNameTok;
3063 bool MacroShadowsKeyword;
3064 ReadMacroName(MacroNameTok, MU_Define, &MacroShadowsKeyword);
3065
3066 // Error reading macro name? If so, diagnostic already issued.
3067 if (MacroNameTok.is(tok::eod))
3068 return;
3069
3070 IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
3071 // Issue a final pragma warning if we're defining a macro that was has been
3072 // undefined and is being redefined.
3073 if (!II->hasMacroDefinition() && II->hadMacroDefinition() && II->isFinal())
3074 emitFinalMacroWarning(MacroNameTok, /*IsUndef=*/false);
3075
3076 // If we are supposed to keep comments in #defines, reenable comment saving
3077 // mode.
3078 if (CurLexer) CurLexer->SetCommentRetentionState(KeepMacroComments);
3079
3080 MacroInfo *const MI = ReadOptionalMacroParameterListAndBody(
3081 MacroNameTok, ImmediatelyAfterHeaderGuard);
3082
3083 if (!MI) return;
3084
3085 if (MacroShadowsKeyword &&
3086 !isConfigurationPattern(MacroNameTok, MI, getLangOpts())) {
3087 Diag(MacroNameTok, diag::warn_pp_macro_hides_keyword);
3088 }
3089 // Check that there is no paste (##) operator at the beginning or end of the
3090 // replacement list.
3091 unsigned NumTokens = MI->getNumTokens();
3092 if (NumTokens != 0) {
3093 if (MI->getReplacementToken(0).is(tok::hashhash)) {
3094 Diag(MI->getReplacementToken(0), diag::err_paste_at_start);
3095 return;
3096 }
3097 if (MI->getReplacementToken(NumTokens-1).is(tok::hashhash)) {
3098 Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end);
3099 return;
3100 }
3101 }
3102
3103 // When skipping just warn about macros that do not match.
3104 if (SkippingUntilPCHThroughHeader) {
3105 const MacroInfo *OtherMI = getMacroInfo(MacroNameTok.getIdentifierInfo());
3106 if (!OtherMI || !MI->isIdenticalTo(*OtherMI, *this,
3107 /*Syntactic=*/LangOpts.MicrosoftExt))
3108 Diag(MI->getDefinitionLoc(), diag::warn_pp_macro_def_mismatch_with_pch)
3109 << MacroNameTok.getIdentifierInfo();
3110 // Issue the diagnostic but allow the change if msvc extensions are enabled
3111 if (!LangOpts.MicrosoftExt)
3112 return;
3113 }
3114
3115 // Finally, if this identifier already had a macro defined for it, verify that
3116 // the macro bodies are identical, and issue diagnostics if they are not.
3117 if (const MacroInfo *OtherMI=getMacroInfo(MacroNameTok.getIdentifierInfo())) {
3118 // Final macros are hard-mode: they always warn. Even if the bodies are
3119 // identical. Even if they are in system headers. Even if they are things we
3120 // would silently allow in the past.
3121 if (MacroNameTok.getIdentifierInfo()->isFinal())
3122 emitFinalMacroWarning(MacroNameTok, /*IsUndef=*/false);
3123
3124 // In Objective-C, ignore attempts to directly redefine the builtin
3125 // definitions of the ownership qualifiers. It's still possible to
3126 // #undef them.
3127 if (getLangOpts().ObjC &&
3128 SourceMgr.getFileID(OtherMI->getDefinitionLoc()) ==
3130 isObjCProtectedMacro(MacroNameTok.getIdentifierInfo())) {
3131 // Warn if it changes the tokens.
3132 if ((!getDiagnostics().getSuppressSystemWarnings() ||
3133 !SourceMgr.isInSystemHeader(DefineTok.getLocation())) &&
3134 !MI->isIdenticalTo(*OtherMI, *this,
3135 /*Syntactic=*/LangOpts.MicrosoftExt)) {
3136 Diag(MI->getDefinitionLoc(), diag::warn_pp_objc_macro_redef_ignored);
3137 }
3138 assert(!OtherMI->isWarnIfUnused());
3139 return;
3140 }
3141
3142 // It is very common for system headers to have tons of macro redefinitions
3143 // and for warnings to be disabled in system headers. If this is the case,
3144 // then don't bother calling MacroInfo::isIdenticalTo.
3145 if (!getDiagnostics().getSuppressSystemWarnings() ||
3146 !SourceMgr.isInSystemHeader(DefineTok.getLocation())) {
3147
3148 if (!OtherMI->isUsed() && OtherMI->isWarnIfUnused())
3149 Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used);
3150
3151 // Warn if defining "__LINE__" and other builtins, per C99 6.10.8/4 and
3152 // C++ [cpp.predefined]p4, but allow it as an extension.
3153 if (isLanguageDefinedBuiltin(SourceMgr, OtherMI, II->getName()))
3154 Diag(MacroNameTok, diag::ext_pp_redef_builtin_macro);
3155 // Macros must be identical. This means all tokens and whitespace
3156 // separation must be the same. C99 6.10.3p2.
3157 else if (!OtherMI->isAllowRedefinitionsWithoutWarning() &&
3158 !MI->isIdenticalTo(*OtherMI, *this, /*Syntactic=*/LangOpts.MicrosoftExt)) {
3159 Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef)
3160 << MacroNameTok.getIdentifierInfo();
3161 Diag(OtherMI->getDefinitionLoc(), diag::note_previous_definition);
3162 }
3163 }
3164 if (OtherMI->isWarnIfUnused())
3165 WarnUnusedMacroLocs.erase(OtherMI->getDefinitionLoc());
3166 }
3167
3168 DefMacroDirective *MD =
3169 appendDefMacroDirective(MacroNameTok.getIdentifierInfo(), MI);
3170
3171 assert(!MI->isUsed());
3172 // If we need warning for not using the macro, add its location in the
3173 // warn-because-unused-macro set. If it gets used it will be removed from set.
3175 !Diags->isIgnored(diag::pp_macro_not_used, MI->getDefinitionLoc()) &&
3176 !MacroExpansionInDirectivesOverride &&
3177 getSourceManager().getFileID(MI->getDefinitionLoc()) !=
3179 MI->setIsWarnIfUnused(true);
3180 WarnUnusedMacroLocs.insert(MI->getDefinitionLoc());
3181 }
3182
3183 // If the callbacks want to know, tell them about the macro definition.
3184 if (Callbacks)
3185 Callbacks->MacroDefined(MacroNameTok, MD);
3186
3187 // If we're in MS compatibility mode and the macro being defined is the
3188 // assert macro, implicitly add a macro definition for static_assert to work
3189 // around their broken assert.h header file in C. Only do so if there isn't
3190 // already a static_assert macro defined.
3191 if (!getLangOpts().CPlusPlus && getLangOpts().MSVCCompat &&
3192 MacroNameTok.getIdentifierInfo()->isStr("assert") &&
3193 !isMacroDefined("static_assert")) {
3195
3196 Token Tok;
3197 Tok.startToken();
3198 Tok.setKind(tok::kw__Static_assert);
3199 Tok.setIdentifierInfo(getIdentifierInfo("_Static_assert"));
3200 MI->setTokens({Tok}, BP);
3201 (void)appendDefMacroDirective(getIdentifierInfo("static_assert"), MI);
3202 }
3203}
3204
3205/// HandleUndefDirective - Implements \#undef.
3206///
3207void Preprocessor::HandleUndefDirective() {
3208 ++NumUndefined;
3209
3210 Token MacroNameTok;
3211 ReadMacroName(MacroNameTok, MU_Undef);
3212
3213 // Error reading macro name? If so, diagnostic already issued.
3214 if (MacroNameTok.is(tok::eod))
3215 return;
3216
3217 // Check to see if this is the last token on the #undef line.
3218 CheckEndOfDirective("undef");
3219
3220 // Okay, we have a valid identifier to undef.
3221 auto *II = MacroNameTok.getIdentifierInfo();
3222 auto MD = getMacroDefinition(II);
3223 UndefMacroDirective *Undef = nullptr;
3224
3225 if (II->isFinal())
3226 emitFinalMacroWarning(MacroNameTok, /*IsUndef=*/true);
3227
3228 // If the macro is not defined, this is a noop undef.
3229 if (const MacroInfo *MI = MD.getMacroInfo()) {
3230 if (!MI->isUsed() && MI->isWarnIfUnused())
3231 Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used);
3232
3233 // Warn if undefining "__LINE__" and other builtins, per C99 6.10.8/4 and
3234 // C++ [cpp.predefined]p4, but allow it as an extension.
3235 if (isLanguageDefinedBuiltin(SourceMgr, MI, II->getName()))
3236 Diag(MacroNameTok, diag::ext_pp_undef_builtin_macro);
3237
3238 if (MI->isWarnIfUnused())
3239 WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());
3240
3241 Undef = AllocateUndefMacroDirective(MacroNameTok.getLocation());
3242 }
3243
3244 // If the callbacks want to know, tell them about the macro #undef.
3245 // Note: no matter if the macro was defined or not.
3246 if (Callbacks)
3247 Callbacks->MacroUndefined(MacroNameTok, MD, Undef);
3248
3249 if (Undef)
3250 appendMacroDirective(II, Undef);
3251}
3252
3253//===----------------------------------------------------------------------===//
3254// Preprocessor Conditional Directive Handling.
3255//===----------------------------------------------------------------------===//
3256
3257/// HandleIfdefDirective - Implements the \#ifdef/\#ifndef directive. isIfndef
3258/// is true when this is a \#ifndef directive. ReadAnyTokensBeforeDirective is
3259/// true if any tokens have been returned or pp-directives activated before this
3260/// \#ifndef has been lexed.
3261///
3262void Preprocessor::HandleIfdefDirective(Token &Result,
3263 const Token &HashToken,
3264 bool isIfndef,
3265 bool ReadAnyTokensBeforeDirective) {
3266 ++NumIf;
3267 Token DirectiveTok = Result;
3268
3269 Token MacroNameTok;
3270 ReadMacroName(MacroNameTok);
3271
3272 // Error reading macro name? If so, diagnostic already issued.
3273 if (MacroNameTok.is(tok::eod)) {
3274 // Skip code until we get to #endif. This helps with recovery by not
3275 // emitting an error when the #endif is reached.
3276 SkipExcludedConditionalBlock(HashToken.getLocation(),
3277 DirectiveTok.getLocation(),
3278 /*Foundnonskip*/ false, /*FoundElse*/ false);
3279 return;
3280 }
3281
3282 emitMacroExpansionWarnings(MacroNameTok);
3283
3284 // Check to see if this is the last token on the #if[n]def line.
3285 CheckEndOfDirective(isIfndef ? "ifndef" : "ifdef");
3286
3287 IdentifierInfo *MII = MacroNameTok.getIdentifierInfo();
3288 auto MD = getMacroDefinition(MII);
3289 MacroInfo *MI = MD.getMacroInfo();
3290
3291 if (CurPPLexer->getConditionalStackDepth() == 0) {
3292 // If the start of a top-level #ifdef and if the macro is not defined,
3293 // inform MIOpt that this might be the start of a proper include guard.
3294 // Otherwise it is some other form of unknown conditional which we can't
3295 // handle.
3296 if (!ReadAnyTokensBeforeDirective && !MI) {
3297 assert(isIfndef && "#ifdef shouldn't reach here");
3298 CurPPLexer->MIOpt.EnterTopLevelIfndef(MII, MacroNameTok.getLocation());
3299 } else
3300 CurPPLexer->MIOpt.EnterTopLevelConditional();
3301 }
3302
3303 // If there is a macro, process it.
3304 if (MI) // Mark it used.
3305 markMacroAsUsed(MI);
3306
3307 if (Callbacks) {
3308 if (isIfndef)
3309 Callbacks->Ifndef(DirectiveTok.getLocation(), MacroNameTok, MD);
3310 else
3311 Callbacks->Ifdef(DirectiveTok.getLocation(), MacroNameTok, MD);
3312 }
3313
3314 bool RetainExcludedCB = PPOpts->RetainExcludedConditionalBlocks &&
3315 getSourceManager().isInMainFile(DirectiveTok.getLocation());
3316
3317 // Should we include the stuff contained by this directive?
3318 if (PPOpts->SingleFileParseMode && !MI) {
3319 // In 'single-file-parse mode' undefined identifiers trigger parsing of all
3320 // the directive blocks.
3321 CurPPLexer->pushConditionalLevel(DirectiveTok.getLocation(),
3322 /*wasskip*/false, /*foundnonskip*/false,
3323 /*foundelse*/false);
3324 } else if (!MI == isIfndef || RetainExcludedCB) {
3325 // Yes, remember that we are inside a conditional, then lex the next token.
3326 CurPPLexer->pushConditionalLevel(DirectiveTok.getLocation(),
3327 /*wasskip*/false, /*foundnonskip*/true,
3328 /*foundelse*/false);
3329 } else {
3330 // No, skip the contents of this block.
3331 SkipExcludedConditionalBlock(HashToken.getLocation(),
3332 DirectiveTok.getLocation(),
3333 /*Foundnonskip*/ false,
3334 /*FoundElse*/ false);
3335 }
3336}
3337
3338/// HandleIfDirective - Implements the \#if directive.
3339///
3340void Preprocessor::HandleIfDirective(Token &IfToken,
3341 const Token &HashToken,
3342 bool ReadAnyTokensBeforeDirective) {
3343 ++NumIf;
3344
3345 // Parse and evaluate the conditional expression.
3346 IdentifierInfo *IfNDefMacro = nullptr;
3347 const DirectiveEvalResult DER = EvaluateDirectiveExpression(IfNDefMacro);
3348 const bool ConditionalTrue = DER.Conditional;
3349 // Lexer might become invalid if we hit code completion point while evaluating
3350 // expression.
3351 if (!CurPPLexer)
3352 return;
3353
3354 // If this condition is equivalent to #ifndef X, and if this is the first
3355 // directive seen, handle it for the multiple-include optimization.
3356 if (CurPPLexer->getConditionalStackDepth() == 0) {
3357 if (!ReadAnyTokensBeforeDirective && IfNDefMacro && ConditionalTrue)
3358 // FIXME: Pass in the location of the macro name, not the 'if' token.
3359 CurPPLexer->MIOpt.EnterTopLevelIfndef(IfNDefMacro, IfToken.getLocation());
3360 else
3361 CurPPLexer->MIOpt.EnterTopLevelConditional();
3362 }
3363
3364 if (Callbacks)
3365 Callbacks->If(
3366 IfToken.getLocation(), DER.ExprRange,
3367 (ConditionalTrue ? PPCallbacks::CVK_True : PPCallbacks::CVK_False));
3368
3369 bool RetainExcludedCB = PPOpts->RetainExcludedConditionalBlocks &&
3371
3372 // Should we include the stuff contained by this directive?
3373 if (PPOpts->SingleFileParseMode && DER.IncludedUndefinedIds) {
3374 // In 'single-file-parse mode' undefined identifiers trigger parsing of all
3375 // the directive blocks.
3376 CurPPLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
3377 /*foundnonskip*/false, /*foundelse*/false);
3378 } else if (ConditionalTrue || RetainExcludedCB) {
3379 // Yes, remember that we are inside a conditional, then lex the next token.
3380 CurPPLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
3381 /*foundnonskip*/true, /*foundelse*/false);
3382 } else {
3383 // No, skip the contents of this block.
3384 SkipExcludedConditionalBlock(HashToken.getLocation(), IfToken.getLocation(),
3385 /*Foundnonskip*/ false,
3386 /*FoundElse*/ false);
3387 }
3388}
3389
3390/// HandleEndifDirective - Implements the \#endif directive.
3391///
3392void Preprocessor::HandleEndifDirective(Token &EndifToken) {
3393 ++NumEndif;
3394
3395 // Check that this is the whole directive.
3396 CheckEndOfDirective("endif");
3397
3398 PPConditionalInfo CondInfo;
3399 if (CurPPLexer->popConditionalLevel(CondInfo)) {
3400 // No conditionals on the stack: this is an #endif without an #if.
3401 Diag(EndifToken, diag::err_pp_endif_without_if);
3402 return;
3403 }
3404
3405 // If this the end of a top-level #endif, inform MIOpt.
3406 if (CurPPLexer->getConditionalStackDepth() == 0)
3407 CurPPLexer->MIOpt.ExitTopLevelConditional();
3408
3409 assert(!CondInfo.WasSkipping && !CurPPLexer->LexingRawMode &&
3410 "This code should only be reachable in the non-skipping case!");
3411
3412 if (Callbacks)
3413 Callbacks->Endif(EndifToken.getLocation(), CondInfo.IfLoc);
3414}
3415
3416/// HandleElseDirective - Implements the \#else directive.
3417///
3418void Preprocessor::HandleElseDirective(Token &Result, const Token &HashToken) {
3419 ++NumElse;
3420
3421 // #else directive in a non-skipping conditional... start skipping.
3422 CheckEndOfDirective("else");
3423
3425 if (CurPPLexer->popConditionalLevel(CI)) {
3426 Diag(Result, diag::pp_err_else_without_if);
3427 return;
3428 }
3429
3430 // If this is a top-level #else, inform the MIOpt.
3431 if (CurPPLexer->getConditionalStackDepth() == 0)
3432 CurPPLexer->MIOpt.EnterTopLevelConditional();
3433
3434 // If this is a #else with a #else before it, report the error.
3435 if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else);
3436
3437 if (Callbacks)
3438 Callbacks->Else(Result.getLocation(), CI.IfLoc);
3439
3440 bool RetainExcludedCB = PPOpts->RetainExcludedConditionalBlocks &&
3441 getSourceManager().isInMainFile(Result.getLocation());
3442
3443 if ((PPOpts->SingleFileParseMode && !CI.FoundNonSkip) || RetainExcludedCB) {
3444 // In 'single-file-parse mode' undefined identifiers trigger parsing of all
3445 // the directive blocks.
3446 CurPPLexer->pushConditionalLevel(CI.IfLoc, /*wasskip*/false,
3447 /*foundnonskip*/false, /*foundelse*/true);
3448 return;
3449 }
3450
3451 // Finally, skip the rest of the contents of this block.
3452 SkipExcludedConditionalBlock(HashToken.getLocation(), CI.IfLoc,
3453 /*Foundnonskip*/ true,
3454 /*FoundElse*/ true, Result.getLocation());
3455}
3456
3457/// Implements the \#elif, \#elifdef, and \#elifndef directives.
3458void Preprocessor::HandleElifFamilyDirective(Token &ElifToken,
3459 const Token &HashToken,
3460 tok::PPKeywordKind Kind) {
3461 PPElifDiag DirKind = Kind == tok::pp_elif ? PED_Elif
3462 : Kind == tok::pp_elifdef ? PED_Elifdef
3463 : PED_Elifndef;
3464 ++NumElse;
3465
3466 // Warn if using `#elifdef` & `#elifndef` in not C23 & C++23 mode.
3467 switch (DirKind) {
3468 case PED_Elifdef:
3469 case PED_Elifndef:
3470 unsigned DiagID;
3471 if (LangOpts.CPlusPlus)
3472 DiagID = LangOpts.CPlusPlus23 ? diag::warn_cxx23_compat_pp_directive
3473 : diag::ext_cxx23_pp_directive;
3474 else
3475 DiagID = LangOpts.C23 ? diag::warn_c23_compat_pp_directive
3476 : diag::ext_c23_pp_directive;
3477 Diag(ElifToken, DiagID) << DirKind;
3478 break;
3479 default:
3480 break;
3481 }
3482
3483 // #elif directive in a non-skipping conditional... start skipping.
3484 // We don't care what the condition is, because we will always skip it (since
3485 // the block immediately before it was included).
3486 SourceRange ConditionRange = DiscardUntilEndOfDirective();
3487
3489 if (CurPPLexer->popConditionalLevel(CI)) {
3490 Diag(ElifToken, diag::pp_err_elif_without_if) << DirKind;
3491 return;
3492 }
3493
3494 // If this is a top-level #elif, inform the MIOpt.
3495 if (CurPPLexer->getConditionalStackDepth() == 0)
3496 CurPPLexer->MIOpt.EnterTopLevelConditional();
3497
3498 // If this is a #elif with a #else before it, report the error.
3499 if (CI.FoundElse)
3500 Diag(ElifToken, diag::pp_err_elif_after_else) << DirKind;
3501
3502 if (Callbacks) {
3503 switch (Kind) {
3504 case tok::pp_elif:
3505 Callbacks->Elif(ElifToken.getLocation(), ConditionRange,
3507 break;
3508 case tok::pp_elifdef:
3509 Callbacks->Elifdef(ElifToken.getLocation(), ConditionRange, CI.IfLoc);
3510 break;
3511 case tok::pp_elifndef:
3512 Callbacks->Elifndef(ElifToken.getLocation(), ConditionRange, CI.IfLoc);
3513 break;
3514 default:
3515 assert(false && "unexpected directive kind");
3516 break;
3517 }
3518 }
3519
3520 bool RetainExcludedCB = PPOpts->RetainExcludedConditionalBlocks &&
3522
3523 if ((PPOpts->SingleFileParseMode && !CI.FoundNonSkip) || RetainExcludedCB) {
3524 // In 'single-file-parse mode' undefined identifiers trigger parsing of all
3525 // the directive blocks.
3526 CurPPLexer->pushConditionalLevel(ElifToken.getLocation(), /*wasskip*/false,
3527 /*foundnonskip*/false, /*foundelse*/false);
3528 return;
3529 }
3530
3531 // Finally, skip the rest of the contents of this block.
3532 SkipExcludedConditionalBlock(
3533 HashToken.getLocation(), CI.IfLoc, /*Foundnonskip*/ true,
3534 /*FoundElse*/ CI.FoundElse, ElifToken.getLocation());
3535}
static bool isInMainFile(const clang::Diagnostic &D)
Definition: ASTUnit.cpp:711
#define SM(sm)
Definition: Cuda.cpp:80
Defines interfaces for clang::DirectoryEntry and clang::DirectoryEntryRef.
Defines the clang::FileManager interface and associated types.
StringRef Text
Definition: Format.cpp:2977
StringRef Filename
Definition: Format.cpp:2976
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 bool trySimplifyPath(SmallVectorImpl< StringRef > &Components, StringRef RealPathName)
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 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.
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:366
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:169
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:531
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:516
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:805
bool HasIncludeAliasMap() const
Checks whether the map exists or not.
Definition: HeaderSearch.h:391
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:407
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:83
bool IsHeaderFile
Indicates whether the front-end is explicitly told that the input is a header file (i....
Definition: LangOptions.h:485
std::string CurrentModule
The name of the current module, of which the main source file is a part.
Definition: LangOptions.h:448
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
OptionalDirectoryEntryRefDegradesToDirectoryEntryPtr getBuiltinDir() const
Get the directory that contains Clang-supplied include files.
Definition: ModuleMap.h:418
@ 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:670
@ Hidden
All of the names in this module are hidden.
Definition: Module.h:388
SourceLocation DefinitionLoc
The location of the module definition.
Definition: Module.h:111
unsigned IsSystem
Whether this is a "system" module (which assumes that all headers in it are system headers).
Definition: Module.h:332
std::string Name
The name of this module.
Definition: Module.h:108
std::pair< std::string, bool > Requirement
An individual requirement: a feature name and a flag indicating the required state of that feature.
Definition: Module.h:289
bool isAvailable() const
Determine whether this module is available for use within the current translation unit.
Definition: Module.h:528
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:243
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...
OptionalFileEntryRefDegradesToFileEntryPtr getFileEntry() const
getFileEntry - Return the FileEntry corresponding to this FileID.
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...
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.
void emitMacroExpansionWarnings(const Token &Identifier) const
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
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
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:212
Token - This structure provides full information about a lexed token.
Definition: Token.h:35
IdentifierInfo * getIdentifierInfo() const
Definition: Token.h:186
SourceLocation getEndLoc() const
Definition: Token.h:158
void setAnnotationEndLoc(SourceLocation L)
Definition: Token.h:149
void clearFlag(TokenFlags Flag)
Unset the specified flag.
Definition: Token.h:253
SourceLocation getLocation() const
Return a source location identifier for the specified offset in the current file.
Definition: Token.h:131
unsigned getLength() const
Definition: Token.h:134
void setKind(tok::TokenKind K)
Definition: Token.h:94
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:98
tok::TokenKind getKind() const
Definition: Token.h:93
bool isAtStartOfLine() const
isAtStartOfLine - Return true if this token is at the start of a line.
Definition: Token.h:275
@ LeadingSpace
Definition: Token.h:76
bool hasLeadingSpace() const
Return true if this token has whitespace before it.
Definition: Token.h:279
void setLocation(SourceLocation L)
Definition: Token.h:139
bool isOneOf(tok::TokenKind K1, tok::TokenKind K2) const
Definition: Token.h:100
bool isNot(tok::TokenKind K) const
Definition: Token.h:99
void setAnnotationValue(void *val)
Definition: Token.h:237
bool hasUDSuffix() const
Return true if this token is a string or character literal which has a ud-suffix.
Definition: Token.h:300
void startToken()
Reset all flags to cleared.
Definition: Token.h:176
bool needsCleaning() const
Return true if this token has trigraphs or escaped newlines in it.
Definition: Token.h:292
void setIdentifierInfo(IdentifierInfo *II)
Definition: Token.h:195
StringRef getRawIdentifier() const
getRawIdentifier - For a raw identifier token (i.e., an identifier lexed in raw mode),...
Definition: Token.h:212
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:135
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:321
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
@ CPlusPlus
Definition: LangStandard.h:54
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:208
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:123
@ 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:99
LLVM_READONLY bool isLowercase(unsigned char c)
Return true if this character is a lowercase ASCII letter: [a-z].
Definition: CharInfo.h:105
@ 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:155
OptionalDirectoryEntryRef Directory
The directory entry which should be used for the cached framework.
Definition: HeaderSearch.h:157
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:322
bool FoundNonSkip
True if we have emitted tokens already, and now we're in an #else block or something.
Definition: Token.h:332
SourceLocation IfLoc
Location where the conditional started.
Definition: Token.h:324
bool WasSkipping
True if this was contained in a skipping directive, e.g., in a "\#if 0" block.
Definition: Token.h:328
bool FoundElse
True if we've seen a #else in this block.
Definition: Token.h:336