1 //===- Pragma.cpp - Pragma registration and handling ----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the PragmaHandler/PragmaTable interfaces and implements
11 // pragma related methods of the Preprocessor class.
13 //===----------------------------------------------------------------------===//
15 #include "clang/Lex/Pragma.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/Token.h"
35 #include "clang/Lex/TokenLexer.h"
36 #include "llvm/ADT/ArrayRef.h"
37 #include "llvm/ADT/DenseMap.h"
38 #include "llvm/ADT/STLExtras.h"
39 #include "llvm/ADT/SmallString.h"
40 #include "llvm/ADT/SmallVector.h"
41 #include "llvm/ADT/StringSwitch.h"
42 #include "llvm/ADT/StringRef.h"
43 #include "llvm/Support/CrashRecoveryContext.h"
44 #include "llvm/Support/Compiler.h"
45 #include "llvm/Support/ErrorHandling.h"
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 PragmaIntroducerKind Introducer,
70 //===----------------------------------------------------------------------===//
71 // PragmaNamespace Implementation.
72 //===----------------------------------------------------------------------===//
74 PragmaNamespace::~PragmaNamespace() {
75 llvm::DeleteContainerSeconds(Handlers);
78 /// FindHandler - Check to see if there is already a handler for the
79 /// specified name. If not, return the handler for the null identifier if it
80 /// exists, otherwise return null. If IgnoreNull is true (the default) then
81 /// the null handler isn't returned on failure to match.
82 PragmaHandler *PragmaNamespace::FindHandler(StringRef Name,
83 bool IgnoreNull) const {
84 if (PragmaHandler *Handler = Handlers.lookup(Name))
86 return IgnoreNull ? nullptr : Handlers.lookup(StringRef());
89 void PragmaNamespace::AddPragma(PragmaHandler *Handler) {
90 assert(!Handlers.lookup(Handler->getName()) &&
91 "A handler with this name is already registered in this namespace");
92 Handlers[Handler->getName()] = Handler;
95 void PragmaNamespace::RemovePragmaHandler(PragmaHandler *Handler) {
96 assert(Handlers.lookup(Handler->getName()) &&
97 "Handler not registered in this namespace");
98 Handlers.erase(Handler->getName());
101 void PragmaNamespace::HandlePragma(Preprocessor &PP,
102 PragmaIntroducerKind Introducer,
104 // Read the 'namespace' that the directive is in, e.g. STDC. Do not macro
105 // expand it, the user can have a STDC #define, that should not affect this.
106 PP.LexUnexpandedToken(Tok);
108 // Get the handler for this token. If there is no handler, ignore the pragma.
109 PragmaHandler *Handler
110 = FindHandler(Tok.getIdentifierInfo() ? Tok.getIdentifierInfo()->getName()
112 /*IgnoreNull=*/false);
114 PP.Diag(Tok, diag::warn_pragma_ignored);
118 // Otherwise, pass it down.
119 Handler->HandlePragma(PP, Introducer, Tok);
122 //===----------------------------------------------------------------------===//
123 // Preprocessor Pragma Directive Handling.
124 //===----------------------------------------------------------------------===//
126 /// HandlePragmaDirective - The "\#pragma" directive has been parsed. Lex the
127 /// rest of the pragma, passing it to the registered pragma handlers.
128 void Preprocessor::HandlePragmaDirective(SourceLocation IntroducerLoc,
129 PragmaIntroducerKind Introducer) {
131 Callbacks->PragmaDirective(IntroducerLoc, Introducer);
138 // Invoke the first level of pragma handlers which reads the namespace id.
140 PragmaHandlers->HandlePragma(*this, Introducer, Tok);
142 // If the pragma handler didn't read the rest of the line, consume it now.
143 if ((CurTokenLexer && CurTokenLexer->isParsingPreprocessorDirective())
144 || (CurPPLexer && CurPPLexer->ParsingPreprocessorDirective))
145 DiscardUntilEndOfDirective();
150 /// Helper class for \see Preprocessor::Handle_Pragma.
151 class LexingFor_PragmaRAII {
153 bool InMacroArgPreExpansion;
159 LexingFor_PragmaRAII(Preprocessor &PP, bool InMacroArgPreExpansion,
161 : PP(PP), InMacroArgPreExpansion(InMacroArgPreExpansion), OutTok(Tok) {
162 if (InMacroArgPreExpansion) {
164 PP.EnableBacktrackAtThisPos();
168 ~LexingFor_PragmaRAII() {
169 if (InMacroArgPreExpansion) {
170 // When committing/backtracking the cached pragma tokens in a macro
171 // argument pre-expansion we want to ensure that either the tokens which
172 // have been committed will be removed from the cache or that the tokens
173 // over which we just backtracked won't remain in the cache after they're
174 // consumed and that the caching will stop after consuming them.
175 // Otherwise the caching will interfere with the way macro expansion
176 // works, because we will continue to cache tokens after consuming the
177 // backtracked tokens, which shouldn't happen when we're dealing with
178 // macro argument pre-expansion.
179 auto CachedTokenRange = PP.LastCachedTokenRange();
181 PP.CommitBacktrackedTokens();
186 PP.EraseCachedTokens(CachedTokenRange);
197 /// Handle_Pragma - Read a _Pragma directive, slice it up, process it, then
198 /// return the first token after the directive. The _Pragma token has just
199 /// been read into 'Tok'.
200 void Preprocessor::Handle_Pragma(Token &Tok) {
201 // This works differently if we are pre-expanding a macro argument.
202 // In that case we don't actually "activate" the pragma now, we only lex it
203 // until we are sure it is lexically correct and then we backtrack so that
204 // we activate the pragma whenever we encounter the tokens again in the token
205 // stream. This ensures that we will activate it in the correct location
206 // or that we will ignore it if it never enters the token stream, e.g:
209 // #define INACTIVE(x) EMPTY(x)
210 // INACTIVE(_Pragma("clang diagnostic ignored \"-Wconversion\""))
212 LexingFor_PragmaRAII _PragmaLexing(*this, InMacroArgPreExpansion, Tok);
214 // Remember the pragma token location.
215 SourceLocation PragmaLoc = Tok.getLocation();
219 if (Tok.isNot(tok::l_paren)) {
220 Diag(PragmaLoc, diag::err__Pragma_malformed);
221 return _PragmaLexing.failed();
226 if (!tok::isStringLiteral(Tok.getKind())) {
227 Diag(PragmaLoc, diag::err__Pragma_malformed);
228 // Skip bad tokens, and the ')', if present.
229 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::eof))
231 while (Tok.isNot(tok::r_paren) &&
232 !Tok.isAtStartOfLine() &&
235 if (Tok.is(tok::r_paren))
237 return _PragmaLexing.failed();
240 if (Tok.hasUDSuffix()) {
241 Diag(Tok, diag::err_invalid_string_udl);
242 // Skip this token, and the ')', if present.
244 if (Tok.is(tok::r_paren))
246 return _PragmaLexing.failed();
249 // Remember the string.
254 if (Tok.isNot(tok::r_paren)) {
255 Diag(PragmaLoc, diag::err__Pragma_malformed);
256 return _PragmaLexing.failed();
259 if (InMacroArgPreExpansion)
262 SourceLocation RParenLoc = Tok.getLocation();
263 std::string StrVal = getSpelling(StrTok);
265 // The _Pragma is lexically sound. Destringize according to C11 6.10.9.1:
266 // "The string literal is destringized by deleting any encoding prefix,
267 // deleting the leading and trailing double-quotes, replacing each escape
268 // sequence \" by a double-quote, and replacing each escape sequence \\ by a
269 // single backslash."
270 if (StrVal[0] == 'L' || StrVal[0] == 'U' ||
271 (StrVal[0] == 'u' && StrVal[1] != '8'))
272 StrVal.erase(StrVal.begin());
273 else if (StrVal[0] == 'u')
274 StrVal.erase(StrVal.begin(), StrVal.begin() + 2);
276 if (StrVal[0] == 'R') {
277 // FIXME: C++11 does not specify how to handle raw-string-literals here.
278 // We strip off the 'R', the quotes, the d-char-sequences, and the parens.
279 assert(StrVal[1] == '"' && StrVal[StrVal.size() - 1] == '"' &&
280 "Invalid raw string token!");
282 // Measure the length of the d-char-sequence.
283 unsigned NumDChars = 0;
284 while (StrVal[2 + NumDChars] != '(') {
285 assert(NumDChars < (StrVal.size() - 5) / 2 &&
286 "Invalid raw string token!");
289 assert(StrVal[StrVal.size() - 2 - NumDChars] == ')');
291 // Remove 'R " d-char-sequence' and 'd-char-sequence "'. We'll replace the
293 StrVal.erase(0, 2 + NumDChars);
294 StrVal.erase(StrVal.size() - 1 - NumDChars);
296 assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
297 "Invalid string token!");
299 // Remove escaped quotes and escapes.
300 unsigned ResultPos = 1;
301 for (size_t i = 1, e = StrVal.size() - 1; i != e; ++i) {
302 // Skip escapes. \\ -> '\' and \" -> '"'.
303 if (StrVal[i] == '\\' && i + 1 < e &&
304 (StrVal[i + 1] == '\\' || StrVal[i + 1] == '"'))
306 StrVal[ResultPos++] = StrVal[i];
308 StrVal.erase(StrVal.begin() + ResultPos, StrVal.end() - 1);
311 // Remove the front quote, replacing it with a space, so that the pragma
312 // contents appear to have a space before them.
315 // Replace the terminating quote with a \n.
316 StrVal[StrVal.size()-1] = '\n';
318 // Plop the string (including the newline and trailing null) into a buffer
319 // where we can lex it.
322 CreateString(StrVal, TmpTok);
323 SourceLocation TokLoc = TmpTok.getLocation();
325 // Make and enter a lexer object so that we lex and expand the tokens just
327 Lexer *TL = Lexer::Create_PragmaLexer(TokLoc, PragmaLoc, RParenLoc,
328 StrVal.size(), *this);
330 EnterSourceFileWithLexer(TL, nullptr);
332 // With everything set up, lex this as a #pragma directive.
333 HandlePragmaDirective(PragmaLoc, PIK__Pragma);
335 // Finally, return whatever came after the pragma directive.
339 /// HandleMicrosoft__pragma - Like Handle_Pragma except the pragma text
340 /// is not enclosed within a string literal.
341 void Preprocessor::HandleMicrosoft__pragma(Token &Tok) {
342 // Remember the pragma token location.
343 SourceLocation PragmaLoc = Tok.getLocation();
347 if (Tok.isNot(tok::l_paren)) {
348 Diag(PragmaLoc, diag::err__Pragma_malformed);
352 // Get the tokens enclosed within the __pragma(), as well as the final ')'.
353 SmallVector<Token, 32> PragmaToks;
356 while (Tok.isNot(tok::eof)) {
357 PragmaToks.push_back(Tok);
358 if (Tok.is(tok::l_paren))
360 else if (Tok.is(tok::r_paren) && NumParens-- == 0)
365 if (Tok.is(tok::eof)) {
366 Diag(PragmaLoc, diag::err_unterminated___pragma);
370 PragmaToks.front().setFlag(Token::LeadingSpace);
372 // Replace the ')' with an EOD to mark the end of the pragma.
373 PragmaToks.back().setKind(tok::eod);
375 Token *TokArray = new Token[PragmaToks.size()];
376 std::copy(PragmaToks.begin(), PragmaToks.end(), TokArray);
378 // Push the tokens onto the stack.
379 EnterTokenStream(TokArray, PragmaToks.size(), true, true);
381 // With everything set up, lex this as a #pragma directive.
382 HandlePragmaDirective(PragmaLoc, PIK___pragma);
384 // Finally, return whatever came after the pragma directive.
388 /// HandlePragmaOnce - Handle \#pragma once. OnceTok is the 'once'.
389 void Preprocessor::HandlePragmaOnce(Token &OnceTok) {
390 // Don't honor the 'once' when handling the primary source file, unless
391 // this is a prefix to a TU, which indicates we're generating a PCH file, or
392 // when the main file is a header (e.g. when -xc-header is provided on the
394 if (isInPrimaryFile() && TUKind != TU_Prefix && !getLangOpts().IsHeaderFile) {
395 Diag(OnceTok, diag::pp_pragma_once_in_main_file);
399 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
400 // Mark the file as a once-only file now.
401 HeaderInfo.MarkFileIncludeOnce(getCurrentFileLexer()->getFileEntry());
404 void Preprocessor::HandlePragmaMark() {
405 assert(CurPPLexer && "No current lexer?");
406 CurLexer->ReadToEndOfLine();
409 /// HandlePragmaPoison - Handle \#pragma GCC poison. PoisonTok is the 'poison'.
410 void Preprocessor::HandlePragmaPoison() {
414 // Read the next token to poison. While doing this, pretend that we are
415 // skipping while reading the identifier to poison.
416 // This avoids errors on code like:
417 // #pragma GCC poison X
418 // #pragma GCC poison X
419 if (CurPPLexer) CurPPLexer->LexingRawMode = true;
420 LexUnexpandedToken(Tok);
421 if (CurPPLexer) CurPPLexer->LexingRawMode = false;
423 // If we reached the end of line, we're done.
424 if (Tok.is(tok::eod)) return;
426 // Can only poison identifiers.
427 if (Tok.isNot(tok::raw_identifier)) {
428 Diag(Tok, diag::err_pp_invalid_poison);
432 // Look up the identifier info for the token. We disabled identifier lookup
433 // by saying we're skipping contents, so we need to do this manually.
434 IdentifierInfo *II = LookUpIdentifierInfo(Tok);
437 if (II->isPoisoned()) continue;
439 // If this is a macro identifier, emit a warning.
440 if (isMacroDefined(II))
441 Diag(Tok, diag::pp_poisoning_existing_macro);
443 // Finally, poison it!
446 II->setChangedSinceDeserialization();
450 /// HandlePragmaSystemHeader - Implement \#pragma GCC system_header. We know
451 /// that the whole directive has been parsed.
452 void Preprocessor::HandlePragmaSystemHeader(Token &SysHeaderTok) {
453 if (isInPrimaryFile()) {
454 Diag(SysHeaderTok, diag::pp_pragma_sysheader_in_main_file);
458 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
459 PreprocessorLexer *TheLexer = getCurrentFileLexer();
461 // Mark the file as a system header.
462 HeaderInfo.MarkFileSystemHeader(TheLexer->getFileEntry());
464 PresumedLoc PLoc = SourceMgr.getPresumedLoc(SysHeaderTok.getLocation());
465 if (PLoc.isInvalid())
468 unsigned FilenameID = SourceMgr.getLineTableFilenameID(PLoc.getFilename());
470 // Notify the client, if desired, that we are in a new source file.
472 Callbacks->FileChanged(SysHeaderTok.getLocation(),
473 PPCallbacks::SystemHeaderPragma, SrcMgr::C_System);
475 // Emit a line marker. This will change any source locations from this point
476 // forward to realize they are in a system header.
477 // Create a line note with this information.
478 SourceMgr.AddLineNote(SysHeaderTok.getLocation(), PLoc.getLine() + 1,
479 FilenameID, /*IsEntry=*/false, /*IsExit=*/false,
483 /// HandlePragmaDependency - Handle \#pragma GCC dependency "foo" blah.
484 void Preprocessor::HandlePragmaDependency(Token &DependencyTok) {
486 CurPPLexer->LexIncludeFilename(FilenameTok);
488 // If the token kind is EOD, the error has already been diagnosed.
489 if (FilenameTok.is(tok::eod))
492 // Reserve a buffer to get the spelling.
493 SmallString<128> FilenameBuffer;
494 bool Invalid = false;
495 StringRef Filename = getSpelling(FilenameTok, FilenameBuffer, &Invalid);
500 GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
501 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
503 if (Filename.empty())
506 // Search include directories for this file.
507 const DirectoryLookup *CurDir;
508 const FileEntry *File =
509 LookupFile(FilenameTok.getLocation(), Filename, isAngled, nullptr,
510 nullptr, CurDir, nullptr, nullptr, nullptr, nullptr);
512 if (!SuppressIncludeNotFoundError)
513 Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
517 const FileEntry *CurFile = getCurrentFileLexer()->getFileEntry();
519 // If this file is older than the file it depends on, emit a diagnostic.
520 if (CurFile && CurFile->getModificationTime() < File->getModificationTime()) {
521 // Lex tokens at the end of the message and include them in the message.
524 while (DependencyTok.isNot(tok::eod)) {
525 Message += getSpelling(DependencyTok) + " ";
529 // Remove the trailing ' ' if present.
530 if (!Message.empty())
531 Message.erase(Message.end()-1);
532 Diag(FilenameTok, diag::pp_out_of_date_dependency) << Message;
536 /// ParsePragmaPushOrPopMacro - Handle parsing of pragma push_macro/pop_macro.
537 /// Return the IdentifierInfo* associated with the macro to push or pop.
538 IdentifierInfo *Preprocessor::ParsePragmaPushOrPopMacro(Token &Tok) {
539 // Remember the pragma token location.
540 Token PragmaTok = Tok;
544 if (Tok.isNot(tok::l_paren)) {
545 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
546 << getSpelling(PragmaTok);
550 // Read the macro name string.
552 if (Tok.isNot(tok::string_literal)) {
553 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
554 << getSpelling(PragmaTok);
558 if (Tok.hasUDSuffix()) {
559 Diag(Tok, diag::err_invalid_string_udl);
563 // Remember the macro string.
564 std::string StrVal = getSpelling(Tok);
568 if (Tok.isNot(tok::r_paren)) {
569 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
570 << getSpelling(PragmaTok);
574 assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
575 "Invalid string token!");
577 // Create a Token from the string.
579 MacroTok.startToken();
580 MacroTok.setKind(tok::raw_identifier);
581 CreateString(StringRef(&StrVal[1], StrVal.size() - 2), MacroTok);
583 // Get the IdentifierInfo of MacroToPushTok.
584 return LookUpIdentifierInfo(MacroTok);
587 /// Handle \#pragma push_macro.
591 /// #pragma push_macro("macro")
593 void Preprocessor::HandlePragmaPushMacro(Token &PushMacroTok) {
594 // Parse the pragma directive and get the macro IdentifierInfo*.
595 IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PushMacroTok);
596 if (!IdentInfo) return;
598 // Get the MacroInfo associated with IdentInfo.
599 MacroInfo *MI = getMacroInfo(IdentInfo);
602 // Allow the original MacroInfo to be redefined later.
603 MI->setIsAllowRedefinitionsWithoutWarning(true);
606 // Push the cloned MacroInfo so we can retrieve it later.
607 PragmaPushMacroInfo[IdentInfo].push_back(MI);
610 /// Handle \#pragma pop_macro.
614 /// #pragma pop_macro("macro")
616 void Preprocessor::HandlePragmaPopMacro(Token &PopMacroTok) {
617 SourceLocation MessageLoc = PopMacroTok.getLocation();
619 // Parse the pragma directive and get the macro IdentifierInfo*.
620 IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PopMacroTok);
621 if (!IdentInfo) return;
623 // Find the vector<MacroInfo*> associated with the macro.
624 llvm::DenseMap<IdentifierInfo *, std::vector<MacroInfo *>>::iterator iter =
625 PragmaPushMacroInfo.find(IdentInfo);
626 if (iter != PragmaPushMacroInfo.end()) {
627 // Forget the MacroInfo currently associated with IdentInfo.
628 if (MacroInfo *MI = getMacroInfo(IdentInfo)) {
629 if (MI->isWarnIfUnused())
630 WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());
631 appendMacroDirective(IdentInfo, AllocateUndefMacroDirective(MessageLoc));
634 // Get the MacroInfo we want to reinstall.
635 MacroInfo *MacroToReInstall = iter->second.back();
637 if (MacroToReInstall)
638 // Reinstall the previously pushed macro.
639 appendDefMacroDirective(IdentInfo, MacroToReInstall, MessageLoc);
641 // Pop PragmaPushMacroInfo stack.
642 iter->second.pop_back();
643 if (iter->second.empty())
644 PragmaPushMacroInfo.erase(iter);
646 Diag(MessageLoc, diag::warn_pragma_pop_macro_no_push)
647 << IdentInfo->getName();
651 void Preprocessor::HandlePragmaIncludeAlias(Token &Tok) {
652 // We will either get a quoted filename or a bracketed filename, and we
653 // have to track which we got. The first filename is the source name,
654 // and the second name is the mapped filename. If the first is quoted,
655 // the second must be as well (cannot mix and match quotes and brackets).
657 // Get the open paren
659 if (Tok.isNot(tok::l_paren)) {
660 Diag(Tok, diag::warn_pragma_include_alias_expected) << "(";
664 // We expect either a quoted string literal, or a bracketed name
665 Token SourceFilenameTok;
666 CurPPLexer->LexIncludeFilename(SourceFilenameTok);
667 if (SourceFilenameTok.is(tok::eod)) {
668 // The diagnostic has already been handled
672 StringRef SourceFileName;
673 SmallString<128> FileNameBuffer;
674 if (SourceFilenameTok.is(tok::string_literal) ||
675 SourceFilenameTok.is(tok::angle_string_literal)) {
676 SourceFileName = getSpelling(SourceFilenameTok, FileNameBuffer);
677 } else if (SourceFilenameTok.is(tok::less)) {
678 // This could be a path instead of just a name
679 FileNameBuffer.push_back('<');
681 if (ConcatenateIncludeName(FileNameBuffer, End))
682 return; // Diagnostic already emitted
683 SourceFileName = FileNameBuffer;
685 Diag(Tok, diag::warn_pragma_include_alias_expected_filename);
688 FileNameBuffer.clear();
690 // Now we expect a comma, followed by another include name
692 if (Tok.isNot(tok::comma)) {
693 Diag(Tok, diag::warn_pragma_include_alias_expected) << ",";
697 Token ReplaceFilenameTok;
698 CurPPLexer->LexIncludeFilename(ReplaceFilenameTok);
699 if (ReplaceFilenameTok.is(tok::eod)) {
700 // The diagnostic has already been handled
704 StringRef ReplaceFileName;
705 if (ReplaceFilenameTok.is(tok::string_literal) ||
706 ReplaceFilenameTok.is(tok::angle_string_literal)) {
707 ReplaceFileName = getSpelling(ReplaceFilenameTok, FileNameBuffer);
708 } else if (ReplaceFilenameTok.is(tok::less)) {
709 // This could be a path instead of just a name
710 FileNameBuffer.push_back('<');
712 if (ConcatenateIncludeName(FileNameBuffer, End))
713 return; // Diagnostic already emitted
714 ReplaceFileName = FileNameBuffer;
716 Diag(Tok, diag::warn_pragma_include_alias_expected_filename);
720 // Finally, we expect the closing paren
722 if (Tok.isNot(tok::r_paren)) {
723 Diag(Tok, diag::warn_pragma_include_alias_expected) << ")";
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(),
734 bool ReplaceIsAngled =
735 GetIncludeFilenameSpelling(ReplaceFilenameTok.getLocation(),
737 if (!SourceFileName.empty() && !ReplaceFileName.empty() &&
738 (SourceIsAngled != ReplaceIsAngled)) {
741 DiagID = diag::warn_pragma_include_alias_mismatch_angle;
743 DiagID = diag::warn_pragma_include_alias_mismatch_quote;
745 Diag(SourceFilenameTok.getLocation(), DiagID)
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,
762 PP.LexUnexpandedToken(Tok);
763 if (Tok.is(tok::string_literal) && !Tok.hasUDSuffix()) {
764 StringLiteralParser Literal(Tok, PP);
765 if (Literal.hadError)
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());
773 PP.Diag(Tok.getLocation(), diag::err_pp_expected_module_name) << First;
779 static bool LexModuleName(
780 Preprocessor &PP, Token &Tok,
781 llvm::SmallVectorImpl<std::pair<IdentifierInfo *, SourceLocation>>
784 std::pair<IdentifierInfo*, SourceLocation> NameComponent;
785 if (LexModuleNameComponent(PP, Tok, NameComponent, ModuleName.empty()))
787 ModuleName.push_back(NameComponent);
789 PP.LexUnexpandedToken(Tok);
790 if (Tok.isNot(tok::period))
795 void Preprocessor::HandlePragmaModuleBuild(Token &Tok) {
796 SourceLocation Loc = Tok.getLocation();
798 std::pair<IdentifierInfo *, SourceLocation> ModuleNameLoc;
799 if (LexModuleNameComponent(*this, Tok, ModuleNameLoc, true))
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)
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;
824 End = CurLexer->getBufferLocation();
827 if (Tok.is(tok::eof)) {
828 Diag(Loc, diag::err_pp_module_build_missing_end);
832 if (Tok.isNot(tok::hash) || !Tok.isAtStartOfLine()) {
833 // Token was part of module; keep going.
837 // We hit something directive-shaped; check to see if this is the end
838 // of the module build.
839 CurLexer->ParsingPreprocessorDirective = true;
841 if (TryConsumeIdentifier("pragma") && TryConsumeIdentifier("clang") &&
842 TryConsumeIdentifier("module")) {
843 if (TryConsumeIdentifier("build"))
844 // #pragma clang module build -> entering a nested module build.
846 else if (TryConsumeIdentifier("endbuild")) {
847 // #pragma clang module endbuild -> leaving a module build.
848 if (--NestingLevel == 0)
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.loadModuleFromSource(Loc, ModuleName->getName(),
866 StringRef(Start, End - Start));
869 void Preprocessor::HandlePragmaHdrstop(Token &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))
878 if (Tok.isNot(tok::r_paren)) {
879 Diag(Tok, diag::err_expected) << tok::r_paren;
884 if (Tok.isNot(tok::eod))
885 Diag(Tok.getLocation(), diag::ext_pp_extra_tokens_at_eol)
888 if (creatingPCHWithPragmaHdrStop() &&
889 SourceMgr.isInMainFile(Tok.getLocation())) {
890 assert(CurLexer && "no lexer for #pragma hdrstop processing");
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!");
917 // Otherwise, this namespace doesn't exist yet, create and insert the
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);
956 bool Preprocessor::LexOnOffSwitch(tok::OnOffSwitch &Result) {
958 LexUnexpandedToken(Tok);
960 if (Tok.isNot(tok::identifier)) {
961 Diag(Tok, diag::ext_on_off_switch_syntax);
964 IdentifierInfo *II = Tok.getIdentifierInfo();
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;
972 Diag(Tok, diag::ext_on_off_switch_syntax);
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);
985 /// PragmaOnceHandler - "\#pragma once" marks the file as atomically included.
986 struct PragmaOnceHandler : public PragmaHandler {
987 PragmaOnceHandler() : PragmaHandler("once") {}
989 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind 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, PragmaIntroducerKind Introducer,
1002 Token &MarkTok) override {
1003 PP.HandlePragmaMark();
1007 /// PragmaPoisonHandler - "\#pragma poison x" marks x as not usable.
1008 struct PragmaPoisonHandler : public PragmaHandler {
1009 PragmaPoisonHandler() : PragmaHandler("poison") {}
1011 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind 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, PragmaIntroducerKind 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, PragmaIntroducerKind Introducer,
1033 Token &DepToken) override {
1034 PP.HandlePragmaDependency(DepToken);
1038 struct PragmaDebugHandler : public PragmaHandler {
1039 PragmaDebugHandler() : PragmaHandler("__debug") {}
1041 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1042 Token &DepToken) override {
1044 PP.LexUnexpandedToken(Tok);
1045 if (Tok.isNot(tok::identifier)) {
1046 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
1049 IdentifierInfo *II = Tok.getIdentifierInfo();
1051 if (II->isStr("assert")) {
1052 llvm_unreachable("This is an assertion!");
1053 } else if (II->isStr("crash")) {
1055 } else if (II->isStr("parser_crash")) {
1057 Crasher.startToken();
1058 Crasher.setKind(tok::annot_pragma_parser_crash);
1059 Crasher.setAnnotationRange(SourceRange(Tok.getLocation()));
1060 PP.EnterToken(Crasher);
1061 } else if (II->isStr("dump")) {
1063 PP.LexUnexpandedToken(Identifier);
1064 if (auto *DumpII = Identifier.getIdentifierInfo()) {
1066 DumpAnnot.startToken();
1067 DumpAnnot.setKind(tok::annot_pragma_dump);
1068 DumpAnnot.setAnnotationRange(
1069 SourceRange(Tok.getLocation(), Identifier.getLocation()));
1070 DumpAnnot.setAnnotationValue(DumpII);
1071 PP.DiscardUntilEndOfDirective();
1072 PP.EnterToken(DumpAnnot);
1074 PP.Diag(Identifier, diag::warn_pragma_debug_missing_argument)
1077 } else if (II->isStr("diag_mapping")) {
1079 PP.LexUnexpandedToken(DiagName);
1080 if (DiagName.is(tok::eod))
1081 PP.getDiagnostics().dump();
1082 else if (DiagName.is(tok::string_literal) && !DiagName.hasUDSuffix()) {
1083 StringLiteralParser Literal(DiagName, PP);
1084 if (Literal.hadError)
1086 PP.getDiagnostics().dump(Literal.GetString());
1088 PP.Diag(DiagName, diag::warn_pragma_debug_missing_argument)
1091 } else if (II->isStr("llvm_fatal_error")) {
1092 llvm::report_fatal_error("#pragma clang __debug llvm_fatal_error");
1093 } else if (II->isStr("llvm_unreachable")) {
1094 llvm_unreachable("#pragma clang __debug llvm_unreachable");
1095 } else if (II->isStr("macro")) {
1097 PP.LexUnexpandedToken(MacroName);
1098 auto *MacroII = MacroName.getIdentifierInfo();
1100 PP.dumpMacroInfo(MacroII);
1102 PP.Diag(MacroName, diag::warn_pragma_debug_missing_argument)
1104 } else if (II->isStr("overflow_stack")) {
1105 DebugOverflowStack();
1106 } else if (II->isStr("handle_crash")) {
1107 llvm::CrashRecoveryContext *CRC =llvm::CrashRecoveryContext::GetCurrent();
1110 } else if (II->isStr("captured")) {
1113 PP.Diag(Tok, diag::warn_pragma_debug_unexpected_command)
1117 PPCallbacks *Callbacks = PP.getPPCallbacks();
1119 Callbacks->PragmaDebug(Tok.getLocation(), II->getName());
1122 void HandleCaptured(Preprocessor &PP) {
1124 PP.LexUnexpandedToken(Tok);
1126 if (Tok.isNot(tok::eod)) {
1127 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol)
1128 << "pragma clang __debug captured";
1132 SourceLocation NameLoc = Tok.getLocation();
1133 MutableArrayRef<Token> Toks(
1134 PP.getPreprocessorAllocator().Allocate<Token>(1), 1);
1135 Toks[0].startToken();
1136 Toks[0].setKind(tok::annot_pragma_captured);
1137 Toks[0].setLocation(NameLoc);
1139 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
1142 // Disable MSVC warning about runtime stack overflow.
1144 #pragma warning(disable : 4717)
1146 static void DebugOverflowStack(void (*P)() = nullptr) {
1147 void (*volatile Self)(void(*P)()) = DebugOverflowStack;
1148 Self(reinterpret_cast<void(*)()>(Self));
1151 #pragma warning(default : 4717)
1155 /// PragmaDiagnosticHandler - e.g. '\#pragma GCC diagnostic ignored "-Wformat"'
1156 struct PragmaDiagnosticHandler : public PragmaHandler {
1158 const char *Namespace;
1161 explicit PragmaDiagnosticHandler(const char *NS)
1162 : PragmaHandler("diagnostic"), Namespace(NS) {}
1164 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1165 Token &DiagToken) override {
1166 SourceLocation DiagLoc = DiagToken.getLocation();
1168 PP.LexUnexpandedToken(Tok);
1169 if (Tok.isNot(tok::identifier)) {
1170 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
1173 IdentifierInfo *II = Tok.getIdentifierInfo();
1174 PPCallbacks *Callbacks = PP.getPPCallbacks();
1176 if (II->isStr("pop")) {
1177 if (!PP.getDiagnostics().popMappings(DiagLoc))
1178 PP.Diag(Tok, diag::warn_pragma_diagnostic_cannot_pop);
1180 Callbacks->PragmaDiagnosticPop(DiagLoc, Namespace);
1182 } else if (II->isStr("push")) {
1183 PP.getDiagnostics().pushMappings(DiagLoc);
1185 Callbacks->PragmaDiagnosticPush(DiagLoc, Namespace);
1189 diag::Severity SV = llvm::StringSwitch<diag::Severity>(II->getName())
1190 .Case("ignored", diag::Severity::Ignored)
1191 .Case("warning", diag::Severity::Warning)
1192 .Case("error", diag::Severity::Error)
1193 .Case("fatal", diag::Severity::Fatal)
1194 .Default(diag::Severity());
1196 if (SV == diag::Severity()) {
1197 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
1201 PP.LexUnexpandedToken(Tok);
1202 SourceLocation StringLoc = Tok.getLocation();
1204 std::string WarningName;
1205 if (!PP.FinishLexStringLiteral(Tok, WarningName, "pragma diagnostic",
1206 /*MacroExpansion=*/false))
1209 if (Tok.isNot(tok::eod)) {
1210 PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token);
1214 if (WarningName.size() < 3 || WarningName[0] != '-' ||
1215 (WarningName[1] != 'W' && WarningName[1] != 'R')) {
1216 PP.Diag(StringLoc, diag::warn_pragma_diagnostic_invalid_option);
1220 diag::Flavor Flavor = WarningName[1] == 'W' ? diag::Flavor::WarningOrError
1221 : diag::Flavor::Remark;
1222 StringRef Group = StringRef(WarningName).substr(2);
1223 bool unknownDiag = false;
1224 if (Group == "everything") {
1225 // Special handling for pragma clang diagnostic ... "-Weverything".
1226 // There is no formal group named "everything", so there has to be a
1227 // special case for it.
1228 PP.getDiagnostics().setSeverityForAll(Flavor, SV, DiagLoc);
1230 unknownDiag = PP.getDiagnostics().setSeverityForGroup(Flavor, Group, SV,
1233 PP.Diag(StringLoc, diag::warn_pragma_diagnostic_unknown_warning)
1236 Callbacks->PragmaDiagnostic(DiagLoc, Namespace, SV, WarningName);
1240 /// "\#pragma hdrstop [<header-name-string>]"
1241 struct PragmaHdrstopHandler : public PragmaHandler {
1242 PragmaHdrstopHandler() : PragmaHandler("hdrstop") {}
1243 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1244 Token &DepToken) override {
1245 PP.HandlePragmaHdrstop(DepToken);
1249 /// "\#pragma warning(...)". MSVC's diagnostics do not map cleanly to clang's
1250 /// diagnostics, so we don't really implement this pragma. We parse it and
1251 /// ignore it to avoid -Wunknown-pragma warnings.
1252 struct PragmaWarningHandler : public PragmaHandler {
1253 PragmaWarningHandler() : PragmaHandler("warning") {}
1255 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1256 Token &Tok) override {
1257 // Parse things like:
1260 // warning(disable : 1 2 3 ; error : 4 5 6 ; suppress : 7 8 9)
1261 SourceLocation DiagLoc = Tok.getLocation();
1262 PPCallbacks *Callbacks = PP.getPPCallbacks();
1265 if (Tok.isNot(tok::l_paren)) {
1266 PP.Diag(Tok, diag::warn_pragma_warning_expected) << "(";
1271 IdentifierInfo *II = Tok.getIdentifierInfo();
1273 if (II && II->isStr("push")) {
1274 // #pragma warning( push[ ,n ] )
1277 if (Tok.is(tok::comma)) {
1280 if (Tok.is(tok::numeric_constant) &&
1281 PP.parseSimpleIntegerLiteral(Tok, Value))
1283 if (Level < 0 || Level > 4) {
1284 PP.Diag(Tok, diag::warn_pragma_warning_push_level);
1289 Callbacks->PragmaWarningPush(DiagLoc, Level);
1290 } else if (II && II->isStr("pop")) {
1291 // #pragma warning( pop )
1294 Callbacks->PragmaWarningPop(DiagLoc);
1296 // #pragma warning( warning-specifier : warning-number-list
1297 // [; warning-specifier : warning-number-list...] )
1299 II = Tok.getIdentifierInfo();
1300 if (!II && !Tok.is(tok::numeric_constant)) {
1301 PP.Diag(Tok, diag::warn_pragma_warning_spec_invalid);
1305 // Figure out which warning specifier this is.
1306 bool SpecifierValid;
1307 StringRef Specifier;
1308 llvm::SmallString<1> SpecifierBuf;
1310 Specifier = II->getName();
1311 SpecifierValid = llvm::StringSwitch<bool>(Specifier)
1312 .Cases("default", "disable", "error", "once",
1315 // If we read a correct specifier, snatch next token (that should be
1316 // ":", checked later).
1320 // Token is a numeric constant. It should be either 1, 2, 3 or 4.
1322 Specifier = PP.getSpelling(Tok, SpecifierBuf);
1323 if (PP.parseSimpleIntegerLiteral(Tok, Value)) {
1324 SpecifierValid = (Value >= 1) && (Value <= 4);
1326 SpecifierValid = false;
1327 // Next token already snatched by parseSimpleIntegerLiteral.
1330 if (!SpecifierValid) {
1331 PP.Diag(Tok, diag::warn_pragma_warning_spec_invalid);
1334 if (Tok.isNot(tok::colon)) {
1335 PP.Diag(Tok, diag::warn_pragma_warning_expected) << ":";
1339 // Collect the warning ids.
1340 SmallVector<int, 4> Ids;
1342 while (Tok.is(tok::numeric_constant)) {
1344 if (!PP.parseSimpleIntegerLiteral(Tok, Value) || Value == 0 ||
1345 Value > std::numeric_limits<int>::max()) {
1346 PP.Diag(Tok, diag::warn_pragma_warning_expected_number);
1349 Ids.push_back(int(Value));
1352 Callbacks->PragmaWarning(DiagLoc, Specifier, Ids);
1354 // Parse the next specifier if there is a semicolon.
1355 if (Tok.isNot(tok::semi))
1361 if (Tok.isNot(tok::r_paren)) {
1362 PP.Diag(Tok, diag::warn_pragma_warning_expected) << ")";
1367 if (Tok.isNot(tok::eod))
1368 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma warning";
1372 /// PragmaIncludeAliasHandler - "\#pragma include_alias("...")".
1373 struct PragmaIncludeAliasHandler : public PragmaHandler {
1374 PragmaIncludeAliasHandler() : PragmaHandler("include_alias") {}
1376 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1377 Token &IncludeAliasTok) override {
1378 PP.HandlePragmaIncludeAlias(IncludeAliasTok);
1382 /// PragmaMessageHandler - Handle the microsoft and gcc \#pragma message
1383 /// extension. The syntax is:
1385 /// #pragma message(string)
1387 /// OR, in GCC mode:
1389 /// #pragma message string
1391 /// string is a string, which is fully macro expanded, and permits string
1392 /// concatenation, embedded escape characters, etc... See MSDN for more details.
1393 /// Also handles \#pragma GCC warning and \#pragma GCC error which take the same
1394 /// form as \#pragma message.
1395 struct PragmaMessageHandler : public PragmaHandler {
1397 const PPCallbacks::PragmaMessageKind Kind;
1398 const StringRef Namespace;
1400 static const char* PragmaKind(PPCallbacks::PragmaMessageKind Kind,
1401 bool PragmaNameOnly = false) {
1403 case PPCallbacks::PMK_Message:
1404 return PragmaNameOnly ? "message" : "pragma message";
1405 case PPCallbacks::PMK_Warning:
1406 return PragmaNameOnly ? "warning" : "pragma warning";
1407 case PPCallbacks::PMK_Error:
1408 return PragmaNameOnly ? "error" : "pragma error";
1410 llvm_unreachable("Unknown PragmaMessageKind!");
1414 PragmaMessageHandler(PPCallbacks::PragmaMessageKind Kind,
1415 StringRef Namespace = StringRef())
1416 : PragmaHandler(PragmaKind(Kind, true)), Kind(Kind),
1417 Namespace(Namespace) {}
1419 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1420 Token &Tok) override {
1421 SourceLocation MessageLoc = Tok.getLocation();
1423 bool ExpectClosingParen = false;
1424 switch (Tok.getKind()) {
1426 // We have a MSVC style pragma message.
1427 ExpectClosingParen = true;
1431 case tok::string_literal:
1432 // We have a GCC style pragma message, and we just read the string.
1435 PP.Diag(MessageLoc, diag::err_pragma_message_malformed) << Kind;
1439 std::string MessageString;
1440 if (!PP.FinishLexStringLiteral(Tok, MessageString, PragmaKind(Kind),
1441 /*MacroExpansion=*/true))
1444 if (ExpectClosingParen) {
1445 if (Tok.isNot(tok::r_paren)) {
1446 PP.Diag(Tok.getLocation(), diag::err_pragma_message_malformed) << Kind;
1449 PP.Lex(Tok); // eat the r_paren.
1452 if (Tok.isNot(tok::eod)) {
1453 PP.Diag(Tok.getLocation(), diag::err_pragma_message_malformed) << Kind;
1457 // Output the message.
1458 PP.Diag(MessageLoc, (Kind == PPCallbacks::PMK_Error)
1459 ? diag::err_pragma_message
1460 : diag::warn_pragma_message) << MessageString;
1462 // If the pragma is lexically sound, notify any interested PPCallbacks.
1463 if (PPCallbacks *Callbacks = PP.getPPCallbacks())
1464 Callbacks->PragmaMessage(MessageLoc, Namespace, Kind, MessageString);
1468 /// Handle the clang \#pragma module import extension. The syntax is:
1470 /// #pragma clang module import some.module.name
1472 struct PragmaModuleImportHandler : public PragmaHandler {
1473 PragmaModuleImportHandler() : PragmaHandler("import") {}
1475 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1476 Token &Tok) override {
1477 SourceLocation ImportLoc = Tok.getLocation();
1479 // Read the module name.
1480 llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 8>
1482 if (LexModuleName(PP, Tok, ModuleName))
1485 if (Tok.isNot(tok::eod))
1486 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
1488 // If we have a non-empty module path, load the named module.
1490 PP.getModuleLoader().loadModule(ImportLoc, ModuleName, Module::Hidden,
1491 /*IsIncludeDirective=*/false);
1495 PP.makeModuleVisible(Imported, ImportLoc);
1496 PP.EnterAnnotationToken(SourceRange(ImportLoc, ModuleName.back().second),
1497 tok::annot_module_include, Imported);
1498 if (auto *CB = PP.getPPCallbacks())
1499 CB->moduleImport(ImportLoc, ModuleName, Imported);
1503 /// Handle the clang \#pragma module begin extension. The syntax is:
1505 /// #pragma clang module begin some.module.name
1507 /// #pragma clang module end
1509 struct PragmaModuleBeginHandler : public PragmaHandler {
1510 PragmaModuleBeginHandler() : PragmaHandler("begin") {}
1512 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1513 Token &Tok) override {
1514 SourceLocation BeginLoc = Tok.getLocation();
1516 // Read the module name.
1517 llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 8>
1519 if (LexModuleName(PP, Tok, ModuleName))
1522 if (Tok.isNot(tok::eod))
1523 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
1525 // We can only enter submodules of the current module.
1526 StringRef Current = PP.getLangOpts().CurrentModule;
1527 if (ModuleName.front().first->getName() != Current) {
1528 PP.Diag(ModuleName.front().second, diag::err_pp_module_begin_wrong_module)
1529 << ModuleName.front().first << (ModuleName.size() > 1)
1530 << Current.empty() << Current;
1534 // Find the module we're entering. We require that a module map for it
1535 // be loaded or implicitly loadable.
1536 // FIXME: We could create the submodule here. We'd need to know whether
1537 // it's supposed to be explicit, but not much else.
1538 Module *M = PP.getHeaderSearchInfo().lookupModule(Current);
1540 PP.Diag(ModuleName.front().second,
1541 diag::err_pp_module_begin_no_module_map) << Current;
1544 for (unsigned I = 1; I != ModuleName.size(); ++I) {
1545 auto *NewM = M->findSubmodule(ModuleName[I].first->getName());
1547 PP.Diag(ModuleName[I].second, diag::err_pp_module_begin_no_submodule)
1548 << M->getFullModuleName() << ModuleName[I].first;
1554 // If the module isn't available, it doesn't make sense to enter it.
1555 if (Preprocessor::checkModuleIsAvailable(
1556 PP.getLangOpts(), PP.getTargetInfo(), PP.getDiagnostics(), M)) {
1557 PP.Diag(BeginLoc, diag::note_pp_module_begin_here)
1558 << M->getTopLevelModuleName();
1562 // Enter the scope of the submodule.
1563 PP.EnterSubmodule(M, BeginLoc, /*ForPragma*/true);
1564 PP.EnterAnnotationToken(SourceRange(BeginLoc, ModuleName.back().second),
1565 tok::annot_module_begin, M);
1569 /// Handle the clang \#pragma module end extension.
1570 struct PragmaModuleEndHandler : public PragmaHandler {
1571 PragmaModuleEndHandler() : PragmaHandler("end") {}
1573 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1574 Token &Tok) override {
1575 SourceLocation Loc = Tok.getLocation();
1577 PP.LexUnexpandedToken(Tok);
1578 if (Tok.isNot(tok::eod))
1579 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
1581 Module *M = PP.LeaveSubmodule(/*ForPragma*/true);
1583 PP.EnterAnnotationToken(SourceRange(Loc), tok::annot_module_end, M);
1585 PP.Diag(Loc, diag::err_pp_module_end_without_module_begin);
1589 /// Handle the clang \#pragma module build extension.
1590 struct PragmaModuleBuildHandler : public PragmaHandler {
1591 PragmaModuleBuildHandler() : PragmaHandler("build") {}
1593 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1594 Token &Tok) override {
1595 PP.HandlePragmaModuleBuild(Tok);
1599 /// Handle the clang \#pragma module load extension.
1600 struct PragmaModuleLoadHandler : public PragmaHandler {
1601 PragmaModuleLoadHandler() : PragmaHandler("load") {}
1603 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1604 Token &Tok) override {
1605 SourceLocation Loc = Tok.getLocation();
1607 // Read the module name.
1608 llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 8>
1610 if (LexModuleName(PP, Tok, ModuleName))
1613 if (Tok.isNot(tok::eod))
1614 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
1616 // Load the module, don't make it visible.
1617 PP.getModuleLoader().loadModule(Loc, ModuleName, Module::Hidden,
1618 /*IsIncludeDirective=*/false);
1622 /// PragmaPushMacroHandler - "\#pragma push_macro" saves the value of the
1623 /// macro on the top of the stack.
1624 struct PragmaPushMacroHandler : public PragmaHandler {
1625 PragmaPushMacroHandler() : PragmaHandler("push_macro") {}
1627 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1628 Token &PushMacroTok) override {
1629 PP.HandlePragmaPushMacro(PushMacroTok);
1633 /// PragmaPopMacroHandler - "\#pragma pop_macro" sets the value of the
1634 /// macro to the value on the top of the stack.
1635 struct PragmaPopMacroHandler : public PragmaHandler {
1636 PragmaPopMacroHandler() : PragmaHandler("pop_macro") {}
1638 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1639 Token &PopMacroTok) override {
1640 PP.HandlePragmaPopMacro(PopMacroTok);
1644 /// PragmaARCCFCodeAuditedHandler -
1645 /// \#pragma clang arc_cf_code_audited begin/end
1646 struct PragmaARCCFCodeAuditedHandler : public PragmaHandler {
1647 PragmaARCCFCodeAuditedHandler() : PragmaHandler("arc_cf_code_audited") {}
1649 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1650 Token &NameTok) override {
1651 SourceLocation Loc = NameTok.getLocation();
1656 // Lex the 'begin' or 'end'.
1657 PP.LexUnexpandedToken(Tok);
1658 const IdentifierInfo *BeginEnd = Tok.getIdentifierInfo();
1659 if (BeginEnd && BeginEnd->isStr("begin")) {
1661 } else if (BeginEnd && BeginEnd->isStr("end")) {
1664 PP.Diag(Tok.getLocation(), diag::err_pp_arc_cf_code_audited_syntax);
1668 // Verify that this is followed by EOD.
1669 PP.LexUnexpandedToken(Tok);
1670 if (Tok.isNot(tok::eod))
1671 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
1673 // The start location of the active audit.
1674 SourceLocation BeginLoc = PP.getPragmaARCCFCodeAuditedLoc();
1676 // The start location we want after processing this.
1677 SourceLocation NewLoc;
1680 // Complain about attempts to re-enter an audit.
1681 if (BeginLoc.isValid()) {
1682 PP.Diag(Loc, diag::err_pp_double_begin_of_arc_cf_code_audited);
1683 PP.Diag(BeginLoc, diag::note_pragma_entered_here);
1687 // Complain about attempts to leave an audit that doesn't exist.
1688 if (!BeginLoc.isValid()) {
1689 PP.Diag(Loc, diag::err_pp_unmatched_end_of_arc_cf_code_audited);
1692 NewLoc = SourceLocation();
1695 PP.setPragmaARCCFCodeAuditedLoc(NewLoc);
1699 /// PragmaAssumeNonNullHandler -
1700 /// \#pragma clang assume_nonnull begin/end
1701 struct PragmaAssumeNonNullHandler : public PragmaHandler {
1702 PragmaAssumeNonNullHandler() : PragmaHandler("assume_nonnull") {}
1704 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1705 Token &NameTok) override {
1706 SourceLocation Loc = NameTok.getLocation();
1711 // Lex the 'begin' or 'end'.
1712 PP.LexUnexpandedToken(Tok);
1713 const IdentifierInfo *BeginEnd = Tok.getIdentifierInfo();
1714 if (BeginEnd && BeginEnd->isStr("begin")) {
1716 } else if (BeginEnd && BeginEnd->isStr("end")) {
1719 PP.Diag(Tok.getLocation(), diag::err_pp_assume_nonnull_syntax);
1723 // Verify that this is followed by EOD.
1724 PP.LexUnexpandedToken(Tok);
1725 if (Tok.isNot(tok::eod))
1726 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
1728 // The start location of the active audit.
1729 SourceLocation BeginLoc = PP.getPragmaAssumeNonNullLoc();
1731 // The start location we want after processing this.
1732 SourceLocation NewLoc;
1733 PPCallbacks *Callbacks = PP.getPPCallbacks();
1736 // Complain about attempts to re-enter an audit.
1737 if (BeginLoc.isValid()) {
1738 PP.Diag(Loc, diag::err_pp_double_begin_of_assume_nonnull);
1739 PP.Diag(BeginLoc, diag::note_pragma_entered_here);
1743 Callbacks->PragmaAssumeNonNullBegin(NewLoc);
1745 // Complain about attempts to leave an audit that doesn't exist.
1746 if (!BeginLoc.isValid()) {
1747 PP.Diag(Loc, diag::err_pp_unmatched_end_of_assume_nonnull);
1750 NewLoc = SourceLocation();
1752 Callbacks->PragmaAssumeNonNullEnd(NewLoc);
1755 PP.setPragmaAssumeNonNullLoc(NewLoc);
1759 /// Handle "\#pragma region [...]"
1763 /// #pragma region [optional name]
1764 /// #pragma endregion [optional comment]
1768 /// <a href="http://msdn.microsoft.com/en-us/library/b6xkz944(v=vs.80).aspx">editor-only</a>
1769 /// pragma, just skipped by compiler.
1770 struct PragmaRegionHandler : public PragmaHandler {
1771 PragmaRegionHandler(const char *pragma) : PragmaHandler(pragma) {}
1773 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1774 Token &NameTok) override {
1775 // #pragma region: endregion matches can be verified
1776 // __pragma(region): no sense, but ignored by msvc
1777 // _Pragma is not valid for MSVC, but there isn't any point
1778 // to handle a _Pragma differently.
1784 /// RegisterBuiltinPragmas - Install the standard preprocessor pragmas:
1785 /// \#pragma GCC poison/system_header/dependency and \#pragma once.
1786 void Preprocessor::RegisterBuiltinPragmas() {
1787 AddPragmaHandler(new PragmaOnceHandler());
1788 AddPragmaHandler(new PragmaMarkHandler());
1789 AddPragmaHandler(new PragmaPushMacroHandler());
1790 AddPragmaHandler(new PragmaPopMacroHandler());
1791 AddPragmaHandler(new PragmaMessageHandler(PPCallbacks::PMK_Message));
1794 AddPragmaHandler("GCC", new PragmaPoisonHandler());
1795 AddPragmaHandler("GCC", new PragmaSystemHeaderHandler());
1796 AddPragmaHandler("GCC", new PragmaDependencyHandler());
1797 AddPragmaHandler("GCC", new PragmaDiagnosticHandler("GCC"));
1798 AddPragmaHandler("GCC", new PragmaMessageHandler(PPCallbacks::PMK_Warning,
1800 AddPragmaHandler("GCC", new PragmaMessageHandler(PPCallbacks::PMK_Error,
1802 // #pragma clang ...
1803 AddPragmaHandler("clang", new PragmaPoisonHandler());
1804 AddPragmaHandler("clang", new PragmaSystemHeaderHandler());
1805 AddPragmaHandler("clang", new PragmaDebugHandler());
1806 AddPragmaHandler("clang", new PragmaDependencyHandler());
1807 AddPragmaHandler("clang", new PragmaDiagnosticHandler("clang"));
1808 AddPragmaHandler("clang", new PragmaARCCFCodeAuditedHandler());
1809 AddPragmaHandler("clang", new PragmaAssumeNonNullHandler());
1811 // #pragma clang module ...
1812 auto *ModuleHandler = new PragmaNamespace("module");
1813 AddPragmaHandler("clang", ModuleHandler);
1814 ModuleHandler->AddPragma(new PragmaModuleImportHandler());
1815 ModuleHandler->AddPragma(new PragmaModuleBeginHandler());
1816 ModuleHandler->AddPragma(new PragmaModuleEndHandler());
1817 ModuleHandler->AddPragma(new PragmaModuleBuildHandler());
1818 ModuleHandler->AddPragma(new PragmaModuleLoadHandler());
1820 // Add region pragmas.
1821 AddPragmaHandler(new PragmaRegionHandler("region"));
1822 AddPragmaHandler(new PragmaRegionHandler("endregion"));
1825 if (LangOpts.MicrosoftExt) {
1826 AddPragmaHandler(new PragmaWarningHandler());
1827 AddPragmaHandler(new PragmaIncludeAliasHandler());
1828 AddPragmaHandler(new PragmaHdrstopHandler());
1831 // Pragmas added by plugins
1832 for (PragmaHandlerRegistry::iterator it = PragmaHandlerRegistry::begin(),
1833 ie = PragmaHandlerRegistry::end();
1835 AddPragmaHandler(it->instantiate().release());
1839 /// Ignore all pragmas, useful for modes such as -Eonly which would otherwise
1840 /// warn about those pragmas being unknown.
1841 void Preprocessor::IgnorePragmas() {
1842 AddPragmaHandler(new EmptyPragmaHandler());
1843 // Also ignore all pragmas in all namespaces created
1844 // in Preprocessor::RegisterBuiltinPragmas().
1845 AddPragmaHandler("GCC", new EmptyPragmaHandler());
1846 AddPragmaHandler("clang", new EmptyPragmaHandler());