blob: e4059eeb6fc0a63d17084d5be97dff7d8fc4d0c6 [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"
Reid Kleckner881dff32013-09-13 22:00:30 +000023#include "llvm/ADT/STLExtras.h"
24#include "llvm/ADT/StringSwitch.h"
Daniel Dunbar211a7872010-08-18 23:09:23 +000025#include "llvm/Support/CrashRecoveryContext.h"
Daniel Dunbarf2cf3292010-08-17 22:32:48 +000026#include "llvm/Support/ErrorHandling.h"
Douglas Gregorc6d5edd2009-07-02 17:08:52 +000027#include <algorithm>
Chris Lattnerb8761832006-06-24 21:31:03 +000028using namespace clang;
29
Reid Kleckner881dff32013-09-13 22:00:30 +000030#include "llvm/Support/raw_ostream.h"
31
Chris Lattnerb8761832006-06-24 21:31:03 +000032// Out-of-line destructor to provide a home for the class.
33PragmaHandler::~PragmaHandler() {
34}
35
Chris Lattner2e155302006-07-03 05:34:41 +000036//===----------------------------------------------------------------------===//
Daniel Dunbard839e772010-06-11 20:10:12 +000037// EmptyPragmaHandler Implementation.
38//===----------------------------------------------------------------------===//
39
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000040EmptyPragmaHandler::EmptyPragmaHandler() {}
Daniel Dunbard839e772010-06-11 20:10:12 +000041
Douglas Gregorc7d65762010-09-09 22:45:38 +000042void EmptyPragmaHandler::HandlePragma(Preprocessor &PP,
43 PragmaIntroducerKind Introducer,
44 Token &FirstToken) {}
Daniel Dunbard839e772010-06-11 20:10:12 +000045
46//===----------------------------------------------------------------------===//
Chris Lattner2e155302006-07-03 05:34:41 +000047// PragmaNamespace Implementation.
48//===----------------------------------------------------------------------===//
49
Chris Lattner2e155302006-07-03 05:34:41 +000050PragmaNamespace::~PragmaNamespace() {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000051 for (llvm::StringMap<PragmaHandler*>::iterator
52 I = Handlers.begin(), E = Handlers.end(); I != E; ++I)
53 delete I->second;
Chris Lattner2e155302006-07-03 05:34:41 +000054}
55
56/// FindHandler - Check to see if there is already a handler for the
57/// specified name. If not, return the handler for the null identifier if it
58/// exists, otherwise return null. If IgnoreNull is true (the default) then
59/// the null handler isn't returned on failure to match.
Chris Lattner0e62c1c2011-07-23 10:55:15 +000060PragmaHandler *PragmaNamespace::FindHandler(StringRef Name,
Chris Lattner2e155302006-07-03 05:34:41 +000061 bool IgnoreNull) const {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000062 if (PragmaHandler *Handler = Handlers.lookup(Name))
63 return Handler;
Chris Lattner0e62c1c2011-07-23 10:55:15 +000064 return IgnoreNull ? 0 : Handlers.lookup(StringRef());
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000065}
Mike Stump11289f42009-09-09 15:08:12 +000066
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000067void PragmaNamespace::AddPragma(PragmaHandler *Handler) {
68 assert(!Handlers.lookup(Handler->getName()) &&
69 "A handler with this name is already registered in this namespace");
70 llvm::StringMapEntry<PragmaHandler *> &Entry =
71 Handlers.GetOrCreateValue(Handler->getName());
72 Entry.setValue(Handler);
Chris Lattner2e155302006-07-03 05:34:41 +000073}
74
Daniel Dunbar40596532008-10-04 19:17:46 +000075void PragmaNamespace::RemovePragmaHandler(PragmaHandler *Handler) {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000076 assert(Handlers.lookup(Handler->getName()) &&
77 "Handler not registered in this namespace");
78 Handlers.erase(Handler->getName());
Daniel Dunbar40596532008-10-04 19:17:46 +000079}
80
Douglas Gregorc7d65762010-09-09 22:45:38 +000081void PragmaNamespace::HandlePragma(Preprocessor &PP,
82 PragmaIntroducerKind Introducer,
83 Token &Tok) {
Chris Lattnerb8761832006-06-24 21:31:03 +000084 // Read the 'namespace' that the directive is in, e.g. STDC. Do not macro
85 // expand it, the user can have a STDC #define, that should not affect this.
86 PP.LexUnexpandedToken(Tok);
Mike Stump11289f42009-09-09 15:08:12 +000087
Chris Lattnerb8761832006-06-24 21:31:03 +000088 // Get the handler for this token. If there is no handler, ignore the pragma.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000089 PragmaHandler *Handler
90 = FindHandler(Tok.getIdentifierInfo() ? Tok.getIdentifierInfo()->getName()
Chris Lattner0e62c1c2011-07-23 10:55:15 +000091 : StringRef(),
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000092 /*IgnoreNull=*/false);
Chris Lattner21656f22009-04-19 21:10:26 +000093 if (Handler == 0) {
94 PP.Diag(Tok, diag::warn_pragma_ignored);
95 return;
96 }
Mike Stump11289f42009-09-09 15:08:12 +000097
Chris Lattnerb8761832006-06-24 21:31:03 +000098 // Otherwise, pass it down.
Douglas Gregorc7d65762010-09-09 22:45:38 +000099 Handler->HandlePragma(PP, Introducer, Tok);
Chris Lattnerb8761832006-06-24 21:31:03 +0000100}
Chris Lattnerb694ba72006-07-02 22:41:36 +0000101
Chris Lattnerb694ba72006-07-02 22:41:36 +0000102//===----------------------------------------------------------------------===//
103// Preprocessor Pragma Directive Handling.
104//===----------------------------------------------------------------------===//
105
James Dennett18a6d792012-06-17 03:26:26 +0000106/// HandlePragmaDirective - The "\#pragma" directive has been parsed. Lex the
Chris Lattnerb694ba72006-07-02 22:41:36 +0000107/// rest of the pragma, passing it to the registered pragma handlers.
Enea Zaffanella5afb04a2013-07-20 20:09:11 +0000108void Preprocessor::HandlePragmaDirective(SourceLocation IntroducerLoc,
109 PragmaIntroducerKind Introducer) {
110 if (Callbacks)
111 Callbacks->PragmaDirective(IntroducerLoc, Introducer);
112
Jordan Rosede1a2922012-06-08 18:06:21 +0000113 if (!PragmasEnabled)
114 return;
115
Chris Lattnerb694ba72006-07-02 22:41:36 +0000116 ++NumPragma;
Mike Stump11289f42009-09-09 15:08:12 +0000117
Chris Lattnerb694ba72006-07-02 22:41:36 +0000118 // Invoke the first level of pragma handlers which reads the namespace id.
Chris Lattner146762e2007-07-20 16:59:19 +0000119 Token Tok;
Enea Zaffanella5afb04a2013-07-20 20:09:11 +0000120 PragmaHandlers->HandlePragma(*this, Introducer, Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000121
Chris Lattnerb694ba72006-07-02 22:41:36 +0000122 // If the pragma handler didn't read the rest of the line, consume it now.
Peter Collingbourne2c9f9662011-02-22 13:49:00 +0000123 if ((CurTokenLexer && CurTokenLexer->isParsingPreprocessorDirective())
124 || (CurPPLexer && CurPPLexer->ParsingPreprocessorDirective))
Chris Lattnerb694ba72006-07-02 22:41:36 +0000125 DiscardUntilEndOfDirective();
126}
127
Argyrios Kyrtzidisf1b64c62012-04-03 16:47:40 +0000128namespace {
129/// \brief Helper class for \see Preprocessor::Handle_Pragma.
130class LexingFor_PragmaRAII {
131 Preprocessor &PP;
132 bool InMacroArgPreExpansion;
133 bool Failed;
134 Token &OutTok;
135 Token PragmaTok;
136
137public:
138 LexingFor_PragmaRAII(Preprocessor &PP, bool InMacroArgPreExpansion,
139 Token &Tok)
140 : PP(PP), InMacroArgPreExpansion(InMacroArgPreExpansion),
141 Failed(false), OutTok(Tok) {
142 if (InMacroArgPreExpansion) {
143 PragmaTok = OutTok;
144 PP.EnableBacktrackAtThisPos();
145 }
146 }
147
148 ~LexingFor_PragmaRAII() {
149 if (InMacroArgPreExpansion) {
150 if (Failed) {
151 PP.CommitBacktrackedTokens();
152 } else {
153 PP.Backtrack();
154 OutTok = PragmaTok;
155 }
156 }
157 }
158
159 void failed() {
160 Failed = true;
161 }
162};
163}
164
Chris Lattnerb694ba72006-07-02 22:41:36 +0000165/// Handle_Pragma - Read a _Pragma directive, slice it up, process it, then
166/// return the first token after the directive. The _Pragma token has just
167/// been read into 'Tok'.
Chris Lattner146762e2007-07-20 16:59:19 +0000168void Preprocessor::Handle_Pragma(Token &Tok) {
Argyrios Kyrtzidisf1b64c62012-04-03 16:47:40 +0000169
170 // This works differently if we are pre-expanding a macro argument.
171 // In that case we don't actually "activate" the pragma now, we only lex it
172 // until we are sure it is lexically correct and then we backtrack so that
173 // we activate the pragma whenever we encounter the tokens again in the token
174 // stream. This ensures that we will activate it in the correct location
175 // or that we will ignore it if it never enters the token stream, e.g:
176 //
177 // #define EMPTY(x)
178 // #define INACTIVE(x) EMPTY(x)
179 // INACTIVE(_Pragma("clang diagnostic ignored \"-Wconversion\""))
180
181 LexingFor_PragmaRAII _PragmaLexing(*this, InMacroArgPreExpansion, Tok);
182
Chris Lattnerb694ba72006-07-02 22:41:36 +0000183 // Remember the pragma token location.
184 SourceLocation PragmaLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000185
Chris Lattnerb694ba72006-07-02 22:41:36 +0000186 // Read the '('.
187 Lex(Tok);
Chris Lattner907dfe92008-11-18 07:59:24 +0000188 if (Tok.isNot(tok::l_paren)) {
189 Diag(PragmaLoc, diag::err__Pragma_malformed);
Argyrios Kyrtzidisf1b64c62012-04-03 16:47:40 +0000190 return _PragmaLexing.failed();
Chris Lattner907dfe92008-11-18 07:59:24 +0000191 }
Chris Lattnerb694ba72006-07-02 22:41:36 +0000192
193 // Read the '"..."'.
194 Lex(Tok);
Richard Smithc98bb4e2013-03-09 23:30:15 +0000195 if (!tok::isStringLiteral(Tok.getKind())) {
Chris Lattner907dfe92008-11-18 07:59:24 +0000196 Diag(PragmaLoc, diag::err__Pragma_malformed);
Richard Smithd67aea22012-03-06 03:21:47 +0000197 // Skip this token, and the ')', if present.
198 if (Tok.isNot(tok::r_paren))
199 Lex(Tok);
200 if (Tok.is(tok::r_paren))
201 Lex(Tok);
Argyrios Kyrtzidisf1b64c62012-04-03 16:47:40 +0000202 return _PragmaLexing.failed();
Richard Smithd67aea22012-03-06 03:21:47 +0000203 }
204
205 if (Tok.hasUDSuffix()) {
206 Diag(Tok, diag::err_invalid_string_udl);
207 // Skip this token, and the ')', if present.
208 Lex(Tok);
209 if (Tok.is(tok::r_paren))
210 Lex(Tok);
Argyrios Kyrtzidisf1b64c62012-04-03 16:47:40 +0000211 return _PragmaLexing.failed();
Chris Lattner907dfe92008-11-18 07:59:24 +0000212 }
Mike Stump11289f42009-09-09 15:08:12 +0000213
Chris Lattnerb694ba72006-07-02 22:41:36 +0000214 // Remember the string.
Argyrios Kyrtzidisf1b64c62012-04-03 16:47:40 +0000215 Token StrTok = Tok;
Chris Lattnerb694ba72006-07-02 22:41:36 +0000216
217 // Read the ')'.
218 Lex(Tok);
Chris Lattner907dfe92008-11-18 07:59:24 +0000219 if (Tok.isNot(tok::r_paren)) {
220 Diag(PragmaLoc, diag::err__Pragma_malformed);
Argyrios Kyrtzidisf1b64c62012-04-03 16:47:40 +0000221 return _PragmaLexing.failed();
Chris Lattner907dfe92008-11-18 07:59:24 +0000222 }
Mike Stump11289f42009-09-09 15:08:12 +0000223
Argyrios Kyrtzidisf1b64c62012-04-03 16:47:40 +0000224 if (InMacroArgPreExpansion)
225 return;
226
Chris Lattner9dc9c202009-02-15 20:52:18 +0000227 SourceLocation RParenLoc = Tok.getLocation();
Argyrios Kyrtzidisf1b64c62012-04-03 16:47:40 +0000228 std::string StrVal = getSpelling(StrTok);
Mike Stump11289f42009-09-09 15:08:12 +0000229
Richard Smithc98bb4e2013-03-09 23:30:15 +0000230 // The _Pragma is lexically sound. Destringize according to C11 6.10.9.1:
231 // "The string literal is destringized by deleting any encoding prefix,
Chris Lattner262d4e32009-01-16 18:59:23 +0000232 // deleting the leading and trailing double-quotes, replacing each escape
233 // sequence \" by a double-quote, and replacing each escape sequence \\ by a
234 // single backslash."
Richard Smithc98bb4e2013-03-09 23:30:15 +0000235 if (StrVal[0] == 'L' || StrVal[0] == 'U' ||
236 (StrVal[0] == 'u' && StrVal[1] != '8'))
Chris Lattnerb694ba72006-07-02 22:41:36 +0000237 StrVal.erase(StrVal.begin());
Richard Smithc98bb4e2013-03-09 23:30:15 +0000238 else if (StrVal[0] == 'u')
239 StrVal.erase(StrVal.begin(), StrVal.begin() + 2);
240
241 if (StrVal[0] == 'R') {
242 // FIXME: C++11 does not specify how to handle raw-string-literals here.
243 // We strip off the 'R', the quotes, the d-char-sequences, and the parens.
244 assert(StrVal[1] == '"' && StrVal[StrVal.size() - 1] == '"' &&
245 "Invalid raw string token!");
246
247 // Measure the length of the d-char-sequence.
248 unsigned NumDChars = 0;
249 while (StrVal[2 + NumDChars] != '(') {
250 assert(NumDChars < (StrVal.size() - 5) / 2 &&
251 "Invalid raw string token!");
252 ++NumDChars;
253 }
254 assert(StrVal[StrVal.size() - 2 - NumDChars] == ')');
255
256 // Remove 'R " d-char-sequence' and 'd-char-sequence "'. We'll replace the
257 // parens below.
258 StrVal.erase(0, 2 + NumDChars);
259 StrVal.erase(StrVal.size() - 1 - NumDChars);
260 } else {
261 assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
262 "Invalid string token!");
263
264 // Remove escaped quotes and escapes.
Benjamin Kramerc2f5f292013-05-04 10:37:20 +0000265 unsigned ResultPos = 1;
Reid Kleckner95e036c2013-09-25 16:42:48 +0000266 for (unsigned i = 1, e = StrVal.size() - 1; i != e; ++i) {
267 // Skip escapes. \\ -> '\' and \" -> '"'.
268 if (StrVal[i] == '\\' && i + 1 < e &&
269 (StrVal[i + 1] == '\\' || StrVal[i + 1] == '"'))
270 ++i;
271 StrVal[ResultPos++] = StrVal[i];
Richard Smithc98bb4e2013-03-09 23:30:15 +0000272 }
Reid Kleckner95e036c2013-09-25 16:42:48 +0000273 StrVal.erase(StrVal.begin() + ResultPos, StrVal.end() - 1);
Richard Smithc98bb4e2013-03-09 23:30:15 +0000274 }
Mike Stump11289f42009-09-09 15:08:12 +0000275
Chris Lattnerb694ba72006-07-02 22:41:36 +0000276 // Remove the front quote, replacing it with a space, so that the pragma
277 // contents appear to have a space before them.
278 StrVal[0] = ' ';
Mike Stump11289f42009-09-09 15:08:12 +0000279
Chris Lattnerfa217bd2009-03-08 08:08:45 +0000280 // Replace the terminating quote with a \n.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000281 StrVal[StrVal.size()-1] = '\n';
Mike Stump11289f42009-09-09 15:08:12 +0000282
Peter Collingbournef29ce972011-02-22 13:49:06 +0000283 // Plop the string (including the newline and trailing null) into a buffer
284 // where we can lex it.
285 Token TmpTok;
286 TmpTok.startToken();
Dmitri Gribenkob8e9e752012-09-24 21:07:17 +0000287 CreateString(StrVal, TmpTok);
Peter Collingbournef29ce972011-02-22 13:49:06 +0000288 SourceLocation TokLoc = TmpTok.getLocation();
289
290 // Make and enter a lexer object so that we lex and expand the tokens just
291 // like any others.
292 Lexer *TL = Lexer::Create_PragmaLexer(TokLoc, PragmaLoc, RParenLoc,
293 StrVal.size(), *this);
294
295 EnterSourceFileWithLexer(TL, 0);
296
297 // With everything set up, lex this as a #pragma directive.
Enea Zaffanella5afb04a2013-07-20 20:09:11 +0000298 HandlePragmaDirective(PragmaLoc, PIK__Pragma);
John McCall89e925d2010-08-28 22:34:47 +0000299
300 // Finally, return whatever came after the pragma directive.
301 return Lex(Tok);
302}
303
304/// HandleMicrosoft__pragma - Like Handle_Pragma except the pragma text
305/// is not enclosed within a string literal.
306void Preprocessor::HandleMicrosoft__pragma(Token &Tok) {
307 // Remember the pragma token location.
308 SourceLocation PragmaLoc = Tok.getLocation();
309
310 // Read the '('.
311 Lex(Tok);
312 if (Tok.isNot(tok::l_paren)) {
313 Diag(PragmaLoc, diag::err__Pragma_malformed);
314 return;
315 }
316
Peter Collingbournef29ce972011-02-22 13:49:06 +0000317 // Get the tokens enclosed within the __pragma(), as well as the final ')'.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000318 SmallVector<Token, 32> PragmaToks;
John McCall89e925d2010-08-28 22:34:47 +0000319 int NumParens = 0;
320 Lex(Tok);
321 while (Tok.isNot(tok::eof)) {
Peter Collingbournef29ce972011-02-22 13:49:06 +0000322 PragmaToks.push_back(Tok);
John McCall89e925d2010-08-28 22:34:47 +0000323 if (Tok.is(tok::l_paren))
324 NumParens++;
325 else if (Tok.is(tok::r_paren) && NumParens-- == 0)
326 break;
John McCall89e925d2010-08-28 22:34:47 +0000327 Lex(Tok);
328 }
329
John McCall49039d42010-08-29 01:09:54 +0000330 if (Tok.is(tok::eof)) {
331 Diag(PragmaLoc, diag::err_unterminated___pragma);
332 return;
333 }
334
Peter Collingbournef29ce972011-02-22 13:49:06 +0000335 PragmaToks.front().setFlag(Token::LeadingSpace);
John McCall89e925d2010-08-28 22:34:47 +0000336
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000337 // Replace the ')' with an EOD to mark the end of the pragma.
338 PragmaToks.back().setKind(tok::eod);
Peter Collingbournef29ce972011-02-22 13:49:06 +0000339
340 Token *TokArray = new Token[PragmaToks.size()];
341 std::copy(PragmaToks.begin(), PragmaToks.end(), TokArray);
342
343 // Push the tokens onto the stack.
344 EnterTokenStream(TokArray, PragmaToks.size(), true, true);
345
346 // With everything set up, lex this as a #pragma directive.
Enea Zaffanella5afb04a2013-07-20 20:09:11 +0000347 HandlePragmaDirective(PragmaLoc, PIK___pragma);
John McCall89e925d2010-08-28 22:34:47 +0000348
349 // Finally, return whatever came after the pragma directive.
350 return Lex(Tok);
351}
352
James Dennett18a6d792012-06-17 03:26:26 +0000353/// HandlePragmaOnce - Handle \#pragma once. OnceTok is the 'once'.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000354///
Chris Lattner146762e2007-07-20 16:59:19 +0000355void Preprocessor::HandlePragmaOnce(Token &OnceTok) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000356 if (isInPrimaryFile()) {
357 Diag(OnceTok, diag::pp_pragma_once_in_main_file);
358 return;
359 }
Mike Stump11289f42009-09-09 15:08:12 +0000360
Chris Lattnerb694ba72006-07-02 22:41:36 +0000361 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000362 // Mark the file as a once-only file now.
Chris Lattnerd32480d2009-01-17 06:22:33 +0000363 HeaderInfo.MarkFileIncludeOnce(getCurrentFileLexer()->getFileEntry());
Chris Lattnerb694ba72006-07-02 22:41:36 +0000364}
365
Chris Lattnerc2383312007-12-19 19:38:36 +0000366void Preprocessor::HandlePragmaMark() {
Ted Kremenek76c34412008-11-19 22:21:33 +0000367 assert(CurPPLexer && "No current lexer?");
Chris Lattnerd9efb6e2009-06-15 05:02:34 +0000368 if (CurLexer)
369 CurLexer->ReadToEndOfLine();
370 else
371 CurPTHLexer->DiscardToEndOfLine();
Chris Lattnerc2383312007-12-19 19:38:36 +0000372}
373
374
James Dennett18a6d792012-06-17 03:26:26 +0000375/// HandlePragmaPoison - Handle \#pragma GCC poison. PoisonTok is the 'poison'.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000376///
Chris Lattner146762e2007-07-20 16:59:19 +0000377void Preprocessor::HandlePragmaPoison(Token &PoisonTok) {
378 Token Tok;
Chris Lattner538d7f32006-07-20 04:31:52 +0000379
Chris Lattnerb694ba72006-07-02 22:41:36 +0000380 while (1) {
381 // Read the next token to poison. While doing this, pretend that we are
382 // skipping while reading the identifier to poison.
383 // This avoids errors on code like:
384 // #pragma GCC poison X
385 // #pragma GCC poison X
Ted Kremenek551c82a2008-11-18 01:12:54 +0000386 if (CurPPLexer) CurPPLexer->LexingRawMode = true;
Chris Lattnerb694ba72006-07-02 22:41:36 +0000387 LexUnexpandedToken(Tok);
Ted Kremenek551c82a2008-11-18 01:12:54 +0000388 if (CurPPLexer) CurPPLexer->LexingRawMode = false;
Mike Stump11289f42009-09-09 15:08:12 +0000389
Chris Lattnerb694ba72006-07-02 22:41:36 +0000390 // If we reached the end of line, we're done.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000391 if (Tok.is(tok::eod)) return;
Mike Stump11289f42009-09-09 15:08:12 +0000392
Chris Lattnerb694ba72006-07-02 22:41:36 +0000393 // Can only poison identifiers.
Abramo Bagnaraea4f7c72010-12-22 08:23:18 +0000394 if (Tok.isNot(tok::raw_identifier)) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000395 Diag(Tok, diag::err_pp_invalid_poison);
396 return;
397 }
Mike Stump11289f42009-09-09 15:08:12 +0000398
Chris Lattnercefc7682006-07-08 08:28:12 +0000399 // Look up the identifier info for the token. We disabled identifier lookup
400 // by saying we're skipping contents, so we need to do this manually.
401 IdentifierInfo *II = LookUpIdentifierInfo(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000402
Chris Lattnerb694ba72006-07-02 22:41:36 +0000403 // Already poisoned.
404 if (II->isPoisoned()) continue;
Mike Stump11289f42009-09-09 15:08:12 +0000405
Chris Lattnerb694ba72006-07-02 22:41:36 +0000406 // If this is a macro identifier, emit a warning.
Chris Lattner259716a2007-10-07 08:04:56 +0000407 if (II->hasMacroDefinition())
Chris Lattnerb694ba72006-07-02 22:41:36 +0000408 Diag(Tok, diag::pp_poisoning_existing_macro);
Mike Stump11289f42009-09-09 15:08:12 +0000409
Chris Lattnerb694ba72006-07-02 22:41:36 +0000410 // Finally, poison it!
411 II->setIsPoisoned();
Douglas Gregor935bc7a22011-10-27 09:33:13 +0000412 if (II->isFromAST())
413 II->setChangedSinceDeserialization();
Chris Lattnerb694ba72006-07-02 22:41:36 +0000414 }
415}
416
James Dennett18a6d792012-06-17 03:26:26 +0000417/// HandlePragmaSystemHeader - Implement \#pragma GCC system_header. We know
Chris Lattnerb694ba72006-07-02 22:41:36 +0000418/// that the whole directive has been parsed.
Chris Lattner146762e2007-07-20 16:59:19 +0000419void Preprocessor::HandlePragmaSystemHeader(Token &SysHeaderTok) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000420 if (isInPrimaryFile()) {
421 Diag(SysHeaderTok, diag::pp_pragma_sysheader_in_main_file);
422 return;
423 }
Mike Stump11289f42009-09-09 15:08:12 +0000424
Chris Lattnerb694ba72006-07-02 22:41:36 +0000425 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
Ted Kremenek300590b2008-11-20 01:45:11 +0000426 PreprocessorLexer *TheLexer = getCurrentFileLexer();
Mike Stump11289f42009-09-09 15:08:12 +0000427
Chris Lattnerb694ba72006-07-02 22:41:36 +0000428 // Mark the file as a system header.
Chris Lattnerd32480d2009-01-17 06:22:33 +0000429 HeaderInfo.MarkFileSystemHeader(TheLexer->getFileEntry());
Mike Stump11289f42009-09-09 15:08:12 +0000430
431
Chris Lattnerd9efb6e2009-06-15 05:02:34 +0000432 PresumedLoc PLoc = SourceMgr.getPresumedLoc(SysHeaderTok.getLocation());
Douglas Gregor453b0122010-11-12 07:15:47 +0000433 if (PLoc.isInvalid())
434 return;
435
Jay Foad9a6b0982011-06-21 15:13:30 +0000436 unsigned FilenameID = SourceMgr.getLineTableFilenameID(PLoc.getFilename());
Mike Stump11289f42009-09-09 15:08:12 +0000437
Chris Lattner3bdc7672011-05-22 22:10:16 +0000438 // Notify the client, if desired, that we are in a new source file.
439 if (Callbacks)
440 Callbacks->FileChanged(SysHeaderTok.getLocation(),
441 PPCallbacks::SystemHeaderPragma, SrcMgr::C_System);
442
Chris Lattnerd9efb6e2009-06-15 05:02:34 +0000443 // Emit a line marker. This will change any source locations from this point
444 // forward to realize they are in a system header.
445 // Create a line note with this information.
Jordan Rose111c4a62013-04-17 19:09:18 +0000446 SourceMgr.AddLineNote(SysHeaderTok.getLocation(), PLoc.getLine()+1,
447 FilenameID, /*IsEntry=*/false, /*IsExit=*/false,
448 /*IsSystem=*/true, /*IsExternC=*/false);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000449}
450
James Dennett18a6d792012-06-17 03:26:26 +0000451/// HandlePragmaDependency - Handle \#pragma GCC dependency "foo" blah.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000452///
Chris Lattner146762e2007-07-20 16:59:19 +0000453void Preprocessor::HandlePragmaDependency(Token &DependencyTok) {
454 Token FilenameTok;
Ted Kremenek551c82a2008-11-18 01:12:54 +0000455 CurPPLexer->LexIncludeFilename(FilenameTok);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000456
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000457 // If the token kind is EOD, the error has already been diagnosed.
458 if (FilenameTok.is(tok::eod))
Chris Lattnerb694ba72006-07-02 22:41:36 +0000459 return;
Mike Stump11289f42009-09-09 15:08:12 +0000460
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000461 // Reserve a buffer to get the spelling.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000462 SmallString<128> FilenameBuffer;
Douglas Gregordc970f02010-03-16 22:30:13 +0000463 bool Invalid = false;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000464 StringRef Filename = getSpelling(FilenameTok, FilenameBuffer, &Invalid);
Douglas Gregordc970f02010-03-16 22:30:13 +0000465 if (Invalid)
466 return;
Mike Stump11289f42009-09-09 15:08:12 +0000467
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000468 bool isAngled =
469 GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000470 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
471 // error.
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000472 if (Filename.empty())
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000473 return;
Mike Stump11289f42009-09-09 15:08:12 +0000474
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000475 // Search include directories for this file.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000476 const DirectoryLookup *CurDir;
Lawrence Crowlb53e5482013-06-20 21:14:14 +0000477 const FileEntry *File = LookupFile(FilenameTok.getLocation(), Filename,
478 isAngled, 0, CurDir, NULL, NULL, NULL);
Chris Lattner97b8e842008-11-18 08:02:48 +0000479 if (File == 0) {
Eli Friedman3781a362011-08-30 23:07:51 +0000480 if (!SuppressIncludeNotFoundError)
481 Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
Chris Lattner97b8e842008-11-18 08:02:48 +0000482 return;
483 }
Mike Stump11289f42009-09-09 15:08:12 +0000484
Chris Lattnerd32480d2009-01-17 06:22:33 +0000485 const FileEntry *CurFile = getCurrentFileLexer()->getFileEntry();
Chris Lattnerb694ba72006-07-02 22:41:36 +0000486
487 // If this file is older than the file it depends on, emit a diagnostic.
488 if (CurFile && CurFile->getModificationTime() < File->getModificationTime()) {
489 // Lex tokens at the end of the message and include them in the message.
490 std::string Message;
491 Lex(DependencyTok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000492 while (DependencyTok.isNot(tok::eod)) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000493 Message += getSpelling(DependencyTok) + " ";
494 Lex(DependencyTok);
495 }
Mike Stump11289f42009-09-09 15:08:12 +0000496
Chris Lattnerf0b04972010-09-05 23:16:09 +0000497 // Remove the trailing ' ' if present.
498 if (!Message.empty())
499 Message.erase(Message.end()-1);
Chris Lattner97b8e842008-11-18 08:02:48 +0000500 Diag(FilenameTok, diag::pp_out_of_date_dependency) << Message;
Chris Lattnerb694ba72006-07-02 22:41:36 +0000501 }
502}
503
Reid Kleckner002562a2013-05-06 21:02:12 +0000504/// ParsePragmaPushOrPopMacro - Handle parsing of pragma push_macro/pop_macro.
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000505/// Return the IdentifierInfo* associated with the macro to push or pop.
506IdentifierInfo *Preprocessor::ParsePragmaPushOrPopMacro(Token &Tok) {
507 // Remember the pragma token location.
508 Token PragmaTok = Tok;
509
510 // Read the '('.
511 Lex(Tok);
512 if (Tok.isNot(tok::l_paren)) {
513 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
514 << getSpelling(PragmaTok);
515 return 0;
516 }
517
518 // Read the macro name string.
519 Lex(Tok);
520 if (Tok.isNot(tok::string_literal)) {
521 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
522 << getSpelling(PragmaTok);
523 return 0;
524 }
525
Richard Smithd67aea22012-03-06 03:21:47 +0000526 if (Tok.hasUDSuffix()) {
527 Diag(Tok, diag::err_invalid_string_udl);
528 return 0;
529 }
530
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000531 // Remember the macro string.
532 std::string StrVal = getSpelling(Tok);
533
534 // Read the ')'.
535 Lex(Tok);
536 if (Tok.isNot(tok::r_paren)) {
537 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
538 << getSpelling(PragmaTok);
539 return 0;
540 }
541
542 assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
543 "Invalid string token!");
544
545 // Create a Token from the string.
546 Token MacroTok;
547 MacroTok.startToken();
Abramo Bagnaraea4f7c72010-12-22 08:23:18 +0000548 MacroTok.setKind(tok::raw_identifier);
Dmitri Gribenkob8e9e752012-09-24 21:07:17 +0000549 CreateString(StringRef(&StrVal[1], StrVal.size() - 2), MacroTok);
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000550
551 // Get the IdentifierInfo of MacroToPushTok.
552 return LookUpIdentifierInfo(MacroTok);
553}
554
James Dennett18a6d792012-06-17 03:26:26 +0000555/// \brief Handle \#pragma push_macro.
556///
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000557/// The syntax is:
James Dennett18a6d792012-06-17 03:26:26 +0000558/// \code
Dmitri Gribenko9ebd1612012-11-30 20:04:39 +0000559/// #pragma push_macro("macro")
James Dennett18a6d792012-06-17 03:26:26 +0000560/// \endcode
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000561void Preprocessor::HandlePragmaPushMacro(Token &PushMacroTok) {
562 // Parse the pragma directive and get the macro IdentifierInfo*.
563 IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PushMacroTok);
564 if (!IdentInfo) return;
565
566 // Get the MacroInfo associated with IdentInfo.
567 MacroInfo *MI = getMacroInfo(IdentInfo);
568
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000569 if (MI) {
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000570 // Allow the original MacroInfo to be redefined later.
571 MI->setIsAllowRedefinitionsWithoutWarning(true);
572 }
573
574 // Push the cloned MacroInfo so we can retrieve it later.
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +0000575 PragmaPushMacroInfo[IdentInfo].push_back(MI);
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000576}
577
James Dennett18a6d792012-06-17 03:26:26 +0000578/// \brief Handle \#pragma pop_macro.
579///
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000580/// The syntax is:
James Dennett18a6d792012-06-17 03:26:26 +0000581/// \code
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000582/// #pragma pop_macro("macro")
James Dennett18a6d792012-06-17 03:26:26 +0000583/// \endcode
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000584void Preprocessor::HandlePragmaPopMacro(Token &PopMacroTok) {
585 SourceLocation MessageLoc = PopMacroTok.getLocation();
586
587 // Parse the pragma directive and get the macro IdentifierInfo*.
588 IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PopMacroTok);
589 if (!IdentInfo) return;
590
591 // Find the vector<MacroInfo*> associated with the macro.
592 llvm::DenseMap<IdentifierInfo*, std::vector<MacroInfo*> >::iterator iter =
593 PragmaPushMacroInfo.find(IdentInfo);
594 if (iter != PragmaPushMacroInfo.end()) {
Alexander Kornienko8b3f6232012-08-29 00:20:03 +0000595 // Forget the MacroInfo currently associated with IdentInfo.
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +0000596 if (MacroDirective *CurrentMD = getMacroDirective(IdentInfo)) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +0000597 MacroInfo *MI = CurrentMD->getMacroInfo();
598 if (MI->isWarnIfUnused())
599 WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());
600 appendMacroDirective(IdentInfo, AllocateUndefMacroDirective(MessageLoc));
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000601 }
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000602
603 // Get the MacroInfo we want to reinstall.
604 MacroInfo *MacroToReInstall = iter->second.back();
605
Alexander Kornienkoc0b49282012-08-29 16:56:24 +0000606 if (MacroToReInstall) {
607 // Reinstall the previously pushed macro.
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +0000608 appendDefMacroDirective(IdentInfo, MacroToReInstall, MessageLoc,
609 /*isImported=*/false);
Alexander Kornienkoc0b49282012-08-29 16:56:24 +0000610 }
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000611
612 // Pop PragmaPushMacroInfo stack.
613 iter->second.pop_back();
614 if (iter->second.size() == 0)
615 PragmaPushMacroInfo.erase(iter);
616 } else {
617 Diag(MessageLoc, diag::warn_pragma_pop_macro_no_push)
618 << IdentInfo->getName();
619 }
620}
Chris Lattnerb694ba72006-07-02 22:41:36 +0000621
Aaron Ballman611306e2012-03-02 22:51:54 +0000622void Preprocessor::HandlePragmaIncludeAlias(Token &Tok) {
623 // We will either get a quoted filename or a bracketed filename, and we
624 // have to track which we got. The first filename is the source name,
625 // and the second name is the mapped filename. If the first is quoted,
626 // the second must be as well (cannot mix and match quotes and brackets).
Aaron Ballman611306e2012-03-02 22:51:54 +0000627
628 // Get the open paren
629 Lex(Tok);
630 if (Tok.isNot(tok::l_paren)) {
631 Diag(Tok, diag::warn_pragma_include_alias_expected) << "(";
632 return;
633 }
634
635 // We expect either a quoted string literal, or a bracketed name
636 Token SourceFilenameTok;
637 CurPPLexer->LexIncludeFilename(SourceFilenameTok);
638 if (SourceFilenameTok.is(tok::eod)) {
639 // The diagnostic has already been handled
640 return;
641 }
642
643 StringRef SourceFileName;
644 SmallString<128> FileNameBuffer;
645 if (SourceFilenameTok.is(tok::string_literal) ||
646 SourceFilenameTok.is(tok::angle_string_literal)) {
647 SourceFileName = getSpelling(SourceFilenameTok, FileNameBuffer);
648 } else if (SourceFilenameTok.is(tok::less)) {
649 // This could be a path instead of just a name
650 FileNameBuffer.push_back('<');
651 SourceLocation End;
652 if (ConcatenateIncludeName(FileNameBuffer, End))
653 return; // Diagnostic already emitted
654 SourceFileName = FileNameBuffer.str();
655 } else {
656 Diag(Tok, diag::warn_pragma_include_alias_expected_filename);
657 return;
658 }
659 FileNameBuffer.clear();
660
661 // Now we expect a comma, followed by another include name
662 Lex(Tok);
663 if (Tok.isNot(tok::comma)) {
664 Diag(Tok, diag::warn_pragma_include_alias_expected) << ",";
665 return;
666 }
667
668 Token ReplaceFilenameTok;
669 CurPPLexer->LexIncludeFilename(ReplaceFilenameTok);
670 if (ReplaceFilenameTok.is(tok::eod)) {
671 // The diagnostic has already been handled
672 return;
673 }
674
675 StringRef ReplaceFileName;
676 if (ReplaceFilenameTok.is(tok::string_literal) ||
677 ReplaceFilenameTok.is(tok::angle_string_literal)) {
678 ReplaceFileName = getSpelling(ReplaceFilenameTok, FileNameBuffer);
679 } else if (ReplaceFilenameTok.is(tok::less)) {
680 // This could be a path instead of just a name
681 FileNameBuffer.push_back('<');
682 SourceLocation End;
683 if (ConcatenateIncludeName(FileNameBuffer, End))
684 return; // Diagnostic already emitted
685 ReplaceFileName = FileNameBuffer.str();
686 } else {
687 Diag(Tok, diag::warn_pragma_include_alias_expected_filename);
688 return;
689 }
690
691 // Finally, we expect the closing paren
692 Lex(Tok);
693 if (Tok.isNot(tok::r_paren)) {
694 Diag(Tok, diag::warn_pragma_include_alias_expected) << ")";
695 return;
696 }
697
698 // Now that we have the source and target filenames, we need to make sure
699 // they're both of the same type (angled vs non-angled)
700 StringRef OriginalSource = SourceFileName;
701
702 bool SourceIsAngled =
703 GetIncludeFilenameSpelling(SourceFilenameTok.getLocation(),
704 SourceFileName);
705 bool ReplaceIsAngled =
706 GetIncludeFilenameSpelling(ReplaceFilenameTok.getLocation(),
707 ReplaceFileName);
708 if (!SourceFileName.empty() && !ReplaceFileName.empty() &&
709 (SourceIsAngled != ReplaceIsAngled)) {
710 unsigned int DiagID;
711 if (SourceIsAngled)
712 DiagID = diag::warn_pragma_include_alias_mismatch_angle;
713 else
714 DiagID = diag::warn_pragma_include_alias_mismatch_quote;
715
716 Diag(SourceFilenameTok.getLocation(), DiagID)
717 << SourceFileName
718 << ReplaceFileName;
719
720 return;
721 }
722
723 // Now we can let the include handler know about this mapping
724 getHeaderSearchInfo().AddIncludeAlias(OriginalSource, ReplaceFileName);
725}
726
Chris Lattnerb694ba72006-07-02 22:41:36 +0000727/// AddPragmaHandler - Add the specified pragma handler to the preprocessor.
728/// If 'Namespace' is non-null, then it is a token required to exist on the
729/// pragma line before the pragma string starts, e.g. "STDC" or "GCC".
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000730void Preprocessor::AddPragmaHandler(StringRef Namespace,
Chris Lattnerb694ba72006-07-02 22:41:36 +0000731 PragmaHandler *Handler) {
732 PragmaNamespace *InsertNS = PragmaHandlers;
Mike Stump11289f42009-09-09 15:08:12 +0000733
Chris Lattnerb694ba72006-07-02 22:41:36 +0000734 // If this is specified to be in a namespace, step down into it.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000735 if (!Namespace.empty()) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000736 // If there is already a pragma handler with the name of this namespace,
737 // we either have an error (directive with the same name as a namespace) or
738 // we already have the namespace to insert into.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000739 if (PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace)) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000740 InsertNS = Existing->getIfNamespace();
741 assert(InsertNS != 0 && "Cannot have a pragma namespace and pragma"
742 " handler with the same name!");
743 } else {
744 // Otherwise, this namespace doesn't exist yet, create and insert the
745 // handler for it.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000746 InsertNS = new PragmaNamespace(Namespace);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000747 PragmaHandlers->AddPragma(InsertNS);
748 }
749 }
Mike Stump11289f42009-09-09 15:08:12 +0000750
Chris Lattnerb694ba72006-07-02 22:41:36 +0000751 // Check to make sure we don't already have a pragma for this identifier.
752 assert(!InsertNS->FindHandler(Handler->getName()) &&
753 "Pragma handler already exists for this identifier!");
754 InsertNS->AddPragma(Handler);
755}
756
Daniel Dunbar40596532008-10-04 19:17:46 +0000757/// RemovePragmaHandler - Remove the specific pragma handler from the
758/// preprocessor. If \arg Namespace is non-null, then it should be the
759/// namespace that \arg Handler was added to. It is an error to remove
760/// a handler that has not been registered.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000761void Preprocessor::RemovePragmaHandler(StringRef Namespace,
Daniel Dunbar40596532008-10-04 19:17:46 +0000762 PragmaHandler *Handler) {
763 PragmaNamespace *NS = PragmaHandlers;
Mike Stump11289f42009-09-09 15:08:12 +0000764
Daniel Dunbar40596532008-10-04 19:17:46 +0000765 // If this is specified to be in a namespace, step down into it.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000766 if (!Namespace.empty()) {
767 PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace);
Daniel Dunbar40596532008-10-04 19:17:46 +0000768 assert(Existing && "Namespace containing handler does not exist!");
769
770 NS = Existing->getIfNamespace();
771 assert(NS && "Invalid namespace, registered as a regular pragma handler!");
772 }
773
774 NS->RemovePragmaHandler(Handler);
Mike Stump11289f42009-09-09 15:08:12 +0000775
Daniel Dunbar40596532008-10-04 19:17:46 +0000776 // If this is a non-default namespace and it is now empty, remove
777 // it.
Argyrios Kyrtzidis7ce75262012-01-06 00:22:09 +0000778 if (NS != PragmaHandlers && NS->IsEmpty()) {
Daniel Dunbar40596532008-10-04 19:17:46 +0000779 PragmaHandlers->RemovePragmaHandler(NS);
Argyrios Kyrtzidis7ce75262012-01-06 00:22:09 +0000780 delete NS;
781 }
Daniel Dunbar40596532008-10-04 19:17:46 +0000782}
783
Peter Collingbourne3bffa522011-02-14 01:42:24 +0000784bool Preprocessor::LexOnOffSwitch(tok::OnOffSwitch &Result) {
785 Token Tok;
786 LexUnexpandedToken(Tok);
787
788 if (Tok.isNot(tok::identifier)) {
789 Diag(Tok, diag::ext_on_off_switch_syntax);
790 return true;
791 }
792 IdentifierInfo *II = Tok.getIdentifierInfo();
793 if (II->isStr("ON"))
794 Result = tok::OOS_ON;
795 else if (II->isStr("OFF"))
796 Result = tok::OOS_OFF;
797 else if (II->isStr("DEFAULT"))
798 Result = tok::OOS_DEFAULT;
799 else {
800 Diag(Tok, diag::ext_on_off_switch_syntax);
801 return true;
802 }
803
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000804 // Verify that this is followed by EOD.
Peter Collingbourne3bffa522011-02-14 01:42:24 +0000805 LexUnexpandedToken(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000806 if (Tok.isNot(tok::eod))
807 Diag(Tok, diag::ext_pragma_syntax_eod);
Peter Collingbourne3bffa522011-02-14 01:42:24 +0000808 return false;
809}
810
Chris Lattnerb694ba72006-07-02 22:41:36 +0000811namespace {
James Dennett18a6d792012-06-17 03:26:26 +0000812/// PragmaOnceHandler - "\#pragma once" marks the file as atomically included.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000813struct PragmaOnceHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000814 PragmaOnceHandler() : PragmaHandler("once") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000815 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
816 Token &OnceTok) {
Chris Lattnerce2ab6f2009-04-14 05:07:49 +0000817 PP.CheckEndOfDirective("pragma once");
Chris Lattnerb694ba72006-07-02 22:41:36 +0000818 PP.HandlePragmaOnce(OnceTok);
819 }
820};
821
James Dennett18a6d792012-06-17 03:26:26 +0000822/// PragmaMarkHandler - "\#pragma mark ..." is ignored by the compiler, and the
Chris Lattnerc2383312007-12-19 19:38:36 +0000823/// rest of the line is not lexed.
824struct PragmaMarkHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000825 PragmaMarkHandler() : PragmaHandler("mark") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000826 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
827 Token &MarkTok) {
Chris Lattnerc2383312007-12-19 19:38:36 +0000828 PP.HandlePragmaMark();
829 }
830};
831
James Dennett18a6d792012-06-17 03:26:26 +0000832/// PragmaPoisonHandler - "\#pragma poison x" marks x as not usable.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000833struct PragmaPoisonHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000834 PragmaPoisonHandler() : PragmaHandler("poison") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000835 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
836 Token &PoisonTok) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000837 PP.HandlePragmaPoison(PoisonTok);
838 }
839};
840
James Dennett18a6d792012-06-17 03:26:26 +0000841/// PragmaSystemHeaderHandler - "\#pragma system_header" marks the current file
Chris Lattnerc2383312007-12-19 19:38:36 +0000842/// as a system header, which silences warnings in it.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000843struct PragmaSystemHeaderHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000844 PragmaSystemHeaderHandler() : PragmaHandler("system_header") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000845 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
846 Token &SHToken) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000847 PP.HandlePragmaSystemHeader(SHToken);
Chris Lattnerce2ab6f2009-04-14 05:07:49 +0000848 PP.CheckEndOfDirective("pragma");
Chris Lattnerb694ba72006-07-02 22:41:36 +0000849 }
850};
851struct PragmaDependencyHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000852 PragmaDependencyHandler() : PragmaHandler("dependency") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000853 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
854 Token &DepToken) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000855 PP.HandlePragmaDependency(DepToken);
856 }
857};
Mike Stump11289f42009-09-09 15:08:12 +0000858
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000859struct PragmaDebugHandler : public PragmaHandler {
860 PragmaDebugHandler() : PragmaHandler("__debug") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000861 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
862 Token &DepToken) {
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000863 Token Tok;
864 PP.LexUnexpandedToken(Tok);
865 if (Tok.isNot(tok::identifier)) {
Douglas Gregor3cc26482010-08-30 15:15:34 +0000866 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000867 return;
868 }
869 IdentifierInfo *II = Tok.getIdentifierInfo();
870
Daniel Dunbarf2cf3292010-08-17 22:32:48 +0000871 if (II->isStr("assert")) {
David Blaikie83d382b2011-09-23 05:06:16 +0000872 llvm_unreachable("This is an assertion!");
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000873 } else if (II->isStr("crash")) {
David Blaikie5bd4c2a2012-08-21 18:56:49 +0000874 LLVM_BUILTIN_TRAP;
David Blaikie5d577a22012-06-29 22:03:56 +0000875 } else if (II->isStr("parser_crash")) {
876 Token Crasher;
877 Crasher.setKind(tok::annot_pragma_parser_crash);
878 PP.EnterToken(Crasher);
Daniel Dunbarf2cf3292010-08-17 22:32:48 +0000879 } else if (II->isStr("llvm_fatal_error")) {
880 llvm::report_fatal_error("#pragma clang __debug llvm_fatal_error");
881 } else if (II->isStr("llvm_unreachable")) {
882 llvm_unreachable("#pragma clang __debug llvm_unreachable");
883 } else if (II->isStr("overflow_stack")) {
884 DebugOverflowStack();
Daniel Dunbar211a7872010-08-18 23:09:23 +0000885 } else if (II->isStr("handle_crash")) {
886 llvm::CrashRecoveryContext *CRC =llvm::CrashRecoveryContext::GetCurrent();
887 if (CRC)
888 CRC->HandleCrash();
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000889 } else if (II->isStr("captured")) {
890 HandleCaptured(PP);
Daniel Dunbarf2cf3292010-08-17 22:32:48 +0000891 } else {
892 PP.Diag(Tok, diag::warn_pragma_debug_unexpected_command)
893 << II->getName();
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000894 }
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000895
896 PPCallbacks *Callbacks = PP.getPPCallbacks();
897 if (Callbacks)
898 Callbacks->PragmaDebug(Tok.getLocation(), II->getName());
899 }
900
901 void HandleCaptured(Preprocessor &PP) {
902 // Skip if emitting preprocessed output.
903 if (PP.isPreprocessedOutput())
904 return;
905
906 Token Tok;
907 PP.LexUnexpandedToken(Tok);
908
909 if (Tok.isNot(tok::eod)) {
910 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol)
911 << "pragma clang __debug captured";
912 return;
913 }
914
915 SourceLocation NameLoc = Tok.getLocation();
916 Token *Toks = PP.getPreprocessorAllocator().Allocate<Token>(1);
917 Toks->startToken();
918 Toks->setKind(tok::annot_pragma_captured);
919 Toks->setLocation(NameLoc);
920
921 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
922 /*OwnsTokens=*/false);
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000923 }
924
Francois Pichet2e11f5d2011-05-25 16:15:03 +0000925// Disable MSVC warning about runtime stack overflow.
926#ifdef _MSC_VER
927 #pragma warning(disable : 4717)
928#endif
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000929 void DebugOverflowStack() {
930 DebugOverflowStack();
931 }
Francois Pichet2e11f5d2011-05-25 16:15:03 +0000932#ifdef _MSC_VER
933 #pragma warning(default : 4717)
934#endif
935
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000936};
937
James Dennett18a6d792012-06-17 03:26:26 +0000938/// PragmaDiagnosticHandler - e.g. '\#pragma GCC diagnostic ignored "-Wformat"'
Chris Lattner504af112009-04-19 23:16:58 +0000939struct PragmaDiagnosticHandler : public PragmaHandler {
Douglas Gregor3bde9b12011-06-22 19:41:48 +0000940private:
941 const char *Namespace;
Chris Lattnerfb42a182009-07-12 21:18:45 +0000942public:
Douglas Gregor3bde9b12011-06-22 19:41:48 +0000943 explicit PragmaDiagnosticHandler(const char *NS) :
944 PragmaHandler("diagnostic"), Namespace(NS) {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000945 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
946 Token &DiagToken) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000947 SourceLocation DiagLoc = DiagToken.getLocation();
Chris Lattner504af112009-04-19 23:16:58 +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);
Chris Lattner504af112009-04-19 23:16:58 +0000952 return;
953 }
954 IdentifierInfo *II = Tok.getIdentifierInfo();
Douglas Gregor3bde9b12011-06-22 19:41:48 +0000955 PPCallbacks *Callbacks = PP.getPPCallbacks();
Mike Stump11289f42009-09-09 15:08:12 +0000956
Chris Lattner504af112009-04-19 23:16:58 +0000957 diag::Mapping Map;
958 if (II->isStr("warning"))
959 Map = diag::MAP_WARNING;
960 else if (II->isStr("error"))
961 Map = diag::MAP_ERROR;
962 else if (II->isStr("ignored"))
963 Map = diag::MAP_IGNORE;
964 else if (II->isStr("fatal"))
965 Map = diag::MAP_FATAL;
Douglas Gregor3cc26482010-08-30 15:15:34 +0000966 else if (II->isStr("pop")) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000967 if (!PP.getDiagnostics().popMappings(DiagLoc))
Douglas Gregor3cc26482010-08-30 15:15:34 +0000968 PP.Diag(Tok, diag::warn_pragma_diagnostic_cannot_pop);
Douglas Gregor3bde9b12011-06-22 19:41:48 +0000969 else if (Callbacks)
970 Callbacks->PragmaDiagnosticPop(DiagLoc, Namespace);
Douglas Gregor3cc26482010-08-30 15:15:34 +0000971 return;
972 } else if (II->isStr("push")) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000973 PP.getDiagnostics().pushMappings(DiagLoc);
Douglas Gregor3bde9b12011-06-22 19:41:48 +0000974 if (Callbacks)
975 Callbacks->PragmaDiagnosticPush(DiagLoc, Namespace);
Chris Lattnerfb42a182009-07-12 21:18:45 +0000976 return;
977 } else {
Douglas Gregor3cc26482010-08-30 15:15:34 +0000978 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Chris Lattner504af112009-04-19 23:16:58 +0000979 return;
980 }
Mike Stump11289f42009-09-09 15:08:12 +0000981
Chris Lattner504af112009-04-19 23:16:58 +0000982 PP.LexUnexpandedToken(Tok);
Andy Gibbs58905d22012-11-17 19:15:38 +0000983 SourceLocation StringLoc = Tok.getLocation();
Chris Lattner504af112009-04-19 23:16:58 +0000984
Andy Gibbs58905d22012-11-17 19:15:38 +0000985 std::string WarningName;
Andy Gibbsa8df57a2012-11-17 19:16:52 +0000986 if (!PP.FinishLexStringLiteral(Tok, WarningName, "pragma diagnostic",
987 /*MacroExpansion=*/false))
Chris Lattner504af112009-04-19 23:16:58 +0000988 return;
Mike Stump11289f42009-09-09 15:08:12 +0000989
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000990 if (Tok.isNot(tok::eod)) {
Chris Lattner504af112009-04-19 23:16:58 +0000991 PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token);
992 return;
993 }
Mike Stump11289f42009-09-09 15:08:12 +0000994
Chris Lattner504af112009-04-19 23:16:58 +0000995 if (WarningName.size() < 3 || WarningName[0] != '-' ||
996 WarningName[1] != 'W') {
Andy Gibbs58905d22012-11-17 19:15:38 +0000997 PP.Diag(StringLoc, diag::warn_pragma_diagnostic_invalid_option);
Chris Lattner504af112009-04-19 23:16:58 +0000998 return;
999 }
Mike Stump11289f42009-09-09 15:08:12 +00001000
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +00001001 if (PP.getDiagnostics().setDiagnosticGroupMapping(WarningName.substr(2),
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +00001002 Map, DiagLoc))
Andy Gibbs58905d22012-11-17 19:15:38 +00001003 PP.Diag(StringLoc, diag::warn_pragma_diagnostic_unknown_warning)
1004 << WarningName;
Douglas Gregor3bde9b12011-06-22 19:41:48 +00001005 else if (Callbacks)
1006 Callbacks->PragmaDiagnostic(DiagLoc, Namespace, Map, WarningName);
Chris Lattner504af112009-04-19 23:16:58 +00001007 }
1008};
Mike Stump11289f42009-09-09 15:08:12 +00001009
Reid Kleckner4d185102013-10-02 15:19:23 +00001010// Returns -1 on failure.
1011static int LexSimpleInt(Preprocessor &PP, Token &Tok) {
Reid Kleckner881dff32013-09-13 22:00:30 +00001012 assert(Tok.is(tok::numeric_constant));
1013 SmallString<8> IntegerBuffer;
1014 bool NumberInvalid = false;
1015 StringRef Spelling = PP.getSpelling(Tok, IntegerBuffer, &NumberInvalid);
1016 if (NumberInvalid)
Reid Kleckner4d185102013-10-02 15:19:23 +00001017 return -1;
Reid Kleckner881dff32013-09-13 22:00:30 +00001018 NumericLiteralParser Literal(Spelling, Tok.getLocation(), PP);
1019 if (Literal.hadError || !Literal.isIntegerLiteral() || Literal.hasUDSuffix())
Reid Kleckner4d185102013-10-02 15:19:23 +00001020 return -1;
Reid Kleckner881dff32013-09-13 22:00:30 +00001021 llvm::APInt APVal(32, 0);
1022 if (Literal.GetIntegerValue(APVal))
Reid Kleckner4d185102013-10-02 15:19:23 +00001023 return -1;
Reid Kleckner881dff32013-09-13 22:00:30 +00001024 PP.Lex(Tok);
Reid Kleckner4d185102013-10-02 15:19:23 +00001025 return int(APVal.getLimitedValue(INT_MAX));
Reid Kleckner881dff32013-09-13 22:00:30 +00001026}
1027
1028/// "\#pragma warning(...)". MSVC's diagnostics do not map cleanly to clang's
1029/// diagnostics, so we don't really implement this pragma. We parse it and
1030/// ignore it to avoid -Wunknown-pragma warnings.
1031struct PragmaWarningHandler : public PragmaHandler {
1032 PragmaWarningHandler() : PragmaHandler("warning") {}
1033
1034 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1035 Token &Tok) {
1036 // Parse things like:
1037 // warning(push, 1)
1038 // warning(pop)
John Thompson4762b232013-11-16 00:16:03 +00001039 // warning(disable : 1 2 3 ; error : 4 5 6 ; suppress : 7 8 9)
Reid Kleckner881dff32013-09-13 22:00:30 +00001040 SourceLocation DiagLoc = Tok.getLocation();
1041 PPCallbacks *Callbacks = PP.getPPCallbacks();
1042
1043 PP.Lex(Tok);
1044 if (Tok.isNot(tok::l_paren)) {
1045 PP.Diag(Tok, diag::warn_pragma_warning_expected) << "(";
1046 return;
1047 }
1048
1049 PP.Lex(Tok);
1050 IdentifierInfo *II = Tok.getIdentifierInfo();
1051 if (!II) {
1052 PP.Diag(Tok, diag::warn_pragma_warning_spec_invalid);
1053 return;
1054 }
1055
1056 if (II->isStr("push")) {
1057 // #pragma warning( push[ ,n ] )
Reid Kleckner4d185102013-10-02 15:19:23 +00001058 int Level = -1;
Reid Kleckner881dff32013-09-13 22:00:30 +00001059 PP.Lex(Tok);
1060 if (Tok.is(tok::comma)) {
1061 PP.Lex(Tok);
1062 if (Tok.is(tok::numeric_constant))
Reid Kleckner4d185102013-10-02 15:19:23 +00001063 Level = LexSimpleInt(PP, Tok);
1064 if (Level < 0 || Level > 4) {
Reid Kleckner881dff32013-09-13 22:00:30 +00001065 PP.Diag(Tok, diag::warn_pragma_warning_push_level);
1066 return;
1067 }
1068 }
1069 if (Callbacks)
1070 Callbacks->PragmaWarningPush(DiagLoc, Level);
1071 } else if (II->isStr("pop")) {
1072 // #pragma warning( pop )
1073 PP.Lex(Tok);
1074 if (Callbacks)
1075 Callbacks->PragmaWarningPop(DiagLoc);
1076 } else {
1077 // #pragma warning( warning-specifier : warning-number-list
1078 // [; warning-specifier : warning-number-list...] )
1079 while (true) {
1080 II = Tok.getIdentifierInfo();
1081 if (!II) {
1082 PP.Diag(Tok, diag::warn_pragma_warning_spec_invalid);
1083 return;
1084 }
1085
1086 // Figure out which warning specifier this is.
1087 StringRef Specifier = II->getName();
1088 bool SpecifierValid =
1089 llvm::StringSwitch<bool>(Specifier)
1090 .Cases("1", "2", "3", "4", true)
1091 .Cases("default", "disable", "error", "once", "suppress", true)
1092 .Default(false);
1093 if (!SpecifierValid) {
1094 PP.Diag(Tok, diag::warn_pragma_warning_spec_invalid);
1095 return;
1096 }
1097 PP.Lex(Tok);
1098 if (Tok.isNot(tok::colon)) {
1099 PP.Diag(Tok, diag::warn_pragma_warning_expected) << ":";
1100 return;
1101 }
1102
1103 // Collect the warning ids.
1104 SmallVector<int, 4> Ids;
1105 PP.Lex(Tok);
1106 while (Tok.is(tok::numeric_constant)) {
Reid Kleckner4d185102013-10-02 15:19:23 +00001107 int Id = LexSimpleInt(PP, Tok);
1108 if (Id <= 0) {
Reid Kleckner881dff32013-09-13 22:00:30 +00001109 PP.Diag(Tok, diag::warn_pragma_warning_expected_number);
1110 return;
1111 }
1112 Ids.push_back(Id);
1113 }
1114 if (Callbacks)
1115 Callbacks->PragmaWarning(DiagLoc, Specifier, Ids);
1116
1117 // Parse the next specifier if there is a semicolon.
1118 if (Tok.isNot(tok::semi))
1119 break;
1120 PP.Lex(Tok);
1121 }
1122 }
1123
1124 if (Tok.isNot(tok::r_paren)) {
1125 PP.Diag(Tok, diag::warn_pragma_warning_expected) << ")";
1126 return;
1127 }
1128
1129 PP.Lex(Tok);
1130 if (Tok.isNot(tok::eod))
1131 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma warning";
1132 }
1133};
1134
James Dennett18a6d792012-06-17 03:26:26 +00001135/// PragmaIncludeAliasHandler - "\#pragma include_alias("...")".
Aaron Ballman611306e2012-03-02 22:51:54 +00001136struct PragmaIncludeAliasHandler : public PragmaHandler {
1137 PragmaIncludeAliasHandler() : PragmaHandler("include_alias") {}
1138 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1139 Token &IncludeAliasTok) {
Reid Kleckner881dff32013-09-13 22:00:30 +00001140 PP.HandlePragmaIncludeAlias(IncludeAliasTok);
Aaron Ballman611306e2012-03-02 22:51:54 +00001141 }
1142};
1143
Andy Gibbs9c2ccd62013-04-17 16:16:16 +00001144/// PragmaMessageHandler - Handle the microsoft and gcc \#pragma message
1145/// extension. The syntax is:
1146/// \code
1147/// #pragma message(string)
1148/// \endcode
1149/// OR, in GCC mode:
1150/// \code
1151/// #pragma message string
1152/// \endcode
1153/// string is a string, which is fully macro expanded, and permits string
1154/// concatenation, embedded escape characters, etc... See MSDN for more details.
1155/// Also handles \#pragma GCC warning and \#pragma GCC error which take the same
1156/// form as \#pragma message.
Chris Lattner30c924b2010-06-26 17:11:39 +00001157struct PragmaMessageHandler : public PragmaHandler {
Andy Gibbs9c2ccd62013-04-17 16:16:16 +00001158private:
1159 const PPCallbacks::PragmaMessageKind Kind;
1160 const StringRef Namespace;
1161
1162 static const char* PragmaKind(PPCallbacks::PragmaMessageKind Kind,
1163 bool PragmaNameOnly = false) {
1164 switch (Kind) {
1165 case PPCallbacks::PMK_Message:
1166 return PragmaNameOnly ? "message" : "pragma message";
1167 case PPCallbacks::PMK_Warning:
1168 return PragmaNameOnly ? "warning" : "pragma warning";
1169 case PPCallbacks::PMK_Error:
1170 return PragmaNameOnly ? "error" : "pragma error";
1171 }
1172 llvm_unreachable("Unknown PragmaMessageKind!");
1173 }
1174
1175public:
1176 PragmaMessageHandler(PPCallbacks::PragmaMessageKind Kind,
1177 StringRef Namespace = StringRef())
1178 : PragmaHandler(PragmaKind(Kind, true)), Kind(Kind), Namespace(Namespace) {}
1179
Douglas Gregorc7d65762010-09-09 22:45:38 +00001180 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
Andy Gibbs9c2ccd62013-04-17 16:16:16 +00001181 Token &Tok) {
1182 SourceLocation MessageLoc = Tok.getLocation();
1183 PP.Lex(Tok);
1184 bool ExpectClosingParen = false;
1185 switch (Tok.getKind()) {
1186 case tok::l_paren:
1187 // We have a MSVC style pragma message.
1188 ExpectClosingParen = true;
1189 // Read the string.
1190 PP.Lex(Tok);
1191 break;
1192 case tok::string_literal:
1193 // We have a GCC style pragma message, and we just read the string.
1194 break;
1195 default:
1196 PP.Diag(MessageLoc, diag::err_pragma_message_malformed) << Kind;
1197 return;
1198 }
1199
1200 std::string MessageString;
1201 if (!PP.FinishLexStringLiteral(Tok, MessageString, PragmaKind(Kind),
1202 /*MacroExpansion=*/true))
1203 return;
1204
1205 if (ExpectClosingParen) {
1206 if (Tok.isNot(tok::r_paren)) {
1207 PP.Diag(Tok.getLocation(), diag::err_pragma_message_malformed) << Kind;
1208 return;
1209 }
1210 PP.Lex(Tok); // eat the r_paren.
1211 }
1212
1213 if (Tok.isNot(tok::eod)) {
1214 PP.Diag(Tok.getLocation(), diag::err_pragma_message_malformed) << Kind;
1215 return;
1216 }
1217
1218 // Output the message.
1219 PP.Diag(MessageLoc, (Kind == PPCallbacks::PMK_Error)
1220 ? diag::err_pragma_message
1221 : diag::warn_pragma_message) << MessageString;
1222
1223 // If the pragma is lexically sound, notify any interested PPCallbacks.
1224 if (PPCallbacks *Callbacks = PP.getPPCallbacks())
1225 Callbacks->PragmaMessage(MessageLoc, Namespace, Kind, MessageString);
Chris Lattner30c924b2010-06-26 17:11:39 +00001226 }
1227};
1228
James Dennett18a6d792012-06-17 03:26:26 +00001229/// PragmaPushMacroHandler - "\#pragma push_macro" saves the value of the
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001230/// macro on the top of the stack.
1231struct PragmaPushMacroHandler : public PragmaHandler {
1232 PragmaPushMacroHandler() : PragmaHandler("push_macro") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +00001233 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1234 Token &PushMacroTok) {
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001235 PP.HandlePragmaPushMacro(PushMacroTok);
1236 }
1237};
1238
1239
James Dennett18a6d792012-06-17 03:26:26 +00001240/// PragmaPopMacroHandler - "\#pragma pop_macro" sets the value of the
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001241/// macro to the value on the top of the stack.
1242struct PragmaPopMacroHandler : public PragmaHandler {
1243 PragmaPopMacroHandler() : PragmaHandler("pop_macro") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +00001244 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1245 Token &PopMacroTok) {
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001246 PP.HandlePragmaPopMacro(PopMacroTok);
1247 }
1248};
1249
Chris Lattner958ee042009-04-19 21:20:35 +00001250// Pragma STDC implementations.
Chris Lattner02ef4e32009-04-19 21:50:08 +00001251
James Dennett18a6d792012-06-17 03:26:26 +00001252/// PragmaSTDC_FENV_ACCESSHandler - "\#pragma STDC FENV_ACCESS ...".
Chris Lattner958ee042009-04-19 21:20:35 +00001253struct PragmaSTDC_FENV_ACCESSHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001254 PragmaSTDC_FENV_ACCESSHandler() : PragmaHandler("FENV_ACCESS") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +00001255 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1256 Token &Tok) {
Peter Collingbourne3bffa522011-02-14 01:42:24 +00001257 tok::OnOffSwitch OOS;
1258 if (PP.LexOnOffSwitch(OOS))
1259 return;
1260 if (OOS == tok::OOS_ON)
Chris Lattnerdf222682009-04-19 21:55:32 +00001261 PP.Diag(Tok, diag::warn_stdc_fenv_access_not_supported);
Chris Lattner958ee042009-04-19 21:20:35 +00001262 }
1263};
Mike Stump11289f42009-09-09 15:08:12 +00001264
James Dennett18a6d792012-06-17 03:26:26 +00001265/// PragmaSTDC_CX_LIMITED_RANGEHandler - "\#pragma STDC CX_LIMITED_RANGE ...".
Chris Lattner958ee042009-04-19 21:20:35 +00001266struct PragmaSTDC_CX_LIMITED_RANGEHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001267 PragmaSTDC_CX_LIMITED_RANGEHandler()
1268 : PragmaHandler("CX_LIMITED_RANGE") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +00001269 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1270 Token &Tok) {
Peter Collingbourne3bffa522011-02-14 01:42:24 +00001271 tok::OnOffSwitch OOS;
1272 PP.LexOnOffSwitch(OOS);
Chris Lattner958ee042009-04-19 21:20:35 +00001273 }
1274};
Mike Stump11289f42009-09-09 15:08:12 +00001275
James Dennett18a6d792012-06-17 03:26:26 +00001276/// PragmaSTDC_UnknownHandler - "\#pragma STDC ...".
Chris Lattner958ee042009-04-19 21:20:35 +00001277struct PragmaSTDC_UnknownHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001278 PragmaSTDC_UnknownHandler() {}
Douglas Gregorc7d65762010-09-09 22:45:38 +00001279 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1280 Token &UnknownTok) {
Chris Lattner02ef4e32009-04-19 21:50:08 +00001281 // C99 6.10.6p2, unknown forms are not allowed.
Chris Lattnera0b1f762009-04-19 21:25:37 +00001282 PP.Diag(UnknownTok, diag::ext_stdc_pragma_ignored);
Chris Lattner958ee042009-04-19 21:20:35 +00001283 }
1284};
Mike Stump11289f42009-09-09 15:08:12 +00001285
John McCall32f5fe12011-09-30 05:12:12 +00001286/// PragmaARCCFCodeAuditedHandler -
James Dennett18a6d792012-06-17 03:26:26 +00001287/// \#pragma clang arc_cf_code_audited begin/end
John McCall32f5fe12011-09-30 05:12:12 +00001288struct PragmaARCCFCodeAuditedHandler : public PragmaHandler {
1289 PragmaARCCFCodeAuditedHandler() : PragmaHandler("arc_cf_code_audited") {}
1290 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1291 Token &NameTok) {
1292 SourceLocation Loc = NameTok.getLocation();
1293 bool IsBegin;
1294
1295 Token Tok;
1296
1297 // Lex the 'begin' or 'end'.
1298 PP.LexUnexpandedToken(Tok);
1299 const IdentifierInfo *BeginEnd = Tok.getIdentifierInfo();
1300 if (BeginEnd && BeginEnd->isStr("begin")) {
1301 IsBegin = true;
1302 } else if (BeginEnd && BeginEnd->isStr("end")) {
1303 IsBegin = false;
1304 } else {
1305 PP.Diag(Tok.getLocation(), diag::err_pp_arc_cf_code_audited_syntax);
1306 return;
1307 }
1308
1309 // Verify that this is followed by EOD.
1310 PP.LexUnexpandedToken(Tok);
1311 if (Tok.isNot(tok::eod))
1312 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
1313
1314 // The start location of the active audit.
1315 SourceLocation BeginLoc = PP.getPragmaARCCFCodeAuditedLoc();
1316
1317 // The start location we want after processing this.
1318 SourceLocation NewLoc;
1319
1320 if (IsBegin) {
1321 // Complain about attempts to re-enter an audit.
1322 if (BeginLoc.isValid()) {
1323 PP.Diag(Loc, diag::err_pp_double_begin_of_arc_cf_code_audited);
1324 PP.Diag(BeginLoc, diag::note_pragma_entered_here);
1325 }
1326 NewLoc = Loc;
1327 } else {
1328 // Complain about attempts to leave an audit that doesn't exist.
1329 if (!BeginLoc.isValid()) {
1330 PP.Diag(Loc, diag::err_pp_unmatched_end_of_arc_cf_code_audited);
1331 return;
1332 }
1333 NewLoc = SourceLocation();
1334 }
1335
1336 PP.setPragmaARCCFCodeAuditedLoc(NewLoc);
1337 }
1338};
1339
David Majnemer7aa8c2f2013-06-30 08:18:16 +00001340/// \brief Handle "\#pragma region [...]"
1341///
1342/// The syntax is
1343/// \code
1344/// #pragma region [optional name]
1345/// #pragma endregion [optional comment]
1346/// \endcode
1347///
1348/// \note This is
1349/// <a href="http://msdn.microsoft.com/en-us/library/b6xkz944(v=vs.80).aspx">editor-only</a>
1350/// pragma, just skipped by compiler.
1351struct PragmaRegionHandler : public PragmaHandler {
1352 PragmaRegionHandler(const char *pragma) : PragmaHandler(pragma) { }
Aaron Ballman406ea512012-11-30 19:52:30 +00001353
David Majnemer7aa8c2f2013-06-30 08:18:16 +00001354 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1355 Token &NameTok) {
1356 // #pragma region: endregion matches can be verified
1357 // __pragma(region): no sense, but ignored by msvc
1358 // _Pragma is not valid for MSVC, but there isn't any point
1359 // to handle a _Pragma differently.
1360 }
1361};
Aaron Ballman406ea512012-11-30 19:52:30 +00001362
Chris Lattnerb694ba72006-07-02 22:41:36 +00001363} // end anonymous namespace
1364
1365
1366/// RegisterBuiltinPragmas - Install the standard preprocessor pragmas:
James Dennett18a6d792012-06-17 03:26:26 +00001367/// \#pragma GCC poison/system_header/dependency and \#pragma once.
Chris Lattnerb694ba72006-07-02 22:41:36 +00001368void Preprocessor::RegisterBuiltinPragmas() {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001369 AddPragmaHandler(new PragmaOnceHandler());
1370 AddPragmaHandler(new PragmaMarkHandler());
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001371 AddPragmaHandler(new PragmaPushMacroHandler());
1372 AddPragmaHandler(new PragmaPopMacroHandler());
Andy Gibbs9c2ccd62013-04-17 16:16:16 +00001373 AddPragmaHandler(new PragmaMessageHandler(PPCallbacks::PMK_Message));
Mike Stump11289f42009-09-09 15:08:12 +00001374
Chris Lattnerb61448d2009-05-12 18:21:11 +00001375 // #pragma GCC ...
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001376 AddPragmaHandler("GCC", new PragmaPoisonHandler());
1377 AddPragmaHandler("GCC", new PragmaSystemHeaderHandler());
1378 AddPragmaHandler("GCC", new PragmaDependencyHandler());
Douglas Gregor3bde9b12011-06-22 19:41:48 +00001379 AddPragmaHandler("GCC", new PragmaDiagnosticHandler("GCC"));
Andy Gibbs9c2ccd62013-04-17 16:16:16 +00001380 AddPragmaHandler("GCC", new PragmaMessageHandler(PPCallbacks::PMK_Warning,
1381 "GCC"));
1382 AddPragmaHandler("GCC", new PragmaMessageHandler(PPCallbacks::PMK_Error,
1383 "GCC"));
Chris Lattnerb61448d2009-05-12 18:21:11 +00001384 // #pragma clang ...
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001385 AddPragmaHandler("clang", new PragmaPoisonHandler());
1386 AddPragmaHandler("clang", new PragmaSystemHeaderHandler());
Daniel Dunbarb8068c32010-07-28 15:40:33 +00001387 AddPragmaHandler("clang", new PragmaDebugHandler());
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001388 AddPragmaHandler("clang", new PragmaDependencyHandler());
Douglas Gregor3bde9b12011-06-22 19:41:48 +00001389 AddPragmaHandler("clang", new PragmaDiagnosticHandler("clang"));
John McCall32f5fe12011-09-30 05:12:12 +00001390 AddPragmaHandler("clang", new PragmaARCCFCodeAuditedHandler());
Chris Lattnerb61448d2009-05-12 18:21:11 +00001391
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001392 AddPragmaHandler("STDC", new PragmaSTDC_FENV_ACCESSHandler());
1393 AddPragmaHandler("STDC", new PragmaSTDC_CX_LIMITED_RANGEHandler());
Chris Lattner958ee042009-04-19 21:20:35 +00001394 AddPragmaHandler("STDC", new PragmaSTDC_UnknownHandler());
Mike Stump11289f42009-09-09 15:08:12 +00001395
Chris Lattner2ff698d2009-01-16 08:21:25 +00001396 // MS extensions.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001397 if (LangOpts.MicrosoftExt) {
Reid Kleckner881dff32013-09-13 22:00:30 +00001398 AddPragmaHandler(new PragmaWarningHandler());
Aaron Ballman611306e2012-03-02 22:51:54 +00001399 AddPragmaHandler(new PragmaIncludeAliasHandler());
Aaron Ballman406ea512012-11-30 19:52:30 +00001400 AddPragmaHandler(new PragmaRegionHandler("region"));
1401 AddPragmaHandler(new PragmaRegionHandler("endregion"));
Chris Lattner30c924b2010-06-26 17:11:39 +00001402 }
Chris Lattnerb694ba72006-07-02 22:41:36 +00001403}