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