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