blob: acf5c74556f429005b9f202a6de7bd6c9ebf24ae [file] [log] [blame]
Chris Lattnerb8761832006-06-24 21:31:03 +00001//===--- Pragma.cpp - Pragma registration and handling --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerb8761832006-06-24 21:31:03 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattnerb694ba72006-07-02 22:41:36 +000010// This file implements the PragmaHandler/PragmaTable interfaces and implements
11// pragma related methods of the Preprocessor class.
Chris Lattnerb8761832006-06-24 21:31:03 +000012//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Lex/Pragma.h"
Chris Lattner07b019a2006-10-22 07:28:56 +000016#include "clang/Lex/HeaderSearch.h"
Chris Lattner262d4e32009-01-16 18:59:23 +000017#include "clang/Lex/LiteralSupport.h"
Chris Lattnerb8761832006-06-24 21:31:03 +000018#include "clang/Lex/Preprocessor.h"
Chris Lattnerc0a585d2010-08-17 15:55:45 +000019#include "clang/Lex/MacroInfo.h"
Chris Lattner60f36222009-01-29 05:15:15 +000020#include "clang/Lex/LexDiagnostic.h"
Chris Lattnerb694ba72006-07-02 22:41:36 +000021#include "clang/Basic/FileManager.h"
22#include "clang/Basic/SourceManager.h"
Daniel Dunbar211a7872010-08-18 23:09:23 +000023#include "llvm/Support/CrashRecoveryContext.h"
Daniel Dunbarf2cf3292010-08-17 22:32:48 +000024#include "llvm/Support/ErrorHandling.h"
Douglas Gregorc6d5edd2009-07-02 17:08:52 +000025#include <algorithm>
Chris Lattnerb8761832006-06-24 21:31:03 +000026using namespace clang;
27
28// Out-of-line destructor to provide a home for the class.
29PragmaHandler::~PragmaHandler() {
30}
31
Chris Lattner2e155302006-07-03 05:34:41 +000032//===----------------------------------------------------------------------===//
Daniel Dunbard839e772010-06-11 20:10:12 +000033// EmptyPragmaHandler Implementation.
34//===----------------------------------------------------------------------===//
35
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000036EmptyPragmaHandler::EmptyPragmaHandler() {}
Daniel Dunbard839e772010-06-11 20:10:12 +000037
Douglas Gregorc7d65762010-09-09 22:45:38 +000038void EmptyPragmaHandler::HandlePragma(Preprocessor &PP,
39 PragmaIntroducerKind Introducer,
40 Token &FirstToken) {}
Daniel Dunbard839e772010-06-11 20:10:12 +000041
42//===----------------------------------------------------------------------===//
Chris Lattner2e155302006-07-03 05:34:41 +000043// PragmaNamespace Implementation.
44//===----------------------------------------------------------------------===//
45
46
47PragmaNamespace::~PragmaNamespace() {
Argyrios Kyrtzidis36745fd2010-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;
Chris Lattner2e155302006-07-03 05:34:41 +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 Lattner0e62c1c2011-07-23 10:55:15 +000057PragmaHandler *PragmaNamespace::FindHandler(StringRef Name,
Chris Lattner2e155302006-07-03 05:34:41 +000058 bool IgnoreNull) const {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000059 if (PragmaHandler *Handler = Handlers.lookup(Name))
60 return Handler;
Chris Lattner0e62c1c2011-07-23 10:55:15 +000061 return IgnoreNull ? 0 : Handlers.lookup(StringRef());
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000062}
Mike Stump11289f42009-09-09 15:08:12 +000063
Argyrios Kyrtzidis36745fd2010-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);
Chris Lattner2e155302006-07-03 05:34:41 +000070}
71
Daniel Dunbar40596532008-10-04 19:17:46 +000072void PragmaNamespace::RemovePragmaHandler(PragmaHandler *Handler) {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000073 assert(Handlers.lookup(Handler->getName()) &&
74 "Handler not registered in this namespace");
75 Handlers.erase(Handler->getName());
Daniel Dunbar40596532008-10-04 19:17:46 +000076}
77
Douglas Gregorc7d65762010-09-09 22:45:38 +000078void PragmaNamespace::HandlePragma(Preprocessor &PP,
79 PragmaIntroducerKind Introducer,
80 Token &Tok) {
Chris Lattnerb8761832006-06-24 21:31:03 +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 Stump11289f42009-09-09 15:08:12 +000084
Chris Lattnerb8761832006-06-24 21:31:03 +000085 // Get the handler for this token. If there is no handler, ignore the pragma.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000086 PragmaHandler *Handler
87 = FindHandler(Tok.getIdentifierInfo() ? Tok.getIdentifierInfo()->getName()
Chris Lattner0e62c1c2011-07-23 10:55:15 +000088 : StringRef(),
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000089 /*IgnoreNull=*/false);
Chris Lattner21656f22009-04-19 21:10:26 +000090 if (Handler == 0) {
91 PP.Diag(Tok, diag::warn_pragma_ignored);
92 return;
93 }
Mike Stump11289f42009-09-09 15:08:12 +000094
Chris Lattnerb8761832006-06-24 21:31:03 +000095 // Otherwise, pass it down.
Douglas Gregorc7d65762010-09-09 22:45:38 +000096 Handler->HandlePragma(PP, Introducer, Tok);
Chris Lattnerb8761832006-06-24 21:31:03 +000097}
Chris Lattnerb694ba72006-07-02 22:41:36 +000098
Chris Lattnerb694ba72006-07-02 22:41:36 +000099//===----------------------------------------------------------------------===//
100// Preprocessor Pragma Directive Handling.
101//===----------------------------------------------------------------------===//
102
James Dennett18a6d792012-06-17 03:26:26 +0000103/// HandlePragmaDirective - The "\#pragma" directive has been parsed. Lex the
Chris Lattnerb694ba72006-07-02 22:41:36 +0000104/// rest of the pragma, passing it to the registered pragma handlers.
Douglas Gregorc7d65762010-09-09 22:45:38 +0000105void Preprocessor::HandlePragmaDirective(unsigned Introducer) {
Jordan Rosede1a2922012-06-08 18:06:21 +0000106 if (!PragmasEnabled)
107 return;
108
Chris Lattnerb694ba72006-07-02 22:41:36 +0000109 ++NumPragma;
Mike Stump11289f42009-09-09 15:08:12 +0000110
Chris Lattnerb694ba72006-07-02 22:41:36 +0000111 // Invoke the first level of pragma handlers which reads the namespace id.
Chris Lattner146762e2007-07-20 16:59:19 +0000112 Token Tok;
Douglas Gregorc7d65762010-09-09 22:45:38 +0000113 PragmaHandlers->HandlePragma(*this, PragmaIntroducerKind(Introducer), Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000114
Chris Lattnerb694ba72006-07-02 22:41:36 +0000115 // If the pragma handler didn't read the rest of the line, consume it now.
Peter Collingbourne2c9f9662011-02-22 13:49:00 +0000116 if ((CurTokenLexer && CurTokenLexer->isParsingPreprocessorDirective())
117 || (CurPPLexer && CurPPLexer->ParsingPreprocessorDirective))
Chris Lattnerb694ba72006-07-02 22:41:36 +0000118 DiscardUntilEndOfDirective();
119}
120
Argyrios Kyrtzidisf1b64c62012-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
Chris Lattnerb694ba72006-07-02 22:41:36 +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 Lattner146762e2007-07-20 16:59:19 +0000161void Preprocessor::Handle_Pragma(Token &Tok) {
Argyrios Kyrtzidisf1b64c62012-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
Chris Lattnerb694ba72006-07-02 22:41:36 +0000176 // Remember the pragma token location.
177 SourceLocation PragmaLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000178
Chris Lattnerb694ba72006-07-02 22:41:36 +0000179 // Read the '('.
180 Lex(Tok);
Chris Lattner907dfe92008-11-18 07:59:24 +0000181 if (Tok.isNot(tok::l_paren)) {
182 Diag(PragmaLoc, diag::err__Pragma_malformed);
Argyrios Kyrtzidisf1b64c62012-04-03 16:47:40 +0000183 return _PragmaLexing.failed();
Chris Lattner907dfe92008-11-18 07:59:24 +0000184 }
Chris Lattnerb694ba72006-07-02 22:41:36 +0000185
186 // Read the '"..."'.
187 Lex(Tok);
Chris Lattner907dfe92008-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 Smithd67aea22012-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 Kyrtzidisf1b64c62012-04-03 16:47:40 +0000195 return _PragmaLexing.failed();
Richard Smithd67aea22012-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 Kyrtzidisf1b64c62012-04-03 16:47:40 +0000204 return _PragmaLexing.failed();
Chris Lattner907dfe92008-11-18 07:59:24 +0000205 }
Mike Stump11289f42009-09-09 15:08:12 +0000206
Chris Lattnerb694ba72006-07-02 22:41:36 +0000207 // Remember the string.
Argyrios Kyrtzidisf1b64c62012-04-03 16:47:40 +0000208 Token StrTok = Tok;
Chris Lattnerb694ba72006-07-02 22:41:36 +0000209
210 // Read the ')'.
211 Lex(Tok);
Chris Lattner907dfe92008-11-18 07:59:24 +0000212 if (Tok.isNot(tok::r_paren)) {
213 Diag(PragmaLoc, diag::err__Pragma_malformed);
Argyrios Kyrtzidisf1b64c62012-04-03 16:47:40 +0000214 return _PragmaLexing.failed();
Chris Lattner907dfe92008-11-18 07:59:24 +0000215 }
Mike Stump11289f42009-09-09 15:08:12 +0000216
Argyrios Kyrtzidisf1b64c62012-04-03 16:47:40 +0000217 if (InMacroArgPreExpansion)
218 return;
219
Chris Lattner9dc9c202009-02-15 20:52:18 +0000220 SourceLocation RParenLoc = Tok.getLocation();
Argyrios Kyrtzidisf1b64c62012-04-03 16:47:40 +0000221 std::string StrVal = getSpelling(StrTok);
Mike Stump11289f42009-09-09 15:08:12 +0000222
Chris Lattner262d4e32009-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."
Chris Lattnerb694ba72006-07-02 22:41:36 +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 Stump11289f42009-09-09 15:08:12 +0000232
Chris Lattnerb694ba72006-07-02 22:41:36 +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 Stump11289f42009-09-09 15:08:12 +0000236
Chris Lattnerfa217bd2009-03-08 08:08:45 +0000237 // Replace the terminating quote with a \n.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000238 StrVal[StrVal.size()-1] = '\n';
Mike Stump11289f42009-09-09 15:08:12 +0000239
Chris Lattnerb694ba72006-07-02 22:41:36 +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 McCall89e925d2010-08-28 22:34:47 +0000249
Peter Collingbournef29ce972011-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 McCall89e925d2010-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 Collingbournef29ce972011-02-22 13:49:06 +0000284 // Get the tokens enclosed within the __pragma(), as well as the final ')'.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000285 SmallVector<Token, 32> PragmaToks;
John McCall89e925d2010-08-28 22:34:47 +0000286 int NumParens = 0;
287 Lex(Tok);
288 while (Tok.isNot(tok::eof)) {
Peter Collingbournef29ce972011-02-22 13:49:06 +0000289 PragmaToks.push_back(Tok);
John McCall89e925d2010-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 McCall89e925d2010-08-28 22:34:47 +0000294 Lex(Tok);
295 }
296
John McCall49039d42010-08-29 01:09:54 +0000297 if (Tok.is(tok::eof)) {
298 Diag(PragmaLoc, diag::err_unterminated___pragma);
299 return;
300 }
301
Peter Collingbournef29ce972011-02-22 13:49:06 +0000302 PragmaToks.front().setFlag(Token::LeadingSpace);
John McCall89e925d2010-08-28 22:34:47 +0000303
Peter Collingbourne2f1e36b2011-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 Collingbournef29ce972011-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 McCall89e925d2010-08-28 22:34:47 +0000315
316 // Finally, return whatever came after the pragma directive.
317 return Lex(Tok);
318}
319
James Dennett18a6d792012-06-17 03:26:26 +0000320/// HandlePragmaOnce - Handle \#pragma once. OnceTok is the 'once'.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000321///
Chris Lattner146762e2007-07-20 16:59:19 +0000322void Preprocessor::HandlePragmaOnce(Token &OnceTok) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000323 if (isInPrimaryFile()) {
324 Diag(OnceTok, diag::pp_pragma_once_in_main_file);
325 return;
326 }
Mike Stump11289f42009-09-09 15:08:12 +0000327
Chris Lattnerb694ba72006-07-02 22:41:36 +0000328 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000329 // Mark the file as a once-only file now.
Chris Lattnerd32480d2009-01-17 06:22:33 +0000330 HeaderInfo.MarkFileIncludeOnce(getCurrentFileLexer()->getFileEntry());
Chris Lattnerb694ba72006-07-02 22:41:36 +0000331}
332
Chris Lattnerc2383312007-12-19 19:38:36 +0000333void Preprocessor::HandlePragmaMark() {
Ted Kremenek76c34412008-11-19 22:21:33 +0000334 assert(CurPPLexer && "No current lexer?");
Chris Lattnerd9efb6e2009-06-15 05:02:34 +0000335 if (CurLexer)
336 CurLexer->ReadToEndOfLine();
337 else
338 CurPTHLexer->DiscardToEndOfLine();
Chris Lattnerc2383312007-12-19 19:38:36 +0000339}
340
341
James Dennett18a6d792012-06-17 03:26:26 +0000342/// HandlePragmaPoison - Handle \#pragma GCC poison. PoisonTok is the 'poison'.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000343///
Chris Lattner146762e2007-07-20 16:59:19 +0000344void Preprocessor::HandlePragmaPoison(Token &PoisonTok) {
345 Token Tok;
Chris Lattner538d7f32006-07-20 04:31:52 +0000346
Chris Lattnerb694ba72006-07-02 22:41:36 +0000347 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 Kremenek551c82a2008-11-18 01:12:54 +0000353 if (CurPPLexer) CurPPLexer->LexingRawMode = true;
Chris Lattnerb694ba72006-07-02 22:41:36 +0000354 LexUnexpandedToken(Tok);
Ted Kremenek551c82a2008-11-18 01:12:54 +0000355 if (CurPPLexer) CurPPLexer->LexingRawMode = false;
Mike Stump11289f42009-09-09 15:08:12 +0000356
Chris Lattnerb694ba72006-07-02 22:41:36 +0000357 // If we reached the end of line, we're done.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000358 if (Tok.is(tok::eod)) return;
Mike Stump11289f42009-09-09 15:08:12 +0000359
Chris Lattnerb694ba72006-07-02 22:41:36 +0000360 // Can only poison identifiers.
Abramo Bagnaraea4f7c72010-12-22 08:23:18 +0000361 if (Tok.isNot(tok::raw_identifier)) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000362 Diag(Tok, diag::err_pp_invalid_poison);
363 return;
364 }
Mike Stump11289f42009-09-09 15:08:12 +0000365
Chris Lattnercefc7682006-07-08 08:28:12 +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 Stump11289f42009-09-09 15:08:12 +0000369
Chris Lattnerb694ba72006-07-02 22:41:36 +0000370 // Already poisoned.
371 if (II->isPoisoned()) continue;
Mike Stump11289f42009-09-09 15:08:12 +0000372
Chris Lattnerb694ba72006-07-02 22:41:36 +0000373 // If this is a macro identifier, emit a warning.
Chris Lattner259716a2007-10-07 08:04:56 +0000374 if (II->hasMacroDefinition())
Chris Lattnerb694ba72006-07-02 22:41:36 +0000375 Diag(Tok, diag::pp_poisoning_existing_macro);
Mike Stump11289f42009-09-09 15:08:12 +0000376
Chris Lattnerb694ba72006-07-02 22:41:36 +0000377 // Finally, poison it!
378 II->setIsPoisoned();
Douglas Gregor935bc7a22011-10-27 09:33:13 +0000379 if (II->isFromAST())
380 II->setChangedSinceDeserialization();
Chris Lattnerb694ba72006-07-02 22:41:36 +0000381 }
382}
383
James Dennett18a6d792012-06-17 03:26:26 +0000384/// HandlePragmaSystemHeader - Implement \#pragma GCC system_header. We know
Chris Lattnerb694ba72006-07-02 22:41:36 +0000385/// that the whole directive has been parsed.
Chris Lattner146762e2007-07-20 16:59:19 +0000386void Preprocessor::HandlePragmaSystemHeader(Token &SysHeaderTok) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000387 if (isInPrimaryFile()) {
388 Diag(SysHeaderTok, diag::pp_pragma_sysheader_in_main_file);
389 return;
390 }
Mike Stump11289f42009-09-09 15:08:12 +0000391
Chris Lattnerb694ba72006-07-02 22:41:36 +0000392 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
Ted Kremenek300590b2008-11-20 01:45:11 +0000393 PreprocessorLexer *TheLexer = getCurrentFileLexer();
Mike Stump11289f42009-09-09 15:08:12 +0000394
Chris Lattnerb694ba72006-07-02 22:41:36 +0000395 // Mark the file as a system header.
Chris Lattnerd32480d2009-01-17 06:22:33 +0000396 HeaderInfo.MarkFileSystemHeader(TheLexer->getFileEntry());
Mike Stump11289f42009-09-09 15:08:12 +0000397
398
Chris Lattnerd9efb6e2009-06-15 05:02:34 +0000399 PresumedLoc PLoc = SourceMgr.getPresumedLoc(SysHeaderTok.getLocation());
Douglas Gregor453b0122010-11-12 07:15:47 +0000400 if (PLoc.isInvalid())
401 return;
402
Jay Foad9a6b0982011-06-21 15:13:30 +0000403 unsigned FilenameID = SourceMgr.getLineTableFilenameID(PLoc.getFilename());
Mike Stump11289f42009-09-09 15:08:12 +0000404
Chris Lattner3bdc7672011-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 Lattnerd9efb6e2009-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);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000415}
416
James Dennett18a6d792012-06-17 03:26:26 +0000417/// HandlePragmaDependency - Handle \#pragma GCC dependency "foo" blah.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000418///
Chris Lattner146762e2007-07-20 16:59:19 +0000419void Preprocessor::HandlePragmaDependency(Token &DependencyTok) {
420 Token FilenameTok;
Ted Kremenek551c82a2008-11-18 01:12:54 +0000421 CurPPLexer->LexIncludeFilename(FilenameTok);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000422
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000423 // If the token kind is EOD, the error has already been diagnosed.
424 if (FilenameTok.is(tok::eod))
Chris Lattnerb694ba72006-07-02 22:41:36 +0000425 return;
Mike Stump11289f42009-09-09 15:08:12 +0000426
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000427 // Reserve a buffer to get the spelling.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000428 SmallString<128> FilenameBuffer;
Douglas Gregordc970f02010-03-16 22:30:13 +0000429 bool Invalid = false;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000430 StringRef Filename = getSpelling(FilenameTok, FilenameBuffer, &Invalid);
Douglas Gregordc970f02010-03-16 22:30:13 +0000431 if (Invalid)
432 return;
Mike Stump11289f42009-09-09 15:08:12 +0000433
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000434 bool isAngled =
435 GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000436 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
437 // error.
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000438 if (Filename.empty())
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000439 return;
Mike Stump11289f42009-09-09 15:08:12 +0000440
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000441 // Search include directories for this file.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000442 const DirectoryLookup *CurDir;
Douglas Gregor97eec242011-09-15 22:00:41 +0000443 const FileEntry *File = LookupFile(Filename, isAngled, 0, CurDir, NULL, NULL,
444 NULL);
Chris Lattner97b8e842008-11-18 08:02:48 +0000445 if (File == 0) {
Eli Friedman3781a362011-08-30 23:07:51 +0000446 if (!SuppressIncludeNotFoundError)
447 Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
Chris Lattner97b8e842008-11-18 08:02:48 +0000448 return;
449 }
Mike Stump11289f42009-09-09 15:08:12 +0000450
Chris Lattnerd32480d2009-01-17 06:22:33 +0000451 const FileEntry *CurFile = getCurrentFileLexer()->getFileEntry();
Chris Lattnerb694ba72006-07-02 22:41:36 +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 Collingbourne2f1e36b2011-02-28 02:37:51 +0000458 while (DependencyTok.isNot(tok::eod)) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000459 Message += getSpelling(DependencyTok) + " ";
460 Lex(DependencyTok);
461 }
Mike Stump11289f42009-09-09 15:08:12 +0000462
Chris Lattnerf0b04972010-09-05 23:16:09 +0000463 // Remove the trailing ' ' if present.
464 if (!Message.empty())
465 Message.erase(Message.end()-1);
Chris Lattner97b8e842008-11-18 08:02:48 +0000466 Diag(FilenameTok, diag::pp_out_of_date_dependency) << Message;
Chris Lattnerb694ba72006-07-02 22:41:36 +0000467 }
468}
469
James Dennett18a6d792012-06-17 03:26:26 +0000470/// \brief Handle the microsoft \#pragma comment extension.
471///
472/// The syntax is:
473/// \code
474/// \#pragma comment(linker, "foo")
475/// \endcode
Chris Lattner2ff698d2009-01-16 08:21:25 +0000476/// 'linker' is one of five identifiers: compiler, exestr, lib, linker, user.
477/// "foo" is a string, which is fully macro expanded, and permits string
Gabor Greif31a082f2009-03-17 11:39:38 +0000478/// concatenation, embedded escape characters etc. See MSDN for more details.
Chris Lattner2ff698d2009-01-16 08:21:25 +0000479void Preprocessor::HandlePragmaComment(Token &Tok) {
480 SourceLocation CommentLoc = Tok.getLocation();
481 Lex(Tok);
482 if (Tok.isNot(tok::l_paren)) {
483 Diag(CommentLoc, diag::err_pragma_comment_malformed);
484 return;
485 }
Mike Stump11289f42009-09-09 15:08:12 +0000486
Chris Lattner2ff698d2009-01-16 08:21:25 +0000487 // Read the identifier.
488 Lex(Tok);
489 if (Tok.isNot(tok::identifier)) {
490 Diag(CommentLoc, diag::err_pragma_comment_malformed);
491 return;
492 }
Mike Stump11289f42009-09-09 15:08:12 +0000493
Chris Lattner2ff698d2009-01-16 08:21:25 +0000494 // Verify that this is one of the 5 whitelisted options.
495 // FIXME: warn that 'exestr' is deprecated.
496 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000497 if (!II->isStr("compiler") && !II->isStr("exestr") && !II->isStr("lib") &&
Chris Lattner2ff698d2009-01-16 08:21:25 +0000498 !II->isStr("linker") && !II->isStr("user")) {
499 Diag(Tok.getLocation(), diag::err_pragma_comment_unknown_kind);
500 return;
501 }
Mike Stump11289f42009-09-09 15:08:12 +0000502
Chris Lattner262d4e32009-01-16 18:59:23 +0000503 // Read the optional string if present.
Chris Lattner2ff698d2009-01-16 08:21:25 +0000504 Lex(Tok);
Chris Lattner262d4e32009-01-16 18:59:23 +0000505 std::string ArgumentString;
Chris Lattner2ff698d2009-01-16 08:21:25 +0000506 if (Tok.is(tok::comma)) {
Chris Lattner262d4e32009-01-16 18:59:23 +0000507 Lex(Tok); // eat the comma.
Chris Lattner2ff698d2009-01-16 08:21:25 +0000508
509 // We need at least one string.
Chris Lattner504af112009-04-19 23:16:58 +0000510 if (Tok.isNot(tok::string_literal)) {
Chris Lattner2ff698d2009-01-16 08:21:25 +0000511 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
512 return;
513 }
514
515 // String concatenation allows multiple strings, which can even come from
516 // macro expansion.
517 // "foo " "bar" "Baz"
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000518 SmallVector<Token, 4> StrToks;
Chris Lattner504af112009-04-19 23:16:58 +0000519 while (Tok.is(tok::string_literal)) {
Richard Smithd67aea22012-03-06 03:21:47 +0000520 if (Tok.hasUDSuffix())
521 Diag(Tok, diag::err_invalid_string_udl);
Chris Lattner262d4e32009-01-16 18:59:23 +0000522 StrToks.push_back(Tok);
Chris Lattner2ff698d2009-01-16 08:21:25 +0000523 Lex(Tok);
Chris Lattner262d4e32009-01-16 18:59:23 +0000524 }
525
526 // Concatenate and parse the strings.
527 StringLiteralParser Literal(&StrToks[0], StrToks.size(), *this);
Douglas Gregorfb65e592011-07-27 05:40:30 +0000528 assert(Literal.isAscii() && "Didn't allow wide strings in");
Chris Lattner262d4e32009-01-16 18:59:23 +0000529 if (Literal.hadError)
530 return;
531 if (Literal.Pascal) {
532 Diag(StrToks[0].getLocation(), diag::err_pragma_comment_malformed);
533 return;
534 }
535
Jay Foad9a6b0982011-06-21 15:13:30 +0000536 ArgumentString = Literal.GetString();
Chris Lattner2ff698d2009-01-16 08:21:25 +0000537 }
Mike Stump11289f42009-09-09 15:08:12 +0000538
Chris Lattner262d4e32009-01-16 18:59:23 +0000539 // FIXME: If the kind is "compiler" warn if the string is present (it is
540 // ignored).
541 // FIXME: 'lib' requires a comment string.
542 // FIXME: 'linker' requires a comment string, and has a specific list of
543 // things that are allowable.
Mike Stump11289f42009-09-09 15:08:12 +0000544
Chris Lattner2ff698d2009-01-16 08:21:25 +0000545 if (Tok.isNot(tok::r_paren)) {
546 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
547 return;
548 }
Chris Lattner262d4e32009-01-16 18:59:23 +0000549 Lex(Tok); // eat the r_paren.
Chris Lattner2ff698d2009-01-16 08:21:25 +0000550
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000551 if (Tok.isNot(tok::eod)) {
Chris Lattner2ff698d2009-01-16 08:21:25 +0000552 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
553 return;
554 }
Mike Stump11289f42009-09-09 15:08:12 +0000555
Chris Lattner262d4e32009-01-16 18:59:23 +0000556 // If the pragma is lexically sound, notify any interested PPCallbacks.
Chris Lattnerf49775d2009-01-16 19:01:46 +0000557 if (Callbacks)
558 Callbacks->PragmaComment(CommentLoc, II, ArgumentString);
Chris Lattner2ff698d2009-01-16 08:21:25 +0000559}
560
James Dennett18a6d792012-06-17 03:26:26 +0000561/// HandlePragmaMessage - Handle the microsoft and gcc \#pragma message
Michael J. Spencera0a820f2010-09-27 06:19:02 +0000562/// extension. The syntax is:
James Dennett18a6d792012-06-17 03:26:26 +0000563/// \code
564/// \#pragma message(string)
565/// \endcode
Michael J. Spencera0a820f2010-09-27 06:19:02 +0000566/// OR, in GCC mode:
James Dennett18a6d792012-06-17 03:26:26 +0000567/// \code
568/// \#pragma message string
569/// \endcode
Michael J. Spencera0a820f2010-09-27 06:19:02 +0000570/// string is a string, which is fully macro expanded, and permits string
571/// concatenation, embedded escape characters, etc... See MSDN for more details.
Chris Lattner30c924b2010-06-26 17:11:39 +0000572void Preprocessor::HandlePragmaMessage(Token &Tok) {
573 SourceLocation MessageLoc = Tok.getLocation();
574 Lex(Tok);
Michael J. Spencera0a820f2010-09-27 06:19:02 +0000575 bool ExpectClosingParen = false;
Michael J. Spencer4362a1c2010-09-27 06:34:47 +0000576 switch (Tok.getKind()) {
Michael J. Spencera0a820f2010-09-27 06:19:02 +0000577 case tok::l_paren:
578 // We have a MSVC style pragma message.
579 ExpectClosingParen = true;
580 // Read the string.
581 Lex(Tok);
582 break;
583 case tok::string_literal:
584 // We have a GCC style pragma message, and we just read the string.
585 break;
586 default:
Chris Lattner30c924b2010-06-26 17:11:39 +0000587 Diag(MessageLoc, diag::err_pragma_message_malformed);
588 return;
589 }
Chris Lattner2ff698d2009-01-16 08:21:25 +0000590
Chris Lattner30c924b2010-06-26 17:11:39 +0000591 // We need at least one string.
592 if (Tok.isNot(tok::string_literal)) {
593 Diag(Tok.getLocation(), diag::err_pragma_message_malformed);
594 return;
595 }
596
597 // String concatenation allows multiple strings, which can even come from
598 // macro expansion.
599 // "foo " "bar" "Baz"
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000600 SmallVector<Token, 4> StrToks;
Chris Lattner30c924b2010-06-26 17:11:39 +0000601 while (Tok.is(tok::string_literal)) {
Richard Smithd67aea22012-03-06 03:21:47 +0000602 if (Tok.hasUDSuffix())
603 Diag(Tok, diag::err_invalid_string_udl);
Chris Lattner30c924b2010-06-26 17:11:39 +0000604 StrToks.push_back(Tok);
605 Lex(Tok);
606 }
607
608 // Concatenate and parse the strings.
609 StringLiteralParser Literal(&StrToks[0], StrToks.size(), *this);
Douglas Gregorfb65e592011-07-27 05:40:30 +0000610 assert(Literal.isAscii() && "Didn't allow wide strings in");
Chris Lattner30c924b2010-06-26 17:11:39 +0000611 if (Literal.hadError)
612 return;
613 if (Literal.Pascal) {
614 Diag(StrToks[0].getLocation(), diag::err_pragma_message_malformed);
615 return;
616 }
617
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000618 StringRef MessageString(Literal.GetString());
Chris Lattner30c924b2010-06-26 17:11:39 +0000619
Michael J. Spencera0a820f2010-09-27 06:19:02 +0000620 if (ExpectClosingParen) {
621 if (Tok.isNot(tok::r_paren)) {
622 Diag(Tok.getLocation(), diag::err_pragma_message_malformed);
623 return;
624 }
625 Lex(Tok); // eat the r_paren.
Chris Lattner30c924b2010-06-26 17:11:39 +0000626 }
Chris Lattner30c924b2010-06-26 17:11:39 +0000627
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000628 if (Tok.isNot(tok::eod)) {
Chris Lattner30c924b2010-06-26 17:11:39 +0000629 Diag(Tok.getLocation(), diag::err_pragma_message_malformed);
630 return;
631 }
632
633 // Output the message.
634 Diag(MessageLoc, diag::warn_pragma_message) << MessageString;
635
636 // If the pragma is lexically sound, notify any interested PPCallbacks.
637 if (Callbacks)
638 Callbacks->PragmaMessage(MessageLoc, MessageString);
639}
Chris Lattner2ff698d2009-01-16 08:21:25 +0000640
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000641/// ParsePragmaPushOrPopMacro - Handle parsing of pragma push_macro/pop_macro.
642/// Return the IdentifierInfo* associated with the macro to push or pop.
643IdentifierInfo *Preprocessor::ParsePragmaPushOrPopMacro(Token &Tok) {
644 // Remember the pragma token location.
645 Token PragmaTok = Tok;
646
647 // Read the '('.
648 Lex(Tok);
649 if (Tok.isNot(tok::l_paren)) {
650 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
651 << getSpelling(PragmaTok);
652 return 0;
653 }
654
655 // Read the macro name string.
656 Lex(Tok);
657 if (Tok.isNot(tok::string_literal)) {
658 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
659 << getSpelling(PragmaTok);
660 return 0;
661 }
662
Richard Smithd67aea22012-03-06 03:21:47 +0000663 if (Tok.hasUDSuffix()) {
664 Diag(Tok, diag::err_invalid_string_udl);
665 return 0;
666 }
667
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000668 // Remember the macro string.
669 std::string StrVal = getSpelling(Tok);
670
671 // Read the ')'.
672 Lex(Tok);
673 if (Tok.isNot(tok::r_paren)) {
674 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
675 << getSpelling(PragmaTok);
676 return 0;
677 }
678
679 assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
680 "Invalid string token!");
681
682 // Create a Token from the string.
683 Token MacroTok;
684 MacroTok.startToken();
Abramo Bagnaraea4f7c72010-12-22 08:23:18 +0000685 MacroTok.setKind(tok::raw_identifier);
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000686 CreateString(&StrVal[1], StrVal.size() - 2, MacroTok);
687
688 // Get the IdentifierInfo of MacroToPushTok.
689 return LookUpIdentifierInfo(MacroTok);
690}
691
James Dennett18a6d792012-06-17 03:26:26 +0000692/// \brief Handle \#pragma push_macro.
693///
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000694/// The syntax is:
James Dennett18a6d792012-06-17 03:26:26 +0000695/// \code
696/// \#pragma push_macro("macro")
697/// \endcode
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000698void Preprocessor::HandlePragmaPushMacro(Token &PushMacroTok) {
699 // Parse the pragma directive and get the macro IdentifierInfo*.
700 IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PushMacroTok);
701 if (!IdentInfo) return;
702
703 // Get the MacroInfo associated with IdentInfo.
704 MacroInfo *MI = getMacroInfo(IdentInfo);
705
706 MacroInfo *MacroCopyToPush = 0;
707 if (MI) {
708 // Make a clone of MI.
709 MacroCopyToPush = CloneMacroInfo(*MI);
710
711 // Allow the original MacroInfo to be redefined later.
712 MI->setIsAllowRedefinitionsWithoutWarning(true);
713 }
714
715 // Push the cloned MacroInfo so we can retrieve it later.
716 PragmaPushMacroInfo[IdentInfo].push_back(MacroCopyToPush);
717}
718
James Dennett18a6d792012-06-17 03:26:26 +0000719/// \brief Handle \#pragma pop_macro.
720///
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000721/// The syntax is:
James Dennett18a6d792012-06-17 03:26:26 +0000722/// \code
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000723/// #pragma pop_macro("macro")
James Dennett18a6d792012-06-17 03:26:26 +0000724/// \endcode
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000725void Preprocessor::HandlePragmaPopMacro(Token &PopMacroTok) {
726 SourceLocation MessageLoc = PopMacroTok.getLocation();
727
728 // Parse the pragma directive and get the macro IdentifierInfo*.
729 IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PopMacroTok);
730 if (!IdentInfo) return;
731
732 // Find the vector<MacroInfo*> associated with the macro.
733 llvm::DenseMap<IdentifierInfo*, std::vector<MacroInfo*> >::iterator iter =
734 PragmaPushMacroInfo.find(IdentInfo);
735 if (iter != PragmaPushMacroInfo.end()) {
Alexander Kornienko8b3f6232012-08-29 00:20:03 +0000736 // Forget the MacroInfo currently associated with IdentInfo.
737 if (MacroInfo *CurrentMI = getMacroInfo(IdentInfo)) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000738 if (CurrentMI->isWarnIfUnused())
739 WarnUnusedMacroLocs.erase(CurrentMI->getDefinitionLoc());
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000740 }
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000741
742 // Get the MacroInfo we want to reinstall.
743 MacroInfo *MacroToReInstall = iter->second.back();
744
745 // Reinstall the previously pushed macro.
746 setMacroInfo(IdentInfo, MacroToReInstall);
747
748 // Pop PragmaPushMacroInfo stack.
749 iter->second.pop_back();
750 if (iter->second.size() == 0)
751 PragmaPushMacroInfo.erase(iter);
752 } else {
753 Diag(MessageLoc, diag::warn_pragma_pop_macro_no_push)
754 << IdentInfo->getName();
755 }
756}
Chris Lattnerb694ba72006-07-02 22:41:36 +0000757
Aaron Ballman611306e2012-03-02 22:51:54 +0000758void Preprocessor::HandlePragmaIncludeAlias(Token &Tok) {
759 // We will either get a quoted filename or a bracketed filename, and we
760 // have to track which we got. The first filename is the source name,
761 // and the second name is the mapped filename. If the first is quoted,
762 // the second must be as well (cannot mix and match quotes and brackets).
Aaron Ballman611306e2012-03-02 22:51:54 +0000763
764 // Get the open paren
765 Lex(Tok);
766 if (Tok.isNot(tok::l_paren)) {
767 Diag(Tok, diag::warn_pragma_include_alias_expected) << "(";
768 return;
769 }
770
771 // We expect either a quoted string literal, or a bracketed name
772 Token SourceFilenameTok;
773 CurPPLexer->LexIncludeFilename(SourceFilenameTok);
774 if (SourceFilenameTok.is(tok::eod)) {
775 // The diagnostic has already been handled
776 return;
777 }
778
779 StringRef SourceFileName;
780 SmallString<128> FileNameBuffer;
781 if (SourceFilenameTok.is(tok::string_literal) ||
782 SourceFilenameTok.is(tok::angle_string_literal)) {
783 SourceFileName = getSpelling(SourceFilenameTok, FileNameBuffer);
784 } else if (SourceFilenameTok.is(tok::less)) {
785 // This could be a path instead of just a name
786 FileNameBuffer.push_back('<');
787 SourceLocation End;
788 if (ConcatenateIncludeName(FileNameBuffer, End))
789 return; // Diagnostic already emitted
790 SourceFileName = FileNameBuffer.str();
791 } else {
792 Diag(Tok, diag::warn_pragma_include_alias_expected_filename);
793 return;
794 }
795 FileNameBuffer.clear();
796
797 // Now we expect a comma, followed by another include name
798 Lex(Tok);
799 if (Tok.isNot(tok::comma)) {
800 Diag(Tok, diag::warn_pragma_include_alias_expected) << ",";
801 return;
802 }
803
804 Token ReplaceFilenameTok;
805 CurPPLexer->LexIncludeFilename(ReplaceFilenameTok);
806 if (ReplaceFilenameTok.is(tok::eod)) {
807 // The diagnostic has already been handled
808 return;
809 }
810
811 StringRef ReplaceFileName;
812 if (ReplaceFilenameTok.is(tok::string_literal) ||
813 ReplaceFilenameTok.is(tok::angle_string_literal)) {
814 ReplaceFileName = getSpelling(ReplaceFilenameTok, FileNameBuffer);
815 } else if (ReplaceFilenameTok.is(tok::less)) {
816 // This could be a path instead of just a name
817 FileNameBuffer.push_back('<');
818 SourceLocation End;
819 if (ConcatenateIncludeName(FileNameBuffer, End))
820 return; // Diagnostic already emitted
821 ReplaceFileName = FileNameBuffer.str();
822 } else {
823 Diag(Tok, diag::warn_pragma_include_alias_expected_filename);
824 return;
825 }
826
827 // Finally, we expect the closing paren
828 Lex(Tok);
829 if (Tok.isNot(tok::r_paren)) {
830 Diag(Tok, diag::warn_pragma_include_alias_expected) << ")";
831 return;
832 }
833
834 // Now that we have the source and target filenames, we need to make sure
835 // they're both of the same type (angled vs non-angled)
836 StringRef OriginalSource = SourceFileName;
837
838 bool SourceIsAngled =
839 GetIncludeFilenameSpelling(SourceFilenameTok.getLocation(),
840 SourceFileName);
841 bool ReplaceIsAngled =
842 GetIncludeFilenameSpelling(ReplaceFilenameTok.getLocation(),
843 ReplaceFileName);
844 if (!SourceFileName.empty() && !ReplaceFileName.empty() &&
845 (SourceIsAngled != ReplaceIsAngled)) {
846 unsigned int DiagID;
847 if (SourceIsAngled)
848 DiagID = diag::warn_pragma_include_alias_mismatch_angle;
849 else
850 DiagID = diag::warn_pragma_include_alias_mismatch_quote;
851
852 Diag(SourceFilenameTok.getLocation(), DiagID)
853 << SourceFileName
854 << ReplaceFileName;
855
856 return;
857 }
858
859 // Now we can let the include handler know about this mapping
860 getHeaderSearchInfo().AddIncludeAlias(OriginalSource, ReplaceFileName);
861}
862
Chris Lattnerb694ba72006-07-02 22:41:36 +0000863/// AddPragmaHandler - Add the specified pragma handler to the preprocessor.
864/// If 'Namespace' is non-null, then it is a token required to exist on the
865/// pragma line before the pragma string starts, e.g. "STDC" or "GCC".
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000866void Preprocessor::AddPragmaHandler(StringRef Namespace,
Chris Lattnerb694ba72006-07-02 22:41:36 +0000867 PragmaHandler *Handler) {
868 PragmaNamespace *InsertNS = PragmaHandlers;
Mike Stump11289f42009-09-09 15:08:12 +0000869
Chris Lattnerb694ba72006-07-02 22:41:36 +0000870 // If this is specified to be in a namespace, step down into it.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000871 if (!Namespace.empty()) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000872 // If there is already a pragma handler with the name of this namespace,
873 // we either have an error (directive with the same name as a namespace) or
874 // we already have the namespace to insert into.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000875 if (PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace)) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000876 InsertNS = Existing->getIfNamespace();
877 assert(InsertNS != 0 && "Cannot have a pragma namespace and pragma"
878 " handler with the same name!");
879 } else {
880 // Otherwise, this namespace doesn't exist yet, create and insert the
881 // handler for it.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000882 InsertNS = new PragmaNamespace(Namespace);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000883 PragmaHandlers->AddPragma(InsertNS);
884 }
885 }
Mike Stump11289f42009-09-09 15:08:12 +0000886
Chris Lattnerb694ba72006-07-02 22:41:36 +0000887 // Check to make sure we don't already have a pragma for this identifier.
888 assert(!InsertNS->FindHandler(Handler->getName()) &&
889 "Pragma handler already exists for this identifier!");
890 InsertNS->AddPragma(Handler);
891}
892
Daniel Dunbar40596532008-10-04 19:17:46 +0000893/// RemovePragmaHandler - Remove the specific pragma handler from the
894/// preprocessor. If \arg Namespace is non-null, then it should be the
895/// namespace that \arg Handler was added to. It is an error to remove
896/// a handler that has not been registered.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000897void Preprocessor::RemovePragmaHandler(StringRef Namespace,
Daniel Dunbar40596532008-10-04 19:17:46 +0000898 PragmaHandler *Handler) {
899 PragmaNamespace *NS = PragmaHandlers;
Mike Stump11289f42009-09-09 15:08:12 +0000900
Daniel Dunbar40596532008-10-04 19:17:46 +0000901 // If this is specified to be in a namespace, step down into it.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000902 if (!Namespace.empty()) {
903 PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace);
Daniel Dunbar40596532008-10-04 19:17:46 +0000904 assert(Existing && "Namespace containing handler does not exist!");
905
906 NS = Existing->getIfNamespace();
907 assert(NS && "Invalid namespace, registered as a regular pragma handler!");
908 }
909
910 NS->RemovePragmaHandler(Handler);
Mike Stump11289f42009-09-09 15:08:12 +0000911
Daniel Dunbar40596532008-10-04 19:17:46 +0000912 // If this is a non-default namespace and it is now empty, remove
913 // it.
Argyrios Kyrtzidis7ce75262012-01-06 00:22:09 +0000914 if (NS != PragmaHandlers && NS->IsEmpty()) {
Daniel Dunbar40596532008-10-04 19:17:46 +0000915 PragmaHandlers->RemovePragmaHandler(NS);
Argyrios Kyrtzidis7ce75262012-01-06 00:22:09 +0000916 delete NS;
917 }
Daniel Dunbar40596532008-10-04 19:17:46 +0000918}
919
Peter Collingbourne3bffa522011-02-14 01:42:24 +0000920bool Preprocessor::LexOnOffSwitch(tok::OnOffSwitch &Result) {
921 Token Tok;
922 LexUnexpandedToken(Tok);
923
924 if (Tok.isNot(tok::identifier)) {
925 Diag(Tok, diag::ext_on_off_switch_syntax);
926 return true;
927 }
928 IdentifierInfo *II = Tok.getIdentifierInfo();
929 if (II->isStr("ON"))
930 Result = tok::OOS_ON;
931 else if (II->isStr("OFF"))
932 Result = tok::OOS_OFF;
933 else if (II->isStr("DEFAULT"))
934 Result = tok::OOS_DEFAULT;
935 else {
936 Diag(Tok, diag::ext_on_off_switch_syntax);
937 return true;
938 }
939
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000940 // Verify that this is followed by EOD.
Peter Collingbourne3bffa522011-02-14 01:42:24 +0000941 LexUnexpandedToken(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000942 if (Tok.isNot(tok::eod))
943 Diag(Tok, diag::ext_pragma_syntax_eod);
Peter Collingbourne3bffa522011-02-14 01:42:24 +0000944 return false;
945}
946
Chris Lattnerb694ba72006-07-02 22:41:36 +0000947namespace {
James Dennett18a6d792012-06-17 03:26:26 +0000948/// PragmaOnceHandler - "\#pragma once" marks the file as atomically included.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000949struct PragmaOnceHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000950 PragmaOnceHandler() : PragmaHandler("once") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000951 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
952 Token &OnceTok) {
Chris Lattnerce2ab6f2009-04-14 05:07:49 +0000953 PP.CheckEndOfDirective("pragma once");
Chris Lattnerb694ba72006-07-02 22:41:36 +0000954 PP.HandlePragmaOnce(OnceTok);
955 }
956};
957
James Dennett18a6d792012-06-17 03:26:26 +0000958/// PragmaMarkHandler - "\#pragma mark ..." is ignored by the compiler, and the
Chris Lattnerc2383312007-12-19 19:38:36 +0000959/// rest of the line is not lexed.
960struct PragmaMarkHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000961 PragmaMarkHandler() : PragmaHandler("mark") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000962 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
963 Token &MarkTok) {
Chris Lattnerc2383312007-12-19 19:38:36 +0000964 PP.HandlePragmaMark();
965 }
966};
967
James Dennett18a6d792012-06-17 03:26:26 +0000968/// PragmaPoisonHandler - "\#pragma poison x" marks x as not usable.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000969struct PragmaPoisonHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000970 PragmaPoisonHandler() : PragmaHandler("poison") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000971 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
972 Token &PoisonTok) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000973 PP.HandlePragmaPoison(PoisonTok);
974 }
975};
976
James Dennett18a6d792012-06-17 03:26:26 +0000977/// PragmaSystemHeaderHandler - "\#pragma system_header" marks the current file
Chris Lattnerc2383312007-12-19 19:38:36 +0000978/// as a system header, which silences warnings in it.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000979struct PragmaSystemHeaderHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000980 PragmaSystemHeaderHandler() : PragmaHandler("system_header") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000981 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
982 Token &SHToken) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000983 PP.HandlePragmaSystemHeader(SHToken);
Chris Lattnerce2ab6f2009-04-14 05:07:49 +0000984 PP.CheckEndOfDirective("pragma");
Chris Lattnerb694ba72006-07-02 22:41:36 +0000985 }
986};
987struct PragmaDependencyHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000988 PragmaDependencyHandler() : PragmaHandler("dependency") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000989 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
990 Token &DepToken) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000991 PP.HandlePragmaDependency(DepToken);
992 }
993};
Mike Stump11289f42009-09-09 15:08:12 +0000994
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000995struct PragmaDebugHandler : public PragmaHandler {
996 PragmaDebugHandler() : PragmaHandler("__debug") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000997 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
998 Token &DepToken) {
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000999 Token Tok;
1000 PP.LexUnexpandedToken(Tok);
1001 if (Tok.isNot(tok::identifier)) {
Douglas Gregor3cc26482010-08-30 15:15:34 +00001002 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Daniel Dunbarb8068c32010-07-28 15:40:33 +00001003 return;
1004 }
1005 IdentifierInfo *II = Tok.getIdentifierInfo();
1006
Daniel Dunbarf2cf3292010-08-17 22:32:48 +00001007 if (II->isStr("assert")) {
David Blaikie83d382b2011-09-23 05:06:16 +00001008 llvm_unreachable("This is an assertion!");
Daniel Dunbarb8068c32010-07-28 15:40:33 +00001009 } else if (II->isStr("crash")) {
David Blaikie5bd4c2a2012-08-21 18:56:49 +00001010 LLVM_BUILTIN_TRAP;
David Blaikie5d577a22012-06-29 22:03:56 +00001011 } else if (II->isStr("parser_crash")) {
1012 Token Crasher;
1013 Crasher.setKind(tok::annot_pragma_parser_crash);
1014 PP.EnterToken(Crasher);
Daniel Dunbarf2cf3292010-08-17 22:32:48 +00001015 } else if (II->isStr("llvm_fatal_error")) {
1016 llvm::report_fatal_error("#pragma clang __debug llvm_fatal_error");
1017 } else if (II->isStr("llvm_unreachable")) {
1018 llvm_unreachable("#pragma clang __debug llvm_unreachable");
1019 } else if (II->isStr("overflow_stack")) {
1020 DebugOverflowStack();
Daniel Dunbar211a7872010-08-18 23:09:23 +00001021 } else if (II->isStr("handle_crash")) {
1022 llvm::CrashRecoveryContext *CRC =llvm::CrashRecoveryContext::GetCurrent();
1023 if (CRC)
1024 CRC->HandleCrash();
Daniel Dunbarf2cf3292010-08-17 22:32:48 +00001025 } else {
1026 PP.Diag(Tok, diag::warn_pragma_debug_unexpected_command)
1027 << II->getName();
Daniel Dunbarb8068c32010-07-28 15:40:33 +00001028 }
1029 }
1030
Francois Pichet2e11f5d2011-05-25 16:15:03 +00001031// Disable MSVC warning about runtime stack overflow.
1032#ifdef _MSC_VER
1033 #pragma warning(disable : 4717)
1034#endif
Daniel Dunbarb8068c32010-07-28 15:40:33 +00001035 void DebugOverflowStack() {
1036 DebugOverflowStack();
1037 }
Francois Pichet2e11f5d2011-05-25 16:15:03 +00001038#ifdef _MSC_VER
1039 #pragma warning(default : 4717)
1040#endif
1041
Daniel Dunbarb8068c32010-07-28 15:40:33 +00001042};
1043
James Dennett18a6d792012-06-17 03:26:26 +00001044/// PragmaDiagnosticHandler - e.g. '\#pragma GCC diagnostic ignored "-Wformat"'
Chris Lattner504af112009-04-19 23:16:58 +00001045struct PragmaDiagnosticHandler : public PragmaHandler {
Douglas Gregor3bde9b12011-06-22 19:41:48 +00001046private:
1047 const char *Namespace;
Chris Lattnerfb42a182009-07-12 21:18:45 +00001048public:
Douglas Gregor3bde9b12011-06-22 19:41:48 +00001049 explicit PragmaDiagnosticHandler(const char *NS) :
1050 PragmaHandler("diagnostic"), Namespace(NS) {}
Douglas Gregorc7d65762010-09-09 22:45:38 +00001051 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1052 Token &DiagToken) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +00001053 SourceLocation DiagLoc = DiagToken.getLocation();
Chris Lattner504af112009-04-19 23:16:58 +00001054 Token Tok;
1055 PP.LexUnexpandedToken(Tok);
1056 if (Tok.isNot(tok::identifier)) {
Douglas Gregor3cc26482010-08-30 15:15:34 +00001057 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Chris Lattner504af112009-04-19 23:16:58 +00001058 return;
1059 }
1060 IdentifierInfo *II = Tok.getIdentifierInfo();
Douglas Gregor3bde9b12011-06-22 19:41:48 +00001061 PPCallbacks *Callbacks = PP.getPPCallbacks();
Mike Stump11289f42009-09-09 15:08:12 +00001062
Chris Lattner504af112009-04-19 23:16:58 +00001063 diag::Mapping Map;
1064 if (II->isStr("warning"))
1065 Map = diag::MAP_WARNING;
1066 else if (II->isStr("error"))
1067 Map = diag::MAP_ERROR;
1068 else if (II->isStr("ignored"))
1069 Map = diag::MAP_IGNORE;
1070 else if (II->isStr("fatal"))
1071 Map = diag::MAP_FATAL;
Douglas Gregor3cc26482010-08-30 15:15:34 +00001072 else if (II->isStr("pop")) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +00001073 if (!PP.getDiagnostics().popMappings(DiagLoc))
Douglas Gregor3cc26482010-08-30 15:15:34 +00001074 PP.Diag(Tok, diag::warn_pragma_diagnostic_cannot_pop);
Douglas Gregor3bde9b12011-06-22 19:41:48 +00001075 else if (Callbacks)
1076 Callbacks->PragmaDiagnosticPop(DiagLoc, Namespace);
Douglas Gregor3cc26482010-08-30 15:15:34 +00001077 return;
1078 } else if (II->isStr("push")) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +00001079 PP.getDiagnostics().pushMappings(DiagLoc);
Douglas Gregor3bde9b12011-06-22 19:41:48 +00001080 if (Callbacks)
1081 Callbacks->PragmaDiagnosticPush(DiagLoc, Namespace);
Chris Lattnerfb42a182009-07-12 21:18:45 +00001082 return;
1083 } else {
Douglas Gregor3cc26482010-08-30 15:15:34 +00001084 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Chris Lattner504af112009-04-19 23:16:58 +00001085 return;
1086 }
Mike Stump11289f42009-09-09 15:08:12 +00001087
Chris Lattner504af112009-04-19 23:16:58 +00001088 PP.LexUnexpandedToken(Tok);
1089
1090 // We need at least one string.
1091 if (Tok.isNot(tok::string_literal)) {
1092 PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token);
1093 return;
1094 }
Mike Stump11289f42009-09-09 15:08:12 +00001095
Chris Lattner504af112009-04-19 23:16:58 +00001096 // String concatenation allows multiple strings, which can even come from
1097 // macro expansion.
1098 // "foo " "bar" "Baz"
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001099 SmallVector<Token, 4> StrToks;
Chris Lattner504af112009-04-19 23:16:58 +00001100 while (Tok.is(tok::string_literal)) {
1101 StrToks.push_back(Tok);
1102 PP.LexUnexpandedToken(Tok);
1103 }
Mike Stump11289f42009-09-09 15:08:12 +00001104
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001105 if (Tok.isNot(tok::eod)) {
Chris Lattner504af112009-04-19 23:16:58 +00001106 PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token);
1107 return;
1108 }
Mike Stump11289f42009-09-09 15:08:12 +00001109
Chris Lattner504af112009-04-19 23:16:58 +00001110 // Concatenate and parse the strings.
1111 StringLiteralParser Literal(&StrToks[0], StrToks.size(), PP);
Douglas Gregorfb65e592011-07-27 05:40:30 +00001112 assert(Literal.isAscii() && "Didn't allow wide strings in");
Chris Lattner504af112009-04-19 23:16:58 +00001113 if (Literal.hadError)
1114 return;
1115 if (Literal.Pascal) {
Douglas Gregor3cc26482010-08-30 15:15:34 +00001116 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Chris Lattner504af112009-04-19 23:16:58 +00001117 return;
1118 }
Chris Lattnerfb42a182009-07-12 21:18:45 +00001119
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001120 StringRef WarningName(Literal.GetString());
Chris Lattner504af112009-04-19 23:16:58 +00001121
1122 if (WarningName.size() < 3 || WarningName[0] != '-' ||
1123 WarningName[1] != 'W') {
1124 PP.Diag(StrToks[0].getLocation(),
1125 diag::warn_pragma_diagnostic_invalid_option);
1126 return;
1127 }
Mike Stump11289f42009-09-09 15:08:12 +00001128
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +00001129 if (PP.getDiagnostics().setDiagnosticGroupMapping(WarningName.substr(2),
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +00001130 Map, DiagLoc))
Chris Lattner504af112009-04-19 23:16:58 +00001131 PP.Diag(StrToks[0].getLocation(),
1132 diag::warn_pragma_diagnostic_unknown_warning) << WarningName;
Douglas Gregor3bde9b12011-06-22 19:41:48 +00001133 else if (Callbacks)
1134 Callbacks->PragmaDiagnostic(DiagLoc, Namespace, Map, WarningName);
Chris Lattner504af112009-04-19 23:16:58 +00001135 }
1136};
Mike Stump11289f42009-09-09 15:08:12 +00001137
James Dennett18a6d792012-06-17 03:26:26 +00001138/// PragmaCommentHandler - "\#pragma comment ...".
Chris Lattner2ff698d2009-01-16 08:21:25 +00001139struct PragmaCommentHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001140 PragmaCommentHandler() : PragmaHandler("comment") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +00001141 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1142 Token &CommentTok) {
Chris Lattner2ff698d2009-01-16 08:21:25 +00001143 PP.HandlePragmaComment(CommentTok);
1144 }
1145};
Mike Stump11289f42009-09-09 15:08:12 +00001146
James Dennett18a6d792012-06-17 03:26:26 +00001147/// PragmaIncludeAliasHandler - "\#pragma include_alias("...")".
Aaron Ballman611306e2012-03-02 22:51:54 +00001148struct PragmaIncludeAliasHandler : public PragmaHandler {
1149 PragmaIncludeAliasHandler() : PragmaHandler("include_alias") {}
1150 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1151 Token &IncludeAliasTok) {
1152 PP.HandlePragmaIncludeAlias(IncludeAliasTok);
1153 }
1154};
1155
James Dennett18a6d792012-06-17 03:26:26 +00001156/// PragmaMessageHandler - "\#pragma message("...")".
Chris Lattner30c924b2010-06-26 17:11:39 +00001157struct PragmaMessageHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001158 PragmaMessageHandler() : PragmaHandler("message") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +00001159 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1160 Token &CommentTok) {
Chris Lattner30c924b2010-06-26 17:11:39 +00001161 PP.HandlePragmaMessage(CommentTok);
1162 }
1163};
1164
James Dennett18a6d792012-06-17 03:26:26 +00001165/// PragmaPushMacroHandler - "\#pragma push_macro" saves the value of the
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001166/// macro on the top of the stack.
1167struct PragmaPushMacroHandler : public PragmaHandler {
1168 PragmaPushMacroHandler() : PragmaHandler("push_macro") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +00001169 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1170 Token &PushMacroTok) {
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001171 PP.HandlePragmaPushMacro(PushMacroTok);
1172 }
1173};
1174
1175
James Dennett18a6d792012-06-17 03:26:26 +00001176/// PragmaPopMacroHandler - "\#pragma pop_macro" sets the value of the
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001177/// macro to the value on the top of the stack.
1178struct PragmaPopMacroHandler : public PragmaHandler {
1179 PragmaPopMacroHandler() : PragmaHandler("pop_macro") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +00001180 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1181 Token &PopMacroTok) {
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001182 PP.HandlePragmaPopMacro(PopMacroTok);
1183 }
1184};
1185
Chris Lattner958ee042009-04-19 21:20:35 +00001186// Pragma STDC implementations.
Chris Lattner02ef4e32009-04-19 21:50:08 +00001187
James Dennett18a6d792012-06-17 03:26:26 +00001188/// PragmaSTDC_FENV_ACCESSHandler - "\#pragma STDC FENV_ACCESS ...".
Chris Lattner958ee042009-04-19 21:20:35 +00001189struct PragmaSTDC_FENV_ACCESSHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001190 PragmaSTDC_FENV_ACCESSHandler() : PragmaHandler("FENV_ACCESS") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +00001191 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1192 Token &Tok) {
Peter Collingbourne3bffa522011-02-14 01:42:24 +00001193 tok::OnOffSwitch OOS;
1194 if (PP.LexOnOffSwitch(OOS))
1195 return;
1196 if (OOS == tok::OOS_ON)
Chris Lattnerdf222682009-04-19 21:55:32 +00001197 PP.Diag(Tok, diag::warn_stdc_fenv_access_not_supported);
Chris Lattner958ee042009-04-19 21:20:35 +00001198 }
1199};
Mike Stump11289f42009-09-09 15:08:12 +00001200
James Dennett18a6d792012-06-17 03:26:26 +00001201/// PragmaSTDC_CX_LIMITED_RANGEHandler - "\#pragma STDC CX_LIMITED_RANGE ...".
Chris Lattner958ee042009-04-19 21:20:35 +00001202struct PragmaSTDC_CX_LIMITED_RANGEHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001203 PragmaSTDC_CX_LIMITED_RANGEHandler()
1204 : PragmaHandler("CX_LIMITED_RANGE") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +00001205 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1206 Token &Tok) {
Peter Collingbourne3bffa522011-02-14 01:42:24 +00001207 tok::OnOffSwitch OOS;
1208 PP.LexOnOffSwitch(OOS);
Chris Lattner958ee042009-04-19 21:20:35 +00001209 }
1210};
Mike Stump11289f42009-09-09 15:08:12 +00001211
James Dennett18a6d792012-06-17 03:26:26 +00001212/// PragmaSTDC_UnknownHandler - "\#pragma STDC ...".
Chris Lattner958ee042009-04-19 21:20:35 +00001213struct PragmaSTDC_UnknownHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001214 PragmaSTDC_UnknownHandler() {}
Douglas Gregorc7d65762010-09-09 22:45:38 +00001215 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1216 Token &UnknownTok) {
Chris Lattner02ef4e32009-04-19 21:50:08 +00001217 // C99 6.10.6p2, unknown forms are not allowed.
Chris Lattnera0b1f762009-04-19 21:25:37 +00001218 PP.Diag(UnknownTok, diag::ext_stdc_pragma_ignored);
Chris Lattner958ee042009-04-19 21:20:35 +00001219 }
1220};
Mike Stump11289f42009-09-09 15:08:12 +00001221
John McCall32f5fe12011-09-30 05:12:12 +00001222/// PragmaARCCFCodeAuditedHandler -
James Dennett18a6d792012-06-17 03:26:26 +00001223/// \#pragma clang arc_cf_code_audited begin/end
John McCall32f5fe12011-09-30 05:12:12 +00001224struct PragmaARCCFCodeAuditedHandler : public PragmaHandler {
1225 PragmaARCCFCodeAuditedHandler() : PragmaHandler("arc_cf_code_audited") {}
1226 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1227 Token &NameTok) {
1228 SourceLocation Loc = NameTok.getLocation();
1229 bool IsBegin;
1230
1231 Token Tok;
1232
1233 // Lex the 'begin' or 'end'.
1234 PP.LexUnexpandedToken(Tok);
1235 const IdentifierInfo *BeginEnd = Tok.getIdentifierInfo();
1236 if (BeginEnd && BeginEnd->isStr("begin")) {
1237 IsBegin = true;
1238 } else if (BeginEnd && BeginEnd->isStr("end")) {
1239 IsBegin = false;
1240 } else {
1241 PP.Diag(Tok.getLocation(), diag::err_pp_arc_cf_code_audited_syntax);
1242 return;
1243 }
1244
1245 // Verify that this is followed by EOD.
1246 PP.LexUnexpandedToken(Tok);
1247 if (Tok.isNot(tok::eod))
1248 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
1249
1250 // The start location of the active audit.
1251 SourceLocation BeginLoc = PP.getPragmaARCCFCodeAuditedLoc();
1252
1253 // The start location we want after processing this.
1254 SourceLocation NewLoc;
1255
1256 if (IsBegin) {
1257 // Complain about attempts to re-enter an audit.
1258 if (BeginLoc.isValid()) {
1259 PP.Diag(Loc, diag::err_pp_double_begin_of_arc_cf_code_audited);
1260 PP.Diag(BeginLoc, diag::note_pragma_entered_here);
1261 }
1262 NewLoc = Loc;
1263 } else {
1264 // Complain about attempts to leave an audit that doesn't exist.
1265 if (!BeginLoc.isValid()) {
1266 PP.Diag(Loc, diag::err_pp_unmatched_end_of_arc_cf_code_audited);
1267 return;
1268 }
1269 NewLoc = SourceLocation();
1270 }
1271
1272 PP.setPragmaARCCFCodeAuditedLoc(NewLoc);
1273 }
1274};
1275
Chris Lattnerb694ba72006-07-02 22:41:36 +00001276} // end anonymous namespace
1277
1278
1279/// RegisterBuiltinPragmas - Install the standard preprocessor pragmas:
James Dennett18a6d792012-06-17 03:26:26 +00001280/// \#pragma GCC poison/system_header/dependency and \#pragma once.
Chris Lattnerb694ba72006-07-02 22:41:36 +00001281void Preprocessor::RegisterBuiltinPragmas() {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001282 AddPragmaHandler(new PragmaOnceHandler());
1283 AddPragmaHandler(new PragmaMarkHandler());
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001284 AddPragmaHandler(new PragmaPushMacroHandler());
1285 AddPragmaHandler(new PragmaPopMacroHandler());
Michael J. Spencera0a820f2010-09-27 06:19:02 +00001286 AddPragmaHandler(new PragmaMessageHandler());
Mike Stump11289f42009-09-09 15:08:12 +00001287
Chris Lattnerb61448d2009-05-12 18:21:11 +00001288 // #pragma GCC ...
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001289 AddPragmaHandler("GCC", new PragmaPoisonHandler());
1290 AddPragmaHandler("GCC", new PragmaSystemHeaderHandler());
1291 AddPragmaHandler("GCC", new PragmaDependencyHandler());
Douglas Gregor3bde9b12011-06-22 19:41:48 +00001292 AddPragmaHandler("GCC", new PragmaDiagnosticHandler("GCC"));
Chris Lattnerb61448d2009-05-12 18:21:11 +00001293 // #pragma clang ...
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001294 AddPragmaHandler("clang", new PragmaPoisonHandler());
1295 AddPragmaHandler("clang", new PragmaSystemHeaderHandler());
Daniel Dunbarb8068c32010-07-28 15:40:33 +00001296 AddPragmaHandler("clang", new PragmaDebugHandler());
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001297 AddPragmaHandler("clang", new PragmaDependencyHandler());
Douglas Gregor3bde9b12011-06-22 19:41:48 +00001298 AddPragmaHandler("clang", new PragmaDiagnosticHandler("clang"));
John McCall32f5fe12011-09-30 05:12:12 +00001299 AddPragmaHandler("clang", new PragmaARCCFCodeAuditedHandler());
Chris Lattnerb61448d2009-05-12 18:21:11 +00001300
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001301 AddPragmaHandler("STDC", new PragmaSTDC_FENV_ACCESSHandler());
1302 AddPragmaHandler("STDC", new PragmaSTDC_CX_LIMITED_RANGEHandler());
Chris Lattner958ee042009-04-19 21:20:35 +00001303 AddPragmaHandler("STDC", new PragmaSTDC_UnknownHandler());
Mike Stump11289f42009-09-09 15:08:12 +00001304
Chris Lattner2ff698d2009-01-16 08:21:25 +00001305 // MS extensions.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001306 if (LangOpts.MicrosoftExt) {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001307 AddPragmaHandler(new PragmaCommentHandler());
Aaron Ballman611306e2012-03-02 22:51:54 +00001308 AddPragmaHandler(new PragmaIncludeAliasHandler());
Chris Lattner30c924b2010-06-26 17:11:39 +00001309 }
Chris Lattnerb694ba72006-07-02 22:41:36 +00001310}