blob: f6532c2175a17758f5178745b2d80b2ecd5b8cd6 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Pragma.cpp - Pragma registration and handling --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the PragmaHandler/PragmaTable interfaces and implements
11// pragma related methods of the Preprocessor class.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Lex/Pragma.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000016#include "clang/Lex/HeaderSearch.h"
Chris Lattnera9d91452009-01-16 18:59:23 +000017#include "clang/Lex/LiteralSupport.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000018#include "clang/Lex/Preprocessor.h"
Chris Lattnerf47724b2010-08-17 15:55:45 +000019#include "clang/Lex/MacroInfo.h"
Chris Lattner500d3292009-01-29 05:15:15 +000020#include "clang/Lex/LexDiagnostic.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000021#include "clang/Basic/FileManager.h"
22#include "clang/Basic/SourceManager.h"
Daniel Dunbarff759a62010-08-18 23:09:23 +000023#include "llvm/Support/CrashRecoveryContext.h"
Daniel Dunbar55054132010-08-17 22:32:48 +000024#include "llvm/Support/ErrorHandling.h"
Douglas Gregor2e222532009-07-02 17:08:52 +000025#include <algorithm>
Reid Spencer5f016e22007-07-11 17:01:13 +000026using namespace clang;
27
28// Out-of-line destructor to provide a home for the class.
29PragmaHandler::~PragmaHandler() {
30}
31
32//===----------------------------------------------------------------------===//
Daniel Dunbarc72cc502010-06-11 20:10:12 +000033// EmptyPragmaHandler Implementation.
34//===----------------------------------------------------------------------===//
35
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +000036EmptyPragmaHandler::EmptyPragmaHandler() {}
Daniel Dunbarc72cc502010-06-11 20:10:12 +000037
Douglas Gregor80c60f72010-09-09 22:45:38 +000038void EmptyPragmaHandler::HandlePragma(Preprocessor &PP,
39 PragmaIntroducerKind Introducer,
40 Token &FirstToken) {}
Daniel Dunbarc72cc502010-06-11 20:10:12 +000041
42//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +000043// PragmaNamespace Implementation.
44//===----------------------------------------------------------------------===//
45
46
47PragmaNamespace::~PragmaNamespace() {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +000048 for (llvm::StringMap<PragmaHandler*>::iterator
49 I = Handlers.begin(), E = Handlers.end(); I != E; ++I)
50 delete I->second;
Reid Spencer5f016e22007-07-11 17:01:13 +000051}
52
53/// FindHandler - Check to see if there is already a handler for the
54/// specified name. If not, return the handler for the null identifier if it
55/// exists, otherwise return null. If IgnoreNull is true (the default) then
56/// the null handler isn't returned on failure to match.
Chris Lattner5f9e2722011-07-23 10:55:15 +000057PragmaHandler *PragmaNamespace::FindHandler(StringRef Name,
Reid Spencer5f016e22007-07-11 17:01:13 +000058 bool IgnoreNull) const {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +000059 if (PragmaHandler *Handler = Handlers.lookup(Name))
60 return Handler;
Chris Lattner5f9e2722011-07-23 10:55:15 +000061 return IgnoreNull ? 0 : Handlers.lookup(StringRef());
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +000062}
Mike Stump1eb44332009-09-09 15:08:12 +000063
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +000064void PragmaNamespace::AddPragma(PragmaHandler *Handler) {
65 assert(!Handlers.lookup(Handler->getName()) &&
66 "A handler with this name is already registered in this namespace");
67 llvm::StringMapEntry<PragmaHandler *> &Entry =
68 Handlers.GetOrCreateValue(Handler->getName());
69 Entry.setValue(Handler);
Reid Spencer5f016e22007-07-11 17:01:13 +000070}
71
Daniel Dunbar40950802008-10-04 19:17:46 +000072void PragmaNamespace::RemovePragmaHandler(PragmaHandler *Handler) {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +000073 assert(Handlers.lookup(Handler->getName()) &&
74 "Handler not registered in this namespace");
75 Handlers.erase(Handler->getName());
Daniel Dunbar40950802008-10-04 19:17:46 +000076}
77
Douglas Gregor80c60f72010-09-09 22:45:38 +000078void PragmaNamespace::HandlePragma(Preprocessor &PP,
79 PragmaIntroducerKind Introducer,
80 Token &Tok) {
Reid Spencer5f016e22007-07-11 17:01:13 +000081 // Read the 'namespace' that the directive is in, e.g. STDC. Do not macro
82 // expand it, the user can have a STDC #define, that should not affect this.
83 PP.LexUnexpandedToken(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +000084
Reid Spencer5f016e22007-07-11 17:01:13 +000085 // Get the handler for this token. If there is no handler, ignore the pragma.
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +000086 PragmaHandler *Handler
87 = FindHandler(Tok.getIdentifierInfo() ? Tok.getIdentifierInfo()->getName()
Chris Lattner5f9e2722011-07-23 10:55:15 +000088 : StringRef(),
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +000089 /*IgnoreNull=*/false);
Chris Lattneraf7cdf42009-04-19 21:10:26 +000090 if (Handler == 0) {
91 PP.Diag(Tok, diag::warn_pragma_ignored);
92 return;
93 }
Mike Stump1eb44332009-09-09 15:08:12 +000094
Reid Spencer5f016e22007-07-11 17:01:13 +000095 // Otherwise, pass it down.
Douglas Gregor80c60f72010-09-09 22:45:38 +000096 Handler->HandlePragma(PP, Introducer, Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +000097}
98
99//===----------------------------------------------------------------------===//
100// Preprocessor Pragma Directive Handling.
101//===----------------------------------------------------------------------===//
102
103/// HandlePragmaDirective - The "#pragma" directive has been parsed. Lex the
104/// rest of the pragma, passing it to the registered pragma handlers.
Douglas Gregor80c60f72010-09-09 22:45:38 +0000105void Preprocessor::HandlePragmaDirective(unsigned Introducer) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000106 ++NumPragma;
Mike Stump1eb44332009-09-09 15:08:12 +0000107
Reid Spencer5f016e22007-07-11 17:01:13 +0000108 // Invoke the first level of pragma handlers which reads the namespace id.
Chris Lattnerd2177732007-07-20 16:59:19 +0000109 Token Tok;
Douglas Gregor80c60f72010-09-09 22:45:38 +0000110 PragmaHandlers->HandlePragma(*this, PragmaIntroducerKind(Introducer), Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000111
Reid Spencer5f016e22007-07-11 17:01:13 +0000112 // If the pragma handler didn't read the rest of the line, consume it now.
Peter Collingbourneb2eb53d2011-02-22 13:49:00 +0000113 if ((CurTokenLexer && CurTokenLexer->isParsingPreprocessorDirective())
114 || (CurPPLexer && CurPPLexer->ParsingPreprocessorDirective))
Reid Spencer5f016e22007-07-11 17:01:13 +0000115 DiscardUntilEndOfDirective();
116}
117
118/// Handle_Pragma - Read a _Pragma directive, slice it up, process it, then
119/// return the first token after the directive. The _Pragma token has just
120/// been read into 'Tok'.
Chris Lattnerd2177732007-07-20 16:59:19 +0000121void Preprocessor::Handle_Pragma(Token &Tok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000122 // Remember the pragma token location.
123 SourceLocation PragmaLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000124
Reid Spencer5f016e22007-07-11 17:01:13 +0000125 // Read the '('.
126 Lex(Tok);
Chris Lattner3692b092008-11-18 07:59:24 +0000127 if (Tok.isNot(tok::l_paren)) {
128 Diag(PragmaLoc, diag::err__Pragma_malformed);
129 return;
130 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000131
132 // Read the '"..."'.
133 Lex(Tok);
Chris Lattner3692b092008-11-18 07:59:24 +0000134 if (Tok.isNot(tok::string_literal) && Tok.isNot(tok::wide_string_literal)) {
135 Diag(PragmaLoc, diag::err__Pragma_malformed);
136 return;
137 }
Mike Stump1eb44332009-09-09 15:08:12 +0000138
Reid Spencer5f016e22007-07-11 17:01:13 +0000139 // Remember the string.
140 std::string StrVal = getSpelling(Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000141
142 // Read the ')'.
143 Lex(Tok);
Chris Lattner3692b092008-11-18 07:59:24 +0000144 if (Tok.isNot(tok::r_paren)) {
145 Diag(PragmaLoc, diag::err__Pragma_malformed);
146 return;
147 }
Mike Stump1eb44332009-09-09 15:08:12 +0000148
Chris Lattnere7fb4842009-02-15 20:52:18 +0000149 SourceLocation RParenLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000150
Chris Lattnera9d91452009-01-16 18:59:23 +0000151 // The _Pragma is lexically sound. Destringize according to C99 6.10.9.1:
152 // "The string literal is destringized by deleting the L prefix, if present,
153 // deleting the leading and trailing double-quotes, replacing each escape
154 // sequence \" by a double-quote, and replacing each escape sequence \\ by a
155 // single backslash."
Reid Spencer5f016e22007-07-11 17:01:13 +0000156 if (StrVal[0] == 'L') // Remove L prefix.
157 StrVal.erase(StrVal.begin());
158 assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
159 "Invalid string token!");
Mike Stump1eb44332009-09-09 15:08:12 +0000160
Reid Spencer5f016e22007-07-11 17:01:13 +0000161 // Remove the front quote, replacing it with a space, so that the pragma
162 // contents appear to have a space before them.
163 StrVal[0] = ' ';
Mike Stump1eb44332009-09-09 15:08:12 +0000164
Chris Lattner1fa49532009-03-08 08:08:45 +0000165 // Replace the terminating quote with a \n.
Reid Spencer5f016e22007-07-11 17:01:13 +0000166 StrVal[StrVal.size()-1] = '\n';
Mike Stump1eb44332009-09-09 15:08:12 +0000167
Reid Spencer5f016e22007-07-11 17:01:13 +0000168 // Remove escaped quotes and escapes.
169 for (unsigned i = 0, e = StrVal.size(); i != e-1; ++i) {
170 if (StrVal[i] == '\\' &&
171 (StrVal[i+1] == '\\' || StrVal[i+1] == '"')) {
172 // \\ -> '\' and \" -> '"'.
173 StrVal.erase(StrVal.begin()+i);
174 --e;
175 }
176 }
John McCall1ef8a2e2010-08-28 22:34:47 +0000177
Peter Collingbournea5ef5842011-02-22 13:49:06 +0000178 // Plop the string (including the newline and trailing null) into a buffer
179 // where we can lex it.
180 Token TmpTok;
181 TmpTok.startToken();
182 CreateString(&StrVal[0], StrVal.size(), TmpTok);
183 SourceLocation TokLoc = TmpTok.getLocation();
184
185 // Make and enter a lexer object so that we lex and expand the tokens just
186 // like any others.
187 Lexer *TL = Lexer::Create_PragmaLexer(TokLoc, PragmaLoc, RParenLoc,
188 StrVal.size(), *this);
189
190 EnterSourceFileWithLexer(TL, 0);
191
192 // With everything set up, lex this as a #pragma directive.
193 HandlePragmaDirective(PIK__Pragma);
John McCall1ef8a2e2010-08-28 22:34:47 +0000194
195 // Finally, return whatever came after the pragma directive.
196 return Lex(Tok);
197}
198
199/// HandleMicrosoft__pragma - Like Handle_Pragma except the pragma text
200/// is not enclosed within a string literal.
201void Preprocessor::HandleMicrosoft__pragma(Token &Tok) {
202 // Remember the pragma token location.
203 SourceLocation PragmaLoc = Tok.getLocation();
204
205 // Read the '('.
206 Lex(Tok);
207 if (Tok.isNot(tok::l_paren)) {
208 Diag(PragmaLoc, diag::err__Pragma_malformed);
209 return;
210 }
211
Peter Collingbournea5ef5842011-02-22 13:49:06 +0000212 // Get the tokens enclosed within the __pragma(), as well as the final ')'.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000213 SmallVector<Token, 32> PragmaToks;
John McCall1ef8a2e2010-08-28 22:34:47 +0000214 int NumParens = 0;
215 Lex(Tok);
216 while (Tok.isNot(tok::eof)) {
Peter Collingbournea5ef5842011-02-22 13:49:06 +0000217 PragmaToks.push_back(Tok);
John McCall1ef8a2e2010-08-28 22:34:47 +0000218 if (Tok.is(tok::l_paren))
219 NumParens++;
220 else if (Tok.is(tok::r_paren) && NumParens-- == 0)
221 break;
John McCall1ef8a2e2010-08-28 22:34:47 +0000222 Lex(Tok);
223 }
224
John McCall3da92a92010-08-29 01:09:54 +0000225 if (Tok.is(tok::eof)) {
226 Diag(PragmaLoc, diag::err_unterminated___pragma);
227 return;
228 }
229
Peter Collingbournea5ef5842011-02-22 13:49:06 +0000230 PragmaToks.front().setFlag(Token::LeadingSpace);
John McCall1ef8a2e2010-08-28 22:34:47 +0000231
Peter Collingbourne84021552011-02-28 02:37:51 +0000232 // Replace the ')' with an EOD to mark the end of the pragma.
233 PragmaToks.back().setKind(tok::eod);
Peter Collingbournea5ef5842011-02-22 13:49:06 +0000234
235 Token *TokArray = new Token[PragmaToks.size()];
236 std::copy(PragmaToks.begin(), PragmaToks.end(), TokArray);
237
238 // Push the tokens onto the stack.
239 EnterTokenStream(TokArray, PragmaToks.size(), true, true);
240
241 // With everything set up, lex this as a #pragma directive.
242 HandlePragmaDirective(PIK___pragma);
John McCall1ef8a2e2010-08-28 22:34:47 +0000243
244 // Finally, return whatever came after the pragma directive.
245 return Lex(Tok);
246}
247
Reid Spencer5f016e22007-07-11 17:01:13 +0000248/// HandlePragmaOnce - Handle #pragma once. OnceTok is the 'once'.
249///
Chris Lattnerd2177732007-07-20 16:59:19 +0000250void Preprocessor::HandlePragmaOnce(Token &OnceTok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000251 if (isInPrimaryFile()) {
252 Diag(OnceTok, diag::pp_pragma_once_in_main_file);
253 return;
254 }
Mike Stump1eb44332009-09-09 15:08:12 +0000255
Reid Spencer5f016e22007-07-11 17:01:13 +0000256 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
Reid Spencer5f016e22007-07-11 17:01:13 +0000257 // Mark the file as a once-only file now.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000258 HeaderInfo.MarkFileIncludeOnce(getCurrentFileLexer()->getFileEntry());
Reid Spencer5f016e22007-07-11 17:01:13 +0000259}
260
Chris Lattner22434492007-12-19 19:38:36 +0000261void Preprocessor::HandlePragmaMark() {
Ted Kremenek17ff58a2008-11-19 22:21:33 +0000262 assert(CurPPLexer && "No current lexer?");
Chris Lattner6896a372009-06-15 05:02:34 +0000263 if (CurLexer)
264 CurLexer->ReadToEndOfLine();
265 else
266 CurPTHLexer->DiscardToEndOfLine();
Chris Lattner22434492007-12-19 19:38:36 +0000267}
268
269
Reid Spencer5f016e22007-07-11 17:01:13 +0000270/// HandlePragmaPoison - Handle #pragma GCC poison. PoisonTok is the 'poison'.
271///
Chris Lattnerd2177732007-07-20 16:59:19 +0000272void Preprocessor::HandlePragmaPoison(Token &PoisonTok) {
273 Token Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000274
275 while (1) {
276 // Read the next token to poison. While doing this, pretend that we are
277 // skipping while reading the identifier to poison.
278 // This avoids errors on code like:
279 // #pragma GCC poison X
280 // #pragma GCC poison X
Ted Kremenek68a91d52008-11-18 01:12:54 +0000281 if (CurPPLexer) CurPPLexer->LexingRawMode = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000282 LexUnexpandedToken(Tok);
Ted Kremenek68a91d52008-11-18 01:12:54 +0000283 if (CurPPLexer) CurPPLexer->LexingRawMode = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000284
Reid Spencer5f016e22007-07-11 17:01:13 +0000285 // If we reached the end of line, we're done.
Peter Collingbourne84021552011-02-28 02:37:51 +0000286 if (Tok.is(tok::eod)) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000287
Reid Spencer5f016e22007-07-11 17:01:13 +0000288 // Can only poison identifiers.
Abramo Bagnarac4bf2b92010-12-22 08:23:18 +0000289 if (Tok.isNot(tok::raw_identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000290 Diag(Tok, diag::err_pp_invalid_poison);
291 return;
292 }
Mike Stump1eb44332009-09-09 15:08:12 +0000293
Reid Spencer5f016e22007-07-11 17:01:13 +0000294 // Look up the identifier info for the token. We disabled identifier lookup
295 // by saying we're skipping contents, so we need to do this manually.
296 IdentifierInfo *II = LookUpIdentifierInfo(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000297
Reid Spencer5f016e22007-07-11 17:01:13 +0000298 // Already poisoned.
299 if (II->isPoisoned()) continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000300
Reid Spencer5f016e22007-07-11 17:01:13 +0000301 // If this is a macro identifier, emit a warning.
Chris Lattner0edde552007-10-07 08:04:56 +0000302 if (II->hasMacroDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000303 Diag(Tok, diag::pp_poisoning_existing_macro);
Mike Stump1eb44332009-09-09 15:08:12 +0000304
Reid Spencer5f016e22007-07-11 17:01:13 +0000305 // Finally, poison it!
306 II->setIsPoisoned();
307 }
308}
309
310/// HandlePragmaSystemHeader - Implement #pragma GCC system_header. We know
311/// that the whole directive has been parsed.
Chris Lattnerd2177732007-07-20 16:59:19 +0000312void Preprocessor::HandlePragmaSystemHeader(Token &SysHeaderTok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000313 if (isInPrimaryFile()) {
314 Diag(SysHeaderTok, diag::pp_pragma_sysheader_in_main_file);
315 return;
316 }
Mike Stump1eb44332009-09-09 15:08:12 +0000317
Reid Spencer5f016e22007-07-11 17:01:13 +0000318 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
Ted Kremenek35c10c22008-11-20 01:45:11 +0000319 PreprocessorLexer *TheLexer = getCurrentFileLexer();
Mike Stump1eb44332009-09-09 15:08:12 +0000320
Reid Spencer5f016e22007-07-11 17:01:13 +0000321 // Mark the file as a system header.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000322 HeaderInfo.MarkFileSystemHeader(TheLexer->getFileEntry());
Mike Stump1eb44332009-09-09 15:08:12 +0000323
324
Chris Lattner6896a372009-06-15 05:02:34 +0000325 PresumedLoc PLoc = SourceMgr.getPresumedLoc(SysHeaderTok.getLocation());
Douglas Gregorcb7b1e12010-11-12 07:15:47 +0000326 if (PLoc.isInvalid())
327 return;
328
Jay Foad65aa6882011-06-21 15:13:30 +0000329 unsigned FilenameID = SourceMgr.getLineTableFilenameID(PLoc.getFilename());
Mike Stump1eb44332009-09-09 15:08:12 +0000330
Chris Lattner784c2572011-05-22 22:10:16 +0000331 // Notify the client, if desired, that we are in a new source file.
332 if (Callbacks)
333 Callbacks->FileChanged(SysHeaderTok.getLocation(),
334 PPCallbacks::SystemHeaderPragma, SrcMgr::C_System);
335
Chris Lattner6896a372009-06-15 05:02:34 +0000336 // Emit a line marker. This will change any source locations from this point
337 // forward to realize they are in a system header.
338 // Create a line note with this information.
339 SourceMgr.AddLineNote(SysHeaderTok.getLocation(), PLoc.getLine(), FilenameID,
340 false, false, true, false);
Reid Spencer5f016e22007-07-11 17:01:13 +0000341}
342
343/// HandlePragmaDependency - Handle #pragma GCC dependency "foo" blah.
344///
Chris Lattnerd2177732007-07-20 16:59:19 +0000345void Preprocessor::HandlePragmaDependency(Token &DependencyTok) {
346 Token FilenameTok;
Ted Kremenek68a91d52008-11-18 01:12:54 +0000347 CurPPLexer->LexIncludeFilename(FilenameTok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000348
Peter Collingbourne84021552011-02-28 02:37:51 +0000349 // If the token kind is EOD, the error has already been diagnosed.
350 if (FilenameTok.is(tok::eod))
Reid Spencer5f016e22007-07-11 17:01:13 +0000351 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000352
Reid Spencer5f016e22007-07-11 17:01:13 +0000353 // Reserve a buffer to get the spelling.
Chris Lattnera1394812010-01-10 01:35:12 +0000354 llvm::SmallString<128> FilenameBuffer;
Douglas Gregor453091c2010-03-16 22:30:13 +0000355 bool Invalid = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000356 StringRef Filename = getSpelling(FilenameTok, FilenameBuffer, &Invalid);
Douglas Gregor453091c2010-03-16 22:30:13 +0000357 if (Invalid)
358 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000359
Chris Lattnera1394812010-01-10 01:35:12 +0000360 bool isAngled =
361 GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
Reid Spencer5f016e22007-07-11 17:01:13 +0000362 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
363 // error.
Chris Lattnera1394812010-01-10 01:35:12 +0000364 if (Filename.empty())
Reid Spencer5f016e22007-07-11 17:01:13 +0000365 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000366
Reid Spencer5f016e22007-07-11 17:01:13 +0000367 // Search include directories for this file.
368 const DirectoryLookup *CurDir;
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000369 const FileEntry *File = LookupFile(Filename, isAngled, 0, CurDir, NULL, NULL,
370 NULL);
Chris Lattner56b05c82008-11-18 08:02:48 +0000371 if (File == 0) {
Eli Friedmanf84139a2011-08-30 23:07:51 +0000372 if (!SuppressIncludeNotFoundError)
373 Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
Chris Lattner56b05c82008-11-18 08:02:48 +0000374 return;
375 }
Mike Stump1eb44332009-09-09 15:08:12 +0000376
Chris Lattner2b2453a2009-01-17 06:22:33 +0000377 const FileEntry *CurFile = getCurrentFileLexer()->getFileEntry();
Reid Spencer5f016e22007-07-11 17:01:13 +0000378
379 // If this file is older than the file it depends on, emit a diagnostic.
380 if (CurFile && CurFile->getModificationTime() < File->getModificationTime()) {
381 // Lex tokens at the end of the message and include them in the message.
382 std::string Message;
383 Lex(DependencyTok);
Peter Collingbourne84021552011-02-28 02:37:51 +0000384 while (DependencyTok.isNot(tok::eod)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000385 Message += getSpelling(DependencyTok) + " ";
386 Lex(DependencyTok);
387 }
Mike Stump1eb44332009-09-09 15:08:12 +0000388
Chris Lattner96de2592010-09-05 23:16:09 +0000389 // Remove the trailing ' ' if present.
390 if (!Message.empty())
391 Message.erase(Message.end()-1);
Chris Lattner56b05c82008-11-18 08:02:48 +0000392 Diag(FilenameTok, diag::pp_out_of_date_dependency) << Message;
Reid Spencer5f016e22007-07-11 17:01:13 +0000393 }
394}
395
Chris Lattner636c5ef2009-01-16 08:21:25 +0000396/// HandlePragmaComment - Handle the microsoft #pragma comment extension. The
397/// syntax is:
398/// #pragma comment(linker, "foo")
399/// 'linker' is one of five identifiers: compiler, exestr, lib, linker, user.
400/// "foo" is a string, which is fully macro expanded, and permits string
Gabor Greifd7ee3492009-03-17 11:39:38 +0000401/// concatenation, embedded escape characters etc. See MSDN for more details.
Chris Lattner636c5ef2009-01-16 08:21:25 +0000402void Preprocessor::HandlePragmaComment(Token &Tok) {
403 SourceLocation CommentLoc = Tok.getLocation();
404 Lex(Tok);
405 if (Tok.isNot(tok::l_paren)) {
406 Diag(CommentLoc, diag::err_pragma_comment_malformed);
407 return;
408 }
Mike Stump1eb44332009-09-09 15:08:12 +0000409
Chris Lattner636c5ef2009-01-16 08:21:25 +0000410 // Read the identifier.
411 Lex(Tok);
412 if (Tok.isNot(tok::identifier)) {
413 Diag(CommentLoc, diag::err_pragma_comment_malformed);
414 return;
415 }
Mike Stump1eb44332009-09-09 15:08:12 +0000416
Chris Lattner636c5ef2009-01-16 08:21:25 +0000417 // Verify that this is one of the 5 whitelisted options.
418 // FIXME: warn that 'exestr' is deprecated.
419 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000420 if (!II->isStr("compiler") && !II->isStr("exestr") && !II->isStr("lib") &&
Chris Lattner636c5ef2009-01-16 08:21:25 +0000421 !II->isStr("linker") && !II->isStr("user")) {
422 Diag(Tok.getLocation(), diag::err_pragma_comment_unknown_kind);
423 return;
424 }
Mike Stump1eb44332009-09-09 15:08:12 +0000425
Chris Lattnera9d91452009-01-16 18:59:23 +0000426 // Read the optional string if present.
Chris Lattner636c5ef2009-01-16 08:21:25 +0000427 Lex(Tok);
Chris Lattnera9d91452009-01-16 18:59:23 +0000428 std::string ArgumentString;
Chris Lattner636c5ef2009-01-16 08:21:25 +0000429 if (Tok.is(tok::comma)) {
Chris Lattnera9d91452009-01-16 18:59:23 +0000430 Lex(Tok); // eat the comma.
Chris Lattner636c5ef2009-01-16 08:21:25 +0000431
432 // We need at least one string.
Chris Lattneredaf8772009-04-19 23:16:58 +0000433 if (Tok.isNot(tok::string_literal)) {
Chris Lattner636c5ef2009-01-16 08:21:25 +0000434 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
435 return;
436 }
437
438 // String concatenation allows multiple strings, which can even come from
439 // macro expansion.
440 // "foo " "bar" "Baz"
Chris Lattner5f9e2722011-07-23 10:55:15 +0000441 SmallVector<Token, 4> StrToks;
Chris Lattneredaf8772009-04-19 23:16:58 +0000442 while (Tok.is(tok::string_literal)) {
Chris Lattnera9d91452009-01-16 18:59:23 +0000443 StrToks.push_back(Tok);
Chris Lattner636c5ef2009-01-16 08:21:25 +0000444 Lex(Tok);
Chris Lattnera9d91452009-01-16 18:59:23 +0000445 }
446
447 // Concatenate and parse the strings.
448 StringLiteralParser Literal(&StrToks[0], StrToks.size(), *this);
Douglas Gregor5cee1192011-07-27 05:40:30 +0000449 assert(Literal.isAscii() && "Didn't allow wide strings in");
Chris Lattnera9d91452009-01-16 18:59:23 +0000450 if (Literal.hadError)
451 return;
452 if (Literal.Pascal) {
453 Diag(StrToks[0].getLocation(), diag::err_pragma_comment_malformed);
454 return;
455 }
456
Jay Foad65aa6882011-06-21 15:13:30 +0000457 ArgumentString = Literal.GetString();
Chris Lattner636c5ef2009-01-16 08:21:25 +0000458 }
Mike Stump1eb44332009-09-09 15:08:12 +0000459
Chris Lattnera9d91452009-01-16 18:59:23 +0000460 // FIXME: If the kind is "compiler" warn if the string is present (it is
461 // ignored).
462 // FIXME: 'lib' requires a comment string.
463 // FIXME: 'linker' requires a comment string, and has a specific list of
464 // things that are allowable.
Mike Stump1eb44332009-09-09 15:08:12 +0000465
Chris Lattner636c5ef2009-01-16 08:21:25 +0000466 if (Tok.isNot(tok::r_paren)) {
467 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
468 return;
469 }
Chris Lattnera9d91452009-01-16 18:59:23 +0000470 Lex(Tok); // eat the r_paren.
Chris Lattner636c5ef2009-01-16 08:21:25 +0000471
Peter Collingbourne84021552011-02-28 02:37:51 +0000472 if (Tok.isNot(tok::eod)) {
Chris Lattner636c5ef2009-01-16 08:21:25 +0000473 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
474 return;
475 }
Mike Stump1eb44332009-09-09 15:08:12 +0000476
Chris Lattnera9d91452009-01-16 18:59:23 +0000477 // If the pragma is lexically sound, notify any interested PPCallbacks.
Chris Lattner172e3362009-01-16 19:01:46 +0000478 if (Callbacks)
479 Callbacks->PragmaComment(CommentLoc, II, ArgumentString);
Chris Lattner636c5ef2009-01-16 08:21:25 +0000480}
481
Michael J. Spencer301669b2010-09-27 06:19:02 +0000482/// HandlePragmaMessage - Handle the microsoft and gcc #pragma message
483/// extension. The syntax is:
484/// #pragma message(string)
485/// OR, in GCC mode:
486/// #pragma message string
487/// string is a string, which is fully macro expanded, and permits string
488/// concatenation, embedded escape characters, etc... See MSDN for more details.
Chris Lattnerabfe0942010-06-26 17:11:39 +0000489void Preprocessor::HandlePragmaMessage(Token &Tok) {
490 SourceLocation MessageLoc = Tok.getLocation();
491 Lex(Tok);
Michael J. Spencer301669b2010-09-27 06:19:02 +0000492 bool ExpectClosingParen = false;
Michael J. Spencerd83fc542010-09-27 06:34:47 +0000493 switch (Tok.getKind()) {
Michael J. Spencer301669b2010-09-27 06:19:02 +0000494 case tok::l_paren:
495 // We have a MSVC style pragma message.
496 ExpectClosingParen = true;
497 // Read the string.
498 Lex(Tok);
499 break;
500 case tok::string_literal:
501 // We have a GCC style pragma message, and we just read the string.
502 break;
503 default:
Chris Lattnerabfe0942010-06-26 17:11:39 +0000504 Diag(MessageLoc, diag::err_pragma_message_malformed);
505 return;
506 }
Chris Lattner636c5ef2009-01-16 08:21:25 +0000507
Chris Lattnerabfe0942010-06-26 17:11:39 +0000508 // We need at least one string.
509 if (Tok.isNot(tok::string_literal)) {
510 Diag(Tok.getLocation(), diag::err_pragma_message_malformed);
511 return;
512 }
513
514 // String concatenation allows multiple strings, which can even come from
515 // macro expansion.
516 // "foo " "bar" "Baz"
Chris Lattner5f9e2722011-07-23 10:55:15 +0000517 SmallVector<Token, 4> StrToks;
Chris Lattnerabfe0942010-06-26 17:11:39 +0000518 while (Tok.is(tok::string_literal)) {
519 StrToks.push_back(Tok);
520 Lex(Tok);
521 }
522
523 // Concatenate and parse the strings.
524 StringLiteralParser Literal(&StrToks[0], StrToks.size(), *this);
Douglas Gregor5cee1192011-07-27 05:40:30 +0000525 assert(Literal.isAscii() && "Didn't allow wide strings in");
Chris Lattnerabfe0942010-06-26 17:11:39 +0000526 if (Literal.hadError)
527 return;
528 if (Literal.Pascal) {
529 Diag(StrToks[0].getLocation(), diag::err_pragma_message_malformed);
530 return;
531 }
532
Chris Lattner5f9e2722011-07-23 10:55:15 +0000533 StringRef MessageString(Literal.GetString());
Chris Lattnerabfe0942010-06-26 17:11:39 +0000534
Michael J. Spencer301669b2010-09-27 06:19:02 +0000535 if (ExpectClosingParen) {
536 if (Tok.isNot(tok::r_paren)) {
537 Diag(Tok.getLocation(), diag::err_pragma_message_malformed);
538 return;
539 }
540 Lex(Tok); // eat the r_paren.
Chris Lattnerabfe0942010-06-26 17:11:39 +0000541 }
Chris Lattnerabfe0942010-06-26 17:11:39 +0000542
Peter Collingbourne84021552011-02-28 02:37:51 +0000543 if (Tok.isNot(tok::eod)) {
Chris Lattnerabfe0942010-06-26 17:11:39 +0000544 Diag(Tok.getLocation(), diag::err_pragma_message_malformed);
545 return;
546 }
547
548 // Output the message.
549 Diag(MessageLoc, diag::warn_pragma_message) << MessageString;
550
551 // If the pragma is lexically sound, notify any interested PPCallbacks.
552 if (Callbacks)
553 Callbacks->PragmaMessage(MessageLoc, MessageString);
554}
Chris Lattner636c5ef2009-01-16 08:21:25 +0000555
Chris Lattnerf47724b2010-08-17 15:55:45 +0000556/// ParsePragmaPushOrPopMacro - Handle parsing of pragma push_macro/pop_macro.
557/// Return the IdentifierInfo* associated with the macro to push or pop.
558IdentifierInfo *Preprocessor::ParsePragmaPushOrPopMacro(Token &Tok) {
559 // Remember the pragma token location.
560 Token PragmaTok = Tok;
561
562 // Read the '('.
563 Lex(Tok);
564 if (Tok.isNot(tok::l_paren)) {
565 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
566 << getSpelling(PragmaTok);
567 return 0;
568 }
569
570 // Read the macro name string.
571 Lex(Tok);
572 if (Tok.isNot(tok::string_literal)) {
573 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
574 << getSpelling(PragmaTok);
575 return 0;
576 }
577
578 // Remember the macro string.
579 std::string StrVal = getSpelling(Tok);
580
581 // Read the ')'.
582 Lex(Tok);
583 if (Tok.isNot(tok::r_paren)) {
584 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
585 << getSpelling(PragmaTok);
586 return 0;
587 }
588
589 assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
590 "Invalid string token!");
591
592 // Create a Token from the string.
593 Token MacroTok;
594 MacroTok.startToken();
Abramo Bagnarac4bf2b92010-12-22 08:23:18 +0000595 MacroTok.setKind(tok::raw_identifier);
Chris Lattnerf47724b2010-08-17 15:55:45 +0000596 CreateString(&StrVal[1], StrVal.size() - 2, MacroTok);
597
598 // Get the IdentifierInfo of MacroToPushTok.
599 return LookUpIdentifierInfo(MacroTok);
600}
601
602/// HandlePragmaPushMacro - Handle #pragma push_macro.
603/// The syntax is:
604/// #pragma push_macro("macro")
605void Preprocessor::HandlePragmaPushMacro(Token &PushMacroTok) {
606 // Parse the pragma directive and get the macro IdentifierInfo*.
607 IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PushMacroTok);
608 if (!IdentInfo) return;
609
610 // Get the MacroInfo associated with IdentInfo.
611 MacroInfo *MI = getMacroInfo(IdentInfo);
612
613 MacroInfo *MacroCopyToPush = 0;
614 if (MI) {
615 // Make a clone of MI.
616 MacroCopyToPush = CloneMacroInfo(*MI);
617
618 // Allow the original MacroInfo to be redefined later.
619 MI->setIsAllowRedefinitionsWithoutWarning(true);
620 }
621
622 // Push the cloned MacroInfo so we can retrieve it later.
623 PragmaPushMacroInfo[IdentInfo].push_back(MacroCopyToPush);
624}
625
Ted Kremenekb275e3d2010-10-19 17:40:50 +0000626/// HandlePragmaPopMacro - Handle #pragma pop_macro.
Chris Lattnerf47724b2010-08-17 15:55:45 +0000627/// The syntax is:
628/// #pragma pop_macro("macro")
629void Preprocessor::HandlePragmaPopMacro(Token &PopMacroTok) {
630 SourceLocation MessageLoc = PopMacroTok.getLocation();
631
632 // Parse the pragma directive and get the macro IdentifierInfo*.
633 IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PopMacroTok);
634 if (!IdentInfo) return;
635
636 // Find the vector<MacroInfo*> associated with the macro.
637 llvm::DenseMap<IdentifierInfo*, std::vector<MacroInfo*> >::iterator iter =
638 PragmaPushMacroInfo.find(IdentInfo);
639 if (iter != PragmaPushMacroInfo.end()) {
640 // Release the MacroInfo currently associated with IdentInfo.
641 MacroInfo *CurrentMI = getMacroInfo(IdentInfo);
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000642 if (CurrentMI) {
643 if (CurrentMI->isWarnIfUnused())
644 WarnUnusedMacroLocs.erase(CurrentMI->getDefinitionLoc());
645 ReleaseMacroInfo(CurrentMI);
646 }
Chris Lattnerf47724b2010-08-17 15:55:45 +0000647
648 // Get the MacroInfo we want to reinstall.
649 MacroInfo *MacroToReInstall = iter->second.back();
650
651 // Reinstall the previously pushed macro.
652 setMacroInfo(IdentInfo, MacroToReInstall);
653
654 // Pop PragmaPushMacroInfo stack.
655 iter->second.pop_back();
656 if (iter->second.size() == 0)
657 PragmaPushMacroInfo.erase(iter);
658 } else {
659 Diag(MessageLoc, diag::warn_pragma_pop_macro_no_push)
660 << IdentInfo->getName();
661 }
662}
Reid Spencer5f016e22007-07-11 17:01:13 +0000663
664/// AddPragmaHandler - Add the specified pragma handler to the preprocessor.
665/// If 'Namespace' is non-null, then it is a token required to exist on the
666/// pragma line before the pragma string starts, e.g. "STDC" or "GCC".
Chris Lattner5f9e2722011-07-23 10:55:15 +0000667void Preprocessor::AddPragmaHandler(StringRef Namespace,
Reid Spencer5f016e22007-07-11 17:01:13 +0000668 PragmaHandler *Handler) {
669 PragmaNamespace *InsertNS = PragmaHandlers;
Mike Stump1eb44332009-09-09 15:08:12 +0000670
Reid Spencer5f016e22007-07-11 17:01:13 +0000671 // If this is specified to be in a namespace, step down into it.
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000672 if (!Namespace.empty()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000673 // If there is already a pragma handler with the name of this namespace,
674 // we either have an error (directive with the same name as a namespace) or
675 // we already have the namespace to insert into.
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000676 if (PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000677 InsertNS = Existing->getIfNamespace();
678 assert(InsertNS != 0 && "Cannot have a pragma namespace and pragma"
679 " handler with the same name!");
680 } else {
681 // Otherwise, this namespace doesn't exist yet, create and insert the
682 // handler for it.
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000683 InsertNS = new PragmaNamespace(Namespace);
Reid Spencer5f016e22007-07-11 17:01:13 +0000684 PragmaHandlers->AddPragma(InsertNS);
685 }
686 }
Mike Stump1eb44332009-09-09 15:08:12 +0000687
Reid Spencer5f016e22007-07-11 17:01:13 +0000688 // Check to make sure we don't already have a pragma for this identifier.
689 assert(!InsertNS->FindHandler(Handler->getName()) &&
690 "Pragma handler already exists for this identifier!");
691 InsertNS->AddPragma(Handler);
692}
693
Daniel Dunbar40950802008-10-04 19:17:46 +0000694/// RemovePragmaHandler - Remove the specific pragma handler from the
695/// preprocessor. If \arg Namespace is non-null, then it should be the
696/// namespace that \arg Handler was added to. It is an error to remove
697/// a handler that has not been registered.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000698void Preprocessor::RemovePragmaHandler(StringRef Namespace,
Daniel Dunbar40950802008-10-04 19:17:46 +0000699 PragmaHandler *Handler) {
700 PragmaNamespace *NS = PragmaHandlers;
Mike Stump1eb44332009-09-09 15:08:12 +0000701
Daniel Dunbar40950802008-10-04 19:17:46 +0000702 // If this is specified to be in a namespace, step down into it.
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000703 if (!Namespace.empty()) {
704 PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace);
Daniel Dunbar40950802008-10-04 19:17:46 +0000705 assert(Existing && "Namespace containing handler does not exist!");
706
707 NS = Existing->getIfNamespace();
708 assert(NS && "Invalid namespace, registered as a regular pragma handler!");
709 }
710
711 NS->RemovePragmaHandler(Handler);
Mike Stump1eb44332009-09-09 15:08:12 +0000712
Daniel Dunbar40950802008-10-04 19:17:46 +0000713 // If this is a non-default namespace and it is now empty, remove
714 // it.
715 if (NS != PragmaHandlers && NS->IsEmpty())
716 PragmaHandlers->RemovePragmaHandler(NS);
717}
718
Peter Collingbourne9d3f5f72011-02-14 01:42:24 +0000719bool Preprocessor::LexOnOffSwitch(tok::OnOffSwitch &Result) {
720 Token Tok;
721 LexUnexpandedToken(Tok);
722
723 if (Tok.isNot(tok::identifier)) {
724 Diag(Tok, diag::ext_on_off_switch_syntax);
725 return true;
726 }
727 IdentifierInfo *II = Tok.getIdentifierInfo();
728 if (II->isStr("ON"))
729 Result = tok::OOS_ON;
730 else if (II->isStr("OFF"))
731 Result = tok::OOS_OFF;
732 else if (II->isStr("DEFAULT"))
733 Result = tok::OOS_DEFAULT;
734 else {
735 Diag(Tok, diag::ext_on_off_switch_syntax);
736 return true;
737 }
738
Peter Collingbourne84021552011-02-28 02:37:51 +0000739 // Verify that this is followed by EOD.
Peter Collingbourne9d3f5f72011-02-14 01:42:24 +0000740 LexUnexpandedToken(Tok);
Peter Collingbourne84021552011-02-28 02:37:51 +0000741 if (Tok.isNot(tok::eod))
742 Diag(Tok, diag::ext_pragma_syntax_eod);
Peter Collingbourne9d3f5f72011-02-14 01:42:24 +0000743 return false;
744}
745
Reid Spencer5f016e22007-07-11 17:01:13 +0000746namespace {
Chris Lattner22434492007-12-19 19:38:36 +0000747/// PragmaOnceHandler - "#pragma once" marks the file as atomically included.
Reid Spencer5f016e22007-07-11 17:01:13 +0000748struct PragmaOnceHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000749 PragmaOnceHandler() : PragmaHandler("once") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000750 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
751 Token &OnceTok) {
Chris Lattner35410d52009-04-14 05:07:49 +0000752 PP.CheckEndOfDirective("pragma once");
Reid Spencer5f016e22007-07-11 17:01:13 +0000753 PP.HandlePragmaOnce(OnceTok);
754 }
755};
756
Chris Lattner22434492007-12-19 19:38:36 +0000757/// PragmaMarkHandler - "#pragma mark ..." is ignored by the compiler, and the
758/// rest of the line is not lexed.
759struct PragmaMarkHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000760 PragmaMarkHandler() : PragmaHandler("mark") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000761 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
762 Token &MarkTok) {
Chris Lattner22434492007-12-19 19:38:36 +0000763 PP.HandlePragmaMark();
764 }
765};
766
767/// PragmaPoisonHandler - "#pragma poison x" marks x as not usable.
Reid Spencer5f016e22007-07-11 17:01:13 +0000768struct PragmaPoisonHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000769 PragmaPoisonHandler() : PragmaHandler("poison") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000770 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
771 Token &PoisonTok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000772 PP.HandlePragmaPoison(PoisonTok);
773 }
774};
775
Chris Lattner22434492007-12-19 19:38:36 +0000776/// PragmaSystemHeaderHandler - "#pragma system_header" marks the current file
777/// as a system header, which silences warnings in it.
Reid Spencer5f016e22007-07-11 17:01:13 +0000778struct PragmaSystemHeaderHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000779 PragmaSystemHeaderHandler() : PragmaHandler("system_header") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000780 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
781 Token &SHToken) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000782 PP.HandlePragmaSystemHeader(SHToken);
Chris Lattner35410d52009-04-14 05:07:49 +0000783 PP.CheckEndOfDirective("pragma");
Reid Spencer5f016e22007-07-11 17:01:13 +0000784 }
785};
786struct PragmaDependencyHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000787 PragmaDependencyHandler() : PragmaHandler("dependency") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000788 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
789 Token &DepToken) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000790 PP.HandlePragmaDependency(DepToken);
791 }
792};
Mike Stump1eb44332009-09-09 15:08:12 +0000793
Daniel Dunbarabf7b722010-07-28 15:40:33 +0000794struct PragmaDebugHandler : public PragmaHandler {
795 PragmaDebugHandler() : PragmaHandler("__debug") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000796 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
797 Token &DepToken) {
Daniel Dunbarabf7b722010-07-28 15:40:33 +0000798 Token Tok;
799 PP.LexUnexpandedToken(Tok);
800 if (Tok.isNot(tok::identifier)) {
Douglas Gregor6493a4d2010-08-30 15:15:34 +0000801 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Daniel Dunbarabf7b722010-07-28 15:40:33 +0000802 return;
803 }
804 IdentifierInfo *II = Tok.getIdentifierInfo();
805
Daniel Dunbar55054132010-08-17 22:32:48 +0000806 if (II->isStr("assert")) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000807 llvm_unreachable("This is an assertion!");
Daniel Dunbarabf7b722010-07-28 15:40:33 +0000808 } else if (II->isStr("crash")) {
Daniel Dunbar55054132010-08-17 22:32:48 +0000809 *(volatile int*) 0x11 = 0;
810 } else if (II->isStr("llvm_fatal_error")) {
811 llvm::report_fatal_error("#pragma clang __debug llvm_fatal_error");
812 } else if (II->isStr("llvm_unreachable")) {
813 llvm_unreachable("#pragma clang __debug llvm_unreachable");
814 } else if (II->isStr("overflow_stack")) {
815 DebugOverflowStack();
Daniel Dunbarff759a62010-08-18 23:09:23 +0000816 } else if (II->isStr("handle_crash")) {
817 llvm::CrashRecoveryContext *CRC =llvm::CrashRecoveryContext::GetCurrent();
818 if (CRC)
819 CRC->HandleCrash();
Daniel Dunbar55054132010-08-17 22:32:48 +0000820 } else {
821 PP.Diag(Tok, diag::warn_pragma_debug_unexpected_command)
822 << II->getName();
Daniel Dunbarabf7b722010-07-28 15:40:33 +0000823 }
824 }
825
Francois Pichet1066c6c2011-05-25 16:15:03 +0000826// Disable MSVC warning about runtime stack overflow.
827#ifdef _MSC_VER
828 #pragma warning(disable : 4717)
829#endif
Daniel Dunbarabf7b722010-07-28 15:40:33 +0000830 void DebugOverflowStack() {
831 DebugOverflowStack();
832 }
Francois Pichet1066c6c2011-05-25 16:15:03 +0000833#ifdef _MSC_VER
834 #pragma warning(default : 4717)
835#endif
836
Daniel Dunbarabf7b722010-07-28 15:40:33 +0000837};
838
Chris Lattneredaf8772009-04-19 23:16:58 +0000839/// PragmaDiagnosticHandler - e.g. '#pragma GCC diagnostic ignored "-Wformat"'
840struct PragmaDiagnosticHandler : public PragmaHandler {
Douglas Gregorc09ce122011-06-22 19:41:48 +0000841private:
842 const char *Namespace;
Chris Lattner04ae2df2009-07-12 21:18:45 +0000843public:
Douglas Gregorc09ce122011-06-22 19:41:48 +0000844 explicit PragmaDiagnosticHandler(const char *NS) :
845 PragmaHandler("diagnostic"), Namespace(NS) {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000846 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
847 Token &DiagToken) {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000848 SourceLocation DiagLoc = DiagToken.getLocation();
Chris Lattneredaf8772009-04-19 23:16:58 +0000849 Token Tok;
850 PP.LexUnexpandedToken(Tok);
851 if (Tok.isNot(tok::identifier)) {
Douglas Gregor6493a4d2010-08-30 15:15:34 +0000852 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Chris Lattneredaf8772009-04-19 23:16:58 +0000853 return;
854 }
855 IdentifierInfo *II = Tok.getIdentifierInfo();
Douglas Gregorc09ce122011-06-22 19:41:48 +0000856 PPCallbacks *Callbacks = PP.getPPCallbacks();
Mike Stump1eb44332009-09-09 15:08:12 +0000857
Chris Lattneredaf8772009-04-19 23:16:58 +0000858 diag::Mapping Map;
859 if (II->isStr("warning"))
860 Map = diag::MAP_WARNING;
861 else if (II->isStr("error"))
862 Map = diag::MAP_ERROR;
863 else if (II->isStr("ignored"))
864 Map = diag::MAP_IGNORE;
865 else if (II->isStr("fatal"))
866 Map = diag::MAP_FATAL;
Douglas Gregor6493a4d2010-08-30 15:15:34 +0000867 else if (II->isStr("pop")) {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000868 if (!PP.getDiagnostics().popMappings(DiagLoc))
Douglas Gregor6493a4d2010-08-30 15:15:34 +0000869 PP.Diag(Tok, diag::warn_pragma_diagnostic_cannot_pop);
Douglas Gregorc09ce122011-06-22 19:41:48 +0000870 else if (Callbacks)
871 Callbacks->PragmaDiagnosticPop(DiagLoc, Namespace);
Douglas Gregor6493a4d2010-08-30 15:15:34 +0000872 return;
873 } else if (II->isStr("push")) {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000874 PP.getDiagnostics().pushMappings(DiagLoc);
Douglas Gregorc09ce122011-06-22 19:41:48 +0000875 if (Callbacks)
876 Callbacks->PragmaDiagnosticPush(DiagLoc, Namespace);
Chris Lattner04ae2df2009-07-12 21:18:45 +0000877 return;
878 } else {
Douglas Gregor6493a4d2010-08-30 15:15:34 +0000879 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Chris Lattneredaf8772009-04-19 23:16:58 +0000880 return;
881 }
Mike Stump1eb44332009-09-09 15:08:12 +0000882
Chris Lattneredaf8772009-04-19 23:16:58 +0000883 PP.LexUnexpandedToken(Tok);
884
885 // We need at least one string.
886 if (Tok.isNot(tok::string_literal)) {
887 PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token);
888 return;
889 }
Mike Stump1eb44332009-09-09 15:08:12 +0000890
Chris Lattneredaf8772009-04-19 23:16:58 +0000891 // String concatenation allows multiple strings, which can even come from
892 // macro expansion.
893 // "foo " "bar" "Baz"
Chris Lattner5f9e2722011-07-23 10:55:15 +0000894 SmallVector<Token, 4> StrToks;
Chris Lattneredaf8772009-04-19 23:16:58 +0000895 while (Tok.is(tok::string_literal)) {
896 StrToks.push_back(Tok);
897 PP.LexUnexpandedToken(Tok);
898 }
Mike Stump1eb44332009-09-09 15:08:12 +0000899
Peter Collingbourne84021552011-02-28 02:37:51 +0000900 if (Tok.isNot(tok::eod)) {
Chris Lattneredaf8772009-04-19 23:16:58 +0000901 PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token);
902 return;
903 }
Mike Stump1eb44332009-09-09 15:08:12 +0000904
Chris Lattneredaf8772009-04-19 23:16:58 +0000905 // Concatenate and parse the strings.
906 StringLiteralParser Literal(&StrToks[0], StrToks.size(), PP);
Douglas Gregor5cee1192011-07-27 05:40:30 +0000907 assert(Literal.isAscii() && "Didn't allow wide strings in");
Chris Lattneredaf8772009-04-19 23:16:58 +0000908 if (Literal.hadError)
909 return;
910 if (Literal.Pascal) {
Douglas Gregor6493a4d2010-08-30 15:15:34 +0000911 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Chris Lattneredaf8772009-04-19 23:16:58 +0000912 return;
913 }
Chris Lattner04ae2df2009-07-12 21:18:45 +0000914
Chris Lattner5f9e2722011-07-23 10:55:15 +0000915 StringRef WarningName(Literal.GetString());
Chris Lattneredaf8772009-04-19 23:16:58 +0000916
917 if (WarningName.size() < 3 || WarningName[0] != '-' ||
918 WarningName[1] != 'W') {
919 PP.Diag(StrToks[0].getLocation(),
920 diag::warn_pragma_diagnostic_invalid_option);
921 return;
922 }
Mike Stump1eb44332009-09-09 15:08:12 +0000923
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000924 if (PP.getDiagnostics().setDiagnosticGroupMapping(WarningName.substr(2),
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000925 Map, DiagLoc))
Chris Lattneredaf8772009-04-19 23:16:58 +0000926 PP.Diag(StrToks[0].getLocation(),
927 diag::warn_pragma_diagnostic_unknown_warning) << WarningName;
Douglas Gregorc09ce122011-06-22 19:41:48 +0000928 else if (Callbacks)
929 Callbacks->PragmaDiagnostic(DiagLoc, Namespace, Map, WarningName);
Chris Lattneredaf8772009-04-19 23:16:58 +0000930 }
931};
Mike Stump1eb44332009-09-09 15:08:12 +0000932
Chris Lattner636c5ef2009-01-16 08:21:25 +0000933/// PragmaCommentHandler - "#pragma comment ...".
934struct PragmaCommentHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000935 PragmaCommentHandler() : PragmaHandler("comment") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000936 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
937 Token &CommentTok) {
Chris Lattner636c5ef2009-01-16 08:21:25 +0000938 PP.HandlePragmaComment(CommentTok);
939 }
940};
Mike Stump1eb44332009-09-09 15:08:12 +0000941
Chris Lattnerabfe0942010-06-26 17:11:39 +0000942/// PragmaMessageHandler - "#pragma message("...")".
943struct PragmaMessageHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000944 PragmaMessageHandler() : PragmaHandler("message") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000945 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
946 Token &CommentTok) {
Chris Lattnerabfe0942010-06-26 17:11:39 +0000947 PP.HandlePragmaMessage(CommentTok);
948 }
949};
950
Chris Lattnerf47724b2010-08-17 15:55:45 +0000951/// PragmaPushMacroHandler - "#pragma push_macro" saves the value of the
952/// macro on the top of the stack.
953struct PragmaPushMacroHandler : public PragmaHandler {
954 PragmaPushMacroHandler() : PragmaHandler("push_macro") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000955 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
956 Token &PushMacroTok) {
Chris Lattnerf47724b2010-08-17 15:55:45 +0000957 PP.HandlePragmaPushMacro(PushMacroTok);
958 }
959};
960
961
962/// PragmaPopMacroHandler - "#pragma pop_macro" sets the value of the
963/// macro to the value on the top of the stack.
964struct PragmaPopMacroHandler : public PragmaHandler {
965 PragmaPopMacroHandler() : PragmaHandler("pop_macro") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000966 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
967 Token &PopMacroTok) {
Chris Lattnerf47724b2010-08-17 15:55:45 +0000968 PP.HandlePragmaPopMacro(PopMacroTok);
969 }
970};
971
Chris Lattner062f2322009-04-19 21:20:35 +0000972// Pragma STDC implementations.
Chris Lattner6c5cf4a2009-04-19 21:50:08 +0000973
Chris Lattner062f2322009-04-19 21:20:35 +0000974/// PragmaSTDC_FENV_ACCESSHandler - "#pragma STDC FENV_ACCESS ...".
975struct PragmaSTDC_FENV_ACCESSHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000976 PragmaSTDC_FENV_ACCESSHandler() : PragmaHandler("FENV_ACCESS") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000977 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
978 Token &Tok) {
Peter Collingbourne9d3f5f72011-02-14 01:42:24 +0000979 tok::OnOffSwitch OOS;
980 if (PP.LexOnOffSwitch(OOS))
981 return;
982 if (OOS == tok::OOS_ON)
Chris Lattner4d8aac32009-04-19 21:55:32 +0000983 PP.Diag(Tok, diag::warn_stdc_fenv_access_not_supported);
Chris Lattner062f2322009-04-19 21:20:35 +0000984 }
985};
Mike Stump1eb44332009-09-09 15:08:12 +0000986
Chris Lattner062f2322009-04-19 21:20:35 +0000987/// PragmaSTDC_CX_LIMITED_RANGEHandler - "#pragma STDC CX_LIMITED_RANGE ...".
988struct PragmaSTDC_CX_LIMITED_RANGEHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000989 PragmaSTDC_CX_LIMITED_RANGEHandler()
990 : PragmaHandler("CX_LIMITED_RANGE") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000991 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
992 Token &Tok) {
Peter Collingbourne9d3f5f72011-02-14 01:42:24 +0000993 tok::OnOffSwitch OOS;
994 PP.LexOnOffSwitch(OOS);
Chris Lattner062f2322009-04-19 21:20:35 +0000995 }
996};
Mike Stump1eb44332009-09-09 15:08:12 +0000997
Chris Lattner062f2322009-04-19 21:20:35 +0000998/// PragmaSTDC_UnknownHandler - "#pragma STDC ...".
999struct PragmaSTDC_UnknownHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001000 PragmaSTDC_UnknownHandler() {}
Douglas Gregor80c60f72010-09-09 22:45:38 +00001001 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1002 Token &UnknownTok) {
Chris Lattner6c5cf4a2009-04-19 21:50:08 +00001003 // C99 6.10.6p2, unknown forms are not allowed.
Chris Lattnerf545be52009-04-19 21:25:37 +00001004 PP.Diag(UnknownTok, diag::ext_stdc_pragma_ignored);
Chris Lattner062f2322009-04-19 21:20:35 +00001005 }
1006};
Mike Stump1eb44332009-09-09 15:08:12 +00001007
John McCall8dfac0b2011-09-30 05:12:12 +00001008/// PragmaARCCFCodeAuditedHandler -
1009/// #pragma clang arc_cf_code_audited begin/end
1010struct PragmaARCCFCodeAuditedHandler : public PragmaHandler {
1011 PragmaARCCFCodeAuditedHandler() : PragmaHandler("arc_cf_code_audited") {}
1012 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1013 Token &NameTok) {
1014 SourceLocation Loc = NameTok.getLocation();
1015 bool IsBegin;
1016
1017 Token Tok;
1018
1019 // Lex the 'begin' or 'end'.
1020 PP.LexUnexpandedToken(Tok);
1021 const IdentifierInfo *BeginEnd = Tok.getIdentifierInfo();
1022 if (BeginEnd && BeginEnd->isStr("begin")) {
1023 IsBegin = true;
1024 } else if (BeginEnd && BeginEnd->isStr("end")) {
1025 IsBegin = false;
1026 } else {
1027 PP.Diag(Tok.getLocation(), diag::err_pp_arc_cf_code_audited_syntax);
1028 return;
1029 }
1030
1031 // Verify that this is followed by EOD.
1032 PP.LexUnexpandedToken(Tok);
1033 if (Tok.isNot(tok::eod))
1034 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
1035
1036 // The start location of the active audit.
1037 SourceLocation BeginLoc = PP.getPragmaARCCFCodeAuditedLoc();
1038
1039 // The start location we want after processing this.
1040 SourceLocation NewLoc;
1041
1042 if (IsBegin) {
1043 // Complain about attempts to re-enter an audit.
1044 if (BeginLoc.isValid()) {
1045 PP.Diag(Loc, diag::err_pp_double_begin_of_arc_cf_code_audited);
1046 PP.Diag(BeginLoc, diag::note_pragma_entered_here);
1047 }
1048 NewLoc = Loc;
1049 } else {
1050 // Complain about attempts to leave an audit that doesn't exist.
1051 if (!BeginLoc.isValid()) {
1052 PP.Diag(Loc, diag::err_pp_unmatched_end_of_arc_cf_code_audited);
1053 return;
1054 }
1055 NewLoc = SourceLocation();
1056 }
1057
1058 PP.setPragmaARCCFCodeAuditedLoc(NewLoc);
1059 }
1060};
1061
Reid Spencer5f016e22007-07-11 17:01:13 +00001062} // end anonymous namespace
1063
1064
1065/// RegisterBuiltinPragmas - Install the standard preprocessor pragmas:
1066/// #pragma GCC poison/system_header/dependency and #pragma once.
1067void Preprocessor::RegisterBuiltinPragmas() {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001068 AddPragmaHandler(new PragmaOnceHandler());
1069 AddPragmaHandler(new PragmaMarkHandler());
Chris Lattnerf47724b2010-08-17 15:55:45 +00001070 AddPragmaHandler(new PragmaPushMacroHandler());
1071 AddPragmaHandler(new PragmaPopMacroHandler());
Michael J. Spencer301669b2010-09-27 06:19:02 +00001072 AddPragmaHandler(new PragmaMessageHandler());
Mike Stump1eb44332009-09-09 15:08:12 +00001073
Chris Lattnere8fa06e2009-05-12 18:21:11 +00001074 // #pragma GCC ...
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001075 AddPragmaHandler("GCC", new PragmaPoisonHandler());
1076 AddPragmaHandler("GCC", new PragmaSystemHeaderHandler());
1077 AddPragmaHandler("GCC", new PragmaDependencyHandler());
Douglas Gregorc09ce122011-06-22 19:41:48 +00001078 AddPragmaHandler("GCC", new PragmaDiagnosticHandler("GCC"));
Chris Lattnere8fa06e2009-05-12 18:21:11 +00001079 // #pragma clang ...
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001080 AddPragmaHandler("clang", new PragmaPoisonHandler());
1081 AddPragmaHandler("clang", new PragmaSystemHeaderHandler());
Daniel Dunbarabf7b722010-07-28 15:40:33 +00001082 AddPragmaHandler("clang", new PragmaDebugHandler());
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001083 AddPragmaHandler("clang", new PragmaDependencyHandler());
Douglas Gregorc09ce122011-06-22 19:41:48 +00001084 AddPragmaHandler("clang", new PragmaDiagnosticHandler("clang"));
John McCall8dfac0b2011-09-30 05:12:12 +00001085 AddPragmaHandler("clang", new PragmaARCCFCodeAuditedHandler());
Chris Lattnere8fa06e2009-05-12 18:21:11 +00001086
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001087 AddPragmaHandler("STDC", new PragmaSTDC_FENV_ACCESSHandler());
1088 AddPragmaHandler("STDC", new PragmaSTDC_CX_LIMITED_RANGEHandler());
Chris Lattner062f2322009-04-19 21:20:35 +00001089 AddPragmaHandler("STDC", new PragmaSTDC_UnknownHandler());
Mike Stump1eb44332009-09-09 15:08:12 +00001090
Chris Lattner636c5ef2009-01-16 08:21:25 +00001091 // MS extensions.
Francois Pichet62ec1f22011-09-17 17:15:52 +00001092 if (Features.MicrosoftExt) {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001093 AddPragmaHandler(new PragmaCommentHandler());
Chris Lattnerabfe0942010-06-26 17:11:39 +00001094 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001095}