xref: /freebsd-src/contrib/llvm-project/clang/lib/Lex/Pragma.cpp (revision 17f01e9963948a18f55eb97173123702c5dae671)
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 <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       if (!PP.getPreprocessorOpts().DisablePragmaDebugCrash)
1039         llvm_unreachable("This is an assertion!");
1040     } else if (II->isStr("crash")) {
1041       if (!PP.getPreprocessorOpts().DisablePragmaDebugCrash)
1042         LLVM_BUILTIN_TRAP;
1043     } else if (II->isStr("parser_crash")) {
1044       if (!PP.getPreprocessorOpts().DisablePragmaDebugCrash) {
1045         Token Crasher;
1046         Crasher.startToken();
1047         Crasher.setKind(tok::annot_pragma_parser_crash);
1048         Crasher.setAnnotationRange(SourceRange(Tok.getLocation()));
1049         PP.EnterToken(Crasher, /*IsReinject*/ false);
1050       }
1051     } else if (II->isStr("dump")) {
1052       Token Identifier;
1053       PP.LexUnexpandedToken(Identifier);
1054       if (auto *DumpII = Identifier.getIdentifierInfo()) {
1055         Token DumpAnnot;
1056         DumpAnnot.startToken();
1057         DumpAnnot.setKind(tok::annot_pragma_dump);
1058         DumpAnnot.setAnnotationRange(
1059             SourceRange(Tok.getLocation(), Identifier.getLocation()));
1060         DumpAnnot.setAnnotationValue(DumpII);
1061         PP.DiscardUntilEndOfDirective();
1062         PP.EnterToken(DumpAnnot, /*IsReinject*/false);
1063       } else {
1064         PP.Diag(Identifier, diag::warn_pragma_debug_missing_argument)
1065             << II->getName();
1066       }
1067     } else if (II->isStr("diag_mapping")) {
1068       Token DiagName;
1069       PP.LexUnexpandedToken(DiagName);
1070       if (DiagName.is(tok::eod))
1071         PP.getDiagnostics().dump();
1072       else if (DiagName.is(tok::string_literal) && !DiagName.hasUDSuffix()) {
1073         StringLiteralParser Literal(DiagName, PP);
1074         if (Literal.hadError)
1075           return;
1076         PP.getDiagnostics().dump(Literal.GetString());
1077       } else {
1078         PP.Diag(DiagName, diag::warn_pragma_debug_missing_argument)
1079             << II->getName();
1080       }
1081     } else if (II->isStr("llvm_fatal_error")) {
1082       if (!PP.getPreprocessorOpts().DisablePragmaDebugCrash)
1083         llvm::report_fatal_error("#pragma clang __debug llvm_fatal_error");
1084     } else if (II->isStr("llvm_unreachable")) {
1085       if (!PP.getPreprocessorOpts().DisablePragmaDebugCrash)
1086         llvm_unreachable("#pragma clang __debug llvm_unreachable");
1087     } else if (II->isStr("macro")) {
1088       Token MacroName;
1089       PP.LexUnexpandedToken(MacroName);
1090       auto *MacroII = MacroName.getIdentifierInfo();
1091       if (MacroII)
1092         PP.dumpMacroInfo(MacroII);
1093       else
1094         PP.Diag(MacroName, diag::warn_pragma_debug_missing_argument)
1095             << II->getName();
1096     } else if (II->isStr("module_map")) {
1097       llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 8>
1098           ModuleName;
1099       if (LexModuleName(PP, Tok, ModuleName))
1100         return;
1101       ModuleMap &MM = PP.getHeaderSearchInfo().getModuleMap();
1102       Module *M = nullptr;
1103       for (auto IIAndLoc : ModuleName) {
1104         M = MM.lookupModuleQualified(IIAndLoc.first->getName(), M);
1105         if (!M) {
1106           PP.Diag(IIAndLoc.second, diag::warn_pragma_debug_unknown_module)
1107               << IIAndLoc.first;
1108           return;
1109         }
1110       }
1111       M->dump();
1112     } else if (II->isStr("overflow_stack")) {
1113       if (!PP.getPreprocessorOpts().DisablePragmaDebugCrash)
1114         DebugOverflowStack();
1115     } else if (II->isStr("captured")) {
1116       HandleCaptured(PP);
1117     } else {
1118       PP.Diag(Tok, diag::warn_pragma_debug_unexpected_command)
1119         << II->getName();
1120     }
1121 
1122     PPCallbacks *Callbacks = PP.getPPCallbacks();
1123     if (Callbacks)
1124       Callbacks->PragmaDebug(Tok.getLocation(), II->getName());
1125   }
1126 
1127   void HandleCaptured(Preprocessor &PP) {
1128     Token Tok;
1129     PP.LexUnexpandedToken(Tok);
1130 
1131     if (Tok.isNot(tok::eod)) {
1132       PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol)
1133         << "pragma clang __debug captured";
1134       return;
1135     }
1136 
1137     SourceLocation NameLoc = Tok.getLocation();
1138     MutableArrayRef<Token> Toks(
1139         PP.getPreprocessorAllocator().Allocate<Token>(1), 1);
1140     Toks[0].startToken();
1141     Toks[0].setKind(tok::annot_pragma_captured);
1142     Toks[0].setLocation(NameLoc);
1143 
1144     PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true,
1145                         /*IsReinject=*/false);
1146   }
1147 
1148 // Disable MSVC warning about runtime stack overflow.
1149 #ifdef _MSC_VER
1150     #pragma warning(disable : 4717)
1151 #endif
1152   static void DebugOverflowStack(void (*P)() = nullptr) {
1153     void (*volatile Self)(void(*P)()) = DebugOverflowStack;
1154     Self(reinterpret_cast<void(*)()>(Self));
1155   }
1156 #ifdef _MSC_VER
1157     #pragma warning(default : 4717)
1158 #endif
1159 };
1160 
1161 /// PragmaDiagnosticHandler - e.g. '\#pragma GCC diagnostic ignored "-Wformat"'
1162 struct PragmaDiagnosticHandler : public PragmaHandler {
1163 private:
1164   const char *Namespace;
1165 
1166 public:
1167   explicit PragmaDiagnosticHandler(const char *NS)
1168       : PragmaHandler("diagnostic"), Namespace(NS) {}
1169 
1170   void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1171                     Token &DiagToken) override {
1172     SourceLocation DiagLoc = DiagToken.getLocation();
1173     Token Tok;
1174     PP.LexUnexpandedToken(Tok);
1175     if (Tok.isNot(tok::identifier)) {
1176       PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
1177       return;
1178     }
1179     IdentifierInfo *II = Tok.getIdentifierInfo();
1180     PPCallbacks *Callbacks = PP.getPPCallbacks();
1181 
1182     if (II->isStr("pop")) {
1183       if (!PP.getDiagnostics().popMappings(DiagLoc))
1184         PP.Diag(Tok, diag::warn_pragma_diagnostic_cannot_pop);
1185       else if (Callbacks)
1186         Callbacks->PragmaDiagnosticPop(DiagLoc, Namespace);
1187       return;
1188     } else if (II->isStr("push")) {
1189       PP.getDiagnostics().pushMappings(DiagLoc);
1190       if (Callbacks)
1191         Callbacks->PragmaDiagnosticPush(DiagLoc, Namespace);
1192       return;
1193     }
1194 
1195     diag::Severity SV = llvm::StringSwitch<diag::Severity>(II->getName())
1196                             .Case("ignored", diag::Severity::Ignored)
1197                             .Case("warning", diag::Severity::Warning)
1198                             .Case("error", diag::Severity::Error)
1199                             .Case("fatal", diag::Severity::Fatal)
1200                             .Default(diag::Severity());
1201 
1202     if (SV == diag::Severity()) {
1203       PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
1204       return;
1205     }
1206 
1207     PP.LexUnexpandedToken(Tok);
1208     SourceLocation StringLoc = Tok.getLocation();
1209 
1210     std::string WarningName;
1211     if (!PP.FinishLexStringLiteral(Tok, WarningName, "pragma diagnostic",
1212                                    /*AllowMacroExpansion=*/false))
1213       return;
1214 
1215     if (Tok.isNot(tok::eod)) {
1216       PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token);
1217       return;
1218     }
1219 
1220     if (WarningName.size() < 3 || WarningName[0] != '-' ||
1221         (WarningName[1] != 'W' && WarningName[1] != 'R')) {
1222       PP.Diag(StringLoc, diag::warn_pragma_diagnostic_invalid_option);
1223       return;
1224     }
1225 
1226     diag::Flavor Flavor = WarningName[1] == 'W' ? diag::Flavor::WarningOrError
1227                                                 : diag::Flavor::Remark;
1228     StringRef Group = StringRef(WarningName).substr(2);
1229     bool unknownDiag = false;
1230     if (Group == "everything") {
1231       // Special handling for pragma clang diagnostic ... "-Weverything".
1232       // There is no formal group named "everything", so there has to be a
1233       // special case for it.
1234       PP.getDiagnostics().setSeverityForAll(Flavor, SV, DiagLoc);
1235     } else
1236       unknownDiag = PP.getDiagnostics().setSeverityForGroup(Flavor, Group, SV,
1237                                                             DiagLoc);
1238     if (unknownDiag)
1239       PP.Diag(StringLoc, diag::warn_pragma_diagnostic_unknown_warning)
1240         << WarningName;
1241     else if (Callbacks)
1242       Callbacks->PragmaDiagnostic(DiagLoc, Namespace, SV, WarningName);
1243   }
1244 };
1245 
1246 /// "\#pragma hdrstop [<header-name-string>]"
1247 struct PragmaHdrstopHandler : public PragmaHandler {
1248   PragmaHdrstopHandler() : PragmaHandler("hdrstop") {}
1249   void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1250                     Token &DepToken) override {
1251     PP.HandlePragmaHdrstop(DepToken);
1252   }
1253 };
1254 
1255 /// "\#pragma warning(...)".  MSVC's diagnostics do not map cleanly to clang's
1256 /// diagnostics, so we don't really implement this pragma.  We parse it and
1257 /// ignore it to avoid -Wunknown-pragma warnings.
1258 struct PragmaWarningHandler : public PragmaHandler {
1259   PragmaWarningHandler() : PragmaHandler("warning") {}
1260 
1261   void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1262                     Token &Tok) override {
1263     // Parse things like:
1264     // warning(push, 1)
1265     // warning(pop)
1266     // warning(disable : 1 2 3 ; error : 4 5 6 ; suppress : 7 8 9)
1267     SourceLocation DiagLoc = Tok.getLocation();
1268     PPCallbacks *Callbacks = PP.getPPCallbacks();
1269 
1270     PP.Lex(Tok);
1271     if (Tok.isNot(tok::l_paren)) {
1272       PP.Diag(Tok, diag::warn_pragma_warning_expected) << "(";
1273       return;
1274     }
1275 
1276     PP.Lex(Tok);
1277     IdentifierInfo *II = Tok.getIdentifierInfo();
1278 
1279     if (II && II->isStr("push")) {
1280       // #pragma warning( push[ ,n ] )
1281       int Level = -1;
1282       PP.Lex(Tok);
1283       if (Tok.is(tok::comma)) {
1284         PP.Lex(Tok);
1285         uint64_t Value;
1286         if (Tok.is(tok::numeric_constant) &&
1287             PP.parseSimpleIntegerLiteral(Tok, Value))
1288           Level = int(Value);
1289         if (Level < 0 || Level > 4) {
1290           PP.Diag(Tok, diag::warn_pragma_warning_push_level);
1291           return;
1292         }
1293       }
1294       if (Callbacks)
1295         Callbacks->PragmaWarningPush(DiagLoc, Level);
1296     } else if (II && II->isStr("pop")) {
1297       // #pragma warning( pop )
1298       PP.Lex(Tok);
1299       if (Callbacks)
1300         Callbacks->PragmaWarningPop(DiagLoc);
1301     } else {
1302       // #pragma warning( warning-specifier : warning-number-list
1303       //                  [; warning-specifier : warning-number-list...] )
1304       while (true) {
1305         II = Tok.getIdentifierInfo();
1306         if (!II && !Tok.is(tok::numeric_constant)) {
1307           PP.Diag(Tok, diag::warn_pragma_warning_spec_invalid);
1308           return;
1309         }
1310 
1311         // Figure out which warning specifier this is.
1312         bool SpecifierValid;
1313         StringRef Specifier;
1314         llvm::SmallString<1> SpecifierBuf;
1315         if (II) {
1316           Specifier = II->getName();
1317           SpecifierValid = llvm::StringSwitch<bool>(Specifier)
1318                                .Cases("default", "disable", "error", "once",
1319                                       "suppress", true)
1320                                .Default(false);
1321           // If we read a correct specifier, snatch next token (that should be
1322           // ":", checked later).
1323           if (SpecifierValid)
1324             PP.Lex(Tok);
1325         } else {
1326           // Token is a numeric constant. It should be either 1, 2, 3 or 4.
1327           uint64_t Value;
1328           Specifier = PP.getSpelling(Tok, SpecifierBuf);
1329           if (PP.parseSimpleIntegerLiteral(Tok, Value)) {
1330             SpecifierValid = (Value >= 1) && (Value <= 4);
1331           } else
1332             SpecifierValid = false;
1333           // Next token already snatched by parseSimpleIntegerLiteral.
1334         }
1335 
1336         if (!SpecifierValid) {
1337           PP.Diag(Tok, diag::warn_pragma_warning_spec_invalid);
1338           return;
1339         }
1340         if (Tok.isNot(tok::colon)) {
1341           PP.Diag(Tok, diag::warn_pragma_warning_expected) << ":";
1342           return;
1343         }
1344 
1345         // Collect the warning ids.
1346         SmallVector<int, 4> Ids;
1347         PP.Lex(Tok);
1348         while (Tok.is(tok::numeric_constant)) {
1349           uint64_t Value;
1350           if (!PP.parseSimpleIntegerLiteral(Tok, Value) || Value == 0 ||
1351               Value > std::numeric_limits<int>::max()) {
1352             PP.Diag(Tok, diag::warn_pragma_warning_expected_number);
1353             return;
1354           }
1355           Ids.push_back(int(Value));
1356         }
1357         if (Callbacks)
1358           Callbacks->PragmaWarning(DiagLoc, Specifier, Ids);
1359 
1360         // Parse the next specifier if there is a semicolon.
1361         if (Tok.isNot(tok::semi))
1362           break;
1363         PP.Lex(Tok);
1364       }
1365     }
1366 
1367     if (Tok.isNot(tok::r_paren)) {
1368       PP.Diag(Tok, diag::warn_pragma_warning_expected) << ")";
1369       return;
1370     }
1371 
1372     PP.Lex(Tok);
1373     if (Tok.isNot(tok::eod))
1374       PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma warning";
1375   }
1376 };
1377 
1378 /// "\#pragma execution_character_set(...)". MSVC supports this pragma only
1379 /// for "UTF-8". We parse it and ignore it if UTF-8 is provided and warn
1380 /// otherwise to avoid -Wunknown-pragma warnings.
1381 struct PragmaExecCharsetHandler : public PragmaHandler {
1382   PragmaExecCharsetHandler() : PragmaHandler("execution_character_set") {}
1383 
1384   void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1385                     Token &Tok) override {
1386     // Parse things like:
1387     // execution_character_set(push, "UTF-8")
1388     // execution_character_set(pop)
1389     SourceLocation DiagLoc = Tok.getLocation();
1390     PPCallbacks *Callbacks = PP.getPPCallbacks();
1391 
1392     PP.Lex(Tok);
1393     if (Tok.isNot(tok::l_paren)) {
1394       PP.Diag(Tok, diag::warn_pragma_exec_charset_expected) << "(";
1395       return;
1396     }
1397 
1398     PP.Lex(Tok);
1399     IdentifierInfo *II = Tok.getIdentifierInfo();
1400 
1401     if (II && II->isStr("push")) {
1402       // #pragma execution_character_set( push[ , string ] )
1403       PP.Lex(Tok);
1404       if (Tok.is(tok::comma)) {
1405         PP.Lex(Tok);
1406 
1407         std::string ExecCharset;
1408         if (!PP.FinishLexStringLiteral(Tok, ExecCharset,
1409                                        "pragma execution_character_set",
1410                                        /*AllowMacroExpansion=*/false))
1411           return;
1412 
1413         // MSVC supports either of these, but nothing else.
1414         if (ExecCharset != "UTF-8" && ExecCharset != "utf-8") {
1415           PP.Diag(Tok, diag::warn_pragma_exec_charset_push_invalid) << ExecCharset;
1416           return;
1417         }
1418       }
1419       if (Callbacks)
1420         Callbacks->PragmaExecCharsetPush(DiagLoc, "UTF-8");
1421     } else if (II && II->isStr("pop")) {
1422       // #pragma execution_character_set( pop )
1423       PP.Lex(Tok);
1424       if (Callbacks)
1425         Callbacks->PragmaExecCharsetPop(DiagLoc);
1426     } else {
1427       PP.Diag(Tok, diag::warn_pragma_exec_charset_spec_invalid);
1428       return;
1429     }
1430 
1431     if (Tok.isNot(tok::r_paren)) {
1432       PP.Diag(Tok, diag::warn_pragma_exec_charset_expected) << ")";
1433       return;
1434     }
1435 
1436     PP.Lex(Tok);
1437     if (Tok.isNot(tok::eod))
1438       PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma execution_character_set";
1439   }
1440 };
1441 
1442 /// PragmaIncludeAliasHandler - "\#pragma include_alias("...")".
1443 struct PragmaIncludeAliasHandler : public PragmaHandler {
1444   PragmaIncludeAliasHandler() : PragmaHandler("include_alias") {}
1445 
1446   void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1447                     Token &IncludeAliasTok) override {
1448     PP.HandlePragmaIncludeAlias(IncludeAliasTok);
1449   }
1450 };
1451 
1452 /// PragmaMessageHandler - Handle the microsoft and gcc \#pragma message
1453 /// extension.  The syntax is:
1454 /// \code
1455 ///   #pragma message(string)
1456 /// \endcode
1457 /// OR, in GCC mode:
1458 /// \code
1459 ///   #pragma message string
1460 /// \endcode
1461 /// string is a string, which is fully macro expanded, and permits string
1462 /// concatenation, embedded escape characters, etc... See MSDN for more details.
1463 /// Also handles \#pragma GCC warning and \#pragma GCC error which take the same
1464 /// form as \#pragma message.
1465 struct PragmaMessageHandler : public PragmaHandler {
1466 private:
1467   const PPCallbacks::PragmaMessageKind Kind;
1468   const StringRef Namespace;
1469 
1470   static const char* PragmaKind(PPCallbacks::PragmaMessageKind Kind,
1471                                 bool PragmaNameOnly = false) {
1472     switch (Kind) {
1473       case PPCallbacks::PMK_Message:
1474         return PragmaNameOnly ? "message" : "pragma message";
1475       case PPCallbacks::PMK_Warning:
1476         return PragmaNameOnly ? "warning" : "pragma warning";
1477       case PPCallbacks::PMK_Error:
1478         return PragmaNameOnly ? "error" : "pragma error";
1479     }
1480     llvm_unreachable("Unknown PragmaMessageKind!");
1481   }
1482 
1483 public:
1484   PragmaMessageHandler(PPCallbacks::PragmaMessageKind Kind,
1485                        StringRef Namespace = StringRef())
1486       : PragmaHandler(PragmaKind(Kind, true)), Kind(Kind),
1487         Namespace(Namespace) {}
1488 
1489   void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1490                     Token &Tok) override {
1491     SourceLocation MessageLoc = Tok.getLocation();
1492     PP.Lex(Tok);
1493     bool ExpectClosingParen = false;
1494     switch (Tok.getKind()) {
1495     case tok::l_paren:
1496       // We have a MSVC style pragma message.
1497       ExpectClosingParen = true;
1498       // Read the string.
1499       PP.Lex(Tok);
1500       break;
1501     case tok::string_literal:
1502       // We have a GCC style pragma message, and we just read the string.
1503       break;
1504     default:
1505       PP.Diag(MessageLoc, diag::err_pragma_message_malformed) << Kind;
1506       return;
1507     }
1508 
1509     std::string MessageString;
1510     if (!PP.FinishLexStringLiteral(Tok, MessageString, PragmaKind(Kind),
1511                                    /*AllowMacroExpansion=*/true))
1512       return;
1513 
1514     if (ExpectClosingParen) {
1515       if (Tok.isNot(tok::r_paren)) {
1516         PP.Diag(Tok.getLocation(), diag::err_pragma_message_malformed) << Kind;
1517         return;
1518       }
1519       PP.Lex(Tok);  // eat the r_paren.
1520     }
1521 
1522     if (Tok.isNot(tok::eod)) {
1523       PP.Diag(Tok.getLocation(), diag::err_pragma_message_malformed) << Kind;
1524       return;
1525     }
1526 
1527     // Output the message.
1528     PP.Diag(MessageLoc, (Kind == PPCallbacks::PMK_Error)
1529                           ? diag::err_pragma_message
1530                           : diag::warn_pragma_message) << MessageString;
1531 
1532     // If the pragma is lexically sound, notify any interested PPCallbacks.
1533     if (PPCallbacks *Callbacks = PP.getPPCallbacks())
1534       Callbacks->PragmaMessage(MessageLoc, Namespace, Kind, MessageString);
1535   }
1536 };
1537 
1538 /// Handle the clang \#pragma module import extension. The syntax is:
1539 /// \code
1540 ///   #pragma clang module import some.module.name
1541 /// \endcode
1542 struct PragmaModuleImportHandler : public PragmaHandler {
1543   PragmaModuleImportHandler() : PragmaHandler("import") {}
1544 
1545   void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1546                     Token &Tok) override {
1547     SourceLocation ImportLoc = Tok.getLocation();
1548 
1549     // Read the module name.
1550     llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 8>
1551         ModuleName;
1552     if (LexModuleName(PP, Tok, ModuleName))
1553       return;
1554 
1555     if (Tok.isNot(tok::eod))
1556       PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
1557 
1558     // If we have a non-empty module path, load the named module.
1559     Module *Imported =
1560         PP.getModuleLoader().loadModule(ImportLoc, ModuleName, Module::Hidden,
1561                                       /*IsInclusionDirective=*/false);
1562     if (!Imported)
1563       return;
1564 
1565     PP.makeModuleVisible(Imported, ImportLoc);
1566     PP.EnterAnnotationToken(SourceRange(ImportLoc, ModuleName.back().second),
1567                             tok::annot_module_include, Imported);
1568     if (auto *CB = PP.getPPCallbacks())
1569       CB->moduleImport(ImportLoc, ModuleName, Imported);
1570   }
1571 };
1572 
1573 /// Handle the clang \#pragma module begin extension. The syntax is:
1574 /// \code
1575 ///   #pragma clang module begin some.module.name
1576 ///   ...
1577 ///   #pragma clang module end
1578 /// \endcode
1579 struct PragmaModuleBeginHandler : public PragmaHandler {
1580   PragmaModuleBeginHandler() : PragmaHandler("begin") {}
1581 
1582   void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1583                     Token &Tok) override {
1584     SourceLocation BeginLoc = Tok.getLocation();
1585 
1586     // Read the module name.
1587     llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 8>
1588         ModuleName;
1589     if (LexModuleName(PP, Tok, ModuleName))
1590       return;
1591 
1592     if (Tok.isNot(tok::eod))
1593       PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
1594 
1595     // We can only enter submodules of the current module.
1596     StringRef Current = PP.getLangOpts().CurrentModule;
1597     if (ModuleName.front().first->getName() != Current) {
1598       PP.Diag(ModuleName.front().second, diag::err_pp_module_begin_wrong_module)
1599         << ModuleName.front().first << (ModuleName.size() > 1)
1600         << Current.empty() << Current;
1601       return;
1602     }
1603 
1604     // Find the module we're entering. We require that a module map for it
1605     // be loaded or implicitly loadable.
1606     auto &HSI = PP.getHeaderSearchInfo();
1607     Module *M = HSI.lookupModule(Current);
1608     if (!M) {
1609       PP.Diag(ModuleName.front().second,
1610               diag::err_pp_module_begin_no_module_map) << Current;
1611       return;
1612     }
1613     for (unsigned I = 1; I != ModuleName.size(); ++I) {
1614       auto *NewM = M->findOrInferSubmodule(ModuleName[I].first->getName());
1615       if (!NewM) {
1616         PP.Diag(ModuleName[I].second, diag::err_pp_module_begin_no_submodule)
1617           << M->getFullModuleName() << ModuleName[I].first;
1618         return;
1619       }
1620       M = NewM;
1621     }
1622 
1623     // If the module isn't available, it doesn't make sense to enter it.
1624     if (Preprocessor::checkModuleIsAvailable(
1625             PP.getLangOpts(), PP.getTargetInfo(), PP.getDiagnostics(), M)) {
1626       PP.Diag(BeginLoc, diag::note_pp_module_begin_here)
1627         << M->getTopLevelModuleName();
1628       return;
1629     }
1630 
1631     // Enter the scope of the submodule.
1632     PP.EnterSubmodule(M, BeginLoc, /*ForPragma*/true);
1633     PP.EnterAnnotationToken(SourceRange(BeginLoc, ModuleName.back().second),
1634                             tok::annot_module_begin, M);
1635   }
1636 };
1637 
1638 /// Handle the clang \#pragma module end extension.
1639 struct PragmaModuleEndHandler : public PragmaHandler {
1640   PragmaModuleEndHandler() : PragmaHandler("end") {}
1641 
1642   void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1643                     Token &Tok) override {
1644     SourceLocation Loc = Tok.getLocation();
1645 
1646     PP.LexUnexpandedToken(Tok);
1647     if (Tok.isNot(tok::eod))
1648       PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
1649 
1650     Module *M = PP.LeaveSubmodule(/*ForPragma*/true);
1651     if (M)
1652       PP.EnterAnnotationToken(SourceRange(Loc), tok::annot_module_end, M);
1653     else
1654       PP.Diag(Loc, diag::err_pp_module_end_without_module_begin);
1655   }
1656 };
1657 
1658 /// Handle the clang \#pragma module build extension.
1659 struct PragmaModuleBuildHandler : public PragmaHandler {
1660   PragmaModuleBuildHandler() : PragmaHandler("build") {}
1661 
1662   void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1663                     Token &Tok) override {
1664     PP.HandlePragmaModuleBuild(Tok);
1665   }
1666 };
1667 
1668 /// Handle the clang \#pragma module load extension.
1669 struct PragmaModuleLoadHandler : public PragmaHandler {
1670   PragmaModuleLoadHandler() : PragmaHandler("load") {}
1671 
1672   void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1673                     Token &Tok) override {
1674     SourceLocation Loc = Tok.getLocation();
1675 
1676     // Read the module name.
1677     llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 8>
1678         ModuleName;
1679     if (LexModuleName(PP, Tok, ModuleName))
1680       return;
1681 
1682     if (Tok.isNot(tok::eod))
1683       PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
1684 
1685     // Load the module, don't make it visible.
1686     PP.getModuleLoader().loadModule(Loc, ModuleName, Module::Hidden,
1687                                     /*IsInclusionDirective=*/false);
1688   }
1689 };
1690 
1691 /// PragmaPushMacroHandler - "\#pragma push_macro" saves the value of the
1692 /// macro on the top of the stack.
1693 struct PragmaPushMacroHandler : public PragmaHandler {
1694   PragmaPushMacroHandler() : PragmaHandler("push_macro") {}
1695 
1696   void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1697                     Token &PushMacroTok) override {
1698     PP.HandlePragmaPushMacro(PushMacroTok);
1699   }
1700 };
1701 
1702 /// PragmaPopMacroHandler - "\#pragma pop_macro" sets the value of the
1703 /// macro to the value on the top of the stack.
1704 struct PragmaPopMacroHandler : public PragmaHandler {
1705   PragmaPopMacroHandler() : PragmaHandler("pop_macro") {}
1706 
1707   void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1708                     Token &PopMacroTok) override {
1709     PP.HandlePragmaPopMacro(PopMacroTok);
1710   }
1711 };
1712 
1713 /// PragmaARCCFCodeAuditedHandler -
1714 ///   \#pragma clang arc_cf_code_audited begin/end
1715 struct PragmaARCCFCodeAuditedHandler : public PragmaHandler {
1716   PragmaARCCFCodeAuditedHandler() : PragmaHandler("arc_cf_code_audited") {}
1717 
1718   void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1719                     Token &NameTok) override {
1720     SourceLocation Loc = NameTok.getLocation();
1721     bool IsBegin;
1722 
1723     Token Tok;
1724 
1725     // Lex the 'begin' or 'end'.
1726     PP.LexUnexpandedToken(Tok);
1727     const IdentifierInfo *BeginEnd = Tok.getIdentifierInfo();
1728     if (BeginEnd && BeginEnd->isStr("begin")) {
1729       IsBegin = true;
1730     } else if (BeginEnd && BeginEnd->isStr("end")) {
1731       IsBegin = false;
1732     } else {
1733       PP.Diag(Tok.getLocation(), diag::err_pp_arc_cf_code_audited_syntax);
1734       return;
1735     }
1736 
1737     // Verify that this is followed by EOD.
1738     PP.LexUnexpandedToken(Tok);
1739     if (Tok.isNot(tok::eod))
1740       PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
1741 
1742     // The start location of the active audit.
1743     SourceLocation BeginLoc = PP.getPragmaARCCFCodeAuditedInfo().second;
1744 
1745     // The start location we want after processing this.
1746     SourceLocation NewLoc;
1747 
1748     if (IsBegin) {
1749       // Complain about attempts to re-enter an audit.
1750       if (BeginLoc.isValid()) {
1751         PP.Diag(Loc, diag::err_pp_double_begin_of_arc_cf_code_audited);
1752         PP.Diag(BeginLoc, diag::note_pragma_entered_here);
1753       }
1754       NewLoc = Loc;
1755     } else {
1756       // Complain about attempts to leave an audit that doesn't exist.
1757       if (!BeginLoc.isValid()) {
1758         PP.Diag(Loc, diag::err_pp_unmatched_end_of_arc_cf_code_audited);
1759         return;
1760       }
1761       NewLoc = SourceLocation();
1762     }
1763 
1764     PP.setPragmaARCCFCodeAuditedInfo(NameTok.getIdentifierInfo(), NewLoc);
1765   }
1766 };
1767 
1768 /// PragmaAssumeNonNullHandler -
1769 ///   \#pragma clang assume_nonnull begin/end
1770 struct PragmaAssumeNonNullHandler : public PragmaHandler {
1771   PragmaAssumeNonNullHandler() : PragmaHandler("assume_nonnull") {}
1772 
1773   void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1774                     Token &NameTok) override {
1775     SourceLocation Loc = NameTok.getLocation();
1776     bool IsBegin;
1777 
1778     Token Tok;
1779 
1780     // Lex the 'begin' or 'end'.
1781     PP.LexUnexpandedToken(Tok);
1782     const IdentifierInfo *BeginEnd = Tok.getIdentifierInfo();
1783     if (BeginEnd && BeginEnd->isStr("begin")) {
1784       IsBegin = true;
1785     } else if (BeginEnd && BeginEnd->isStr("end")) {
1786       IsBegin = false;
1787     } else {
1788       PP.Diag(Tok.getLocation(), diag::err_pp_assume_nonnull_syntax);
1789       return;
1790     }
1791 
1792     // Verify that this is followed by EOD.
1793     PP.LexUnexpandedToken(Tok);
1794     if (Tok.isNot(tok::eod))
1795       PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
1796 
1797     // The start location of the active audit.
1798     SourceLocation BeginLoc = PP.getPragmaAssumeNonNullLoc();
1799 
1800     // The start location we want after processing this.
1801     SourceLocation NewLoc;
1802     PPCallbacks *Callbacks = PP.getPPCallbacks();
1803 
1804     if (IsBegin) {
1805       // Complain about attempts to re-enter an audit.
1806       if (BeginLoc.isValid()) {
1807         PP.Diag(Loc, diag::err_pp_double_begin_of_assume_nonnull);
1808         PP.Diag(BeginLoc, diag::note_pragma_entered_here);
1809       }
1810       NewLoc = Loc;
1811       if (Callbacks)
1812         Callbacks->PragmaAssumeNonNullBegin(NewLoc);
1813     } else {
1814       // Complain about attempts to leave an audit that doesn't exist.
1815       if (!BeginLoc.isValid()) {
1816         PP.Diag(Loc, diag::err_pp_unmatched_end_of_assume_nonnull);
1817         return;
1818       }
1819       NewLoc = SourceLocation();
1820       if (Callbacks)
1821         Callbacks->PragmaAssumeNonNullEnd(NewLoc);
1822     }
1823 
1824     PP.setPragmaAssumeNonNullLoc(NewLoc);
1825   }
1826 };
1827 
1828 /// Handle "\#pragma region [...]"
1829 ///
1830 /// The syntax is
1831 /// \code
1832 ///   #pragma region [optional name]
1833 ///   #pragma endregion [optional comment]
1834 /// \endcode
1835 ///
1836 /// \note This is
1837 /// <a href="http://msdn.microsoft.com/en-us/library/b6xkz944(v=vs.80).aspx">editor-only</a>
1838 /// pragma, just skipped by compiler.
1839 struct PragmaRegionHandler : public PragmaHandler {
1840   PragmaRegionHandler(const char *pragma) : PragmaHandler(pragma) {}
1841 
1842   void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1843                     Token &NameTok) override {
1844     // #pragma region: endregion matches can be verified
1845     // __pragma(region): no sense, but ignored by msvc
1846     // _Pragma is not valid for MSVC, but there isn't any point
1847     // to handle a _Pragma differently.
1848   }
1849 };
1850 
1851 } // namespace
1852 
1853 /// RegisterBuiltinPragmas - Install the standard preprocessor pragmas:
1854 /// \#pragma GCC poison/system_header/dependency and \#pragma once.
1855 void Preprocessor::RegisterBuiltinPragmas() {
1856   AddPragmaHandler(new PragmaOnceHandler());
1857   AddPragmaHandler(new PragmaMarkHandler());
1858   AddPragmaHandler(new PragmaPushMacroHandler());
1859   AddPragmaHandler(new PragmaPopMacroHandler());
1860   AddPragmaHandler(new PragmaMessageHandler(PPCallbacks::PMK_Message));
1861 
1862   // #pragma GCC ...
1863   AddPragmaHandler("GCC", new PragmaPoisonHandler());
1864   AddPragmaHandler("GCC", new PragmaSystemHeaderHandler());
1865   AddPragmaHandler("GCC", new PragmaDependencyHandler());
1866   AddPragmaHandler("GCC", new PragmaDiagnosticHandler("GCC"));
1867   AddPragmaHandler("GCC", new PragmaMessageHandler(PPCallbacks::PMK_Warning,
1868                                                    "GCC"));
1869   AddPragmaHandler("GCC", new PragmaMessageHandler(PPCallbacks::PMK_Error,
1870                                                    "GCC"));
1871   // #pragma clang ...
1872   AddPragmaHandler("clang", new PragmaPoisonHandler());
1873   AddPragmaHandler("clang", new PragmaSystemHeaderHandler());
1874   AddPragmaHandler("clang", new PragmaDebugHandler());
1875   AddPragmaHandler("clang", new PragmaDependencyHandler());
1876   AddPragmaHandler("clang", new PragmaDiagnosticHandler("clang"));
1877   AddPragmaHandler("clang", new PragmaARCCFCodeAuditedHandler());
1878   AddPragmaHandler("clang", new PragmaAssumeNonNullHandler());
1879 
1880   // #pragma clang module ...
1881   auto *ModuleHandler = new PragmaNamespace("module");
1882   AddPragmaHandler("clang", ModuleHandler);
1883   ModuleHandler->AddPragma(new PragmaModuleImportHandler());
1884   ModuleHandler->AddPragma(new PragmaModuleBeginHandler());
1885   ModuleHandler->AddPragma(new PragmaModuleEndHandler());
1886   ModuleHandler->AddPragma(new PragmaModuleBuildHandler());
1887   ModuleHandler->AddPragma(new PragmaModuleLoadHandler());
1888 
1889   // Add region pragmas.
1890   AddPragmaHandler(new PragmaRegionHandler("region"));
1891   AddPragmaHandler(new PragmaRegionHandler("endregion"));
1892 
1893   // MS extensions.
1894   if (LangOpts.MicrosoftExt) {
1895     AddPragmaHandler(new PragmaWarningHandler());
1896     AddPragmaHandler(new PragmaExecCharsetHandler());
1897     AddPragmaHandler(new PragmaIncludeAliasHandler());
1898     AddPragmaHandler(new PragmaHdrstopHandler());
1899   }
1900 
1901   // Pragmas added by plugins
1902   for (PragmaHandlerRegistry::iterator it = PragmaHandlerRegistry::begin(),
1903                                        ie = PragmaHandlerRegistry::end();
1904        it != ie; ++it) {
1905     AddPragmaHandler(it->instantiate().release());
1906   }
1907 }
1908 
1909 /// Ignore all pragmas, useful for modes such as -Eonly which would otherwise
1910 /// warn about those pragmas being unknown.
1911 void Preprocessor::IgnorePragmas() {
1912   AddPragmaHandler(new EmptyPragmaHandler());
1913   // Also ignore all pragmas in all namespaces created
1914   // in Preprocessor::RegisterBuiltinPragmas().
1915   AddPragmaHandler("GCC", new EmptyPragmaHandler());
1916   AddPragmaHandler("clang", new EmptyPragmaHandler());
1917 }
1918