blob: 324bbd29a2bd420eb534214003b1d4b5ea5eff09 [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"
17#include "clang/Basic/SourceManager.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000018#include "clang/Lex/HeaderSearch.h"
19#include "clang/Lex/LexDiagnostic.h"
20#include "clang/Lex/LiteralSupport.h"
21#include "clang/Lex/MacroInfo.h"
22#include "clang/Lex/Preprocessor.h"
Daniel Dunbar211a7872010-08-18 23:09:23 +000023#include "llvm/Support/CrashRecoveryContext.h"
Daniel Dunbarf2cf3292010-08-17 22:32:48 +000024#include "llvm/Support/ErrorHandling.h"
Douglas Gregorc6d5edd2009-07-02 17:08:52 +000025#include <algorithm>
Chris Lattnerb8761832006-06-24 21:31:03 +000026using namespace clang;
27
28// Out-of-line destructor to provide a home for the class.
29PragmaHandler::~PragmaHandler() {
30}
31
Chris Lattner2e155302006-07-03 05:34:41 +000032//===----------------------------------------------------------------------===//
Daniel Dunbard839e772010-06-11 20:10:12 +000033// EmptyPragmaHandler Implementation.
34//===----------------------------------------------------------------------===//
35
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000036EmptyPragmaHandler::EmptyPragmaHandler() {}
Daniel Dunbard839e772010-06-11 20:10:12 +000037
Douglas Gregorc7d65762010-09-09 22:45:38 +000038void EmptyPragmaHandler::HandlePragma(Preprocessor &PP,
39 PragmaIntroducerKind Introducer,
40 Token &FirstToken) {}
Daniel Dunbard839e772010-06-11 20:10:12 +000041
42//===----------------------------------------------------------------------===//
Chris Lattner2e155302006-07-03 05:34:41 +000043// PragmaNamespace Implementation.
44//===----------------------------------------------------------------------===//
45
Chris Lattner2e155302006-07-03 05:34:41 +000046PragmaNamespace::~PragmaNamespace() {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000047 for (llvm::StringMap<PragmaHandler*>::iterator
48 I = Handlers.begin(), E = Handlers.end(); I != E; ++I)
49 delete I->second;
Chris Lattner2e155302006-07-03 05:34:41 +000050}
51
52/// FindHandler - Check to see if there is already a handler for the
53/// specified name. If not, return the handler for the null identifier if it
54/// exists, otherwise return null. If IgnoreNull is true (the default) then
55/// the null handler isn't returned on failure to match.
Chris Lattner0e62c1c2011-07-23 10:55:15 +000056PragmaHandler *PragmaNamespace::FindHandler(StringRef Name,
Chris Lattner2e155302006-07-03 05:34:41 +000057 bool IgnoreNull) const {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000058 if (PragmaHandler *Handler = Handlers.lookup(Name))
59 return Handler;
Chris Lattner0e62c1c2011-07-23 10:55:15 +000060 return IgnoreNull ? 0 : Handlers.lookup(StringRef());
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000061}
Mike Stump11289f42009-09-09 15:08:12 +000062
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000063void PragmaNamespace::AddPragma(PragmaHandler *Handler) {
64 assert(!Handlers.lookup(Handler->getName()) &&
65 "A handler with this name is already registered in this namespace");
66 llvm::StringMapEntry<PragmaHandler *> &Entry =
67 Handlers.GetOrCreateValue(Handler->getName());
68 Entry.setValue(Handler);
Chris Lattner2e155302006-07-03 05:34:41 +000069}
70
Daniel Dunbar40596532008-10-04 19:17:46 +000071void PragmaNamespace::RemovePragmaHandler(PragmaHandler *Handler) {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000072 assert(Handlers.lookup(Handler->getName()) &&
73 "Handler not registered in this namespace");
74 Handlers.erase(Handler->getName());
Daniel Dunbar40596532008-10-04 19:17:46 +000075}
76
Douglas Gregorc7d65762010-09-09 22:45:38 +000077void PragmaNamespace::HandlePragma(Preprocessor &PP,
78 PragmaIntroducerKind Introducer,
79 Token &Tok) {
Chris Lattnerb8761832006-06-24 21:31:03 +000080 // Read the 'namespace' that the directive is in, e.g. STDC. Do not macro
81 // expand it, the user can have a STDC #define, that should not affect this.
82 PP.LexUnexpandedToken(Tok);
Mike Stump11289f42009-09-09 15:08:12 +000083
Chris Lattnerb8761832006-06-24 21:31:03 +000084 // Get the handler for this token. If there is no handler, ignore the pragma.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000085 PragmaHandler *Handler
86 = FindHandler(Tok.getIdentifierInfo() ? Tok.getIdentifierInfo()->getName()
Chris Lattner0e62c1c2011-07-23 10:55:15 +000087 : StringRef(),
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000088 /*IgnoreNull=*/false);
Chris Lattner21656f22009-04-19 21:10:26 +000089 if (Handler == 0) {
90 PP.Diag(Tok, diag::warn_pragma_ignored);
91 return;
92 }
Mike Stump11289f42009-09-09 15:08:12 +000093
Chris Lattnerb8761832006-06-24 21:31:03 +000094 // Otherwise, pass it down.
Douglas Gregorc7d65762010-09-09 22:45:38 +000095 Handler->HandlePragma(PP, Introducer, Tok);
Chris Lattnerb8761832006-06-24 21:31:03 +000096}
Chris Lattnerb694ba72006-07-02 22:41:36 +000097
Chris Lattnerb694ba72006-07-02 22:41:36 +000098//===----------------------------------------------------------------------===//
99// Preprocessor Pragma Directive Handling.
100//===----------------------------------------------------------------------===//
101
James Dennett18a6d792012-06-17 03:26:26 +0000102/// HandlePragmaDirective - The "\#pragma" directive has been parsed. Lex the
Chris Lattnerb694ba72006-07-02 22:41:36 +0000103/// rest of the pragma, passing it to the registered pragma handlers.
Enea Zaffanella5afb04a2013-07-20 20:09:11 +0000104void Preprocessor::HandlePragmaDirective(SourceLocation IntroducerLoc,
105 PragmaIntroducerKind Introducer) {
106 if (Callbacks)
107 Callbacks->PragmaDirective(IntroducerLoc, Introducer);
108
Jordan Rosede1a2922012-06-08 18:06:21 +0000109 if (!PragmasEnabled)
110 return;
111
Chris Lattnerb694ba72006-07-02 22:41:36 +0000112 ++NumPragma;
Mike Stump11289f42009-09-09 15:08:12 +0000113
Chris Lattnerb694ba72006-07-02 22:41:36 +0000114 // Invoke the first level of pragma handlers which reads the namespace id.
Chris Lattner146762e2007-07-20 16:59:19 +0000115 Token Tok;
Enea Zaffanella5afb04a2013-07-20 20:09:11 +0000116 PragmaHandlers->HandlePragma(*this, Introducer, Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000117
Chris Lattnerb694ba72006-07-02 22:41:36 +0000118 // If the pragma handler didn't read the rest of the line, consume it now.
Peter Collingbourne2c9f9662011-02-22 13:49:00 +0000119 if ((CurTokenLexer && CurTokenLexer->isParsingPreprocessorDirective())
120 || (CurPPLexer && CurPPLexer->ParsingPreprocessorDirective))
Chris Lattnerb694ba72006-07-02 22:41:36 +0000121 DiscardUntilEndOfDirective();
122}
123
Argyrios Kyrtzidisf1b64c62012-04-03 16:47:40 +0000124namespace {
125/// \brief Helper class for \see Preprocessor::Handle_Pragma.
126class LexingFor_PragmaRAII {
127 Preprocessor &PP;
128 bool InMacroArgPreExpansion;
129 bool Failed;
130 Token &OutTok;
131 Token PragmaTok;
132
133public:
134 LexingFor_PragmaRAII(Preprocessor &PP, bool InMacroArgPreExpansion,
135 Token &Tok)
136 : PP(PP), InMacroArgPreExpansion(InMacroArgPreExpansion),
137 Failed(false), OutTok(Tok) {
138 if (InMacroArgPreExpansion) {
139 PragmaTok = OutTok;
140 PP.EnableBacktrackAtThisPos();
141 }
142 }
143
144 ~LexingFor_PragmaRAII() {
145 if (InMacroArgPreExpansion) {
146 if (Failed) {
147 PP.CommitBacktrackedTokens();
148 } else {
149 PP.Backtrack();
150 OutTok = PragmaTok;
151 }
152 }
153 }
154
155 void failed() {
156 Failed = true;
157 }
158};
159}
160
Chris Lattnerb694ba72006-07-02 22:41:36 +0000161/// Handle_Pragma - Read a _Pragma directive, slice it up, process it, then
162/// return the first token after the directive. The _Pragma token has just
163/// been read into 'Tok'.
Chris Lattner146762e2007-07-20 16:59:19 +0000164void Preprocessor::Handle_Pragma(Token &Tok) {
Argyrios Kyrtzidisf1b64c62012-04-03 16:47:40 +0000165
166 // This works differently if we are pre-expanding a macro argument.
167 // In that case we don't actually "activate" the pragma now, we only lex it
168 // until we are sure it is lexically correct and then we backtrack so that
169 // we activate the pragma whenever we encounter the tokens again in the token
170 // stream. This ensures that we will activate it in the correct location
171 // or that we will ignore it if it never enters the token stream, e.g:
172 //
173 // #define EMPTY(x)
174 // #define INACTIVE(x) EMPTY(x)
175 // INACTIVE(_Pragma("clang diagnostic ignored \"-Wconversion\""))
176
177 LexingFor_PragmaRAII _PragmaLexing(*this, InMacroArgPreExpansion, Tok);
178
Chris Lattnerb694ba72006-07-02 22:41:36 +0000179 // Remember the pragma token location.
180 SourceLocation PragmaLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000181
Chris Lattnerb694ba72006-07-02 22:41:36 +0000182 // Read the '('.
183 Lex(Tok);
Chris Lattner907dfe92008-11-18 07:59:24 +0000184 if (Tok.isNot(tok::l_paren)) {
185 Diag(PragmaLoc, diag::err__Pragma_malformed);
Argyrios Kyrtzidisf1b64c62012-04-03 16:47:40 +0000186 return _PragmaLexing.failed();
Chris Lattner907dfe92008-11-18 07:59:24 +0000187 }
Chris Lattnerb694ba72006-07-02 22:41:36 +0000188
189 // Read the '"..."'.
190 Lex(Tok);
Richard Smithc98bb4e2013-03-09 23:30:15 +0000191 if (!tok::isStringLiteral(Tok.getKind())) {
Chris Lattner907dfe92008-11-18 07:59:24 +0000192 Diag(PragmaLoc, diag::err__Pragma_malformed);
Richard Smithd67aea22012-03-06 03:21:47 +0000193 // Skip this token, and the ')', if present.
194 if (Tok.isNot(tok::r_paren))
195 Lex(Tok);
196 if (Tok.is(tok::r_paren))
197 Lex(Tok);
Argyrios Kyrtzidisf1b64c62012-04-03 16:47:40 +0000198 return _PragmaLexing.failed();
Richard Smithd67aea22012-03-06 03:21:47 +0000199 }
200
201 if (Tok.hasUDSuffix()) {
202 Diag(Tok, diag::err_invalid_string_udl);
203 // Skip this token, and the ')', if present.
204 Lex(Tok);
205 if (Tok.is(tok::r_paren))
206 Lex(Tok);
Argyrios Kyrtzidisf1b64c62012-04-03 16:47:40 +0000207 return _PragmaLexing.failed();
Chris Lattner907dfe92008-11-18 07:59:24 +0000208 }
Mike Stump11289f42009-09-09 15:08:12 +0000209
Chris Lattnerb694ba72006-07-02 22:41:36 +0000210 // Remember the string.
Argyrios Kyrtzidisf1b64c62012-04-03 16:47:40 +0000211 Token StrTok = Tok;
Chris Lattnerb694ba72006-07-02 22:41:36 +0000212
213 // Read the ')'.
214 Lex(Tok);
Chris Lattner907dfe92008-11-18 07:59:24 +0000215 if (Tok.isNot(tok::r_paren)) {
216 Diag(PragmaLoc, diag::err__Pragma_malformed);
Argyrios Kyrtzidisf1b64c62012-04-03 16:47:40 +0000217 return _PragmaLexing.failed();
Chris Lattner907dfe92008-11-18 07:59:24 +0000218 }
Mike Stump11289f42009-09-09 15:08:12 +0000219
Argyrios Kyrtzidisf1b64c62012-04-03 16:47:40 +0000220 if (InMacroArgPreExpansion)
221 return;
222
Chris Lattner9dc9c202009-02-15 20:52:18 +0000223 SourceLocation RParenLoc = Tok.getLocation();
Argyrios Kyrtzidisf1b64c62012-04-03 16:47:40 +0000224 std::string StrVal = getSpelling(StrTok);
Mike Stump11289f42009-09-09 15:08:12 +0000225
Richard Smithc98bb4e2013-03-09 23:30:15 +0000226 // The _Pragma is lexically sound. Destringize according to C11 6.10.9.1:
227 // "The string literal is destringized by deleting any encoding prefix,
Chris Lattner262d4e32009-01-16 18:59:23 +0000228 // deleting the leading and trailing double-quotes, replacing each escape
229 // sequence \" by a double-quote, and replacing each escape sequence \\ by a
230 // single backslash."
Richard Smithc98bb4e2013-03-09 23:30:15 +0000231 if (StrVal[0] == 'L' || StrVal[0] == 'U' ||
232 (StrVal[0] == 'u' && StrVal[1] != '8'))
Chris Lattnerb694ba72006-07-02 22:41:36 +0000233 StrVal.erase(StrVal.begin());
Richard Smithc98bb4e2013-03-09 23:30:15 +0000234 else if (StrVal[0] == 'u')
235 StrVal.erase(StrVal.begin(), StrVal.begin() + 2);
236
237 if (StrVal[0] == 'R') {
238 // FIXME: C++11 does not specify how to handle raw-string-literals here.
239 // We strip off the 'R', the quotes, the d-char-sequences, and the parens.
240 assert(StrVal[1] == '"' && StrVal[StrVal.size() - 1] == '"' &&
241 "Invalid raw string token!");
242
243 // Measure the length of the d-char-sequence.
244 unsigned NumDChars = 0;
245 while (StrVal[2 + NumDChars] != '(') {
246 assert(NumDChars < (StrVal.size() - 5) / 2 &&
247 "Invalid raw string token!");
248 ++NumDChars;
249 }
250 assert(StrVal[StrVal.size() - 2 - NumDChars] == ')');
251
252 // Remove 'R " d-char-sequence' and 'd-char-sequence "'. We'll replace the
253 // parens below.
254 StrVal.erase(0, 2 + NumDChars);
255 StrVal.erase(StrVal.size() - 1 - NumDChars);
256 } else {
257 assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
258 "Invalid string token!");
259
260 // Remove escaped quotes and escapes.
Benjamin Kramerc2f5f292013-05-04 10:37:20 +0000261 unsigned ResultPos = 1;
262 for (unsigned i = 1, e = StrVal.size() - 2; i != e; ++i) {
263 if (StrVal[i] != '\\' ||
264 (StrVal[i + 1] != '\\' && StrVal[i + 1] != '"')) {
Richard Smithc98bb4e2013-03-09 23:30:15 +0000265 // \\ -> '\' and \" -> '"'.
Benjamin Kramerc2f5f292013-05-04 10:37:20 +0000266 StrVal[ResultPos++] = StrVal[i];
Richard Smithc98bb4e2013-03-09 23:30:15 +0000267 }
268 }
Benjamin Kramerc2f5f292013-05-04 10:37:20 +0000269 StrVal.erase(StrVal.begin() + ResultPos, StrVal.end() - 2);
Richard Smithc98bb4e2013-03-09 23:30:15 +0000270 }
Mike Stump11289f42009-09-09 15:08:12 +0000271
Chris Lattnerb694ba72006-07-02 22:41:36 +0000272 // Remove the front quote, replacing it with a space, so that the pragma
273 // contents appear to have a space before them.
274 StrVal[0] = ' ';
Mike Stump11289f42009-09-09 15:08:12 +0000275
Chris Lattnerfa217bd2009-03-08 08:08:45 +0000276 // Replace the terminating quote with a \n.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000277 StrVal[StrVal.size()-1] = '\n';
Mike Stump11289f42009-09-09 15:08:12 +0000278
Peter Collingbournef29ce972011-02-22 13:49:06 +0000279 // Plop the string (including the newline and trailing null) into a buffer
280 // where we can lex it.
281 Token TmpTok;
282 TmpTok.startToken();
Dmitri Gribenkob8e9e752012-09-24 21:07:17 +0000283 CreateString(StrVal, TmpTok);
Peter Collingbournef29ce972011-02-22 13:49:06 +0000284 SourceLocation TokLoc = TmpTok.getLocation();
285
286 // Make and enter a lexer object so that we lex and expand the tokens just
287 // like any others.
288 Lexer *TL = Lexer::Create_PragmaLexer(TokLoc, PragmaLoc, RParenLoc,
289 StrVal.size(), *this);
290
291 EnterSourceFileWithLexer(TL, 0);
292
293 // With everything set up, lex this as a #pragma directive.
Enea Zaffanella5afb04a2013-07-20 20:09:11 +0000294 HandlePragmaDirective(PragmaLoc, PIK__Pragma);
John McCall89e925d2010-08-28 22:34:47 +0000295
296 // Finally, return whatever came after the pragma directive.
297 return Lex(Tok);
298}
299
300/// HandleMicrosoft__pragma - Like Handle_Pragma except the pragma text
301/// is not enclosed within a string literal.
302void Preprocessor::HandleMicrosoft__pragma(Token &Tok) {
303 // Remember the pragma token location.
304 SourceLocation PragmaLoc = Tok.getLocation();
305
306 // Read the '('.
307 Lex(Tok);
308 if (Tok.isNot(tok::l_paren)) {
309 Diag(PragmaLoc, diag::err__Pragma_malformed);
310 return;
311 }
312
Peter Collingbournef29ce972011-02-22 13:49:06 +0000313 // Get the tokens enclosed within the __pragma(), as well as the final ')'.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000314 SmallVector<Token, 32> PragmaToks;
John McCall89e925d2010-08-28 22:34:47 +0000315 int NumParens = 0;
316 Lex(Tok);
317 while (Tok.isNot(tok::eof)) {
Peter Collingbournef29ce972011-02-22 13:49:06 +0000318 PragmaToks.push_back(Tok);
John McCall89e925d2010-08-28 22:34:47 +0000319 if (Tok.is(tok::l_paren))
320 NumParens++;
321 else if (Tok.is(tok::r_paren) && NumParens-- == 0)
322 break;
John McCall89e925d2010-08-28 22:34:47 +0000323 Lex(Tok);
324 }
325
John McCall49039d42010-08-29 01:09:54 +0000326 if (Tok.is(tok::eof)) {
327 Diag(PragmaLoc, diag::err_unterminated___pragma);
328 return;
329 }
330
Peter Collingbournef29ce972011-02-22 13:49:06 +0000331 PragmaToks.front().setFlag(Token::LeadingSpace);
John McCall89e925d2010-08-28 22:34:47 +0000332
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000333 // Replace the ')' with an EOD to mark the end of the pragma.
334 PragmaToks.back().setKind(tok::eod);
Peter Collingbournef29ce972011-02-22 13:49:06 +0000335
336 Token *TokArray = new Token[PragmaToks.size()];
337 std::copy(PragmaToks.begin(), PragmaToks.end(), TokArray);
338
339 // Push the tokens onto the stack.
340 EnterTokenStream(TokArray, PragmaToks.size(), true, true);
341
342 // With everything set up, lex this as a #pragma directive.
Enea Zaffanella5afb04a2013-07-20 20:09:11 +0000343 HandlePragmaDirective(PragmaLoc, PIK___pragma);
John McCall89e925d2010-08-28 22:34:47 +0000344
345 // Finally, return whatever came after the pragma directive.
346 return Lex(Tok);
347}
348
James Dennett18a6d792012-06-17 03:26:26 +0000349/// HandlePragmaOnce - Handle \#pragma once. OnceTok is the 'once'.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000350///
Chris Lattner146762e2007-07-20 16:59:19 +0000351void Preprocessor::HandlePragmaOnce(Token &OnceTok) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000352 if (isInPrimaryFile()) {
353 Diag(OnceTok, diag::pp_pragma_once_in_main_file);
354 return;
355 }
Mike Stump11289f42009-09-09 15:08:12 +0000356
Chris Lattnerb694ba72006-07-02 22:41:36 +0000357 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000358 // Mark the file as a once-only file now.
Chris Lattnerd32480d2009-01-17 06:22:33 +0000359 HeaderInfo.MarkFileIncludeOnce(getCurrentFileLexer()->getFileEntry());
Chris Lattnerb694ba72006-07-02 22:41:36 +0000360}
361
Chris Lattnerc2383312007-12-19 19:38:36 +0000362void Preprocessor::HandlePragmaMark() {
Ted Kremenek76c34412008-11-19 22:21:33 +0000363 assert(CurPPLexer && "No current lexer?");
Chris Lattnerd9efb6e2009-06-15 05:02:34 +0000364 if (CurLexer)
365 CurLexer->ReadToEndOfLine();
366 else
367 CurPTHLexer->DiscardToEndOfLine();
Chris Lattnerc2383312007-12-19 19:38:36 +0000368}
369
370
James Dennett18a6d792012-06-17 03:26:26 +0000371/// HandlePragmaPoison - Handle \#pragma GCC poison. PoisonTok is the 'poison'.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000372///
Chris Lattner146762e2007-07-20 16:59:19 +0000373void Preprocessor::HandlePragmaPoison(Token &PoisonTok) {
374 Token Tok;
Chris Lattner538d7f32006-07-20 04:31:52 +0000375
Chris Lattnerb694ba72006-07-02 22:41:36 +0000376 while (1) {
377 // Read the next token to poison. While doing this, pretend that we are
378 // skipping while reading the identifier to poison.
379 // This avoids errors on code like:
380 // #pragma GCC poison X
381 // #pragma GCC poison X
Ted Kremenek551c82a2008-11-18 01:12:54 +0000382 if (CurPPLexer) CurPPLexer->LexingRawMode = true;
Chris Lattnerb694ba72006-07-02 22:41:36 +0000383 LexUnexpandedToken(Tok);
Ted Kremenek551c82a2008-11-18 01:12:54 +0000384 if (CurPPLexer) CurPPLexer->LexingRawMode = false;
Mike Stump11289f42009-09-09 15:08:12 +0000385
Chris Lattnerb694ba72006-07-02 22:41:36 +0000386 // If we reached the end of line, we're done.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000387 if (Tok.is(tok::eod)) return;
Mike Stump11289f42009-09-09 15:08:12 +0000388
Chris Lattnerb694ba72006-07-02 22:41:36 +0000389 // Can only poison identifiers.
Abramo Bagnaraea4f7c72010-12-22 08:23:18 +0000390 if (Tok.isNot(tok::raw_identifier)) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000391 Diag(Tok, diag::err_pp_invalid_poison);
392 return;
393 }
Mike Stump11289f42009-09-09 15:08:12 +0000394
Chris Lattnercefc7682006-07-08 08:28:12 +0000395 // Look up the identifier info for the token. We disabled identifier lookup
396 // by saying we're skipping contents, so we need to do this manually.
397 IdentifierInfo *II = LookUpIdentifierInfo(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000398
Chris Lattnerb694ba72006-07-02 22:41:36 +0000399 // Already poisoned.
400 if (II->isPoisoned()) continue;
Mike Stump11289f42009-09-09 15:08:12 +0000401
Chris Lattnerb694ba72006-07-02 22:41:36 +0000402 // If this is a macro identifier, emit a warning.
Chris Lattner259716a2007-10-07 08:04:56 +0000403 if (II->hasMacroDefinition())
Chris Lattnerb694ba72006-07-02 22:41:36 +0000404 Diag(Tok, diag::pp_poisoning_existing_macro);
Mike Stump11289f42009-09-09 15:08:12 +0000405
Chris Lattnerb694ba72006-07-02 22:41:36 +0000406 // Finally, poison it!
407 II->setIsPoisoned();
Douglas Gregor935bc7a22011-10-27 09:33:13 +0000408 if (II->isFromAST())
409 II->setChangedSinceDeserialization();
Chris Lattnerb694ba72006-07-02 22:41:36 +0000410 }
411}
412
James Dennett18a6d792012-06-17 03:26:26 +0000413/// HandlePragmaSystemHeader - Implement \#pragma GCC system_header. We know
Chris Lattnerb694ba72006-07-02 22:41:36 +0000414/// that the whole directive has been parsed.
Chris Lattner146762e2007-07-20 16:59:19 +0000415void Preprocessor::HandlePragmaSystemHeader(Token &SysHeaderTok) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000416 if (isInPrimaryFile()) {
417 Diag(SysHeaderTok, diag::pp_pragma_sysheader_in_main_file);
418 return;
419 }
Mike Stump11289f42009-09-09 15:08:12 +0000420
Chris Lattnerb694ba72006-07-02 22:41:36 +0000421 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
Ted Kremenek300590b2008-11-20 01:45:11 +0000422 PreprocessorLexer *TheLexer = getCurrentFileLexer();
Mike Stump11289f42009-09-09 15:08:12 +0000423
Chris Lattnerb694ba72006-07-02 22:41:36 +0000424 // Mark the file as a system header.
Chris Lattnerd32480d2009-01-17 06:22:33 +0000425 HeaderInfo.MarkFileSystemHeader(TheLexer->getFileEntry());
Mike Stump11289f42009-09-09 15:08:12 +0000426
427
Chris Lattnerd9efb6e2009-06-15 05:02:34 +0000428 PresumedLoc PLoc = SourceMgr.getPresumedLoc(SysHeaderTok.getLocation());
Douglas Gregor453b0122010-11-12 07:15:47 +0000429 if (PLoc.isInvalid())
430 return;
431
Jay Foad9a6b0982011-06-21 15:13:30 +0000432 unsigned FilenameID = SourceMgr.getLineTableFilenameID(PLoc.getFilename());
Mike Stump11289f42009-09-09 15:08:12 +0000433
Chris Lattner3bdc7672011-05-22 22:10:16 +0000434 // Notify the client, if desired, that we are in a new source file.
435 if (Callbacks)
436 Callbacks->FileChanged(SysHeaderTok.getLocation(),
437 PPCallbacks::SystemHeaderPragma, SrcMgr::C_System);
438
Chris Lattnerd9efb6e2009-06-15 05:02:34 +0000439 // Emit a line marker. This will change any source locations from this point
440 // forward to realize they are in a system header.
441 // Create a line note with this information.
Jordan Rose111c4a62013-04-17 19:09:18 +0000442 SourceMgr.AddLineNote(SysHeaderTok.getLocation(), PLoc.getLine()+1,
443 FilenameID, /*IsEntry=*/false, /*IsExit=*/false,
444 /*IsSystem=*/true, /*IsExternC=*/false);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000445}
446
James Dennett18a6d792012-06-17 03:26:26 +0000447/// HandlePragmaDependency - Handle \#pragma GCC dependency "foo" blah.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000448///
Chris Lattner146762e2007-07-20 16:59:19 +0000449void Preprocessor::HandlePragmaDependency(Token &DependencyTok) {
450 Token FilenameTok;
Ted Kremenek551c82a2008-11-18 01:12:54 +0000451 CurPPLexer->LexIncludeFilename(FilenameTok);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000452
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000453 // If the token kind is EOD, the error has already been diagnosed.
454 if (FilenameTok.is(tok::eod))
Chris Lattnerb694ba72006-07-02 22:41:36 +0000455 return;
Mike Stump11289f42009-09-09 15:08:12 +0000456
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000457 // Reserve a buffer to get the spelling.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000458 SmallString<128> FilenameBuffer;
Douglas Gregordc970f02010-03-16 22:30:13 +0000459 bool Invalid = false;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000460 StringRef Filename = getSpelling(FilenameTok, FilenameBuffer, &Invalid);
Douglas Gregordc970f02010-03-16 22:30:13 +0000461 if (Invalid)
462 return;
Mike Stump11289f42009-09-09 15:08:12 +0000463
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000464 bool isAngled =
465 GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000466 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
467 // error.
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000468 if (Filename.empty())
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000469 return;
Mike Stump11289f42009-09-09 15:08:12 +0000470
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000471 // Search include directories for this file.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000472 const DirectoryLookup *CurDir;
Lawrence Crowlb53e5482013-06-20 21:14:14 +0000473 const FileEntry *File = LookupFile(FilenameTok.getLocation(), Filename,
474 isAngled, 0, CurDir, NULL, NULL, NULL);
Chris Lattner97b8e842008-11-18 08:02:48 +0000475 if (File == 0) {
Eli Friedman3781a362011-08-30 23:07:51 +0000476 if (!SuppressIncludeNotFoundError)
477 Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
Chris Lattner97b8e842008-11-18 08:02:48 +0000478 return;
479 }
Mike Stump11289f42009-09-09 15:08:12 +0000480
Chris Lattnerd32480d2009-01-17 06:22:33 +0000481 const FileEntry *CurFile = getCurrentFileLexer()->getFileEntry();
Chris Lattnerb694ba72006-07-02 22:41:36 +0000482
483 // If this file is older than the file it depends on, emit a diagnostic.
484 if (CurFile && CurFile->getModificationTime() < File->getModificationTime()) {
485 // Lex tokens at the end of the message and include them in the message.
486 std::string Message;
487 Lex(DependencyTok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000488 while (DependencyTok.isNot(tok::eod)) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000489 Message += getSpelling(DependencyTok) + " ";
490 Lex(DependencyTok);
491 }
Mike Stump11289f42009-09-09 15:08:12 +0000492
Chris Lattnerf0b04972010-09-05 23:16:09 +0000493 // Remove the trailing ' ' if present.
494 if (!Message.empty())
495 Message.erase(Message.end()-1);
Chris Lattner97b8e842008-11-18 08:02:48 +0000496 Diag(FilenameTok, diag::pp_out_of_date_dependency) << Message;
Chris Lattnerb694ba72006-07-02 22:41:36 +0000497 }
498}
499
Reid Kleckner002562a2013-05-06 21:02:12 +0000500/// ParsePragmaPushOrPopMacro - Handle parsing of pragma push_macro/pop_macro.
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000501/// Return the IdentifierInfo* associated with the macro to push or pop.
502IdentifierInfo *Preprocessor::ParsePragmaPushOrPopMacro(Token &Tok) {
503 // Remember the pragma token location.
504 Token PragmaTok = Tok;
505
506 // Read the '('.
507 Lex(Tok);
508 if (Tok.isNot(tok::l_paren)) {
509 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
510 << getSpelling(PragmaTok);
511 return 0;
512 }
513
514 // Read the macro name string.
515 Lex(Tok);
516 if (Tok.isNot(tok::string_literal)) {
517 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
518 << getSpelling(PragmaTok);
519 return 0;
520 }
521
Richard Smithd67aea22012-03-06 03:21:47 +0000522 if (Tok.hasUDSuffix()) {
523 Diag(Tok, diag::err_invalid_string_udl);
524 return 0;
525 }
526
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000527 // Remember the macro string.
528 std::string StrVal = getSpelling(Tok);
529
530 // Read the ')'.
531 Lex(Tok);
532 if (Tok.isNot(tok::r_paren)) {
533 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
534 << getSpelling(PragmaTok);
535 return 0;
536 }
537
538 assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
539 "Invalid string token!");
540
541 // Create a Token from the string.
542 Token MacroTok;
543 MacroTok.startToken();
Abramo Bagnaraea4f7c72010-12-22 08:23:18 +0000544 MacroTok.setKind(tok::raw_identifier);
Dmitri Gribenkob8e9e752012-09-24 21:07:17 +0000545 CreateString(StringRef(&StrVal[1], StrVal.size() - 2), MacroTok);
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000546
547 // Get the IdentifierInfo of MacroToPushTok.
548 return LookUpIdentifierInfo(MacroTok);
549}
550
James Dennett18a6d792012-06-17 03:26:26 +0000551/// \brief Handle \#pragma push_macro.
552///
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000553/// The syntax is:
James Dennett18a6d792012-06-17 03:26:26 +0000554/// \code
Dmitri Gribenko9ebd1612012-11-30 20:04:39 +0000555/// #pragma push_macro("macro")
James Dennett18a6d792012-06-17 03:26:26 +0000556/// \endcode
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000557void Preprocessor::HandlePragmaPushMacro(Token &PushMacroTok) {
558 // Parse the pragma directive and get the macro IdentifierInfo*.
559 IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PushMacroTok);
560 if (!IdentInfo) return;
561
562 // Get the MacroInfo associated with IdentInfo.
563 MacroInfo *MI = getMacroInfo(IdentInfo);
564
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000565 if (MI) {
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000566 // Allow the original MacroInfo to be redefined later.
567 MI->setIsAllowRedefinitionsWithoutWarning(true);
568 }
569
570 // Push the cloned MacroInfo so we can retrieve it later.
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +0000571 PragmaPushMacroInfo[IdentInfo].push_back(MI);
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000572}
573
James Dennett18a6d792012-06-17 03:26:26 +0000574/// \brief Handle \#pragma pop_macro.
575///
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000576/// The syntax is:
James Dennett18a6d792012-06-17 03:26:26 +0000577/// \code
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000578/// #pragma pop_macro("macro")
James Dennett18a6d792012-06-17 03:26:26 +0000579/// \endcode
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000580void Preprocessor::HandlePragmaPopMacro(Token &PopMacroTok) {
581 SourceLocation MessageLoc = PopMacroTok.getLocation();
582
583 // Parse the pragma directive and get the macro IdentifierInfo*.
584 IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PopMacroTok);
585 if (!IdentInfo) return;
586
587 // Find the vector<MacroInfo*> associated with the macro.
588 llvm::DenseMap<IdentifierInfo*, std::vector<MacroInfo*> >::iterator iter =
589 PragmaPushMacroInfo.find(IdentInfo);
590 if (iter != PragmaPushMacroInfo.end()) {
Alexander Kornienko8b3f6232012-08-29 00:20:03 +0000591 // Forget the MacroInfo currently associated with IdentInfo.
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +0000592 if (MacroDirective *CurrentMD = getMacroDirective(IdentInfo)) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +0000593 MacroInfo *MI = CurrentMD->getMacroInfo();
594 if (MI->isWarnIfUnused())
595 WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());
596 appendMacroDirective(IdentInfo, AllocateUndefMacroDirective(MessageLoc));
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000597 }
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000598
599 // Get the MacroInfo we want to reinstall.
600 MacroInfo *MacroToReInstall = iter->second.back();
601
Alexander Kornienkoc0b49282012-08-29 16:56:24 +0000602 if (MacroToReInstall) {
603 // Reinstall the previously pushed macro.
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +0000604 appendDefMacroDirective(IdentInfo, MacroToReInstall, MessageLoc,
605 /*isImported=*/false);
Alexander Kornienkoc0b49282012-08-29 16:56:24 +0000606 }
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000607
608 // Pop PragmaPushMacroInfo stack.
609 iter->second.pop_back();
610 if (iter->second.size() == 0)
611 PragmaPushMacroInfo.erase(iter);
612 } else {
613 Diag(MessageLoc, diag::warn_pragma_pop_macro_no_push)
614 << IdentInfo->getName();
615 }
616}
Chris Lattnerb694ba72006-07-02 22:41:36 +0000617
Aaron Ballman611306e2012-03-02 22:51:54 +0000618void Preprocessor::HandlePragmaIncludeAlias(Token &Tok) {
619 // We will either get a quoted filename or a bracketed filename, and we
620 // have to track which we got. The first filename is the source name,
621 // and the second name is the mapped filename. If the first is quoted,
622 // the second must be as well (cannot mix and match quotes and brackets).
Aaron Ballman611306e2012-03-02 22:51:54 +0000623
624 // Get the open paren
625 Lex(Tok);
626 if (Tok.isNot(tok::l_paren)) {
627 Diag(Tok, diag::warn_pragma_include_alias_expected) << "(";
628 return;
629 }
630
631 // We expect either a quoted string literal, or a bracketed name
632 Token SourceFilenameTok;
633 CurPPLexer->LexIncludeFilename(SourceFilenameTok);
634 if (SourceFilenameTok.is(tok::eod)) {
635 // The diagnostic has already been handled
636 return;
637 }
638
639 StringRef SourceFileName;
640 SmallString<128> FileNameBuffer;
641 if (SourceFilenameTok.is(tok::string_literal) ||
642 SourceFilenameTok.is(tok::angle_string_literal)) {
643 SourceFileName = getSpelling(SourceFilenameTok, FileNameBuffer);
644 } else if (SourceFilenameTok.is(tok::less)) {
645 // This could be a path instead of just a name
646 FileNameBuffer.push_back('<');
647 SourceLocation End;
648 if (ConcatenateIncludeName(FileNameBuffer, End))
649 return; // Diagnostic already emitted
650 SourceFileName = FileNameBuffer.str();
651 } else {
652 Diag(Tok, diag::warn_pragma_include_alias_expected_filename);
653 return;
654 }
655 FileNameBuffer.clear();
656
657 // Now we expect a comma, followed by another include name
658 Lex(Tok);
659 if (Tok.isNot(tok::comma)) {
660 Diag(Tok, diag::warn_pragma_include_alias_expected) << ",";
661 return;
662 }
663
664 Token ReplaceFilenameTok;
665 CurPPLexer->LexIncludeFilename(ReplaceFilenameTok);
666 if (ReplaceFilenameTok.is(tok::eod)) {
667 // The diagnostic has already been handled
668 return;
669 }
670
671 StringRef ReplaceFileName;
672 if (ReplaceFilenameTok.is(tok::string_literal) ||
673 ReplaceFilenameTok.is(tok::angle_string_literal)) {
674 ReplaceFileName = getSpelling(ReplaceFilenameTok, FileNameBuffer);
675 } else if (ReplaceFilenameTok.is(tok::less)) {
676 // This could be a path instead of just a name
677 FileNameBuffer.push_back('<');
678 SourceLocation End;
679 if (ConcatenateIncludeName(FileNameBuffer, End))
680 return; // Diagnostic already emitted
681 ReplaceFileName = FileNameBuffer.str();
682 } else {
683 Diag(Tok, diag::warn_pragma_include_alias_expected_filename);
684 return;
685 }
686
687 // Finally, we expect the closing paren
688 Lex(Tok);
689 if (Tok.isNot(tok::r_paren)) {
690 Diag(Tok, diag::warn_pragma_include_alias_expected) << ")";
691 return;
692 }
693
694 // Now that we have the source and target filenames, we need to make sure
695 // they're both of the same type (angled vs non-angled)
696 StringRef OriginalSource = SourceFileName;
697
698 bool SourceIsAngled =
699 GetIncludeFilenameSpelling(SourceFilenameTok.getLocation(),
700 SourceFileName);
701 bool ReplaceIsAngled =
702 GetIncludeFilenameSpelling(ReplaceFilenameTok.getLocation(),
703 ReplaceFileName);
704 if (!SourceFileName.empty() && !ReplaceFileName.empty() &&
705 (SourceIsAngled != ReplaceIsAngled)) {
706 unsigned int DiagID;
707 if (SourceIsAngled)
708 DiagID = diag::warn_pragma_include_alias_mismatch_angle;
709 else
710 DiagID = diag::warn_pragma_include_alias_mismatch_quote;
711
712 Diag(SourceFilenameTok.getLocation(), DiagID)
713 << SourceFileName
714 << ReplaceFileName;
715
716 return;
717 }
718
719 // Now we can let the include handler know about this mapping
720 getHeaderSearchInfo().AddIncludeAlias(OriginalSource, ReplaceFileName);
721}
722
Chris Lattnerb694ba72006-07-02 22:41:36 +0000723/// AddPragmaHandler - Add the specified pragma handler to the preprocessor.
724/// If 'Namespace' is non-null, then it is a token required to exist on the
725/// pragma line before the pragma string starts, e.g. "STDC" or "GCC".
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000726void Preprocessor::AddPragmaHandler(StringRef Namespace,
Chris Lattnerb694ba72006-07-02 22:41:36 +0000727 PragmaHandler *Handler) {
728 PragmaNamespace *InsertNS = PragmaHandlers;
Mike Stump11289f42009-09-09 15:08:12 +0000729
Chris Lattnerb694ba72006-07-02 22:41:36 +0000730 // If this is specified to be in a namespace, step down into it.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000731 if (!Namespace.empty()) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000732 // If there is already a pragma handler with the name of this namespace,
733 // we either have an error (directive with the same name as a namespace) or
734 // we already have the namespace to insert into.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000735 if (PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace)) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000736 InsertNS = Existing->getIfNamespace();
737 assert(InsertNS != 0 && "Cannot have a pragma namespace and pragma"
738 " handler with the same name!");
739 } else {
740 // Otherwise, this namespace doesn't exist yet, create and insert the
741 // handler for it.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000742 InsertNS = new PragmaNamespace(Namespace);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000743 PragmaHandlers->AddPragma(InsertNS);
744 }
745 }
Mike Stump11289f42009-09-09 15:08:12 +0000746
Chris Lattnerb694ba72006-07-02 22:41:36 +0000747 // Check to make sure we don't already have a pragma for this identifier.
748 assert(!InsertNS->FindHandler(Handler->getName()) &&
749 "Pragma handler already exists for this identifier!");
750 InsertNS->AddPragma(Handler);
751}
752
Daniel Dunbar40596532008-10-04 19:17:46 +0000753/// RemovePragmaHandler - Remove the specific pragma handler from the
754/// preprocessor. If \arg Namespace is non-null, then it should be the
755/// namespace that \arg Handler was added to. It is an error to remove
756/// a handler that has not been registered.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000757void Preprocessor::RemovePragmaHandler(StringRef Namespace,
Daniel Dunbar40596532008-10-04 19:17:46 +0000758 PragmaHandler *Handler) {
759 PragmaNamespace *NS = PragmaHandlers;
Mike Stump11289f42009-09-09 15:08:12 +0000760
Daniel Dunbar40596532008-10-04 19:17:46 +0000761 // If this is specified to be in a namespace, step down into it.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000762 if (!Namespace.empty()) {
763 PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace);
Daniel Dunbar40596532008-10-04 19:17:46 +0000764 assert(Existing && "Namespace containing handler does not exist!");
765
766 NS = Existing->getIfNamespace();
767 assert(NS && "Invalid namespace, registered as a regular pragma handler!");
768 }
769
770 NS->RemovePragmaHandler(Handler);
Mike Stump11289f42009-09-09 15:08:12 +0000771
Daniel Dunbar40596532008-10-04 19:17:46 +0000772 // If this is a non-default namespace and it is now empty, remove
773 // it.
Argyrios Kyrtzidis7ce75262012-01-06 00:22:09 +0000774 if (NS != PragmaHandlers && NS->IsEmpty()) {
Daniel Dunbar40596532008-10-04 19:17:46 +0000775 PragmaHandlers->RemovePragmaHandler(NS);
Argyrios Kyrtzidis7ce75262012-01-06 00:22:09 +0000776 delete NS;
777 }
Daniel Dunbar40596532008-10-04 19:17:46 +0000778}
779
Peter Collingbourne3bffa522011-02-14 01:42:24 +0000780bool Preprocessor::LexOnOffSwitch(tok::OnOffSwitch &Result) {
781 Token Tok;
782 LexUnexpandedToken(Tok);
783
784 if (Tok.isNot(tok::identifier)) {
785 Diag(Tok, diag::ext_on_off_switch_syntax);
786 return true;
787 }
788 IdentifierInfo *II = Tok.getIdentifierInfo();
789 if (II->isStr("ON"))
790 Result = tok::OOS_ON;
791 else if (II->isStr("OFF"))
792 Result = tok::OOS_OFF;
793 else if (II->isStr("DEFAULT"))
794 Result = tok::OOS_DEFAULT;
795 else {
796 Diag(Tok, diag::ext_on_off_switch_syntax);
797 return true;
798 }
799
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000800 // Verify that this is followed by EOD.
Peter Collingbourne3bffa522011-02-14 01:42:24 +0000801 LexUnexpandedToken(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000802 if (Tok.isNot(tok::eod))
803 Diag(Tok, diag::ext_pragma_syntax_eod);
Peter Collingbourne3bffa522011-02-14 01:42:24 +0000804 return false;
805}
806
Chris Lattnerb694ba72006-07-02 22:41:36 +0000807namespace {
James Dennett18a6d792012-06-17 03:26:26 +0000808/// PragmaOnceHandler - "\#pragma once" marks the file as atomically included.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000809struct PragmaOnceHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000810 PragmaOnceHandler() : PragmaHandler("once") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000811 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
812 Token &OnceTok) {
Chris Lattnerce2ab6f2009-04-14 05:07:49 +0000813 PP.CheckEndOfDirective("pragma once");
Chris Lattnerb694ba72006-07-02 22:41:36 +0000814 PP.HandlePragmaOnce(OnceTok);
815 }
816};
817
James Dennett18a6d792012-06-17 03:26:26 +0000818/// PragmaMarkHandler - "\#pragma mark ..." is ignored by the compiler, and the
Chris Lattnerc2383312007-12-19 19:38:36 +0000819/// rest of the line is not lexed.
820struct PragmaMarkHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000821 PragmaMarkHandler() : PragmaHandler("mark") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000822 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
823 Token &MarkTok) {
Chris Lattnerc2383312007-12-19 19:38:36 +0000824 PP.HandlePragmaMark();
825 }
826};
827
James Dennett18a6d792012-06-17 03:26:26 +0000828/// PragmaPoisonHandler - "\#pragma poison x" marks x as not usable.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000829struct PragmaPoisonHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000830 PragmaPoisonHandler() : PragmaHandler("poison") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000831 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
832 Token &PoisonTok) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000833 PP.HandlePragmaPoison(PoisonTok);
834 }
835};
836
James Dennett18a6d792012-06-17 03:26:26 +0000837/// PragmaSystemHeaderHandler - "\#pragma system_header" marks the current file
Chris Lattnerc2383312007-12-19 19:38:36 +0000838/// as a system header, which silences warnings in it.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000839struct PragmaSystemHeaderHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000840 PragmaSystemHeaderHandler() : PragmaHandler("system_header") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000841 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
842 Token &SHToken) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000843 PP.HandlePragmaSystemHeader(SHToken);
Chris Lattnerce2ab6f2009-04-14 05:07:49 +0000844 PP.CheckEndOfDirective("pragma");
Chris Lattnerb694ba72006-07-02 22:41:36 +0000845 }
846};
847struct PragmaDependencyHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000848 PragmaDependencyHandler() : PragmaHandler("dependency") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000849 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
850 Token &DepToken) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000851 PP.HandlePragmaDependency(DepToken);
852 }
853};
Mike Stump11289f42009-09-09 15:08:12 +0000854
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000855struct PragmaDebugHandler : public PragmaHandler {
856 PragmaDebugHandler() : PragmaHandler("__debug") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000857 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
858 Token &DepToken) {
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000859 Token Tok;
860 PP.LexUnexpandedToken(Tok);
861 if (Tok.isNot(tok::identifier)) {
Douglas Gregor3cc26482010-08-30 15:15:34 +0000862 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000863 return;
864 }
865 IdentifierInfo *II = Tok.getIdentifierInfo();
866
Daniel Dunbarf2cf3292010-08-17 22:32:48 +0000867 if (II->isStr("assert")) {
David Blaikie83d382b2011-09-23 05:06:16 +0000868 llvm_unreachable("This is an assertion!");
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000869 } else if (II->isStr("crash")) {
David Blaikie5bd4c2a2012-08-21 18:56:49 +0000870 LLVM_BUILTIN_TRAP;
David Blaikie5d577a22012-06-29 22:03:56 +0000871 } else if (II->isStr("parser_crash")) {
872 Token Crasher;
873 Crasher.setKind(tok::annot_pragma_parser_crash);
874 PP.EnterToken(Crasher);
Daniel Dunbarf2cf3292010-08-17 22:32:48 +0000875 } else if (II->isStr("llvm_fatal_error")) {
876 llvm::report_fatal_error("#pragma clang __debug llvm_fatal_error");
877 } else if (II->isStr("llvm_unreachable")) {
878 llvm_unreachable("#pragma clang __debug llvm_unreachable");
879 } else if (II->isStr("overflow_stack")) {
880 DebugOverflowStack();
Daniel Dunbar211a7872010-08-18 23:09:23 +0000881 } else if (II->isStr("handle_crash")) {
882 llvm::CrashRecoveryContext *CRC =llvm::CrashRecoveryContext::GetCurrent();
883 if (CRC)
884 CRC->HandleCrash();
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000885 } else if (II->isStr("captured")) {
886 HandleCaptured(PP);
Daniel Dunbarf2cf3292010-08-17 22:32:48 +0000887 } else {
888 PP.Diag(Tok, diag::warn_pragma_debug_unexpected_command)
889 << II->getName();
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000890 }
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000891
892 PPCallbacks *Callbacks = PP.getPPCallbacks();
893 if (Callbacks)
894 Callbacks->PragmaDebug(Tok.getLocation(), II->getName());
895 }
896
897 void HandleCaptured(Preprocessor &PP) {
898 // Skip if emitting preprocessed output.
899 if (PP.isPreprocessedOutput())
900 return;
901
902 Token Tok;
903 PP.LexUnexpandedToken(Tok);
904
905 if (Tok.isNot(tok::eod)) {
906 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol)
907 << "pragma clang __debug captured";
908 return;
909 }
910
911 SourceLocation NameLoc = Tok.getLocation();
912 Token *Toks = PP.getPreprocessorAllocator().Allocate<Token>(1);
913 Toks->startToken();
914 Toks->setKind(tok::annot_pragma_captured);
915 Toks->setLocation(NameLoc);
916
917 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
918 /*OwnsTokens=*/false);
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000919 }
920
Francois Pichet2e11f5d2011-05-25 16:15:03 +0000921// Disable MSVC warning about runtime stack overflow.
922#ifdef _MSC_VER
923 #pragma warning(disable : 4717)
924#endif
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000925 void DebugOverflowStack() {
926 DebugOverflowStack();
927 }
Francois Pichet2e11f5d2011-05-25 16:15:03 +0000928#ifdef _MSC_VER
929 #pragma warning(default : 4717)
930#endif
931
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000932};
933
James Dennett18a6d792012-06-17 03:26:26 +0000934/// PragmaDiagnosticHandler - e.g. '\#pragma GCC diagnostic ignored "-Wformat"'
Chris Lattner504af112009-04-19 23:16:58 +0000935struct PragmaDiagnosticHandler : public PragmaHandler {
Douglas Gregor3bde9b12011-06-22 19:41:48 +0000936private:
937 const char *Namespace;
Chris Lattnerfb42a182009-07-12 21:18:45 +0000938public:
Douglas Gregor3bde9b12011-06-22 19:41:48 +0000939 explicit PragmaDiagnosticHandler(const char *NS) :
940 PragmaHandler("diagnostic"), Namespace(NS) {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000941 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
942 Token &DiagToken) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000943 SourceLocation DiagLoc = DiagToken.getLocation();
Chris Lattner504af112009-04-19 23:16:58 +0000944 Token Tok;
945 PP.LexUnexpandedToken(Tok);
946 if (Tok.isNot(tok::identifier)) {
Douglas Gregor3cc26482010-08-30 15:15:34 +0000947 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Chris Lattner504af112009-04-19 23:16:58 +0000948 return;
949 }
950 IdentifierInfo *II = Tok.getIdentifierInfo();
Douglas Gregor3bde9b12011-06-22 19:41:48 +0000951 PPCallbacks *Callbacks = PP.getPPCallbacks();
Mike Stump11289f42009-09-09 15:08:12 +0000952
Chris Lattner504af112009-04-19 23:16:58 +0000953 diag::Mapping Map;
954 if (II->isStr("warning"))
955 Map = diag::MAP_WARNING;
956 else if (II->isStr("error"))
957 Map = diag::MAP_ERROR;
958 else if (II->isStr("ignored"))
959 Map = diag::MAP_IGNORE;
960 else if (II->isStr("fatal"))
961 Map = diag::MAP_FATAL;
Douglas Gregor3cc26482010-08-30 15:15:34 +0000962 else if (II->isStr("pop")) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000963 if (!PP.getDiagnostics().popMappings(DiagLoc))
Douglas Gregor3cc26482010-08-30 15:15:34 +0000964 PP.Diag(Tok, diag::warn_pragma_diagnostic_cannot_pop);
Douglas Gregor3bde9b12011-06-22 19:41:48 +0000965 else if (Callbacks)
966 Callbacks->PragmaDiagnosticPop(DiagLoc, Namespace);
Douglas Gregor3cc26482010-08-30 15:15:34 +0000967 return;
968 } else if (II->isStr("push")) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000969 PP.getDiagnostics().pushMappings(DiagLoc);
Douglas Gregor3bde9b12011-06-22 19:41:48 +0000970 if (Callbacks)
971 Callbacks->PragmaDiagnosticPush(DiagLoc, Namespace);
Chris Lattnerfb42a182009-07-12 21:18:45 +0000972 return;
973 } else {
Douglas Gregor3cc26482010-08-30 15:15:34 +0000974 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Chris Lattner504af112009-04-19 23:16:58 +0000975 return;
976 }
Mike Stump11289f42009-09-09 15:08:12 +0000977
Chris Lattner504af112009-04-19 23:16:58 +0000978 PP.LexUnexpandedToken(Tok);
Andy Gibbs58905d22012-11-17 19:15:38 +0000979 SourceLocation StringLoc = Tok.getLocation();
Chris Lattner504af112009-04-19 23:16:58 +0000980
Andy Gibbs58905d22012-11-17 19:15:38 +0000981 std::string WarningName;
Andy Gibbsa8df57a2012-11-17 19:16:52 +0000982 if (!PP.FinishLexStringLiteral(Tok, WarningName, "pragma diagnostic",
983 /*MacroExpansion=*/false))
Chris Lattner504af112009-04-19 23:16:58 +0000984 return;
Mike Stump11289f42009-09-09 15:08:12 +0000985
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000986 if (Tok.isNot(tok::eod)) {
Chris Lattner504af112009-04-19 23:16:58 +0000987 PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token);
988 return;
989 }
Mike Stump11289f42009-09-09 15:08:12 +0000990
Chris Lattner504af112009-04-19 23:16:58 +0000991 if (WarningName.size() < 3 || WarningName[0] != '-' ||
992 WarningName[1] != 'W') {
Andy Gibbs58905d22012-11-17 19:15:38 +0000993 PP.Diag(StringLoc, diag::warn_pragma_diagnostic_invalid_option);
Chris Lattner504af112009-04-19 23:16:58 +0000994 return;
995 }
Mike Stump11289f42009-09-09 15:08:12 +0000996
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +0000997 if (PP.getDiagnostics().setDiagnosticGroupMapping(WarningName.substr(2),
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000998 Map, DiagLoc))
Andy Gibbs58905d22012-11-17 19:15:38 +0000999 PP.Diag(StringLoc, diag::warn_pragma_diagnostic_unknown_warning)
1000 << WarningName;
Douglas Gregor3bde9b12011-06-22 19:41:48 +00001001 else if (Callbacks)
1002 Callbacks->PragmaDiagnostic(DiagLoc, Namespace, Map, WarningName);
Chris Lattner504af112009-04-19 23:16:58 +00001003 }
1004};
Mike Stump11289f42009-09-09 15:08:12 +00001005
James Dennett18a6d792012-06-17 03:26:26 +00001006/// PragmaIncludeAliasHandler - "\#pragma include_alias("...")".
Aaron Ballman611306e2012-03-02 22:51:54 +00001007struct PragmaIncludeAliasHandler : public PragmaHandler {
1008 PragmaIncludeAliasHandler() : PragmaHandler("include_alias") {}
1009 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1010 Token &IncludeAliasTok) {
1011 PP.HandlePragmaIncludeAlias(IncludeAliasTok);
1012 }
1013};
1014
Andy Gibbs9c2ccd62013-04-17 16:16:16 +00001015/// PragmaMessageHandler - Handle the microsoft and gcc \#pragma message
1016/// extension. The syntax is:
1017/// \code
1018/// #pragma message(string)
1019/// \endcode
1020/// OR, in GCC mode:
1021/// \code
1022/// #pragma message string
1023/// \endcode
1024/// string is a string, which is fully macro expanded, and permits string
1025/// concatenation, embedded escape characters, etc... See MSDN for more details.
1026/// Also handles \#pragma GCC warning and \#pragma GCC error which take the same
1027/// form as \#pragma message.
Chris Lattner30c924b2010-06-26 17:11:39 +00001028struct PragmaMessageHandler : public PragmaHandler {
Andy Gibbs9c2ccd62013-04-17 16:16:16 +00001029private:
1030 const PPCallbacks::PragmaMessageKind Kind;
1031 const StringRef Namespace;
1032
1033 static const char* PragmaKind(PPCallbacks::PragmaMessageKind Kind,
1034 bool PragmaNameOnly = false) {
1035 switch (Kind) {
1036 case PPCallbacks::PMK_Message:
1037 return PragmaNameOnly ? "message" : "pragma message";
1038 case PPCallbacks::PMK_Warning:
1039 return PragmaNameOnly ? "warning" : "pragma warning";
1040 case PPCallbacks::PMK_Error:
1041 return PragmaNameOnly ? "error" : "pragma error";
1042 }
1043 llvm_unreachable("Unknown PragmaMessageKind!");
1044 }
1045
1046public:
1047 PragmaMessageHandler(PPCallbacks::PragmaMessageKind Kind,
1048 StringRef Namespace = StringRef())
1049 : PragmaHandler(PragmaKind(Kind, true)), Kind(Kind), Namespace(Namespace) {}
1050
Douglas Gregorc7d65762010-09-09 22:45:38 +00001051 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
Andy Gibbs9c2ccd62013-04-17 16:16:16 +00001052 Token &Tok) {
1053 SourceLocation MessageLoc = Tok.getLocation();
1054 PP.Lex(Tok);
1055 bool ExpectClosingParen = false;
1056 switch (Tok.getKind()) {
1057 case tok::l_paren:
1058 // We have a MSVC style pragma message.
1059 ExpectClosingParen = true;
1060 // Read the string.
1061 PP.Lex(Tok);
1062 break;
1063 case tok::string_literal:
1064 // We have a GCC style pragma message, and we just read the string.
1065 break;
1066 default:
1067 PP.Diag(MessageLoc, diag::err_pragma_message_malformed) << Kind;
1068 return;
1069 }
1070
1071 std::string MessageString;
1072 if (!PP.FinishLexStringLiteral(Tok, MessageString, PragmaKind(Kind),
1073 /*MacroExpansion=*/true))
1074 return;
1075
1076 if (ExpectClosingParen) {
1077 if (Tok.isNot(tok::r_paren)) {
1078 PP.Diag(Tok.getLocation(), diag::err_pragma_message_malformed) << Kind;
1079 return;
1080 }
1081 PP.Lex(Tok); // eat the r_paren.
1082 }
1083
1084 if (Tok.isNot(tok::eod)) {
1085 PP.Diag(Tok.getLocation(), diag::err_pragma_message_malformed) << Kind;
1086 return;
1087 }
1088
1089 // Output the message.
1090 PP.Diag(MessageLoc, (Kind == PPCallbacks::PMK_Error)
1091 ? diag::err_pragma_message
1092 : diag::warn_pragma_message) << MessageString;
1093
1094 // If the pragma is lexically sound, notify any interested PPCallbacks.
1095 if (PPCallbacks *Callbacks = PP.getPPCallbacks())
1096 Callbacks->PragmaMessage(MessageLoc, Namespace, Kind, MessageString);
Chris Lattner30c924b2010-06-26 17:11:39 +00001097 }
1098};
1099
James Dennett18a6d792012-06-17 03:26:26 +00001100/// PragmaPushMacroHandler - "\#pragma push_macro" saves the value of the
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001101/// macro on the top of the stack.
1102struct PragmaPushMacroHandler : public PragmaHandler {
1103 PragmaPushMacroHandler() : PragmaHandler("push_macro") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +00001104 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1105 Token &PushMacroTok) {
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001106 PP.HandlePragmaPushMacro(PushMacroTok);
1107 }
1108};
1109
1110
James Dennett18a6d792012-06-17 03:26:26 +00001111/// PragmaPopMacroHandler - "\#pragma pop_macro" sets the value of the
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001112/// macro to the value on the top of the stack.
1113struct PragmaPopMacroHandler : public PragmaHandler {
1114 PragmaPopMacroHandler() : PragmaHandler("pop_macro") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +00001115 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1116 Token &PopMacroTok) {
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001117 PP.HandlePragmaPopMacro(PopMacroTok);
1118 }
1119};
1120
Chris Lattner958ee042009-04-19 21:20:35 +00001121// Pragma STDC implementations.
Chris Lattner02ef4e32009-04-19 21:50:08 +00001122
James Dennett18a6d792012-06-17 03:26:26 +00001123/// PragmaSTDC_FENV_ACCESSHandler - "\#pragma STDC FENV_ACCESS ...".
Chris Lattner958ee042009-04-19 21:20:35 +00001124struct PragmaSTDC_FENV_ACCESSHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001125 PragmaSTDC_FENV_ACCESSHandler() : PragmaHandler("FENV_ACCESS") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +00001126 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1127 Token &Tok) {
Peter Collingbourne3bffa522011-02-14 01:42:24 +00001128 tok::OnOffSwitch OOS;
1129 if (PP.LexOnOffSwitch(OOS))
1130 return;
1131 if (OOS == tok::OOS_ON)
Chris Lattnerdf222682009-04-19 21:55:32 +00001132 PP.Diag(Tok, diag::warn_stdc_fenv_access_not_supported);
Chris Lattner958ee042009-04-19 21:20:35 +00001133 }
1134};
Mike Stump11289f42009-09-09 15:08:12 +00001135
James Dennett18a6d792012-06-17 03:26:26 +00001136/// PragmaSTDC_CX_LIMITED_RANGEHandler - "\#pragma STDC CX_LIMITED_RANGE ...".
Chris Lattner958ee042009-04-19 21:20:35 +00001137struct PragmaSTDC_CX_LIMITED_RANGEHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001138 PragmaSTDC_CX_LIMITED_RANGEHandler()
1139 : PragmaHandler("CX_LIMITED_RANGE") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +00001140 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1141 Token &Tok) {
Peter Collingbourne3bffa522011-02-14 01:42:24 +00001142 tok::OnOffSwitch OOS;
1143 PP.LexOnOffSwitch(OOS);
Chris Lattner958ee042009-04-19 21:20:35 +00001144 }
1145};
Mike Stump11289f42009-09-09 15:08:12 +00001146
James Dennett18a6d792012-06-17 03:26:26 +00001147/// PragmaSTDC_UnknownHandler - "\#pragma STDC ...".
Chris Lattner958ee042009-04-19 21:20:35 +00001148struct PragmaSTDC_UnknownHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001149 PragmaSTDC_UnknownHandler() {}
Douglas Gregorc7d65762010-09-09 22:45:38 +00001150 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1151 Token &UnknownTok) {
Chris Lattner02ef4e32009-04-19 21:50:08 +00001152 // C99 6.10.6p2, unknown forms are not allowed.
Chris Lattnera0b1f762009-04-19 21:25:37 +00001153 PP.Diag(UnknownTok, diag::ext_stdc_pragma_ignored);
Chris Lattner958ee042009-04-19 21:20:35 +00001154 }
1155};
Mike Stump11289f42009-09-09 15:08:12 +00001156
John McCall32f5fe12011-09-30 05:12:12 +00001157/// PragmaARCCFCodeAuditedHandler -
James Dennett18a6d792012-06-17 03:26:26 +00001158/// \#pragma clang arc_cf_code_audited begin/end
John McCall32f5fe12011-09-30 05:12:12 +00001159struct PragmaARCCFCodeAuditedHandler : public PragmaHandler {
1160 PragmaARCCFCodeAuditedHandler() : PragmaHandler("arc_cf_code_audited") {}
1161 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1162 Token &NameTok) {
1163 SourceLocation Loc = NameTok.getLocation();
1164 bool IsBegin;
1165
1166 Token Tok;
1167
1168 // Lex the 'begin' or 'end'.
1169 PP.LexUnexpandedToken(Tok);
1170 const IdentifierInfo *BeginEnd = Tok.getIdentifierInfo();
1171 if (BeginEnd && BeginEnd->isStr("begin")) {
1172 IsBegin = true;
1173 } else if (BeginEnd && BeginEnd->isStr("end")) {
1174 IsBegin = false;
1175 } else {
1176 PP.Diag(Tok.getLocation(), diag::err_pp_arc_cf_code_audited_syntax);
1177 return;
1178 }
1179
1180 // Verify that this is followed by EOD.
1181 PP.LexUnexpandedToken(Tok);
1182 if (Tok.isNot(tok::eod))
1183 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
1184
1185 // The start location of the active audit.
1186 SourceLocation BeginLoc = PP.getPragmaARCCFCodeAuditedLoc();
1187
1188 // The start location we want after processing this.
1189 SourceLocation NewLoc;
1190
1191 if (IsBegin) {
1192 // Complain about attempts to re-enter an audit.
1193 if (BeginLoc.isValid()) {
1194 PP.Diag(Loc, diag::err_pp_double_begin_of_arc_cf_code_audited);
1195 PP.Diag(BeginLoc, diag::note_pragma_entered_here);
1196 }
1197 NewLoc = Loc;
1198 } else {
1199 // Complain about attempts to leave an audit that doesn't exist.
1200 if (!BeginLoc.isValid()) {
1201 PP.Diag(Loc, diag::err_pp_unmatched_end_of_arc_cf_code_audited);
1202 return;
1203 }
1204 NewLoc = SourceLocation();
1205 }
1206
1207 PP.setPragmaARCCFCodeAuditedLoc(NewLoc);
1208 }
1209};
1210
David Majnemer7aa8c2f2013-06-30 08:18:16 +00001211/// \brief Handle "\#pragma region [...]"
1212///
1213/// The syntax is
1214/// \code
1215/// #pragma region [optional name]
1216/// #pragma endregion [optional comment]
1217/// \endcode
1218///
1219/// \note This is
1220/// <a href="http://msdn.microsoft.com/en-us/library/b6xkz944(v=vs.80).aspx">editor-only</a>
1221/// pragma, just skipped by compiler.
1222struct PragmaRegionHandler : public PragmaHandler {
1223 PragmaRegionHandler(const char *pragma) : PragmaHandler(pragma) { }
Aaron Ballman406ea512012-11-30 19:52:30 +00001224
David Majnemer7aa8c2f2013-06-30 08:18:16 +00001225 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1226 Token &NameTok) {
1227 // #pragma region: endregion matches can be verified
1228 // __pragma(region): no sense, but ignored by msvc
1229 // _Pragma is not valid for MSVC, but there isn't any point
1230 // to handle a _Pragma differently.
1231 }
1232};
Aaron Ballman406ea512012-11-30 19:52:30 +00001233
Chris Lattnerb694ba72006-07-02 22:41:36 +00001234} // end anonymous namespace
1235
1236
1237/// RegisterBuiltinPragmas - Install the standard preprocessor pragmas:
James Dennett18a6d792012-06-17 03:26:26 +00001238/// \#pragma GCC poison/system_header/dependency and \#pragma once.
Chris Lattnerb694ba72006-07-02 22:41:36 +00001239void Preprocessor::RegisterBuiltinPragmas() {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001240 AddPragmaHandler(new PragmaOnceHandler());
1241 AddPragmaHandler(new PragmaMarkHandler());
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001242 AddPragmaHandler(new PragmaPushMacroHandler());
1243 AddPragmaHandler(new PragmaPopMacroHandler());
Andy Gibbs9c2ccd62013-04-17 16:16:16 +00001244 AddPragmaHandler(new PragmaMessageHandler(PPCallbacks::PMK_Message));
Mike Stump11289f42009-09-09 15:08:12 +00001245
Chris Lattnerb61448d2009-05-12 18:21:11 +00001246 // #pragma GCC ...
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001247 AddPragmaHandler("GCC", new PragmaPoisonHandler());
1248 AddPragmaHandler("GCC", new PragmaSystemHeaderHandler());
1249 AddPragmaHandler("GCC", new PragmaDependencyHandler());
Douglas Gregor3bde9b12011-06-22 19:41:48 +00001250 AddPragmaHandler("GCC", new PragmaDiagnosticHandler("GCC"));
Andy Gibbs9c2ccd62013-04-17 16:16:16 +00001251 AddPragmaHandler("GCC", new PragmaMessageHandler(PPCallbacks::PMK_Warning,
1252 "GCC"));
1253 AddPragmaHandler("GCC", new PragmaMessageHandler(PPCallbacks::PMK_Error,
1254 "GCC"));
Chris Lattnerb61448d2009-05-12 18:21:11 +00001255 // #pragma clang ...
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001256 AddPragmaHandler("clang", new PragmaPoisonHandler());
1257 AddPragmaHandler("clang", new PragmaSystemHeaderHandler());
Daniel Dunbarb8068c32010-07-28 15:40:33 +00001258 AddPragmaHandler("clang", new PragmaDebugHandler());
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001259 AddPragmaHandler("clang", new PragmaDependencyHandler());
Douglas Gregor3bde9b12011-06-22 19:41:48 +00001260 AddPragmaHandler("clang", new PragmaDiagnosticHandler("clang"));
John McCall32f5fe12011-09-30 05:12:12 +00001261 AddPragmaHandler("clang", new PragmaARCCFCodeAuditedHandler());
Chris Lattnerb61448d2009-05-12 18:21:11 +00001262
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001263 AddPragmaHandler("STDC", new PragmaSTDC_FENV_ACCESSHandler());
1264 AddPragmaHandler("STDC", new PragmaSTDC_CX_LIMITED_RANGEHandler());
Chris Lattner958ee042009-04-19 21:20:35 +00001265 AddPragmaHandler("STDC", new PragmaSTDC_UnknownHandler());
Mike Stump11289f42009-09-09 15:08:12 +00001266
Chris Lattner2ff698d2009-01-16 08:21:25 +00001267 // MS extensions.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001268 if (LangOpts.MicrosoftExt) {
Aaron Ballman611306e2012-03-02 22:51:54 +00001269 AddPragmaHandler(new PragmaIncludeAliasHandler());
Aaron Ballman406ea512012-11-30 19:52:30 +00001270 AddPragmaHandler(new PragmaRegionHandler("region"));
1271 AddPragmaHandler(new PragmaRegionHandler("endregion"));
Chris Lattner30c924b2010-06-26 17:11:39 +00001272 }
Chris Lattnerb694ba72006-07-02 22:41:36 +00001273}