blob: 99ba8dee2dbab367b88011706915ef8ab6c4efc6 [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() {
Reid Kleckner588c9372014-02-19 23:44:52 +000051 llvm::DeleteContainerSeconds(Handlers);
Chris Lattner2e155302006-07-03 05:34:41 +000052}
53
54/// FindHandler - Check to see if there is already a handler for the
55/// specified name. If not, return the handler for the null identifier if it
56/// exists, otherwise return null. If IgnoreNull is true (the default) then
57/// the null handler isn't returned on failure to match.
Chris Lattner0e62c1c2011-07-23 10:55:15 +000058PragmaHandler *PragmaNamespace::FindHandler(StringRef Name,
Chris Lattner2e155302006-07-03 05:34:41 +000059 bool IgnoreNull) const {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000060 if (PragmaHandler *Handler = Handlers.lookup(Name))
61 return Handler;
Chris Lattner0e62c1c2011-07-23 10:55:15 +000062 return IgnoreNull ? 0 : Handlers.lookup(StringRef());
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000063}
Mike Stump11289f42009-09-09 15:08:12 +000064
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000065void PragmaNamespace::AddPragma(PragmaHandler *Handler) {
66 assert(!Handlers.lookup(Handler->getName()) &&
67 "A handler with this name is already registered in this namespace");
68 llvm::StringMapEntry<PragmaHandler *> &Entry =
69 Handlers.GetOrCreateValue(Handler->getName());
70 Entry.setValue(Handler);
Chris Lattner2e155302006-07-03 05:34:41 +000071}
72
Daniel Dunbar40596532008-10-04 19:17:46 +000073void PragmaNamespace::RemovePragmaHandler(PragmaHandler *Handler) {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000074 assert(Handlers.lookup(Handler->getName()) &&
75 "Handler not registered in this namespace");
76 Handlers.erase(Handler->getName());
Daniel Dunbar40596532008-10-04 19:17:46 +000077}
78
Douglas Gregorc7d65762010-09-09 22:45:38 +000079void PragmaNamespace::HandlePragma(Preprocessor &PP,
80 PragmaIntroducerKind Introducer,
81 Token &Tok) {
Chris Lattnerb8761832006-06-24 21:31:03 +000082 // Read the 'namespace' that the directive is in, e.g. STDC. Do not macro
83 // expand it, the user can have a STDC #define, that should not affect this.
84 PP.LexUnexpandedToken(Tok);
Mike Stump11289f42009-09-09 15:08:12 +000085
Chris Lattnerb8761832006-06-24 21:31:03 +000086 // Get the handler for this token. If there is no handler, ignore the pragma.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000087 PragmaHandler *Handler
88 = FindHandler(Tok.getIdentifierInfo() ? Tok.getIdentifierInfo()->getName()
Chris Lattner0e62c1c2011-07-23 10:55:15 +000089 : StringRef(),
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000090 /*IgnoreNull=*/false);
Chris Lattner21656f22009-04-19 21:10:26 +000091 if (Handler == 0) {
92 PP.Diag(Tok, diag::warn_pragma_ignored);
93 return;
94 }
Mike Stump11289f42009-09-09 15:08:12 +000095
Chris Lattnerb8761832006-06-24 21:31:03 +000096 // Otherwise, pass it down.
Douglas Gregorc7d65762010-09-09 22:45:38 +000097 Handler->HandlePragma(PP, Introducer, Tok);
Chris Lattnerb8761832006-06-24 21:31:03 +000098}
Chris Lattnerb694ba72006-07-02 22:41:36 +000099
Chris Lattnerb694ba72006-07-02 22:41:36 +0000100//===----------------------------------------------------------------------===//
101// Preprocessor Pragma Directive Handling.
102//===----------------------------------------------------------------------===//
103
James Dennett18a6d792012-06-17 03:26:26 +0000104/// HandlePragmaDirective - The "\#pragma" directive has been parsed. Lex the
Chris Lattnerb694ba72006-07-02 22:41:36 +0000105/// rest of the pragma, passing it to the registered pragma handlers.
Enea Zaffanella5afb04a2013-07-20 20:09:11 +0000106void Preprocessor::HandlePragmaDirective(SourceLocation IntroducerLoc,
107 PragmaIntroducerKind Introducer) {
108 if (Callbacks)
109 Callbacks->PragmaDirective(IntroducerLoc, Introducer);
110
Jordan Rosede1a2922012-06-08 18:06:21 +0000111 if (!PragmasEnabled)
112 return;
113
Chris Lattnerb694ba72006-07-02 22:41:36 +0000114 ++NumPragma;
Mike Stump11289f42009-09-09 15:08:12 +0000115
Chris Lattnerb694ba72006-07-02 22:41:36 +0000116 // Invoke the first level of pragma handlers which reads the namespace id.
Chris Lattner146762e2007-07-20 16:59:19 +0000117 Token Tok;
Enea Zaffanella5afb04a2013-07-20 20:09:11 +0000118 PragmaHandlers->HandlePragma(*this, Introducer, Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000119
Chris Lattnerb694ba72006-07-02 22:41:36 +0000120 // If the pragma handler didn't read the rest of the line, consume it now.
Peter Collingbourne2c9f9662011-02-22 13:49:00 +0000121 if ((CurTokenLexer && CurTokenLexer->isParsingPreprocessorDirective())
122 || (CurPPLexer && CurPPLexer->ParsingPreprocessorDirective))
Chris Lattnerb694ba72006-07-02 22:41:36 +0000123 DiscardUntilEndOfDirective();
124}
125
Argyrios Kyrtzidisf1b64c62012-04-03 16:47:40 +0000126namespace {
127/// \brief Helper class for \see Preprocessor::Handle_Pragma.
128class LexingFor_PragmaRAII {
129 Preprocessor &PP;
130 bool InMacroArgPreExpansion;
131 bool Failed;
132 Token &OutTok;
133 Token PragmaTok;
134
135public:
136 LexingFor_PragmaRAII(Preprocessor &PP, bool InMacroArgPreExpansion,
137 Token &Tok)
138 : PP(PP), InMacroArgPreExpansion(InMacroArgPreExpansion),
139 Failed(false), OutTok(Tok) {
140 if (InMacroArgPreExpansion) {
141 PragmaTok = OutTok;
142 PP.EnableBacktrackAtThisPos();
143 }
144 }
145
146 ~LexingFor_PragmaRAII() {
147 if (InMacroArgPreExpansion) {
148 if (Failed) {
149 PP.CommitBacktrackedTokens();
150 } else {
151 PP.Backtrack();
152 OutTok = PragmaTok;
153 }
154 }
155 }
156
157 void failed() {
158 Failed = true;
159 }
160};
161}
162
Chris Lattnerb694ba72006-07-02 22:41:36 +0000163/// Handle_Pragma - Read a _Pragma directive, slice it up, process it, then
164/// return the first token after the directive. The _Pragma token has just
165/// been read into 'Tok'.
Chris Lattner146762e2007-07-20 16:59:19 +0000166void Preprocessor::Handle_Pragma(Token &Tok) {
Argyrios Kyrtzidisf1b64c62012-04-03 16:47:40 +0000167
168 // This works differently if we are pre-expanding a macro argument.
169 // In that case we don't actually "activate" the pragma now, we only lex it
170 // until we are sure it is lexically correct and then we backtrack so that
171 // we activate the pragma whenever we encounter the tokens again in the token
172 // stream. This ensures that we will activate it in the correct location
173 // or that we will ignore it if it never enters the token stream, e.g:
174 //
175 // #define EMPTY(x)
176 // #define INACTIVE(x) EMPTY(x)
177 // INACTIVE(_Pragma("clang diagnostic ignored \"-Wconversion\""))
178
179 LexingFor_PragmaRAII _PragmaLexing(*this, InMacroArgPreExpansion, Tok);
180
Chris Lattnerb694ba72006-07-02 22:41:36 +0000181 // Remember the pragma token location.
182 SourceLocation PragmaLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000183
Chris Lattnerb694ba72006-07-02 22:41:36 +0000184 // Read the '('.
185 Lex(Tok);
Chris Lattner907dfe92008-11-18 07:59:24 +0000186 if (Tok.isNot(tok::l_paren)) {
187 Diag(PragmaLoc, diag::err__Pragma_malformed);
Argyrios Kyrtzidisf1b64c62012-04-03 16:47:40 +0000188 return _PragmaLexing.failed();
Chris Lattner907dfe92008-11-18 07:59:24 +0000189 }
Chris Lattnerb694ba72006-07-02 22:41:36 +0000190
191 // Read the '"..."'.
192 Lex(Tok);
Richard Smithc98bb4e2013-03-09 23:30:15 +0000193 if (!tok::isStringLiteral(Tok.getKind())) {
Chris Lattner907dfe92008-11-18 07:59:24 +0000194 Diag(PragmaLoc, diag::err__Pragma_malformed);
Richard Smithd67aea22012-03-06 03:21:47 +0000195 // Skip this token, and the ')', if present.
196 if (Tok.isNot(tok::r_paren))
197 Lex(Tok);
198 if (Tok.is(tok::r_paren))
199 Lex(Tok);
Argyrios Kyrtzidisf1b64c62012-04-03 16:47:40 +0000200 return _PragmaLexing.failed();
Richard Smithd67aea22012-03-06 03:21:47 +0000201 }
202
203 if (Tok.hasUDSuffix()) {
204 Diag(Tok, diag::err_invalid_string_udl);
205 // Skip this token, and the ')', if present.
206 Lex(Tok);
207 if (Tok.is(tok::r_paren))
208 Lex(Tok);
Argyrios Kyrtzidisf1b64c62012-04-03 16:47:40 +0000209 return _PragmaLexing.failed();
Chris Lattner907dfe92008-11-18 07:59:24 +0000210 }
Mike Stump11289f42009-09-09 15:08:12 +0000211
Chris Lattnerb694ba72006-07-02 22:41:36 +0000212 // Remember the string.
Argyrios Kyrtzidisf1b64c62012-04-03 16:47:40 +0000213 Token StrTok = Tok;
Chris Lattnerb694ba72006-07-02 22:41:36 +0000214
215 // Read the ')'.
216 Lex(Tok);
Chris Lattner907dfe92008-11-18 07:59:24 +0000217 if (Tok.isNot(tok::r_paren)) {
218 Diag(PragmaLoc, diag::err__Pragma_malformed);
Argyrios Kyrtzidisf1b64c62012-04-03 16:47:40 +0000219 return _PragmaLexing.failed();
Chris Lattner907dfe92008-11-18 07:59:24 +0000220 }
Mike Stump11289f42009-09-09 15:08:12 +0000221
Argyrios Kyrtzidisf1b64c62012-04-03 16:47:40 +0000222 if (InMacroArgPreExpansion)
223 return;
224
Chris Lattner9dc9c202009-02-15 20:52:18 +0000225 SourceLocation RParenLoc = Tok.getLocation();
Argyrios Kyrtzidisf1b64c62012-04-03 16:47:40 +0000226 std::string StrVal = getSpelling(StrTok);
Mike Stump11289f42009-09-09 15:08:12 +0000227
Richard Smithc98bb4e2013-03-09 23:30:15 +0000228 // The _Pragma is lexically sound. Destringize according to C11 6.10.9.1:
229 // "The string literal is destringized by deleting any encoding prefix,
Chris Lattner262d4e32009-01-16 18:59:23 +0000230 // deleting the leading and trailing double-quotes, replacing each escape
231 // sequence \" by a double-quote, and replacing each escape sequence \\ by a
232 // single backslash."
Richard Smithc98bb4e2013-03-09 23:30:15 +0000233 if (StrVal[0] == 'L' || StrVal[0] == 'U' ||
234 (StrVal[0] == 'u' && StrVal[1] != '8'))
Chris Lattnerb694ba72006-07-02 22:41:36 +0000235 StrVal.erase(StrVal.begin());
Richard Smithc98bb4e2013-03-09 23:30:15 +0000236 else if (StrVal[0] == 'u')
237 StrVal.erase(StrVal.begin(), StrVal.begin() + 2);
238
239 if (StrVal[0] == 'R') {
240 // FIXME: C++11 does not specify how to handle raw-string-literals here.
241 // We strip off the 'R', the quotes, the d-char-sequences, and the parens.
242 assert(StrVal[1] == '"' && StrVal[StrVal.size() - 1] == '"' &&
243 "Invalid raw string token!");
244
245 // Measure the length of the d-char-sequence.
246 unsigned NumDChars = 0;
247 while (StrVal[2 + NumDChars] != '(') {
248 assert(NumDChars < (StrVal.size() - 5) / 2 &&
249 "Invalid raw string token!");
250 ++NumDChars;
251 }
252 assert(StrVal[StrVal.size() - 2 - NumDChars] == ')');
253
254 // Remove 'R " d-char-sequence' and 'd-char-sequence "'. We'll replace the
255 // parens below.
256 StrVal.erase(0, 2 + NumDChars);
257 StrVal.erase(StrVal.size() - 1 - NumDChars);
258 } else {
259 assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
260 "Invalid string token!");
261
262 // Remove escaped quotes and escapes.
Benjamin Kramerc2f5f292013-05-04 10:37:20 +0000263 unsigned ResultPos = 1;
Reid Kleckner95e036c2013-09-25 16:42:48 +0000264 for (unsigned i = 1, e = StrVal.size() - 1; i != e; ++i) {
265 // Skip escapes. \\ -> '\' and \" -> '"'.
266 if (StrVal[i] == '\\' && i + 1 < e &&
267 (StrVal[i + 1] == '\\' || StrVal[i + 1] == '"'))
268 ++i;
269 StrVal[ResultPos++] = StrVal[i];
Richard Smithc98bb4e2013-03-09 23:30:15 +0000270 }
Reid Kleckner95e036c2013-09-25 16:42:48 +0000271 StrVal.erase(StrVal.begin() + ResultPos, StrVal.end() - 1);
Richard Smithc98bb4e2013-03-09 23:30:15 +0000272 }
Mike Stump11289f42009-09-09 15:08:12 +0000273
Chris Lattnerb694ba72006-07-02 22:41:36 +0000274 // Remove the front quote, replacing it with a space, so that the pragma
275 // contents appear to have a space before them.
276 StrVal[0] = ' ';
Mike Stump11289f42009-09-09 15:08:12 +0000277
Chris Lattnerfa217bd2009-03-08 08:08:45 +0000278 // Replace the terminating quote with a \n.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000279 StrVal[StrVal.size()-1] = '\n';
Mike Stump11289f42009-09-09 15:08:12 +0000280
Peter Collingbournef29ce972011-02-22 13:49:06 +0000281 // Plop the string (including the newline and trailing null) into a buffer
282 // where we can lex it.
283 Token TmpTok;
284 TmpTok.startToken();
Dmitri Gribenkob8e9e752012-09-24 21:07:17 +0000285 CreateString(StrVal, TmpTok);
Peter Collingbournef29ce972011-02-22 13:49:06 +0000286 SourceLocation TokLoc = TmpTok.getLocation();
287
288 // Make and enter a lexer object so that we lex and expand the tokens just
289 // like any others.
290 Lexer *TL = Lexer::Create_PragmaLexer(TokLoc, PragmaLoc, RParenLoc,
291 StrVal.size(), *this);
292
293 EnterSourceFileWithLexer(TL, 0);
294
295 // With everything set up, lex this as a #pragma directive.
Enea Zaffanella5afb04a2013-07-20 20:09:11 +0000296 HandlePragmaDirective(PragmaLoc, PIK__Pragma);
John McCall89e925d2010-08-28 22:34:47 +0000297
298 // Finally, return whatever came after the pragma directive.
299 return Lex(Tok);
300}
301
302/// HandleMicrosoft__pragma - Like Handle_Pragma except the pragma text
303/// is not enclosed within a string literal.
304void Preprocessor::HandleMicrosoft__pragma(Token &Tok) {
305 // Remember the pragma token location.
306 SourceLocation PragmaLoc = Tok.getLocation();
307
308 // Read the '('.
309 Lex(Tok);
310 if (Tok.isNot(tok::l_paren)) {
311 Diag(PragmaLoc, diag::err__Pragma_malformed);
312 return;
313 }
314
Peter Collingbournef29ce972011-02-22 13:49:06 +0000315 // Get the tokens enclosed within the __pragma(), as well as the final ')'.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000316 SmallVector<Token, 32> PragmaToks;
John McCall89e925d2010-08-28 22:34:47 +0000317 int NumParens = 0;
318 Lex(Tok);
319 while (Tok.isNot(tok::eof)) {
Peter Collingbournef29ce972011-02-22 13:49:06 +0000320 PragmaToks.push_back(Tok);
John McCall89e925d2010-08-28 22:34:47 +0000321 if (Tok.is(tok::l_paren))
322 NumParens++;
323 else if (Tok.is(tok::r_paren) && NumParens-- == 0)
324 break;
John McCall89e925d2010-08-28 22:34:47 +0000325 Lex(Tok);
326 }
327
John McCall49039d42010-08-29 01:09:54 +0000328 if (Tok.is(tok::eof)) {
329 Diag(PragmaLoc, diag::err_unterminated___pragma);
330 return;
331 }
332
Peter Collingbournef29ce972011-02-22 13:49:06 +0000333 PragmaToks.front().setFlag(Token::LeadingSpace);
John McCall89e925d2010-08-28 22:34:47 +0000334
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000335 // Replace the ')' with an EOD to mark the end of the pragma.
336 PragmaToks.back().setKind(tok::eod);
Peter Collingbournef29ce972011-02-22 13:49:06 +0000337
338 Token *TokArray = new Token[PragmaToks.size()];
339 std::copy(PragmaToks.begin(), PragmaToks.end(), TokArray);
340
341 // Push the tokens onto the stack.
342 EnterTokenStream(TokArray, PragmaToks.size(), true, true);
343
344 // With everything set up, lex this as a #pragma directive.
Enea Zaffanella5afb04a2013-07-20 20:09:11 +0000345 HandlePragmaDirective(PragmaLoc, PIK___pragma);
John McCall89e925d2010-08-28 22:34:47 +0000346
347 // Finally, return whatever came after the pragma directive.
348 return Lex(Tok);
349}
350
James Dennett18a6d792012-06-17 03:26:26 +0000351/// HandlePragmaOnce - Handle \#pragma once. OnceTok is the 'once'.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000352///
Chris Lattner146762e2007-07-20 16:59:19 +0000353void Preprocessor::HandlePragmaOnce(Token &OnceTok) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000354 if (isInPrimaryFile()) {
355 Diag(OnceTok, diag::pp_pragma_once_in_main_file);
356 return;
357 }
Mike Stump11289f42009-09-09 15:08:12 +0000358
Chris Lattnerb694ba72006-07-02 22:41:36 +0000359 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000360 // Mark the file as a once-only file now.
Chris Lattnerd32480d2009-01-17 06:22:33 +0000361 HeaderInfo.MarkFileIncludeOnce(getCurrentFileLexer()->getFileEntry());
Chris Lattnerb694ba72006-07-02 22:41:36 +0000362}
363
Chris Lattnerc2383312007-12-19 19:38:36 +0000364void Preprocessor::HandlePragmaMark() {
Ted Kremenek76c34412008-11-19 22:21:33 +0000365 assert(CurPPLexer && "No current lexer?");
Chris Lattnerd9efb6e2009-06-15 05:02:34 +0000366 if (CurLexer)
367 CurLexer->ReadToEndOfLine();
368 else
369 CurPTHLexer->DiscardToEndOfLine();
Chris Lattnerc2383312007-12-19 19:38:36 +0000370}
371
372
James Dennett18a6d792012-06-17 03:26:26 +0000373/// HandlePragmaPoison - Handle \#pragma GCC poison. PoisonTok is the 'poison'.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000374///
Chris Lattner146762e2007-07-20 16:59:19 +0000375void Preprocessor::HandlePragmaPoison(Token &PoisonTok) {
376 Token Tok;
Chris Lattner538d7f32006-07-20 04:31:52 +0000377
Chris Lattnerb694ba72006-07-02 22:41:36 +0000378 while (1) {
379 // Read the next token to poison. While doing this, pretend that we are
380 // skipping while reading the identifier to poison.
381 // This avoids errors on code like:
382 // #pragma GCC poison X
383 // #pragma GCC poison X
Ted Kremenek551c82a2008-11-18 01:12:54 +0000384 if (CurPPLexer) CurPPLexer->LexingRawMode = true;
Chris Lattnerb694ba72006-07-02 22:41:36 +0000385 LexUnexpandedToken(Tok);
Ted Kremenek551c82a2008-11-18 01:12:54 +0000386 if (CurPPLexer) CurPPLexer->LexingRawMode = false;
Mike Stump11289f42009-09-09 15:08:12 +0000387
Chris Lattnerb694ba72006-07-02 22:41:36 +0000388 // If we reached the end of line, we're done.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000389 if (Tok.is(tok::eod)) return;
Mike Stump11289f42009-09-09 15:08:12 +0000390
Chris Lattnerb694ba72006-07-02 22:41:36 +0000391 // Can only poison identifiers.
Abramo Bagnaraea4f7c72010-12-22 08:23:18 +0000392 if (Tok.isNot(tok::raw_identifier)) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000393 Diag(Tok, diag::err_pp_invalid_poison);
394 return;
395 }
Mike Stump11289f42009-09-09 15:08:12 +0000396
Chris Lattnercefc7682006-07-08 08:28:12 +0000397 // Look up the identifier info for the token. We disabled identifier lookup
398 // by saying we're skipping contents, so we need to do this manually.
399 IdentifierInfo *II = LookUpIdentifierInfo(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000400
Chris Lattnerb694ba72006-07-02 22:41:36 +0000401 // Already poisoned.
402 if (II->isPoisoned()) continue;
Mike Stump11289f42009-09-09 15:08:12 +0000403
Chris Lattnerb694ba72006-07-02 22:41:36 +0000404 // If this is a macro identifier, emit a warning.
Chris Lattner259716a2007-10-07 08:04:56 +0000405 if (II->hasMacroDefinition())
Chris Lattnerb694ba72006-07-02 22:41:36 +0000406 Diag(Tok, diag::pp_poisoning_existing_macro);
Mike Stump11289f42009-09-09 15:08:12 +0000407
Chris Lattnerb694ba72006-07-02 22:41:36 +0000408 // Finally, poison it!
409 II->setIsPoisoned();
Douglas Gregor935bc7a22011-10-27 09:33:13 +0000410 if (II->isFromAST())
411 II->setChangedSinceDeserialization();
Chris Lattnerb694ba72006-07-02 22:41:36 +0000412 }
413}
414
James Dennett18a6d792012-06-17 03:26:26 +0000415/// HandlePragmaSystemHeader - Implement \#pragma GCC system_header. We know
Chris Lattnerb694ba72006-07-02 22:41:36 +0000416/// that the whole directive has been parsed.
Chris Lattner146762e2007-07-20 16:59:19 +0000417void Preprocessor::HandlePragmaSystemHeader(Token &SysHeaderTok) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000418 if (isInPrimaryFile()) {
419 Diag(SysHeaderTok, diag::pp_pragma_sysheader_in_main_file);
420 return;
421 }
Mike Stump11289f42009-09-09 15:08:12 +0000422
Chris Lattnerb694ba72006-07-02 22:41:36 +0000423 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
Ted Kremenek300590b2008-11-20 01:45:11 +0000424 PreprocessorLexer *TheLexer = getCurrentFileLexer();
Mike Stump11289f42009-09-09 15:08:12 +0000425
Chris Lattnerb694ba72006-07-02 22:41:36 +0000426 // Mark the file as a system header.
Chris Lattnerd32480d2009-01-17 06:22:33 +0000427 HeaderInfo.MarkFileSystemHeader(TheLexer->getFileEntry());
Mike Stump11289f42009-09-09 15:08:12 +0000428
429
Chris Lattnerd9efb6e2009-06-15 05:02:34 +0000430 PresumedLoc PLoc = SourceMgr.getPresumedLoc(SysHeaderTok.getLocation());
Douglas Gregor453b0122010-11-12 07:15:47 +0000431 if (PLoc.isInvalid())
432 return;
433
Jay Foad9a6b0982011-06-21 15:13:30 +0000434 unsigned FilenameID = SourceMgr.getLineTableFilenameID(PLoc.getFilename());
Mike Stump11289f42009-09-09 15:08:12 +0000435
Chris Lattner3bdc7672011-05-22 22:10:16 +0000436 // Notify the client, if desired, that we are in a new source file.
437 if (Callbacks)
438 Callbacks->FileChanged(SysHeaderTok.getLocation(),
439 PPCallbacks::SystemHeaderPragma, SrcMgr::C_System);
440
Chris Lattnerd9efb6e2009-06-15 05:02:34 +0000441 // Emit a line marker. This will change any source locations from this point
442 // forward to realize they are in a system header.
443 // Create a line note with this information.
Jordan Rose111c4a62013-04-17 19:09:18 +0000444 SourceMgr.AddLineNote(SysHeaderTok.getLocation(), PLoc.getLine()+1,
445 FilenameID, /*IsEntry=*/false, /*IsExit=*/false,
446 /*IsSystem=*/true, /*IsExternC=*/false);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000447}
448
James Dennett18a6d792012-06-17 03:26:26 +0000449/// HandlePragmaDependency - Handle \#pragma GCC dependency "foo" blah.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000450///
Chris Lattner146762e2007-07-20 16:59:19 +0000451void Preprocessor::HandlePragmaDependency(Token &DependencyTok) {
452 Token FilenameTok;
Ted Kremenek551c82a2008-11-18 01:12:54 +0000453 CurPPLexer->LexIncludeFilename(FilenameTok);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000454
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000455 // If the token kind is EOD, the error has already been diagnosed.
456 if (FilenameTok.is(tok::eod))
Chris Lattnerb694ba72006-07-02 22:41:36 +0000457 return;
Mike Stump11289f42009-09-09 15:08:12 +0000458
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000459 // Reserve a buffer to get the spelling.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000460 SmallString<128> FilenameBuffer;
Douglas Gregordc970f02010-03-16 22:30:13 +0000461 bool Invalid = false;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000462 StringRef Filename = getSpelling(FilenameTok, FilenameBuffer, &Invalid);
Douglas Gregordc970f02010-03-16 22:30:13 +0000463 if (Invalid)
464 return;
Mike Stump11289f42009-09-09 15:08:12 +0000465
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000466 bool isAngled =
467 GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000468 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
469 // error.
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000470 if (Filename.empty())
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000471 return;
Mike Stump11289f42009-09-09 15:08:12 +0000472
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000473 // Search include directories for this file.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000474 const DirectoryLookup *CurDir;
Lawrence Crowlb53e5482013-06-20 21:14:14 +0000475 const FileEntry *File = LookupFile(FilenameTok.getLocation(), Filename,
476 isAngled, 0, CurDir, NULL, NULL, NULL);
Chris Lattner97b8e842008-11-18 08:02:48 +0000477 if (File == 0) {
Eli Friedman3781a362011-08-30 23:07:51 +0000478 if (!SuppressIncludeNotFoundError)
479 Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
Chris Lattner97b8e842008-11-18 08:02:48 +0000480 return;
481 }
Mike Stump11289f42009-09-09 15:08:12 +0000482
Chris Lattnerd32480d2009-01-17 06:22:33 +0000483 const FileEntry *CurFile = getCurrentFileLexer()->getFileEntry();
Chris Lattnerb694ba72006-07-02 22:41:36 +0000484
485 // If this file is older than the file it depends on, emit a diagnostic.
486 if (CurFile && CurFile->getModificationTime() < File->getModificationTime()) {
487 // Lex tokens at the end of the message and include them in the message.
488 std::string Message;
489 Lex(DependencyTok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000490 while (DependencyTok.isNot(tok::eod)) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000491 Message += getSpelling(DependencyTok) + " ";
492 Lex(DependencyTok);
493 }
Mike Stump11289f42009-09-09 15:08:12 +0000494
Chris Lattnerf0b04972010-09-05 23:16:09 +0000495 // Remove the trailing ' ' if present.
496 if (!Message.empty())
497 Message.erase(Message.end()-1);
Chris Lattner97b8e842008-11-18 08:02:48 +0000498 Diag(FilenameTok, diag::pp_out_of_date_dependency) << Message;
Chris Lattnerb694ba72006-07-02 22:41:36 +0000499 }
500}
501
Reid Kleckner002562a2013-05-06 21:02:12 +0000502/// ParsePragmaPushOrPopMacro - Handle parsing of pragma push_macro/pop_macro.
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000503/// Return the IdentifierInfo* associated with the macro to push or pop.
504IdentifierInfo *Preprocessor::ParsePragmaPushOrPopMacro(Token &Tok) {
505 // Remember the pragma token location.
506 Token PragmaTok = Tok;
507
508 // Read the '('.
509 Lex(Tok);
510 if (Tok.isNot(tok::l_paren)) {
511 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
512 << getSpelling(PragmaTok);
513 return 0;
514 }
515
516 // Read the macro name string.
517 Lex(Tok);
518 if (Tok.isNot(tok::string_literal)) {
519 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
520 << getSpelling(PragmaTok);
521 return 0;
522 }
523
Richard Smithd67aea22012-03-06 03:21:47 +0000524 if (Tok.hasUDSuffix()) {
525 Diag(Tok, diag::err_invalid_string_udl);
526 return 0;
527 }
528
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000529 // Remember the macro string.
530 std::string StrVal = getSpelling(Tok);
531
532 // Read the ')'.
533 Lex(Tok);
534 if (Tok.isNot(tok::r_paren)) {
535 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
536 << getSpelling(PragmaTok);
537 return 0;
538 }
539
540 assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
541 "Invalid string token!");
542
543 // Create a Token from the string.
544 Token MacroTok;
545 MacroTok.startToken();
Abramo Bagnaraea4f7c72010-12-22 08:23:18 +0000546 MacroTok.setKind(tok::raw_identifier);
Dmitri Gribenkob8e9e752012-09-24 21:07:17 +0000547 CreateString(StringRef(&StrVal[1], StrVal.size() - 2), MacroTok);
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000548
549 // Get the IdentifierInfo of MacroToPushTok.
550 return LookUpIdentifierInfo(MacroTok);
551}
552
James Dennett18a6d792012-06-17 03:26:26 +0000553/// \brief Handle \#pragma push_macro.
554///
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000555/// The syntax is:
James Dennett18a6d792012-06-17 03:26:26 +0000556/// \code
Dmitri Gribenko9ebd1612012-11-30 20:04:39 +0000557/// #pragma push_macro("macro")
James Dennett18a6d792012-06-17 03:26:26 +0000558/// \endcode
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000559void Preprocessor::HandlePragmaPushMacro(Token &PushMacroTok) {
560 // Parse the pragma directive and get the macro IdentifierInfo*.
561 IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PushMacroTok);
562 if (!IdentInfo) return;
563
564 // Get the MacroInfo associated with IdentInfo.
565 MacroInfo *MI = getMacroInfo(IdentInfo);
566
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000567 if (MI) {
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000568 // Allow the original MacroInfo to be redefined later.
569 MI->setIsAllowRedefinitionsWithoutWarning(true);
570 }
571
572 // Push the cloned MacroInfo so we can retrieve it later.
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +0000573 PragmaPushMacroInfo[IdentInfo].push_back(MI);
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000574}
575
James Dennett18a6d792012-06-17 03:26:26 +0000576/// \brief Handle \#pragma pop_macro.
577///
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000578/// The syntax is:
James Dennett18a6d792012-06-17 03:26:26 +0000579/// \code
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000580/// #pragma pop_macro("macro")
James Dennett18a6d792012-06-17 03:26:26 +0000581/// \endcode
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000582void Preprocessor::HandlePragmaPopMacro(Token &PopMacroTok) {
583 SourceLocation MessageLoc = PopMacroTok.getLocation();
584
585 // Parse the pragma directive and get the macro IdentifierInfo*.
586 IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PopMacroTok);
587 if (!IdentInfo) return;
588
589 // Find the vector<MacroInfo*> associated with the macro.
590 llvm::DenseMap<IdentifierInfo*, std::vector<MacroInfo*> >::iterator iter =
591 PragmaPushMacroInfo.find(IdentInfo);
592 if (iter != PragmaPushMacroInfo.end()) {
Alexander Kornienko8b3f6232012-08-29 00:20:03 +0000593 // Forget the MacroInfo currently associated with IdentInfo.
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +0000594 if (MacroDirective *CurrentMD = getMacroDirective(IdentInfo)) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +0000595 MacroInfo *MI = CurrentMD->getMacroInfo();
596 if (MI->isWarnIfUnused())
597 WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());
598 appendMacroDirective(IdentInfo, AllocateUndefMacroDirective(MessageLoc));
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000599 }
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000600
601 // Get the MacroInfo we want to reinstall.
602 MacroInfo *MacroToReInstall = iter->second.back();
603
Alexander Kornienkoc0b49282012-08-29 16:56:24 +0000604 if (MacroToReInstall) {
605 // Reinstall the previously pushed macro.
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +0000606 appendDefMacroDirective(IdentInfo, MacroToReInstall, MessageLoc,
607 /*isImported=*/false);
Alexander Kornienkoc0b49282012-08-29 16:56:24 +0000608 }
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000609
610 // Pop PragmaPushMacroInfo stack.
611 iter->second.pop_back();
612 if (iter->second.size() == 0)
613 PragmaPushMacroInfo.erase(iter);
614 } else {
615 Diag(MessageLoc, diag::warn_pragma_pop_macro_no_push)
616 << IdentInfo->getName();
617 }
618}
Chris Lattnerb694ba72006-07-02 22:41:36 +0000619
Aaron Ballman611306e2012-03-02 22:51:54 +0000620void Preprocessor::HandlePragmaIncludeAlias(Token &Tok) {
621 // We will either get a quoted filename or a bracketed filename, and we
622 // have to track which we got. The first filename is the source name,
623 // and the second name is the mapped filename. If the first is quoted,
624 // the second must be as well (cannot mix and match quotes and brackets).
Aaron Ballman611306e2012-03-02 22:51:54 +0000625
626 // Get the open paren
627 Lex(Tok);
628 if (Tok.isNot(tok::l_paren)) {
629 Diag(Tok, diag::warn_pragma_include_alias_expected) << "(";
630 return;
631 }
632
633 // We expect either a quoted string literal, or a bracketed name
634 Token SourceFilenameTok;
635 CurPPLexer->LexIncludeFilename(SourceFilenameTok);
636 if (SourceFilenameTok.is(tok::eod)) {
637 // The diagnostic has already been handled
638 return;
639 }
640
641 StringRef SourceFileName;
642 SmallString<128> FileNameBuffer;
643 if (SourceFilenameTok.is(tok::string_literal) ||
644 SourceFilenameTok.is(tok::angle_string_literal)) {
645 SourceFileName = getSpelling(SourceFilenameTok, FileNameBuffer);
646 } else if (SourceFilenameTok.is(tok::less)) {
647 // This could be a path instead of just a name
648 FileNameBuffer.push_back('<');
649 SourceLocation End;
650 if (ConcatenateIncludeName(FileNameBuffer, End))
651 return; // Diagnostic already emitted
652 SourceFileName = FileNameBuffer.str();
653 } else {
654 Diag(Tok, diag::warn_pragma_include_alias_expected_filename);
655 return;
656 }
657 FileNameBuffer.clear();
658
659 // Now we expect a comma, followed by another include name
660 Lex(Tok);
661 if (Tok.isNot(tok::comma)) {
662 Diag(Tok, diag::warn_pragma_include_alias_expected) << ",";
663 return;
664 }
665
666 Token ReplaceFilenameTok;
667 CurPPLexer->LexIncludeFilename(ReplaceFilenameTok);
668 if (ReplaceFilenameTok.is(tok::eod)) {
669 // The diagnostic has already been handled
670 return;
671 }
672
673 StringRef ReplaceFileName;
674 if (ReplaceFilenameTok.is(tok::string_literal) ||
675 ReplaceFilenameTok.is(tok::angle_string_literal)) {
676 ReplaceFileName = getSpelling(ReplaceFilenameTok, FileNameBuffer);
677 } else if (ReplaceFilenameTok.is(tok::less)) {
678 // This could be a path instead of just a name
679 FileNameBuffer.push_back('<');
680 SourceLocation End;
681 if (ConcatenateIncludeName(FileNameBuffer, End))
682 return; // Diagnostic already emitted
683 ReplaceFileName = FileNameBuffer.str();
684 } else {
685 Diag(Tok, diag::warn_pragma_include_alias_expected_filename);
686 return;
687 }
688
689 // Finally, we expect the closing paren
690 Lex(Tok);
691 if (Tok.isNot(tok::r_paren)) {
692 Diag(Tok, diag::warn_pragma_include_alias_expected) << ")";
693 return;
694 }
695
696 // Now that we have the source and target filenames, we need to make sure
697 // they're both of the same type (angled vs non-angled)
698 StringRef OriginalSource = SourceFileName;
699
700 bool SourceIsAngled =
701 GetIncludeFilenameSpelling(SourceFilenameTok.getLocation(),
702 SourceFileName);
703 bool ReplaceIsAngled =
704 GetIncludeFilenameSpelling(ReplaceFilenameTok.getLocation(),
705 ReplaceFileName);
706 if (!SourceFileName.empty() && !ReplaceFileName.empty() &&
707 (SourceIsAngled != ReplaceIsAngled)) {
708 unsigned int DiagID;
709 if (SourceIsAngled)
710 DiagID = diag::warn_pragma_include_alias_mismatch_angle;
711 else
712 DiagID = diag::warn_pragma_include_alias_mismatch_quote;
713
714 Diag(SourceFilenameTok.getLocation(), DiagID)
715 << SourceFileName
716 << ReplaceFileName;
717
718 return;
719 }
720
721 // Now we can let the include handler know about this mapping
722 getHeaderSearchInfo().AddIncludeAlias(OriginalSource, ReplaceFileName);
723}
724
Chris Lattnerb694ba72006-07-02 22:41:36 +0000725/// AddPragmaHandler - Add the specified pragma handler to the preprocessor.
726/// If 'Namespace' is non-null, then it is a token required to exist on the
727/// pragma line before the pragma string starts, e.g. "STDC" or "GCC".
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000728void Preprocessor::AddPragmaHandler(StringRef Namespace,
Chris Lattnerb694ba72006-07-02 22:41:36 +0000729 PragmaHandler *Handler) {
730 PragmaNamespace *InsertNS = PragmaHandlers;
Mike Stump11289f42009-09-09 15:08:12 +0000731
Chris Lattnerb694ba72006-07-02 22:41:36 +0000732 // If this is specified to be in a namespace, step down into it.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000733 if (!Namespace.empty()) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000734 // If there is already a pragma handler with the name of this namespace,
735 // we either have an error (directive with the same name as a namespace) or
736 // we already have the namespace to insert into.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000737 if (PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace)) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000738 InsertNS = Existing->getIfNamespace();
739 assert(InsertNS != 0 && "Cannot have a pragma namespace and pragma"
740 " handler with the same name!");
741 } else {
742 // Otherwise, this namespace doesn't exist yet, create and insert the
743 // handler for it.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000744 InsertNS = new PragmaNamespace(Namespace);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000745 PragmaHandlers->AddPragma(InsertNS);
746 }
747 }
Mike Stump11289f42009-09-09 15:08:12 +0000748
Chris Lattnerb694ba72006-07-02 22:41:36 +0000749 // Check to make sure we don't already have a pragma for this identifier.
750 assert(!InsertNS->FindHandler(Handler->getName()) &&
751 "Pragma handler already exists for this identifier!");
752 InsertNS->AddPragma(Handler);
753}
754
Daniel Dunbar40596532008-10-04 19:17:46 +0000755/// RemovePragmaHandler - Remove the specific pragma handler from the
756/// preprocessor. If \arg Namespace is non-null, then it should be the
757/// namespace that \arg Handler was added to. It is an error to remove
758/// a handler that has not been registered.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000759void Preprocessor::RemovePragmaHandler(StringRef Namespace,
Daniel Dunbar40596532008-10-04 19:17:46 +0000760 PragmaHandler *Handler) {
761 PragmaNamespace *NS = PragmaHandlers;
Mike Stump11289f42009-09-09 15:08:12 +0000762
Daniel Dunbar40596532008-10-04 19:17:46 +0000763 // If this is specified to be in a namespace, step down into it.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000764 if (!Namespace.empty()) {
765 PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace);
Daniel Dunbar40596532008-10-04 19:17:46 +0000766 assert(Existing && "Namespace containing handler does not exist!");
767
768 NS = Existing->getIfNamespace();
769 assert(NS && "Invalid namespace, registered as a regular pragma handler!");
770 }
771
772 NS->RemovePragmaHandler(Handler);
Mike Stump11289f42009-09-09 15:08:12 +0000773
Daniel Dunbar40596532008-10-04 19:17:46 +0000774 // If this is a non-default namespace and it is now empty, remove
775 // it.
Argyrios Kyrtzidis7ce75262012-01-06 00:22:09 +0000776 if (NS != PragmaHandlers && NS->IsEmpty()) {
Daniel Dunbar40596532008-10-04 19:17:46 +0000777 PragmaHandlers->RemovePragmaHandler(NS);
Argyrios Kyrtzidis7ce75262012-01-06 00:22:09 +0000778 delete NS;
779 }
Daniel Dunbar40596532008-10-04 19:17:46 +0000780}
781
Peter Collingbourne3bffa522011-02-14 01:42:24 +0000782bool Preprocessor::LexOnOffSwitch(tok::OnOffSwitch &Result) {
783 Token Tok;
784 LexUnexpandedToken(Tok);
785
786 if (Tok.isNot(tok::identifier)) {
787 Diag(Tok, diag::ext_on_off_switch_syntax);
788 return true;
789 }
790 IdentifierInfo *II = Tok.getIdentifierInfo();
791 if (II->isStr("ON"))
792 Result = tok::OOS_ON;
793 else if (II->isStr("OFF"))
794 Result = tok::OOS_OFF;
795 else if (II->isStr("DEFAULT"))
796 Result = tok::OOS_DEFAULT;
797 else {
798 Diag(Tok, diag::ext_on_off_switch_syntax);
799 return true;
800 }
801
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000802 // Verify that this is followed by EOD.
Peter Collingbourne3bffa522011-02-14 01:42:24 +0000803 LexUnexpandedToken(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000804 if (Tok.isNot(tok::eod))
805 Diag(Tok, diag::ext_pragma_syntax_eod);
Peter Collingbourne3bffa522011-02-14 01:42:24 +0000806 return false;
807}
808
Chris Lattnerb694ba72006-07-02 22:41:36 +0000809namespace {
James Dennett18a6d792012-06-17 03:26:26 +0000810/// PragmaOnceHandler - "\#pragma once" marks the file as atomically included.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000811struct PragmaOnceHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000812 PragmaOnceHandler() : PragmaHandler("once") {}
Craig Topper9140dd22014-03-11 06:50:42 +0000813 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
814 Token &OnceTok) override {
Chris Lattnerce2ab6f2009-04-14 05:07:49 +0000815 PP.CheckEndOfDirective("pragma once");
Chris Lattnerb694ba72006-07-02 22:41:36 +0000816 PP.HandlePragmaOnce(OnceTok);
817 }
818};
819
James Dennett18a6d792012-06-17 03:26:26 +0000820/// PragmaMarkHandler - "\#pragma mark ..." is ignored by the compiler, and the
Chris Lattnerc2383312007-12-19 19:38:36 +0000821/// rest of the line is not lexed.
822struct PragmaMarkHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000823 PragmaMarkHandler() : PragmaHandler("mark") {}
Craig Topper9140dd22014-03-11 06:50:42 +0000824 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
825 Token &MarkTok) override {
Chris Lattnerc2383312007-12-19 19:38:36 +0000826 PP.HandlePragmaMark();
827 }
828};
829
James Dennett18a6d792012-06-17 03:26:26 +0000830/// PragmaPoisonHandler - "\#pragma poison x" marks x as not usable.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000831struct PragmaPoisonHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000832 PragmaPoisonHandler() : PragmaHandler("poison") {}
Craig Topper9140dd22014-03-11 06:50:42 +0000833 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
834 Token &PoisonTok) override {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000835 PP.HandlePragmaPoison(PoisonTok);
836 }
837};
838
James Dennett18a6d792012-06-17 03:26:26 +0000839/// PragmaSystemHeaderHandler - "\#pragma system_header" marks the current file
Chris Lattnerc2383312007-12-19 19:38:36 +0000840/// as a system header, which silences warnings in it.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000841struct PragmaSystemHeaderHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000842 PragmaSystemHeaderHandler() : PragmaHandler("system_header") {}
Craig Topper9140dd22014-03-11 06:50:42 +0000843 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
844 Token &SHToken) override {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000845 PP.HandlePragmaSystemHeader(SHToken);
Chris Lattnerce2ab6f2009-04-14 05:07:49 +0000846 PP.CheckEndOfDirective("pragma");
Chris Lattnerb694ba72006-07-02 22:41:36 +0000847 }
848};
849struct PragmaDependencyHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000850 PragmaDependencyHandler() : PragmaHandler("dependency") {}
Craig Topper9140dd22014-03-11 06:50:42 +0000851 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
852 Token &DepToken) override {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000853 PP.HandlePragmaDependency(DepToken);
854 }
855};
Mike Stump11289f42009-09-09 15:08:12 +0000856
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000857struct PragmaDebugHandler : public PragmaHandler {
858 PragmaDebugHandler() : PragmaHandler("__debug") {}
Craig Topper9140dd22014-03-11 06:50:42 +0000859 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
860 Token &DepToken) override {
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000861 Token Tok;
862 PP.LexUnexpandedToken(Tok);
863 if (Tok.isNot(tok::identifier)) {
Douglas Gregor3cc26482010-08-30 15:15:34 +0000864 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000865 return;
866 }
867 IdentifierInfo *II = Tok.getIdentifierInfo();
868
Daniel Dunbarf2cf3292010-08-17 22:32:48 +0000869 if (II->isStr("assert")) {
David Blaikie83d382b2011-09-23 05:06:16 +0000870 llvm_unreachable("This is an assertion!");
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000871 } else if (II->isStr("crash")) {
David Blaikie5bd4c2a2012-08-21 18:56:49 +0000872 LLVM_BUILTIN_TRAP;
David Blaikie5d577a22012-06-29 22:03:56 +0000873 } else if (II->isStr("parser_crash")) {
874 Token Crasher;
875 Crasher.setKind(tok::annot_pragma_parser_crash);
876 PP.EnterToken(Crasher);
Daniel Dunbarf2cf3292010-08-17 22:32:48 +0000877 } else if (II->isStr("llvm_fatal_error")) {
878 llvm::report_fatal_error("#pragma clang __debug llvm_fatal_error");
879 } else if (II->isStr("llvm_unreachable")) {
880 llvm_unreachable("#pragma clang __debug llvm_unreachable");
881 } else if (II->isStr("overflow_stack")) {
882 DebugOverflowStack();
Daniel Dunbar211a7872010-08-18 23:09:23 +0000883 } else if (II->isStr("handle_crash")) {
884 llvm::CrashRecoveryContext *CRC =llvm::CrashRecoveryContext::GetCurrent();
885 if (CRC)
886 CRC->HandleCrash();
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000887 } else if (II->isStr("captured")) {
888 HandleCaptured(PP);
Daniel Dunbarf2cf3292010-08-17 22:32:48 +0000889 } else {
890 PP.Diag(Tok, diag::warn_pragma_debug_unexpected_command)
891 << II->getName();
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000892 }
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000893
894 PPCallbacks *Callbacks = PP.getPPCallbacks();
895 if (Callbacks)
896 Callbacks->PragmaDebug(Tok.getLocation(), II->getName());
897 }
898
899 void HandleCaptured(Preprocessor &PP) {
900 // Skip if emitting preprocessed output.
901 if (PP.isPreprocessedOutput())
902 return;
903
904 Token Tok;
905 PP.LexUnexpandedToken(Tok);
906
907 if (Tok.isNot(tok::eod)) {
908 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol)
909 << "pragma clang __debug captured";
910 return;
911 }
912
913 SourceLocation NameLoc = Tok.getLocation();
914 Token *Toks = PP.getPreprocessorAllocator().Allocate<Token>(1);
915 Toks->startToken();
916 Toks->setKind(tok::annot_pragma_captured);
917 Toks->setLocation(NameLoc);
918
919 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
920 /*OwnsTokens=*/false);
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000921 }
922
Francois Pichet2e11f5d2011-05-25 16:15:03 +0000923// Disable MSVC warning about runtime stack overflow.
924#ifdef _MSC_VER
925 #pragma warning(disable : 4717)
926#endif
Richard Trieu0732beb2013-12-21 01:04:02 +0000927 static void DebugOverflowStack() {
928 void (*volatile Self)() = DebugOverflowStack;
929 Self();
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000930 }
Francois Pichet2e11f5d2011-05-25 16:15:03 +0000931#ifdef _MSC_VER
932 #pragma warning(default : 4717)
933#endif
934
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000935};
936
James Dennett18a6d792012-06-17 03:26:26 +0000937/// PragmaDiagnosticHandler - e.g. '\#pragma GCC diagnostic ignored "-Wformat"'
Chris Lattner504af112009-04-19 23:16:58 +0000938struct PragmaDiagnosticHandler : public PragmaHandler {
Douglas Gregor3bde9b12011-06-22 19:41:48 +0000939private:
940 const char *Namespace;
Chris Lattnerfb42a182009-07-12 21:18:45 +0000941public:
Douglas Gregor3bde9b12011-06-22 19:41:48 +0000942 explicit PragmaDiagnosticHandler(const char *NS) :
943 PragmaHandler("diagnostic"), Namespace(NS) {}
Craig Topper9140dd22014-03-11 06:50:42 +0000944 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
945 Token &DiagToken) override {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000946 SourceLocation DiagLoc = DiagToken.getLocation();
Chris Lattner504af112009-04-19 23:16:58 +0000947 Token Tok;
948 PP.LexUnexpandedToken(Tok);
949 if (Tok.isNot(tok::identifier)) {
Douglas Gregor3cc26482010-08-30 15:15:34 +0000950 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Chris Lattner504af112009-04-19 23:16:58 +0000951 return;
952 }
953 IdentifierInfo *II = Tok.getIdentifierInfo();
Douglas Gregor3bde9b12011-06-22 19:41:48 +0000954 PPCallbacks *Callbacks = PP.getPPCallbacks();
Mike Stump11289f42009-09-09 15:08:12 +0000955
Chris Lattner504af112009-04-19 23:16:58 +0000956 diag::Mapping Map;
957 if (II->isStr("warning"))
958 Map = diag::MAP_WARNING;
959 else if (II->isStr("error"))
960 Map = diag::MAP_ERROR;
961 else if (II->isStr("ignored"))
962 Map = diag::MAP_IGNORE;
963 else if (II->isStr("fatal"))
964 Map = diag::MAP_FATAL;
Douglas Gregor3cc26482010-08-30 15:15:34 +0000965 else if (II->isStr("pop")) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000966 if (!PP.getDiagnostics().popMappings(DiagLoc))
Douglas Gregor3cc26482010-08-30 15:15:34 +0000967 PP.Diag(Tok, diag::warn_pragma_diagnostic_cannot_pop);
Douglas Gregor3bde9b12011-06-22 19:41:48 +0000968 else if (Callbacks)
969 Callbacks->PragmaDiagnosticPop(DiagLoc, Namespace);
Douglas Gregor3cc26482010-08-30 15:15:34 +0000970 return;
971 } else if (II->isStr("push")) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000972 PP.getDiagnostics().pushMappings(DiagLoc);
Douglas Gregor3bde9b12011-06-22 19:41:48 +0000973 if (Callbacks)
974 Callbacks->PragmaDiagnosticPush(DiagLoc, Namespace);
Chris Lattnerfb42a182009-07-12 21:18:45 +0000975 return;
976 } else {
Douglas Gregor3cc26482010-08-30 15:15:34 +0000977 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Chris Lattner504af112009-04-19 23:16:58 +0000978 return;
979 }
Mike Stump11289f42009-09-09 15:08:12 +0000980
Chris Lattner504af112009-04-19 23:16:58 +0000981 PP.LexUnexpandedToken(Tok);
Andy Gibbs58905d22012-11-17 19:15:38 +0000982 SourceLocation StringLoc = Tok.getLocation();
Chris Lattner504af112009-04-19 23:16:58 +0000983
Andy Gibbs58905d22012-11-17 19:15:38 +0000984 std::string WarningName;
Andy Gibbsa8df57a2012-11-17 19:16:52 +0000985 if (!PP.FinishLexStringLiteral(Tok, WarningName, "pragma diagnostic",
986 /*MacroExpansion=*/false))
Chris Lattner504af112009-04-19 23:16:58 +0000987 return;
Mike Stump11289f42009-09-09 15:08:12 +0000988
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000989 if (Tok.isNot(tok::eod)) {
Chris Lattner504af112009-04-19 23:16:58 +0000990 PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token);
991 return;
992 }
Mike Stump11289f42009-09-09 15:08:12 +0000993
Chris Lattner504af112009-04-19 23:16:58 +0000994 if (WarningName.size() < 3 || WarningName[0] != '-' ||
995 WarningName[1] != 'W') {
Andy Gibbs58905d22012-11-17 19:15:38 +0000996 PP.Diag(StringLoc, diag::warn_pragma_diagnostic_invalid_option);
Chris Lattner504af112009-04-19 23:16:58 +0000997 return;
998 }
Mike Stump11289f42009-09-09 15:08:12 +0000999
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +00001000 if (PP.getDiagnostics().setDiagnosticGroupMapping(WarningName.substr(2),
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +00001001 Map, DiagLoc))
Andy Gibbs58905d22012-11-17 19:15:38 +00001002 PP.Diag(StringLoc, diag::warn_pragma_diagnostic_unknown_warning)
1003 << WarningName;
Douglas Gregor3bde9b12011-06-22 19:41:48 +00001004 else if (Callbacks)
1005 Callbacks->PragmaDiagnostic(DiagLoc, Namespace, Map, WarningName);
Chris Lattner504af112009-04-19 23:16:58 +00001006 }
1007};
Mike Stump11289f42009-09-09 15:08:12 +00001008
Reid Kleckner881dff32013-09-13 22:00:30 +00001009/// "\#pragma warning(...)". MSVC's diagnostics do not map cleanly to clang's
1010/// diagnostics, so we don't really implement this pragma. We parse it and
1011/// ignore it to avoid -Wunknown-pragma warnings.
1012struct PragmaWarningHandler : public PragmaHandler {
1013 PragmaWarningHandler() : PragmaHandler("warning") {}
1014
Craig Topper9140dd22014-03-11 06:50:42 +00001015 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1016 Token &Tok) override {
Reid Kleckner881dff32013-09-13 22:00:30 +00001017 // Parse things like:
1018 // warning(push, 1)
1019 // warning(pop)
John Thompson4762b232013-11-16 00:16:03 +00001020 // warning(disable : 1 2 3 ; error : 4 5 6 ; suppress : 7 8 9)
Reid Kleckner881dff32013-09-13 22:00:30 +00001021 SourceLocation DiagLoc = Tok.getLocation();
1022 PPCallbacks *Callbacks = PP.getPPCallbacks();
1023
1024 PP.Lex(Tok);
1025 if (Tok.isNot(tok::l_paren)) {
1026 PP.Diag(Tok, diag::warn_pragma_warning_expected) << "(";
1027 return;
1028 }
1029
1030 PP.Lex(Tok);
1031 IdentifierInfo *II = Tok.getIdentifierInfo();
1032 if (!II) {
1033 PP.Diag(Tok, diag::warn_pragma_warning_spec_invalid);
1034 return;
1035 }
1036
1037 if (II->isStr("push")) {
1038 // #pragma warning( push[ ,n ] )
Reid Kleckner4d185102013-10-02 15:19:23 +00001039 int Level = -1;
Reid Kleckner881dff32013-09-13 22:00:30 +00001040 PP.Lex(Tok);
1041 if (Tok.is(tok::comma)) {
1042 PP.Lex(Tok);
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001043 uint64_t Value;
1044 if (Tok.is(tok::numeric_constant) &&
1045 PP.parseSimpleIntegerLiteral(Tok, Value))
1046 Level = int(Value);
Reid Kleckner4d185102013-10-02 15:19:23 +00001047 if (Level < 0 || Level > 4) {
Reid Kleckner881dff32013-09-13 22:00:30 +00001048 PP.Diag(Tok, diag::warn_pragma_warning_push_level);
1049 return;
1050 }
1051 }
1052 if (Callbacks)
1053 Callbacks->PragmaWarningPush(DiagLoc, Level);
1054 } else if (II->isStr("pop")) {
1055 // #pragma warning( pop )
1056 PP.Lex(Tok);
1057 if (Callbacks)
1058 Callbacks->PragmaWarningPop(DiagLoc);
1059 } else {
1060 // #pragma warning( warning-specifier : warning-number-list
1061 // [; warning-specifier : warning-number-list...] )
1062 while (true) {
1063 II = Tok.getIdentifierInfo();
1064 if (!II) {
1065 PP.Diag(Tok, diag::warn_pragma_warning_spec_invalid);
1066 return;
1067 }
1068
1069 // Figure out which warning specifier this is.
1070 StringRef Specifier = II->getName();
1071 bool SpecifierValid =
1072 llvm::StringSwitch<bool>(Specifier)
1073 .Cases("1", "2", "3", "4", true)
1074 .Cases("default", "disable", "error", "once", "suppress", true)
1075 .Default(false);
1076 if (!SpecifierValid) {
1077 PP.Diag(Tok, diag::warn_pragma_warning_spec_invalid);
1078 return;
1079 }
1080 PP.Lex(Tok);
1081 if (Tok.isNot(tok::colon)) {
1082 PP.Diag(Tok, diag::warn_pragma_warning_expected) << ":";
1083 return;
1084 }
1085
1086 // Collect the warning ids.
1087 SmallVector<int, 4> Ids;
1088 PP.Lex(Tok);
1089 while (Tok.is(tok::numeric_constant)) {
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001090 uint64_t Value;
1091 if (!PP.parseSimpleIntegerLiteral(Tok, Value) || Value == 0 ||
1092 Value > INT_MAX) {
Reid Kleckner881dff32013-09-13 22:00:30 +00001093 PP.Diag(Tok, diag::warn_pragma_warning_expected_number);
1094 return;
1095 }
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001096 Ids.push_back(int(Value));
Reid Kleckner881dff32013-09-13 22:00:30 +00001097 }
1098 if (Callbacks)
1099 Callbacks->PragmaWarning(DiagLoc, Specifier, Ids);
1100
1101 // Parse the next specifier if there is a semicolon.
1102 if (Tok.isNot(tok::semi))
1103 break;
1104 PP.Lex(Tok);
1105 }
1106 }
1107
1108 if (Tok.isNot(tok::r_paren)) {
1109 PP.Diag(Tok, diag::warn_pragma_warning_expected) << ")";
1110 return;
1111 }
1112
1113 PP.Lex(Tok);
1114 if (Tok.isNot(tok::eod))
1115 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma warning";
1116 }
1117};
1118
James Dennett18a6d792012-06-17 03:26:26 +00001119/// PragmaIncludeAliasHandler - "\#pragma include_alias("...")".
Aaron Ballman611306e2012-03-02 22:51:54 +00001120struct PragmaIncludeAliasHandler : public PragmaHandler {
1121 PragmaIncludeAliasHandler() : PragmaHandler("include_alias") {}
Craig Topper9140dd22014-03-11 06:50:42 +00001122 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1123 Token &IncludeAliasTok) override {
Reid Kleckner881dff32013-09-13 22:00:30 +00001124 PP.HandlePragmaIncludeAlias(IncludeAliasTok);
Aaron Ballman611306e2012-03-02 22:51:54 +00001125 }
1126};
1127
Andy Gibbs9c2ccd62013-04-17 16:16:16 +00001128/// PragmaMessageHandler - Handle the microsoft and gcc \#pragma message
1129/// extension. The syntax is:
1130/// \code
1131/// #pragma message(string)
1132/// \endcode
1133/// OR, in GCC mode:
1134/// \code
1135/// #pragma message string
1136/// \endcode
1137/// string is a string, which is fully macro expanded, and permits string
1138/// concatenation, embedded escape characters, etc... See MSDN for more details.
1139/// Also handles \#pragma GCC warning and \#pragma GCC error which take the same
1140/// form as \#pragma message.
Chris Lattner30c924b2010-06-26 17:11:39 +00001141struct PragmaMessageHandler : public PragmaHandler {
Andy Gibbs9c2ccd62013-04-17 16:16:16 +00001142private:
1143 const PPCallbacks::PragmaMessageKind Kind;
1144 const StringRef Namespace;
1145
1146 static const char* PragmaKind(PPCallbacks::PragmaMessageKind Kind,
1147 bool PragmaNameOnly = false) {
1148 switch (Kind) {
1149 case PPCallbacks::PMK_Message:
1150 return PragmaNameOnly ? "message" : "pragma message";
1151 case PPCallbacks::PMK_Warning:
1152 return PragmaNameOnly ? "warning" : "pragma warning";
1153 case PPCallbacks::PMK_Error:
1154 return PragmaNameOnly ? "error" : "pragma error";
1155 }
1156 llvm_unreachable("Unknown PragmaMessageKind!");
1157 }
1158
1159public:
1160 PragmaMessageHandler(PPCallbacks::PragmaMessageKind Kind,
1161 StringRef Namespace = StringRef())
1162 : PragmaHandler(PragmaKind(Kind, true)), Kind(Kind), Namespace(Namespace) {}
1163
Craig Topper9140dd22014-03-11 06:50:42 +00001164 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1165 Token &Tok) override {
Andy Gibbs9c2ccd62013-04-17 16:16:16 +00001166 SourceLocation MessageLoc = Tok.getLocation();
1167 PP.Lex(Tok);
1168 bool ExpectClosingParen = false;
1169 switch (Tok.getKind()) {
1170 case tok::l_paren:
1171 // We have a MSVC style pragma message.
1172 ExpectClosingParen = true;
1173 // Read the string.
1174 PP.Lex(Tok);
1175 break;
1176 case tok::string_literal:
1177 // We have a GCC style pragma message, and we just read the string.
1178 break;
1179 default:
1180 PP.Diag(MessageLoc, diag::err_pragma_message_malformed) << Kind;
1181 return;
1182 }
1183
1184 std::string MessageString;
1185 if (!PP.FinishLexStringLiteral(Tok, MessageString, PragmaKind(Kind),
1186 /*MacroExpansion=*/true))
1187 return;
1188
1189 if (ExpectClosingParen) {
1190 if (Tok.isNot(tok::r_paren)) {
1191 PP.Diag(Tok.getLocation(), diag::err_pragma_message_malformed) << Kind;
1192 return;
1193 }
1194 PP.Lex(Tok); // eat the r_paren.
1195 }
1196
1197 if (Tok.isNot(tok::eod)) {
1198 PP.Diag(Tok.getLocation(), diag::err_pragma_message_malformed) << Kind;
1199 return;
1200 }
1201
1202 // Output the message.
1203 PP.Diag(MessageLoc, (Kind == PPCallbacks::PMK_Error)
1204 ? diag::err_pragma_message
1205 : diag::warn_pragma_message) << MessageString;
1206
1207 // If the pragma is lexically sound, notify any interested PPCallbacks.
1208 if (PPCallbacks *Callbacks = PP.getPPCallbacks())
1209 Callbacks->PragmaMessage(MessageLoc, Namespace, Kind, MessageString);
Chris Lattner30c924b2010-06-26 17:11:39 +00001210 }
1211};
1212
James Dennett18a6d792012-06-17 03:26:26 +00001213/// PragmaPushMacroHandler - "\#pragma push_macro" saves the value of the
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001214/// macro on the top of the stack.
1215struct PragmaPushMacroHandler : public PragmaHandler {
1216 PragmaPushMacroHandler() : PragmaHandler("push_macro") {}
Craig Topper9140dd22014-03-11 06:50:42 +00001217 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1218 Token &PushMacroTok) override {
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001219 PP.HandlePragmaPushMacro(PushMacroTok);
1220 }
1221};
1222
1223
James Dennett18a6d792012-06-17 03:26:26 +00001224/// PragmaPopMacroHandler - "\#pragma pop_macro" sets the value of the
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001225/// macro to the value on the top of the stack.
1226struct PragmaPopMacroHandler : public PragmaHandler {
1227 PragmaPopMacroHandler() : PragmaHandler("pop_macro") {}
Craig Topper9140dd22014-03-11 06:50:42 +00001228 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1229 Token &PopMacroTok) override {
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001230 PP.HandlePragmaPopMacro(PopMacroTok);
1231 }
1232};
1233
Chris Lattner958ee042009-04-19 21:20:35 +00001234// Pragma STDC implementations.
Chris Lattner02ef4e32009-04-19 21:50:08 +00001235
James Dennett18a6d792012-06-17 03:26:26 +00001236/// PragmaSTDC_FENV_ACCESSHandler - "\#pragma STDC FENV_ACCESS ...".
Chris Lattner958ee042009-04-19 21:20:35 +00001237struct PragmaSTDC_FENV_ACCESSHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001238 PragmaSTDC_FENV_ACCESSHandler() : PragmaHandler("FENV_ACCESS") {}
Craig Topper9140dd22014-03-11 06:50:42 +00001239 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1240 Token &Tok) override {
Peter Collingbourne3bffa522011-02-14 01:42:24 +00001241 tok::OnOffSwitch OOS;
1242 if (PP.LexOnOffSwitch(OOS))
1243 return;
1244 if (OOS == tok::OOS_ON)
Chris Lattnerdf222682009-04-19 21:55:32 +00001245 PP.Diag(Tok, diag::warn_stdc_fenv_access_not_supported);
Chris Lattner958ee042009-04-19 21:20:35 +00001246 }
1247};
Mike Stump11289f42009-09-09 15:08:12 +00001248
James Dennett18a6d792012-06-17 03:26:26 +00001249/// PragmaSTDC_CX_LIMITED_RANGEHandler - "\#pragma STDC CX_LIMITED_RANGE ...".
Chris Lattner958ee042009-04-19 21:20:35 +00001250struct PragmaSTDC_CX_LIMITED_RANGEHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001251 PragmaSTDC_CX_LIMITED_RANGEHandler()
1252 : PragmaHandler("CX_LIMITED_RANGE") {}
Craig Topper9140dd22014-03-11 06:50:42 +00001253 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1254 Token &Tok) override {
Peter Collingbourne3bffa522011-02-14 01:42:24 +00001255 tok::OnOffSwitch OOS;
1256 PP.LexOnOffSwitch(OOS);
Chris Lattner958ee042009-04-19 21:20:35 +00001257 }
1258};
Mike Stump11289f42009-09-09 15:08:12 +00001259
James Dennett18a6d792012-06-17 03:26:26 +00001260/// PragmaSTDC_UnknownHandler - "\#pragma STDC ...".
Chris Lattner958ee042009-04-19 21:20:35 +00001261struct PragmaSTDC_UnknownHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001262 PragmaSTDC_UnknownHandler() {}
Craig Topper9140dd22014-03-11 06:50:42 +00001263 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1264 Token &UnknownTok) override {
Chris Lattner02ef4e32009-04-19 21:50:08 +00001265 // C99 6.10.6p2, unknown forms are not allowed.
Chris Lattnera0b1f762009-04-19 21:25:37 +00001266 PP.Diag(UnknownTok, diag::ext_stdc_pragma_ignored);
Chris Lattner958ee042009-04-19 21:20:35 +00001267 }
1268};
Mike Stump11289f42009-09-09 15:08:12 +00001269
John McCall32f5fe12011-09-30 05:12:12 +00001270/// PragmaARCCFCodeAuditedHandler -
James Dennett18a6d792012-06-17 03:26:26 +00001271/// \#pragma clang arc_cf_code_audited begin/end
John McCall32f5fe12011-09-30 05:12:12 +00001272struct PragmaARCCFCodeAuditedHandler : public PragmaHandler {
1273 PragmaARCCFCodeAuditedHandler() : PragmaHandler("arc_cf_code_audited") {}
Craig Topper9140dd22014-03-11 06:50:42 +00001274 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1275 Token &NameTok) override {
John McCall32f5fe12011-09-30 05:12:12 +00001276 SourceLocation Loc = NameTok.getLocation();
1277 bool IsBegin;
1278
1279 Token Tok;
1280
1281 // Lex the 'begin' or 'end'.
1282 PP.LexUnexpandedToken(Tok);
1283 const IdentifierInfo *BeginEnd = Tok.getIdentifierInfo();
1284 if (BeginEnd && BeginEnd->isStr("begin")) {
1285 IsBegin = true;
1286 } else if (BeginEnd && BeginEnd->isStr("end")) {
1287 IsBegin = false;
1288 } else {
1289 PP.Diag(Tok.getLocation(), diag::err_pp_arc_cf_code_audited_syntax);
1290 return;
1291 }
1292
1293 // Verify that this is followed by EOD.
1294 PP.LexUnexpandedToken(Tok);
1295 if (Tok.isNot(tok::eod))
1296 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
1297
1298 // The start location of the active audit.
1299 SourceLocation BeginLoc = PP.getPragmaARCCFCodeAuditedLoc();
1300
1301 // The start location we want after processing this.
1302 SourceLocation NewLoc;
1303
1304 if (IsBegin) {
1305 // Complain about attempts to re-enter an audit.
1306 if (BeginLoc.isValid()) {
1307 PP.Diag(Loc, diag::err_pp_double_begin_of_arc_cf_code_audited);
1308 PP.Diag(BeginLoc, diag::note_pragma_entered_here);
1309 }
1310 NewLoc = Loc;
1311 } else {
1312 // Complain about attempts to leave an audit that doesn't exist.
1313 if (!BeginLoc.isValid()) {
1314 PP.Diag(Loc, diag::err_pp_unmatched_end_of_arc_cf_code_audited);
1315 return;
1316 }
1317 NewLoc = SourceLocation();
1318 }
1319
1320 PP.setPragmaARCCFCodeAuditedLoc(NewLoc);
1321 }
1322};
1323
David Majnemer7aa8c2f2013-06-30 08:18:16 +00001324/// \brief Handle "\#pragma region [...]"
1325///
1326/// The syntax is
1327/// \code
1328/// #pragma region [optional name]
1329/// #pragma endregion [optional comment]
1330/// \endcode
1331///
1332/// \note This is
1333/// <a href="http://msdn.microsoft.com/en-us/library/b6xkz944(v=vs.80).aspx">editor-only</a>
1334/// pragma, just skipped by compiler.
1335struct PragmaRegionHandler : public PragmaHandler {
1336 PragmaRegionHandler(const char *pragma) : PragmaHandler(pragma) { }
Aaron Ballman406ea512012-11-30 19:52:30 +00001337
Craig Topper9140dd22014-03-11 06:50:42 +00001338 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1339 Token &NameTok) override {
David Majnemer7aa8c2f2013-06-30 08:18:16 +00001340 // #pragma region: endregion matches can be verified
1341 // __pragma(region): no sense, but ignored by msvc
1342 // _Pragma is not valid for MSVC, but there isn't any point
1343 // to handle a _Pragma differently.
1344 }
1345};
Aaron Ballman406ea512012-11-30 19:52:30 +00001346
Chris Lattnerb694ba72006-07-02 22:41:36 +00001347} // end anonymous namespace
1348
1349
1350/// RegisterBuiltinPragmas - Install the standard preprocessor pragmas:
James Dennett18a6d792012-06-17 03:26:26 +00001351/// \#pragma GCC poison/system_header/dependency and \#pragma once.
Chris Lattnerb694ba72006-07-02 22:41:36 +00001352void Preprocessor::RegisterBuiltinPragmas() {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001353 AddPragmaHandler(new PragmaOnceHandler());
1354 AddPragmaHandler(new PragmaMarkHandler());
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001355 AddPragmaHandler(new PragmaPushMacroHandler());
1356 AddPragmaHandler(new PragmaPopMacroHandler());
Andy Gibbs9c2ccd62013-04-17 16:16:16 +00001357 AddPragmaHandler(new PragmaMessageHandler(PPCallbacks::PMK_Message));
Mike Stump11289f42009-09-09 15:08:12 +00001358
Chris Lattnerb61448d2009-05-12 18:21:11 +00001359 // #pragma GCC ...
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001360 AddPragmaHandler("GCC", new PragmaPoisonHandler());
1361 AddPragmaHandler("GCC", new PragmaSystemHeaderHandler());
1362 AddPragmaHandler("GCC", new PragmaDependencyHandler());
Douglas Gregor3bde9b12011-06-22 19:41:48 +00001363 AddPragmaHandler("GCC", new PragmaDiagnosticHandler("GCC"));
Andy Gibbs9c2ccd62013-04-17 16:16:16 +00001364 AddPragmaHandler("GCC", new PragmaMessageHandler(PPCallbacks::PMK_Warning,
1365 "GCC"));
1366 AddPragmaHandler("GCC", new PragmaMessageHandler(PPCallbacks::PMK_Error,
1367 "GCC"));
Chris Lattnerb61448d2009-05-12 18:21:11 +00001368 // #pragma clang ...
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001369 AddPragmaHandler("clang", new PragmaPoisonHandler());
1370 AddPragmaHandler("clang", new PragmaSystemHeaderHandler());
Daniel Dunbarb8068c32010-07-28 15:40:33 +00001371 AddPragmaHandler("clang", new PragmaDebugHandler());
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001372 AddPragmaHandler("clang", new PragmaDependencyHandler());
Douglas Gregor3bde9b12011-06-22 19:41:48 +00001373 AddPragmaHandler("clang", new PragmaDiagnosticHandler("clang"));
John McCall32f5fe12011-09-30 05:12:12 +00001374 AddPragmaHandler("clang", new PragmaARCCFCodeAuditedHandler());
Chris Lattnerb61448d2009-05-12 18:21:11 +00001375
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001376 AddPragmaHandler("STDC", new PragmaSTDC_FENV_ACCESSHandler());
1377 AddPragmaHandler("STDC", new PragmaSTDC_CX_LIMITED_RANGEHandler());
Chris Lattner958ee042009-04-19 21:20:35 +00001378 AddPragmaHandler("STDC", new PragmaSTDC_UnknownHandler());
Mike Stump11289f42009-09-09 15:08:12 +00001379
Chris Lattner2ff698d2009-01-16 08:21:25 +00001380 // MS extensions.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001381 if (LangOpts.MicrosoftExt) {
Reid Kleckner881dff32013-09-13 22:00:30 +00001382 AddPragmaHandler(new PragmaWarningHandler());
Aaron Ballman611306e2012-03-02 22:51:54 +00001383 AddPragmaHandler(new PragmaIncludeAliasHandler());
Aaron Ballman406ea512012-11-30 19:52:30 +00001384 AddPragmaHandler(new PragmaRegionHandler("region"));
1385 AddPragmaHandler(new PragmaRegionHandler("endregion"));
Chris Lattner30c924b2010-06-26 17:11:39 +00001386 }
Chris Lattnerb694ba72006-07-02 22:41:36 +00001387}