blob: d678ce5d7fbeaa3f67982c3786d0a32af37c3ffd [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) {
Jordan Rose6fe6a492012-06-08 18:06:21 +0000106 if (!PragmasEnabled)
107 return;
108
Reid Spencer5f016e22007-07-11 17:01:13 +0000109 ++NumPragma;
Mike Stump1eb44332009-09-09 15:08:12 +0000110
Reid Spencer5f016e22007-07-11 17:01:13 +0000111 // Invoke the first level of pragma handlers which reads the namespace id.
Chris Lattnerd2177732007-07-20 16:59:19 +0000112 Token Tok;
Douglas Gregor80c60f72010-09-09 22:45:38 +0000113 PragmaHandlers->HandlePragma(*this, PragmaIntroducerKind(Introducer), Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000114
Reid Spencer5f016e22007-07-11 17:01:13 +0000115 // If the pragma handler didn't read the rest of the line, consume it now.
Peter Collingbourneb2eb53d2011-02-22 13:49:00 +0000116 if ((CurTokenLexer && CurTokenLexer->isParsingPreprocessorDirective())
117 || (CurPPLexer && CurPPLexer->ParsingPreprocessorDirective))
Reid Spencer5f016e22007-07-11 17:01:13 +0000118 DiscardUntilEndOfDirective();
119}
120
Argyrios Kyrtzidis14e64552012-04-03 16:47:40 +0000121namespace {
122/// \brief Helper class for \see Preprocessor::Handle_Pragma.
123class LexingFor_PragmaRAII {
124 Preprocessor &PP;
125 bool InMacroArgPreExpansion;
126 bool Failed;
127 Token &OutTok;
128 Token PragmaTok;
129
130public:
131 LexingFor_PragmaRAII(Preprocessor &PP, bool InMacroArgPreExpansion,
132 Token &Tok)
133 : PP(PP), InMacroArgPreExpansion(InMacroArgPreExpansion),
134 Failed(false), OutTok(Tok) {
135 if (InMacroArgPreExpansion) {
136 PragmaTok = OutTok;
137 PP.EnableBacktrackAtThisPos();
138 }
139 }
140
141 ~LexingFor_PragmaRAII() {
142 if (InMacroArgPreExpansion) {
143 if (Failed) {
144 PP.CommitBacktrackedTokens();
145 } else {
146 PP.Backtrack();
147 OutTok = PragmaTok;
148 }
149 }
150 }
151
152 void failed() {
153 Failed = true;
154 }
155};
156}
157
Reid Spencer5f016e22007-07-11 17:01:13 +0000158/// Handle_Pragma - Read a _Pragma directive, slice it up, process it, then
159/// return the first token after the directive. The _Pragma token has just
160/// been read into 'Tok'.
Chris Lattnerd2177732007-07-20 16:59:19 +0000161void Preprocessor::Handle_Pragma(Token &Tok) {
Argyrios Kyrtzidis14e64552012-04-03 16:47:40 +0000162
163 // This works differently if we are pre-expanding a macro argument.
164 // In that case we don't actually "activate" the pragma now, we only lex it
165 // until we are sure it is lexically correct and then we backtrack so that
166 // we activate the pragma whenever we encounter the tokens again in the token
167 // stream. This ensures that we will activate it in the correct location
168 // or that we will ignore it if it never enters the token stream, e.g:
169 //
170 // #define EMPTY(x)
171 // #define INACTIVE(x) EMPTY(x)
172 // INACTIVE(_Pragma("clang diagnostic ignored \"-Wconversion\""))
173
174 LexingFor_PragmaRAII _PragmaLexing(*this, InMacroArgPreExpansion, Tok);
175
Reid Spencer5f016e22007-07-11 17:01:13 +0000176 // Remember the pragma token location.
177 SourceLocation PragmaLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000178
Reid Spencer5f016e22007-07-11 17:01:13 +0000179 // Read the '('.
180 Lex(Tok);
Chris Lattner3692b092008-11-18 07:59:24 +0000181 if (Tok.isNot(tok::l_paren)) {
182 Diag(PragmaLoc, diag::err__Pragma_malformed);
Argyrios Kyrtzidis14e64552012-04-03 16:47:40 +0000183 return _PragmaLexing.failed();
Chris Lattner3692b092008-11-18 07:59:24 +0000184 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000185
186 // Read the '"..."'.
187 Lex(Tok);
Chris Lattner3692b092008-11-18 07:59:24 +0000188 if (Tok.isNot(tok::string_literal) && Tok.isNot(tok::wide_string_literal)) {
189 Diag(PragmaLoc, diag::err__Pragma_malformed);
Richard Smith99831e42012-03-06 03:21:47 +0000190 // Skip this token, and the ')', if present.
191 if (Tok.isNot(tok::r_paren))
192 Lex(Tok);
193 if (Tok.is(tok::r_paren))
194 Lex(Tok);
Argyrios Kyrtzidis14e64552012-04-03 16:47:40 +0000195 return _PragmaLexing.failed();
Richard Smith99831e42012-03-06 03:21:47 +0000196 }
197
198 if (Tok.hasUDSuffix()) {
199 Diag(Tok, diag::err_invalid_string_udl);
200 // Skip this token, and the ')', if present.
201 Lex(Tok);
202 if (Tok.is(tok::r_paren))
203 Lex(Tok);
Argyrios Kyrtzidis14e64552012-04-03 16:47:40 +0000204 return _PragmaLexing.failed();
Chris Lattner3692b092008-11-18 07:59:24 +0000205 }
Mike Stump1eb44332009-09-09 15:08:12 +0000206
Reid Spencer5f016e22007-07-11 17:01:13 +0000207 // Remember the string.
Argyrios Kyrtzidis14e64552012-04-03 16:47:40 +0000208 Token StrTok = Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000209
210 // Read the ')'.
211 Lex(Tok);
Chris Lattner3692b092008-11-18 07:59:24 +0000212 if (Tok.isNot(tok::r_paren)) {
213 Diag(PragmaLoc, diag::err__Pragma_malformed);
Argyrios Kyrtzidis14e64552012-04-03 16:47:40 +0000214 return _PragmaLexing.failed();
Chris Lattner3692b092008-11-18 07:59:24 +0000215 }
Mike Stump1eb44332009-09-09 15:08:12 +0000216
Argyrios Kyrtzidis14e64552012-04-03 16:47:40 +0000217 if (InMacroArgPreExpansion)
218 return;
219
Chris Lattnere7fb4842009-02-15 20:52:18 +0000220 SourceLocation RParenLoc = Tok.getLocation();
Argyrios Kyrtzidis14e64552012-04-03 16:47:40 +0000221 std::string StrVal = getSpelling(StrTok);
Mike Stump1eb44332009-09-09 15:08:12 +0000222
Chris Lattnera9d91452009-01-16 18:59:23 +0000223 // The _Pragma is lexically sound. Destringize according to C99 6.10.9.1:
224 // "The string literal is destringized by deleting the L prefix, if present,
225 // deleting the leading and trailing double-quotes, replacing each escape
226 // sequence \" by a double-quote, and replacing each escape sequence \\ by a
227 // single backslash."
Reid Spencer5f016e22007-07-11 17:01:13 +0000228 if (StrVal[0] == 'L') // Remove L prefix.
229 StrVal.erase(StrVal.begin());
230 assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
231 "Invalid string token!");
Mike Stump1eb44332009-09-09 15:08:12 +0000232
Reid Spencer5f016e22007-07-11 17:01:13 +0000233 // Remove the front quote, replacing it with a space, so that the pragma
234 // contents appear to have a space before them.
235 StrVal[0] = ' ';
Mike Stump1eb44332009-09-09 15:08:12 +0000236
Chris Lattner1fa49532009-03-08 08:08:45 +0000237 // Replace the terminating quote with a \n.
Reid Spencer5f016e22007-07-11 17:01:13 +0000238 StrVal[StrVal.size()-1] = '\n';
Mike Stump1eb44332009-09-09 15:08:12 +0000239
Reid Spencer5f016e22007-07-11 17:01:13 +0000240 // Remove escaped quotes and escapes.
241 for (unsigned i = 0, e = StrVal.size(); i != e-1; ++i) {
242 if (StrVal[i] == '\\' &&
243 (StrVal[i+1] == '\\' || StrVal[i+1] == '"')) {
244 // \\ -> '\' and \" -> '"'.
245 StrVal.erase(StrVal.begin()+i);
246 --e;
247 }
248 }
John McCall1ef8a2e2010-08-28 22:34:47 +0000249
Peter Collingbournea5ef5842011-02-22 13:49:06 +0000250 // Plop the string (including the newline and trailing null) into a buffer
251 // where we can lex it.
252 Token TmpTok;
253 TmpTok.startToken();
254 CreateString(&StrVal[0], StrVal.size(), TmpTok);
255 SourceLocation TokLoc = TmpTok.getLocation();
256
257 // Make and enter a lexer object so that we lex and expand the tokens just
258 // like any others.
259 Lexer *TL = Lexer::Create_PragmaLexer(TokLoc, PragmaLoc, RParenLoc,
260 StrVal.size(), *this);
261
262 EnterSourceFileWithLexer(TL, 0);
263
264 // With everything set up, lex this as a #pragma directive.
265 HandlePragmaDirective(PIK__Pragma);
John McCall1ef8a2e2010-08-28 22:34:47 +0000266
267 // Finally, return whatever came after the pragma directive.
268 return Lex(Tok);
269}
270
271/// HandleMicrosoft__pragma - Like Handle_Pragma except the pragma text
272/// is not enclosed within a string literal.
273void Preprocessor::HandleMicrosoft__pragma(Token &Tok) {
274 // Remember the pragma token location.
275 SourceLocation PragmaLoc = Tok.getLocation();
276
277 // Read the '('.
278 Lex(Tok);
279 if (Tok.isNot(tok::l_paren)) {
280 Diag(PragmaLoc, diag::err__Pragma_malformed);
281 return;
282 }
283
Peter Collingbournea5ef5842011-02-22 13:49:06 +0000284 // Get the tokens enclosed within the __pragma(), as well as the final ')'.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000285 SmallVector<Token, 32> PragmaToks;
John McCall1ef8a2e2010-08-28 22:34:47 +0000286 int NumParens = 0;
287 Lex(Tok);
288 while (Tok.isNot(tok::eof)) {
Peter Collingbournea5ef5842011-02-22 13:49:06 +0000289 PragmaToks.push_back(Tok);
John McCall1ef8a2e2010-08-28 22:34:47 +0000290 if (Tok.is(tok::l_paren))
291 NumParens++;
292 else if (Tok.is(tok::r_paren) && NumParens-- == 0)
293 break;
John McCall1ef8a2e2010-08-28 22:34:47 +0000294 Lex(Tok);
295 }
296
John McCall3da92a92010-08-29 01:09:54 +0000297 if (Tok.is(tok::eof)) {
298 Diag(PragmaLoc, diag::err_unterminated___pragma);
299 return;
300 }
301
Peter Collingbournea5ef5842011-02-22 13:49:06 +0000302 PragmaToks.front().setFlag(Token::LeadingSpace);
John McCall1ef8a2e2010-08-28 22:34:47 +0000303
Peter Collingbourne84021552011-02-28 02:37:51 +0000304 // Replace the ')' with an EOD to mark the end of the pragma.
305 PragmaToks.back().setKind(tok::eod);
Peter Collingbournea5ef5842011-02-22 13:49:06 +0000306
307 Token *TokArray = new Token[PragmaToks.size()];
308 std::copy(PragmaToks.begin(), PragmaToks.end(), TokArray);
309
310 // Push the tokens onto the stack.
311 EnterTokenStream(TokArray, PragmaToks.size(), true, true);
312
313 // With everything set up, lex this as a #pragma directive.
314 HandlePragmaDirective(PIK___pragma);
John McCall1ef8a2e2010-08-28 22:34:47 +0000315
316 // Finally, return whatever came after the pragma directive.
317 return Lex(Tok);
318}
319
Reid Spencer5f016e22007-07-11 17:01:13 +0000320/// HandlePragmaOnce - Handle #pragma once. OnceTok is the 'once'.
321///
Chris Lattnerd2177732007-07-20 16:59:19 +0000322void Preprocessor::HandlePragmaOnce(Token &OnceTok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000323 if (isInPrimaryFile()) {
324 Diag(OnceTok, diag::pp_pragma_once_in_main_file);
325 return;
326 }
Mike Stump1eb44332009-09-09 15:08:12 +0000327
Reid Spencer5f016e22007-07-11 17:01:13 +0000328 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
Reid Spencer5f016e22007-07-11 17:01:13 +0000329 // Mark the file as a once-only file now.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000330 HeaderInfo.MarkFileIncludeOnce(getCurrentFileLexer()->getFileEntry());
Reid Spencer5f016e22007-07-11 17:01:13 +0000331}
332
Chris Lattner22434492007-12-19 19:38:36 +0000333void Preprocessor::HandlePragmaMark() {
Ted Kremenek17ff58a2008-11-19 22:21:33 +0000334 assert(CurPPLexer && "No current lexer?");
Chris Lattner6896a372009-06-15 05:02:34 +0000335 if (CurLexer)
336 CurLexer->ReadToEndOfLine();
337 else
338 CurPTHLexer->DiscardToEndOfLine();
Chris Lattner22434492007-12-19 19:38:36 +0000339}
340
341
Reid Spencer5f016e22007-07-11 17:01:13 +0000342/// HandlePragmaPoison - Handle #pragma GCC poison. PoisonTok is the 'poison'.
343///
Chris Lattnerd2177732007-07-20 16:59:19 +0000344void Preprocessor::HandlePragmaPoison(Token &PoisonTok) {
345 Token Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000346
347 while (1) {
348 // Read the next token to poison. While doing this, pretend that we are
349 // skipping while reading the identifier to poison.
350 // This avoids errors on code like:
351 // #pragma GCC poison X
352 // #pragma GCC poison X
Ted Kremenek68a91d52008-11-18 01:12:54 +0000353 if (CurPPLexer) CurPPLexer->LexingRawMode = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000354 LexUnexpandedToken(Tok);
Ted Kremenek68a91d52008-11-18 01:12:54 +0000355 if (CurPPLexer) CurPPLexer->LexingRawMode = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000356
Reid Spencer5f016e22007-07-11 17:01:13 +0000357 // If we reached the end of line, we're done.
Peter Collingbourne84021552011-02-28 02:37:51 +0000358 if (Tok.is(tok::eod)) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000359
Reid Spencer5f016e22007-07-11 17:01:13 +0000360 // Can only poison identifiers.
Abramo Bagnarac4bf2b92010-12-22 08:23:18 +0000361 if (Tok.isNot(tok::raw_identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000362 Diag(Tok, diag::err_pp_invalid_poison);
363 return;
364 }
Mike Stump1eb44332009-09-09 15:08:12 +0000365
Reid Spencer5f016e22007-07-11 17:01:13 +0000366 // Look up the identifier info for the token. We disabled identifier lookup
367 // by saying we're skipping contents, so we need to do this manually.
368 IdentifierInfo *II = LookUpIdentifierInfo(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000369
Reid Spencer5f016e22007-07-11 17:01:13 +0000370 // Already poisoned.
371 if (II->isPoisoned()) continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000372
Reid Spencer5f016e22007-07-11 17:01:13 +0000373 // If this is a macro identifier, emit a warning.
Chris Lattner0edde552007-10-07 08:04:56 +0000374 if (II->hasMacroDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000375 Diag(Tok, diag::pp_poisoning_existing_macro);
Mike Stump1eb44332009-09-09 15:08:12 +0000376
Reid Spencer5f016e22007-07-11 17:01:13 +0000377 // Finally, poison it!
378 II->setIsPoisoned();
Douglas Gregoreee242f2011-10-27 09:33:13 +0000379 if (II->isFromAST())
380 II->setChangedSinceDeserialization();
Reid Spencer5f016e22007-07-11 17:01:13 +0000381 }
382}
383
384/// HandlePragmaSystemHeader - Implement #pragma GCC system_header. We know
385/// that the whole directive has been parsed.
Chris Lattnerd2177732007-07-20 16:59:19 +0000386void Preprocessor::HandlePragmaSystemHeader(Token &SysHeaderTok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000387 if (isInPrimaryFile()) {
388 Diag(SysHeaderTok, diag::pp_pragma_sysheader_in_main_file);
389 return;
390 }
Mike Stump1eb44332009-09-09 15:08:12 +0000391
Reid Spencer5f016e22007-07-11 17:01:13 +0000392 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
Ted Kremenek35c10c22008-11-20 01:45:11 +0000393 PreprocessorLexer *TheLexer = getCurrentFileLexer();
Mike Stump1eb44332009-09-09 15:08:12 +0000394
Reid Spencer5f016e22007-07-11 17:01:13 +0000395 // Mark the file as a system header.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000396 HeaderInfo.MarkFileSystemHeader(TheLexer->getFileEntry());
Mike Stump1eb44332009-09-09 15:08:12 +0000397
398
Chris Lattner6896a372009-06-15 05:02:34 +0000399 PresumedLoc PLoc = SourceMgr.getPresumedLoc(SysHeaderTok.getLocation());
Douglas Gregorcb7b1e12010-11-12 07:15:47 +0000400 if (PLoc.isInvalid())
401 return;
402
Jay Foad65aa6882011-06-21 15:13:30 +0000403 unsigned FilenameID = SourceMgr.getLineTableFilenameID(PLoc.getFilename());
Mike Stump1eb44332009-09-09 15:08:12 +0000404
Chris Lattner784c2572011-05-22 22:10:16 +0000405 // Notify the client, if desired, that we are in a new source file.
406 if (Callbacks)
407 Callbacks->FileChanged(SysHeaderTok.getLocation(),
408 PPCallbacks::SystemHeaderPragma, SrcMgr::C_System);
409
Chris Lattner6896a372009-06-15 05:02:34 +0000410 // Emit a line marker. This will change any source locations from this point
411 // forward to realize they are in a system header.
412 // Create a line note with this information.
413 SourceMgr.AddLineNote(SysHeaderTok.getLocation(), PLoc.getLine(), FilenameID,
414 false, false, true, false);
Reid Spencer5f016e22007-07-11 17:01:13 +0000415}
416
417/// HandlePragmaDependency - Handle #pragma GCC dependency "foo" blah.
418///
Chris Lattnerd2177732007-07-20 16:59:19 +0000419void Preprocessor::HandlePragmaDependency(Token &DependencyTok) {
420 Token FilenameTok;
Ted Kremenek68a91d52008-11-18 01:12:54 +0000421 CurPPLexer->LexIncludeFilename(FilenameTok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000422
Peter Collingbourne84021552011-02-28 02:37:51 +0000423 // If the token kind is EOD, the error has already been diagnosed.
424 if (FilenameTok.is(tok::eod))
Reid Spencer5f016e22007-07-11 17:01:13 +0000425 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000426
Reid Spencer5f016e22007-07-11 17:01:13 +0000427 // Reserve a buffer to get the spelling.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000428 SmallString<128> FilenameBuffer;
Douglas Gregor453091c2010-03-16 22:30:13 +0000429 bool Invalid = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000430 StringRef Filename = getSpelling(FilenameTok, FilenameBuffer, &Invalid);
Douglas Gregor453091c2010-03-16 22:30:13 +0000431 if (Invalid)
432 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000433
Chris Lattnera1394812010-01-10 01:35:12 +0000434 bool isAngled =
435 GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
Reid Spencer5f016e22007-07-11 17:01:13 +0000436 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
437 // error.
Chris Lattnera1394812010-01-10 01:35:12 +0000438 if (Filename.empty())
Reid Spencer5f016e22007-07-11 17:01:13 +0000439 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000440
Reid Spencer5f016e22007-07-11 17:01:13 +0000441 // Search include directories for this file.
442 const DirectoryLookup *CurDir;
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000443 const FileEntry *File = LookupFile(Filename, isAngled, 0, CurDir, NULL, NULL,
444 NULL);
Chris Lattner56b05c82008-11-18 08:02:48 +0000445 if (File == 0) {
Eli Friedmanf84139a2011-08-30 23:07:51 +0000446 if (!SuppressIncludeNotFoundError)
447 Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
Chris Lattner56b05c82008-11-18 08:02:48 +0000448 return;
449 }
Mike Stump1eb44332009-09-09 15:08:12 +0000450
Chris Lattner2b2453a2009-01-17 06:22:33 +0000451 const FileEntry *CurFile = getCurrentFileLexer()->getFileEntry();
Reid Spencer5f016e22007-07-11 17:01:13 +0000452
453 // If this file is older than the file it depends on, emit a diagnostic.
454 if (CurFile && CurFile->getModificationTime() < File->getModificationTime()) {
455 // Lex tokens at the end of the message and include them in the message.
456 std::string Message;
457 Lex(DependencyTok);
Peter Collingbourne84021552011-02-28 02:37:51 +0000458 while (DependencyTok.isNot(tok::eod)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000459 Message += getSpelling(DependencyTok) + " ";
460 Lex(DependencyTok);
461 }
Mike Stump1eb44332009-09-09 15:08:12 +0000462
Chris Lattner96de2592010-09-05 23:16:09 +0000463 // Remove the trailing ' ' if present.
464 if (!Message.empty())
465 Message.erase(Message.end()-1);
Chris Lattner56b05c82008-11-18 08:02:48 +0000466 Diag(FilenameTok, diag::pp_out_of_date_dependency) << Message;
Reid Spencer5f016e22007-07-11 17:01:13 +0000467 }
468}
469
Chris Lattner636c5ef2009-01-16 08:21:25 +0000470/// HandlePragmaComment - Handle the microsoft #pragma comment extension. The
471/// syntax is:
472/// #pragma comment(linker, "foo")
473/// 'linker' is one of five identifiers: compiler, exestr, lib, linker, user.
474/// "foo" is a string, which is fully macro expanded, and permits string
Gabor Greifd7ee3492009-03-17 11:39:38 +0000475/// concatenation, embedded escape characters etc. See MSDN for more details.
Chris Lattner636c5ef2009-01-16 08:21:25 +0000476void Preprocessor::HandlePragmaComment(Token &Tok) {
477 SourceLocation CommentLoc = Tok.getLocation();
478 Lex(Tok);
479 if (Tok.isNot(tok::l_paren)) {
480 Diag(CommentLoc, diag::err_pragma_comment_malformed);
481 return;
482 }
Mike Stump1eb44332009-09-09 15:08:12 +0000483
Chris Lattner636c5ef2009-01-16 08:21:25 +0000484 // Read the identifier.
485 Lex(Tok);
486 if (Tok.isNot(tok::identifier)) {
487 Diag(CommentLoc, diag::err_pragma_comment_malformed);
488 return;
489 }
Mike Stump1eb44332009-09-09 15:08:12 +0000490
Chris Lattner636c5ef2009-01-16 08:21:25 +0000491 // Verify that this is one of the 5 whitelisted options.
492 // FIXME: warn that 'exestr' is deprecated.
493 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000494 if (!II->isStr("compiler") && !II->isStr("exestr") && !II->isStr("lib") &&
Chris Lattner636c5ef2009-01-16 08:21:25 +0000495 !II->isStr("linker") && !II->isStr("user")) {
496 Diag(Tok.getLocation(), diag::err_pragma_comment_unknown_kind);
497 return;
498 }
Mike Stump1eb44332009-09-09 15:08:12 +0000499
Chris Lattnera9d91452009-01-16 18:59:23 +0000500 // Read the optional string if present.
Chris Lattner636c5ef2009-01-16 08:21:25 +0000501 Lex(Tok);
Chris Lattnera9d91452009-01-16 18:59:23 +0000502 std::string ArgumentString;
Chris Lattner636c5ef2009-01-16 08:21:25 +0000503 if (Tok.is(tok::comma)) {
Chris Lattnera9d91452009-01-16 18:59:23 +0000504 Lex(Tok); // eat the comma.
Chris Lattner636c5ef2009-01-16 08:21:25 +0000505
506 // We need at least one string.
Chris Lattneredaf8772009-04-19 23:16:58 +0000507 if (Tok.isNot(tok::string_literal)) {
Chris Lattner636c5ef2009-01-16 08:21:25 +0000508 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
509 return;
510 }
511
512 // String concatenation allows multiple strings, which can even come from
513 // macro expansion.
514 // "foo " "bar" "Baz"
Chris Lattner5f9e2722011-07-23 10:55:15 +0000515 SmallVector<Token, 4> StrToks;
Chris Lattneredaf8772009-04-19 23:16:58 +0000516 while (Tok.is(tok::string_literal)) {
Richard Smith99831e42012-03-06 03:21:47 +0000517 if (Tok.hasUDSuffix())
518 Diag(Tok, diag::err_invalid_string_udl);
Chris Lattnera9d91452009-01-16 18:59:23 +0000519 StrToks.push_back(Tok);
Chris Lattner636c5ef2009-01-16 08:21:25 +0000520 Lex(Tok);
Chris Lattnera9d91452009-01-16 18:59:23 +0000521 }
522
523 // Concatenate and parse the strings.
524 StringLiteralParser Literal(&StrToks[0], StrToks.size(), *this);
Douglas Gregor5cee1192011-07-27 05:40:30 +0000525 assert(Literal.isAscii() && "Didn't allow wide strings in");
Chris Lattnera9d91452009-01-16 18:59:23 +0000526 if (Literal.hadError)
527 return;
528 if (Literal.Pascal) {
529 Diag(StrToks[0].getLocation(), diag::err_pragma_comment_malformed);
530 return;
531 }
532
Jay Foad65aa6882011-06-21 15:13:30 +0000533 ArgumentString = Literal.GetString();
Chris Lattner636c5ef2009-01-16 08:21:25 +0000534 }
Mike Stump1eb44332009-09-09 15:08:12 +0000535
Chris Lattnera9d91452009-01-16 18:59:23 +0000536 // FIXME: If the kind is "compiler" warn if the string is present (it is
537 // ignored).
538 // FIXME: 'lib' requires a comment string.
539 // FIXME: 'linker' requires a comment string, and has a specific list of
540 // things that are allowable.
Mike Stump1eb44332009-09-09 15:08:12 +0000541
Chris Lattner636c5ef2009-01-16 08:21:25 +0000542 if (Tok.isNot(tok::r_paren)) {
543 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
544 return;
545 }
Chris Lattnera9d91452009-01-16 18:59:23 +0000546 Lex(Tok); // eat the r_paren.
Chris Lattner636c5ef2009-01-16 08:21:25 +0000547
Peter Collingbourne84021552011-02-28 02:37:51 +0000548 if (Tok.isNot(tok::eod)) {
Chris Lattner636c5ef2009-01-16 08:21:25 +0000549 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
550 return;
551 }
Mike Stump1eb44332009-09-09 15:08:12 +0000552
Chris Lattnera9d91452009-01-16 18:59:23 +0000553 // If the pragma is lexically sound, notify any interested PPCallbacks.
Chris Lattner172e3362009-01-16 19:01:46 +0000554 if (Callbacks)
555 Callbacks->PragmaComment(CommentLoc, II, ArgumentString);
Chris Lattner636c5ef2009-01-16 08:21:25 +0000556}
557
Michael J. Spencer301669b2010-09-27 06:19:02 +0000558/// HandlePragmaMessage - Handle the microsoft and gcc #pragma message
559/// extension. The syntax is:
560/// #pragma message(string)
561/// OR, in GCC mode:
562/// #pragma message string
563/// string is a string, which is fully macro expanded, and permits string
564/// concatenation, embedded escape characters, etc... See MSDN for more details.
Chris Lattnerabfe0942010-06-26 17:11:39 +0000565void Preprocessor::HandlePragmaMessage(Token &Tok) {
566 SourceLocation MessageLoc = Tok.getLocation();
567 Lex(Tok);
Michael J. Spencer301669b2010-09-27 06:19:02 +0000568 bool ExpectClosingParen = false;
Michael J. Spencerd83fc542010-09-27 06:34:47 +0000569 switch (Tok.getKind()) {
Michael J. Spencer301669b2010-09-27 06:19:02 +0000570 case tok::l_paren:
571 // We have a MSVC style pragma message.
572 ExpectClosingParen = true;
573 // Read the string.
574 Lex(Tok);
575 break;
576 case tok::string_literal:
577 // We have a GCC style pragma message, and we just read the string.
578 break;
579 default:
Chris Lattnerabfe0942010-06-26 17:11:39 +0000580 Diag(MessageLoc, diag::err_pragma_message_malformed);
581 return;
582 }
Chris Lattner636c5ef2009-01-16 08:21:25 +0000583
Chris Lattnerabfe0942010-06-26 17:11:39 +0000584 // We need at least one string.
585 if (Tok.isNot(tok::string_literal)) {
586 Diag(Tok.getLocation(), diag::err_pragma_message_malformed);
587 return;
588 }
589
590 // String concatenation allows multiple strings, which can even come from
591 // macro expansion.
592 // "foo " "bar" "Baz"
Chris Lattner5f9e2722011-07-23 10:55:15 +0000593 SmallVector<Token, 4> StrToks;
Chris Lattnerabfe0942010-06-26 17:11:39 +0000594 while (Tok.is(tok::string_literal)) {
Richard Smith99831e42012-03-06 03:21:47 +0000595 if (Tok.hasUDSuffix())
596 Diag(Tok, diag::err_invalid_string_udl);
Chris Lattnerabfe0942010-06-26 17:11:39 +0000597 StrToks.push_back(Tok);
598 Lex(Tok);
599 }
600
601 // Concatenate and parse the strings.
602 StringLiteralParser Literal(&StrToks[0], StrToks.size(), *this);
Douglas Gregor5cee1192011-07-27 05:40:30 +0000603 assert(Literal.isAscii() && "Didn't allow wide strings in");
Chris Lattnerabfe0942010-06-26 17:11:39 +0000604 if (Literal.hadError)
605 return;
606 if (Literal.Pascal) {
607 Diag(StrToks[0].getLocation(), diag::err_pragma_message_malformed);
608 return;
609 }
610
Chris Lattner5f9e2722011-07-23 10:55:15 +0000611 StringRef MessageString(Literal.GetString());
Chris Lattnerabfe0942010-06-26 17:11:39 +0000612
Michael J. Spencer301669b2010-09-27 06:19:02 +0000613 if (ExpectClosingParen) {
614 if (Tok.isNot(tok::r_paren)) {
615 Diag(Tok.getLocation(), diag::err_pragma_message_malformed);
616 return;
617 }
618 Lex(Tok); // eat the r_paren.
Chris Lattnerabfe0942010-06-26 17:11:39 +0000619 }
Chris Lattnerabfe0942010-06-26 17:11:39 +0000620
Peter Collingbourne84021552011-02-28 02:37:51 +0000621 if (Tok.isNot(tok::eod)) {
Chris Lattnerabfe0942010-06-26 17:11:39 +0000622 Diag(Tok.getLocation(), diag::err_pragma_message_malformed);
623 return;
624 }
625
626 // Output the message.
627 Diag(MessageLoc, diag::warn_pragma_message) << MessageString;
628
629 // If the pragma is lexically sound, notify any interested PPCallbacks.
630 if (Callbacks)
631 Callbacks->PragmaMessage(MessageLoc, MessageString);
632}
Chris Lattner636c5ef2009-01-16 08:21:25 +0000633
Chris Lattnerf47724b2010-08-17 15:55:45 +0000634/// ParsePragmaPushOrPopMacro - Handle parsing of pragma push_macro/pop_macro.
635/// Return the IdentifierInfo* associated with the macro to push or pop.
636IdentifierInfo *Preprocessor::ParsePragmaPushOrPopMacro(Token &Tok) {
637 // Remember the pragma token location.
638 Token PragmaTok = Tok;
639
640 // Read the '('.
641 Lex(Tok);
642 if (Tok.isNot(tok::l_paren)) {
643 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
644 << getSpelling(PragmaTok);
645 return 0;
646 }
647
648 // Read the macro name string.
649 Lex(Tok);
650 if (Tok.isNot(tok::string_literal)) {
651 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
652 << getSpelling(PragmaTok);
653 return 0;
654 }
655
Richard Smith99831e42012-03-06 03:21:47 +0000656 if (Tok.hasUDSuffix()) {
657 Diag(Tok, diag::err_invalid_string_udl);
658 return 0;
659 }
660
Chris Lattnerf47724b2010-08-17 15:55:45 +0000661 // Remember the macro string.
662 std::string StrVal = getSpelling(Tok);
663
664 // Read the ')'.
665 Lex(Tok);
666 if (Tok.isNot(tok::r_paren)) {
667 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
668 << getSpelling(PragmaTok);
669 return 0;
670 }
671
672 assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
673 "Invalid string token!");
674
675 // Create a Token from the string.
676 Token MacroTok;
677 MacroTok.startToken();
Abramo Bagnarac4bf2b92010-12-22 08:23:18 +0000678 MacroTok.setKind(tok::raw_identifier);
Chris Lattnerf47724b2010-08-17 15:55:45 +0000679 CreateString(&StrVal[1], StrVal.size() - 2, MacroTok);
680
681 // Get the IdentifierInfo of MacroToPushTok.
682 return LookUpIdentifierInfo(MacroTok);
683}
684
685/// HandlePragmaPushMacro - Handle #pragma push_macro.
686/// The syntax is:
687/// #pragma push_macro("macro")
688void Preprocessor::HandlePragmaPushMacro(Token &PushMacroTok) {
689 // Parse the pragma directive and get the macro IdentifierInfo*.
690 IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PushMacroTok);
691 if (!IdentInfo) return;
692
693 // Get the MacroInfo associated with IdentInfo.
694 MacroInfo *MI = getMacroInfo(IdentInfo);
695
696 MacroInfo *MacroCopyToPush = 0;
697 if (MI) {
698 // Make a clone of MI.
699 MacroCopyToPush = CloneMacroInfo(*MI);
700
701 // Allow the original MacroInfo to be redefined later.
702 MI->setIsAllowRedefinitionsWithoutWarning(true);
703 }
704
705 // Push the cloned MacroInfo so we can retrieve it later.
706 PragmaPushMacroInfo[IdentInfo].push_back(MacroCopyToPush);
707}
708
Ted Kremenekb275e3d2010-10-19 17:40:50 +0000709/// HandlePragmaPopMacro - Handle #pragma pop_macro.
Chris Lattnerf47724b2010-08-17 15:55:45 +0000710/// The syntax is:
711/// #pragma pop_macro("macro")
712void Preprocessor::HandlePragmaPopMacro(Token &PopMacroTok) {
713 SourceLocation MessageLoc = PopMacroTok.getLocation();
714
715 // Parse the pragma directive and get the macro IdentifierInfo*.
716 IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PopMacroTok);
717 if (!IdentInfo) return;
718
719 // Find the vector<MacroInfo*> associated with the macro.
720 llvm::DenseMap<IdentifierInfo*, std::vector<MacroInfo*> >::iterator iter =
721 PragmaPushMacroInfo.find(IdentInfo);
722 if (iter != PragmaPushMacroInfo.end()) {
723 // Release the MacroInfo currently associated with IdentInfo.
724 MacroInfo *CurrentMI = getMacroInfo(IdentInfo);
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000725 if (CurrentMI) {
726 if (CurrentMI->isWarnIfUnused())
727 WarnUnusedMacroLocs.erase(CurrentMI->getDefinitionLoc());
728 ReleaseMacroInfo(CurrentMI);
729 }
Chris Lattnerf47724b2010-08-17 15:55:45 +0000730
731 // Get the MacroInfo we want to reinstall.
732 MacroInfo *MacroToReInstall = iter->second.back();
733
734 // Reinstall the previously pushed macro.
735 setMacroInfo(IdentInfo, MacroToReInstall);
736
737 // Pop PragmaPushMacroInfo stack.
738 iter->second.pop_back();
739 if (iter->second.size() == 0)
740 PragmaPushMacroInfo.erase(iter);
741 } else {
742 Diag(MessageLoc, diag::warn_pragma_pop_macro_no_push)
743 << IdentInfo->getName();
744 }
745}
Reid Spencer5f016e22007-07-11 17:01:13 +0000746
Aaron Ballman4c55c542012-03-02 22:51:54 +0000747void Preprocessor::HandlePragmaIncludeAlias(Token &Tok) {
748 // We will either get a quoted filename or a bracketed filename, and we
749 // have to track which we got. The first filename is the source name,
750 // and the second name is the mapped filename. If the first is quoted,
751 // the second must be as well (cannot mix and match quotes and brackets).
Aaron Ballman4c55c542012-03-02 22:51:54 +0000752
753 // Get the open paren
754 Lex(Tok);
755 if (Tok.isNot(tok::l_paren)) {
756 Diag(Tok, diag::warn_pragma_include_alias_expected) << "(";
757 return;
758 }
759
760 // We expect either a quoted string literal, or a bracketed name
761 Token SourceFilenameTok;
762 CurPPLexer->LexIncludeFilename(SourceFilenameTok);
763 if (SourceFilenameTok.is(tok::eod)) {
764 // The diagnostic has already been handled
765 return;
766 }
767
768 StringRef SourceFileName;
769 SmallString<128> FileNameBuffer;
770 if (SourceFilenameTok.is(tok::string_literal) ||
771 SourceFilenameTok.is(tok::angle_string_literal)) {
772 SourceFileName = getSpelling(SourceFilenameTok, FileNameBuffer);
773 } else if (SourceFilenameTok.is(tok::less)) {
774 // This could be a path instead of just a name
775 FileNameBuffer.push_back('<');
776 SourceLocation End;
777 if (ConcatenateIncludeName(FileNameBuffer, End))
778 return; // Diagnostic already emitted
779 SourceFileName = FileNameBuffer.str();
780 } else {
781 Diag(Tok, diag::warn_pragma_include_alias_expected_filename);
782 return;
783 }
784 FileNameBuffer.clear();
785
786 // Now we expect a comma, followed by another include name
787 Lex(Tok);
788 if (Tok.isNot(tok::comma)) {
789 Diag(Tok, diag::warn_pragma_include_alias_expected) << ",";
790 return;
791 }
792
793 Token ReplaceFilenameTok;
794 CurPPLexer->LexIncludeFilename(ReplaceFilenameTok);
795 if (ReplaceFilenameTok.is(tok::eod)) {
796 // The diagnostic has already been handled
797 return;
798 }
799
800 StringRef ReplaceFileName;
801 if (ReplaceFilenameTok.is(tok::string_literal) ||
802 ReplaceFilenameTok.is(tok::angle_string_literal)) {
803 ReplaceFileName = getSpelling(ReplaceFilenameTok, FileNameBuffer);
804 } else if (ReplaceFilenameTok.is(tok::less)) {
805 // This could be a path instead of just a name
806 FileNameBuffer.push_back('<');
807 SourceLocation End;
808 if (ConcatenateIncludeName(FileNameBuffer, End))
809 return; // Diagnostic already emitted
810 ReplaceFileName = FileNameBuffer.str();
811 } else {
812 Diag(Tok, diag::warn_pragma_include_alias_expected_filename);
813 return;
814 }
815
816 // Finally, we expect the closing paren
817 Lex(Tok);
818 if (Tok.isNot(tok::r_paren)) {
819 Diag(Tok, diag::warn_pragma_include_alias_expected) << ")";
820 return;
821 }
822
823 // Now that we have the source and target filenames, we need to make sure
824 // they're both of the same type (angled vs non-angled)
825 StringRef OriginalSource = SourceFileName;
826
827 bool SourceIsAngled =
828 GetIncludeFilenameSpelling(SourceFilenameTok.getLocation(),
829 SourceFileName);
830 bool ReplaceIsAngled =
831 GetIncludeFilenameSpelling(ReplaceFilenameTok.getLocation(),
832 ReplaceFileName);
833 if (!SourceFileName.empty() && !ReplaceFileName.empty() &&
834 (SourceIsAngled != ReplaceIsAngled)) {
835 unsigned int DiagID;
836 if (SourceIsAngled)
837 DiagID = diag::warn_pragma_include_alias_mismatch_angle;
838 else
839 DiagID = diag::warn_pragma_include_alias_mismatch_quote;
840
841 Diag(SourceFilenameTok.getLocation(), DiagID)
842 << SourceFileName
843 << ReplaceFileName;
844
845 return;
846 }
847
848 // Now we can let the include handler know about this mapping
849 getHeaderSearchInfo().AddIncludeAlias(OriginalSource, ReplaceFileName);
850}
851
Reid Spencer5f016e22007-07-11 17:01:13 +0000852/// AddPragmaHandler - Add the specified pragma handler to the preprocessor.
853/// If 'Namespace' is non-null, then it is a token required to exist on the
854/// pragma line before the pragma string starts, e.g. "STDC" or "GCC".
Chris Lattner5f9e2722011-07-23 10:55:15 +0000855void Preprocessor::AddPragmaHandler(StringRef Namespace,
Reid Spencer5f016e22007-07-11 17:01:13 +0000856 PragmaHandler *Handler) {
857 PragmaNamespace *InsertNS = PragmaHandlers;
Mike Stump1eb44332009-09-09 15:08:12 +0000858
Reid Spencer5f016e22007-07-11 17:01:13 +0000859 // If this is specified to be in a namespace, step down into it.
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000860 if (!Namespace.empty()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000861 // If there is already a pragma handler with the name of this namespace,
862 // we either have an error (directive with the same name as a namespace) or
863 // we already have the namespace to insert into.
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000864 if (PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000865 InsertNS = Existing->getIfNamespace();
866 assert(InsertNS != 0 && "Cannot have a pragma namespace and pragma"
867 " handler with the same name!");
868 } else {
869 // Otherwise, this namespace doesn't exist yet, create and insert the
870 // handler for it.
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000871 InsertNS = new PragmaNamespace(Namespace);
Reid Spencer5f016e22007-07-11 17:01:13 +0000872 PragmaHandlers->AddPragma(InsertNS);
873 }
874 }
Mike Stump1eb44332009-09-09 15:08:12 +0000875
Reid Spencer5f016e22007-07-11 17:01:13 +0000876 // Check to make sure we don't already have a pragma for this identifier.
877 assert(!InsertNS->FindHandler(Handler->getName()) &&
878 "Pragma handler already exists for this identifier!");
879 InsertNS->AddPragma(Handler);
880}
881
Daniel Dunbar40950802008-10-04 19:17:46 +0000882/// RemovePragmaHandler - Remove the specific pragma handler from the
883/// preprocessor. If \arg Namespace is non-null, then it should be the
884/// namespace that \arg Handler was added to. It is an error to remove
885/// a handler that has not been registered.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000886void Preprocessor::RemovePragmaHandler(StringRef Namespace,
Daniel Dunbar40950802008-10-04 19:17:46 +0000887 PragmaHandler *Handler) {
888 PragmaNamespace *NS = PragmaHandlers;
Mike Stump1eb44332009-09-09 15:08:12 +0000889
Daniel Dunbar40950802008-10-04 19:17:46 +0000890 // If this is specified to be in a namespace, step down into it.
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000891 if (!Namespace.empty()) {
892 PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace);
Daniel Dunbar40950802008-10-04 19:17:46 +0000893 assert(Existing && "Namespace containing handler does not exist!");
894
895 NS = Existing->getIfNamespace();
896 assert(NS && "Invalid namespace, registered as a regular pragma handler!");
897 }
898
899 NS->RemovePragmaHandler(Handler);
Mike Stump1eb44332009-09-09 15:08:12 +0000900
Daniel Dunbar40950802008-10-04 19:17:46 +0000901 // If this is a non-default namespace and it is now empty, remove
902 // it.
Argyrios Kyrtzidisce52bb32012-01-06 00:22:09 +0000903 if (NS != PragmaHandlers && NS->IsEmpty()) {
Daniel Dunbar40950802008-10-04 19:17:46 +0000904 PragmaHandlers->RemovePragmaHandler(NS);
Argyrios Kyrtzidisce52bb32012-01-06 00:22:09 +0000905 delete NS;
906 }
Daniel Dunbar40950802008-10-04 19:17:46 +0000907}
908
Peter Collingbourne9d3f5f72011-02-14 01:42:24 +0000909bool Preprocessor::LexOnOffSwitch(tok::OnOffSwitch &Result) {
910 Token Tok;
911 LexUnexpandedToken(Tok);
912
913 if (Tok.isNot(tok::identifier)) {
914 Diag(Tok, diag::ext_on_off_switch_syntax);
915 return true;
916 }
917 IdentifierInfo *II = Tok.getIdentifierInfo();
918 if (II->isStr("ON"))
919 Result = tok::OOS_ON;
920 else if (II->isStr("OFF"))
921 Result = tok::OOS_OFF;
922 else if (II->isStr("DEFAULT"))
923 Result = tok::OOS_DEFAULT;
924 else {
925 Diag(Tok, diag::ext_on_off_switch_syntax);
926 return true;
927 }
928
Peter Collingbourne84021552011-02-28 02:37:51 +0000929 // Verify that this is followed by EOD.
Peter Collingbourne9d3f5f72011-02-14 01:42:24 +0000930 LexUnexpandedToken(Tok);
Peter Collingbourne84021552011-02-28 02:37:51 +0000931 if (Tok.isNot(tok::eod))
932 Diag(Tok, diag::ext_pragma_syntax_eod);
Peter Collingbourne9d3f5f72011-02-14 01:42:24 +0000933 return false;
934}
935
Reid Spencer5f016e22007-07-11 17:01:13 +0000936namespace {
Chris Lattner22434492007-12-19 19:38:36 +0000937/// PragmaOnceHandler - "#pragma once" marks the file as atomically included.
Reid Spencer5f016e22007-07-11 17:01:13 +0000938struct PragmaOnceHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000939 PragmaOnceHandler() : PragmaHandler("once") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000940 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
941 Token &OnceTok) {
Chris Lattner35410d52009-04-14 05:07:49 +0000942 PP.CheckEndOfDirective("pragma once");
Reid Spencer5f016e22007-07-11 17:01:13 +0000943 PP.HandlePragmaOnce(OnceTok);
944 }
945};
946
Chris Lattner22434492007-12-19 19:38:36 +0000947/// PragmaMarkHandler - "#pragma mark ..." is ignored by the compiler, and the
948/// rest of the line is not lexed.
949struct PragmaMarkHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000950 PragmaMarkHandler() : PragmaHandler("mark") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000951 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
952 Token &MarkTok) {
Chris Lattner22434492007-12-19 19:38:36 +0000953 PP.HandlePragmaMark();
954 }
955};
956
957/// PragmaPoisonHandler - "#pragma poison x" marks x as not usable.
Reid Spencer5f016e22007-07-11 17:01:13 +0000958struct PragmaPoisonHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000959 PragmaPoisonHandler() : PragmaHandler("poison") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000960 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
961 Token &PoisonTok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000962 PP.HandlePragmaPoison(PoisonTok);
963 }
964};
965
Chris Lattner22434492007-12-19 19:38:36 +0000966/// PragmaSystemHeaderHandler - "#pragma system_header" marks the current file
967/// as a system header, which silences warnings in it.
Reid Spencer5f016e22007-07-11 17:01:13 +0000968struct PragmaSystemHeaderHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000969 PragmaSystemHeaderHandler() : PragmaHandler("system_header") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000970 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
971 Token &SHToken) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000972 PP.HandlePragmaSystemHeader(SHToken);
Chris Lattner35410d52009-04-14 05:07:49 +0000973 PP.CheckEndOfDirective("pragma");
Reid Spencer5f016e22007-07-11 17:01:13 +0000974 }
975};
976struct PragmaDependencyHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000977 PragmaDependencyHandler() : PragmaHandler("dependency") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000978 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
979 Token &DepToken) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000980 PP.HandlePragmaDependency(DepToken);
981 }
982};
Mike Stump1eb44332009-09-09 15:08:12 +0000983
Daniel Dunbarabf7b722010-07-28 15:40:33 +0000984struct PragmaDebugHandler : public PragmaHandler {
985 PragmaDebugHandler() : PragmaHandler("__debug") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000986 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
987 Token &DepToken) {
Daniel Dunbarabf7b722010-07-28 15:40:33 +0000988 Token Tok;
989 PP.LexUnexpandedToken(Tok);
990 if (Tok.isNot(tok::identifier)) {
Douglas Gregor6493a4d2010-08-30 15:15:34 +0000991 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Daniel Dunbarabf7b722010-07-28 15:40:33 +0000992 return;
993 }
994 IdentifierInfo *II = Tok.getIdentifierInfo();
995
Daniel Dunbar55054132010-08-17 22:32:48 +0000996 if (II->isStr("assert")) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000997 llvm_unreachable("This is an assertion!");
Daniel Dunbarabf7b722010-07-28 15:40:33 +0000998 } else if (II->isStr("crash")) {
Daniel Dunbar55054132010-08-17 22:32:48 +0000999 *(volatile int*) 0x11 = 0;
1000 } else if (II->isStr("llvm_fatal_error")) {
1001 llvm::report_fatal_error("#pragma clang __debug llvm_fatal_error");
1002 } else if (II->isStr("llvm_unreachable")) {
1003 llvm_unreachable("#pragma clang __debug llvm_unreachable");
1004 } else if (II->isStr("overflow_stack")) {
1005 DebugOverflowStack();
Daniel Dunbarff759a62010-08-18 23:09:23 +00001006 } else if (II->isStr("handle_crash")) {
1007 llvm::CrashRecoveryContext *CRC =llvm::CrashRecoveryContext::GetCurrent();
1008 if (CRC)
1009 CRC->HandleCrash();
Daniel Dunbar55054132010-08-17 22:32:48 +00001010 } else {
1011 PP.Diag(Tok, diag::warn_pragma_debug_unexpected_command)
1012 << II->getName();
Daniel Dunbarabf7b722010-07-28 15:40:33 +00001013 }
1014 }
1015
Francois Pichet1066c6c2011-05-25 16:15:03 +00001016// Disable MSVC warning about runtime stack overflow.
1017#ifdef _MSC_VER
1018 #pragma warning(disable : 4717)
1019#endif
Daniel Dunbarabf7b722010-07-28 15:40:33 +00001020 void DebugOverflowStack() {
1021 DebugOverflowStack();
1022 }
Francois Pichet1066c6c2011-05-25 16:15:03 +00001023#ifdef _MSC_VER
1024 #pragma warning(default : 4717)
1025#endif
1026
Daniel Dunbarabf7b722010-07-28 15:40:33 +00001027};
1028
Chris Lattneredaf8772009-04-19 23:16:58 +00001029/// PragmaDiagnosticHandler - e.g. '#pragma GCC diagnostic ignored "-Wformat"'
1030struct PragmaDiagnosticHandler : public PragmaHandler {
Douglas Gregorc09ce122011-06-22 19:41:48 +00001031private:
1032 const char *Namespace;
Chris Lattner04ae2df2009-07-12 21:18:45 +00001033public:
Douglas Gregorc09ce122011-06-22 19:41:48 +00001034 explicit PragmaDiagnosticHandler(const char *NS) :
1035 PragmaHandler("diagnostic"), Namespace(NS) {}
Douglas Gregor80c60f72010-09-09 22:45:38 +00001036 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1037 Token &DiagToken) {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001038 SourceLocation DiagLoc = DiagToken.getLocation();
Chris Lattneredaf8772009-04-19 23:16:58 +00001039 Token Tok;
1040 PP.LexUnexpandedToken(Tok);
1041 if (Tok.isNot(tok::identifier)) {
Douglas Gregor6493a4d2010-08-30 15:15:34 +00001042 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Chris Lattneredaf8772009-04-19 23:16:58 +00001043 return;
1044 }
1045 IdentifierInfo *II = Tok.getIdentifierInfo();
Douglas Gregorc09ce122011-06-22 19:41:48 +00001046 PPCallbacks *Callbacks = PP.getPPCallbacks();
Mike Stump1eb44332009-09-09 15:08:12 +00001047
Chris Lattneredaf8772009-04-19 23:16:58 +00001048 diag::Mapping Map;
1049 if (II->isStr("warning"))
1050 Map = diag::MAP_WARNING;
1051 else if (II->isStr("error"))
1052 Map = diag::MAP_ERROR;
1053 else if (II->isStr("ignored"))
1054 Map = diag::MAP_IGNORE;
1055 else if (II->isStr("fatal"))
1056 Map = diag::MAP_FATAL;
Douglas Gregor6493a4d2010-08-30 15:15:34 +00001057 else if (II->isStr("pop")) {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001058 if (!PP.getDiagnostics().popMappings(DiagLoc))
Douglas Gregor6493a4d2010-08-30 15:15:34 +00001059 PP.Diag(Tok, diag::warn_pragma_diagnostic_cannot_pop);
Douglas Gregorc09ce122011-06-22 19:41:48 +00001060 else if (Callbacks)
1061 Callbacks->PragmaDiagnosticPop(DiagLoc, Namespace);
Douglas Gregor6493a4d2010-08-30 15:15:34 +00001062 return;
1063 } else if (II->isStr("push")) {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001064 PP.getDiagnostics().pushMappings(DiagLoc);
Douglas Gregorc09ce122011-06-22 19:41:48 +00001065 if (Callbacks)
1066 Callbacks->PragmaDiagnosticPush(DiagLoc, Namespace);
Chris Lattner04ae2df2009-07-12 21:18:45 +00001067 return;
1068 } else {
Douglas Gregor6493a4d2010-08-30 15:15:34 +00001069 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Chris Lattneredaf8772009-04-19 23:16:58 +00001070 return;
1071 }
Mike Stump1eb44332009-09-09 15:08:12 +00001072
Chris Lattneredaf8772009-04-19 23:16:58 +00001073 PP.LexUnexpandedToken(Tok);
1074
1075 // We need at least one string.
1076 if (Tok.isNot(tok::string_literal)) {
1077 PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token);
1078 return;
1079 }
Mike Stump1eb44332009-09-09 15:08:12 +00001080
Chris Lattneredaf8772009-04-19 23:16:58 +00001081 // String concatenation allows multiple strings, which can even come from
1082 // macro expansion.
1083 // "foo " "bar" "Baz"
Chris Lattner5f9e2722011-07-23 10:55:15 +00001084 SmallVector<Token, 4> StrToks;
Chris Lattneredaf8772009-04-19 23:16:58 +00001085 while (Tok.is(tok::string_literal)) {
1086 StrToks.push_back(Tok);
1087 PP.LexUnexpandedToken(Tok);
1088 }
Mike Stump1eb44332009-09-09 15:08:12 +00001089
Peter Collingbourne84021552011-02-28 02:37:51 +00001090 if (Tok.isNot(tok::eod)) {
Chris Lattneredaf8772009-04-19 23:16:58 +00001091 PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token);
1092 return;
1093 }
Mike Stump1eb44332009-09-09 15:08:12 +00001094
Chris Lattneredaf8772009-04-19 23:16:58 +00001095 // Concatenate and parse the strings.
1096 StringLiteralParser Literal(&StrToks[0], StrToks.size(), PP);
Douglas Gregor5cee1192011-07-27 05:40:30 +00001097 assert(Literal.isAscii() && "Didn't allow wide strings in");
Chris Lattneredaf8772009-04-19 23:16:58 +00001098 if (Literal.hadError)
1099 return;
1100 if (Literal.Pascal) {
Douglas Gregor6493a4d2010-08-30 15:15:34 +00001101 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Chris Lattneredaf8772009-04-19 23:16:58 +00001102 return;
1103 }
Chris Lattner04ae2df2009-07-12 21:18:45 +00001104
Chris Lattner5f9e2722011-07-23 10:55:15 +00001105 StringRef WarningName(Literal.GetString());
Chris Lattneredaf8772009-04-19 23:16:58 +00001106
1107 if (WarningName.size() < 3 || WarningName[0] != '-' ||
1108 WarningName[1] != 'W') {
1109 PP.Diag(StrToks[0].getLocation(),
1110 diag::warn_pragma_diagnostic_invalid_option);
1111 return;
1112 }
Mike Stump1eb44332009-09-09 15:08:12 +00001113
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +00001114 if (PP.getDiagnostics().setDiagnosticGroupMapping(WarningName.substr(2),
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001115 Map, DiagLoc))
Chris Lattneredaf8772009-04-19 23:16:58 +00001116 PP.Diag(StrToks[0].getLocation(),
1117 diag::warn_pragma_diagnostic_unknown_warning) << WarningName;
Douglas Gregorc09ce122011-06-22 19:41:48 +00001118 else if (Callbacks)
1119 Callbacks->PragmaDiagnostic(DiagLoc, Namespace, Map, WarningName);
Chris Lattneredaf8772009-04-19 23:16:58 +00001120 }
1121};
Mike Stump1eb44332009-09-09 15:08:12 +00001122
Chris Lattner636c5ef2009-01-16 08:21:25 +00001123/// PragmaCommentHandler - "#pragma comment ...".
1124struct PragmaCommentHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001125 PragmaCommentHandler() : PragmaHandler("comment") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +00001126 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1127 Token &CommentTok) {
Chris Lattner636c5ef2009-01-16 08:21:25 +00001128 PP.HandlePragmaComment(CommentTok);
1129 }
1130};
Mike Stump1eb44332009-09-09 15:08:12 +00001131
Aaron Ballman4c55c542012-03-02 22:51:54 +00001132/// PragmaIncludeAliasHandler - "#pragma include_alias("...")".
1133struct PragmaIncludeAliasHandler : public PragmaHandler {
1134 PragmaIncludeAliasHandler() : PragmaHandler("include_alias") {}
1135 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1136 Token &IncludeAliasTok) {
1137 PP.HandlePragmaIncludeAlias(IncludeAliasTok);
1138 }
1139};
1140
Chris Lattnerabfe0942010-06-26 17:11:39 +00001141/// PragmaMessageHandler - "#pragma message("...")".
1142struct PragmaMessageHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001143 PragmaMessageHandler() : PragmaHandler("message") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +00001144 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1145 Token &CommentTok) {
Chris Lattnerabfe0942010-06-26 17:11:39 +00001146 PP.HandlePragmaMessage(CommentTok);
1147 }
1148};
1149
Chris Lattnerf47724b2010-08-17 15:55:45 +00001150/// PragmaPushMacroHandler - "#pragma push_macro" saves the value of the
1151/// macro on the top of the stack.
1152struct PragmaPushMacroHandler : public PragmaHandler {
1153 PragmaPushMacroHandler() : PragmaHandler("push_macro") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +00001154 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1155 Token &PushMacroTok) {
Chris Lattnerf47724b2010-08-17 15:55:45 +00001156 PP.HandlePragmaPushMacro(PushMacroTok);
1157 }
1158};
1159
1160
1161/// PragmaPopMacroHandler - "#pragma pop_macro" sets the value of the
1162/// macro to the value on the top of the stack.
1163struct PragmaPopMacroHandler : public PragmaHandler {
1164 PragmaPopMacroHandler() : PragmaHandler("pop_macro") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +00001165 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1166 Token &PopMacroTok) {
Chris Lattnerf47724b2010-08-17 15:55:45 +00001167 PP.HandlePragmaPopMacro(PopMacroTok);
1168 }
1169};
1170
Chris Lattner062f2322009-04-19 21:20:35 +00001171// Pragma STDC implementations.
Chris Lattner6c5cf4a2009-04-19 21:50:08 +00001172
Chris Lattner062f2322009-04-19 21:20:35 +00001173/// PragmaSTDC_FENV_ACCESSHandler - "#pragma STDC FENV_ACCESS ...".
1174struct PragmaSTDC_FENV_ACCESSHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001175 PragmaSTDC_FENV_ACCESSHandler() : PragmaHandler("FENV_ACCESS") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +00001176 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1177 Token &Tok) {
Peter Collingbourne9d3f5f72011-02-14 01:42:24 +00001178 tok::OnOffSwitch OOS;
1179 if (PP.LexOnOffSwitch(OOS))
1180 return;
1181 if (OOS == tok::OOS_ON)
Chris Lattner4d8aac32009-04-19 21:55:32 +00001182 PP.Diag(Tok, diag::warn_stdc_fenv_access_not_supported);
Chris Lattner062f2322009-04-19 21:20:35 +00001183 }
1184};
Mike Stump1eb44332009-09-09 15:08:12 +00001185
Chris Lattner062f2322009-04-19 21:20:35 +00001186/// PragmaSTDC_CX_LIMITED_RANGEHandler - "#pragma STDC CX_LIMITED_RANGE ...".
1187struct PragmaSTDC_CX_LIMITED_RANGEHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001188 PragmaSTDC_CX_LIMITED_RANGEHandler()
1189 : PragmaHandler("CX_LIMITED_RANGE") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +00001190 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1191 Token &Tok) {
Peter Collingbourne9d3f5f72011-02-14 01:42:24 +00001192 tok::OnOffSwitch OOS;
1193 PP.LexOnOffSwitch(OOS);
Chris Lattner062f2322009-04-19 21:20:35 +00001194 }
1195};
Mike Stump1eb44332009-09-09 15:08:12 +00001196
Chris Lattner062f2322009-04-19 21:20:35 +00001197/// PragmaSTDC_UnknownHandler - "#pragma STDC ...".
1198struct PragmaSTDC_UnknownHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001199 PragmaSTDC_UnknownHandler() {}
Douglas Gregor80c60f72010-09-09 22:45:38 +00001200 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1201 Token &UnknownTok) {
Chris Lattner6c5cf4a2009-04-19 21:50:08 +00001202 // C99 6.10.6p2, unknown forms are not allowed.
Chris Lattnerf545be52009-04-19 21:25:37 +00001203 PP.Diag(UnknownTok, diag::ext_stdc_pragma_ignored);
Chris Lattner062f2322009-04-19 21:20:35 +00001204 }
1205};
Mike Stump1eb44332009-09-09 15:08:12 +00001206
John McCall8dfac0b2011-09-30 05:12:12 +00001207/// PragmaARCCFCodeAuditedHandler -
1208/// #pragma clang arc_cf_code_audited begin/end
1209struct PragmaARCCFCodeAuditedHandler : public PragmaHandler {
1210 PragmaARCCFCodeAuditedHandler() : PragmaHandler("arc_cf_code_audited") {}
1211 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1212 Token &NameTok) {
1213 SourceLocation Loc = NameTok.getLocation();
1214 bool IsBegin;
1215
1216 Token Tok;
1217
1218 // Lex the 'begin' or 'end'.
1219 PP.LexUnexpandedToken(Tok);
1220 const IdentifierInfo *BeginEnd = Tok.getIdentifierInfo();
1221 if (BeginEnd && BeginEnd->isStr("begin")) {
1222 IsBegin = true;
1223 } else if (BeginEnd && BeginEnd->isStr("end")) {
1224 IsBegin = false;
1225 } else {
1226 PP.Diag(Tok.getLocation(), diag::err_pp_arc_cf_code_audited_syntax);
1227 return;
1228 }
1229
1230 // Verify that this is followed by EOD.
1231 PP.LexUnexpandedToken(Tok);
1232 if (Tok.isNot(tok::eod))
1233 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
1234
1235 // The start location of the active audit.
1236 SourceLocation BeginLoc = PP.getPragmaARCCFCodeAuditedLoc();
1237
1238 // The start location we want after processing this.
1239 SourceLocation NewLoc;
1240
1241 if (IsBegin) {
1242 // Complain about attempts to re-enter an audit.
1243 if (BeginLoc.isValid()) {
1244 PP.Diag(Loc, diag::err_pp_double_begin_of_arc_cf_code_audited);
1245 PP.Diag(BeginLoc, diag::note_pragma_entered_here);
1246 }
1247 NewLoc = Loc;
1248 } else {
1249 // Complain about attempts to leave an audit that doesn't exist.
1250 if (!BeginLoc.isValid()) {
1251 PP.Diag(Loc, diag::err_pp_unmatched_end_of_arc_cf_code_audited);
1252 return;
1253 }
1254 NewLoc = SourceLocation();
1255 }
1256
1257 PP.setPragmaARCCFCodeAuditedLoc(NewLoc);
1258 }
1259};
1260
Reid Spencer5f016e22007-07-11 17:01:13 +00001261} // end anonymous namespace
1262
1263
1264/// RegisterBuiltinPragmas - Install the standard preprocessor pragmas:
1265/// #pragma GCC poison/system_header/dependency and #pragma once.
1266void Preprocessor::RegisterBuiltinPragmas() {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001267 AddPragmaHandler(new PragmaOnceHandler());
1268 AddPragmaHandler(new PragmaMarkHandler());
Chris Lattnerf47724b2010-08-17 15:55:45 +00001269 AddPragmaHandler(new PragmaPushMacroHandler());
1270 AddPragmaHandler(new PragmaPopMacroHandler());
Michael J. Spencer301669b2010-09-27 06:19:02 +00001271 AddPragmaHandler(new PragmaMessageHandler());
Mike Stump1eb44332009-09-09 15:08:12 +00001272
Chris Lattnere8fa06e2009-05-12 18:21:11 +00001273 // #pragma GCC ...
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001274 AddPragmaHandler("GCC", new PragmaPoisonHandler());
1275 AddPragmaHandler("GCC", new PragmaSystemHeaderHandler());
1276 AddPragmaHandler("GCC", new PragmaDependencyHandler());
Douglas Gregorc09ce122011-06-22 19:41:48 +00001277 AddPragmaHandler("GCC", new PragmaDiagnosticHandler("GCC"));
Chris Lattnere8fa06e2009-05-12 18:21:11 +00001278 // #pragma clang ...
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001279 AddPragmaHandler("clang", new PragmaPoisonHandler());
1280 AddPragmaHandler("clang", new PragmaSystemHeaderHandler());
Daniel Dunbarabf7b722010-07-28 15:40:33 +00001281 AddPragmaHandler("clang", new PragmaDebugHandler());
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001282 AddPragmaHandler("clang", new PragmaDependencyHandler());
Douglas Gregorc09ce122011-06-22 19:41:48 +00001283 AddPragmaHandler("clang", new PragmaDiagnosticHandler("clang"));
John McCall8dfac0b2011-09-30 05:12:12 +00001284 AddPragmaHandler("clang", new PragmaARCCFCodeAuditedHandler());
Chris Lattnere8fa06e2009-05-12 18:21:11 +00001285
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001286 AddPragmaHandler("STDC", new PragmaSTDC_FENV_ACCESSHandler());
1287 AddPragmaHandler("STDC", new PragmaSTDC_CX_LIMITED_RANGEHandler());
Chris Lattner062f2322009-04-19 21:20:35 +00001288 AddPragmaHandler("STDC", new PragmaSTDC_UnknownHandler());
Mike Stump1eb44332009-09-09 15:08:12 +00001289
Chris Lattner636c5ef2009-01-16 08:21:25 +00001290 // MS extensions.
David Blaikie4e4d0842012-03-11 07:00:24 +00001291 if (LangOpts.MicrosoftExt) {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001292 AddPragmaHandler(new PragmaCommentHandler());
Aaron Ballman4c55c542012-03-02 22:51:54 +00001293 AddPragmaHandler(new PragmaIncludeAliasHandler());
Chris Lattnerabfe0942010-06-26 17:11:39 +00001294 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001295}