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