1193326Sed//===--- Pragma.cpp - Pragma registration and handling --------------------===//
2193326Sed//
3193326Sed//                     The LLVM Compiler Infrastructure
4193326Sed//
5193326Sed// This file is distributed under the University of Illinois Open Source
6193326Sed// License. See LICENSE.TXT for details.
7193326Sed//
8193326Sed//===----------------------------------------------------------------------===//
9193326Sed//
10193326Sed// This file implements the PragmaHandler/PragmaTable interfaces and implements
11193326Sed// pragma related methods of the Preprocessor class.
12193326Sed//
13193326Sed//===----------------------------------------------------------------------===//
14193326Sed
15193326Sed#include "clang/Lex/Pragma.h"
16252723Sdim#include "clang/Basic/FileManager.h"
17252723Sdim#include "clang/Basic/SourceManager.h"
18193326Sed#include "clang/Lex/HeaderSearch.h"
19252723Sdim#include "clang/Lex/LexDiagnostic.h"
20193326Sed#include "clang/Lex/LiteralSupport.h"
21252723Sdim#include "clang/Lex/MacroInfo.h"
22193326Sed#include "clang/Lex/Preprocessor.h"
23263509Sdim#include "llvm/ADT/STLExtras.h"
24263509Sdim#include "llvm/ADT/StringSwitch.h"
25212904Sdim#include "llvm/Support/CrashRecoveryContext.h"
26212904Sdim#include "llvm/Support/ErrorHandling.h"
27195341Sed#include <algorithm>
28193326Sedusing namespace clang;
29193326Sed
30263509Sdim#include "llvm/Support/raw_ostream.h"
31263509Sdim
32193326Sed// Out-of-line destructor to provide a home for the class.
33193326SedPragmaHandler::~PragmaHandler() {
34193326Sed}
35193326Sed
36193326Sed//===----------------------------------------------------------------------===//
37210299Sed// EmptyPragmaHandler Implementation.
38210299Sed//===----------------------------------------------------------------------===//
39210299Sed
40210299SedEmptyPragmaHandler::EmptyPragmaHandler() {}
41210299Sed
42218893Sdimvoid EmptyPragmaHandler::HandlePragma(Preprocessor &PP,
43218893Sdim                                      PragmaIntroducerKind Introducer,
44218893Sdim                                      Token &FirstToken) {}
45210299Sed
46210299Sed//===----------------------------------------------------------------------===//
47193326Sed// PragmaNamespace Implementation.
48193326Sed//===----------------------------------------------------------------------===//
49193326Sed
50193326SedPragmaNamespace::~PragmaNamespace() {
51210299Sed  for (llvm::StringMap<PragmaHandler*>::iterator
52210299Sed         I = Handlers.begin(), E = Handlers.end(); I != E; ++I)
53210299Sed    delete I->second;
54193326Sed}
55193326Sed
56193326Sed/// FindHandler - Check to see if there is already a handler for the
57193326Sed/// specified name.  If not, return the handler for the null identifier if it
58193326Sed/// exists, otherwise return null.  If IgnoreNull is true (the default) then
59193326Sed/// the null handler isn't returned on failure to match.
60226890SdimPragmaHandler *PragmaNamespace::FindHandler(StringRef Name,
61193326Sed                                            bool IgnoreNull) const {
62210299Sed  if (PragmaHandler *Handler = Handlers.lookup(Name))
63210299Sed    return Handler;
64226890Sdim  return IgnoreNull ? 0 : Handlers.lookup(StringRef());
65210299Sed}
66198092Srdivacky
67210299Sedvoid PragmaNamespace::AddPragma(PragmaHandler *Handler) {
68210299Sed  assert(!Handlers.lookup(Handler->getName()) &&
69210299Sed         "A handler with this name is already registered in this namespace");
70210299Sed  llvm::StringMapEntry<PragmaHandler *> &Entry =
71210299Sed    Handlers.GetOrCreateValue(Handler->getName());
72210299Sed  Entry.setValue(Handler);
73193326Sed}
74193326Sed
75193326Sedvoid PragmaNamespace::RemovePragmaHandler(PragmaHandler *Handler) {
76210299Sed  assert(Handlers.lookup(Handler->getName()) &&
77210299Sed         "Handler not registered in this namespace");
78210299Sed  Handlers.erase(Handler->getName());
79193326Sed}
80193326Sed
81218893Sdimvoid PragmaNamespace::HandlePragma(Preprocessor &PP,
82218893Sdim                                   PragmaIntroducerKind Introducer,
83218893Sdim                                   Token &Tok) {
84193326Sed  // Read the 'namespace' that the directive is in, e.g. STDC.  Do not macro
85193326Sed  // expand it, the user can have a STDC #define, that should not affect this.
86193326Sed  PP.LexUnexpandedToken(Tok);
87198092Srdivacky
88193326Sed  // Get the handler for this token.  If there is no handler, ignore the pragma.
89210299Sed  PragmaHandler *Handler
90210299Sed    = FindHandler(Tok.getIdentifierInfo() ? Tok.getIdentifierInfo()->getName()
91226890Sdim                                          : StringRef(),
92210299Sed                  /*IgnoreNull=*/false);
93193326Sed  if (Handler == 0) {
94193326Sed    PP.Diag(Tok, diag::warn_pragma_ignored);
95193326Sed    return;
96193326Sed  }
97198092Srdivacky
98193326Sed  // Otherwise, pass it down.
99218893Sdim  Handler->HandlePragma(PP, Introducer, Tok);
100193326Sed}
101193326Sed
102193326Sed//===----------------------------------------------------------------------===//
103193326Sed// Preprocessor Pragma Directive Handling.
104193326Sed//===----------------------------------------------------------------------===//
105193326Sed
106245431Sdim/// HandlePragmaDirective - The "\#pragma" directive has been parsed.  Lex the
107193326Sed/// rest of the pragma, passing it to the registered pragma handlers.
108263509Sdimvoid Preprocessor::HandlePragmaDirective(SourceLocation IntroducerLoc,
109263509Sdim                                         PragmaIntroducerKind Introducer) {
110263509Sdim  if (Callbacks)
111263509Sdim    Callbacks->PragmaDirective(IntroducerLoc, Introducer);
112263509Sdim
113245431Sdim  if (!PragmasEnabled)
114245431Sdim    return;
115245431Sdim
116193326Sed  ++NumPragma;
117198092Srdivacky
118193326Sed  // Invoke the first level of pragma handlers which reads the namespace id.
119193326Sed  Token Tok;
120263509Sdim  PragmaHandlers->HandlePragma(*this, Introducer, Tok);
121198092Srdivacky
122193326Sed  // If the pragma handler didn't read the rest of the line, consume it now.
123219077Sdim  if ((CurTokenLexer && CurTokenLexer->isParsingPreprocessorDirective())
124219077Sdim   || (CurPPLexer && CurPPLexer->ParsingPreprocessorDirective))
125193326Sed    DiscardUntilEndOfDirective();
126193326Sed}
127193326Sed
128235633Sdimnamespace {
129235633Sdim/// \brief Helper class for \see Preprocessor::Handle_Pragma.
130235633Sdimclass LexingFor_PragmaRAII {
131235633Sdim  Preprocessor &PP;
132235633Sdim  bool InMacroArgPreExpansion;
133235633Sdim  bool Failed;
134235633Sdim  Token &OutTok;
135235633Sdim  Token PragmaTok;
136235633Sdim
137235633Sdimpublic:
138235633Sdim  LexingFor_PragmaRAII(Preprocessor &PP, bool InMacroArgPreExpansion,
139235633Sdim                       Token &Tok)
140235633Sdim    : PP(PP), InMacroArgPreExpansion(InMacroArgPreExpansion),
141235633Sdim      Failed(false), OutTok(Tok) {
142235633Sdim    if (InMacroArgPreExpansion) {
143235633Sdim      PragmaTok = OutTok;
144235633Sdim      PP.EnableBacktrackAtThisPos();
145235633Sdim    }
146235633Sdim  }
147235633Sdim
148235633Sdim  ~LexingFor_PragmaRAII() {
149235633Sdim    if (InMacroArgPreExpansion) {
150235633Sdim      if (Failed) {
151235633Sdim        PP.CommitBacktrackedTokens();
152235633Sdim      } else {
153235633Sdim        PP.Backtrack();
154235633Sdim        OutTok = PragmaTok;
155235633Sdim      }
156235633Sdim    }
157235633Sdim  }
158235633Sdim
159235633Sdim  void failed() {
160235633Sdim    Failed = true;
161235633Sdim  }
162235633Sdim};
163235633Sdim}
164235633Sdim
165193326Sed/// Handle_Pragma - Read a _Pragma directive, slice it up, process it, then
166193326Sed/// return the first token after the directive.  The _Pragma token has just
167193326Sed/// been read into 'Tok'.
168193326Sedvoid Preprocessor::Handle_Pragma(Token &Tok) {
169235633Sdim
170235633Sdim  // This works differently if we are pre-expanding a macro argument.
171235633Sdim  // In that case we don't actually "activate" the pragma now, we only lex it
172235633Sdim  // until we are sure it is lexically correct and then we backtrack so that
173235633Sdim  // we activate the pragma whenever we encounter the tokens again in the token
174235633Sdim  // stream. This ensures that we will activate it in the correct location
175235633Sdim  // or that we will ignore it if it never enters the token stream, e.g:
176235633Sdim  //
177235633Sdim  //     #define EMPTY(x)
178235633Sdim  //     #define INACTIVE(x) EMPTY(x)
179235633Sdim  //     INACTIVE(_Pragma("clang diagnostic ignored \"-Wconversion\""))
180235633Sdim
181235633Sdim  LexingFor_PragmaRAII _PragmaLexing(*this, InMacroArgPreExpansion, Tok);
182235633Sdim
183193326Sed  // Remember the pragma token location.
184193326Sed  SourceLocation PragmaLoc = Tok.getLocation();
185198092Srdivacky
186193326Sed  // Read the '('.
187193326Sed  Lex(Tok);
188193326Sed  if (Tok.isNot(tok::l_paren)) {
189193326Sed    Diag(PragmaLoc, diag::err__Pragma_malformed);
190235633Sdim    return _PragmaLexing.failed();
191193326Sed  }
192193326Sed
193193326Sed  // Read the '"..."'.
194193326Sed  Lex(Tok);
195252723Sdim  if (!tok::isStringLiteral(Tok.getKind())) {
196193326Sed    Diag(PragmaLoc, diag::err__Pragma_malformed);
197235633Sdim    // Skip this token, and the ')', if present.
198235633Sdim    if (Tok.isNot(tok::r_paren))
199235633Sdim      Lex(Tok);
200235633Sdim    if (Tok.is(tok::r_paren))
201235633Sdim      Lex(Tok);
202235633Sdim    return _PragmaLexing.failed();
203193326Sed  }
204198092Srdivacky
205235633Sdim  if (Tok.hasUDSuffix()) {
206235633Sdim    Diag(Tok, diag::err_invalid_string_udl);
207235633Sdim    // Skip this token, and the ')', if present.
208235633Sdim    Lex(Tok);
209235633Sdim    if (Tok.is(tok::r_paren))
210235633Sdim      Lex(Tok);
211235633Sdim    return _PragmaLexing.failed();
212235633Sdim  }
213235633Sdim
214193326Sed  // Remember the string.
215235633Sdim  Token StrTok = Tok;
216193326Sed
217193326Sed  // Read the ')'.
218193326Sed  Lex(Tok);
219193326Sed  if (Tok.isNot(tok::r_paren)) {
220193326Sed    Diag(PragmaLoc, diag::err__Pragma_malformed);
221235633Sdim    return _PragmaLexing.failed();
222193326Sed  }
223198092Srdivacky
224235633Sdim  if (InMacroArgPreExpansion)
225235633Sdim    return;
226235633Sdim
227193326Sed  SourceLocation RParenLoc = Tok.getLocation();
228235633Sdim  std::string StrVal = getSpelling(StrTok);
229198092Srdivacky
230252723Sdim  // The _Pragma is lexically sound.  Destringize according to C11 6.10.9.1:
231252723Sdim  // "The string literal is destringized by deleting any encoding prefix,
232193326Sed  // deleting the leading and trailing double-quotes, replacing each escape
233193326Sed  // sequence \" by a double-quote, and replacing each escape sequence \\ by a
234193326Sed  // single backslash."
235252723Sdim  if (StrVal[0] == 'L' || StrVal[0] == 'U' ||
236252723Sdim      (StrVal[0] == 'u' && StrVal[1] != '8'))
237193326Sed    StrVal.erase(StrVal.begin());
238252723Sdim  else if (StrVal[0] == 'u')
239252723Sdim    StrVal.erase(StrVal.begin(), StrVal.begin() + 2);
240198092Srdivacky
241252723Sdim  if (StrVal[0] == 'R') {
242252723Sdim    // FIXME: C++11 does not specify how to handle raw-string-literals here.
243252723Sdim    // We strip off the 'R', the quotes, the d-char-sequences, and the parens.
244252723Sdim    assert(StrVal[1] == '"' && StrVal[StrVal.size() - 1] == '"' &&
245252723Sdim           "Invalid raw string token!");
246252723Sdim
247252723Sdim    // Measure the length of the d-char-sequence.
248252723Sdim    unsigned NumDChars = 0;
249252723Sdim    while (StrVal[2 + NumDChars] != '(') {
250252723Sdim      assert(NumDChars < (StrVal.size() - 5) / 2 &&
251252723Sdim             "Invalid raw string token!");
252252723Sdim      ++NumDChars;
253252723Sdim    }
254252723Sdim    assert(StrVal[StrVal.size() - 2 - NumDChars] == ')');
255252723Sdim
256252723Sdim    // Remove 'R " d-char-sequence' and 'd-char-sequence "'. We'll replace the
257252723Sdim    // parens below.
258252723Sdim    StrVal.erase(0, 2 + NumDChars);
259252723Sdim    StrVal.erase(StrVal.size() - 1 - NumDChars);
260252723Sdim  } else {
261252723Sdim    assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
262252723Sdim           "Invalid string token!");
263252723Sdim
264252723Sdim    // Remove escaped quotes and escapes.
265252723Sdim    unsigned ResultPos = 1;
266263509Sdim    for (unsigned i = 1, e = StrVal.size() - 1; i != e; ++i) {
267263509Sdim      // Skip escapes.  \\ -> '\' and \" -> '"'.
268263509Sdim      if (StrVal[i] == '\\' && i + 1 < e &&
269263509Sdim          (StrVal[i + 1] == '\\' || StrVal[i + 1] == '"'))
270263509Sdim        ++i;
271263509Sdim      StrVal[ResultPos++] = StrVal[i];
272252723Sdim    }
273263509Sdim    StrVal.erase(StrVal.begin() + ResultPos, StrVal.end() - 1);
274252723Sdim  }
275252723Sdim
276193326Sed  // Remove the front quote, replacing it with a space, so that the pragma
277193326Sed  // contents appear to have a space before them.
278193326Sed  StrVal[0] = ' ';
279198092Srdivacky
280193326Sed  // Replace the terminating quote with a \n.
281193326Sed  StrVal[StrVal.size()-1] = '\n';
282198092Srdivacky
283219077Sdim  // Plop the string (including the newline and trailing null) into a buffer
284219077Sdim  // where we can lex it.
285219077Sdim  Token TmpTok;
286219077Sdim  TmpTok.startToken();
287245431Sdim  CreateString(StrVal, TmpTok);
288219077Sdim  SourceLocation TokLoc = TmpTok.getLocation();
289198092Srdivacky
290219077Sdim  // Make and enter a lexer object so that we lex and expand the tokens just
291219077Sdim  // like any others.
292219077Sdim  Lexer *TL = Lexer::Create_PragmaLexer(TokLoc, PragmaLoc, RParenLoc,
293219077Sdim                                        StrVal.size(), *this);
294219077Sdim
295219077Sdim  EnterSourceFileWithLexer(TL, 0);
296219077Sdim
297219077Sdim  // With everything set up, lex this as a #pragma directive.
298263509Sdim  HandlePragmaDirective(PragmaLoc, PIK__Pragma);
299219077Sdim
300212904Sdim  // Finally, return whatever came after the pragma directive.
301212904Sdim  return Lex(Tok);
302212904Sdim}
303212904Sdim
304212904Sdim/// HandleMicrosoft__pragma - Like Handle_Pragma except the pragma text
305212904Sdim/// is not enclosed within a string literal.
306212904Sdimvoid Preprocessor::HandleMicrosoft__pragma(Token &Tok) {
307212904Sdim  // Remember the pragma token location.
308212904Sdim  SourceLocation PragmaLoc = Tok.getLocation();
309212904Sdim
310212904Sdim  // Read the '('.
311212904Sdim  Lex(Tok);
312212904Sdim  if (Tok.isNot(tok::l_paren)) {
313212904Sdim    Diag(PragmaLoc, diag::err__Pragma_malformed);
314212904Sdim    return;
315212904Sdim  }
316212904Sdim
317219077Sdim  // Get the tokens enclosed within the __pragma(), as well as the final ')'.
318226890Sdim  SmallVector<Token, 32> PragmaToks;
319212904Sdim  int NumParens = 0;
320212904Sdim  Lex(Tok);
321212904Sdim  while (Tok.isNot(tok::eof)) {
322219077Sdim    PragmaToks.push_back(Tok);
323212904Sdim    if (Tok.is(tok::l_paren))
324212904Sdim      NumParens++;
325212904Sdim    else if (Tok.is(tok::r_paren) && NumParens-- == 0)
326212904Sdim      break;
327212904Sdim    Lex(Tok);
328212904Sdim  }
329212904Sdim
330212904Sdim  if (Tok.is(tok::eof)) {
331212904Sdim    Diag(PragmaLoc, diag::err_unterminated___pragma);
332212904Sdim    return;
333212904Sdim  }
334212904Sdim
335219077Sdim  PragmaToks.front().setFlag(Token::LeadingSpace);
336212904Sdim
337221345Sdim  // Replace the ')' with an EOD to mark the end of the pragma.
338221345Sdim  PragmaToks.back().setKind(tok::eod);
339212904Sdim
340219077Sdim  Token *TokArray = new Token[PragmaToks.size()];
341219077Sdim  std::copy(PragmaToks.begin(), PragmaToks.end(), TokArray);
342212904Sdim
343219077Sdim  // Push the tokens onto the stack.
344219077Sdim  EnterTokenStream(TokArray, PragmaToks.size(), true, true);
345212904Sdim
346219077Sdim  // With everything set up, lex this as a #pragma directive.
347263509Sdim  HandlePragmaDirective(PragmaLoc, PIK___pragma);
348193326Sed
349219077Sdim  // Finally, return whatever came after the pragma directive.
350219077Sdim  return Lex(Tok);
351193326Sed}
352193326Sed
353245431Sdim/// HandlePragmaOnce - Handle \#pragma once.  OnceTok is the 'once'.
354193326Sed///
355193326Sedvoid Preprocessor::HandlePragmaOnce(Token &OnceTok) {
356193326Sed  if (isInPrimaryFile()) {
357193326Sed    Diag(OnceTok, diag::pp_pragma_once_in_main_file);
358193326Sed    return;
359193326Sed  }
360198092Srdivacky
361193326Sed  // Get the current file lexer we're looking at.  Ignore _Pragma 'files' etc.
362193326Sed  // Mark the file as a once-only file now.
363193326Sed  HeaderInfo.MarkFileIncludeOnce(getCurrentFileLexer()->getFileEntry());
364193326Sed}
365193326Sed
366193326Sedvoid Preprocessor::HandlePragmaMark() {
367193326Sed  assert(CurPPLexer && "No current lexer?");
368194613Sed  if (CurLexer)
369194613Sed    CurLexer->ReadToEndOfLine();
370194613Sed  else
371194613Sed    CurPTHLexer->DiscardToEndOfLine();
372193326Sed}
373193326Sed
374193326Sed
375245431Sdim/// HandlePragmaPoison - Handle \#pragma GCC poison.  PoisonTok is the 'poison'.
376193326Sed///
377193326Sedvoid Preprocessor::HandlePragmaPoison(Token &PoisonTok) {
378193326Sed  Token Tok;
379193326Sed
380193326Sed  while (1) {
381193326Sed    // Read the next token to poison.  While doing this, pretend that we are
382193326Sed    // skipping while reading the identifier to poison.
383193326Sed    // This avoids errors on code like:
384193326Sed    //   #pragma GCC poison X
385193326Sed    //   #pragma GCC poison X
386193326Sed    if (CurPPLexer) CurPPLexer->LexingRawMode = true;
387193326Sed    LexUnexpandedToken(Tok);
388193326Sed    if (CurPPLexer) CurPPLexer->LexingRawMode = false;
389198092Srdivacky
390193326Sed    // If we reached the end of line, we're done.
391221345Sdim    if (Tok.is(tok::eod)) return;
392198092Srdivacky
393193326Sed    // Can only poison identifiers.
394218893Sdim    if (Tok.isNot(tok::raw_identifier)) {
395193326Sed      Diag(Tok, diag::err_pp_invalid_poison);
396193326Sed      return;
397193326Sed    }
398198092Srdivacky
399193326Sed    // Look up the identifier info for the token.  We disabled identifier lookup
400193326Sed    // by saying we're skipping contents, so we need to do this manually.
401193326Sed    IdentifierInfo *II = LookUpIdentifierInfo(Tok);
402198092Srdivacky
403193326Sed    // Already poisoned.
404193326Sed    if (II->isPoisoned()) continue;
405198092Srdivacky
406193326Sed    // If this is a macro identifier, emit a warning.
407193326Sed    if (II->hasMacroDefinition())
408193326Sed      Diag(Tok, diag::pp_poisoning_existing_macro);
409198092Srdivacky
410193326Sed    // Finally, poison it!
411193326Sed    II->setIsPoisoned();
412235633Sdim    if (II->isFromAST())
413235633Sdim      II->setChangedSinceDeserialization();
414193326Sed  }
415193326Sed}
416193326Sed
417245431Sdim/// HandlePragmaSystemHeader - Implement \#pragma GCC system_header.  We know
418193326Sed/// that the whole directive has been parsed.
419193326Sedvoid Preprocessor::HandlePragmaSystemHeader(Token &SysHeaderTok) {
420193326Sed  if (isInPrimaryFile()) {
421193326Sed    Diag(SysHeaderTok, diag::pp_pragma_sysheader_in_main_file);
422193326Sed    return;
423193326Sed  }
424198092Srdivacky
425193326Sed  // Get the current file lexer we're looking at.  Ignore _Pragma 'files' etc.
426193326Sed  PreprocessorLexer *TheLexer = getCurrentFileLexer();
427198092Srdivacky
428193326Sed  // Mark the file as a system header.
429193326Sed  HeaderInfo.MarkFileSystemHeader(TheLexer->getFileEntry());
430198092Srdivacky
431198092Srdivacky
432194613Sed  PresumedLoc PLoc = SourceMgr.getPresumedLoc(SysHeaderTok.getLocation());
433218893Sdim  if (PLoc.isInvalid())
434218893Sdim    return;
435218893Sdim
436224145Sdim  unsigned FilenameID = SourceMgr.getLineTableFilenameID(PLoc.getFilename());
437198092Srdivacky
438223017Sdim  // Notify the client, if desired, that we are in a new source file.
439223017Sdim  if (Callbacks)
440223017Sdim    Callbacks->FileChanged(SysHeaderTok.getLocation(),
441223017Sdim                           PPCallbacks::SystemHeaderPragma, SrcMgr::C_System);
442223017Sdim
443194613Sed  // Emit a line marker.  This will change any source locations from this point
444194613Sed  // forward to realize they are in a system header.
445194613Sed  // Create a line note with this information.
446252723Sdim  SourceMgr.AddLineNote(SysHeaderTok.getLocation(), PLoc.getLine()+1,
447252723Sdim                        FilenameID, /*IsEntry=*/false, /*IsExit=*/false,
448252723Sdim                        /*IsSystem=*/true, /*IsExternC=*/false);
449193326Sed}
450193326Sed
451245431Sdim/// HandlePragmaDependency - Handle \#pragma GCC dependency "foo" blah.
452193326Sed///
453193326Sedvoid Preprocessor::HandlePragmaDependency(Token &DependencyTok) {
454193326Sed  Token FilenameTok;
455193326Sed  CurPPLexer->LexIncludeFilename(FilenameTok);
456193326Sed
457221345Sdim  // If the token kind is EOD, the error has already been diagnosed.
458221345Sdim  if (FilenameTok.is(tok::eod))
459193326Sed    return;
460198092Srdivacky
461193326Sed  // Reserve a buffer to get the spelling.
462235633Sdim  SmallString<128> FilenameBuffer;
463205408Srdivacky  bool Invalid = false;
464226890Sdim  StringRef Filename = getSpelling(FilenameTok, FilenameBuffer, &Invalid);
465205408Srdivacky  if (Invalid)
466205408Srdivacky    return;
467198092Srdivacky
468202379Srdivacky  bool isAngled =
469202379Srdivacky    GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
470193326Sed  // If GetIncludeFilenameSpelling set the start ptr to null, there was an
471193326Sed  // error.
472202379Srdivacky  if (Filename.empty())
473193326Sed    return;
474198092Srdivacky
475193326Sed  // Search include directories for this file.
476193326Sed  const DirectoryLookup *CurDir;
477263509Sdim  const FileEntry *File = LookupFile(FilenameTok.getLocation(), Filename,
478263509Sdim                                     isAngled, 0, CurDir, NULL, NULL, NULL);
479193326Sed  if (File == 0) {
480226890Sdim    if (!SuppressIncludeNotFoundError)
481226890Sdim      Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
482193326Sed    return;
483193326Sed  }
484198092Srdivacky
485193326Sed  const FileEntry *CurFile = getCurrentFileLexer()->getFileEntry();
486193326Sed
487193326Sed  // If this file is older than the file it depends on, emit a diagnostic.
488193326Sed  if (CurFile && CurFile->getModificationTime() < File->getModificationTime()) {
489193326Sed    // Lex tokens at the end of the message and include them in the message.
490193326Sed    std::string Message;
491193326Sed    Lex(DependencyTok);
492221345Sdim    while (DependencyTok.isNot(tok::eod)) {
493193326Sed      Message += getSpelling(DependencyTok) + " ";
494193326Sed      Lex(DependencyTok);
495193326Sed    }
496198092Srdivacky
497212904Sdim    // Remove the trailing ' ' if present.
498212904Sdim    if (!Message.empty())
499212904Sdim      Message.erase(Message.end()-1);
500193326Sed    Diag(FilenameTok, diag::pp_out_of_date_dependency) << Message;
501193326Sed  }
502193326Sed}
503193326Sed
504252723Sdim/// ParsePragmaPushOrPopMacro - Handle parsing of pragma push_macro/pop_macro.
505212904Sdim/// Return the IdentifierInfo* associated with the macro to push or pop.
506212904SdimIdentifierInfo *Preprocessor::ParsePragmaPushOrPopMacro(Token &Tok) {
507212904Sdim  // Remember the pragma token location.
508212904Sdim  Token PragmaTok = Tok;
509210299Sed
510212904Sdim  // Read the '('.
511212904Sdim  Lex(Tok);
512212904Sdim  if (Tok.isNot(tok::l_paren)) {
513212904Sdim    Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
514212904Sdim      << getSpelling(PragmaTok);
515212904Sdim    return 0;
516212904Sdim  }
517212904Sdim
518212904Sdim  // Read the macro name string.
519212904Sdim  Lex(Tok);
520212904Sdim  if (Tok.isNot(tok::string_literal)) {
521212904Sdim    Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
522212904Sdim      << getSpelling(PragmaTok);
523212904Sdim    return 0;
524212904Sdim  }
525212904Sdim
526235633Sdim  if (Tok.hasUDSuffix()) {
527235633Sdim    Diag(Tok, diag::err_invalid_string_udl);
528235633Sdim    return 0;
529235633Sdim  }
530235633Sdim
531212904Sdim  // Remember the macro string.
532212904Sdim  std::string StrVal = getSpelling(Tok);
533212904Sdim
534212904Sdim  // Read the ')'.
535212904Sdim  Lex(Tok);
536212904Sdim  if (Tok.isNot(tok::r_paren)) {
537212904Sdim    Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
538212904Sdim      << getSpelling(PragmaTok);
539212904Sdim    return 0;
540212904Sdim  }
541212904Sdim
542212904Sdim  assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
543212904Sdim         "Invalid string token!");
544212904Sdim
545212904Sdim  // Create a Token from the string.
546212904Sdim  Token MacroTok;
547212904Sdim  MacroTok.startToken();
548218893Sdim  MacroTok.setKind(tok::raw_identifier);
549245431Sdim  CreateString(StringRef(&StrVal[1], StrVal.size() - 2), MacroTok);
550212904Sdim
551212904Sdim  // Get the IdentifierInfo of MacroToPushTok.
552212904Sdim  return LookUpIdentifierInfo(MacroTok);
553212904Sdim}
554212904Sdim
555245431Sdim/// \brief Handle \#pragma push_macro.
556245431Sdim///
557212904Sdim/// The syntax is:
558245431Sdim/// \code
559252723Sdim///   #pragma push_macro("macro")
560245431Sdim/// \endcode
561212904Sdimvoid Preprocessor::HandlePragmaPushMacro(Token &PushMacroTok) {
562212904Sdim  // Parse the pragma directive and get the macro IdentifierInfo*.
563212904Sdim  IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PushMacroTok);
564212904Sdim  if (!IdentInfo) return;
565212904Sdim
566212904Sdim  // Get the MacroInfo associated with IdentInfo.
567212904Sdim  MacroInfo *MI = getMacroInfo(IdentInfo);
568212904Sdim
569212904Sdim  if (MI) {
570212904Sdim    // Allow the original MacroInfo to be redefined later.
571212904Sdim    MI->setIsAllowRedefinitionsWithoutWarning(true);
572212904Sdim  }
573212904Sdim
574212904Sdim  // Push the cloned MacroInfo so we can retrieve it later.
575252723Sdim  PragmaPushMacroInfo[IdentInfo].push_back(MI);
576212904Sdim}
577212904Sdim
578245431Sdim/// \brief Handle \#pragma pop_macro.
579245431Sdim///
580212904Sdim/// The syntax is:
581245431Sdim/// \code
582212904Sdim///   #pragma pop_macro("macro")
583245431Sdim/// \endcode
584212904Sdimvoid Preprocessor::HandlePragmaPopMacro(Token &PopMacroTok) {
585212904Sdim  SourceLocation MessageLoc = PopMacroTok.getLocation();
586212904Sdim
587212904Sdim  // Parse the pragma directive and get the macro IdentifierInfo*.
588212904Sdim  IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PopMacroTok);
589212904Sdim  if (!IdentInfo) return;
590212904Sdim
591212904Sdim  // Find the vector<MacroInfo*> associated with the macro.
592212904Sdim  llvm::DenseMap<IdentifierInfo*, std::vector<MacroInfo*> >::iterator iter =
593212904Sdim    PragmaPushMacroInfo.find(IdentInfo);
594212904Sdim  if (iter != PragmaPushMacroInfo.end()) {
595245431Sdim    // Forget the MacroInfo currently associated with IdentInfo.
596252723Sdim    if (MacroDirective *CurrentMD = getMacroDirective(IdentInfo)) {
597252723Sdim      MacroInfo *MI = CurrentMD->getMacroInfo();
598252723Sdim      if (MI->isWarnIfUnused())
599252723Sdim        WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());
600252723Sdim      appendMacroDirective(IdentInfo, AllocateUndefMacroDirective(MessageLoc));
601218893Sdim    }
602212904Sdim
603212904Sdim    // Get the MacroInfo we want to reinstall.
604212904Sdim    MacroInfo *MacroToReInstall = iter->second.back();
605212904Sdim
606245431Sdim    if (MacroToReInstall) {
607245431Sdim      // Reinstall the previously pushed macro.
608252723Sdim      appendDefMacroDirective(IdentInfo, MacroToReInstall, MessageLoc,
609252723Sdim                              /*isImported=*/false);
610245431Sdim    }
611212904Sdim
612212904Sdim    // Pop PragmaPushMacroInfo stack.
613212904Sdim    iter->second.pop_back();
614212904Sdim    if (iter->second.size() == 0)
615212904Sdim      PragmaPushMacroInfo.erase(iter);
616212904Sdim  } else {
617212904Sdim    Diag(MessageLoc, diag::warn_pragma_pop_macro_no_push)
618212904Sdim      << IdentInfo->getName();
619212904Sdim  }
620212904Sdim}
621212904Sdim
622235633Sdimvoid Preprocessor::HandlePragmaIncludeAlias(Token &Tok) {
623235633Sdim  // We will either get a quoted filename or a bracketed filename, and we
624235633Sdim  // have to track which we got.  The first filename is the source name,
625235633Sdim  // and the second name is the mapped filename.  If the first is quoted,
626235633Sdim  // the second must be as well (cannot mix and match quotes and brackets).
627235633Sdim
628235633Sdim  // Get the open paren
629235633Sdim  Lex(Tok);
630235633Sdim  if (Tok.isNot(tok::l_paren)) {
631235633Sdim    Diag(Tok, diag::warn_pragma_include_alias_expected) << "(";
632235633Sdim    return;
633235633Sdim  }
634235633Sdim
635235633Sdim  // We expect either a quoted string literal, or a bracketed name
636235633Sdim  Token SourceFilenameTok;
637235633Sdim  CurPPLexer->LexIncludeFilename(SourceFilenameTok);
638235633Sdim  if (SourceFilenameTok.is(tok::eod)) {
639235633Sdim    // The diagnostic has already been handled
640235633Sdim    return;
641235633Sdim  }
642235633Sdim
643235633Sdim  StringRef SourceFileName;
644235633Sdim  SmallString<128> FileNameBuffer;
645235633Sdim  if (SourceFilenameTok.is(tok::string_literal) ||
646235633Sdim      SourceFilenameTok.is(tok::angle_string_literal)) {
647235633Sdim    SourceFileName = getSpelling(SourceFilenameTok, FileNameBuffer);
648235633Sdim  } else if (SourceFilenameTok.is(tok::less)) {
649235633Sdim    // This could be a path instead of just a name
650235633Sdim    FileNameBuffer.push_back('<');
651235633Sdim    SourceLocation End;
652235633Sdim    if (ConcatenateIncludeName(FileNameBuffer, End))
653235633Sdim      return; // Diagnostic already emitted
654235633Sdim    SourceFileName = FileNameBuffer.str();
655235633Sdim  } else {
656235633Sdim    Diag(Tok, diag::warn_pragma_include_alias_expected_filename);
657235633Sdim    return;
658235633Sdim  }
659235633Sdim  FileNameBuffer.clear();
660235633Sdim
661235633Sdim  // Now we expect a comma, followed by another include name
662235633Sdim  Lex(Tok);
663235633Sdim  if (Tok.isNot(tok::comma)) {
664235633Sdim    Diag(Tok, diag::warn_pragma_include_alias_expected) << ",";
665235633Sdim    return;
666235633Sdim  }
667235633Sdim
668235633Sdim  Token ReplaceFilenameTok;
669235633Sdim  CurPPLexer->LexIncludeFilename(ReplaceFilenameTok);
670235633Sdim  if (ReplaceFilenameTok.is(tok::eod)) {
671235633Sdim    // The diagnostic has already been handled
672235633Sdim    return;
673235633Sdim  }
674235633Sdim
675235633Sdim  StringRef ReplaceFileName;
676235633Sdim  if (ReplaceFilenameTok.is(tok::string_literal) ||
677235633Sdim      ReplaceFilenameTok.is(tok::angle_string_literal)) {
678235633Sdim    ReplaceFileName = getSpelling(ReplaceFilenameTok, FileNameBuffer);
679235633Sdim  } else if (ReplaceFilenameTok.is(tok::less)) {
680235633Sdim    // This could be a path instead of just a name
681235633Sdim    FileNameBuffer.push_back('<');
682235633Sdim    SourceLocation End;
683235633Sdim    if (ConcatenateIncludeName(FileNameBuffer, End))
684235633Sdim      return; // Diagnostic already emitted
685235633Sdim    ReplaceFileName = FileNameBuffer.str();
686235633Sdim  } else {
687235633Sdim    Diag(Tok, diag::warn_pragma_include_alias_expected_filename);
688235633Sdim    return;
689235633Sdim  }
690235633Sdim
691235633Sdim  // Finally, we expect the closing paren
692235633Sdim  Lex(Tok);
693235633Sdim  if (Tok.isNot(tok::r_paren)) {
694235633Sdim    Diag(Tok, diag::warn_pragma_include_alias_expected) << ")";
695235633Sdim    return;
696235633Sdim  }
697235633Sdim
698235633Sdim  // Now that we have the source and target filenames, we need to make sure
699235633Sdim  // they're both of the same type (angled vs non-angled)
700235633Sdim  StringRef OriginalSource = SourceFileName;
701235633Sdim
702235633Sdim  bool SourceIsAngled =
703235633Sdim    GetIncludeFilenameSpelling(SourceFilenameTok.getLocation(),
704235633Sdim                                SourceFileName);
705235633Sdim  bool ReplaceIsAngled =
706235633Sdim    GetIncludeFilenameSpelling(ReplaceFilenameTok.getLocation(),
707235633Sdim                                ReplaceFileName);
708235633Sdim  if (!SourceFileName.empty() && !ReplaceFileName.empty() &&
709235633Sdim      (SourceIsAngled != ReplaceIsAngled)) {
710235633Sdim    unsigned int DiagID;
711235633Sdim    if (SourceIsAngled)
712235633Sdim      DiagID = diag::warn_pragma_include_alias_mismatch_angle;
713235633Sdim    else
714235633Sdim      DiagID = diag::warn_pragma_include_alias_mismatch_quote;
715235633Sdim
716235633Sdim    Diag(SourceFilenameTok.getLocation(), DiagID)
717235633Sdim      << SourceFileName
718235633Sdim      << ReplaceFileName;
719235633Sdim
720235633Sdim    return;
721235633Sdim  }
722235633Sdim
723235633Sdim  // Now we can let the include handler know about this mapping
724235633Sdim  getHeaderSearchInfo().AddIncludeAlias(OriginalSource, ReplaceFileName);
725235633Sdim}
726235633Sdim
727193326Sed/// AddPragmaHandler - Add the specified pragma handler to the preprocessor.
728193326Sed/// If 'Namespace' is non-null, then it is a token required to exist on the
729193326Sed/// pragma line before the pragma string starts, e.g. "STDC" or "GCC".
730226890Sdimvoid Preprocessor::AddPragmaHandler(StringRef Namespace,
731193326Sed                                    PragmaHandler *Handler) {
732193326Sed  PragmaNamespace *InsertNS = PragmaHandlers;
733198092Srdivacky
734193326Sed  // If this is specified to be in a namespace, step down into it.
735210299Sed  if (!Namespace.empty()) {
736193326Sed    // If there is already a pragma handler with the name of this namespace,
737193326Sed    // we either have an error (directive with the same name as a namespace) or
738193326Sed    // we already have the namespace to insert into.
739210299Sed    if (PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace)) {
740193326Sed      InsertNS = Existing->getIfNamespace();
741193326Sed      assert(InsertNS != 0 && "Cannot have a pragma namespace and pragma"
742193326Sed             " handler with the same name!");
743193326Sed    } else {
744193326Sed      // Otherwise, this namespace doesn't exist yet, create and insert the
745193326Sed      // handler for it.
746210299Sed      InsertNS = new PragmaNamespace(Namespace);
747193326Sed      PragmaHandlers->AddPragma(InsertNS);
748193326Sed    }
749193326Sed  }
750198092Srdivacky
751193326Sed  // Check to make sure we don't already have a pragma for this identifier.
752193326Sed  assert(!InsertNS->FindHandler(Handler->getName()) &&
753193326Sed         "Pragma handler already exists for this identifier!");
754193326Sed  InsertNS->AddPragma(Handler);
755193326Sed}
756193326Sed
757193326Sed/// RemovePragmaHandler - Remove the specific pragma handler from the
758193326Sed/// preprocessor. If \arg Namespace is non-null, then it should be the
759193326Sed/// namespace that \arg Handler was added to. It is an error to remove
760193326Sed/// a handler that has not been registered.
761226890Sdimvoid Preprocessor::RemovePragmaHandler(StringRef Namespace,
762193326Sed                                       PragmaHandler *Handler) {
763193326Sed  PragmaNamespace *NS = PragmaHandlers;
764198092Srdivacky
765193326Sed  // If this is specified to be in a namespace, step down into it.
766210299Sed  if (!Namespace.empty()) {
767210299Sed    PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace);
768193326Sed    assert(Existing && "Namespace containing handler does not exist!");
769193326Sed
770193326Sed    NS = Existing->getIfNamespace();
771193326Sed    assert(NS && "Invalid namespace, registered as a regular pragma handler!");
772193326Sed  }
773193326Sed
774193326Sed  NS->RemovePragmaHandler(Handler);
775198092Srdivacky
776193326Sed  // If this is a non-default namespace and it is now empty, remove
777193326Sed  // it.
778235633Sdim  if (NS != PragmaHandlers && NS->IsEmpty()) {
779193326Sed    PragmaHandlers->RemovePragmaHandler(NS);
780235633Sdim    delete NS;
781235633Sdim  }
782193326Sed}
783193326Sed
784218893Sdimbool Preprocessor::LexOnOffSwitch(tok::OnOffSwitch &Result) {
785218893Sdim  Token Tok;
786218893Sdim  LexUnexpandedToken(Tok);
787218893Sdim
788218893Sdim  if (Tok.isNot(tok::identifier)) {
789218893Sdim    Diag(Tok, diag::ext_on_off_switch_syntax);
790218893Sdim    return true;
791218893Sdim  }
792218893Sdim  IdentifierInfo *II = Tok.getIdentifierInfo();
793218893Sdim  if (II->isStr("ON"))
794218893Sdim    Result = tok::OOS_ON;
795218893Sdim  else if (II->isStr("OFF"))
796218893Sdim    Result = tok::OOS_OFF;
797218893Sdim  else if (II->isStr("DEFAULT"))
798218893Sdim    Result = tok::OOS_DEFAULT;
799218893Sdim  else {
800218893Sdim    Diag(Tok, diag::ext_on_off_switch_syntax);
801218893Sdim    return true;
802218893Sdim  }
803218893Sdim
804221345Sdim  // Verify that this is followed by EOD.
805218893Sdim  LexUnexpandedToken(Tok);
806221345Sdim  if (Tok.isNot(tok::eod))
807221345Sdim    Diag(Tok, diag::ext_pragma_syntax_eod);
808218893Sdim  return false;
809218893Sdim}
810218893Sdim
811193326Sednamespace {
812245431Sdim/// PragmaOnceHandler - "\#pragma once" marks the file as atomically included.
813193326Sedstruct PragmaOnceHandler : public PragmaHandler {
814210299Sed  PragmaOnceHandler() : PragmaHandler("once") {}
815218893Sdim  virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
816218893Sdim                            Token &OnceTok) {
817193326Sed    PP.CheckEndOfDirective("pragma once");
818193326Sed    PP.HandlePragmaOnce(OnceTok);
819193326Sed  }
820193326Sed};
821193326Sed
822245431Sdim/// PragmaMarkHandler - "\#pragma mark ..." is ignored by the compiler, and the
823193326Sed/// rest of the line is not lexed.
824193326Sedstruct PragmaMarkHandler : public PragmaHandler {
825210299Sed  PragmaMarkHandler() : PragmaHandler("mark") {}
826218893Sdim  virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
827218893Sdim                            Token &MarkTok) {
828193326Sed    PP.HandlePragmaMark();
829193326Sed  }
830193326Sed};
831193326Sed
832245431Sdim/// PragmaPoisonHandler - "\#pragma poison x" marks x as not usable.
833193326Sedstruct PragmaPoisonHandler : public PragmaHandler {
834210299Sed  PragmaPoisonHandler() : PragmaHandler("poison") {}
835218893Sdim  virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
836218893Sdim                            Token &PoisonTok) {
837193326Sed    PP.HandlePragmaPoison(PoisonTok);
838193326Sed  }
839193326Sed};
840193326Sed
841245431Sdim/// PragmaSystemHeaderHandler - "\#pragma system_header" marks the current file
842193326Sed/// as a system header, which silences warnings in it.
843193326Sedstruct PragmaSystemHeaderHandler : public PragmaHandler {
844210299Sed  PragmaSystemHeaderHandler() : PragmaHandler("system_header") {}
845218893Sdim  virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
846218893Sdim                            Token &SHToken) {
847193326Sed    PP.HandlePragmaSystemHeader(SHToken);
848193326Sed    PP.CheckEndOfDirective("pragma");
849193326Sed  }
850193326Sed};
851193326Sedstruct PragmaDependencyHandler : public PragmaHandler {
852210299Sed  PragmaDependencyHandler() : PragmaHandler("dependency") {}
853218893Sdim  virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
854218893Sdim                            Token &DepToken) {
855193326Sed    PP.HandlePragmaDependency(DepToken);
856193326Sed  }
857193326Sed};
858198092Srdivacky
859212904Sdimstruct PragmaDebugHandler : public PragmaHandler {
860212904Sdim  PragmaDebugHandler() : PragmaHandler("__debug") {}
861218893Sdim  virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
862218893Sdim                            Token &DepToken) {
863212904Sdim    Token Tok;
864212904Sdim    PP.LexUnexpandedToken(Tok);
865212904Sdim    if (Tok.isNot(tok::identifier)) {
866212904Sdim      PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
867212904Sdim      return;
868212904Sdim    }
869212904Sdim    IdentifierInfo *II = Tok.getIdentifierInfo();
870212904Sdim
871212904Sdim    if (II->isStr("assert")) {
872226890Sdim      llvm_unreachable("This is an assertion!");
873212904Sdim    } else if (II->isStr("crash")) {
874245431Sdim      LLVM_BUILTIN_TRAP;
875245431Sdim    } else if (II->isStr("parser_crash")) {
876245431Sdim      Token Crasher;
877245431Sdim      Crasher.setKind(tok::annot_pragma_parser_crash);
878245431Sdim      PP.EnterToken(Crasher);
879212904Sdim    } else if (II->isStr("llvm_fatal_error")) {
880212904Sdim      llvm::report_fatal_error("#pragma clang __debug llvm_fatal_error");
881212904Sdim    } else if (II->isStr("llvm_unreachable")) {
882212904Sdim      llvm_unreachable("#pragma clang __debug llvm_unreachable");
883212904Sdim    } else if (II->isStr("overflow_stack")) {
884212904Sdim      DebugOverflowStack();
885212904Sdim    } else if (II->isStr("handle_crash")) {
886212904Sdim      llvm::CrashRecoveryContext *CRC =llvm::CrashRecoveryContext::GetCurrent();
887212904Sdim      if (CRC)
888212904Sdim        CRC->HandleCrash();
889252723Sdim    } else if (II->isStr("captured")) {
890252723Sdim      HandleCaptured(PP);
891212904Sdim    } else {
892212904Sdim      PP.Diag(Tok, diag::warn_pragma_debug_unexpected_command)
893212904Sdim        << II->getName();
894212904Sdim    }
895252723Sdim
896252723Sdim    PPCallbacks *Callbacks = PP.getPPCallbacks();
897252723Sdim    if (Callbacks)
898252723Sdim      Callbacks->PragmaDebug(Tok.getLocation(), II->getName());
899212904Sdim  }
900212904Sdim
901252723Sdim  void HandleCaptured(Preprocessor &PP) {
902252723Sdim    // Skip if emitting preprocessed output.
903252723Sdim    if (PP.isPreprocessedOutput())
904252723Sdim      return;
905252723Sdim
906252723Sdim    Token Tok;
907252723Sdim    PP.LexUnexpandedToken(Tok);
908252723Sdim
909252723Sdim    if (Tok.isNot(tok::eod)) {
910252723Sdim      PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol)
911252723Sdim        << "pragma clang __debug captured";
912252723Sdim      return;
913252723Sdim    }
914252723Sdim
915252723Sdim    SourceLocation NameLoc = Tok.getLocation();
916252723Sdim    Token *Toks = PP.getPreprocessorAllocator().Allocate<Token>(1);
917252723Sdim    Toks->startToken();
918252723Sdim    Toks->setKind(tok::annot_pragma_captured);
919252723Sdim    Toks->setLocation(NameLoc);
920252723Sdim
921252723Sdim    PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
922252723Sdim                        /*OwnsTokens=*/false);
923252723Sdim  }
924252723Sdim
925223017Sdim// Disable MSVC warning about runtime stack overflow.
926223017Sdim#ifdef _MSC_VER
927223017Sdim    #pragma warning(disable : 4717)
928223017Sdim#endif
929212904Sdim  void DebugOverflowStack() {
930212904Sdim    DebugOverflowStack();
931212904Sdim  }
932223017Sdim#ifdef _MSC_VER
933223017Sdim    #pragma warning(default : 4717)
934223017Sdim#endif
935223017Sdim
936212904Sdim};
937212904Sdim
938245431Sdim/// PragmaDiagnosticHandler - e.g. '\#pragma GCC diagnostic ignored "-Wformat"'
939193326Sedstruct PragmaDiagnosticHandler : public PragmaHandler {
940224145Sdimprivate:
941224145Sdim  const char *Namespace;
942198092Srdivackypublic:
943224145Sdim  explicit PragmaDiagnosticHandler(const char *NS) :
944224145Sdim    PragmaHandler("diagnostic"), Namespace(NS) {}
945218893Sdim  virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
946218893Sdim                            Token &DiagToken) {
947218893Sdim    SourceLocation DiagLoc = DiagToken.getLocation();
948193326Sed    Token Tok;
949193326Sed    PP.LexUnexpandedToken(Tok);
950193326Sed    if (Tok.isNot(tok::identifier)) {
951212904Sdim      PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
952193326Sed      return;
953193326Sed    }
954193326Sed    IdentifierInfo *II = Tok.getIdentifierInfo();
955224145Sdim    PPCallbacks *Callbacks = PP.getPPCallbacks();
956198092Srdivacky
957193326Sed    diag::Mapping Map;
958193326Sed    if (II->isStr("warning"))
959193326Sed      Map = diag::MAP_WARNING;
960193326Sed    else if (II->isStr("error"))
961193326Sed      Map = diag::MAP_ERROR;
962193326Sed    else if (II->isStr("ignored"))
963193326Sed      Map = diag::MAP_IGNORE;
964193326Sed    else if (II->isStr("fatal"))
965193326Sed      Map = diag::MAP_FATAL;
966212904Sdim    else if (II->isStr("pop")) {
967218893Sdim      if (!PP.getDiagnostics().popMappings(DiagLoc))
968212904Sdim        PP.Diag(Tok, diag::warn_pragma_diagnostic_cannot_pop);
969224145Sdim      else if (Callbacks)
970224145Sdim        Callbacks->PragmaDiagnosticPop(DiagLoc, Namespace);
971193326Sed      return;
972212904Sdim    } else if (II->isStr("push")) {
973218893Sdim      PP.getDiagnostics().pushMappings(DiagLoc);
974224145Sdim      if (Callbacks)
975224145Sdim        Callbacks->PragmaDiagnosticPush(DiagLoc, Namespace);
976212904Sdim      return;
977198092Srdivacky    } else {
978212904Sdim      PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
979198092Srdivacky      return;
980193326Sed    }
981198092Srdivacky
982193326Sed    PP.LexUnexpandedToken(Tok);
983252723Sdim    SourceLocation StringLoc = Tok.getLocation();
984193326Sed
985252723Sdim    std::string WarningName;
986252723Sdim    if (!PP.FinishLexStringLiteral(Tok, WarningName, "pragma diagnostic",
987252723Sdim                                   /*MacroExpansion=*/false))
988193326Sed      return;
989198092Srdivacky
990221345Sdim    if (Tok.isNot(tok::eod)) {
991193326Sed      PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token);
992193326Sed      return;
993193326Sed    }
994198092Srdivacky
995193326Sed    if (WarningName.size() < 3 || WarningName[0] != '-' ||
996193326Sed        WarningName[1] != 'W') {
997252723Sdim      PP.Diag(StringLoc, diag::warn_pragma_diagnostic_invalid_option);
998193326Sed      return;
999193326Sed    }
1000198092Srdivacky
1001223017Sdim    if (PP.getDiagnostics().setDiagnosticGroupMapping(WarningName.substr(2),
1002218893Sdim                                                      Map, DiagLoc))
1003252723Sdim      PP.Diag(StringLoc, diag::warn_pragma_diagnostic_unknown_warning)
1004252723Sdim        << WarningName;
1005224145Sdim    else if (Callbacks)
1006224145Sdim      Callbacks->PragmaDiagnostic(DiagLoc, Namespace, Map, WarningName);
1007193326Sed  }
1008193326Sed};
1009198092Srdivacky
1010263509Sdim// Returns -1 on failure.
1011263509Sdimstatic int LexSimpleInt(Preprocessor &PP, Token &Tok) {
1012263509Sdim  assert(Tok.is(tok::numeric_constant));
1013263509Sdim  SmallString<8> IntegerBuffer;
1014263509Sdim  bool NumberInvalid = false;
1015263509Sdim  StringRef Spelling = PP.getSpelling(Tok, IntegerBuffer, &NumberInvalid);
1016263509Sdim  if (NumberInvalid)
1017263509Sdim    return -1;
1018263509Sdim  NumericLiteralParser Literal(Spelling, Tok.getLocation(), PP);
1019263509Sdim  if (Literal.hadError || !Literal.isIntegerLiteral() || Literal.hasUDSuffix())
1020263509Sdim    return -1;
1021263509Sdim  llvm::APInt APVal(32, 0);
1022263509Sdim  if (Literal.GetIntegerValue(APVal))
1023263509Sdim    return -1;
1024263509Sdim  PP.Lex(Tok);
1025263509Sdim  return int(APVal.getLimitedValue(INT_MAX));
1026263509Sdim}
1027263509Sdim
1028263509Sdim/// "\#pragma warning(...)".  MSVC's diagnostics do not map cleanly to clang's
1029263509Sdim/// diagnostics, so we don't really implement this pragma.  We parse it and
1030263509Sdim/// ignore it to avoid -Wunknown-pragma warnings.
1031263509Sdimstruct PragmaWarningHandler : public PragmaHandler {
1032263509Sdim  PragmaWarningHandler() : PragmaHandler("warning") {}
1033263509Sdim
1034263509Sdim  virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1035263509Sdim                            Token &Tok) {
1036263509Sdim    // Parse things like:
1037263509Sdim    // warning(push, 1)
1038263509Sdim    // warning(pop)
1039263509Sdim    // warning(disable : 1 2 3 ; error : 4 5 6 ; suppress : 7 8 9)
1040263509Sdim    SourceLocation DiagLoc = Tok.getLocation();
1041263509Sdim    PPCallbacks *Callbacks = PP.getPPCallbacks();
1042263509Sdim
1043263509Sdim    PP.Lex(Tok);
1044263509Sdim    if (Tok.isNot(tok::l_paren)) {
1045263509Sdim      PP.Diag(Tok, diag::warn_pragma_warning_expected) << "(";
1046263509Sdim      return;
1047263509Sdim    }
1048263509Sdim
1049263509Sdim    PP.Lex(Tok);
1050263509Sdim    IdentifierInfo *II = Tok.getIdentifierInfo();
1051263509Sdim    if (!II) {
1052263509Sdim      PP.Diag(Tok, diag::warn_pragma_warning_spec_invalid);
1053263509Sdim      return;
1054263509Sdim    }
1055263509Sdim
1056263509Sdim    if (II->isStr("push")) {
1057263509Sdim      // #pragma warning( push[ ,n ] )
1058263509Sdim      int Level = -1;
1059263509Sdim      PP.Lex(Tok);
1060263509Sdim      if (Tok.is(tok::comma)) {
1061263509Sdim        PP.Lex(Tok);
1062263509Sdim        if (Tok.is(tok::numeric_constant))
1063263509Sdim          Level = LexSimpleInt(PP, Tok);
1064263509Sdim        if (Level < 0 || Level > 4) {
1065263509Sdim          PP.Diag(Tok, diag::warn_pragma_warning_push_level);
1066263509Sdim          return;
1067263509Sdim        }
1068263509Sdim      }
1069263509Sdim      if (Callbacks)
1070263509Sdim        Callbacks->PragmaWarningPush(DiagLoc, Level);
1071263509Sdim    } else if (II->isStr("pop")) {
1072263509Sdim      // #pragma warning( pop )
1073263509Sdim      PP.Lex(Tok);
1074263509Sdim      if (Callbacks)
1075263509Sdim        Callbacks->PragmaWarningPop(DiagLoc);
1076263509Sdim    } else {
1077263509Sdim      // #pragma warning( warning-specifier : warning-number-list
1078263509Sdim      //                  [; warning-specifier : warning-number-list...] )
1079263509Sdim      while (true) {
1080263509Sdim        II = Tok.getIdentifierInfo();
1081263509Sdim        if (!II) {
1082263509Sdim          PP.Diag(Tok, diag::warn_pragma_warning_spec_invalid);
1083263509Sdim          return;
1084263509Sdim        }
1085263509Sdim
1086263509Sdim        // Figure out which warning specifier this is.
1087263509Sdim        StringRef Specifier = II->getName();
1088263509Sdim        bool SpecifierValid =
1089263509Sdim            llvm::StringSwitch<bool>(Specifier)
1090263509Sdim                .Cases("1", "2", "3", "4", true)
1091263509Sdim                .Cases("default", "disable", "error", "once", "suppress", true)
1092263509Sdim                .Default(false);
1093263509Sdim        if (!SpecifierValid) {
1094263509Sdim          PP.Diag(Tok, diag::warn_pragma_warning_spec_invalid);
1095263509Sdim          return;
1096263509Sdim        }
1097263509Sdim        PP.Lex(Tok);
1098263509Sdim        if (Tok.isNot(tok::colon)) {
1099263509Sdim          PP.Diag(Tok, diag::warn_pragma_warning_expected) << ":";
1100263509Sdim          return;
1101263509Sdim        }
1102263509Sdim
1103263509Sdim        // Collect the warning ids.
1104263509Sdim        SmallVector<int, 4> Ids;
1105263509Sdim        PP.Lex(Tok);
1106263509Sdim        while (Tok.is(tok::numeric_constant)) {
1107263509Sdim          int Id = LexSimpleInt(PP, Tok);
1108263509Sdim          if (Id <= 0) {
1109263509Sdim            PP.Diag(Tok, diag::warn_pragma_warning_expected_number);
1110263509Sdim            return;
1111263509Sdim          }
1112263509Sdim          Ids.push_back(Id);
1113263509Sdim        }
1114263509Sdim        if (Callbacks)
1115263509Sdim          Callbacks->PragmaWarning(DiagLoc, Specifier, Ids);
1116263509Sdim
1117263509Sdim        // Parse the next specifier if there is a semicolon.
1118263509Sdim        if (Tok.isNot(tok::semi))
1119263509Sdim          break;
1120263509Sdim        PP.Lex(Tok);
1121263509Sdim      }
1122263509Sdim    }
1123263509Sdim
1124263509Sdim    if (Tok.isNot(tok::r_paren)) {
1125263509Sdim      PP.Diag(Tok, diag::warn_pragma_warning_expected) << ")";
1126263509Sdim      return;
1127263509Sdim    }
1128263509Sdim
1129263509Sdim    PP.Lex(Tok);
1130263509Sdim    if (Tok.isNot(tok::eod))
1131263509Sdim      PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma warning";
1132263509Sdim  }
1133263509Sdim};
1134263509Sdim
1135245431Sdim/// PragmaIncludeAliasHandler - "\#pragma include_alias("...")".
1136235633Sdimstruct PragmaIncludeAliasHandler : public PragmaHandler {
1137235633Sdim  PragmaIncludeAliasHandler() : PragmaHandler("include_alias") {}
1138235633Sdim  virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1139235633Sdim                            Token &IncludeAliasTok) {
1140263509Sdim    PP.HandlePragmaIncludeAlias(IncludeAliasTok);
1141235633Sdim  }
1142235633Sdim};
1143235633Sdim
1144252723Sdim/// PragmaMessageHandler - Handle the microsoft and gcc \#pragma message
1145252723Sdim/// extension.  The syntax is:
1146252723Sdim/// \code
1147252723Sdim///   #pragma message(string)
1148252723Sdim/// \endcode
1149252723Sdim/// OR, in GCC mode:
1150252723Sdim/// \code
1151252723Sdim///   #pragma message string
1152252723Sdim/// \endcode
1153252723Sdim/// string is a string, which is fully macro expanded, and permits string
1154252723Sdim/// concatenation, embedded escape characters, etc... See MSDN for more details.
1155252723Sdim/// Also handles \#pragma GCC warning and \#pragma GCC error which take the same
1156252723Sdim/// form as \#pragma message.
1157210299Sedstruct PragmaMessageHandler : public PragmaHandler {
1158252723Sdimprivate:
1159252723Sdim  const PPCallbacks::PragmaMessageKind Kind;
1160252723Sdim  const StringRef Namespace;
1161252723Sdim
1162252723Sdim  static const char* PragmaKind(PPCallbacks::PragmaMessageKind Kind,
1163252723Sdim                                bool PragmaNameOnly = false) {
1164252723Sdim    switch (Kind) {
1165252723Sdim      case PPCallbacks::PMK_Message:
1166252723Sdim        return PragmaNameOnly ? "message" : "pragma message";
1167252723Sdim      case PPCallbacks::PMK_Warning:
1168252723Sdim        return PragmaNameOnly ? "warning" : "pragma warning";
1169252723Sdim      case PPCallbacks::PMK_Error:
1170252723Sdim        return PragmaNameOnly ? "error" : "pragma error";
1171252723Sdim    }
1172252723Sdim    llvm_unreachable("Unknown PragmaMessageKind!");
1173252723Sdim  }
1174252723Sdim
1175252723Sdimpublic:
1176252723Sdim  PragmaMessageHandler(PPCallbacks::PragmaMessageKind Kind,
1177252723Sdim                       StringRef Namespace = StringRef())
1178252723Sdim    : PragmaHandler(PragmaKind(Kind, true)), Kind(Kind), Namespace(Namespace) {}
1179252723Sdim
1180218893Sdim  virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1181252723Sdim                            Token &Tok) {
1182252723Sdim    SourceLocation MessageLoc = Tok.getLocation();
1183252723Sdim    PP.Lex(Tok);
1184252723Sdim    bool ExpectClosingParen = false;
1185252723Sdim    switch (Tok.getKind()) {
1186252723Sdim    case tok::l_paren:
1187252723Sdim      // We have a MSVC style pragma message.
1188252723Sdim      ExpectClosingParen = true;
1189252723Sdim      // Read the string.
1190252723Sdim      PP.Lex(Tok);
1191252723Sdim      break;
1192252723Sdim    case tok::string_literal:
1193252723Sdim      // We have a GCC style pragma message, and we just read the string.
1194252723Sdim      break;
1195252723Sdim    default:
1196252723Sdim      PP.Diag(MessageLoc, diag::err_pragma_message_malformed) << Kind;
1197252723Sdim      return;
1198252723Sdim    }
1199252723Sdim
1200252723Sdim    std::string MessageString;
1201252723Sdim    if (!PP.FinishLexStringLiteral(Tok, MessageString, PragmaKind(Kind),
1202252723Sdim                                   /*MacroExpansion=*/true))
1203252723Sdim      return;
1204252723Sdim
1205252723Sdim    if (ExpectClosingParen) {
1206252723Sdim      if (Tok.isNot(tok::r_paren)) {
1207252723Sdim        PP.Diag(Tok.getLocation(), diag::err_pragma_message_malformed) << Kind;
1208252723Sdim        return;
1209252723Sdim      }
1210252723Sdim      PP.Lex(Tok);  // eat the r_paren.
1211252723Sdim    }
1212252723Sdim
1213252723Sdim    if (Tok.isNot(tok::eod)) {
1214252723Sdim      PP.Diag(Tok.getLocation(), diag::err_pragma_message_malformed) << Kind;
1215252723Sdim      return;
1216252723Sdim    }
1217252723Sdim
1218252723Sdim    // Output the message.
1219252723Sdim    PP.Diag(MessageLoc, (Kind == PPCallbacks::PMK_Error)
1220252723Sdim                          ? diag::err_pragma_message
1221252723Sdim                          : diag::warn_pragma_message) << MessageString;
1222252723Sdim
1223252723Sdim    // If the pragma is lexically sound, notify any interested PPCallbacks.
1224252723Sdim    if (PPCallbacks *Callbacks = PP.getPPCallbacks())
1225252723Sdim      Callbacks->PragmaMessage(MessageLoc, Namespace, Kind, MessageString);
1226210299Sed  }
1227210299Sed};
1228210299Sed
1229245431Sdim/// PragmaPushMacroHandler - "\#pragma push_macro" saves the value of the
1230212904Sdim/// macro on the top of the stack.
1231212904Sdimstruct PragmaPushMacroHandler : public PragmaHandler {
1232212904Sdim  PragmaPushMacroHandler() : PragmaHandler("push_macro") {}
1233218893Sdim  virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1234218893Sdim                            Token &PushMacroTok) {
1235212904Sdim    PP.HandlePragmaPushMacro(PushMacroTok);
1236212904Sdim  }
1237212904Sdim};
1238212904Sdim
1239212904Sdim
1240245431Sdim/// PragmaPopMacroHandler - "\#pragma pop_macro" sets the value of the
1241212904Sdim/// macro to the value on the top of the stack.
1242212904Sdimstruct PragmaPopMacroHandler : public PragmaHandler {
1243212904Sdim  PragmaPopMacroHandler() : PragmaHandler("pop_macro") {}
1244218893Sdim  virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1245218893Sdim                            Token &PopMacroTok) {
1246212904Sdim    PP.HandlePragmaPopMacro(PopMacroTok);
1247212904Sdim  }
1248212904Sdim};
1249212904Sdim
1250193326Sed// Pragma STDC implementations.
1251193326Sed
1252245431Sdim/// PragmaSTDC_FENV_ACCESSHandler - "\#pragma STDC FENV_ACCESS ...".
1253193326Sedstruct PragmaSTDC_FENV_ACCESSHandler : public PragmaHandler {
1254210299Sed  PragmaSTDC_FENV_ACCESSHandler() : PragmaHandler("FENV_ACCESS") {}
1255218893Sdim  virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1256218893Sdim                            Token &Tok) {
1257218893Sdim    tok::OnOffSwitch OOS;
1258218893Sdim    if (PP.LexOnOffSwitch(OOS))
1259218893Sdim     return;
1260218893Sdim    if (OOS == tok::OOS_ON)
1261193326Sed      PP.Diag(Tok, diag::warn_stdc_fenv_access_not_supported);
1262193326Sed  }
1263193326Sed};
1264198092Srdivacky
1265245431Sdim/// PragmaSTDC_CX_LIMITED_RANGEHandler - "\#pragma STDC CX_LIMITED_RANGE ...".
1266193326Sedstruct PragmaSTDC_CX_LIMITED_RANGEHandler : public PragmaHandler {
1267210299Sed  PragmaSTDC_CX_LIMITED_RANGEHandler()
1268210299Sed    : PragmaHandler("CX_LIMITED_RANGE") {}
1269218893Sdim  virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1270218893Sdim                            Token &Tok) {
1271218893Sdim    tok::OnOffSwitch OOS;
1272218893Sdim    PP.LexOnOffSwitch(OOS);
1273193326Sed  }
1274193326Sed};
1275198092Srdivacky
1276245431Sdim/// PragmaSTDC_UnknownHandler - "\#pragma STDC ...".
1277193326Sedstruct PragmaSTDC_UnknownHandler : public PragmaHandler {
1278210299Sed  PragmaSTDC_UnknownHandler() {}
1279218893Sdim  virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1280218893Sdim                            Token &UnknownTok) {
1281193326Sed    // C99 6.10.6p2, unknown forms are not allowed.
1282193326Sed    PP.Diag(UnknownTok, diag::ext_stdc_pragma_ignored);
1283193326Sed  }
1284193326Sed};
1285198092Srdivacky
1286226890Sdim/// PragmaARCCFCodeAuditedHandler -
1287245431Sdim///   \#pragma clang arc_cf_code_audited begin/end
1288226890Sdimstruct PragmaARCCFCodeAuditedHandler : public PragmaHandler {
1289226890Sdim  PragmaARCCFCodeAuditedHandler() : PragmaHandler("arc_cf_code_audited") {}
1290226890Sdim  virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1291226890Sdim                            Token &NameTok) {
1292226890Sdim    SourceLocation Loc = NameTok.getLocation();
1293226890Sdim    bool IsBegin;
1294226890Sdim
1295226890Sdim    Token Tok;
1296226890Sdim
1297226890Sdim    // Lex the 'begin' or 'end'.
1298226890Sdim    PP.LexUnexpandedToken(Tok);
1299226890Sdim    const IdentifierInfo *BeginEnd = Tok.getIdentifierInfo();
1300226890Sdim    if (BeginEnd && BeginEnd->isStr("begin")) {
1301226890Sdim      IsBegin = true;
1302226890Sdim    } else if (BeginEnd && BeginEnd->isStr("end")) {
1303226890Sdim      IsBegin = false;
1304226890Sdim    } else {
1305226890Sdim      PP.Diag(Tok.getLocation(), diag::err_pp_arc_cf_code_audited_syntax);
1306226890Sdim      return;
1307226890Sdim    }
1308226890Sdim
1309226890Sdim    // Verify that this is followed by EOD.
1310226890Sdim    PP.LexUnexpandedToken(Tok);
1311226890Sdim    if (Tok.isNot(tok::eod))
1312226890Sdim      PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
1313226890Sdim
1314226890Sdim    // The start location of the active audit.
1315226890Sdim    SourceLocation BeginLoc = PP.getPragmaARCCFCodeAuditedLoc();
1316226890Sdim
1317226890Sdim    // The start location we want after processing this.
1318226890Sdim    SourceLocation NewLoc;
1319226890Sdim
1320226890Sdim    if (IsBegin) {
1321226890Sdim      // Complain about attempts to re-enter an audit.
1322226890Sdim      if (BeginLoc.isValid()) {
1323226890Sdim        PP.Diag(Loc, diag::err_pp_double_begin_of_arc_cf_code_audited);
1324226890Sdim        PP.Diag(BeginLoc, diag::note_pragma_entered_here);
1325226890Sdim      }
1326226890Sdim      NewLoc = Loc;
1327226890Sdim    } else {
1328226890Sdim      // Complain about attempts to leave an audit that doesn't exist.
1329226890Sdim      if (!BeginLoc.isValid()) {
1330226890Sdim        PP.Diag(Loc, diag::err_pp_unmatched_end_of_arc_cf_code_audited);
1331226890Sdim        return;
1332226890Sdim      }
1333226890Sdim      NewLoc = SourceLocation();
1334226890Sdim    }
1335226890Sdim
1336226890Sdim    PP.setPragmaARCCFCodeAuditedLoc(NewLoc);
1337226890Sdim  }
1338226890Sdim};
1339226890Sdim
1340263509Sdim/// \brief Handle "\#pragma region [...]"
1341263509Sdim///
1342263509Sdim/// The syntax is
1343263509Sdim/// \code
1344263509Sdim///   #pragma region [optional name]
1345263509Sdim///   #pragma endregion [optional comment]
1346263509Sdim/// \endcode
1347263509Sdim///
1348263509Sdim/// \note This is
1349263509Sdim/// <a href="http://msdn.microsoft.com/en-us/library/b6xkz944(v=vs.80).aspx">editor-only</a>
1350263509Sdim/// pragma, just skipped by compiler.
1351263509Sdimstruct PragmaRegionHandler : public PragmaHandler {
1352263509Sdim  PragmaRegionHandler(const char *pragma) : PragmaHandler(pragma) { }
1353252723Sdim
1354263509Sdim  virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1355263509Sdim                            Token &NameTok) {
1356263509Sdim    // #pragma region: endregion matches can be verified
1357263509Sdim    // __pragma(region): no sense, but ignored by msvc
1358263509Sdim    // _Pragma is not valid for MSVC, but there isn't any point
1359263509Sdim    // to handle a _Pragma differently.
1360263509Sdim  }
1361263509Sdim};
1362252723Sdim
1363193326Sed}  // end anonymous namespace
1364193326Sed
1365193326Sed
1366193326Sed/// RegisterBuiltinPragmas - Install the standard preprocessor pragmas:
1367245431Sdim/// \#pragma GCC poison/system_header/dependency and \#pragma once.
1368193326Sedvoid Preprocessor::RegisterBuiltinPragmas() {
1369210299Sed  AddPragmaHandler(new PragmaOnceHandler());
1370210299Sed  AddPragmaHandler(new PragmaMarkHandler());
1371212904Sdim  AddPragmaHandler(new PragmaPushMacroHandler());
1372212904Sdim  AddPragmaHandler(new PragmaPopMacroHandler());
1373252723Sdim  AddPragmaHandler(new PragmaMessageHandler(PPCallbacks::PMK_Message));
1374198092Srdivacky
1375193326Sed  // #pragma GCC ...
1376210299Sed  AddPragmaHandler("GCC", new PragmaPoisonHandler());
1377210299Sed  AddPragmaHandler("GCC", new PragmaSystemHeaderHandler());
1378210299Sed  AddPragmaHandler("GCC", new PragmaDependencyHandler());
1379224145Sdim  AddPragmaHandler("GCC", new PragmaDiagnosticHandler("GCC"));
1380252723Sdim  AddPragmaHandler("GCC", new PragmaMessageHandler(PPCallbacks::PMK_Warning,
1381252723Sdim                                                   "GCC"));
1382252723Sdim  AddPragmaHandler("GCC", new PragmaMessageHandler(PPCallbacks::PMK_Error,
1383252723Sdim                                                   "GCC"));
1384193326Sed  // #pragma clang ...
1385210299Sed  AddPragmaHandler("clang", new PragmaPoisonHandler());
1386210299Sed  AddPragmaHandler("clang", new PragmaSystemHeaderHandler());
1387212904Sdim  AddPragmaHandler("clang", new PragmaDebugHandler());
1388210299Sed  AddPragmaHandler("clang", new PragmaDependencyHandler());
1389224145Sdim  AddPragmaHandler("clang", new PragmaDiagnosticHandler("clang"));
1390226890Sdim  AddPragmaHandler("clang", new PragmaARCCFCodeAuditedHandler());
1391193326Sed
1392210299Sed  AddPragmaHandler("STDC", new PragmaSTDC_FENV_ACCESSHandler());
1393210299Sed  AddPragmaHandler("STDC", new PragmaSTDC_CX_LIMITED_RANGEHandler());
1394193326Sed  AddPragmaHandler("STDC", new PragmaSTDC_UnknownHandler());
1395198092Srdivacky
1396193326Sed  // MS extensions.
1397235633Sdim  if (LangOpts.MicrosoftExt) {
1398263509Sdim    AddPragmaHandler(new PragmaWarningHandler());
1399235633Sdim    AddPragmaHandler(new PragmaIncludeAliasHandler());
1400252723Sdim    AddPragmaHandler(new PragmaRegionHandler("region"));
1401252723Sdim    AddPragmaHandler(new PragmaRegionHandler("endregion"));
1402210299Sed  }
1403193326Sed}
1404