blob: d674ad34b47508d0c0acc5769345a40330d29d98 [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);
Richard Smithc98bb4e2013-03-09 23:30:15 +0000187 if (!tok::isStringLiteral(Tok.getKind())) {
Chris Lattner907dfe92008-11-18 07:59:24 +0000188 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
Richard Smithc98bb4e2013-03-09 23:30:15 +0000222 // The _Pragma is lexically sound. Destringize according to C11 6.10.9.1:
223 // "The string literal is destringized by deleting any encoding prefix,
Chris Lattner262d4e32009-01-16 18:59:23 +0000224 // 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."
Richard Smithc98bb4e2013-03-09 23:30:15 +0000227 if (StrVal[0] == 'L' || StrVal[0] == 'U' ||
228 (StrVal[0] == 'u' && StrVal[1] != '8'))
Chris Lattnerb694ba72006-07-02 22:41:36 +0000229 StrVal.erase(StrVal.begin());
Richard Smithc98bb4e2013-03-09 23:30:15 +0000230 else if (StrVal[0] == 'u')
231 StrVal.erase(StrVal.begin(), StrVal.begin() + 2);
232
233 if (StrVal[0] == 'R') {
234 // FIXME: C++11 does not specify how to handle raw-string-literals here.
235 // We strip off the 'R', the quotes, the d-char-sequences, and the parens.
236 assert(StrVal[1] == '"' && StrVal[StrVal.size() - 1] == '"' &&
237 "Invalid raw string token!");
238
239 // Measure the length of the d-char-sequence.
240 unsigned NumDChars = 0;
241 while (StrVal[2 + NumDChars] != '(') {
242 assert(NumDChars < (StrVal.size() - 5) / 2 &&
243 "Invalid raw string token!");
244 ++NumDChars;
245 }
246 assert(StrVal[StrVal.size() - 2 - NumDChars] == ')');
247
248 // Remove 'R " d-char-sequence' and 'd-char-sequence "'. We'll replace the
249 // parens below.
250 StrVal.erase(0, 2 + NumDChars);
251 StrVal.erase(StrVal.size() - 1 - NumDChars);
252 } else {
253 assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
254 "Invalid string token!");
255
256 // Remove escaped quotes and escapes.
Benjamin Kramerc2f5f292013-05-04 10:37:20 +0000257 unsigned ResultPos = 1;
258 for (unsigned i = 1, e = StrVal.size() - 2; i != e; ++i) {
259 if (StrVal[i] != '\\' ||
260 (StrVal[i + 1] != '\\' && StrVal[i + 1] != '"')) {
Richard Smithc98bb4e2013-03-09 23:30:15 +0000261 // \\ -> '\' and \" -> '"'.
Benjamin Kramerc2f5f292013-05-04 10:37:20 +0000262 StrVal[ResultPos++] = StrVal[i];
Richard Smithc98bb4e2013-03-09 23:30:15 +0000263 }
264 }
Benjamin Kramerc2f5f292013-05-04 10:37:20 +0000265 StrVal.erase(StrVal.begin() + ResultPos, StrVal.end() - 2);
Richard Smithc98bb4e2013-03-09 23:30:15 +0000266 }
Mike Stump11289f42009-09-09 15:08:12 +0000267
Chris Lattnerb694ba72006-07-02 22:41:36 +0000268 // Remove the front quote, replacing it with a space, so that the pragma
269 // contents appear to have a space before them.
270 StrVal[0] = ' ';
Mike Stump11289f42009-09-09 15:08:12 +0000271
Chris Lattnerfa217bd2009-03-08 08:08:45 +0000272 // Replace the terminating quote with a \n.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000273 StrVal[StrVal.size()-1] = '\n';
Mike Stump11289f42009-09-09 15:08:12 +0000274
Peter Collingbournef29ce972011-02-22 13:49:06 +0000275 // Plop the string (including the newline and trailing null) into a buffer
276 // where we can lex it.
277 Token TmpTok;
278 TmpTok.startToken();
Dmitri Gribenkob8e9e752012-09-24 21:07:17 +0000279 CreateString(StrVal, TmpTok);
Peter Collingbournef29ce972011-02-22 13:49:06 +0000280 SourceLocation TokLoc = TmpTok.getLocation();
281
282 // Make and enter a lexer object so that we lex and expand the tokens just
283 // like any others.
284 Lexer *TL = Lexer::Create_PragmaLexer(TokLoc, PragmaLoc, RParenLoc,
285 StrVal.size(), *this);
286
287 EnterSourceFileWithLexer(TL, 0);
288
289 // With everything set up, lex this as a #pragma directive.
290 HandlePragmaDirective(PIK__Pragma);
John McCall89e925d2010-08-28 22:34:47 +0000291
292 // Finally, return whatever came after the pragma directive.
293 return Lex(Tok);
294}
295
296/// HandleMicrosoft__pragma - Like Handle_Pragma except the pragma text
297/// is not enclosed within a string literal.
298void Preprocessor::HandleMicrosoft__pragma(Token &Tok) {
299 // Remember the pragma token location.
300 SourceLocation PragmaLoc = Tok.getLocation();
301
302 // Read the '('.
303 Lex(Tok);
304 if (Tok.isNot(tok::l_paren)) {
305 Diag(PragmaLoc, diag::err__Pragma_malformed);
306 return;
307 }
308
Peter Collingbournef29ce972011-02-22 13:49:06 +0000309 // Get the tokens enclosed within the __pragma(), as well as the final ')'.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000310 SmallVector<Token, 32> PragmaToks;
John McCall89e925d2010-08-28 22:34:47 +0000311 int NumParens = 0;
312 Lex(Tok);
313 while (Tok.isNot(tok::eof)) {
Peter Collingbournef29ce972011-02-22 13:49:06 +0000314 PragmaToks.push_back(Tok);
John McCall89e925d2010-08-28 22:34:47 +0000315 if (Tok.is(tok::l_paren))
316 NumParens++;
317 else if (Tok.is(tok::r_paren) && NumParens-- == 0)
318 break;
John McCall89e925d2010-08-28 22:34:47 +0000319 Lex(Tok);
320 }
321
John McCall49039d42010-08-29 01:09:54 +0000322 if (Tok.is(tok::eof)) {
323 Diag(PragmaLoc, diag::err_unterminated___pragma);
324 return;
325 }
326
Peter Collingbournef29ce972011-02-22 13:49:06 +0000327 PragmaToks.front().setFlag(Token::LeadingSpace);
John McCall89e925d2010-08-28 22:34:47 +0000328
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000329 // Replace the ')' with an EOD to mark the end of the pragma.
330 PragmaToks.back().setKind(tok::eod);
Peter Collingbournef29ce972011-02-22 13:49:06 +0000331
332 Token *TokArray = new Token[PragmaToks.size()];
333 std::copy(PragmaToks.begin(), PragmaToks.end(), TokArray);
334
335 // Push the tokens onto the stack.
336 EnterTokenStream(TokArray, PragmaToks.size(), true, true);
337
338 // With everything set up, lex this as a #pragma directive.
339 HandlePragmaDirective(PIK___pragma);
John McCall89e925d2010-08-28 22:34:47 +0000340
341 // Finally, return whatever came after the pragma directive.
342 return Lex(Tok);
343}
344
James Dennett18a6d792012-06-17 03:26:26 +0000345/// HandlePragmaOnce - Handle \#pragma once. OnceTok is the 'once'.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000346///
Chris Lattner146762e2007-07-20 16:59:19 +0000347void Preprocessor::HandlePragmaOnce(Token &OnceTok) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000348 if (isInPrimaryFile()) {
349 Diag(OnceTok, diag::pp_pragma_once_in_main_file);
350 return;
351 }
Mike Stump11289f42009-09-09 15:08:12 +0000352
Chris Lattnerb694ba72006-07-02 22:41:36 +0000353 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000354 // Mark the file as a once-only file now.
Chris Lattnerd32480d2009-01-17 06:22:33 +0000355 HeaderInfo.MarkFileIncludeOnce(getCurrentFileLexer()->getFileEntry());
Chris Lattnerb694ba72006-07-02 22:41:36 +0000356}
357
Chris Lattnerc2383312007-12-19 19:38:36 +0000358void Preprocessor::HandlePragmaMark() {
Ted Kremenek76c34412008-11-19 22:21:33 +0000359 assert(CurPPLexer && "No current lexer?");
Chris Lattnerd9efb6e2009-06-15 05:02:34 +0000360 if (CurLexer)
361 CurLexer->ReadToEndOfLine();
362 else
363 CurPTHLexer->DiscardToEndOfLine();
Chris Lattnerc2383312007-12-19 19:38:36 +0000364}
365
366
James Dennett18a6d792012-06-17 03:26:26 +0000367/// HandlePragmaPoison - Handle \#pragma GCC poison. PoisonTok is the 'poison'.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000368///
Chris Lattner146762e2007-07-20 16:59:19 +0000369void Preprocessor::HandlePragmaPoison(Token &PoisonTok) {
370 Token Tok;
Chris Lattner538d7f32006-07-20 04:31:52 +0000371
Chris Lattnerb694ba72006-07-02 22:41:36 +0000372 while (1) {
373 // Read the next token to poison. While doing this, pretend that we are
374 // skipping while reading the identifier to poison.
375 // This avoids errors on code like:
376 // #pragma GCC poison X
377 // #pragma GCC poison X
Ted Kremenek551c82a2008-11-18 01:12:54 +0000378 if (CurPPLexer) CurPPLexer->LexingRawMode = true;
Chris Lattnerb694ba72006-07-02 22:41:36 +0000379 LexUnexpandedToken(Tok);
Ted Kremenek551c82a2008-11-18 01:12:54 +0000380 if (CurPPLexer) CurPPLexer->LexingRawMode = false;
Mike Stump11289f42009-09-09 15:08:12 +0000381
Chris Lattnerb694ba72006-07-02 22:41:36 +0000382 // If we reached the end of line, we're done.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000383 if (Tok.is(tok::eod)) return;
Mike Stump11289f42009-09-09 15:08:12 +0000384
Chris Lattnerb694ba72006-07-02 22:41:36 +0000385 // Can only poison identifiers.
Abramo Bagnaraea4f7c72010-12-22 08:23:18 +0000386 if (Tok.isNot(tok::raw_identifier)) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000387 Diag(Tok, diag::err_pp_invalid_poison);
388 return;
389 }
Mike Stump11289f42009-09-09 15:08:12 +0000390
Chris Lattnercefc7682006-07-08 08:28:12 +0000391 // Look up the identifier info for the token. We disabled identifier lookup
392 // by saying we're skipping contents, so we need to do this manually.
393 IdentifierInfo *II = LookUpIdentifierInfo(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000394
Chris Lattnerb694ba72006-07-02 22:41:36 +0000395 // Already poisoned.
396 if (II->isPoisoned()) continue;
Mike Stump11289f42009-09-09 15:08:12 +0000397
Chris Lattnerb694ba72006-07-02 22:41:36 +0000398 // If this is a macro identifier, emit a warning.
Chris Lattner259716a2007-10-07 08:04:56 +0000399 if (II->hasMacroDefinition())
Chris Lattnerb694ba72006-07-02 22:41:36 +0000400 Diag(Tok, diag::pp_poisoning_existing_macro);
Mike Stump11289f42009-09-09 15:08:12 +0000401
Chris Lattnerb694ba72006-07-02 22:41:36 +0000402 // Finally, poison it!
403 II->setIsPoisoned();
Douglas Gregor935bc7a22011-10-27 09:33:13 +0000404 if (II->isFromAST())
405 II->setChangedSinceDeserialization();
Chris Lattnerb694ba72006-07-02 22:41:36 +0000406 }
407}
408
James Dennett18a6d792012-06-17 03:26:26 +0000409/// HandlePragmaSystemHeader - Implement \#pragma GCC system_header. We know
Chris Lattnerb694ba72006-07-02 22:41:36 +0000410/// that the whole directive has been parsed.
Chris Lattner146762e2007-07-20 16:59:19 +0000411void Preprocessor::HandlePragmaSystemHeader(Token &SysHeaderTok) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000412 if (isInPrimaryFile()) {
413 Diag(SysHeaderTok, diag::pp_pragma_sysheader_in_main_file);
414 return;
415 }
Mike Stump11289f42009-09-09 15:08:12 +0000416
Chris Lattnerb694ba72006-07-02 22:41:36 +0000417 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
Ted Kremenek300590b2008-11-20 01:45:11 +0000418 PreprocessorLexer *TheLexer = getCurrentFileLexer();
Mike Stump11289f42009-09-09 15:08:12 +0000419
Chris Lattnerb694ba72006-07-02 22:41:36 +0000420 // Mark the file as a system header.
Chris Lattnerd32480d2009-01-17 06:22:33 +0000421 HeaderInfo.MarkFileSystemHeader(TheLexer->getFileEntry());
Mike Stump11289f42009-09-09 15:08:12 +0000422
423
Chris Lattnerd9efb6e2009-06-15 05:02:34 +0000424 PresumedLoc PLoc = SourceMgr.getPresumedLoc(SysHeaderTok.getLocation());
Douglas Gregor453b0122010-11-12 07:15:47 +0000425 if (PLoc.isInvalid())
426 return;
427
Jay Foad9a6b0982011-06-21 15:13:30 +0000428 unsigned FilenameID = SourceMgr.getLineTableFilenameID(PLoc.getFilename());
Mike Stump11289f42009-09-09 15:08:12 +0000429
Chris Lattner3bdc7672011-05-22 22:10:16 +0000430 // Notify the client, if desired, that we are in a new source file.
431 if (Callbacks)
432 Callbacks->FileChanged(SysHeaderTok.getLocation(),
433 PPCallbacks::SystemHeaderPragma, SrcMgr::C_System);
434
Chris Lattnerd9efb6e2009-06-15 05:02:34 +0000435 // Emit a line marker. This will change any source locations from this point
436 // forward to realize they are in a system header.
437 // Create a line note with this information.
Jordan Rose111c4a62013-04-17 19:09:18 +0000438 SourceMgr.AddLineNote(SysHeaderTok.getLocation(), PLoc.getLine()+1,
439 FilenameID, /*IsEntry=*/false, /*IsExit=*/false,
440 /*IsSystem=*/true, /*IsExternC=*/false);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000441}
442
James Dennett18a6d792012-06-17 03:26:26 +0000443/// HandlePragmaDependency - Handle \#pragma GCC dependency "foo" blah.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000444///
Chris Lattner146762e2007-07-20 16:59:19 +0000445void Preprocessor::HandlePragmaDependency(Token &DependencyTok) {
446 Token FilenameTok;
Ted Kremenek551c82a2008-11-18 01:12:54 +0000447 CurPPLexer->LexIncludeFilename(FilenameTok);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000448
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000449 // If the token kind is EOD, the error has already been diagnosed.
450 if (FilenameTok.is(tok::eod))
Chris Lattnerb694ba72006-07-02 22:41:36 +0000451 return;
Mike Stump11289f42009-09-09 15:08:12 +0000452
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000453 // Reserve a buffer to get the spelling.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000454 SmallString<128> FilenameBuffer;
Douglas Gregordc970f02010-03-16 22:30:13 +0000455 bool Invalid = false;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000456 StringRef Filename = getSpelling(FilenameTok, FilenameBuffer, &Invalid);
Douglas Gregordc970f02010-03-16 22:30:13 +0000457 if (Invalid)
458 return;
Mike Stump11289f42009-09-09 15:08:12 +0000459
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000460 bool isAngled =
461 GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000462 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
463 // error.
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000464 if (Filename.empty())
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000465 return;
Mike Stump11289f42009-09-09 15:08:12 +0000466
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000467 // Search include directories for this file.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000468 const DirectoryLookup *CurDir;
Douglas Gregor97eec242011-09-15 22:00:41 +0000469 const FileEntry *File = LookupFile(Filename, isAngled, 0, CurDir, NULL, NULL,
470 NULL);
Chris Lattner97b8e842008-11-18 08:02:48 +0000471 if (File == 0) {
Eli Friedman3781a362011-08-30 23:07:51 +0000472 if (!SuppressIncludeNotFoundError)
473 Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
Chris Lattner97b8e842008-11-18 08:02:48 +0000474 return;
475 }
Mike Stump11289f42009-09-09 15:08:12 +0000476
Chris Lattnerd32480d2009-01-17 06:22:33 +0000477 const FileEntry *CurFile = getCurrentFileLexer()->getFileEntry();
Chris Lattnerb694ba72006-07-02 22:41:36 +0000478
479 // If this file is older than the file it depends on, emit a diagnostic.
480 if (CurFile && CurFile->getModificationTime() < File->getModificationTime()) {
481 // Lex tokens at the end of the message and include them in the message.
482 std::string Message;
483 Lex(DependencyTok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000484 while (DependencyTok.isNot(tok::eod)) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000485 Message += getSpelling(DependencyTok) + " ";
486 Lex(DependencyTok);
487 }
Mike Stump11289f42009-09-09 15:08:12 +0000488
Chris Lattnerf0b04972010-09-05 23:16:09 +0000489 // Remove the trailing ' ' if present.
490 if (!Message.empty())
491 Message.erase(Message.end()-1);
Chris Lattner97b8e842008-11-18 08:02:48 +0000492 Diag(FilenameTok, diag::pp_out_of_date_dependency) << Message;
Chris Lattnerb694ba72006-07-02 22:41:36 +0000493 }
494}
495
James Dennett18a6d792012-06-17 03:26:26 +0000496/// \brief Handle the microsoft \#pragma comment extension.
497///
498/// The syntax is:
499/// \code
Dmitri Gribenko9ebd1612012-11-30 20:04:39 +0000500/// #pragma comment(linker, "foo")
James Dennett18a6d792012-06-17 03:26:26 +0000501/// \endcode
Chris Lattner2ff698d2009-01-16 08:21:25 +0000502/// 'linker' is one of five identifiers: compiler, exestr, lib, linker, user.
503/// "foo" is a string, which is fully macro expanded, and permits string
Gabor Greif31a082f2009-03-17 11:39:38 +0000504/// concatenation, embedded escape characters etc. See MSDN for more details.
Chris Lattner2ff698d2009-01-16 08:21:25 +0000505void Preprocessor::HandlePragmaComment(Token &Tok) {
506 SourceLocation CommentLoc = Tok.getLocation();
507 Lex(Tok);
508 if (Tok.isNot(tok::l_paren)) {
509 Diag(CommentLoc, diag::err_pragma_comment_malformed);
510 return;
511 }
Mike Stump11289f42009-09-09 15:08:12 +0000512
Chris Lattner2ff698d2009-01-16 08:21:25 +0000513 // Read the identifier.
514 Lex(Tok);
515 if (Tok.isNot(tok::identifier)) {
516 Diag(CommentLoc, diag::err_pragma_comment_malformed);
517 return;
518 }
Mike Stump11289f42009-09-09 15:08:12 +0000519
Chris Lattner2ff698d2009-01-16 08:21:25 +0000520 // Verify that this is one of the 5 whitelisted options.
521 // FIXME: warn that 'exestr' is deprecated.
522 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000523 if (!II->isStr("compiler") && !II->isStr("exestr") && !II->isStr("lib") &&
Chris Lattner2ff698d2009-01-16 08:21:25 +0000524 !II->isStr("linker") && !II->isStr("user")) {
525 Diag(Tok.getLocation(), diag::err_pragma_comment_unknown_kind);
526 return;
527 }
Mike Stump11289f42009-09-09 15:08:12 +0000528
Chris Lattner262d4e32009-01-16 18:59:23 +0000529 // Read the optional string if present.
Chris Lattner2ff698d2009-01-16 08:21:25 +0000530 Lex(Tok);
Chris Lattner262d4e32009-01-16 18:59:23 +0000531 std::string ArgumentString;
Andy Gibbs58905d22012-11-17 19:15:38 +0000532 if (Tok.is(tok::comma) && !LexStringLiteral(Tok, ArgumentString,
Andy Gibbsa8df57a2012-11-17 19:16:52 +0000533 "pragma comment",
Andy Gibbs58905d22012-11-17 19:15:38 +0000534 /*MacroExpansion=*/true))
535 return;
Mike Stump11289f42009-09-09 15:08:12 +0000536
Chris Lattner262d4e32009-01-16 18:59:23 +0000537 // FIXME: If the kind is "compiler" warn if the string is present (it is
538 // ignored).
539 // FIXME: 'lib' requires a comment string.
540 // FIXME: 'linker' requires a comment string, and has a specific list of
541 // things that are allowable.
Mike Stump11289f42009-09-09 15:08:12 +0000542
Chris Lattner2ff698d2009-01-16 08:21:25 +0000543 if (Tok.isNot(tok::r_paren)) {
544 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
545 return;
546 }
Chris Lattner262d4e32009-01-16 18:59:23 +0000547 Lex(Tok); // eat the r_paren.
Chris Lattner2ff698d2009-01-16 08:21:25 +0000548
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000549 if (Tok.isNot(tok::eod)) {
Chris Lattner2ff698d2009-01-16 08:21:25 +0000550 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
551 return;
552 }
Mike Stump11289f42009-09-09 15:08:12 +0000553
Chris Lattner262d4e32009-01-16 18:59:23 +0000554 // If the pragma is lexically sound, notify any interested PPCallbacks.
Chris Lattnerf49775d2009-01-16 19:01:46 +0000555 if (Callbacks)
556 Callbacks->PragmaComment(CommentLoc, II, ArgumentString);
Chris Lattner2ff698d2009-01-16 08:21:25 +0000557}
558
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000559/// ParsePragmaPushOrPopMacro - Handle parsing of pragma push_macro/pop_macro.
560/// Return the IdentifierInfo* associated with the macro to push or pop.
561IdentifierInfo *Preprocessor::ParsePragmaPushOrPopMacro(Token &Tok) {
562 // Remember the pragma token location.
563 Token PragmaTok = Tok;
564
565 // Read the '('.
566 Lex(Tok);
567 if (Tok.isNot(tok::l_paren)) {
568 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
569 << getSpelling(PragmaTok);
570 return 0;
571 }
572
573 // Read the macro name string.
574 Lex(Tok);
575 if (Tok.isNot(tok::string_literal)) {
576 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
577 << getSpelling(PragmaTok);
578 return 0;
579 }
580
Richard Smithd67aea22012-03-06 03:21:47 +0000581 if (Tok.hasUDSuffix()) {
582 Diag(Tok, diag::err_invalid_string_udl);
583 return 0;
584 }
585
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000586 // Remember the macro string.
587 std::string StrVal = getSpelling(Tok);
588
589 // Read the ')'.
590 Lex(Tok);
591 if (Tok.isNot(tok::r_paren)) {
592 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
593 << getSpelling(PragmaTok);
594 return 0;
595 }
596
597 assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
598 "Invalid string token!");
599
600 // Create a Token from the string.
601 Token MacroTok;
602 MacroTok.startToken();
Abramo Bagnaraea4f7c72010-12-22 08:23:18 +0000603 MacroTok.setKind(tok::raw_identifier);
Dmitri Gribenkob8e9e752012-09-24 21:07:17 +0000604 CreateString(StringRef(&StrVal[1], StrVal.size() - 2), MacroTok);
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000605
606 // Get the IdentifierInfo of MacroToPushTok.
607 return LookUpIdentifierInfo(MacroTok);
608}
609
James Dennett18a6d792012-06-17 03:26:26 +0000610/// \brief Handle \#pragma push_macro.
611///
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000612/// The syntax is:
James Dennett18a6d792012-06-17 03:26:26 +0000613/// \code
Dmitri Gribenko9ebd1612012-11-30 20:04:39 +0000614/// #pragma push_macro("macro")
James Dennett18a6d792012-06-17 03:26:26 +0000615/// \endcode
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000616void Preprocessor::HandlePragmaPushMacro(Token &PushMacroTok) {
617 // Parse the pragma directive and get the macro IdentifierInfo*.
618 IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PushMacroTok);
619 if (!IdentInfo) return;
620
621 // Get the MacroInfo associated with IdentInfo.
622 MacroInfo *MI = getMacroInfo(IdentInfo);
623
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000624 if (MI) {
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000625 // Allow the original MacroInfo to be redefined later.
626 MI->setIsAllowRedefinitionsWithoutWarning(true);
627 }
628
629 // Push the cloned MacroInfo so we can retrieve it later.
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +0000630 PragmaPushMacroInfo[IdentInfo].push_back(MI);
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000631}
632
James Dennett18a6d792012-06-17 03:26:26 +0000633/// \brief Handle \#pragma pop_macro.
634///
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000635/// The syntax is:
James Dennett18a6d792012-06-17 03:26:26 +0000636/// \code
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000637/// #pragma pop_macro("macro")
James Dennett18a6d792012-06-17 03:26:26 +0000638/// \endcode
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000639void Preprocessor::HandlePragmaPopMacro(Token &PopMacroTok) {
640 SourceLocation MessageLoc = PopMacroTok.getLocation();
641
642 // Parse the pragma directive and get the macro IdentifierInfo*.
643 IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PopMacroTok);
644 if (!IdentInfo) return;
645
646 // Find the vector<MacroInfo*> associated with the macro.
647 llvm::DenseMap<IdentifierInfo*, std::vector<MacroInfo*> >::iterator iter =
648 PragmaPushMacroInfo.find(IdentInfo);
649 if (iter != PragmaPushMacroInfo.end()) {
Alexander Kornienko8b3f6232012-08-29 00:20:03 +0000650 // Forget the MacroInfo currently associated with IdentInfo.
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +0000651 if (MacroDirective *CurrentMD = getMacroDirective(IdentInfo)) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +0000652 MacroInfo *MI = CurrentMD->getMacroInfo();
653 if (MI->isWarnIfUnused())
654 WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());
655 appendMacroDirective(IdentInfo, AllocateUndefMacroDirective(MessageLoc));
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000656 }
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000657
658 // Get the MacroInfo we want to reinstall.
659 MacroInfo *MacroToReInstall = iter->second.back();
660
Alexander Kornienkoc0b49282012-08-29 16:56:24 +0000661 if (MacroToReInstall) {
662 // Reinstall the previously pushed macro.
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +0000663 appendDefMacroDirective(IdentInfo, MacroToReInstall, MessageLoc,
664 /*isImported=*/false);
Alexander Kornienkoc0b49282012-08-29 16:56:24 +0000665 }
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000666
667 // Pop PragmaPushMacroInfo stack.
668 iter->second.pop_back();
669 if (iter->second.size() == 0)
670 PragmaPushMacroInfo.erase(iter);
671 } else {
672 Diag(MessageLoc, diag::warn_pragma_pop_macro_no_push)
673 << IdentInfo->getName();
674 }
675}
Chris Lattnerb694ba72006-07-02 22:41:36 +0000676
Aaron Ballman611306e2012-03-02 22:51:54 +0000677void Preprocessor::HandlePragmaIncludeAlias(Token &Tok) {
678 // We will either get a quoted filename or a bracketed filename, and we
679 // have to track which we got. The first filename is the source name,
680 // and the second name is the mapped filename. If the first is quoted,
681 // the second must be as well (cannot mix and match quotes and brackets).
Aaron Ballman611306e2012-03-02 22:51:54 +0000682
683 // Get the open paren
684 Lex(Tok);
685 if (Tok.isNot(tok::l_paren)) {
686 Diag(Tok, diag::warn_pragma_include_alias_expected) << "(";
687 return;
688 }
689
690 // We expect either a quoted string literal, or a bracketed name
691 Token SourceFilenameTok;
692 CurPPLexer->LexIncludeFilename(SourceFilenameTok);
693 if (SourceFilenameTok.is(tok::eod)) {
694 // The diagnostic has already been handled
695 return;
696 }
697
698 StringRef SourceFileName;
699 SmallString<128> FileNameBuffer;
700 if (SourceFilenameTok.is(tok::string_literal) ||
701 SourceFilenameTok.is(tok::angle_string_literal)) {
702 SourceFileName = getSpelling(SourceFilenameTok, FileNameBuffer);
703 } else if (SourceFilenameTok.is(tok::less)) {
704 // This could be a path instead of just a name
705 FileNameBuffer.push_back('<');
706 SourceLocation End;
707 if (ConcatenateIncludeName(FileNameBuffer, End))
708 return; // Diagnostic already emitted
709 SourceFileName = FileNameBuffer.str();
710 } else {
711 Diag(Tok, diag::warn_pragma_include_alias_expected_filename);
712 return;
713 }
714 FileNameBuffer.clear();
715
716 // Now we expect a comma, followed by another include name
717 Lex(Tok);
718 if (Tok.isNot(tok::comma)) {
719 Diag(Tok, diag::warn_pragma_include_alias_expected) << ",";
720 return;
721 }
722
723 Token ReplaceFilenameTok;
724 CurPPLexer->LexIncludeFilename(ReplaceFilenameTok);
725 if (ReplaceFilenameTok.is(tok::eod)) {
726 // The diagnostic has already been handled
727 return;
728 }
729
730 StringRef ReplaceFileName;
731 if (ReplaceFilenameTok.is(tok::string_literal) ||
732 ReplaceFilenameTok.is(tok::angle_string_literal)) {
733 ReplaceFileName = getSpelling(ReplaceFilenameTok, FileNameBuffer);
734 } else if (ReplaceFilenameTok.is(tok::less)) {
735 // This could be a path instead of just a name
736 FileNameBuffer.push_back('<');
737 SourceLocation End;
738 if (ConcatenateIncludeName(FileNameBuffer, End))
739 return; // Diagnostic already emitted
740 ReplaceFileName = FileNameBuffer.str();
741 } else {
742 Diag(Tok, diag::warn_pragma_include_alias_expected_filename);
743 return;
744 }
745
746 // Finally, we expect the closing paren
747 Lex(Tok);
748 if (Tok.isNot(tok::r_paren)) {
749 Diag(Tok, diag::warn_pragma_include_alias_expected) << ")";
750 return;
751 }
752
753 // Now that we have the source and target filenames, we need to make sure
754 // they're both of the same type (angled vs non-angled)
755 StringRef OriginalSource = SourceFileName;
756
757 bool SourceIsAngled =
758 GetIncludeFilenameSpelling(SourceFilenameTok.getLocation(),
759 SourceFileName);
760 bool ReplaceIsAngled =
761 GetIncludeFilenameSpelling(ReplaceFilenameTok.getLocation(),
762 ReplaceFileName);
763 if (!SourceFileName.empty() && !ReplaceFileName.empty() &&
764 (SourceIsAngled != ReplaceIsAngled)) {
765 unsigned int DiagID;
766 if (SourceIsAngled)
767 DiagID = diag::warn_pragma_include_alias_mismatch_angle;
768 else
769 DiagID = diag::warn_pragma_include_alias_mismatch_quote;
770
771 Diag(SourceFilenameTok.getLocation(), DiagID)
772 << SourceFileName
773 << ReplaceFileName;
774
775 return;
776 }
777
778 // Now we can let the include handler know about this mapping
779 getHeaderSearchInfo().AddIncludeAlias(OriginalSource, ReplaceFileName);
780}
781
Chris Lattnerb694ba72006-07-02 22:41:36 +0000782/// AddPragmaHandler - Add the specified pragma handler to the preprocessor.
783/// If 'Namespace' is non-null, then it is a token required to exist on the
784/// pragma line before the pragma string starts, e.g. "STDC" or "GCC".
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000785void Preprocessor::AddPragmaHandler(StringRef Namespace,
Chris Lattnerb694ba72006-07-02 22:41:36 +0000786 PragmaHandler *Handler) {
787 PragmaNamespace *InsertNS = PragmaHandlers;
Mike Stump11289f42009-09-09 15:08:12 +0000788
Chris Lattnerb694ba72006-07-02 22:41:36 +0000789 // If this is specified to be in a namespace, step down into it.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000790 if (!Namespace.empty()) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000791 // If there is already a pragma handler with the name of this namespace,
792 // we either have an error (directive with the same name as a namespace) or
793 // we already have the namespace to insert into.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000794 if (PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace)) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000795 InsertNS = Existing->getIfNamespace();
796 assert(InsertNS != 0 && "Cannot have a pragma namespace and pragma"
797 " handler with the same name!");
798 } else {
799 // Otherwise, this namespace doesn't exist yet, create and insert the
800 // handler for it.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000801 InsertNS = new PragmaNamespace(Namespace);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000802 PragmaHandlers->AddPragma(InsertNS);
803 }
804 }
Mike Stump11289f42009-09-09 15:08:12 +0000805
Chris Lattnerb694ba72006-07-02 22:41:36 +0000806 // Check to make sure we don't already have a pragma for this identifier.
807 assert(!InsertNS->FindHandler(Handler->getName()) &&
808 "Pragma handler already exists for this identifier!");
809 InsertNS->AddPragma(Handler);
810}
811
Daniel Dunbar40596532008-10-04 19:17:46 +0000812/// RemovePragmaHandler - Remove the specific pragma handler from the
813/// preprocessor. If \arg Namespace is non-null, then it should be the
814/// namespace that \arg Handler was added to. It is an error to remove
815/// a handler that has not been registered.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000816void Preprocessor::RemovePragmaHandler(StringRef Namespace,
Daniel Dunbar40596532008-10-04 19:17:46 +0000817 PragmaHandler *Handler) {
818 PragmaNamespace *NS = PragmaHandlers;
Mike Stump11289f42009-09-09 15:08:12 +0000819
Daniel Dunbar40596532008-10-04 19:17:46 +0000820 // If this is specified to be in a namespace, step down into it.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000821 if (!Namespace.empty()) {
822 PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace);
Daniel Dunbar40596532008-10-04 19:17:46 +0000823 assert(Existing && "Namespace containing handler does not exist!");
824
825 NS = Existing->getIfNamespace();
826 assert(NS && "Invalid namespace, registered as a regular pragma handler!");
827 }
828
829 NS->RemovePragmaHandler(Handler);
Mike Stump11289f42009-09-09 15:08:12 +0000830
Daniel Dunbar40596532008-10-04 19:17:46 +0000831 // If this is a non-default namespace and it is now empty, remove
832 // it.
Argyrios Kyrtzidis7ce75262012-01-06 00:22:09 +0000833 if (NS != PragmaHandlers && NS->IsEmpty()) {
Daniel Dunbar40596532008-10-04 19:17:46 +0000834 PragmaHandlers->RemovePragmaHandler(NS);
Argyrios Kyrtzidis7ce75262012-01-06 00:22:09 +0000835 delete NS;
836 }
Daniel Dunbar40596532008-10-04 19:17:46 +0000837}
838
Peter Collingbourne3bffa522011-02-14 01:42:24 +0000839bool Preprocessor::LexOnOffSwitch(tok::OnOffSwitch &Result) {
840 Token Tok;
841 LexUnexpandedToken(Tok);
842
843 if (Tok.isNot(tok::identifier)) {
844 Diag(Tok, diag::ext_on_off_switch_syntax);
845 return true;
846 }
847 IdentifierInfo *II = Tok.getIdentifierInfo();
848 if (II->isStr("ON"))
849 Result = tok::OOS_ON;
850 else if (II->isStr("OFF"))
851 Result = tok::OOS_OFF;
852 else if (II->isStr("DEFAULT"))
853 Result = tok::OOS_DEFAULT;
854 else {
855 Diag(Tok, diag::ext_on_off_switch_syntax);
856 return true;
857 }
858
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000859 // Verify that this is followed by EOD.
Peter Collingbourne3bffa522011-02-14 01:42:24 +0000860 LexUnexpandedToken(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000861 if (Tok.isNot(tok::eod))
862 Diag(Tok, diag::ext_pragma_syntax_eod);
Peter Collingbourne3bffa522011-02-14 01:42:24 +0000863 return false;
864}
865
Chris Lattnerb694ba72006-07-02 22:41:36 +0000866namespace {
James Dennett18a6d792012-06-17 03:26:26 +0000867/// PragmaOnceHandler - "\#pragma once" marks the file as atomically included.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000868struct PragmaOnceHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000869 PragmaOnceHandler() : PragmaHandler("once") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000870 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
871 Token &OnceTok) {
Chris Lattnerce2ab6f2009-04-14 05:07:49 +0000872 PP.CheckEndOfDirective("pragma once");
Chris Lattnerb694ba72006-07-02 22:41:36 +0000873 PP.HandlePragmaOnce(OnceTok);
874 }
875};
876
James Dennett18a6d792012-06-17 03:26:26 +0000877/// PragmaMarkHandler - "\#pragma mark ..." is ignored by the compiler, and the
Chris Lattnerc2383312007-12-19 19:38:36 +0000878/// rest of the line is not lexed.
879struct PragmaMarkHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000880 PragmaMarkHandler() : PragmaHandler("mark") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000881 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
882 Token &MarkTok) {
Chris Lattnerc2383312007-12-19 19:38:36 +0000883 PP.HandlePragmaMark();
884 }
885};
886
James Dennett18a6d792012-06-17 03:26:26 +0000887/// PragmaPoisonHandler - "\#pragma poison x" marks x as not usable.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000888struct PragmaPoisonHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000889 PragmaPoisonHandler() : PragmaHandler("poison") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000890 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
891 Token &PoisonTok) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000892 PP.HandlePragmaPoison(PoisonTok);
893 }
894};
895
James Dennett18a6d792012-06-17 03:26:26 +0000896/// PragmaSystemHeaderHandler - "\#pragma system_header" marks the current file
Chris Lattnerc2383312007-12-19 19:38:36 +0000897/// as a system header, which silences warnings in it.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000898struct PragmaSystemHeaderHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000899 PragmaSystemHeaderHandler() : PragmaHandler("system_header") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000900 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
901 Token &SHToken) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000902 PP.HandlePragmaSystemHeader(SHToken);
Chris Lattnerce2ab6f2009-04-14 05:07:49 +0000903 PP.CheckEndOfDirective("pragma");
Chris Lattnerb694ba72006-07-02 22:41:36 +0000904 }
905};
906struct PragmaDependencyHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000907 PragmaDependencyHandler() : PragmaHandler("dependency") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000908 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
909 Token &DepToken) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000910 PP.HandlePragmaDependency(DepToken);
911 }
912};
Mike Stump11289f42009-09-09 15:08:12 +0000913
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000914struct PragmaDebugHandler : public PragmaHandler {
915 PragmaDebugHandler() : PragmaHandler("__debug") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000916 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
917 Token &DepToken) {
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000918 Token Tok;
919 PP.LexUnexpandedToken(Tok);
920 if (Tok.isNot(tok::identifier)) {
Douglas Gregor3cc26482010-08-30 15:15:34 +0000921 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000922 return;
923 }
924 IdentifierInfo *II = Tok.getIdentifierInfo();
925
Daniel Dunbarf2cf3292010-08-17 22:32:48 +0000926 if (II->isStr("assert")) {
David Blaikie83d382b2011-09-23 05:06:16 +0000927 llvm_unreachable("This is an assertion!");
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000928 } else if (II->isStr("crash")) {
David Blaikie5bd4c2a2012-08-21 18:56:49 +0000929 LLVM_BUILTIN_TRAP;
David Blaikie5d577a22012-06-29 22:03:56 +0000930 } else if (II->isStr("parser_crash")) {
931 Token Crasher;
932 Crasher.setKind(tok::annot_pragma_parser_crash);
933 PP.EnterToken(Crasher);
Daniel Dunbarf2cf3292010-08-17 22:32:48 +0000934 } else if (II->isStr("llvm_fatal_error")) {
935 llvm::report_fatal_error("#pragma clang __debug llvm_fatal_error");
936 } else if (II->isStr("llvm_unreachable")) {
937 llvm_unreachable("#pragma clang __debug llvm_unreachable");
938 } else if (II->isStr("overflow_stack")) {
939 DebugOverflowStack();
Daniel Dunbar211a7872010-08-18 23:09:23 +0000940 } else if (II->isStr("handle_crash")) {
941 llvm::CrashRecoveryContext *CRC =llvm::CrashRecoveryContext::GetCurrent();
942 if (CRC)
943 CRC->HandleCrash();
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000944 } else if (II->isStr("captured")) {
945 HandleCaptured(PP);
Daniel Dunbarf2cf3292010-08-17 22:32:48 +0000946 } else {
947 PP.Diag(Tok, diag::warn_pragma_debug_unexpected_command)
948 << II->getName();
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000949 }
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000950
951 PPCallbacks *Callbacks = PP.getPPCallbacks();
952 if (Callbacks)
953 Callbacks->PragmaDebug(Tok.getLocation(), II->getName());
954 }
955
956 void HandleCaptured(Preprocessor &PP) {
957 // Skip if emitting preprocessed output.
958 if (PP.isPreprocessedOutput())
959 return;
960
961 Token Tok;
962 PP.LexUnexpandedToken(Tok);
963
964 if (Tok.isNot(tok::eod)) {
965 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol)
966 << "pragma clang __debug captured";
967 return;
968 }
969
970 SourceLocation NameLoc = Tok.getLocation();
971 Token *Toks = PP.getPreprocessorAllocator().Allocate<Token>(1);
972 Toks->startToken();
973 Toks->setKind(tok::annot_pragma_captured);
974 Toks->setLocation(NameLoc);
975
976 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
977 /*OwnsTokens=*/false);
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000978 }
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
Andy Gibbs9c2ccd62013-04-17 16:16:16 +00001083/// PragmaMessageHandler - Handle the microsoft and gcc \#pragma message
1084/// extension. The syntax is:
1085/// \code
1086/// #pragma message(string)
1087/// \endcode
1088/// OR, in GCC mode:
1089/// \code
1090/// #pragma message string
1091/// \endcode
1092/// string is a string, which is fully macro expanded, and permits string
1093/// concatenation, embedded escape characters, etc... See MSDN for more details.
1094/// Also handles \#pragma GCC warning and \#pragma GCC error which take the same
1095/// form as \#pragma message.
Chris Lattner30c924b2010-06-26 17:11:39 +00001096struct PragmaMessageHandler : public PragmaHandler {
Andy Gibbs9c2ccd62013-04-17 16:16:16 +00001097private:
1098 const PPCallbacks::PragmaMessageKind Kind;
1099 const StringRef Namespace;
1100
1101 static const char* PragmaKind(PPCallbacks::PragmaMessageKind Kind,
1102 bool PragmaNameOnly = false) {
1103 switch (Kind) {
1104 case PPCallbacks::PMK_Message:
1105 return PragmaNameOnly ? "message" : "pragma message";
1106 case PPCallbacks::PMK_Warning:
1107 return PragmaNameOnly ? "warning" : "pragma warning";
1108 case PPCallbacks::PMK_Error:
1109 return PragmaNameOnly ? "error" : "pragma error";
1110 }
1111 llvm_unreachable("Unknown PragmaMessageKind!");
1112 }
1113
1114public:
1115 PragmaMessageHandler(PPCallbacks::PragmaMessageKind Kind,
1116 StringRef Namespace = StringRef())
1117 : PragmaHandler(PragmaKind(Kind, true)), Kind(Kind), Namespace(Namespace) {}
1118
Douglas Gregorc7d65762010-09-09 22:45:38 +00001119 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
Andy Gibbs9c2ccd62013-04-17 16:16:16 +00001120 Token &Tok) {
1121 SourceLocation MessageLoc = Tok.getLocation();
1122 PP.Lex(Tok);
1123 bool ExpectClosingParen = false;
1124 switch (Tok.getKind()) {
1125 case tok::l_paren:
1126 // We have a MSVC style pragma message.
1127 ExpectClosingParen = true;
1128 // Read the string.
1129 PP.Lex(Tok);
1130 break;
1131 case tok::string_literal:
1132 // We have a GCC style pragma message, and we just read the string.
1133 break;
1134 default:
1135 PP.Diag(MessageLoc, diag::err_pragma_message_malformed) << Kind;
1136 return;
1137 }
1138
1139 std::string MessageString;
1140 if (!PP.FinishLexStringLiteral(Tok, MessageString, PragmaKind(Kind),
1141 /*MacroExpansion=*/true))
1142 return;
1143
1144 if (ExpectClosingParen) {
1145 if (Tok.isNot(tok::r_paren)) {
1146 PP.Diag(Tok.getLocation(), diag::err_pragma_message_malformed) << Kind;
1147 return;
1148 }
1149 PP.Lex(Tok); // eat the r_paren.
1150 }
1151
1152 if (Tok.isNot(tok::eod)) {
1153 PP.Diag(Tok.getLocation(), diag::err_pragma_message_malformed) << Kind;
1154 return;
1155 }
1156
1157 // Output the message.
1158 PP.Diag(MessageLoc, (Kind == PPCallbacks::PMK_Error)
1159 ? diag::err_pragma_message
1160 : diag::warn_pragma_message) << MessageString;
1161
1162 // If the pragma is lexically sound, notify any interested PPCallbacks.
1163 if (PPCallbacks *Callbacks = PP.getPPCallbacks())
1164 Callbacks->PragmaMessage(MessageLoc, Namespace, Kind, MessageString);
Chris Lattner30c924b2010-06-26 17:11:39 +00001165 }
1166};
1167
James Dennett18a6d792012-06-17 03:26:26 +00001168/// PragmaPushMacroHandler - "\#pragma push_macro" saves the value of the
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001169/// macro on the top of the stack.
1170struct PragmaPushMacroHandler : public PragmaHandler {
1171 PragmaPushMacroHandler() : PragmaHandler("push_macro") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +00001172 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1173 Token &PushMacroTok) {
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001174 PP.HandlePragmaPushMacro(PushMacroTok);
1175 }
1176};
1177
1178
James Dennett18a6d792012-06-17 03:26:26 +00001179/// PragmaPopMacroHandler - "\#pragma pop_macro" sets the value of the
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001180/// macro to the value on the top of the stack.
1181struct PragmaPopMacroHandler : public PragmaHandler {
1182 PragmaPopMacroHandler() : PragmaHandler("pop_macro") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +00001183 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1184 Token &PopMacroTok) {
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001185 PP.HandlePragmaPopMacro(PopMacroTok);
1186 }
1187};
1188
Chris Lattner958ee042009-04-19 21:20:35 +00001189// Pragma STDC implementations.
Chris Lattner02ef4e32009-04-19 21:50:08 +00001190
James Dennett18a6d792012-06-17 03:26:26 +00001191/// PragmaSTDC_FENV_ACCESSHandler - "\#pragma STDC FENV_ACCESS ...".
Chris Lattner958ee042009-04-19 21:20:35 +00001192struct PragmaSTDC_FENV_ACCESSHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001193 PragmaSTDC_FENV_ACCESSHandler() : PragmaHandler("FENV_ACCESS") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +00001194 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1195 Token &Tok) {
Peter Collingbourne3bffa522011-02-14 01:42:24 +00001196 tok::OnOffSwitch OOS;
1197 if (PP.LexOnOffSwitch(OOS))
1198 return;
1199 if (OOS == tok::OOS_ON)
Chris Lattnerdf222682009-04-19 21:55:32 +00001200 PP.Diag(Tok, diag::warn_stdc_fenv_access_not_supported);
Chris Lattner958ee042009-04-19 21:20:35 +00001201 }
1202};
Mike Stump11289f42009-09-09 15:08:12 +00001203
James Dennett18a6d792012-06-17 03:26:26 +00001204/// PragmaSTDC_CX_LIMITED_RANGEHandler - "\#pragma STDC CX_LIMITED_RANGE ...".
Chris Lattner958ee042009-04-19 21:20:35 +00001205struct PragmaSTDC_CX_LIMITED_RANGEHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001206 PragmaSTDC_CX_LIMITED_RANGEHandler()
1207 : PragmaHandler("CX_LIMITED_RANGE") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +00001208 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1209 Token &Tok) {
Peter Collingbourne3bffa522011-02-14 01:42:24 +00001210 tok::OnOffSwitch OOS;
1211 PP.LexOnOffSwitch(OOS);
Chris Lattner958ee042009-04-19 21:20:35 +00001212 }
1213};
Mike Stump11289f42009-09-09 15:08:12 +00001214
James Dennett18a6d792012-06-17 03:26:26 +00001215/// PragmaSTDC_UnknownHandler - "\#pragma STDC ...".
Chris Lattner958ee042009-04-19 21:20:35 +00001216struct PragmaSTDC_UnknownHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001217 PragmaSTDC_UnknownHandler() {}
Douglas Gregorc7d65762010-09-09 22:45:38 +00001218 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1219 Token &UnknownTok) {
Chris Lattner02ef4e32009-04-19 21:50:08 +00001220 // C99 6.10.6p2, unknown forms are not allowed.
Chris Lattnera0b1f762009-04-19 21:25:37 +00001221 PP.Diag(UnknownTok, diag::ext_stdc_pragma_ignored);
Chris Lattner958ee042009-04-19 21:20:35 +00001222 }
1223};
Mike Stump11289f42009-09-09 15:08:12 +00001224
John McCall32f5fe12011-09-30 05:12:12 +00001225/// PragmaARCCFCodeAuditedHandler -
James Dennett18a6d792012-06-17 03:26:26 +00001226/// \#pragma clang arc_cf_code_audited begin/end
John McCall32f5fe12011-09-30 05:12:12 +00001227struct PragmaARCCFCodeAuditedHandler : public PragmaHandler {
1228 PragmaARCCFCodeAuditedHandler() : PragmaHandler("arc_cf_code_audited") {}
1229 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1230 Token &NameTok) {
1231 SourceLocation Loc = NameTok.getLocation();
1232 bool IsBegin;
1233
1234 Token Tok;
1235
1236 // Lex the 'begin' or 'end'.
1237 PP.LexUnexpandedToken(Tok);
1238 const IdentifierInfo *BeginEnd = Tok.getIdentifierInfo();
1239 if (BeginEnd && BeginEnd->isStr("begin")) {
1240 IsBegin = true;
1241 } else if (BeginEnd && BeginEnd->isStr("end")) {
1242 IsBegin = false;
1243 } else {
1244 PP.Diag(Tok.getLocation(), diag::err_pp_arc_cf_code_audited_syntax);
1245 return;
1246 }
1247
1248 // Verify that this is followed by EOD.
1249 PP.LexUnexpandedToken(Tok);
1250 if (Tok.isNot(tok::eod))
1251 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
1252
1253 // The start location of the active audit.
1254 SourceLocation BeginLoc = PP.getPragmaARCCFCodeAuditedLoc();
1255
1256 // The start location we want after processing this.
1257 SourceLocation NewLoc;
1258
1259 if (IsBegin) {
1260 // Complain about attempts to re-enter an audit.
1261 if (BeginLoc.isValid()) {
1262 PP.Diag(Loc, diag::err_pp_double_begin_of_arc_cf_code_audited);
1263 PP.Diag(BeginLoc, diag::note_pragma_entered_here);
1264 }
1265 NewLoc = Loc;
1266 } else {
1267 // Complain about attempts to leave an audit that doesn't exist.
1268 if (!BeginLoc.isValid()) {
1269 PP.Diag(Loc, diag::err_pp_unmatched_end_of_arc_cf_code_audited);
1270 return;
1271 }
1272 NewLoc = SourceLocation();
1273 }
1274
1275 PP.setPragmaARCCFCodeAuditedLoc(NewLoc);
1276 }
1277};
1278
Aaron Ballman406ea512012-11-30 19:52:30 +00001279 /// \brief Handle "\#pragma region [...]"
1280 ///
1281 /// The syntax is
1282 /// \code
Dmitri Gribenko9ebd1612012-11-30 20:04:39 +00001283 /// #pragma region [optional name]
1284 /// #pragma endregion [optional comment]
Aaron Ballman406ea512012-11-30 19:52:30 +00001285 /// \endcode
1286 ///
1287 /// \note This is
1288 /// <a href="http://msdn.microsoft.com/en-us/library/b6xkz944(v=vs.80).aspx">editor-only</a>
1289 /// pragma, just skipped by compiler.
1290 struct PragmaRegionHandler : public PragmaHandler {
1291 PragmaRegionHandler(const char *pragma) : PragmaHandler(pragma) { }
1292
1293 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1294 Token &NameTok) {
1295 // #pragma region: endregion matches can be verified
1296 // __pragma(region): no sense, but ignored by msvc
1297 // _Pragma is not valid for MSVC, but there isn't any point
1298 // to handle a _Pragma differently.
1299 }
1300 };
1301
Chris Lattnerb694ba72006-07-02 22:41:36 +00001302} // end anonymous namespace
1303
1304
1305/// RegisterBuiltinPragmas - Install the standard preprocessor pragmas:
James Dennett18a6d792012-06-17 03:26:26 +00001306/// \#pragma GCC poison/system_header/dependency and \#pragma once.
Chris Lattnerb694ba72006-07-02 22:41:36 +00001307void Preprocessor::RegisterBuiltinPragmas() {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001308 AddPragmaHandler(new PragmaOnceHandler());
1309 AddPragmaHandler(new PragmaMarkHandler());
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001310 AddPragmaHandler(new PragmaPushMacroHandler());
1311 AddPragmaHandler(new PragmaPopMacroHandler());
Andy Gibbs9c2ccd62013-04-17 16:16:16 +00001312 AddPragmaHandler(new PragmaMessageHandler(PPCallbacks::PMK_Message));
Mike Stump11289f42009-09-09 15:08:12 +00001313
Chris Lattnerb61448d2009-05-12 18:21:11 +00001314 // #pragma GCC ...
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001315 AddPragmaHandler("GCC", new PragmaPoisonHandler());
1316 AddPragmaHandler("GCC", new PragmaSystemHeaderHandler());
1317 AddPragmaHandler("GCC", new PragmaDependencyHandler());
Douglas Gregor3bde9b12011-06-22 19:41:48 +00001318 AddPragmaHandler("GCC", new PragmaDiagnosticHandler("GCC"));
Andy Gibbs9c2ccd62013-04-17 16:16:16 +00001319 AddPragmaHandler("GCC", new PragmaMessageHandler(PPCallbacks::PMK_Warning,
1320 "GCC"));
1321 AddPragmaHandler("GCC", new PragmaMessageHandler(PPCallbacks::PMK_Error,
1322 "GCC"));
Chris Lattnerb61448d2009-05-12 18:21:11 +00001323 // #pragma clang ...
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001324 AddPragmaHandler("clang", new PragmaPoisonHandler());
1325 AddPragmaHandler("clang", new PragmaSystemHeaderHandler());
Daniel Dunbarb8068c32010-07-28 15:40:33 +00001326 AddPragmaHandler("clang", new PragmaDebugHandler());
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001327 AddPragmaHandler("clang", new PragmaDependencyHandler());
Douglas Gregor3bde9b12011-06-22 19:41:48 +00001328 AddPragmaHandler("clang", new PragmaDiagnosticHandler("clang"));
John McCall32f5fe12011-09-30 05:12:12 +00001329 AddPragmaHandler("clang", new PragmaARCCFCodeAuditedHandler());
Chris Lattnerb61448d2009-05-12 18:21:11 +00001330
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001331 AddPragmaHandler("STDC", new PragmaSTDC_FENV_ACCESSHandler());
1332 AddPragmaHandler("STDC", new PragmaSTDC_CX_LIMITED_RANGEHandler());
Chris Lattner958ee042009-04-19 21:20:35 +00001333 AddPragmaHandler("STDC", new PragmaSTDC_UnknownHandler());
Mike Stump11289f42009-09-09 15:08:12 +00001334
Chris Lattner2ff698d2009-01-16 08:21:25 +00001335 // MS extensions.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001336 if (LangOpts.MicrosoftExt) {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001337 AddPragmaHandler(new PragmaCommentHandler());
Aaron Ballman611306e2012-03-02 22:51:54 +00001338 AddPragmaHandler(new PragmaIncludeAliasHandler());
Aaron Ballman406ea512012-11-30 19:52:30 +00001339 AddPragmaHandler(new PragmaRegionHandler("region"));
1340 AddPragmaHandler(new PragmaRegionHandler("endregion"));
Chris Lattner30c924b2010-06-26 17:11:39 +00001341 }
Chris Lattnerb694ba72006-07-02 22:41:36 +00001342}