blob: 23d088a9fb23040137ab88cb6cfe449ac3eb296f [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 Lattnerb694ba72006-07-02 22:41:36 +000016#include "clang/Basic/FileManager.h"
17#include "clang/Basic/SourceManager.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000018#include "clang/Lex/HeaderSearch.h"
19#include "clang/Lex/LexDiagnostic.h"
20#include "clang/Lex/LiteralSupport.h"
21#include "clang/Lex/MacroInfo.h"
22#include "clang/Lex/Preprocessor.h"
Daniel 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
Chris Lattner2e155302006-07-03 05:34:41 +000046PragmaNamespace::~PragmaNamespace() {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000047 for (llvm::StringMap<PragmaHandler*>::iterator
48 I = Handlers.begin(), E = Handlers.end(); I != E; ++I)
49 delete I->second;
Chris Lattner2e155302006-07-03 05:34:41 +000050}
51
52/// FindHandler - Check to see if there is already a handler for the
53/// specified name. If not, return the handler for the null identifier if it
54/// exists, otherwise return null. If IgnoreNull is true (the default) then
55/// the null handler isn't returned on failure to match.
Chris Lattner0e62c1c2011-07-23 10:55:15 +000056PragmaHandler *PragmaNamespace::FindHandler(StringRef Name,
Chris Lattner2e155302006-07-03 05:34:41 +000057 bool IgnoreNull) const {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000058 if (PragmaHandler *Handler = Handlers.lookup(Name))
59 return Handler;
Chris Lattner0e62c1c2011-07-23 10:55:15 +000060 return IgnoreNull ? 0 : Handlers.lookup(StringRef());
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000061}
Mike Stump11289f42009-09-09 15:08:12 +000062
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000063void PragmaNamespace::AddPragma(PragmaHandler *Handler) {
64 assert(!Handlers.lookup(Handler->getName()) &&
65 "A handler with this name is already registered in this namespace");
66 llvm::StringMapEntry<PragmaHandler *> &Entry =
67 Handlers.GetOrCreateValue(Handler->getName());
68 Entry.setValue(Handler);
Chris Lattner2e155302006-07-03 05:34:41 +000069}
70
Daniel Dunbar40596532008-10-04 19:17:46 +000071void PragmaNamespace::RemovePragmaHandler(PragmaHandler *Handler) {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000072 assert(Handlers.lookup(Handler->getName()) &&
73 "Handler not registered in this namespace");
74 Handlers.erase(Handler->getName());
Daniel Dunbar40596532008-10-04 19:17:46 +000075}
76
Douglas Gregorc7d65762010-09-09 22:45:38 +000077void PragmaNamespace::HandlePragma(Preprocessor &PP,
78 PragmaIntroducerKind Introducer,
79 Token &Tok) {
Chris Lattnerb8761832006-06-24 21:31:03 +000080 // Read the 'namespace' that the directive is in, e.g. STDC. Do not macro
81 // expand it, the user can have a STDC #define, that should not affect this.
82 PP.LexUnexpandedToken(Tok);
Mike Stump11289f42009-09-09 15:08:12 +000083
Chris Lattnerb8761832006-06-24 21:31:03 +000084 // Get the handler for this token. If there is no handler, ignore the pragma.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000085 PragmaHandler *Handler
86 = FindHandler(Tok.getIdentifierInfo() ? Tok.getIdentifierInfo()->getName()
Chris Lattner0e62c1c2011-07-23 10:55:15 +000087 : StringRef(),
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000088 /*IgnoreNull=*/false);
Chris Lattner21656f22009-04-19 21:10:26 +000089 if (Handler == 0) {
90 PP.Diag(Tok, diag::warn_pragma_ignored);
91 return;
92 }
Mike Stump11289f42009-09-09 15:08:12 +000093
Chris Lattnerb8761832006-06-24 21:31:03 +000094 // Otherwise, pass it down.
Douglas Gregorc7d65762010-09-09 22:45:38 +000095 Handler->HandlePragma(PP, Introducer, Tok);
Chris Lattnerb8761832006-06-24 21:31:03 +000096}
Chris Lattnerb694ba72006-07-02 22:41:36 +000097
Chris Lattnerb694ba72006-07-02 22:41:36 +000098//===----------------------------------------------------------------------===//
99// Preprocessor Pragma Directive Handling.
100//===----------------------------------------------------------------------===//
101
James Dennett18a6d792012-06-17 03:26:26 +0000102/// HandlePragmaDirective - The "\#pragma" directive has been parsed. Lex the
Chris Lattnerb694ba72006-07-02 22:41:36 +0000103/// rest of the pragma, passing it to the registered pragma handlers.
Douglas Gregorc7d65762010-09-09 22:45:38 +0000104void Preprocessor::HandlePragmaDirective(unsigned Introducer) {
Jordan Rosede1a2922012-06-08 18:06:21 +0000105 if (!PragmasEnabled)
106 return;
107
Chris Lattnerb694ba72006-07-02 22:41:36 +0000108 ++NumPragma;
Mike Stump11289f42009-09-09 15:08:12 +0000109
Chris Lattnerb694ba72006-07-02 22:41:36 +0000110 // Invoke the first level of pragma handlers which reads the namespace id.
Chris Lattner146762e2007-07-20 16:59:19 +0000111 Token Tok;
Douglas Gregorc7d65762010-09-09 22:45:38 +0000112 PragmaHandlers->HandlePragma(*this, PragmaIntroducerKind(Introducer), Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000113
Chris Lattnerb694ba72006-07-02 22:41:36 +0000114 // If the pragma handler didn't read the rest of the line, consume it now.
Peter Collingbourne2c9f9662011-02-22 13:49:00 +0000115 if ((CurTokenLexer && CurTokenLexer->isParsingPreprocessorDirective())
116 || (CurPPLexer && CurPPLexer->ParsingPreprocessorDirective))
Chris Lattnerb694ba72006-07-02 22:41:36 +0000117 DiscardUntilEndOfDirective();
118}
119
Argyrios Kyrtzidisf1b64c62012-04-03 16:47:40 +0000120namespace {
121/// \brief Helper class for \see Preprocessor::Handle_Pragma.
122class LexingFor_PragmaRAII {
123 Preprocessor &PP;
124 bool InMacroArgPreExpansion;
125 bool Failed;
126 Token &OutTok;
127 Token PragmaTok;
128
129public:
130 LexingFor_PragmaRAII(Preprocessor &PP, bool InMacroArgPreExpansion,
131 Token &Tok)
132 : PP(PP), InMacroArgPreExpansion(InMacroArgPreExpansion),
133 Failed(false), OutTok(Tok) {
134 if (InMacroArgPreExpansion) {
135 PragmaTok = OutTok;
136 PP.EnableBacktrackAtThisPos();
137 }
138 }
139
140 ~LexingFor_PragmaRAII() {
141 if (InMacroArgPreExpansion) {
142 if (Failed) {
143 PP.CommitBacktrackedTokens();
144 } else {
145 PP.Backtrack();
146 OutTok = PragmaTok;
147 }
148 }
149 }
150
151 void failed() {
152 Failed = true;
153 }
154};
155}
156
Chris Lattnerb694ba72006-07-02 22:41:36 +0000157/// Handle_Pragma - Read a _Pragma directive, slice it up, process it, then
158/// return the first token after the directive. The _Pragma token has just
159/// been read into 'Tok'.
Chris Lattner146762e2007-07-20 16:59:19 +0000160void Preprocessor::Handle_Pragma(Token &Tok) {
Argyrios Kyrtzidisf1b64c62012-04-03 16:47:40 +0000161
162 // This works differently if we are pre-expanding a macro argument.
163 // In that case we don't actually "activate" the pragma now, we only lex it
164 // until we are sure it is lexically correct and then we backtrack so that
165 // we activate the pragma whenever we encounter the tokens again in the token
166 // stream. This ensures that we will activate it in the correct location
167 // or that we will ignore it if it never enters the token stream, e.g:
168 //
169 // #define EMPTY(x)
170 // #define INACTIVE(x) EMPTY(x)
171 // INACTIVE(_Pragma("clang diagnostic ignored \"-Wconversion\""))
172
173 LexingFor_PragmaRAII _PragmaLexing(*this, InMacroArgPreExpansion, Tok);
174
Chris Lattnerb694ba72006-07-02 22:41:36 +0000175 // Remember the pragma token location.
176 SourceLocation PragmaLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000177
Chris Lattnerb694ba72006-07-02 22:41:36 +0000178 // Read the '('.
179 Lex(Tok);
Chris Lattner907dfe92008-11-18 07:59:24 +0000180 if (Tok.isNot(tok::l_paren)) {
181 Diag(PragmaLoc, diag::err__Pragma_malformed);
Argyrios Kyrtzidisf1b64c62012-04-03 16:47:40 +0000182 return _PragmaLexing.failed();
Chris Lattner907dfe92008-11-18 07:59:24 +0000183 }
Chris Lattnerb694ba72006-07-02 22:41:36 +0000184
185 // Read the '"..."'.
186 Lex(Tok);
Chris Lattner907dfe92008-11-18 07:59:24 +0000187 if (Tok.isNot(tok::string_literal) && Tok.isNot(tok::wide_string_literal)) {
188 Diag(PragmaLoc, diag::err__Pragma_malformed);
Richard Smithd67aea22012-03-06 03:21:47 +0000189 // Skip this token, and the ')', if present.
190 if (Tok.isNot(tok::r_paren))
191 Lex(Tok);
192 if (Tok.is(tok::r_paren))
193 Lex(Tok);
Argyrios Kyrtzidisf1b64c62012-04-03 16:47:40 +0000194 return _PragmaLexing.failed();
Richard Smithd67aea22012-03-06 03:21:47 +0000195 }
196
197 if (Tok.hasUDSuffix()) {
198 Diag(Tok, diag::err_invalid_string_udl);
199 // Skip this token, and the ')', if present.
200 Lex(Tok);
201 if (Tok.is(tok::r_paren))
202 Lex(Tok);
Argyrios Kyrtzidisf1b64c62012-04-03 16:47:40 +0000203 return _PragmaLexing.failed();
Chris Lattner907dfe92008-11-18 07:59:24 +0000204 }
Mike Stump11289f42009-09-09 15:08:12 +0000205
Chris Lattnerb694ba72006-07-02 22:41:36 +0000206 // Remember the string.
Argyrios Kyrtzidisf1b64c62012-04-03 16:47:40 +0000207 Token StrTok = Tok;
Chris Lattnerb694ba72006-07-02 22:41:36 +0000208
209 // Read the ')'.
210 Lex(Tok);
Chris Lattner907dfe92008-11-18 07:59:24 +0000211 if (Tok.isNot(tok::r_paren)) {
212 Diag(PragmaLoc, diag::err__Pragma_malformed);
Argyrios Kyrtzidisf1b64c62012-04-03 16:47:40 +0000213 return _PragmaLexing.failed();
Chris Lattner907dfe92008-11-18 07:59:24 +0000214 }
Mike Stump11289f42009-09-09 15:08:12 +0000215
Argyrios Kyrtzidisf1b64c62012-04-03 16:47:40 +0000216 if (InMacroArgPreExpansion)
217 return;
218
Chris Lattner9dc9c202009-02-15 20:52:18 +0000219 SourceLocation RParenLoc = Tok.getLocation();
Argyrios Kyrtzidisf1b64c62012-04-03 16:47:40 +0000220 std::string StrVal = getSpelling(StrTok);
Mike Stump11289f42009-09-09 15:08:12 +0000221
Chris Lattner262d4e32009-01-16 18:59:23 +0000222 // The _Pragma is lexically sound. Destringize according to C99 6.10.9.1:
223 // "The string literal is destringized by deleting the L prefix, if present,
224 // deleting the leading and trailing double-quotes, replacing each escape
225 // sequence \" by a double-quote, and replacing each escape sequence \\ by a
226 // single backslash."
Chris Lattnerb694ba72006-07-02 22:41:36 +0000227 if (StrVal[0] == 'L') // Remove L prefix.
228 StrVal.erase(StrVal.begin());
229 assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
230 "Invalid string token!");
Mike Stump11289f42009-09-09 15:08:12 +0000231
Chris Lattnerb694ba72006-07-02 22:41:36 +0000232 // Remove the front quote, replacing it with a space, so that the pragma
233 // contents appear to have a space before them.
234 StrVal[0] = ' ';
Mike Stump11289f42009-09-09 15:08:12 +0000235
Chris Lattnerfa217bd2009-03-08 08:08:45 +0000236 // Replace the terminating quote with a \n.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000237 StrVal[StrVal.size()-1] = '\n';
Mike Stump11289f42009-09-09 15:08:12 +0000238
Chris Lattnerb694ba72006-07-02 22:41:36 +0000239 // Remove escaped quotes and escapes.
240 for (unsigned i = 0, e = StrVal.size(); i != e-1; ++i) {
241 if (StrVal[i] == '\\' &&
242 (StrVal[i+1] == '\\' || StrVal[i+1] == '"')) {
243 // \\ -> '\' and \" -> '"'.
244 StrVal.erase(StrVal.begin()+i);
245 --e;
246 }
247 }
John McCall89e925d2010-08-28 22:34:47 +0000248
Peter Collingbournef29ce972011-02-22 13:49:06 +0000249 // Plop the string (including the newline and trailing null) into a buffer
250 // where we can lex it.
251 Token TmpTok;
252 TmpTok.startToken();
Dmitri Gribenkob8e9e752012-09-24 21:07:17 +0000253 CreateString(StrVal, TmpTok);
Peter Collingbournef29ce972011-02-22 13:49:06 +0000254 SourceLocation TokLoc = TmpTok.getLocation();
255
256 // Make and enter a lexer object so that we lex and expand the tokens just
257 // like any others.
258 Lexer *TL = Lexer::Create_PragmaLexer(TokLoc, PragmaLoc, RParenLoc,
259 StrVal.size(), *this);
260
261 EnterSourceFileWithLexer(TL, 0);
262
263 // With everything set up, lex this as a #pragma directive.
264 HandlePragmaDirective(PIK__Pragma);
John McCall89e925d2010-08-28 22:34:47 +0000265
266 // Finally, return whatever came after the pragma directive.
267 return Lex(Tok);
268}
269
270/// HandleMicrosoft__pragma - Like Handle_Pragma except the pragma text
271/// is not enclosed within a string literal.
272void Preprocessor::HandleMicrosoft__pragma(Token &Tok) {
273 // Remember the pragma token location.
274 SourceLocation PragmaLoc = Tok.getLocation();
275
276 // Read the '('.
277 Lex(Tok);
278 if (Tok.isNot(tok::l_paren)) {
279 Diag(PragmaLoc, diag::err__Pragma_malformed);
280 return;
281 }
282
Peter Collingbournef29ce972011-02-22 13:49:06 +0000283 // Get the tokens enclosed within the __pragma(), as well as the final ')'.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000284 SmallVector<Token, 32> PragmaToks;
John McCall89e925d2010-08-28 22:34:47 +0000285 int NumParens = 0;
286 Lex(Tok);
287 while (Tok.isNot(tok::eof)) {
Peter Collingbournef29ce972011-02-22 13:49:06 +0000288 PragmaToks.push_back(Tok);
John McCall89e925d2010-08-28 22:34:47 +0000289 if (Tok.is(tok::l_paren))
290 NumParens++;
291 else if (Tok.is(tok::r_paren) && NumParens-- == 0)
292 break;
John McCall89e925d2010-08-28 22:34:47 +0000293 Lex(Tok);
294 }
295
John McCall49039d42010-08-29 01:09:54 +0000296 if (Tok.is(tok::eof)) {
297 Diag(PragmaLoc, diag::err_unterminated___pragma);
298 return;
299 }
300
Peter Collingbournef29ce972011-02-22 13:49:06 +0000301 PragmaToks.front().setFlag(Token::LeadingSpace);
John McCall89e925d2010-08-28 22:34:47 +0000302
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000303 // Replace the ')' with an EOD to mark the end of the pragma.
304 PragmaToks.back().setKind(tok::eod);
Peter Collingbournef29ce972011-02-22 13:49:06 +0000305
306 Token *TokArray = new Token[PragmaToks.size()];
307 std::copy(PragmaToks.begin(), PragmaToks.end(), TokArray);
308
309 // Push the tokens onto the stack.
310 EnterTokenStream(TokArray, PragmaToks.size(), true, true);
311
312 // With everything set up, lex this as a #pragma directive.
313 HandlePragmaDirective(PIK___pragma);
John McCall89e925d2010-08-28 22:34:47 +0000314
315 // Finally, return whatever came after the pragma directive.
316 return Lex(Tok);
317}
318
James Dennett18a6d792012-06-17 03:26:26 +0000319/// HandlePragmaOnce - Handle \#pragma once. OnceTok is the 'once'.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000320///
Chris Lattner146762e2007-07-20 16:59:19 +0000321void Preprocessor::HandlePragmaOnce(Token &OnceTok) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000322 if (isInPrimaryFile()) {
323 Diag(OnceTok, diag::pp_pragma_once_in_main_file);
324 return;
325 }
Mike Stump11289f42009-09-09 15:08:12 +0000326
Chris Lattnerb694ba72006-07-02 22:41:36 +0000327 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000328 // Mark the file as a once-only file now.
Chris Lattnerd32480d2009-01-17 06:22:33 +0000329 HeaderInfo.MarkFileIncludeOnce(getCurrentFileLexer()->getFileEntry());
Chris Lattnerb694ba72006-07-02 22:41:36 +0000330}
331
Chris Lattnerc2383312007-12-19 19:38:36 +0000332void Preprocessor::HandlePragmaMark() {
Ted Kremenek76c34412008-11-19 22:21:33 +0000333 assert(CurPPLexer && "No current lexer?");
Chris Lattnerd9efb6e2009-06-15 05:02:34 +0000334 if (CurLexer)
335 CurLexer->ReadToEndOfLine();
336 else
337 CurPTHLexer->DiscardToEndOfLine();
Chris Lattnerc2383312007-12-19 19:38:36 +0000338}
339
340
James Dennett18a6d792012-06-17 03:26:26 +0000341/// HandlePragmaPoison - Handle \#pragma GCC poison. PoisonTok is the 'poison'.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000342///
Chris Lattner146762e2007-07-20 16:59:19 +0000343void Preprocessor::HandlePragmaPoison(Token &PoisonTok) {
344 Token Tok;
Chris Lattner538d7f32006-07-20 04:31:52 +0000345
Chris Lattnerb694ba72006-07-02 22:41:36 +0000346 while (1) {
347 // Read the next token to poison. While doing this, pretend that we are
348 // skipping while reading the identifier to poison.
349 // This avoids errors on code like:
350 // #pragma GCC poison X
351 // #pragma GCC poison X
Ted Kremenek551c82a2008-11-18 01:12:54 +0000352 if (CurPPLexer) CurPPLexer->LexingRawMode = true;
Chris Lattnerb694ba72006-07-02 22:41:36 +0000353 LexUnexpandedToken(Tok);
Ted Kremenek551c82a2008-11-18 01:12:54 +0000354 if (CurPPLexer) CurPPLexer->LexingRawMode = false;
Mike Stump11289f42009-09-09 15:08:12 +0000355
Chris Lattnerb694ba72006-07-02 22:41:36 +0000356 // If we reached the end of line, we're done.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000357 if (Tok.is(tok::eod)) return;
Mike Stump11289f42009-09-09 15:08:12 +0000358
Chris Lattnerb694ba72006-07-02 22:41:36 +0000359 // Can only poison identifiers.
Abramo Bagnaraea4f7c72010-12-22 08:23:18 +0000360 if (Tok.isNot(tok::raw_identifier)) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000361 Diag(Tok, diag::err_pp_invalid_poison);
362 return;
363 }
Mike Stump11289f42009-09-09 15:08:12 +0000364
Chris Lattnercefc7682006-07-08 08:28:12 +0000365 // Look up the identifier info for the token. We disabled identifier lookup
366 // by saying we're skipping contents, so we need to do this manually.
367 IdentifierInfo *II = LookUpIdentifierInfo(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000368
Chris Lattnerb694ba72006-07-02 22:41:36 +0000369 // Already poisoned.
370 if (II->isPoisoned()) continue;
Mike Stump11289f42009-09-09 15:08:12 +0000371
Chris Lattnerb694ba72006-07-02 22:41:36 +0000372 // If this is a macro identifier, emit a warning.
Chris Lattner259716a2007-10-07 08:04:56 +0000373 if (II->hasMacroDefinition())
Chris Lattnerb694ba72006-07-02 22:41:36 +0000374 Diag(Tok, diag::pp_poisoning_existing_macro);
Mike Stump11289f42009-09-09 15:08:12 +0000375
Chris Lattnerb694ba72006-07-02 22:41:36 +0000376 // Finally, poison it!
377 II->setIsPoisoned();
Douglas Gregor935bc7a22011-10-27 09:33:13 +0000378 if (II->isFromAST())
379 II->setChangedSinceDeserialization();
Chris Lattnerb694ba72006-07-02 22:41:36 +0000380 }
381}
382
James Dennett18a6d792012-06-17 03:26:26 +0000383/// HandlePragmaSystemHeader - Implement \#pragma GCC system_header. We know
Chris Lattnerb694ba72006-07-02 22:41:36 +0000384/// that the whole directive has been parsed.
Chris Lattner146762e2007-07-20 16:59:19 +0000385void Preprocessor::HandlePragmaSystemHeader(Token &SysHeaderTok) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000386 if (isInPrimaryFile()) {
387 Diag(SysHeaderTok, diag::pp_pragma_sysheader_in_main_file);
388 return;
389 }
Mike Stump11289f42009-09-09 15:08:12 +0000390
Chris Lattnerb694ba72006-07-02 22:41:36 +0000391 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
Ted Kremenek300590b2008-11-20 01:45:11 +0000392 PreprocessorLexer *TheLexer = getCurrentFileLexer();
Mike Stump11289f42009-09-09 15:08:12 +0000393
Chris Lattnerb694ba72006-07-02 22:41:36 +0000394 // Mark the file as a system header.
Chris Lattnerd32480d2009-01-17 06:22:33 +0000395 HeaderInfo.MarkFileSystemHeader(TheLexer->getFileEntry());
Mike Stump11289f42009-09-09 15:08:12 +0000396
397
Chris Lattnerd9efb6e2009-06-15 05:02:34 +0000398 PresumedLoc PLoc = SourceMgr.getPresumedLoc(SysHeaderTok.getLocation());
Douglas Gregor453b0122010-11-12 07:15:47 +0000399 if (PLoc.isInvalid())
400 return;
401
Jay Foad9a6b0982011-06-21 15:13:30 +0000402 unsigned FilenameID = SourceMgr.getLineTableFilenameID(PLoc.getFilename());
Mike Stump11289f42009-09-09 15:08:12 +0000403
Chris Lattner3bdc7672011-05-22 22:10:16 +0000404 // Notify the client, if desired, that we are in a new source file.
405 if (Callbacks)
406 Callbacks->FileChanged(SysHeaderTok.getLocation(),
407 PPCallbacks::SystemHeaderPragma, SrcMgr::C_System);
408
Chris Lattnerd9efb6e2009-06-15 05:02:34 +0000409 // Emit a line marker. This will change any source locations from this point
410 // forward to realize they are in a system header.
411 // Create a line note with this information.
412 SourceMgr.AddLineNote(SysHeaderTok.getLocation(), PLoc.getLine(), FilenameID,
413 false, false, true, false);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000414}
415
James Dennett18a6d792012-06-17 03:26:26 +0000416/// HandlePragmaDependency - Handle \#pragma GCC dependency "foo" blah.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000417///
Chris Lattner146762e2007-07-20 16:59:19 +0000418void Preprocessor::HandlePragmaDependency(Token &DependencyTok) {
419 Token FilenameTok;
Ted Kremenek551c82a2008-11-18 01:12:54 +0000420 CurPPLexer->LexIncludeFilename(FilenameTok);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000421
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000422 // If the token kind is EOD, the error has already been diagnosed.
423 if (FilenameTok.is(tok::eod))
Chris Lattnerb694ba72006-07-02 22:41:36 +0000424 return;
Mike Stump11289f42009-09-09 15:08:12 +0000425
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000426 // Reserve a buffer to get the spelling.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000427 SmallString<128> FilenameBuffer;
Douglas Gregordc970f02010-03-16 22:30:13 +0000428 bool Invalid = false;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000429 StringRef Filename = getSpelling(FilenameTok, FilenameBuffer, &Invalid);
Douglas Gregordc970f02010-03-16 22:30:13 +0000430 if (Invalid)
431 return;
Mike Stump11289f42009-09-09 15:08:12 +0000432
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000433 bool isAngled =
434 GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000435 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
436 // error.
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000437 if (Filename.empty())
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000438 return;
Mike Stump11289f42009-09-09 15:08:12 +0000439
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000440 // Search include directories for this file.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000441 const DirectoryLookup *CurDir;
Douglas Gregor97eec242011-09-15 22:00:41 +0000442 const FileEntry *File = LookupFile(Filename, isAngled, 0, CurDir, NULL, NULL,
443 NULL);
Chris Lattner97b8e842008-11-18 08:02:48 +0000444 if (File == 0) {
Eli Friedman3781a362011-08-30 23:07:51 +0000445 if (!SuppressIncludeNotFoundError)
446 Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
Chris Lattner97b8e842008-11-18 08:02:48 +0000447 return;
448 }
Mike Stump11289f42009-09-09 15:08:12 +0000449
Chris Lattnerd32480d2009-01-17 06:22:33 +0000450 const FileEntry *CurFile = getCurrentFileLexer()->getFileEntry();
Chris Lattnerb694ba72006-07-02 22:41:36 +0000451
452 // If this file is older than the file it depends on, emit a diagnostic.
453 if (CurFile && CurFile->getModificationTime() < File->getModificationTime()) {
454 // Lex tokens at the end of the message and include them in the message.
455 std::string Message;
456 Lex(DependencyTok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000457 while (DependencyTok.isNot(tok::eod)) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000458 Message += getSpelling(DependencyTok) + " ";
459 Lex(DependencyTok);
460 }
Mike Stump11289f42009-09-09 15:08:12 +0000461
Chris Lattnerf0b04972010-09-05 23:16:09 +0000462 // Remove the trailing ' ' if present.
463 if (!Message.empty())
464 Message.erase(Message.end()-1);
Chris Lattner97b8e842008-11-18 08:02:48 +0000465 Diag(FilenameTok, diag::pp_out_of_date_dependency) << Message;
Chris Lattnerb694ba72006-07-02 22:41:36 +0000466 }
467}
468
James Dennett18a6d792012-06-17 03:26:26 +0000469/// \brief Handle the microsoft \#pragma comment extension.
470///
471/// The syntax is:
472/// \code
Dmitri Gribenko9ebd1612012-11-30 20:04:39 +0000473/// #pragma comment(linker, "foo")
James Dennett18a6d792012-06-17 03:26:26 +0000474/// \endcode
Chris Lattner2ff698d2009-01-16 08:21:25 +0000475/// 'linker' is one of five identifiers: compiler, exestr, lib, linker, user.
476/// "foo" is a string, which is fully macro expanded, and permits string
Gabor Greif31a082f2009-03-17 11:39:38 +0000477/// concatenation, embedded escape characters etc. See MSDN for more details.
Chris Lattner2ff698d2009-01-16 08:21:25 +0000478void Preprocessor::HandlePragmaComment(Token &Tok) {
479 SourceLocation CommentLoc = Tok.getLocation();
480 Lex(Tok);
481 if (Tok.isNot(tok::l_paren)) {
482 Diag(CommentLoc, diag::err_pragma_comment_malformed);
483 return;
484 }
Mike Stump11289f42009-09-09 15:08:12 +0000485
Chris Lattner2ff698d2009-01-16 08:21:25 +0000486 // Read the identifier.
487 Lex(Tok);
488 if (Tok.isNot(tok::identifier)) {
489 Diag(CommentLoc, diag::err_pragma_comment_malformed);
490 return;
491 }
Mike Stump11289f42009-09-09 15:08:12 +0000492
Chris Lattner2ff698d2009-01-16 08:21:25 +0000493 // Verify that this is one of the 5 whitelisted options.
494 // FIXME: warn that 'exestr' is deprecated.
495 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000496 if (!II->isStr("compiler") && !II->isStr("exestr") && !II->isStr("lib") &&
Chris Lattner2ff698d2009-01-16 08:21:25 +0000497 !II->isStr("linker") && !II->isStr("user")) {
498 Diag(Tok.getLocation(), diag::err_pragma_comment_unknown_kind);
499 return;
500 }
Mike Stump11289f42009-09-09 15:08:12 +0000501
Chris Lattner262d4e32009-01-16 18:59:23 +0000502 // Read the optional string if present.
Chris Lattner2ff698d2009-01-16 08:21:25 +0000503 Lex(Tok);
Chris Lattner262d4e32009-01-16 18:59:23 +0000504 std::string ArgumentString;
Andy Gibbs58905d22012-11-17 19:15:38 +0000505 if (Tok.is(tok::comma) && !LexStringLiteral(Tok, ArgumentString,
Andy Gibbsa8df57a2012-11-17 19:16:52 +0000506 "pragma comment",
Andy Gibbs58905d22012-11-17 19:15:38 +0000507 /*MacroExpansion=*/true))
508 return;
Mike Stump11289f42009-09-09 15:08:12 +0000509
Chris Lattner262d4e32009-01-16 18:59:23 +0000510 // FIXME: If the kind is "compiler" warn if the string is present (it is
511 // ignored).
512 // FIXME: 'lib' requires a comment string.
513 // FIXME: 'linker' requires a comment string, and has a specific list of
514 // things that are allowable.
Mike Stump11289f42009-09-09 15:08:12 +0000515
Chris Lattner2ff698d2009-01-16 08:21:25 +0000516 if (Tok.isNot(tok::r_paren)) {
517 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
518 return;
519 }
Chris Lattner262d4e32009-01-16 18:59:23 +0000520 Lex(Tok); // eat the r_paren.
Chris Lattner2ff698d2009-01-16 08:21:25 +0000521
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000522 if (Tok.isNot(tok::eod)) {
Chris Lattner2ff698d2009-01-16 08:21:25 +0000523 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
524 return;
525 }
Mike Stump11289f42009-09-09 15:08:12 +0000526
Chris Lattner262d4e32009-01-16 18:59:23 +0000527 // If the pragma is lexically sound, notify any interested PPCallbacks.
Chris Lattnerf49775d2009-01-16 19:01:46 +0000528 if (Callbacks)
529 Callbacks->PragmaComment(CommentLoc, II, ArgumentString);
Chris Lattner2ff698d2009-01-16 08:21:25 +0000530}
531
James Dennett18a6d792012-06-17 03:26:26 +0000532/// HandlePragmaMessage - Handle the microsoft and gcc \#pragma message
Michael J. Spencera0a820f2010-09-27 06:19:02 +0000533/// extension. The syntax is:
James Dennett18a6d792012-06-17 03:26:26 +0000534/// \code
Dmitri Gribenko9ebd1612012-11-30 20:04:39 +0000535/// #pragma message(string)
James Dennett18a6d792012-06-17 03:26:26 +0000536/// \endcode
Michael J. Spencera0a820f2010-09-27 06:19:02 +0000537/// OR, in GCC mode:
James Dennett18a6d792012-06-17 03:26:26 +0000538/// \code
Dmitri Gribenko9ebd1612012-11-30 20:04:39 +0000539/// #pragma message string
James Dennett18a6d792012-06-17 03:26:26 +0000540/// \endcode
Michael J. Spencera0a820f2010-09-27 06:19:02 +0000541/// string is a string, which is fully macro expanded, and permits string
542/// concatenation, embedded escape characters, etc... See MSDN for more details.
Chris Lattner30c924b2010-06-26 17:11:39 +0000543void Preprocessor::HandlePragmaMessage(Token &Tok) {
544 SourceLocation MessageLoc = Tok.getLocation();
545 Lex(Tok);
Michael J. Spencera0a820f2010-09-27 06:19:02 +0000546 bool ExpectClosingParen = false;
Michael J. Spencer4362a1c2010-09-27 06:34:47 +0000547 switch (Tok.getKind()) {
Michael J. Spencera0a820f2010-09-27 06:19:02 +0000548 case tok::l_paren:
549 // We have a MSVC style pragma message.
550 ExpectClosingParen = true;
551 // Read the string.
552 Lex(Tok);
553 break;
554 case tok::string_literal:
555 // We have a GCC style pragma message, and we just read the string.
556 break;
557 default:
Chris Lattner30c924b2010-06-26 17:11:39 +0000558 Diag(MessageLoc, diag::err_pragma_message_malformed);
559 return;
560 }
Chris Lattner2ff698d2009-01-16 08:21:25 +0000561
Andy Gibbs58905d22012-11-17 19:15:38 +0000562 std::string MessageString;
Andy Gibbsa8df57a2012-11-17 19:16:52 +0000563 if (!FinishLexStringLiteral(Tok, MessageString, "pragma message",
564 /*MacroExpansion=*/true))
Chris Lattner30c924b2010-06-26 17:11:39 +0000565 return;
Chris Lattner30c924b2010-06-26 17:11:39 +0000566
Michael J. Spencera0a820f2010-09-27 06:19:02 +0000567 if (ExpectClosingParen) {
568 if (Tok.isNot(tok::r_paren)) {
569 Diag(Tok.getLocation(), diag::err_pragma_message_malformed);
570 return;
571 }
572 Lex(Tok); // eat the r_paren.
Chris Lattner30c924b2010-06-26 17:11:39 +0000573 }
Chris Lattner30c924b2010-06-26 17:11:39 +0000574
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000575 if (Tok.isNot(tok::eod)) {
Chris Lattner30c924b2010-06-26 17:11:39 +0000576 Diag(Tok.getLocation(), diag::err_pragma_message_malformed);
577 return;
578 }
579
580 // Output the message.
581 Diag(MessageLoc, diag::warn_pragma_message) << MessageString;
582
583 // If the pragma is lexically sound, notify any interested PPCallbacks.
584 if (Callbacks)
585 Callbacks->PragmaMessage(MessageLoc, MessageString);
586}
Chris Lattner2ff698d2009-01-16 08:21:25 +0000587
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000588/// ParsePragmaPushOrPopMacro - Handle parsing of pragma push_macro/pop_macro.
589/// Return the IdentifierInfo* associated with the macro to push or pop.
590IdentifierInfo *Preprocessor::ParsePragmaPushOrPopMacro(Token &Tok) {
591 // Remember the pragma token location.
592 Token PragmaTok = Tok;
593
594 // Read the '('.
595 Lex(Tok);
596 if (Tok.isNot(tok::l_paren)) {
597 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
598 << getSpelling(PragmaTok);
599 return 0;
600 }
601
602 // Read the macro name string.
603 Lex(Tok);
604 if (Tok.isNot(tok::string_literal)) {
605 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
606 << getSpelling(PragmaTok);
607 return 0;
608 }
609
Richard Smithd67aea22012-03-06 03:21:47 +0000610 if (Tok.hasUDSuffix()) {
611 Diag(Tok, diag::err_invalid_string_udl);
612 return 0;
613 }
614
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000615 // Remember the macro string.
616 std::string StrVal = getSpelling(Tok);
617
618 // Read the ')'.
619 Lex(Tok);
620 if (Tok.isNot(tok::r_paren)) {
621 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
622 << getSpelling(PragmaTok);
623 return 0;
624 }
625
626 assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
627 "Invalid string token!");
628
629 // Create a Token from the string.
630 Token MacroTok;
631 MacroTok.startToken();
Abramo Bagnaraea4f7c72010-12-22 08:23:18 +0000632 MacroTok.setKind(tok::raw_identifier);
Dmitri Gribenkob8e9e752012-09-24 21:07:17 +0000633 CreateString(StringRef(&StrVal[1], StrVal.size() - 2), MacroTok);
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000634
635 // Get the IdentifierInfo of MacroToPushTok.
636 return LookUpIdentifierInfo(MacroTok);
637}
638
James Dennett18a6d792012-06-17 03:26:26 +0000639/// \brief Handle \#pragma push_macro.
640///
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000641/// The syntax is:
James Dennett18a6d792012-06-17 03:26:26 +0000642/// \code
Dmitri Gribenko9ebd1612012-11-30 20:04:39 +0000643/// #pragma push_macro("macro")
James Dennett18a6d792012-06-17 03:26:26 +0000644/// \endcode
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000645void Preprocessor::HandlePragmaPushMacro(Token &PushMacroTok) {
646 // Parse the pragma directive and get the macro IdentifierInfo*.
647 IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PushMacroTok);
648 if (!IdentInfo) return;
649
650 // Get the MacroInfo associated with IdentInfo.
651 MacroInfo *MI = getMacroInfo(IdentInfo);
652
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000653 if (MI) {
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000654 // Allow the original MacroInfo to be redefined later.
655 MI->setIsAllowRedefinitionsWithoutWarning(true);
656 }
657
658 // Push the cloned MacroInfo so we can retrieve it later.
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +0000659 PragmaPushMacroInfo[IdentInfo].push_back(MI);
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000660}
661
James Dennett18a6d792012-06-17 03:26:26 +0000662/// \brief Handle \#pragma pop_macro.
663///
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000664/// The syntax is:
James Dennett18a6d792012-06-17 03:26:26 +0000665/// \code
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000666/// #pragma pop_macro("macro")
James Dennett18a6d792012-06-17 03:26:26 +0000667/// \endcode
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000668void Preprocessor::HandlePragmaPopMacro(Token &PopMacroTok) {
669 SourceLocation MessageLoc = PopMacroTok.getLocation();
670
671 // Parse the pragma directive and get the macro IdentifierInfo*.
672 IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PopMacroTok);
673 if (!IdentInfo) return;
674
675 // Find the vector<MacroInfo*> associated with the macro.
676 llvm::DenseMap<IdentifierInfo*, std::vector<MacroInfo*> >::iterator iter =
677 PragmaPushMacroInfo.find(IdentInfo);
678 if (iter != PragmaPushMacroInfo.end()) {
Alexander Kornienko8b3f6232012-08-29 00:20:03 +0000679 // Forget the MacroInfo currently associated with IdentInfo.
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +0000680 if (MacroDirective *CurrentMD = getMacroDirective(IdentInfo)) {
681 if (CurrentMD->getInfo()->isWarnIfUnused())
682 WarnUnusedMacroLocs.erase(CurrentMD->getInfo()->getDefinitionLoc());
683 UndefineMacro(IdentInfo, CurrentMD, MessageLoc);
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000684 }
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000685
686 // Get the MacroInfo we want to reinstall.
687 MacroInfo *MacroToReInstall = iter->second.back();
688
Alexander Kornienkoc0b49282012-08-29 16:56:24 +0000689 if (MacroToReInstall) {
690 // Reinstall the previously pushed macro.
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +0000691 setMacroDirective(IdentInfo, MacroToReInstall, MessageLoc,
692 /*isImported=*/false);
Alexander Kornienkoc0b49282012-08-29 16:56:24 +0000693 } else if (IdentInfo->hasMacroDefinition()) {
694 clearMacroInfo(IdentInfo);
695 }
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000696
697 // Pop PragmaPushMacroInfo stack.
698 iter->second.pop_back();
699 if (iter->second.size() == 0)
700 PragmaPushMacroInfo.erase(iter);
701 } else {
702 Diag(MessageLoc, diag::warn_pragma_pop_macro_no_push)
703 << IdentInfo->getName();
704 }
705}
Chris Lattnerb694ba72006-07-02 22:41:36 +0000706
Aaron Ballman611306e2012-03-02 22:51:54 +0000707void Preprocessor::HandlePragmaIncludeAlias(Token &Tok) {
708 // We will either get a quoted filename or a bracketed filename, and we
709 // have to track which we got. The first filename is the source name,
710 // and the second name is the mapped filename. If the first is quoted,
711 // the second must be as well (cannot mix and match quotes and brackets).
Aaron Ballman611306e2012-03-02 22:51:54 +0000712
713 // Get the open paren
714 Lex(Tok);
715 if (Tok.isNot(tok::l_paren)) {
716 Diag(Tok, diag::warn_pragma_include_alias_expected) << "(";
717 return;
718 }
719
720 // We expect either a quoted string literal, or a bracketed name
721 Token SourceFilenameTok;
722 CurPPLexer->LexIncludeFilename(SourceFilenameTok);
723 if (SourceFilenameTok.is(tok::eod)) {
724 // The diagnostic has already been handled
725 return;
726 }
727
728 StringRef SourceFileName;
729 SmallString<128> FileNameBuffer;
730 if (SourceFilenameTok.is(tok::string_literal) ||
731 SourceFilenameTok.is(tok::angle_string_literal)) {
732 SourceFileName = getSpelling(SourceFilenameTok, FileNameBuffer);
733 } else if (SourceFilenameTok.is(tok::less)) {
734 // This could be a path instead of just a name
735 FileNameBuffer.push_back('<');
736 SourceLocation End;
737 if (ConcatenateIncludeName(FileNameBuffer, End))
738 return; // Diagnostic already emitted
739 SourceFileName = FileNameBuffer.str();
740 } else {
741 Diag(Tok, diag::warn_pragma_include_alias_expected_filename);
742 return;
743 }
744 FileNameBuffer.clear();
745
746 // Now we expect a comma, followed by another include name
747 Lex(Tok);
748 if (Tok.isNot(tok::comma)) {
749 Diag(Tok, diag::warn_pragma_include_alias_expected) << ",";
750 return;
751 }
752
753 Token ReplaceFilenameTok;
754 CurPPLexer->LexIncludeFilename(ReplaceFilenameTok);
755 if (ReplaceFilenameTok.is(tok::eod)) {
756 // The diagnostic has already been handled
757 return;
758 }
759
760 StringRef ReplaceFileName;
761 if (ReplaceFilenameTok.is(tok::string_literal) ||
762 ReplaceFilenameTok.is(tok::angle_string_literal)) {
763 ReplaceFileName = getSpelling(ReplaceFilenameTok, FileNameBuffer);
764 } else if (ReplaceFilenameTok.is(tok::less)) {
765 // This could be a path instead of just a name
766 FileNameBuffer.push_back('<');
767 SourceLocation End;
768 if (ConcatenateIncludeName(FileNameBuffer, End))
769 return; // Diagnostic already emitted
770 ReplaceFileName = FileNameBuffer.str();
771 } else {
772 Diag(Tok, diag::warn_pragma_include_alias_expected_filename);
773 return;
774 }
775
776 // Finally, we expect the closing paren
777 Lex(Tok);
778 if (Tok.isNot(tok::r_paren)) {
779 Diag(Tok, diag::warn_pragma_include_alias_expected) << ")";
780 return;
781 }
782
783 // Now that we have the source and target filenames, we need to make sure
784 // they're both of the same type (angled vs non-angled)
785 StringRef OriginalSource = SourceFileName;
786
787 bool SourceIsAngled =
788 GetIncludeFilenameSpelling(SourceFilenameTok.getLocation(),
789 SourceFileName);
790 bool ReplaceIsAngled =
791 GetIncludeFilenameSpelling(ReplaceFilenameTok.getLocation(),
792 ReplaceFileName);
793 if (!SourceFileName.empty() && !ReplaceFileName.empty() &&
794 (SourceIsAngled != ReplaceIsAngled)) {
795 unsigned int DiagID;
796 if (SourceIsAngled)
797 DiagID = diag::warn_pragma_include_alias_mismatch_angle;
798 else
799 DiagID = diag::warn_pragma_include_alias_mismatch_quote;
800
801 Diag(SourceFilenameTok.getLocation(), DiagID)
802 << SourceFileName
803 << ReplaceFileName;
804
805 return;
806 }
807
808 // Now we can let the include handler know about this mapping
809 getHeaderSearchInfo().AddIncludeAlias(OriginalSource, ReplaceFileName);
810}
811
Chris Lattnerb694ba72006-07-02 22:41:36 +0000812/// AddPragmaHandler - Add the specified pragma handler to the preprocessor.
813/// If 'Namespace' is non-null, then it is a token required to exist on the
814/// pragma line before the pragma string starts, e.g. "STDC" or "GCC".
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000815void Preprocessor::AddPragmaHandler(StringRef Namespace,
Chris Lattnerb694ba72006-07-02 22:41:36 +0000816 PragmaHandler *Handler) {
817 PragmaNamespace *InsertNS = PragmaHandlers;
Mike Stump11289f42009-09-09 15:08:12 +0000818
Chris Lattnerb694ba72006-07-02 22:41:36 +0000819 // If this is specified to be in a namespace, step down into it.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000820 if (!Namespace.empty()) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000821 // If there is already a pragma handler with the name of this namespace,
822 // we either have an error (directive with the same name as a namespace) or
823 // we already have the namespace to insert into.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000824 if (PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace)) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000825 InsertNS = Existing->getIfNamespace();
826 assert(InsertNS != 0 && "Cannot have a pragma namespace and pragma"
827 " handler with the same name!");
828 } else {
829 // Otherwise, this namespace doesn't exist yet, create and insert the
830 // handler for it.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000831 InsertNS = new PragmaNamespace(Namespace);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000832 PragmaHandlers->AddPragma(InsertNS);
833 }
834 }
Mike Stump11289f42009-09-09 15:08:12 +0000835
Chris Lattnerb694ba72006-07-02 22:41:36 +0000836 // Check to make sure we don't already have a pragma for this identifier.
837 assert(!InsertNS->FindHandler(Handler->getName()) &&
838 "Pragma handler already exists for this identifier!");
839 InsertNS->AddPragma(Handler);
840}
841
Daniel Dunbar40596532008-10-04 19:17:46 +0000842/// RemovePragmaHandler - Remove the specific pragma handler from the
843/// preprocessor. If \arg Namespace is non-null, then it should be the
844/// namespace that \arg Handler was added to. It is an error to remove
845/// a handler that has not been registered.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000846void Preprocessor::RemovePragmaHandler(StringRef Namespace,
Daniel Dunbar40596532008-10-04 19:17:46 +0000847 PragmaHandler *Handler) {
848 PragmaNamespace *NS = PragmaHandlers;
Mike Stump11289f42009-09-09 15:08:12 +0000849
Daniel Dunbar40596532008-10-04 19:17:46 +0000850 // If this is specified to be in a namespace, step down into it.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000851 if (!Namespace.empty()) {
852 PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace);
Daniel Dunbar40596532008-10-04 19:17:46 +0000853 assert(Existing && "Namespace containing handler does not exist!");
854
855 NS = Existing->getIfNamespace();
856 assert(NS && "Invalid namespace, registered as a regular pragma handler!");
857 }
858
859 NS->RemovePragmaHandler(Handler);
Mike Stump11289f42009-09-09 15:08:12 +0000860
Daniel Dunbar40596532008-10-04 19:17:46 +0000861 // If this is a non-default namespace and it is now empty, remove
862 // it.
Argyrios Kyrtzidis7ce75262012-01-06 00:22:09 +0000863 if (NS != PragmaHandlers && NS->IsEmpty()) {
Daniel Dunbar40596532008-10-04 19:17:46 +0000864 PragmaHandlers->RemovePragmaHandler(NS);
Argyrios Kyrtzidis7ce75262012-01-06 00:22:09 +0000865 delete NS;
866 }
Daniel Dunbar40596532008-10-04 19:17:46 +0000867}
868
Peter Collingbourne3bffa522011-02-14 01:42:24 +0000869bool Preprocessor::LexOnOffSwitch(tok::OnOffSwitch &Result) {
870 Token Tok;
871 LexUnexpandedToken(Tok);
872
873 if (Tok.isNot(tok::identifier)) {
874 Diag(Tok, diag::ext_on_off_switch_syntax);
875 return true;
876 }
877 IdentifierInfo *II = Tok.getIdentifierInfo();
878 if (II->isStr("ON"))
879 Result = tok::OOS_ON;
880 else if (II->isStr("OFF"))
881 Result = tok::OOS_OFF;
882 else if (II->isStr("DEFAULT"))
883 Result = tok::OOS_DEFAULT;
884 else {
885 Diag(Tok, diag::ext_on_off_switch_syntax);
886 return true;
887 }
888
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000889 // Verify that this is followed by EOD.
Peter Collingbourne3bffa522011-02-14 01:42:24 +0000890 LexUnexpandedToken(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000891 if (Tok.isNot(tok::eod))
892 Diag(Tok, diag::ext_pragma_syntax_eod);
Peter Collingbourne3bffa522011-02-14 01:42:24 +0000893 return false;
894}
895
Chris Lattnerb694ba72006-07-02 22:41:36 +0000896namespace {
James Dennett18a6d792012-06-17 03:26:26 +0000897/// PragmaOnceHandler - "\#pragma once" marks the file as atomically included.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000898struct PragmaOnceHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000899 PragmaOnceHandler() : PragmaHandler("once") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000900 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
901 Token &OnceTok) {
Chris Lattnerce2ab6f2009-04-14 05:07:49 +0000902 PP.CheckEndOfDirective("pragma once");
Chris Lattnerb694ba72006-07-02 22:41:36 +0000903 PP.HandlePragmaOnce(OnceTok);
904 }
905};
906
James Dennett18a6d792012-06-17 03:26:26 +0000907/// PragmaMarkHandler - "\#pragma mark ..." is ignored by the compiler, and the
Chris Lattnerc2383312007-12-19 19:38:36 +0000908/// rest of the line is not lexed.
909struct PragmaMarkHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000910 PragmaMarkHandler() : PragmaHandler("mark") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000911 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
912 Token &MarkTok) {
Chris Lattnerc2383312007-12-19 19:38:36 +0000913 PP.HandlePragmaMark();
914 }
915};
916
James Dennett18a6d792012-06-17 03:26:26 +0000917/// PragmaPoisonHandler - "\#pragma poison x" marks x as not usable.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000918struct PragmaPoisonHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000919 PragmaPoisonHandler() : PragmaHandler("poison") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000920 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
921 Token &PoisonTok) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000922 PP.HandlePragmaPoison(PoisonTok);
923 }
924};
925
James Dennett18a6d792012-06-17 03:26:26 +0000926/// PragmaSystemHeaderHandler - "\#pragma system_header" marks the current file
Chris Lattnerc2383312007-12-19 19:38:36 +0000927/// as a system header, which silences warnings in it.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000928struct PragmaSystemHeaderHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000929 PragmaSystemHeaderHandler() : PragmaHandler("system_header") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000930 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
931 Token &SHToken) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000932 PP.HandlePragmaSystemHeader(SHToken);
Chris Lattnerce2ab6f2009-04-14 05:07:49 +0000933 PP.CheckEndOfDirective("pragma");
Chris Lattnerb694ba72006-07-02 22:41:36 +0000934 }
935};
936struct PragmaDependencyHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000937 PragmaDependencyHandler() : PragmaHandler("dependency") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000938 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
939 Token &DepToken) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000940 PP.HandlePragmaDependency(DepToken);
941 }
942};
Mike Stump11289f42009-09-09 15:08:12 +0000943
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000944struct PragmaDebugHandler : public PragmaHandler {
945 PragmaDebugHandler() : PragmaHandler("__debug") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000946 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
947 Token &DepToken) {
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000948 Token Tok;
949 PP.LexUnexpandedToken(Tok);
950 if (Tok.isNot(tok::identifier)) {
Douglas Gregor3cc26482010-08-30 15:15:34 +0000951 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000952 return;
953 }
954 IdentifierInfo *II = Tok.getIdentifierInfo();
955
Daniel Dunbarf2cf3292010-08-17 22:32:48 +0000956 if (II->isStr("assert")) {
David Blaikie83d382b2011-09-23 05:06:16 +0000957 llvm_unreachable("This is an assertion!");
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000958 } else if (II->isStr("crash")) {
David Blaikie5bd4c2a2012-08-21 18:56:49 +0000959 LLVM_BUILTIN_TRAP;
David Blaikie5d577a22012-06-29 22:03:56 +0000960 } else if (II->isStr("parser_crash")) {
961 Token Crasher;
962 Crasher.setKind(tok::annot_pragma_parser_crash);
963 PP.EnterToken(Crasher);
Daniel Dunbarf2cf3292010-08-17 22:32:48 +0000964 } else if (II->isStr("llvm_fatal_error")) {
965 llvm::report_fatal_error("#pragma clang __debug llvm_fatal_error");
966 } else if (II->isStr("llvm_unreachable")) {
967 llvm_unreachable("#pragma clang __debug llvm_unreachable");
968 } else if (II->isStr("overflow_stack")) {
969 DebugOverflowStack();
Daniel Dunbar211a7872010-08-18 23:09:23 +0000970 } else if (II->isStr("handle_crash")) {
971 llvm::CrashRecoveryContext *CRC =llvm::CrashRecoveryContext::GetCurrent();
972 if (CRC)
973 CRC->HandleCrash();
Daniel Dunbarf2cf3292010-08-17 22:32:48 +0000974 } else {
975 PP.Diag(Tok, diag::warn_pragma_debug_unexpected_command)
976 << II->getName();
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000977 }
978 }
979
Francois Pichet2e11f5d2011-05-25 16:15:03 +0000980// Disable MSVC warning about runtime stack overflow.
981#ifdef _MSC_VER
982 #pragma warning(disable : 4717)
983#endif
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000984 void DebugOverflowStack() {
985 DebugOverflowStack();
986 }
Francois Pichet2e11f5d2011-05-25 16:15:03 +0000987#ifdef _MSC_VER
988 #pragma warning(default : 4717)
989#endif
990
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000991};
992
James Dennett18a6d792012-06-17 03:26:26 +0000993/// PragmaDiagnosticHandler - e.g. '\#pragma GCC diagnostic ignored "-Wformat"'
Chris Lattner504af112009-04-19 23:16:58 +0000994struct PragmaDiagnosticHandler : public PragmaHandler {
Douglas Gregor3bde9b12011-06-22 19:41:48 +0000995private:
996 const char *Namespace;
Chris Lattnerfb42a182009-07-12 21:18:45 +0000997public:
Douglas Gregor3bde9b12011-06-22 19:41:48 +0000998 explicit PragmaDiagnosticHandler(const char *NS) :
999 PragmaHandler("diagnostic"), Namespace(NS) {}
Douglas Gregorc7d65762010-09-09 22:45:38 +00001000 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1001 Token &DiagToken) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +00001002 SourceLocation DiagLoc = DiagToken.getLocation();
Chris Lattner504af112009-04-19 23:16:58 +00001003 Token Tok;
1004 PP.LexUnexpandedToken(Tok);
1005 if (Tok.isNot(tok::identifier)) {
Douglas Gregor3cc26482010-08-30 15:15:34 +00001006 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Chris Lattner504af112009-04-19 23:16:58 +00001007 return;
1008 }
1009 IdentifierInfo *II = Tok.getIdentifierInfo();
Douglas Gregor3bde9b12011-06-22 19:41:48 +00001010 PPCallbacks *Callbacks = PP.getPPCallbacks();
Mike Stump11289f42009-09-09 15:08:12 +00001011
Chris Lattner504af112009-04-19 23:16:58 +00001012 diag::Mapping Map;
1013 if (II->isStr("warning"))
1014 Map = diag::MAP_WARNING;
1015 else if (II->isStr("error"))
1016 Map = diag::MAP_ERROR;
1017 else if (II->isStr("ignored"))
1018 Map = diag::MAP_IGNORE;
1019 else if (II->isStr("fatal"))
1020 Map = diag::MAP_FATAL;
Douglas Gregor3cc26482010-08-30 15:15:34 +00001021 else if (II->isStr("pop")) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +00001022 if (!PP.getDiagnostics().popMappings(DiagLoc))
Douglas Gregor3cc26482010-08-30 15:15:34 +00001023 PP.Diag(Tok, diag::warn_pragma_diagnostic_cannot_pop);
Douglas Gregor3bde9b12011-06-22 19:41:48 +00001024 else if (Callbacks)
1025 Callbacks->PragmaDiagnosticPop(DiagLoc, Namespace);
Douglas Gregor3cc26482010-08-30 15:15:34 +00001026 return;
1027 } else if (II->isStr("push")) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +00001028 PP.getDiagnostics().pushMappings(DiagLoc);
Douglas Gregor3bde9b12011-06-22 19:41:48 +00001029 if (Callbacks)
1030 Callbacks->PragmaDiagnosticPush(DiagLoc, Namespace);
Chris Lattnerfb42a182009-07-12 21:18:45 +00001031 return;
1032 } else {
Douglas Gregor3cc26482010-08-30 15:15:34 +00001033 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Chris Lattner504af112009-04-19 23:16:58 +00001034 return;
1035 }
Mike Stump11289f42009-09-09 15:08:12 +00001036
Chris Lattner504af112009-04-19 23:16:58 +00001037 PP.LexUnexpandedToken(Tok);
Andy Gibbs58905d22012-11-17 19:15:38 +00001038 SourceLocation StringLoc = Tok.getLocation();
Chris Lattner504af112009-04-19 23:16:58 +00001039
Andy Gibbs58905d22012-11-17 19:15:38 +00001040 std::string WarningName;
Andy Gibbsa8df57a2012-11-17 19:16:52 +00001041 if (!PP.FinishLexStringLiteral(Tok, WarningName, "pragma diagnostic",
1042 /*MacroExpansion=*/false))
Chris Lattner504af112009-04-19 23:16:58 +00001043 return;
Mike Stump11289f42009-09-09 15:08:12 +00001044
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001045 if (Tok.isNot(tok::eod)) {
Chris Lattner504af112009-04-19 23:16:58 +00001046 PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token);
1047 return;
1048 }
Mike Stump11289f42009-09-09 15:08:12 +00001049
Chris Lattner504af112009-04-19 23:16:58 +00001050 if (WarningName.size() < 3 || WarningName[0] != '-' ||
1051 WarningName[1] != 'W') {
Andy Gibbs58905d22012-11-17 19:15:38 +00001052 PP.Diag(StringLoc, diag::warn_pragma_diagnostic_invalid_option);
Chris Lattner504af112009-04-19 23:16:58 +00001053 return;
1054 }
Mike Stump11289f42009-09-09 15:08:12 +00001055
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +00001056 if (PP.getDiagnostics().setDiagnosticGroupMapping(WarningName.substr(2),
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +00001057 Map, DiagLoc))
Andy Gibbs58905d22012-11-17 19:15:38 +00001058 PP.Diag(StringLoc, diag::warn_pragma_diagnostic_unknown_warning)
1059 << WarningName;
Douglas Gregor3bde9b12011-06-22 19:41:48 +00001060 else if (Callbacks)
1061 Callbacks->PragmaDiagnostic(DiagLoc, Namespace, Map, WarningName);
Chris Lattner504af112009-04-19 23:16:58 +00001062 }
1063};
Mike Stump11289f42009-09-09 15:08:12 +00001064
James Dennett18a6d792012-06-17 03:26:26 +00001065/// PragmaCommentHandler - "\#pragma comment ...".
Chris Lattner2ff698d2009-01-16 08:21:25 +00001066struct PragmaCommentHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001067 PragmaCommentHandler() : PragmaHandler("comment") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +00001068 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1069 Token &CommentTok) {
Chris Lattner2ff698d2009-01-16 08:21:25 +00001070 PP.HandlePragmaComment(CommentTok);
1071 }
1072};
Mike Stump11289f42009-09-09 15:08:12 +00001073
James Dennett18a6d792012-06-17 03:26:26 +00001074/// PragmaIncludeAliasHandler - "\#pragma include_alias("...")".
Aaron Ballman611306e2012-03-02 22:51:54 +00001075struct PragmaIncludeAliasHandler : public PragmaHandler {
1076 PragmaIncludeAliasHandler() : PragmaHandler("include_alias") {}
1077 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1078 Token &IncludeAliasTok) {
1079 PP.HandlePragmaIncludeAlias(IncludeAliasTok);
1080 }
1081};
1082
James Dennett18a6d792012-06-17 03:26:26 +00001083/// PragmaMessageHandler - "\#pragma message("...")".
Chris Lattner30c924b2010-06-26 17:11:39 +00001084struct PragmaMessageHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001085 PragmaMessageHandler() : PragmaHandler("message") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +00001086 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1087 Token &CommentTok) {
Chris Lattner30c924b2010-06-26 17:11:39 +00001088 PP.HandlePragmaMessage(CommentTok);
1089 }
1090};
1091
James Dennett18a6d792012-06-17 03:26:26 +00001092/// PragmaPushMacroHandler - "\#pragma push_macro" saves the value of the
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001093/// macro on the top of the stack.
1094struct PragmaPushMacroHandler : public PragmaHandler {
1095 PragmaPushMacroHandler() : PragmaHandler("push_macro") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +00001096 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1097 Token &PushMacroTok) {
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001098 PP.HandlePragmaPushMacro(PushMacroTok);
1099 }
1100};
1101
1102
James Dennett18a6d792012-06-17 03:26:26 +00001103/// PragmaPopMacroHandler - "\#pragma pop_macro" sets the value of the
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001104/// macro to the value on the top of the stack.
1105struct PragmaPopMacroHandler : public PragmaHandler {
1106 PragmaPopMacroHandler() : PragmaHandler("pop_macro") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +00001107 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1108 Token &PopMacroTok) {
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001109 PP.HandlePragmaPopMacro(PopMacroTok);
1110 }
1111};
1112
Chris Lattner958ee042009-04-19 21:20:35 +00001113// Pragma STDC implementations.
Chris Lattner02ef4e32009-04-19 21:50:08 +00001114
James Dennett18a6d792012-06-17 03:26:26 +00001115/// PragmaSTDC_FENV_ACCESSHandler - "\#pragma STDC FENV_ACCESS ...".
Chris Lattner958ee042009-04-19 21:20:35 +00001116struct PragmaSTDC_FENV_ACCESSHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001117 PragmaSTDC_FENV_ACCESSHandler() : PragmaHandler("FENV_ACCESS") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +00001118 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1119 Token &Tok) {
Peter Collingbourne3bffa522011-02-14 01:42:24 +00001120 tok::OnOffSwitch OOS;
1121 if (PP.LexOnOffSwitch(OOS))
1122 return;
1123 if (OOS == tok::OOS_ON)
Chris Lattnerdf222682009-04-19 21:55:32 +00001124 PP.Diag(Tok, diag::warn_stdc_fenv_access_not_supported);
Chris Lattner958ee042009-04-19 21:20:35 +00001125 }
1126};
Mike Stump11289f42009-09-09 15:08:12 +00001127
James Dennett18a6d792012-06-17 03:26:26 +00001128/// PragmaSTDC_CX_LIMITED_RANGEHandler - "\#pragma STDC CX_LIMITED_RANGE ...".
Chris Lattner958ee042009-04-19 21:20:35 +00001129struct PragmaSTDC_CX_LIMITED_RANGEHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001130 PragmaSTDC_CX_LIMITED_RANGEHandler()
1131 : PragmaHandler("CX_LIMITED_RANGE") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +00001132 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1133 Token &Tok) {
Peter Collingbourne3bffa522011-02-14 01:42:24 +00001134 tok::OnOffSwitch OOS;
1135 PP.LexOnOffSwitch(OOS);
Chris Lattner958ee042009-04-19 21:20:35 +00001136 }
1137};
Mike Stump11289f42009-09-09 15:08:12 +00001138
James Dennett18a6d792012-06-17 03:26:26 +00001139/// PragmaSTDC_UnknownHandler - "\#pragma STDC ...".
Chris Lattner958ee042009-04-19 21:20:35 +00001140struct PragmaSTDC_UnknownHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001141 PragmaSTDC_UnknownHandler() {}
Douglas Gregorc7d65762010-09-09 22:45:38 +00001142 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1143 Token &UnknownTok) {
Chris Lattner02ef4e32009-04-19 21:50:08 +00001144 // C99 6.10.6p2, unknown forms are not allowed.
Chris Lattnera0b1f762009-04-19 21:25:37 +00001145 PP.Diag(UnknownTok, diag::ext_stdc_pragma_ignored);
Chris Lattner958ee042009-04-19 21:20:35 +00001146 }
1147};
Mike Stump11289f42009-09-09 15:08:12 +00001148
John McCall32f5fe12011-09-30 05:12:12 +00001149/// PragmaARCCFCodeAuditedHandler -
James Dennett18a6d792012-06-17 03:26:26 +00001150/// \#pragma clang arc_cf_code_audited begin/end
John McCall32f5fe12011-09-30 05:12:12 +00001151struct PragmaARCCFCodeAuditedHandler : public PragmaHandler {
1152 PragmaARCCFCodeAuditedHandler() : PragmaHandler("arc_cf_code_audited") {}
1153 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1154 Token &NameTok) {
1155 SourceLocation Loc = NameTok.getLocation();
1156 bool IsBegin;
1157
1158 Token Tok;
1159
1160 // Lex the 'begin' or 'end'.
1161 PP.LexUnexpandedToken(Tok);
1162 const IdentifierInfo *BeginEnd = Tok.getIdentifierInfo();
1163 if (BeginEnd && BeginEnd->isStr("begin")) {
1164 IsBegin = true;
1165 } else if (BeginEnd && BeginEnd->isStr("end")) {
1166 IsBegin = false;
1167 } else {
1168 PP.Diag(Tok.getLocation(), diag::err_pp_arc_cf_code_audited_syntax);
1169 return;
1170 }
1171
1172 // Verify that this is followed by EOD.
1173 PP.LexUnexpandedToken(Tok);
1174 if (Tok.isNot(tok::eod))
1175 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
1176
1177 // The start location of the active audit.
1178 SourceLocation BeginLoc = PP.getPragmaARCCFCodeAuditedLoc();
1179
1180 // The start location we want after processing this.
1181 SourceLocation NewLoc;
1182
1183 if (IsBegin) {
1184 // Complain about attempts to re-enter an audit.
1185 if (BeginLoc.isValid()) {
1186 PP.Diag(Loc, diag::err_pp_double_begin_of_arc_cf_code_audited);
1187 PP.Diag(BeginLoc, diag::note_pragma_entered_here);
1188 }
1189 NewLoc = Loc;
1190 } else {
1191 // Complain about attempts to leave an audit that doesn't exist.
1192 if (!BeginLoc.isValid()) {
1193 PP.Diag(Loc, diag::err_pp_unmatched_end_of_arc_cf_code_audited);
1194 return;
1195 }
1196 NewLoc = SourceLocation();
1197 }
1198
1199 PP.setPragmaARCCFCodeAuditedLoc(NewLoc);
1200 }
1201};
1202
Aaron Ballman406ea512012-11-30 19:52:30 +00001203 /// \brief Handle "\#pragma region [...]"
1204 ///
1205 /// The syntax is
1206 /// \code
Dmitri Gribenko9ebd1612012-11-30 20:04:39 +00001207 /// #pragma region [optional name]
1208 /// #pragma endregion [optional comment]
Aaron Ballman406ea512012-11-30 19:52:30 +00001209 /// \endcode
1210 ///
1211 /// \note This is
1212 /// <a href="http://msdn.microsoft.com/en-us/library/b6xkz944(v=vs.80).aspx">editor-only</a>
1213 /// pragma, just skipped by compiler.
1214 struct PragmaRegionHandler : public PragmaHandler {
1215 PragmaRegionHandler(const char *pragma) : PragmaHandler(pragma) { }
1216
1217 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1218 Token &NameTok) {
1219 // #pragma region: endregion matches can be verified
1220 // __pragma(region): no sense, but ignored by msvc
1221 // _Pragma is not valid for MSVC, but there isn't any point
1222 // to handle a _Pragma differently.
1223 }
1224 };
1225
Chris Lattnerb694ba72006-07-02 22:41:36 +00001226} // end anonymous namespace
1227
1228
1229/// RegisterBuiltinPragmas - Install the standard preprocessor pragmas:
James Dennett18a6d792012-06-17 03:26:26 +00001230/// \#pragma GCC poison/system_header/dependency and \#pragma once.
Chris Lattnerb694ba72006-07-02 22:41:36 +00001231void Preprocessor::RegisterBuiltinPragmas() {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001232 AddPragmaHandler(new PragmaOnceHandler());
1233 AddPragmaHandler(new PragmaMarkHandler());
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001234 AddPragmaHandler(new PragmaPushMacroHandler());
1235 AddPragmaHandler(new PragmaPopMacroHandler());
Michael J. Spencera0a820f2010-09-27 06:19:02 +00001236 AddPragmaHandler(new PragmaMessageHandler());
Mike Stump11289f42009-09-09 15:08:12 +00001237
Chris Lattnerb61448d2009-05-12 18:21:11 +00001238 // #pragma GCC ...
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001239 AddPragmaHandler("GCC", new PragmaPoisonHandler());
1240 AddPragmaHandler("GCC", new PragmaSystemHeaderHandler());
1241 AddPragmaHandler("GCC", new PragmaDependencyHandler());
Douglas Gregor3bde9b12011-06-22 19:41:48 +00001242 AddPragmaHandler("GCC", new PragmaDiagnosticHandler("GCC"));
Chris Lattnerb61448d2009-05-12 18:21:11 +00001243 // #pragma clang ...
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001244 AddPragmaHandler("clang", new PragmaPoisonHandler());
1245 AddPragmaHandler("clang", new PragmaSystemHeaderHandler());
Daniel Dunbarb8068c32010-07-28 15:40:33 +00001246 AddPragmaHandler("clang", new PragmaDebugHandler());
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001247 AddPragmaHandler("clang", new PragmaDependencyHandler());
Douglas Gregor3bde9b12011-06-22 19:41:48 +00001248 AddPragmaHandler("clang", new PragmaDiagnosticHandler("clang"));
John McCall32f5fe12011-09-30 05:12:12 +00001249 AddPragmaHandler("clang", new PragmaARCCFCodeAuditedHandler());
Chris Lattnerb61448d2009-05-12 18:21:11 +00001250
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001251 AddPragmaHandler("STDC", new PragmaSTDC_FENV_ACCESSHandler());
1252 AddPragmaHandler("STDC", new PragmaSTDC_CX_LIMITED_RANGEHandler());
Chris Lattner958ee042009-04-19 21:20:35 +00001253 AddPragmaHandler("STDC", new PragmaSTDC_UnknownHandler());
Mike Stump11289f42009-09-09 15:08:12 +00001254
Chris Lattner2ff698d2009-01-16 08:21:25 +00001255 // MS extensions.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001256 if (LangOpts.MicrosoftExt) {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001257 AddPragmaHandler(new PragmaCommentHandler());
Aaron Ballman611306e2012-03-02 22:51:54 +00001258 AddPragmaHandler(new PragmaIncludeAliasHandler());
Aaron Ballman406ea512012-11-30 19:52:30 +00001259 AddPragmaHandler(new PragmaRegionHandler("region"));
1260 AddPragmaHandler(new PragmaRegionHandler("endregion"));
Chris Lattner30c924b2010-06-26 17:11:39 +00001261 }
Chris Lattnerb694ba72006-07-02 22:41:36 +00001262}