blob: e2a192b01f287c38eed37aff7fe8942724335595 [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/Lex/HeaderSearch.h"
Chris Lattnera9d91452009-01-16 18:59:23 +000017#include "clang/Lex/LiteralSupport.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000018#include "clang/Lex/Preprocessor.h"
Chris Lattnerf47724b2010-08-17 15:55:45 +000019#include "clang/Lex/MacroInfo.h"
Chris Lattner500d3292009-01-29 05:15:15 +000020#include "clang/Lex/LexDiagnostic.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000021#include "clang/Basic/FileManager.h"
22#include "clang/Basic/SourceManager.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
46
47PragmaNamespace::~PragmaNamespace() {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +000048 for (llvm::StringMap<PragmaHandler*>::iterator
49 I = Handlers.begin(), E = Handlers.end(); I != E; ++I)
50 delete I->second;
Reid Spencer5f016e22007-07-11 17:01:13 +000051}
52
53/// FindHandler - Check to see if there is already a handler for the
54/// specified name. If not, return the handler for the null identifier if it
55/// exists, otherwise return null. If IgnoreNull is true (the default) then
56/// the null handler isn't returned on failure to match.
Chris Lattner5f9e2722011-07-23 10:55:15 +000057PragmaHandler *PragmaNamespace::FindHandler(StringRef Name,
Reid Spencer5f016e22007-07-11 17:01:13 +000058 bool IgnoreNull) const {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +000059 if (PragmaHandler *Handler = Handlers.lookup(Name))
60 return Handler;
Chris Lattner5f9e2722011-07-23 10:55:15 +000061 return IgnoreNull ? 0 : Handlers.lookup(StringRef());
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +000062}
Mike Stump1eb44332009-09-09 15:08:12 +000063
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +000064void PragmaNamespace::AddPragma(PragmaHandler *Handler) {
65 assert(!Handlers.lookup(Handler->getName()) &&
66 "A handler with this name is already registered in this namespace");
67 llvm::StringMapEntry<PragmaHandler *> &Entry =
68 Handlers.GetOrCreateValue(Handler->getName());
69 Entry.setValue(Handler);
Reid Spencer5f016e22007-07-11 17:01:13 +000070}
71
Daniel Dunbar40950802008-10-04 19:17:46 +000072void PragmaNamespace::RemovePragmaHandler(PragmaHandler *Handler) {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +000073 assert(Handlers.lookup(Handler->getName()) &&
74 "Handler not registered in this namespace");
75 Handlers.erase(Handler->getName());
Daniel Dunbar40950802008-10-04 19:17:46 +000076}
77
Douglas Gregor80c60f72010-09-09 22:45:38 +000078void PragmaNamespace::HandlePragma(Preprocessor &PP,
79 PragmaIntroducerKind Introducer,
80 Token &Tok) {
Reid Spencer5f016e22007-07-11 17:01:13 +000081 // Read the 'namespace' that the directive is in, e.g. STDC. Do not macro
82 // expand it, the user can have a STDC #define, that should not affect this.
83 PP.LexUnexpandedToken(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +000084
Reid Spencer5f016e22007-07-11 17:01:13 +000085 // Get the handler for this token. If there is no handler, ignore the pragma.
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +000086 PragmaHandler *Handler
87 = FindHandler(Tok.getIdentifierInfo() ? Tok.getIdentifierInfo()->getName()
Chris Lattner5f9e2722011-07-23 10:55:15 +000088 : StringRef(),
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +000089 /*IgnoreNull=*/false);
Chris Lattneraf7cdf42009-04-19 21:10:26 +000090 if (Handler == 0) {
91 PP.Diag(Tok, diag::warn_pragma_ignored);
92 return;
93 }
Mike Stump1eb44332009-09-09 15:08:12 +000094
Reid Spencer5f016e22007-07-11 17:01:13 +000095 // Otherwise, pass it down.
Douglas Gregor80c60f72010-09-09 22:45:38 +000096 Handler->HandlePragma(PP, Introducer, Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +000097}
98
99//===----------------------------------------------------------------------===//
100// Preprocessor Pragma Directive Handling.
101//===----------------------------------------------------------------------===//
102
103/// HandlePragmaDirective - The "#pragma" directive has been parsed. Lex the
104/// rest of the pragma, passing it to the registered pragma handlers.
Douglas Gregor80c60f72010-09-09 22:45:38 +0000105void Preprocessor::HandlePragmaDirective(unsigned Introducer) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000106 ++NumPragma;
Mike Stump1eb44332009-09-09 15:08:12 +0000107
Reid Spencer5f016e22007-07-11 17:01:13 +0000108 // Invoke the first level of pragma handlers which reads the namespace id.
Chris Lattnerd2177732007-07-20 16:59:19 +0000109 Token Tok;
Douglas Gregor80c60f72010-09-09 22:45:38 +0000110 PragmaHandlers->HandlePragma(*this, PragmaIntroducerKind(Introducer), Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000111
Reid Spencer5f016e22007-07-11 17:01:13 +0000112 // If the pragma handler didn't read the rest of the line, consume it now.
Peter Collingbourneb2eb53d2011-02-22 13:49:00 +0000113 if ((CurTokenLexer && CurTokenLexer->isParsingPreprocessorDirective())
114 || (CurPPLexer && CurPPLexer->ParsingPreprocessorDirective))
Reid Spencer5f016e22007-07-11 17:01:13 +0000115 DiscardUntilEndOfDirective();
116}
117
Argyrios Kyrtzidis14e64552012-04-03 16:47:40 +0000118namespace {
119/// \brief Helper class for \see Preprocessor::Handle_Pragma.
120class LexingFor_PragmaRAII {
121 Preprocessor &PP;
122 bool InMacroArgPreExpansion;
123 bool Failed;
124 Token &OutTok;
125 Token PragmaTok;
126
127public:
128 LexingFor_PragmaRAII(Preprocessor &PP, bool InMacroArgPreExpansion,
129 Token &Tok)
130 : PP(PP), InMacroArgPreExpansion(InMacroArgPreExpansion),
131 Failed(false), OutTok(Tok) {
132 if (InMacroArgPreExpansion) {
133 PragmaTok = OutTok;
134 PP.EnableBacktrackAtThisPos();
135 }
136 }
137
138 ~LexingFor_PragmaRAII() {
139 if (InMacroArgPreExpansion) {
140 if (Failed) {
141 PP.CommitBacktrackedTokens();
142 } else {
143 PP.Backtrack();
144 OutTok = PragmaTok;
145 }
146 }
147 }
148
149 void failed() {
150 Failed = true;
151 }
152};
153}
154
Reid Spencer5f016e22007-07-11 17:01:13 +0000155/// Handle_Pragma - Read a _Pragma directive, slice it up, process it, then
156/// return the first token after the directive. The _Pragma token has just
157/// been read into 'Tok'.
Chris Lattnerd2177732007-07-20 16:59:19 +0000158void Preprocessor::Handle_Pragma(Token &Tok) {
Argyrios Kyrtzidis14e64552012-04-03 16:47:40 +0000159
160 // This works differently if we are pre-expanding a macro argument.
161 // In that case we don't actually "activate" the pragma now, we only lex it
162 // until we are sure it is lexically correct and then we backtrack so that
163 // we activate the pragma whenever we encounter the tokens again in the token
164 // stream. This ensures that we will activate it in the correct location
165 // or that we will ignore it if it never enters the token stream, e.g:
166 //
167 // #define EMPTY(x)
168 // #define INACTIVE(x) EMPTY(x)
169 // INACTIVE(_Pragma("clang diagnostic ignored \"-Wconversion\""))
170
171 LexingFor_PragmaRAII _PragmaLexing(*this, InMacroArgPreExpansion, Tok);
172
Reid Spencer5f016e22007-07-11 17:01:13 +0000173 // Remember the pragma token location.
174 SourceLocation PragmaLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000175
Reid Spencer5f016e22007-07-11 17:01:13 +0000176 // Read the '('.
177 Lex(Tok);
Chris Lattner3692b092008-11-18 07:59:24 +0000178 if (Tok.isNot(tok::l_paren)) {
179 Diag(PragmaLoc, diag::err__Pragma_malformed);
Argyrios Kyrtzidis14e64552012-04-03 16:47:40 +0000180 return _PragmaLexing.failed();
Chris Lattner3692b092008-11-18 07:59:24 +0000181 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000182
183 // Read the '"..."'.
184 Lex(Tok);
Chris Lattner3692b092008-11-18 07:59:24 +0000185 if (Tok.isNot(tok::string_literal) && Tok.isNot(tok::wide_string_literal)) {
186 Diag(PragmaLoc, diag::err__Pragma_malformed);
Richard Smith99831e42012-03-06 03:21:47 +0000187 // Skip this token, and the ')', if present.
188 if (Tok.isNot(tok::r_paren))
189 Lex(Tok);
190 if (Tok.is(tok::r_paren))
191 Lex(Tok);
Argyrios Kyrtzidis14e64552012-04-03 16:47:40 +0000192 return _PragmaLexing.failed();
Richard Smith99831e42012-03-06 03:21:47 +0000193 }
194
195 if (Tok.hasUDSuffix()) {
196 Diag(Tok, diag::err_invalid_string_udl);
197 // Skip this token, and the ')', if present.
198 Lex(Tok);
199 if (Tok.is(tok::r_paren))
200 Lex(Tok);
Argyrios Kyrtzidis14e64552012-04-03 16:47:40 +0000201 return _PragmaLexing.failed();
Chris Lattner3692b092008-11-18 07:59:24 +0000202 }
Mike Stump1eb44332009-09-09 15:08:12 +0000203
Reid Spencer5f016e22007-07-11 17:01:13 +0000204 // Remember the string.
Argyrios Kyrtzidis14e64552012-04-03 16:47:40 +0000205 Token StrTok = Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000206
207 // Read the ')'.
208 Lex(Tok);
Chris Lattner3692b092008-11-18 07:59:24 +0000209 if (Tok.isNot(tok::r_paren)) {
210 Diag(PragmaLoc, diag::err__Pragma_malformed);
Argyrios Kyrtzidis14e64552012-04-03 16:47:40 +0000211 return _PragmaLexing.failed();
Chris Lattner3692b092008-11-18 07:59:24 +0000212 }
Mike Stump1eb44332009-09-09 15:08:12 +0000213
Argyrios Kyrtzidis14e64552012-04-03 16:47:40 +0000214 if (InMacroArgPreExpansion)
215 return;
216
Chris Lattnere7fb4842009-02-15 20:52:18 +0000217 SourceLocation RParenLoc = Tok.getLocation();
Argyrios Kyrtzidis14e64552012-04-03 16:47:40 +0000218 std::string StrVal = getSpelling(StrTok);
Mike Stump1eb44332009-09-09 15:08:12 +0000219
Chris Lattnera9d91452009-01-16 18:59:23 +0000220 // The _Pragma is lexically sound. Destringize according to C99 6.10.9.1:
221 // "The string literal is destringized by deleting the L prefix, if present,
222 // deleting the leading and trailing double-quotes, replacing each escape
223 // sequence \" by a double-quote, and replacing each escape sequence \\ by a
224 // single backslash."
Reid Spencer5f016e22007-07-11 17:01:13 +0000225 if (StrVal[0] == 'L') // Remove L prefix.
226 StrVal.erase(StrVal.begin());
227 assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
228 "Invalid string token!");
Mike Stump1eb44332009-09-09 15:08:12 +0000229
Reid Spencer5f016e22007-07-11 17:01:13 +0000230 // Remove the front quote, replacing it with a space, so that the pragma
231 // contents appear to have a space before them.
232 StrVal[0] = ' ';
Mike Stump1eb44332009-09-09 15:08:12 +0000233
Chris Lattner1fa49532009-03-08 08:08:45 +0000234 // Replace the terminating quote with a \n.
Reid Spencer5f016e22007-07-11 17:01:13 +0000235 StrVal[StrVal.size()-1] = '\n';
Mike Stump1eb44332009-09-09 15:08:12 +0000236
Reid Spencer5f016e22007-07-11 17:01:13 +0000237 // Remove escaped quotes and escapes.
238 for (unsigned i = 0, e = StrVal.size(); i != e-1; ++i) {
239 if (StrVal[i] == '\\' &&
240 (StrVal[i+1] == '\\' || StrVal[i+1] == '"')) {
241 // \\ -> '\' and \" -> '"'.
242 StrVal.erase(StrVal.begin()+i);
243 --e;
244 }
245 }
John McCall1ef8a2e2010-08-28 22:34:47 +0000246
Peter Collingbournea5ef5842011-02-22 13:49:06 +0000247 // Plop the string (including the newline and trailing null) into a buffer
248 // where we can lex it.
249 Token TmpTok;
250 TmpTok.startToken();
251 CreateString(&StrVal[0], StrVal.size(), TmpTok);
252 SourceLocation TokLoc = TmpTok.getLocation();
253
254 // Make and enter a lexer object so that we lex and expand the tokens just
255 // like any others.
256 Lexer *TL = Lexer::Create_PragmaLexer(TokLoc, PragmaLoc, RParenLoc,
257 StrVal.size(), *this);
258
259 EnterSourceFileWithLexer(TL, 0);
260
261 // With everything set up, lex this as a #pragma directive.
262 HandlePragmaDirective(PIK__Pragma);
John McCall1ef8a2e2010-08-28 22:34:47 +0000263
264 // Finally, return whatever came after the pragma directive.
265 return Lex(Tok);
266}
267
268/// HandleMicrosoft__pragma - Like Handle_Pragma except the pragma text
269/// is not enclosed within a string literal.
270void Preprocessor::HandleMicrosoft__pragma(Token &Tok) {
271 // Remember the pragma token location.
272 SourceLocation PragmaLoc = Tok.getLocation();
273
274 // Read the '('.
275 Lex(Tok);
276 if (Tok.isNot(tok::l_paren)) {
277 Diag(PragmaLoc, diag::err__Pragma_malformed);
278 return;
279 }
280
Peter Collingbournea5ef5842011-02-22 13:49:06 +0000281 // Get the tokens enclosed within the __pragma(), as well as the final ')'.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000282 SmallVector<Token, 32> PragmaToks;
John McCall1ef8a2e2010-08-28 22:34:47 +0000283 int NumParens = 0;
284 Lex(Tok);
285 while (Tok.isNot(tok::eof)) {
Peter Collingbournea5ef5842011-02-22 13:49:06 +0000286 PragmaToks.push_back(Tok);
John McCall1ef8a2e2010-08-28 22:34:47 +0000287 if (Tok.is(tok::l_paren))
288 NumParens++;
289 else if (Tok.is(tok::r_paren) && NumParens-- == 0)
290 break;
John McCall1ef8a2e2010-08-28 22:34:47 +0000291 Lex(Tok);
292 }
293
John McCall3da92a92010-08-29 01:09:54 +0000294 if (Tok.is(tok::eof)) {
295 Diag(PragmaLoc, diag::err_unterminated___pragma);
296 return;
297 }
298
Peter Collingbournea5ef5842011-02-22 13:49:06 +0000299 PragmaToks.front().setFlag(Token::LeadingSpace);
John McCall1ef8a2e2010-08-28 22:34:47 +0000300
Peter Collingbourne84021552011-02-28 02:37:51 +0000301 // Replace the ')' with an EOD to mark the end of the pragma.
302 PragmaToks.back().setKind(tok::eod);
Peter Collingbournea5ef5842011-02-22 13:49:06 +0000303
304 Token *TokArray = new Token[PragmaToks.size()];
305 std::copy(PragmaToks.begin(), PragmaToks.end(), TokArray);
306
307 // Push the tokens onto the stack.
308 EnterTokenStream(TokArray, PragmaToks.size(), true, true);
309
310 // With everything set up, lex this as a #pragma directive.
311 HandlePragmaDirective(PIK___pragma);
John McCall1ef8a2e2010-08-28 22:34:47 +0000312
313 // Finally, return whatever came after the pragma directive.
314 return Lex(Tok);
315}
316
Reid Spencer5f016e22007-07-11 17:01:13 +0000317/// HandlePragmaOnce - Handle #pragma once. OnceTok is the 'once'.
318///
Chris Lattnerd2177732007-07-20 16:59:19 +0000319void Preprocessor::HandlePragmaOnce(Token &OnceTok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000320 if (isInPrimaryFile()) {
321 Diag(OnceTok, diag::pp_pragma_once_in_main_file);
322 return;
323 }
Mike Stump1eb44332009-09-09 15:08:12 +0000324
Reid Spencer5f016e22007-07-11 17:01:13 +0000325 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
Reid Spencer5f016e22007-07-11 17:01:13 +0000326 // Mark the file as a once-only file now.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000327 HeaderInfo.MarkFileIncludeOnce(getCurrentFileLexer()->getFileEntry());
Reid Spencer5f016e22007-07-11 17:01:13 +0000328}
329
Chris Lattner22434492007-12-19 19:38:36 +0000330void Preprocessor::HandlePragmaMark() {
Ted Kremenek17ff58a2008-11-19 22:21:33 +0000331 assert(CurPPLexer && "No current lexer?");
Chris Lattner6896a372009-06-15 05:02:34 +0000332 if (CurLexer)
333 CurLexer->ReadToEndOfLine();
334 else
335 CurPTHLexer->DiscardToEndOfLine();
Chris Lattner22434492007-12-19 19:38:36 +0000336}
337
338
Reid Spencer5f016e22007-07-11 17:01:13 +0000339/// HandlePragmaPoison - Handle #pragma GCC poison. PoisonTok is the 'poison'.
340///
Chris Lattnerd2177732007-07-20 16:59:19 +0000341void Preprocessor::HandlePragmaPoison(Token &PoisonTok) {
342 Token Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000343
344 while (1) {
345 // Read the next token to poison. While doing this, pretend that we are
346 // skipping while reading the identifier to poison.
347 // This avoids errors on code like:
348 // #pragma GCC poison X
349 // #pragma GCC poison X
Ted Kremenek68a91d52008-11-18 01:12:54 +0000350 if (CurPPLexer) CurPPLexer->LexingRawMode = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000351 LexUnexpandedToken(Tok);
Ted Kremenek68a91d52008-11-18 01:12:54 +0000352 if (CurPPLexer) CurPPLexer->LexingRawMode = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000353
Reid Spencer5f016e22007-07-11 17:01:13 +0000354 // If we reached the end of line, we're done.
Peter Collingbourne84021552011-02-28 02:37:51 +0000355 if (Tok.is(tok::eod)) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000356
Reid Spencer5f016e22007-07-11 17:01:13 +0000357 // Can only poison identifiers.
Abramo Bagnarac4bf2b92010-12-22 08:23:18 +0000358 if (Tok.isNot(tok::raw_identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000359 Diag(Tok, diag::err_pp_invalid_poison);
360 return;
361 }
Mike Stump1eb44332009-09-09 15:08:12 +0000362
Reid Spencer5f016e22007-07-11 17:01:13 +0000363 // Look up the identifier info for the token. We disabled identifier lookup
364 // by saying we're skipping contents, so we need to do this manually.
365 IdentifierInfo *II = LookUpIdentifierInfo(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000366
Reid Spencer5f016e22007-07-11 17:01:13 +0000367 // Already poisoned.
368 if (II->isPoisoned()) continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000369
Reid Spencer5f016e22007-07-11 17:01:13 +0000370 // If this is a macro identifier, emit a warning.
Chris Lattner0edde552007-10-07 08:04:56 +0000371 if (II->hasMacroDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000372 Diag(Tok, diag::pp_poisoning_existing_macro);
Mike Stump1eb44332009-09-09 15:08:12 +0000373
Reid Spencer5f016e22007-07-11 17:01:13 +0000374 // Finally, poison it!
375 II->setIsPoisoned();
Douglas Gregoreee242f2011-10-27 09:33:13 +0000376 if (II->isFromAST())
377 II->setChangedSinceDeserialization();
Reid Spencer5f016e22007-07-11 17:01:13 +0000378 }
379}
380
381/// HandlePragmaSystemHeader - Implement #pragma GCC system_header. We know
382/// that the whole directive has been parsed.
Chris Lattnerd2177732007-07-20 16:59:19 +0000383void Preprocessor::HandlePragmaSystemHeader(Token &SysHeaderTok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000384 if (isInPrimaryFile()) {
385 Diag(SysHeaderTok, diag::pp_pragma_sysheader_in_main_file);
386 return;
387 }
Mike Stump1eb44332009-09-09 15:08:12 +0000388
Reid Spencer5f016e22007-07-11 17:01:13 +0000389 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
Ted Kremenek35c10c22008-11-20 01:45:11 +0000390 PreprocessorLexer *TheLexer = getCurrentFileLexer();
Mike Stump1eb44332009-09-09 15:08:12 +0000391
Reid Spencer5f016e22007-07-11 17:01:13 +0000392 // Mark the file as a system header.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000393 HeaderInfo.MarkFileSystemHeader(TheLexer->getFileEntry());
Mike Stump1eb44332009-09-09 15:08:12 +0000394
395
Chris Lattner6896a372009-06-15 05:02:34 +0000396 PresumedLoc PLoc = SourceMgr.getPresumedLoc(SysHeaderTok.getLocation());
Douglas Gregorcb7b1e12010-11-12 07:15:47 +0000397 if (PLoc.isInvalid())
398 return;
399
Jay Foad65aa6882011-06-21 15:13:30 +0000400 unsigned FilenameID = SourceMgr.getLineTableFilenameID(PLoc.getFilename());
Mike Stump1eb44332009-09-09 15:08:12 +0000401
Chris Lattner784c2572011-05-22 22:10:16 +0000402 // Notify the client, if desired, that we are in a new source file.
403 if (Callbacks)
404 Callbacks->FileChanged(SysHeaderTok.getLocation(),
405 PPCallbacks::SystemHeaderPragma, SrcMgr::C_System);
406
Chris Lattner6896a372009-06-15 05:02:34 +0000407 // Emit a line marker. This will change any source locations from this point
408 // forward to realize they are in a system header.
409 // Create a line note with this information.
410 SourceMgr.AddLineNote(SysHeaderTok.getLocation(), PLoc.getLine(), FilenameID,
411 false, false, true, false);
Reid Spencer5f016e22007-07-11 17:01:13 +0000412}
413
414/// HandlePragmaDependency - Handle #pragma GCC dependency "foo" blah.
415///
Chris Lattnerd2177732007-07-20 16:59:19 +0000416void Preprocessor::HandlePragmaDependency(Token &DependencyTok) {
417 Token FilenameTok;
Ted Kremenek68a91d52008-11-18 01:12:54 +0000418 CurPPLexer->LexIncludeFilename(FilenameTok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000419
Peter Collingbourne84021552011-02-28 02:37:51 +0000420 // If the token kind is EOD, the error has already been diagnosed.
421 if (FilenameTok.is(tok::eod))
Reid Spencer5f016e22007-07-11 17:01:13 +0000422 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000423
Reid Spencer5f016e22007-07-11 17:01:13 +0000424 // Reserve a buffer to get the spelling.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000425 SmallString<128> FilenameBuffer;
Douglas Gregor453091c2010-03-16 22:30:13 +0000426 bool Invalid = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000427 StringRef Filename = getSpelling(FilenameTok, FilenameBuffer, &Invalid);
Douglas Gregor453091c2010-03-16 22:30:13 +0000428 if (Invalid)
429 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000430
Chris Lattnera1394812010-01-10 01:35:12 +0000431 bool isAngled =
432 GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
Reid Spencer5f016e22007-07-11 17:01:13 +0000433 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
434 // error.
Chris Lattnera1394812010-01-10 01:35:12 +0000435 if (Filename.empty())
Reid Spencer5f016e22007-07-11 17:01:13 +0000436 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000437
Reid Spencer5f016e22007-07-11 17:01:13 +0000438 // Search include directories for this file.
439 const DirectoryLookup *CurDir;
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000440 const FileEntry *File = LookupFile(Filename, isAngled, 0, CurDir, NULL, NULL,
441 NULL);
Chris Lattner56b05c82008-11-18 08:02:48 +0000442 if (File == 0) {
Eli Friedmanf84139a2011-08-30 23:07:51 +0000443 if (!SuppressIncludeNotFoundError)
444 Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
Chris Lattner56b05c82008-11-18 08:02:48 +0000445 return;
446 }
Mike Stump1eb44332009-09-09 15:08:12 +0000447
Chris Lattner2b2453a2009-01-17 06:22:33 +0000448 const FileEntry *CurFile = getCurrentFileLexer()->getFileEntry();
Reid Spencer5f016e22007-07-11 17:01:13 +0000449
450 // If this file is older than the file it depends on, emit a diagnostic.
451 if (CurFile && CurFile->getModificationTime() < File->getModificationTime()) {
452 // Lex tokens at the end of the message and include them in the message.
453 std::string Message;
454 Lex(DependencyTok);
Peter Collingbourne84021552011-02-28 02:37:51 +0000455 while (DependencyTok.isNot(tok::eod)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000456 Message += getSpelling(DependencyTok) + " ";
457 Lex(DependencyTok);
458 }
Mike Stump1eb44332009-09-09 15:08:12 +0000459
Chris Lattner96de2592010-09-05 23:16:09 +0000460 // Remove the trailing ' ' if present.
461 if (!Message.empty())
462 Message.erase(Message.end()-1);
Chris Lattner56b05c82008-11-18 08:02:48 +0000463 Diag(FilenameTok, diag::pp_out_of_date_dependency) << Message;
Reid Spencer5f016e22007-07-11 17:01:13 +0000464 }
465}
466
Chris Lattner636c5ef2009-01-16 08:21:25 +0000467/// HandlePragmaComment - Handle the microsoft #pragma comment extension. The
468/// syntax is:
469/// #pragma comment(linker, "foo")
470/// 'linker' is one of five identifiers: compiler, exestr, lib, linker, user.
471/// "foo" is a string, which is fully macro expanded, and permits string
Gabor Greifd7ee3492009-03-17 11:39:38 +0000472/// concatenation, embedded escape characters etc. See MSDN for more details.
Chris Lattner636c5ef2009-01-16 08:21:25 +0000473void Preprocessor::HandlePragmaComment(Token &Tok) {
474 SourceLocation CommentLoc = Tok.getLocation();
475 Lex(Tok);
476 if (Tok.isNot(tok::l_paren)) {
477 Diag(CommentLoc, diag::err_pragma_comment_malformed);
478 return;
479 }
Mike Stump1eb44332009-09-09 15:08:12 +0000480
Chris Lattner636c5ef2009-01-16 08:21:25 +0000481 // Read the identifier.
482 Lex(Tok);
483 if (Tok.isNot(tok::identifier)) {
484 Diag(CommentLoc, diag::err_pragma_comment_malformed);
485 return;
486 }
Mike Stump1eb44332009-09-09 15:08:12 +0000487
Chris Lattner636c5ef2009-01-16 08:21:25 +0000488 // Verify that this is one of the 5 whitelisted options.
489 // FIXME: warn that 'exestr' is deprecated.
490 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000491 if (!II->isStr("compiler") && !II->isStr("exestr") && !II->isStr("lib") &&
Chris Lattner636c5ef2009-01-16 08:21:25 +0000492 !II->isStr("linker") && !II->isStr("user")) {
493 Diag(Tok.getLocation(), diag::err_pragma_comment_unknown_kind);
494 return;
495 }
Mike Stump1eb44332009-09-09 15:08:12 +0000496
Chris Lattnera9d91452009-01-16 18:59:23 +0000497 // Read the optional string if present.
Chris Lattner636c5ef2009-01-16 08:21:25 +0000498 Lex(Tok);
Chris Lattnera9d91452009-01-16 18:59:23 +0000499 std::string ArgumentString;
Chris Lattner636c5ef2009-01-16 08:21:25 +0000500 if (Tok.is(tok::comma)) {
Chris Lattnera9d91452009-01-16 18:59:23 +0000501 Lex(Tok); // eat the comma.
Chris Lattner636c5ef2009-01-16 08:21:25 +0000502
503 // We need at least one string.
Chris Lattneredaf8772009-04-19 23:16:58 +0000504 if (Tok.isNot(tok::string_literal)) {
Chris Lattner636c5ef2009-01-16 08:21:25 +0000505 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
506 return;
507 }
508
509 // String concatenation allows multiple strings, which can even come from
510 // macro expansion.
511 // "foo " "bar" "Baz"
Chris Lattner5f9e2722011-07-23 10:55:15 +0000512 SmallVector<Token, 4> StrToks;
Chris Lattneredaf8772009-04-19 23:16:58 +0000513 while (Tok.is(tok::string_literal)) {
Richard Smith99831e42012-03-06 03:21:47 +0000514 if (Tok.hasUDSuffix())
515 Diag(Tok, diag::err_invalid_string_udl);
Chris Lattnera9d91452009-01-16 18:59:23 +0000516 StrToks.push_back(Tok);
Chris Lattner636c5ef2009-01-16 08:21:25 +0000517 Lex(Tok);
Chris Lattnera9d91452009-01-16 18:59:23 +0000518 }
519
520 // Concatenate and parse the strings.
521 StringLiteralParser Literal(&StrToks[0], StrToks.size(), *this);
Douglas Gregor5cee1192011-07-27 05:40:30 +0000522 assert(Literal.isAscii() && "Didn't allow wide strings in");
Chris Lattnera9d91452009-01-16 18:59:23 +0000523 if (Literal.hadError)
524 return;
525 if (Literal.Pascal) {
526 Diag(StrToks[0].getLocation(), diag::err_pragma_comment_malformed);
527 return;
528 }
529
Jay Foad65aa6882011-06-21 15:13:30 +0000530 ArgumentString = Literal.GetString();
Chris Lattner636c5ef2009-01-16 08:21:25 +0000531 }
Mike Stump1eb44332009-09-09 15:08:12 +0000532
Chris Lattnera9d91452009-01-16 18:59:23 +0000533 // FIXME: If the kind is "compiler" warn if the string is present (it is
534 // ignored).
535 // FIXME: 'lib' requires a comment string.
536 // FIXME: 'linker' requires a comment string, and has a specific list of
537 // things that are allowable.
Mike Stump1eb44332009-09-09 15:08:12 +0000538
Chris Lattner636c5ef2009-01-16 08:21:25 +0000539 if (Tok.isNot(tok::r_paren)) {
540 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
541 return;
542 }
Chris Lattnera9d91452009-01-16 18:59:23 +0000543 Lex(Tok); // eat the r_paren.
Chris Lattner636c5ef2009-01-16 08:21:25 +0000544
Peter Collingbourne84021552011-02-28 02:37:51 +0000545 if (Tok.isNot(tok::eod)) {
Chris Lattner636c5ef2009-01-16 08:21:25 +0000546 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
547 return;
548 }
Mike Stump1eb44332009-09-09 15:08:12 +0000549
Chris Lattnera9d91452009-01-16 18:59:23 +0000550 // If the pragma is lexically sound, notify any interested PPCallbacks.
Chris Lattner172e3362009-01-16 19:01:46 +0000551 if (Callbacks)
552 Callbacks->PragmaComment(CommentLoc, II, ArgumentString);
Chris Lattner636c5ef2009-01-16 08:21:25 +0000553}
554
Michael J. Spencer301669b2010-09-27 06:19:02 +0000555/// HandlePragmaMessage - Handle the microsoft and gcc #pragma message
556/// extension. The syntax is:
557/// #pragma message(string)
558/// OR, in GCC mode:
559/// #pragma message string
560/// string is a string, which is fully macro expanded, and permits string
561/// concatenation, embedded escape characters, etc... See MSDN for more details.
Chris Lattnerabfe0942010-06-26 17:11:39 +0000562void Preprocessor::HandlePragmaMessage(Token &Tok) {
563 SourceLocation MessageLoc = Tok.getLocation();
564 Lex(Tok);
Michael J. Spencer301669b2010-09-27 06:19:02 +0000565 bool ExpectClosingParen = false;
Michael J. Spencerd83fc542010-09-27 06:34:47 +0000566 switch (Tok.getKind()) {
Michael J. Spencer301669b2010-09-27 06:19:02 +0000567 case tok::l_paren:
568 // We have a MSVC style pragma message.
569 ExpectClosingParen = true;
570 // Read the string.
571 Lex(Tok);
572 break;
573 case tok::string_literal:
574 // We have a GCC style pragma message, and we just read the string.
575 break;
576 default:
Chris Lattnerabfe0942010-06-26 17:11:39 +0000577 Diag(MessageLoc, diag::err_pragma_message_malformed);
578 return;
579 }
Chris Lattner636c5ef2009-01-16 08:21:25 +0000580
Chris Lattnerabfe0942010-06-26 17:11:39 +0000581 // We need at least one string.
582 if (Tok.isNot(tok::string_literal)) {
583 Diag(Tok.getLocation(), diag::err_pragma_message_malformed);
584 return;
585 }
586
587 // String concatenation allows multiple strings, which can even come from
588 // macro expansion.
589 // "foo " "bar" "Baz"
Chris Lattner5f9e2722011-07-23 10:55:15 +0000590 SmallVector<Token, 4> StrToks;
Chris Lattnerabfe0942010-06-26 17:11:39 +0000591 while (Tok.is(tok::string_literal)) {
Richard Smith99831e42012-03-06 03:21:47 +0000592 if (Tok.hasUDSuffix())
593 Diag(Tok, diag::err_invalid_string_udl);
Chris Lattnerabfe0942010-06-26 17:11:39 +0000594 StrToks.push_back(Tok);
595 Lex(Tok);
596 }
597
598 // Concatenate and parse the strings.
599 StringLiteralParser Literal(&StrToks[0], StrToks.size(), *this);
Douglas Gregor5cee1192011-07-27 05:40:30 +0000600 assert(Literal.isAscii() && "Didn't allow wide strings in");
Chris Lattnerabfe0942010-06-26 17:11:39 +0000601 if (Literal.hadError)
602 return;
603 if (Literal.Pascal) {
604 Diag(StrToks[0].getLocation(), diag::err_pragma_message_malformed);
605 return;
606 }
607
Chris Lattner5f9e2722011-07-23 10:55:15 +0000608 StringRef MessageString(Literal.GetString());
Chris Lattnerabfe0942010-06-26 17:11:39 +0000609
Michael J. Spencer301669b2010-09-27 06:19:02 +0000610 if (ExpectClosingParen) {
611 if (Tok.isNot(tok::r_paren)) {
612 Diag(Tok.getLocation(), diag::err_pragma_message_malformed);
613 return;
614 }
615 Lex(Tok); // eat the r_paren.
Chris Lattnerabfe0942010-06-26 17:11:39 +0000616 }
Chris Lattnerabfe0942010-06-26 17:11:39 +0000617
Peter Collingbourne84021552011-02-28 02:37:51 +0000618 if (Tok.isNot(tok::eod)) {
Chris Lattnerabfe0942010-06-26 17:11:39 +0000619 Diag(Tok.getLocation(), diag::err_pragma_message_malformed);
620 return;
621 }
622
623 // Output the message.
624 Diag(MessageLoc, diag::warn_pragma_message) << MessageString;
625
626 // If the pragma is lexically sound, notify any interested PPCallbacks.
627 if (Callbacks)
628 Callbacks->PragmaMessage(MessageLoc, MessageString);
629}
Chris Lattner636c5ef2009-01-16 08:21:25 +0000630
Chris Lattnerf47724b2010-08-17 15:55:45 +0000631/// ParsePragmaPushOrPopMacro - Handle parsing of pragma push_macro/pop_macro.
632/// Return the IdentifierInfo* associated with the macro to push or pop.
633IdentifierInfo *Preprocessor::ParsePragmaPushOrPopMacro(Token &Tok) {
634 // Remember the pragma token location.
635 Token PragmaTok = Tok;
636
637 // Read the '('.
638 Lex(Tok);
639 if (Tok.isNot(tok::l_paren)) {
640 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
641 << getSpelling(PragmaTok);
642 return 0;
643 }
644
645 // Read the macro name string.
646 Lex(Tok);
647 if (Tok.isNot(tok::string_literal)) {
648 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
649 << getSpelling(PragmaTok);
650 return 0;
651 }
652
Richard Smith99831e42012-03-06 03:21:47 +0000653 if (Tok.hasUDSuffix()) {
654 Diag(Tok, diag::err_invalid_string_udl);
655 return 0;
656 }
657
Chris Lattnerf47724b2010-08-17 15:55:45 +0000658 // Remember the macro string.
659 std::string StrVal = getSpelling(Tok);
660
661 // Read the ')'.
662 Lex(Tok);
663 if (Tok.isNot(tok::r_paren)) {
664 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
665 << getSpelling(PragmaTok);
666 return 0;
667 }
668
669 assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
670 "Invalid string token!");
671
672 // Create a Token from the string.
673 Token MacroTok;
674 MacroTok.startToken();
Abramo Bagnarac4bf2b92010-12-22 08:23:18 +0000675 MacroTok.setKind(tok::raw_identifier);
Chris Lattnerf47724b2010-08-17 15:55:45 +0000676 CreateString(&StrVal[1], StrVal.size() - 2, MacroTok);
677
678 // Get the IdentifierInfo of MacroToPushTok.
679 return LookUpIdentifierInfo(MacroTok);
680}
681
682/// HandlePragmaPushMacro - Handle #pragma push_macro.
683/// The syntax is:
684/// #pragma push_macro("macro")
685void Preprocessor::HandlePragmaPushMacro(Token &PushMacroTok) {
686 // Parse the pragma directive and get the macro IdentifierInfo*.
687 IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PushMacroTok);
688 if (!IdentInfo) return;
689
690 // Get the MacroInfo associated with IdentInfo.
691 MacroInfo *MI = getMacroInfo(IdentInfo);
692
693 MacroInfo *MacroCopyToPush = 0;
694 if (MI) {
695 // Make a clone of MI.
696 MacroCopyToPush = CloneMacroInfo(*MI);
697
698 // Allow the original MacroInfo to be redefined later.
699 MI->setIsAllowRedefinitionsWithoutWarning(true);
700 }
701
702 // Push the cloned MacroInfo so we can retrieve it later.
703 PragmaPushMacroInfo[IdentInfo].push_back(MacroCopyToPush);
704}
705
Ted Kremenekb275e3d2010-10-19 17:40:50 +0000706/// HandlePragmaPopMacro - Handle #pragma pop_macro.
Chris Lattnerf47724b2010-08-17 15:55:45 +0000707/// The syntax is:
708/// #pragma pop_macro("macro")
709void Preprocessor::HandlePragmaPopMacro(Token &PopMacroTok) {
710 SourceLocation MessageLoc = PopMacroTok.getLocation();
711
712 // Parse the pragma directive and get the macro IdentifierInfo*.
713 IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PopMacroTok);
714 if (!IdentInfo) return;
715
716 // Find the vector<MacroInfo*> associated with the macro.
717 llvm::DenseMap<IdentifierInfo*, std::vector<MacroInfo*> >::iterator iter =
718 PragmaPushMacroInfo.find(IdentInfo);
719 if (iter != PragmaPushMacroInfo.end()) {
720 // Release the MacroInfo currently associated with IdentInfo.
721 MacroInfo *CurrentMI = getMacroInfo(IdentInfo);
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000722 if (CurrentMI) {
723 if (CurrentMI->isWarnIfUnused())
724 WarnUnusedMacroLocs.erase(CurrentMI->getDefinitionLoc());
725 ReleaseMacroInfo(CurrentMI);
726 }
Chris Lattnerf47724b2010-08-17 15:55:45 +0000727
728 // Get the MacroInfo we want to reinstall.
729 MacroInfo *MacroToReInstall = iter->second.back();
730
731 // Reinstall the previously pushed macro.
732 setMacroInfo(IdentInfo, MacroToReInstall);
733
734 // Pop PragmaPushMacroInfo stack.
735 iter->second.pop_back();
736 if (iter->second.size() == 0)
737 PragmaPushMacroInfo.erase(iter);
738 } else {
739 Diag(MessageLoc, diag::warn_pragma_pop_macro_no_push)
740 << IdentInfo->getName();
741 }
742}
Reid Spencer5f016e22007-07-11 17:01:13 +0000743
Aaron Ballman4c55c542012-03-02 22:51:54 +0000744void Preprocessor::HandlePragmaIncludeAlias(Token &Tok) {
745 // We will either get a quoted filename or a bracketed filename, and we
746 // have to track which we got. The first filename is the source name,
747 // and the second name is the mapped filename. If the first is quoted,
748 // the second must be as well (cannot mix and match quotes and brackets).
Aaron Ballman4c55c542012-03-02 22:51:54 +0000749
750 // Get the open paren
751 Lex(Tok);
752 if (Tok.isNot(tok::l_paren)) {
753 Diag(Tok, diag::warn_pragma_include_alias_expected) << "(";
754 return;
755 }
756
757 // We expect either a quoted string literal, or a bracketed name
758 Token SourceFilenameTok;
759 CurPPLexer->LexIncludeFilename(SourceFilenameTok);
760 if (SourceFilenameTok.is(tok::eod)) {
761 // The diagnostic has already been handled
762 return;
763 }
764
765 StringRef SourceFileName;
766 SmallString<128> FileNameBuffer;
767 if (SourceFilenameTok.is(tok::string_literal) ||
768 SourceFilenameTok.is(tok::angle_string_literal)) {
769 SourceFileName = getSpelling(SourceFilenameTok, FileNameBuffer);
770 } else if (SourceFilenameTok.is(tok::less)) {
771 // This could be a path instead of just a name
772 FileNameBuffer.push_back('<');
773 SourceLocation End;
774 if (ConcatenateIncludeName(FileNameBuffer, End))
775 return; // Diagnostic already emitted
776 SourceFileName = FileNameBuffer.str();
777 } else {
778 Diag(Tok, diag::warn_pragma_include_alias_expected_filename);
779 return;
780 }
781 FileNameBuffer.clear();
782
783 // Now we expect a comma, followed by another include name
784 Lex(Tok);
785 if (Tok.isNot(tok::comma)) {
786 Diag(Tok, diag::warn_pragma_include_alias_expected) << ",";
787 return;
788 }
789
790 Token ReplaceFilenameTok;
791 CurPPLexer->LexIncludeFilename(ReplaceFilenameTok);
792 if (ReplaceFilenameTok.is(tok::eod)) {
793 // The diagnostic has already been handled
794 return;
795 }
796
797 StringRef ReplaceFileName;
798 if (ReplaceFilenameTok.is(tok::string_literal) ||
799 ReplaceFilenameTok.is(tok::angle_string_literal)) {
800 ReplaceFileName = getSpelling(ReplaceFilenameTok, FileNameBuffer);
801 } else if (ReplaceFilenameTok.is(tok::less)) {
802 // This could be a path instead of just a name
803 FileNameBuffer.push_back('<');
804 SourceLocation End;
805 if (ConcatenateIncludeName(FileNameBuffer, End))
806 return; // Diagnostic already emitted
807 ReplaceFileName = FileNameBuffer.str();
808 } else {
809 Diag(Tok, diag::warn_pragma_include_alias_expected_filename);
810 return;
811 }
812
813 // Finally, we expect the closing paren
814 Lex(Tok);
815 if (Tok.isNot(tok::r_paren)) {
816 Diag(Tok, diag::warn_pragma_include_alias_expected) << ")";
817 return;
818 }
819
820 // Now that we have the source and target filenames, we need to make sure
821 // they're both of the same type (angled vs non-angled)
822 StringRef OriginalSource = SourceFileName;
823
824 bool SourceIsAngled =
825 GetIncludeFilenameSpelling(SourceFilenameTok.getLocation(),
826 SourceFileName);
827 bool ReplaceIsAngled =
828 GetIncludeFilenameSpelling(ReplaceFilenameTok.getLocation(),
829 ReplaceFileName);
830 if (!SourceFileName.empty() && !ReplaceFileName.empty() &&
831 (SourceIsAngled != ReplaceIsAngled)) {
832 unsigned int DiagID;
833 if (SourceIsAngled)
834 DiagID = diag::warn_pragma_include_alias_mismatch_angle;
835 else
836 DiagID = diag::warn_pragma_include_alias_mismatch_quote;
837
838 Diag(SourceFilenameTok.getLocation(), DiagID)
839 << SourceFileName
840 << ReplaceFileName;
841
842 return;
843 }
844
845 // Now we can let the include handler know about this mapping
846 getHeaderSearchInfo().AddIncludeAlias(OriginalSource, ReplaceFileName);
847}
848
Reid Spencer5f016e22007-07-11 17:01:13 +0000849/// AddPragmaHandler - Add the specified pragma handler to the preprocessor.
850/// If 'Namespace' is non-null, then it is a token required to exist on the
851/// pragma line before the pragma string starts, e.g. "STDC" or "GCC".
Chris Lattner5f9e2722011-07-23 10:55:15 +0000852void Preprocessor::AddPragmaHandler(StringRef Namespace,
Reid Spencer5f016e22007-07-11 17:01:13 +0000853 PragmaHandler *Handler) {
854 PragmaNamespace *InsertNS = PragmaHandlers;
Mike Stump1eb44332009-09-09 15:08:12 +0000855
Reid Spencer5f016e22007-07-11 17:01:13 +0000856 // If this is specified to be in a namespace, step down into it.
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000857 if (!Namespace.empty()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000858 // If there is already a pragma handler with the name of this namespace,
859 // we either have an error (directive with the same name as a namespace) or
860 // we already have the namespace to insert into.
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000861 if (PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000862 InsertNS = Existing->getIfNamespace();
863 assert(InsertNS != 0 && "Cannot have a pragma namespace and pragma"
864 " handler with the same name!");
865 } else {
866 // Otherwise, this namespace doesn't exist yet, create and insert the
867 // handler for it.
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000868 InsertNS = new PragmaNamespace(Namespace);
Reid Spencer5f016e22007-07-11 17:01:13 +0000869 PragmaHandlers->AddPragma(InsertNS);
870 }
871 }
Mike Stump1eb44332009-09-09 15:08:12 +0000872
Reid Spencer5f016e22007-07-11 17:01:13 +0000873 // Check to make sure we don't already have a pragma for this identifier.
874 assert(!InsertNS->FindHandler(Handler->getName()) &&
875 "Pragma handler already exists for this identifier!");
876 InsertNS->AddPragma(Handler);
877}
878
Daniel Dunbar40950802008-10-04 19:17:46 +0000879/// RemovePragmaHandler - Remove the specific pragma handler from the
880/// preprocessor. If \arg Namespace is non-null, then it should be the
881/// namespace that \arg Handler was added to. It is an error to remove
882/// a handler that has not been registered.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000883void Preprocessor::RemovePragmaHandler(StringRef Namespace,
Daniel Dunbar40950802008-10-04 19:17:46 +0000884 PragmaHandler *Handler) {
885 PragmaNamespace *NS = PragmaHandlers;
Mike Stump1eb44332009-09-09 15:08:12 +0000886
Daniel Dunbar40950802008-10-04 19:17:46 +0000887 // If this is specified to be in a namespace, step down into it.
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000888 if (!Namespace.empty()) {
889 PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace);
Daniel Dunbar40950802008-10-04 19:17:46 +0000890 assert(Existing && "Namespace containing handler does not exist!");
891
892 NS = Existing->getIfNamespace();
893 assert(NS && "Invalid namespace, registered as a regular pragma handler!");
894 }
895
896 NS->RemovePragmaHandler(Handler);
Mike Stump1eb44332009-09-09 15:08:12 +0000897
Daniel Dunbar40950802008-10-04 19:17:46 +0000898 // If this is a non-default namespace and it is now empty, remove
899 // it.
Argyrios Kyrtzidisce52bb32012-01-06 00:22:09 +0000900 if (NS != PragmaHandlers && NS->IsEmpty()) {
Daniel Dunbar40950802008-10-04 19:17:46 +0000901 PragmaHandlers->RemovePragmaHandler(NS);
Argyrios Kyrtzidisce52bb32012-01-06 00:22:09 +0000902 delete NS;
903 }
Daniel Dunbar40950802008-10-04 19:17:46 +0000904}
905
Peter Collingbourne9d3f5f72011-02-14 01:42:24 +0000906bool Preprocessor::LexOnOffSwitch(tok::OnOffSwitch &Result) {
907 Token Tok;
908 LexUnexpandedToken(Tok);
909
910 if (Tok.isNot(tok::identifier)) {
911 Diag(Tok, diag::ext_on_off_switch_syntax);
912 return true;
913 }
914 IdentifierInfo *II = Tok.getIdentifierInfo();
915 if (II->isStr("ON"))
916 Result = tok::OOS_ON;
917 else if (II->isStr("OFF"))
918 Result = tok::OOS_OFF;
919 else if (II->isStr("DEFAULT"))
920 Result = tok::OOS_DEFAULT;
921 else {
922 Diag(Tok, diag::ext_on_off_switch_syntax);
923 return true;
924 }
925
Peter Collingbourne84021552011-02-28 02:37:51 +0000926 // Verify that this is followed by EOD.
Peter Collingbourne9d3f5f72011-02-14 01:42:24 +0000927 LexUnexpandedToken(Tok);
Peter Collingbourne84021552011-02-28 02:37:51 +0000928 if (Tok.isNot(tok::eod))
929 Diag(Tok, diag::ext_pragma_syntax_eod);
Peter Collingbourne9d3f5f72011-02-14 01:42:24 +0000930 return false;
931}
932
Reid Spencer5f016e22007-07-11 17:01:13 +0000933namespace {
Chris Lattner22434492007-12-19 19:38:36 +0000934/// PragmaOnceHandler - "#pragma once" marks the file as atomically included.
Reid Spencer5f016e22007-07-11 17:01:13 +0000935struct PragmaOnceHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000936 PragmaOnceHandler() : PragmaHandler("once") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000937 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
938 Token &OnceTok) {
Chris Lattner35410d52009-04-14 05:07:49 +0000939 PP.CheckEndOfDirective("pragma once");
Reid Spencer5f016e22007-07-11 17:01:13 +0000940 PP.HandlePragmaOnce(OnceTok);
941 }
942};
943
Chris Lattner22434492007-12-19 19:38:36 +0000944/// PragmaMarkHandler - "#pragma mark ..." is ignored by the compiler, and the
945/// rest of the line is not lexed.
946struct PragmaMarkHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000947 PragmaMarkHandler() : PragmaHandler("mark") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000948 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
949 Token &MarkTok) {
Chris Lattner22434492007-12-19 19:38:36 +0000950 PP.HandlePragmaMark();
951 }
952};
953
954/// PragmaPoisonHandler - "#pragma poison x" marks x as not usable.
Reid Spencer5f016e22007-07-11 17:01:13 +0000955struct PragmaPoisonHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000956 PragmaPoisonHandler() : PragmaHandler("poison") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000957 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
958 Token &PoisonTok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000959 PP.HandlePragmaPoison(PoisonTok);
960 }
961};
962
Chris Lattner22434492007-12-19 19:38:36 +0000963/// PragmaSystemHeaderHandler - "#pragma system_header" marks the current file
964/// as a system header, which silences warnings in it.
Reid Spencer5f016e22007-07-11 17:01:13 +0000965struct PragmaSystemHeaderHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000966 PragmaSystemHeaderHandler() : PragmaHandler("system_header") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000967 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
968 Token &SHToken) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000969 PP.HandlePragmaSystemHeader(SHToken);
Chris Lattner35410d52009-04-14 05:07:49 +0000970 PP.CheckEndOfDirective("pragma");
Reid Spencer5f016e22007-07-11 17:01:13 +0000971 }
972};
973struct PragmaDependencyHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000974 PragmaDependencyHandler() : PragmaHandler("dependency") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000975 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
976 Token &DepToken) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000977 PP.HandlePragmaDependency(DepToken);
978 }
979};
Mike Stump1eb44332009-09-09 15:08:12 +0000980
Daniel Dunbarabf7b722010-07-28 15:40:33 +0000981struct PragmaDebugHandler : public PragmaHandler {
982 PragmaDebugHandler() : PragmaHandler("__debug") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000983 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
984 Token &DepToken) {
Daniel Dunbarabf7b722010-07-28 15:40:33 +0000985 Token Tok;
986 PP.LexUnexpandedToken(Tok);
987 if (Tok.isNot(tok::identifier)) {
Douglas Gregor6493a4d2010-08-30 15:15:34 +0000988 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Daniel Dunbarabf7b722010-07-28 15:40:33 +0000989 return;
990 }
991 IdentifierInfo *II = Tok.getIdentifierInfo();
992
Daniel Dunbar55054132010-08-17 22:32:48 +0000993 if (II->isStr("assert")) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000994 llvm_unreachable("This is an assertion!");
Daniel Dunbarabf7b722010-07-28 15:40:33 +0000995 } else if (II->isStr("crash")) {
Daniel Dunbar55054132010-08-17 22:32:48 +0000996 *(volatile int*) 0x11 = 0;
997 } else if (II->isStr("llvm_fatal_error")) {
998 llvm::report_fatal_error("#pragma clang __debug llvm_fatal_error");
999 } else if (II->isStr("llvm_unreachable")) {
1000 llvm_unreachable("#pragma clang __debug llvm_unreachable");
1001 } else if (II->isStr("overflow_stack")) {
1002 DebugOverflowStack();
Daniel Dunbarff759a62010-08-18 23:09:23 +00001003 } else if (II->isStr("handle_crash")) {
1004 llvm::CrashRecoveryContext *CRC =llvm::CrashRecoveryContext::GetCurrent();
1005 if (CRC)
1006 CRC->HandleCrash();
Daniel Dunbar55054132010-08-17 22:32:48 +00001007 } else {
1008 PP.Diag(Tok, diag::warn_pragma_debug_unexpected_command)
1009 << II->getName();
Daniel Dunbarabf7b722010-07-28 15:40:33 +00001010 }
1011 }
1012
Francois Pichet1066c6c2011-05-25 16:15:03 +00001013// Disable MSVC warning about runtime stack overflow.
1014#ifdef _MSC_VER
1015 #pragma warning(disable : 4717)
1016#endif
Daniel Dunbarabf7b722010-07-28 15:40:33 +00001017 void DebugOverflowStack() {
1018 DebugOverflowStack();
1019 }
Francois Pichet1066c6c2011-05-25 16:15:03 +00001020#ifdef _MSC_VER
1021 #pragma warning(default : 4717)
1022#endif
1023
Daniel Dunbarabf7b722010-07-28 15:40:33 +00001024};
1025
Chris Lattneredaf8772009-04-19 23:16:58 +00001026/// PragmaDiagnosticHandler - e.g. '#pragma GCC diagnostic ignored "-Wformat"'
1027struct PragmaDiagnosticHandler : public PragmaHandler {
Douglas Gregorc09ce122011-06-22 19:41:48 +00001028private:
1029 const char *Namespace;
Chris Lattner04ae2df2009-07-12 21:18:45 +00001030public:
Douglas Gregorc09ce122011-06-22 19:41:48 +00001031 explicit PragmaDiagnosticHandler(const char *NS) :
1032 PragmaHandler("diagnostic"), Namespace(NS) {}
Douglas Gregor80c60f72010-09-09 22:45:38 +00001033 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1034 Token &DiagToken) {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001035 SourceLocation DiagLoc = DiagToken.getLocation();
Chris Lattneredaf8772009-04-19 23:16:58 +00001036 Token Tok;
1037 PP.LexUnexpandedToken(Tok);
1038 if (Tok.isNot(tok::identifier)) {
Douglas Gregor6493a4d2010-08-30 15:15:34 +00001039 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Chris Lattneredaf8772009-04-19 23:16:58 +00001040 return;
1041 }
1042 IdentifierInfo *II = Tok.getIdentifierInfo();
Douglas Gregorc09ce122011-06-22 19:41:48 +00001043 PPCallbacks *Callbacks = PP.getPPCallbacks();
Mike Stump1eb44332009-09-09 15:08:12 +00001044
Chris Lattneredaf8772009-04-19 23:16:58 +00001045 diag::Mapping Map;
1046 if (II->isStr("warning"))
1047 Map = diag::MAP_WARNING;
1048 else if (II->isStr("error"))
1049 Map = diag::MAP_ERROR;
1050 else if (II->isStr("ignored"))
1051 Map = diag::MAP_IGNORE;
1052 else if (II->isStr("fatal"))
1053 Map = diag::MAP_FATAL;
Douglas Gregor6493a4d2010-08-30 15:15:34 +00001054 else if (II->isStr("pop")) {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001055 if (!PP.getDiagnostics().popMappings(DiagLoc))
Douglas Gregor6493a4d2010-08-30 15:15:34 +00001056 PP.Diag(Tok, diag::warn_pragma_diagnostic_cannot_pop);
Douglas Gregorc09ce122011-06-22 19:41:48 +00001057 else if (Callbacks)
1058 Callbacks->PragmaDiagnosticPop(DiagLoc, Namespace);
Douglas Gregor6493a4d2010-08-30 15:15:34 +00001059 return;
1060 } else if (II->isStr("push")) {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001061 PP.getDiagnostics().pushMappings(DiagLoc);
Douglas Gregorc09ce122011-06-22 19:41:48 +00001062 if (Callbacks)
1063 Callbacks->PragmaDiagnosticPush(DiagLoc, Namespace);
Chris Lattner04ae2df2009-07-12 21:18:45 +00001064 return;
1065 } else {
Douglas Gregor6493a4d2010-08-30 15:15:34 +00001066 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Chris Lattneredaf8772009-04-19 23:16:58 +00001067 return;
1068 }
Mike Stump1eb44332009-09-09 15:08:12 +00001069
Chris Lattneredaf8772009-04-19 23:16:58 +00001070 PP.LexUnexpandedToken(Tok);
1071
1072 // We need at least one string.
1073 if (Tok.isNot(tok::string_literal)) {
1074 PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token);
1075 return;
1076 }
Mike Stump1eb44332009-09-09 15:08:12 +00001077
Chris Lattneredaf8772009-04-19 23:16:58 +00001078 // String concatenation allows multiple strings, which can even come from
1079 // macro expansion.
1080 // "foo " "bar" "Baz"
Chris Lattner5f9e2722011-07-23 10:55:15 +00001081 SmallVector<Token, 4> StrToks;
Chris Lattneredaf8772009-04-19 23:16:58 +00001082 while (Tok.is(tok::string_literal)) {
1083 StrToks.push_back(Tok);
1084 PP.LexUnexpandedToken(Tok);
1085 }
Mike Stump1eb44332009-09-09 15:08:12 +00001086
Peter Collingbourne84021552011-02-28 02:37:51 +00001087 if (Tok.isNot(tok::eod)) {
Chris Lattneredaf8772009-04-19 23:16:58 +00001088 PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token);
1089 return;
1090 }
Mike Stump1eb44332009-09-09 15:08:12 +00001091
Chris Lattneredaf8772009-04-19 23:16:58 +00001092 // Concatenate and parse the strings.
1093 StringLiteralParser Literal(&StrToks[0], StrToks.size(), PP);
Douglas Gregor5cee1192011-07-27 05:40:30 +00001094 assert(Literal.isAscii() && "Didn't allow wide strings in");
Chris Lattneredaf8772009-04-19 23:16:58 +00001095 if (Literal.hadError)
1096 return;
1097 if (Literal.Pascal) {
Douglas Gregor6493a4d2010-08-30 15:15:34 +00001098 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Chris Lattneredaf8772009-04-19 23:16:58 +00001099 return;
1100 }
Chris Lattner04ae2df2009-07-12 21:18:45 +00001101
Chris Lattner5f9e2722011-07-23 10:55:15 +00001102 StringRef WarningName(Literal.GetString());
Chris Lattneredaf8772009-04-19 23:16:58 +00001103
1104 if (WarningName.size() < 3 || WarningName[0] != '-' ||
1105 WarningName[1] != 'W') {
1106 PP.Diag(StrToks[0].getLocation(),
1107 diag::warn_pragma_diagnostic_invalid_option);
1108 return;
1109 }
Mike Stump1eb44332009-09-09 15:08:12 +00001110
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +00001111 if (PP.getDiagnostics().setDiagnosticGroupMapping(WarningName.substr(2),
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001112 Map, DiagLoc))
Chris Lattneredaf8772009-04-19 23:16:58 +00001113 PP.Diag(StrToks[0].getLocation(),
1114 diag::warn_pragma_diagnostic_unknown_warning) << WarningName;
Douglas Gregorc09ce122011-06-22 19:41:48 +00001115 else if (Callbacks)
1116 Callbacks->PragmaDiagnostic(DiagLoc, Namespace, Map, WarningName);
Chris Lattneredaf8772009-04-19 23:16:58 +00001117 }
1118};
Mike Stump1eb44332009-09-09 15:08:12 +00001119
Chris Lattner636c5ef2009-01-16 08:21:25 +00001120/// PragmaCommentHandler - "#pragma comment ...".
1121struct PragmaCommentHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001122 PragmaCommentHandler() : PragmaHandler("comment") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +00001123 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1124 Token &CommentTok) {
Chris Lattner636c5ef2009-01-16 08:21:25 +00001125 PP.HandlePragmaComment(CommentTok);
1126 }
1127};
Mike Stump1eb44332009-09-09 15:08:12 +00001128
Aaron Ballman4c55c542012-03-02 22:51:54 +00001129/// PragmaIncludeAliasHandler - "#pragma include_alias("...")".
1130struct PragmaIncludeAliasHandler : public PragmaHandler {
1131 PragmaIncludeAliasHandler() : PragmaHandler("include_alias") {}
1132 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1133 Token &IncludeAliasTok) {
1134 PP.HandlePragmaIncludeAlias(IncludeAliasTok);
1135 }
1136};
1137
Chris Lattnerabfe0942010-06-26 17:11:39 +00001138/// PragmaMessageHandler - "#pragma message("...")".
1139struct PragmaMessageHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001140 PragmaMessageHandler() : PragmaHandler("message") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +00001141 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1142 Token &CommentTok) {
Chris Lattnerabfe0942010-06-26 17:11:39 +00001143 PP.HandlePragmaMessage(CommentTok);
1144 }
1145};
1146
Chris Lattnerf47724b2010-08-17 15:55:45 +00001147/// PragmaPushMacroHandler - "#pragma push_macro" saves the value of the
1148/// macro on the top of the stack.
1149struct PragmaPushMacroHandler : public PragmaHandler {
1150 PragmaPushMacroHandler() : PragmaHandler("push_macro") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +00001151 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1152 Token &PushMacroTok) {
Chris Lattnerf47724b2010-08-17 15:55:45 +00001153 PP.HandlePragmaPushMacro(PushMacroTok);
1154 }
1155};
1156
1157
1158/// PragmaPopMacroHandler - "#pragma pop_macro" sets the value of the
1159/// macro to the value on the top of the stack.
1160struct PragmaPopMacroHandler : public PragmaHandler {
1161 PragmaPopMacroHandler() : PragmaHandler("pop_macro") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +00001162 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1163 Token &PopMacroTok) {
Chris Lattnerf47724b2010-08-17 15:55:45 +00001164 PP.HandlePragmaPopMacro(PopMacroTok);
1165 }
1166};
1167
Chris Lattner062f2322009-04-19 21:20:35 +00001168// Pragma STDC implementations.
Chris Lattner6c5cf4a2009-04-19 21:50:08 +00001169
Chris Lattner062f2322009-04-19 21:20:35 +00001170/// PragmaSTDC_FENV_ACCESSHandler - "#pragma STDC FENV_ACCESS ...".
1171struct PragmaSTDC_FENV_ACCESSHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001172 PragmaSTDC_FENV_ACCESSHandler() : PragmaHandler("FENV_ACCESS") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +00001173 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1174 Token &Tok) {
Peter Collingbourne9d3f5f72011-02-14 01:42:24 +00001175 tok::OnOffSwitch OOS;
1176 if (PP.LexOnOffSwitch(OOS))
1177 return;
1178 if (OOS == tok::OOS_ON)
Chris Lattner4d8aac32009-04-19 21:55:32 +00001179 PP.Diag(Tok, diag::warn_stdc_fenv_access_not_supported);
Chris Lattner062f2322009-04-19 21:20:35 +00001180 }
1181};
Mike Stump1eb44332009-09-09 15:08:12 +00001182
Chris Lattner062f2322009-04-19 21:20:35 +00001183/// PragmaSTDC_CX_LIMITED_RANGEHandler - "#pragma STDC CX_LIMITED_RANGE ...".
1184struct PragmaSTDC_CX_LIMITED_RANGEHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001185 PragmaSTDC_CX_LIMITED_RANGEHandler()
1186 : PragmaHandler("CX_LIMITED_RANGE") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +00001187 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1188 Token &Tok) {
Peter Collingbourne9d3f5f72011-02-14 01:42:24 +00001189 tok::OnOffSwitch OOS;
1190 PP.LexOnOffSwitch(OOS);
Chris Lattner062f2322009-04-19 21:20:35 +00001191 }
1192};
Mike Stump1eb44332009-09-09 15:08:12 +00001193
Chris Lattner062f2322009-04-19 21:20:35 +00001194/// PragmaSTDC_UnknownHandler - "#pragma STDC ...".
1195struct PragmaSTDC_UnknownHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001196 PragmaSTDC_UnknownHandler() {}
Douglas Gregor80c60f72010-09-09 22:45:38 +00001197 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1198 Token &UnknownTok) {
Chris Lattner6c5cf4a2009-04-19 21:50:08 +00001199 // C99 6.10.6p2, unknown forms are not allowed.
Chris Lattnerf545be52009-04-19 21:25:37 +00001200 PP.Diag(UnknownTok, diag::ext_stdc_pragma_ignored);
Chris Lattner062f2322009-04-19 21:20:35 +00001201 }
1202};
Mike Stump1eb44332009-09-09 15:08:12 +00001203
John McCall8dfac0b2011-09-30 05:12:12 +00001204/// PragmaARCCFCodeAuditedHandler -
1205/// #pragma clang arc_cf_code_audited begin/end
1206struct PragmaARCCFCodeAuditedHandler : public PragmaHandler {
1207 PragmaARCCFCodeAuditedHandler() : PragmaHandler("arc_cf_code_audited") {}
1208 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1209 Token &NameTok) {
1210 SourceLocation Loc = NameTok.getLocation();
1211 bool IsBegin;
1212
1213 Token Tok;
1214
1215 // Lex the 'begin' or 'end'.
1216 PP.LexUnexpandedToken(Tok);
1217 const IdentifierInfo *BeginEnd = Tok.getIdentifierInfo();
1218 if (BeginEnd && BeginEnd->isStr("begin")) {
1219 IsBegin = true;
1220 } else if (BeginEnd && BeginEnd->isStr("end")) {
1221 IsBegin = false;
1222 } else {
1223 PP.Diag(Tok.getLocation(), diag::err_pp_arc_cf_code_audited_syntax);
1224 return;
1225 }
1226
1227 // Verify that this is followed by EOD.
1228 PP.LexUnexpandedToken(Tok);
1229 if (Tok.isNot(tok::eod))
1230 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
1231
1232 // The start location of the active audit.
1233 SourceLocation BeginLoc = PP.getPragmaARCCFCodeAuditedLoc();
1234
1235 // The start location we want after processing this.
1236 SourceLocation NewLoc;
1237
1238 if (IsBegin) {
1239 // Complain about attempts to re-enter an audit.
1240 if (BeginLoc.isValid()) {
1241 PP.Diag(Loc, diag::err_pp_double_begin_of_arc_cf_code_audited);
1242 PP.Diag(BeginLoc, diag::note_pragma_entered_here);
1243 }
1244 NewLoc = Loc;
1245 } else {
1246 // Complain about attempts to leave an audit that doesn't exist.
1247 if (!BeginLoc.isValid()) {
1248 PP.Diag(Loc, diag::err_pp_unmatched_end_of_arc_cf_code_audited);
1249 return;
1250 }
1251 NewLoc = SourceLocation();
1252 }
1253
1254 PP.setPragmaARCCFCodeAuditedLoc(NewLoc);
1255 }
1256};
1257
Reid Spencer5f016e22007-07-11 17:01:13 +00001258} // end anonymous namespace
1259
1260
1261/// RegisterBuiltinPragmas - Install the standard preprocessor pragmas:
1262/// #pragma GCC poison/system_header/dependency and #pragma once.
1263void Preprocessor::RegisterBuiltinPragmas() {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001264 AddPragmaHandler(new PragmaOnceHandler());
1265 AddPragmaHandler(new PragmaMarkHandler());
Chris Lattnerf47724b2010-08-17 15:55:45 +00001266 AddPragmaHandler(new PragmaPushMacroHandler());
1267 AddPragmaHandler(new PragmaPopMacroHandler());
Michael J. Spencer301669b2010-09-27 06:19:02 +00001268 AddPragmaHandler(new PragmaMessageHandler());
Mike Stump1eb44332009-09-09 15:08:12 +00001269
Chris Lattnere8fa06e2009-05-12 18:21:11 +00001270 // #pragma GCC ...
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001271 AddPragmaHandler("GCC", new PragmaPoisonHandler());
1272 AddPragmaHandler("GCC", new PragmaSystemHeaderHandler());
1273 AddPragmaHandler("GCC", new PragmaDependencyHandler());
Douglas Gregorc09ce122011-06-22 19:41:48 +00001274 AddPragmaHandler("GCC", new PragmaDiagnosticHandler("GCC"));
Chris Lattnere8fa06e2009-05-12 18:21:11 +00001275 // #pragma clang ...
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001276 AddPragmaHandler("clang", new PragmaPoisonHandler());
1277 AddPragmaHandler("clang", new PragmaSystemHeaderHandler());
Daniel Dunbarabf7b722010-07-28 15:40:33 +00001278 AddPragmaHandler("clang", new PragmaDebugHandler());
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001279 AddPragmaHandler("clang", new PragmaDependencyHandler());
Douglas Gregorc09ce122011-06-22 19:41:48 +00001280 AddPragmaHandler("clang", new PragmaDiagnosticHandler("clang"));
John McCall8dfac0b2011-09-30 05:12:12 +00001281 AddPragmaHandler("clang", new PragmaARCCFCodeAuditedHandler());
Chris Lattnere8fa06e2009-05-12 18:21:11 +00001282
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001283 AddPragmaHandler("STDC", new PragmaSTDC_FENV_ACCESSHandler());
1284 AddPragmaHandler("STDC", new PragmaSTDC_CX_LIMITED_RANGEHandler());
Chris Lattner062f2322009-04-19 21:20:35 +00001285 AddPragmaHandler("STDC", new PragmaSTDC_UnknownHandler());
Mike Stump1eb44332009-09-09 15:08:12 +00001286
Chris Lattner636c5ef2009-01-16 08:21:25 +00001287 // MS extensions.
David Blaikie4e4d0842012-03-11 07:00:24 +00001288 if (LangOpts.MicrosoftExt) {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001289 AddPragmaHandler(new PragmaCommentHandler());
Aaron Ballman4c55c542012-03-02 22:51:54 +00001290 AddPragmaHandler(new PragmaIncludeAliasHandler());
Chris Lattnerabfe0942010-06-26 17:11:39 +00001291 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001292}