blob: b2ae4c9c443e70dc89c7ef7ac83b586c6fb6df42 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Pragma.cpp - Pragma registration and handling --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the PragmaHandler/PragmaTable interfaces and implements
11// pragma related methods of the Preprocessor class.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Lex/Pragma.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000016#include "clang/Basic/FileManager.h"
17#include "clang/Basic/SourceManager.h"
Chandler Carruth55fc8732012-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 Dunbarff759a62010-08-18 23:09:23 +000023#include "llvm/Support/CrashRecoveryContext.h"
Daniel Dunbar55054132010-08-17 22:32:48 +000024#include "llvm/Support/ErrorHandling.h"
Douglas Gregor2e222532009-07-02 17:08:52 +000025#include <algorithm>
Reid Spencer5f016e22007-07-11 17:01:13 +000026using namespace clang;
27
28// Out-of-line destructor to provide a home for the class.
29PragmaHandler::~PragmaHandler() {
30}
31
32//===----------------------------------------------------------------------===//
Daniel Dunbarc72cc502010-06-11 20:10:12 +000033// EmptyPragmaHandler Implementation.
34//===----------------------------------------------------------------------===//
35
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +000036EmptyPragmaHandler::EmptyPragmaHandler() {}
Daniel Dunbarc72cc502010-06-11 20:10:12 +000037
Douglas Gregor80c60f72010-09-09 22:45:38 +000038void EmptyPragmaHandler::HandlePragma(Preprocessor &PP,
39 PragmaIntroducerKind Introducer,
40 Token &FirstToken) {}
Daniel Dunbarc72cc502010-06-11 20:10:12 +000041
42//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +000043// PragmaNamespace Implementation.
44//===----------------------------------------------------------------------===//
45
Reid Spencer5f016e22007-07-11 17:01:13 +000046PragmaNamespace::~PragmaNamespace() {
Argyrios Kyrtzidis9b36c3f2010-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;
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner5f9e2722011-07-23 10:55:15 +000056PragmaHandler *PragmaNamespace::FindHandler(StringRef Name,
Reid Spencer5f016e22007-07-11 17:01:13 +000057 bool IgnoreNull) const {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +000058 if (PragmaHandler *Handler = Handlers.lookup(Name))
59 return Handler;
Chris Lattner5f9e2722011-07-23 10:55:15 +000060 return IgnoreNull ? 0 : Handlers.lookup(StringRef());
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +000061}
Mike Stump1eb44332009-09-09 15:08:12 +000062
Argyrios Kyrtzidis9b36c3f2010-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);
Reid Spencer5f016e22007-07-11 17:01:13 +000069}
70
Daniel Dunbar40950802008-10-04 19:17:46 +000071void PragmaNamespace::RemovePragmaHandler(PragmaHandler *Handler) {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +000072 assert(Handlers.lookup(Handler->getName()) &&
73 "Handler not registered in this namespace");
74 Handlers.erase(Handler->getName());
Daniel Dunbar40950802008-10-04 19:17:46 +000075}
76
Douglas Gregor80c60f72010-09-09 22:45:38 +000077void PragmaNamespace::HandlePragma(Preprocessor &PP,
78 PragmaIntroducerKind Introducer,
79 Token &Tok) {
Reid Spencer5f016e22007-07-11 17:01:13 +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 Stump1eb44332009-09-09 15:08:12 +000083
Reid Spencer5f016e22007-07-11 17:01:13 +000084 // Get the handler for this token. If there is no handler, ignore the pragma.
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +000085 PragmaHandler *Handler
86 = FindHandler(Tok.getIdentifierInfo() ? Tok.getIdentifierInfo()->getName()
Chris Lattner5f9e2722011-07-23 10:55:15 +000087 : StringRef(),
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +000088 /*IgnoreNull=*/false);
Chris Lattneraf7cdf42009-04-19 21:10:26 +000089 if (Handler == 0) {
90 PP.Diag(Tok, diag::warn_pragma_ignored);
91 return;
92 }
Mike Stump1eb44332009-09-09 15:08:12 +000093
Reid Spencer5f016e22007-07-11 17:01:13 +000094 // Otherwise, pass it down.
Douglas Gregor80c60f72010-09-09 22:45:38 +000095 Handler->HandlePragma(PP, Introducer, Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +000096}
97
98//===----------------------------------------------------------------------===//
99// Preprocessor Pragma Directive Handling.
100//===----------------------------------------------------------------------===//
101
James Dennettb6e95b72012-06-17 03:26:26 +0000102/// HandlePragmaDirective - The "\#pragma" directive has been parsed. Lex the
Reid Spencer5f016e22007-07-11 17:01:13 +0000103/// rest of the pragma, passing it to the registered pragma handlers.
Douglas Gregor80c60f72010-09-09 22:45:38 +0000104void Preprocessor::HandlePragmaDirective(unsigned Introducer) {
Jordan Rose6fe6a492012-06-08 18:06:21 +0000105 if (!PragmasEnabled)
106 return;
107
Reid Spencer5f016e22007-07-11 17:01:13 +0000108 ++NumPragma;
Mike Stump1eb44332009-09-09 15:08:12 +0000109
Reid Spencer5f016e22007-07-11 17:01:13 +0000110 // Invoke the first level of pragma handlers which reads the namespace id.
Chris Lattnerd2177732007-07-20 16:59:19 +0000111 Token Tok;
Douglas Gregor80c60f72010-09-09 22:45:38 +0000112 PragmaHandlers->HandlePragma(*this, PragmaIntroducerKind(Introducer), Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000113
Reid Spencer5f016e22007-07-11 17:01:13 +0000114 // If the pragma handler didn't read the rest of the line, consume it now.
Peter Collingbourneb2eb53d2011-02-22 13:49:00 +0000115 if ((CurTokenLexer && CurTokenLexer->isParsingPreprocessorDirective())
116 || (CurPPLexer && CurPPLexer->ParsingPreprocessorDirective))
Reid Spencer5f016e22007-07-11 17:01:13 +0000117 DiscardUntilEndOfDirective();
118}
119
Argyrios Kyrtzidis14e64552012-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
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattnerd2177732007-07-20 16:59:19 +0000160void Preprocessor::Handle_Pragma(Token &Tok) {
Argyrios Kyrtzidis14e64552012-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
Reid Spencer5f016e22007-07-11 17:01:13 +0000175 // Remember the pragma token location.
176 SourceLocation PragmaLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000177
Reid Spencer5f016e22007-07-11 17:01:13 +0000178 // Read the '('.
179 Lex(Tok);
Chris Lattner3692b092008-11-18 07:59:24 +0000180 if (Tok.isNot(tok::l_paren)) {
181 Diag(PragmaLoc, diag::err__Pragma_malformed);
Argyrios Kyrtzidis14e64552012-04-03 16:47:40 +0000182 return _PragmaLexing.failed();
Chris Lattner3692b092008-11-18 07:59:24 +0000183 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000184
185 // Read the '"..."'.
186 Lex(Tok);
Richard Smith0b91cc42013-03-09 23:30:15 +0000187 if (!tok::isStringLiteral(Tok.getKind())) {
Chris Lattner3692b092008-11-18 07:59:24 +0000188 Diag(PragmaLoc, diag::err__Pragma_malformed);
Richard Smith99831e42012-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 Kyrtzidis14e64552012-04-03 16:47:40 +0000194 return _PragmaLexing.failed();
Richard Smith99831e42012-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 Kyrtzidis14e64552012-04-03 16:47:40 +0000203 return _PragmaLexing.failed();
Chris Lattner3692b092008-11-18 07:59:24 +0000204 }
Mike Stump1eb44332009-09-09 15:08:12 +0000205
Reid Spencer5f016e22007-07-11 17:01:13 +0000206 // Remember the string.
Argyrios Kyrtzidis14e64552012-04-03 16:47:40 +0000207 Token StrTok = Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000208
209 // Read the ')'.
210 Lex(Tok);
Chris Lattner3692b092008-11-18 07:59:24 +0000211 if (Tok.isNot(tok::r_paren)) {
212 Diag(PragmaLoc, diag::err__Pragma_malformed);
Argyrios Kyrtzidis14e64552012-04-03 16:47:40 +0000213 return _PragmaLexing.failed();
Chris Lattner3692b092008-11-18 07:59:24 +0000214 }
Mike Stump1eb44332009-09-09 15:08:12 +0000215
Argyrios Kyrtzidis14e64552012-04-03 16:47:40 +0000216 if (InMacroArgPreExpansion)
217 return;
218
Chris Lattnere7fb4842009-02-15 20:52:18 +0000219 SourceLocation RParenLoc = Tok.getLocation();
Argyrios Kyrtzidis14e64552012-04-03 16:47:40 +0000220 std::string StrVal = getSpelling(StrTok);
Mike Stump1eb44332009-09-09 15:08:12 +0000221
Richard Smith0b91cc42013-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 Lattnera9d91452009-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 Smith0b91cc42013-03-09 23:30:15 +0000227 if (StrVal[0] == 'L' || StrVal[0] == 'U' ||
228 (StrVal[0] == 'u' && StrVal[1] != '8'))
Reid Spencer5f016e22007-07-11 17:01:13 +0000229 StrVal.erase(StrVal.begin());
Richard Smith0b91cc42013-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.
Benjamin Kramer269cc2d2013-05-04 10:37:20 +0000257 unsigned ResultPos = 1;
258 for (unsigned i = 1, e = StrVal.size() - 2; i != e; ++i) {
259 if (StrVal[i] != '\\' ||
260 (StrVal[i + 1] != '\\' && StrVal[i + 1] != '"')) {
Richard Smith0b91cc42013-03-09 23:30:15 +0000261 // \\ -> '\' and \" -> '"'.
Benjamin Kramer269cc2d2013-05-04 10:37:20 +0000262 StrVal[ResultPos++] = StrVal[i];
Richard Smith0b91cc42013-03-09 23:30:15 +0000263 }
264 }
Benjamin Kramer269cc2d2013-05-04 10:37:20 +0000265 StrVal.erase(StrVal.begin() + ResultPos, StrVal.end() - 2);
Richard Smith0b91cc42013-03-09 23:30:15 +0000266 }
Mike Stump1eb44332009-09-09 15:08:12 +0000267
Reid Spencer5f016e22007-07-11 17:01:13 +0000268 // Remove the front quote, replacing it with a space, so that the pragma
269 // contents appear to have a space before them.
270 StrVal[0] = ' ';
Mike Stump1eb44332009-09-09 15:08:12 +0000271
Chris Lattner1fa49532009-03-08 08:08:45 +0000272 // Replace the terminating quote with a \n.
Reid Spencer5f016e22007-07-11 17:01:13 +0000273 StrVal[StrVal.size()-1] = '\n';
Mike Stump1eb44332009-09-09 15:08:12 +0000274
Peter Collingbournea5ef5842011-02-22 13:49:06 +0000275 // Plop the string (including the newline and trailing null) into a buffer
276 // where we can lex it.
277 Token TmpTok;
278 TmpTok.startToken();
Dmitri Gribenko374b3832012-09-24 21:07:17 +0000279 CreateString(StrVal, TmpTok);
Peter Collingbournea5ef5842011-02-22 13:49:06 +0000280 SourceLocation TokLoc = TmpTok.getLocation();
281
282 // Make and enter a lexer object so that we lex and expand the tokens just
283 // like any others.
284 Lexer *TL = Lexer::Create_PragmaLexer(TokLoc, PragmaLoc, RParenLoc,
285 StrVal.size(), *this);
286
287 EnterSourceFileWithLexer(TL, 0);
288
289 // With everything set up, lex this as a #pragma directive.
290 HandlePragmaDirective(PIK__Pragma);
John McCall1ef8a2e2010-08-28 22:34:47 +0000291
292 // Finally, return whatever came after the pragma directive.
293 return Lex(Tok);
294}
295
296/// HandleMicrosoft__pragma - Like Handle_Pragma except the pragma text
297/// is not enclosed within a string literal.
298void Preprocessor::HandleMicrosoft__pragma(Token &Tok) {
299 // Remember the pragma token location.
300 SourceLocation PragmaLoc = Tok.getLocation();
301
302 // Read the '('.
303 Lex(Tok);
304 if (Tok.isNot(tok::l_paren)) {
305 Diag(PragmaLoc, diag::err__Pragma_malformed);
306 return;
307 }
308
Peter Collingbournea5ef5842011-02-22 13:49:06 +0000309 // Get the tokens enclosed within the __pragma(), as well as the final ')'.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000310 SmallVector<Token, 32> PragmaToks;
John McCall1ef8a2e2010-08-28 22:34:47 +0000311 int NumParens = 0;
312 Lex(Tok);
313 while (Tok.isNot(tok::eof)) {
Peter Collingbournea5ef5842011-02-22 13:49:06 +0000314 PragmaToks.push_back(Tok);
John McCall1ef8a2e2010-08-28 22:34:47 +0000315 if (Tok.is(tok::l_paren))
316 NumParens++;
317 else if (Tok.is(tok::r_paren) && NumParens-- == 0)
318 break;
John McCall1ef8a2e2010-08-28 22:34:47 +0000319 Lex(Tok);
320 }
321
John McCall3da92a92010-08-29 01:09:54 +0000322 if (Tok.is(tok::eof)) {
323 Diag(PragmaLoc, diag::err_unterminated___pragma);
324 return;
325 }
326
Peter Collingbournea5ef5842011-02-22 13:49:06 +0000327 PragmaToks.front().setFlag(Token::LeadingSpace);
John McCall1ef8a2e2010-08-28 22:34:47 +0000328
Peter Collingbourne84021552011-02-28 02:37:51 +0000329 // Replace the ')' with an EOD to mark the end of the pragma.
330 PragmaToks.back().setKind(tok::eod);
Peter Collingbournea5ef5842011-02-22 13:49:06 +0000331
332 Token *TokArray = new Token[PragmaToks.size()];
333 std::copy(PragmaToks.begin(), PragmaToks.end(), TokArray);
334
335 // Push the tokens onto the stack.
336 EnterTokenStream(TokArray, PragmaToks.size(), true, true);
337
338 // With everything set up, lex this as a #pragma directive.
339 HandlePragmaDirective(PIK___pragma);
John McCall1ef8a2e2010-08-28 22:34:47 +0000340
341 // Finally, return whatever came after the pragma directive.
342 return Lex(Tok);
343}
344
James Dennettb6e95b72012-06-17 03:26:26 +0000345/// HandlePragmaOnce - Handle \#pragma once. OnceTok is the 'once'.
Reid Spencer5f016e22007-07-11 17:01:13 +0000346///
Chris Lattnerd2177732007-07-20 16:59:19 +0000347void Preprocessor::HandlePragmaOnce(Token &OnceTok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000348 if (isInPrimaryFile()) {
349 Diag(OnceTok, diag::pp_pragma_once_in_main_file);
350 return;
351 }
Mike Stump1eb44332009-09-09 15:08:12 +0000352
Reid Spencer5f016e22007-07-11 17:01:13 +0000353 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
Reid Spencer5f016e22007-07-11 17:01:13 +0000354 // Mark the file as a once-only file now.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000355 HeaderInfo.MarkFileIncludeOnce(getCurrentFileLexer()->getFileEntry());
Reid Spencer5f016e22007-07-11 17:01:13 +0000356}
357
Chris Lattner22434492007-12-19 19:38:36 +0000358void Preprocessor::HandlePragmaMark() {
Ted Kremenek17ff58a2008-11-19 22:21:33 +0000359 assert(CurPPLexer && "No current lexer?");
Chris Lattner6896a372009-06-15 05:02:34 +0000360 if (CurLexer)
361 CurLexer->ReadToEndOfLine();
362 else
363 CurPTHLexer->DiscardToEndOfLine();
Chris Lattner22434492007-12-19 19:38:36 +0000364}
365
366
James Dennettb6e95b72012-06-17 03:26:26 +0000367/// HandlePragmaPoison - Handle \#pragma GCC poison. PoisonTok is the 'poison'.
Reid Spencer5f016e22007-07-11 17:01:13 +0000368///
Chris Lattnerd2177732007-07-20 16:59:19 +0000369void Preprocessor::HandlePragmaPoison(Token &PoisonTok) {
370 Token Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000371
372 while (1) {
373 // Read the next token to poison. While doing this, pretend that we are
374 // skipping while reading the identifier to poison.
375 // This avoids errors on code like:
376 // #pragma GCC poison X
377 // #pragma GCC poison X
Ted Kremenek68a91d52008-11-18 01:12:54 +0000378 if (CurPPLexer) CurPPLexer->LexingRawMode = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000379 LexUnexpandedToken(Tok);
Ted Kremenek68a91d52008-11-18 01:12:54 +0000380 if (CurPPLexer) CurPPLexer->LexingRawMode = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000381
Reid Spencer5f016e22007-07-11 17:01:13 +0000382 // If we reached the end of line, we're done.
Peter Collingbourne84021552011-02-28 02:37:51 +0000383 if (Tok.is(tok::eod)) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000384
Reid Spencer5f016e22007-07-11 17:01:13 +0000385 // Can only poison identifiers.
Abramo Bagnarac4bf2b92010-12-22 08:23:18 +0000386 if (Tok.isNot(tok::raw_identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000387 Diag(Tok, diag::err_pp_invalid_poison);
388 return;
389 }
Mike Stump1eb44332009-09-09 15:08:12 +0000390
Reid Spencer5f016e22007-07-11 17:01:13 +0000391 // Look up the identifier info for the token. We disabled identifier lookup
392 // by saying we're skipping contents, so we need to do this manually.
393 IdentifierInfo *II = LookUpIdentifierInfo(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000394
Reid Spencer5f016e22007-07-11 17:01:13 +0000395 // Already poisoned.
396 if (II->isPoisoned()) continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000397
Reid Spencer5f016e22007-07-11 17:01:13 +0000398 // If this is a macro identifier, emit a warning.
Chris Lattner0edde552007-10-07 08:04:56 +0000399 if (II->hasMacroDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000400 Diag(Tok, diag::pp_poisoning_existing_macro);
Mike Stump1eb44332009-09-09 15:08:12 +0000401
Reid Spencer5f016e22007-07-11 17:01:13 +0000402 // Finally, poison it!
403 II->setIsPoisoned();
Douglas Gregoreee242f2011-10-27 09:33:13 +0000404 if (II->isFromAST())
405 II->setChangedSinceDeserialization();
Reid Spencer5f016e22007-07-11 17:01:13 +0000406 }
407}
408
James Dennettb6e95b72012-06-17 03:26:26 +0000409/// HandlePragmaSystemHeader - Implement \#pragma GCC system_header. We know
Reid Spencer5f016e22007-07-11 17:01:13 +0000410/// that the whole directive has been parsed.
Chris Lattnerd2177732007-07-20 16:59:19 +0000411void Preprocessor::HandlePragmaSystemHeader(Token &SysHeaderTok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000412 if (isInPrimaryFile()) {
413 Diag(SysHeaderTok, diag::pp_pragma_sysheader_in_main_file);
414 return;
415 }
Mike Stump1eb44332009-09-09 15:08:12 +0000416
Reid Spencer5f016e22007-07-11 17:01:13 +0000417 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
Ted Kremenek35c10c22008-11-20 01:45:11 +0000418 PreprocessorLexer *TheLexer = getCurrentFileLexer();
Mike Stump1eb44332009-09-09 15:08:12 +0000419
Reid Spencer5f016e22007-07-11 17:01:13 +0000420 // Mark the file as a system header.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000421 HeaderInfo.MarkFileSystemHeader(TheLexer->getFileEntry());
Mike Stump1eb44332009-09-09 15:08:12 +0000422
423
Chris Lattner6896a372009-06-15 05:02:34 +0000424 PresumedLoc PLoc = SourceMgr.getPresumedLoc(SysHeaderTok.getLocation());
Douglas Gregorcb7b1e12010-11-12 07:15:47 +0000425 if (PLoc.isInvalid())
426 return;
427
Jay Foad65aa6882011-06-21 15:13:30 +0000428 unsigned FilenameID = SourceMgr.getLineTableFilenameID(PLoc.getFilename());
Mike Stump1eb44332009-09-09 15:08:12 +0000429
Chris Lattner784c2572011-05-22 22:10:16 +0000430 // Notify the client, if desired, that we are in a new source file.
431 if (Callbacks)
432 Callbacks->FileChanged(SysHeaderTok.getLocation(),
433 PPCallbacks::SystemHeaderPragma, SrcMgr::C_System);
434
Chris Lattner6896a372009-06-15 05:02:34 +0000435 // Emit a line marker. This will change any source locations from this point
436 // forward to realize they are in a system header.
437 // Create a line note with this information.
Jordan Rose142b35e2013-04-17 19:09:18 +0000438 SourceMgr.AddLineNote(SysHeaderTok.getLocation(), PLoc.getLine()+1,
439 FilenameID, /*IsEntry=*/false, /*IsExit=*/false,
440 /*IsSystem=*/true, /*IsExternC=*/false);
Reid Spencer5f016e22007-07-11 17:01:13 +0000441}
442
James Dennettb6e95b72012-06-17 03:26:26 +0000443/// HandlePragmaDependency - Handle \#pragma GCC dependency "foo" blah.
Reid Spencer5f016e22007-07-11 17:01:13 +0000444///
Chris Lattnerd2177732007-07-20 16:59:19 +0000445void Preprocessor::HandlePragmaDependency(Token &DependencyTok) {
446 Token FilenameTok;
Ted Kremenek68a91d52008-11-18 01:12:54 +0000447 CurPPLexer->LexIncludeFilename(FilenameTok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000448
Peter Collingbourne84021552011-02-28 02:37:51 +0000449 // If the token kind is EOD, the error has already been diagnosed.
450 if (FilenameTok.is(tok::eod))
Reid Spencer5f016e22007-07-11 17:01:13 +0000451 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000452
Reid Spencer5f016e22007-07-11 17:01:13 +0000453 // Reserve a buffer to get the spelling.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000454 SmallString<128> FilenameBuffer;
Douglas Gregor453091c2010-03-16 22:30:13 +0000455 bool Invalid = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000456 StringRef Filename = getSpelling(FilenameTok, FilenameBuffer, &Invalid);
Douglas Gregor453091c2010-03-16 22:30:13 +0000457 if (Invalid)
458 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000459
Chris Lattnera1394812010-01-10 01:35:12 +0000460 bool isAngled =
461 GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
Reid Spencer5f016e22007-07-11 17:01:13 +0000462 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
463 // error.
Chris Lattnera1394812010-01-10 01:35:12 +0000464 if (Filename.empty())
Reid Spencer5f016e22007-07-11 17:01:13 +0000465 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000466
Reid Spencer5f016e22007-07-11 17:01:13 +0000467 // Search include directories for this file.
468 const DirectoryLookup *CurDir;
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000469 const FileEntry *File = LookupFile(Filename, isAngled, 0, CurDir, NULL, NULL,
470 NULL);
Chris Lattner56b05c82008-11-18 08:02:48 +0000471 if (File == 0) {
Eli Friedmanf84139a2011-08-30 23:07:51 +0000472 if (!SuppressIncludeNotFoundError)
473 Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
Chris Lattner56b05c82008-11-18 08:02:48 +0000474 return;
475 }
Mike Stump1eb44332009-09-09 15:08:12 +0000476
Chris Lattner2b2453a2009-01-17 06:22:33 +0000477 const FileEntry *CurFile = getCurrentFileLexer()->getFileEntry();
Reid Spencer5f016e22007-07-11 17:01:13 +0000478
479 // If this file is older than the file it depends on, emit a diagnostic.
480 if (CurFile && CurFile->getModificationTime() < File->getModificationTime()) {
481 // Lex tokens at the end of the message and include them in the message.
482 std::string Message;
483 Lex(DependencyTok);
Peter Collingbourne84021552011-02-28 02:37:51 +0000484 while (DependencyTok.isNot(tok::eod)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000485 Message += getSpelling(DependencyTok) + " ";
486 Lex(DependencyTok);
487 }
Mike Stump1eb44332009-09-09 15:08:12 +0000488
Chris Lattner96de2592010-09-05 23:16:09 +0000489 // Remove the trailing ' ' if present.
490 if (!Message.empty())
491 Message.erase(Message.end()-1);
Chris Lattner56b05c82008-11-18 08:02:48 +0000492 Diag(FilenameTok, diag::pp_out_of_date_dependency) << Message;
Reid Spencer5f016e22007-07-11 17:01:13 +0000493 }
494}
495
Reid Kleckner7adf79a2013-05-06 21:02:12 +0000496/// ParsePragmaPushOrPopMacro - Handle parsing of pragma push_macro/pop_macro.
Chris Lattnerf47724b2010-08-17 15:55:45 +0000497/// Return the IdentifierInfo* associated with the macro to push or pop.
498IdentifierInfo *Preprocessor::ParsePragmaPushOrPopMacro(Token &Tok) {
499 // Remember the pragma token location.
500 Token PragmaTok = Tok;
501
502 // Read the '('.
503 Lex(Tok);
504 if (Tok.isNot(tok::l_paren)) {
505 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
506 << getSpelling(PragmaTok);
507 return 0;
508 }
509
510 // Read the macro name string.
511 Lex(Tok);
512 if (Tok.isNot(tok::string_literal)) {
513 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
514 << getSpelling(PragmaTok);
515 return 0;
516 }
517
Richard Smith99831e42012-03-06 03:21:47 +0000518 if (Tok.hasUDSuffix()) {
519 Diag(Tok, diag::err_invalid_string_udl);
520 return 0;
521 }
522
Chris Lattnerf47724b2010-08-17 15:55:45 +0000523 // Remember the macro string.
524 std::string StrVal = getSpelling(Tok);
525
526 // Read the ')'.
527 Lex(Tok);
528 if (Tok.isNot(tok::r_paren)) {
529 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
530 << getSpelling(PragmaTok);
531 return 0;
532 }
533
534 assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
535 "Invalid string token!");
536
537 // Create a Token from the string.
538 Token MacroTok;
539 MacroTok.startToken();
Abramo Bagnarac4bf2b92010-12-22 08:23:18 +0000540 MacroTok.setKind(tok::raw_identifier);
Dmitri Gribenko374b3832012-09-24 21:07:17 +0000541 CreateString(StringRef(&StrVal[1], StrVal.size() - 2), MacroTok);
Chris Lattnerf47724b2010-08-17 15:55:45 +0000542
543 // Get the IdentifierInfo of MacroToPushTok.
544 return LookUpIdentifierInfo(MacroTok);
545}
546
James Dennettb6e95b72012-06-17 03:26:26 +0000547/// \brief Handle \#pragma push_macro.
548///
Chris Lattnerf47724b2010-08-17 15:55:45 +0000549/// The syntax is:
James Dennettb6e95b72012-06-17 03:26:26 +0000550/// \code
Dmitri Gribenkoe74dc192012-11-30 20:04:39 +0000551/// #pragma push_macro("macro")
James Dennettb6e95b72012-06-17 03:26:26 +0000552/// \endcode
Chris Lattnerf47724b2010-08-17 15:55:45 +0000553void Preprocessor::HandlePragmaPushMacro(Token &PushMacroTok) {
554 // Parse the pragma directive and get the macro IdentifierInfo*.
555 IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PushMacroTok);
556 if (!IdentInfo) return;
557
558 // Get the MacroInfo associated with IdentInfo.
559 MacroInfo *MI = getMacroInfo(IdentInfo);
560
Chris Lattnerf47724b2010-08-17 15:55:45 +0000561 if (MI) {
Chris Lattnerf47724b2010-08-17 15:55:45 +0000562 // Allow the original MacroInfo to be redefined later.
563 MI->setIsAllowRedefinitionsWithoutWarning(true);
564 }
565
566 // Push the cloned MacroInfo so we can retrieve it later.
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +0000567 PragmaPushMacroInfo[IdentInfo].push_back(MI);
Chris Lattnerf47724b2010-08-17 15:55:45 +0000568}
569
James Dennettb6e95b72012-06-17 03:26:26 +0000570/// \brief Handle \#pragma pop_macro.
571///
Chris Lattnerf47724b2010-08-17 15:55:45 +0000572/// The syntax is:
James Dennettb6e95b72012-06-17 03:26:26 +0000573/// \code
Chris Lattnerf47724b2010-08-17 15:55:45 +0000574/// #pragma pop_macro("macro")
James Dennettb6e95b72012-06-17 03:26:26 +0000575/// \endcode
Chris Lattnerf47724b2010-08-17 15:55:45 +0000576void Preprocessor::HandlePragmaPopMacro(Token &PopMacroTok) {
577 SourceLocation MessageLoc = PopMacroTok.getLocation();
578
579 // Parse the pragma directive and get the macro IdentifierInfo*.
580 IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PopMacroTok);
581 if (!IdentInfo) return;
582
583 // Find the vector<MacroInfo*> associated with the macro.
584 llvm::DenseMap<IdentifierInfo*, std::vector<MacroInfo*> >::iterator iter =
585 PragmaPushMacroInfo.find(IdentInfo);
586 if (iter != PragmaPushMacroInfo.end()) {
Alexander Kornienko8a64bb52012-08-29 00:20:03 +0000587 // Forget the MacroInfo currently associated with IdentInfo.
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +0000588 if (MacroDirective *CurrentMD = getMacroDirective(IdentInfo)) {
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +0000589 MacroInfo *MI = CurrentMD->getMacroInfo();
590 if (MI->isWarnIfUnused())
591 WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());
592 appendMacroDirective(IdentInfo, AllocateUndefMacroDirective(MessageLoc));
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000593 }
Chris Lattnerf47724b2010-08-17 15:55:45 +0000594
595 // Get the MacroInfo we want to reinstall.
596 MacroInfo *MacroToReInstall = iter->second.back();
597
Alexander Kornienkoe40c4232012-08-29 16:56:24 +0000598 if (MacroToReInstall) {
599 // Reinstall the previously pushed macro.
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +0000600 appendDefMacroDirective(IdentInfo, MacroToReInstall, MessageLoc,
601 /*isImported=*/false);
Alexander Kornienkoe40c4232012-08-29 16:56:24 +0000602 }
Chris Lattnerf47724b2010-08-17 15:55:45 +0000603
604 // Pop PragmaPushMacroInfo stack.
605 iter->second.pop_back();
606 if (iter->second.size() == 0)
607 PragmaPushMacroInfo.erase(iter);
608 } else {
609 Diag(MessageLoc, diag::warn_pragma_pop_macro_no_push)
610 << IdentInfo->getName();
611 }
612}
Reid Spencer5f016e22007-07-11 17:01:13 +0000613
Aaron Ballman4c55c542012-03-02 22:51:54 +0000614void Preprocessor::HandlePragmaIncludeAlias(Token &Tok) {
615 // We will either get a quoted filename or a bracketed filename, and we
616 // have to track which we got. The first filename is the source name,
617 // and the second name is the mapped filename. If the first is quoted,
618 // the second must be as well (cannot mix and match quotes and brackets).
Aaron Ballman4c55c542012-03-02 22:51:54 +0000619
620 // Get the open paren
621 Lex(Tok);
622 if (Tok.isNot(tok::l_paren)) {
623 Diag(Tok, diag::warn_pragma_include_alias_expected) << "(";
624 return;
625 }
626
627 // We expect either a quoted string literal, or a bracketed name
628 Token SourceFilenameTok;
629 CurPPLexer->LexIncludeFilename(SourceFilenameTok);
630 if (SourceFilenameTok.is(tok::eod)) {
631 // The diagnostic has already been handled
632 return;
633 }
634
635 StringRef SourceFileName;
636 SmallString<128> FileNameBuffer;
637 if (SourceFilenameTok.is(tok::string_literal) ||
638 SourceFilenameTok.is(tok::angle_string_literal)) {
639 SourceFileName = getSpelling(SourceFilenameTok, FileNameBuffer);
640 } else if (SourceFilenameTok.is(tok::less)) {
641 // This could be a path instead of just a name
642 FileNameBuffer.push_back('<');
643 SourceLocation End;
644 if (ConcatenateIncludeName(FileNameBuffer, End))
645 return; // Diagnostic already emitted
646 SourceFileName = FileNameBuffer.str();
647 } else {
648 Diag(Tok, diag::warn_pragma_include_alias_expected_filename);
649 return;
650 }
651 FileNameBuffer.clear();
652
653 // Now we expect a comma, followed by another include name
654 Lex(Tok);
655 if (Tok.isNot(tok::comma)) {
656 Diag(Tok, diag::warn_pragma_include_alias_expected) << ",";
657 return;
658 }
659
660 Token ReplaceFilenameTok;
661 CurPPLexer->LexIncludeFilename(ReplaceFilenameTok);
662 if (ReplaceFilenameTok.is(tok::eod)) {
663 // The diagnostic has already been handled
664 return;
665 }
666
667 StringRef ReplaceFileName;
668 if (ReplaceFilenameTok.is(tok::string_literal) ||
669 ReplaceFilenameTok.is(tok::angle_string_literal)) {
670 ReplaceFileName = getSpelling(ReplaceFilenameTok, FileNameBuffer);
671 } else if (ReplaceFilenameTok.is(tok::less)) {
672 // This could be a path instead of just a name
673 FileNameBuffer.push_back('<');
674 SourceLocation End;
675 if (ConcatenateIncludeName(FileNameBuffer, End))
676 return; // Diagnostic already emitted
677 ReplaceFileName = FileNameBuffer.str();
678 } else {
679 Diag(Tok, diag::warn_pragma_include_alias_expected_filename);
680 return;
681 }
682
683 // Finally, we expect the closing paren
684 Lex(Tok);
685 if (Tok.isNot(tok::r_paren)) {
686 Diag(Tok, diag::warn_pragma_include_alias_expected) << ")";
687 return;
688 }
689
690 // Now that we have the source and target filenames, we need to make sure
691 // they're both of the same type (angled vs non-angled)
692 StringRef OriginalSource = SourceFileName;
693
694 bool SourceIsAngled =
695 GetIncludeFilenameSpelling(SourceFilenameTok.getLocation(),
696 SourceFileName);
697 bool ReplaceIsAngled =
698 GetIncludeFilenameSpelling(ReplaceFilenameTok.getLocation(),
699 ReplaceFileName);
700 if (!SourceFileName.empty() && !ReplaceFileName.empty() &&
701 (SourceIsAngled != ReplaceIsAngled)) {
702 unsigned int DiagID;
703 if (SourceIsAngled)
704 DiagID = diag::warn_pragma_include_alias_mismatch_angle;
705 else
706 DiagID = diag::warn_pragma_include_alias_mismatch_quote;
707
708 Diag(SourceFilenameTok.getLocation(), DiagID)
709 << SourceFileName
710 << ReplaceFileName;
711
712 return;
713 }
714
715 // Now we can let the include handler know about this mapping
716 getHeaderSearchInfo().AddIncludeAlias(OriginalSource, ReplaceFileName);
717}
718
Reid Spencer5f016e22007-07-11 17:01:13 +0000719/// AddPragmaHandler - Add the specified pragma handler to the preprocessor.
720/// If 'Namespace' is non-null, then it is a token required to exist on the
721/// pragma line before the pragma string starts, e.g. "STDC" or "GCC".
Chris Lattner5f9e2722011-07-23 10:55:15 +0000722void Preprocessor::AddPragmaHandler(StringRef Namespace,
Reid Spencer5f016e22007-07-11 17:01:13 +0000723 PragmaHandler *Handler) {
724 PragmaNamespace *InsertNS = PragmaHandlers;
Mike Stump1eb44332009-09-09 15:08:12 +0000725
Reid Spencer5f016e22007-07-11 17:01:13 +0000726 // If this is specified to be in a namespace, step down into it.
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000727 if (!Namespace.empty()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000728 // If there is already a pragma handler with the name of this namespace,
729 // we either have an error (directive with the same name as a namespace) or
730 // we already have the namespace to insert into.
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000731 if (PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000732 InsertNS = Existing->getIfNamespace();
733 assert(InsertNS != 0 && "Cannot have a pragma namespace and pragma"
734 " handler with the same name!");
735 } else {
736 // Otherwise, this namespace doesn't exist yet, create and insert the
737 // handler for it.
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000738 InsertNS = new PragmaNamespace(Namespace);
Reid Spencer5f016e22007-07-11 17:01:13 +0000739 PragmaHandlers->AddPragma(InsertNS);
740 }
741 }
Mike Stump1eb44332009-09-09 15:08:12 +0000742
Reid Spencer5f016e22007-07-11 17:01:13 +0000743 // Check to make sure we don't already have a pragma for this identifier.
744 assert(!InsertNS->FindHandler(Handler->getName()) &&
745 "Pragma handler already exists for this identifier!");
746 InsertNS->AddPragma(Handler);
747}
748
Daniel Dunbar40950802008-10-04 19:17:46 +0000749/// RemovePragmaHandler - Remove the specific pragma handler from the
750/// preprocessor. If \arg Namespace is non-null, then it should be the
751/// namespace that \arg Handler was added to. It is an error to remove
752/// a handler that has not been registered.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000753void Preprocessor::RemovePragmaHandler(StringRef Namespace,
Daniel Dunbar40950802008-10-04 19:17:46 +0000754 PragmaHandler *Handler) {
755 PragmaNamespace *NS = PragmaHandlers;
Mike Stump1eb44332009-09-09 15:08:12 +0000756
Daniel Dunbar40950802008-10-04 19:17:46 +0000757 // If this is specified to be in a namespace, step down into it.
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000758 if (!Namespace.empty()) {
759 PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace);
Daniel Dunbar40950802008-10-04 19:17:46 +0000760 assert(Existing && "Namespace containing handler does not exist!");
761
762 NS = Existing->getIfNamespace();
763 assert(NS && "Invalid namespace, registered as a regular pragma handler!");
764 }
765
766 NS->RemovePragmaHandler(Handler);
Mike Stump1eb44332009-09-09 15:08:12 +0000767
Daniel Dunbar40950802008-10-04 19:17:46 +0000768 // If this is a non-default namespace and it is now empty, remove
769 // it.
Argyrios Kyrtzidisce52bb32012-01-06 00:22:09 +0000770 if (NS != PragmaHandlers && NS->IsEmpty()) {
Daniel Dunbar40950802008-10-04 19:17:46 +0000771 PragmaHandlers->RemovePragmaHandler(NS);
Argyrios Kyrtzidisce52bb32012-01-06 00:22:09 +0000772 delete NS;
773 }
Daniel Dunbar40950802008-10-04 19:17:46 +0000774}
775
Peter Collingbourne9d3f5f72011-02-14 01:42:24 +0000776bool Preprocessor::LexOnOffSwitch(tok::OnOffSwitch &Result) {
777 Token Tok;
778 LexUnexpandedToken(Tok);
779
780 if (Tok.isNot(tok::identifier)) {
781 Diag(Tok, diag::ext_on_off_switch_syntax);
782 return true;
783 }
784 IdentifierInfo *II = Tok.getIdentifierInfo();
785 if (II->isStr("ON"))
786 Result = tok::OOS_ON;
787 else if (II->isStr("OFF"))
788 Result = tok::OOS_OFF;
789 else if (II->isStr("DEFAULT"))
790 Result = tok::OOS_DEFAULT;
791 else {
792 Diag(Tok, diag::ext_on_off_switch_syntax);
793 return true;
794 }
795
Peter Collingbourne84021552011-02-28 02:37:51 +0000796 // Verify that this is followed by EOD.
Peter Collingbourne9d3f5f72011-02-14 01:42:24 +0000797 LexUnexpandedToken(Tok);
Peter Collingbourne84021552011-02-28 02:37:51 +0000798 if (Tok.isNot(tok::eod))
799 Diag(Tok, diag::ext_pragma_syntax_eod);
Peter Collingbourne9d3f5f72011-02-14 01:42:24 +0000800 return false;
801}
802
Reid Spencer5f016e22007-07-11 17:01:13 +0000803namespace {
James Dennettb6e95b72012-06-17 03:26:26 +0000804/// PragmaOnceHandler - "\#pragma once" marks the file as atomically included.
Reid Spencer5f016e22007-07-11 17:01:13 +0000805struct PragmaOnceHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000806 PragmaOnceHandler() : PragmaHandler("once") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000807 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
808 Token &OnceTok) {
Chris Lattner35410d52009-04-14 05:07:49 +0000809 PP.CheckEndOfDirective("pragma once");
Reid Spencer5f016e22007-07-11 17:01:13 +0000810 PP.HandlePragmaOnce(OnceTok);
811 }
812};
813
James Dennettb6e95b72012-06-17 03:26:26 +0000814/// PragmaMarkHandler - "\#pragma mark ..." is ignored by the compiler, and the
Chris Lattner22434492007-12-19 19:38:36 +0000815/// rest of the line is not lexed.
816struct PragmaMarkHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000817 PragmaMarkHandler() : PragmaHandler("mark") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000818 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
819 Token &MarkTok) {
Chris Lattner22434492007-12-19 19:38:36 +0000820 PP.HandlePragmaMark();
821 }
822};
823
James Dennettb6e95b72012-06-17 03:26:26 +0000824/// PragmaPoisonHandler - "\#pragma poison x" marks x as not usable.
Reid Spencer5f016e22007-07-11 17:01:13 +0000825struct PragmaPoisonHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000826 PragmaPoisonHandler() : PragmaHandler("poison") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000827 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
828 Token &PoisonTok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000829 PP.HandlePragmaPoison(PoisonTok);
830 }
831};
832
James Dennettb6e95b72012-06-17 03:26:26 +0000833/// PragmaSystemHeaderHandler - "\#pragma system_header" marks the current file
Chris Lattner22434492007-12-19 19:38:36 +0000834/// as a system header, which silences warnings in it.
Reid Spencer5f016e22007-07-11 17:01:13 +0000835struct PragmaSystemHeaderHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000836 PragmaSystemHeaderHandler() : PragmaHandler("system_header") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000837 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
838 Token &SHToken) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000839 PP.HandlePragmaSystemHeader(SHToken);
Chris Lattner35410d52009-04-14 05:07:49 +0000840 PP.CheckEndOfDirective("pragma");
Reid Spencer5f016e22007-07-11 17:01:13 +0000841 }
842};
843struct PragmaDependencyHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000844 PragmaDependencyHandler() : PragmaHandler("dependency") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000845 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
846 Token &DepToken) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000847 PP.HandlePragmaDependency(DepToken);
848 }
849};
Mike Stump1eb44332009-09-09 15:08:12 +0000850
Daniel Dunbarabf7b722010-07-28 15:40:33 +0000851struct PragmaDebugHandler : public PragmaHandler {
852 PragmaDebugHandler() : PragmaHandler("__debug") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000853 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
854 Token &DepToken) {
Daniel Dunbarabf7b722010-07-28 15:40:33 +0000855 Token Tok;
856 PP.LexUnexpandedToken(Tok);
857 if (Tok.isNot(tok::identifier)) {
Douglas Gregor6493a4d2010-08-30 15:15:34 +0000858 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Daniel Dunbarabf7b722010-07-28 15:40:33 +0000859 return;
860 }
861 IdentifierInfo *II = Tok.getIdentifierInfo();
862
Daniel Dunbar55054132010-08-17 22:32:48 +0000863 if (II->isStr("assert")) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000864 llvm_unreachable("This is an assertion!");
Daniel Dunbarabf7b722010-07-28 15:40:33 +0000865 } else if (II->isStr("crash")) {
David Blaikie377da4c2012-08-21 18:56:49 +0000866 LLVM_BUILTIN_TRAP;
David Blaikiee75d9cf2012-06-29 22:03:56 +0000867 } else if (II->isStr("parser_crash")) {
868 Token Crasher;
869 Crasher.setKind(tok::annot_pragma_parser_crash);
870 PP.EnterToken(Crasher);
Daniel Dunbar55054132010-08-17 22:32:48 +0000871 } else if (II->isStr("llvm_fatal_error")) {
872 llvm::report_fatal_error("#pragma clang __debug llvm_fatal_error");
873 } else if (II->isStr("llvm_unreachable")) {
874 llvm_unreachable("#pragma clang __debug llvm_unreachable");
875 } else if (II->isStr("overflow_stack")) {
876 DebugOverflowStack();
Daniel Dunbarff759a62010-08-18 23:09:23 +0000877 } else if (II->isStr("handle_crash")) {
878 llvm::CrashRecoveryContext *CRC =llvm::CrashRecoveryContext::GetCurrent();
879 if (CRC)
880 CRC->HandleCrash();
Tareq A. Siraj85192c72013-04-16 18:41:26 +0000881 } else if (II->isStr("captured")) {
882 HandleCaptured(PP);
Daniel Dunbar55054132010-08-17 22:32:48 +0000883 } else {
884 PP.Diag(Tok, diag::warn_pragma_debug_unexpected_command)
885 << II->getName();
Daniel Dunbarabf7b722010-07-28 15:40:33 +0000886 }
Tareq A. Siraj85192c72013-04-16 18:41:26 +0000887
888 PPCallbacks *Callbacks = PP.getPPCallbacks();
889 if (Callbacks)
890 Callbacks->PragmaDebug(Tok.getLocation(), II->getName());
891 }
892
893 void HandleCaptured(Preprocessor &PP) {
894 // Skip if emitting preprocessed output.
895 if (PP.isPreprocessedOutput())
896 return;
897
898 Token Tok;
899 PP.LexUnexpandedToken(Tok);
900
901 if (Tok.isNot(tok::eod)) {
902 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol)
903 << "pragma clang __debug captured";
904 return;
905 }
906
907 SourceLocation NameLoc = Tok.getLocation();
908 Token *Toks = PP.getPreprocessorAllocator().Allocate<Token>(1);
909 Toks->startToken();
910 Toks->setKind(tok::annot_pragma_captured);
911 Toks->setLocation(NameLoc);
912
913 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
914 /*OwnsTokens=*/false);
Daniel Dunbarabf7b722010-07-28 15:40:33 +0000915 }
916
Francois Pichet1066c6c2011-05-25 16:15:03 +0000917// Disable MSVC warning about runtime stack overflow.
918#ifdef _MSC_VER
919 #pragma warning(disable : 4717)
920#endif
Daniel Dunbarabf7b722010-07-28 15:40:33 +0000921 void DebugOverflowStack() {
922 DebugOverflowStack();
923 }
Francois Pichet1066c6c2011-05-25 16:15:03 +0000924#ifdef _MSC_VER
925 #pragma warning(default : 4717)
926#endif
927
Daniel Dunbarabf7b722010-07-28 15:40:33 +0000928};
929
James Dennettb6e95b72012-06-17 03:26:26 +0000930/// PragmaDiagnosticHandler - e.g. '\#pragma GCC diagnostic ignored "-Wformat"'
Chris Lattneredaf8772009-04-19 23:16:58 +0000931struct PragmaDiagnosticHandler : public PragmaHandler {
Douglas Gregorc09ce122011-06-22 19:41:48 +0000932private:
933 const char *Namespace;
Chris Lattner04ae2df2009-07-12 21:18:45 +0000934public:
Douglas Gregorc09ce122011-06-22 19:41:48 +0000935 explicit PragmaDiagnosticHandler(const char *NS) :
936 PragmaHandler("diagnostic"), Namespace(NS) {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000937 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
938 Token &DiagToken) {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000939 SourceLocation DiagLoc = DiagToken.getLocation();
Chris Lattneredaf8772009-04-19 23:16:58 +0000940 Token Tok;
941 PP.LexUnexpandedToken(Tok);
942 if (Tok.isNot(tok::identifier)) {
Douglas Gregor6493a4d2010-08-30 15:15:34 +0000943 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Chris Lattneredaf8772009-04-19 23:16:58 +0000944 return;
945 }
946 IdentifierInfo *II = Tok.getIdentifierInfo();
Douglas Gregorc09ce122011-06-22 19:41:48 +0000947 PPCallbacks *Callbacks = PP.getPPCallbacks();
Mike Stump1eb44332009-09-09 15:08:12 +0000948
Chris Lattneredaf8772009-04-19 23:16:58 +0000949 diag::Mapping Map;
950 if (II->isStr("warning"))
951 Map = diag::MAP_WARNING;
952 else if (II->isStr("error"))
953 Map = diag::MAP_ERROR;
954 else if (II->isStr("ignored"))
955 Map = diag::MAP_IGNORE;
956 else if (II->isStr("fatal"))
957 Map = diag::MAP_FATAL;
Douglas Gregor6493a4d2010-08-30 15:15:34 +0000958 else if (II->isStr("pop")) {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000959 if (!PP.getDiagnostics().popMappings(DiagLoc))
Douglas Gregor6493a4d2010-08-30 15:15:34 +0000960 PP.Diag(Tok, diag::warn_pragma_diagnostic_cannot_pop);
Douglas Gregorc09ce122011-06-22 19:41:48 +0000961 else if (Callbacks)
962 Callbacks->PragmaDiagnosticPop(DiagLoc, Namespace);
Douglas Gregor6493a4d2010-08-30 15:15:34 +0000963 return;
964 } else if (II->isStr("push")) {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000965 PP.getDiagnostics().pushMappings(DiagLoc);
Douglas Gregorc09ce122011-06-22 19:41:48 +0000966 if (Callbacks)
967 Callbacks->PragmaDiagnosticPush(DiagLoc, Namespace);
Chris Lattner04ae2df2009-07-12 21:18:45 +0000968 return;
969 } else {
Douglas Gregor6493a4d2010-08-30 15:15:34 +0000970 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Chris Lattneredaf8772009-04-19 23:16:58 +0000971 return;
972 }
Mike Stump1eb44332009-09-09 15:08:12 +0000973
Chris Lattneredaf8772009-04-19 23:16:58 +0000974 PP.LexUnexpandedToken(Tok);
Andy Gibbs02a17682012-11-17 19:15:38 +0000975 SourceLocation StringLoc = Tok.getLocation();
Chris Lattneredaf8772009-04-19 23:16:58 +0000976
Andy Gibbs02a17682012-11-17 19:15:38 +0000977 std::string WarningName;
Andy Gibbs97f84612012-11-17 19:16:52 +0000978 if (!PP.FinishLexStringLiteral(Tok, WarningName, "pragma diagnostic",
979 /*MacroExpansion=*/false))
Chris Lattneredaf8772009-04-19 23:16:58 +0000980 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000981
Peter Collingbourne84021552011-02-28 02:37:51 +0000982 if (Tok.isNot(tok::eod)) {
Chris Lattneredaf8772009-04-19 23:16:58 +0000983 PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token);
984 return;
985 }
Mike Stump1eb44332009-09-09 15:08:12 +0000986
Chris Lattneredaf8772009-04-19 23:16:58 +0000987 if (WarningName.size() < 3 || WarningName[0] != '-' ||
988 WarningName[1] != 'W') {
Andy Gibbs02a17682012-11-17 19:15:38 +0000989 PP.Diag(StringLoc, diag::warn_pragma_diagnostic_invalid_option);
Chris Lattneredaf8772009-04-19 23:16:58 +0000990 return;
991 }
Mike Stump1eb44332009-09-09 15:08:12 +0000992
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000993 if (PP.getDiagnostics().setDiagnosticGroupMapping(WarningName.substr(2),
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000994 Map, DiagLoc))
Andy Gibbs02a17682012-11-17 19:15:38 +0000995 PP.Diag(StringLoc, diag::warn_pragma_diagnostic_unknown_warning)
996 << WarningName;
Douglas Gregorc09ce122011-06-22 19:41:48 +0000997 else if (Callbacks)
998 Callbacks->PragmaDiagnostic(DiagLoc, Namespace, Map, WarningName);
Chris Lattneredaf8772009-04-19 23:16:58 +0000999 }
1000};
Mike Stump1eb44332009-09-09 15:08:12 +00001001
James Dennettb6e95b72012-06-17 03:26:26 +00001002/// PragmaIncludeAliasHandler - "\#pragma include_alias("...")".
Aaron Ballman4c55c542012-03-02 22:51:54 +00001003struct PragmaIncludeAliasHandler : public PragmaHandler {
1004 PragmaIncludeAliasHandler() : PragmaHandler("include_alias") {}
1005 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1006 Token &IncludeAliasTok) {
1007 PP.HandlePragmaIncludeAlias(IncludeAliasTok);
1008 }
1009};
1010
Andy Gibbs076eea22013-04-17 16:16:16 +00001011/// PragmaMessageHandler - Handle the microsoft and gcc \#pragma message
1012/// extension. The syntax is:
1013/// \code
1014/// #pragma message(string)
1015/// \endcode
1016/// OR, in GCC mode:
1017/// \code
1018/// #pragma message string
1019/// \endcode
1020/// string is a string, which is fully macro expanded, and permits string
1021/// concatenation, embedded escape characters, etc... See MSDN for more details.
1022/// Also handles \#pragma GCC warning and \#pragma GCC error which take the same
1023/// form as \#pragma message.
Chris Lattnerabfe0942010-06-26 17:11:39 +00001024struct PragmaMessageHandler : public PragmaHandler {
Andy Gibbs076eea22013-04-17 16:16:16 +00001025private:
1026 const PPCallbacks::PragmaMessageKind Kind;
1027 const StringRef Namespace;
1028
1029 static const char* PragmaKind(PPCallbacks::PragmaMessageKind Kind,
1030 bool PragmaNameOnly = false) {
1031 switch (Kind) {
1032 case PPCallbacks::PMK_Message:
1033 return PragmaNameOnly ? "message" : "pragma message";
1034 case PPCallbacks::PMK_Warning:
1035 return PragmaNameOnly ? "warning" : "pragma warning";
1036 case PPCallbacks::PMK_Error:
1037 return PragmaNameOnly ? "error" : "pragma error";
1038 }
1039 llvm_unreachable("Unknown PragmaMessageKind!");
1040 }
1041
1042public:
1043 PragmaMessageHandler(PPCallbacks::PragmaMessageKind Kind,
1044 StringRef Namespace = StringRef())
1045 : PragmaHandler(PragmaKind(Kind, true)), Kind(Kind), Namespace(Namespace) {}
1046
Douglas Gregor80c60f72010-09-09 22:45:38 +00001047 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
Andy Gibbs076eea22013-04-17 16:16:16 +00001048 Token &Tok) {
1049 SourceLocation MessageLoc = Tok.getLocation();
1050 PP.Lex(Tok);
1051 bool ExpectClosingParen = false;
1052 switch (Tok.getKind()) {
1053 case tok::l_paren:
1054 // We have a MSVC style pragma message.
1055 ExpectClosingParen = true;
1056 // Read the string.
1057 PP.Lex(Tok);
1058 break;
1059 case tok::string_literal:
1060 // We have a GCC style pragma message, and we just read the string.
1061 break;
1062 default:
1063 PP.Diag(MessageLoc, diag::err_pragma_message_malformed) << Kind;
1064 return;
1065 }
1066
1067 std::string MessageString;
1068 if (!PP.FinishLexStringLiteral(Tok, MessageString, PragmaKind(Kind),
1069 /*MacroExpansion=*/true))
1070 return;
1071
1072 if (ExpectClosingParen) {
1073 if (Tok.isNot(tok::r_paren)) {
1074 PP.Diag(Tok.getLocation(), diag::err_pragma_message_malformed) << Kind;
1075 return;
1076 }
1077 PP.Lex(Tok); // eat the r_paren.
1078 }
1079
1080 if (Tok.isNot(tok::eod)) {
1081 PP.Diag(Tok.getLocation(), diag::err_pragma_message_malformed) << Kind;
1082 return;
1083 }
1084
1085 // Output the message.
1086 PP.Diag(MessageLoc, (Kind == PPCallbacks::PMK_Error)
1087 ? diag::err_pragma_message
1088 : diag::warn_pragma_message) << MessageString;
1089
1090 // If the pragma is lexically sound, notify any interested PPCallbacks.
1091 if (PPCallbacks *Callbacks = PP.getPPCallbacks())
1092 Callbacks->PragmaMessage(MessageLoc, Namespace, Kind, MessageString);
Chris Lattnerabfe0942010-06-26 17:11:39 +00001093 }
1094};
1095
James Dennettb6e95b72012-06-17 03:26:26 +00001096/// PragmaPushMacroHandler - "\#pragma push_macro" saves the value of the
Chris Lattnerf47724b2010-08-17 15:55:45 +00001097/// macro on the top of the stack.
1098struct PragmaPushMacroHandler : public PragmaHandler {
1099 PragmaPushMacroHandler() : PragmaHandler("push_macro") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +00001100 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1101 Token &PushMacroTok) {
Chris Lattnerf47724b2010-08-17 15:55:45 +00001102 PP.HandlePragmaPushMacro(PushMacroTok);
1103 }
1104};
1105
1106
James Dennettb6e95b72012-06-17 03:26:26 +00001107/// PragmaPopMacroHandler - "\#pragma pop_macro" sets the value of the
Chris Lattnerf47724b2010-08-17 15:55:45 +00001108/// macro to the value on the top of the stack.
1109struct PragmaPopMacroHandler : public PragmaHandler {
1110 PragmaPopMacroHandler() : PragmaHandler("pop_macro") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +00001111 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1112 Token &PopMacroTok) {
Chris Lattnerf47724b2010-08-17 15:55:45 +00001113 PP.HandlePragmaPopMacro(PopMacroTok);
1114 }
1115};
1116
Chris Lattner062f2322009-04-19 21:20:35 +00001117// Pragma STDC implementations.
Chris Lattner6c5cf4a2009-04-19 21:50:08 +00001118
James Dennettb6e95b72012-06-17 03:26:26 +00001119/// PragmaSTDC_FENV_ACCESSHandler - "\#pragma STDC FENV_ACCESS ...".
Chris Lattner062f2322009-04-19 21:20:35 +00001120struct PragmaSTDC_FENV_ACCESSHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001121 PragmaSTDC_FENV_ACCESSHandler() : PragmaHandler("FENV_ACCESS") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +00001122 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1123 Token &Tok) {
Peter Collingbourne9d3f5f72011-02-14 01:42:24 +00001124 tok::OnOffSwitch OOS;
1125 if (PP.LexOnOffSwitch(OOS))
1126 return;
1127 if (OOS == tok::OOS_ON)
Chris Lattner4d8aac32009-04-19 21:55:32 +00001128 PP.Diag(Tok, diag::warn_stdc_fenv_access_not_supported);
Chris Lattner062f2322009-04-19 21:20:35 +00001129 }
1130};
Mike Stump1eb44332009-09-09 15:08:12 +00001131
James Dennettb6e95b72012-06-17 03:26:26 +00001132/// PragmaSTDC_CX_LIMITED_RANGEHandler - "\#pragma STDC CX_LIMITED_RANGE ...".
Chris Lattner062f2322009-04-19 21:20:35 +00001133struct PragmaSTDC_CX_LIMITED_RANGEHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001134 PragmaSTDC_CX_LIMITED_RANGEHandler()
1135 : PragmaHandler("CX_LIMITED_RANGE") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +00001136 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1137 Token &Tok) {
Peter Collingbourne9d3f5f72011-02-14 01:42:24 +00001138 tok::OnOffSwitch OOS;
1139 PP.LexOnOffSwitch(OOS);
Chris Lattner062f2322009-04-19 21:20:35 +00001140 }
1141};
Mike Stump1eb44332009-09-09 15:08:12 +00001142
James Dennettb6e95b72012-06-17 03:26:26 +00001143/// PragmaSTDC_UnknownHandler - "\#pragma STDC ...".
Chris Lattner062f2322009-04-19 21:20:35 +00001144struct PragmaSTDC_UnknownHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001145 PragmaSTDC_UnknownHandler() {}
Douglas Gregor80c60f72010-09-09 22:45:38 +00001146 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1147 Token &UnknownTok) {
Chris Lattner6c5cf4a2009-04-19 21:50:08 +00001148 // C99 6.10.6p2, unknown forms are not allowed.
Chris Lattnerf545be52009-04-19 21:25:37 +00001149 PP.Diag(UnknownTok, diag::ext_stdc_pragma_ignored);
Chris Lattner062f2322009-04-19 21:20:35 +00001150 }
1151};
Mike Stump1eb44332009-09-09 15:08:12 +00001152
John McCall8dfac0b2011-09-30 05:12:12 +00001153/// PragmaARCCFCodeAuditedHandler -
James Dennettb6e95b72012-06-17 03:26:26 +00001154/// \#pragma clang arc_cf_code_audited begin/end
John McCall8dfac0b2011-09-30 05:12:12 +00001155struct PragmaARCCFCodeAuditedHandler : public PragmaHandler {
1156 PragmaARCCFCodeAuditedHandler() : PragmaHandler("arc_cf_code_audited") {}
1157 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1158 Token &NameTok) {
1159 SourceLocation Loc = NameTok.getLocation();
1160 bool IsBegin;
1161
1162 Token Tok;
1163
1164 // Lex the 'begin' or 'end'.
1165 PP.LexUnexpandedToken(Tok);
1166 const IdentifierInfo *BeginEnd = Tok.getIdentifierInfo();
1167 if (BeginEnd && BeginEnd->isStr("begin")) {
1168 IsBegin = true;
1169 } else if (BeginEnd && BeginEnd->isStr("end")) {
1170 IsBegin = false;
1171 } else {
1172 PP.Diag(Tok.getLocation(), diag::err_pp_arc_cf_code_audited_syntax);
1173 return;
1174 }
1175
1176 // Verify that this is followed by EOD.
1177 PP.LexUnexpandedToken(Tok);
1178 if (Tok.isNot(tok::eod))
1179 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
1180
1181 // The start location of the active audit.
1182 SourceLocation BeginLoc = PP.getPragmaARCCFCodeAuditedLoc();
1183
1184 // The start location we want after processing this.
1185 SourceLocation NewLoc;
1186
1187 if (IsBegin) {
1188 // Complain about attempts to re-enter an audit.
1189 if (BeginLoc.isValid()) {
1190 PP.Diag(Loc, diag::err_pp_double_begin_of_arc_cf_code_audited);
1191 PP.Diag(BeginLoc, diag::note_pragma_entered_here);
1192 }
1193 NewLoc = Loc;
1194 } else {
1195 // Complain about attempts to leave an audit that doesn't exist.
1196 if (!BeginLoc.isValid()) {
1197 PP.Diag(Loc, diag::err_pp_unmatched_end_of_arc_cf_code_audited);
1198 return;
1199 }
1200 NewLoc = SourceLocation();
1201 }
1202
1203 PP.setPragmaARCCFCodeAuditedLoc(NewLoc);
1204 }
1205};
1206
Aaron Ballmanfafd1012012-11-30 19:52:30 +00001207 /// \brief Handle "\#pragma region [...]"
1208 ///
1209 /// The syntax is
1210 /// \code
Dmitri Gribenkoe74dc192012-11-30 20:04:39 +00001211 /// #pragma region [optional name]
1212 /// #pragma endregion [optional comment]
Aaron Ballmanfafd1012012-11-30 19:52:30 +00001213 /// \endcode
1214 ///
1215 /// \note This is
1216 /// <a href="http://msdn.microsoft.com/en-us/library/b6xkz944(v=vs.80).aspx">editor-only</a>
1217 /// pragma, just skipped by compiler.
1218 struct PragmaRegionHandler : public PragmaHandler {
1219 PragmaRegionHandler(const char *pragma) : PragmaHandler(pragma) { }
1220
1221 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1222 Token &NameTok) {
1223 // #pragma region: endregion matches can be verified
1224 // __pragma(region): no sense, but ignored by msvc
1225 // _Pragma is not valid for MSVC, but there isn't any point
1226 // to handle a _Pragma differently.
1227 }
1228 };
1229
Reid Spencer5f016e22007-07-11 17:01:13 +00001230} // end anonymous namespace
1231
1232
1233/// RegisterBuiltinPragmas - Install the standard preprocessor pragmas:
James Dennettb6e95b72012-06-17 03:26:26 +00001234/// \#pragma GCC poison/system_header/dependency and \#pragma once.
Reid Spencer5f016e22007-07-11 17:01:13 +00001235void Preprocessor::RegisterBuiltinPragmas() {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001236 AddPragmaHandler(new PragmaOnceHandler());
1237 AddPragmaHandler(new PragmaMarkHandler());
Chris Lattnerf47724b2010-08-17 15:55:45 +00001238 AddPragmaHandler(new PragmaPushMacroHandler());
1239 AddPragmaHandler(new PragmaPopMacroHandler());
Andy Gibbs076eea22013-04-17 16:16:16 +00001240 AddPragmaHandler(new PragmaMessageHandler(PPCallbacks::PMK_Message));
Mike Stump1eb44332009-09-09 15:08:12 +00001241
Chris Lattnere8fa06e2009-05-12 18:21:11 +00001242 // #pragma GCC ...
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001243 AddPragmaHandler("GCC", new PragmaPoisonHandler());
1244 AddPragmaHandler("GCC", new PragmaSystemHeaderHandler());
1245 AddPragmaHandler("GCC", new PragmaDependencyHandler());
Douglas Gregorc09ce122011-06-22 19:41:48 +00001246 AddPragmaHandler("GCC", new PragmaDiagnosticHandler("GCC"));
Andy Gibbs076eea22013-04-17 16:16:16 +00001247 AddPragmaHandler("GCC", new PragmaMessageHandler(PPCallbacks::PMK_Warning,
1248 "GCC"));
1249 AddPragmaHandler("GCC", new PragmaMessageHandler(PPCallbacks::PMK_Error,
1250 "GCC"));
Chris Lattnere8fa06e2009-05-12 18:21:11 +00001251 // #pragma clang ...
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001252 AddPragmaHandler("clang", new PragmaPoisonHandler());
1253 AddPragmaHandler("clang", new PragmaSystemHeaderHandler());
Daniel Dunbarabf7b722010-07-28 15:40:33 +00001254 AddPragmaHandler("clang", new PragmaDebugHandler());
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001255 AddPragmaHandler("clang", new PragmaDependencyHandler());
Douglas Gregorc09ce122011-06-22 19:41:48 +00001256 AddPragmaHandler("clang", new PragmaDiagnosticHandler("clang"));
John McCall8dfac0b2011-09-30 05:12:12 +00001257 AddPragmaHandler("clang", new PragmaARCCFCodeAuditedHandler());
Chris Lattnere8fa06e2009-05-12 18:21:11 +00001258
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001259 AddPragmaHandler("STDC", new PragmaSTDC_FENV_ACCESSHandler());
1260 AddPragmaHandler("STDC", new PragmaSTDC_CX_LIMITED_RANGEHandler());
Chris Lattner062f2322009-04-19 21:20:35 +00001261 AddPragmaHandler("STDC", new PragmaSTDC_UnknownHandler());
Mike Stump1eb44332009-09-09 15:08:12 +00001262
Chris Lattner636c5ef2009-01-16 08:21:25 +00001263 // MS extensions.
David Blaikie4e4d0842012-03-11 07:00:24 +00001264 if (LangOpts.MicrosoftExt) {
Aaron Ballman4c55c542012-03-02 22:51:54 +00001265 AddPragmaHandler(new PragmaIncludeAliasHandler());
Aaron Ballmanfafd1012012-11-30 19:52:30 +00001266 AddPragmaHandler(new PragmaRegionHandler("region"));
1267 AddPragmaHandler(new PragmaRegionHandler("endregion"));
Chris Lattnerabfe0942010-06-26 17:11:39 +00001268 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001269}