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