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