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