blob: b5a76fd3cb223563db26007508f43451053123b0 [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.
Jordan Rose111c4a62013-04-17 19:09:18 +0000437 SourceMgr.AddLineNote(SysHeaderTok.getLocation(), PLoc.getLine()+1,
438 FilenameID, /*IsEntry=*/false, /*IsExit=*/false,
439 /*IsSystem=*/true, /*IsExternC=*/false);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000440}
441
James Dennett18a6d792012-06-17 03:26:26 +0000442/// HandlePragmaDependency - Handle \#pragma GCC dependency "foo" blah.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000443///
Chris Lattner146762e2007-07-20 16:59:19 +0000444void Preprocessor::HandlePragmaDependency(Token &DependencyTok) {
445 Token FilenameTok;
Ted Kremenek551c82a2008-11-18 01:12:54 +0000446 CurPPLexer->LexIncludeFilename(FilenameTok);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000447
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000448 // If the token kind is EOD, the error has already been diagnosed.
449 if (FilenameTok.is(tok::eod))
Chris Lattnerb694ba72006-07-02 22:41:36 +0000450 return;
Mike Stump11289f42009-09-09 15:08:12 +0000451
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000452 // Reserve a buffer to get the spelling.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000453 SmallString<128> FilenameBuffer;
Douglas Gregordc970f02010-03-16 22:30:13 +0000454 bool Invalid = false;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000455 StringRef Filename = getSpelling(FilenameTok, FilenameBuffer, &Invalid);
Douglas Gregordc970f02010-03-16 22:30:13 +0000456 if (Invalid)
457 return;
Mike Stump11289f42009-09-09 15:08:12 +0000458
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000459 bool isAngled =
460 GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000461 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
462 // error.
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000463 if (Filename.empty())
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000464 return;
Mike Stump11289f42009-09-09 15:08:12 +0000465
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000466 // Search include directories for this file.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000467 const DirectoryLookup *CurDir;
Douglas Gregor97eec242011-09-15 22:00:41 +0000468 const FileEntry *File = LookupFile(Filename, isAngled, 0, CurDir, NULL, NULL,
469 NULL);
Chris Lattner97b8e842008-11-18 08:02:48 +0000470 if (File == 0) {
Eli Friedman3781a362011-08-30 23:07:51 +0000471 if (!SuppressIncludeNotFoundError)
472 Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
Chris Lattner97b8e842008-11-18 08:02:48 +0000473 return;
474 }
Mike Stump11289f42009-09-09 15:08:12 +0000475
Chris Lattnerd32480d2009-01-17 06:22:33 +0000476 const FileEntry *CurFile = getCurrentFileLexer()->getFileEntry();
Chris Lattnerb694ba72006-07-02 22:41:36 +0000477
478 // If this file is older than the file it depends on, emit a diagnostic.
479 if (CurFile && CurFile->getModificationTime() < File->getModificationTime()) {
480 // Lex tokens at the end of the message and include them in the message.
481 std::string Message;
482 Lex(DependencyTok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000483 while (DependencyTok.isNot(tok::eod)) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000484 Message += getSpelling(DependencyTok) + " ";
485 Lex(DependencyTok);
486 }
Mike Stump11289f42009-09-09 15:08:12 +0000487
Chris Lattnerf0b04972010-09-05 23:16:09 +0000488 // Remove the trailing ' ' if present.
489 if (!Message.empty())
490 Message.erase(Message.end()-1);
Chris Lattner97b8e842008-11-18 08:02:48 +0000491 Diag(FilenameTok, diag::pp_out_of_date_dependency) << Message;
Chris Lattnerb694ba72006-07-02 22:41:36 +0000492 }
493}
494
James Dennett18a6d792012-06-17 03:26:26 +0000495/// \brief Handle the microsoft \#pragma comment extension.
496///
497/// The syntax is:
498/// \code
Dmitri Gribenko9ebd1612012-11-30 20:04:39 +0000499/// #pragma comment(linker, "foo")
James Dennett18a6d792012-06-17 03:26:26 +0000500/// \endcode
Chris Lattner2ff698d2009-01-16 08:21:25 +0000501/// 'linker' is one of five identifiers: compiler, exestr, lib, linker, user.
502/// "foo" is a string, which is fully macro expanded, and permits string
Gabor Greif31a082f2009-03-17 11:39:38 +0000503/// concatenation, embedded escape characters etc. See MSDN for more details.
Chris Lattner2ff698d2009-01-16 08:21:25 +0000504void Preprocessor::HandlePragmaComment(Token &Tok) {
505 SourceLocation CommentLoc = Tok.getLocation();
506 Lex(Tok);
507 if (Tok.isNot(tok::l_paren)) {
508 Diag(CommentLoc, diag::err_pragma_comment_malformed);
509 return;
510 }
Mike Stump11289f42009-09-09 15:08:12 +0000511
Chris Lattner2ff698d2009-01-16 08:21:25 +0000512 // Read the identifier.
513 Lex(Tok);
514 if (Tok.isNot(tok::identifier)) {
515 Diag(CommentLoc, diag::err_pragma_comment_malformed);
516 return;
517 }
Mike Stump11289f42009-09-09 15:08:12 +0000518
Chris Lattner2ff698d2009-01-16 08:21:25 +0000519 // Verify that this is one of the 5 whitelisted options.
520 // FIXME: warn that 'exestr' is deprecated.
521 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000522 if (!II->isStr("compiler") && !II->isStr("exestr") && !II->isStr("lib") &&
Chris Lattner2ff698d2009-01-16 08:21:25 +0000523 !II->isStr("linker") && !II->isStr("user")) {
524 Diag(Tok.getLocation(), diag::err_pragma_comment_unknown_kind);
525 return;
526 }
Mike Stump11289f42009-09-09 15:08:12 +0000527
Chris Lattner262d4e32009-01-16 18:59:23 +0000528 // Read the optional string if present.
Chris Lattner2ff698d2009-01-16 08:21:25 +0000529 Lex(Tok);
Chris Lattner262d4e32009-01-16 18:59:23 +0000530 std::string ArgumentString;
Andy Gibbs58905d22012-11-17 19:15:38 +0000531 if (Tok.is(tok::comma) && !LexStringLiteral(Tok, ArgumentString,
Andy Gibbsa8df57a2012-11-17 19:16:52 +0000532 "pragma comment",
Andy Gibbs58905d22012-11-17 19:15:38 +0000533 /*MacroExpansion=*/true))
534 return;
Mike Stump11289f42009-09-09 15:08:12 +0000535
Chris Lattner262d4e32009-01-16 18:59:23 +0000536 // FIXME: If the kind is "compiler" warn if the string is present (it is
537 // ignored).
538 // FIXME: 'lib' requires a comment string.
539 // FIXME: 'linker' requires a comment string, and has a specific list of
540 // things that are allowable.
Mike Stump11289f42009-09-09 15:08:12 +0000541
Chris Lattner2ff698d2009-01-16 08:21:25 +0000542 if (Tok.isNot(tok::r_paren)) {
543 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
544 return;
545 }
Chris Lattner262d4e32009-01-16 18:59:23 +0000546 Lex(Tok); // eat the r_paren.
Chris Lattner2ff698d2009-01-16 08:21:25 +0000547
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000548 if (Tok.isNot(tok::eod)) {
Chris Lattner2ff698d2009-01-16 08:21:25 +0000549 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
550 return;
551 }
Mike Stump11289f42009-09-09 15:08:12 +0000552
Chris Lattner262d4e32009-01-16 18:59:23 +0000553 // If the pragma is lexically sound, notify any interested PPCallbacks.
Chris Lattnerf49775d2009-01-16 19:01:46 +0000554 if (Callbacks)
555 Callbacks->PragmaComment(CommentLoc, II, ArgumentString);
Chris Lattner2ff698d2009-01-16 08:21:25 +0000556}
557
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000558/// ParsePragmaPushOrPopMacro - Handle parsing of pragma push_macro/pop_macro.
559/// Return the IdentifierInfo* associated with the macro to push or pop.
560IdentifierInfo *Preprocessor::ParsePragmaPushOrPopMacro(Token &Tok) {
561 // Remember the pragma token location.
562 Token PragmaTok = Tok;
563
564 // Read the '('.
565 Lex(Tok);
566 if (Tok.isNot(tok::l_paren)) {
567 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
568 << getSpelling(PragmaTok);
569 return 0;
570 }
571
572 // Read the macro name string.
573 Lex(Tok);
574 if (Tok.isNot(tok::string_literal)) {
575 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
576 << getSpelling(PragmaTok);
577 return 0;
578 }
579
Richard Smithd67aea22012-03-06 03:21:47 +0000580 if (Tok.hasUDSuffix()) {
581 Diag(Tok, diag::err_invalid_string_udl);
582 return 0;
583 }
584
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000585 // Remember the macro string.
586 std::string StrVal = getSpelling(Tok);
587
588 // Read the ')'.
589 Lex(Tok);
590 if (Tok.isNot(tok::r_paren)) {
591 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
592 << getSpelling(PragmaTok);
593 return 0;
594 }
595
596 assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
597 "Invalid string token!");
598
599 // Create a Token from the string.
600 Token MacroTok;
601 MacroTok.startToken();
Abramo Bagnaraea4f7c72010-12-22 08:23:18 +0000602 MacroTok.setKind(tok::raw_identifier);
Dmitri Gribenkob8e9e752012-09-24 21:07:17 +0000603 CreateString(StringRef(&StrVal[1], StrVal.size() - 2), MacroTok);
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000604
605 // Get the IdentifierInfo of MacroToPushTok.
606 return LookUpIdentifierInfo(MacroTok);
607}
608
James Dennett18a6d792012-06-17 03:26:26 +0000609/// \brief Handle \#pragma push_macro.
610///
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000611/// The syntax is:
James Dennett18a6d792012-06-17 03:26:26 +0000612/// \code
Dmitri Gribenko9ebd1612012-11-30 20:04:39 +0000613/// #pragma push_macro("macro")
James Dennett18a6d792012-06-17 03:26:26 +0000614/// \endcode
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000615void Preprocessor::HandlePragmaPushMacro(Token &PushMacroTok) {
616 // Parse the pragma directive and get the macro IdentifierInfo*.
617 IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PushMacroTok);
618 if (!IdentInfo) return;
619
620 // Get the MacroInfo associated with IdentInfo.
621 MacroInfo *MI = getMacroInfo(IdentInfo);
622
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000623 if (MI) {
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000624 // Allow the original MacroInfo to be redefined later.
625 MI->setIsAllowRedefinitionsWithoutWarning(true);
626 }
627
628 // Push the cloned MacroInfo so we can retrieve it later.
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +0000629 PragmaPushMacroInfo[IdentInfo].push_back(MI);
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000630}
631
James Dennett18a6d792012-06-17 03:26:26 +0000632/// \brief Handle \#pragma pop_macro.
633///
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000634/// The syntax is:
James Dennett18a6d792012-06-17 03:26:26 +0000635/// \code
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000636/// #pragma pop_macro("macro")
James Dennett18a6d792012-06-17 03:26:26 +0000637/// \endcode
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000638void Preprocessor::HandlePragmaPopMacro(Token &PopMacroTok) {
639 SourceLocation MessageLoc = PopMacroTok.getLocation();
640
641 // Parse the pragma directive and get the macro IdentifierInfo*.
642 IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PopMacroTok);
643 if (!IdentInfo) return;
644
645 // Find the vector<MacroInfo*> associated with the macro.
646 llvm::DenseMap<IdentifierInfo*, std::vector<MacroInfo*> >::iterator iter =
647 PragmaPushMacroInfo.find(IdentInfo);
648 if (iter != PragmaPushMacroInfo.end()) {
Alexander Kornienko8b3f6232012-08-29 00:20:03 +0000649 // Forget the MacroInfo currently associated with IdentInfo.
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +0000650 if (MacroDirective *CurrentMD = getMacroDirective(IdentInfo)) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +0000651 MacroInfo *MI = CurrentMD->getMacroInfo();
652 if (MI->isWarnIfUnused())
653 WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());
654 appendMacroDirective(IdentInfo, AllocateUndefMacroDirective(MessageLoc));
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000655 }
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000656
657 // Get the MacroInfo we want to reinstall.
658 MacroInfo *MacroToReInstall = iter->second.back();
659
Alexander Kornienkoc0b49282012-08-29 16:56:24 +0000660 if (MacroToReInstall) {
661 // Reinstall the previously pushed macro.
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +0000662 appendDefMacroDirective(IdentInfo, MacroToReInstall, MessageLoc,
663 /*isImported=*/false);
Alexander Kornienkoc0b49282012-08-29 16:56:24 +0000664 }
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000665
666 // Pop PragmaPushMacroInfo stack.
667 iter->second.pop_back();
668 if (iter->second.size() == 0)
669 PragmaPushMacroInfo.erase(iter);
670 } else {
671 Diag(MessageLoc, diag::warn_pragma_pop_macro_no_push)
672 << IdentInfo->getName();
673 }
674}
Chris Lattnerb694ba72006-07-02 22:41:36 +0000675
Aaron Ballman611306e2012-03-02 22:51:54 +0000676void Preprocessor::HandlePragmaIncludeAlias(Token &Tok) {
677 // We will either get a quoted filename or a bracketed filename, and we
678 // have to track which we got. The first filename is the source name,
679 // and the second name is the mapped filename. If the first is quoted,
680 // the second must be as well (cannot mix and match quotes and brackets).
Aaron Ballman611306e2012-03-02 22:51:54 +0000681
682 // Get the open paren
683 Lex(Tok);
684 if (Tok.isNot(tok::l_paren)) {
685 Diag(Tok, diag::warn_pragma_include_alias_expected) << "(";
686 return;
687 }
688
689 // We expect either a quoted string literal, or a bracketed name
690 Token SourceFilenameTok;
691 CurPPLexer->LexIncludeFilename(SourceFilenameTok);
692 if (SourceFilenameTok.is(tok::eod)) {
693 // The diagnostic has already been handled
694 return;
695 }
696
697 StringRef SourceFileName;
698 SmallString<128> FileNameBuffer;
699 if (SourceFilenameTok.is(tok::string_literal) ||
700 SourceFilenameTok.is(tok::angle_string_literal)) {
701 SourceFileName = getSpelling(SourceFilenameTok, FileNameBuffer);
702 } else if (SourceFilenameTok.is(tok::less)) {
703 // This could be a path instead of just a name
704 FileNameBuffer.push_back('<');
705 SourceLocation End;
706 if (ConcatenateIncludeName(FileNameBuffer, End))
707 return; // Diagnostic already emitted
708 SourceFileName = FileNameBuffer.str();
709 } else {
710 Diag(Tok, diag::warn_pragma_include_alias_expected_filename);
711 return;
712 }
713 FileNameBuffer.clear();
714
715 // Now we expect a comma, followed by another include name
716 Lex(Tok);
717 if (Tok.isNot(tok::comma)) {
718 Diag(Tok, diag::warn_pragma_include_alias_expected) << ",";
719 return;
720 }
721
722 Token ReplaceFilenameTok;
723 CurPPLexer->LexIncludeFilename(ReplaceFilenameTok);
724 if (ReplaceFilenameTok.is(tok::eod)) {
725 // The diagnostic has already been handled
726 return;
727 }
728
729 StringRef ReplaceFileName;
730 if (ReplaceFilenameTok.is(tok::string_literal) ||
731 ReplaceFilenameTok.is(tok::angle_string_literal)) {
732 ReplaceFileName = getSpelling(ReplaceFilenameTok, FileNameBuffer);
733 } else if (ReplaceFilenameTok.is(tok::less)) {
734 // This could be a path instead of just a name
735 FileNameBuffer.push_back('<');
736 SourceLocation End;
737 if (ConcatenateIncludeName(FileNameBuffer, End))
738 return; // Diagnostic already emitted
739 ReplaceFileName = FileNameBuffer.str();
740 } else {
741 Diag(Tok, diag::warn_pragma_include_alias_expected_filename);
742 return;
743 }
744
745 // Finally, we expect the closing paren
746 Lex(Tok);
747 if (Tok.isNot(tok::r_paren)) {
748 Diag(Tok, diag::warn_pragma_include_alias_expected) << ")";
749 return;
750 }
751
752 // Now that we have the source and target filenames, we need to make sure
753 // they're both of the same type (angled vs non-angled)
754 StringRef OriginalSource = SourceFileName;
755
756 bool SourceIsAngled =
757 GetIncludeFilenameSpelling(SourceFilenameTok.getLocation(),
758 SourceFileName);
759 bool ReplaceIsAngled =
760 GetIncludeFilenameSpelling(ReplaceFilenameTok.getLocation(),
761 ReplaceFileName);
762 if (!SourceFileName.empty() && !ReplaceFileName.empty() &&
763 (SourceIsAngled != ReplaceIsAngled)) {
764 unsigned int DiagID;
765 if (SourceIsAngled)
766 DiagID = diag::warn_pragma_include_alias_mismatch_angle;
767 else
768 DiagID = diag::warn_pragma_include_alias_mismatch_quote;
769
770 Diag(SourceFilenameTok.getLocation(), DiagID)
771 << SourceFileName
772 << ReplaceFileName;
773
774 return;
775 }
776
777 // Now we can let the include handler know about this mapping
778 getHeaderSearchInfo().AddIncludeAlias(OriginalSource, ReplaceFileName);
779}
780
Chris Lattnerb694ba72006-07-02 22:41:36 +0000781/// AddPragmaHandler - Add the specified pragma handler to the preprocessor.
782/// If 'Namespace' is non-null, then it is a token required to exist on the
783/// pragma line before the pragma string starts, e.g. "STDC" or "GCC".
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000784void Preprocessor::AddPragmaHandler(StringRef Namespace,
Chris Lattnerb694ba72006-07-02 22:41:36 +0000785 PragmaHandler *Handler) {
786 PragmaNamespace *InsertNS = PragmaHandlers;
Mike Stump11289f42009-09-09 15:08:12 +0000787
Chris Lattnerb694ba72006-07-02 22:41:36 +0000788 // If this is specified to be in a namespace, step down into it.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000789 if (!Namespace.empty()) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000790 // If there is already a pragma handler with the name of this namespace,
791 // we either have an error (directive with the same name as a namespace) or
792 // we already have the namespace to insert into.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000793 if (PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace)) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000794 InsertNS = Existing->getIfNamespace();
795 assert(InsertNS != 0 && "Cannot have a pragma namespace and pragma"
796 " handler with the same name!");
797 } else {
798 // Otherwise, this namespace doesn't exist yet, create and insert the
799 // handler for it.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000800 InsertNS = new PragmaNamespace(Namespace);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000801 PragmaHandlers->AddPragma(InsertNS);
802 }
803 }
Mike Stump11289f42009-09-09 15:08:12 +0000804
Chris Lattnerb694ba72006-07-02 22:41:36 +0000805 // Check to make sure we don't already have a pragma for this identifier.
806 assert(!InsertNS->FindHandler(Handler->getName()) &&
807 "Pragma handler already exists for this identifier!");
808 InsertNS->AddPragma(Handler);
809}
810
Daniel Dunbar40596532008-10-04 19:17:46 +0000811/// RemovePragmaHandler - Remove the specific pragma handler from the
812/// preprocessor. If \arg Namespace is non-null, then it should be the
813/// namespace that \arg Handler was added to. It is an error to remove
814/// a handler that has not been registered.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000815void Preprocessor::RemovePragmaHandler(StringRef Namespace,
Daniel Dunbar40596532008-10-04 19:17:46 +0000816 PragmaHandler *Handler) {
817 PragmaNamespace *NS = PragmaHandlers;
Mike Stump11289f42009-09-09 15:08:12 +0000818
Daniel Dunbar40596532008-10-04 19:17:46 +0000819 // If this is specified to be in a namespace, step down into it.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000820 if (!Namespace.empty()) {
821 PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace);
Daniel Dunbar40596532008-10-04 19:17:46 +0000822 assert(Existing && "Namespace containing handler does not exist!");
823
824 NS = Existing->getIfNamespace();
825 assert(NS && "Invalid namespace, registered as a regular pragma handler!");
826 }
827
828 NS->RemovePragmaHandler(Handler);
Mike Stump11289f42009-09-09 15:08:12 +0000829
Daniel Dunbar40596532008-10-04 19:17:46 +0000830 // If this is a non-default namespace and it is now empty, remove
831 // it.
Argyrios Kyrtzidis7ce75262012-01-06 00:22:09 +0000832 if (NS != PragmaHandlers && NS->IsEmpty()) {
Daniel Dunbar40596532008-10-04 19:17:46 +0000833 PragmaHandlers->RemovePragmaHandler(NS);
Argyrios Kyrtzidis7ce75262012-01-06 00:22:09 +0000834 delete NS;
835 }
Daniel Dunbar40596532008-10-04 19:17:46 +0000836}
837
Peter Collingbourne3bffa522011-02-14 01:42:24 +0000838bool Preprocessor::LexOnOffSwitch(tok::OnOffSwitch &Result) {
839 Token Tok;
840 LexUnexpandedToken(Tok);
841
842 if (Tok.isNot(tok::identifier)) {
843 Diag(Tok, diag::ext_on_off_switch_syntax);
844 return true;
845 }
846 IdentifierInfo *II = Tok.getIdentifierInfo();
847 if (II->isStr("ON"))
848 Result = tok::OOS_ON;
849 else if (II->isStr("OFF"))
850 Result = tok::OOS_OFF;
851 else if (II->isStr("DEFAULT"))
852 Result = tok::OOS_DEFAULT;
853 else {
854 Diag(Tok, diag::ext_on_off_switch_syntax);
855 return true;
856 }
857
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000858 // Verify that this is followed by EOD.
Peter Collingbourne3bffa522011-02-14 01:42:24 +0000859 LexUnexpandedToken(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000860 if (Tok.isNot(tok::eod))
861 Diag(Tok, diag::ext_pragma_syntax_eod);
Peter Collingbourne3bffa522011-02-14 01:42:24 +0000862 return false;
863}
864
Chris Lattnerb694ba72006-07-02 22:41:36 +0000865namespace {
James Dennett18a6d792012-06-17 03:26:26 +0000866/// PragmaOnceHandler - "\#pragma once" marks the file as atomically included.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000867struct PragmaOnceHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000868 PragmaOnceHandler() : PragmaHandler("once") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000869 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
870 Token &OnceTok) {
Chris Lattnerce2ab6f2009-04-14 05:07:49 +0000871 PP.CheckEndOfDirective("pragma once");
Chris Lattnerb694ba72006-07-02 22:41:36 +0000872 PP.HandlePragmaOnce(OnceTok);
873 }
874};
875
James Dennett18a6d792012-06-17 03:26:26 +0000876/// PragmaMarkHandler - "\#pragma mark ..." is ignored by the compiler, and the
Chris Lattnerc2383312007-12-19 19:38:36 +0000877/// rest of the line is not lexed.
878struct PragmaMarkHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000879 PragmaMarkHandler() : PragmaHandler("mark") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000880 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
881 Token &MarkTok) {
Chris Lattnerc2383312007-12-19 19:38:36 +0000882 PP.HandlePragmaMark();
883 }
884};
885
James Dennett18a6d792012-06-17 03:26:26 +0000886/// PragmaPoisonHandler - "\#pragma poison x" marks x as not usable.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000887struct PragmaPoisonHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000888 PragmaPoisonHandler() : PragmaHandler("poison") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000889 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
890 Token &PoisonTok) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000891 PP.HandlePragmaPoison(PoisonTok);
892 }
893};
894
James Dennett18a6d792012-06-17 03:26:26 +0000895/// PragmaSystemHeaderHandler - "\#pragma system_header" marks the current file
Chris Lattnerc2383312007-12-19 19:38:36 +0000896/// as a system header, which silences warnings in it.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000897struct PragmaSystemHeaderHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000898 PragmaSystemHeaderHandler() : PragmaHandler("system_header") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000899 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
900 Token &SHToken) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000901 PP.HandlePragmaSystemHeader(SHToken);
Chris Lattnerce2ab6f2009-04-14 05:07:49 +0000902 PP.CheckEndOfDirective("pragma");
Chris Lattnerb694ba72006-07-02 22:41:36 +0000903 }
904};
905struct PragmaDependencyHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000906 PragmaDependencyHandler() : PragmaHandler("dependency") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000907 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
908 Token &DepToken) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000909 PP.HandlePragmaDependency(DepToken);
910 }
911};
Mike Stump11289f42009-09-09 15:08:12 +0000912
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000913struct PragmaDebugHandler : public PragmaHandler {
914 PragmaDebugHandler() : PragmaHandler("__debug") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000915 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
916 Token &DepToken) {
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000917 Token Tok;
918 PP.LexUnexpandedToken(Tok);
919 if (Tok.isNot(tok::identifier)) {
Douglas Gregor3cc26482010-08-30 15:15:34 +0000920 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000921 return;
922 }
923 IdentifierInfo *II = Tok.getIdentifierInfo();
924
Daniel Dunbarf2cf3292010-08-17 22:32:48 +0000925 if (II->isStr("assert")) {
David Blaikie83d382b2011-09-23 05:06:16 +0000926 llvm_unreachable("This is an assertion!");
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000927 } else if (II->isStr("crash")) {
David Blaikie5bd4c2a2012-08-21 18:56:49 +0000928 LLVM_BUILTIN_TRAP;
David Blaikie5d577a22012-06-29 22:03:56 +0000929 } else if (II->isStr("parser_crash")) {
930 Token Crasher;
931 Crasher.setKind(tok::annot_pragma_parser_crash);
932 PP.EnterToken(Crasher);
Daniel Dunbarf2cf3292010-08-17 22:32:48 +0000933 } else if (II->isStr("llvm_fatal_error")) {
934 llvm::report_fatal_error("#pragma clang __debug llvm_fatal_error");
935 } else if (II->isStr("llvm_unreachable")) {
936 llvm_unreachable("#pragma clang __debug llvm_unreachable");
937 } else if (II->isStr("overflow_stack")) {
938 DebugOverflowStack();
Daniel Dunbar211a7872010-08-18 23:09:23 +0000939 } else if (II->isStr("handle_crash")) {
940 llvm::CrashRecoveryContext *CRC =llvm::CrashRecoveryContext::GetCurrent();
941 if (CRC)
942 CRC->HandleCrash();
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000943 } else if (II->isStr("captured")) {
944 HandleCaptured(PP);
Daniel Dunbarf2cf3292010-08-17 22:32:48 +0000945 } else {
946 PP.Diag(Tok, diag::warn_pragma_debug_unexpected_command)
947 << II->getName();
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000948 }
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000949
950 PPCallbacks *Callbacks = PP.getPPCallbacks();
951 if (Callbacks)
952 Callbacks->PragmaDebug(Tok.getLocation(), II->getName());
953 }
954
955 void HandleCaptured(Preprocessor &PP) {
956 // Skip if emitting preprocessed output.
957 if (PP.isPreprocessedOutput())
958 return;
959
960 Token Tok;
961 PP.LexUnexpandedToken(Tok);
962
963 if (Tok.isNot(tok::eod)) {
964 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol)
965 << "pragma clang __debug captured";
966 return;
967 }
968
969 SourceLocation NameLoc = Tok.getLocation();
970 Token *Toks = PP.getPreprocessorAllocator().Allocate<Token>(1);
971 Toks->startToken();
972 Toks->setKind(tok::annot_pragma_captured);
973 Toks->setLocation(NameLoc);
974
975 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
976 /*OwnsTokens=*/false);
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000977 }
978
Francois Pichet2e11f5d2011-05-25 16:15:03 +0000979// Disable MSVC warning about runtime stack overflow.
980#ifdef _MSC_VER
981 #pragma warning(disable : 4717)
982#endif
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000983 void DebugOverflowStack() {
984 DebugOverflowStack();
985 }
Francois Pichet2e11f5d2011-05-25 16:15:03 +0000986#ifdef _MSC_VER
987 #pragma warning(default : 4717)
988#endif
989
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000990};
991
James Dennett18a6d792012-06-17 03:26:26 +0000992/// PragmaDiagnosticHandler - e.g. '\#pragma GCC diagnostic ignored "-Wformat"'
Chris Lattner504af112009-04-19 23:16:58 +0000993struct PragmaDiagnosticHandler : public PragmaHandler {
Douglas Gregor3bde9b12011-06-22 19:41:48 +0000994private:
995 const char *Namespace;
Chris Lattnerfb42a182009-07-12 21:18:45 +0000996public:
Douglas Gregor3bde9b12011-06-22 19:41:48 +0000997 explicit PragmaDiagnosticHandler(const char *NS) :
998 PragmaHandler("diagnostic"), Namespace(NS) {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000999 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1000 Token &DiagToken) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +00001001 SourceLocation DiagLoc = DiagToken.getLocation();
Chris Lattner504af112009-04-19 23:16:58 +00001002 Token Tok;
1003 PP.LexUnexpandedToken(Tok);
1004 if (Tok.isNot(tok::identifier)) {
Douglas Gregor3cc26482010-08-30 15:15:34 +00001005 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Chris Lattner504af112009-04-19 23:16:58 +00001006 return;
1007 }
1008 IdentifierInfo *II = Tok.getIdentifierInfo();
Douglas Gregor3bde9b12011-06-22 19:41:48 +00001009 PPCallbacks *Callbacks = PP.getPPCallbacks();
Mike Stump11289f42009-09-09 15:08:12 +00001010
Chris Lattner504af112009-04-19 23:16:58 +00001011 diag::Mapping Map;
1012 if (II->isStr("warning"))
1013 Map = diag::MAP_WARNING;
1014 else if (II->isStr("error"))
1015 Map = diag::MAP_ERROR;
1016 else if (II->isStr("ignored"))
1017 Map = diag::MAP_IGNORE;
1018 else if (II->isStr("fatal"))
1019 Map = diag::MAP_FATAL;
Douglas Gregor3cc26482010-08-30 15:15:34 +00001020 else if (II->isStr("pop")) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +00001021 if (!PP.getDiagnostics().popMappings(DiagLoc))
Douglas Gregor3cc26482010-08-30 15:15:34 +00001022 PP.Diag(Tok, diag::warn_pragma_diagnostic_cannot_pop);
Douglas Gregor3bde9b12011-06-22 19:41:48 +00001023 else if (Callbacks)
1024 Callbacks->PragmaDiagnosticPop(DiagLoc, Namespace);
Douglas Gregor3cc26482010-08-30 15:15:34 +00001025 return;
1026 } else if (II->isStr("push")) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +00001027 PP.getDiagnostics().pushMappings(DiagLoc);
Douglas Gregor3bde9b12011-06-22 19:41:48 +00001028 if (Callbacks)
1029 Callbacks->PragmaDiagnosticPush(DiagLoc, Namespace);
Chris Lattnerfb42a182009-07-12 21:18:45 +00001030 return;
1031 } else {
Douglas Gregor3cc26482010-08-30 15:15:34 +00001032 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Chris Lattner504af112009-04-19 23:16:58 +00001033 return;
1034 }
Mike Stump11289f42009-09-09 15:08:12 +00001035
Chris Lattner504af112009-04-19 23:16:58 +00001036 PP.LexUnexpandedToken(Tok);
Andy Gibbs58905d22012-11-17 19:15:38 +00001037 SourceLocation StringLoc = Tok.getLocation();
Chris Lattner504af112009-04-19 23:16:58 +00001038
Andy Gibbs58905d22012-11-17 19:15:38 +00001039 std::string WarningName;
Andy Gibbsa8df57a2012-11-17 19:16:52 +00001040 if (!PP.FinishLexStringLiteral(Tok, WarningName, "pragma diagnostic",
1041 /*MacroExpansion=*/false))
Chris Lattner504af112009-04-19 23:16:58 +00001042 return;
Mike Stump11289f42009-09-09 15:08:12 +00001043
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001044 if (Tok.isNot(tok::eod)) {
Chris Lattner504af112009-04-19 23:16:58 +00001045 PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token);
1046 return;
1047 }
Mike Stump11289f42009-09-09 15:08:12 +00001048
Chris Lattner504af112009-04-19 23:16:58 +00001049 if (WarningName.size() < 3 || WarningName[0] != '-' ||
1050 WarningName[1] != 'W') {
Andy Gibbs58905d22012-11-17 19:15:38 +00001051 PP.Diag(StringLoc, diag::warn_pragma_diagnostic_invalid_option);
Chris Lattner504af112009-04-19 23:16:58 +00001052 return;
1053 }
Mike Stump11289f42009-09-09 15:08:12 +00001054
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +00001055 if (PP.getDiagnostics().setDiagnosticGroupMapping(WarningName.substr(2),
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +00001056 Map, DiagLoc))
Andy Gibbs58905d22012-11-17 19:15:38 +00001057 PP.Diag(StringLoc, diag::warn_pragma_diagnostic_unknown_warning)
1058 << WarningName;
Douglas Gregor3bde9b12011-06-22 19:41:48 +00001059 else if (Callbacks)
1060 Callbacks->PragmaDiagnostic(DiagLoc, Namespace, Map, WarningName);
Chris Lattner504af112009-04-19 23:16:58 +00001061 }
1062};
Mike Stump11289f42009-09-09 15:08:12 +00001063
James Dennett18a6d792012-06-17 03:26:26 +00001064/// PragmaCommentHandler - "\#pragma comment ...".
Chris Lattner2ff698d2009-01-16 08:21:25 +00001065struct PragmaCommentHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001066 PragmaCommentHandler() : PragmaHandler("comment") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +00001067 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1068 Token &CommentTok) {
Chris Lattner2ff698d2009-01-16 08:21:25 +00001069 PP.HandlePragmaComment(CommentTok);
1070 }
1071};
Mike Stump11289f42009-09-09 15:08:12 +00001072
James Dennett18a6d792012-06-17 03:26:26 +00001073/// PragmaIncludeAliasHandler - "\#pragma include_alias("...")".
Aaron Ballman611306e2012-03-02 22:51:54 +00001074struct PragmaIncludeAliasHandler : public PragmaHandler {
1075 PragmaIncludeAliasHandler() : PragmaHandler("include_alias") {}
1076 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1077 Token &IncludeAliasTok) {
1078 PP.HandlePragmaIncludeAlias(IncludeAliasTok);
1079 }
1080};
1081
Andy Gibbs9c2ccd62013-04-17 16:16:16 +00001082/// PragmaMessageHandler - Handle the microsoft and gcc \#pragma message
1083/// extension. The syntax is:
1084/// \code
1085/// #pragma message(string)
1086/// \endcode
1087/// OR, in GCC mode:
1088/// \code
1089/// #pragma message string
1090/// \endcode
1091/// string is a string, which is fully macro expanded, and permits string
1092/// concatenation, embedded escape characters, etc... See MSDN for more details.
1093/// Also handles \#pragma GCC warning and \#pragma GCC error which take the same
1094/// form as \#pragma message.
Chris Lattner30c924b2010-06-26 17:11:39 +00001095struct PragmaMessageHandler : public PragmaHandler {
Andy Gibbs9c2ccd62013-04-17 16:16:16 +00001096private:
1097 const PPCallbacks::PragmaMessageKind Kind;
1098 const StringRef Namespace;
1099
1100 static const char* PragmaKind(PPCallbacks::PragmaMessageKind Kind,
1101 bool PragmaNameOnly = false) {
1102 switch (Kind) {
1103 case PPCallbacks::PMK_Message:
1104 return PragmaNameOnly ? "message" : "pragma message";
1105 case PPCallbacks::PMK_Warning:
1106 return PragmaNameOnly ? "warning" : "pragma warning";
1107 case PPCallbacks::PMK_Error:
1108 return PragmaNameOnly ? "error" : "pragma error";
1109 }
1110 llvm_unreachable("Unknown PragmaMessageKind!");
1111 }
1112
1113public:
1114 PragmaMessageHandler(PPCallbacks::PragmaMessageKind Kind,
1115 StringRef Namespace = StringRef())
1116 : PragmaHandler(PragmaKind(Kind, true)), Kind(Kind), Namespace(Namespace) {}
1117
Douglas Gregorc7d65762010-09-09 22:45:38 +00001118 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
Andy Gibbs9c2ccd62013-04-17 16:16:16 +00001119 Token &Tok) {
1120 SourceLocation MessageLoc = Tok.getLocation();
1121 PP.Lex(Tok);
1122 bool ExpectClosingParen = false;
1123 switch (Tok.getKind()) {
1124 case tok::l_paren:
1125 // We have a MSVC style pragma message.
1126 ExpectClosingParen = true;
1127 // Read the string.
1128 PP.Lex(Tok);
1129 break;
1130 case tok::string_literal:
1131 // We have a GCC style pragma message, and we just read the string.
1132 break;
1133 default:
1134 PP.Diag(MessageLoc, diag::err_pragma_message_malformed) << Kind;
1135 return;
1136 }
1137
1138 std::string MessageString;
1139 if (!PP.FinishLexStringLiteral(Tok, MessageString, PragmaKind(Kind),
1140 /*MacroExpansion=*/true))
1141 return;
1142
1143 if (ExpectClosingParen) {
1144 if (Tok.isNot(tok::r_paren)) {
1145 PP.Diag(Tok.getLocation(), diag::err_pragma_message_malformed) << Kind;
1146 return;
1147 }
1148 PP.Lex(Tok); // eat the r_paren.
1149 }
1150
1151 if (Tok.isNot(tok::eod)) {
1152 PP.Diag(Tok.getLocation(), diag::err_pragma_message_malformed) << Kind;
1153 return;
1154 }
1155
1156 // Output the message.
1157 PP.Diag(MessageLoc, (Kind == PPCallbacks::PMK_Error)
1158 ? diag::err_pragma_message
1159 : diag::warn_pragma_message) << MessageString;
1160
1161 // If the pragma is lexically sound, notify any interested PPCallbacks.
1162 if (PPCallbacks *Callbacks = PP.getPPCallbacks())
1163 Callbacks->PragmaMessage(MessageLoc, Namespace, Kind, MessageString);
Chris Lattner30c924b2010-06-26 17:11:39 +00001164 }
1165};
1166
James Dennett18a6d792012-06-17 03:26:26 +00001167/// PragmaPushMacroHandler - "\#pragma push_macro" saves the value of the
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001168/// macro on the top of the stack.
1169struct PragmaPushMacroHandler : public PragmaHandler {
1170 PragmaPushMacroHandler() : PragmaHandler("push_macro") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +00001171 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1172 Token &PushMacroTok) {
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001173 PP.HandlePragmaPushMacro(PushMacroTok);
1174 }
1175};
1176
1177
James Dennett18a6d792012-06-17 03:26:26 +00001178/// PragmaPopMacroHandler - "\#pragma pop_macro" sets the value of the
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001179/// macro to the value on the top of the stack.
1180struct PragmaPopMacroHandler : public PragmaHandler {
1181 PragmaPopMacroHandler() : PragmaHandler("pop_macro") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +00001182 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1183 Token &PopMacroTok) {
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001184 PP.HandlePragmaPopMacro(PopMacroTok);
1185 }
1186};
1187
Chris Lattner958ee042009-04-19 21:20:35 +00001188// Pragma STDC implementations.
Chris Lattner02ef4e32009-04-19 21:50:08 +00001189
James Dennett18a6d792012-06-17 03:26:26 +00001190/// PragmaSTDC_FENV_ACCESSHandler - "\#pragma STDC FENV_ACCESS ...".
Chris Lattner958ee042009-04-19 21:20:35 +00001191struct PragmaSTDC_FENV_ACCESSHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001192 PragmaSTDC_FENV_ACCESSHandler() : PragmaHandler("FENV_ACCESS") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +00001193 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1194 Token &Tok) {
Peter Collingbourne3bffa522011-02-14 01:42:24 +00001195 tok::OnOffSwitch OOS;
1196 if (PP.LexOnOffSwitch(OOS))
1197 return;
1198 if (OOS == tok::OOS_ON)
Chris Lattnerdf222682009-04-19 21:55:32 +00001199 PP.Diag(Tok, diag::warn_stdc_fenv_access_not_supported);
Chris Lattner958ee042009-04-19 21:20:35 +00001200 }
1201};
Mike Stump11289f42009-09-09 15:08:12 +00001202
James Dennett18a6d792012-06-17 03:26:26 +00001203/// PragmaSTDC_CX_LIMITED_RANGEHandler - "\#pragma STDC CX_LIMITED_RANGE ...".
Chris Lattner958ee042009-04-19 21:20:35 +00001204struct PragmaSTDC_CX_LIMITED_RANGEHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001205 PragmaSTDC_CX_LIMITED_RANGEHandler()
1206 : PragmaHandler("CX_LIMITED_RANGE") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +00001207 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1208 Token &Tok) {
Peter Collingbourne3bffa522011-02-14 01:42:24 +00001209 tok::OnOffSwitch OOS;
1210 PP.LexOnOffSwitch(OOS);
Chris Lattner958ee042009-04-19 21:20:35 +00001211 }
1212};
Mike Stump11289f42009-09-09 15:08:12 +00001213
James Dennett18a6d792012-06-17 03:26:26 +00001214/// PragmaSTDC_UnknownHandler - "\#pragma STDC ...".
Chris Lattner958ee042009-04-19 21:20:35 +00001215struct PragmaSTDC_UnknownHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001216 PragmaSTDC_UnknownHandler() {}
Douglas Gregorc7d65762010-09-09 22:45:38 +00001217 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1218 Token &UnknownTok) {
Chris Lattner02ef4e32009-04-19 21:50:08 +00001219 // C99 6.10.6p2, unknown forms are not allowed.
Chris Lattnera0b1f762009-04-19 21:25:37 +00001220 PP.Diag(UnknownTok, diag::ext_stdc_pragma_ignored);
Chris Lattner958ee042009-04-19 21:20:35 +00001221 }
1222};
Mike Stump11289f42009-09-09 15:08:12 +00001223
John McCall32f5fe12011-09-30 05:12:12 +00001224/// PragmaARCCFCodeAuditedHandler -
James Dennett18a6d792012-06-17 03:26:26 +00001225/// \#pragma clang arc_cf_code_audited begin/end
John McCall32f5fe12011-09-30 05:12:12 +00001226struct PragmaARCCFCodeAuditedHandler : public PragmaHandler {
1227 PragmaARCCFCodeAuditedHandler() : PragmaHandler("arc_cf_code_audited") {}
1228 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1229 Token &NameTok) {
1230 SourceLocation Loc = NameTok.getLocation();
1231 bool IsBegin;
1232
1233 Token Tok;
1234
1235 // Lex the 'begin' or 'end'.
1236 PP.LexUnexpandedToken(Tok);
1237 const IdentifierInfo *BeginEnd = Tok.getIdentifierInfo();
1238 if (BeginEnd && BeginEnd->isStr("begin")) {
1239 IsBegin = true;
1240 } else if (BeginEnd && BeginEnd->isStr("end")) {
1241 IsBegin = false;
1242 } else {
1243 PP.Diag(Tok.getLocation(), diag::err_pp_arc_cf_code_audited_syntax);
1244 return;
1245 }
1246
1247 // Verify that this is followed by EOD.
1248 PP.LexUnexpandedToken(Tok);
1249 if (Tok.isNot(tok::eod))
1250 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
1251
1252 // The start location of the active audit.
1253 SourceLocation BeginLoc = PP.getPragmaARCCFCodeAuditedLoc();
1254
1255 // The start location we want after processing this.
1256 SourceLocation NewLoc;
1257
1258 if (IsBegin) {
1259 // Complain about attempts to re-enter an audit.
1260 if (BeginLoc.isValid()) {
1261 PP.Diag(Loc, diag::err_pp_double_begin_of_arc_cf_code_audited);
1262 PP.Diag(BeginLoc, diag::note_pragma_entered_here);
1263 }
1264 NewLoc = Loc;
1265 } else {
1266 // Complain about attempts to leave an audit that doesn't exist.
1267 if (!BeginLoc.isValid()) {
1268 PP.Diag(Loc, diag::err_pp_unmatched_end_of_arc_cf_code_audited);
1269 return;
1270 }
1271 NewLoc = SourceLocation();
1272 }
1273
1274 PP.setPragmaARCCFCodeAuditedLoc(NewLoc);
1275 }
1276};
1277
Aaron Ballman406ea512012-11-30 19:52:30 +00001278 /// \brief Handle "\#pragma region [...]"
1279 ///
1280 /// The syntax is
1281 /// \code
Dmitri Gribenko9ebd1612012-11-30 20:04:39 +00001282 /// #pragma region [optional name]
1283 /// #pragma endregion [optional comment]
Aaron Ballman406ea512012-11-30 19:52:30 +00001284 /// \endcode
1285 ///
1286 /// \note This is
1287 /// <a href="http://msdn.microsoft.com/en-us/library/b6xkz944(v=vs.80).aspx">editor-only</a>
1288 /// pragma, just skipped by compiler.
1289 struct PragmaRegionHandler : public PragmaHandler {
1290 PragmaRegionHandler(const char *pragma) : PragmaHandler(pragma) { }
1291
1292 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1293 Token &NameTok) {
1294 // #pragma region: endregion matches can be verified
1295 // __pragma(region): no sense, but ignored by msvc
1296 // _Pragma is not valid for MSVC, but there isn't any point
1297 // to handle a _Pragma differently.
1298 }
1299 };
1300
Chris Lattnerb694ba72006-07-02 22:41:36 +00001301} // end anonymous namespace
1302
1303
1304/// RegisterBuiltinPragmas - Install the standard preprocessor pragmas:
James Dennett18a6d792012-06-17 03:26:26 +00001305/// \#pragma GCC poison/system_header/dependency and \#pragma once.
Chris Lattnerb694ba72006-07-02 22:41:36 +00001306void Preprocessor::RegisterBuiltinPragmas() {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001307 AddPragmaHandler(new PragmaOnceHandler());
1308 AddPragmaHandler(new PragmaMarkHandler());
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001309 AddPragmaHandler(new PragmaPushMacroHandler());
1310 AddPragmaHandler(new PragmaPopMacroHandler());
Andy Gibbs9c2ccd62013-04-17 16:16:16 +00001311 AddPragmaHandler(new PragmaMessageHandler(PPCallbacks::PMK_Message));
Mike Stump11289f42009-09-09 15:08:12 +00001312
Chris Lattnerb61448d2009-05-12 18:21:11 +00001313 // #pragma GCC ...
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001314 AddPragmaHandler("GCC", new PragmaPoisonHandler());
1315 AddPragmaHandler("GCC", new PragmaSystemHeaderHandler());
1316 AddPragmaHandler("GCC", new PragmaDependencyHandler());
Douglas Gregor3bde9b12011-06-22 19:41:48 +00001317 AddPragmaHandler("GCC", new PragmaDiagnosticHandler("GCC"));
Andy Gibbs9c2ccd62013-04-17 16:16:16 +00001318 AddPragmaHandler("GCC", new PragmaMessageHandler(PPCallbacks::PMK_Warning,
1319 "GCC"));
1320 AddPragmaHandler("GCC", new PragmaMessageHandler(PPCallbacks::PMK_Error,
1321 "GCC"));
Chris Lattnerb61448d2009-05-12 18:21:11 +00001322 // #pragma clang ...
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001323 AddPragmaHandler("clang", new PragmaPoisonHandler());
1324 AddPragmaHandler("clang", new PragmaSystemHeaderHandler());
Daniel Dunbarb8068c32010-07-28 15:40:33 +00001325 AddPragmaHandler("clang", new PragmaDebugHandler());
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001326 AddPragmaHandler("clang", new PragmaDependencyHandler());
Douglas Gregor3bde9b12011-06-22 19:41:48 +00001327 AddPragmaHandler("clang", new PragmaDiagnosticHandler("clang"));
John McCall32f5fe12011-09-30 05:12:12 +00001328 AddPragmaHandler("clang", new PragmaARCCFCodeAuditedHandler());
Chris Lattnerb61448d2009-05-12 18:21:11 +00001329
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001330 AddPragmaHandler("STDC", new PragmaSTDC_FENV_ACCESSHandler());
1331 AddPragmaHandler("STDC", new PragmaSTDC_CX_LIMITED_RANGEHandler());
Chris Lattner958ee042009-04-19 21:20:35 +00001332 AddPragmaHandler("STDC", new PragmaSTDC_UnknownHandler());
Mike Stump11289f42009-09-09 15:08:12 +00001333
Chris Lattner2ff698d2009-01-16 08:21:25 +00001334 // MS extensions.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001335 if (LangOpts.MicrosoftExt) {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001336 AddPragmaHandler(new PragmaCommentHandler());
Aaron Ballman611306e2012-03-02 22:51:54 +00001337 AddPragmaHandler(new PragmaIncludeAliasHandler());
Aaron Ballman406ea512012-11-30 19:52:30 +00001338 AddPragmaHandler(new PragmaRegionHandler("region"));
1339 AddPragmaHandler(new PragmaRegionHandler("endregion"));
Chris Lattner30c924b2010-06-26 17:11:39 +00001340 }
Chris Lattnerb694ba72006-07-02 22:41:36 +00001341}