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