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