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