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