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