blob: 092216aef56faf99ee2503101ff15d7ccbfb705a [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);
Chris Lattner3692b092008-11-18 07:59:24 +0000187 if (Tok.isNot(tok::string_literal) && Tok.isNot(tok::wide_string_literal)) {
188 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
Chris Lattnera9d91452009-01-16 18:59:23 +0000222 // The _Pragma is lexically sound. Destringize according to C99 6.10.9.1:
223 // "The string literal is destringized by deleting the L prefix, if present,
224 // 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."
Reid Spencer5f016e22007-07-11 17:01:13 +0000227 if (StrVal[0] == 'L') // Remove L prefix.
228 StrVal.erase(StrVal.begin());
229 assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
230 "Invalid string token!");
Mike Stump1eb44332009-09-09 15:08:12 +0000231
Reid Spencer5f016e22007-07-11 17:01:13 +0000232 // Remove the front quote, replacing it with a space, so that the pragma
233 // contents appear to have a space before them.
234 StrVal[0] = ' ';
Mike Stump1eb44332009-09-09 15:08:12 +0000235
Chris Lattner1fa49532009-03-08 08:08:45 +0000236 // Replace the terminating quote with a \n.
Reid Spencer5f016e22007-07-11 17:01:13 +0000237 StrVal[StrVal.size()-1] = '\n';
Mike Stump1eb44332009-09-09 15:08:12 +0000238
Reid Spencer5f016e22007-07-11 17:01:13 +0000239 // Remove escaped quotes and escapes.
240 for (unsigned i = 0, e = StrVal.size(); i != e-1; ++i) {
241 if (StrVal[i] == '\\' &&
242 (StrVal[i+1] == '\\' || StrVal[i+1] == '"')) {
243 // \\ -> '\' and \" -> '"'.
244 StrVal.erase(StrVal.begin()+i);
245 --e;
246 }
247 }
John McCall1ef8a2e2010-08-28 22:34:47 +0000248
Peter Collingbournea5ef5842011-02-22 13:49:06 +0000249 // Plop the string (including the newline and trailing null) into a buffer
250 // where we can lex it.
251 Token TmpTok;
252 TmpTok.startToken();
Dmitri Gribenko374b3832012-09-24 21:07:17 +0000253 CreateString(StrVal, TmpTok);
Peter Collingbournea5ef5842011-02-22 13:49:06 +0000254 SourceLocation TokLoc = TmpTok.getLocation();
255
256 // Make and enter a lexer object so that we lex and expand the tokens just
257 // like any others.
258 Lexer *TL = Lexer::Create_PragmaLexer(TokLoc, PragmaLoc, RParenLoc,
259 StrVal.size(), *this);
260
261 EnterSourceFileWithLexer(TL, 0);
262
263 // With everything set up, lex this as a #pragma directive.
264 HandlePragmaDirective(PIK__Pragma);
John McCall1ef8a2e2010-08-28 22:34:47 +0000265
266 // Finally, return whatever came after the pragma directive.
267 return Lex(Tok);
268}
269
270/// HandleMicrosoft__pragma - Like Handle_Pragma except the pragma text
271/// is not enclosed within a string literal.
272void Preprocessor::HandleMicrosoft__pragma(Token &Tok) {
273 // Remember the pragma token location.
274 SourceLocation PragmaLoc = Tok.getLocation();
275
276 // Read the '('.
277 Lex(Tok);
278 if (Tok.isNot(tok::l_paren)) {
279 Diag(PragmaLoc, diag::err__Pragma_malformed);
280 return;
281 }
282
Peter Collingbournea5ef5842011-02-22 13:49:06 +0000283 // Get the tokens enclosed within the __pragma(), as well as the final ')'.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000284 SmallVector<Token, 32> PragmaToks;
John McCall1ef8a2e2010-08-28 22:34:47 +0000285 int NumParens = 0;
286 Lex(Tok);
287 while (Tok.isNot(tok::eof)) {
Peter Collingbournea5ef5842011-02-22 13:49:06 +0000288 PragmaToks.push_back(Tok);
John McCall1ef8a2e2010-08-28 22:34:47 +0000289 if (Tok.is(tok::l_paren))
290 NumParens++;
291 else if (Tok.is(tok::r_paren) && NumParens-- == 0)
292 break;
John McCall1ef8a2e2010-08-28 22:34:47 +0000293 Lex(Tok);
294 }
295
John McCall3da92a92010-08-29 01:09:54 +0000296 if (Tok.is(tok::eof)) {
297 Diag(PragmaLoc, diag::err_unterminated___pragma);
298 return;
299 }
300
Peter Collingbournea5ef5842011-02-22 13:49:06 +0000301 PragmaToks.front().setFlag(Token::LeadingSpace);
John McCall1ef8a2e2010-08-28 22:34:47 +0000302
Peter Collingbourne84021552011-02-28 02:37:51 +0000303 // Replace the ')' with an EOD to mark the end of the pragma.
304 PragmaToks.back().setKind(tok::eod);
Peter Collingbournea5ef5842011-02-22 13:49:06 +0000305
306 Token *TokArray = new Token[PragmaToks.size()];
307 std::copy(PragmaToks.begin(), PragmaToks.end(), TokArray);
308
309 // Push the tokens onto the stack.
310 EnterTokenStream(TokArray, PragmaToks.size(), true, true);
311
312 // With everything set up, lex this as a #pragma directive.
313 HandlePragmaDirective(PIK___pragma);
John McCall1ef8a2e2010-08-28 22:34:47 +0000314
315 // Finally, return whatever came after the pragma directive.
316 return Lex(Tok);
317}
318
James Dennettb6e95b72012-06-17 03:26:26 +0000319/// HandlePragmaOnce - Handle \#pragma once. OnceTok is the 'once'.
Reid Spencer5f016e22007-07-11 17:01:13 +0000320///
Chris Lattnerd2177732007-07-20 16:59:19 +0000321void Preprocessor::HandlePragmaOnce(Token &OnceTok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000322 if (isInPrimaryFile()) {
323 Diag(OnceTok, diag::pp_pragma_once_in_main_file);
324 return;
325 }
Mike Stump1eb44332009-09-09 15:08:12 +0000326
Reid Spencer5f016e22007-07-11 17:01:13 +0000327 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
Reid Spencer5f016e22007-07-11 17:01:13 +0000328 // Mark the file as a once-only file now.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000329 HeaderInfo.MarkFileIncludeOnce(getCurrentFileLexer()->getFileEntry());
Reid Spencer5f016e22007-07-11 17:01:13 +0000330}
331
Chris Lattner22434492007-12-19 19:38:36 +0000332void Preprocessor::HandlePragmaMark() {
Ted Kremenek17ff58a2008-11-19 22:21:33 +0000333 assert(CurPPLexer && "No current lexer?");
Chris Lattner6896a372009-06-15 05:02:34 +0000334 if (CurLexer)
335 CurLexer->ReadToEndOfLine();
336 else
337 CurPTHLexer->DiscardToEndOfLine();
Chris Lattner22434492007-12-19 19:38:36 +0000338}
339
340
James Dennettb6e95b72012-06-17 03:26:26 +0000341/// HandlePragmaPoison - Handle \#pragma GCC poison. PoisonTok is the 'poison'.
Reid Spencer5f016e22007-07-11 17:01:13 +0000342///
Chris Lattnerd2177732007-07-20 16:59:19 +0000343void Preprocessor::HandlePragmaPoison(Token &PoisonTok) {
344 Token Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000345
346 while (1) {
347 // Read the next token to poison. While doing this, pretend that we are
348 // skipping while reading the identifier to poison.
349 // This avoids errors on code like:
350 // #pragma GCC poison X
351 // #pragma GCC poison X
Ted Kremenek68a91d52008-11-18 01:12:54 +0000352 if (CurPPLexer) CurPPLexer->LexingRawMode = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000353 LexUnexpandedToken(Tok);
Ted Kremenek68a91d52008-11-18 01:12:54 +0000354 if (CurPPLexer) CurPPLexer->LexingRawMode = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000355
Reid Spencer5f016e22007-07-11 17:01:13 +0000356 // If we reached the end of line, we're done.
Peter Collingbourne84021552011-02-28 02:37:51 +0000357 if (Tok.is(tok::eod)) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000358
Reid Spencer5f016e22007-07-11 17:01:13 +0000359 // Can only poison identifiers.
Abramo Bagnarac4bf2b92010-12-22 08:23:18 +0000360 if (Tok.isNot(tok::raw_identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000361 Diag(Tok, diag::err_pp_invalid_poison);
362 return;
363 }
Mike Stump1eb44332009-09-09 15:08:12 +0000364
Reid Spencer5f016e22007-07-11 17:01:13 +0000365 // Look up the identifier info for the token. We disabled identifier lookup
366 // by saying we're skipping contents, so we need to do this manually.
367 IdentifierInfo *II = LookUpIdentifierInfo(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000368
Reid Spencer5f016e22007-07-11 17:01:13 +0000369 // Already poisoned.
370 if (II->isPoisoned()) continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000371
Reid Spencer5f016e22007-07-11 17:01:13 +0000372 // If this is a macro identifier, emit a warning.
Chris Lattner0edde552007-10-07 08:04:56 +0000373 if (II->hasMacroDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000374 Diag(Tok, diag::pp_poisoning_existing_macro);
Mike Stump1eb44332009-09-09 15:08:12 +0000375
Reid Spencer5f016e22007-07-11 17:01:13 +0000376 // Finally, poison it!
377 II->setIsPoisoned();
Douglas Gregoreee242f2011-10-27 09:33:13 +0000378 if (II->isFromAST())
379 II->setChangedSinceDeserialization();
Reid Spencer5f016e22007-07-11 17:01:13 +0000380 }
381}
382
James Dennettb6e95b72012-06-17 03:26:26 +0000383/// HandlePragmaSystemHeader - Implement \#pragma GCC system_header. We know
Reid Spencer5f016e22007-07-11 17:01:13 +0000384/// that the whole directive has been parsed.
Chris Lattnerd2177732007-07-20 16:59:19 +0000385void Preprocessor::HandlePragmaSystemHeader(Token &SysHeaderTok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000386 if (isInPrimaryFile()) {
387 Diag(SysHeaderTok, diag::pp_pragma_sysheader_in_main_file);
388 return;
389 }
Mike Stump1eb44332009-09-09 15:08:12 +0000390
Reid Spencer5f016e22007-07-11 17:01:13 +0000391 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
Ted Kremenek35c10c22008-11-20 01:45:11 +0000392 PreprocessorLexer *TheLexer = getCurrentFileLexer();
Mike Stump1eb44332009-09-09 15:08:12 +0000393
Reid Spencer5f016e22007-07-11 17:01:13 +0000394 // Mark the file as a system header.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000395 HeaderInfo.MarkFileSystemHeader(TheLexer->getFileEntry());
Mike Stump1eb44332009-09-09 15:08:12 +0000396
397
Chris Lattner6896a372009-06-15 05:02:34 +0000398 PresumedLoc PLoc = SourceMgr.getPresumedLoc(SysHeaderTok.getLocation());
Douglas Gregorcb7b1e12010-11-12 07:15:47 +0000399 if (PLoc.isInvalid())
400 return;
401
Jay Foad65aa6882011-06-21 15:13:30 +0000402 unsigned FilenameID = SourceMgr.getLineTableFilenameID(PLoc.getFilename());
Mike Stump1eb44332009-09-09 15:08:12 +0000403
Chris Lattner784c2572011-05-22 22:10:16 +0000404 // Notify the client, if desired, that we are in a new source file.
405 if (Callbacks)
406 Callbacks->FileChanged(SysHeaderTok.getLocation(),
407 PPCallbacks::SystemHeaderPragma, SrcMgr::C_System);
408
Chris Lattner6896a372009-06-15 05:02:34 +0000409 // Emit a line marker. This will change any source locations from this point
410 // forward to realize they are in a system header.
411 // Create a line note with this information.
412 SourceMgr.AddLineNote(SysHeaderTok.getLocation(), PLoc.getLine(), FilenameID,
413 false, false, true, false);
Reid Spencer5f016e22007-07-11 17:01:13 +0000414}
415
James Dennettb6e95b72012-06-17 03:26:26 +0000416/// HandlePragmaDependency - Handle \#pragma GCC dependency "foo" blah.
Reid Spencer5f016e22007-07-11 17:01:13 +0000417///
Chris Lattnerd2177732007-07-20 16:59:19 +0000418void Preprocessor::HandlePragmaDependency(Token &DependencyTok) {
419 Token FilenameTok;
Ted Kremenek68a91d52008-11-18 01:12:54 +0000420 CurPPLexer->LexIncludeFilename(FilenameTok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000421
Peter Collingbourne84021552011-02-28 02:37:51 +0000422 // If the token kind is EOD, the error has already been diagnosed.
423 if (FilenameTok.is(tok::eod))
Reid Spencer5f016e22007-07-11 17:01:13 +0000424 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000425
Reid Spencer5f016e22007-07-11 17:01:13 +0000426 // Reserve a buffer to get the spelling.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000427 SmallString<128> FilenameBuffer;
Douglas Gregor453091c2010-03-16 22:30:13 +0000428 bool Invalid = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000429 StringRef Filename = getSpelling(FilenameTok, FilenameBuffer, &Invalid);
Douglas Gregor453091c2010-03-16 22:30:13 +0000430 if (Invalid)
431 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000432
Chris Lattnera1394812010-01-10 01:35:12 +0000433 bool isAngled =
434 GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
Reid Spencer5f016e22007-07-11 17:01:13 +0000435 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
436 // error.
Chris Lattnera1394812010-01-10 01:35:12 +0000437 if (Filename.empty())
Reid Spencer5f016e22007-07-11 17:01:13 +0000438 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000439
Reid Spencer5f016e22007-07-11 17:01:13 +0000440 // Search include directories for this file.
441 const DirectoryLookup *CurDir;
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000442 const FileEntry *File = LookupFile(Filename, isAngled, 0, CurDir, NULL, NULL,
443 NULL);
Chris Lattner56b05c82008-11-18 08:02:48 +0000444 if (File == 0) {
Eli Friedmanf84139a2011-08-30 23:07:51 +0000445 if (!SuppressIncludeNotFoundError)
446 Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
Chris Lattner56b05c82008-11-18 08:02:48 +0000447 return;
448 }
Mike Stump1eb44332009-09-09 15:08:12 +0000449
Chris Lattner2b2453a2009-01-17 06:22:33 +0000450 const FileEntry *CurFile = getCurrentFileLexer()->getFileEntry();
Reid Spencer5f016e22007-07-11 17:01:13 +0000451
452 // If this file is older than the file it depends on, emit a diagnostic.
453 if (CurFile && CurFile->getModificationTime() < File->getModificationTime()) {
454 // Lex tokens at the end of the message and include them in the message.
455 std::string Message;
456 Lex(DependencyTok);
Peter Collingbourne84021552011-02-28 02:37:51 +0000457 while (DependencyTok.isNot(tok::eod)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000458 Message += getSpelling(DependencyTok) + " ";
459 Lex(DependencyTok);
460 }
Mike Stump1eb44332009-09-09 15:08:12 +0000461
Chris Lattner96de2592010-09-05 23:16:09 +0000462 // Remove the trailing ' ' if present.
463 if (!Message.empty())
464 Message.erase(Message.end()-1);
Chris Lattner56b05c82008-11-18 08:02:48 +0000465 Diag(FilenameTok, diag::pp_out_of_date_dependency) << Message;
Reid Spencer5f016e22007-07-11 17:01:13 +0000466 }
467}
468
James Dennettb6e95b72012-06-17 03:26:26 +0000469/// \brief Handle the microsoft \#pragma comment extension.
470///
471/// The syntax is:
472/// \code
Dmitri Gribenkoe74dc192012-11-30 20:04:39 +0000473/// #pragma comment(linker, "foo")
James Dennettb6e95b72012-06-17 03:26:26 +0000474/// \endcode
Chris Lattner636c5ef2009-01-16 08:21:25 +0000475/// 'linker' is one of five identifiers: compiler, exestr, lib, linker, user.
476/// "foo" is a string, which is fully macro expanded, and permits string
Gabor Greifd7ee3492009-03-17 11:39:38 +0000477/// concatenation, embedded escape characters etc. See MSDN for more details.
Chris Lattner636c5ef2009-01-16 08:21:25 +0000478void Preprocessor::HandlePragmaComment(Token &Tok) {
479 SourceLocation CommentLoc = Tok.getLocation();
480 Lex(Tok);
481 if (Tok.isNot(tok::l_paren)) {
482 Diag(CommentLoc, diag::err_pragma_comment_malformed);
483 return;
484 }
Mike Stump1eb44332009-09-09 15:08:12 +0000485
Chris Lattner636c5ef2009-01-16 08:21:25 +0000486 // Read the identifier.
487 Lex(Tok);
488 if (Tok.isNot(tok::identifier)) {
489 Diag(CommentLoc, diag::err_pragma_comment_malformed);
490 return;
491 }
Mike Stump1eb44332009-09-09 15:08:12 +0000492
Chris Lattner636c5ef2009-01-16 08:21:25 +0000493 // Verify that this is one of the 5 whitelisted options.
494 // FIXME: warn that 'exestr' is deprecated.
495 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000496 if (!II->isStr("compiler") && !II->isStr("exestr") && !II->isStr("lib") &&
Chris Lattner636c5ef2009-01-16 08:21:25 +0000497 !II->isStr("linker") && !II->isStr("user")) {
498 Diag(Tok.getLocation(), diag::err_pragma_comment_unknown_kind);
499 return;
500 }
Mike Stump1eb44332009-09-09 15:08:12 +0000501
Chris Lattnera9d91452009-01-16 18:59:23 +0000502 // Read the optional string if present.
Chris Lattner636c5ef2009-01-16 08:21:25 +0000503 Lex(Tok);
Chris Lattnera9d91452009-01-16 18:59:23 +0000504 std::string ArgumentString;
Andy Gibbs02a17682012-11-17 19:15:38 +0000505 if (Tok.is(tok::comma) && !LexStringLiteral(Tok, ArgumentString,
Andy Gibbs97f84612012-11-17 19:16:52 +0000506 "pragma comment",
Andy Gibbs02a17682012-11-17 19:15:38 +0000507 /*MacroExpansion=*/true))
508 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000509
Chris Lattnera9d91452009-01-16 18:59:23 +0000510 // FIXME: If the kind is "compiler" warn if the string is present (it is
511 // ignored).
512 // FIXME: 'lib' requires a comment string.
513 // FIXME: 'linker' requires a comment string, and has a specific list of
514 // things that are allowable.
Mike Stump1eb44332009-09-09 15:08:12 +0000515
Chris Lattner636c5ef2009-01-16 08:21:25 +0000516 if (Tok.isNot(tok::r_paren)) {
517 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
518 return;
519 }
Chris Lattnera9d91452009-01-16 18:59:23 +0000520 Lex(Tok); // eat the r_paren.
Chris Lattner636c5ef2009-01-16 08:21:25 +0000521
Peter Collingbourne84021552011-02-28 02:37:51 +0000522 if (Tok.isNot(tok::eod)) {
Chris Lattner636c5ef2009-01-16 08:21:25 +0000523 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
524 return;
525 }
Mike Stump1eb44332009-09-09 15:08:12 +0000526
Chris Lattnera9d91452009-01-16 18:59:23 +0000527 // If the pragma is lexically sound, notify any interested PPCallbacks.
Chris Lattner172e3362009-01-16 19:01:46 +0000528 if (Callbacks)
529 Callbacks->PragmaComment(CommentLoc, II, ArgumentString);
Chris Lattner636c5ef2009-01-16 08:21:25 +0000530}
531
James Dennettb6e95b72012-06-17 03:26:26 +0000532/// HandlePragmaMessage - Handle the microsoft and gcc \#pragma message
Michael J. Spencer301669b2010-09-27 06:19:02 +0000533/// extension. The syntax is:
James Dennettb6e95b72012-06-17 03:26:26 +0000534/// \code
Dmitri Gribenkoe74dc192012-11-30 20:04:39 +0000535/// #pragma message(string)
James Dennettb6e95b72012-06-17 03:26:26 +0000536/// \endcode
Michael J. Spencer301669b2010-09-27 06:19:02 +0000537/// OR, in GCC mode:
James Dennettb6e95b72012-06-17 03:26:26 +0000538/// \code
Dmitri Gribenkoe74dc192012-11-30 20:04:39 +0000539/// #pragma message string
James Dennettb6e95b72012-06-17 03:26:26 +0000540/// \endcode
Michael J. Spencer301669b2010-09-27 06:19:02 +0000541/// string is a string, which is fully macro expanded, and permits string
542/// concatenation, embedded escape characters, etc... See MSDN for more details.
Chris Lattnerabfe0942010-06-26 17:11:39 +0000543void Preprocessor::HandlePragmaMessage(Token &Tok) {
544 SourceLocation MessageLoc = Tok.getLocation();
545 Lex(Tok);
Michael J. Spencer301669b2010-09-27 06:19:02 +0000546 bool ExpectClosingParen = false;
Michael J. Spencerd83fc542010-09-27 06:34:47 +0000547 switch (Tok.getKind()) {
Michael J. Spencer301669b2010-09-27 06:19:02 +0000548 case tok::l_paren:
549 // We have a MSVC style pragma message.
550 ExpectClosingParen = true;
551 // Read the string.
552 Lex(Tok);
553 break;
554 case tok::string_literal:
555 // We have a GCC style pragma message, and we just read the string.
556 break;
557 default:
Chris Lattnerabfe0942010-06-26 17:11:39 +0000558 Diag(MessageLoc, diag::err_pragma_message_malformed);
559 return;
560 }
Chris Lattner636c5ef2009-01-16 08:21:25 +0000561
Andy Gibbs02a17682012-11-17 19:15:38 +0000562 std::string MessageString;
Andy Gibbs97f84612012-11-17 19:16:52 +0000563 if (!FinishLexStringLiteral(Tok, MessageString, "pragma message",
564 /*MacroExpansion=*/true))
Chris Lattnerabfe0942010-06-26 17:11:39 +0000565 return;
Chris Lattnerabfe0942010-06-26 17:11:39 +0000566
Michael J. Spencer301669b2010-09-27 06:19:02 +0000567 if (ExpectClosingParen) {
568 if (Tok.isNot(tok::r_paren)) {
569 Diag(Tok.getLocation(), diag::err_pragma_message_malformed);
570 return;
571 }
572 Lex(Tok); // eat the r_paren.
Chris Lattnerabfe0942010-06-26 17:11:39 +0000573 }
Chris Lattnerabfe0942010-06-26 17:11:39 +0000574
Peter Collingbourne84021552011-02-28 02:37:51 +0000575 if (Tok.isNot(tok::eod)) {
Chris Lattnerabfe0942010-06-26 17:11:39 +0000576 Diag(Tok.getLocation(), diag::err_pragma_message_malformed);
577 return;
578 }
579
580 // Output the message.
581 Diag(MessageLoc, diag::warn_pragma_message) << MessageString;
582
583 // If the pragma is lexically sound, notify any interested PPCallbacks.
584 if (Callbacks)
585 Callbacks->PragmaMessage(MessageLoc, MessageString);
586}
Chris Lattner636c5ef2009-01-16 08:21:25 +0000587
Chris Lattnerf47724b2010-08-17 15:55:45 +0000588/// ParsePragmaPushOrPopMacro - Handle parsing of pragma push_macro/pop_macro.
589/// Return the IdentifierInfo* associated with the macro to push or pop.
590IdentifierInfo *Preprocessor::ParsePragmaPushOrPopMacro(Token &Tok) {
591 // Remember the pragma token location.
592 Token PragmaTok = Tok;
593
594 // Read the '('.
595 Lex(Tok);
596 if (Tok.isNot(tok::l_paren)) {
597 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
598 << getSpelling(PragmaTok);
599 return 0;
600 }
601
602 // Read the macro name string.
603 Lex(Tok);
604 if (Tok.isNot(tok::string_literal)) {
605 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
606 << getSpelling(PragmaTok);
607 return 0;
608 }
609
Richard Smith99831e42012-03-06 03:21:47 +0000610 if (Tok.hasUDSuffix()) {
611 Diag(Tok, diag::err_invalid_string_udl);
612 return 0;
613 }
614
Chris Lattnerf47724b2010-08-17 15:55:45 +0000615 // Remember the macro string.
616 std::string StrVal = getSpelling(Tok);
617
618 // Read the ')'.
619 Lex(Tok);
620 if (Tok.isNot(tok::r_paren)) {
621 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
622 << getSpelling(PragmaTok);
623 return 0;
624 }
625
626 assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
627 "Invalid string token!");
628
629 // Create a Token from the string.
630 Token MacroTok;
631 MacroTok.startToken();
Abramo Bagnarac4bf2b92010-12-22 08:23:18 +0000632 MacroTok.setKind(tok::raw_identifier);
Dmitri Gribenko374b3832012-09-24 21:07:17 +0000633 CreateString(StringRef(&StrVal[1], StrVal.size() - 2), MacroTok);
Chris Lattnerf47724b2010-08-17 15:55:45 +0000634
635 // Get the IdentifierInfo of MacroToPushTok.
636 return LookUpIdentifierInfo(MacroTok);
637}
638
James Dennettb6e95b72012-06-17 03:26:26 +0000639/// \brief Handle \#pragma push_macro.
640///
Chris Lattnerf47724b2010-08-17 15:55:45 +0000641/// The syntax is:
James Dennettb6e95b72012-06-17 03:26:26 +0000642/// \code
Dmitri Gribenkoe74dc192012-11-30 20:04:39 +0000643/// #pragma push_macro("macro")
James Dennettb6e95b72012-06-17 03:26:26 +0000644/// \endcode
Chris Lattnerf47724b2010-08-17 15:55:45 +0000645void Preprocessor::HandlePragmaPushMacro(Token &PushMacroTok) {
646 // Parse the pragma directive and get the macro IdentifierInfo*.
647 IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PushMacroTok);
648 if (!IdentInfo) return;
649
650 // Get the MacroInfo associated with IdentInfo.
651 MacroInfo *MI = getMacroInfo(IdentInfo);
652
653 MacroInfo *MacroCopyToPush = 0;
654 if (MI) {
655 // Make a clone of MI.
656 MacroCopyToPush = CloneMacroInfo(*MI);
657
658 // Allow the original MacroInfo to be redefined later.
659 MI->setIsAllowRedefinitionsWithoutWarning(true);
660 }
661
662 // Push the cloned MacroInfo so we can retrieve it later.
663 PragmaPushMacroInfo[IdentInfo].push_back(MacroCopyToPush);
664}
665
James Dennettb6e95b72012-06-17 03:26:26 +0000666/// \brief Handle \#pragma pop_macro.
667///
Chris Lattnerf47724b2010-08-17 15:55:45 +0000668/// The syntax is:
James Dennettb6e95b72012-06-17 03:26:26 +0000669/// \code
Chris Lattnerf47724b2010-08-17 15:55:45 +0000670/// #pragma pop_macro("macro")
James Dennettb6e95b72012-06-17 03:26:26 +0000671/// \endcode
Chris Lattnerf47724b2010-08-17 15:55:45 +0000672void Preprocessor::HandlePragmaPopMacro(Token &PopMacroTok) {
673 SourceLocation MessageLoc = PopMacroTok.getLocation();
674
675 // Parse the pragma directive and get the macro IdentifierInfo*.
676 IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PopMacroTok);
677 if (!IdentInfo) return;
678
679 // Find the vector<MacroInfo*> associated with the macro.
680 llvm::DenseMap<IdentifierInfo*, std::vector<MacroInfo*> >::iterator iter =
681 PragmaPushMacroInfo.find(IdentInfo);
682 if (iter != PragmaPushMacroInfo.end()) {
Alexander Kornienko8a64bb52012-08-29 00:20:03 +0000683 // Forget the MacroInfo currently associated with IdentInfo.
684 if (MacroInfo *CurrentMI = getMacroInfo(IdentInfo)) {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000685 if (CurrentMI->isWarnIfUnused())
686 WarnUnusedMacroLocs.erase(CurrentMI->getDefinitionLoc());
Douglas Gregora8235d62012-10-09 23:05:51 +0000687 UndefineMacro(IdentInfo, CurrentMI, MessageLoc);
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000688 }
Chris Lattnerf47724b2010-08-17 15:55:45 +0000689
690 // Get the MacroInfo we want to reinstall.
691 MacroInfo *MacroToReInstall = iter->second.back();
692
Alexander Kornienkoe40c4232012-08-29 16:56:24 +0000693 if (MacroToReInstall) {
694 // Reinstall the previously pushed macro.
695 setMacroInfo(IdentInfo, MacroToReInstall);
696 } else if (IdentInfo->hasMacroDefinition()) {
697 clearMacroInfo(IdentInfo);
698 }
Chris Lattnerf47724b2010-08-17 15:55:45 +0000699
700 // Pop PragmaPushMacroInfo stack.
701 iter->second.pop_back();
702 if (iter->second.size() == 0)
703 PragmaPushMacroInfo.erase(iter);
704 } else {
705 Diag(MessageLoc, diag::warn_pragma_pop_macro_no_push)
706 << IdentInfo->getName();
707 }
708}
Reid Spencer5f016e22007-07-11 17:01:13 +0000709
Aaron Ballman4c55c542012-03-02 22:51:54 +0000710void Preprocessor::HandlePragmaIncludeAlias(Token &Tok) {
711 // We will either get a quoted filename or a bracketed filename, and we
712 // have to track which we got. The first filename is the source name,
713 // and the second name is the mapped filename. If the first is quoted,
714 // the second must be as well (cannot mix and match quotes and brackets).
Aaron Ballman4c55c542012-03-02 22:51:54 +0000715
716 // Get the open paren
717 Lex(Tok);
718 if (Tok.isNot(tok::l_paren)) {
719 Diag(Tok, diag::warn_pragma_include_alias_expected) << "(";
720 return;
721 }
722
723 // We expect either a quoted string literal, or a bracketed name
724 Token SourceFilenameTok;
725 CurPPLexer->LexIncludeFilename(SourceFilenameTok);
726 if (SourceFilenameTok.is(tok::eod)) {
727 // The diagnostic has already been handled
728 return;
729 }
730
731 StringRef SourceFileName;
732 SmallString<128> FileNameBuffer;
733 if (SourceFilenameTok.is(tok::string_literal) ||
734 SourceFilenameTok.is(tok::angle_string_literal)) {
735 SourceFileName = getSpelling(SourceFilenameTok, FileNameBuffer);
736 } else if (SourceFilenameTok.is(tok::less)) {
737 // This could be a path instead of just a name
738 FileNameBuffer.push_back('<');
739 SourceLocation End;
740 if (ConcatenateIncludeName(FileNameBuffer, End))
741 return; // Diagnostic already emitted
742 SourceFileName = FileNameBuffer.str();
743 } else {
744 Diag(Tok, diag::warn_pragma_include_alias_expected_filename);
745 return;
746 }
747 FileNameBuffer.clear();
748
749 // Now we expect a comma, followed by another include name
750 Lex(Tok);
751 if (Tok.isNot(tok::comma)) {
752 Diag(Tok, diag::warn_pragma_include_alias_expected) << ",";
753 return;
754 }
755
756 Token ReplaceFilenameTok;
757 CurPPLexer->LexIncludeFilename(ReplaceFilenameTok);
758 if (ReplaceFilenameTok.is(tok::eod)) {
759 // The diagnostic has already been handled
760 return;
761 }
762
763 StringRef ReplaceFileName;
764 if (ReplaceFilenameTok.is(tok::string_literal) ||
765 ReplaceFilenameTok.is(tok::angle_string_literal)) {
766 ReplaceFileName = getSpelling(ReplaceFilenameTok, FileNameBuffer);
767 } else if (ReplaceFilenameTok.is(tok::less)) {
768 // This could be a path instead of just a name
769 FileNameBuffer.push_back('<');
770 SourceLocation End;
771 if (ConcatenateIncludeName(FileNameBuffer, End))
772 return; // Diagnostic already emitted
773 ReplaceFileName = FileNameBuffer.str();
774 } else {
775 Diag(Tok, diag::warn_pragma_include_alias_expected_filename);
776 return;
777 }
778
779 // Finally, we expect the closing paren
780 Lex(Tok);
781 if (Tok.isNot(tok::r_paren)) {
782 Diag(Tok, diag::warn_pragma_include_alias_expected) << ")";
783 return;
784 }
785
786 // Now that we have the source and target filenames, we need to make sure
787 // they're both of the same type (angled vs non-angled)
788 StringRef OriginalSource = SourceFileName;
789
790 bool SourceIsAngled =
791 GetIncludeFilenameSpelling(SourceFilenameTok.getLocation(),
792 SourceFileName);
793 bool ReplaceIsAngled =
794 GetIncludeFilenameSpelling(ReplaceFilenameTok.getLocation(),
795 ReplaceFileName);
796 if (!SourceFileName.empty() && !ReplaceFileName.empty() &&
797 (SourceIsAngled != ReplaceIsAngled)) {
798 unsigned int DiagID;
799 if (SourceIsAngled)
800 DiagID = diag::warn_pragma_include_alias_mismatch_angle;
801 else
802 DiagID = diag::warn_pragma_include_alias_mismatch_quote;
803
804 Diag(SourceFilenameTok.getLocation(), DiagID)
805 << SourceFileName
806 << ReplaceFileName;
807
808 return;
809 }
810
811 // Now we can let the include handler know about this mapping
812 getHeaderSearchInfo().AddIncludeAlias(OriginalSource, ReplaceFileName);
813}
814
Reid Spencer5f016e22007-07-11 17:01:13 +0000815/// AddPragmaHandler - Add the specified pragma handler to the preprocessor.
816/// If 'Namespace' is non-null, then it is a token required to exist on the
817/// pragma line before the pragma string starts, e.g. "STDC" or "GCC".
Chris Lattner5f9e2722011-07-23 10:55:15 +0000818void Preprocessor::AddPragmaHandler(StringRef Namespace,
Reid Spencer5f016e22007-07-11 17:01:13 +0000819 PragmaHandler *Handler) {
820 PragmaNamespace *InsertNS = PragmaHandlers;
Mike Stump1eb44332009-09-09 15:08:12 +0000821
Reid Spencer5f016e22007-07-11 17:01:13 +0000822 // If this is specified to be in a namespace, step down into it.
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000823 if (!Namespace.empty()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000824 // If there is already a pragma handler with the name of this namespace,
825 // we either have an error (directive with the same name as a namespace) or
826 // we already have the namespace to insert into.
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000827 if (PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000828 InsertNS = Existing->getIfNamespace();
829 assert(InsertNS != 0 && "Cannot have a pragma namespace and pragma"
830 " handler with the same name!");
831 } else {
832 // Otherwise, this namespace doesn't exist yet, create and insert the
833 // handler for it.
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000834 InsertNS = new PragmaNamespace(Namespace);
Reid Spencer5f016e22007-07-11 17:01:13 +0000835 PragmaHandlers->AddPragma(InsertNS);
836 }
837 }
Mike Stump1eb44332009-09-09 15:08:12 +0000838
Reid Spencer5f016e22007-07-11 17:01:13 +0000839 // Check to make sure we don't already have a pragma for this identifier.
840 assert(!InsertNS->FindHandler(Handler->getName()) &&
841 "Pragma handler already exists for this identifier!");
842 InsertNS->AddPragma(Handler);
843}
844
Daniel Dunbar40950802008-10-04 19:17:46 +0000845/// RemovePragmaHandler - Remove the specific pragma handler from the
846/// preprocessor. If \arg Namespace is non-null, then it should be the
847/// namespace that \arg Handler was added to. It is an error to remove
848/// a handler that has not been registered.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000849void Preprocessor::RemovePragmaHandler(StringRef Namespace,
Daniel Dunbar40950802008-10-04 19:17:46 +0000850 PragmaHandler *Handler) {
851 PragmaNamespace *NS = PragmaHandlers;
Mike Stump1eb44332009-09-09 15:08:12 +0000852
Daniel Dunbar40950802008-10-04 19:17:46 +0000853 // If this is specified to be in a namespace, step down into it.
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000854 if (!Namespace.empty()) {
855 PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace);
Daniel Dunbar40950802008-10-04 19:17:46 +0000856 assert(Existing && "Namespace containing handler does not exist!");
857
858 NS = Existing->getIfNamespace();
859 assert(NS && "Invalid namespace, registered as a regular pragma handler!");
860 }
861
862 NS->RemovePragmaHandler(Handler);
Mike Stump1eb44332009-09-09 15:08:12 +0000863
Daniel Dunbar40950802008-10-04 19:17:46 +0000864 // If this is a non-default namespace and it is now empty, remove
865 // it.
Argyrios Kyrtzidisce52bb32012-01-06 00:22:09 +0000866 if (NS != PragmaHandlers && NS->IsEmpty()) {
Daniel Dunbar40950802008-10-04 19:17:46 +0000867 PragmaHandlers->RemovePragmaHandler(NS);
Argyrios Kyrtzidisce52bb32012-01-06 00:22:09 +0000868 delete NS;
869 }
Daniel Dunbar40950802008-10-04 19:17:46 +0000870}
871
Peter Collingbourne9d3f5f72011-02-14 01:42:24 +0000872bool Preprocessor::LexOnOffSwitch(tok::OnOffSwitch &Result) {
873 Token Tok;
874 LexUnexpandedToken(Tok);
875
876 if (Tok.isNot(tok::identifier)) {
877 Diag(Tok, diag::ext_on_off_switch_syntax);
878 return true;
879 }
880 IdentifierInfo *II = Tok.getIdentifierInfo();
881 if (II->isStr("ON"))
882 Result = tok::OOS_ON;
883 else if (II->isStr("OFF"))
884 Result = tok::OOS_OFF;
885 else if (II->isStr("DEFAULT"))
886 Result = tok::OOS_DEFAULT;
887 else {
888 Diag(Tok, diag::ext_on_off_switch_syntax);
889 return true;
890 }
891
Peter Collingbourne84021552011-02-28 02:37:51 +0000892 // Verify that this is followed by EOD.
Peter Collingbourne9d3f5f72011-02-14 01:42:24 +0000893 LexUnexpandedToken(Tok);
Peter Collingbourne84021552011-02-28 02:37:51 +0000894 if (Tok.isNot(tok::eod))
895 Diag(Tok, diag::ext_pragma_syntax_eod);
Peter Collingbourne9d3f5f72011-02-14 01:42:24 +0000896 return false;
897}
898
Reid Spencer5f016e22007-07-11 17:01:13 +0000899namespace {
James Dennettb6e95b72012-06-17 03:26:26 +0000900/// PragmaOnceHandler - "\#pragma once" marks the file as atomically included.
Reid Spencer5f016e22007-07-11 17:01:13 +0000901struct PragmaOnceHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000902 PragmaOnceHandler() : PragmaHandler("once") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000903 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
904 Token &OnceTok) {
Chris Lattner35410d52009-04-14 05:07:49 +0000905 PP.CheckEndOfDirective("pragma once");
Reid Spencer5f016e22007-07-11 17:01:13 +0000906 PP.HandlePragmaOnce(OnceTok);
907 }
908};
909
James Dennettb6e95b72012-06-17 03:26:26 +0000910/// PragmaMarkHandler - "\#pragma mark ..." is ignored by the compiler, and the
Chris Lattner22434492007-12-19 19:38:36 +0000911/// rest of the line is not lexed.
912struct PragmaMarkHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000913 PragmaMarkHandler() : PragmaHandler("mark") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000914 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
915 Token &MarkTok) {
Chris Lattner22434492007-12-19 19:38:36 +0000916 PP.HandlePragmaMark();
917 }
918};
919
James Dennettb6e95b72012-06-17 03:26:26 +0000920/// PragmaPoisonHandler - "\#pragma poison x" marks x as not usable.
Reid Spencer5f016e22007-07-11 17:01:13 +0000921struct PragmaPoisonHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000922 PragmaPoisonHandler() : PragmaHandler("poison") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000923 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
924 Token &PoisonTok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000925 PP.HandlePragmaPoison(PoisonTok);
926 }
927};
928
James Dennettb6e95b72012-06-17 03:26:26 +0000929/// PragmaSystemHeaderHandler - "\#pragma system_header" marks the current file
Chris Lattner22434492007-12-19 19:38:36 +0000930/// as a system header, which silences warnings in it.
Reid Spencer5f016e22007-07-11 17:01:13 +0000931struct PragmaSystemHeaderHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000932 PragmaSystemHeaderHandler() : PragmaHandler("system_header") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000933 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
934 Token &SHToken) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000935 PP.HandlePragmaSystemHeader(SHToken);
Chris Lattner35410d52009-04-14 05:07:49 +0000936 PP.CheckEndOfDirective("pragma");
Reid Spencer5f016e22007-07-11 17:01:13 +0000937 }
938};
939struct PragmaDependencyHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000940 PragmaDependencyHandler() : PragmaHandler("dependency") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000941 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
942 Token &DepToken) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000943 PP.HandlePragmaDependency(DepToken);
944 }
945};
Mike Stump1eb44332009-09-09 15:08:12 +0000946
Daniel Dunbarabf7b722010-07-28 15:40:33 +0000947struct PragmaDebugHandler : public PragmaHandler {
948 PragmaDebugHandler() : PragmaHandler("__debug") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000949 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
950 Token &DepToken) {
Daniel Dunbarabf7b722010-07-28 15:40:33 +0000951 Token Tok;
952 PP.LexUnexpandedToken(Tok);
953 if (Tok.isNot(tok::identifier)) {
Douglas Gregor6493a4d2010-08-30 15:15:34 +0000954 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Daniel Dunbarabf7b722010-07-28 15:40:33 +0000955 return;
956 }
957 IdentifierInfo *II = Tok.getIdentifierInfo();
958
Daniel Dunbar55054132010-08-17 22:32:48 +0000959 if (II->isStr("assert")) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000960 llvm_unreachable("This is an assertion!");
Daniel Dunbarabf7b722010-07-28 15:40:33 +0000961 } else if (II->isStr("crash")) {
David Blaikie377da4c2012-08-21 18:56:49 +0000962 LLVM_BUILTIN_TRAP;
David Blaikiee75d9cf2012-06-29 22:03:56 +0000963 } else if (II->isStr("parser_crash")) {
964 Token Crasher;
965 Crasher.setKind(tok::annot_pragma_parser_crash);
966 PP.EnterToken(Crasher);
Daniel Dunbar55054132010-08-17 22:32:48 +0000967 } else if (II->isStr("llvm_fatal_error")) {
968 llvm::report_fatal_error("#pragma clang __debug llvm_fatal_error");
969 } else if (II->isStr("llvm_unreachable")) {
970 llvm_unreachable("#pragma clang __debug llvm_unreachable");
971 } else if (II->isStr("overflow_stack")) {
972 DebugOverflowStack();
Daniel Dunbarff759a62010-08-18 23:09:23 +0000973 } else if (II->isStr("handle_crash")) {
974 llvm::CrashRecoveryContext *CRC =llvm::CrashRecoveryContext::GetCurrent();
975 if (CRC)
976 CRC->HandleCrash();
Daniel Dunbar55054132010-08-17 22:32:48 +0000977 } else {
978 PP.Diag(Tok, diag::warn_pragma_debug_unexpected_command)
979 << II->getName();
Daniel Dunbarabf7b722010-07-28 15:40:33 +0000980 }
981 }
982
Francois Pichet1066c6c2011-05-25 16:15:03 +0000983// Disable MSVC warning about runtime stack overflow.
984#ifdef _MSC_VER
985 #pragma warning(disable : 4717)
986#endif
Daniel Dunbarabf7b722010-07-28 15:40:33 +0000987 void DebugOverflowStack() {
988 DebugOverflowStack();
989 }
Francois Pichet1066c6c2011-05-25 16:15:03 +0000990#ifdef _MSC_VER
991 #pragma warning(default : 4717)
992#endif
993
Daniel Dunbarabf7b722010-07-28 15:40:33 +0000994};
995
James Dennettb6e95b72012-06-17 03:26:26 +0000996/// PragmaDiagnosticHandler - e.g. '\#pragma GCC diagnostic ignored "-Wformat"'
Chris Lattneredaf8772009-04-19 23:16:58 +0000997struct PragmaDiagnosticHandler : public PragmaHandler {
Douglas Gregorc09ce122011-06-22 19:41:48 +0000998private:
999 const char *Namespace;
Chris Lattner04ae2df2009-07-12 21:18:45 +00001000public:
Douglas Gregorc09ce122011-06-22 19:41:48 +00001001 explicit PragmaDiagnosticHandler(const char *NS) :
1002 PragmaHandler("diagnostic"), Namespace(NS) {}
Douglas Gregor80c60f72010-09-09 22:45:38 +00001003 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1004 Token &DiagToken) {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001005 SourceLocation DiagLoc = DiagToken.getLocation();
Chris Lattneredaf8772009-04-19 23:16:58 +00001006 Token Tok;
1007 PP.LexUnexpandedToken(Tok);
1008 if (Tok.isNot(tok::identifier)) {
Douglas Gregor6493a4d2010-08-30 15:15:34 +00001009 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Chris Lattneredaf8772009-04-19 23:16:58 +00001010 return;
1011 }
1012 IdentifierInfo *II = Tok.getIdentifierInfo();
Douglas Gregorc09ce122011-06-22 19:41:48 +00001013 PPCallbacks *Callbacks = PP.getPPCallbacks();
Mike Stump1eb44332009-09-09 15:08:12 +00001014
Chris Lattneredaf8772009-04-19 23:16:58 +00001015 diag::Mapping Map;
1016 if (II->isStr("warning"))
1017 Map = diag::MAP_WARNING;
1018 else if (II->isStr("error"))
1019 Map = diag::MAP_ERROR;
1020 else if (II->isStr("ignored"))
1021 Map = diag::MAP_IGNORE;
1022 else if (II->isStr("fatal"))
1023 Map = diag::MAP_FATAL;
Douglas Gregor6493a4d2010-08-30 15:15:34 +00001024 else if (II->isStr("pop")) {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001025 if (!PP.getDiagnostics().popMappings(DiagLoc))
Douglas Gregor6493a4d2010-08-30 15:15:34 +00001026 PP.Diag(Tok, diag::warn_pragma_diagnostic_cannot_pop);
Douglas Gregorc09ce122011-06-22 19:41:48 +00001027 else if (Callbacks)
1028 Callbacks->PragmaDiagnosticPop(DiagLoc, Namespace);
Douglas Gregor6493a4d2010-08-30 15:15:34 +00001029 return;
1030 } else if (II->isStr("push")) {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001031 PP.getDiagnostics().pushMappings(DiagLoc);
Douglas Gregorc09ce122011-06-22 19:41:48 +00001032 if (Callbacks)
1033 Callbacks->PragmaDiagnosticPush(DiagLoc, Namespace);
Chris Lattner04ae2df2009-07-12 21:18:45 +00001034 return;
1035 } else {
Douglas Gregor6493a4d2010-08-30 15:15:34 +00001036 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Chris Lattneredaf8772009-04-19 23:16:58 +00001037 return;
1038 }
Mike Stump1eb44332009-09-09 15:08:12 +00001039
Chris Lattneredaf8772009-04-19 23:16:58 +00001040 PP.LexUnexpandedToken(Tok);
Andy Gibbs02a17682012-11-17 19:15:38 +00001041 SourceLocation StringLoc = Tok.getLocation();
Chris Lattneredaf8772009-04-19 23:16:58 +00001042
Andy Gibbs02a17682012-11-17 19:15:38 +00001043 std::string WarningName;
Andy Gibbs97f84612012-11-17 19:16:52 +00001044 if (!PP.FinishLexStringLiteral(Tok, WarningName, "pragma diagnostic",
1045 /*MacroExpansion=*/false))
Chris Lattneredaf8772009-04-19 23:16:58 +00001046 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001047
Peter Collingbourne84021552011-02-28 02:37:51 +00001048 if (Tok.isNot(tok::eod)) {
Chris Lattneredaf8772009-04-19 23:16:58 +00001049 PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token);
1050 return;
1051 }
Mike Stump1eb44332009-09-09 15:08:12 +00001052
Chris Lattneredaf8772009-04-19 23:16:58 +00001053 if (WarningName.size() < 3 || WarningName[0] != '-' ||
1054 WarningName[1] != 'W') {
Andy Gibbs02a17682012-11-17 19:15:38 +00001055 PP.Diag(StringLoc, diag::warn_pragma_diagnostic_invalid_option);
Chris Lattneredaf8772009-04-19 23:16:58 +00001056 return;
1057 }
Mike Stump1eb44332009-09-09 15:08:12 +00001058
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +00001059 if (PP.getDiagnostics().setDiagnosticGroupMapping(WarningName.substr(2),
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001060 Map, DiagLoc))
Andy Gibbs02a17682012-11-17 19:15:38 +00001061 PP.Diag(StringLoc, diag::warn_pragma_diagnostic_unknown_warning)
1062 << WarningName;
Douglas Gregorc09ce122011-06-22 19:41:48 +00001063 else if (Callbacks)
1064 Callbacks->PragmaDiagnostic(DiagLoc, Namespace, Map, WarningName);
Chris Lattneredaf8772009-04-19 23:16:58 +00001065 }
1066};
Mike Stump1eb44332009-09-09 15:08:12 +00001067
James Dennettb6e95b72012-06-17 03:26:26 +00001068/// PragmaCommentHandler - "\#pragma comment ...".
Chris Lattner636c5ef2009-01-16 08:21:25 +00001069struct PragmaCommentHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001070 PragmaCommentHandler() : PragmaHandler("comment") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +00001071 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1072 Token &CommentTok) {
Chris Lattner636c5ef2009-01-16 08:21:25 +00001073 PP.HandlePragmaComment(CommentTok);
1074 }
1075};
Mike Stump1eb44332009-09-09 15:08:12 +00001076
James Dennettb6e95b72012-06-17 03:26:26 +00001077/// PragmaIncludeAliasHandler - "\#pragma include_alias("...")".
Aaron Ballman4c55c542012-03-02 22:51:54 +00001078struct PragmaIncludeAliasHandler : public PragmaHandler {
1079 PragmaIncludeAliasHandler() : PragmaHandler("include_alias") {}
1080 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1081 Token &IncludeAliasTok) {
1082 PP.HandlePragmaIncludeAlias(IncludeAliasTok);
1083 }
1084};
1085
James Dennettb6e95b72012-06-17 03:26:26 +00001086/// PragmaMessageHandler - "\#pragma message("...")".
Chris Lattnerabfe0942010-06-26 17:11:39 +00001087struct PragmaMessageHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001088 PragmaMessageHandler() : PragmaHandler("message") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +00001089 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1090 Token &CommentTok) {
Chris Lattnerabfe0942010-06-26 17:11:39 +00001091 PP.HandlePragmaMessage(CommentTok);
1092 }
1093};
1094
James Dennettb6e95b72012-06-17 03:26:26 +00001095/// PragmaPushMacroHandler - "\#pragma push_macro" saves the value of the
Chris Lattnerf47724b2010-08-17 15:55:45 +00001096/// macro on the top of the stack.
1097struct PragmaPushMacroHandler : public PragmaHandler {
1098 PragmaPushMacroHandler() : PragmaHandler("push_macro") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +00001099 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1100 Token &PushMacroTok) {
Chris Lattnerf47724b2010-08-17 15:55:45 +00001101 PP.HandlePragmaPushMacro(PushMacroTok);
1102 }
1103};
1104
1105
James Dennettb6e95b72012-06-17 03:26:26 +00001106/// PragmaPopMacroHandler - "\#pragma pop_macro" sets the value of the
Chris Lattnerf47724b2010-08-17 15:55:45 +00001107/// macro to the value on the top of the stack.
1108struct PragmaPopMacroHandler : public PragmaHandler {
1109 PragmaPopMacroHandler() : PragmaHandler("pop_macro") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +00001110 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1111 Token &PopMacroTok) {
Chris Lattnerf47724b2010-08-17 15:55:45 +00001112 PP.HandlePragmaPopMacro(PopMacroTok);
1113 }
1114};
1115
Chris Lattner062f2322009-04-19 21:20:35 +00001116// Pragma STDC implementations.
Chris Lattner6c5cf4a2009-04-19 21:50:08 +00001117
James Dennettb6e95b72012-06-17 03:26:26 +00001118/// PragmaSTDC_FENV_ACCESSHandler - "\#pragma STDC FENV_ACCESS ...".
Chris Lattner062f2322009-04-19 21:20:35 +00001119struct PragmaSTDC_FENV_ACCESSHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001120 PragmaSTDC_FENV_ACCESSHandler() : PragmaHandler("FENV_ACCESS") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +00001121 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1122 Token &Tok) {
Peter Collingbourne9d3f5f72011-02-14 01:42:24 +00001123 tok::OnOffSwitch OOS;
1124 if (PP.LexOnOffSwitch(OOS))
1125 return;
1126 if (OOS == tok::OOS_ON)
Chris Lattner4d8aac32009-04-19 21:55:32 +00001127 PP.Diag(Tok, diag::warn_stdc_fenv_access_not_supported);
Chris Lattner062f2322009-04-19 21:20:35 +00001128 }
1129};
Mike Stump1eb44332009-09-09 15:08:12 +00001130
James Dennettb6e95b72012-06-17 03:26:26 +00001131/// PragmaSTDC_CX_LIMITED_RANGEHandler - "\#pragma STDC CX_LIMITED_RANGE ...".
Chris Lattner062f2322009-04-19 21:20:35 +00001132struct PragmaSTDC_CX_LIMITED_RANGEHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001133 PragmaSTDC_CX_LIMITED_RANGEHandler()
1134 : PragmaHandler("CX_LIMITED_RANGE") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +00001135 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1136 Token &Tok) {
Peter Collingbourne9d3f5f72011-02-14 01:42:24 +00001137 tok::OnOffSwitch OOS;
1138 PP.LexOnOffSwitch(OOS);
Chris Lattner062f2322009-04-19 21:20:35 +00001139 }
1140};
Mike Stump1eb44332009-09-09 15:08:12 +00001141
James Dennettb6e95b72012-06-17 03:26:26 +00001142/// PragmaSTDC_UnknownHandler - "\#pragma STDC ...".
Chris Lattner062f2322009-04-19 21:20:35 +00001143struct PragmaSTDC_UnknownHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001144 PragmaSTDC_UnknownHandler() {}
Douglas Gregor80c60f72010-09-09 22:45:38 +00001145 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1146 Token &UnknownTok) {
Chris Lattner6c5cf4a2009-04-19 21:50:08 +00001147 // C99 6.10.6p2, unknown forms are not allowed.
Chris Lattnerf545be52009-04-19 21:25:37 +00001148 PP.Diag(UnknownTok, diag::ext_stdc_pragma_ignored);
Chris Lattner062f2322009-04-19 21:20:35 +00001149 }
1150};
Mike Stump1eb44332009-09-09 15:08:12 +00001151
John McCall8dfac0b2011-09-30 05:12:12 +00001152/// PragmaARCCFCodeAuditedHandler -
James Dennettb6e95b72012-06-17 03:26:26 +00001153/// \#pragma clang arc_cf_code_audited begin/end
John McCall8dfac0b2011-09-30 05:12:12 +00001154struct PragmaARCCFCodeAuditedHandler : public PragmaHandler {
1155 PragmaARCCFCodeAuditedHandler() : PragmaHandler("arc_cf_code_audited") {}
1156 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1157 Token &NameTok) {
1158 SourceLocation Loc = NameTok.getLocation();
1159 bool IsBegin;
1160
1161 Token Tok;
1162
1163 // Lex the 'begin' or 'end'.
1164 PP.LexUnexpandedToken(Tok);
1165 const IdentifierInfo *BeginEnd = Tok.getIdentifierInfo();
1166 if (BeginEnd && BeginEnd->isStr("begin")) {
1167 IsBegin = true;
1168 } else if (BeginEnd && BeginEnd->isStr("end")) {
1169 IsBegin = false;
1170 } else {
1171 PP.Diag(Tok.getLocation(), diag::err_pp_arc_cf_code_audited_syntax);
1172 return;
1173 }
1174
1175 // Verify that this is followed by EOD.
1176 PP.LexUnexpandedToken(Tok);
1177 if (Tok.isNot(tok::eod))
1178 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
1179
1180 // The start location of the active audit.
1181 SourceLocation BeginLoc = PP.getPragmaARCCFCodeAuditedLoc();
1182
1183 // The start location we want after processing this.
1184 SourceLocation NewLoc;
1185
1186 if (IsBegin) {
1187 // Complain about attempts to re-enter an audit.
1188 if (BeginLoc.isValid()) {
1189 PP.Diag(Loc, diag::err_pp_double_begin_of_arc_cf_code_audited);
1190 PP.Diag(BeginLoc, diag::note_pragma_entered_here);
1191 }
1192 NewLoc = Loc;
1193 } else {
1194 // Complain about attempts to leave an audit that doesn't exist.
1195 if (!BeginLoc.isValid()) {
1196 PP.Diag(Loc, diag::err_pp_unmatched_end_of_arc_cf_code_audited);
1197 return;
1198 }
1199 NewLoc = SourceLocation();
1200 }
1201
1202 PP.setPragmaARCCFCodeAuditedLoc(NewLoc);
1203 }
1204};
1205
Aaron Ballmanfafd1012012-11-30 19:52:30 +00001206 /// \brief Handle "\#pragma region [...]"
1207 ///
1208 /// The syntax is
1209 /// \code
Dmitri Gribenkoe74dc192012-11-30 20:04:39 +00001210 /// #pragma region [optional name]
1211 /// #pragma endregion [optional comment]
Aaron Ballmanfafd1012012-11-30 19:52:30 +00001212 /// \endcode
1213 ///
1214 /// \note This is
1215 /// <a href="http://msdn.microsoft.com/en-us/library/b6xkz944(v=vs.80).aspx">editor-only</a>
1216 /// pragma, just skipped by compiler.
1217 struct PragmaRegionHandler : public PragmaHandler {
1218 PragmaRegionHandler(const char *pragma) : PragmaHandler(pragma) { }
1219
1220 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1221 Token &NameTok) {
1222 // #pragma region: endregion matches can be verified
1223 // __pragma(region): no sense, but ignored by msvc
1224 // _Pragma is not valid for MSVC, but there isn't any point
1225 // to handle a _Pragma differently.
1226 }
1227 };
1228
Reid Spencer5f016e22007-07-11 17:01:13 +00001229} // end anonymous namespace
1230
1231
1232/// RegisterBuiltinPragmas - Install the standard preprocessor pragmas:
James Dennettb6e95b72012-06-17 03:26:26 +00001233/// \#pragma GCC poison/system_header/dependency and \#pragma once.
Reid Spencer5f016e22007-07-11 17:01:13 +00001234void Preprocessor::RegisterBuiltinPragmas() {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001235 AddPragmaHandler(new PragmaOnceHandler());
1236 AddPragmaHandler(new PragmaMarkHandler());
Chris Lattnerf47724b2010-08-17 15:55:45 +00001237 AddPragmaHandler(new PragmaPushMacroHandler());
1238 AddPragmaHandler(new PragmaPopMacroHandler());
Michael J. Spencer301669b2010-09-27 06:19:02 +00001239 AddPragmaHandler(new PragmaMessageHandler());
Mike Stump1eb44332009-09-09 15:08:12 +00001240
Chris Lattnere8fa06e2009-05-12 18:21:11 +00001241 // #pragma GCC ...
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001242 AddPragmaHandler("GCC", new PragmaPoisonHandler());
1243 AddPragmaHandler("GCC", new PragmaSystemHeaderHandler());
1244 AddPragmaHandler("GCC", new PragmaDependencyHandler());
Douglas Gregorc09ce122011-06-22 19:41:48 +00001245 AddPragmaHandler("GCC", new PragmaDiagnosticHandler("GCC"));
Chris Lattnere8fa06e2009-05-12 18:21:11 +00001246 // #pragma clang ...
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001247 AddPragmaHandler("clang", new PragmaPoisonHandler());
1248 AddPragmaHandler("clang", new PragmaSystemHeaderHandler());
Daniel Dunbarabf7b722010-07-28 15:40:33 +00001249 AddPragmaHandler("clang", new PragmaDebugHandler());
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001250 AddPragmaHandler("clang", new PragmaDependencyHandler());
Douglas Gregorc09ce122011-06-22 19:41:48 +00001251 AddPragmaHandler("clang", new PragmaDiagnosticHandler("clang"));
John McCall8dfac0b2011-09-30 05:12:12 +00001252 AddPragmaHandler("clang", new PragmaARCCFCodeAuditedHandler());
Chris Lattnere8fa06e2009-05-12 18:21:11 +00001253
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001254 AddPragmaHandler("STDC", new PragmaSTDC_FENV_ACCESSHandler());
1255 AddPragmaHandler("STDC", new PragmaSTDC_CX_LIMITED_RANGEHandler());
Chris Lattner062f2322009-04-19 21:20:35 +00001256 AddPragmaHandler("STDC", new PragmaSTDC_UnknownHandler());
Mike Stump1eb44332009-09-09 15:08:12 +00001257
Chris Lattner636c5ef2009-01-16 08:21:25 +00001258 // MS extensions.
David Blaikie4e4d0842012-03-11 07:00:24 +00001259 if (LangOpts.MicrosoftExt) {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001260 AddPragmaHandler(new PragmaCommentHandler());
Aaron Ballman4c55c542012-03-02 22:51:54 +00001261 AddPragmaHandler(new PragmaIncludeAliasHandler());
Aaron Ballmanfafd1012012-11-30 19:52:30 +00001262 AddPragmaHandler(new PragmaRegionHandler("region"));
1263 AddPragmaHandler(new PragmaRegionHandler("endregion"));
Chris Lattnerabfe0942010-06-26 17:11:39 +00001264 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001265}