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