xref: /openbsd/gnu/llvm/clang/lib/Lex/Pragma.cpp (revision 12c85518)
1 //===- Pragma.cpp - Pragma registration and handling ----------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the PragmaHandler/PragmaTable interfaces and implements
10 // pragma related methods of the Preprocessor class.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "clang/Lex/Pragma.h"
15 #include "clang/Basic/CLWarnings.h"
16 #include "clang/Basic/Diagnostic.h"
17 #include "clang/Basic/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>
55 
56 using namespace clang;
57 
58 // Out-of-line destructor to provide a home for the class.
59 PragmaHandler::~PragmaHandler() = default;
60 
61 //===----------------------------------------------------------------------===//
62 // EmptyPragmaHandler Implementation.
63 //===----------------------------------------------------------------------===//
64 
EmptyPragmaHandler(StringRef Name)65 EmptyPragmaHandler::EmptyPragmaHandler(StringRef Name) : PragmaHandler(Name) {}
66 
HandlePragma(Preprocessor & PP,PragmaIntroducer Introducer,Token & FirstToken)67 void EmptyPragmaHandler::HandlePragma(Preprocessor &PP,
68                                       PragmaIntroducer Introducer,
69                                       Token &FirstToken) {}
70 
71 //===----------------------------------------------------------------------===//
72 // PragmaNamespace Implementation.
73 //===----------------------------------------------------------------------===//
74 
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.
FindHandler(StringRef Name,bool IgnoreNull) const79 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;
90 }
91 
AddPragma(PragmaHandler * Handler)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);
96 }
97 
RemovePragmaHandler(PragmaHandler * 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);
105 }
106 
HandlePragma(Preprocessor & PP,PragmaIntroducer Introducer,Token & Tok)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);
112 
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;
121   }
122 
123   // Otherwise, pass it down.
124   Handler->HandlePragma(PP, Introducer, Tok);
125 }
126 
127 //===----------------------------------------------------------------------===//
128 // Preprocessor Pragma Directive Handling.
129 //===----------------------------------------------------------------------===//
130 
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;
140 
lex__anon095d5c770111::TokenCollector141   void lex() {
142     if (Collect)
143       Tokens.push_back(Tok);
144     Self.Lex(Tok);
145   }
146 
revert__anon095d5c770111::TokenCollector147   void revert() {
148     assert(Collect && "did not collect tokens");
149     assert(!Tokens.empty() && "collected unexpected number of tokens");
150 
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);
158 
159     // ... and return the pragma token unchanged.
160     Tok = *Tokens.begin();
161   }
162 };
163 } // namespace
164 
165 /// HandlePragmaDirective - The "\#pragma" directive has been parsed.  Lex the
166 /// rest of the pragma, passing it to the registered pragma handlers.
HandlePragmaDirective(PragmaIntroducer Introducer)167 void Preprocessor::HandlePragmaDirective(PragmaIntroducer Introducer) {
168   if (Callbacks)
169     Callbacks->PragmaDirective(Introducer.Loc, Introducer.Kind);
170 
171   if (!PragmasEnabled)
172     return;
173 
174   ++NumPragma;
175 
176   // Invoke the first level of pragma handlers which reads the namespace id.
177   Token Tok;
178   PragmaHandlers->HandlePragma(*this, Introducer, Tok);
179 
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();
184 }
185 
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'.
Handle_Pragma(Token & 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]
194   //
195   // This means that we execute _Pragma operators in two cases:
196   //
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
201   //
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.
206   //
207   // In Case #2, we check the syntax now, but then put the tokens back into the
208   // token stream for later consumption.
209 
210   TokenCollector Toks = {*this, InMacroArgPreExpansion, {}, Tok};
211 
212   // Remember the pragma token location.
213   SourceLocation PragmaLoc = Tok.getLocation();
214 
215   // Read the '('.
216   Toks.lex();
217   if (Tok.isNot(tok::l_paren)) {
218     Diag(PragmaLoc, diag::err__Pragma_malformed);
219     return;
220   }
221 
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;
236   }
237 
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;
245   }
246 
247   // Remember the string.
248   Token StrTok = Tok;
249 
250   // Read the ')'.
251   Toks.lex();
252   if (Tok.isNot(tok::r_paren)) {
253     Diag(PragmaLoc, diag::err__Pragma_malformed);
254     return;
255   }
256 
257   // If we're expanding a macro argument, put the tokens back.
258   if (InMacroArgPreExpansion) {
259     Toks.revert();
260     return;
261   }
262 
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;
269   }
270 
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);
281 
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!");
287 
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;
294     }
295     assert(StrVal[StrVal.size() - 2 - NumDChars] == ')');
296 
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!");
304 
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];
313     }
314     StrVal.erase(StrVal.begin() + ResultPos, StrVal.end() - 1);
315   }
316 
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] = ' ';
320 
321   // Replace the terminating quote with a \n.
322   StrVal[StrVal.size()-1] = '\n';
323 
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();
330 
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);
335 
336   EnterSourceFileWithLexer(TL, nullptr);
337 
338   // With everything set up, lex this as a #pragma directive.
339   HandlePragmaDirective({PIK__Pragma, PragmaLoc});
340 
341   // Finally, return whatever came after the pragma directive.
342   return Lex(Tok);
343 }
344 
345 /// HandleMicrosoft__pragma - Like Handle_Pragma except the pragma text
346 /// is not enclosed within a string literal.
HandleMicrosoft__pragma(Token & Tok)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};
351 
352   // Remember the pragma token location.
353   SourceLocation PragmaLoc = Tok.getLocation();
354 
355   // Read the '('.
356   Toks.lex();
357   if (Tok.isNot(tok::l_paren)) {
358     Diag(PragmaLoc, diag::err__Pragma_malformed);
359     return;
360   }
361 
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();
373   }
374 
375   if (Tok.is(tok::eof)) {
376     Diag(PragmaLoc, diag::err_unterminated___pragma);
377     return;
378   }
379 
380   // If we're expanding a macro argument, put the tokens back.
381   if (InMacroArgPreExpansion) {
382     Toks.revert();
383     return;
384   }
385 
386   PragmaToks.front().setFlag(Token::LeadingSpace);
387 
388   // Replace the ')' with an EOD to mark the end of the pragma.
389   PragmaToks.back().setKind(tok::eod);
390 
391   Token *TokArray = new Token[PragmaToks.size()];
392   std::copy(PragmaToks.begin(), PragmaToks.end(), TokArray);
393 
394   // Push the tokens onto the stack.
395   EnterTokenStream(TokArray, PragmaToks.size(), true, true,
396                    /*IsReinject*/ false);
397 
398   // With everything set up, lex this as a #pragma directive.
399   HandlePragmaDirective({PIK___pragma, PragmaLoc});
400 
401   // Finally, return whatever came after the pragma directive.
402   return Lex(Tok);
403 }
404 
405 /// HandlePragmaOnce - Handle \#pragma once.  OnceTok is the 'once'.
HandlePragmaOnce(Token & OnceTok)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;
414   }
415 
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());
419 }
420 
HandlePragmaMark(Token & MarkTok)421 void Preprocessor::HandlePragmaMark(Token &MarkTok) {
422   assert(CurPPLexer && "No current lexer?");
423 
424   SmallString<64> Buffer;
425   CurLexer->ReadToEndOfLine(&Buffer);
426   if (Callbacks)
427     Callbacks->PragmaMark(MarkTok.getLocation(), Buffer);
428 }
429 
430 /// HandlePragmaPoison - Handle \#pragma GCC poison.  PoisonTok is the 'poison'.
HandlePragmaPoison()431 void Preprocessor::HandlePragmaPoison() {
432   Token Tok;
433 
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;
443 
444     // If we reached the end of line, we're done.
445     if (Tok.is(tok::eod)) return;
446 
447     // Can only poison identifiers.
448     if (Tok.isNot(tok::raw_identifier)) {
449       Diag(Tok, diag::err_pp_invalid_poison);
450       return;
451     }
452 
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);
456 
457     // Already poisoned.
458     if (II->isPoisoned()) continue;
459 
460     // If this is a macro identifier, emit a warning.
461     if (isMacroDefined(II))
462       Diag(Tok, diag::pp_poisoning_existing_macro);
463 
464     // Finally, poison it!
465     II->setIsPoisoned();
466     if (II->isFromAST())
467       II->setChangedSinceDeserialization();
468   }
469 }
470 
471 /// HandlePragmaSystemHeader - Implement \#pragma GCC system_header.  We know
472 /// that the whole directive has been parsed.
HandlePragmaSystemHeader(Token & SysHeaderTok)473 void Preprocessor::HandlePragmaSystemHeader(Token &SysHeaderTok) {
474   if (isInPrimaryFile()) {
475     Diag(SysHeaderTok, diag::pp_pragma_sysheader_in_main_file);
476     return;
477   }
478 
479   // Get the current file lexer we're looking at.  Ignore _Pragma 'files' etc.
480   PreprocessorLexer *TheLexer = getCurrentFileLexer();
481 
482   // Mark the file as a system header.
483   HeaderInfo.MarkFileSystemHeader(TheLexer->getFileEntry());
484 
485   PresumedLoc PLoc = SourceMgr.getPresumedLoc(SysHeaderTok.getLocation());
486   if (PLoc.isInvalid())
487     return;
488 
489   unsigned FilenameID = SourceMgr.getLineTableFilenameID(PLoc.getFilename());
490 
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);
495 
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);
502 }
503 
504 /// HandlePragmaDependency - Handle \#pragma GCC dependency "foo" blah.
HandlePragmaDependency(Token & DependencyTok)505 void Preprocessor::HandlePragmaDependency(Token &DependencyTok) {
506   Token FilenameTok;
507   if (LexHeaderName(FilenameTok, /*AllowConcatenation*/false))
508     return;
509 
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;
514   }
515 
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;
522 
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;
529 
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;
538   }
539 
540   const FileEntry *CurFile = getCurrentFileLexer()->getFileEntry();
541 
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);
550     }
551 
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;
556   }
557 }
558 
559 /// ParsePragmaPushOrPopMacro - Handle parsing of pragma push_macro/pop_macro.
560 /// Return the IdentifierInfo* associated with the macro to push or pop.
ParsePragmaPushOrPopMacro(Token & Tok)561 IdentifierInfo *Preprocessor::ParsePragmaPushOrPopMacro(Token &Tok) {
562   // Remember the pragma token location.
563   Token PragmaTok = Tok;
564 
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;
571   }
572 
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;
579   }
580 
581   if (Tok.hasUDSuffix()) {
582     Diag(Tok, diag::err_invalid_string_udl);
583     return nullptr;
584   }
585 
586   // Remember the macro string.
587   std::string StrVal = getSpelling(Tok);
588 
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;
595   }
596 
597   assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
598          "Invalid string token!");
599 
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);
605 
606   // Get the IdentifierInfo of MacroToPushTok.
607   return LookUpIdentifierInfo(MacroTok);
608 }
609 
610 /// Handle \#pragma push_macro.
611 ///
612 /// The syntax is:
613 /// \code
614 ///   #pragma push_macro("macro")
615 /// \endcode
HandlePragmaPushMacro(Token & PushMacroTok)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;
620 
621   // Get the MacroInfo associated with IdentInfo.
622   MacroInfo *MI = getMacroInfo(IdentInfo);
623 
624   if (MI) {
625     // Allow the original MacroInfo to be redefined later.
626     MI->setIsAllowRedefinitionsWithoutWarning(true);
627   }
628 
629   // Push the cloned MacroInfo so we can retrieve it later.
630   PragmaPushMacroInfo[IdentInfo].push_back(MI);
631 }
632 
633 /// Handle \#pragma pop_macro.
634 ///
635 /// The syntax is:
636 /// \code
637 ///   #pragma pop_macro("macro")
638 /// \endcode
HandlePragmaPopMacro(Token & PopMacroTok)639 void Preprocessor::HandlePragmaPopMacro(Token &PopMacroTok) {
640   SourceLocation MessageLoc = PopMacroTok.getLocation();
641 
642   // Parse the pragma directive and get the macro IdentifierInfo*.
643   IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PopMacroTok);
644   if (!IdentInfo) return;
645 
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));
655     }
656 
657     // Get the MacroInfo we want to reinstall.
658     MacroInfo *MacroToReInstall = iter->second.back();
659 
660     if (MacroToReInstall)
661       // Reinstall the previously pushed macro.
662       appendDefMacroDirective(IdentInfo, MacroToReInstall, MessageLoc);
663 
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();
671   }
672 }
673 
HandlePragmaIncludeAlias(Token & Tok)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).
679 
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;
685   }
686 
687   // We expect either a quoted string literal, or a bracketed name
688   Token SourceFilenameTok;
689   if (LexHeaderName(SourceFilenameTok))
690     return;
691 
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;
699   }
700   FileNameBuffer.clear();
701 
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;
707   }
708 
709   Token ReplaceFilenameTok;
710   if (LexHeaderName(ReplaceFilenameTok))
711     return;
712 
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;
719   }
720 
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;
726   }
727 
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;
731 
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;
745 
746     Diag(SourceFilenameTok.getLocation(), DiagID)
747       << SourceFileName
748       << ReplaceFileName;
749 
750     return;
751   }
752 
753   // Now we can let the include handler know about this mapping
754   getHeaderSearchInfo().AddIncludeAlias(OriginalSource, ReplaceFileName);
755 }
756 
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.
LexModuleNameComponent(Preprocessor & PP,Token & Tok,std::pair<IdentifierInfo *,SourceLocation> & ModuleNameComponent,bool First)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;
776   }
777   return false;
778 }
779 
LexModuleName(Preprocessor & PP,Token & Tok,llvm::SmallVectorImpl<std::pair<IdentifierInfo *,SourceLocation>> & ModuleName)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);
789 
790     PP.LexUnexpandedToken(Tok);
791     if (Tok.isNot(tok::period))
792       return false;
793   }
794 }
795 
HandlePragmaModuleBuild(Token & Tok)796 void Preprocessor::HandlePragmaModuleBuild(Token &Tok) {
797   SourceLocation Loc = Tok.getLocation();
798 
799   std::pair<IdentifierInfo *, SourceLocation> ModuleNameLoc;
800   if (LexModuleNameComponent(*this, Tok, ModuleNameLoc, true))
801     return;
802   IdentifierInfo *ModuleName = ModuleNameLoc.first;
803 
804   LexUnexpandedToken(Tok);
805   if (Tok.isNot(tok::eod)) {
806     Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
807     DiscardUntilEndOfDirective();
808   }
809 
810   CurLexer->LexingRawMode = true;
811 
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;
818   };
819 
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);
827 
828     if (Tok.is(tok::eof)) {
829       Diag(Loc, diag::err_pp_module_build_missing_end);
830       break;
831     }
832 
833     if (Tok.isNot(tok::hash) || !Tok.isAtStartOfLine()) {
834       // Token was part of module; keep going.
835       continue;
836     }
837 
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;
851       }
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");
855     }
856   }
857 
858   CurLexer->LexingRawMode = false;
859 
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));
868 }
869 
HandlePragmaHdrstop(Token & Tok)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);
874 
875     std::string FileName;
876     if (!LexStringLiteral(Tok, FileName, "pragma hdrstop", false))
877       return;
878 
879     if (Tok.isNot(tok::r_paren)) {
880       Diag(Tok, diag::err_expected) << tok::r_paren;
881       return;
882     }
883     Lex(Tok);
884   }
885   if (Tok.isNot(tok::eod))
886     Diag(Tok.getLocation(), diag::ext_pp_extra_tokens_at_eol)
887         << "pragma hdrstop";
888 
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();
896   }
897   if (usingPCHWithPragmaHdrStop())
898     SkippingUntilPragmaHdrStop = false;
899 }
900 
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".
AddPragmaHandler(StringRef Namespace,PragmaHandler * Handler)904 void Preprocessor::AddPragmaHandler(StringRef Namespace,
905                                     PragmaHandler *Handler) {
906   PragmaNamespace *InsertNS = PragmaHandlers.get();
907 
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);
922     }
923   }
924 
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);
929 }
930 
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.
RemovePragmaHandler(StringRef Namespace,PragmaHandler * Handler)935 void Preprocessor::RemovePragmaHandler(StringRef Namespace,
936                                        PragmaHandler *Handler) {
937   PragmaNamespace *NS = PragmaHandlers.get();
938 
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!");
943 
944     NS = Existing->getIfNamespace();
945     assert(NS && "Invalid namespace, registered as a regular pragma handler!");
946   }
947 
948   NS->RemovePragmaHandler(Handler);
949 
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;
954   }
955 }
956 
LexOnOffSwitch(tok::OnOffSwitch & Result)957 bool Preprocessor::LexOnOffSwitch(tok::OnOffSwitch &Result) {
958   Token Tok;
959   LexUnexpandedToken(Tok);
960 
961   if (Tok.isNot(tok::identifier)) {
962     Diag(Tok, diag::ext_on_off_switch_syntax);
963     return true;
964   }
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;
975   }
976 
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;
982 }
983 
984 namespace {
985 
986 /// PragmaOnceHandler - "\#pragma once" marks the file as atomically included.
987 struct PragmaOnceHandler : public PragmaHandler {
PragmaOnceHandler__anon095d5c770311::PragmaOnceHandler988   PragmaOnceHandler() : PragmaHandler("once") {}
989 
HandlePragma__anon095d5c770311::PragmaOnceHandler990   void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
991                     Token &OnceTok) override {
992     PP.CheckEndOfDirective("pragma once");
993     PP.HandlePragmaOnce(OnceTok);
994   }
995 };
996 
997 /// PragmaMarkHandler - "\#pragma mark ..." is ignored by the compiler, and the
998 /// rest of the line is not lexed.
999 struct PragmaMarkHandler : public PragmaHandler {
PragmaMarkHandler__anon095d5c770311::PragmaMarkHandler1000   PragmaMarkHandler() : PragmaHandler("mark") {}
1001 
HandlePragma__anon095d5c770311::PragmaMarkHandler1002   void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1003                     Token &MarkTok) override {
1004     PP.HandlePragmaMark(MarkTok);
1005   }
1006 };
1007 
1008 /// PragmaPoisonHandler - "\#pragma poison x" marks x as not usable.
1009 struct PragmaPoisonHandler : public PragmaHandler {
PragmaPoisonHandler__anon095d5c770311::PragmaPoisonHandler1010   PragmaPoisonHandler() : PragmaHandler("poison") {}
1011 
HandlePragma__anon095d5c770311::PragmaPoisonHandler1012   void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1013                     Token &PoisonTok) override {
1014     PP.HandlePragmaPoison();
1015   }
1016 };
1017 
1018 /// PragmaSystemHeaderHandler - "\#pragma system_header" marks the current file
1019 /// as a system header, which silences warnings in it.
1020 struct PragmaSystemHeaderHandler : public PragmaHandler {
PragmaSystemHeaderHandler__anon095d5c770311::PragmaSystemHeaderHandler1021   PragmaSystemHeaderHandler() : PragmaHandler("system_header") {}
1022 
HandlePragma__anon095d5c770311::PragmaSystemHeaderHandler1023   void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1024                     Token &SHToken) override {
1025     PP.HandlePragmaSystemHeader(SHToken);
1026     PP.CheckEndOfDirective("pragma");
1027   }
1028 };
1029 
1030 struct PragmaDependencyHandler : public PragmaHandler {
PragmaDependencyHandler__anon095d5c770311::PragmaDependencyHandler1031   PragmaDependencyHandler() : PragmaHandler("dependency") {}
1032 
HandlePragma__anon095d5c770311::PragmaDependencyHandler1033   void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1034                     Token &DepToken) override {
1035     PP.HandlePragmaDependency(DepToken);
1036   }
1037 };
1038 
1039 struct PragmaDebugHandler : public PragmaHandler {
PragmaDebugHandler__anon095d5c770311::PragmaDebugHandler1040   PragmaDebugHandler() : PragmaHandler("__debug") {}
1041 
HandlePragma__anon095d5c770311::PragmaDebugHandler1042   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;
1049     }
1050     IdentifierInfo *II = Tok.getIdentifierInfo();
1051 
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);
1067       }
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();
1083       }
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();
1097       }
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;
1126         }
1127       }
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());
1144             }
1145             llvm::errs() << "\n";
1146           }
1147           for (Module *Sub : M->submodules()) {
1148             if (!VisibleOnly || ImportLoc.isInvalid() || Sub->IsExplicit)
1149               visit(Sub, VisibleOnly);
1150           }
1151         }
1152         void visitAll(bool VisibleOnly) {
1153           for (auto &NameAndMod :
1154                PP.getHeaderSearchInfo().getModuleMap().modules())
1155             visit(NameAndMod.second, VisibleOnly);
1156         }
1157       } Visitor{PP};
1158 
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";
1179           }
1180         }
1181       } else {
1182         PP.Diag(Tok, diag::warn_pragma_debug_unexpected_command)
1183           << DumpII->getName();
1184       }
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);
1197       }
1198 
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();
1205     }
1206 
1207     PPCallbacks *Callbacks = PP.getPPCallbacks();
1208     if (Callbacks)
1209       Callbacks->PragmaDebug(Tok.getLocation(), II->getName());
1210   }
1211 
HandleCaptured__anon095d5c770311::PragmaDebugHandler1212   void HandleCaptured(Preprocessor &PP) {
1213     Token Tok;
1214     PP.LexUnexpandedToken(Tok);
1215 
1216     if (Tok.isNot(tok::eod)) {
1217       PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol)
1218         << "pragma clang __debug captured";
1219       return;
1220     }
1221 
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);
1228 
1229     PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true,
1230                         /*IsReinject=*/false);
1231   }
1232 
1233 // Disable MSVC warning about runtime stack overflow.
1234 #ifdef _MSC_VER
1235     #pragma warning(disable : 4717)
1236 #endif
DebugOverflowStack__anon095d5c770311::PragmaDebugHandler1237   static void DebugOverflowStack(void (*P)() = nullptr) {
1238     void (*volatile Self)(void(*P)()) = DebugOverflowStack;
1239     Self(reinterpret_cast<void(*)()>(Self));
1240   }
1241 #ifdef _MSC_VER
1242     #pragma warning(default : 4717)
1243 #endif
1244 };
1245 
1246 /// PragmaDiagnosticHandler - e.g. '\#pragma GCC diagnostic ignored "-Wformat"'
1247 struct PragmaDiagnosticHandler : public PragmaHandler {
1248 private:
1249   const char *Namespace;
1250 
1251 public:
PragmaDiagnosticHandler__anon095d5c770311::PragmaDiagnosticHandler1252   explicit PragmaDiagnosticHandler(const char *NS)
1253       : PragmaHandler("diagnostic"), Namespace(NS) {}
1254 
HandlePragma__anon095d5c770311::PragmaDiagnosticHandler1255   void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1256                     Token &DiagToken) override {
1257     SourceLocation DiagLoc = DiagToken.getLocation();
1258     Token Tok;
1259     PP.LexUnexpandedToken(Tok);
1260     if (Tok.isNot(tok::identifier)) {
1261       PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
1262       return;
1263     }
1264     IdentifierInfo *II = Tok.getIdentifierInfo();
1265     PPCallbacks *Callbacks = PP.getPPCallbacks();
1266 
1267     if (II->isStr("pop")) {
1268       if (!PP.getDiagnostics().popMappings(DiagLoc))
1269         PP.Diag(Tok, diag::warn_pragma_diagnostic_cannot_pop);
1270       else if (Callbacks)
1271         Callbacks->PragmaDiagnosticPop(DiagLoc, Namespace);
1272       return;
1273     } else if (II->isStr("push")) {
1274       PP.getDiagnostics().pushMappings(DiagLoc);
1275       if (Callbacks)
1276         Callbacks->PragmaDiagnosticPush(DiagLoc, Namespace);
1277       return;
1278     }
1279 
1280     diag::Severity SV = llvm::StringSwitch<diag::Severity>(II->getName())
1281                             .Case("ignored", diag::Severity::Ignored)
1282                             .Case("warning", diag::Severity::Warning)
1283                             .Case("error", diag::Severity::Error)
1284                             .Case("fatal", diag::Severity::Fatal)
1285                             .Default(diag::Severity());
1286 
1287     if (SV == diag::Severity()) {
1288       PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
1289       return;
1290     }
1291 
1292     PP.LexUnexpandedToken(Tok);
1293     SourceLocation StringLoc = Tok.getLocation();
1294 
1295     std::string WarningName;
1296     if (!PP.FinishLexStringLiteral(Tok, WarningName, "pragma diagnostic",
1297                                    /*AllowMacroExpansion=*/false))
1298       return;
1299 
1300     if (Tok.isNot(tok::eod)) {
1301       PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token);
1302       return;
1303     }
1304 
1305     if (WarningName.size() < 3 || WarningName[0] != '-' ||
1306         (WarningName[1] != 'W' && WarningName[1] != 'R')) {
1307       PP.Diag(StringLoc, diag::warn_pragma_diagnostic_invalid_option);
1308       return;
1309     }
1310 
1311     diag::Flavor Flavor = WarningName[1] == 'W' ? diag::Flavor::WarningOrError
1312                                                 : diag::Flavor::Remark;
1313     StringRef Group = StringRef(WarningName).substr(2);
1314     bool unknownDiag = false;
1315     if (Group == "everything") {
1316       // Special handling for pragma clang diagnostic ... "-Weverything".
1317       // There is no formal group named "everything", so there has to be a
1318       // special case for it.
1319       PP.getDiagnostics().setSeverityForAll(Flavor, SV, DiagLoc);
1320     } else
1321       unknownDiag = PP.getDiagnostics().setSeverityForGroup(Flavor, Group, SV,
1322                                                             DiagLoc);
1323     if (unknownDiag)
1324       PP.Diag(StringLoc, diag::warn_pragma_diagnostic_unknown_warning)
1325         << WarningName;
1326     else if (Callbacks)
1327       Callbacks->PragmaDiagnostic(DiagLoc, Namespace, SV, WarningName);
1328   }
1329 };
1330 
1331 /// "\#pragma hdrstop [<header-name-string>]"
1332 struct PragmaHdrstopHandler : public PragmaHandler {
PragmaHdrstopHandler__anon095d5c770311::PragmaHdrstopHandler1333   PragmaHdrstopHandler() : PragmaHandler("hdrstop") {}
HandlePragma__anon095d5c770311::PragmaHdrstopHandler1334   void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1335                     Token &DepToken) override {
1336     PP.HandlePragmaHdrstop(DepToken);
1337   }
1338 };
1339 
1340 /// "\#pragma warning(...)".  MSVC's diagnostics do not map cleanly to clang's
1341 /// diagnostics, so we don't really implement this pragma.  We parse it and
1342 /// ignore it to avoid -Wunknown-pragma warnings.
1343 struct PragmaWarningHandler : public PragmaHandler {
PragmaWarningHandler__anon095d5c770311::PragmaWarningHandler1344   PragmaWarningHandler() : PragmaHandler("warning") {}
1345 
HandlePragma__anon095d5c770311::PragmaWarningHandler1346   void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1347                     Token &Tok) override {
1348     // Parse things like:
1349     // warning(push, 1)
1350     // warning(pop)
1351     // warning(disable : 1 2 3 ; error : 4 5 6 ; suppress : 7 8 9)
1352     SourceLocation DiagLoc = Tok.getLocation();
1353     PPCallbacks *Callbacks = PP.getPPCallbacks();
1354 
1355     PP.Lex(Tok);
1356     if (Tok.isNot(tok::l_paren)) {
1357       PP.Diag(Tok, diag::warn_pragma_warning_expected) << "(";
1358       return;
1359     }
1360 
1361     PP.Lex(Tok);
1362     IdentifierInfo *II = Tok.getIdentifierInfo();
1363 
1364     if (II && II->isStr("push")) {
1365       // #pragma warning( push[ ,n ] )
1366       int Level = -1;
1367       PP.Lex(Tok);
1368       if (Tok.is(tok::comma)) {
1369         PP.Lex(Tok);
1370         uint64_t Value;
1371         if (Tok.is(tok::numeric_constant) &&
1372             PP.parseSimpleIntegerLiteral(Tok, Value))
1373           Level = int(Value);
1374         if (Level < 0 || Level > 4) {
1375           PP.Diag(Tok, diag::warn_pragma_warning_push_level);
1376           return;
1377         }
1378       }
1379       PP.getDiagnostics().pushMappings(DiagLoc);
1380       if (Callbacks)
1381         Callbacks->PragmaWarningPush(DiagLoc, Level);
1382     } else if (II && II->isStr("pop")) {
1383       // #pragma warning( pop )
1384       PP.Lex(Tok);
1385       if (!PP.getDiagnostics().popMappings(DiagLoc))
1386         PP.Diag(Tok, diag::warn_pragma_diagnostic_cannot_pop);
1387       else if (Callbacks)
1388         Callbacks->PragmaWarningPop(DiagLoc);
1389     } else {
1390       // #pragma warning( warning-specifier : warning-number-list
1391       //                  [; warning-specifier : warning-number-list...] )
1392       while (true) {
1393         II = Tok.getIdentifierInfo();
1394         if (!II && !Tok.is(tok::numeric_constant)) {
1395           PP.Diag(Tok, diag::warn_pragma_warning_spec_invalid);
1396           return;
1397         }
1398 
1399         // Figure out which warning specifier this is.
1400         bool SpecifierValid;
1401         PPCallbacks::PragmaWarningSpecifier Specifier;
1402         if (II) {
1403           int SpecifierInt = llvm::StringSwitch<int>(II->getName())
1404                                  .Case("default", PPCallbacks::PWS_Default)
1405                                  .Case("disable", PPCallbacks::PWS_Disable)
1406                                  .Case("error", PPCallbacks::PWS_Error)
1407                                  .Case("once", PPCallbacks::PWS_Once)
1408                                  .Case("suppress", PPCallbacks::PWS_Suppress)
1409                                  .Default(-1);
1410           if ((SpecifierValid = SpecifierInt != -1))
1411             Specifier =
1412                 static_cast<PPCallbacks::PragmaWarningSpecifier>(SpecifierInt);
1413 
1414           // If we read a correct specifier, snatch next token (that should be
1415           // ":", checked later).
1416           if (SpecifierValid)
1417             PP.Lex(Tok);
1418         } else {
1419           // Token is a numeric constant. It should be either 1, 2, 3 or 4.
1420           uint64_t Value;
1421           if (PP.parseSimpleIntegerLiteral(Tok, Value)) {
1422             if ((SpecifierValid = (Value >= 1) && (Value <= 4)))
1423               Specifier = static_cast<PPCallbacks::PragmaWarningSpecifier>(
1424                   PPCallbacks::PWS_Level1 + Value - 1);
1425           } else
1426             SpecifierValid = false;
1427           // Next token already snatched by parseSimpleIntegerLiteral.
1428         }
1429 
1430         if (!SpecifierValid) {
1431           PP.Diag(Tok, diag::warn_pragma_warning_spec_invalid);
1432           return;
1433         }
1434         if (Tok.isNot(tok::colon)) {
1435           PP.Diag(Tok, diag::warn_pragma_warning_expected) << ":";
1436           return;
1437         }
1438 
1439         // Collect the warning ids.
1440         SmallVector<int, 4> Ids;
1441         PP.Lex(Tok);
1442         while (Tok.is(tok::numeric_constant)) {
1443           uint64_t Value;
1444           if (!PP.parseSimpleIntegerLiteral(Tok, Value) || Value == 0 ||
1445               Value > INT_MAX) {
1446             PP.Diag(Tok, diag::warn_pragma_warning_expected_number);
1447             return;
1448           }
1449           Ids.push_back(int(Value));
1450         }
1451 
1452         // Only act on disable for now.
1453         diag::Severity SV = diag::Severity();
1454         if (Specifier == PPCallbacks::PWS_Disable)
1455           SV = diag::Severity::Ignored;
1456         if (SV != diag::Severity())
1457           for (int Id : Ids) {
1458             if (auto Group = diagGroupFromCLWarningID(Id)) {
1459               bool unknownDiag = PP.getDiagnostics().setSeverityForGroup(
1460                   diag::Flavor::WarningOrError, *Group, SV, DiagLoc);
1461               assert(!unknownDiag &&
1462                      "wd table should only contain known diags");
1463               (void)unknownDiag;
1464             }
1465           }
1466 
1467         if (Callbacks)
1468           Callbacks->PragmaWarning(DiagLoc, Specifier, Ids);
1469 
1470         // Parse the next specifier if there is a semicolon.
1471         if (Tok.isNot(tok::semi))
1472           break;
1473         PP.Lex(Tok);
1474       }
1475     }
1476 
1477     if (Tok.isNot(tok::r_paren)) {
1478       PP.Diag(Tok, diag::warn_pragma_warning_expected) << ")";
1479       return;
1480     }
1481 
1482     PP.Lex(Tok);
1483     if (Tok.isNot(tok::eod))
1484       PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma warning";
1485   }
1486 };
1487 
1488 /// "\#pragma execution_character_set(...)". MSVC supports this pragma only
1489 /// for "UTF-8". We parse it and ignore it if UTF-8 is provided and warn
1490 /// otherwise to avoid -Wunknown-pragma warnings.
1491 struct PragmaExecCharsetHandler : public PragmaHandler {
PragmaExecCharsetHandler__anon095d5c770311::PragmaExecCharsetHandler1492   PragmaExecCharsetHandler() : PragmaHandler("execution_character_set") {}
1493 
HandlePragma__anon095d5c770311::PragmaExecCharsetHandler1494   void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1495                     Token &Tok) override {
1496     // Parse things like:
1497     // execution_character_set(push, "UTF-8")
1498     // execution_character_set(pop)
1499     SourceLocation DiagLoc = Tok.getLocation();
1500     PPCallbacks *Callbacks = PP.getPPCallbacks();
1501 
1502     PP.Lex(Tok);
1503     if (Tok.isNot(tok::l_paren)) {
1504       PP.Diag(Tok, diag::warn_pragma_exec_charset_expected) << "(";
1505       return;
1506     }
1507 
1508     PP.Lex(Tok);
1509     IdentifierInfo *II = Tok.getIdentifierInfo();
1510 
1511     if (II && II->isStr("push")) {
1512       // #pragma execution_character_set( push[ , string ] )
1513       PP.Lex(Tok);
1514       if (Tok.is(tok::comma)) {
1515         PP.Lex(Tok);
1516 
1517         std::string ExecCharset;
1518         if (!PP.FinishLexStringLiteral(Tok, ExecCharset,
1519                                        "pragma execution_character_set",
1520                                        /*AllowMacroExpansion=*/false))
1521           return;
1522 
1523         // MSVC supports either of these, but nothing else.
1524         if (ExecCharset != "UTF-8" && ExecCharset != "utf-8") {
1525           PP.Diag(Tok, diag::warn_pragma_exec_charset_push_invalid) << ExecCharset;
1526           return;
1527         }
1528       }
1529       if (Callbacks)
1530         Callbacks->PragmaExecCharsetPush(DiagLoc, "UTF-8");
1531     } else if (II && II->isStr("pop")) {
1532       // #pragma execution_character_set( pop )
1533       PP.Lex(Tok);
1534       if (Callbacks)
1535         Callbacks->PragmaExecCharsetPop(DiagLoc);
1536     } else {
1537       PP.Diag(Tok, diag::warn_pragma_exec_charset_spec_invalid);
1538       return;
1539     }
1540 
1541     if (Tok.isNot(tok::r_paren)) {
1542       PP.Diag(Tok, diag::warn_pragma_exec_charset_expected) << ")";
1543       return;
1544     }
1545 
1546     PP.Lex(Tok);
1547     if (Tok.isNot(tok::eod))
1548       PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma execution_character_set";
1549   }
1550 };
1551 
1552 /// PragmaIncludeAliasHandler - "\#pragma include_alias("...")".
1553 struct PragmaIncludeAliasHandler : public PragmaHandler {
PragmaIncludeAliasHandler__anon095d5c770311::PragmaIncludeAliasHandler1554   PragmaIncludeAliasHandler() : PragmaHandler("include_alias") {}
1555 
HandlePragma__anon095d5c770311::PragmaIncludeAliasHandler1556   void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1557                     Token &IncludeAliasTok) override {
1558     PP.HandlePragmaIncludeAlias(IncludeAliasTok);
1559   }
1560 };
1561 
1562 /// PragmaMessageHandler - Handle the microsoft and gcc \#pragma message
1563 /// extension.  The syntax is:
1564 /// \code
1565 ///   #pragma message(string)
1566 /// \endcode
1567 /// OR, in GCC mode:
1568 /// \code
1569 ///   #pragma message string
1570 /// \endcode
1571 /// string is a string, which is fully macro expanded, and permits string
1572 /// concatenation, embedded escape characters, etc... See MSDN for more details.
1573 /// Also handles \#pragma GCC warning and \#pragma GCC error which take the same
1574 /// form as \#pragma message.
1575 struct PragmaMessageHandler : public PragmaHandler {
1576 private:
1577   const PPCallbacks::PragmaMessageKind Kind;
1578   const StringRef Namespace;
1579 
PragmaKind__anon095d5c770311::PragmaMessageHandler1580   static const char* PragmaKind(PPCallbacks::PragmaMessageKind Kind,
1581                                 bool PragmaNameOnly = false) {
1582     switch (Kind) {
1583       case PPCallbacks::PMK_Message:
1584         return PragmaNameOnly ? "message" : "pragma message";
1585       case PPCallbacks::PMK_Warning:
1586         return PragmaNameOnly ? "warning" : "pragma warning";
1587       case PPCallbacks::PMK_Error:
1588         return PragmaNameOnly ? "error" : "pragma error";
1589     }
1590     llvm_unreachable("Unknown PragmaMessageKind!");
1591   }
1592 
1593 public:
PragmaMessageHandler__anon095d5c770311::PragmaMessageHandler1594   PragmaMessageHandler(PPCallbacks::PragmaMessageKind Kind,
1595                        StringRef Namespace = StringRef())
1596       : PragmaHandler(PragmaKind(Kind, true)), Kind(Kind),
1597         Namespace(Namespace) {}
1598 
HandlePragma__anon095d5c770311::PragmaMessageHandler1599   void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1600                     Token &Tok) override {
1601     SourceLocation MessageLoc = Tok.getLocation();
1602     PP.Lex(Tok);
1603     bool ExpectClosingParen = false;
1604     switch (Tok.getKind()) {
1605     case tok::l_paren:
1606       // We have a MSVC style pragma message.
1607       ExpectClosingParen = true;
1608       // Read the string.
1609       PP.Lex(Tok);
1610       break;
1611     case tok::string_literal:
1612       // We have a GCC style pragma message, and we just read the string.
1613       break;
1614     default:
1615       PP.Diag(MessageLoc, diag::err_pragma_message_malformed) << Kind;
1616       return;
1617     }
1618 
1619     std::string MessageString;
1620     if (!PP.FinishLexStringLiteral(Tok, MessageString, PragmaKind(Kind),
1621                                    /*AllowMacroExpansion=*/true))
1622       return;
1623 
1624     if (ExpectClosingParen) {
1625       if (Tok.isNot(tok::r_paren)) {
1626         PP.Diag(Tok.getLocation(), diag::err_pragma_message_malformed) << Kind;
1627         return;
1628       }
1629       PP.Lex(Tok);  // eat the r_paren.
1630     }
1631 
1632     if (Tok.isNot(tok::eod)) {
1633       PP.Diag(Tok.getLocation(), diag::err_pragma_message_malformed) << Kind;
1634       return;
1635     }
1636 
1637     // Output the message.
1638     PP.Diag(MessageLoc, (Kind == PPCallbacks::PMK_Error)
1639                           ? diag::err_pragma_message
1640                           : diag::warn_pragma_message) << MessageString;
1641 
1642     // If the pragma is lexically sound, notify any interested PPCallbacks.
1643     if (PPCallbacks *Callbacks = PP.getPPCallbacks())
1644       Callbacks->PragmaMessage(MessageLoc, Namespace, Kind, MessageString);
1645   }
1646 };
1647 
1648 /// Handle the clang \#pragma module import extension. The syntax is:
1649 /// \code
1650 ///   #pragma clang module import some.module.name
1651 /// \endcode
1652 struct PragmaModuleImportHandler : public PragmaHandler {
PragmaModuleImportHandler__anon095d5c770311::PragmaModuleImportHandler1653   PragmaModuleImportHandler() : PragmaHandler("import") {}
1654 
HandlePragma__anon095d5c770311::PragmaModuleImportHandler1655   void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1656                     Token &Tok) override {
1657     SourceLocation ImportLoc = Tok.getLocation();
1658 
1659     // Read the module name.
1660     llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 8>
1661         ModuleName;
1662     if (LexModuleName(PP, Tok, ModuleName))
1663       return;
1664 
1665     if (Tok.isNot(tok::eod))
1666       PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
1667 
1668     // If we have a non-empty module path, load the named module.
1669     Module *Imported =
1670         PP.getModuleLoader().loadModule(ImportLoc, ModuleName, Module::Hidden,
1671                                       /*IsInclusionDirective=*/false);
1672     if (!Imported)
1673       return;
1674 
1675     PP.makeModuleVisible(Imported, ImportLoc);
1676     PP.EnterAnnotationToken(SourceRange(ImportLoc, ModuleName.back().second),
1677                             tok::annot_module_include, Imported);
1678     if (auto *CB = PP.getPPCallbacks())
1679       CB->moduleImport(ImportLoc, ModuleName, Imported);
1680   }
1681 };
1682 
1683 /// Handle the clang \#pragma module begin extension. The syntax is:
1684 /// \code
1685 ///   #pragma clang module begin some.module.name
1686 ///   ...
1687 ///   #pragma clang module end
1688 /// \endcode
1689 struct PragmaModuleBeginHandler : public PragmaHandler {
PragmaModuleBeginHandler__anon095d5c770311::PragmaModuleBeginHandler1690   PragmaModuleBeginHandler() : PragmaHandler("begin") {}
1691 
HandlePragma__anon095d5c770311::PragmaModuleBeginHandler1692   void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1693                     Token &Tok) override {
1694     SourceLocation BeginLoc = Tok.getLocation();
1695 
1696     // Read the module name.
1697     llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 8>
1698         ModuleName;
1699     if (LexModuleName(PP, Tok, ModuleName))
1700       return;
1701 
1702     if (Tok.isNot(tok::eod))
1703       PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
1704 
1705     // We can only enter submodules of the current module.
1706     StringRef Current = PP.getLangOpts().CurrentModule;
1707     if (ModuleName.front().first->getName() != Current) {
1708       PP.Diag(ModuleName.front().second, diag::err_pp_module_begin_wrong_module)
1709         << ModuleName.front().first << (ModuleName.size() > 1)
1710         << Current.empty() << Current;
1711       return;
1712     }
1713 
1714     // Find the module we're entering. We require that a module map for it
1715     // be loaded or implicitly loadable.
1716     auto &HSI = PP.getHeaderSearchInfo();
1717     Module *M = HSI.lookupModule(Current, ModuleName.front().second);
1718     if (!M) {
1719       PP.Diag(ModuleName.front().second,
1720               diag::err_pp_module_begin_no_module_map) << Current;
1721       return;
1722     }
1723     for (unsigned I = 1; I != ModuleName.size(); ++I) {
1724       auto *NewM = M->findOrInferSubmodule(ModuleName[I].first->getName());
1725       if (!NewM) {
1726         PP.Diag(ModuleName[I].second, diag::err_pp_module_begin_no_submodule)
1727           << M->getFullModuleName() << ModuleName[I].first;
1728         return;
1729       }
1730       M = NewM;
1731     }
1732 
1733     // If the module isn't available, it doesn't make sense to enter it.
1734     if (Preprocessor::checkModuleIsAvailable(
1735             PP.getLangOpts(), PP.getTargetInfo(), PP.getDiagnostics(), M)) {
1736       PP.Diag(BeginLoc, diag::note_pp_module_begin_here)
1737         << M->getTopLevelModuleName();
1738       return;
1739     }
1740 
1741     // Enter the scope of the submodule.
1742     PP.EnterSubmodule(M, BeginLoc, /*ForPragma*/true);
1743     PP.EnterAnnotationToken(SourceRange(BeginLoc, ModuleName.back().second),
1744                             tok::annot_module_begin, M);
1745   }
1746 };
1747 
1748 /// Handle the clang \#pragma module end extension.
1749 struct PragmaModuleEndHandler : public PragmaHandler {
PragmaModuleEndHandler__anon095d5c770311::PragmaModuleEndHandler1750   PragmaModuleEndHandler() : PragmaHandler("end") {}
1751 
HandlePragma__anon095d5c770311::PragmaModuleEndHandler1752   void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1753                     Token &Tok) override {
1754     SourceLocation Loc = Tok.getLocation();
1755 
1756     PP.LexUnexpandedToken(Tok);
1757     if (Tok.isNot(tok::eod))
1758       PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
1759 
1760     Module *M = PP.LeaveSubmodule(/*ForPragma*/true);
1761     if (M)
1762       PP.EnterAnnotationToken(SourceRange(Loc), tok::annot_module_end, M);
1763     else
1764       PP.Diag(Loc, diag::err_pp_module_end_without_module_begin);
1765   }
1766 };
1767 
1768 /// Handle the clang \#pragma module build extension.
1769 struct PragmaModuleBuildHandler : public PragmaHandler {
PragmaModuleBuildHandler__anon095d5c770311::PragmaModuleBuildHandler1770   PragmaModuleBuildHandler() : PragmaHandler("build") {}
1771 
HandlePragma__anon095d5c770311::PragmaModuleBuildHandler1772   void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1773                     Token &Tok) override {
1774     PP.HandlePragmaModuleBuild(Tok);
1775   }
1776 };
1777 
1778 /// Handle the clang \#pragma module load extension.
1779 struct PragmaModuleLoadHandler : public PragmaHandler {
PragmaModuleLoadHandler__anon095d5c770311::PragmaModuleLoadHandler1780   PragmaModuleLoadHandler() : PragmaHandler("load") {}
1781 
HandlePragma__anon095d5c770311::PragmaModuleLoadHandler1782   void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1783                     Token &Tok) override {
1784     SourceLocation Loc = Tok.getLocation();
1785 
1786     // Read the module name.
1787     llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 8>
1788         ModuleName;
1789     if (LexModuleName(PP, Tok, ModuleName))
1790       return;
1791 
1792     if (Tok.isNot(tok::eod))
1793       PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
1794 
1795     // Load the module, don't make it visible.
1796     PP.getModuleLoader().loadModule(Loc, ModuleName, Module::Hidden,
1797                                     /*IsInclusionDirective=*/false);
1798   }
1799 };
1800 
1801 /// PragmaPushMacroHandler - "\#pragma push_macro" saves the value of the
1802 /// macro on the top of the stack.
1803 struct PragmaPushMacroHandler : public PragmaHandler {
PragmaPushMacroHandler__anon095d5c770311::PragmaPushMacroHandler1804   PragmaPushMacroHandler() : PragmaHandler("push_macro") {}
1805 
HandlePragma__anon095d5c770311::PragmaPushMacroHandler1806   void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1807                     Token &PushMacroTok) override {
1808     PP.HandlePragmaPushMacro(PushMacroTok);
1809   }
1810 };
1811 
1812 /// PragmaPopMacroHandler - "\#pragma pop_macro" sets the value of the
1813 /// macro to the value on the top of the stack.
1814 struct PragmaPopMacroHandler : public PragmaHandler {
PragmaPopMacroHandler__anon095d5c770311::PragmaPopMacroHandler1815   PragmaPopMacroHandler() : PragmaHandler("pop_macro") {}
1816 
HandlePragma__anon095d5c770311::PragmaPopMacroHandler1817   void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1818                     Token &PopMacroTok) override {
1819     PP.HandlePragmaPopMacro(PopMacroTok);
1820   }
1821 };
1822 
1823 /// PragmaARCCFCodeAuditedHandler -
1824 ///   \#pragma clang arc_cf_code_audited begin/end
1825 struct PragmaARCCFCodeAuditedHandler : public PragmaHandler {
PragmaARCCFCodeAuditedHandler__anon095d5c770311::PragmaARCCFCodeAuditedHandler1826   PragmaARCCFCodeAuditedHandler() : PragmaHandler("arc_cf_code_audited") {}
1827 
HandlePragma__anon095d5c770311::PragmaARCCFCodeAuditedHandler1828   void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1829                     Token &NameTok) override {
1830     SourceLocation Loc = NameTok.getLocation();
1831     bool IsBegin;
1832 
1833     Token Tok;
1834 
1835     // Lex the 'begin' or 'end'.
1836     PP.LexUnexpandedToken(Tok);
1837     const IdentifierInfo *BeginEnd = Tok.getIdentifierInfo();
1838     if (BeginEnd && BeginEnd->isStr("begin")) {
1839       IsBegin = true;
1840     } else if (BeginEnd && BeginEnd->isStr("end")) {
1841       IsBegin = false;
1842     } else {
1843       PP.Diag(Tok.getLocation(), diag::err_pp_arc_cf_code_audited_syntax);
1844       return;
1845     }
1846 
1847     // Verify that this is followed by EOD.
1848     PP.LexUnexpandedToken(Tok);
1849     if (Tok.isNot(tok::eod))
1850       PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
1851 
1852     // The start location of the active audit.
1853     SourceLocation BeginLoc = PP.getPragmaARCCFCodeAuditedInfo().second;
1854 
1855     // The start location we want after processing this.
1856     SourceLocation NewLoc;
1857 
1858     if (IsBegin) {
1859       // Complain about attempts to re-enter an audit.
1860       if (BeginLoc.isValid()) {
1861         PP.Diag(Loc, diag::err_pp_double_begin_of_arc_cf_code_audited);
1862         PP.Diag(BeginLoc, diag::note_pragma_entered_here);
1863       }
1864       NewLoc = Loc;
1865     } else {
1866       // Complain about attempts to leave an audit that doesn't exist.
1867       if (!BeginLoc.isValid()) {
1868         PP.Diag(Loc, diag::err_pp_unmatched_end_of_arc_cf_code_audited);
1869         return;
1870       }
1871       NewLoc = SourceLocation();
1872     }
1873 
1874     PP.setPragmaARCCFCodeAuditedInfo(NameTok.getIdentifierInfo(), NewLoc);
1875   }
1876 };
1877 
1878 /// PragmaAssumeNonNullHandler -
1879 ///   \#pragma clang assume_nonnull begin/end
1880 struct PragmaAssumeNonNullHandler : public PragmaHandler {
PragmaAssumeNonNullHandler__anon095d5c770311::PragmaAssumeNonNullHandler1881   PragmaAssumeNonNullHandler() : PragmaHandler("assume_nonnull") {}
1882 
HandlePragma__anon095d5c770311::PragmaAssumeNonNullHandler1883   void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1884                     Token &NameTok) override {
1885     SourceLocation Loc = NameTok.getLocation();
1886     bool IsBegin;
1887 
1888     Token Tok;
1889 
1890     // Lex the 'begin' or 'end'.
1891     PP.LexUnexpandedToken(Tok);
1892     const IdentifierInfo *BeginEnd = Tok.getIdentifierInfo();
1893     if (BeginEnd && BeginEnd->isStr("begin")) {
1894       IsBegin = true;
1895     } else if (BeginEnd && BeginEnd->isStr("end")) {
1896       IsBegin = false;
1897     } else {
1898       PP.Diag(Tok.getLocation(), diag::err_pp_assume_nonnull_syntax);
1899       return;
1900     }
1901 
1902     // Verify that this is followed by EOD.
1903     PP.LexUnexpandedToken(Tok);
1904     if (Tok.isNot(tok::eod))
1905       PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
1906 
1907     // The start location of the active audit.
1908     SourceLocation BeginLoc = PP.getPragmaAssumeNonNullLoc();
1909 
1910     // The start location we want after processing this.
1911     SourceLocation NewLoc;
1912     PPCallbacks *Callbacks = PP.getPPCallbacks();
1913 
1914     if (IsBegin) {
1915       // Complain about attempts to re-enter an audit.
1916       if (BeginLoc.isValid()) {
1917         PP.Diag(Loc, diag::err_pp_double_begin_of_assume_nonnull);
1918         PP.Diag(BeginLoc, diag::note_pragma_entered_here);
1919       }
1920       NewLoc = Loc;
1921       if (Callbacks)
1922         Callbacks->PragmaAssumeNonNullBegin(NewLoc);
1923     } else {
1924       // Complain about attempts to leave an audit that doesn't exist.
1925       if (!BeginLoc.isValid()) {
1926         PP.Diag(Loc, diag::err_pp_unmatched_end_of_assume_nonnull);
1927         return;
1928       }
1929       NewLoc = SourceLocation();
1930       if (Callbacks)
1931         Callbacks->PragmaAssumeNonNullEnd(NewLoc);
1932     }
1933 
1934     PP.setPragmaAssumeNonNullLoc(NewLoc);
1935   }
1936 };
1937 
1938 /// Handle "\#pragma region [...]"
1939 ///
1940 /// The syntax is
1941 /// \code
1942 ///   #pragma region [optional name]
1943 ///   #pragma endregion [optional comment]
1944 /// \endcode
1945 ///
1946 /// \note This is
1947 /// <a href="http://msdn.microsoft.com/en-us/library/b6xkz944(v=vs.80).aspx">editor-only</a>
1948 /// pragma, just skipped by compiler.
1949 struct PragmaRegionHandler : public PragmaHandler {
PragmaRegionHandler__anon095d5c770311::PragmaRegionHandler1950   PragmaRegionHandler(const char *pragma) : PragmaHandler(pragma) {}
1951 
HandlePragma__anon095d5c770311::PragmaRegionHandler1952   void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1953                     Token &NameTok) override {
1954     // #pragma region: endregion matches can be verified
1955     // __pragma(region): no sense, but ignored by msvc
1956     // _Pragma is not valid for MSVC, but there isn't any point
1957     // to handle a _Pragma differently.
1958   }
1959 };
1960 
1961 /// "\#pragma managed"
1962 /// "\#pragma managed(...)"
1963 /// "\#pragma unmanaged"
1964 /// MSVC ignores this pragma when not compiling using /clr, which clang doesn't
1965 /// support. We parse it and ignore it to avoid -Wunknown-pragma warnings.
1966 struct PragmaManagedHandler : public EmptyPragmaHandler {
PragmaManagedHandler__anon095d5c770311::PragmaManagedHandler1967   PragmaManagedHandler(const char *pragma) : EmptyPragmaHandler(pragma) {}
1968 };
1969 
1970 /// This handles parsing pragmas that take a macro name and optional message
HandleMacroAnnotationPragma(Preprocessor & PP,Token & Tok,const char * Pragma,std::string & MessageString)1971 static IdentifierInfo *HandleMacroAnnotationPragma(Preprocessor &PP, Token &Tok,
1972                                                    const char *Pragma,
1973                                                    std::string &MessageString) {
1974   PP.Lex(Tok);
1975   if (Tok.isNot(tok::l_paren)) {
1976     PP.Diag(Tok, diag::err_expected) << "(";
1977     return nullptr;
1978   }
1979 
1980   PP.LexUnexpandedToken(Tok);
1981   if (!Tok.is(tok::identifier)) {
1982     PP.Diag(Tok, diag::err_expected) << tok::identifier;
1983     return nullptr;
1984   }
1985   IdentifierInfo *II = Tok.getIdentifierInfo();
1986 
1987   if (!II->hasMacroDefinition()) {
1988     PP.Diag(Tok, diag::err_pp_visibility_non_macro) << II;
1989     return nullptr;
1990   }
1991 
1992   PP.Lex(Tok);
1993   if (Tok.is(tok::comma)) {
1994     PP.Lex(Tok);
1995     if (!PP.FinishLexStringLiteral(Tok, MessageString, Pragma,
1996                                    /*AllowMacroExpansion=*/true))
1997       return nullptr;
1998   }
1999 
2000   if (Tok.isNot(tok::r_paren)) {
2001     PP.Diag(Tok, diag::err_expected) << ")";
2002     return nullptr;
2003   }
2004   return II;
2005 }
2006 
2007 /// "\#pragma clang deprecated(...)"
2008 ///
2009 /// The syntax is
2010 /// \code
2011 ///   #pragma clang deprecate(MACRO_NAME [, Message])
2012 /// \endcode
2013 struct PragmaDeprecatedHandler : public PragmaHandler {
PragmaDeprecatedHandler__anon095d5c770311::PragmaDeprecatedHandler2014   PragmaDeprecatedHandler() : PragmaHandler("deprecated") {}
2015 
HandlePragma__anon095d5c770311::PragmaDeprecatedHandler2016   void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
2017                     Token &Tok) override {
2018     std::string MessageString;
2019 
2020     if (IdentifierInfo *II = HandleMacroAnnotationPragma(
2021             PP, Tok, "#pragma clang deprecated", MessageString)) {
2022       II->setIsDeprecatedMacro(true);
2023       PP.addMacroDeprecationMsg(II, std::move(MessageString),
2024                                 Tok.getLocation());
2025     }
2026   }
2027 };
2028 
2029 /// "\#pragma clang restrict_expansion(...)"
2030 ///
2031 /// The syntax is
2032 /// \code
2033 ///   #pragma clang restrict_expansion(MACRO_NAME [, Message])
2034 /// \endcode
2035 struct PragmaRestrictExpansionHandler : public PragmaHandler {
PragmaRestrictExpansionHandler__anon095d5c770311::PragmaRestrictExpansionHandler2036   PragmaRestrictExpansionHandler() : PragmaHandler("restrict_expansion") {}
2037 
HandlePragma__anon095d5c770311::PragmaRestrictExpansionHandler2038   void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
2039                     Token &Tok) override {
2040     std::string MessageString;
2041 
2042     if (IdentifierInfo *II = HandleMacroAnnotationPragma(
2043             PP, Tok, "#pragma clang restrict_expansion", MessageString)) {
2044       II->setIsRestrictExpansion(true);
2045       PP.addRestrictExpansionMsg(II, std::move(MessageString),
2046                                  Tok.getLocation());
2047     }
2048   }
2049 };
2050 
2051 /// "\#pragma clang final(...)"
2052 ///
2053 /// The syntax is
2054 /// \code
2055 ///   #pragma clang final(MACRO_NAME)
2056 /// \endcode
2057 struct PragmaFinalHandler : public PragmaHandler {
PragmaFinalHandler__anon095d5c770311::PragmaFinalHandler2058   PragmaFinalHandler() : PragmaHandler("final") {}
2059 
HandlePragma__anon095d5c770311::PragmaFinalHandler2060   void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
2061                     Token &Tok) override {
2062     PP.Lex(Tok);
2063     if (Tok.isNot(tok::l_paren)) {
2064       PP.Diag(Tok, diag::err_expected) << "(";
2065       return;
2066     }
2067 
2068     PP.LexUnexpandedToken(Tok);
2069     if (!Tok.is(tok::identifier)) {
2070       PP.Diag(Tok, diag::err_expected) << tok::identifier;
2071       return;
2072     }
2073     IdentifierInfo *II = Tok.getIdentifierInfo();
2074 
2075     if (!II->hasMacroDefinition()) {
2076       PP.Diag(Tok, diag::err_pp_visibility_non_macro) << II;
2077       return;
2078     }
2079 
2080     PP.Lex(Tok);
2081     if (Tok.isNot(tok::r_paren)) {
2082       PP.Diag(Tok, diag::err_expected) << ")";
2083       return;
2084     }
2085     II->setIsFinal(true);
2086     PP.addFinalLoc(II, Tok.getLocation());
2087   }
2088 };
2089 
2090 } // namespace
2091 
2092 /// RegisterBuiltinPragmas - Install the standard preprocessor pragmas:
2093 /// \#pragma GCC poison/system_header/dependency and \#pragma once.
RegisterBuiltinPragmas()2094 void Preprocessor::RegisterBuiltinPragmas() {
2095   AddPragmaHandler(new PragmaOnceHandler());
2096   AddPragmaHandler(new PragmaMarkHandler());
2097   AddPragmaHandler(new PragmaPushMacroHandler());
2098   AddPragmaHandler(new PragmaPopMacroHandler());
2099   AddPragmaHandler(new PragmaMessageHandler(PPCallbacks::PMK_Message));
2100 
2101   // #pragma GCC ...
2102   AddPragmaHandler("GCC", new PragmaPoisonHandler());
2103   AddPragmaHandler("GCC", new PragmaSystemHeaderHandler());
2104   AddPragmaHandler("GCC", new PragmaDependencyHandler());
2105   AddPragmaHandler("GCC", new PragmaDiagnosticHandler("GCC"));
2106   AddPragmaHandler("GCC", new PragmaMessageHandler(PPCallbacks::PMK_Warning,
2107                                                    "GCC"));
2108   AddPragmaHandler("GCC", new PragmaMessageHandler(PPCallbacks::PMK_Error,
2109                                                    "GCC"));
2110   // #pragma clang ...
2111   AddPragmaHandler("clang", new PragmaPoisonHandler());
2112   AddPragmaHandler("clang", new PragmaSystemHeaderHandler());
2113   AddPragmaHandler("clang", new PragmaDebugHandler());
2114   AddPragmaHandler("clang", new PragmaDependencyHandler());
2115   AddPragmaHandler("clang", new PragmaDiagnosticHandler("clang"));
2116   AddPragmaHandler("clang", new PragmaARCCFCodeAuditedHandler());
2117   AddPragmaHandler("clang", new PragmaAssumeNonNullHandler());
2118   AddPragmaHandler("clang", new PragmaDeprecatedHandler());
2119   AddPragmaHandler("clang", new PragmaRestrictExpansionHandler());
2120   AddPragmaHandler("clang", new PragmaFinalHandler());
2121 
2122   // #pragma clang module ...
2123   auto *ModuleHandler = new PragmaNamespace("module");
2124   AddPragmaHandler("clang", ModuleHandler);
2125   ModuleHandler->AddPragma(new PragmaModuleImportHandler());
2126   ModuleHandler->AddPragma(new PragmaModuleBeginHandler());
2127   ModuleHandler->AddPragma(new PragmaModuleEndHandler());
2128   ModuleHandler->AddPragma(new PragmaModuleBuildHandler());
2129   ModuleHandler->AddPragma(new PragmaModuleLoadHandler());
2130 
2131   // Add region pragmas.
2132   AddPragmaHandler(new PragmaRegionHandler("region"));
2133   AddPragmaHandler(new PragmaRegionHandler("endregion"));
2134 
2135   // MS extensions.
2136   if (LangOpts.MicrosoftExt) {
2137     AddPragmaHandler(new PragmaWarningHandler());
2138     AddPragmaHandler(new PragmaExecCharsetHandler());
2139     AddPragmaHandler(new PragmaIncludeAliasHandler());
2140     AddPragmaHandler(new PragmaHdrstopHandler());
2141     AddPragmaHandler(new PragmaSystemHeaderHandler());
2142     AddPragmaHandler(new PragmaManagedHandler("managed"));
2143     AddPragmaHandler(new PragmaManagedHandler("unmanaged"));
2144   }
2145 
2146   // Pragmas added by plugins
2147   for (const PragmaHandlerRegistry::entry &handler :
2148        PragmaHandlerRegistry::entries()) {
2149     AddPragmaHandler(handler.instantiate().release());
2150   }
2151 }
2152 
2153 /// Ignore all pragmas, useful for modes such as -Eonly which would otherwise
2154 /// warn about those pragmas being unknown.
IgnorePragmas()2155 void Preprocessor::IgnorePragmas() {
2156   AddPragmaHandler(new EmptyPragmaHandler());
2157   // Also ignore all pragmas in all namespaces created
2158   // in Preprocessor::RegisterBuiltinPragmas().
2159   AddPragmaHandler("GCC", new EmptyPragmaHandler());
2160   AddPragmaHandler("clang", new EmptyPragmaHandler());
2161 }
2162