blob: 2094dd1e1c6b08bfb6bc877475678deaadab2ff7 [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)) {
706 if (CurrentMD->getInfo()->isWarnIfUnused())
707 WarnUnusedMacroLocs.erase(CurrentMD->getInfo()->getDefinitionLoc());
708 UndefineMacro(IdentInfo, CurrentMD, MessageLoc);
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000709 }
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000710
711 // Get the MacroInfo we want to reinstall.
712 MacroInfo *MacroToReInstall = iter->second.back();
713
Alexander Kornienkoc0b49282012-08-29 16:56:24 +0000714 if (MacroToReInstall) {
715 // Reinstall the previously pushed macro.
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +0000716 setMacroDirective(IdentInfo, MacroToReInstall, MessageLoc,
717 /*isImported=*/false);
Alexander Kornienkoc0b49282012-08-29 16:56:24 +0000718 } else if (IdentInfo->hasMacroDefinition()) {
719 clearMacroInfo(IdentInfo);
720 }
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000721
722 // Pop PragmaPushMacroInfo stack.
723 iter->second.pop_back();
724 if (iter->second.size() == 0)
725 PragmaPushMacroInfo.erase(iter);
726 } else {
727 Diag(MessageLoc, diag::warn_pragma_pop_macro_no_push)
728 << IdentInfo->getName();
729 }
730}
Chris Lattnerb694ba72006-07-02 22:41:36 +0000731
Aaron Ballman611306e2012-03-02 22:51:54 +0000732void Preprocessor::HandlePragmaIncludeAlias(Token &Tok) {
733 // We will either get a quoted filename or a bracketed filename, and we
734 // have to track which we got. The first filename is the source name,
735 // and the second name is the mapped filename. If the first is quoted,
736 // the second must be as well (cannot mix and match quotes and brackets).
Aaron Ballman611306e2012-03-02 22:51:54 +0000737
738 // Get the open paren
739 Lex(Tok);
740 if (Tok.isNot(tok::l_paren)) {
741 Diag(Tok, diag::warn_pragma_include_alias_expected) << "(";
742 return;
743 }
744
745 // We expect either a quoted string literal, or a bracketed name
746 Token SourceFilenameTok;
747 CurPPLexer->LexIncludeFilename(SourceFilenameTok);
748 if (SourceFilenameTok.is(tok::eod)) {
749 // The diagnostic has already been handled
750 return;
751 }
752
753 StringRef SourceFileName;
754 SmallString<128> FileNameBuffer;
755 if (SourceFilenameTok.is(tok::string_literal) ||
756 SourceFilenameTok.is(tok::angle_string_literal)) {
757 SourceFileName = getSpelling(SourceFilenameTok, FileNameBuffer);
758 } else if (SourceFilenameTok.is(tok::less)) {
759 // This could be a path instead of just a name
760 FileNameBuffer.push_back('<');
761 SourceLocation End;
762 if (ConcatenateIncludeName(FileNameBuffer, End))
763 return; // Diagnostic already emitted
764 SourceFileName = FileNameBuffer.str();
765 } else {
766 Diag(Tok, diag::warn_pragma_include_alias_expected_filename);
767 return;
768 }
769 FileNameBuffer.clear();
770
771 // Now we expect a comma, followed by another include name
772 Lex(Tok);
773 if (Tok.isNot(tok::comma)) {
774 Diag(Tok, diag::warn_pragma_include_alias_expected) << ",";
775 return;
776 }
777
778 Token ReplaceFilenameTok;
779 CurPPLexer->LexIncludeFilename(ReplaceFilenameTok);
780 if (ReplaceFilenameTok.is(tok::eod)) {
781 // The diagnostic has already been handled
782 return;
783 }
784
785 StringRef ReplaceFileName;
786 if (ReplaceFilenameTok.is(tok::string_literal) ||
787 ReplaceFilenameTok.is(tok::angle_string_literal)) {
788 ReplaceFileName = getSpelling(ReplaceFilenameTok, FileNameBuffer);
789 } else if (ReplaceFilenameTok.is(tok::less)) {
790 // This could be a path instead of just a name
791 FileNameBuffer.push_back('<');
792 SourceLocation End;
793 if (ConcatenateIncludeName(FileNameBuffer, End))
794 return; // Diagnostic already emitted
795 ReplaceFileName = FileNameBuffer.str();
796 } else {
797 Diag(Tok, diag::warn_pragma_include_alias_expected_filename);
798 return;
799 }
800
801 // Finally, we expect the closing paren
802 Lex(Tok);
803 if (Tok.isNot(tok::r_paren)) {
804 Diag(Tok, diag::warn_pragma_include_alias_expected) << ")";
805 return;
806 }
807
808 // Now that we have the source and target filenames, we need to make sure
809 // they're both of the same type (angled vs non-angled)
810 StringRef OriginalSource = SourceFileName;
811
812 bool SourceIsAngled =
813 GetIncludeFilenameSpelling(SourceFilenameTok.getLocation(),
814 SourceFileName);
815 bool ReplaceIsAngled =
816 GetIncludeFilenameSpelling(ReplaceFilenameTok.getLocation(),
817 ReplaceFileName);
818 if (!SourceFileName.empty() && !ReplaceFileName.empty() &&
819 (SourceIsAngled != ReplaceIsAngled)) {
820 unsigned int DiagID;
821 if (SourceIsAngled)
822 DiagID = diag::warn_pragma_include_alias_mismatch_angle;
823 else
824 DiagID = diag::warn_pragma_include_alias_mismatch_quote;
825
826 Diag(SourceFilenameTok.getLocation(), DiagID)
827 << SourceFileName
828 << ReplaceFileName;
829
830 return;
831 }
832
833 // Now we can let the include handler know about this mapping
834 getHeaderSearchInfo().AddIncludeAlias(OriginalSource, ReplaceFileName);
835}
836
Chris Lattnerb694ba72006-07-02 22:41:36 +0000837/// AddPragmaHandler - Add the specified pragma handler to the preprocessor.
838/// If 'Namespace' is non-null, then it is a token required to exist on the
839/// pragma line before the pragma string starts, e.g. "STDC" or "GCC".
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000840void Preprocessor::AddPragmaHandler(StringRef Namespace,
Chris Lattnerb694ba72006-07-02 22:41:36 +0000841 PragmaHandler *Handler) {
842 PragmaNamespace *InsertNS = PragmaHandlers;
Mike Stump11289f42009-09-09 15:08:12 +0000843
Chris Lattnerb694ba72006-07-02 22:41:36 +0000844 // If this is specified to be in a namespace, step down into it.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000845 if (!Namespace.empty()) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000846 // If there is already a pragma handler with the name of this namespace,
847 // we either have an error (directive with the same name as a namespace) or
848 // we already have the namespace to insert into.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000849 if (PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace)) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000850 InsertNS = Existing->getIfNamespace();
851 assert(InsertNS != 0 && "Cannot have a pragma namespace and pragma"
852 " handler with the same name!");
853 } else {
854 // Otherwise, this namespace doesn't exist yet, create and insert the
855 // handler for it.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000856 InsertNS = new PragmaNamespace(Namespace);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000857 PragmaHandlers->AddPragma(InsertNS);
858 }
859 }
Mike Stump11289f42009-09-09 15:08:12 +0000860
Chris Lattnerb694ba72006-07-02 22:41:36 +0000861 // Check to make sure we don't already have a pragma for this identifier.
862 assert(!InsertNS->FindHandler(Handler->getName()) &&
863 "Pragma handler already exists for this identifier!");
864 InsertNS->AddPragma(Handler);
865}
866
Daniel Dunbar40596532008-10-04 19:17:46 +0000867/// RemovePragmaHandler - Remove the specific pragma handler from the
868/// preprocessor. If \arg Namespace is non-null, then it should be the
869/// namespace that \arg Handler was added to. It is an error to remove
870/// a handler that has not been registered.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000871void Preprocessor::RemovePragmaHandler(StringRef Namespace,
Daniel Dunbar40596532008-10-04 19:17:46 +0000872 PragmaHandler *Handler) {
873 PragmaNamespace *NS = PragmaHandlers;
Mike Stump11289f42009-09-09 15:08:12 +0000874
Daniel Dunbar40596532008-10-04 19:17:46 +0000875 // If this is specified to be in a namespace, step down into it.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000876 if (!Namespace.empty()) {
877 PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace);
Daniel Dunbar40596532008-10-04 19:17:46 +0000878 assert(Existing && "Namespace containing handler does not exist!");
879
880 NS = Existing->getIfNamespace();
881 assert(NS && "Invalid namespace, registered as a regular pragma handler!");
882 }
883
884 NS->RemovePragmaHandler(Handler);
Mike Stump11289f42009-09-09 15:08:12 +0000885
Daniel Dunbar40596532008-10-04 19:17:46 +0000886 // If this is a non-default namespace and it is now empty, remove
887 // it.
Argyrios Kyrtzidis7ce75262012-01-06 00:22:09 +0000888 if (NS != PragmaHandlers && NS->IsEmpty()) {
Daniel Dunbar40596532008-10-04 19:17:46 +0000889 PragmaHandlers->RemovePragmaHandler(NS);
Argyrios Kyrtzidis7ce75262012-01-06 00:22:09 +0000890 delete NS;
891 }
Daniel Dunbar40596532008-10-04 19:17:46 +0000892}
893
Peter Collingbourne3bffa522011-02-14 01:42:24 +0000894bool Preprocessor::LexOnOffSwitch(tok::OnOffSwitch &Result) {
895 Token Tok;
896 LexUnexpandedToken(Tok);
897
898 if (Tok.isNot(tok::identifier)) {
899 Diag(Tok, diag::ext_on_off_switch_syntax);
900 return true;
901 }
902 IdentifierInfo *II = Tok.getIdentifierInfo();
903 if (II->isStr("ON"))
904 Result = tok::OOS_ON;
905 else if (II->isStr("OFF"))
906 Result = tok::OOS_OFF;
907 else if (II->isStr("DEFAULT"))
908 Result = tok::OOS_DEFAULT;
909 else {
910 Diag(Tok, diag::ext_on_off_switch_syntax);
911 return true;
912 }
913
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000914 // Verify that this is followed by EOD.
Peter Collingbourne3bffa522011-02-14 01:42:24 +0000915 LexUnexpandedToken(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000916 if (Tok.isNot(tok::eod))
917 Diag(Tok, diag::ext_pragma_syntax_eod);
Peter Collingbourne3bffa522011-02-14 01:42:24 +0000918 return false;
919}
920
Chris Lattnerb694ba72006-07-02 22:41:36 +0000921namespace {
James Dennett18a6d792012-06-17 03:26:26 +0000922/// PragmaOnceHandler - "\#pragma once" marks the file as atomically included.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000923struct PragmaOnceHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000924 PragmaOnceHandler() : PragmaHandler("once") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000925 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
926 Token &OnceTok) {
Chris Lattnerce2ab6f2009-04-14 05:07:49 +0000927 PP.CheckEndOfDirective("pragma once");
Chris Lattnerb694ba72006-07-02 22:41:36 +0000928 PP.HandlePragmaOnce(OnceTok);
929 }
930};
931
James Dennett18a6d792012-06-17 03:26:26 +0000932/// PragmaMarkHandler - "\#pragma mark ..." is ignored by the compiler, and the
Chris Lattnerc2383312007-12-19 19:38:36 +0000933/// rest of the line is not lexed.
934struct PragmaMarkHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000935 PragmaMarkHandler() : PragmaHandler("mark") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000936 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
937 Token &MarkTok) {
Chris Lattnerc2383312007-12-19 19:38:36 +0000938 PP.HandlePragmaMark();
939 }
940};
941
James Dennett18a6d792012-06-17 03:26:26 +0000942/// PragmaPoisonHandler - "\#pragma poison x" marks x as not usable.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000943struct PragmaPoisonHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000944 PragmaPoisonHandler() : PragmaHandler("poison") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000945 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
946 Token &PoisonTok) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000947 PP.HandlePragmaPoison(PoisonTok);
948 }
949};
950
James Dennett18a6d792012-06-17 03:26:26 +0000951/// PragmaSystemHeaderHandler - "\#pragma system_header" marks the current file
Chris Lattnerc2383312007-12-19 19:38:36 +0000952/// as a system header, which silences warnings in it.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000953struct PragmaSystemHeaderHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000954 PragmaSystemHeaderHandler() : PragmaHandler("system_header") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000955 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
956 Token &SHToken) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000957 PP.HandlePragmaSystemHeader(SHToken);
Chris Lattnerce2ab6f2009-04-14 05:07:49 +0000958 PP.CheckEndOfDirective("pragma");
Chris Lattnerb694ba72006-07-02 22:41:36 +0000959 }
960};
961struct PragmaDependencyHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000962 PragmaDependencyHandler() : PragmaHandler("dependency") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000963 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
964 Token &DepToken) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000965 PP.HandlePragmaDependency(DepToken);
966 }
967};
Mike Stump11289f42009-09-09 15:08:12 +0000968
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000969struct PragmaDebugHandler : public PragmaHandler {
970 PragmaDebugHandler() : PragmaHandler("__debug") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000971 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
972 Token &DepToken) {
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000973 Token Tok;
974 PP.LexUnexpandedToken(Tok);
975 if (Tok.isNot(tok::identifier)) {
Douglas Gregor3cc26482010-08-30 15:15:34 +0000976 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000977 return;
978 }
979 IdentifierInfo *II = Tok.getIdentifierInfo();
980
Daniel Dunbarf2cf3292010-08-17 22:32:48 +0000981 if (II->isStr("assert")) {
David Blaikie83d382b2011-09-23 05:06:16 +0000982 llvm_unreachable("This is an assertion!");
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000983 } else if (II->isStr("crash")) {
David Blaikie5bd4c2a2012-08-21 18:56:49 +0000984 LLVM_BUILTIN_TRAP;
David Blaikie5d577a22012-06-29 22:03:56 +0000985 } else if (II->isStr("parser_crash")) {
986 Token Crasher;
987 Crasher.setKind(tok::annot_pragma_parser_crash);
988 PP.EnterToken(Crasher);
Daniel Dunbarf2cf3292010-08-17 22:32:48 +0000989 } else if (II->isStr("llvm_fatal_error")) {
990 llvm::report_fatal_error("#pragma clang __debug llvm_fatal_error");
991 } else if (II->isStr("llvm_unreachable")) {
992 llvm_unreachable("#pragma clang __debug llvm_unreachable");
993 } else if (II->isStr("overflow_stack")) {
994 DebugOverflowStack();
Daniel Dunbar211a7872010-08-18 23:09:23 +0000995 } else if (II->isStr("handle_crash")) {
996 llvm::CrashRecoveryContext *CRC =llvm::CrashRecoveryContext::GetCurrent();
997 if (CRC)
998 CRC->HandleCrash();
Daniel Dunbarf2cf3292010-08-17 22:32:48 +0000999 } else {
1000 PP.Diag(Tok, diag::warn_pragma_debug_unexpected_command)
1001 << II->getName();
Daniel Dunbarb8068c32010-07-28 15:40:33 +00001002 }
1003 }
1004
Francois Pichet2e11f5d2011-05-25 16:15:03 +00001005// Disable MSVC warning about runtime stack overflow.
1006#ifdef _MSC_VER
1007 #pragma warning(disable : 4717)
1008#endif
Daniel Dunbarb8068c32010-07-28 15:40:33 +00001009 void DebugOverflowStack() {
1010 DebugOverflowStack();
1011 }
Francois Pichet2e11f5d2011-05-25 16:15:03 +00001012#ifdef _MSC_VER
1013 #pragma warning(default : 4717)
1014#endif
1015
Daniel Dunbarb8068c32010-07-28 15:40:33 +00001016};
1017
James Dennett18a6d792012-06-17 03:26:26 +00001018/// PragmaDiagnosticHandler - e.g. '\#pragma GCC diagnostic ignored "-Wformat"'
Chris Lattner504af112009-04-19 23:16:58 +00001019struct PragmaDiagnosticHandler : public PragmaHandler {
Douglas Gregor3bde9b12011-06-22 19:41:48 +00001020private:
1021 const char *Namespace;
Chris Lattnerfb42a182009-07-12 21:18:45 +00001022public:
Douglas Gregor3bde9b12011-06-22 19:41:48 +00001023 explicit PragmaDiagnosticHandler(const char *NS) :
1024 PragmaHandler("diagnostic"), Namespace(NS) {}
Douglas Gregorc7d65762010-09-09 22:45:38 +00001025 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1026 Token &DiagToken) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +00001027 SourceLocation DiagLoc = DiagToken.getLocation();
Chris Lattner504af112009-04-19 23:16:58 +00001028 Token Tok;
1029 PP.LexUnexpandedToken(Tok);
1030 if (Tok.isNot(tok::identifier)) {
Douglas Gregor3cc26482010-08-30 15:15:34 +00001031 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Chris Lattner504af112009-04-19 23:16:58 +00001032 return;
1033 }
1034 IdentifierInfo *II = Tok.getIdentifierInfo();
Douglas Gregor3bde9b12011-06-22 19:41:48 +00001035 PPCallbacks *Callbacks = PP.getPPCallbacks();
Mike Stump11289f42009-09-09 15:08:12 +00001036
Chris Lattner504af112009-04-19 23:16:58 +00001037 diag::Mapping Map;
1038 if (II->isStr("warning"))
1039 Map = diag::MAP_WARNING;
1040 else if (II->isStr("error"))
1041 Map = diag::MAP_ERROR;
1042 else if (II->isStr("ignored"))
1043 Map = diag::MAP_IGNORE;
1044 else if (II->isStr("fatal"))
1045 Map = diag::MAP_FATAL;
Douglas Gregor3cc26482010-08-30 15:15:34 +00001046 else if (II->isStr("pop")) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +00001047 if (!PP.getDiagnostics().popMappings(DiagLoc))
Douglas Gregor3cc26482010-08-30 15:15:34 +00001048 PP.Diag(Tok, diag::warn_pragma_diagnostic_cannot_pop);
Douglas Gregor3bde9b12011-06-22 19:41:48 +00001049 else if (Callbacks)
1050 Callbacks->PragmaDiagnosticPop(DiagLoc, Namespace);
Douglas Gregor3cc26482010-08-30 15:15:34 +00001051 return;
1052 } else if (II->isStr("push")) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +00001053 PP.getDiagnostics().pushMappings(DiagLoc);
Douglas Gregor3bde9b12011-06-22 19:41:48 +00001054 if (Callbacks)
1055 Callbacks->PragmaDiagnosticPush(DiagLoc, Namespace);
Chris Lattnerfb42a182009-07-12 21:18:45 +00001056 return;
1057 } else {
Douglas Gregor3cc26482010-08-30 15:15:34 +00001058 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Chris Lattner504af112009-04-19 23:16:58 +00001059 return;
1060 }
Mike Stump11289f42009-09-09 15:08:12 +00001061
Chris Lattner504af112009-04-19 23:16:58 +00001062 PP.LexUnexpandedToken(Tok);
Andy Gibbs58905d22012-11-17 19:15:38 +00001063 SourceLocation StringLoc = Tok.getLocation();
Chris Lattner504af112009-04-19 23:16:58 +00001064
Andy Gibbs58905d22012-11-17 19:15:38 +00001065 std::string WarningName;
Andy Gibbsa8df57a2012-11-17 19:16:52 +00001066 if (!PP.FinishLexStringLiteral(Tok, WarningName, "pragma diagnostic",
1067 /*MacroExpansion=*/false))
Chris Lattner504af112009-04-19 23:16:58 +00001068 return;
Mike Stump11289f42009-09-09 15:08:12 +00001069
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001070 if (Tok.isNot(tok::eod)) {
Chris Lattner504af112009-04-19 23:16:58 +00001071 PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token);
1072 return;
1073 }
Mike Stump11289f42009-09-09 15:08:12 +00001074
Chris Lattner504af112009-04-19 23:16:58 +00001075 if (WarningName.size() < 3 || WarningName[0] != '-' ||
1076 WarningName[1] != 'W') {
Andy Gibbs58905d22012-11-17 19:15:38 +00001077 PP.Diag(StringLoc, diag::warn_pragma_diagnostic_invalid_option);
Chris Lattner504af112009-04-19 23:16:58 +00001078 return;
1079 }
Mike Stump11289f42009-09-09 15:08:12 +00001080
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +00001081 if (PP.getDiagnostics().setDiagnosticGroupMapping(WarningName.substr(2),
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +00001082 Map, DiagLoc))
Andy Gibbs58905d22012-11-17 19:15:38 +00001083 PP.Diag(StringLoc, diag::warn_pragma_diagnostic_unknown_warning)
1084 << WarningName;
Douglas Gregor3bde9b12011-06-22 19:41:48 +00001085 else if (Callbacks)
1086 Callbacks->PragmaDiagnostic(DiagLoc, Namespace, Map, WarningName);
Chris Lattner504af112009-04-19 23:16:58 +00001087 }
1088};
Mike Stump11289f42009-09-09 15:08:12 +00001089
James Dennett18a6d792012-06-17 03:26:26 +00001090/// PragmaCommentHandler - "\#pragma comment ...".
Chris Lattner2ff698d2009-01-16 08:21:25 +00001091struct PragmaCommentHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001092 PragmaCommentHandler() : PragmaHandler("comment") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +00001093 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1094 Token &CommentTok) {
Chris Lattner2ff698d2009-01-16 08:21:25 +00001095 PP.HandlePragmaComment(CommentTok);
1096 }
1097};
Mike Stump11289f42009-09-09 15:08:12 +00001098
James Dennett18a6d792012-06-17 03:26:26 +00001099/// PragmaIncludeAliasHandler - "\#pragma include_alias("...")".
Aaron Ballman611306e2012-03-02 22:51:54 +00001100struct PragmaIncludeAliasHandler : public PragmaHandler {
1101 PragmaIncludeAliasHandler() : PragmaHandler("include_alias") {}
1102 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1103 Token &IncludeAliasTok) {
1104 PP.HandlePragmaIncludeAlias(IncludeAliasTok);
1105 }
1106};
1107
James Dennett18a6d792012-06-17 03:26:26 +00001108/// PragmaMessageHandler - "\#pragma message("...")".
Chris Lattner30c924b2010-06-26 17:11:39 +00001109struct PragmaMessageHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001110 PragmaMessageHandler() : PragmaHandler("message") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +00001111 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1112 Token &CommentTok) {
Chris Lattner30c924b2010-06-26 17:11:39 +00001113 PP.HandlePragmaMessage(CommentTok);
1114 }
1115};
1116
James Dennett18a6d792012-06-17 03:26:26 +00001117/// PragmaPushMacroHandler - "\#pragma push_macro" saves the value of the
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001118/// macro on the top of the stack.
1119struct PragmaPushMacroHandler : public PragmaHandler {
1120 PragmaPushMacroHandler() : PragmaHandler("push_macro") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +00001121 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1122 Token &PushMacroTok) {
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001123 PP.HandlePragmaPushMacro(PushMacroTok);
1124 }
1125};
1126
1127
James Dennett18a6d792012-06-17 03:26:26 +00001128/// PragmaPopMacroHandler - "\#pragma pop_macro" sets the value of the
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001129/// macro to the value on the top of the stack.
1130struct PragmaPopMacroHandler : public PragmaHandler {
1131 PragmaPopMacroHandler() : PragmaHandler("pop_macro") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +00001132 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1133 Token &PopMacroTok) {
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001134 PP.HandlePragmaPopMacro(PopMacroTok);
1135 }
1136};
1137
Chris Lattner958ee042009-04-19 21:20:35 +00001138// Pragma STDC implementations.
Chris Lattner02ef4e32009-04-19 21:50:08 +00001139
James Dennett18a6d792012-06-17 03:26:26 +00001140/// PragmaSTDC_FENV_ACCESSHandler - "\#pragma STDC FENV_ACCESS ...".
Chris Lattner958ee042009-04-19 21:20:35 +00001141struct PragmaSTDC_FENV_ACCESSHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001142 PragmaSTDC_FENV_ACCESSHandler() : PragmaHandler("FENV_ACCESS") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +00001143 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1144 Token &Tok) {
Peter Collingbourne3bffa522011-02-14 01:42:24 +00001145 tok::OnOffSwitch OOS;
1146 if (PP.LexOnOffSwitch(OOS))
1147 return;
1148 if (OOS == tok::OOS_ON)
Chris Lattnerdf222682009-04-19 21:55:32 +00001149 PP.Diag(Tok, diag::warn_stdc_fenv_access_not_supported);
Chris Lattner958ee042009-04-19 21:20:35 +00001150 }
1151};
Mike Stump11289f42009-09-09 15:08:12 +00001152
James Dennett18a6d792012-06-17 03:26:26 +00001153/// PragmaSTDC_CX_LIMITED_RANGEHandler - "\#pragma STDC CX_LIMITED_RANGE ...".
Chris Lattner958ee042009-04-19 21:20:35 +00001154struct PragmaSTDC_CX_LIMITED_RANGEHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001155 PragmaSTDC_CX_LIMITED_RANGEHandler()
1156 : PragmaHandler("CX_LIMITED_RANGE") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +00001157 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1158 Token &Tok) {
Peter Collingbourne3bffa522011-02-14 01:42:24 +00001159 tok::OnOffSwitch OOS;
1160 PP.LexOnOffSwitch(OOS);
Chris Lattner958ee042009-04-19 21:20:35 +00001161 }
1162};
Mike Stump11289f42009-09-09 15:08:12 +00001163
James Dennett18a6d792012-06-17 03:26:26 +00001164/// PragmaSTDC_UnknownHandler - "\#pragma STDC ...".
Chris Lattner958ee042009-04-19 21:20:35 +00001165struct PragmaSTDC_UnknownHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001166 PragmaSTDC_UnknownHandler() {}
Douglas Gregorc7d65762010-09-09 22:45:38 +00001167 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1168 Token &UnknownTok) {
Chris Lattner02ef4e32009-04-19 21:50:08 +00001169 // C99 6.10.6p2, unknown forms are not allowed.
Chris Lattnera0b1f762009-04-19 21:25:37 +00001170 PP.Diag(UnknownTok, diag::ext_stdc_pragma_ignored);
Chris Lattner958ee042009-04-19 21:20:35 +00001171 }
1172};
Mike Stump11289f42009-09-09 15:08:12 +00001173
John McCall32f5fe12011-09-30 05:12:12 +00001174/// PragmaARCCFCodeAuditedHandler -
James Dennett18a6d792012-06-17 03:26:26 +00001175/// \#pragma clang arc_cf_code_audited begin/end
John McCall32f5fe12011-09-30 05:12:12 +00001176struct PragmaARCCFCodeAuditedHandler : public PragmaHandler {
1177 PragmaARCCFCodeAuditedHandler() : PragmaHandler("arc_cf_code_audited") {}
1178 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1179 Token &NameTok) {
1180 SourceLocation Loc = NameTok.getLocation();
1181 bool IsBegin;
1182
1183 Token Tok;
1184
1185 // Lex the 'begin' or 'end'.
1186 PP.LexUnexpandedToken(Tok);
1187 const IdentifierInfo *BeginEnd = Tok.getIdentifierInfo();
1188 if (BeginEnd && BeginEnd->isStr("begin")) {
1189 IsBegin = true;
1190 } else if (BeginEnd && BeginEnd->isStr("end")) {
1191 IsBegin = false;
1192 } else {
1193 PP.Diag(Tok.getLocation(), diag::err_pp_arc_cf_code_audited_syntax);
1194 return;
1195 }
1196
1197 // Verify that this is followed by EOD.
1198 PP.LexUnexpandedToken(Tok);
1199 if (Tok.isNot(tok::eod))
1200 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
1201
1202 // The start location of the active audit.
1203 SourceLocation BeginLoc = PP.getPragmaARCCFCodeAuditedLoc();
1204
1205 // The start location we want after processing this.
1206 SourceLocation NewLoc;
1207
1208 if (IsBegin) {
1209 // Complain about attempts to re-enter an audit.
1210 if (BeginLoc.isValid()) {
1211 PP.Diag(Loc, diag::err_pp_double_begin_of_arc_cf_code_audited);
1212 PP.Diag(BeginLoc, diag::note_pragma_entered_here);
1213 }
1214 NewLoc = Loc;
1215 } else {
1216 // Complain about attempts to leave an audit that doesn't exist.
1217 if (!BeginLoc.isValid()) {
1218 PP.Diag(Loc, diag::err_pp_unmatched_end_of_arc_cf_code_audited);
1219 return;
1220 }
1221 NewLoc = SourceLocation();
1222 }
1223
1224 PP.setPragmaARCCFCodeAuditedLoc(NewLoc);
1225 }
1226};
1227
Aaron Ballman406ea512012-11-30 19:52:30 +00001228 /// \brief Handle "\#pragma region [...]"
1229 ///
1230 /// The syntax is
1231 /// \code
Dmitri Gribenko9ebd1612012-11-30 20:04:39 +00001232 /// #pragma region [optional name]
1233 /// #pragma endregion [optional comment]
Aaron Ballman406ea512012-11-30 19:52:30 +00001234 /// \endcode
1235 ///
1236 /// \note This is
1237 /// <a href="http://msdn.microsoft.com/en-us/library/b6xkz944(v=vs.80).aspx">editor-only</a>
1238 /// pragma, just skipped by compiler.
1239 struct PragmaRegionHandler : public PragmaHandler {
1240 PragmaRegionHandler(const char *pragma) : PragmaHandler(pragma) { }
1241
1242 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1243 Token &NameTok) {
1244 // #pragma region: endregion matches can be verified
1245 // __pragma(region): no sense, but ignored by msvc
1246 // _Pragma is not valid for MSVC, but there isn't any point
1247 // to handle a _Pragma differently.
1248 }
1249 };
1250
Chris Lattnerb694ba72006-07-02 22:41:36 +00001251} // end anonymous namespace
1252
1253
1254/// RegisterBuiltinPragmas - Install the standard preprocessor pragmas:
James Dennett18a6d792012-06-17 03:26:26 +00001255/// \#pragma GCC poison/system_header/dependency and \#pragma once.
Chris Lattnerb694ba72006-07-02 22:41:36 +00001256void Preprocessor::RegisterBuiltinPragmas() {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001257 AddPragmaHandler(new PragmaOnceHandler());
1258 AddPragmaHandler(new PragmaMarkHandler());
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001259 AddPragmaHandler(new PragmaPushMacroHandler());
1260 AddPragmaHandler(new PragmaPopMacroHandler());
Michael J. Spencera0a820f2010-09-27 06:19:02 +00001261 AddPragmaHandler(new PragmaMessageHandler());
Mike Stump11289f42009-09-09 15:08:12 +00001262
Chris Lattnerb61448d2009-05-12 18:21:11 +00001263 // #pragma GCC ...
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001264 AddPragmaHandler("GCC", new PragmaPoisonHandler());
1265 AddPragmaHandler("GCC", new PragmaSystemHeaderHandler());
1266 AddPragmaHandler("GCC", new PragmaDependencyHandler());
Douglas Gregor3bde9b12011-06-22 19:41:48 +00001267 AddPragmaHandler("GCC", new PragmaDiagnosticHandler("GCC"));
Chris Lattnerb61448d2009-05-12 18:21:11 +00001268 // #pragma clang ...
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001269 AddPragmaHandler("clang", new PragmaPoisonHandler());
1270 AddPragmaHandler("clang", new PragmaSystemHeaderHandler());
Daniel Dunbarb8068c32010-07-28 15:40:33 +00001271 AddPragmaHandler("clang", new PragmaDebugHandler());
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001272 AddPragmaHandler("clang", new PragmaDependencyHandler());
Douglas Gregor3bde9b12011-06-22 19:41:48 +00001273 AddPragmaHandler("clang", new PragmaDiagnosticHandler("clang"));
John McCall32f5fe12011-09-30 05:12:12 +00001274 AddPragmaHandler("clang", new PragmaARCCFCodeAuditedHandler());
Chris Lattnerb61448d2009-05-12 18:21:11 +00001275
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001276 AddPragmaHandler("STDC", new PragmaSTDC_FENV_ACCESSHandler());
1277 AddPragmaHandler("STDC", new PragmaSTDC_CX_LIMITED_RANGEHandler());
Chris Lattner958ee042009-04-19 21:20:35 +00001278 AddPragmaHandler("STDC", new PragmaSTDC_UnknownHandler());
Mike Stump11289f42009-09-09 15:08:12 +00001279
Chris Lattner2ff698d2009-01-16 08:21:25 +00001280 // MS extensions.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001281 if (LangOpts.MicrosoftExt) {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001282 AddPragmaHandler(new PragmaCommentHandler());
Aaron Ballman611306e2012-03-02 22:51:54 +00001283 AddPragmaHandler(new PragmaIncludeAliasHandler());
Aaron Ballman406ea512012-11-30 19:52:30 +00001284 AddPragmaHandler(new PragmaRegionHandler("region"));
1285 AddPragmaHandler(new PragmaRegionHandler("endregion"));
Chris Lattner30c924b2010-06-26 17:11:39 +00001286 }
Chris Lattnerb694ba72006-07-02 22:41:36 +00001287}