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