1 //===- Pragma.cpp - Pragma registration and handling ----------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements the PragmaHandler/PragmaTable interfaces and implements 10 // pragma related methods of the Preprocessor class. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "clang/Lex/Pragma.h" 15 #include "clang/Basic/Diagnostic.h" 16 #include "clang/Basic/FileManager.h" 17 #include "clang/Basic/IdentifierTable.h" 18 #include "clang/Basic/LLVM.h" 19 #include "clang/Basic/LangOptions.h" 20 #include "clang/Basic/Module.h" 21 #include "clang/Basic/SourceLocation.h" 22 #include "clang/Basic/SourceManager.h" 23 #include "clang/Basic/TokenKinds.h" 24 #include "clang/Lex/HeaderSearch.h" 25 #include "clang/Lex/LexDiagnostic.h" 26 #include "clang/Lex/Lexer.h" 27 #include "clang/Lex/LiteralSupport.h" 28 #include "clang/Lex/MacroInfo.h" 29 #include "clang/Lex/ModuleLoader.h" 30 #include "clang/Lex/PPCallbacks.h" 31 #include "clang/Lex/Preprocessor.h" 32 #include "clang/Lex/PreprocessorLexer.h" 33 #include "clang/Lex/PreprocessorOptions.h" 34 #include "clang/Lex/Token.h" 35 #include "clang/Lex/TokenLexer.h" 36 #include "llvm/ADT/ArrayRef.h" 37 #include "llvm/ADT/DenseMap.h" 38 #include "llvm/ADT/STLExtras.h" 39 #include "llvm/ADT/SmallString.h" 40 #include "llvm/ADT/SmallVector.h" 41 #include "llvm/ADT/StringSwitch.h" 42 #include "llvm/ADT/StringRef.h" 43 #include "llvm/Support/Compiler.h" 44 #include "llvm/Support/ErrorHandling.h" 45 #include "llvm/Support/Timer.h" 46 #include <algorithm> 47 #include <cassert> 48 #include <cstddef> 49 #include <cstdint> 50 #include <limits> 51 #include <string> 52 #include <utility> 53 #include <vector> 54 55 using namespace clang; 56 57 // Out-of-line destructor to provide a home for the class. 58 PragmaHandler::~PragmaHandler() = default; 59 60 //===----------------------------------------------------------------------===// 61 // EmptyPragmaHandler Implementation. 62 //===----------------------------------------------------------------------===// 63 64 EmptyPragmaHandler::EmptyPragmaHandler(StringRef Name) : PragmaHandler(Name) {} 65 66 void EmptyPragmaHandler::HandlePragma(Preprocessor &PP, 67 PragmaIntroducer Introducer, 68 Token &FirstToken) {} 69 70 //===----------------------------------------------------------------------===// 71 // PragmaNamespace Implementation. 72 //===----------------------------------------------------------------------===// 73 74 /// FindHandler - Check to see if there is already a handler for the 75 /// specified name. If not, return the handler for the null identifier if it 76 /// exists, otherwise return null. If IgnoreNull is true (the default) then 77 /// the null handler isn't returned on failure to match. 78 PragmaHandler *PragmaNamespace::FindHandler(StringRef Name, 79 bool IgnoreNull) const { 80 auto I = Handlers.find(Name); 81 if (I != Handlers.end()) 82 return I->getValue().get(); 83 if (IgnoreNull) 84 return nullptr; 85 I = Handlers.find(StringRef()); 86 if (I != Handlers.end()) 87 return I->getValue().get(); 88 return nullptr; 89 } 90 91 void PragmaNamespace::AddPragma(PragmaHandler *Handler) { 92 assert(!Handlers.count(Handler->getName()) && 93 "A handler with this name is already registered in this namespace"); 94 Handlers[Handler->getName()].reset(Handler); 95 } 96 97 void PragmaNamespace::RemovePragmaHandler(PragmaHandler *Handler) { 98 auto I = Handlers.find(Handler->getName()); 99 assert(I != Handlers.end() && 100 "Handler not registered in this namespace"); 101 // Release ownership back to the caller. 102 I->getValue().release(); 103 Handlers.erase(I); 104 } 105 106 void PragmaNamespace::HandlePragma(Preprocessor &PP, 107 PragmaIntroducer Introducer, Token &Tok) { 108 // Read the 'namespace' that the directive is in, e.g. STDC. Do not macro 109 // expand it, the user can have a STDC #define, that should not affect this. 110 PP.LexUnexpandedToken(Tok); 111 112 // Get the handler for this token. If there is no handler, ignore the pragma. 113 PragmaHandler *Handler 114 = FindHandler(Tok.getIdentifierInfo() ? Tok.getIdentifierInfo()->getName() 115 : StringRef(), 116 /*IgnoreNull=*/false); 117 if (!Handler) { 118 PP.Diag(Tok, diag::warn_pragma_ignored); 119 return; 120 } 121 122 // Otherwise, pass it down. 123 Handler->HandlePragma(PP, Introducer, Tok); 124 } 125 126 //===----------------------------------------------------------------------===// 127 // Preprocessor Pragma Directive Handling. 128 //===----------------------------------------------------------------------===// 129 130 namespace { 131 // TokenCollector provides the option to collect tokens that were "read" 132 // and return them to the stream to be read later. 133 // Currently used when reading _Pragma/__pragma directives. 134 struct TokenCollector { 135 Preprocessor &Self; 136 bool Collect; 137 SmallVector<Token, 3> Tokens; 138 Token &Tok; 139 140 void lex() { 141 if (Collect) 142 Tokens.push_back(Tok); 143 Self.Lex(Tok); 144 } 145 146 void revert() { 147 assert(Collect && "did not collect tokens"); 148 assert(!Tokens.empty() && "collected unexpected number of tokens"); 149 150 // Push the ( "string" ) tokens into the token stream. 151 auto Toks = std::make_unique<Token[]>(Tokens.size()); 152 std::copy(Tokens.begin() + 1, Tokens.end(), Toks.get()); 153 Toks[Tokens.size() - 1] = Tok; 154 Self.EnterTokenStream(std::move(Toks), Tokens.size(), 155 /*DisableMacroExpansion*/ true, 156 /*IsReinject*/ true); 157 158 // ... and return the pragma token unchanged. 159 Tok = *Tokens.begin(); 160 } 161 }; 162 } // namespace 163 164 /// HandlePragmaDirective - The "\#pragma" directive has been parsed. Lex the 165 /// rest of the pragma, passing it to the registered pragma handlers. 166 void Preprocessor::HandlePragmaDirective(PragmaIntroducer Introducer) { 167 if (Callbacks) 168 Callbacks->PragmaDirective(Introducer.Loc, Introducer.Kind); 169 170 if (!PragmasEnabled) 171 return; 172 173 ++NumPragma; 174 175 // Invoke the first level of pragma handlers which reads the namespace id. 176 Token Tok; 177 PragmaHandlers->HandlePragma(*this, Introducer, Tok); 178 179 // If the pragma handler didn't read the rest of the line, consume it now. 180 if ((CurTokenLexer && CurTokenLexer->isParsingPreprocessorDirective()) 181 || (CurPPLexer && CurPPLexer->ParsingPreprocessorDirective)) 182 DiscardUntilEndOfDirective(); 183 } 184 185 /// Handle_Pragma - Read a _Pragma directive, slice it up, process it, then 186 /// return the first token after the directive. The _Pragma token has just 187 /// been read into 'Tok'. 188 void Preprocessor::Handle_Pragma(Token &Tok) { 189 // C11 6.10.3.4/3: 190 // all pragma unary operator expressions within [a completely 191 // macro-replaced preprocessing token sequence] are [...] processed [after 192 // rescanning is complete] 193 // 194 // This means that we execute _Pragma operators in two cases: 195 // 196 // 1) on token sequences that would otherwise be produced as the output of 197 // phase 4 of preprocessing, and 198 // 2) on token sequences formed as the macro-replaced token sequence of a 199 // macro argument 200 // 201 // Case #2 appears to be a wording bug: only _Pragmas that would survive to 202 // the end of phase 4 should actually be executed. Discussion on the WG14 203 // mailing list suggests that a _Pragma operator is notionally checked early, 204 // but only pragmas that survive to the end of phase 4 should be executed. 205 // 206 // In Case #2, we check the syntax now, but then put the tokens back into the 207 // token stream for later consumption. 208 209 TokenCollector Toks = {*this, InMacroArgPreExpansion, {}, Tok}; 210 211 // Remember the pragma token location. 212 SourceLocation PragmaLoc = Tok.getLocation(); 213 214 // Read the '('. 215 Toks.lex(); 216 if (Tok.isNot(tok::l_paren)) { 217 Diag(PragmaLoc, diag::err__Pragma_malformed); 218 return; 219 } 220 221 // Read the '"..."'. 222 Toks.lex(); 223 if (!tok::isStringLiteral(Tok.getKind())) { 224 Diag(PragmaLoc, diag::err__Pragma_malformed); 225 // Skip bad tokens, and the ')', if present. 226 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::eof)) 227 Lex(Tok); 228 while (Tok.isNot(tok::r_paren) && 229 !Tok.isAtStartOfLine() && 230 Tok.isNot(tok::eof)) 231 Lex(Tok); 232 if (Tok.is(tok::r_paren)) 233 Lex(Tok); 234 return; 235 } 236 237 if (Tok.hasUDSuffix()) { 238 Diag(Tok, diag::err_invalid_string_udl); 239 // Skip this token, and the ')', if present. 240 Lex(Tok); 241 if (Tok.is(tok::r_paren)) 242 Lex(Tok); 243 return; 244 } 245 246 // Remember the string. 247 Token StrTok = Tok; 248 249 // Read the ')'. 250 Toks.lex(); 251 if (Tok.isNot(tok::r_paren)) { 252 Diag(PragmaLoc, diag::err__Pragma_malformed); 253 return; 254 } 255 256 // If we're expanding a macro argument, put the tokens back. 257 if (InMacroArgPreExpansion) { 258 Toks.revert(); 259 return; 260 } 261 262 SourceLocation RParenLoc = Tok.getLocation(); 263 std::string StrVal = getSpelling(StrTok); 264 265 // The _Pragma is lexically sound. Destringize according to C11 6.10.9.1: 266 // "The string literal is destringized by deleting any encoding prefix, 267 // deleting the leading and trailing double-quotes, replacing each escape 268 // sequence \" by a double-quote, and replacing each escape sequence \\ by a 269 // single backslash." 270 if (StrVal[0] == 'L' || StrVal[0] == 'U' || 271 (StrVal[0] == 'u' && StrVal[1] != '8')) 272 StrVal.erase(StrVal.begin()); 273 else if (StrVal[0] == 'u') 274 StrVal.erase(StrVal.begin(), StrVal.begin() + 2); 275 276 if (StrVal[0] == 'R') { 277 // FIXME: C++11 does not specify how to handle raw-string-literals here. 278 // We strip off the 'R', the quotes, the d-char-sequences, and the parens. 279 assert(StrVal[1] == '"' && StrVal[StrVal.size() - 1] == '"' && 280 "Invalid raw string token!"); 281 282 // Measure the length of the d-char-sequence. 283 unsigned NumDChars = 0; 284 while (StrVal[2 + NumDChars] != '(') { 285 assert(NumDChars < (StrVal.size() - 5) / 2 && 286 "Invalid raw string token!"); 287 ++NumDChars; 288 } 289 assert(StrVal[StrVal.size() - 2 - NumDChars] == ')'); 290 291 // Remove 'R " d-char-sequence' and 'd-char-sequence "'. We'll replace the 292 // parens below. 293 StrVal.erase(0, 2 + NumDChars); 294 StrVal.erase(StrVal.size() - 1 - NumDChars); 295 } else { 296 assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' && 297 "Invalid string token!"); 298 299 // Remove escaped quotes and escapes. 300 unsigned ResultPos = 1; 301 for (size_t i = 1, e = StrVal.size() - 1; i != e; ++i) { 302 // Skip escapes. \\ -> '\' and \" -> '"'. 303 if (StrVal[i] == '\\' && i + 1 < e && 304 (StrVal[i + 1] == '\\' || StrVal[i + 1] == '"')) 305 ++i; 306 StrVal[ResultPos++] = StrVal[i]; 307 } 308 StrVal.erase(StrVal.begin() + ResultPos, StrVal.end() - 1); 309 } 310 311 // Remove the front quote, replacing it with a space, so that the pragma 312 // contents appear to have a space before them. 313 StrVal[0] = ' '; 314 315 // Replace the terminating quote with a \n. 316 StrVal[StrVal.size()-1] = '\n'; 317 318 // Plop the string (including the newline and trailing null) into a buffer 319 // where we can lex it. 320 Token TmpTok; 321 TmpTok.startToken(); 322 CreateString(StrVal, TmpTok); 323 SourceLocation TokLoc = TmpTok.getLocation(); 324 325 // Make and enter a lexer object so that we lex and expand the tokens just 326 // like any others. 327 Lexer *TL = Lexer::Create_PragmaLexer(TokLoc, PragmaLoc, RParenLoc, 328 StrVal.size(), *this); 329 330 EnterSourceFileWithLexer(TL, nullptr); 331 332 // With everything set up, lex this as a #pragma directive. 333 HandlePragmaDirective({PIK__Pragma, PragmaLoc}); 334 335 // Finally, return whatever came after the pragma directive. 336 return Lex(Tok); 337 } 338 339 /// HandleMicrosoft__pragma - Like Handle_Pragma except the pragma text 340 /// is not enclosed within a string literal. 341 void Preprocessor::HandleMicrosoft__pragma(Token &Tok) { 342 // During macro pre-expansion, check the syntax now but put the tokens back 343 // into the token stream for later consumption. Same as Handle_Pragma. 344 TokenCollector Toks = {*this, InMacroArgPreExpansion, {}, Tok}; 345 346 // Remember the pragma token location. 347 SourceLocation PragmaLoc = Tok.getLocation(); 348 349 // Read the '('. 350 Toks.lex(); 351 if (Tok.isNot(tok::l_paren)) { 352 Diag(PragmaLoc, diag::err__Pragma_malformed); 353 return; 354 } 355 356 // Get the tokens enclosed within the __pragma(), as well as the final ')'. 357 SmallVector<Token, 32> PragmaToks; 358 int NumParens = 0; 359 Toks.lex(); 360 while (Tok.isNot(tok::eof)) { 361 PragmaToks.push_back(Tok); 362 if (Tok.is(tok::l_paren)) 363 NumParens++; 364 else if (Tok.is(tok::r_paren) && NumParens-- == 0) 365 break; 366 Toks.lex(); 367 } 368 369 if (Tok.is(tok::eof)) { 370 Diag(PragmaLoc, diag::err_unterminated___pragma); 371 return; 372 } 373 374 // If we're expanding a macro argument, put the tokens back. 375 if (InMacroArgPreExpansion) { 376 Toks.revert(); 377 return; 378 } 379 380 PragmaToks.front().setFlag(Token::LeadingSpace); 381 382 // Replace the ')' with an EOD to mark the end of the pragma. 383 PragmaToks.back().setKind(tok::eod); 384 385 Token *TokArray = new Token[PragmaToks.size()]; 386 std::copy(PragmaToks.begin(), PragmaToks.end(), TokArray); 387 388 // Push the tokens onto the stack. 389 EnterTokenStream(TokArray, PragmaToks.size(), true, true, 390 /*IsReinject*/ false); 391 392 // With everything set up, lex this as a #pragma directive. 393 HandlePragmaDirective({PIK___pragma, PragmaLoc}); 394 395 // Finally, return whatever came after the pragma directive. 396 return Lex(Tok); 397 } 398 399 /// HandlePragmaOnce - Handle \#pragma once. OnceTok is the 'once'. 400 void Preprocessor::HandlePragmaOnce(Token &OnceTok) { 401 // Don't honor the 'once' when handling the primary source file, unless 402 // this is a prefix to a TU, which indicates we're generating a PCH file, or 403 // when the main file is a header (e.g. when -xc-header is provided on the 404 // commandline). 405 if (isInPrimaryFile() && TUKind != TU_Prefix && !getLangOpts().IsHeaderFile) { 406 Diag(OnceTok, diag::pp_pragma_once_in_main_file); 407 return; 408 } 409 410 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc. 411 // Mark the file as a once-only file now. 412 HeaderInfo.MarkFileIncludeOnce(getCurrentFileLexer()->getFileEntry()); 413 } 414 415 void Preprocessor::HandlePragmaMark() { 416 assert(CurPPLexer && "No current lexer?"); 417 CurLexer->ReadToEndOfLine(); 418 } 419 420 /// HandlePragmaPoison - Handle \#pragma GCC poison. PoisonTok is the 'poison'. 421 void Preprocessor::HandlePragmaPoison() { 422 Token Tok; 423 424 while (true) { 425 // Read the next token to poison. While doing this, pretend that we are 426 // skipping while reading the identifier to poison. 427 // This avoids errors on code like: 428 // #pragma GCC poison X 429 // #pragma GCC poison X 430 if (CurPPLexer) CurPPLexer->LexingRawMode = true; 431 LexUnexpandedToken(Tok); 432 if (CurPPLexer) CurPPLexer->LexingRawMode = false; 433 434 // If we reached the end of line, we're done. 435 if (Tok.is(tok::eod)) return; 436 437 // Can only poison identifiers. 438 if (Tok.isNot(tok::raw_identifier)) { 439 Diag(Tok, diag::err_pp_invalid_poison); 440 return; 441 } 442 443 // Look up the identifier info for the token. We disabled identifier lookup 444 // by saying we're skipping contents, so we need to do this manually. 445 IdentifierInfo *II = LookUpIdentifierInfo(Tok); 446 447 // Already poisoned. 448 if (II->isPoisoned()) continue; 449 450 // If this is a macro identifier, emit a warning. 451 if (isMacroDefined(II)) 452 Diag(Tok, diag::pp_poisoning_existing_macro); 453 454 // Finally, poison it! 455 II->setIsPoisoned(); 456 if (II->isFromAST()) 457 II->setChangedSinceDeserialization(); 458 } 459 } 460 461 /// HandlePragmaSystemHeader - Implement \#pragma GCC system_header. We know 462 /// that the whole directive has been parsed. 463 void Preprocessor::HandlePragmaSystemHeader(Token &SysHeaderTok) { 464 if (isInPrimaryFile()) { 465 Diag(SysHeaderTok, diag::pp_pragma_sysheader_in_main_file); 466 return; 467 } 468 469 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc. 470 PreprocessorLexer *TheLexer = getCurrentFileLexer(); 471 472 // Mark the file as a system header. 473 HeaderInfo.MarkFileSystemHeader(TheLexer->getFileEntry()); 474 475 PresumedLoc PLoc = SourceMgr.getPresumedLoc(SysHeaderTok.getLocation()); 476 if (PLoc.isInvalid()) 477 return; 478 479 unsigned FilenameID = SourceMgr.getLineTableFilenameID(PLoc.getFilename()); 480 481 // Notify the client, if desired, that we are in a new source file. 482 if (Callbacks) 483 Callbacks->FileChanged(SysHeaderTok.getLocation(), 484 PPCallbacks::SystemHeaderPragma, SrcMgr::C_System); 485 486 // Emit a line marker. This will change any source locations from this point 487 // forward to realize they are in a system header. 488 // Create a line note with this information. 489 SourceMgr.AddLineNote(SysHeaderTok.getLocation(), PLoc.getLine() + 1, 490 FilenameID, /*IsEntry=*/false, /*IsExit=*/false, 491 SrcMgr::C_System); 492 } 493 494 /// HandlePragmaDependency - Handle \#pragma GCC dependency "foo" blah. 495 void Preprocessor::HandlePragmaDependency(Token &DependencyTok) { 496 Token FilenameTok; 497 if (LexHeaderName(FilenameTok, /*AllowConcatenation*/false)) 498 return; 499 500 // If the next token wasn't a header-name, diagnose the error. 501 if (FilenameTok.isNot(tok::header_name)) { 502 Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename); 503 return; 504 } 505 506 // Reserve a buffer to get the spelling. 507 SmallString<128> FilenameBuffer; 508 bool Invalid = false; 509 StringRef Filename = getSpelling(FilenameTok, FilenameBuffer, &Invalid); 510 if (Invalid) 511 return; 512 513 bool isAngled = 514 GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename); 515 // If GetIncludeFilenameSpelling set the start ptr to null, there was an 516 // error. 517 if (Filename.empty()) 518 return; 519 520 // Search include directories for this file. 521 const DirectoryLookup *CurDir; 522 Optional<FileEntryRef> File = 523 LookupFile(FilenameTok.getLocation(), Filename, isAngled, nullptr, 524 nullptr, CurDir, nullptr, nullptr, nullptr, nullptr, nullptr); 525 if (!File) { 526 if (!SuppressIncludeNotFoundError) 527 Diag(FilenameTok, diag::err_pp_file_not_found) << Filename; 528 return; 529 } 530 531 const FileEntry *CurFile = getCurrentFileLexer()->getFileEntry(); 532 533 // If this file is older than the file it depends on, emit a diagnostic. 534 if (CurFile && CurFile->getModificationTime() < File->getModificationTime()) { 535 // Lex tokens at the end of the message and include them in the message. 536 std::string Message; 537 Lex(DependencyTok); 538 while (DependencyTok.isNot(tok::eod)) { 539 Message += getSpelling(DependencyTok) + " "; 540 Lex(DependencyTok); 541 } 542 543 // Remove the trailing ' ' if present. 544 if (!Message.empty()) 545 Message.erase(Message.end()-1); 546 Diag(FilenameTok, diag::pp_out_of_date_dependency) << Message; 547 } 548 } 549 550 /// ParsePragmaPushOrPopMacro - Handle parsing of pragma push_macro/pop_macro. 551 /// Return the IdentifierInfo* associated with the macro to push or pop. 552 IdentifierInfo *Preprocessor::ParsePragmaPushOrPopMacro(Token &Tok) { 553 // Remember the pragma token location. 554 Token PragmaTok = Tok; 555 556 // Read the '('. 557 Lex(Tok); 558 if (Tok.isNot(tok::l_paren)) { 559 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed) 560 << getSpelling(PragmaTok); 561 return nullptr; 562 } 563 564 // Read the macro name string. 565 Lex(Tok); 566 if (Tok.isNot(tok::string_literal)) { 567 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed) 568 << getSpelling(PragmaTok); 569 return nullptr; 570 } 571 572 if (Tok.hasUDSuffix()) { 573 Diag(Tok, diag::err_invalid_string_udl); 574 return nullptr; 575 } 576 577 // Remember the macro string. 578 std::string StrVal = getSpelling(Tok); 579 580 // Read the ')'. 581 Lex(Tok); 582 if (Tok.isNot(tok::r_paren)) { 583 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed) 584 << getSpelling(PragmaTok); 585 return nullptr; 586 } 587 588 assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' && 589 "Invalid string token!"); 590 591 // Create a Token from the string. 592 Token MacroTok; 593 MacroTok.startToken(); 594 MacroTok.setKind(tok::raw_identifier); 595 CreateString(StringRef(&StrVal[1], StrVal.size() - 2), MacroTok); 596 597 // Get the IdentifierInfo of MacroToPushTok. 598 return LookUpIdentifierInfo(MacroTok); 599 } 600 601 /// Handle \#pragma push_macro. 602 /// 603 /// The syntax is: 604 /// \code 605 /// #pragma push_macro("macro") 606 /// \endcode 607 void Preprocessor::HandlePragmaPushMacro(Token &PushMacroTok) { 608 // Parse the pragma directive and get the macro IdentifierInfo*. 609 IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PushMacroTok); 610 if (!IdentInfo) return; 611 612 // Get the MacroInfo associated with IdentInfo. 613 MacroInfo *MI = getMacroInfo(IdentInfo); 614 615 if (MI) { 616 // Allow the original MacroInfo to be redefined later. 617 MI->setIsAllowRedefinitionsWithoutWarning(true); 618 } 619 620 // Push the cloned MacroInfo so we can retrieve it later. 621 PragmaPushMacroInfo[IdentInfo].push_back(MI); 622 } 623 624 /// Handle \#pragma pop_macro. 625 /// 626 /// The syntax is: 627 /// \code 628 /// #pragma pop_macro("macro") 629 /// \endcode 630 void Preprocessor::HandlePragmaPopMacro(Token &PopMacroTok) { 631 SourceLocation MessageLoc = PopMacroTok.getLocation(); 632 633 // Parse the pragma directive and get the macro IdentifierInfo*. 634 IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PopMacroTok); 635 if (!IdentInfo) return; 636 637 // Find the vector<MacroInfo*> associated with the macro. 638 llvm::DenseMap<IdentifierInfo *, std::vector<MacroInfo *>>::iterator iter = 639 PragmaPushMacroInfo.find(IdentInfo); 640 if (iter != PragmaPushMacroInfo.end()) { 641 // Forget the MacroInfo currently associated with IdentInfo. 642 if (MacroInfo *MI = getMacroInfo(IdentInfo)) { 643 if (MI->isWarnIfUnused()) 644 WarnUnusedMacroLocs.erase(MI->getDefinitionLoc()); 645 appendMacroDirective(IdentInfo, AllocateUndefMacroDirective(MessageLoc)); 646 } 647 648 // Get the MacroInfo we want to reinstall. 649 MacroInfo *MacroToReInstall = iter->second.back(); 650 651 if (MacroToReInstall) 652 // Reinstall the previously pushed macro. 653 appendDefMacroDirective(IdentInfo, MacroToReInstall, MessageLoc); 654 655 // Pop PragmaPushMacroInfo stack. 656 iter->second.pop_back(); 657 if (iter->second.empty()) 658 PragmaPushMacroInfo.erase(iter); 659 } else { 660 Diag(MessageLoc, diag::warn_pragma_pop_macro_no_push) 661 << IdentInfo->getName(); 662 } 663 } 664 665 void Preprocessor::HandlePragmaIncludeAlias(Token &Tok) { 666 // We will either get a quoted filename or a bracketed filename, and we 667 // have to track which we got. The first filename is the source name, 668 // and the second name is the mapped filename. If the first is quoted, 669 // the second must be as well (cannot mix and match quotes and brackets). 670 671 // Get the open paren 672 Lex(Tok); 673 if (Tok.isNot(tok::l_paren)) { 674 Diag(Tok, diag::warn_pragma_include_alias_expected) << "("; 675 return; 676 } 677 678 // We expect either a quoted string literal, or a bracketed name 679 Token SourceFilenameTok; 680 if (LexHeaderName(SourceFilenameTok)) 681 return; 682 683 StringRef SourceFileName; 684 SmallString<128> FileNameBuffer; 685 if (SourceFilenameTok.is(tok::header_name)) { 686 SourceFileName = getSpelling(SourceFilenameTok, FileNameBuffer); 687 } else { 688 Diag(Tok, diag::warn_pragma_include_alias_expected_filename); 689 return; 690 } 691 FileNameBuffer.clear(); 692 693 // Now we expect a comma, followed by another include name 694 Lex(Tok); 695 if (Tok.isNot(tok::comma)) { 696 Diag(Tok, diag::warn_pragma_include_alias_expected) << ","; 697 return; 698 } 699 700 Token ReplaceFilenameTok; 701 if (LexHeaderName(ReplaceFilenameTok)) 702 return; 703 704 StringRef ReplaceFileName; 705 if (ReplaceFilenameTok.is(tok::header_name)) { 706 ReplaceFileName = getSpelling(ReplaceFilenameTok, FileNameBuffer); 707 } else { 708 Diag(Tok, diag::warn_pragma_include_alias_expected_filename); 709 return; 710 } 711 712 // Finally, we expect the closing paren 713 Lex(Tok); 714 if (Tok.isNot(tok::r_paren)) { 715 Diag(Tok, diag::warn_pragma_include_alias_expected) << ")"; 716 return; 717 } 718 719 // Now that we have the source and target filenames, we need to make sure 720 // they're both of the same type (angled vs non-angled) 721 StringRef OriginalSource = SourceFileName; 722 723 bool SourceIsAngled = 724 GetIncludeFilenameSpelling(SourceFilenameTok.getLocation(), 725 SourceFileName); 726 bool ReplaceIsAngled = 727 GetIncludeFilenameSpelling(ReplaceFilenameTok.getLocation(), 728 ReplaceFileName); 729 if (!SourceFileName.empty() && !ReplaceFileName.empty() && 730 (SourceIsAngled != ReplaceIsAngled)) { 731 unsigned int DiagID; 732 if (SourceIsAngled) 733 DiagID = diag::warn_pragma_include_alias_mismatch_angle; 734 else 735 DiagID = diag::warn_pragma_include_alias_mismatch_quote; 736 737 Diag(SourceFilenameTok.getLocation(), DiagID) 738 << SourceFileName 739 << ReplaceFileName; 740 741 return; 742 } 743 744 // Now we can let the include handler know about this mapping 745 getHeaderSearchInfo().AddIncludeAlias(OriginalSource, ReplaceFileName); 746 } 747 748 // Lex a component of a module name: either an identifier or a string literal; 749 // for components that can be expressed both ways, the two forms are equivalent. 750 static bool LexModuleNameComponent( 751 Preprocessor &PP, Token &Tok, 752 std::pair<IdentifierInfo *, SourceLocation> &ModuleNameComponent, 753 bool First) { 754 PP.LexUnexpandedToken(Tok); 755 if (Tok.is(tok::string_literal) && !Tok.hasUDSuffix()) { 756 StringLiteralParser Literal(Tok, PP); 757 if (Literal.hadError) 758 return true; 759 ModuleNameComponent = std::make_pair( 760 PP.getIdentifierInfo(Literal.GetString()), Tok.getLocation()); 761 } else if (!Tok.isAnnotation() && Tok.getIdentifierInfo()) { 762 ModuleNameComponent = 763 std::make_pair(Tok.getIdentifierInfo(), Tok.getLocation()); 764 } else { 765 PP.Diag(Tok.getLocation(), diag::err_pp_expected_module_name) << First; 766 return true; 767 } 768 return false; 769 } 770 771 static bool LexModuleName( 772 Preprocessor &PP, Token &Tok, 773 llvm::SmallVectorImpl<std::pair<IdentifierInfo *, SourceLocation>> 774 &ModuleName) { 775 while (true) { 776 std::pair<IdentifierInfo*, SourceLocation> NameComponent; 777 if (LexModuleNameComponent(PP, Tok, NameComponent, ModuleName.empty())) 778 return true; 779 ModuleName.push_back(NameComponent); 780 781 PP.LexUnexpandedToken(Tok); 782 if (Tok.isNot(tok::period)) 783 return false; 784 } 785 } 786 787 void Preprocessor::HandlePragmaModuleBuild(Token &Tok) { 788 SourceLocation Loc = Tok.getLocation(); 789 790 std::pair<IdentifierInfo *, SourceLocation> ModuleNameLoc; 791 if (LexModuleNameComponent(*this, Tok, ModuleNameLoc, true)) 792 return; 793 IdentifierInfo *ModuleName = ModuleNameLoc.first; 794 795 LexUnexpandedToken(Tok); 796 if (Tok.isNot(tok::eod)) { 797 Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma"; 798 DiscardUntilEndOfDirective(); 799 } 800 801 CurLexer->LexingRawMode = true; 802 803 auto TryConsumeIdentifier = [&](StringRef Ident) -> bool { 804 if (Tok.getKind() != tok::raw_identifier || 805 Tok.getRawIdentifier() != Ident) 806 return false; 807 CurLexer->Lex(Tok); 808 return true; 809 }; 810 811 // Scan forward looking for the end of the module. 812 const char *Start = CurLexer->getBufferLocation(); 813 const char *End = nullptr; 814 unsigned NestingLevel = 1; 815 while (true) { 816 End = CurLexer->getBufferLocation(); 817 CurLexer->Lex(Tok); 818 819 if (Tok.is(tok::eof)) { 820 Diag(Loc, diag::err_pp_module_build_missing_end); 821 break; 822 } 823 824 if (Tok.isNot(tok::hash) || !Tok.isAtStartOfLine()) { 825 // Token was part of module; keep going. 826 continue; 827 } 828 829 // We hit something directive-shaped; check to see if this is the end 830 // of the module build. 831 CurLexer->ParsingPreprocessorDirective = true; 832 CurLexer->Lex(Tok); 833 if (TryConsumeIdentifier("pragma") && TryConsumeIdentifier("clang") && 834 TryConsumeIdentifier("module")) { 835 if (TryConsumeIdentifier("build")) 836 // #pragma clang module build -> entering a nested module build. 837 ++NestingLevel; 838 else if (TryConsumeIdentifier("endbuild")) { 839 // #pragma clang module endbuild -> leaving a module build. 840 if (--NestingLevel == 0) 841 break; 842 } 843 // We should either be looking at the EOD or more of the current directive 844 // preceding the EOD. Either way we can ignore this token and keep going. 845 assert(Tok.getKind() != tok::eof && "missing EOD before EOF"); 846 } 847 } 848 849 CurLexer->LexingRawMode = false; 850 851 // Load the extracted text as a preprocessed module. 852 assert(CurLexer->getBuffer().begin() <= Start && 853 Start <= CurLexer->getBuffer().end() && 854 CurLexer->getBuffer().begin() <= End && 855 End <= CurLexer->getBuffer().end() && 856 "module source range not contained within same file buffer"); 857 TheModuleLoader.createModuleFromSource(Loc, ModuleName->getName(), 858 StringRef(Start, End - Start)); 859 } 860 861 void Preprocessor::HandlePragmaHdrstop(Token &Tok) { 862 Lex(Tok); 863 if (Tok.is(tok::l_paren)) { 864 Diag(Tok.getLocation(), diag::warn_pp_hdrstop_filename_ignored); 865 866 std::string FileName; 867 if (!LexStringLiteral(Tok, FileName, "pragma hdrstop", false)) 868 return; 869 870 if (Tok.isNot(tok::r_paren)) { 871 Diag(Tok, diag::err_expected) << tok::r_paren; 872 return; 873 } 874 Lex(Tok); 875 } 876 if (Tok.isNot(tok::eod)) 877 Diag(Tok.getLocation(), diag::ext_pp_extra_tokens_at_eol) 878 << "pragma hdrstop"; 879 880 if (creatingPCHWithPragmaHdrStop() && 881 SourceMgr.isInMainFile(Tok.getLocation())) { 882 assert(CurLexer && "no lexer for #pragma hdrstop processing"); 883 Token &Result = Tok; 884 Result.startToken(); 885 CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd, tok::eof); 886 CurLexer->cutOffLexing(); 887 } 888 if (usingPCHWithPragmaHdrStop()) 889 SkippingUntilPragmaHdrStop = false; 890 } 891 892 /// AddPragmaHandler - Add the specified pragma handler to the preprocessor. 893 /// If 'Namespace' is non-null, then it is a token required to exist on the 894 /// pragma line before the pragma string starts, e.g. "STDC" or "GCC". 895 void Preprocessor::AddPragmaHandler(StringRef Namespace, 896 PragmaHandler *Handler) { 897 PragmaNamespace *InsertNS = PragmaHandlers.get(); 898 899 // If this is specified to be in a namespace, step down into it. 900 if (!Namespace.empty()) { 901 // If there is already a pragma handler with the name of this namespace, 902 // we either have an error (directive with the same name as a namespace) or 903 // we already have the namespace to insert into. 904 if (PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace)) { 905 InsertNS = Existing->getIfNamespace(); 906 assert(InsertNS != nullptr && "Cannot have a pragma namespace and pragma" 907 " handler with the same name!"); 908 } else { 909 // Otherwise, this namespace doesn't exist yet, create and insert the 910 // handler for it. 911 InsertNS = new PragmaNamespace(Namespace); 912 PragmaHandlers->AddPragma(InsertNS); 913 } 914 } 915 916 // Check to make sure we don't already have a pragma for this identifier. 917 assert(!InsertNS->FindHandler(Handler->getName()) && 918 "Pragma handler already exists for this identifier!"); 919 InsertNS->AddPragma(Handler); 920 } 921 922 /// RemovePragmaHandler - Remove the specific pragma handler from the 923 /// preprocessor. If \arg Namespace is non-null, then it should be the 924 /// namespace that \arg Handler was added to. It is an error to remove 925 /// a handler that has not been registered. 926 void Preprocessor::RemovePragmaHandler(StringRef Namespace, 927 PragmaHandler *Handler) { 928 PragmaNamespace *NS = PragmaHandlers.get(); 929 930 // If this is specified to be in a namespace, step down into it. 931 if (!Namespace.empty()) { 932 PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace); 933 assert(Existing && "Namespace containing handler does not exist!"); 934 935 NS = Existing->getIfNamespace(); 936 assert(NS && "Invalid namespace, registered as a regular pragma handler!"); 937 } 938 939 NS->RemovePragmaHandler(Handler); 940 941 // If this is a non-default namespace and it is now empty, remove it. 942 if (NS != PragmaHandlers.get() && NS->IsEmpty()) { 943 PragmaHandlers->RemovePragmaHandler(NS); 944 delete NS; 945 } 946 } 947 948 bool Preprocessor::LexOnOffSwitch(tok::OnOffSwitch &Result) { 949 Token Tok; 950 LexUnexpandedToken(Tok); 951 952 if (Tok.isNot(tok::identifier)) { 953 Diag(Tok, diag::ext_on_off_switch_syntax); 954 return true; 955 } 956 IdentifierInfo *II = Tok.getIdentifierInfo(); 957 if (II->isStr("ON")) 958 Result = tok::OOS_ON; 959 else if (II->isStr("OFF")) 960 Result = tok::OOS_OFF; 961 else if (II->isStr("DEFAULT")) 962 Result = tok::OOS_DEFAULT; 963 else { 964 Diag(Tok, diag::ext_on_off_switch_syntax); 965 return true; 966 } 967 968 // Verify that this is followed by EOD. 969 LexUnexpandedToken(Tok); 970 if (Tok.isNot(tok::eod)) 971 Diag(Tok, diag::ext_pragma_syntax_eod); 972 return false; 973 } 974 975 namespace { 976 977 /// PragmaOnceHandler - "\#pragma once" marks the file as atomically included. 978 struct PragmaOnceHandler : public PragmaHandler { 979 PragmaOnceHandler() : PragmaHandler("once") {} 980 981 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, 982 Token &OnceTok) override { 983 PP.CheckEndOfDirective("pragma once"); 984 PP.HandlePragmaOnce(OnceTok); 985 } 986 }; 987 988 /// PragmaMarkHandler - "\#pragma mark ..." is ignored by the compiler, and the 989 /// rest of the line is not lexed. 990 struct PragmaMarkHandler : public PragmaHandler { 991 PragmaMarkHandler() : PragmaHandler("mark") {} 992 993 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, 994 Token &MarkTok) override { 995 PP.HandlePragmaMark(); 996 } 997 }; 998 999 /// PragmaPoisonHandler - "\#pragma poison x" marks x as not usable. 1000 struct PragmaPoisonHandler : public PragmaHandler { 1001 PragmaPoisonHandler() : PragmaHandler("poison") {} 1002 1003 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, 1004 Token &PoisonTok) override { 1005 PP.HandlePragmaPoison(); 1006 } 1007 }; 1008 1009 /// PragmaSystemHeaderHandler - "\#pragma system_header" marks the current file 1010 /// as a system header, which silences warnings in it. 1011 struct PragmaSystemHeaderHandler : public PragmaHandler { 1012 PragmaSystemHeaderHandler() : PragmaHandler("system_header") {} 1013 1014 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, 1015 Token &SHToken) override { 1016 PP.HandlePragmaSystemHeader(SHToken); 1017 PP.CheckEndOfDirective("pragma"); 1018 } 1019 }; 1020 1021 struct PragmaDependencyHandler : public PragmaHandler { 1022 PragmaDependencyHandler() : PragmaHandler("dependency") {} 1023 1024 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, 1025 Token &DepToken) override { 1026 PP.HandlePragmaDependency(DepToken); 1027 } 1028 }; 1029 1030 struct PragmaDebugHandler : public PragmaHandler { 1031 PragmaDebugHandler() : PragmaHandler("__debug") {} 1032 1033 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, 1034 Token &DebugToken) override { 1035 Token Tok; 1036 PP.LexUnexpandedToken(Tok); 1037 if (Tok.isNot(tok::identifier)) { 1038 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid); 1039 return; 1040 } 1041 IdentifierInfo *II = Tok.getIdentifierInfo(); 1042 1043 if (II->isStr("assert")) { 1044 if (!PP.getPreprocessorOpts().DisablePragmaDebugCrash) 1045 llvm_unreachable("This is an assertion!"); 1046 } else if (II->isStr("crash")) { 1047 llvm::Timer T("crash", "pragma crash"); 1048 llvm::TimeRegion R(&T); 1049 if (!PP.getPreprocessorOpts().DisablePragmaDebugCrash) 1050 LLVM_BUILTIN_TRAP; 1051 } else if (II->isStr("parser_crash")) { 1052 if (!PP.getPreprocessorOpts().DisablePragmaDebugCrash) { 1053 Token Crasher; 1054 Crasher.startToken(); 1055 Crasher.setKind(tok::annot_pragma_parser_crash); 1056 Crasher.setAnnotationRange(SourceRange(Tok.getLocation())); 1057 PP.EnterToken(Crasher, /*IsReinject*/ false); 1058 } 1059 } else if (II->isStr("dump")) { 1060 Token Identifier; 1061 PP.LexUnexpandedToken(Identifier); 1062 if (auto *DumpII = Identifier.getIdentifierInfo()) { 1063 Token DumpAnnot; 1064 DumpAnnot.startToken(); 1065 DumpAnnot.setKind(tok::annot_pragma_dump); 1066 DumpAnnot.setAnnotationRange( 1067 SourceRange(Tok.getLocation(), Identifier.getLocation())); 1068 DumpAnnot.setAnnotationValue(DumpII); 1069 PP.DiscardUntilEndOfDirective(); 1070 PP.EnterToken(DumpAnnot, /*IsReinject*/false); 1071 } else { 1072 PP.Diag(Identifier, diag::warn_pragma_debug_missing_argument) 1073 << II->getName(); 1074 } 1075 } else if (II->isStr("diag_mapping")) { 1076 Token DiagName; 1077 PP.LexUnexpandedToken(DiagName); 1078 if (DiagName.is(tok::eod)) 1079 PP.getDiagnostics().dump(); 1080 else if (DiagName.is(tok::string_literal) && !DiagName.hasUDSuffix()) { 1081 StringLiteralParser Literal(DiagName, PP); 1082 if (Literal.hadError) 1083 return; 1084 PP.getDiagnostics().dump(Literal.GetString()); 1085 } else { 1086 PP.Diag(DiagName, diag::warn_pragma_debug_missing_argument) 1087 << II->getName(); 1088 } 1089 } else if (II->isStr("llvm_fatal_error")) { 1090 if (!PP.getPreprocessorOpts().DisablePragmaDebugCrash) 1091 llvm::report_fatal_error("#pragma clang __debug llvm_fatal_error"); 1092 } else if (II->isStr("llvm_unreachable")) { 1093 if (!PP.getPreprocessorOpts().DisablePragmaDebugCrash) 1094 llvm_unreachable("#pragma clang __debug llvm_unreachable"); 1095 } else if (II->isStr("macro")) { 1096 Token MacroName; 1097 PP.LexUnexpandedToken(MacroName); 1098 auto *MacroII = MacroName.getIdentifierInfo(); 1099 if (MacroII) 1100 PP.dumpMacroInfo(MacroII); 1101 else 1102 PP.Diag(MacroName, diag::warn_pragma_debug_missing_argument) 1103 << II->getName(); 1104 } else if (II->isStr("module_map")) { 1105 llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 8> 1106 ModuleName; 1107 if (LexModuleName(PP, Tok, ModuleName)) 1108 return; 1109 ModuleMap &MM = PP.getHeaderSearchInfo().getModuleMap(); 1110 Module *M = nullptr; 1111 for (auto IIAndLoc : ModuleName) { 1112 M = MM.lookupModuleQualified(IIAndLoc.first->getName(), M); 1113 if (!M) { 1114 PP.Diag(IIAndLoc.second, diag::warn_pragma_debug_unknown_module) 1115 << IIAndLoc.first; 1116 return; 1117 } 1118 } 1119 M->dump(); 1120 } else if (II->isStr("overflow_stack")) { 1121 if (!PP.getPreprocessorOpts().DisablePragmaDebugCrash) 1122 DebugOverflowStack(); 1123 } else if (II->isStr("captured")) { 1124 HandleCaptured(PP); 1125 } else { 1126 PP.Diag(Tok, diag::warn_pragma_debug_unexpected_command) 1127 << II->getName(); 1128 } 1129 1130 PPCallbacks *Callbacks = PP.getPPCallbacks(); 1131 if (Callbacks) 1132 Callbacks->PragmaDebug(Tok.getLocation(), II->getName()); 1133 } 1134 1135 void HandleCaptured(Preprocessor &PP) { 1136 Token Tok; 1137 PP.LexUnexpandedToken(Tok); 1138 1139 if (Tok.isNot(tok::eod)) { 1140 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) 1141 << "pragma clang __debug captured"; 1142 return; 1143 } 1144 1145 SourceLocation NameLoc = Tok.getLocation(); 1146 MutableArrayRef<Token> Toks( 1147 PP.getPreprocessorAllocator().Allocate<Token>(1), 1); 1148 Toks[0].startToken(); 1149 Toks[0].setKind(tok::annot_pragma_captured); 1150 Toks[0].setLocation(NameLoc); 1151 1152 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true, 1153 /*IsReinject=*/false); 1154 } 1155 1156 // Disable MSVC warning about runtime stack overflow. 1157 #ifdef _MSC_VER 1158 #pragma warning(disable : 4717) 1159 #endif 1160 static void DebugOverflowStack(void (*P)() = nullptr) { 1161 void (*volatile Self)(void(*P)()) = DebugOverflowStack; 1162 Self(reinterpret_cast<void(*)()>(Self)); 1163 } 1164 #ifdef _MSC_VER 1165 #pragma warning(default : 4717) 1166 #endif 1167 }; 1168 1169 /// PragmaDiagnosticHandler - e.g. '\#pragma GCC diagnostic ignored "-Wformat"' 1170 struct PragmaDiagnosticHandler : public PragmaHandler { 1171 private: 1172 const char *Namespace; 1173 1174 public: 1175 explicit PragmaDiagnosticHandler(const char *NS) 1176 : PragmaHandler("diagnostic"), Namespace(NS) {} 1177 1178 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, 1179 Token &DiagToken) override { 1180 SourceLocation DiagLoc = DiagToken.getLocation(); 1181 Token Tok; 1182 PP.LexUnexpandedToken(Tok); 1183 if (Tok.isNot(tok::identifier)) { 1184 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid); 1185 return; 1186 } 1187 IdentifierInfo *II = Tok.getIdentifierInfo(); 1188 PPCallbacks *Callbacks = PP.getPPCallbacks(); 1189 1190 if (II->isStr("pop")) { 1191 if (!PP.getDiagnostics().popMappings(DiagLoc)) 1192 PP.Diag(Tok, diag::warn_pragma_diagnostic_cannot_pop); 1193 else if (Callbacks) 1194 Callbacks->PragmaDiagnosticPop(DiagLoc, Namespace); 1195 return; 1196 } else if (II->isStr("push")) { 1197 PP.getDiagnostics().pushMappings(DiagLoc); 1198 if (Callbacks) 1199 Callbacks->PragmaDiagnosticPush(DiagLoc, Namespace); 1200 return; 1201 } 1202 1203 diag::Severity SV = llvm::StringSwitch<diag::Severity>(II->getName()) 1204 .Case("ignored", diag::Severity::Ignored) 1205 .Case("warning", diag::Severity::Warning) 1206 .Case("error", diag::Severity::Error) 1207 .Case("fatal", diag::Severity::Fatal) 1208 .Default(diag::Severity()); 1209 1210 if (SV == diag::Severity()) { 1211 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid); 1212 return; 1213 } 1214 1215 PP.LexUnexpandedToken(Tok); 1216 SourceLocation StringLoc = Tok.getLocation(); 1217 1218 std::string WarningName; 1219 if (!PP.FinishLexStringLiteral(Tok, WarningName, "pragma diagnostic", 1220 /*AllowMacroExpansion=*/false)) 1221 return; 1222 1223 if (Tok.isNot(tok::eod)) { 1224 PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token); 1225 return; 1226 } 1227 1228 if (WarningName.size() < 3 || WarningName[0] != '-' || 1229 (WarningName[1] != 'W' && WarningName[1] != 'R')) { 1230 PP.Diag(StringLoc, diag::warn_pragma_diagnostic_invalid_option); 1231 return; 1232 } 1233 1234 diag::Flavor Flavor = WarningName[1] == 'W' ? diag::Flavor::WarningOrError 1235 : diag::Flavor::Remark; 1236 StringRef Group = StringRef(WarningName).substr(2); 1237 bool unknownDiag = false; 1238 if (Group == "everything") { 1239 // Special handling for pragma clang diagnostic ... "-Weverything". 1240 // There is no formal group named "everything", so there has to be a 1241 // special case for it. 1242 PP.getDiagnostics().setSeverityForAll(Flavor, SV, DiagLoc); 1243 } else 1244 unknownDiag = PP.getDiagnostics().setSeverityForGroup(Flavor, Group, SV, 1245 DiagLoc); 1246 if (unknownDiag) 1247 PP.Diag(StringLoc, diag::warn_pragma_diagnostic_unknown_warning) 1248 << WarningName; 1249 else if (Callbacks) 1250 Callbacks->PragmaDiagnostic(DiagLoc, Namespace, SV, WarningName); 1251 } 1252 }; 1253 1254 /// "\#pragma hdrstop [<header-name-string>]" 1255 struct PragmaHdrstopHandler : public PragmaHandler { 1256 PragmaHdrstopHandler() : PragmaHandler("hdrstop") {} 1257 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, 1258 Token &DepToken) override { 1259 PP.HandlePragmaHdrstop(DepToken); 1260 } 1261 }; 1262 1263 /// "\#pragma warning(...)". MSVC's diagnostics do not map cleanly to clang's 1264 /// diagnostics, so we don't really implement this pragma. We parse it and 1265 /// ignore it to avoid -Wunknown-pragma warnings. 1266 struct PragmaWarningHandler : public PragmaHandler { 1267 PragmaWarningHandler() : PragmaHandler("warning") {} 1268 1269 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, 1270 Token &Tok) override { 1271 // Parse things like: 1272 // warning(push, 1) 1273 // warning(pop) 1274 // warning(disable : 1 2 3 ; error : 4 5 6 ; suppress : 7 8 9) 1275 SourceLocation DiagLoc = Tok.getLocation(); 1276 PPCallbacks *Callbacks = PP.getPPCallbacks(); 1277 1278 PP.Lex(Tok); 1279 if (Tok.isNot(tok::l_paren)) { 1280 PP.Diag(Tok, diag::warn_pragma_warning_expected) << "("; 1281 return; 1282 } 1283 1284 PP.Lex(Tok); 1285 IdentifierInfo *II = Tok.getIdentifierInfo(); 1286 1287 if (II && II->isStr("push")) { 1288 // #pragma warning( push[ ,n ] ) 1289 int Level = -1; 1290 PP.Lex(Tok); 1291 if (Tok.is(tok::comma)) { 1292 PP.Lex(Tok); 1293 uint64_t Value; 1294 if (Tok.is(tok::numeric_constant) && 1295 PP.parseSimpleIntegerLiteral(Tok, Value)) 1296 Level = int(Value); 1297 if (Level < 0 || Level > 4) { 1298 PP.Diag(Tok, diag::warn_pragma_warning_push_level); 1299 return; 1300 } 1301 } 1302 if (Callbacks) 1303 Callbacks->PragmaWarningPush(DiagLoc, Level); 1304 } else if (II && II->isStr("pop")) { 1305 // #pragma warning( pop ) 1306 PP.Lex(Tok); 1307 if (Callbacks) 1308 Callbacks->PragmaWarningPop(DiagLoc); 1309 } else { 1310 // #pragma warning( warning-specifier : warning-number-list 1311 // [; warning-specifier : warning-number-list...] ) 1312 while (true) { 1313 II = Tok.getIdentifierInfo(); 1314 if (!II && !Tok.is(tok::numeric_constant)) { 1315 PP.Diag(Tok, diag::warn_pragma_warning_spec_invalid); 1316 return; 1317 } 1318 1319 // Figure out which warning specifier this is. 1320 bool SpecifierValid; 1321 StringRef Specifier; 1322 llvm::SmallString<1> SpecifierBuf; 1323 if (II) { 1324 Specifier = II->getName(); 1325 SpecifierValid = llvm::StringSwitch<bool>(Specifier) 1326 .Cases("default", "disable", "error", "once", 1327 "suppress", true) 1328 .Default(false); 1329 // If we read a correct specifier, snatch next token (that should be 1330 // ":", checked later). 1331 if (SpecifierValid) 1332 PP.Lex(Tok); 1333 } else { 1334 // Token is a numeric constant. It should be either 1, 2, 3 or 4. 1335 uint64_t Value; 1336 Specifier = PP.getSpelling(Tok, SpecifierBuf); 1337 if (PP.parseSimpleIntegerLiteral(Tok, Value)) { 1338 SpecifierValid = (Value >= 1) && (Value <= 4); 1339 } else 1340 SpecifierValid = false; 1341 // Next token already snatched by parseSimpleIntegerLiteral. 1342 } 1343 1344 if (!SpecifierValid) { 1345 PP.Diag(Tok, diag::warn_pragma_warning_spec_invalid); 1346 return; 1347 } 1348 if (Tok.isNot(tok::colon)) { 1349 PP.Diag(Tok, diag::warn_pragma_warning_expected) << ":"; 1350 return; 1351 } 1352 1353 // Collect the warning ids. 1354 SmallVector<int, 4> Ids; 1355 PP.Lex(Tok); 1356 while (Tok.is(tok::numeric_constant)) { 1357 uint64_t Value; 1358 if (!PP.parseSimpleIntegerLiteral(Tok, Value) || Value == 0 || 1359 Value > std::numeric_limits<int>::max()) { 1360 PP.Diag(Tok, diag::warn_pragma_warning_expected_number); 1361 return; 1362 } 1363 Ids.push_back(int(Value)); 1364 } 1365 if (Callbacks) 1366 Callbacks->PragmaWarning(DiagLoc, Specifier, Ids); 1367 1368 // Parse the next specifier if there is a semicolon. 1369 if (Tok.isNot(tok::semi)) 1370 break; 1371 PP.Lex(Tok); 1372 } 1373 } 1374 1375 if (Tok.isNot(tok::r_paren)) { 1376 PP.Diag(Tok, diag::warn_pragma_warning_expected) << ")"; 1377 return; 1378 } 1379 1380 PP.Lex(Tok); 1381 if (Tok.isNot(tok::eod)) 1382 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma warning"; 1383 } 1384 }; 1385 1386 /// "\#pragma execution_character_set(...)". MSVC supports this pragma only 1387 /// for "UTF-8". We parse it and ignore it if UTF-8 is provided and warn 1388 /// otherwise to avoid -Wunknown-pragma warnings. 1389 struct PragmaExecCharsetHandler : public PragmaHandler { 1390 PragmaExecCharsetHandler() : PragmaHandler("execution_character_set") {} 1391 1392 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, 1393 Token &Tok) override { 1394 // Parse things like: 1395 // execution_character_set(push, "UTF-8") 1396 // execution_character_set(pop) 1397 SourceLocation DiagLoc = Tok.getLocation(); 1398 PPCallbacks *Callbacks = PP.getPPCallbacks(); 1399 1400 PP.Lex(Tok); 1401 if (Tok.isNot(tok::l_paren)) { 1402 PP.Diag(Tok, diag::warn_pragma_exec_charset_expected) << "("; 1403 return; 1404 } 1405 1406 PP.Lex(Tok); 1407 IdentifierInfo *II = Tok.getIdentifierInfo(); 1408 1409 if (II && II->isStr("push")) { 1410 // #pragma execution_character_set( push[ , string ] ) 1411 PP.Lex(Tok); 1412 if (Tok.is(tok::comma)) { 1413 PP.Lex(Tok); 1414 1415 std::string ExecCharset; 1416 if (!PP.FinishLexStringLiteral(Tok, ExecCharset, 1417 "pragma execution_character_set", 1418 /*AllowMacroExpansion=*/false)) 1419 return; 1420 1421 // MSVC supports either of these, but nothing else. 1422 if (ExecCharset != "UTF-8" && ExecCharset != "utf-8") { 1423 PP.Diag(Tok, diag::warn_pragma_exec_charset_push_invalid) << ExecCharset; 1424 return; 1425 } 1426 } 1427 if (Callbacks) 1428 Callbacks->PragmaExecCharsetPush(DiagLoc, "UTF-8"); 1429 } else if (II && II->isStr("pop")) { 1430 // #pragma execution_character_set( pop ) 1431 PP.Lex(Tok); 1432 if (Callbacks) 1433 Callbacks->PragmaExecCharsetPop(DiagLoc); 1434 } else { 1435 PP.Diag(Tok, diag::warn_pragma_exec_charset_spec_invalid); 1436 return; 1437 } 1438 1439 if (Tok.isNot(tok::r_paren)) { 1440 PP.Diag(Tok, diag::warn_pragma_exec_charset_expected) << ")"; 1441 return; 1442 } 1443 1444 PP.Lex(Tok); 1445 if (Tok.isNot(tok::eod)) 1446 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma execution_character_set"; 1447 } 1448 }; 1449 1450 /// PragmaIncludeAliasHandler - "\#pragma include_alias("...")". 1451 struct PragmaIncludeAliasHandler : public PragmaHandler { 1452 PragmaIncludeAliasHandler() : PragmaHandler("include_alias") {} 1453 1454 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, 1455 Token &IncludeAliasTok) override { 1456 PP.HandlePragmaIncludeAlias(IncludeAliasTok); 1457 } 1458 }; 1459 1460 /// PragmaMessageHandler - Handle the microsoft and gcc \#pragma message 1461 /// extension. The syntax is: 1462 /// \code 1463 /// #pragma message(string) 1464 /// \endcode 1465 /// OR, in GCC mode: 1466 /// \code 1467 /// #pragma message string 1468 /// \endcode 1469 /// string is a string, which is fully macro expanded, and permits string 1470 /// concatenation, embedded escape characters, etc... See MSDN for more details. 1471 /// Also handles \#pragma GCC warning and \#pragma GCC error which take the same 1472 /// form as \#pragma message. 1473 struct PragmaMessageHandler : public PragmaHandler { 1474 private: 1475 const PPCallbacks::PragmaMessageKind Kind; 1476 const StringRef Namespace; 1477 1478 static const char* PragmaKind(PPCallbacks::PragmaMessageKind Kind, 1479 bool PragmaNameOnly = false) { 1480 switch (Kind) { 1481 case PPCallbacks::PMK_Message: 1482 return PragmaNameOnly ? "message" : "pragma message"; 1483 case PPCallbacks::PMK_Warning: 1484 return PragmaNameOnly ? "warning" : "pragma warning"; 1485 case PPCallbacks::PMK_Error: 1486 return PragmaNameOnly ? "error" : "pragma error"; 1487 } 1488 llvm_unreachable("Unknown PragmaMessageKind!"); 1489 } 1490 1491 public: 1492 PragmaMessageHandler(PPCallbacks::PragmaMessageKind Kind, 1493 StringRef Namespace = StringRef()) 1494 : PragmaHandler(PragmaKind(Kind, true)), Kind(Kind), 1495 Namespace(Namespace) {} 1496 1497 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, 1498 Token &Tok) override { 1499 SourceLocation MessageLoc = Tok.getLocation(); 1500 PP.Lex(Tok); 1501 bool ExpectClosingParen = false; 1502 switch (Tok.getKind()) { 1503 case tok::l_paren: 1504 // We have a MSVC style pragma message. 1505 ExpectClosingParen = true; 1506 // Read the string. 1507 PP.Lex(Tok); 1508 break; 1509 case tok::string_literal: 1510 // We have a GCC style pragma message, and we just read the string. 1511 break; 1512 default: 1513 PP.Diag(MessageLoc, diag::err_pragma_message_malformed) << Kind; 1514 return; 1515 } 1516 1517 std::string MessageString; 1518 if (!PP.FinishLexStringLiteral(Tok, MessageString, PragmaKind(Kind), 1519 /*AllowMacroExpansion=*/true)) 1520 return; 1521 1522 if (ExpectClosingParen) { 1523 if (Tok.isNot(tok::r_paren)) { 1524 PP.Diag(Tok.getLocation(), diag::err_pragma_message_malformed) << Kind; 1525 return; 1526 } 1527 PP.Lex(Tok); // eat the r_paren. 1528 } 1529 1530 if (Tok.isNot(tok::eod)) { 1531 PP.Diag(Tok.getLocation(), diag::err_pragma_message_malformed) << Kind; 1532 return; 1533 } 1534 1535 // Output the message. 1536 PP.Diag(MessageLoc, (Kind == PPCallbacks::PMK_Error) 1537 ? diag::err_pragma_message 1538 : diag::warn_pragma_message) << MessageString; 1539 1540 // If the pragma is lexically sound, notify any interested PPCallbacks. 1541 if (PPCallbacks *Callbacks = PP.getPPCallbacks()) 1542 Callbacks->PragmaMessage(MessageLoc, Namespace, Kind, MessageString); 1543 } 1544 }; 1545 1546 /// Handle the clang \#pragma module import extension. The syntax is: 1547 /// \code 1548 /// #pragma clang module import some.module.name 1549 /// \endcode 1550 struct PragmaModuleImportHandler : public PragmaHandler { 1551 PragmaModuleImportHandler() : PragmaHandler("import") {} 1552 1553 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, 1554 Token &Tok) override { 1555 SourceLocation ImportLoc = Tok.getLocation(); 1556 1557 // Read the module name. 1558 llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 8> 1559 ModuleName; 1560 if (LexModuleName(PP, Tok, ModuleName)) 1561 return; 1562 1563 if (Tok.isNot(tok::eod)) 1564 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma"; 1565 1566 // If we have a non-empty module path, load the named module. 1567 Module *Imported = 1568 PP.getModuleLoader().loadModule(ImportLoc, ModuleName, Module::Hidden, 1569 /*IsInclusionDirective=*/false); 1570 if (!Imported) 1571 return; 1572 1573 PP.makeModuleVisible(Imported, ImportLoc); 1574 PP.EnterAnnotationToken(SourceRange(ImportLoc, ModuleName.back().second), 1575 tok::annot_module_include, Imported); 1576 if (auto *CB = PP.getPPCallbacks()) 1577 CB->moduleImport(ImportLoc, ModuleName, Imported); 1578 } 1579 }; 1580 1581 /// Handle the clang \#pragma module begin extension. The syntax is: 1582 /// \code 1583 /// #pragma clang module begin some.module.name 1584 /// ... 1585 /// #pragma clang module end 1586 /// \endcode 1587 struct PragmaModuleBeginHandler : public PragmaHandler { 1588 PragmaModuleBeginHandler() : PragmaHandler("begin") {} 1589 1590 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, 1591 Token &Tok) override { 1592 SourceLocation BeginLoc = Tok.getLocation(); 1593 1594 // Read the module name. 1595 llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 8> 1596 ModuleName; 1597 if (LexModuleName(PP, Tok, ModuleName)) 1598 return; 1599 1600 if (Tok.isNot(tok::eod)) 1601 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma"; 1602 1603 // We can only enter submodules of the current module. 1604 StringRef Current = PP.getLangOpts().CurrentModule; 1605 if (ModuleName.front().first->getName() != Current) { 1606 PP.Diag(ModuleName.front().second, diag::err_pp_module_begin_wrong_module) 1607 << ModuleName.front().first << (ModuleName.size() > 1) 1608 << Current.empty() << Current; 1609 return; 1610 } 1611 1612 // Find the module we're entering. We require that a module map for it 1613 // be loaded or implicitly loadable. 1614 auto &HSI = PP.getHeaderSearchInfo(); 1615 Module *M = HSI.lookupModule(Current); 1616 if (!M) { 1617 PP.Diag(ModuleName.front().second, 1618 diag::err_pp_module_begin_no_module_map) << Current; 1619 return; 1620 } 1621 for (unsigned I = 1; I != ModuleName.size(); ++I) { 1622 auto *NewM = M->findOrInferSubmodule(ModuleName[I].first->getName()); 1623 if (!NewM) { 1624 PP.Diag(ModuleName[I].second, diag::err_pp_module_begin_no_submodule) 1625 << M->getFullModuleName() << ModuleName[I].first; 1626 return; 1627 } 1628 M = NewM; 1629 } 1630 1631 // If the module isn't available, it doesn't make sense to enter it. 1632 if (Preprocessor::checkModuleIsAvailable( 1633 PP.getLangOpts(), PP.getTargetInfo(), PP.getDiagnostics(), M)) { 1634 PP.Diag(BeginLoc, diag::note_pp_module_begin_here) 1635 << M->getTopLevelModuleName(); 1636 return; 1637 } 1638 1639 // Enter the scope of the submodule. 1640 PP.EnterSubmodule(M, BeginLoc, /*ForPragma*/true); 1641 PP.EnterAnnotationToken(SourceRange(BeginLoc, ModuleName.back().second), 1642 tok::annot_module_begin, M); 1643 } 1644 }; 1645 1646 /// Handle the clang \#pragma module end extension. 1647 struct PragmaModuleEndHandler : public PragmaHandler { 1648 PragmaModuleEndHandler() : PragmaHandler("end") {} 1649 1650 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, 1651 Token &Tok) override { 1652 SourceLocation Loc = Tok.getLocation(); 1653 1654 PP.LexUnexpandedToken(Tok); 1655 if (Tok.isNot(tok::eod)) 1656 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma"; 1657 1658 Module *M = PP.LeaveSubmodule(/*ForPragma*/true); 1659 if (M) 1660 PP.EnterAnnotationToken(SourceRange(Loc), tok::annot_module_end, M); 1661 else 1662 PP.Diag(Loc, diag::err_pp_module_end_without_module_begin); 1663 } 1664 }; 1665 1666 /// Handle the clang \#pragma module build extension. 1667 struct PragmaModuleBuildHandler : public PragmaHandler { 1668 PragmaModuleBuildHandler() : PragmaHandler("build") {} 1669 1670 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, 1671 Token &Tok) override { 1672 PP.HandlePragmaModuleBuild(Tok); 1673 } 1674 }; 1675 1676 /// Handle the clang \#pragma module load extension. 1677 struct PragmaModuleLoadHandler : public PragmaHandler { 1678 PragmaModuleLoadHandler() : PragmaHandler("load") {} 1679 1680 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, 1681 Token &Tok) override { 1682 SourceLocation Loc = Tok.getLocation(); 1683 1684 // Read the module name. 1685 llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 8> 1686 ModuleName; 1687 if (LexModuleName(PP, Tok, ModuleName)) 1688 return; 1689 1690 if (Tok.isNot(tok::eod)) 1691 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma"; 1692 1693 // Load the module, don't make it visible. 1694 PP.getModuleLoader().loadModule(Loc, ModuleName, Module::Hidden, 1695 /*IsInclusionDirective=*/false); 1696 } 1697 }; 1698 1699 /// PragmaPushMacroHandler - "\#pragma push_macro" saves the value of the 1700 /// macro on the top of the stack. 1701 struct PragmaPushMacroHandler : public PragmaHandler { 1702 PragmaPushMacroHandler() : PragmaHandler("push_macro") {} 1703 1704 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, 1705 Token &PushMacroTok) override { 1706 PP.HandlePragmaPushMacro(PushMacroTok); 1707 } 1708 }; 1709 1710 /// PragmaPopMacroHandler - "\#pragma pop_macro" sets the value of the 1711 /// macro to the value on the top of the stack. 1712 struct PragmaPopMacroHandler : public PragmaHandler { 1713 PragmaPopMacroHandler() : PragmaHandler("pop_macro") {} 1714 1715 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, 1716 Token &PopMacroTok) override { 1717 PP.HandlePragmaPopMacro(PopMacroTok); 1718 } 1719 }; 1720 1721 /// PragmaARCCFCodeAuditedHandler - 1722 /// \#pragma clang arc_cf_code_audited begin/end 1723 struct PragmaARCCFCodeAuditedHandler : public PragmaHandler { 1724 PragmaARCCFCodeAuditedHandler() : PragmaHandler("arc_cf_code_audited") {} 1725 1726 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, 1727 Token &NameTok) override { 1728 SourceLocation Loc = NameTok.getLocation(); 1729 bool IsBegin; 1730 1731 Token Tok; 1732 1733 // Lex the 'begin' or 'end'. 1734 PP.LexUnexpandedToken(Tok); 1735 const IdentifierInfo *BeginEnd = Tok.getIdentifierInfo(); 1736 if (BeginEnd && BeginEnd->isStr("begin")) { 1737 IsBegin = true; 1738 } else if (BeginEnd && BeginEnd->isStr("end")) { 1739 IsBegin = false; 1740 } else { 1741 PP.Diag(Tok.getLocation(), diag::err_pp_arc_cf_code_audited_syntax); 1742 return; 1743 } 1744 1745 // Verify that this is followed by EOD. 1746 PP.LexUnexpandedToken(Tok); 1747 if (Tok.isNot(tok::eod)) 1748 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma"; 1749 1750 // The start location of the active audit. 1751 SourceLocation BeginLoc = PP.getPragmaARCCFCodeAuditedInfo().second; 1752 1753 // The start location we want after processing this. 1754 SourceLocation NewLoc; 1755 1756 if (IsBegin) { 1757 // Complain about attempts to re-enter an audit. 1758 if (BeginLoc.isValid()) { 1759 PP.Diag(Loc, diag::err_pp_double_begin_of_arc_cf_code_audited); 1760 PP.Diag(BeginLoc, diag::note_pragma_entered_here); 1761 } 1762 NewLoc = Loc; 1763 } else { 1764 // Complain about attempts to leave an audit that doesn't exist. 1765 if (!BeginLoc.isValid()) { 1766 PP.Diag(Loc, diag::err_pp_unmatched_end_of_arc_cf_code_audited); 1767 return; 1768 } 1769 NewLoc = SourceLocation(); 1770 } 1771 1772 PP.setPragmaARCCFCodeAuditedInfo(NameTok.getIdentifierInfo(), NewLoc); 1773 } 1774 }; 1775 1776 /// PragmaAssumeNonNullHandler - 1777 /// \#pragma clang assume_nonnull begin/end 1778 struct PragmaAssumeNonNullHandler : public PragmaHandler { 1779 PragmaAssumeNonNullHandler() : PragmaHandler("assume_nonnull") {} 1780 1781 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, 1782 Token &NameTok) override { 1783 SourceLocation Loc = NameTok.getLocation(); 1784 bool IsBegin; 1785 1786 Token Tok; 1787 1788 // Lex the 'begin' or 'end'. 1789 PP.LexUnexpandedToken(Tok); 1790 const IdentifierInfo *BeginEnd = Tok.getIdentifierInfo(); 1791 if (BeginEnd && BeginEnd->isStr("begin")) { 1792 IsBegin = true; 1793 } else if (BeginEnd && BeginEnd->isStr("end")) { 1794 IsBegin = false; 1795 } else { 1796 PP.Diag(Tok.getLocation(), diag::err_pp_assume_nonnull_syntax); 1797 return; 1798 } 1799 1800 // Verify that this is followed by EOD. 1801 PP.LexUnexpandedToken(Tok); 1802 if (Tok.isNot(tok::eod)) 1803 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma"; 1804 1805 // The start location of the active audit. 1806 SourceLocation BeginLoc = PP.getPragmaAssumeNonNullLoc(); 1807 1808 // The start location we want after processing this. 1809 SourceLocation NewLoc; 1810 PPCallbacks *Callbacks = PP.getPPCallbacks(); 1811 1812 if (IsBegin) { 1813 // Complain about attempts to re-enter an audit. 1814 if (BeginLoc.isValid()) { 1815 PP.Diag(Loc, diag::err_pp_double_begin_of_assume_nonnull); 1816 PP.Diag(BeginLoc, diag::note_pragma_entered_here); 1817 } 1818 NewLoc = Loc; 1819 if (Callbacks) 1820 Callbacks->PragmaAssumeNonNullBegin(NewLoc); 1821 } else { 1822 // Complain about attempts to leave an audit that doesn't exist. 1823 if (!BeginLoc.isValid()) { 1824 PP.Diag(Loc, diag::err_pp_unmatched_end_of_assume_nonnull); 1825 return; 1826 } 1827 NewLoc = SourceLocation(); 1828 if (Callbacks) 1829 Callbacks->PragmaAssumeNonNullEnd(NewLoc); 1830 } 1831 1832 PP.setPragmaAssumeNonNullLoc(NewLoc); 1833 } 1834 }; 1835 1836 /// Handle "\#pragma region [...]" 1837 /// 1838 /// The syntax is 1839 /// \code 1840 /// #pragma region [optional name] 1841 /// #pragma endregion [optional comment] 1842 /// \endcode 1843 /// 1844 /// \note This is 1845 /// <a href="http://msdn.microsoft.com/en-us/library/b6xkz944(v=vs.80).aspx">editor-only</a> 1846 /// pragma, just skipped by compiler. 1847 struct PragmaRegionHandler : public PragmaHandler { 1848 PragmaRegionHandler(const char *pragma) : PragmaHandler(pragma) {} 1849 1850 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, 1851 Token &NameTok) override { 1852 // #pragma region: endregion matches can be verified 1853 // __pragma(region): no sense, but ignored by msvc 1854 // _Pragma is not valid for MSVC, but there isn't any point 1855 // to handle a _Pragma differently. 1856 } 1857 }; 1858 1859 } // namespace 1860 1861 /// RegisterBuiltinPragmas - Install the standard preprocessor pragmas: 1862 /// \#pragma GCC poison/system_header/dependency and \#pragma once. 1863 void Preprocessor::RegisterBuiltinPragmas() { 1864 AddPragmaHandler(new PragmaOnceHandler()); 1865 AddPragmaHandler(new PragmaMarkHandler()); 1866 AddPragmaHandler(new PragmaPushMacroHandler()); 1867 AddPragmaHandler(new PragmaPopMacroHandler()); 1868 AddPragmaHandler(new PragmaMessageHandler(PPCallbacks::PMK_Message)); 1869 1870 // #pragma GCC ... 1871 AddPragmaHandler("GCC", new PragmaPoisonHandler()); 1872 AddPragmaHandler("GCC", new PragmaSystemHeaderHandler()); 1873 AddPragmaHandler("GCC", new PragmaDependencyHandler()); 1874 AddPragmaHandler("GCC", new PragmaDiagnosticHandler("GCC")); 1875 AddPragmaHandler("GCC", new PragmaMessageHandler(PPCallbacks::PMK_Warning, 1876 "GCC")); 1877 AddPragmaHandler("GCC", new PragmaMessageHandler(PPCallbacks::PMK_Error, 1878 "GCC")); 1879 // #pragma clang ... 1880 AddPragmaHandler("clang", new PragmaPoisonHandler()); 1881 AddPragmaHandler("clang", new PragmaSystemHeaderHandler()); 1882 AddPragmaHandler("clang", new PragmaDebugHandler()); 1883 AddPragmaHandler("clang", new PragmaDependencyHandler()); 1884 AddPragmaHandler("clang", new PragmaDiagnosticHandler("clang")); 1885 AddPragmaHandler("clang", new PragmaARCCFCodeAuditedHandler()); 1886 AddPragmaHandler("clang", new PragmaAssumeNonNullHandler()); 1887 1888 // #pragma clang module ... 1889 auto *ModuleHandler = new PragmaNamespace("module"); 1890 AddPragmaHandler("clang", ModuleHandler); 1891 ModuleHandler->AddPragma(new PragmaModuleImportHandler()); 1892 ModuleHandler->AddPragma(new PragmaModuleBeginHandler()); 1893 ModuleHandler->AddPragma(new PragmaModuleEndHandler()); 1894 ModuleHandler->AddPragma(new PragmaModuleBuildHandler()); 1895 ModuleHandler->AddPragma(new PragmaModuleLoadHandler()); 1896 1897 // Add region pragmas. 1898 AddPragmaHandler(new PragmaRegionHandler("region")); 1899 AddPragmaHandler(new PragmaRegionHandler("endregion")); 1900 1901 // MS extensions. 1902 if (LangOpts.MicrosoftExt) { 1903 AddPragmaHandler(new PragmaWarningHandler()); 1904 AddPragmaHandler(new PragmaExecCharsetHandler()); 1905 AddPragmaHandler(new PragmaIncludeAliasHandler()); 1906 AddPragmaHandler(new PragmaHdrstopHandler()); 1907 } 1908 1909 // Pragmas added by plugins 1910 for (const PragmaHandlerRegistry::entry &handler : 1911 PragmaHandlerRegistry::entries()) { 1912 AddPragmaHandler(handler.instantiate().release()); 1913 } 1914 } 1915 1916 /// Ignore all pragmas, useful for modes such as -Eonly which would otherwise 1917 /// warn about those pragmas being unknown. 1918 void Preprocessor::IgnorePragmas() { 1919 AddPragmaHandler(new EmptyPragmaHandler()); 1920 // Also ignore all pragmas in all namespaces created 1921 // in Preprocessor::RegisterBuiltinPragmas(). 1922 AddPragmaHandler("GCC", new EmptyPragmaHandler()); 1923 AddPragmaHandler("clang", new EmptyPragmaHandler()); 1924 } 1925