blob: 512b024ad8cce39d4ea0fc2718137a6664945ca5 [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.
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +000057PragmaHandler *PragmaNamespace::FindHandler(llvm::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;
61 return IgnoreNull ? 0 : Handlers.lookup(llvm::StringRef());
62}
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()
88 : llvm::StringRef(),
89 /*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 ')'.
John McCall1ef8a2e2010-08-28 22:34:47 +0000213 llvm::SmallVector<Token, 32> PragmaToks;
214 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;
356 llvm::StringRef Filename = getSpelling(FilenameTok, FilenameBuffer, &Invalid);
357 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;
Manuel Klimek74124942011-04-26 21:50:03 +0000369 const FileEntry *File = LookupFile(Filename, isAngled, 0, CurDir, NULL, NULL);
Chris Lattner56b05c82008-11-18 08:02:48 +0000370 if (File == 0) {
Chris Lattnera1394812010-01-10 01:35:12 +0000371 Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
Chris Lattner56b05c82008-11-18 08:02:48 +0000372 return;
373 }
Mike Stump1eb44332009-09-09 15:08:12 +0000374
Chris Lattner2b2453a2009-01-17 06:22:33 +0000375 const FileEntry *CurFile = getCurrentFileLexer()->getFileEntry();
Reid Spencer5f016e22007-07-11 17:01:13 +0000376
377 // If this file is older than the file it depends on, emit a diagnostic.
378 if (CurFile && CurFile->getModificationTime() < File->getModificationTime()) {
379 // Lex tokens at the end of the message and include them in the message.
380 std::string Message;
381 Lex(DependencyTok);
Peter Collingbourne84021552011-02-28 02:37:51 +0000382 while (DependencyTok.isNot(tok::eod)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000383 Message += getSpelling(DependencyTok) + " ";
384 Lex(DependencyTok);
385 }
Mike Stump1eb44332009-09-09 15:08:12 +0000386
Chris Lattner96de2592010-09-05 23:16:09 +0000387 // Remove the trailing ' ' if present.
388 if (!Message.empty())
389 Message.erase(Message.end()-1);
Chris Lattner56b05c82008-11-18 08:02:48 +0000390 Diag(FilenameTok, diag::pp_out_of_date_dependency) << Message;
Reid Spencer5f016e22007-07-11 17:01:13 +0000391 }
392}
393
Chris Lattner636c5ef2009-01-16 08:21:25 +0000394/// HandlePragmaComment - Handle the microsoft #pragma comment extension. The
395/// syntax is:
396/// #pragma comment(linker, "foo")
397/// 'linker' is one of five identifiers: compiler, exestr, lib, linker, user.
398/// "foo" is a string, which is fully macro expanded, and permits string
Gabor Greifd7ee3492009-03-17 11:39:38 +0000399/// concatenation, embedded escape characters etc. See MSDN for more details.
Chris Lattner636c5ef2009-01-16 08:21:25 +0000400void Preprocessor::HandlePragmaComment(Token &Tok) {
401 SourceLocation CommentLoc = Tok.getLocation();
402 Lex(Tok);
403 if (Tok.isNot(tok::l_paren)) {
404 Diag(CommentLoc, diag::err_pragma_comment_malformed);
405 return;
406 }
Mike Stump1eb44332009-09-09 15:08:12 +0000407
Chris Lattner636c5ef2009-01-16 08:21:25 +0000408 // Read the identifier.
409 Lex(Tok);
410 if (Tok.isNot(tok::identifier)) {
411 Diag(CommentLoc, diag::err_pragma_comment_malformed);
412 return;
413 }
Mike Stump1eb44332009-09-09 15:08:12 +0000414
Chris Lattner636c5ef2009-01-16 08:21:25 +0000415 // Verify that this is one of the 5 whitelisted options.
416 // FIXME: warn that 'exestr' is deprecated.
417 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000418 if (!II->isStr("compiler") && !II->isStr("exestr") && !II->isStr("lib") &&
Chris Lattner636c5ef2009-01-16 08:21:25 +0000419 !II->isStr("linker") && !II->isStr("user")) {
420 Diag(Tok.getLocation(), diag::err_pragma_comment_unknown_kind);
421 return;
422 }
Mike Stump1eb44332009-09-09 15:08:12 +0000423
Chris Lattnera9d91452009-01-16 18:59:23 +0000424 // Read the optional string if present.
Chris Lattner636c5ef2009-01-16 08:21:25 +0000425 Lex(Tok);
Chris Lattnera9d91452009-01-16 18:59:23 +0000426 std::string ArgumentString;
Chris Lattner636c5ef2009-01-16 08:21:25 +0000427 if (Tok.is(tok::comma)) {
Chris Lattnera9d91452009-01-16 18:59:23 +0000428 Lex(Tok); // eat the comma.
Chris Lattner636c5ef2009-01-16 08:21:25 +0000429
430 // We need at least one string.
Chris Lattneredaf8772009-04-19 23:16:58 +0000431 if (Tok.isNot(tok::string_literal)) {
Chris Lattner636c5ef2009-01-16 08:21:25 +0000432 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
433 return;
434 }
435
436 // String concatenation allows multiple strings, which can even come from
437 // macro expansion.
438 // "foo " "bar" "Baz"
Chris Lattnera9d91452009-01-16 18:59:23 +0000439 llvm::SmallVector<Token, 4> StrToks;
Chris Lattneredaf8772009-04-19 23:16:58 +0000440 while (Tok.is(tok::string_literal)) {
Chris Lattnera9d91452009-01-16 18:59:23 +0000441 StrToks.push_back(Tok);
Chris Lattner636c5ef2009-01-16 08:21:25 +0000442 Lex(Tok);
Chris Lattnera9d91452009-01-16 18:59:23 +0000443 }
444
445 // Concatenate and parse the strings.
446 StringLiteralParser Literal(&StrToks[0], StrToks.size(), *this);
447 assert(!Literal.AnyWide && "Didn't allow wide strings in");
448 if (Literal.hadError)
449 return;
450 if (Literal.Pascal) {
451 Diag(StrToks[0].getLocation(), diag::err_pragma_comment_malformed);
452 return;
453 }
454
Jay Foad65aa6882011-06-21 15:13:30 +0000455 ArgumentString = Literal.GetString();
Chris Lattner636c5ef2009-01-16 08:21:25 +0000456 }
Mike Stump1eb44332009-09-09 15:08:12 +0000457
Chris Lattnera9d91452009-01-16 18:59:23 +0000458 // FIXME: If the kind is "compiler" warn if the string is present (it is
459 // ignored).
460 // FIXME: 'lib' requires a comment string.
461 // FIXME: 'linker' requires a comment string, and has a specific list of
462 // things that are allowable.
Mike Stump1eb44332009-09-09 15:08:12 +0000463
Chris Lattner636c5ef2009-01-16 08:21:25 +0000464 if (Tok.isNot(tok::r_paren)) {
465 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
466 return;
467 }
Chris Lattnera9d91452009-01-16 18:59:23 +0000468 Lex(Tok); // eat the r_paren.
Chris Lattner636c5ef2009-01-16 08:21:25 +0000469
Peter Collingbourne84021552011-02-28 02:37:51 +0000470 if (Tok.isNot(tok::eod)) {
Chris Lattner636c5ef2009-01-16 08:21:25 +0000471 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
472 return;
473 }
Mike Stump1eb44332009-09-09 15:08:12 +0000474
Chris Lattnera9d91452009-01-16 18:59:23 +0000475 // If the pragma is lexically sound, notify any interested PPCallbacks.
Chris Lattner172e3362009-01-16 19:01:46 +0000476 if (Callbacks)
477 Callbacks->PragmaComment(CommentLoc, II, ArgumentString);
Chris Lattner636c5ef2009-01-16 08:21:25 +0000478}
479
Michael J. Spencer301669b2010-09-27 06:19:02 +0000480/// HandlePragmaMessage - Handle the microsoft and gcc #pragma message
481/// extension. The syntax is:
482/// #pragma message(string)
483/// OR, in GCC mode:
484/// #pragma message string
485/// string is a string, which is fully macro expanded, and permits string
486/// concatenation, embedded escape characters, etc... See MSDN for more details.
Chris Lattnerabfe0942010-06-26 17:11:39 +0000487void Preprocessor::HandlePragmaMessage(Token &Tok) {
488 SourceLocation MessageLoc = Tok.getLocation();
489 Lex(Tok);
Michael J. Spencer301669b2010-09-27 06:19:02 +0000490 bool ExpectClosingParen = false;
Michael J. Spencerd83fc542010-09-27 06:34:47 +0000491 switch (Tok.getKind()) {
Michael J. Spencer301669b2010-09-27 06:19:02 +0000492 case tok::l_paren:
493 // We have a MSVC style pragma message.
494 ExpectClosingParen = true;
495 // Read the string.
496 Lex(Tok);
497 break;
498 case tok::string_literal:
499 // We have a GCC style pragma message, and we just read the string.
500 break;
501 default:
Chris Lattnerabfe0942010-06-26 17:11:39 +0000502 Diag(MessageLoc, diag::err_pragma_message_malformed);
503 return;
504 }
Chris Lattner636c5ef2009-01-16 08:21:25 +0000505
Chris Lattnerabfe0942010-06-26 17:11:39 +0000506 // We need at least one string.
507 if (Tok.isNot(tok::string_literal)) {
508 Diag(Tok.getLocation(), diag::err_pragma_message_malformed);
509 return;
510 }
511
512 // String concatenation allows multiple strings, which can even come from
513 // macro expansion.
514 // "foo " "bar" "Baz"
515 llvm::SmallVector<Token, 4> StrToks;
516 while (Tok.is(tok::string_literal)) {
517 StrToks.push_back(Tok);
518 Lex(Tok);
519 }
520
521 // Concatenate and parse the strings.
522 StringLiteralParser Literal(&StrToks[0], StrToks.size(), *this);
523 assert(!Literal.AnyWide && "Didn't allow wide strings in");
524 if (Literal.hadError)
525 return;
526 if (Literal.Pascal) {
527 Diag(StrToks[0].getLocation(), diag::err_pragma_message_malformed);
528 return;
529 }
530
Jay Foad65aa6882011-06-21 15:13:30 +0000531 llvm::StringRef MessageString(Literal.GetString());
Chris Lattnerabfe0942010-06-26 17:11:39 +0000532
Michael J. Spencer301669b2010-09-27 06:19:02 +0000533 if (ExpectClosingParen) {
534 if (Tok.isNot(tok::r_paren)) {
535 Diag(Tok.getLocation(), diag::err_pragma_message_malformed);
536 return;
537 }
538 Lex(Tok); // eat the r_paren.
Chris Lattnerabfe0942010-06-26 17:11:39 +0000539 }
Chris Lattnerabfe0942010-06-26 17:11:39 +0000540
Peter Collingbourne84021552011-02-28 02:37:51 +0000541 if (Tok.isNot(tok::eod)) {
Chris Lattnerabfe0942010-06-26 17:11:39 +0000542 Diag(Tok.getLocation(), diag::err_pragma_message_malformed);
543 return;
544 }
545
546 // Output the message.
547 Diag(MessageLoc, diag::warn_pragma_message) << MessageString;
548
549 // If the pragma is lexically sound, notify any interested PPCallbacks.
550 if (Callbacks)
551 Callbacks->PragmaMessage(MessageLoc, MessageString);
552}
Chris Lattner636c5ef2009-01-16 08:21:25 +0000553
Chris Lattnerf47724b2010-08-17 15:55:45 +0000554/// ParsePragmaPushOrPopMacro - Handle parsing of pragma push_macro/pop_macro.
555/// Return the IdentifierInfo* associated with the macro to push or pop.
556IdentifierInfo *Preprocessor::ParsePragmaPushOrPopMacro(Token &Tok) {
557 // Remember the pragma token location.
558 Token PragmaTok = Tok;
559
560 // Read the '('.
561 Lex(Tok);
562 if (Tok.isNot(tok::l_paren)) {
563 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
564 << getSpelling(PragmaTok);
565 return 0;
566 }
567
568 // Read the macro name string.
569 Lex(Tok);
570 if (Tok.isNot(tok::string_literal)) {
571 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
572 << getSpelling(PragmaTok);
573 return 0;
574 }
575
576 // Remember the macro string.
577 std::string StrVal = getSpelling(Tok);
578
579 // Read the ')'.
580 Lex(Tok);
581 if (Tok.isNot(tok::r_paren)) {
582 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
583 << getSpelling(PragmaTok);
584 return 0;
585 }
586
587 assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
588 "Invalid string token!");
589
590 // Create a Token from the string.
591 Token MacroTok;
592 MacroTok.startToken();
Abramo Bagnarac4bf2b92010-12-22 08:23:18 +0000593 MacroTok.setKind(tok::raw_identifier);
Chris Lattnerf47724b2010-08-17 15:55:45 +0000594 CreateString(&StrVal[1], StrVal.size() - 2, MacroTok);
595
596 // Get the IdentifierInfo of MacroToPushTok.
597 return LookUpIdentifierInfo(MacroTok);
598}
599
600/// HandlePragmaPushMacro - Handle #pragma push_macro.
601/// The syntax is:
602/// #pragma push_macro("macro")
603void Preprocessor::HandlePragmaPushMacro(Token &PushMacroTok) {
604 // Parse the pragma directive and get the macro IdentifierInfo*.
605 IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PushMacroTok);
606 if (!IdentInfo) return;
607
608 // Get the MacroInfo associated with IdentInfo.
609 MacroInfo *MI = getMacroInfo(IdentInfo);
610
611 MacroInfo *MacroCopyToPush = 0;
612 if (MI) {
613 // Make a clone of MI.
614 MacroCopyToPush = CloneMacroInfo(*MI);
615
616 // Allow the original MacroInfo to be redefined later.
617 MI->setIsAllowRedefinitionsWithoutWarning(true);
618 }
619
620 // Push the cloned MacroInfo so we can retrieve it later.
621 PragmaPushMacroInfo[IdentInfo].push_back(MacroCopyToPush);
622}
623
Ted Kremenekb275e3d2010-10-19 17:40:50 +0000624/// HandlePragmaPopMacro - Handle #pragma pop_macro.
Chris Lattnerf47724b2010-08-17 15:55:45 +0000625/// The syntax is:
626/// #pragma pop_macro("macro")
627void Preprocessor::HandlePragmaPopMacro(Token &PopMacroTok) {
628 SourceLocation MessageLoc = PopMacroTok.getLocation();
629
630 // Parse the pragma directive and get the macro IdentifierInfo*.
631 IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PopMacroTok);
632 if (!IdentInfo) return;
633
634 // Find the vector<MacroInfo*> associated with the macro.
635 llvm::DenseMap<IdentifierInfo*, std::vector<MacroInfo*> >::iterator iter =
636 PragmaPushMacroInfo.find(IdentInfo);
637 if (iter != PragmaPushMacroInfo.end()) {
638 // Release the MacroInfo currently associated with IdentInfo.
639 MacroInfo *CurrentMI = getMacroInfo(IdentInfo);
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000640 if (CurrentMI) {
641 if (CurrentMI->isWarnIfUnused())
642 WarnUnusedMacroLocs.erase(CurrentMI->getDefinitionLoc());
643 ReleaseMacroInfo(CurrentMI);
644 }
Chris Lattnerf47724b2010-08-17 15:55:45 +0000645
646 // Get the MacroInfo we want to reinstall.
647 MacroInfo *MacroToReInstall = iter->second.back();
648
649 // Reinstall the previously pushed macro.
650 setMacroInfo(IdentInfo, MacroToReInstall);
651
652 // Pop PragmaPushMacroInfo stack.
653 iter->second.pop_back();
654 if (iter->second.size() == 0)
655 PragmaPushMacroInfo.erase(iter);
656 } else {
657 Diag(MessageLoc, diag::warn_pragma_pop_macro_no_push)
658 << IdentInfo->getName();
659 }
660}
Reid Spencer5f016e22007-07-11 17:01:13 +0000661
662/// AddPragmaHandler - Add the specified pragma handler to the preprocessor.
663/// If 'Namespace' is non-null, then it is a token required to exist on the
664/// pragma line before the pragma string starts, e.g. "STDC" or "GCC".
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000665void Preprocessor::AddPragmaHandler(llvm::StringRef Namespace,
Reid Spencer5f016e22007-07-11 17:01:13 +0000666 PragmaHandler *Handler) {
667 PragmaNamespace *InsertNS = PragmaHandlers;
Mike Stump1eb44332009-09-09 15:08:12 +0000668
Reid Spencer5f016e22007-07-11 17:01:13 +0000669 // If this is specified to be in a namespace, step down into it.
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000670 if (!Namespace.empty()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000671 // If there is already a pragma handler with the name of this namespace,
672 // we either have an error (directive with the same name as a namespace) or
673 // we already have the namespace to insert into.
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000674 if (PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000675 InsertNS = Existing->getIfNamespace();
676 assert(InsertNS != 0 && "Cannot have a pragma namespace and pragma"
677 " handler with the same name!");
678 } else {
679 // Otherwise, this namespace doesn't exist yet, create and insert the
680 // handler for it.
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000681 InsertNS = new PragmaNamespace(Namespace);
Reid Spencer5f016e22007-07-11 17:01:13 +0000682 PragmaHandlers->AddPragma(InsertNS);
683 }
684 }
Mike Stump1eb44332009-09-09 15:08:12 +0000685
Reid Spencer5f016e22007-07-11 17:01:13 +0000686 // Check to make sure we don't already have a pragma for this identifier.
687 assert(!InsertNS->FindHandler(Handler->getName()) &&
688 "Pragma handler already exists for this identifier!");
689 InsertNS->AddPragma(Handler);
690}
691
Daniel Dunbar40950802008-10-04 19:17:46 +0000692/// RemovePragmaHandler - Remove the specific pragma handler from the
693/// preprocessor. If \arg Namespace is non-null, then it should be the
694/// namespace that \arg Handler was added to. It is an error to remove
695/// a handler that has not been registered.
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000696void Preprocessor::RemovePragmaHandler(llvm::StringRef Namespace,
Daniel Dunbar40950802008-10-04 19:17:46 +0000697 PragmaHandler *Handler) {
698 PragmaNamespace *NS = PragmaHandlers;
Mike Stump1eb44332009-09-09 15:08:12 +0000699
Daniel Dunbar40950802008-10-04 19:17:46 +0000700 // If this is specified to be in a namespace, step down into it.
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000701 if (!Namespace.empty()) {
702 PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace);
Daniel Dunbar40950802008-10-04 19:17:46 +0000703 assert(Existing && "Namespace containing handler does not exist!");
704
705 NS = Existing->getIfNamespace();
706 assert(NS && "Invalid namespace, registered as a regular pragma handler!");
707 }
708
709 NS->RemovePragmaHandler(Handler);
Mike Stump1eb44332009-09-09 15:08:12 +0000710
Daniel Dunbar40950802008-10-04 19:17:46 +0000711 // If this is a non-default namespace and it is now empty, remove
712 // it.
713 if (NS != PragmaHandlers && NS->IsEmpty())
714 PragmaHandlers->RemovePragmaHandler(NS);
715}
716
Peter Collingbourne9d3f5f72011-02-14 01:42:24 +0000717bool Preprocessor::LexOnOffSwitch(tok::OnOffSwitch &Result) {
718 Token Tok;
719 LexUnexpandedToken(Tok);
720
721 if (Tok.isNot(tok::identifier)) {
722 Diag(Tok, diag::ext_on_off_switch_syntax);
723 return true;
724 }
725 IdentifierInfo *II = Tok.getIdentifierInfo();
726 if (II->isStr("ON"))
727 Result = tok::OOS_ON;
728 else if (II->isStr("OFF"))
729 Result = tok::OOS_OFF;
730 else if (II->isStr("DEFAULT"))
731 Result = tok::OOS_DEFAULT;
732 else {
733 Diag(Tok, diag::ext_on_off_switch_syntax);
734 return true;
735 }
736
Peter Collingbourne84021552011-02-28 02:37:51 +0000737 // Verify that this is followed by EOD.
Peter Collingbourne9d3f5f72011-02-14 01:42:24 +0000738 LexUnexpandedToken(Tok);
Peter Collingbourne84021552011-02-28 02:37:51 +0000739 if (Tok.isNot(tok::eod))
740 Diag(Tok, diag::ext_pragma_syntax_eod);
Peter Collingbourne9d3f5f72011-02-14 01:42:24 +0000741 return false;
742}
743
Reid Spencer5f016e22007-07-11 17:01:13 +0000744namespace {
Chris Lattner22434492007-12-19 19:38:36 +0000745/// PragmaOnceHandler - "#pragma once" marks the file as atomically included.
Reid Spencer5f016e22007-07-11 17:01:13 +0000746struct PragmaOnceHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000747 PragmaOnceHandler() : PragmaHandler("once") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000748 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
749 Token &OnceTok) {
Chris Lattner35410d52009-04-14 05:07:49 +0000750 PP.CheckEndOfDirective("pragma once");
Reid Spencer5f016e22007-07-11 17:01:13 +0000751 PP.HandlePragmaOnce(OnceTok);
752 }
753};
754
Chris Lattner22434492007-12-19 19:38:36 +0000755/// PragmaMarkHandler - "#pragma mark ..." is ignored by the compiler, and the
756/// rest of the line is not lexed.
757struct PragmaMarkHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000758 PragmaMarkHandler() : PragmaHandler("mark") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000759 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
760 Token &MarkTok) {
Chris Lattner22434492007-12-19 19:38:36 +0000761 PP.HandlePragmaMark();
762 }
763};
764
765/// PragmaPoisonHandler - "#pragma poison x" marks x as not usable.
Reid Spencer5f016e22007-07-11 17:01:13 +0000766struct PragmaPoisonHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000767 PragmaPoisonHandler() : PragmaHandler("poison") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000768 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
769 Token &PoisonTok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000770 PP.HandlePragmaPoison(PoisonTok);
771 }
772};
773
Chris Lattner22434492007-12-19 19:38:36 +0000774/// PragmaSystemHeaderHandler - "#pragma system_header" marks the current file
775/// as a system header, which silences warnings in it.
Reid Spencer5f016e22007-07-11 17:01:13 +0000776struct PragmaSystemHeaderHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000777 PragmaSystemHeaderHandler() : PragmaHandler("system_header") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000778 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
779 Token &SHToken) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000780 PP.HandlePragmaSystemHeader(SHToken);
Chris Lattner35410d52009-04-14 05:07:49 +0000781 PP.CheckEndOfDirective("pragma");
Reid Spencer5f016e22007-07-11 17:01:13 +0000782 }
783};
784struct PragmaDependencyHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000785 PragmaDependencyHandler() : PragmaHandler("dependency") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000786 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
787 Token &DepToken) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000788 PP.HandlePragmaDependency(DepToken);
789 }
790};
Mike Stump1eb44332009-09-09 15:08:12 +0000791
Daniel Dunbarabf7b722010-07-28 15:40:33 +0000792struct PragmaDebugHandler : public PragmaHandler {
793 PragmaDebugHandler() : PragmaHandler("__debug") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000794 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
795 Token &DepToken) {
Daniel Dunbarabf7b722010-07-28 15:40:33 +0000796 Token Tok;
797 PP.LexUnexpandedToken(Tok);
798 if (Tok.isNot(tok::identifier)) {
Douglas Gregor6493a4d2010-08-30 15:15:34 +0000799 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Daniel Dunbarabf7b722010-07-28 15:40:33 +0000800 return;
801 }
802 IdentifierInfo *II = Tok.getIdentifierInfo();
803
Daniel Dunbar55054132010-08-17 22:32:48 +0000804 if (II->isStr("assert")) {
805 assert(0 && "This is an assertion!");
Daniel Dunbarabf7b722010-07-28 15:40:33 +0000806 } else if (II->isStr("crash")) {
Daniel Dunbar55054132010-08-17 22:32:48 +0000807 *(volatile int*) 0x11 = 0;
808 } else if (II->isStr("llvm_fatal_error")) {
809 llvm::report_fatal_error("#pragma clang __debug llvm_fatal_error");
810 } else if (II->isStr("llvm_unreachable")) {
811 llvm_unreachable("#pragma clang __debug llvm_unreachable");
812 } else if (II->isStr("overflow_stack")) {
813 DebugOverflowStack();
Daniel Dunbarff759a62010-08-18 23:09:23 +0000814 } else if (II->isStr("handle_crash")) {
815 llvm::CrashRecoveryContext *CRC =llvm::CrashRecoveryContext::GetCurrent();
816 if (CRC)
817 CRC->HandleCrash();
Daniel Dunbar55054132010-08-17 22:32:48 +0000818 } else {
819 PP.Diag(Tok, diag::warn_pragma_debug_unexpected_command)
820 << II->getName();
Daniel Dunbarabf7b722010-07-28 15:40:33 +0000821 }
822 }
823
Francois Pichet1066c6c2011-05-25 16:15:03 +0000824// Disable MSVC warning about runtime stack overflow.
825#ifdef _MSC_VER
826 #pragma warning(disable : 4717)
827#endif
Daniel Dunbarabf7b722010-07-28 15:40:33 +0000828 void DebugOverflowStack() {
829 DebugOverflowStack();
830 }
Francois Pichet1066c6c2011-05-25 16:15:03 +0000831#ifdef _MSC_VER
832 #pragma warning(default : 4717)
833#endif
834
Daniel Dunbarabf7b722010-07-28 15:40:33 +0000835};
836
Chris Lattneredaf8772009-04-19 23:16:58 +0000837/// PragmaDiagnosticHandler - e.g. '#pragma GCC diagnostic ignored "-Wformat"'
838struct PragmaDiagnosticHandler : public PragmaHandler {
Chris Lattner04ae2df2009-07-12 21:18:45 +0000839public:
Douglas Gregor6493a4d2010-08-30 15:15:34 +0000840 explicit PragmaDiagnosticHandler() : PragmaHandler("diagnostic") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000841 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
842 Token &DiagToken) {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000843 SourceLocation DiagLoc = DiagToken.getLocation();
Chris Lattneredaf8772009-04-19 23:16:58 +0000844 Token Tok;
845 PP.LexUnexpandedToken(Tok);
846 if (Tok.isNot(tok::identifier)) {
Douglas Gregor6493a4d2010-08-30 15:15:34 +0000847 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Chris Lattneredaf8772009-04-19 23:16:58 +0000848 return;
849 }
850 IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000851
Chris Lattneredaf8772009-04-19 23:16:58 +0000852 diag::Mapping Map;
853 if (II->isStr("warning"))
854 Map = diag::MAP_WARNING;
855 else if (II->isStr("error"))
856 Map = diag::MAP_ERROR;
857 else if (II->isStr("ignored"))
858 Map = diag::MAP_IGNORE;
859 else if (II->isStr("fatal"))
860 Map = diag::MAP_FATAL;
Douglas Gregor6493a4d2010-08-30 15:15:34 +0000861 else if (II->isStr("pop")) {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000862 if (!PP.getDiagnostics().popMappings(DiagLoc))
Douglas Gregor6493a4d2010-08-30 15:15:34 +0000863 PP.Diag(Tok, diag::warn_pragma_diagnostic_cannot_pop);
Chris Lattner04ae2df2009-07-12 21:18:45 +0000864
Douglas Gregor6493a4d2010-08-30 15:15:34 +0000865 return;
866 } else if (II->isStr("push")) {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000867 PP.getDiagnostics().pushMappings(DiagLoc);
Chris Lattner04ae2df2009-07-12 21:18:45 +0000868 return;
869 } else {
Douglas Gregor6493a4d2010-08-30 15:15:34 +0000870 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Chris Lattneredaf8772009-04-19 23:16:58 +0000871 return;
872 }
Mike Stump1eb44332009-09-09 15:08:12 +0000873
Chris Lattneredaf8772009-04-19 23:16:58 +0000874 PP.LexUnexpandedToken(Tok);
875
876 // We need at least one string.
877 if (Tok.isNot(tok::string_literal)) {
878 PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token);
879 return;
880 }
Mike Stump1eb44332009-09-09 15:08:12 +0000881
Chris Lattneredaf8772009-04-19 23:16:58 +0000882 // String concatenation allows multiple strings, which can even come from
883 // macro expansion.
884 // "foo " "bar" "Baz"
885 llvm::SmallVector<Token, 4> StrToks;
886 while (Tok.is(tok::string_literal)) {
887 StrToks.push_back(Tok);
888 PP.LexUnexpandedToken(Tok);
889 }
Mike Stump1eb44332009-09-09 15:08:12 +0000890
Peter Collingbourne84021552011-02-28 02:37:51 +0000891 if (Tok.isNot(tok::eod)) {
Chris Lattneredaf8772009-04-19 23:16:58 +0000892 PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token);
893 return;
894 }
Mike Stump1eb44332009-09-09 15:08:12 +0000895
Chris Lattneredaf8772009-04-19 23:16:58 +0000896 // Concatenate and parse the strings.
897 StringLiteralParser Literal(&StrToks[0], StrToks.size(), PP);
898 assert(!Literal.AnyWide && "Didn't allow wide strings in");
899 if (Literal.hadError)
900 return;
901 if (Literal.Pascal) {
Douglas Gregor6493a4d2010-08-30 15:15:34 +0000902 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Chris Lattneredaf8772009-04-19 23:16:58 +0000903 return;
904 }
Chris Lattner04ae2df2009-07-12 21:18:45 +0000905
Jay Foad65aa6882011-06-21 15:13:30 +0000906 llvm::StringRef WarningName(Literal.GetString());
Chris Lattneredaf8772009-04-19 23:16:58 +0000907
908 if (WarningName.size() < 3 || WarningName[0] != '-' ||
909 WarningName[1] != 'W') {
910 PP.Diag(StrToks[0].getLocation(),
911 diag::warn_pragma_diagnostic_invalid_option);
912 return;
913 }
Mike Stump1eb44332009-09-09 15:08:12 +0000914
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000915 if (PP.getDiagnostics().setDiagnosticGroupMapping(WarningName.substr(2),
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000916 Map, DiagLoc))
Chris Lattneredaf8772009-04-19 23:16:58 +0000917 PP.Diag(StrToks[0].getLocation(),
918 diag::warn_pragma_diagnostic_unknown_warning) << WarningName;
919 }
920};
Mike Stump1eb44332009-09-09 15:08:12 +0000921
Chris Lattner636c5ef2009-01-16 08:21:25 +0000922/// PragmaCommentHandler - "#pragma comment ...".
923struct PragmaCommentHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000924 PragmaCommentHandler() : PragmaHandler("comment") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000925 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
926 Token &CommentTok) {
Chris Lattner636c5ef2009-01-16 08:21:25 +0000927 PP.HandlePragmaComment(CommentTok);
928 }
929};
Mike Stump1eb44332009-09-09 15:08:12 +0000930
Chris Lattnerabfe0942010-06-26 17:11:39 +0000931/// PragmaMessageHandler - "#pragma message("...")".
932struct PragmaMessageHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000933 PragmaMessageHandler() : PragmaHandler("message") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000934 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
935 Token &CommentTok) {
Chris Lattnerabfe0942010-06-26 17:11:39 +0000936 PP.HandlePragmaMessage(CommentTok);
937 }
938};
939
Chris Lattnerf47724b2010-08-17 15:55:45 +0000940/// PragmaPushMacroHandler - "#pragma push_macro" saves the value of the
941/// macro on the top of the stack.
942struct PragmaPushMacroHandler : public PragmaHandler {
943 PragmaPushMacroHandler() : PragmaHandler("push_macro") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000944 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
945 Token &PushMacroTok) {
Chris Lattnerf47724b2010-08-17 15:55:45 +0000946 PP.HandlePragmaPushMacro(PushMacroTok);
947 }
948};
949
950
951/// PragmaPopMacroHandler - "#pragma pop_macro" sets the value of the
952/// macro to the value on the top of the stack.
953struct PragmaPopMacroHandler : public PragmaHandler {
954 PragmaPopMacroHandler() : PragmaHandler("pop_macro") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000955 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
956 Token &PopMacroTok) {
Chris Lattnerf47724b2010-08-17 15:55:45 +0000957 PP.HandlePragmaPopMacro(PopMacroTok);
958 }
959};
960
Chris Lattner062f2322009-04-19 21:20:35 +0000961// Pragma STDC implementations.
Chris Lattner6c5cf4a2009-04-19 21:50:08 +0000962
Chris Lattner062f2322009-04-19 21:20:35 +0000963/// PragmaSTDC_FENV_ACCESSHandler - "#pragma STDC FENV_ACCESS ...".
964struct PragmaSTDC_FENV_ACCESSHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000965 PragmaSTDC_FENV_ACCESSHandler() : PragmaHandler("FENV_ACCESS") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000966 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
967 Token &Tok) {
Peter Collingbourne9d3f5f72011-02-14 01:42:24 +0000968 tok::OnOffSwitch OOS;
969 if (PP.LexOnOffSwitch(OOS))
970 return;
971 if (OOS == tok::OOS_ON)
Chris Lattner4d8aac32009-04-19 21:55:32 +0000972 PP.Diag(Tok, diag::warn_stdc_fenv_access_not_supported);
Chris Lattner062f2322009-04-19 21:20:35 +0000973 }
974};
Mike Stump1eb44332009-09-09 15:08:12 +0000975
Chris Lattner062f2322009-04-19 21:20:35 +0000976/// PragmaSTDC_CX_LIMITED_RANGEHandler - "#pragma STDC CX_LIMITED_RANGE ...".
977struct PragmaSTDC_CX_LIMITED_RANGEHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000978 PragmaSTDC_CX_LIMITED_RANGEHandler()
979 : PragmaHandler("CX_LIMITED_RANGE") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000980 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
981 Token &Tok) {
Peter Collingbourne9d3f5f72011-02-14 01:42:24 +0000982 tok::OnOffSwitch OOS;
983 PP.LexOnOffSwitch(OOS);
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_UnknownHandler - "#pragma STDC ...".
988struct PragmaSTDC_UnknownHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000989 PragmaSTDC_UnknownHandler() {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000990 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
991 Token &UnknownTok) {
Chris Lattner6c5cf4a2009-04-19 21:50:08 +0000992 // C99 6.10.6p2, unknown forms are not allowed.
Chris Lattnerf545be52009-04-19 21:25:37 +0000993 PP.Diag(UnknownTok, diag::ext_stdc_pragma_ignored);
Chris Lattner062f2322009-04-19 21:20:35 +0000994 }
995};
Mike Stump1eb44332009-09-09 15:08:12 +0000996
Reid Spencer5f016e22007-07-11 17:01:13 +0000997} // end anonymous namespace
998
999
1000/// RegisterBuiltinPragmas - Install the standard preprocessor pragmas:
1001/// #pragma GCC poison/system_header/dependency and #pragma once.
1002void Preprocessor::RegisterBuiltinPragmas() {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001003 AddPragmaHandler(new PragmaOnceHandler());
1004 AddPragmaHandler(new PragmaMarkHandler());
Chris Lattnerf47724b2010-08-17 15:55:45 +00001005 AddPragmaHandler(new PragmaPushMacroHandler());
1006 AddPragmaHandler(new PragmaPopMacroHandler());
Michael J. Spencer301669b2010-09-27 06:19:02 +00001007 AddPragmaHandler(new PragmaMessageHandler());
Mike Stump1eb44332009-09-09 15:08:12 +00001008
Chris Lattnere8fa06e2009-05-12 18:21:11 +00001009 // #pragma GCC ...
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001010 AddPragmaHandler("GCC", new PragmaPoisonHandler());
1011 AddPragmaHandler("GCC", new PragmaSystemHeaderHandler());
1012 AddPragmaHandler("GCC", new PragmaDependencyHandler());
Douglas Gregor6493a4d2010-08-30 15:15:34 +00001013 AddPragmaHandler("GCC", new PragmaDiagnosticHandler());
Chris Lattnere8fa06e2009-05-12 18:21:11 +00001014 // #pragma clang ...
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001015 AddPragmaHandler("clang", new PragmaPoisonHandler());
1016 AddPragmaHandler("clang", new PragmaSystemHeaderHandler());
Daniel Dunbarabf7b722010-07-28 15:40:33 +00001017 AddPragmaHandler("clang", new PragmaDebugHandler());
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001018 AddPragmaHandler("clang", new PragmaDependencyHandler());
Douglas Gregor6493a4d2010-08-30 15:15:34 +00001019 AddPragmaHandler("clang", new PragmaDiagnosticHandler());
Chris Lattnere8fa06e2009-05-12 18:21:11 +00001020
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001021 AddPragmaHandler("STDC", new PragmaSTDC_FENV_ACCESSHandler());
1022 AddPragmaHandler("STDC", new PragmaSTDC_CX_LIMITED_RANGEHandler());
Chris Lattner062f2322009-04-19 21:20:35 +00001023 AddPragmaHandler("STDC", new PragmaSTDC_UnknownHandler());
Mike Stump1eb44332009-09-09 15:08:12 +00001024
Chris Lattner636c5ef2009-01-16 08:21:25 +00001025 // MS extensions.
Chris Lattnerabfe0942010-06-26 17:11:39 +00001026 if (Features.Microsoft) {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001027 AddPragmaHandler(new PragmaCommentHandler());
Chris Lattnerabfe0942010-06-26 17:11:39 +00001028 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001029}