blob: 95e8a8ca8fc86189046d21fffe61b35f2e6c79be [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.
Douglas Gregorc7d65762010-09-09 22:45:38 +0000104void Preprocessor::HandlePragmaDirective(unsigned Introducer) {
Jordan Rosede1a2922012-06-08 18:06:21 +0000105 if (!PragmasEnabled)
106 return;
107
Chris Lattnerb694ba72006-07-02 22:41:36 +0000108 ++NumPragma;
Mike Stump11289f42009-09-09 15:08:12 +0000109
Chris Lattnerb694ba72006-07-02 22:41:36 +0000110 // Invoke the first level of pragma handlers which reads the namespace id.
Chris Lattner146762e2007-07-20 16:59:19 +0000111 Token Tok;
Douglas Gregorc7d65762010-09-09 22:45:38 +0000112 PragmaHandlers->HandlePragma(*this, PragmaIntroducerKind(Introducer), Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000113
Chris Lattnerb694ba72006-07-02 22:41:36 +0000114 // If the pragma handler didn't read the rest of the line, consume it now.
Peter Collingbourne2c9f9662011-02-22 13:49:00 +0000115 if ((CurTokenLexer && CurTokenLexer->isParsingPreprocessorDirective())
116 || (CurPPLexer && CurPPLexer->ParsingPreprocessorDirective))
Chris Lattnerb694ba72006-07-02 22:41:36 +0000117 DiscardUntilEndOfDirective();
118}
119
Argyrios Kyrtzidisf1b64c62012-04-03 16:47:40 +0000120namespace {
121/// \brief Helper class for \see Preprocessor::Handle_Pragma.
122class LexingFor_PragmaRAII {
123 Preprocessor &PP;
124 bool InMacroArgPreExpansion;
125 bool Failed;
126 Token &OutTok;
127 Token PragmaTok;
128
129public:
130 LexingFor_PragmaRAII(Preprocessor &PP, bool InMacroArgPreExpansion,
131 Token &Tok)
132 : PP(PP), InMacroArgPreExpansion(InMacroArgPreExpansion),
133 Failed(false), OutTok(Tok) {
134 if (InMacroArgPreExpansion) {
135 PragmaTok = OutTok;
136 PP.EnableBacktrackAtThisPos();
137 }
138 }
139
140 ~LexingFor_PragmaRAII() {
141 if (InMacroArgPreExpansion) {
142 if (Failed) {
143 PP.CommitBacktrackedTokens();
144 } else {
145 PP.Backtrack();
146 OutTok = PragmaTok;
147 }
148 }
149 }
150
151 void failed() {
152 Failed = true;
153 }
154};
155}
156
Chris Lattnerb694ba72006-07-02 22:41:36 +0000157/// Handle_Pragma - Read a _Pragma directive, slice it up, process it, then
158/// return the first token after the directive. The _Pragma token has just
159/// been read into 'Tok'.
Chris Lattner146762e2007-07-20 16:59:19 +0000160void Preprocessor::Handle_Pragma(Token &Tok) {
Argyrios Kyrtzidisf1b64c62012-04-03 16:47:40 +0000161
162 // This works differently if we are pre-expanding a macro argument.
163 // In that case we don't actually "activate" the pragma now, we only lex it
164 // until we are sure it is lexically correct and then we backtrack so that
165 // we activate the pragma whenever we encounter the tokens again in the token
166 // stream. This ensures that we will activate it in the correct location
167 // or that we will ignore it if it never enters the token stream, e.g:
168 //
169 // #define EMPTY(x)
170 // #define INACTIVE(x) EMPTY(x)
171 // INACTIVE(_Pragma("clang diagnostic ignored \"-Wconversion\""))
172
173 LexingFor_PragmaRAII _PragmaLexing(*this, InMacroArgPreExpansion, Tok);
174
Chris Lattnerb694ba72006-07-02 22:41:36 +0000175 // Remember the pragma token location.
176 SourceLocation PragmaLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000177
Chris Lattnerb694ba72006-07-02 22:41:36 +0000178 // Read the '('.
179 Lex(Tok);
Chris Lattner907dfe92008-11-18 07:59:24 +0000180 if (Tok.isNot(tok::l_paren)) {
181 Diag(PragmaLoc, diag::err__Pragma_malformed);
Argyrios Kyrtzidisf1b64c62012-04-03 16:47:40 +0000182 return _PragmaLexing.failed();
Chris Lattner907dfe92008-11-18 07:59:24 +0000183 }
Chris Lattnerb694ba72006-07-02 22:41:36 +0000184
185 // Read the '"..."'.
186 Lex(Tok);
Richard Smithc98bb4e2013-03-09 23:30:15 +0000187 if (!tok::isStringLiteral(Tok.getKind())) {
Chris Lattner907dfe92008-11-18 07:59:24 +0000188 Diag(PragmaLoc, diag::err__Pragma_malformed);
Richard Smithd67aea22012-03-06 03:21:47 +0000189 // Skip this token, and the ')', if present.
190 if (Tok.isNot(tok::r_paren))
191 Lex(Tok);
192 if (Tok.is(tok::r_paren))
193 Lex(Tok);
Argyrios Kyrtzidisf1b64c62012-04-03 16:47:40 +0000194 return _PragmaLexing.failed();
Richard Smithd67aea22012-03-06 03:21:47 +0000195 }
196
197 if (Tok.hasUDSuffix()) {
198 Diag(Tok, diag::err_invalid_string_udl);
199 // Skip this token, and the ')', if present.
200 Lex(Tok);
201 if (Tok.is(tok::r_paren))
202 Lex(Tok);
Argyrios Kyrtzidisf1b64c62012-04-03 16:47:40 +0000203 return _PragmaLexing.failed();
Chris Lattner907dfe92008-11-18 07:59:24 +0000204 }
Mike Stump11289f42009-09-09 15:08:12 +0000205
Chris Lattnerb694ba72006-07-02 22:41:36 +0000206 // Remember the string.
Argyrios Kyrtzidisf1b64c62012-04-03 16:47:40 +0000207 Token StrTok = Tok;
Chris Lattnerb694ba72006-07-02 22:41:36 +0000208
209 // Read the ')'.
210 Lex(Tok);
Chris Lattner907dfe92008-11-18 07:59:24 +0000211 if (Tok.isNot(tok::r_paren)) {
212 Diag(PragmaLoc, diag::err__Pragma_malformed);
Argyrios Kyrtzidisf1b64c62012-04-03 16:47:40 +0000213 return _PragmaLexing.failed();
Chris Lattner907dfe92008-11-18 07:59:24 +0000214 }
Mike Stump11289f42009-09-09 15:08:12 +0000215
Argyrios Kyrtzidisf1b64c62012-04-03 16:47:40 +0000216 if (InMacroArgPreExpansion)
217 return;
218
Chris Lattner9dc9c202009-02-15 20:52:18 +0000219 SourceLocation RParenLoc = Tok.getLocation();
Argyrios Kyrtzidisf1b64c62012-04-03 16:47:40 +0000220 std::string StrVal = getSpelling(StrTok);
Mike Stump11289f42009-09-09 15:08:12 +0000221
Richard Smithc98bb4e2013-03-09 23:30:15 +0000222 // The _Pragma is lexically sound. Destringize according to C11 6.10.9.1:
223 // "The string literal is destringized by deleting any encoding prefix,
Chris Lattner262d4e32009-01-16 18:59:23 +0000224 // deleting the leading and trailing double-quotes, replacing each escape
225 // sequence \" by a double-quote, and replacing each escape sequence \\ by a
226 // single backslash."
Richard Smithc98bb4e2013-03-09 23:30:15 +0000227 if (StrVal[0] == 'L' || StrVal[0] == 'U' ||
228 (StrVal[0] == 'u' && StrVal[1] != '8'))
Chris Lattnerb694ba72006-07-02 22:41:36 +0000229 StrVal.erase(StrVal.begin());
Richard Smithc98bb4e2013-03-09 23:30:15 +0000230 else if (StrVal[0] == 'u')
231 StrVal.erase(StrVal.begin(), StrVal.begin() + 2);
232
233 if (StrVal[0] == 'R') {
234 // FIXME: C++11 does not specify how to handle raw-string-literals here.
235 // We strip off the 'R', the quotes, the d-char-sequences, and the parens.
236 assert(StrVal[1] == '"' && StrVal[StrVal.size() - 1] == '"' &&
237 "Invalid raw string token!");
238
239 // Measure the length of the d-char-sequence.
240 unsigned NumDChars = 0;
241 while (StrVal[2 + NumDChars] != '(') {
242 assert(NumDChars < (StrVal.size() - 5) / 2 &&
243 "Invalid raw string token!");
244 ++NumDChars;
245 }
246 assert(StrVal[StrVal.size() - 2 - NumDChars] == ')');
247
248 // Remove 'R " d-char-sequence' and 'd-char-sequence "'. We'll replace the
249 // parens below.
250 StrVal.erase(0, 2 + NumDChars);
251 StrVal.erase(StrVal.size() - 1 - NumDChars);
252 } else {
253 assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
254 "Invalid string token!");
255
256 // Remove escaped quotes and escapes.
257 for (unsigned i = 1, e = StrVal.size(); i < e-2; ++i) {
258 if (StrVal[i] == '\\' &&
259 (StrVal[i+1] == '\\' || StrVal[i+1] == '"')) {
260 // \\ -> '\' and \" -> '"'.
261 StrVal.erase(StrVal.begin()+i);
262 --e;
263 }
264 }
265 }
Mike Stump11289f42009-09-09 15:08:12 +0000266
Chris Lattnerb694ba72006-07-02 22:41:36 +0000267 // Remove the front quote, replacing it with a space, so that the pragma
268 // contents appear to have a space before them.
269 StrVal[0] = ' ';
Mike Stump11289f42009-09-09 15:08:12 +0000270
Chris Lattnerfa217bd2009-03-08 08:08:45 +0000271 // Replace the terminating quote with a \n.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000272 StrVal[StrVal.size()-1] = '\n';
Mike Stump11289f42009-09-09 15:08:12 +0000273
Peter Collingbournef29ce972011-02-22 13:49:06 +0000274 // Plop the string (including the newline and trailing null) into a buffer
275 // where we can lex it.
276 Token TmpTok;
277 TmpTok.startToken();
Dmitri Gribenkob8e9e752012-09-24 21:07:17 +0000278 CreateString(StrVal, TmpTok);
Peter Collingbournef29ce972011-02-22 13:49:06 +0000279 SourceLocation TokLoc = TmpTok.getLocation();
280
281 // Make and enter a lexer object so that we lex and expand the tokens just
282 // like any others.
283 Lexer *TL = Lexer::Create_PragmaLexer(TokLoc, PragmaLoc, RParenLoc,
284 StrVal.size(), *this);
285
286 EnterSourceFileWithLexer(TL, 0);
287
288 // With everything set up, lex this as a #pragma directive.
289 HandlePragmaDirective(PIK__Pragma);
John McCall89e925d2010-08-28 22:34:47 +0000290
291 // Finally, return whatever came after the pragma directive.
292 return Lex(Tok);
293}
294
295/// HandleMicrosoft__pragma - Like Handle_Pragma except the pragma text
296/// is not enclosed within a string literal.
297void Preprocessor::HandleMicrosoft__pragma(Token &Tok) {
298 // Remember the pragma token location.
299 SourceLocation PragmaLoc = Tok.getLocation();
300
301 // Read the '('.
302 Lex(Tok);
303 if (Tok.isNot(tok::l_paren)) {
304 Diag(PragmaLoc, diag::err__Pragma_malformed);
305 return;
306 }
307
Peter Collingbournef29ce972011-02-22 13:49:06 +0000308 // Get the tokens enclosed within the __pragma(), as well as the final ')'.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000309 SmallVector<Token, 32> PragmaToks;
John McCall89e925d2010-08-28 22:34:47 +0000310 int NumParens = 0;
311 Lex(Tok);
312 while (Tok.isNot(tok::eof)) {
Peter Collingbournef29ce972011-02-22 13:49:06 +0000313 PragmaToks.push_back(Tok);
John McCall89e925d2010-08-28 22:34:47 +0000314 if (Tok.is(tok::l_paren))
315 NumParens++;
316 else if (Tok.is(tok::r_paren) && NumParens-- == 0)
317 break;
John McCall89e925d2010-08-28 22:34:47 +0000318 Lex(Tok);
319 }
320
John McCall49039d42010-08-29 01:09:54 +0000321 if (Tok.is(tok::eof)) {
322 Diag(PragmaLoc, diag::err_unterminated___pragma);
323 return;
324 }
325
Peter Collingbournef29ce972011-02-22 13:49:06 +0000326 PragmaToks.front().setFlag(Token::LeadingSpace);
John McCall89e925d2010-08-28 22:34:47 +0000327
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000328 // Replace the ')' with an EOD to mark the end of the pragma.
329 PragmaToks.back().setKind(tok::eod);
Peter Collingbournef29ce972011-02-22 13:49:06 +0000330
331 Token *TokArray = new Token[PragmaToks.size()];
332 std::copy(PragmaToks.begin(), PragmaToks.end(), TokArray);
333
334 // Push the tokens onto the stack.
335 EnterTokenStream(TokArray, PragmaToks.size(), true, true);
336
337 // With everything set up, lex this as a #pragma directive.
338 HandlePragmaDirective(PIK___pragma);
John McCall89e925d2010-08-28 22:34:47 +0000339
340 // Finally, return whatever came after the pragma directive.
341 return Lex(Tok);
342}
343
James Dennett18a6d792012-06-17 03:26:26 +0000344/// HandlePragmaOnce - Handle \#pragma once. OnceTok is the 'once'.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000345///
Chris Lattner146762e2007-07-20 16:59:19 +0000346void Preprocessor::HandlePragmaOnce(Token &OnceTok) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000347 if (isInPrimaryFile()) {
348 Diag(OnceTok, diag::pp_pragma_once_in_main_file);
349 return;
350 }
Mike Stump11289f42009-09-09 15:08:12 +0000351
Chris Lattnerb694ba72006-07-02 22:41:36 +0000352 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000353 // Mark the file as a once-only file now.
Chris Lattnerd32480d2009-01-17 06:22:33 +0000354 HeaderInfo.MarkFileIncludeOnce(getCurrentFileLexer()->getFileEntry());
Chris Lattnerb694ba72006-07-02 22:41:36 +0000355}
356
Chris Lattnerc2383312007-12-19 19:38:36 +0000357void Preprocessor::HandlePragmaMark() {
Ted Kremenek76c34412008-11-19 22:21:33 +0000358 assert(CurPPLexer && "No current lexer?");
Chris Lattnerd9efb6e2009-06-15 05:02:34 +0000359 if (CurLexer)
360 CurLexer->ReadToEndOfLine();
361 else
362 CurPTHLexer->DiscardToEndOfLine();
Chris Lattnerc2383312007-12-19 19:38:36 +0000363}
364
365
James Dennett18a6d792012-06-17 03:26:26 +0000366/// HandlePragmaPoison - Handle \#pragma GCC poison. PoisonTok is the 'poison'.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000367///
Chris Lattner146762e2007-07-20 16:59:19 +0000368void Preprocessor::HandlePragmaPoison(Token &PoisonTok) {
369 Token Tok;
Chris Lattner538d7f32006-07-20 04:31:52 +0000370
Chris Lattnerb694ba72006-07-02 22:41:36 +0000371 while (1) {
372 // Read the next token to poison. While doing this, pretend that we are
373 // skipping while reading the identifier to poison.
374 // This avoids errors on code like:
375 // #pragma GCC poison X
376 // #pragma GCC poison X
Ted Kremenek551c82a2008-11-18 01:12:54 +0000377 if (CurPPLexer) CurPPLexer->LexingRawMode = true;
Chris Lattnerb694ba72006-07-02 22:41:36 +0000378 LexUnexpandedToken(Tok);
Ted Kremenek551c82a2008-11-18 01:12:54 +0000379 if (CurPPLexer) CurPPLexer->LexingRawMode = false;
Mike Stump11289f42009-09-09 15:08:12 +0000380
Chris Lattnerb694ba72006-07-02 22:41:36 +0000381 // If we reached the end of line, we're done.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000382 if (Tok.is(tok::eod)) return;
Mike Stump11289f42009-09-09 15:08:12 +0000383
Chris Lattnerb694ba72006-07-02 22:41:36 +0000384 // Can only poison identifiers.
Abramo Bagnaraea4f7c72010-12-22 08:23:18 +0000385 if (Tok.isNot(tok::raw_identifier)) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000386 Diag(Tok, diag::err_pp_invalid_poison);
387 return;
388 }
Mike Stump11289f42009-09-09 15:08:12 +0000389
Chris Lattnercefc7682006-07-08 08:28:12 +0000390 // Look up the identifier info for the token. We disabled identifier lookup
391 // by saying we're skipping contents, so we need to do this manually.
392 IdentifierInfo *II = LookUpIdentifierInfo(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000393
Chris Lattnerb694ba72006-07-02 22:41:36 +0000394 // Already poisoned.
395 if (II->isPoisoned()) continue;
Mike Stump11289f42009-09-09 15:08:12 +0000396
Chris Lattnerb694ba72006-07-02 22:41:36 +0000397 // If this is a macro identifier, emit a warning.
Chris Lattner259716a2007-10-07 08:04:56 +0000398 if (II->hasMacroDefinition())
Chris Lattnerb694ba72006-07-02 22:41:36 +0000399 Diag(Tok, diag::pp_poisoning_existing_macro);
Mike Stump11289f42009-09-09 15:08:12 +0000400
Chris Lattnerb694ba72006-07-02 22:41:36 +0000401 // Finally, poison it!
402 II->setIsPoisoned();
Douglas Gregor935bc7a22011-10-27 09:33:13 +0000403 if (II->isFromAST())
404 II->setChangedSinceDeserialization();
Chris Lattnerb694ba72006-07-02 22:41:36 +0000405 }
406}
407
James Dennett18a6d792012-06-17 03:26:26 +0000408/// HandlePragmaSystemHeader - Implement \#pragma GCC system_header. We know
Chris Lattnerb694ba72006-07-02 22:41:36 +0000409/// that the whole directive has been parsed.
Chris Lattner146762e2007-07-20 16:59:19 +0000410void Preprocessor::HandlePragmaSystemHeader(Token &SysHeaderTok) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000411 if (isInPrimaryFile()) {
412 Diag(SysHeaderTok, diag::pp_pragma_sysheader_in_main_file);
413 return;
414 }
Mike Stump11289f42009-09-09 15:08:12 +0000415
Chris Lattnerb694ba72006-07-02 22:41:36 +0000416 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
Ted Kremenek300590b2008-11-20 01:45:11 +0000417 PreprocessorLexer *TheLexer = getCurrentFileLexer();
Mike Stump11289f42009-09-09 15:08:12 +0000418
Chris Lattnerb694ba72006-07-02 22:41:36 +0000419 // Mark the file as a system header.
Chris Lattnerd32480d2009-01-17 06:22:33 +0000420 HeaderInfo.MarkFileSystemHeader(TheLexer->getFileEntry());
Mike Stump11289f42009-09-09 15:08:12 +0000421
422
Chris Lattnerd9efb6e2009-06-15 05:02:34 +0000423 PresumedLoc PLoc = SourceMgr.getPresumedLoc(SysHeaderTok.getLocation());
Douglas Gregor453b0122010-11-12 07:15:47 +0000424 if (PLoc.isInvalid())
425 return;
426
Jay Foad9a6b0982011-06-21 15:13:30 +0000427 unsigned FilenameID = SourceMgr.getLineTableFilenameID(PLoc.getFilename());
Mike Stump11289f42009-09-09 15:08:12 +0000428
Chris Lattner3bdc7672011-05-22 22:10:16 +0000429 // Notify the client, if desired, that we are in a new source file.
430 if (Callbacks)
431 Callbacks->FileChanged(SysHeaderTok.getLocation(),
432 PPCallbacks::SystemHeaderPragma, SrcMgr::C_System);
433
Chris Lattnerd9efb6e2009-06-15 05:02:34 +0000434 // Emit a line marker. This will change any source locations from this point
435 // forward to realize they are in a system header.
436 // Create a line note with this information.
437 SourceMgr.AddLineNote(SysHeaderTok.getLocation(), PLoc.getLine(), FilenameID,
438 false, false, true, false);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000439}
440
James Dennett18a6d792012-06-17 03:26:26 +0000441/// HandlePragmaDependency - Handle \#pragma GCC dependency "foo" blah.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000442///
Chris Lattner146762e2007-07-20 16:59:19 +0000443void Preprocessor::HandlePragmaDependency(Token &DependencyTok) {
444 Token FilenameTok;
Ted Kremenek551c82a2008-11-18 01:12:54 +0000445 CurPPLexer->LexIncludeFilename(FilenameTok);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000446
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000447 // If the token kind is EOD, the error has already been diagnosed.
448 if (FilenameTok.is(tok::eod))
Chris Lattnerb694ba72006-07-02 22:41:36 +0000449 return;
Mike Stump11289f42009-09-09 15:08:12 +0000450
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000451 // Reserve a buffer to get the spelling.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000452 SmallString<128> FilenameBuffer;
Douglas Gregordc970f02010-03-16 22:30:13 +0000453 bool Invalid = false;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000454 StringRef Filename = getSpelling(FilenameTok, FilenameBuffer, &Invalid);
Douglas Gregordc970f02010-03-16 22:30:13 +0000455 if (Invalid)
456 return;
Mike Stump11289f42009-09-09 15:08:12 +0000457
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000458 bool isAngled =
459 GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000460 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
461 // error.
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000462 if (Filename.empty())
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000463 return;
Mike Stump11289f42009-09-09 15:08:12 +0000464
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000465 // Search include directories for this file.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000466 const DirectoryLookup *CurDir;
Douglas Gregor97eec242011-09-15 22:00:41 +0000467 const FileEntry *File = LookupFile(Filename, isAngled, 0, CurDir, NULL, NULL,
468 NULL);
Chris Lattner97b8e842008-11-18 08:02:48 +0000469 if (File == 0) {
Eli Friedman3781a362011-08-30 23:07:51 +0000470 if (!SuppressIncludeNotFoundError)
471 Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
Chris Lattner97b8e842008-11-18 08:02:48 +0000472 return;
473 }
Mike Stump11289f42009-09-09 15:08:12 +0000474
Chris Lattnerd32480d2009-01-17 06:22:33 +0000475 const FileEntry *CurFile = getCurrentFileLexer()->getFileEntry();
Chris Lattnerb694ba72006-07-02 22:41:36 +0000476
477 // If this file is older than the file it depends on, emit a diagnostic.
478 if (CurFile && CurFile->getModificationTime() < File->getModificationTime()) {
479 // Lex tokens at the end of the message and include them in the message.
480 std::string Message;
481 Lex(DependencyTok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000482 while (DependencyTok.isNot(tok::eod)) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000483 Message += getSpelling(DependencyTok) + " ";
484 Lex(DependencyTok);
485 }
Mike Stump11289f42009-09-09 15:08:12 +0000486
Chris Lattnerf0b04972010-09-05 23:16:09 +0000487 // Remove the trailing ' ' if present.
488 if (!Message.empty())
489 Message.erase(Message.end()-1);
Chris Lattner97b8e842008-11-18 08:02:48 +0000490 Diag(FilenameTok, diag::pp_out_of_date_dependency) << Message;
Chris Lattnerb694ba72006-07-02 22:41:36 +0000491 }
492}
493
James Dennett18a6d792012-06-17 03:26:26 +0000494/// \brief Handle the microsoft \#pragma comment extension.
495///
496/// The syntax is:
497/// \code
Dmitri Gribenko9ebd1612012-11-30 20:04:39 +0000498/// #pragma comment(linker, "foo")
James Dennett18a6d792012-06-17 03:26:26 +0000499/// \endcode
Chris Lattner2ff698d2009-01-16 08:21:25 +0000500/// 'linker' is one of five identifiers: compiler, exestr, lib, linker, user.
501/// "foo" is a string, which is fully macro expanded, and permits string
Gabor Greif31a082f2009-03-17 11:39:38 +0000502/// concatenation, embedded escape characters etc. See MSDN for more details.
Chris Lattner2ff698d2009-01-16 08:21:25 +0000503void Preprocessor::HandlePragmaComment(Token &Tok) {
504 SourceLocation CommentLoc = Tok.getLocation();
505 Lex(Tok);
506 if (Tok.isNot(tok::l_paren)) {
507 Diag(CommentLoc, diag::err_pragma_comment_malformed);
508 return;
509 }
Mike Stump11289f42009-09-09 15:08:12 +0000510
Chris Lattner2ff698d2009-01-16 08:21:25 +0000511 // Read the identifier.
512 Lex(Tok);
513 if (Tok.isNot(tok::identifier)) {
514 Diag(CommentLoc, diag::err_pragma_comment_malformed);
515 return;
516 }
Mike Stump11289f42009-09-09 15:08:12 +0000517
Chris Lattner2ff698d2009-01-16 08:21:25 +0000518 // Verify that this is one of the 5 whitelisted options.
519 // FIXME: warn that 'exestr' is deprecated.
520 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000521 if (!II->isStr("compiler") && !II->isStr("exestr") && !II->isStr("lib") &&
Chris Lattner2ff698d2009-01-16 08:21:25 +0000522 !II->isStr("linker") && !II->isStr("user")) {
523 Diag(Tok.getLocation(), diag::err_pragma_comment_unknown_kind);
524 return;
525 }
Mike Stump11289f42009-09-09 15:08:12 +0000526
Chris Lattner262d4e32009-01-16 18:59:23 +0000527 // Read the optional string if present.
Chris Lattner2ff698d2009-01-16 08:21:25 +0000528 Lex(Tok);
Chris Lattner262d4e32009-01-16 18:59:23 +0000529 std::string ArgumentString;
Andy Gibbs58905d22012-11-17 19:15:38 +0000530 if (Tok.is(tok::comma) && !LexStringLiteral(Tok, ArgumentString,
Andy Gibbsa8df57a2012-11-17 19:16:52 +0000531 "pragma comment",
Andy Gibbs58905d22012-11-17 19:15:38 +0000532 /*MacroExpansion=*/true))
533 return;
Mike Stump11289f42009-09-09 15:08:12 +0000534
Chris Lattner262d4e32009-01-16 18:59:23 +0000535 // FIXME: If the kind is "compiler" warn if the string is present (it is
536 // ignored).
537 // FIXME: 'lib' requires a comment string.
538 // FIXME: 'linker' requires a comment string, and has a specific list of
539 // things that are allowable.
Mike Stump11289f42009-09-09 15:08:12 +0000540
Chris Lattner2ff698d2009-01-16 08:21:25 +0000541 if (Tok.isNot(tok::r_paren)) {
542 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
543 return;
544 }
Chris Lattner262d4e32009-01-16 18:59:23 +0000545 Lex(Tok); // eat the r_paren.
Chris Lattner2ff698d2009-01-16 08:21:25 +0000546
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000547 if (Tok.isNot(tok::eod)) {
Chris Lattner2ff698d2009-01-16 08:21:25 +0000548 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
549 return;
550 }
Mike Stump11289f42009-09-09 15:08:12 +0000551
Chris Lattner262d4e32009-01-16 18:59:23 +0000552 // If the pragma is lexically sound, notify any interested PPCallbacks.
Chris Lattnerf49775d2009-01-16 19:01:46 +0000553 if (Callbacks)
554 Callbacks->PragmaComment(CommentLoc, II, ArgumentString);
Chris Lattner2ff698d2009-01-16 08:21:25 +0000555}
556
James Dennett18a6d792012-06-17 03:26:26 +0000557/// HandlePragmaMessage - Handle the microsoft and gcc \#pragma message
Michael J. Spencera0a820f2010-09-27 06:19:02 +0000558/// extension. The syntax is:
James Dennett18a6d792012-06-17 03:26:26 +0000559/// \code
Dmitri Gribenko9ebd1612012-11-30 20:04:39 +0000560/// #pragma message(string)
James Dennett18a6d792012-06-17 03:26:26 +0000561/// \endcode
Michael J. Spencera0a820f2010-09-27 06:19:02 +0000562/// OR, in GCC mode:
James Dennett18a6d792012-06-17 03:26:26 +0000563/// \code
Dmitri Gribenko9ebd1612012-11-30 20:04:39 +0000564/// #pragma message string
James Dennett18a6d792012-06-17 03:26:26 +0000565/// \endcode
Michael J. Spencera0a820f2010-09-27 06:19:02 +0000566/// string is a string, which is fully macro expanded, and permits string
567/// concatenation, embedded escape characters, etc... See MSDN for more details.
Chris Lattner30c924b2010-06-26 17:11:39 +0000568void Preprocessor::HandlePragmaMessage(Token &Tok) {
569 SourceLocation MessageLoc = Tok.getLocation();
570 Lex(Tok);
Michael J. Spencera0a820f2010-09-27 06:19:02 +0000571 bool ExpectClosingParen = false;
Michael J. Spencer4362a1c2010-09-27 06:34:47 +0000572 switch (Tok.getKind()) {
Michael J. Spencera0a820f2010-09-27 06:19:02 +0000573 case tok::l_paren:
574 // We have a MSVC style pragma message.
575 ExpectClosingParen = true;
576 // Read the string.
577 Lex(Tok);
578 break;
579 case tok::string_literal:
580 // We have a GCC style pragma message, and we just read the string.
581 break;
582 default:
Chris Lattner30c924b2010-06-26 17:11:39 +0000583 Diag(MessageLoc, diag::err_pragma_message_malformed);
584 return;
585 }
Chris Lattner2ff698d2009-01-16 08:21:25 +0000586
Andy Gibbs58905d22012-11-17 19:15:38 +0000587 std::string MessageString;
Andy Gibbsa8df57a2012-11-17 19:16:52 +0000588 if (!FinishLexStringLiteral(Tok, MessageString, "pragma message",
589 /*MacroExpansion=*/true))
Chris Lattner30c924b2010-06-26 17:11:39 +0000590 return;
Chris Lattner30c924b2010-06-26 17:11:39 +0000591
Michael J. Spencera0a820f2010-09-27 06:19:02 +0000592 if (ExpectClosingParen) {
593 if (Tok.isNot(tok::r_paren)) {
594 Diag(Tok.getLocation(), diag::err_pragma_message_malformed);
595 return;
596 }
597 Lex(Tok); // eat the r_paren.
Chris Lattner30c924b2010-06-26 17:11:39 +0000598 }
Chris Lattner30c924b2010-06-26 17:11:39 +0000599
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000600 if (Tok.isNot(tok::eod)) {
Chris Lattner30c924b2010-06-26 17:11:39 +0000601 Diag(Tok.getLocation(), diag::err_pragma_message_malformed);
602 return;
603 }
604
605 // Output the message.
606 Diag(MessageLoc, diag::warn_pragma_message) << MessageString;
607
608 // If the pragma is lexically sound, notify any interested PPCallbacks.
609 if (Callbacks)
610 Callbacks->PragmaMessage(MessageLoc, MessageString);
611}
Chris Lattner2ff698d2009-01-16 08:21:25 +0000612
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000613/// ParsePragmaPushOrPopMacro - Handle parsing of pragma push_macro/pop_macro.
614/// Return the IdentifierInfo* associated with the macro to push or pop.
615IdentifierInfo *Preprocessor::ParsePragmaPushOrPopMacro(Token &Tok) {
616 // Remember the pragma token location.
617 Token PragmaTok = Tok;
618
619 // Read the '('.
620 Lex(Tok);
621 if (Tok.isNot(tok::l_paren)) {
622 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
623 << getSpelling(PragmaTok);
624 return 0;
625 }
626
627 // Read the macro name string.
628 Lex(Tok);
629 if (Tok.isNot(tok::string_literal)) {
630 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
631 << getSpelling(PragmaTok);
632 return 0;
633 }
634
Richard Smithd67aea22012-03-06 03:21:47 +0000635 if (Tok.hasUDSuffix()) {
636 Diag(Tok, diag::err_invalid_string_udl);
637 return 0;
638 }
639
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000640 // Remember the macro string.
641 std::string StrVal = getSpelling(Tok);
642
643 // Read the ')'.
644 Lex(Tok);
645 if (Tok.isNot(tok::r_paren)) {
646 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
647 << getSpelling(PragmaTok);
648 return 0;
649 }
650
651 assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
652 "Invalid string token!");
653
654 // Create a Token from the string.
655 Token MacroTok;
656 MacroTok.startToken();
Abramo Bagnaraea4f7c72010-12-22 08:23:18 +0000657 MacroTok.setKind(tok::raw_identifier);
Dmitri Gribenkob8e9e752012-09-24 21:07:17 +0000658 CreateString(StringRef(&StrVal[1], StrVal.size() - 2), MacroTok);
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000659
660 // Get the IdentifierInfo of MacroToPushTok.
661 return LookUpIdentifierInfo(MacroTok);
662}
663
James Dennett18a6d792012-06-17 03:26:26 +0000664/// \brief Handle \#pragma push_macro.
665///
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000666/// The syntax is:
James Dennett18a6d792012-06-17 03:26:26 +0000667/// \code
Dmitri Gribenko9ebd1612012-11-30 20:04:39 +0000668/// #pragma push_macro("macro")
James Dennett18a6d792012-06-17 03:26:26 +0000669/// \endcode
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000670void Preprocessor::HandlePragmaPushMacro(Token &PushMacroTok) {
671 // Parse the pragma directive and get the macro IdentifierInfo*.
672 IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PushMacroTok);
673 if (!IdentInfo) return;
674
675 // Get the MacroInfo associated with IdentInfo.
676 MacroInfo *MI = getMacroInfo(IdentInfo);
677
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000678 if (MI) {
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000679 // Allow the original MacroInfo to be redefined later.
680 MI->setIsAllowRedefinitionsWithoutWarning(true);
681 }
682
683 // Push the cloned MacroInfo so we can retrieve it later.
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +0000684 PragmaPushMacroInfo[IdentInfo].push_back(MI);
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000685}
686
James Dennett18a6d792012-06-17 03:26:26 +0000687/// \brief Handle \#pragma pop_macro.
688///
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000689/// The syntax is:
James Dennett18a6d792012-06-17 03:26:26 +0000690/// \code
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000691/// #pragma pop_macro("macro")
James Dennett18a6d792012-06-17 03:26:26 +0000692/// \endcode
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000693void Preprocessor::HandlePragmaPopMacro(Token &PopMacroTok) {
694 SourceLocation MessageLoc = PopMacroTok.getLocation();
695
696 // Parse the pragma directive and get the macro IdentifierInfo*.
697 IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PopMacroTok);
698 if (!IdentInfo) return;
699
700 // Find the vector<MacroInfo*> associated with the macro.
701 llvm::DenseMap<IdentifierInfo*, std::vector<MacroInfo*> >::iterator iter =
702 PragmaPushMacroInfo.find(IdentInfo);
703 if (iter != PragmaPushMacroInfo.end()) {
Alexander Kornienko8b3f6232012-08-29 00:20:03 +0000704 // Forget the MacroInfo currently associated with IdentInfo.
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +0000705 if (MacroDirective *CurrentMD = getMacroDirective(IdentInfo)) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +0000706 MacroInfo *MI = CurrentMD->getMacroInfo();
707 if (MI->isWarnIfUnused())
708 WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());
709 appendMacroDirective(IdentInfo, AllocateUndefMacroDirective(MessageLoc));
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000710 }
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000711
712 // Get the MacroInfo we want to reinstall.
713 MacroInfo *MacroToReInstall = iter->second.back();
714
Alexander Kornienkoc0b49282012-08-29 16:56:24 +0000715 if (MacroToReInstall) {
716 // Reinstall the previously pushed macro.
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +0000717 appendDefMacroDirective(IdentInfo, MacroToReInstall, MessageLoc,
718 /*isImported=*/false);
Alexander Kornienkoc0b49282012-08-29 16:56:24 +0000719 }
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000720
721 // Pop PragmaPushMacroInfo stack.
722 iter->second.pop_back();
723 if (iter->second.size() == 0)
724 PragmaPushMacroInfo.erase(iter);
725 } else {
726 Diag(MessageLoc, diag::warn_pragma_pop_macro_no_push)
727 << IdentInfo->getName();
728 }
729}
Chris Lattnerb694ba72006-07-02 22:41:36 +0000730
Aaron Ballman611306e2012-03-02 22:51:54 +0000731void Preprocessor::HandlePragmaIncludeAlias(Token &Tok) {
732 // We will either get a quoted filename or a bracketed filename, and we
733 // have to track which we got. The first filename is the source name,
734 // and the second name is the mapped filename. If the first is quoted,
735 // the second must be as well (cannot mix and match quotes and brackets).
Aaron Ballman611306e2012-03-02 22:51:54 +0000736
737 // Get the open paren
738 Lex(Tok);
739 if (Tok.isNot(tok::l_paren)) {
740 Diag(Tok, diag::warn_pragma_include_alias_expected) << "(";
741 return;
742 }
743
744 // We expect either a quoted string literal, or a bracketed name
745 Token SourceFilenameTok;
746 CurPPLexer->LexIncludeFilename(SourceFilenameTok);
747 if (SourceFilenameTok.is(tok::eod)) {
748 // The diagnostic has already been handled
749 return;
750 }
751
752 StringRef SourceFileName;
753 SmallString<128> FileNameBuffer;
754 if (SourceFilenameTok.is(tok::string_literal) ||
755 SourceFilenameTok.is(tok::angle_string_literal)) {
756 SourceFileName = getSpelling(SourceFilenameTok, FileNameBuffer);
757 } else if (SourceFilenameTok.is(tok::less)) {
758 // This could be a path instead of just a name
759 FileNameBuffer.push_back('<');
760 SourceLocation End;
761 if (ConcatenateIncludeName(FileNameBuffer, End))
762 return; // Diagnostic already emitted
763 SourceFileName = FileNameBuffer.str();
764 } else {
765 Diag(Tok, diag::warn_pragma_include_alias_expected_filename);
766 return;
767 }
768 FileNameBuffer.clear();
769
770 // Now we expect a comma, followed by another include name
771 Lex(Tok);
772 if (Tok.isNot(tok::comma)) {
773 Diag(Tok, diag::warn_pragma_include_alias_expected) << ",";
774 return;
775 }
776
777 Token ReplaceFilenameTok;
778 CurPPLexer->LexIncludeFilename(ReplaceFilenameTok);
779 if (ReplaceFilenameTok.is(tok::eod)) {
780 // The diagnostic has already been handled
781 return;
782 }
783
784 StringRef ReplaceFileName;
785 if (ReplaceFilenameTok.is(tok::string_literal) ||
786 ReplaceFilenameTok.is(tok::angle_string_literal)) {
787 ReplaceFileName = getSpelling(ReplaceFilenameTok, FileNameBuffer);
788 } else if (ReplaceFilenameTok.is(tok::less)) {
789 // This could be a path instead of just a name
790 FileNameBuffer.push_back('<');
791 SourceLocation End;
792 if (ConcatenateIncludeName(FileNameBuffer, End))
793 return; // Diagnostic already emitted
794 ReplaceFileName = FileNameBuffer.str();
795 } else {
796 Diag(Tok, diag::warn_pragma_include_alias_expected_filename);
797 return;
798 }
799
800 // Finally, we expect the closing paren
801 Lex(Tok);
802 if (Tok.isNot(tok::r_paren)) {
803 Diag(Tok, diag::warn_pragma_include_alias_expected) << ")";
804 return;
805 }
806
807 // Now that we have the source and target filenames, we need to make sure
808 // they're both of the same type (angled vs non-angled)
809 StringRef OriginalSource = SourceFileName;
810
811 bool SourceIsAngled =
812 GetIncludeFilenameSpelling(SourceFilenameTok.getLocation(),
813 SourceFileName);
814 bool ReplaceIsAngled =
815 GetIncludeFilenameSpelling(ReplaceFilenameTok.getLocation(),
816 ReplaceFileName);
817 if (!SourceFileName.empty() && !ReplaceFileName.empty() &&
818 (SourceIsAngled != ReplaceIsAngled)) {
819 unsigned int DiagID;
820 if (SourceIsAngled)
821 DiagID = diag::warn_pragma_include_alias_mismatch_angle;
822 else
823 DiagID = diag::warn_pragma_include_alias_mismatch_quote;
824
825 Diag(SourceFilenameTok.getLocation(), DiagID)
826 << SourceFileName
827 << ReplaceFileName;
828
829 return;
830 }
831
832 // Now we can let the include handler know about this mapping
833 getHeaderSearchInfo().AddIncludeAlias(OriginalSource, ReplaceFileName);
834}
835
Chris Lattnerb694ba72006-07-02 22:41:36 +0000836/// AddPragmaHandler - Add the specified pragma handler to the preprocessor.
837/// If 'Namespace' is non-null, then it is a token required to exist on the
838/// pragma line before the pragma string starts, e.g. "STDC" or "GCC".
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000839void Preprocessor::AddPragmaHandler(StringRef Namespace,
Chris Lattnerb694ba72006-07-02 22:41:36 +0000840 PragmaHandler *Handler) {
841 PragmaNamespace *InsertNS = PragmaHandlers;
Mike Stump11289f42009-09-09 15:08:12 +0000842
Chris Lattnerb694ba72006-07-02 22:41:36 +0000843 // If this is specified to be in a namespace, step down into it.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000844 if (!Namespace.empty()) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000845 // If there is already a pragma handler with the name of this namespace,
846 // we either have an error (directive with the same name as a namespace) or
847 // we already have the namespace to insert into.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000848 if (PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace)) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000849 InsertNS = Existing->getIfNamespace();
850 assert(InsertNS != 0 && "Cannot have a pragma namespace and pragma"
851 " handler with the same name!");
852 } else {
853 // Otherwise, this namespace doesn't exist yet, create and insert the
854 // handler for it.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000855 InsertNS = new PragmaNamespace(Namespace);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000856 PragmaHandlers->AddPragma(InsertNS);
857 }
858 }
Mike Stump11289f42009-09-09 15:08:12 +0000859
Chris Lattnerb694ba72006-07-02 22:41:36 +0000860 // Check to make sure we don't already have a pragma for this identifier.
861 assert(!InsertNS->FindHandler(Handler->getName()) &&
862 "Pragma handler already exists for this identifier!");
863 InsertNS->AddPragma(Handler);
864}
865
Daniel Dunbar40596532008-10-04 19:17:46 +0000866/// RemovePragmaHandler - Remove the specific pragma handler from the
867/// preprocessor. If \arg Namespace is non-null, then it should be the
868/// namespace that \arg Handler was added to. It is an error to remove
869/// a handler that has not been registered.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000870void Preprocessor::RemovePragmaHandler(StringRef Namespace,
Daniel Dunbar40596532008-10-04 19:17:46 +0000871 PragmaHandler *Handler) {
872 PragmaNamespace *NS = PragmaHandlers;
Mike Stump11289f42009-09-09 15:08:12 +0000873
Daniel Dunbar40596532008-10-04 19:17:46 +0000874 // If this is specified to be in a namespace, step down into it.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000875 if (!Namespace.empty()) {
876 PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace);
Daniel Dunbar40596532008-10-04 19:17:46 +0000877 assert(Existing && "Namespace containing handler does not exist!");
878
879 NS = Existing->getIfNamespace();
880 assert(NS && "Invalid namespace, registered as a regular pragma handler!");
881 }
882
883 NS->RemovePragmaHandler(Handler);
Mike Stump11289f42009-09-09 15:08:12 +0000884
Daniel Dunbar40596532008-10-04 19:17:46 +0000885 // If this is a non-default namespace and it is now empty, remove
886 // it.
Argyrios Kyrtzidis7ce75262012-01-06 00:22:09 +0000887 if (NS != PragmaHandlers && NS->IsEmpty()) {
Daniel Dunbar40596532008-10-04 19:17:46 +0000888 PragmaHandlers->RemovePragmaHandler(NS);
Argyrios Kyrtzidis7ce75262012-01-06 00:22:09 +0000889 delete NS;
890 }
Daniel Dunbar40596532008-10-04 19:17:46 +0000891}
892
Peter Collingbourne3bffa522011-02-14 01:42:24 +0000893bool Preprocessor::LexOnOffSwitch(tok::OnOffSwitch &Result) {
894 Token Tok;
895 LexUnexpandedToken(Tok);
896
897 if (Tok.isNot(tok::identifier)) {
898 Diag(Tok, diag::ext_on_off_switch_syntax);
899 return true;
900 }
901 IdentifierInfo *II = Tok.getIdentifierInfo();
902 if (II->isStr("ON"))
903 Result = tok::OOS_ON;
904 else if (II->isStr("OFF"))
905 Result = tok::OOS_OFF;
906 else if (II->isStr("DEFAULT"))
907 Result = tok::OOS_DEFAULT;
908 else {
909 Diag(Tok, diag::ext_on_off_switch_syntax);
910 return true;
911 }
912
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000913 // Verify that this is followed by EOD.
Peter Collingbourne3bffa522011-02-14 01:42:24 +0000914 LexUnexpandedToken(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000915 if (Tok.isNot(tok::eod))
916 Diag(Tok, diag::ext_pragma_syntax_eod);
Peter Collingbourne3bffa522011-02-14 01:42:24 +0000917 return false;
918}
919
Chris Lattnerb694ba72006-07-02 22:41:36 +0000920namespace {
James Dennett18a6d792012-06-17 03:26:26 +0000921/// PragmaOnceHandler - "\#pragma once" marks the file as atomically included.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000922struct PragmaOnceHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000923 PragmaOnceHandler() : PragmaHandler("once") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000924 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
925 Token &OnceTok) {
Chris Lattnerce2ab6f2009-04-14 05:07:49 +0000926 PP.CheckEndOfDirective("pragma once");
Chris Lattnerb694ba72006-07-02 22:41:36 +0000927 PP.HandlePragmaOnce(OnceTok);
928 }
929};
930
James Dennett18a6d792012-06-17 03:26:26 +0000931/// PragmaMarkHandler - "\#pragma mark ..." is ignored by the compiler, and the
Chris Lattnerc2383312007-12-19 19:38:36 +0000932/// rest of the line is not lexed.
933struct PragmaMarkHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000934 PragmaMarkHandler() : PragmaHandler("mark") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000935 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
936 Token &MarkTok) {
Chris Lattnerc2383312007-12-19 19:38:36 +0000937 PP.HandlePragmaMark();
938 }
939};
940
James Dennett18a6d792012-06-17 03:26:26 +0000941/// PragmaPoisonHandler - "\#pragma poison x" marks x as not usable.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000942struct PragmaPoisonHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000943 PragmaPoisonHandler() : PragmaHandler("poison") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000944 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
945 Token &PoisonTok) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000946 PP.HandlePragmaPoison(PoisonTok);
947 }
948};
949
James Dennett18a6d792012-06-17 03:26:26 +0000950/// PragmaSystemHeaderHandler - "\#pragma system_header" marks the current file
Chris Lattnerc2383312007-12-19 19:38:36 +0000951/// as a system header, which silences warnings in it.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000952struct PragmaSystemHeaderHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000953 PragmaSystemHeaderHandler() : PragmaHandler("system_header") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000954 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
955 Token &SHToken) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000956 PP.HandlePragmaSystemHeader(SHToken);
Chris Lattnerce2ab6f2009-04-14 05:07:49 +0000957 PP.CheckEndOfDirective("pragma");
Chris Lattnerb694ba72006-07-02 22:41:36 +0000958 }
959};
960struct PragmaDependencyHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000961 PragmaDependencyHandler() : PragmaHandler("dependency") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000962 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
963 Token &DepToken) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000964 PP.HandlePragmaDependency(DepToken);
965 }
966};
Mike Stump11289f42009-09-09 15:08:12 +0000967
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000968struct PragmaDebugHandler : public PragmaHandler {
969 PragmaDebugHandler() : PragmaHandler("__debug") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000970 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
971 Token &DepToken) {
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000972 Token Tok;
973 PP.LexUnexpandedToken(Tok);
974 if (Tok.isNot(tok::identifier)) {
Douglas Gregor3cc26482010-08-30 15:15:34 +0000975 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000976 return;
977 }
978 IdentifierInfo *II = Tok.getIdentifierInfo();
979
Daniel Dunbarf2cf3292010-08-17 22:32:48 +0000980 if (II->isStr("assert")) {
David Blaikie83d382b2011-09-23 05:06:16 +0000981 llvm_unreachable("This is an assertion!");
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000982 } else if (II->isStr("crash")) {
David Blaikie5bd4c2a2012-08-21 18:56:49 +0000983 LLVM_BUILTIN_TRAP;
David Blaikie5d577a22012-06-29 22:03:56 +0000984 } else if (II->isStr("parser_crash")) {
985 Token Crasher;
986 Crasher.setKind(tok::annot_pragma_parser_crash);
987 PP.EnterToken(Crasher);
Daniel Dunbarf2cf3292010-08-17 22:32:48 +0000988 } else if (II->isStr("llvm_fatal_error")) {
989 llvm::report_fatal_error("#pragma clang __debug llvm_fatal_error");
990 } else if (II->isStr("llvm_unreachable")) {
991 llvm_unreachable("#pragma clang __debug llvm_unreachable");
992 } else if (II->isStr("overflow_stack")) {
993 DebugOverflowStack();
Daniel Dunbar211a7872010-08-18 23:09:23 +0000994 } else if (II->isStr("handle_crash")) {
995 llvm::CrashRecoveryContext *CRC =llvm::CrashRecoveryContext::GetCurrent();
996 if (CRC)
997 CRC->HandleCrash();
Daniel Dunbarf2cf3292010-08-17 22:32:48 +0000998 } else {
999 PP.Diag(Tok, diag::warn_pragma_debug_unexpected_command)
1000 << II->getName();
Daniel Dunbarb8068c32010-07-28 15:40:33 +00001001 }
1002 }
1003
Francois Pichet2e11f5d2011-05-25 16:15:03 +00001004// Disable MSVC warning about runtime stack overflow.
1005#ifdef _MSC_VER
1006 #pragma warning(disable : 4717)
1007#endif
Daniel Dunbarb8068c32010-07-28 15:40:33 +00001008 void DebugOverflowStack() {
1009 DebugOverflowStack();
1010 }
Francois Pichet2e11f5d2011-05-25 16:15:03 +00001011#ifdef _MSC_VER
1012 #pragma warning(default : 4717)
1013#endif
1014
Daniel Dunbarb8068c32010-07-28 15:40:33 +00001015};
1016
James Dennett18a6d792012-06-17 03:26:26 +00001017/// PragmaDiagnosticHandler - e.g. '\#pragma GCC diagnostic ignored "-Wformat"'
Chris Lattner504af112009-04-19 23:16:58 +00001018struct PragmaDiagnosticHandler : public PragmaHandler {
Douglas Gregor3bde9b12011-06-22 19:41:48 +00001019private:
1020 const char *Namespace;
Chris Lattnerfb42a182009-07-12 21:18:45 +00001021public:
Douglas Gregor3bde9b12011-06-22 19:41:48 +00001022 explicit PragmaDiagnosticHandler(const char *NS) :
1023 PragmaHandler("diagnostic"), Namespace(NS) {}
Douglas Gregorc7d65762010-09-09 22:45:38 +00001024 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1025 Token &DiagToken) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +00001026 SourceLocation DiagLoc = DiagToken.getLocation();
Chris Lattner504af112009-04-19 23:16:58 +00001027 Token Tok;
1028 PP.LexUnexpandedToken(Tok);
1029 if (Tok.isNot(tok::identifier)) {
Douglas Gregor3cc26482010-08-30 15:15:34 +00001030 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Chris Lattner504af112009-04-19 23:16:58 +00001031 return;
1032 }
1033 IdentifierInfo *II = Tok.getIdentifierInfo();
Douglas Gregor3bde9b12011-06-22 19:41:48 +00001034 PPCallbacks *Callbacks = PP.getPPCallbacks();
Mike Stump11289f42009-09-09 15:08:12 +00001035
Chris Lattner504af112009-04-19 23:16:58 +00001036 diag::Mapping Map;
1037 if (II->isStr("warning"))
1038 Map = diag::MAP_WARNING;
1039 else if (II->isStr("error"))
1040 Map = diag::MAP_ERROR;
1041 else if (II->isStr("ignored"))
1042 Map = diag::MAP_IGNORE;
1043 else if (II->isStr("fatal"))
1044 Map = diag::MAP_FATAL;
Douglas Gregor3cc26482010-08-30 15:15:34 +00001045 else if (II->isStr("pop")) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +00001046 if (!PP.getDiagnostics().popMappings(DiagLoc))
Douglas Gregor3cc26482010-08-30 15:15:34 +00001047 PP.Diag(Tok, diag::warn_pragma_diagnostic_cannot_pop);
Douglas Gregor3bde9b12011-06-22 19:41:48 +00001048 else if (Callbacks)
1049 Callbacks->PragmaDiagnosticPop(DiagLoc, Namespace);
Douglas Gregor3cc26482010-08-30 15:15:34 +00001050 return;
1051 } else if (II->isStr("push")) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +00001052 PP.getDiagnostics().pushMappings(DiagLoc);
Douglas Gregor3bde9b12011-06-22 19:41:48 +00001053 if (Callbacks)
1054 Callbacks->PragmaDiagnosticPush(DiagLoc, Namespace);
Chris Lattnerfb42a182009-07-12 21:18:45 +00001055 return;
1056 } else {
Douglas Gregor3cc26482010-08-30 15:15:34 +00001057 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Chris Lattner504af112009-04-19 23:16:58 +00001058 return;
1059 }
Mike Stump11289f42009-09-09 15:08:12 +00001060
Chris Lattner504af112009-04-19 23:16:58 +00001061 PP.LexUnexpandedToken(Tok);
Andy Gibbs58905d22012-11-17 19:15:38 +00001062 SourceLocation StringLoc = Tok.getLocation();
Chris Lattner504af112009-04-19 23:16:58 +00001063
Andy Gibbs58905d22012-11-17 19:15:38 +00001064 std::string WarningName;
Andy Gibbsa8df57a2012-11-17 19:16:52 +00001065 if (!PP.FinishLexStringLiteral(Tok, WarningName, "pragma diagnostic",
1066 /*MacroExpansion=*/false))
Chris Lattner504af112009-04-19 23:16:58 +00001067 return;
Mike Stump11289f42009-09-09 15:08:12 +00001068
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001069 if (Tok.isNot(tok::eod)) {
Chris Lattner504af112009-04-19 23:16:58 +00001070 PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token);
1071 return;
1072 }
Mike Stump11289f42009-09-09 15:08:12 +00001073
Chris Lattner504af112009-04-19 23:16:58 +00001074 if (WarningName.size() < 3 || WarningName[0] != '-' ||
1075 WarningName[1] != 'W') {
Andy Gibbs58905d22012-11-17 19:15:38 +00001076 PP.Diag(StringLoc, diag::warn_pragma_diagnostic_invalid_option);
Chris Lattner504af112009-04-19 23:16:58 +00001077 return;
1078 }
Mike Stump11289f42009-09-09 15:08:12 +00001079
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +00001080 if (PP.getDiagnostics().setDiagnosticGroupMapping(WarningName.substr(2),
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +00001081 Map, DiagLoc))
Andy Gibbs58905d22012-11-17 19:15:38 +00001082 PP.Diag(StringLoc, diag::warn_pragma_diagnostic_unknown_warning)
1083 << WarningName;
Douglas Gregor3bde9b12011-06-22 19:41:48 +00001084 else if (Callbacks)
1085 Callbacks->PragmaDiagnostic(DiagLoc, Namespace, Map, WarningName);
Chris Lattner504af112009-04-19 23:16:58 +00001086 }
1087};
Mike Stump11289f42009-09-09 15:08:12 +00001088
James Dennett18a6d792012-06-17 03:26:26 +00001089/// PragmaCommentHandler - "\#pragma comment ...".
Chris Lattner2ff698d2009-01-16 08:21:25 +00001090struct PragmaCommentHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001091 PragmaCommentHandler() : PragmaHandler("comment") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +00001092 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1093 Token &CommentTok) {
Chris Lattner2ff698d2009-01-16 08:21:25 +00001094 PP.HandlePragmaComment(CommentTok);
1095 }
1096};
Mike Stump11289f42009-09-09 15:08:12 +00001097
James Dennett18a6d792012-06-17 03:26:26 +00001098/// PragmaIncludeAliasHandler - "\#pragma include_alias("...")".
Aaron Ballman611306e2012-03-02 22:51:54 +00001099struct PragmaIncludeAliasHandler : public PragmaHandler {
1100 PragmaIncludeAliasHandler() : PragmaHandler("include_alias") {}
1101 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1102 Token &IncludeAliasTok) {
1103 PP.HandlePragmaIncludeAlias(IncludeAliasTok);
1104 }
1105};
1106
James Dennett18a6d792012-06-17 03:26:26 +00001107/// PragmaMessageHandler - "\#pragma message("...")".
Chris Lattner30c924b2010-06-26 17:11:39 +00001108struct PragmaMessageHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001109 PragmaMessageHandler() : PragmaHandler("message") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +00001110 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1111 Token &CommentTok) {
Chris Lattner30c924b2010-06-26 17:11:39 +00001112 PP.HandlePragmaMessage(CommentTok);
1113 }
1114};
1115
James Dennett18a6d792012-06-17 03:26:26 +00001116/// PragmaPushMacroHandler - "\#pragma push_macro" saves the value of the
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001117/// macro on the top of the stack.
1118struct PragmaPushMacroHandler : public PragmaHandler {
1119 PragmaPushMacroHandler() : PragmaHandler("push_macro") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +00001120 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1121 Token &PushMacroTok) {
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001122 PP.HandlePragmaPushMacro(PushMacroTok);
1123 }
1124};
1125
1126
James Dennett18a6d792012-06-17 03:26:26 +00001127/// PragmaPopMacroHandler - "\#pragma pop_macro" sets the value of the
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001128/// macro to the value on the top of the stack.
1129struct PragmaPopMacroHandler : public PragmaHandler {
1130 PragmaPopMacroHandler() : PragmaHandler("pop_macro") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +00001131 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1132 Token &PopMacroTok) {
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001133 PP.HandlePragmaPopMacro(PopMacroTok);
1134 }
1135};
1136
Chris Lattner958ee042009-04-19 21:20:35 +00001137// Pragma STDC implementations.
Chris Lattner02ef4e32009-04-19 21:50:08 +00001138
James Dennett18a6d792012-06-17 03:26:26 +00001139/// PragmaSTDC_FENV_ACCESSHandler - "\#pragma STDC FENV_ACCESS ...".
Chris Lattner958ee042009-04-19 21:20:35 +00001140struct PragmaSTDC_FENV_ACCESSHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001141 PragmaSTDC_FENV_ACCESSHandler() : PragmaHandler("FENV_ACCESS") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +00001142 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1143 Token &Tok) {
Peter Collingbourne3bffa522011-02-14 01:42:24 +00001144 tok::OnOffSwitch OOS;
1145 if (PP.LexOnOffSwitch(OOS))
1146 return;
1147 if (OOS == tok::OOS_ON)
Chris Lattnerdf222682009-04-19 21:55:32 +00001148 PP.Diag(Tok, diag::warn_stdc_fenv_access_not_supported);
Chris Lattner958ee042009-04-19 21:20:35 +00001149 }
1150};
Mike Stump11289f42009-09-09 15:08:12 +00001151
James Dennett18a6d792012-06-17 03:26:26 +00001152/// PragmaSTDC_CX_LIMITED_RANGEHandler - "\#pragma STDC CX_LIMITED_RANGE ...".
Chris Lattner958ee042009-04-19 21:20:35 +00001153struct PragmaSTDC_CX_LIMITED_RANGEHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001154 PragmaSTDC_CX_LIMITED_RANGEHandler()
1155 : PragmaHandler("CX_LIMITED_RANGE") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +00001156 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1157 Token &Tok) {
Peter Collingbourne3bffa522011-02-14 01:42:24 +00001158 tok::OnOffSwitch OOS;
1159 PP.LexOnOffSwitch(OOS);
Chris Lattner958ee042009-04-19 21:20:35 +00001160 }
1161};
Mike Stump11289f42009-09-09 15:08:12 +00001162
James Dennett18a6d792012-06-17 03:26:26 +00001163/// PragmaSTDC_UnknownHandler - "\#pragma STDC ...".
Chris Lattner958ee042009-04-19 21:20:35 +00001164struct PragmaSTDC_UnknownHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001165 PragmaSTDC_UnknownHandler() {}
Douglas Gregorc7d65762010-09-09 22:45:38 +00001166 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1167 Token &UnknownTok) {
Chris Lattner02ef4e32009-04-19 21:50:08 +00001168 // C99 6.10.6p2, unknown forms are not allowed.
Chris Lattnera0b1f762009-04-19 21:25:37 +00001169 PP.Diag(UnknownTok, diag::ext_stdc_pragma_ignored);
Chris Lattner958ee042009-04-19 21:20:35 +00001170 }
1171};
Mike Stump11289f42009-09-09 15:08:12 +00001172
John McCall32f5fe12011-09-30 05:12:12 +00001173/// PragmaARCCFCodeAuditedHandler -
James Dennett18a6d792012-06-17 03:26:26 +00001174/// \#pragma clang arc_cf_code_audited begin/end
John McCall32f5fe12011-09-30 05:12:12 +00001175struct PragmaARCCFCodeAuditedHandler : public PragmaHandler {
1176 PragmaARCCFCodeAuditedHandler() : PragmaHandler("arc_cf_code_audited") {}
1177 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1178 Token &NameTok) {
1179 SourceLocation Loc = NameTok.getLocation();
1180 bool IsBegin;
1181
1182 Token Tok;
1183
1184 // Lex the 'begin' or 'end'.
1185 PP.LexUnexpandedToken(Tok);
1186 const IdentifierInfo *BeginEnd = Tok.getIdentifierInfo();
1187 if (BeginEnd && BeginEnd->isStr("begin")) {
1188 IsBegin = true;
1189 } else if (BeginEnd && BeginEnd->isStr("end")) {
1190 IsBegin = false;
1191 } else {
1192 PP.Diag(Tok.getLocation(), diag::err_pp_arc_cf_code_audited_syntax);
1193 return;
1194 }
1195
1196 // Verify that this is followed by EOD.
1197 PP.LexUnexpandedToken(Tok);
1198 if (Tok.isNot(tok::eod))
1199 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
1200
1201 // The start location of the active audit.
1202 SourceLocation BeginLoc = PP.getPragmaARCCFCodeAuditedLoc();
1203
1204 // The start location we want after processing this.
1205 SourceLocation NewLoc;
1206
1207 if (IsBegin) {
1208 // Complain about attempts to re-enter an audit.
1209 if (BeginLoc.isValid()) {
1210 PP.Diag(Loc, diag::err_pp_double_begin_of_arc_cf_code_audited);
1211 PP.Diag(BeginLoc, diag::note_pragma_entered_here);
1212 }
1213 NewLoc = Loc;
1214 } else {
1215 // Complain about attempts to leave an audit that doesn't exist.
1216 if (!BeginLoc.isValid()) {
1217 PP.Diag(Loc, diag::err_pp_unmatched_end_of_arc_cf_code_audited);
1218 return;
1219 }
1220 NewLoc = SourceLocation();
1221 }
1222
1223 PP.setPragmaARCCFCodeAuditedLoc(NewLoc);
1224 }
1225};
1226
Aaron Ballman406ea512012-11-30 19:52:30 +00001227 /// \brief Handle "\#pragma region [...]"
1228 ///
1229 /// The syntax is
1230 /// \code
Dmitri Gribenko9ebd1612012-11-30 20:04:39 +00001231 /// #pragma region [optional name]
1232 /// #pragma endregion [optional comment]
Aaron Ballman406ea512012-11-30 19:52:30 +00001233 /// \endcode
1234 ///
1235 /// \note This is
1236 /// <a href="http://msdn.microsoft.com/en-us/library/b6xkz944(v=vs.80).aspx">editor-only</a>
1237 /// pragma, just skipped by compiler.
1238 struct PragmaRegionHandler : public PragmaHandler {
1239 PragmaRegionHandler(const char *pragma) : PragmaHandler(pragma) { }
1240
1241 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1242 Token &NameTok) {
1243 // #pragma region: endregion matches can be verified
1244 // __pragma(region): no sense, but ignored by msvc
1245 // _Pragma is not valid for MSVC, but there isn't any point
1246 // to handle a _Pragma differently.
1247 }
1248 };
1249
Chris Lattnerb694ba72006-07-02 22:41:36 +00001250} // end anonymous namespace
1251
1252
1253/// RegisterBuiltinPragmas - Install the standard preprocessor pragmas:
James Dennett18a6d792012-06-17 03:26:26 +00001254/// \#pragma GCC poison/system_header/dependency and \#pragma once.
Chris Lattnerb694ba72006-07-02 22:41:36 +00001255void Preprocessor::RegisterBuiltinPragmas() {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001256 AddPragmaHandler(new PragmaOnceHandler());
1257 AddPragmaHandler(new PragmaMarkHandler());
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001258 AddPragmaHandler(new PragmaPushMacroHandler());
1259 AddPragmaHandler(new PragmaPopMacroHandler());
Michael J. Spencera0a820f2010-09-27 06:19:02 +00001260 AddPragmaHandler(new PragmaMessageHandler());
Mike Stump11289f42009-09-09 15:08:12 +00001261
Chris Lattnerb61448d2009-05-12 18:21:11 +00001262 // #pragma GCC ...
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001263 AddPragmaHandler("GCC", new PragmaPoisonHandler());
1264 AddPragmaHandler("GCC", new PragmaSystemHeaderHandler());
1265 AddPragmaHandler("GCC", new PragmaDependencyHandler());
Douglas Gregor3bde9b12011-06-22 19:41:48 +00001266 AddPragmaHandler("GCC", new PragmaDiagnosticHandler("GCC"));
Chris Lattnerb61448d2009-05-12 18:21:11 +00001267 // #pragma clang ...
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001268 AddPragmaHandler("clang", new PragmaPoisonHandler());
1269 AddPragmaHandler("clang", new PragmaSystemHeaderHandler());
Daniel Dunbarb8068c32010-07-28 15:40:33 +00001270 AddPragmaHandler("clang", new PragmaDebugHandler());
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001271 AddPragmaHandler("clang", new PragmaDependencyHandler());
Douglas Gregor3bde9b12011-06-22 19:41:48 +00001272 AddPragmaHandler("clang", new PragmaDiagnosticHandler("clang"));
John McCall32f5fe12011-09-30 05:12:12 +00001273 AddPragmaHandler("clang", new PragmaARCCFCodeAuditedHandler());
Chris Lattnerb61448d2009-05-12 18:21:11 +00001274
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001275 AddPragmaHandler("STDC", new PragmaSTDC_FENV_ACCESSHandler());
1276 AddPragmaHandler("STDC", new PragmaSTDC_CX_LIMITED_RANGEHandler());
Chris Lattner958ee042009-04-19 21:20:35 +00001277 AddPragmaHandler("STDC", new PragmaSTDC_UnknownHandler());
Mike Stump11289f42009-09-09 15:08:12 +00001278
Chris Lattner2ff698d2009-01-16 08:21:25 +00001279 // MS extensions.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001280 if (LangOpts.MicrosoftExt) {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001281 AddPragmaHandler(new PragmaCommentHandler());
Aaron Ballman611306e2012-03-02 22:51:54 +00001282 AddPragmaHandler(new PragmaIncludeAliasHandler());
Aaron Ballman406ea512012-11-30 19:52:30 +00001283 AddPragmaHandler(new PragmaRegionHandler("region"));
1284 AddPragmaHandler(new PragmaRegionHandler("endregion"));
Chris Lattner30c924b2010-06-26 17:11:39 +00001285 }
Chris Lattnerb694ba72006-07-02 22:41:36 +00001286}