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/FileManager.h" 17 #include "clang/Basic/SourceManager.h" 18 #include "clang/Lex/HeaderSearch.h" 19 #include "clang/Lex/LexDiagnostic.h" 20 #include "clang/Lex/LiteralSupport.h" 21 #include "clang/Lex/MacroInfo.h" 22 #include "clang/Lex/Preprocessor.h" 23 #include "llvm/Support/CrashRecoveryContext.h" 24 #include "llvm/Support/ErrorHandling.h" 25 #include <algorithm> 26 using namespace clang; 27 28 // Out-of-line destructor to provide a home for the class. 29 PragmaHandler::~PragmaHandler() { 30 } 31 32 //===----------------------------------------------------------------------===// 33 // EmptyPragmaHandler Implementation. 34 //===----------------------------------------------------------------------===// 35 36 EmptyPragmaHandler::EmptyPragmaHandler() {} 37 38 void EmptyPragmaHandler::HandlePragma(Preprocessor &PP, 39 PragmaIntroducerKind Introducer, 40 Token &FirstToken) {} 41 42 //===----------------------------------------------------------------------===// 43 // PragmaNamespace Implementation. 44 //===----------------------------------------------------------------------===// 45 46 PragmaNamespace::~PragmaNamespace() { 47 for (llvm::StringMap<PragmaHandler*>::iterator 48 I = Handlers.begin(), E = Handlers.end(); I != E; ++I) 49 delete I->second; 50 } 51 52 /// FindHandler - Check to see if there is already a handler for the 53 /// specified name. If not, return the handler for the null identifier if it 54 /// exists, otherwise return null. If IgnoreNull is true (the default) then 55 /// the null handler isn't returned on failure to match. 56 PragmaHandler *PragmaNamespace::FindHandler(StringRef Name, 57 bool IgnoreNull) const { 58 if (PragmaHandler *Handler = Handlers.lookup(Name)) 59 return Handler; 60 return IgnoreNull ? 0 : Handlers.lookup(StringRef()); 61 } 62 63 void PragmaNamespace::AddPragma(PragmaHandler *Handler) { 64 assert(!Handlers.lookup(Handler->getName()) && 65 "A handler with this name is already registered in this namespace"); 66 llvm::StringMapEntry<PragmaHandler *> &Entry = 67 Handlers.GetOrCreateValue(Handler->getName()); 68 Entry.setValue(Handler); 69 } 70 71 void PragmaNamespace::RemovePragmaHandler(PragmaHandler *Handler) { 72 assert(Handlers.lookup(Handler->getName()) && 73 "Handler not registered in this namespace"); 74 Handlers.erase(Handler->getName()); 75 } 76 77 void PragmaNamespace::HandlePragma(Preprocessor &PP, 78 PragmaIntroducerKind Introducer, 79 Token &Tok) { 80 // Read the 'namespace' that the directive is in, e.g. STDC. Do not macro 81 // expand it, the user can have a STDC #define, that should not affect this. 82 PP.LexUnexpandedToken(Tok); 83 84 // Get the handler for this token. If there is no handler, ignore the pragma. 85 PragmaHandler *Handler 86 = FindHandler(Tok.getIdentifierInfo() ? Tok.getIdentifierInfo()->getName() 87 : StringRef(), 88 /*IgnoreNull=*/false); 89 if (Handler == 0) { 90 PP.Diag(Tok, diag::warn_pragma_ignored); 91 return; 92 } 93 94 // Otherwise, pass it down. 95 Handler->HandlePragma(PP, Introducer, Tok); 96 } 97 98 //===----------------------------------------------------------------------===// 99 // Preprocessor Pragma Directive Handling. 100 //===----------------------------------------------------------------------===// 101 102 /// HandlePragmaDirective - The "\#pragma" directive has been parsed. Lex the 103 /// rest of the pragma, passing it to the registered pragma handlers. 104 void Preprocessor::HandlePragmaDirective(SourceLocation IntroducerLoc, 105 PragmaIntroducerKind Introducer) { 106 if (Callbacks) 107 Callbacks->PragmaDirective(IntroducerLoc, Introducer); 108 109 if (!PragmasEnabled) 110 return; 111 112 ++NumPragma; 113 114 // Invoke the first level of pragma handlers which reads the namespace id. 115 Token Tok; 116 PragmaHandlers->HandlePragma(*this, Introducer, Tok); 117 118 // If the pragma handler didn't read the rest of the line, consume it now. 119 if ((CurTokenLexer && CurTokenLexer->isParsingPreprocessorDirective()) 120 || (CurPPLexer && CurPPLexer->ParsingPreprocessorDirective)) 121 DiscardUntilEndOfDirective(); 122 } 123 124 namespace { 125 /// \brief Helper class for \see Preprocessor::Handle_Pragma. 126 class LexingFor_PragmaRAII { 127 Preprocessor &PP; 128 bool InMacroArgPreExpansion; 129 bool Failed; 130 Token &OutTok; 131 Token PragmaTok; 132 133 public: 134 LexingFor_PragmaRAII(Preprocessor &PP, bool InMacroArgPreExpansion, 135 Token &Tok) 136 : PP(PP), InMacroArgPreExpansion(InMacroArgPreExpansion), 137 Failed(false), OutTok(Tok) { 138 if (InMacroArgPreExpansion) { 139 PragmaTok = OutTok; 140 PP.EnableBacktrackAtThisPos(); 141 } 142 } 143 144 ~LexingFor_PragmaRAII() { 145 if (InMacroArgPreExpansion) { 146 if (Failed) { 147 PP.CommitBacktrackedTokens(); 148 } else { 149 PP.Backtrack(); 150 OutTok = PragmaTok; 151 } 152 } 153 } 154 155 void failed() { 156 Failed = true; 157 } 158 }; 159 } 160 161 /// Handle_Pragma - Read a _Pragma directive, slice it up, process it, then 162 /// return the first token after the directive. The _Pragma token has just 163 /// been read into 'Tok'. 164 void Preprocessor::Handle_Pragma(Token &Tok) { 165 166 // This works differently if we are pre-expanding a macro argument. 167 // In that case we don't actually "activate" the pragma now, we only lex it 168 // until we are sure it is lexically correct and then we backtrack so that 169 // we activate the pragma whenever we encounter the tokens again in the token 170 // stream. This ensures that we will activate it in the correct location 171 // or that we will ignore it if it never enters the token stream, e.g: 172 // 173 // #define EMPTY(x) 174 // #define INACTIVE(x) EMPTY(x) 175 // INACTIVE(_Pragma("clang diagnostic ignored \"-Wconversion\"")) 176 177 LexingFor_PragmaRAII _PragmaLexing(*this, InMacroArgPreExpansion, Tok); 178 179 // Remember the pragma token location. 180 SourceLocation PragmaLoc = Tok.getLocation(); 181 182 // Read the '('. 183 Lex(Tok); 184 if (Tok.isNot(tok::l_paren)) { 185 Diag(PragmaLoc, diag::err__Pragma_malformed); 186 return _PragmaLexing.failed(); 187 } 188 189 // Read the '"..."'. 190 Lex(Tok); 191 if (!tok::isStringLiteral(Tok.getKind())) { 192 Diag(PragmaLoc, diag::err__Pragma_malformed); 193 // Skip this token, and the ')', if present. 194 if (Tok.isNot(tok::r_paren)) 195 Lex(Tok); 196 if (Tok.is(tok::r_paren)) 197 Lex(Tok); 198 return _PragmaLexing.failed(); 199 } 200 201 if (Tok.hasUDSuffix()) { 202 Diag(Tok, diag::err_invalid_string_udl); 203 // Skip this token, and the ')', if present. 204 Lex(Tok); 205 if (Tok.is(tok::r_paren)) 206 Lex(Tok); 207 return _PragmaLexing.failed(); 208 } 209 210 // Remember the string. 211 Token StrTok = Tok; 212 213 // Read the ')'. 214 Lex(Tok); 215 if (Tok.isNot(tok::r_paren)) { 216 Diag(PragmaLoc, diag::err__Pragma_malformed); 217 return _PragmaLexing.failed(); 218 } 219 220 if (InMacroArgPreExpansion) 221 return; 222 223 SourceLocation RParenLoc = Tok.getLocation(); 224 std::string StrVal = getSpelling(StrTok); 225 226 // The _Pragma is lexically sound. Destringize according to C11 6.10.9.1: 227 // "The string literal is destringized by deleting any encoding prefix, 228 // deleting the leading and trailing double-quotes, replacing each escape 229 // sequence \" by a double-quote, and replacing each escape sequence \\ by a 230 // single backslash." 231 if (StrVal[0] == 'L' || StrVal[0] == 'U' || 232 (StrVal[0] == 'u' && StrVal[1] != '8')) 233 StrVal.erase(StrVal.begin()); 234 else if (StrVal[0] == 'u') 235 StrVal.erase(StrVal.begin(), StrVal.begin() + 2); 236 237 if (StrVal[0] == 'R') { 238 // FIXME: C++11 does not specify how to handle raw-string-literals here. 239 // We strip off the 'R', the quotes, the d-char-sequences, and the parens. 240 assert(StrVal[1] == '"' && StrVal[StrVal.size() - 1] == '"' && 241 "Invalid raw string token!"); 242 243 // Measure the length of the d-char-sequence. 244 unsigned NumDChars = 0; 245 while (StrVal[2 + NumDChars] != '(') { 246 assert(NumDChars < (StrVal.size() - 5) / 2 && 247 "Invalid raw string token!"); 248 ++NumDChars; 249 } 250 assert(StrVal[StrVal.size() - 2 - NumDChars] == ')'); 251 252 // Remove 'R " d-char-sequence' and 'd-char-sequence "'. We'll replace the 253 // parens below. 254 StrVal.erase(0, 2 + NumDChars); 255 StrVal.erase(StrVal.size() - 1 - NumDChars); 256 } else { 257 assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' && 258 "Invalid string token!"); 259 260 // Remove escaped quotes and escapes. 261 unsigned ResultPos = 1; 262 for (unsigned i = 1, e = StrVal.size() - 2; i != e; ++i) { 263 if (StrVal[i] != '\\' || 264 (StrVal[i + 1] != '\\' && StrVal[i + 1] != '"')) { 265 // \\ -> '\' and \" -> '"'. 266 StrVal[ResultPos++] = StrVal[i]; 267 } 268 } 269 StrVal.erase(StrVal.begin() + ResultPos, StrVal.end() - 2); 270 } 271 272 // Remove the front quote, replacing it with a space, so that the pragma 273 // contents appear to have a space before them. 274 StrVal[0] = ' '; 275 276 // Replace the terminating quote with a \n. 277 StrVal[StrVal.size()-1] = '\n'; 278 279 // Plop the string (including the newline and trailing null) into a buffer 280 // where we can lex it. 281 Token TmpTok; 282 TmpTok.startToken(); 283 CreateString(StrVal, TmpTok); 284 SourceLocation TokLoc = TmpTok.getLocation(); 285 286 // Make and enter a lexer object so that we lex and expand the tokens just 287 // like any others. 288 Lexer *TL = Lexer::Create_PragmaLexer(TokLoc, PragmaLoc, RParenLoc, 289 StrVal.size(), *this); 290 291 EnterSourceFileWithLexer(TL, 0); 292 293 // With everything set up, lex this as a #pragma directive. 294 HandlePragmaDirective(PragmaLoc, PIK__Pragma); 295 296 // Finally, return whatever came after the pragma directive. 297 return Lex(Tok); 298 } 299 300 /// HandleMicrosoft__pragma - Like Handle_Pragma except the pragma text 301 /// is not enclosed within a string literal. 302 void Preprocessor::HandleMicrosoft__pragma(Token &Tok) { 303 // Remember the pragma token location. 304 SourceLocation PragmaLoc = Tok.getLocation(); 305 306 // Read the '('. 307 Lex(Tok); 308 if (Tok.isNot(tok::l_paren)) { 309 Diag(PragmaLoc, diag::err__Pragma_malformed); 310 return; 311 } 312 313 // Get the tokens enclosed within the __pragma(), as well as the final ')'. 314 SmallVector<Token, 32> PragmaToks; 315 int NumParens = 0; 316 Lex(Tok); 317 while (Tok.isNot(tok::eof)) { 318 PragmaToks.push_back(Tok); 319 if (Tok.is(tok::l_paren)) 320 NumParens++; 321 else if (Tok.is(tok::r_paren) && NumParens-- == 0) 322 break; 323 Lex(Tok); 324 } 325 326 if (Tok.is(tok::eof)) { 327 Diag(PragmaLoc, diag::err_unterminated___pragma); 328 return; 329 } 330 331 PragmaToks.front().setFlag(Token::LeadingSpace); 332 333 // Replace the ')' with an EOD to mark the end of the pragma. 334 PragmaToks.back().setKind(tok::eod); 335 336 Token *TokArray = new Token[PragmaToks.size()]; 337 std::copy(PragmaToks.begin(), PragmaToks.end(), TokArray); 338 339 // Push the tokens onto the stack. 340 EnterTokenStream(TokArray, PragmaToks.size(), true, true); 341 342 // With everything set up, lex this as a #pragma directive. 343 HandlePragmaDirective(PragmaLoc, PIK___pragma); 344 345 // Finally, return whatever came after the pragma directive. 346 return Lex(Tok); 347 } 348 349 /// HandlePragmaOnce - Handle \#pragma once. OnceTok is the 'once'. 350 /// 351 void Preprocessor::HandlePragmaOnce(Token &OnceTok) { 352 if (isInPrimaryFile()) { 353 Diag(OnceTok, diag::pp_pragma_once_in_main_file); 354 return; 355 } 356 357 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc. 358 // Mark the file as a once-only file now. 359 HeaderInfo.MarkFileIncludeOnce(getCurrentFileLexer()->getFileEntry()); 360 } 361 362 void Preprocessor::HandlePragmaMark() { 363 assert(CurPPLexer && "No current lexer?"); 364 if (CurLexer) 365 CurLexer->ReadToEndOfLine(); 366 else 367 CurPTHLexer->DiscardToEndOfLine(); 368 } 369 370 371 /// HandlePragmaPoison - Handle \#pragma GCC poison. PoisonTok is the 'poison'. 372 /// 373 void Preprocessor::HandlePragmaPoison(Token &PoisonTok) { 374 Token Tok; 375 376 while (1) { 377 // Read the next token to poison. While doing this, pretend that we are 378 // skipping while reading the identifier to poison. 379 // This avoids errors on code like: 380 // #pragma GCC poison X 381 // #pragma GCC poison X 382 if (CurPPLexer) CurPPLexer->LexingRawMode = true; 383 LexUnexpandedToken(Tok); 384 if (CurPPLexer) CurPPLexer->LexingRawMode = false; 385 386 // If we reached the end of line, we're done. 387 if (Tok.is(tok::eod)) return; 388 389 // Can only poison identifiers. 390 if (Tok.isNot(tok::raw_identifier)) { 391 Diag(Tok, diag::err_pp_invalid_poison); 392 return; 393 } 394 395 // Look up the identifier info for the token. We disabled identifier lookup 396 // by saying we're skipping contents, so we need to do this manually. 397 IdentifierInfo *II = LookUpIdentifierInfo(Tok); 398 399 // Already poisoned. 400 if (II->isPoisoned()) continue; 401 402 // If this is a macro identifier, emit a warning. 403 if (II->hasMacroDefinition()) 404 Diag(Tok, diag::pp_poisoning_existing_macro); 405 406 // Finally, poison it! 407 II->setIsPoisoned(); 408 if (II->isFromAST()) 409 II->setChangedSinceDeserialization(); 410 } 411 } 412 413 /// HandlePragmaSystemHeader - Implement \#pragma GCC system_header. We know 414 /// that the whole directive has been parsed. 415 void Preprocessor::HandlePragmaSystemHeader(Token &SysHeaderTok) { 416 if (isInPrimaryFile()) { 417 Diag(SysHeaderTok, diag::pp_pragma_sysheader_in_main_file); 418 return; 419 } 420 421 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc. 422 PreprocessorLexer *TheLexer = getCurrentFileLexer(); 423 424 // Mark the file as a system header. 425 HeaderInfo.MarkFileSystemHeader(TheLexer->getFileEntry()); 426 427 428 PresumedLoc PLoc = SourceMgr.getPresumedLoc(SysHeaderTok.getLocation()); 429 if (PLoc.isInvalid()) 430 return; 431 432 unsigned FilenameID = SourceMgr.getLineTableFilenameID(PLoc.getFilename()); 433 434 // Notify the client, if desired, that we are in a new source file. 435 if (Callbacks) 436 Callbacks->FileChanged(SysHeaderTok.getLocation(), 437 PPCallbacks::SystemHeaderPragma, SrcMgr::C_System); 438 439 // Emit a line marker. This will change any source locations from this point 440 // forward to realize they are in a system header. 441 // Create a line note with this information. 442 SourceMgr.AddLineNote(SysHeaderTok.getLocation(), PLoc.getLine()+1, 443 FilenameID, /*IsEntry=*/false, /*IsExit=*/false, 444 /*IsSystem=*/true, /*IsExternC=*/false); 445 } 446 447 /// HandlePragmaDependency - Handle \#pragma GCC dependency "foo" blah. 448 /// 449 void Preprocessor::HandlePragmaDependency(Token &DependencyTok) { 450 Token FilenameTok; 451 CurPPLexer->LexIncludeFilename(FilenameTok); 452 453 // If the token kind is EOD, the error has already been diagnosed. 454 if (FilenameTok.is(tok::eod)) 455 return; 456 457 // Reserve a buffer to get the spelling. 458 SmallString<128> FilenameBuffer; 459 bool Invalid = false; 460 StringRef Filename = getSpelling(FilenameTok, FilenameBuffer, &Invalid); 461 if (Invalid) 462 return; 463 464 bool isAngled = 465 GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename); 466 // If GetIncludeFilenameSpelling set the start ptr to null, there was an 467 // error. 468 if (Filename.empty()) 469 return; 470 471 // Search include directories for this file. 472 const DirectoryLookup *CurDir; 473 const FileEntry *File = LookupFile(FilenameTok.getLocation(), Filename, 474 isAngled, 0, CurDir, NULL, NULL, NULL); 475 if (File == 0) { 476 if (!SuppressIncludeNotFoundError) 477 Diag(FilenameTok, diag::err_pp_file_not_found) << Filename; 478 return; 479 } 480 481 const FileEntry *CurFile = getCurrentFileLexer()->getFileEntry(); 482 483 // If this file is older than the file it depends on, emit a diagnostic. 484 if (CurFile && CurFile->getModificationTime() < File->getModificationTime()) { 485 // Lex tokens at the end of the message and include them in the message. 486 std::string Message; 487 Lex(DependencyTok); 488 while (DependencyTok.isNot(tok::eod)) { 489 Message += getSpelling(DependencyTok) + " "; 490 Lex(DependencyTok); 491 } 492 493 // Remove the trailing ' ' if present. 494 if (!Message.empty()) 495 Message.erase(Message.end()-1); 496 Diag(FilenameTok, diag::pp_out_of_date_dependency) << Message; 497 } 498 } 499 500 /// ParsePragmaPushOrPopMacro - Handle parsing of pragma push_macro/pop_macro. 501 /// Return the IdentifierInfo* associated with the macro to push or pop. 502 IdentifierInfo *Preprocessor::ParsePragmaPushOrPopMacro(Token &Tok) { 503 // Remember the pragma token location. 504 Token PragmaTok = Tok; 505 506 // Read the '('. 507 Lex(Tok); 508 if (Tok.isNot(tok::l_paren)) { 509 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed) 510 << getSpelling(PragmaTok); 511 return 0; 512 } 513 514 // Read the macro name string. 515 Lex(Tok); 516 if (Tok.isNot(tok::string_literal)) { 517 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed) 518 << getSpelling(PragmaTok); 519 return 0; 520 } 521 522 if (Tok.hasUDSuffix()) { 523 Diag(Tok, diag::err_invalid_string_udl); 524 return 0; 525 } 526 527 // Remember the macro string. 528 std::string StrVal = getSpelling(Tok); 529 530 // Read the ')'. 531 Lex(Tok); 532 if (Tok.isNot(tok::r_paren)) { 533 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed) 534 << getSpelling(PragmaTok); 535 return 0; 536 } 537 538 assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' && 539 "Invalid string token!"); 540 541 // Create a Token from the string. 542 Token MacroTok; 543 MacroTok.startToken(); 544 MacroTok.setKind(tok::raw_identifier); 545 CreateString(StringRef(&StrVal[1], StrVal.size() - 2), MacroTok); 546 547 // Get the IdentifierInfo of MacroToPushTok. 548 return LookUpIdentifierInfo(MacroTok); 549 } 550 551 /// \brief Handle \#pragma push_macro. 552 /// 553 /// The syntax is: 554 /// \code 555 /// #pragma push_macro("macro") 556 /// \endcode 557 void Preprocessor::HandlePragmaPushMacro(Token &PushMacroTok) { 558 // Parse the pragma directive and get the macro IdentifierInfo*. 559 IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PushMacroTok); 560 if (!IdentInfo) return; 561 562 // Get the MacroInfo associated with IdentInfo. 563 MacroInfo *MI = getMacroInfo(IdentInfo); 564 565 if (MI) { 566 // Allow the original MacroInfo to be redefined later. 567 MI->setIsAllowRedefinitionsWithoutWarning(true); 568 } 569 570 // Push the cloned MacroInfo so we can retrieve it later. 571 PragmaPushMacroInfo[IdentInfo].push_back(MI); 572 } 573 574 /// \brief Handle \#pragma pop_macro. 575 /// 576 /// The syntax is: 577 /// \code 578 /// #pragma pop_macro("macro") 579 /// \endcode 580 void Preprocessor::HandlePragmaPopMacro(Token &PopMacroTok) { 581 SourceLocation MessageLoc = PopMacroTok.getLocation(); 582 583 // Parse the pragma directive and get the macro IdentifierInfo*. 584 IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PopMacroTok); 585 if (!IdentInfo) return; 586 587 // Find the vector<MacroInfo*> associated with the macro. 588 llvm::DenseMap<IdentifierInfo*, std::vector<MacroInfo*> >::iterator iter = 589 PragmaPushMacroInfo.find(IdentInfo); 590 if (iter != PragmaPushMacroInfo.end()) { 591 // Forget the MacroInfo currently associated with IdentInfo. 592 if (MacroDirective *CurrentMD = getMacroDirective(IdentInfo)) { 593 MacroInfo *MI = CurrentMD->getMacroInfo(); 594 if (MI->isWarnIfUnused()) 595 WarnUnusedMacroLocs.erase(MI->getDefinitionLoc()); 596 appendMacroDirective(IdentInfo, AllocateUndefMacroDirective(MessageLoc)); 597 } 598 599 // Get the MacroInfo we want to reinstall. 600 MacroInfo *MacroToReInstall = iter->second.back(); 601 602 if (MacroToReInstall) { 603 // Reinstall the previously pushed macro. 604 appendDefMacroDirective(IdentInfo, MacroToReInstall, MessageLoc, 605 /*isImported=*/false); 606 } 607 608 // Pop PragmaPushMacroInfo stack. 609 iter->second.pop_back(); 610 if (iter->second.size() == 0) 611 PragmaPushMacroInfo.erase(iter); 612 } else { 613 Diag(MessageLoc, diag::warn_pragma_pop_macro_no_push) 614 << IdentInfo->getName(); 615 } 616 } 617 618 void Preprocessor::HandlePragmaIncludeAlias(Token &Tok) { 619 // We will either get a quoted filename or a bracketed filename, and we 620 // have to track which we got. The first filename is the source name, 621 // and the second name is the mapped filename. If the first is quoted, 622 // the second must be as well (cannot mix and match quotes and brackets). 623 624 // Get the open paren 625 Lex(Tok); 626 if (Tok.isNot(tok::l_paren)) { 627 Diag(Tok, diag::warn_pragma_include_alias_expected) << "("; 628 return; 629 } 630 631 // We expect either a quoted string literal, or a bracketed name 632 Token SourceFilenameTok; 633 CurPPLexer->LexIncludeFilename(SourceFilenameTok); 634 if (SourceFilenameTok.is(tok::eod)) { 635 // The diagnostic has already been handled 636 return; 637 } 638 639 StringRef SourceFileName; 640 SmallString<128> FileNameBuffer; 641 if (SourceFilenameTok.is(tok::string_literal) || 642 SourceFilenameTok.is(tok::angle_string_literal)) { 643 SourceFileName = getSpelling(SourceFilenameTok, FileNameBuffer); 644 } else if (SourceFilenameTok.is(tok::less)) { 645 // This could be a path instead of just a name 646 FileNameBuffer.push_back('<'); 647 SourceLocation End; 648 if (ConcatenateIncludeName(FileNameBuffer, End)) 649 return; // Diagnostic already emitted 650 SourceFileName = FileNameBuffer.str(); 651 } else { 652 Diag(Tok, diag::warn_pragma_include_alias_expected_filename); 653 return; 654 } 655 FileNameBuffer.clear(); 656 657 // Now we expect a comma, followed by another include name 658 Lex(Tok); 659 if (Tok.isNot(tok::comma)) { 660 Diag(Tok, diag::warn_pragma_include_alias_expected) << ","; 661 return; 662 } 663 664 Token ReplaceFilenameTok; 665 CurPPLexer->LexIncludeFilename(ReplaceFilenameTok); 666 if (ReplaceFilenameTok.is(tok::eod)) { 667 // The diagnostic has already been handled 668 return; 669 } 670 671 StringRef ReplaceFileName; 672 if (ReplaceFilenameTok.is(tok::string_literal) || 673 ReplaceFilenameTok.is(tok::angle_string_literal)) { 674 ReplaceFileName = getSpelling(ReplaceFilenameTok, FileNameBuffer); 675 } else if (ReplaceFilenameTok.is(tok::less)) { 676 // This could be a path instead of just a name 677 FileNameBuffer.push_back('<'); 678 SourceLocation End; 679 if (ConcatenateIncludeName(FileNameBuffer, End)) 680 return; // Diagnostic already emitted 681 ReplaceFileName = FileNameBuffer.str(); 682 } else { 683 Diag(Tok, diag::warn_pragma_include_alias_expected_filename); 684 return; 685 } 686 687 // Finally, we expect the closing paren 688 Lex(Tok); 689 if (Tok.isNot(tok::r_paren)) { 690 Diag(Tok, diag::warn_pragma_include_alias_expected) << ")"; 691 return; 692 } 693 694 // Now that we have the source and target filenames, we need to make sure 695 // they're both of the same type (angled vs non-angled) 696 StringRef OriginalSource = SourceFileName; 697 698 bool SourceIsAngled = 699 GetIncludeFilenameSpelling(SourceFilenameTok.getLocation(), 700 SourceFileName); 701 bool ReplaceIsAngled = 702 GetIncludeFilenameSpelling(ReplaceFilenameTok.getLocation(), 703 ReplaceFileName); 704 if (!SourceFileName.empty() && !ReplaceFileName.empty() && 705 (SourceIsAngled != ReplaceIsAngled)) { 706 unsigned int DiagID; 707 if (SourceIsAngled) 708 DiagID = diag::warn_pragma_include_alias_mismatch_angle; 709 else 710 DiagID = diag::warn_pragma_include_alias_mismatch_quote; 711 712 Diag(SourceFilenameTok.getLocation(), DiagID) 713 << SourceFileName 714 << ReplaceFileName; 715 716 return; 717 } 718 719 // Now we can let the include handler know about this mapping 720 getHeaderSearchInfo().AddIncludeAlias(OriginalSource, ReplaceFileName); 721 } 722 723 /// AddPragmaHandler - Add the specified pragma handler to the preprocessor. 724 /// If 'Namespace' is non-null, then it is a token required to exist on the 725 /// pragma line before the pragma string starts, e.g. "STDC" or "GCC". 726 void Preprocessor::AddPragmaHandler(StringRef Namespace, 727 PragmaHandler *Handler) { 728 PragmaNamespace *InsertNS = PragmaHandlers; 729 730 // If this is specified to be in a namespace, step down into it. 731 if (!Namespace.empty()) { 732 // If there is already a pragma handler with the name of this namespace, 733 // we either have an error (directive with the same name as a namespace) or 734 // we already have the namespace to insert into. 735 if (PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace)) { 736 InsertNS = Existing->getIfNamespace(); 737 assert(InsertNS != 0 && "Cannot have a pragma namespace and pragma" 738 " handler with the same name!"); 739 } else { 740 // Otherwise, this namespace doesn't exist yet, create and insert the 741 // handler for it. 742 InsertNS = new PragmaNamespace(Namespace); 743 PragmaHandlers->AddPragma(InsertNS); 744 } 745 } 746 747 // Check to make sure we don't already have a pragma for this identifier. 748 assert(!InsertNS->FindHandler(Handler->getName()) && 749 "Pragma handler already exists for this identifier!"); 750 InsertNS->AddPragma(Handler); 751 } 752 753 /// RemovePragmaHandler - Remove the specific pragma handler from the 754 /// preprocessor. If \arg Namespace is non-null, then it should be the 755 /// namespace that \arg Handler was added to. It is an error to remove 756 /// a handler that has not been registered. 757 void Preprocessor::RemovePragmaHandler(StringRef Namespace, 758 PragmaHandler *Handler) { 759 PragmaNamespace *NS = PragmaHandlers; 760 761 // If this is specified to be in a namespace, step down into it. 762 if (!Namespace.empty()) { 763 PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace); 764 assert(Existing && "Namespace containing handler does not exist!"); 765 766 NS = Existing->getIfNamespace(); 767 assert(NS && "Invalid namespace, registered as a regular pragma handler!"); 768 } 769 770 NS->RemovePragmaHandler(Handler); 771 772 // If this is a non-default namespace and it is now empty, remove 773 // it. 774 if (NS != PragmaHandlers && NS->IsEmpty()) { 775 PragmaHandlers->RemovePragmaHandler(NS); 776 delete NS; 777 } 778 } 779 780 bool Preprocessor::LexOnOffSwitch(tok::OnOffSwitch &Result) { 781 Token Tok; 782 LexUnexpandedToken(Tok); 783 784 if (Tok.isNot(tok::identifier)) { 785 Diag(Tok, diag::ext_on_off_switch_syntax); 786 return true; 787 } 788 IdentifierInfo *II = Tok.getIdentifierInfo(); 789 if (II->isStr("ON")) 790 Result = tok::OOS_ON; 791 else if (II->isStr("OFF")) 792 Result = tok::OOS_OFF; 793 else if (II->isStr("DEFAULT")) 794 Result = tok::OOS_DEFAULT; 795 else { 796 Diag(Tok, diag::ext_on_off_switch_syntax); 797 return true; 798 } 799 800 // Verify that this is followed by EOD. 801 LexUnexpandedToken(Tok); 802 if (Tok.isNot(tok::eod)) 803 Diag(Tok, diag::ext_pragma_syntax_eod); 804 return false; 805 } 806 807 namespace { 808 /// PragmaOnceHandler - "\#pragma once" marks the file as atomically included. 809 struct PragmaOnceHandler : public PragmaHandler { 810 PragmaOnceHandler() : PragmaHandler("once") {} 811 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, 812 Token &OnceTok) { 813 PP.CheckEndOfDirective("pragma once"); 814 PP.HandlePragmaOnce(OnceTok); 815 } 816 }; 817 818 /// PragmaMarkHandler - "\#pragma mark ..." is ignored by the compiler, and the 819 /// rest of the line is not lexed. 820 struct PragmaMarkHandler : public PragmaHandler { 821 PragmaMarkHandler() : PragmaHandler("mark") {} 822 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, 823 Token &MarkTok) { 824 PP.HandlePragmaMark(); 825 } 826 }; 827 828 /// PragmaPoisonHandler - "\#pragma poison x" marks x as not usable. 829 struct PragmaPoisonHandler : public PragmaHandler { 830 PragmaPoisonHandler() : PragmaHandler("poison") {} 831 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, 832 Token &PoisonTok) { 833 PP.HandlePragmaPoison(PoisonTok); 834 } 835 }; 836 837 /// PragmaSystemHeaderHandler - "\#pragma system_header" marks the current file 838 /// as a system header, which silences warnings in it. 839 struct PragmaSystemHeaderHandler : public PragmaHandler { 840 PragmaSystemHeaderHandler() : PragmaHandler("system_header") {} 841 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, 842 Token &SHToken) { 843 PP.HandlePragmaSystemHeader(SHToken); 844 PP.CheckEndOfDirective("pragma"); 845 } 846 }; 847 struct PragmaDependencyHandler : public PragmaHandler { 848 PragmaDependencyHandler() : PragmaHandler("dependency") {} 849 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, 850 Token &DepToken) { 851 PP.HandlePragmaDependency(DepToken); 852 } 853 }; 854 855 struct PragmaDebugHandler : public PragmaHandler { 856 PragmaDebugHandler() : PragmaHandler("__debug") {} 857 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, 858 Token &DepToken) { 859 Token Tok; 860 PP.LexUnexpandedToken(Tok); 861 if (Tok.isNot(tok::identifier)) { 862 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid); 863 return; 864 } 865 IdentifierInfo *II = Tok.getIdentifierInfo(); 866 867 if (II->isStr("assert")) { 868 llvm_unreachable("This is an assertion!"); 869 } else if (II->isStr("crash")) { 870 LLVM_BUILTIN_TRAP; 871 } else if (II->isStr("parser_crash")) { 872 Token Crasher; 873 Crasher.setKind(tok::annot_pragma_parser_crash); 874 PP.EnterToken(Crasher); 875 } else if (II->isStr("llvm_fatal_error")) { 876 llvm::report_fatal_error("#pragma clang __debug llvm_fatal_error"); 877 } else if (II->isStr("llvm_unreachable")) { 878 llvm_unreachable("#pragma clang __debug llvm_unreachable"); 879 } else if (II->isStr("overflow_stack")) { 880 DebugOverflowStack(); 881 } else if (II->isStr("handle_crash")) { 882 llvm::CrashRecoveryContext *CRC =llvm::CrashRecoveryContext::GetCurrent(); 883 if (CRC) 884 CRC->HandleCrash(); 885 } else if (II->isStr("captured")) { 886 HandleCaptured(PP); 887 } else { 888 PP.Diag(Tok, diag::warn_pragma_debug_unexpected_command) 889 << II->getName(); 890 } 891 892 PPCallbacks *Callbacks = PP.getPPCallbacks(); 893 if (Callbacks) 894 Callbacks->PragmaDebug(Tok.getLocation(), II->getName()); 895 } 896 897 void HandleCaptured(Preprocessor &PP) { 898 // Skip if emitting preprocessed output. 899 if (PP.isPreprocessedOutput()) 900 return; 901 902 Token Tok; 903 PP.LexUnexpandedToken(Tok); 904 905 if (Tok.isNot(tok::eod)) { 906 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) 907 << "pragma clang __debug captured"; 908 return; 909 } 910 911 SourceLocation NameLoc = Tok.getLocation(); 912 Token *Toks = PP.getPreprocessorAllocator().Allocate<Token>(1); 913 Toks->startToken(); 914 Toks->setKind(tok::annot_pragma_captured); 915 Toks->setLocation(NameLoc); 916 917 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true, 918 /*OwnsTokens=*/false); 919 } 920 921 // Disable MSVC warning about runtime stack overflow. 922 #ifdef _MSC_VER 923 #pragma warning(disable : 4717) 924 #endif 925 void DebugOverflowStack() { 926 DebugOverflowStack(); 927 } 928 #ifdef _MSC_VER 929 #pragma warning(default : 4717) 930 #endif 931 932 }; 933 934 /// PragmaDiagnosticHandler - e.g. '\#pragma GCC diagnostic ignored "-Wformat"' 935 struct PragmaDiagnosticHandler : public PragmaHandler { 936 private: 937 const char *Namespace; 938 public: 939 explicit PragmaDiagnosticHandler(const char *NS) : 940 PragmaHandler("diagnostic"), Namespace(NS) {} 941 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, 942 Token &DiagToken) { 943 SourceLocation DiagLoc = DiagToken.getLocation(); 944 Token Tok; 945 PP.LexUnexpandedToken(Tok); 946 if (Tok.isNot(tok::identifier)) { 947 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid); 948 return; 949 } 950 IdentifierInfo *II = Tok.getIdentifierInfo(); 951 PPCallbacks *Callbacks = PP.getPPCallbacks(); 952 953 diag::Mapping Map; 954 if (II->isStr("warning")) 955 Map = diag::MAP_WARNING; 956 else if (II->isStr("error")) 957 Map = diag::MAP_ERROR; 958 else if (II->isStr("ignored")) 959 Map = diag::MAP_IGNORE; 960 else if (II->isStr("fatal")) 961 Map = diag::MAP_FATAL; 962 else if (II->isStr("pop")) { 963 if (!PP.getDiagnostics().popMappings(DiagLoc)) 964 PP.Diag(Tok, diag::warn_pragma_diagnostic_cannot_pop); 965 else if (Callbacks) 966 Callbacks->PragmaDiagnosticPop(DiagLoc, Namespace); 967 return; 968 } else if (II->isStr("push")) { 969 PP.getDiagnostics().pushMappings(DiagLoc); 970 if (Callbacks) 971 Callbacks->PragmaDiagnosticPush(DiagLoc, Namespace); 972 return; 973 } else { 974 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid); 975 return; 976 } 977 978 PP.LexUnexpandedToken(Tok); 979 SourceLocation StringLoc = Tok.getLocation(); 980 981 std::string WarningName; 982 if (!PP.FinishLexStringLiteral(Tok, WarningName, "pragma diagnostic", 983 /*MacroExpansion=*/false)) 984 return; 985 986 if (Tok.isNot(tok::eod)) { 987 PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token); 988 return; 989 } 990 991 if (WarningName.size() < 3 || WarningName[0] != '-' || 992 WarningName[1] != 'W') { 993 PP.Diag(StringLoc, diag::warn_pragma_diagnostic_invalid_option); 994 return; 995 } 996 997 if (PP.getDiagnostics().setDiagnosticGroupMapping(WarningName.substr(2), 998 Map, DiagLoc)) 999 PP.Diag(StringLoc, diag::warn_pragma_diagnostic_unknown_warning) 1000 << WarningName; 1001 else if (Callbacks) 1002 Callbacks->PragmaDiagnostic(DiagLoc, Namespace, Map, WarningName); 1003 } 1004 }; 1005 1006 /// PragmaIncludeAliasHandler - "\#pragma include_alias("...")". 1007 struct PragmaIncludeAliasHandler : public PragmaHandler { 1008 PragmaIncludeAliasHandler() : PragmaHandler("include_alias") {} 1009 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, 1010 Token &IncludeAliasTok) { 1011 PP.HandlePragmaIncludeAlias(IncludeAliasTok); 1012 } 1013 }; 1014 1015 /// PragmaMessageHandler - Handle the microsoft and gcc \#pragma message 1016 /// extension. The syntax is: 1017 /// \code 1018 /// #pragma message(string) 1019 /// \endcode 1020 /// OR, in GCC mode: 1021 /// \code 1022 /// #pragma message string 1023 /// \endcode 1024 /// string is a string, which is fully macro expanded, and permits string 1025 /// concatenation, embedded escape characters, etc... See MSDN for more details. 1026 /// Also handles \#pragma GCC warning and \#pragma GCC error which take the same 1027 /// form as \#pragma message. 1028 struct PragmaMessageHandler : public PragmaHandler { 1029 private: 1030 const PPCallbacks::PragmaMessageKind Kind; 1031 const StringRef Namespace; 1032 1033 static const char* PragmaKind(PPCallbacks::PragmaMessageKind Kind, 1034 bool PragmaNameOnly = false) { 1035 switch (Kind) { 1036 case PPCallbacks::PMK_Message: 1037 return PragmaNameOnly ? "message" : "pragma message"; 1038 case PPCallbacks::PMK_Warning: 1039 return PragmaNameOnly ? "warning" : "pragma warning"; 1040 case PPCallbacks::PMK_Error: 1041 return PragmaNameOnly ? "error" : "pragma error"; 1042 } 1043 llvm_unreachable("Unknown PragmaMessageKind!"); 1044 } 1045 1046 public: 1047 PragmaMessageHandler(PPCallbacks::PragmaMessageKind Kind, 1048 StringRef Namespace = StringRef()) 1049 : PragmaHandler(PragmaKind(Kind, true)), Kind(Kind), Namespace(Namespace) {} 1050 1051 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, 1052 Token &Tok) { 1053 SourceLocation MessageLoc = Tok.getLocation(); 1054 PP.Lex(Tok); 1055 bool ExpectClosingParen = false; 1056 switch (Tok.getKind()) { 1057 case tok::l_paren: 1058 // We have a MSVC style pragma message. 1059 ExpectClosingParen = true; 1060 // Read the string. 1061 PP.Lex(Tok); 1062 break; 1063 case tok::string_literal: 1064 // We have a GCC style pragma message, and we just read the string. 1065 break; 1066 default: 1067 PP.Diag(MessageLoc, diag::err_pragma_message_malformed) << Kind; 1068 return; 1069 } 1070 1071 std::string MessageString; 1072 if (!PP.FinishLexStringLiteral(Tok, MessageString, PragmaKind(Kind), 1073 /*MacroExpansion=*/true)) 1074 return; 1075 1076 if (ExpectClosingParen) { 1077 if (Tok.isNot(tok::r_paren)) { 1078 PP.Diag(Tok.getLocation(), diag::err_pragma_message_malformed) << Kind; 1079 return; 1080 } 1081 PP.Lex(Tok); // eat the r_paren. 1082 } 1083 1084 if (Tok.isNot(tok::eod)) { 1085 PP.Diag(Tok.getLocation(), diag::err_pragma_message_malformed) << Kind; 1086 return; 1087 } 1088 1089 // Output the message. 1090 PP.Diag(MessageLoc, (Kind == PPCallbacks::PMK_Error) 1091 ? diag::err_pragma_message 1092 : diag::warn_pragma_message) << MessageString; 1093 1094 // If the pragma is lexically sound, notify any interested PPCallbacks. 1095 if (PPCallbacks *Callbacks = PP.getPPCallbacks()) 1096 Callbacks->PragmaMessage(MessageLoc, Namespace, Kind, MessageString); 1097 } 1098 }; 1099 1100 /// PragmaPushMacroHandler - "\#pragma push_macro" saves the value of the 1101 /// macro on the top of the stack. 1102 struct PragmaPushMacroHandler : public PragmaHandler { 1103 PragmaPushMacroHandler() : PragmaHandler("push_macro") {} 1104 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, 1105 Token &PushMacroTok) { 1106 PP.HandlePragmaPushMacro(PushMacroTok); 1107 } 1108 }; 1109 1110 1111 /// PragmaPopMacroHandler - "\#pragma pop_macro" sets the value of the 1112 /// macro to the value on the top of the stack. 1113 struct PragmaPopMacroHandler : public PragmaHandler { 1114 PragmaPopMacroHandler() : PragmaHandler("pop_macro") {} 1115 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, 1116 Token &PopMacroTok) { 1117 PP.HandlePragmaPopMacro(PopMacroTok); 1118 } 1119 }; 1120 1121 // Pragma STDC implementations. 1122 1123 /// PragmaSTDC_FENV_ACCESSHandler - "\#pragma STDC FENV_ACCESS ...". 1124 struct PragmaSTDC_FENV_ACCESSHandler : public PragmaHandler { 1125 PragmaSTDC_FENV_ACCESSHandler() : PragmaHandler("FENV_ACCESS") {} 1126 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, 1127 Token &Tok) { 1128 tok::OnOffSwitch OOS; 1129 if (PP.LexOnOffSwitch(OOS)) 1130 return; 1131 if (OOS == tok::OOS_ON) 1132 PP.Diag(Tok, diag::warn_stdc_fenv_access_not_supported); 1133 } 1134 }; 1135 1136 /// PragmaSTDC_CX_LIMITED_RANGEHandler - "\#pragma STDC CX_LIMITED_RANGE ...". 1137 struct PragmaSTDC_CX_LIMITED_RANGEHandler : public PragmaHandler { 1138 PragmaSTDC_CX_LIMITED_RANGEHandler() 1139 : PragmaHandler("CX_LIMITED_RANGE") {} 1140 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, 1141 Token &Tok) { 1142 tok::OnOffSwitch OOS; 1143 PP.LexOnOffSwitch(OOS); 1144 } 1145 }; 1146 1147 /// PragmaSTDC_UnknownHandler - "\#pragma STDC ...". 1148 struct PragmaSTDC_UnknownHandler : public PragmaHandler { 1149 PragmaSTDC_UnknownHandler() {} 1150 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, 1151 Token &UnknownTok) { 1152 // C99 6.10.6p2, unknown forms are not allowed. 1153 PP.Diag(UnknownTok, diag::ext_stdc_pragma_ignored); 1154 } 1155 }; 1156 1157 /// PragmaARCCFCodeAuditedHandler - 1158 /// \#pragma clang arc_cf_code_audited begin/end 1159 struct PragmaARCCFCodeAuditedHandler : public PragmaHandler { 1160 PragmaARCCFCodeAuditedHandler() : PragmaHandler("arc_cf_code_audited") {} 1161 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, 1162 Token &NameTok) { 1163 SourceLocation Loc = NameTok.getLocation(); 1164 bool IsBegin; 1165 1166 Token Tok; 1167 1168 // Lex the 'begin' or 'end'. 1169 PP.LexUnexpandedToken(Tok); 1170 const IdentifierInfo *BeginEnd = Tok.getIdentifierInfo(); 1171 if (BeginEnd && BeginEnd->isStr("begin")) { 1172 IsBegin = true; 1173 } else if (BeginEnd && BeginEnd->isStr("end")) { 1174 IsBegin = false; 1175 } else { 1176 PP.Diag(Tok.getLocation(), diag::err_pp_arc_cf_code_audited_syntax); 1177 return; 1178 } 1179 1180 // Verify that this is followed by EOD. 1181 PP.LexUnexpandedToken(Tok); 1182 if (Tok.isNot(tok::eod)) 1183 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma"; 1184 1185 // The start location of the active audit. 1186 SourceLocation BeginLoc = PP.getPragmaARCCFCodeAuditedLoc(); 1187 1188 // The start location we want after processing this. 1189 SourceLocation NewLoc; 1190 1191 if (IsBegin) { 1192 // Complain about attempts to re-enter an audit. 1193 if (BeginLoc.isValid()) { 1194 PP.Diag(Loc, diag::err_pp_double_begin_of_arc_cf_code_audited); 1195 PP.Diag(BeginLoc, diag::note_pragma_entered_here); 1196 } 1197 NewLoc = Loc; 1198 } else { 1199 // Complain about attempts to leave an audit that doesn't exist. 1200 if (!BeginLoc.isValid()) { 1201 PP.Diag(Loc, diag::err_pp_unmatched_end_of_arc_cf_code_audited); 1202 return; 1203 } 1204 NewLoc = SourceLocation(); 1205 } 1206 1207 PP.setPragmaARCCFCodeAuditedLoc(NewLoc); 1208 } 1209 }; 1210 1211 /// \brief Handle "\#pragma region [...]" 1212 /// 1213 /// The syntax is 1214 /// \code 1215 /// #pragma region [optional name] 1216 /// #pragma endregion [optional comment] 1217 /// \endcode 1218 /// 1219 /// \note This is 1220 /// <a href="http://msdn.microsoft.com/en-us/library/b6xkz944(v=vs.80).aspx">editor-only</a> 1221 /// pragma, just skipped by compiler. 1222 struct PragmaRegionHandler : public PragmaHandler { 1223 PragmaRegionHandler(const char *pragma) : PragmaHandler(pragma) { } 1224 1225 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, 1226 Token &NameTok) { 1227 // #pragma region: endregion matches can be verified 1228 // __pragma(region): no sense, but ignored by msvc 1229 // _Pragma is not valid for MSVC, but there isn't any point 1230 // to handle a _Pragma differently. 1231 } 1232 }; 1233 1234 } // end anonymous namespace 1235 1236 1237 /// RegisterBuiltinPragmas - Install the standard preprocessor pragmas: 1238 /// \#pragma GCC poison/system_header/dependency and \#pragma once. 1239 void Preprocessor::RegisterBuiltinPragmas() { 1240 AddPragmaHandler(new PragmaOnceHandler()); 1241 AddPragmaHandler(new PragmaMarkHandler()); 1242 AddPragmaHandler(new PragmaPushMacroHandler()); 1243 AddPragmaHandler(new PragmaPopMacroHandler()); 1244 AddPragmaHandler(new PragmaMessageHandler(PPCallbacks::PMK_Message)); 1245 1246 // #pragma GCC ... 1247 AddPragmaHandler("GCC", new PragmaPoisonHandler()); 1248 AddPragmaHandler("GCC", new PragmaSystemHeaderHandler()); 1249 AddPragmaHandler("GCC", new PragmaDependencyHandler()); 1250 AddPragmaHandler("GCC", new PragmaDiagnosticHandler("GCC")); 1251 AddPragmaHandler("GCC", new PragmaMessageHandler(PPCallbacks::PMK_Warning, 1252 "GCC")); 1253 AddPragmaHandler("GCC", new PragmaMessageHandler(PPCallbacks::PMK_Error, 1254 "GCC")); 1255 // #pragma clang ... 1256 AddPragmaHandler("clang", new PragmaPoisonHandler()); 1257 AddPragmaHandler("clang", new PragmaSystemHeaderHandler()); 1258 AddPragmaHandler("clang", new PragmaDebugHandler()); 1259 AddPragmaHandler("clang", new PragmaDependencyHandler()); 1260 AddPragmaHandler("clang", new PragmaDiagnosticHandler("clang")); 1261 AddPragmaHandler("clang", new PragmaARCCFCodeAuditedHandler()); 1262 1263 AddPragmaHandler("STDC", new PragmaSTDC_FENV_ACCESSHandler()); 1264 AddPragmaHandler("STDC", new PragmaSTDC_CX_LIMITED_RANGEHandler()); 1265 AddPragmaHandler("STDC", new PragmaSTDC_UnknownHandler()); 1266 1267 // MS extensions. 1268 if (LangOpts.MicrosoftExt) { 1269 AddPragmaHandler(new PragmaIncludeAliasHandler()); 1270 AddPragmaHandler(new PragmaRegionHandler("region")); 1271 AddPragmaHandler(new PragmaRegionHandler("endregion")); 1272 } 1273 } 1274