blob: 206722347eb2829f57b31aac51c26dac57c885bb [file] [log] [blame]
Chris Lattnerb8761832006-06-24 21:31:03 +00001//===--- Pragma.cpp - Pragma registration and handling --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerb8761832006-06-24 21:31:03 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattnerb694ba72006-07-02 22:41:36 +000010// This file implements the PragmaHandler/PragmaTable interfaces and implements
11// pragma related methods of the Preprocessor class.
Chris Lattnerb8761832006-06-24 21:31:03 +000012//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Lex/Pragma.h"
Chris Lattnerb694ba72006-07-02 22:41:36 +000016#include "clang/Basic/FileManager.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000017#include "clang/Basic/IdentifierTable.h"
18#include "clang/Basic/SourceLocation.h"
Chris Lattnerb694ba72006-07-02 22:41:36 +000019#include "clang/Basic/SourceManager.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000020#include "clang/Basic/TokenKinds.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000021#include "clang/Lex/HeaderSearch.h"
22#include "clang/Lex/LexDiagnostic.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000023#include "clang/Lex/MacroInfo.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000024#include "clang/Lex/PPCallbacks.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000025#include "clang/Lex/Preprocessor.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000026#include "clang/Lex/PreprocessorLexer.h"
27#include "clang/Lex/PTHLexer.h"
28#include "clang/Lex/Token.h"
29#include "clang/Lex/TokenLexer.h"
30#include "llvm/ADT/ArrayRef.h"
31#include "llvm/ADT/DenseMap.h"
32#include "llvm/ADT/SmallString.h"
33#include "llvm/ADT/SmallVector.h"
Reid Kleckner881dff32013-09-13 22:00:30 +000034#include "llvm/ADT/STLExtras.h"
35#include "llvm/ADT/StringSwitch.h"
Daniel Dunbar211a7872010-08-18 23:09:23 +000036#include "llvm/Support/CrashRecoveryContext.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000037#include "llvm/Support/Compiler.h"
Daniel Dunbarf2cf3292010-08-17 22:32:48 +000038#include "llvm/Support/ErrorHandling.h"
Douglas Gregorc6d5edd2009-07-02 17:08:52 +000039#include <algorithm>
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000040#include <cassert>
41#include <cstdint>
42#include <limits>
43#include <string>
44#include <vector>
45
Chris Lattnerb8761832006-06-24 21:31:03 +000046using namespace clang;
47
48// Out-of-line destructor to provide a home for the class.
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +000049PragmaHandler::~PragmaHandler() {
50}
Chris Lattnerb8761832006-06-24 21:31:03 +000051
Chris Lattner2e155302006-07-03 05:34:41 +000052//===----------------------------------------------------------------------===//
Daniel Dunbard839e772010-06-11 20:10:12 +000053// EmptyPragmaHandler Implementation.
54//===----------------------------------------------------------------------===//
55
Hans Wennborg7357bbc2015-10-12 20:47:58 +000056EmptyPragmaHandler::EmptyPragmaHandler(StringRef Name) : PragmaHandler(Name) {}
Daniel Dunbard839e772010-06-11 20:10:12 +000057
Douglas Gregorc7d65762010-09-09 22:45:38 +000058void EmptyPragmaHandler::HandlePragma(Preprocessor &PP,
59 PragmaIntroducerKind Introducer,
60 Token &FirstToken) {}
Daniel Dunbard839e772010-06-11 20:10:12 +000061
62//===----------------------------------------------------------------------===//
Chris Lattner2e155302006-07-03 05:34:41 +000063// PragmaNamespace Implementation.
64//===----------------------------------------------------------------------===//
65
Chris Lattner2e155302006-07-03 05:34:41 +000066PragmaNamespace::~PragmaNamespace() {
Reid Kleckner588c9372014-02-19 23:44:52 +000067 llvm::DeleteContainerSeconds(Handlers);
Chris Lattner2e155302006-07-03 05:34:41 +000068}
69
70/// FindHandler - Check to see if there is already a handler for the
71/// specified name. If not, return the handler for the null identifier if it
72/// exists, otherwise return null. If IgnoreNull is true (the default) then
73/// the null handler isn't returned on failure to match.
Chris Lattner0e62c1c2011-07-23 10:55:15 +000074PragmaHandler *PragmaNamespace::FindHandler(StringRef Name,
Chris Lattner2e155302006-07-03 05:34:41 +000075 bool IgnoreNull) const {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000076 if (PragmaHandler *Handler = Handlers.lookup(Name))
77 return Handler;
Craig Topperd2d442c2014-05-17 23:10:59 +000078 return IgnoreNull ? nullptr : Handlers.lookup(StringRef());
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000079}
Mike Stump11289f42009-09-09 15:08:12 +000080
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000081void PragmaNamespace::AddPragma(PragmaHandler *Handler) {
82 assert(!Handlers.lookup(Handler->getName()) &&
83 "A handler with this name is already registered in this namespace");
David Blaikie13156b62014-11-19 03:06:06 +000084 Handlers[Handler->getName()] = Handler;
Chris Lattner2e155302006-07-03 05:34:41 +000085}
86
Daniel Dunbar40596532008-10-04 19:17:46 +000087void PragmaNamespace::RemovePragmaHandler(PragmaHandler *Handler) {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000088 assert(Handlers.lookup(Handler->getName()) &&
89 "Handler not registered in this namespace");
90 Handlers.erase(Handler->getName());
Daniel Dunbar40596532008-10-04 19:17:46 +000091}
92
Douglas Gregorc7d65762010-09-09 22:45:38 +000093void PragmaNamespace::HandlePragma(Preprocessor &PP,
94 PragmaIntroducerKind Introducer,
95 Token &Tok) {
Chris Lattnerb8761832006-06-24 21:31:03 +000096 // Read the 'namespace' that the directive is in, e.g. STDC. Do not macro
97 // expand it, the user can have a STDC #define, that should not affect this.
98 PP.LexUnexpandedToken(Tok);
Mike Stump11289f42009-09-09 15:08:12 +000099
Chris Lattnerb8761832006-06-24 21:31:03 +0000100 // Get the handler for this token. If there is no handler, ignore the pragma.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000101 PragmaHandler *Handler
102 = FindHandler(Tok.getIdentifierInfo() ? Tok.getIdentifierInfo()->getName()
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000103 : StringRef(),
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000104 /*IgnoreNull=*/false);
Craig Topperd2d442c2014-05-17 23:10:59 +0000105 if (!Handler) {
Chris Lattner21656f22009-04-19 21:10:26 +0000106 PP.Diag(Tok, diag::warn_pragma_ignored);
107 return;
108 }
Mike Stump11289f42009-09-09 15:08:12 +0000109
Chris Lattnerb8761832006-06-24 21:31:03 +0000110 // Otherwise, pass it down.
Douglas Gregorc7d65762010-09-09 22:45:38 +0000111 Handler->HandlePragma(PP, Introducer, Tok);
Chris Lattnerb8761832006-06-24 21:31:03 +0000112}
Chris Lattnerb694ba72006-07-02 22:41:36 +0000113
Chris Lattnerb694ba72006-07-02 22:41:36 +0000114//===----------------------------------------------------------------------===//
115// Preprocessor Pragma Directive Handling.
116//===----------------------------------------------------------------------===//
117
James Dennett18a6d792012-06-17 03:26:26 +0000118/// HandlePragmaDirective - The "\#pragma" directive has been parsed. Lex the
Chris Lattnerb694ba72006-07-02 22:41:36 +0000119/// rest of the pragma, passing it to the registered pragma handlers.
Enea Zaffanella5afb04a2013-07-20 20:09:11 +0000120void Preprocessor::HandlePragmaDirective(SourceLocation IntroducerLoc,
121 PragmaIntroducerKind Introducer) {
122 if (Callbacks)
123 Callbacks->PragmaDirective(IntroducerLoc, Introducer);
124
Jordan Rosede1a2922012-06-08 18:06:21 +0000125 if (!PragmasEnabled)
126 return;
127
Chris Lattnerb694ba72006-07-02 22:41:36 +0000128 ++NumPragma;
Mike Stump11289f42009-09-09 15:08:12 +0000129
Chris Lattnerb694ba72006-07-02 22:41:36 +0000130 // Invoke the first level of pragma handlers which reads the namespace id.
Chris Lattner146762e2007-07-20 16:59:19 +0000131 Token Tok;
Enea Zaffanella5afb04a2013-07-20 20:09:11 +0000132 PragmaHandlers->HandlePragma(*this, Introducer, Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000133
Chris Lattnerb694ba72006-07-02 22:41:36 +0000134 // If the pragma handler didn't read the rest of the line, consume it now.
Peter Collingbourne2c9f9662011-02-22 13:49:00 +0000135 if ((CurTokenLexer && CurTokenLexer->isParsingPreprocessorDirective())
136 || (CurPPLexer && CurPPLexer->ParsingPreprocessorDirective))
Chris Lattnerb694ba72006-07-02 22:41:36 +0000137 DiscardUntilEndOfDirective();
138}
139
Argyrios Kyrtzidisf1b64c62012-04-03 16:47:40 +0000140namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000141
Argyrios Kyrtzidisf1b64c62012-04-03 16:47:40 +0000142/// \brief Helper class for \see Preprocessor::Handle_Pragma.
143class LexingFor_PragmaRAII {
144 Preprocessor &PP;
145 bool InMacroArgPreExpansion;
146 bool Failed;
147 Token &OutTok;
148 Token PragmaTok;
149
150public:
151 LexingFor_PragmaRAII(Preprocessor &PP, bool InMacroArgPreExpansion,
152 Token &Tok)
153 : PP(PP), InMacroArgPreExpansion(InMacroArgPreExpansion),
154 Failed(false), OutTok(Tok) {
155 if (InMacroArgPreExpansion) {
156 PragmaTok = OutTok;
157 PP.EnableBacktrackAtThisPos();
158 }
159 }
160
161 ~LexingFor_PragmaRAII() {
162 if (InMacroArgPreExpansion) {
163 if (Failed) {
164 PP.CommitBacktrackedTokens();
165 } else {
166 PP.Backtrack();
167 OutTok = PragmaTok;
168 }
169 }
170 }
171
172 void failed() {
173 Failed = true;
174 }
175};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000176
177} // end anonymous namespace
Argyrios Kyrtzidisf1b64c62012-04-03 16:47:40 +0000178
Chris Lattnerb694ba72006-07-02 22:41:36 +0000179/// Handle_Pragma - Read a _Pragma directive, slice it up, process it, then
180/// return the first token after the directive. The _Pragma token has just
181/// been read into 'Tok'.
Chris Lattner146762e2007-07-20 16:59:19 +0000182void Preprocessor::Handle_Pragma(Token &Tok) {
Argyrios Kyrtzidisf1b64c62012-04-03 16:47:40 +0000183
184 // This works differently if we are pre-expanding a macro argument.
185 // In that case we don't actually "activate" the pragma now, we only lex it
186 // until we are sure it is lexically correct and then we backtrack so that
187 // we activate the pragma whenever we encounter the tokens again in the token
188 // stream. This ensures that we will activate it in the correct location
189 // or that we will ignore it if it never enters the token stream, e.g:
190 //
191 // #define EMPTY(x)
192 // #define INACTIVE(x) EMPTY(x)
193 // INACTIVE(_Pragma("clang diagnostic ignored \"-Wconversion\""))
194
195 LexingFor_PragmaRAII _PragmaLexing(*this, InMacroArgPreExpansion, Tok);
196
Chris Lattnerb694ba72006-07-02 22:41:36 +0000197 // Remember the pragma token location.
198 SourceLocation PragmaLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000199
Chris Lattnerb694ba72006-07-02 22:41:36 +0000200 // Read the '('.
201 Lex(Tok);
Chris Lattner907dfe92008-11-18 07:59:24 +0000202 if (Tok.isNot(tok::l_paren)) {
203 Diag(PragmaLoc, diag::err__Pragma_malformed);
Argyrios Kyrtzidisf1b64c62012-04-03 16:47:40 +0000204 return _PragmaLexing.failed();
Chris Lattner907dfe92008-11-18 07:59:24 +0000205 }
Chris Lattnerb694ba72006-07-02 22:41:36 +0000206
207 // Read the '"..."'.
208 Lex(Tok);
Richard Smithc98bb4e2013-03-09 23:30:15 +0000209 if (!tok::isStringLiteral(Tok.getKind())) {
Chris Lattner907dfe92008-11-18 07:59:24 +0000210 Diag(PragmaLoc, diag::err__Pragma_malformed);
Hubert Tong0deb6942015-07-30 21:30:00 +0000211 // Skip bad tokens, and the ')', if present.
Reid Kleckner53e6a5d2014-08-14 19:47:06 +0000212 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::eof))
Richard Smithd67aea22012-03-06 03:21:47 +0000213 Lex(Tok);
Hubert Tong0deb6942015-07-30 21:30:00 +0000214 while (Tok.isNot(tok::r_paren) &&
215 !Tok.isAtStartOfLine() &&
216 Tok.isNot(tok::eof))
217 Lex(Tok);
Richard Smithd67aea22012-03-06 03:21:47 +0000218 if (Tok.is(tok::r_paren))
219 Lex(Tok);
Argyrios Kyrtzidisf1b64c62012-04-03 16:47:40 +0000220 return _PragmaLexing.failed();
Richard Smithd67aea22012-03-06 03:21:47 +0000221 }
222
223 if (Tok.hasUDSuffix()) {
224 Diag(Tok, diag::err_invalid_string_udl);
225 // Skip this token, and the ')', if present.
226 Lex(Tok);
227 if (Tok.is(tok::r_paren))
228 Lex(Tok);
Argyrios Kyrtzidisf1b64c62012-04-03 16:47:40 +0000229 return _PragmaLexing.failed();
Chris Lattner907dfe92008-11-18 07:59:24 +0000230 }
Mike Stump11289f42009-09-09 15:08:12 +0000231
Chris Lattnerb694ba72006-07-02 22:41:36 +0000232 // Remember the string.
Argyrios Kyrtzidisf1b64c62012-04-03 16:47:40 +0000233 Token StrTok = Tok;
Chris Lattnerb694ba72006-07-02 22:41:36 +0000234
235 // Read the ')'.
236 Lex(Tok);
Chris Lattner907dfe92008-11-18 07:59:24 +0000237 if (Tok.isNot(tok::r_paren)) {
238 Diag(PragmaLoc, diag::err__Pragma_malformed);
Argyrios Kyrtzidisf1b64c62012-04-03 16:47:40 +0000239 return _PragmaLexing.failed();
Chris Lattner907dfe92008-11-18 07:59:24 +0000240 }
Mike Stump11289f42009-09-09 15:08:12 +0000241
Argyrios Kyrtzidisf1b64c62012-04-03 16:47:40 +0000242 if (InMacroArgPreExpansion)
243 return;
244
Chris Lattner9dc9c202009-02-15 20:52:18 +0000245 SourceLocation RParenLoc = Tok.getLocation();
Argyrios Kyrtzidisf1b64c62012-04-03 16:47:40 +0000246 std::string StrVal = getSpelling(StrTok);
Mike Stump11289f42009-09-09 15:08:12 +0000247
Richard Smithc98bb4e2013-03-09 23:30:15 +0000248 // The _Pragma is lexically sound. Destringize according to C11 6.10.9.1:
249 // "The string literal is destringized by deleting any encoding prefix,
Chris Lattner262d4e32009-01-16 18:59:23 +0000250 // deleting the leading and trailing double-quotes, replacing each escape
251 // sequence \" by a double-quote, and replacing each escape sequence \\ by a
252 // single backslash."
Richard Smithc98bb4e2013-03-09 23:30:15 +0000253 if (StrVal[0] == 'L' || StrVal[0] == 'U' ||
254 (StrVal[0] == 'u' && StrVal[1] != '8'))
Chris Lattnerb694ba72006-07-02 22:41:36 +0000255 StrVal.erase(StrVal.begin());
Richard Smithc98bb4e2013-03-09 23:30:15 +0000256 else if (StrVal[0] == 'u')
257 StrVal.erase(StrVal.begin(), StrVal.begin() + 2);
258
259 if (StrVal[0] == 'R') {
260 // FIXME: C++11 does not specify how to handle raw-string-literals here.
261 // We strip off the 'R', the quotes, the d-char-sequences, and the parens.
262 assert(StrVal[1] == '"' && StrVal[StrVal.size() - 1] == '"' &&
263 "Invalid raw string token!");
264
265 // Measure the length of the d-char-sequence.
266 unsigned NumDChars = 0;
267 while (StrVal[2 + NumDChars] != '(') {
268 assert(NumDChars < (StrVal.size() - 5) / 2 &&
269 "Invalid raw string token!");
270 ++NumDChars;
271 }
272 assert(StrVal[StrVal.size() - 2 - NumDChars] == ')');
273
274 // Remove 'R " d-char-sequence' and 'd-char-sequence "'. We'll replace the
275 // parens below.
276 StrVal.erase(0, 2 + NumDChars);
277 StrVal.erase(StrVal.size() - 1 - NumDChars);
278 } else {
279 assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
280 "Invalid string token!");
281
282 // Remove escaped quotes and escapes.
Benjamin Kramerc2f5f292013-05-04 10:37:20 +0000283 unsigned ResultPos = 1;
Reid Kleckner95e036c2013-09-25 16:42:48 +0000284 for (unsigned i = 1, e = StrVal.size() - 1; i != e; ++i) {
285 // Skip escapes. \\ -> '\' and \" -> '"'.
286 if (StrVal[i] == '\\' && i + 1 < e &&
287 (StrVal[i + 1] == '\\' || StrVal[i + 1] == '"'))
288 ++i;
289 StrVal[ResultPos++] = StrVal[i];
Richard Smithc98bb4e2013-03-09 23:30:15 +0000290 }
Reid Kleckner95e036c2013-09-25 16:42:48 +0000291 StrVal.erase(StrVal.begin() + ResultPos, StrVal.end() - 1);
Richard Smithc98bb4e2013-03-09 23:30:15 +0000292 }
Mike Stump11289f42009-09-09 15:08:12 +0000293
Chris Lattnerb694ba72006-07-02 22:41:36 +0000294 // Remove the front quote, replacing it with a space, so that the pragma
295 // contents appear to have a space before them.
296 StrVal[0] = ' ';
Mike Stump11289f42009-09-09 15:08:12 +0000297
Chris Lattnerfa217bd2009-03-08 08:08:45 +0000298 // Replace the terminating quote with a \n.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000299 StrVal[StrVal.size()-1] = '\n';
Mike Stump11289f42009-09-09 15:08:12 +0000300
Peter Collingbournef29ce972011-02-22 13:49:06 +0000301 // Plop the string (including the newline and trailing null) into a buffer
302 // where we can lex it.
303 Token TmpTok;
304 TmpTok.startToken();
Dmitri Gribenkob8e9e752012-09-24 21:07:17 +0000305 CreateString(StrVal, TmpTok);
Peter Collingbournef29ce972011-02-22 13:49:06 +0000306 SourceLocation TokLoc = TmpTok.getLocation();
307
308 // Make and enter a lexer object so that we lex and expand the tokens just
309 // like any others.
310 Lexer *TL = Lexer::Create_PragmaLexer(TokLoc, PragmaLoc, RParenLoc,
311 StrVal.size(), *this);
312
Craig Topperd2d442c2014-05-17 23:10:59 +0000313 EnterSourceFileWithLexer(TL, nullptr);
Peter Collingbournef29ce972011-02-22 13:49:06 +0000314
315 // With everything set up, lex this as a #pragma directive.
Enea Zaffanella5afb04a2013-07-20 20:09:11 +0000316 HandlePragmaDirective(PragmaLoc, PIK__Pragma);
John McCall89e925d2010-08-28 22:34:47 +0000317
318 // Finally, return whatever came after the pragma directive.
319 return Lex(Tok);
320}
321
322/// HandleMicrosoft__pragma - Like Handle_Pragma except the pragma text
323/// is not enclosed within a string literal.
324void Preprocessor::HandleMicrosoft__pragma(Token &Tok) {
325 // Remember the pragma token location.
326 SourceLocation PragmaLoc = Tok.getLocation();
327
328 // Read the '('.
329 Lex(Tok);
330 if (Tok.isNot(tok::l_paren)) {
331 Diag(PragmaLoc, diag::err__Pragma_malformed);
332 return;
333 }
334
Peter Collingbournef29ce972011-02-22 13:49:06 +0000335 // Get the tokens enclosed within the __pragma(), as well as the final ')'.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000336 SmallVector<Token, 32> PragmaToks;
John McCall89e925d2010-08-28 22:34:47 +0000337 int NumParens = 0;
338 Lex(Tok);
339 while (Tok.isNot(tok::eof)) {
Peter Collingbournef29ce972011-02-22 13:49:06 +0000340 PragmaToks.push_back(Tok);
John McCall89e925d2010-08-28 22:34:47 +0000341 if (Tok.is(tok::l_paren))
342 NumParens++;
343 else if (Tok.is(tok::r_paren) && NumParens-- == 0)
344 break;
John McCall89e925d2010-08-28 22:34:47 +0000345 Lex(Tok);
346 }
347
John McCall49039d42010-08-29 01:09:54 +0000348 if (Tok.is(tok::eof)) {
349 Diag(PragmaLoc, diag::err_unterminated___pragma);
350 return;
351 }
352
Peter Collingbournef29ce972011-02-22 13:49:06 +0000353 PragmaToks.front().setFlag(Token::LeadingSpace);
John McCall89e925d2010-08-28 22:34:47 +0000354
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000355 // Replace the ')' with an EOD to mark the end of the pragma.
356 PragmaToks.back().setKind(tok::eod);
Peter Collingbournef29ce972011-02-22 13:49:06 +0000357
358 Token *TokArray = new Token[PragmaToks.size()];
359 std::copy(PragmaToks.begin(), PragmaToks.end(), TokArray);
360
361 // Push the tokens onto the stack.
362 EnterTokenStream(TokArray, PragmaToks.size(), true, true);
363
364 // With everything set up, lex this as a #pragma directive.
Enea Zaffanella5afb04a2013-07-20 20:09:11 +0000365 HandlePragmaDirective(PragmaLoc, PIK___pragma);
John McCall89e925d2010-08-28 22:34:47 +0000366
367 // Finally, return whatever came after the pragma directive.
368 return Lex(Tok);
369}
370
James Dennett18a6d792012-06-17 03:26:26 +0000371/// HandlePragmaOnce - Handle \#pragma once. OnceTok is the 'once'.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000372///
Chris Lattner146762e2007-07-20 16:59:19 +0000373void Preprocessor::HandlePragmaOnce(Token &OnceTok) {
Sunil Srivastavafe583272016-07-25 17:17:06 +0000374 // Don't honor the 'once' when handling the primary source file, unless
375 // this is a prefix to a TU, which indicates we're generating a PCH file.
376 if (isInPrimaryFile() && TUKind != TU_Prefix) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000377 Diag(OnceTok, diag::pp_pragma_once_in_main_file);
378 return;
379 }
Mike Stump11289f42009-09-09 15:08:12 +0000380
Chris Lattnerb694ba72006-07-02 22:41:36 +0000381 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000382 // Mark the file as a once-only file now.
Chris Lattnerd32480d2009-01-17 06:22:33 +0000383 HeaderInfo.MarkFileIncludeOnce(getCurrentFileLexer()->getFileEntry());
Chris Lattnerb694ba72006-07-02 22:41:36 +0000384}
385
Chris Lattnerc2383312007-12-19 19:38:36 +0000386void Preprocessor::HandlePragmaMark() {
Ted Kremenek76c34412008-11-19 22:21:33 +0000387 assert(CurPPLexer && "No current lexer?");
Chris Lattnerd9efb6e2009-06-15 05:02:34 +0000388 if (CurLexer)
389 CurLexer->ReadToEndOfLine();
390 else
391 CurPTHLexer->DiscardToEndOfLine();
Chris Lattnerc2383312007-12-19 19:38:36 +0000392}
393
James Dennett18a6d792012-06-17 03:26:26 +0000394/// HandlePragmaPoison - Handle \#pragma GCC poison. PoisonTok is the 'poison'.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000395///
Erik Verbruggen851ce0e2016-10-26 11:46:10 +0000396void Preprocessor::HandlePragmaPoison() {
Chris Lattner146762e2007-07-20 16:59:19 +0000397 Token Tok;
Chris Lattner538d7f32006-07-20 04:31:52 +0000398
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000399 while (true) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000400 // Read the next token to poison. While doing this, pretend that we are
401 // skipping while reading the identifier to poison.
402 // This avoids errors on code like:
403 // #pragma GCC poison X
404 // #pragma GCC poison X
Ted Kremenek551c82a2008-11-18 01:12:54 +0000405 if (CurPPLexer) CurPPLexer->LexingRawMode = true;
Chris Lattnerb694ba72006-07-02 22:41:36 +0000406 LexUnexpandedToken(Tok);
Ted Kremenek551c82a2008-11-18 01:12:54 +0000407 if (CurPPLexer) CurPPLexer->LexingRawMode = false;
Mike Stump11289f42009-09-09 15:08:12 +0000408
Chris Lattnerb694ba72006-07-02 22:41:36 +0000409 // If we reached the end of line, we're done.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000410 if (Tok.is(tok::eod)) return;
Mike Stump11289f42009-09-09 15:08:12 +0000411
Chris Lattnerb694ba72006-07-02 22:41:36 +0000412 // Can only poison identifiers.
Abramo Bagnaraea4f7c72010-12-22 08:23:18 +0000413 if (Tok.isNot(tok::raw_identifier)) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000414 Diag(Tok, diag::err_pp_invalid_poison);
415 return;
416 }
Mike Stump11289f42009-09-09 15:08:12 +0000417
Chris Lattnercefc7682006-07-08 08:28:12 +0000418 // Look up the identifier info for the token. We disabled identifier lookup
419 // by saying we're skipping contents, so we need to do this manually.
420 IdentifierInfo *II = LookUpIdentifierInfo(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000421
Chris Lattnerb694ba72006-07-02 22:41:36 +0000422 // Already poisoned.
423 if (II->isPoisoned()) continue;
Mike Stump11289f42009-09-09 15:08:12 +0000424
Chris Lattnerb694ba72006-07-02 22:41:36 +0000425 // If this is a macro identifier, emit a warning.
Richard Smith20e883e2015-04-29 23:20:19 +0000426 if (isMacroDefined(II))
Chris Lattnerb694ba72006-07-02 22:41:36 +0000427 Diag(Tok, diag::pp_poisoning_existing_macro);
Mike Stump11289f42009-09-09 15:08:12 +0000428
Chris Lattnerb694ba72006-07-02 22:41:36 +0000429 // Finally, poison it!
430 II->setIsPoisoned();
Douglas Gregor935bc7a22011-10-27 09:33:13 +0000431 if (II->isFromAST())
432 II->setChangedSinceDeserialization();
Chris Lattnerb694ba72006-07-02 22:41:36 +0000433 }
434}
435
James Dennett18a6d792012-06-17 03:26:26 +0000436/// HandlePragmaSystemHeader - Implement \#pragma GCC system_header. We know
Chris Lattnerb694ba72006-07-02 22:41:36 +0000437/// that the whole directive has been parsed.
Chris Lattner146762e2007-07-20 16:59:19 +0000438void Preprocessor::HandlePragmaSystemHeader(Token &SysHeaderTok) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000439 if (isInPrimaryFile()) {
440 Diag(SysHeaderTok, diag::pp_pragma_sysheader_in_main_file);
441 return;
442 }
Mike Stump11289f42009-09-09 15:08:12 +0000443
Chris Lattnerb694ba72006-07-02 22:41:36 +0000444 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
Ted Kremenek300590b2008-11-20 01:45:11 +0000445 PreprocessorLexer *TheLexer = getCurrentFileLexer();
Mike Stump11289f42009-09-09 15:08:12 +0000446
Chris Lattnerb694ba72006-07-02 22:41:36 +0000447 // Mark the file as a system header.
Chris Lattnerd32480d2009-01-17 06:22:33 +0000448 HeaderInfo.MarkFileSystemHeader(TheLexer->getFileEntry());
Mike Stump11289f42009-09-09 15:08:12 +0000449
450
Chris Lattnerd9efb6e2009-06-15 05:02:34 +0000451 PresumedLoc PLoc = SourceMgr.getPresumedLoc(SysHeaderTok.getLocation());
Douglas Gregor453b0122010-11-12 07:15:47 +0000452 if (PLoc.isInvalid())
453 return;
454
Jay Foad9a6b0982011-06-21 15:13:30 +0000455 unsigned FilenameID = SourceMgr.getLineTableFilenameID(PLoc.getFilename());
Mike Stump11289f42009-09-09 15:08:12 +0000456
Chris Lattner3bdc7672011-05-22 22:10:16 +0000457 // Notify the client, if desired, that we are in a new source file.
458 if (Callbacks)
459 Callbacks->FileChanged(SysHeaderTok.getLocation(),
460 PPCallbacks::SystemHeaderPragma, SrcMgr::C_System);
461
Chris Lattnerd9efb6e2009-06-15 05:02:34 +0000462 // Emit a line marker. This will change any source locations from this point
463 // forward to realize they are in a system header.
464 // Create a line note with this information.
Jordan Rose111c4a62013-04-17 19:09:18 +0000465 SourceMgr.AddLineNote(SysHeaderTok.getLocation(), PLoc.getLine()+1,
466 FilenameID, /*IsEntry=*/false, /*IsExit=*/false,
467 /*IsSystem=*/true, /*IsExternC=*/false);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000468}
469
James Dennett18a6d792012-06-17 03:26:26 +0000470/// HandlePragmaDependency - Handle \#pragma GCC dependency "foo" blah.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000471///
Chris Lattner146762e2007-07-20 16:59:19 +0000472void Preprocessor::HandlePragmaDependency(Token &DependencyTok) {
473 Token FilenameTok;
Ted Kremenek551c82a2008-11-18 01:12:54 +0000474 CurPPLexer->LexIncludeFilename(FilenameTok);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000475
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000476 // If the token kind is EOD, the error has already been diagnosed.
477 if (FilenameTok.is(tok::eod))
Chris Lattnerb694ba72006-07-02 22:41:36 +0000478 return;
Mike Stump11289f42009-09-09 15:08:12 +0000479
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000480 // Reserve a buffer to get the spelling.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000481 SmallString<128> FilenameBuffer;
Douglas Gregordc970f02010-03-16 22:30:13 +0000482 bool Invalid = false;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000483 StringRef Filename = getSpelling(FilenameTok, FilenameBuffer, &Invalid);
Douglas Gregordc970f02010-03-16 22:30:13 +0000484 if (Invalid)
485 return;
Mike Stump11289f42009-09-09 15:08:12 +0000486
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000487 bool isAngled =
488 GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000489 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
490 // error.
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000491 if (Filename.empty())
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000492 return;
Mike Stump11289f42009-09-09 15:08:12 +0000493
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000494 // Search include directories for this file.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000495 const DirectoryLookup *CurDir;
Richard Smith25d50752014-10-20 00:15:49 +0000496 const FileEntry *File =
497 LookupFile(FilenameTok.getLocation(), Filename, isAngled, nullptr,
498 nullptr, CurDir, nullptr, nullptr, nullptr);
Craig Topperd2d442c2014-05-17 23:10:59 +0000499 if (!File) {
Eli Friedman3781a362011-08-30 23:07:51 +0000500 if (!SuppressIncludeNotFoundError)
501 Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
Chris Lattner97b8e842008-11-18 08:02:48 +0000502 return;
503 }
Mike Stump11289f42009-09-09 15:08:12 +0000504
Chris Lattnerd32480d2009-01-17 06:22:33 +0000505 const FileEntry *CurFile = getCurrentFileLexer()->getFileEntry();
Chris Lattnerb694ba72006-07-02 22:41:36 +0000506
507 // If this file is older than the file it depends on, emit a diagnostic.
508 if (CurFile && CurFile->getModificationTime() < File->getModificationTime()) {
509 // Lex tokens at the end of the message and include them in the message.
510 std::string Message;
511 Lex(DependencyTok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000512 while (DependencyTok.isNot(tok::eod)) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000513 Message += getSpelling(DependencyTok) + " ";
514 Lex(DependencyTok);
515 }
Mike Stump11289f42009-09-09 15:08:12 +0000516
Chris Lattnerf0b04972010-09-05 23:16:09 +0000517 // Remove the trailing ' ' if present.
518 if (!Message.empty())
519 Message.erase(Message.end()-1);
Chris Lattner97b8e842008-11-18 08:02:48 +0000520 Diag(FilenameTok, diag::pp_out_of_date_dependency) << Message;
Chris Lattnerb694ba72006-07-02 22:41:36 +0000521 }
522}
523
Reid Kleckner002562a2013-05-06 21:02:12 +0000524/// ParsePragmaPushOrPopMacro - Handle parsing of pragma push_macro/pop_macro.
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000525/// Return the IdentifierInfo* associated with the macro to push or pop.
526IdentifierInfo *Preprocessor::ParsePragmaPushOrPopMacro(Token &Tok) {
527 // Remember the pragma token location.
528 Token PragmaTok = Tok;
529
530 // Read the '('.
531 Lex(Tok);
532 if (Tok.isNot(tok::l_paren)) {
533 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
534 << getSpelling(PragmaTok);
Craig Topperd2d442c2014-05-17 23:10:59 +0000535 return nullptr;
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000536 }
537
538 // Read the macro name string.
539 Lex(Tok);
540 if (Tok.isNot(tok::string_literal)) {
541 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
542 << getSpelling(PragmaTok);
Craig Topperd2d442c2014-05-17 23:10:59 +0000543 return nullptr;
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000544 }
545
Richard Smithd67aea22012-03-06 03:21:47 +0000546 if (Tok.hasUDSuffix()) {
547 Diag(Tok, diag::err_invalid_string_udl);
Craig Topperd2d442c2014-05-17 23:10:59 +0000548 return nullptr;
Richard Smithd67aea22012-03-06 03:21:47 +0000549 }
550
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000551 // Remember the macro string.
552 std::string StrVal = getSpelling(Tok);
553
554 // Read the ')'.
555 Lex(Tok);
556 if (Tok.isNot(tok::r_paren)) {
557 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
558 << getSpelling(PragmaTok);
Craig Topperd2d442c2014-05-17 23:10:59 +0000559 return nullptr;
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000560 }
561
562 assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
563 "Invalid string token!");
564
565 // Create a Token from the string.
566 Token MacroTok;
567 MacroTok.startToken();
Abramo Bagnaraea4f7c72010-12-22 08:23:18 +0000568 MacroTok.setKind(tok::raw_identifier);
Dmitri Gribenkob8e9e752012-09-24 21:07:17 +0000569 CreateString(StringRef(&StrVal[1], StrVal.size() - 2), MacroTok);
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000570
571 // Get the IdentifierInfo of MacroToPushTok.
572 return LookUpIdentifierInfo(MacroTok);
573}
574
James Dennett18a6d792012-06-17 03:26:26 +0000575/// \brief Handle \#pragma push_macro.
576///
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000577/// The syntax is:
James Dennett18a6d792012-06-17 03:26:26 +0000578/// \code
Dmitri Gribenko9ebd1612012-11-30 20:04:39 +0000579/// #pragma push_macro("macro")
James Dennett18a6d792012-06-17 03:26:26 +0000580/// \endcode
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000581void Preprocessor::HandlePragmaPushMacro(Token &PushMacroTok) {
582 // Parse the pragma directive and get the macro IdentifierInfo*.
583 IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PushMacroTok);
584 if (!IdentInfo) return;
585
586 // Get the MacroInfo associated with IdentInfo.
587 MacroInfo *MI = getMacroInfo(IdentInfo);
588
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000589 if (MI) {
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000590 // Allow the original MacroInfo to be redefined later.
591 MI->setIsAllowRedefinitionsWithoutWarning(true);
592 }
593
594 // Push the cloned MacroInfo so we can retrieve it later.
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +0000595 PragmaPushMacroInfo[IdentInfo].push_back(MI);
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000596}
597
James Dennett18a6d792012-06-17 03:26:26 +0000598/// \brief Handle \#pragma pop_macro.
599///
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000600/// The syntax is:
James Dennett18a6d792012-06-17 03:26:26 +0000601/// \code
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000602/// #pragma pop_macro("macro")
James Dennett18a6d792012-06-17 03:26:26 +0000603/// \endcode
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000604void Preprocessor::HandlePragmaPopMacro(Token &PopMacroTok) {
605 SourceLocation MessageLoc = PopMacroTok.getLocation();
606
607 // Parse the pragma directive and get the macro IdentifierInfo*.
608 IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PopMacroTok);
609 if (!IdentInfo) return;
610
611 // Find the vector<MacroInfo*> associated with the macro.
612 llvm::DenseMap<IdentifierInfo*, std::vector<MacroInfo*> >::iterator iter =
613 PragmaPushMacroInfo.find(IdentInfo);
614 if (iter != PragmaPushMacroInfo.end()) {
Alexander Kornienko8b3f6232012-08-29 00:20:03 +0000615 // Forget the MacroInfo currently associated with IdentInfo.
Richard Smith20e883e2015-04-29 23:20:19 +0000616 if (MacroInfo *MI = getMacroInfo(IdentInfo)) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +0000617 if (MI->isWarnIfUnused())
618 WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());
619 appendMacroDirective(IdentInfo, AllocateUndefMacroDirective(MessageLoc));
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000620 }
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000621
622 // Get the MacroInfo we want to reinstall.
623 MacroInfo *MacroToReInstall = iter->second.back();
624
Richard Smith713369b2015-04-23 20:40:50 +0000625 if (MacroToReInstall)
Alexander Kornienkoc0b49282012-08-29 16:56:24 +0000626 // Reinstall the previously pushed macro.
Richard Smith713369b2015-04-23 20:40:50 +0000627 appendDefMacroDirective(IdentInfo, MacroToReInstall, MessageLoc);
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000628
629 // Pop PragmaPushMacroInfo stack.
630 iter->second.pop_back();
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000631 if (iter->second.empty())
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000632 PragmaPushMacroInfo.erase(iter);
633 } else {
634 Diag(MessageLoc, diag::warn_pragma_pop_macro_no_push)
635 << IdentInfo->getName();
636 }
637}
Chris Lattnerb694ba72006-07-02 22:41:36 +0000638
Aaron Ballman611306e2012-03-02 22:51:54 +0000639void Preprocessor::HandlePragmaIncludeAlias(Token &Tok) {
640 // We will either get a quoted filename or a bracketed filename, and we
641 // have to track which we got. The first filename is the source name,
642 // and the second name is the mapped filename. If the first is quoted,
643 // the second must be as well (cannot mix and match quotes and brackets).
Aaron Ballman611306e2012-03-02 22:51:54 +0000644
645 // Get the open paren
646 Lex(Tok);
647 if (Tok.isNot(tok::l_paren)) {
648 Diag(Tok, diag::warn_pragma_include_alias_expected) << "(";
649 return;
650 }
651
652 // We expect either a quoted string literal, or a bracketed name
653 Token SourceFilenameTok;
654 CurPPLexer->LexIncludeFilename(SourceFilenameTok);
655 if (SourceFilenameTok.is(tok::eod)) {
656 // The diagnostic has already been handled
657 return;
658 }
659
660 StringRef SourceFileName;
661 SmallString<128> FileNameBuffer;
662 if (SourceFilenameTok.is(tok::string_literal) ||
663 SourceFilenameTok.is(tok::angle_string_literal)) {
664 SourceFileName = getSpelling(SourceFilenameTok, FileNameBuffer);
665 } else if (SourceFilenameTok.is(tok::less)) {
666 // This could be a path instead of just a name
667 FileNameBuffer.push_back('<');
668 SourceLocation End;
669 if (ConcatenateIncludeName(FileNameBuffer, End))
670 return; // Diagnostic already emitted
Yaron Keren92e1b622015-03-18 10:17:07 +0000671 SourceFileName = FileNameBuffer;
Aaron Ballman611306e2012-03-02 22:51:54 +0000672 } else {
673 Diag(Tok, diag::warn_pragma_include_alias_expected_filename);
674 return;
675 }
676 FileNameBuffer.clear();
677
678 // Now we expect a comma, followed by another include name
679 Lex(Tok);
680 if (Tok.isNot(tok::comma)) {
681 Diag(Tok, diag::warn_pragma_include_alias_expected) << ",";
682 return;
683 }
684
685 Token ReplaceFilenameTok;
686 CurPPLexer->LexIncludeFilename(ReplaceFilenameTok);
687 if (ReplaceFilenameTok.is(tok::eod)) {
688 // The diagnostic has already been handled
689 return;
690 }
691
692 StringRef ReplaceFileName;
693 if (ReplaceFilenameTok.is(tok::string_literal) ||
694 ReplaceFilenameTok.is(tok::angle_string_literal)) {
695 ReplaceFileName = getSpelling(ReplaceFilenameTok, FileNameBuffer);
696 } else if (ReplaceFilenameTok.is(tok::less)) {
697 // This could be a path instead of just a name
698 FileNameBuffer.push_back('<');
699 SourceLocation End;
700 if (ConcatenateIncludeName(FileNameBuffer, End))
701 return; // Diagnostic already emitted
Yaron Keren92e1b622015-03-18 10:17:07 +0000702 ReplaceFileName = FileNameBuffer;
Aaron Ballman611306e2012-03-02 22:51:54 +0000703 } else {
704 Diag(Tok, diag::warn_pragma_include_alias_expected_filename);
705 return;
706 }
707
708 // Finally, we expect the closing paren
709 Lex(Tok);
710 if (Tok.isNot(tok::r_paren)) {
711 Diag(Tok, diag::warn_pragma_include_alias_expected) << ")";
712 return;
713 }
714
715 // Now that we have the source and target filenames, we need to make sure
716 // they're both of the same type (angled vs non-angled)
717 StringRef OriginalSource = SourceFileName;
718
719 bool SourceIsAngled =
720 GetIncludeFilenameSpelling(SourceFilenameTok.getLocation(),
721 SourceFileName);
722 bool ReplaceIsAngled =
723 GetIncludeFilenameSpelling(ReplaceFilenameTok.getLocation(),
724 ReplaceFileName);
725 if (!SourceFileName.empty() && !ReplaceFileName.empty() &&
726 (SourceIsAngled != ReplaceIsAngled)) {
727 unsigned int DiagID;
728 if (SourceIsAngled)
729 DiagID = diag::warn_pragma_include_alias_mismatch_angle;
730 else
731 DiagID = diag::warn_pragma_include_alias_mismatch_quote;
732
733 Diag(SourceFilenameTok.getLocation(), DiagID)
734 << SourceFileName
735 << ReplaceFileName;
736
737 return;
738 }
739
740 // Now we can let the include handler know about this mapping
741 getHeaderSearchInfo().AddIncludeAlias(OriginalSource, ReplaceFileName);
742}
743
Chris Lattnerb694ba72006-07-02 22:41:36 +0000744/// AddPragmaHandler - Add the specified pragma handler to the preprocessor.
745/// If 'Namespace' is non-null, then it is a token required to exist on the
746/// pragma line before the pragma string starts, e.g. "STDC" or "GCC".
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000747void Preprocessor::AddPragmaHandler(StringRef Namespace,
Chris Lattnerb694ba72006-07-02 22:41:36 +0000748 PragmaHandler *Handler) {
Craig Topperbe250302014-09-12 05:19:24 +0000749 PragmaNamespace *InsertNS = PragmaHandlers.get();
Mike Stump11289f42009-09-09 15:08:12 +0000750
Chris Lattnerb694ba72006-07-02 22:41:36 +0000751 // If this is specified to be in a namespace, step down into it.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000752 if (!Namespace.empty()) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000753 // If there is already a pragma handler with the name of this namespace,
754 // we either have an error (directive with the same name as a namespace) or
755 // we already have the namespace to insert into.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000756 if (PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace)) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000757 InsertNS = Existing->getIfNamespace();
Craig Topperd2d442c2014-05-17 23:10:59 +0000758 assert(InsertNS != nullptr && "Cannot have a pragma namespace and pragma"
Chris Lattnerb694ba72006-07-02 22:41:36 +0000759 " handler with the same name!");
760 } else {
761 // Otherwise, this namespace doesn't exist yet, create and insert the
762 // handler for it.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000763 InsertNS = new PragmaNamespace(Namespace);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000764 PragmaHandlers->AddPragma(InsertNS);
765 }
766 }
Mike Stump11289f42009-09-09 15:08:12 +0000767
Chris Lattnerb694ba72006-07-02 22:41:36 +0000768 // Check to make sure we don't already have a pragma for this identifier.
769 assert(!InsertNS->FindHandler(Handler->getName()) &&
770 "Pragma handler already exists for this identifier!");
771 InsertNS->AddPragma(Handler);
772}
773
Daniel Dunbar40596532008-10-04 19:17:46 +0000774/// RemovePragmaHandler - Remove the specific pragma handler from the
775/// preprocessor. If \arg Namespace is non-null, then it should be the
776/// namespace that \arg Handler was added to. It is an error to remove
777/// a handler that has not been registered.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000778void Preprocessor::RemovePragmaHandler(StringRef Namespace,
Daniel Dunbar40596532008-10-04 19:17:46 +0000779 PragmaHandler *Handler) {
Craig Topperbe250302014-09-12 05:19:24 +0000780 PragmaNamespace *NS = PragmaHandlers.get();
Mike Stump11289f42009-09-09 15:08:12 +0000781
Daniel Dunbar40596532008-10-04 19:17:46 +0000782 // If this is specified to be in a namespace, step down into it.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000783 if (!Namespace.empty()) {
784 PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace);
Daniel Dunbar40596532008-10-04 19:17:46 +0000785 assert(Existing && "Namespace containing handler does not exist!");
786
787 NS = Existing->getIfNamespace();
788 assert(NS && "Invalid namespace, registered as a regular pragma handler!");
789 }
790
791 NS->RemovePragmaHandler(Handler);
Mike Stump11289f42009-09-09 15:08:12 +0000792
Craig Topperbe250302014-09-12 05:19:24 +0000793 // If this is a non-default namespace and it is now empty, remove it.
794 if (NS != PragmaHandlers.get() && NS->IsEmpty()) {
Daniel Dunbar40596532008-10-04 19:17:46 +0000795 PragmaHandlers->RemovePragmaHandler(NS);
Argyrios Kyrtzidis7ce75262012-01-06 00:22:09 +0000796 delete NS;
797 }
Daniel Dunbar40596532008-10-04 19:17:46 +0000798}
799
Peter Collingbourne3bffa522011-02-14 01:42:24 +0000800bool Preprocessor::LexOnOffSwitch(tok::OnOffSwitch &Result) {
801 Token Tok;
802 LexUnexpandedToken(Tok);
803
804 if (Tok.isNot(tok::identifier)) {
805 Diag(Tok, diag::ext_on_off_switch_syntax);
806 return true;
807 }
808 IdentifierInfo *II = Tok.getIdentifierInfo();
809 if (II->isStr("ON"))
810 Result = tok::OOS_ON;
811 else if (II->isStr("OFF"))
812 Result = tok::OOS_OFF;
813 else if (II->isStr("DEFAULT"))
814 Result = tok::OOS_DEFAULT;
815 else {
816 Diag(Tok, diag::ext_on_off_switch_syntax);
817 return true;
818 }
819
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000820 // Verify that this is followed by EOD.
Peter Collingbourne3bffa522011-02-14 01:42:24 +0000821 LexUnexpandedToken(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000822 if (Tok.isNot(tok::eod))
823 Diag(Tok, diag::ext_pragma_syntax_eod);
Peter Collingbourne3bffa522011-02-14 01:42:24 +0000824 return false;
825}
826
Chris Lattnerb694ba72006-07-02 22:41:36 +0000827namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000828
James Dennett18a6d792012-06-17 03:26:26 +0000829/// PragmaOnceHandler - "\#pragma once" marks the file as atomically included.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000830struct PragmaOnceHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000831 PragmaOnceHandler() : PragmaHandler("once") {}
Craig Topper9140dd22014-03-11 06:50:42 +0000832 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
833 Token &OnceTok) override {
Chris Lattnerce2ab6f2009-04-14 05:07:49 +0000834 PP.CheckEndOfDirective("pragma once");
Chris Lattnerb694ba72006-07-02 22:41:36 +0000835 PP.HandlePragmaOnce(OnceTok);
836 }
837};
838
James Dennett18a6d792012-06-17 03:26:26 +0000839/// PragmaMarkHandler - "\#pragma mark ..." is ignored by the compiler, and the
Chris Lattnerc2383312007-12-19 19:38:36 +0000840/// rest of the line is not lexed.
841struct PragmaMarkHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000842 PragmaMarkHandler() : PragmaHandler("mark") {}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000843
Craig Topper9140dd22014-03-11 06:50:42 +0000844 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
845 Token &MarkTok) override {
Chris Lattnerc2383312007-12-19 19:38:36 +0000846 PP.HandlePragmaMark();
847 }
848};
849
James Dennett18a6d792012-06-17 03:26:26 +0000850/// PragmaPoisonHandler - "\#pragma poison x" marks x as not usable.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000851struct PragmaPoisonHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000852 PragmaPoisonHandler() : PragmaHandler("poison") {}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000853
Craig Topper9140dd22014-03-11 06:50:42 +0000854 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
855 Token &PoisonTok) override {
Erik Verbruggen851ce0e2016-10-26 11:46:10 +0000856 PP.HandlePragmaPoison();
Chris Lattnerb694ba72006-07-02 22:41:36 +0000857 }
858};
859
James Dennett18a6d792012-06-17 03:26:26 +0000860/// PragmaSystemHeaderHandler - "\#pragma system_header" marks the current file
Chris Lattnerc2383312007-12-19 19:38:36 +0000861/// as a system header, which silences warnings in it.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000862struct PragmaSystemHeaderHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000863 PragmaSystemHeaderHandler() : PragmaHandler("system_header") {}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000864
Craig Topper9140dd22014-03-11 06:50:42 +0000865 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
866 Token &SHToken) override {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000867 PP.HandlePragmaSystemHeader(SHToken);
Chris Lattnerce2ab6f2009-04-14 05:07:49 +0000868 PP.CheckEndOfDirective("pragma");
Chris Lattnerb694ba72006-07-02 22:41:36 +0000869 }
870};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000871
Chris Lattnerb694ba72006-07-02 22:41:36 +0000872struct PragmaDependencyHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000873 PragmaDependencyHandler() : PragmaHandler("dependency") {}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000874
Craig Topper9140dd22014-03-11 06:50:42 +0000875 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
876 Token &DepToken) override {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000877 PP.HandlePragmaDependency(DepToken);
878 }
879};
Mike Stump11289f42009-09-09 15:08:12 +0000880
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000881struct PragmaDebugHandler : public PragmaHandler {
882 PragmaDebugHandler() : PragmaHandler("__debug") {}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000883
Craig Topper9140dd22014-03-11 06:50:42 +0000884 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
885 Token &DepToken) override {
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000886 Token Tok;
887 PP.LexUnexpandedToken(Tok);
888 if (Tok.isNot(tok::identifier)) {
Douglas Gregor3cc26482010-08-30 15:15:34 +0000889 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000890 return;
891 }
892 IdentifierInfo *II = Tok.getIdentifierInfo();
893
Daniel Dunbarf2cf3292010-08-17 22:32:48 +0000894 if (II->isStr("assert")) {
David Blaikie83d382b2011-09-23 05:06:16 +0000895 llvm_unreachable("This is an assertion!");
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000896 } else if (II->isStr("crash")) {
David Blaikie5bd4c2a2012-08-21 18:56:49 +0000897 LLVM_BUILTIN_TRAP;
David Blaikie5d577a22012-06-29 22:03:56 +0000898 } else if (II->isStr("parser_crash")) {
899 Token Crasher;
Benjamin Kramer3162f292015-03-08 19:28:24 +0000900 Crasher.startToken();
David Blaikie5d577a22012-06-29 22:03:56 +0000901 Crasher.setKind(tok::annot_pragma_parser_crash);
Benjamin Kramer3162f292015-03-08 19:28:24 +0000902 Crasher.setAnnotationRange(SourceRange(Tok.getLocation()));
David Blaikie5d577a22012-06-29 22:03:56 +0000903 PP.EnterToken(Crasher);
Richard Smithba3a4f92016-01-12 21:59:26 +0000904 } else if (II->isStr("dump")) {
905 Token Identifier;
906 PP.LexUnexpandedToken(Identifier);
907 if (auto *DumpII = Identifier.getIdentifierInfo()) {
908 Token DumpAnnot;
909 DumpAnnot.startToken();
910 DumpAnnot.setKind(tok::annot_pragma_dump);
911 DumpAnnot.setAnnotationRange(
912 SourceRange(Tok.getLocation(), Identifier.getLocation()));
913 DumpAnnot.setAnnotationValue(DumpII);
914 PP.DiscardUntilEndOfDirective();
915 PP.EnterToken(DumpAnnot);
916 } else {
917 PP.Diag(Identifier, diag::warn_pragma_debug_missing_argument)
918 << II->getName();
919 }
Daniel Dunbarf2cf3292010-08-17 22:32:48 +0000920 } else if (II->isStr("llvm_fatal_error")) {
921 llvm::report_fatal_error("#pragma clang __debug llvm_fatal_error");
922 } else if (II->isStr("llvm_unreachable")) {
923 llvm_unreachable("#pragma clang __debug llvm_unreachable");
Richard Smith3ffa61d2015-04-30 23:10:40 +0000924 } else if (II->isStr("macro")) {
925 Token MacroName;
926 PP.LexUnexpandedToken(MacroName);
927 auto *MacroII = MacroName.getIdentifierInfo();
928 if (MacroII)
929 PP.dumpMacroInfo(MacroII);
930 else
Richard Smithba3a4f92016-01-12 21:59:26 +0000931 PP.Diag(MacroName, diag::warn_pragma_debug_missing_argument)
932 << II->getName();
Daniel Dunbarf2cf3292010-08-17 22:32:48 +0000933 } else if (II->isStr("overflow_stack")) {
934 DebugOverflowStack();
Daniel Dunbar211a7872010-08-18 23:09:23 +0000935 } else if (II->isStr("handle_crash")) {
936 llvm::CrashRecoveryContext *CRC =llvm::CrashRecoveryContext::GetCurrent();
937 if (CRC)
938 CRC->HandleCrash();
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000939 } else if (II->isStr("captured")) {
940 HandleCaptured(PP);
Daniel Dunbarf2cf3292010-08-17 22:32:48 +0000941 } else {
942 PP.Diag(Tok, diag::warn_pragma_debug_unexpected_command)
943 << II->getName();
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000944 }
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000945
946 PPCallbacks *Callbacks = PP.getPPCallbacks();
947 if (Callbacks)
948 Callbacks->PragmaDebug(Tok.getLocation(), II->getName());
949 }
950
951 void HandleCaptured(Preprocessor &PP) {
952 // Skip if emitting preprocessed output.
953 if (PP.isPreprocessedOutput())
954 return;
955
956 Token Tok;
957 PP.LexUnexpandedToken(Tok);
958
959 if (Tok.isNot(tok::eod)) {
960 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol)
961 << "pragma clang __debug captured";
962 return;
963 }
964
965 SourceLocation NameLoc = Tok.getLocation();
David Blaikie2eabcc92016-02-09 18:52:09 +0000966 MutableArrayRef<Token> Toks(
967 PP.getPreprocessorAllocator().Allocate<Token>(1), 1);
968 Toks[0].startToken();
969 Toks[0].setKind(tok::annot_pragma_captured);
970 Toks[0].setLocation(NameLoc);
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000971
David Blaikie2eabcc92016-02-09 18:52:09 +0000972 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000973 }
974
Francois Pichet2e11f5d2011-05-25 16:15:03 +0000975// Disable MSVC warning about runtime stack overflow.
976#ifdef _MSC_VER
977 #pragma warning(disable : 4717)
978#endif
Richard Trieu0732beb2013-12-21 01:04:02 +0000979 static void DebugOverflowStack() {
980 void (*volatile Self)() = DebugOverflowStack;
981 Self();
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000982 }
Francois Pichet2e11f5d2011-05-25 16:15:03 +0000983#ifdef _MSC_VER
984 #pragma warning(default : 4717)
985#endif
986
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000987};
988
James Dennett18a6d792012-06-17 03:26:26 +0000989/// PragmaDiagnosticHandler - e.g. '\#pragma GCC diagnostic ignored "-Wformat"'
Chris Lattner504af112009-04-19 23:16:58 +0000990struct PragmaDiagnosticHandler : public PragmaHandler {
Douglas Gregor3bde9b12011-06-22 19:41:48 +0000991private:
992 const char *Namespace;
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000993
Chris Lattnerfb42a182009-07-12 21:18:45 +0000994public:
Douglas Gregor3bde9b12011-06-22 19:41:48 +0000995 explicit PragmaDiagnosticHandler(const char *NS) :
996 PragmaHandler("diagnostic"), Namespace(NS) {}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000997
Craig Topper9140dd22014-03-11 06:50:42 +0000998 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
999 Token &DiagToken) override {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +00001000 SourceLocation DiagLoc = DiagToken.getLocation();
Chris Lattner504af112009-04-19 23:16:58 +00001001 Token Tok;
1002 PP.LexUnexpandedToken(Tok);
1003 if (Tok.isNot(tok::identifier)) {
Douglas Gregor3cc26482010-08-30 15:15:34 +00001004 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Chris Lattner504af112009-04-19 23:16:58 +00001005 return;
1006 }
1007 IdentifierInfo *II = Tok.getIdentifierInfo();
Douglas Gregor3bde9b12011-06-22 19:41:48 +00001008 PPCallbacks *Callbacks = PP.getPPCallbacks();
Mike Stump11289f42009-09-09 15:08:12 +00001009
Alp Toker46df1c02014-06-12 10:15:20 +00001010 if (II->isStr("pop")) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +00001011 if (!PP.getDiagnostics().popMappings(DiagLoc))
Douglas Gregor3cc26482010-08-30 15:15:34 +00001012 PP.Diag(Tok, diag::warn_pragma_diagnostic_cannot_pop);
Douglas Gregor3bde9b12011-06-22 19:41:48 +00001013 else if (Callbacks)
1014 Callbacks->PragmaDiagnosticPop(DiagLoc, Namespace);
Douglas Gregor3cc26482010-08-30 15:15:34 +00001015 return;
1016 } else if (II->isStr("push")) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +00001017 PP.getDiagnostics().pushMappings(DiagLoc);
Douglas Gregor3bde9b12011-06-22 19:41:48 +00001018 if (Callbacks)
1019 Callbacks->PragmaDiagnosticPush(DiagLoc, Namespace);
Chris Lattnerfb42a182009-07-12 21:18:45 +00001020 return;
Alp Toker46df1c02014-06-12 10:15:20 +00001021 }
1022
1023 diag::Severity SV = llvm::StringSwitch<diag::Severity>(II->getName())
1024 .Case("ignored", diag::Severity::Ignored)
1025 .Case("warning", diag::Severity::Warning)
1026 .Case("error", diag::Severity::Error)
1027 .Case("fatal", diag::Severity::Fatal)
1028 .Default(diag::Severity());
1029
1030 if (SV == diag::Severity()) {
Douglas Gregor3cc26482010-08-30 15:15:34 +00001031 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Chris Lattner504af112009-04-19 23:16:58 +00001032 return;
1033 }
Mike Stump11289f42009-09-09 15:08:12 +00001034
Chris Lattner504af112009-04-19 23:16:58 +00001035 PP.LexUnexpandedToken(Tok);
Andy Gibbs58905d22012-11-17 19:15:38 +00001036 SourceLocation StringLoc = Tok.getLocation();
Chris Lattner504af112009-04-19 23:16:58 +00001037
Andy Gibbs58905d22012-11-17 19:15:38 +00001038 std::string WarningName;
Andy Gibbsa8df57a2012-11-17 19:16:52 +00001039 if (!PP.FinishLexStringLiteral(Tok, WarningName, "pragma diagnostic",
1040 /*MacroExpansion=*/false))
Chris Lattner504af112009-04-19 23:16:58 +00001041 return;
Mike Stump11289f42009-09-09 15:08:12 +00001042
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001043 if (Tok.isNot(tok::eod)) {
Chris Lattner504af112009-04-19 23:16:58 +00001044 PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token);
1045 return;
1046 }
Mike Stump11289f42009-09-09 15:08:12 +00001047
Chris Lattner504af112009-04-19 23:16:58 +00001048 if (WarningName.size() < 3 || WarningName[0] != '-' ||
Richard Smith3be1cb22014-08-07 00:24:21 +00001049 (WarningName[1] != 'W' && WarningName[1] != 'R')) {
Andy Gibbs58905d22012-11-17 19:15:38 +00001050 PP.Diag(StringLoc, diag::warn_pragma_diagnostic_invalid_option);
Chris Lattner504af112009-04-19 23:16:58 +00001051 return;
1052 }
Mike Stump11289f42009-09-09 15:08:12 +00001053
Sunil Srivastava5239de72016-02-13 01:44:05 +00001054 diag::Flavor Flavor = WarningName[1] == 'W' ? diag::Flavor::WarningOrError
1055 : diag::Flavor::Remark;
Benjamin Kramer2193e232016-02-13 13:42:41 +00001056 StringRef Group = StringRef(WarningName).substr(2);
Sunil Srivastava5239de72016-02-13 01:44:05 +00001057 bool unknownDiag = false;
1058 if (Group == "everything") {
1059 // Special handling for pragma clang diagnostic ... "-Weverything".
1060 // There is no formal group named "everything", so there has to be a
1061 // special case for it.
1062 PP.getDiagnostics().setSeverityForAll(Flavor, SV, DiagLoc);
1063 } else
1064 unknownDiag = PP.getDiagnostics().setSeverityForGroup(Flavor, Group, SV,
1065 DiagLoc);
1066 if (unknownDiag)
Andy Gibbs58905d22012-11-17 19:15:38 +00001067 PP.Diag(StringLoc, diag::warn_pragma_diagnostic_unknown_warning)
1068 << WarningName;
Douglas Gregor3bde9b12011-06-22 19:41:48 +00001069 else if (Callbacks)
Alp Toker46df1c02014-06-12 10:15:20 +00001070 Callbacks->PragmaDiagnostic(DiagLoc, Namespace, SV, WarningName);
Chris Lattner504af112009-04-19 23:16:58 +00001071 }
1072};
Mike Stump11289f42009-09-09 15:08:12 +00001073
Reid Kleckner881dff32013-09-13 22:00:30 +00001074/// "\#pragma warning(...)". MSVC's diagnostics do not map cleanly to clang's
1075/// diagnostics, so we don't really implement this pragma. We parse it and
1076/// ignore it to avoid -Wunknown-pragma warnings.
1077struct PragmaWarningHandler : public PragmaHandler {
1078 PragmaWarningHandler() : PragmaHandler("warning") {}
1079
Craig Topper9140dd22014-03-11 06:50:42 +00001080 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1081 Token &Tok) override {
Reid Kleckner881dff32013-09-13 22:00:30 +00001082 // Parse things like:
1083 // warning(push, 1)
1084 // warning(pop)
John Thompson4762b232013-11-16 00:16:03 +00001085 // warning(disable : 1 2 3 ; error : 4 5 6 ; suppress : 7 8 9)
Reid Kleckner881dff32013-09-13 22:00:30 +00001086 SourceLocation DiagLoc = Tok.getLocation();
1087 PPCallbacks *Callbacks = PP.getPPCallbacks();
1088
1089 PP.Lex(Tok);
1090 if (Tok.isNot(tok::l_paren)) {
1091 PP.Diag(Tok, diag::warn_pragma_warning_expected) << "(";
1092 return;
1093 }
1094
1095 PP.Lex(Tok);
1096 IdentifierInfo *II = Tok.getIdentifierInfo();
Reid Kleckner881dff32013-09-13 22:00:30 +00001097
Alexander Musman6b080fc2015-05-25 11:21:20 +00001098 if (II && II->isStr("push")) {
Reid Kleckner881dff32013-09-13 22:00:30 +00001099 // #pragma warning( push[ ,n ] )
Reid Kleckner4d185102013-10-02 15:19:23 +00001100 int Level = -1;
Reid Kleckner881dff32013-09-13 22:00:30 +00001101 PP.Lex(Tok);
1102 if (Tok.is(tok::comma)) {
1103 PP.Lex(Tok);
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001104 uint64_t Value;
1105 if (Tok.is(tok::numeric_constant) &&
1106 PP.parseSimpleIntegerLiteral(Tok, Value))
1107 Level = int(Value);
Reid Kleckner4d185102013-10-02 15:19:23 +00001108 if (Level < 0 || Level > 4) {
Reid Kleckner881dff32013-09-13 22:00:30 +00001109 PP.Diag(Tok, diag::warn_pragma_warning_push_level);
1110 return;
1111 }
1112 }
1113 if (Callbacks)
1114 Callbacks->PragmaWarningPush(DiagLoc, Level);
Alexander Musman6b080fc2015-05-25 11:21:20 +00001115 } else if (II && II->isStr("pop")) {
Reid Kleckner881dff32013-09-13 22:00:30 +00001116 // #pragma warning( pop )
1117 PP.Lex(Tok);
1118 if (Callbacks)
1119 Callbacks->PragmaWarningPop(DiagLoc);
1120 } else {
1121 // #pragma warning( warning-specifier : warning-number-list
1122 // [; warning-specifier : warning-number-list...] )
1123 while (true) {
1124 II = Tok.getIdentifierInfo();
Alexander Musman6b080fc2015-05-25 11:21:20 +00001125 if (!II && !Tok.is(tok::numeric_constant)) {
Reid Kleckner881dff32013-09-13 22:00:30 +00001126 PP.Diag(Tok, diag::warn_pragma_warning_spec_invalid);
1127 return;
1128 }
1129
1130 // Figure out which warning specifier this is.
Alexander Musman6b080fc2015-05-25 11:21:20 +00001131 bool SpecifierValid;
1132 StringRef Specifier;
1133 llvm::SmallString<1> SpecifierBuf;
1134 if (II) {
1135 Specifier = II->getName();
1136 SpecifierValid = llvm::StringSwitch<bool>(Specifier)
1137 .Cases("default", "disable", "error", "once",
1138 "suppress", true)
1139 .Default(false);
1140 // If we read a correct specifier, snatch next token (that should be
1141 // ":", checked later).
1142 if (SpecifierValid)
1143 PP.Lex(Tok);
1144 } else {
1145 // Token is a numeric constant. It should be either 1, 2, 3 or 4.
1146 uint64_t Value;
1147 Specifier = PP.getSpelling(Tok, SpecifierBuf);
1148 if (PP.parseSimpleIntegerLiteral(Tok, Value)) {
1149 SpecifierValid = (Value >= 1) && (Value <= 4);
1150 } else
1151 SpecifierValid = false;
1152 // Next token already snatched by parseSimpleIntegerLiteral.
1153 }
1154
Reid Kleckner881dff32013-09-13 22:00:30 +00001155 if (!SpecifierValid) {
1156 PP.Diag(Tok, diag::warn_pragma_warning_spec_invalid);
1157 return;
1158 }
Reid Kleckner881dff32013-09-13 22:00:30 +00001159 if (Tok.isNot(tok::colon)) {
1160 PP.Diag(Tok, diag::warn_pragma_warning_expected) << ":";
1161 return;
1162 }
1163
1164 // Collect the warning ids.
1165 SmallVector<int, 4> Ids;
1166 PP.Lex(Tok);
1167 while (Tok.is(tok::numeric_constant)) {
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001168 uint64_t Value;
1169 if (!PP.parseSimpleIntegerLiteral(Tok, Value) || Value == 0 ||
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001170 Value > std::numeric_limits<int>::max()) {
Reid Kleckner881dff32013-09-13 22:00:30 +00001171 PP.Diag(Tok, diag::warn_pragma_warning_expected_number);
1172 return;
1173 }
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001174 Ids.push_back(int(Value));
Reid Kleckner881dff32013-09-13 22:00:30 +00001175 }
1176 if (Callbacks)
1177 Callbacks->PragmaWarning(DiagLoc, Specifier, Ids);
1178
1179 // Parse the next specifier if there is a semicolon.
1180 if (Tok.isNot(tok::semi))
1181 break;
1182 PP.Lex(Tok);
1183 }
1184 }
1185
1186 if (Tok.isNot(tok::r_paren)) {
1187 PP.Diag(Tok, diag::warn_pragma_warning_expected) << ")";
1188 return;
1189 }
1190
1191 PP.Lex(Tok);
1192 if (Tok.isNot(tok::eod))
1193 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma warning";
1194 }
1195};
1196
James Dennett18a6d792012-06-17 03:26:26 +00001197/// PragmaIncludeAliasHandler - "\#pragma include_alias("...")".
Aaron Ballman611306e2012-03-02 22:51:54 +00001198struct PragmaIncludeAliasHandler : public PragmaHandler {
1199 PragmaIncludeAliasHandler() : PragmaHandler("include_alias") {}
Craig Topper9140dd22014-03-11 06:50:42 +00001200 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1201 Token &IncludeAliasTok) override {
Reid Kleckner881dff32013-09-13 22:00:30 +00001202 PP.HandlePragmaIncludeAlias(IncludeAliasTok);
Aaron Ballman611306e2012-03-02 22:51:54 +00001203 }
1204};
1205
Andy Gibbs9c2ccd62013-04-17 16:16:16 +00001206/// PragmaMessageHandler - Handle the microsoft and gcc \#pragma message
1207/// extension. The syntax is:
1208/// \code
1209/// #pragma message(string)
1210/// \endcode
1211/// OR, in GCC mode:
1212/// \code
1213/// #pragma message string
1214/// \endcode
1215/// string is a string, which is fully macro expanded, and permits string
1216/// concatenation, embedded escape characters, etc... See MSDN for more details.
1217/// Also handles \#pragma GCC warning and \#pragma GCC error which take the same
1218/// form as \#pragma message.
Chris Lattner30c924b2010-06-26 17:11:39 +00001219struct PragmaMessageHandler : public PragmaHandler {
Andy Gibbs9c2ccd62013-04-17 16:16:16 +00001220private:
1221 const PPCallbacks::PragmaMessageKind Kind;
1222 const StringRef Namespace;
1223
1224 static const char* PragmaKind(PPCallbacks::PragmaMessageKind Kind,
1225 bool PragmaNameOnly = false) {
1226 switch (Kind) {
1227 case PPCallbacks::PMK_Message:
1228 return PragmaNameOnly ? "message" : "pragma message";
1229 case PPCallbacks::PMK_Warning:
1230 return PragmaNameOnly ? "warning" : "pragma warning";
1231 case PPCallbacks::PMK_Error:
1232 return PragmaNameOnly ? "error" : "pragma error";
1233 }
1234 llvm_unreachable("Unknown PragmaMessageKind!");
1235 }
1236
1237public:
1238 PragmaMessageHandler(PPCallbacks::PragmaMessageKind Kind,
1239 StringRef Namespace = StringRef())
1240 : PragmaHandler(PragmaKind(Kind, true)), Kind(Kind), Namespace(Namespace) {}
1241
Craig Topper9140dd22014-03-11 06:50:42 +00001242 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1243 Token &Tok) override {
Andy Gibbs9c2ccd62013-04-17 16:16:16 +00001244 SourceLocation MessageLoc = Tok.getLocation();
1245 PP.Lex(Tok);
1246 bool ExpectClosingParen = false;
1247 switch (Tok.getKind()) {
1248 case tok::l_paren:
1249 // We have a MSVC style pragma message.
1250 ExpectClosingParen = true;
1251 // Read the string.
1252 PP.Lex(Tok);
1253 break;
1254 case tok::string_literal:
1255 // We have a GCC style pragma message, and we just read the string.
1256 break;
1257 default:
1258 PP.Diag(MessageLoc, diag::err_pragma_message_malformed) << Kind;
1259 return;
1260 }
1261
1262 std::string MessageString;
1263 if (!PP.FinishLexStringLiteral(Tok, MessageString, PragmaKind(Kind),
1264 /*MacroExpansion=*/true))
1265 return;
1266
1267 if (ExpectClosingParen) {
1268 if (Tok.isNot(tok::r_paren)) {
1269 PP.Diag(Tok.getLocation(), diag::err_pragma_message_malformed) << Kind;
1270 return;
1271 }
1272 PP.Lex(Tok); // eat the r_paren.
1273 }
1274
1275 if (Tok.isNot(tok::eod)) {
1276 PP.Diag(Tok.getLocation(), diag::err_pragma_message_malformed) << Kind;
1277 return;
1278 }
1279
1280 // Output the message.
1281 PP.Diag(MessageLoc, (Kind == PPCallbacks::PMK_Error)
1282 ? diag::err_pragma_message
1283 : diag::warn_pragma_message) << MessageString;
1284
1285 // If the pragma is lexically sound, notify any interested PPCallbacks.
1286 if (PPCallbacks *Callbacks = PP.getPPCallbacks())
1287 Callbacks->PragmaMessage(MessageLoc, Namespace, Kind, MessageString);
Chris Lattner30c924b2010-06-26 17:11:39 +00001288 }
1289};
1290
James Dennett18a6d792012-06-17 03:26:26 +00001291/// PragmaPushMacroHandler - "\#pragma push_macro" saves the value of the
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001292/// macro on the top of the stack.
1293struct PragmaPushMacroHandler : public PragmaHandler {
1294 PragmaPushMacroHandler() : PragmaHandler("push_macro") {}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001295
Craig Topper9140dd22014-03-11 06:50:42 +00001296 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1297 Token &PushMacroTok) override {
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001298 PP.HandlePragmaPushMacro(PushMacroTok);
1299 }
1300};
1301
James Dennett18a6d792012-06-17 03:26:26 +00001302/// PragmaPopMacroHandler - "\#pragma pop_macro" sets the value of the
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001303/// macro to the value on the top of the stack.
1304struct PragmaPopMacroHandler : public PragmaHandler {
1305 PragmaPopMacroHandler() : PragmaHandler("pop_macro") {}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001306
Craig Topper9140dd22014-03-11 06:50:42 +00001307 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1308 Token &PopMacroTok) override {
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001309 PP.HandlePragmaPopMacro(PopMacroTok);
1310 }
1311};
1312
Chris Lattner958ee042009-04-19 21:20:35 +00001313// Pragma STDC implementations.
Chris Lattner02ef4e32009-04-19 21:50:08 +00001314
James Dennett18a6d792012-06-17 03:26:26 +00001315/// PragmaSTDC_FENV_ACCESSHandler - "\#pragma STDC FENV_ACCESS ...".
Chris Lattner958ee042009-04-19 21:20:35 +00001316struct PragmaSTDC_FENV_ACCESSHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001317 PragmaSTDC_FENV_ACCESSHandler() : PragmaHandler("FENV_ACCESS") {}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001318
Craig Topper9140dd22014-03-11 06:50:42 +00001319 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1320 Token &Tok) override {
Peter Collingbourne3bffa522011-02-14 01:42:24 +00001321 tok::OnOffSwitch OOS;
1322 if (PP.LexOnOffSwitch(OOS))
1323 return;
1324 if (OOS == tok::OOS_ON)
Chris Lattnerdf222682009-04-19 21:55:32 +00001325 PP.Diag(Tok, diag::warn_stdc_fenv_access_not_supported);
Chris Lattner958ee042009-04-19 21:20:35 +00001326 }
1327};
Mike Stump11289f42009-09-09 15:08:12 +00001328
James Dennett18a6d792012-06-17 03:26:26 +00001329/// PragmaSTDC_CX_LIMITED_RANGEHandler - "\#pragma STDC CX_LIMITED_RANGE ...".
Chris Lattner958ee042009-04-19 21:20:35 +00001330struct PragmaSTDC_CX_LIMITED_RANGEHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001331 PragmaSTDC_CX_LIMITED_RANGEHandler()
1332 : PragmaHandler("CX_LIMITED_RANGE") {}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001333
Craig Topper9140dd22014-03-11 06:50:42 +00001334 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1335 Token &Tok) override {
Peter Collingbourne3bffa522011-02-14 01:42:24 +00001336 tok::OnOffSwitch OOS;
1337 PP.LexOnOffSwitch(OOS);
Chris Lattner958ee042009-04-19 21:20:35 +00001338 }
1339};
Mike Stump11289f42009-09-09 15:08:12 +00001340
James Dennett18a6d792012-06-17 03:26:26 +00001341/// PragmaSTDC_UnknownHandler - "\#pragma STDC ...".
Chris Lattner958ee042009-04-19 21:20:35 +00001342struct PragmaSTDC_UnknownHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001343 PragmaSTDC_UnknownHandler() {}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001344
Craig Topper9140dd22014-03-11 06:50:42 +00001345 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1346 Token &UnknownTok) override {
Chris Lattner02ef4e32009-04-19 21:50:08 +00001347 // C99 6.10.6p2, unknown forms are not allowed.
Chris Lattnera0b1f762009-04-19 21:25:37 +00001348 PP.Diag(UnknownTok, diag::ext_stdc_pragma_ignored);
Chris Lattner958ee042009-04-19 21:20:35 +00001349 }
1350};
Mike Stump11289f42009-09-09 15:08:12 +00001351
John McCall32f5fe12011-09-30 05:12:12 +00001352/// PragmaARCCFCodeAuditedHandler -
James Dennett18a6d792012-06-17 03:26:26 +00001353/// \#pragma clang arc_cf_code_audited begin/end
John McCall32f5fe12011-09-30 05:12:12 +00001354struct PragmaARCCFCodeAuditedHandler : public PragmaHandler {
1355 PragmaARCCFCodeAuditedHandler() : PragmaHandler("arc_cf_code_audited") {}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001356
Craig Topper9140dd22014-03-11 06:50:42 +00001357 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1358 Token &NameTok) override {
John McCall32f5fe12011-09-30 05:12:12 +00001359 SourceLocation Loc = NameTok.getLocation();
1360 bool IsBegin;
1361
1362 Token Tok;
1363
1364 // Lex the 'begin' or 'end'.
1365 PP.LexUnexpandedToken(Tok);
1366 const IdentifierInfo *BeginEnd = Tok.getIdentifierInfo();
1367 if (BeginEnd && BeginEnd->isStr("begin")) {
1368 IsBegin = true;
1369 } else if (BeginEnd && BeginEnd->isStr("end")) {
1370 IsBegin = false;
1371 } else {
1372 PP.Diag(Tok.getLocation(), diag::err_pp_arc_cf_code_audited_syntax);
1373 return;
1374 }
1375
1376 // Verify that this is followed by EOD.
1377 PP.LexUnexpandedToken(Tok);
1378 if (Tok.isNot(tok::eod))
1379 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
1380
1381 // The start location of the active audit.
1382 SourceLocation BeginLoc = PP.getPragmaARCCFCodeAuditedLoc();
1383
1384 // The start location we want after processing this.
1385 SourceLocation NewLoc;
1386
1387 if (IsBegin) {
1388 // Complain about attempts to re-enter an audit.
1389 if (BeginLoc.isValid()) {
1390 PP.Diag(Loc, diag::err_pp_double_begin_of_arc_cf_code_audited);
1391 PP.Diag(BeginLoc, diag::note_pragma_entered_here);
1392 }
1393 NewLoc = Loc;
1394 } else {
1395 // Complain about attempts to leave an audit that doesn't exist.
1396 if (!BeginLoc.isValid()) {
1397 PP.Diag(Loc, diag::err_pp_unmatched_end_of_arc_cf_code_audited);
1398 return;
1399 }
1400 NewLoc = SourceLocation();
1401 }
1402
1403 PP.setPragmaARCCFCodeAuditedLoc(NewLoc);
1404 }
1405};
1406
Douglas Gregor2a20bd12015-06-19 18:25:57 +00001407/// PragmaAssumeNonNullHandler -
1408/// \#pragma clang assume_nonnull begin/end
1409struct PragmaAssumeNonNullHandler : public PragmaHandler {
1410 PragmaAssumeNonNullHandler() : PragmaHandler("assume_nonnull") {}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001411
Douglas Gregor2a20bd12015-06-19 18:25:57 +00001412 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1413 Token &NameTok) override {
1414 SourceLocation Loc = NameTok.getLocation();
1415 bool IsBegin;
1416
1417 Token Tok;
1418
1419 // Lex the 'begin' or 'end'.
1420 PP.LexUnexpandedToken(Tok);
1421 const IdentifierInfo *BeginEnd = Tok.getIdentifierInfo();
1422 if (BeginEnd && BeginEnd->isStr("begin")) {
1423 IsBegin = true;
1424 } else if (BeginEnd && BeginEnd->isStr("end")) {
1425 IsBegin = false;
1426 } else {
1427 PP.Diag(Tok.getLocation(), diag::err_pp_assume_nonnull_syntax);
1428 return;
1429 }
1430
1431 // Verify that this is followed by EOD.
1432 PP.LexUnexpandedToken(Tok);
1433 if (Tok.isNot(tok::eod))
1434 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
1435
1436 // The start location of the active audit.
1437 SourceLocation BeginLoc = PP.getPragmaAssumeNonNullLoc();
1438
1439 // The start location we want after processing this.
1440 SourceLocation NewLoc;
1441
1442 if (IsBegin) {
1443 // Complain about attempts to re-enter an audit.
1444 if (BeginLoc.isValid()) {
1445 PP.Diag(Loc, diag::err_pp_double_begin_of_assume_nonnull);
1446 PP.Diag(BeginLoc, diag::note_pragma_entered_here);
1447 }
1448 NewLoc = Loc;
1449 } else {
1450 // Complain about attempts to leave an audit that doesn't exist.
1451 if (!BeginLoc.isValid()) {
1452 PP.Diag(Loc, diag::err_pp_unmatched_end_of_assume_nonnull);
1453 return;
1454 }
1455 NewLoc = SourceLocation();
1456 }
1457
1458 PP.setPragmaAssumeNonNullLoc(NewLoc);
1459 }
1460};
1461
David Majnemer7aa8c2f2013-06-30 08:18:16 +00001462/// \brief Handle "\#pragma region [...]"
1463///
1464/// The syntax is
1465/// \code
1466/// #pragma region [optional name]
1467/// #pragma endregion [optional comment]
1468/// \endcode
1469///
1470/// \note This is
1471/// <a href="http://msdn.microsoft.com/en-us/library/b6xkz944(v=vs.80).aspx">editor-only</a>
1472/// pragma, just skipped by compiler.
1473struct PragmaRegionHandler : public PragmaHandler {
1474 PragmaRegionHandler(const char *pragma) : PragmaHandler(pragma) { }
Aaron Ballman406ea512012-11-30 19:52:30 +00001475
Craig Topper9140dd22014-03-11 06:50:42 +00001476 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1477 Token &NameTok) override {
David Majnemer7aa8c2f2013-06-30 08:18:16 +00001478 // #pragma region: endregion matches can be verified
1479 // __pragma(region): no sense, but ignored by msvc
1480 // _Pragma is not valid for MSVC, but there isn't any point
1481 // to handle a _Pragma differently.
1482 }
1483};
Aaron Ballman406ea512012-11-30 19:52:30 +00001484
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001485} // end anonymous namespace
Chris Lattnerb694ba72006-07-02 22:41:36 +00001486
1487/// RegisterBuiltinPragmas - Install the standard preprocessor pragmas:
James Dennett18a6d792012-06-17 03:26:26 +00001488/// \#pragma GCC poison/system_header/dependency and \#pragma once.
Chris Lattnerb694ba72006-07-02 22:41:36 +00001489void Preprocessor::RegisterBuiltinPragmas() {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001490 AddPragmaHandler(new PragmaOnceHandler());
1491 AddPragmaHandler(new PragmaMarkHandler());
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001492 AddPragmaHandler(new PragmaPushMacroHandler());
1493 AddPragmaHandler(new PragmaPopMacroHandler());
Andy Gibbs9c2ccd62013-04-17 16:16:16 +00001494 AddPragmaHandler(new PragmaMessageHandler(PPCallbacks::PMK_Message));
Mike Stump11289f42009-09-09 15:08:12 +00001495
Chris Lattnerb61448d2009-05-12 18:21:11 +00001496 // #pragma GCC ...
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001497 AddPragmaHandler("GCC", new PragmaPoisonHandler());
1498 AddPragmaHandler("GCC", new PragmaSystemHeaderHandler());
1499 AddPragmaHandler("GCC", new PragmaDependencyHandler());
Douglas Gregor3bde9b12011-06-22 19:41:48 +00001500 AddPragmaHandler("GCC", new PragmaDiagnosticHandler("GCC"));
Andy Gibbs9c2ccd62013-04-17 16:16:16 +00001501 AddPragmaHandler("GCC", new PragmaMessageHandler(PPCallbacks::PMK_Warning,
1502 "GCC"));
1503 AddPragmaHandler("GCC", new PragmaMessageHandler(PPCallbacks::PMK_Error,
1504 "GCC"));
Chris Lattnerb61448d2009-05-12 18:21:11 +00001505 // #pragma clang ...
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001506 AddPragmaHandler("clang", new PragmaPoisonHandler());
1507 AddPragmaHandler("clang", new PragmaSystemHeaderHandler());
Daniel Dunbarb8068c32010-07-28 15:40:33 +00001508 AddPragmaHandler("clang", new PragmaDebugHandler());
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001509 AddPragmaHandler("clang", new PragmaDependencyHandler());
Douglas Gregor3bde9b12011-06-22 19:41:48 +00001510 AddPragmaHandler("clang", new PragmaDiagnosticHandler("clang"));
John McCall32f5fe12011-09-30 05:12:12 +00001511 AddPragmaHandler("clang", new PragmaARCCFCodeAuditedHandler());
Douglas Gregor2a20bd12015-06-19 18:25:57 +00001512 AddPragmaHandler("clang", new PragmaAssumeNonNullHandler());
Chris Lattnerb61448d2009-05-12 18:21:11 +00001513
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001514 AddPragmaHandler("STDC", new PragmaSTDC_FENV_ACCESSHandler());
1515 AddPragmaHandler("STDC", new PragmaSTDC_CX_LIMITED_RANGEHandler());
Chris Lattner958ee042009-04-19 21:20:35 +00001516 AddPragmaHandler("STDC", new PragmaSTDC_UnknownHandler());
Mike Stump11289f42009-09-09 15:08:12 +00001517
Chris Lattner2ff698d2009-01-16 08:21:25 +00001518 // MS extensions.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001519 if (LangOpts.MicrosoftExt) {
Reid Kleckner881dff32013-09-13 22:00:30 +00001520 AddPragmaHandler(new PragmaWarningHandler());
Aaron Ballman611306e2012-03-02 22:51:54 +00001521 AddPragmaHandler(new PragmaIncludeAliasHandler());
Aaron Ballman406ea512012-11-30 19:52:30 +00001522 AddPragmaHandler(new PragmaRegionHandler("region"));
1523 AddPragmaHandler(new PragmaRegionHandler("endregion"));
Chris Lattner30c924b2010-06-26 17:11:39 +00001524 }
John Brawn8e62db32016-04-04 14:22:58 +00001525
1526 // Pragmas added by plugins
1527 for (PragmaHandlerRegistry::iterator it = PragmaHandlerRegistry::begin(),
1528 ie = PragmaHandlerRegistry::end();
1529 it != ie; ++it) {
1530 AddPragmaHandler(it->instantiate().release());
1531 }
Chris Lattnerb694ba72006-07-02 22:41:36 +00001532}
Lubos Lunak576a0412014-05-01 12:54:03 +00001533
1534/// Ignore all pragmas, useful for modes such as -Eonly which would otherwise
1535/// warn about those pragmas being unknown.
1536void Preprocessor::IgnorePragmas() {
1537 AddPragmaHandler(new EmptyPragmaHandler());
1538 // Also ignore all pragmas in all namespaces created
1539 // in Preprocessor::RegisterBuiltinPragmas().
1540 AddPragmaHandler("GCC", new EmptyPragmaHandler());
1541 AddPragmaHandler("clang", new EmptyPragmaHandler());
1542 if (PragmaHandler *NS = PragmaHandlers->FindHandler("STDC")) {
1543 // Preprocessor::RegisterBuiltinPragmas() already registers
1544 // PragmaSTDC_UnknownHandler as the empty handler, so remove it first,
1545 // otherwise there will be an assert about a duplicate handler.
1546 PragmaNamespace *STDCNamespace = NS->getIfNamespace();
1547 assert(STDCNamespace &&
1548 "Invalid namespace, registered as a regular pragma handler!");
1549 if (PragmaHandler *Existing = STDCNamespace->FindHandler("", false)) {
1550 RemovePragmaHandler("STDC", Existing);
Chandler Carruth4d9c3df2014-05-02 21:44:48 +00001551 delete Existing;
Lubos Lunak576a0412014-05-01 12:54:03 +00001552 }
1553 }
1554 AddPragmaHandler("STDC", new EmptyPragmaHandler());
1555}