blob: 100da514144a6f81ad70d29aa1ca08b852f464f9 [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;
Erik Verbruggene4fd6522016-10-26 13:06:13 +0000284 for (size_t i = 1, e = StrVal.size() - 1; i != e; ++i) {
Reid Kleckner95e036c2013-09-25 16:42:48 +0000285 // 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
Erik Verbruggene0bde752016-10-27 14:17:10 +0000375 // this is a prefix to a TU, which indicates we're generating a PCH file, or
376 // when the main file is a header (e.g. when -xc-header is provided on the
377 // commandline).
378 if (isInPrimaryFile() && TUKind != TU_Prefix && !getLangOpts().IsHeaderFile) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000379 Diag(OnceTok, diag::pp_pragma_once_in_main_file);
380 return;
381 }
Mike Stump11289f42009-09-09 15:08:12 +0000382
Chris Lattnerb694ba72006-07-02 22:41:36 +0000383 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000384 // Mark the file as a once-only file now.
Chris Lattnerd32480d2009-01-17 06:22:33 +0000385 HeaderInfo.MarkFileIncludeOnce(getCurrentFileLexer()->getFileEntry());
Chris Lattnerb694ba72006-07-02 22:41:36 +0000386}
387
Chris Lattnerc2383312007-12-19 19:38:36 +0000388void Preprocessor::HandlePragmaMark() {
Ted Kremenek76c34412008-11-19 22:21:33 +0000389 assert(CurPPLexer && "No current lexer?");
Chris Lattnerd9efb6e2009-06-15 05:02:34 +0000390 if (CurLexer)
391 CurLexer->ReadToEndOfLine();
392 else
393 CurPTHLexer->DiscardToEndOfLine();
Chris Lattnerc2383312007-12-19 19:38:36 +0000394}
395
James Dennett18a6d792012-06-17 03:26:26 +0000396/// HandlePragmaPoison - Handle \#pragma GCC poison. PoisonTok is the 'poison'.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000397///
Erik Verbruggen851ce0e2016-10-26 11:46:10 +0000398void Preprocessor::HandlePragmaPoison() {
Chris Lattner146762e2007-07-20 16:59:19 +0000399 Token Tok;
Chris Lattner538d7f32006-07-20 04:31:52 +0000400
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000401 while (true) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000402 // Read the next token to poison. While doing this, pretend that we are
403 // skipping while reading the identifier to poison.
404 // This avoids errors on code like:
405 // #pragma GCC poison X
406 // #pragma GCC poison X
Ted Kremenek551c82a2008-11-18 01:12:54 +0000407 if (CurPPLexer) CurPPLexer->LexingRawMode = true;
Chris Lattnerb694ba72006-07-02 22:41:36 +0000408 LexUnexpandedToken(Tok);
Ted Kremenek551c82a2008-11-18 01:12:54 +0000409 if (CurPPLexer) CurPPLexer->LexingRawMode = false;
Mike Stump11289f42009-09-09 15:08:12 +0000410
Chris Lattnerb694ba72006-07-02 22:41:36 +0000411 // If we reached the end of line, we're done.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000412 if (Tok.is(tok::eod)) return;
Mike Stump11289f42009-09-09 15:08:12 +0000413
Chris Lattnerb694ba72006-07-02 22:41:36 +0000414 // Can only poison identifiers.
Abramo Bagnaraea4f7c72010-12-22 08:23:18 +0000415 if (Tok.isNot(tok::raw_identifier)) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000416 Diag(Tok, diag::err_pp_invalid_poison);
417 return;
418 }
Mike Stump11289f42009-09-09 15:08:12 +0000419
Chris Lattnercefc7682006-07-08 08:28:12 +0000420 // Look up the identifier info for the token. We disabled identifier lookup
421 // by saying we're skipping contents, so we need to do this manually.
422 IdentifierInfo *II = LookUpIdentifierInfo(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000423
Chris Lattnerb694ba72006-07-02 22:41:36 +0000424 // Already poisoned.
425 if (II->isPoisoned()) continue;
Mike Stump11289f42009-09-09 15:08:12 +0000426
Chris Lattnerb694ba72006-07-02 22:41:36 +0000427 // If this is a macro identifier, emit a warning.
Richard Smith20e883e2015-04-29 23:20:19 +0000428 if (isMacroDefined(II))
Chris Lattnerb694ba72006-07-02 22:41:36 +0000429 Diag(Tok, diag::pp_poisoning_existing_macro);
Mike Stump11289f42009-09-09 15:08:12 +0000430
Chris Lattnerb694ba72006-07-02 22:41:36 +0000431 // Finally, poison it!
432 II->setIsPoisoned();
Douglas Gregor935bc7a22011-10-27 09:33:13 +0000433 if (II->isFromAST())
434 II->setChangedSinceDeserialization();
Chris Lattnerb694ba72006-07-02 22:41:36 +0000435 }
436}
437
James Dennett18a6d792012-06-17 03:26:26 +0000438/// HandlePragmaSystemHeader - Implement \#pragma GCC system_header. We know
Chris Lattnerb694ba72006-07-02 22:41:36 +0000439/// that the whole directive has been parsed.
Chris Lattner146762e2007-07-20 16:59:19 +0000440void Preprocessor::HandlePragmaSystemHeader(Token &SysHeaderTok) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000441 if (isInPrimaryFile()) {
442 Diag(SysHeaderTok, diag::pp_pragma_sysheader_in_main_file);
443 return;
444 }
Mike Stump11289f42009-09-09 15:08:12 +0000445
Chris Lattnerb694ba72006-07-02 22:41:36 +0000446 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
Ted Kremenek300590b2008-11-20 01:45:11 +0000447 PreprocessorLexer *TheLexer = getCurrentFileLexer();
Mike Stump11289f42009-09-09 15:08:12 +0000448
Chris Lattnerb694ba72006-07-02 22:41:36 +0000449 // Mark the file as a system header.
Chris Lattnerd32480d2009-01-17 06:22:33 +0000450 HeaderInfo.MarkFileSystemHeader(TheLexer->getFileEntry());
Mike Stump11289f42009-09-09 15:08:12 +0000451
452
Chris Lattnerd9efb6e2009-06-15 05:02:34 +0000453 PresumedLoc PLoc = SourceMgr.getPresumedLoc(SysHeaderTok.getLocation());
Douglas Gregor453b0122010-11-12 07:15:47 +0000454 if (PLoc.isInvalid())
455 return;
456
Jay Foad9a6b0982011-06-21 15:13:30 +0000457 unsigned FilenameID = SourceMgr.getLineTableFilenameID(PLoc.getFilename());
Mike Stump11289f42009-09-09 15:08:12 +0000458
Chris Lattner3bdc7672011-05-22 22:10:16 +0000459 // Notify the client, if desired, that we are in a new source file.
460 if (Callbacks)
461 Callbacks->FileChanged(SysHeaderTok.getLocation(),
462 PPCallbacks::SystemHeaderPragma, SrcMgr::C_System);
463
Chris Lattnerd9efb6e2009-06-15 05:02:34 +0000464 // Emit a line marker. This will change any source locations from this point
465 // forward to realize they are in a system header.
466 // Create a line note with this information.
Jordan Rose111c4a62013-04-17 19:09:18 +0000467 SourceMgr.AddLineNote(SysHeaderTok.getLocation(), PLoc.getLine()+1,
468 FilenameID, /*IsEntry=*/false, /*IsExit=*/false,
469 /*IsSystem=*/true, /*IsExternC=*/false);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000470}
471
James Dennett18a6d792012-06-17 03:26:26 +0000472/// HandlePragmaDependency - Handle \#pragma GCC dependency "foo" blah.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000473///
Chris Lattner146762e2007-07-20 16:59:19 +0000474void Preprocessor::HandlePragmaDependency(Token &DependencyTok) {
475 Token FilenameTok;
Ted Kremenek551c82a2008-11-18 01:12:54 +0000476 CurPPLexer->LexIncludeFilename(FilenameTok);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000477
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000478 // If the token kind is EOD, the error has already been diagnosed.
479 if (FilenameTok.is(tok::eod))
Chris Lattnerb694ba72006-07-02 22:41:36 +0000480 return;
Mike Stump11289f42009-09-09 15:08:12 +0000481
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000482 // Reserve a buffer to get the spelling.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000483 SmallString<128> FilenameBuffer;
Douglas Gregordc970f02010-03-16 22:30:13 +0000484 bool Invalid = false;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000485 StringRef Filename = getSpelling(FilenameTok, FilenameBuffer, &Invalid);
Douglas Gregordc970f02010-03-16 22:30:13 +0000486 if (Invalid)
487 return;
Mike Stump11289f42009-09-09 15:08:12 +0000488
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000489 bool isAngled =
490 GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000491 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
492 // error.
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000493 if (Filename.empty())
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000494 return;
Mike Stump11289f42009-09-09 15:08:12 +0000495
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000496 // Search include directories for this file.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000497 const DirectoryLookup *CurDir;
Richard Smith25d50752014-10-20 00:15:49 +0000498 const FileEntry *File =
499 LookupFile(FilenameTok.getLocation(), Filename, isAngled, nullptr,
500 nullptr, CurDir, nullptr, nullptr, nullptr);
Craig Topperd2d442c2014-05-17 23:10:59 +0000501 if (!File) {
Eli Friedman3781a362011-08-30 23:07:51 +0000502 if (!SuppressIncludeNotFoundError)
503 Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
Chris Lattner97b8e842008-11-18 08:02:48 +0000504 return;
505 }
Mike Stump11289f42009-09-09 15:08:12 +0000506
Chris Lattnerd32480d2009-01-17 06:22:33 +0000507 const FileEntry *CurFile = getCurrentFileLexer()->getFileEntry();
Chris Lattnerb694ba72006-07-02 22:41:36 +0000508
509 // If this file is older than the file it depends on, emit a diagnostic.
510 if (CurFile && CurFile->getModificationTime() < File->getModificationTime()) {
511 // Lex tokens at the end of the message and include them in the message.
512 std::string Message;
513 Lex(DependencyTok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000514 while (DependencyTok.isNot(tok::eod)) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000515 Message += getSpelling(DependencyTok) + " ";
516 Lex(DependencyTok);
517 }
Mike Stump11289f42009-09-09 15:08:12 +0000518
Chris Lattnerf0b04972010-09-05 23:16:09 +0000519 // Remove the trailing ' ' if present.
520 if (!Message.empty())
521 Message.erase(Message.end()-1);
Chris Lattner97b8e842008-11-18 08:02:48 +0000522 Diag(FilenameTok, diag::pp_out_of_date_dependency) << Message;
Chris Lattnerb694ba72006-07-02 22:41:36 +0000523 }
524}
525
Reid Kleckner002562a2013-05-06 21:02:12 +0000526/// ParsePragmaPushOrPopMacro - Handle parsing of pragma push_macro/pop_macro.
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000527/// Return the IdentifierInfo* associated with the macro to push or pop.
528IdentifierInfo *Preprocessor::ParsePragmaPushOrPopMacro(Token &Tok) {
529 // Remember the pragma token location.
530 Token PragmaTok = Tok;
531
532 // Read the '('.
533 Lex(Tok);
534 if (Tok.isNot(tok::l_paren)) {
535 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
536 << getSpelling(PragmaTok);
Craig Topperd2d442c2014-05-17 23:10:59 +0000537 return nullptr;
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000538 }
539
540 // Read the macro name string.
541 Lex(Tok);
542 if (Tok.isNot(tok::string_literal)) {
543 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
544 << getSpelling(PragmaTok);
Craig Topperd2d442c2014-05-17 23:10:59 +0000545 return nullptr;
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000546 }
547
Richard Smithd67aea22012-03-06 03:21:47 +0000548 if (Tok.hasUDSuffix()) {
549 Diag(Tok, diag::err_invalid_string_udl);
Craig Topperd2d442c2014-05-17 23:10:59 +0000550 return nullptr;
Richard Smithd67aea22012-03-06 03:21:47 +0000551 }
552
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000553 // Remember the macro string.
554 std::string StrVal = getSpelling(Tok);
555
556 // Read the ')'.
557 Lex(Tok);
558 if (Tok.isNot(tok::r_paren)) {
559 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
560 << getSpelling(PragmaTok);
Craig Topperd2d442c2014-05-17 23:10:59 +0000561 return nullptr;
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000562 }
563
564 assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
565 "Invalid string token!");
566
567 // Create a Token from the string.
568 Token MacroTok;
569 MacroTok.startToken();
Abramo Bagnaraea4f7c72010-12-22 08:23:18 +0000570 MacroTok.setKind(tok::raw_identifier);
Dmitri Gribenkob8e9e752012-09-24 21:07:17 +0000571 CreateString(StringRef(&StrVal[1], StrVal.size() - 2), MacroTok);
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000572
573 // Get the IdentifierInfo of MacroToPushTok.
574 return LookUpIdentifierInfo(MacroTok);
575}
576
James Dennett18a6d792012-06-17 03:26:26 +0000577/// \brief Handle \#pragma push_macro.
578///
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000579/// The syntax is:
James Dennett18a6d792012-06-17 03:26:26 +0000580/// \code
Dmitri Gribenko9ebd1612012-11-30 20:04:39 +0000581/// #pragma push_macro("macro")
James Dennett18a6d792012-06-17 03:26:26 +0000582/// \endcode
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000583void Preprocessor::HandlePragmaPushMacro(Token &PushMacroTok) {
584 // Parse the pragma directive and get the macro IdentifierInfo*.
585 IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PushMacroTok);
586 if (!IdentInfo) return;
587
588 // Get the MacroInfo associated with IdentInfo.
589 MacroInfo *MI = getMacroInfo(IdentInfo);
590
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000591 if (MI) {
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000592 // Allow the original MacroInfo to be redefined later.
593 MI->setIsAllowRedefinitionsWithoutWarning(true);
594 }
595
596 // Push the cloned MacroInfo so we can retrieve it later.
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +0000597 PragmaPushMacroInfo[IdentInfo].push_back(MI);
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000598}
599
James Dennett18a6d792012-06-17 03:26:26 +0000600/// \brief Handle \#pragma pop_macro.
601///
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000602/// The syntax is:
James Dennett18a6d792012-06-17 03:26:26 +0000603/// \code
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000604/// #pragma pop_macro("macro")
James Dennett18a6d792012-06-17 03:26:26 +0000605/// \endcode
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000606void Preprocessor::HandlePragmaPopMacro(Token &PopMacroTok) {
607 SourceLocation MessageLoc = PopMacroTok.getLocation();
608
609 // Parse the pragma directive and get the macro IdentifierInfo*.
610 IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PopMacroTok);
611 if (!IdentInfo) return;
612
613 // Find the vector<MacroInfo*> associated with the macro.
614 llvm::DenseMap<IdentifierInfo*, std::vector<MacroInfo*> >::iterator iter =
615 PragmaPushMacroInfo.find(IdentInfo);
616 if (iter != PragmaPushMacroInfo.end()) {
Alexander Kornienko8b3f6232012-08-29 00:20:03 +0000617 // Forget the MacroInfo currently associated with IdentInfo.
Richard Smith20e883e2015-04-29 23:20:19 +0000618 if (MacroInfo *MI = getMacroInfo(IdentInfo)) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +0000619 if (MI->isWarnIfUnused())
620 WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());
621 appendMacroDirective(IdentInfo, AllocateUndefMacroDirective(MessageLoc));
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000622 }
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000623
624 // Get the MacroInfo we want to reinstall.
625 MacroInfo *MacroToReInstall = iter->second.back();
626
Richard Smith713369b2015-04-23 20:40:50 +0000627 if (MacroToReInstall)
Alexander Kornienkoc0b49282012-08-29 16:56:24 +0000628 // Reinstall the previously pushed macro.
Richard Smith713369b2015-04-23 20:40:50 +0000629 appendDefMacroDirective(IdentInfo, MacroToReInstall, MessageLoc);
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000630
631 // Pop PragmaPushMacroInfo stack.
632 iter->second.pop_back();
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000633 if (iter->second.empty())
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000634 PragmaPushMacroInfo.erase(iter);
635 } else {
636 Diag(MessageLoc, diag::warn_pragma_pop_macro_no_push)
637 << IdentInfo->getName();
638 }
639}
Chris Lattnerb694ba72006-07-02 22:41:36 +0000640
Aaron Ballman611306e2012-03-02 22:51:54 +0000641void Preprocessor::HandlePragmaIncludeAlias(Token &Tok) {
642 // We will either get a quoted filename or a bracketed filename, and we
643 // have to track which we got. The first filename is the source name,
644 // and the second name is the mapped filename. If the first is quoted,
645 // the second must be as well (cannot mix and match quotes and brackets).
Aaron Ballman611306e2012-03-02 22:51:54 +0000646
647 // Get the open paren
648 Lex(Tok);
649 if (Tok.isNot(tok::l_paren)) {
650 Diag(Tok, diag::warn_pragma_include_alias_expected) << "(";
651 return;
652 }
653
654 // We expect either a quoted string literal, or a bracketed name
655 Token SourceFilenameTok;
656 CurPPLexer->LexIncludeFilename(SourceFilenameTok);
657 if (SourceFilenameTok.is(tok::eod)) {
658 // The diagnostic has already been handled
659 return;
660 }
661
662 StringRef SourceFileName;
663 SmallString<128> FileNameBuffer;
664 if (SourceFilenameTok.is(tok::string_literal) ||
665 SourceFilenameTok.is(tok::angle_string_literal)) {
666 SourceFileName = getSpelling(SourceFilenameTok, FileNameBuffer);
667 } else if (SourceFilenameTok.is(tok::less)) {
668 // This could be a path instead of just a name
669 FileNameBuffer.push_back('<');
670 SourceLocation End;
671 if (ConcatenateIncludeName(FileNameBuffer, End))
672 return; // Diagnostic already emitted
Yaron Keren92e1b622015-03-18 10:17:07 +0000673 SourceFileName = FileNameBuffer;
Aaron Ballman611306e2012-03-02 22:51:54 +0000674 } else {
675 Diag(Tok, diag::warn_pragma_include_alias_expected_filename);
676 return;
677 }
678 FileNameBuffer.clear();
679
680 // Now we expect a comma, followed by another include name
681 Lex(Tok);
682 if (Tok.isNot(tok::comma)) {
683 Diag(Tok, diag::warn_pragma_include_alias_expected) << ",";
684 return;
685 }
686
687 Token ReplaceFilenameTok;
688 CurPPLexer->LexIncludeFilename(ReplaceFilenameTok);
689 if (ReplaceFilenameTok.is(tok::eod)) {
690 // The diagnostic has already been handled
691 return;
692 }
693
694 StringRef ReplaceFileName;
695 if (ReplaceFilenameTok.is(tok::string_literal) ||
696 ReplaceFilenameTok.is(tok::angle_string_literal)) {
697 ReplaceFileName = getSpelling(ReplaceFilenameTok, FileNameBuffer);
698 } else if (ReplaceFilenameTok.is(tok::less)) {
699 // This could be a path instead of just a name
700 FileNameBuffer.push_back('<');
701 SourceLocation End;
702 if (ConcatenateIncludeName(FileNameBuffer, End))
703 return; // Diagnostic already emitted
Yaron Keren92e1b622015-03-18 10:17:07 +0000704 ReplaceFileName = FileNameBuffer;
Aaron Ballman611306e2012-03-02 22:51:54 +0000705 } else {
706 Diag(Tok, diag::warn_pragma_include_alias_expected_filename);
707 return;
708 }
709
710 // Finally, we expect the closing paren
711 Lex(Tok);
712 if (Tok.isNot(tok::r_paren)) {
713 Diag(Tok, diag::warn_pragma_include_alias_expected) << ")";
714 return;
715 }
716
717 // Now that we have the source and target filenames, we need to make sure
718 // they're both of the same type (angled vs non-angled)
719 StringRef OriginalSource = SourceFileName;
720
721 bool SourceIsAngled =
722 GetIncludeFilenameSpelling(SourceFilenameTok.getLocation(),
723 SourceFileName);
724 bool ReplaceIsAngled =
725 GetIncludeFilenameSpelling(ReplaceFilenameTok.getLocation(),
726 ReplaceFileName);
727 if (!SourceFileName.empty() && !ReplaceFileName.empty() &&
728 (SourceIsAngled != ReplaceIsAngled)) {
729 unsigned int DiagID;
730 if (SourceIsAngled)
731 DiagID = diag::warn_pragma_include_alias_mismatch_angle;
732 else
733 DiagID = diag::warn_pragma_include_alias_mismatch_quote;
734
735 Diag(SourceFilenameTok.getLocation(), DiagID)
736 << SourceFileName
737 << ReplaceFileName;
738
739 return;
740 }
741
742 // Now we can let the include handler know about this mapping
743 getHeaderSearchInfo().AddIncludeAlias(OriginalSource, ReplaceFileName);
744}
745
Chris Lattnerb694ba72006-07-02 22:41:36 +0000746/// AddPragmaHandler - Add the specified pragma handler to the preprocessor.
747/// If 'Namespace' is non-null, then it is a token required to exist on the
748/// pragma line before the pragma string starts, e.g. "STDC" or "GCC".
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000749void Preprocessor::AddPragmaHandler(StringRef Namespace,
Chris Lattnerb694ba72006-07-02 22:41:36 +0000750 PragmaHandler *Handler) {
Craig Topperbe250302014-09-12 05:19:24 +0000751 PragmaNamespace *InsertNS = PragmaHandlers.get();
Mike Stump11289f42009-09-09 15:08:12 +0000752
Chris Lattnerb694ba72006-07-02 22:41:36 +0000753 // If this is specified to be in a namespace, step down into it.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000754 if (!Namespace.empty()) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000755 // If there is already a pragma handler with the name of this namespace,
756 // we either have an error (directive with the same name as a namespace) or
757 // we already have the namespace to insert into.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000758 if (PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace)) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000759 InsertNS = Existing->getIfNamespace();
Craig Topperd2d442c2014-05-17 23:10:59 +0000760 assert(InsertNS != nullptr && "Cannot have a pragma namespace and pragma"
Chris Lattnerb694ba72006-07-02 22:41:36 +0000761 " handler with the same name!");
762 } else {
763 // Otherwise, this namespace doesn't exist yet, create and insert the
764 // handler for it.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000765 InsertNS = new PragmaNamespace(Namespace);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000766 PragmaHandlers->AddPragma(InsertNS);
767 }
768 }
Mike Stump11289f42009-09-09 15:08:12 +0000769
Chris Lattnerb694ba72006-07-02 22:41:36 +0000770 // Check to make sure we don't already have a pragma for this identifier.
771 assert(!InsertNS->FindHandler(Handler->getName()) &&
772 "Pragma handler already exists for this identifier!");
773 InsertNS->AddPragma(Handler);
774}
775
Daniel Dunbar40596532008-10-04 19:17:46 +0000776/// RemovePragmaHandler - Remove the specific pragma handler from the
777/// preprocessor. If \arg Namespace is non-null, then it should be the
778/// namespace that \arg Handler was added to. It is an error to remove
779/// a handler that has not been registered.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000780void Preprocessor::RemovePragmaHandler(StringRef Namespace,
Daniel Dunbar40596532008-10-04 19:17:46 +0000781 PragmaHandler *Handler) {
Craig Topperbe250302014-09-12 05:19:24 +0000782 PragmaNamespace *NS = PragmaHandlers.get();
Mike Stump11289f42009-09-09 15:08:12 +0000783
Daniel Dunbar40596532008-10-04 19:17:46 +0000784 // If this is specified to be in a namespace, step down into it.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000785 if (!Namespace.empty()) {
786 PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace);
Daniel Dunbar40596532008-10-04 19:17:46 +0000787 assert(Existing && "Namespace containing handler does not exist!");
788
789 NS = Existing->getIfNamespace();
790 assert(NS && "Invalid namespace, registered as a regular pragma handler!");
791 }
792
793 NS->RemovePragmaHandler(Handler);
Mike Stump11289f42009-09-09 15:08:12 +0000794
Craig Topperbe250302014-09-12 05:19:24 +0000795 // If this is a non-default namespace and it is now empty, remove it.
796 if (NS != PragmaHandlers.get() && NS->IsEmpty()) {
Daniel Dunbar40596532008-10-04 19:17:46 +0000797 PragmaHandlers->RemovePragmaHandler(NS);
Argyrios Kyrtzidis7ce75262012-01-06 00:22:09 +0000798 delete NS;
799 }
Daniel Dunbar40596532008-10-04 19:17:46 +0000800}
801
Peter Collingbourne3bffa522011-02-14 01:42:24 +0000802bool Preprocessor::LexOnOffSwitch(tok::OnOffSwitch &Result) {
803 Token Tok;
804 LexUnexpandedToken(Tok);
805
806 if (Tok.isNot(tok::identifier)) {
807 Diag(Tok, diag::ext_on_off_switch_syntax);
808 return true;
809 }
810 IdentifierInfo *II = Tok.getIdentifierInfo();
811 if (II->isStr("ON"))
812 Result = tok::OOS_ON;
813 else if (II->isStr("OFF"))
814 Result = tok::OOS_OFF;
815 else if (II->isStr("DEFAULT"))
816 Result = tok::OOS_DEFAULT;
817 else {
818 Diag(Tok, diag::ext_on_off_switch_syntax);
819 return true;
820 }
821
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000822 // Verify that this is followed by EOD.
Peter Collingbourne3bffa522011-02-14 01:42:24 +0000823 LexUnexpandedToken(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000824 if (Tok.isNot(tok::eod))
825 Diag(Tok, diag::ext_pragma_syntax_eod);
Peter Collingbourne3bffa522011-02-14 01:42:24 +0000826 return false;
827}
828
Chris Lattnerb694ba72006-07-02 22:41:36 +0000829namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000830
James Dennett18a6d792012-06-17 03:26:26 +0000831/// PragmaOnceHandler - "\#pragma once" marks the file as atomically included.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000832struct PragmaOnceHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000833 PragmaOnceHandler() : PragmaHandler("once") {}
Craig Topper9140dd22014-03-11 06:50:42 +0000834 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
835 Token &OnceTok) override {
Chris Lattnerce2ab6f2009-04-14 05:07:49 +0000836 PP.CheckEndOfDirective("pragma once");
Chris Lattnerb694ba72006-07-02 22:41:36 +0000837 PP.HandlePragmaOnce(OnceTok);
838 }
839};
840
James Dennett18a6d792012-06-17 03:26:26 +0000841/// PragmaMarkHandler - "\#pragma mark ..." is ignored by the compiler, and the
Chris Lattnerc2383312007-12-19 19:38:36 +0000842/// rest of the line is not lexed.
843struct PragmaMarkHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000844 PragmaMarkHandler() : PragmaHandler("mark") {}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000845
Craig Topper9140dd22014-03-11 06:50:42 +0000846 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
847 Token &MarkTok) override {
Chris Lattnerc2383312007-12-19 19:38:36 +0000848 PP.HandlePragmaMark();
849 }
850};
851
James Dennett18a6d792012-06-17 03:26:26 +0000852/// PragmaPoisonHandler - "\#pragma poison x" marks x as not usable.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000853struct PragmaPoisonHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000854 PragmaPoisonHandler() : PragmaHandler("poison") {}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000855
Craig Topper9140dd22014-03-11 06:50:42 +0000856 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
857 Token &PoisonTok) override {
Erik Verbruggen851ce0e2016-10-26 11:46:10 +0000858 PP.HandlePragmaPoison();
Chris Lattnerb694ba72006-07-02 22:41:36 +0000859 }
860};
861
James Dennett18a6d792012-06-17 03:26:26 +0000862/// PragmaSystemHeaderHandler - "\#pragma system_header" marks the current file
Chris Lattnerc2383312007-12-19 19:38:36 +0000863/// as a system header, which silences warnings in it.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000864struct PragmaSystemHeaderHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000865 PragmaSystemHeaderHandler() : PragmaHandler("system_header") {}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000866
Craig Topper9140dd22014-03-11 06:50:42 +0000867 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
868 Token &SHToken) override {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000869 PP.HandlePragmaSystemHeader(SHToken);
Chris Lattnerce2ab6f2009-04-14 05:07:49 +0000870 PP.CheckEndOfDirective("pragma");
Chris Lattnerb694ba72006-07-02 22:41:36 +0000871 }
872};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000873
Chris Lattnerb694ba72006-07-02 22:41:36 +0000874struct PragmaDependencyHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000875 PragmaDependencyHandler() : PragmaHandler("dependency") {}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000876
Craig Topper9140dd22014-03-11 06:50:42 +0000877 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
878 Token &DepToken) override {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000879 PP.HandlePragmaDependency(DepToken);
880 }
881};
Mike Stump11289f42009-09-09 15:08:12 +0000882
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000883struct PragmaDebugHandler : public PragmaHandler {
884 PragmaDebugHandler() : PragmaHandler("__debug") {}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000885
Craig Topper9140dd22014-03-11 06:50:42 +0000886 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
887 Token &DepToken) override {
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000888 Token Tok;
889 PP.LexUnexpandedToken(Tok);
890 if (Tok.isNot(tok::identifier)) {
Douglas Gregor3cc26482010-08-30 15:15:34 +0000891 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000892 return;
893 }
894 IdentifierInfo *II = Tok.getIdentifierInfo();
895
Daniel Dunbarf2cf3292010-08-17 22:32:48 +0000896 if (II->isStr("assert")) {
David Blaikie83d382b2011-09-23 05:06:16 +0000897 llvm_unreachable("This is an assertion!");
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000898 } else if (II->isStr("crash")) {
David Blaikie5bd4c2a2012-08-21 18:56:49 +0000899 LLVM_BUILTIN_TRAP;
David Blaikie5d577a22012-06-29 22:03:56 +0000900 } else if (II->isStr("parser_crash")) {
901 Token Crasher;
Benjamin Kramer3162f292015-03-08 19:28:24 +0000902 Crasher.startToken();
David Blaikie5d577a22012-06-29 22:03:56 +0000903 Crasher.setKind(tok::annot_pragma_parser_crash);
Benjamin Kramer3162f292015-03-08 19:28:24 +0000904 Crasher.setAnnotationRange(SourceRange(Tok.getLocation()));
David Blaikie5d577a22012-06-29 22:03:56 +0000905 PP.EnterToken(Crasher);
Richard Smithba3a4f92016-01-12 21:59:26 +0000906 } else if (II->isStr("dump")) {
907 Token Identifier;
908 PP.LexUnexpandedToken(Identifier);
909 if (auto *DumpII = Identifier.getIdentifierInfo()) {
910 Token DumpAnnot;
911 DumpAnnot.startToken();
912 DumpAnnot.setKind(tok::annot_pragma_dump);
913 DumpAnnot.setAnnotationRange(
914 SourceRange(Tok.getLocation(), Identifier.getLocation()));
915 DumpAnnot.setAnnotationValue(DumpII);
916 PP.DiscardUntilEndOfDirective();
917 PP.EnterToken(DumpAnnot);
918 } else {
919 PP.Diag(Identifier, diag::warn_pragma_debug_missing_argument)
920 << II->getName();
921 }
Daniel Dunbarf2cf3292010-08-17 22:32:48 +0000922 } else if (II->isStr("llvm_fatal_error")) {
923 llvm::report_fatal_error("#pragma clang __debug llvm_fatal_error");
924 } else if (II->isStr("llvm_unreachable")) {
925 llvm_unreachable("#pragma clang __debug llvm_unreachable");
Richard Smith3ffa61d2015-04-30 23:10:40 +0000926 } else if (II->isStr("macro")) {
927 Token MacroName;
928 PP.LexUnexpandedToken(MacroName);
929 auto *MacroII = MacroName.getIdentifierInfo();
930 if (MacroII)
931 PP.dumpMacroInfo(MacroII);
932 else
Richard Smithba3a4f92016-01-12 21:59:26 +0000933 PP.Diag(MacroName, diag::warn_pragma_debug_missing_argument)
934 << II->getName();
Daniel Dunbarf2cf3292010-08-17 22:32:48 +0000935 } else if (II->isStr("overflow_stack")) {
936 DebugOverflowStack();
Daniel Dunbar211a7872010-08-18 23:09:23 +0000937 } else if (II->isStr("handle_crash")) {
938 llvm::CrashRecoveryContext *CRC =llvm::CrashRecoveryContext::GetCurrent();
939 if (CRC)
940 CRC->HandleCrash();
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000941 } else if (II->isStr("captured")) {
942 HandleCaptured(PP);
Daniel Dunbarf2cf3292010-08-17 22:32:48 +0000943 } else {
944 PP.Diag(Tok, diag::warn_pragma_debug_unexpected_command)
945 << II->getName();
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000946 }
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000947
948 PPCallbacks *Callbacks = PP.getPPCallbacks();
949 if (Callbacks)
950 Callbacks->PragmaDebug(Tok.getLocation(), II->getName());
951 }
952
953 void HandleCaptured(Preprocessor &PP) {
954 // Skip if emitting preprocessed output.
955 if (PP.isPreprocessedOutput())
956 return;
957
958 Token Tok;
959 PP.LexUnexpandedToken(Tok);
960
961 if (Tok.isNot(tok::eod)) {
962 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol)
963 << "pragma clang __debug captured";
964 return;
965 }
966
967 SourceLocation NameLoc = Tok.getLocation();
David Blaikie2eabcc92016-02-09 18:52:09 +0000968 MutableArrayRef<Token> Toks(
969 PP.getPreprocessorAllocator().Allocate<Token>(1), 1);
970 Toks[0].startToken();
971 Toks[0].setKind(tok::annot_pragma_captured);
972 Toks[0].setLocation(NameLoc);
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000973
David Blaikie2eabcc92016-02-09 18:52:09 +0000974 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000975 }
976
Francois Pichet2e11f5d2011-05-25 16:15:03 +0000977// Disable MSVC warning about runtime stack overflow.
978#ifdef _MSC_VER
979 #pragma warning(disable : 4717)
980#endif
Richard Trieu0732beb2013-12-21 01:04:02 +0000981 static void DebugOverflowStack() {
982 void (*volatile Self)() = DebugOverflowStack;
983 Self();
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000984 }
Francois Pichet2e11f5d2011-05-25 16:15:03 +0000985#ifdef _MSC_VER
986 #pragma warning(default : 4717)
987#endif
988
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000989};
990
James Dennett18a6d792012-06-17 03:26:26 +0000991/// PragmaDiagnosticHandler - e.g. '\#pragma GCC diagnostic ignored "-Wformat"'
Chris Lattner504af112009-04-19 23:16:58 +0000992struct PragmaDiagnosticHandler : public PragmaHandler {
Douglas Gregor3bde9b12011-06-22 19:41:48 +0000993private:
994 const char *Namespace;
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000995
Chris Lattnerfb42a182009-07-12 21:18:45 +0000996public:
Douglas Gregor3bde9b12011-06-22 19:41:48 +0000997 explicit PragmaDiagnosticHandler(const char *NS) :
998 PragmaHandler("diagnostic"), Namespace(NS) {}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000999
Craig Topper9140dd22014-03-11 06:50:42 +00001000 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1001 Token &DiagToken) override {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +00001002 SourceLocation DiagLoc = DiagToken.getLocation();
Chris Lattner504af112009-04-19 23:16:58 +00001003 Token Tok;
1004 PP.LexUnexpandedToken(Tok);
1005 if (Tok.isNot(tok::identifier)) {
Douglas Gregor3cc26482010-08-30 15:15:34 +00001006 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Chris Lattner504af112009-04-19 23:16:58 +00001007 return;
1008 }
1009 IdentifierInfo *II = Tok.getIdentifierInfo();
Douglas Gregor3bde9b12011-06-22 19:41:48 +00001010 PPCallbacks *Callbacks = PP.getPPCallbacks();
Mike Stump11289f42009-09-09 15:08:12 +00001011
Alp Toker46df1c02014-06-12 10:15:20 +00001012 if (II->isStr("pop")) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +00001013 if (!PP.getDiagnostics().popMappings(DiagLoc))
Douglas Gregor3cc26482010-08-30 15:15:34 +00001014 PP.Diag(Tok, diag::warn_pragma_diagnostic_cannot_pop);
Douglas Gregor3bde9b12011-06-22 19:41:48 +00001015 else if (Callbacks)
1016 Callbacks->PragmaDiagnosticPop(DiagLoc, Namespace);
Douglas Gregor3cc26482010-08-30 15:15:34 +00001017 return;
1018 } else if (II->isStr("push")) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +00001019 PP.getDiagnostics().pushMappings(DiagLoc);
Douglas Gregor3bde9b12011-06-22 19:41:48 +00001020 if (Callbacks)
1021 Callbacks->PragmaDiagnosticPush(DiagLoc, Namespace);
Chris Lattnerfb42a182009-07-12 21:18:45 +00001022 return;
Alp Toker46df1c02014-06-12 10:15:20 +00001023 }
1024
1025 diag::Severity SV = llvm::StringSwitch<diag::Severity>(II->getName())
1026 .Case("ignored", diag::Severity::Ignored)
1027 .Case("warning", diag::Severity::Warning)
1028 .Case("error", diag::Severity::Error)
1029 .Case("fatal", diag::Severity::Fatal)
1030 .Default(diag::Severity());
1031
1032 if (SV == diag::Severity()) {
Douglas Gregor3cc26482010-08-30 15:15:34 +00001033 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Chris Lattner504af112009-04-19 23:16:58 +00001034 return;
1035 }
Mike Stump11289f42009-09-09 15:08:12 +00001036
Chris Lattner504af112009-04-19 23:16:58 +00001037 PP.LexUnexpandedToken(Tok);
Andy Gibbs58905d22012-11-17 19:15:38 +00001038 SourceLocation StringLoc = Tok.getLocation();
Chris Lattner504af112009-04-19 23:16:58 +00001039
Andy Gibbs58905d22012-11-17 19:15:38 +00001040 std::string WarningName;
Andy Gibbsa8df57a2012-11-17 19:16:52 +00001041 if (!PP.FinishLexStringLiteral(Tok, WarningName, "pragma diagnostic",
1042 /*MacroExpansion=*/false))
Chris Lattner504af112009-04-19 23:16:58 +00001043 return;
Mike Stump11289f42009-09-09 15:08:12 +00001044
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001045 if (Tok.isNot(tok::eod)) {
Chris Lattner504af112009-04-19 23:16:58 +00001046 PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token);
1047 return;
1048 }
Mike Stump11289f42009-09-09 15:08:12 +00001049
Chris Lattner504af112009-04-19 23:16:58 +00001050 if (WarningName.size() < 3 || WarningName[0] != '-' ||
Richard Smith3be1cb22014-08-07 00:24:21 +00001051 (WarningName[1] != 'W' && WarningName[1] != 'R')) {
Andy Gibbs58905d22012-11-17 19:15:38 +00001052 PP.Diag(StringLoc, diag::warn_pragma_diagnostic_invalid_option);
Chris Lattner504af112009-04-19 23:16:58 +00001053 return;
1054 }
Mike Stump11289f42009-09-09 15:08:12 +00001055
Sunil Srivastava5239de72016-02-13 01:44:05 +00001056 diag::Flavor Flavor = WarningName[1] == 'W' ? diag::Flavor::WarningOrError
1057 : diag::Flavor::Remark;
Benjamin Kramer2193e232016-02-13 13:42:41 +00001058 StringRef Group = StringRef(WarningName).substr(2);
Sunil Srivastava5239de72016-02-13 01:44:05 +00001059 bool unknownDiag = false;
1060 if (Group == "everything") {
1061 // Special handling for pragma clang diagnostic ... "-Weverything".
1062 // There is no formal group named "everything", so there has to be a
1063 // special case for it.
1064 PP.getDiagnostics().setSeverityForAll(Flavor, SV, DiagLoc);
1065 } else
1066 unknownDiag = PP.getDiagnostics().setSeverityForGroup(Flavor, Group, SV,
1067 DiagLoc);
1068 if (unknownDiag)
Andy Gibbs58905d22012-11-17 19:15:38 +00001069 PP.Diag(StringLoc, diag::warn_pragma_diagnostic_unknown_warning)
1070 << WarningName;
Douglas Gregor3bde9b12011-06-22 19:41:48 +00001071 else if (Callbacks)
Alp Toker46df1c02014-06-12 10:15:20 +00001072 Callbacks->PragmaDiagnostic(DiagLoc, Namespace, SV, WarningName);
Chris Lattner504af112009-04-19 23:16:58 +00001073 }
1074};
Mike Stump11289f42009-09-09 15:08:12 +00001075
Reid Kleckner881dff32013-09-13 22:00:30 +00001076/// "\#pragma warning(...)". MSVC's diagnostics do not map cleanly to clang's
1077/// diagnostics, so we don't really implement this pragma. We parse it and
1078/// ignore it to avoid -Wunknown-pragma warnings.
1079struct PragmaWarningHandler : public PragmaHandler {
1080 PragmaWarningHandler() : PragmaHandler("warning") {}
1081
Craig Topper9140dd22014-03-11 06:50:42 +00001082 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1083 Token &Tok) override {
Reid Kleckner881dff32013-09-13 22:00:30 +00001084 // Parse things like:
1085 // warning(push, 1)
1086 // warning(pop)
John Thompson4762b232013-11-16 00:16:03 +00001087 // warning(disable : 1 2 3 ; error : 4 5 6 ; suppress : 7 8 9)
Reid Kleckner881dff32013-09-13 22:00:30 +00001088 SourceLocation DiagLoc = Tok.getLocation();
1089 PPCallbacks *Callbacks = PP.getPPCallbacks();
1090
1091 PP.Lex(Tok);
1092 if (Tok.isNot(tok::l_paren)) {
1093 PP.Diag(Tok, diag::warn_pragma_warning_expected) << "(";
1094 return;
1095 }
1096
1097 PP.Lex(Tok);
1098 IdentifierInfo *II = Tok.getIdentifierInfo();
Reid Kleckner881dff32013-09-13 22:00:30 +00001099
Alexander Musman6b080fc2015-05-25 11:21:20 +00001100 if (II && II->isStr("push")) {
Reid Kleckner881dff32013-09-13 22:00:30 +00001101 // #pragma warning( push[ ,n ] )
Reid Kleckner4d185102013-10-02 15:19:23 +00001102 int Level = -1;
Reid Kleckner881dff32013-09-13 22:00:30 +00001103 PP.Lex(Tok);
1104 if (Tok.is(tok::comma)) {
1105 PP.Lex(Tok);
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001106 uint64_t Value;
1107 if (Tok.is(tok::numeric_constant) &&
1108 PP.parseSimpleIntegerLiteral(Tok, Value))
1109 Level = int(Value);
Reid Kleckner4d185102013-10-02 15:19:23 +00001110 if (Level < 0 || Level > 4) {
Reid Kleckner881dff32013-09-13 22:00:30 +00001111 PP.Diag(Tok, diag::warn_pragma_warning_push_level);
1112 return;
1113 }
1114 }
1115 if (Callbacks)
1116 Callbacks->PragmaWarningPush(DiagLoc, Level);
Alexander Musman6b080fc2015-05-25 11:21:20 +00001117 } else if (II && II->isStr("pop")) {
Reid Kleckner881dff32013-09-13 22:00:30 +00001118 // #pragma warning( pop )
1119 PP.Lex(Tok);
1120 if (Callbacks)
1121 Callbacks->PragmaWarningPop(DiagLoc);
1122 } else {
1123 // #pragma warning( warning-specifier : warning-number-list
1124 // [; warning-specifier : warning-number-list...] )
1125 while (true) {
1126 II = Tok.getIdentifierInfo();
Alexander Musman6b080fc2015-05-25 11:21:20 +00001127 if (!II && !Tok.is(tok::numeric_constant)) {
Reid Kleckner881dff32013-09-13 22:00:30 +00001128 PP.Diag(Tok, diag::warn_pragma_warning_spec_invalid);
1129 return;
1130 }
1131
1132 // Figure out which warning specifier this is.
Alexander Musman6b080fc2015-05-25 11:21:20 +00001133 bool SpecifierValid;
1134 StringRef Specifier;
1135 llvm::SmallString<1> SpecifierBuf;
1136 if (II) {
1137 Specifier = II->getName();
1138 SpecifierValid = llvm::StringSwitch<bool>(Specifier)
1139 .Cases("default", "disable", "error", "once",
1140 "suppress", true)
1141 .Default(false);
1142 // If we read a correct specifier, snatch next token (that should be
1143 // ":", checked later).
1144 if (SpecifierValid)
1145 PP.Lex(Tok);
1146 } else {
1147 // Token is a numeric constant. It should be either 1, 2, 3 or 4.
1148 uint64_t Value;
1149 Specifier = PP.getSpelling(Tok, SpecifierBuf);
1150 if (PP.parseSimpleIntegerLiteral(Tok, Value)) {
1151 SpecifierValid = (Value >= 1) && (Value <= 4);
1152 } else
1153 SpecifierValid = false;
1154 // Next token already snatched by parseSimpleIntegerLiteral.
1155 }
1156
Reid Kleckner881dff32013-09-13 22:00:30 +00001157 if (!SpecifierValid) {
1158 PP.Diag(Tok, diag::warn_pragma_warning_spec_invalid);
1159 return;
1160 }
Reid Kleckner881dff32013-09-13 22:00:30 +00001161 if (Tok.isNot(tok::colon)) {
1162 PP.Diag(Tok, diag::warn_pragma_warning_expected) << ":";
1163 return;
1164 }
1165
1166 // Collect the warning ids.
1167 SmallVector<int, 4> Ids;
1168 PP.Lex(Tok);
1169 while (Tok.is(tok::numeric_constant)) {
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001170 uint64_t Value;
1171 if (!PP.parseSimpleIntegerLiteral(Tok, Value) || Value == 0 ||
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001172 Value > std::numeric_limits<int>::max()) {
Reid Kleckner881dff32013-09-13 22:00:30 +00001173 PP.Diag(Tok, diag::warn_pragma_warning_expected_number);
1174 return;
1175 }
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001176 Ids.push_back(int(Value));
Reid Kleckner881dff32013-09-13 22:00:30 +00001177 }
1178 if (Callbacks)
1179 Callbacks->PragmaWarning(DiagLoc, Specifier, Ids);
1180
1181 // Parse the next specifier if there is a semicolon.
1182 if (Tok.isNot(tok::semi))
1183 break;
1184 PP.Lex(Tok);
1185 }
1186 }
1187
1188 if (Tok.isNot(tok::r_paren)) {
1189 PP.Diag(Tok, diag::warn_pragma_warning_expected) << ")";
1190 return;
1191 }
1192
1193 PP.Lex(Tok);
1194 if (Tok.isNot(tok::eod))
1195 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma warning";
1196 }
1197};
1198
James Dennett18a6d792012-06-17 03:26:26 +00001199/// PragmaIncludeAliasHandler - "\#pragma include_alias("...")".
Aaron Ballman611306e2012-03-02 22:51:54 +00001200struct PragmaIncludeAliasHandler : public PragmaHandler {
1201 PragmaIncludeAliasHandler() : PragmaHandler("include_alias") {}
Craig Topper9140dd22014-03-11 06:50:42 +00001202 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1203 Token &IncludeAliasTok) override {
Reid Kleckner881dff32013-09-13 22:00:30 +00001204 PP.HandlePragmaIncludeAlias(IncludeAliasTok);
Aaron Ballman611306e2012-03-02 22:51:54 +00001205 }
1206};
1207
Andy Gibbs9c2ccd62013-04-17 16:16:16 +00001208/// PragmaMessageHandler - Handle the microsoft and gcc \#pragma message
1209/// extension. The syntax is:
1210/// \code
1211/// #pragma message(string)
1212/// \endcode
1213/// OR, in GCC mode:
1214/// \code
1215/// #pragma message string
1216/// \endcode
1217/// string is a string, which is fully macro expanded, and permits string
1218/// concatenation, embedded escape characters, etc... See MSDN for more details.
1219/// Also handles \#pragma GCC warning and \#pragma GCC error which take the same
1220/// form as \#pragma message.
Chris Lattner30c924b2010-06-26 17:11:39 +00001221struct PragmaMessageHandler : public PragmaHandler {
Andy Gibbs9c2ccd62013-04-17 16:16:16 +00001222private:
1223 const PPCallbacks::PragmaMessageKind Kind;
1224 const StringRef Namespace;
1225
1226 static const char* PragmaKind(PPCallbacks::PragmaMessageKind Kind,
1227 bool PragmaNameOnly = false) {
1228 switch (Kind) {
1229 case PPCallbacks::PMK_Message:
1230 return PragmaNameOnly ? "message" : "pragma message";
1231 case PPCallbacks::PMK_Warning:
1232 return PragmaNameOnly ? "warning" : "pragma warning";
1233 case PPCallbacks::PMK_Error:
1234 return PragmaNameOnly ? "error" : "pragma error";
1235 }
1236 llvm_unreachable("Unknown PragmaMessageKind!");
1237 }
1238
1239public:
1240 PragmaMessageHandler(PPCallbacks::PragmaMessageKind Kind,
1241 StringRef Namespace = StringRef())
1242 : PragmaHandler(PragmaKind(Kind, true)), Kind(Kind), Namespace(Namespace) {}
1243
Craig Topper9140dd22014-03-11 06:50:42 +00001244 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1245 Token &Tok) override {
Andy Gibbs9c2ccd62013-04-17 16:16:16 +00001246 SourceLocation MessageLoc = Tok.getLocation();
1247 PP.Lex(Tok);
1248 bool ExpectClosingParen = false;
1249 switch (Tok.getKind()) {
1250 case tok::l_paren:
1251 // We have a MSVC style pragma message.
1252 ExpectClosingParen = true;
1253 // Read the string.
1254 PP.Lex(Tok);
1255 break;
1256 case tok::string_literal:
1257 // We have a GCC style pragma message, and we just read the string.
1258 break;
1259 default:
1260 PP.Diag(MessageLoc, diag::err_pragma_message_malformed) << Kind;
1261 return;
1262 }
1263
1264 std::string MessageString;
1265 if (!PP.FinishLexStringLiteral(Tok, MessageString, PragmaKind(Kind),
1266 /*MacroExpansion=*/true))
1267 return;
1268
1269 if (ExpectClosingParen) {
1270 if (Tok.isNot(tok::r_paren)) {
1271 PP.Diag(Tok.getLocation(), diag::err_pragma_message_malformed) << Kind;
1272 return;
1273 }
1274 PP.Lex(Tok); // eat the r_paren.
1275 }
1276
1277 if (Tok.isNot(tok::eod)) {
1278 PP.Diag(Tok.getLocation(), diag::err_pragma_message_malformed) << Kind;
1279 return;
1280 }
1281
1282 // Output the message.
1283 PP.Diag(MessageLoc, (Kind == PPCallbacks::PMK_Error)
1284 ? diag::err_pragma_message
1285 : diag::warn_pragma_message) << MessageString;
1286
1287 // If the pragma is lexically sound, notify any interested PPCallbacks.
1288 if (PPCallbacks *Callbacks = PP.getPPCallbacks())
1289 Callbacks->PragmaMessage(MessageLoc, Namespace, Kind, MessageString);
Chris Lattner30c924b2010-06-26 17:11:39 +00001290 }
1291};
1292
James Dennett18a6d792012-06-17 03:26:26 +00001293/// PragmaPushMacroHandler - "\#pragma push_macro" saves the value of the
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001294/// macro on the top of the stack.
1295struct PragmaPushMacroHandler : public PragmaHandler {
1296 PragmaPushMacroHandler() : PragmaHandler("push_macro") {}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001297
Craig Topper9140dd22014-03-11 06:50:42 +00001298 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1299 Token &PushMacroTok) override {
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001300 PP.HandlePragmaPushMacro(PushMacroTok);
1301 }
1302};
1303
James Dennett18a6d792012-06-17 03:26:26 +00001304/// PragmaPopMacroHandler - "\#pragma pop_macro" sets the value of the
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001305/// macro to the value on the top of the stack.
1306struct PragmaPopMacroHandler : public PragmaHandler {
1307 PragmaPopMacroHandler() : PragmaHandler("pop_macro") {}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001308
Craig Topper9140dd22014-03-11 06:50:42 +00001309 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1310 Token &PopMacroTok) override {
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001311 PP.HandlePragmaPopMacro(PopMacroTok);
1312 }
1313};
1314
Chris Lattner958ee042009-04-19 21:20:35 +00001315// Pragma STDC implementations.
Chris Lattner02ef4e32009-04-19 21:50:08 +00001316
James Dennett18a6d792012-06-17 03:26:26 +00001317/// PragmaSTDC_FENV_ACCESSHandler - "\#pragma STDC FENV_ACCESS ...".
Chris Lattner958ee042009-04-19 21:20:35 +00001318struct PragmaSTDC_FENV_ACCESSHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001319 PragmaSTDC_FENV_ACCESSHandler() : PragmaHandler("FENV_ACCESS") {}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001320
Craig Topper9140dd22014-03-11 06:50:42 +00001321 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1322 Token &Tok) override {
Peter Collingbourne3bffa522011-02-14 01:42:24 +00001323 tok::OnOffSwitch OOS;
1324 if (PP.LexOnOffSwitch(OOS))
1325 return;
1326 if (OOS == tok::OOS_ON)
Chris Lattnerdf222682009-04-19 21:55:32 +00001327 PP.Diag(Tok, diag::warn_stdc_fenv_access_not_supported);
Chris Lattner958ee042009-04-19 21:20:35 +00001328 }
1329};
Mike Stump11289f42009-09-09 15:08:12 +00001330
James Dennett18a6d792012-06-17 03:26:26 +00001331/// PragmaSTDC_CX_LIMITED_RANGEHandler - "\#pragma STDC CX_LIMITED_RANGE ...".
Chris Lattner958ee042009-04-19 21:20:35 +00001332struct PragmaSTDC_CX_LIMITED_RANGEHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001333 PragmaSTDC_CX_LIMITED_RANGEHandler()
1334 : PragmaHandler("CX_LIMITED_RANGE") {}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001335
Craig Topper9140dd22014-03-11 06:50:42 +00001336 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1337 Token &Tok) override {
Peter Collingbourne3bffa522011-02-14 01:42:24 +00001338 tok::OnOffSwitch OOS;
1339 PP.LexOnOffSwitch(OOS);
Chris Lattner958ee042009-04-19 21:20:35 +00001340 }
1341};
Mike Stump11289f42009-09-09 15:08:12 +00001342
James Dennett18a6d792012-06-17 03:26:26 +00001343/// PragmaSTDC_UnknownHandler - "\#pragma STDC ...".
Chris Lattner958ee042009-04-19 21:20:35 +00001344struct PragmaSTDC_UnknownHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001345 PragmaSTDC_UnknownHandler() {}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001346
Craig Topper9140dd22014-03-11 06:50:42 +00001347 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1348 Token &UnknownTok) override {
Chris Lattner02ef4e32009-04-19 21:50:08 +00001349 // C99 6.10.6p2, unknown forms are not allowed.
Chris Lattnera0b1f762009-04-19 21:25:37 +00001350 PP.Diag(UnknownTok, diag::ext_stdc_pragma_ignored);
Chris Lattner958ee042009-04-19 21:20:35 +00001351 }
1352};
Mike Stump11289f42009-09-09 15:08:12 +00001353
John McCall32f5fe12011-09-30 05:12:12 +00001354/// PragmaARCCFCodeAuditedHandler -
James Dennett18a6d792012-06-17 03:26:26 +00001355/// \#pragma clang arc_cf_code_audited begin/end
John McCall32f5fe12011-09-30 05:12:12 +00001356struct PragmaARCCFCodeAuditedHandler : public PragmaHandler {
1357 PragmaARCCFCodeAuditedHandler() : PragmaHandler("arc_cf_code_audited") {}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001358
Craig Topper9140dd22014-03-11 06:50:42 +00001359 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1360 Token &NameTok) override {
John McCall32f5fe12011-09-30 05:12:12 +00001361 SourceLocation Loc = NameTok.getLocation();
1362 bool IsBegin;
1363
1364 Token Tok;
1365
1366 // Lex the 'begin' or 'end'.
1367 PP.LexUnexpandedToken(Tok);
1368 const IdentifierInfo *BeginEnd = Tok.getIdentifierInfo();
1369 if (BeginEnd && BeginEnd->isStr("begin")) {
1370 IsBegin = true;
1371 } else if (BeginEnd && BeginEnd->isStr("end")) {
1372 IsBegin = false;
1373 } else {
1374 PP.Diag(Tok.getLocation(), diag::err_pp_arc_cf_code_audited_syntax);
1375 return;
1376 }
1377
1378 // Verify that this is followed by EOD.
1379 PP.LexUnexpandedToken(Tok);
1380 if (Tok.isNot(tok::eod))
1381 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
1382
1383 // The start location of the active audit.
1384 SourceLocation BeginLoc = PP.getPragmaARCCFCodeAuditedLoc();
1385
1386 // The start location we want after processing this.
1387 SourceLocation NewLoc;
1388
1389 if (IsBegin) {
1390 // Complain about attempts to re-enter an audit.
1391 if (BeginLoc.isValid()) {
1392 PP.Diag(Loc, diag::err_pp_double_begin_of_arc_cf_code_audited);
1393 PP.Diag(BeginLoc, diag::note_pragma_entered_here);
1394 }
1395 NewLoc = Loc;
1396 } else {
1397 // Complain about attempts to leave an audit that doesn't exist.
1398 if (!BeginLoc.isValid()) {
1399 PP.Diag(Loc, diag::err_pp_unmatched_end_of_arc_cf_code_audited);
1400 return;
1401 }
1402 NewLoc = SourceLocation();
1403 }
1404
1405 PP.setPragmaARCCFCodeAuditedLoc(NewLoc);
1406 }
1407};
1408
Douglas Gregor2a20bd12015-06-19 18:25:57 +00001409/// PragmaAssumeNonNullHandler -
1410/// \#pragma clang assume_nonnull begin/end
1411struct PragmaAssumeNonNullHandler : public PragmaHandler {
1412 PragmaAssumeNonNullHandler() : PragmaHandler("assume_nonnull") {}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001413
Douglas Gregor2a20bd12015-06-19 18:25:57 +00001414 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1415 Token &NameTok) override {
1416 SourceLocation Loc = NameTok.getLocation();
1417 bool IsBegin;
1418
1419 Token Tok;
1420
1421 // Lex the 'begin' or 'end'.
1422 PP.LexUnexpandedToken(Tok);
1423 const IdentifierInfo *BeginEnd = Tok.getIdentifierInfo();
1424 if (BeginEnd && BeginEnd->isStr("begin")) {
1425 IsBegin = true;
1426 } else if (BeginEnd && BeginEnd->isStr("end")) {
1427 IsBegin = false;
1428 } else {
1429 PP.Diag(Tok.getLocation(), diag::err_pp_assume_nonnull_syntax);
1430 return;
1431 }
1432
1433 // Verify that this is followed by EOD.
1434 PP.LexUnexpandedToken(Tok);
1435 if (Tok.isNot(tok::eod))
1436 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
1437
1438 // The start location of the active audit.
1439 SourceLocation BeginLoc = PP.getPragmaAssumeNonNullLoc();
1440
1441 // The start location we want after processing this.
1442 SourceLocation NewLoc;
1443
1444 if (IsBegin) {
1445 // Complain about attempts to re-enter an audit.
1446 if (BeginLoc.isValid()) {
1447 PP.Diag(Loc, diag::err_pp_double_begin_of_assume_nonnull);
1448 PP.Diag(BeginLoc, diag::note_pragma_entered_here);
1449 }
1450 NewLoc = Loc;
1451 } else {
1452 // Complain about attempts to leave an audit that doesn't exist.
1453 if (!BeginLoc.isValid()) {
1454 PP.Diag(Loc, diag::err_pp_unmatched_end_of_assume_nonnull);
1455 return;
1456 }
1457 NewLoc = SourceLocation();
1458 }
1459
1460 PP.setPragmaAssumeNonNullLoc(NewLoc);
1461 }
1462};
1463
David Majnemer7aa8c2f2013-06-30 08:18:16 +00001464/// \brief Handle "\#pragma region [...]"
1465///
1466/// The syntax is
1467/// \code
1468/// #pragma region [optional name]
1469/// #pragma endregion [optional comment]
1470/// \endcode
1471///
1472/// \note This is
1473/// <a href="http://msdn.microsoft.com/en-us/library/b6xkz944(v=vs.80).aspx">editor-only</a>
1474/// pragma, just skipped by compiler.
1475struct PragmaRegionHandler : public PragmaHandler {
1476 PragmaRegionHandler(const char *pragma) : PragmaHandler(pragma) { }
Aaron Ballman406ea512012-11-30 19:52:30 +00001477
Craig Topper9140dd22014-03-11 06:50:42 +00001478 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1479 Token &NameTok) override {
David Majnemer7aa8c2f2013-06-30 08:18:16 +00001480 // #pragma region: endregion matches can be verified
1481 // __pragma(region): no sense, but ignored by msvc
1482 // _Pragma is not valid for MSVC, but there isn't any point
1483 // to handle a _Pragma differently.
1484 }
1485};
Aaron Ballman406ea512012-11-30 19:52:30 +00001486
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001487} // end anonymous namespace
Chris Lattnerb694ba72006-07-02 22:41:36 +00001488
1489/// RegisterBuiltinPragmas - Install the standard preprocessor pragmas:
James Dennett18a6d792012-06-17 03:26:26 +00001490/// \#pragma GCC poison/system_header/dependency and \#pragma once.
Chris Lattnerb694ba72006-07-02 22:41:36 +00001491void Preprocessor::RegisterBuiltinPragmas() {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001492 AddPragmaHandler(new PragmaOnceHandler());
1493 AddPragmaHandler(new PragmaMarkHandler());
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001494 AddPragmaHandler(new PragmaPushMacroHandler());
1495 AddPragmaHandler(new PragmaPopMacroHandler());
Andy Gibbs9c2ccd62013-04-17 16:16:16 +00001496 AddPragmaHandler(new PragmaMessageHandler(PPCallbacks::PMK_Message));
Mike Stump11289f42009-09-09 15:08:12 +00001497
Chris Lattnerb61448d2009-05-12 18:21:11 +00001498 // #pragma GCC ...
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001499 AddPragmaHandler("GCC", new PragmaPoisonHandler());
1500 AddPragmaHandler("GCC", new PragmaSystemHeaderHandler());
1501 AddPragmaHandler("GCC", new PragmaDependencyHandler());
Douglas Gregor3bde9b12011-06-22 19:41:48 +00001502 AddPragmaHandler("GCC", new PragmaDiagnosticHandler("GCC"));
Andy Gibbs9c2ccd62013-04-17 16:16:16 +00001503 AddPragmaHandler("GCC", new PragmaMessageHandler(PPCallbacks::PMK_Warning,
1504 "GCC"));
1505 AddPragmaHandler("GCC", new PragmaMessageHandler(PPCallbacks::PMK_Error,
1506 "GCC"));
Chris Lattnerb61448d2009-05-12 18:21:11 +00001507 // #pragma clang ...
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001508 AddPragmaHandler("clang", new PragmaPoisonHandler());
1509 AddPragmaHandler("clang", new PragmaSystemHeaderHandler());
Daniel Dunbarb8068c32010-07-28 15:40:33 +00001510 AddPragmaHandler("clang", new PragmaDebugHandler());
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001511 AddPragmaHandler("clang", new PragmaDependencyHandler());
Douglas Gregor3bde9b12011-06-22 19:41:48 +00001512 AddPragmaHandler("clang", new PragmaDiagnosticHandler("clang"));
John McCall32f5fe12011-09-30 05:12:12 +00001513 AddPragmaHandler("clang", new PragmaARCCFCodeAuditedHandler());
Douglas Gregor2a20bd12015-06-19 18:25:57 +00001514 AddPragmaHandler("clang", new PragmaAssumeNonNullHandler());
Chris Lattnerb61448d2009-05-12 18:21:11 +00001515
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001516 AddPragmaHandler("STDC", new PragmaSTDC_FENV_ACCESSHandler());
1517 AddPragmaHandler("STDC", new PragmaSTDC_CX_LIMITED_RANGEHandler());
Chris Lattner958ee042009-04-19 21:20:35 +00001518 AddPragmaHandler("STDC", new PragmaSTDC_UnknownHandler());
Mike Stump11289f42009-09-09 15:08:12 +00001519
Chris Lattner2ff698d2009-01-16 08:21:25 +00001520 // MS extensions.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001521 if (LangOpts.MicrosoftExt) {
Reid Kleckner881dff32013-09-13 22:00:30 +00001522 AddPragmaHandler(new PragmaWarningHandler());
Aaron Ballman611306e2012-03-02 22:51:54 +00001523 AddPragmaHandler(new PragmaIncludeAliasHandler());
Aaron Ballman406ea512012-11-30 19:52:30 +00001524 AddPragmaHandler(new PragmaRegionHandler("region"));
1525 AddPragmaHandler(new PragmaRegionHandler("endregion"));
Chris Lattner30c924b2010-06-26 17:11:39 +00001526 }
John Brawn8e62db32016-04-04 14:22:58 +00001527
1528 // Pragmas added by plugins
1529 for (PragmaHandlerRegistry::iterator it = PragmaHandlerRegistry::begin(),
1530 ie = PragmaHandlerRegistry::end();
1531 it != ie; ++it) {
1532 AddPragmaHandler(it->instantiate().release());
1533 }
Chris Lattnerb694ba72006-07-02 22:41:36 +00001534}
Lubos Lunak576a0412014-05-01 12:54:03 +00001535
1536/// Ignore all pragmas, useful for modes such as -Eonly which would otherwise
1537/// warn about those pragmas being unknown.
1538void Preprocessor::IgnorePragmas() {
1539 AddPragmaHandler(new EmptyPragmaHandler());
1540 // Also ignore all pragmas in all namespaces created
1541 // in Preprocessor::RegisterBuiltinPragmas().
1542 AddPragmaHandler("GCC", new EmptyPragmaHandler());
1543 AddPragmaHandler("clang", new EmptyPragmaHandler());
1544 if (PragmaHandler *NS = PragmaHandlers->FindHandler("STDC")) {
1545 // Preprocessor::RegisterBuiltinPragmas() already registers
1546 // PragmaSTDC_UnknownHandler as the empty handler, so remove it first,
1547 // otherwise there will be an assert about a duplicate handler.
1548 PragmaNamespace *STDCNamespace = NS->getIfNamespace();
1549 assert(STDCNamespace &&
1550 "Invalid namespace, registered as a regular pragma handler!");
1551 if (PragmaHandler *Existing = STDCNamespace->FindHandler("", false)) {
1552 RemovePragmaHandler("STDC", Existing);
Chandler Carruth4d9c3df2014-05-02 21:44:48 +00001553 delete Existing;
Lubos Lunak576a0412014-05-01 12:54:03 +00001554 }
1555 }
1556 AddPragmaHandler("STDC", new EmptyPragmaHandler());
1557}