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