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