blob: 23855d4a4748c4470f7358e75a9dfd4b78ddfdb7 [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
Chris Lattner6896a372009-06-15 05:02:34 +0000329 unsigned FilenameLen = strlen(PLoc.getFilename());
330 unsigned FilenameID = SourceMgr.getLineTableFilenameID(PLoc.getFilename(),
331 FilenameLen);
Mike Stump1eb44332009-09-09 15:08:12 +0000332
Chris Lattner784c2572011-05-22 22:10:16 +0000333 // Notify the client, if desired, that we are in a new source file.
334 if (Callbacks)
335 Callbacks->FileChanged(SysHeaderTok.getLocation(),
336 PPCallbacks::SystemHeaderPragma, SrcMgr::C_System);
337
Chris Lattner6896a372009-06-15 05:02:34 +0000338 // Emit a line marker. This will change any source locations from this point
339 // forward to realize they are in a system header.
340 // Create a line note with this information.
341 SourceMgr.AddLineNote(SysHeaderTok.getLocation(), PLoc.getLine(), FilenameID,
342 false, false, true, false);
Reid Spencer5f016e22007-07-11 17:01:13 +0000343}
344
345/// HandlePragmaDependency - Handle #pragma GCC dependency "foo" blah.
346///
Chris Lattnerd2177732007-07-20 16:59:19 +0000347void Preprocessor::HandlePragmaDependency(Token &DependencyTok) {
348 Token FilenameTok;
Ted Kremenek68a91d52008-11-18 01:12:54 +0000349 CurPPLexer->LexIncludeFilename(FilenameTok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000350
Peter Collingbourne84021552011-02-28 02:37:51 +0000351 // If the token kind is EOD, the error has already been diagnosed.
352 if (FilenameTok.is(tok::eod))
Reid Spencer5f016e22007-07-11 17:01:13 +0000353 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000354
Reid Spencer5f016e22007-07-11 17:01:13 +0000355 // Reserve a buffer to get the spelling.
Chris Lattnera1394812010-01-10 01:35:12 +0000356 llvm::SmallString<128> FilenameBuffer;
Douglas Gregor453091c2010-03-16 22:30:13 +0000357 bool Invalid = false;
358 llvm::StringRef Filename = getSpelling(FilenameTok, FilenameBuffer, &Invalid);
359 if (Invalid)
360 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000361
Chris Lattnera1394812010-01-10 01:35:12 +0000362 bool isAngled =
363 GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
Reid Spencer5f016e22007-07-11 17:01:13 +0000364 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
365 // error.
Chris Lattnera1394812010-01-10 01:35:12 +0000366 if (Filename.empty())
Reid Spencer5f016e22007-07-11 17:01:13 +0000367 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000368
Reid Spencer5f016e22007-07-11 17:01:13 +0000369 // Search include directories for this file.
370 const DirectoryLookup *CurDir;
Manuel Klimek74124942011-04-26 21:50:03 +0000371 const FileEntry *File = LookupFile(Filename, isAngled, 0, CurDir, NULL, NULL);
Chris Lattner56b05c82008-11-18 08:02:48 +0000372 if (File == 0) {
Chris Lattnera1394812010-01-10 01:35:12 +0000373 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 Lattnera9d91452009-01-16 18:59:23 +0000441 llvm::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);
449 assert(!Literal.AnyWide && "Didn't allow wide strings in");
450 if (Literal.hadError)
451 return;
452 if (Literal.Pascal) {
453 Diag(StrToks[0].getLocation(), diag::err_pragma_comment_malformed);
454 return;
455 }
456
457 ArgumentString = std::string(Literal.GetString(),
458 Literal.GetString()+Literal.GetStringLength());
Chris Lattner636c5ef2009-01-16 08:21:25 +0000459 }
Mike Stump1eb44332009-09-09 15:08:12 +0000460
Chris Lattnera9d91452009-01-16 18:59:23 +0000461 // FIXME: If the kind is "compiler" warn if the string is present (it is
462 // ignored).
463 // FIXME: 'lib' requires a comment string.
464 // FIXME: 'linker' requires a comment string, and has a specific list of
465 // things that are allowable.
Mike Stump1eb44332009-09-09 15:08:12 +0000466
Chris Lattner636c5ef2009-01-16 08:21:25 +0000467 if (Tok.isNot(tok::r_paren)) {
468 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
469 return;
470 }
Chris Lattnera9d91452009-01-16 18:59:23 +0000471 Lex(Tok); // eat the r_paren.
Chris Lattner636c5ef2009-01-16 08:21:25 +0000472
Peter Collingbourne84021552011-02-28 02:37:51 +0000473 if (Tok.isNot(tok::eod)) {
Chris Lattner636c5ef2009-01-16 08:21:25 +0000474 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
475 return;
476 }
Mike Stump1eb44332009-09-09 15:08:12 +0000477
Chris Lattnera9d91452009-01-16 18:59:23 +0000478 // If the pragma is lexically sound, notify any interested PPCallbacks.
Chris Lattner172e3362009-01-16 19:01:46 +0000479 if (Callbacks)
480 Callbacks->PragmaComment(CommentLoc, II, ArgumentString);
Chris Lattner636c5ef2009-01-16 08:21:25 +0000481}
482
Michael J. Spencer301669b2010-09-27 06:19:02 +0000483/// HandlePragmaMessage - Handle the microsoft and gcc #pragma message
484/// extension. The syntax is:
485/// #pragma message(string)
486/// OR, in GCC mode:
487/// #pragma message string
488/// string is a string, which is fully macro expanded, and permits string
489/// concatenation, embedded escape characters, etc... See MSDN for more details.
Chris Lattnerabfe0942010-06-26 17:11:39 +0000490void Preprocessor::HandlePragmaMessage(Token &Tok) {
491 SourceLocation MessageLoc = Tok.getLocation();
492 Lex(Tok);
Michael J. Spencer301669b2010-09-27 06:19:02 +0000493 bool ExpectClosingParen = false;
Michael J. Spencerd83fc542010-09-27 06:34:47 +0000494 switch (Tok.getKind()) {
Michael J. Spencer301669b2010-09-27 06:19:02 +0000495 case tok::l_paren:
496 // We have a MSVC style pragma message.
497 ExpectClosingParen = true;
498 // Read the string.
499 Lex(Tok);
500 break;
501 case tok::string_literal:
502 // We have a GCC style pragma message, and we just read the string.
503 break;
504 default:
Chris Lattnerabfe0942010-06-26 17:11:39 +0000505 Diag(MessageLoc, diag::err_pragma_message_malformed);
506 return;
507 }
Chris Lattner636c5ef2009-01-16 08:21:25 +0000508
Chris Lattnerabfe0942010-06-26 17:11:39 +0000509 // We need at least one string.
510 if (Tok.isNot(tok::string_literal)) {
511 Diag(Tok.getLocation(), diag::err_pragma_message_malformed);
512 return;
513 }
514
515 // String concatenation allows multiple strings, which can even come from
516 // macro expansion.
517 // "foo " "bar" "Baz"
518 llvm::SmallVector<Token, 4> StrToks;
519 while (Tok.is(tok::string_literal)) {
520 StrToks.push_back(Tok);
521 Lex(Tok);
522 }
523
524 // Concatenate and parse the strings.
525 StringLiteralParser Literal(&StrToks[0], StrToks.size(), *this);
526 assert(!Literal.AnyWide && "Didn't allow wide strings in");
527 if (Literal.hadError)
528 return;
529 if (Literal.Pascal) {
530 Diag(StrToks[0].getLocation(), diag::err_pragma_message_malformed);
531 return;
532 }
533
534 llvm::StringRef MessageString(Literal.GetString(), Literal.GetStringLength());
535
Michael J. Spencer301669b2010-09-27 06:19:02 +0000536 if (ExpectClosingParen) {
537 if (Tok.isNot(tok::r_paren)) {
538 Diag(Tok.getLocation(), diag::err_pragma_message_malformed);
539 return;
540 }
541 Lex(Tok); // eat the r_paren.
Chris Lattnerabfe0942010-06-26 17:11:39 +0000542 }
Chris Lattnerabfe0942010-06-26 17:11:39 +0000543
Peter Collingbourne84021552011-02-28 02:37:51 +0000544 if (Tok.isNot(tok::eod)) {
Chris Lattnerabfe0942010-06-26 17:11:39 +0000545 Diag(Tok.getLocation(), diag::err_pragma_message_malformed);
546 return;
547 }
548
549 // Output the message.
550 Diag(MessageLoc, diag::warn_pragma_message) << MessageString;
551
552 // If the pragma is lexically sound, notify any interested PPCallbacks.
553 if (Callbacks)
554 Callbacks->PragmaMessage(MessageLoc, MessageString);
555}
Chris Lattner636c5ef2009-01-16 08:21:25 +0000556
Chris Lattnerf47724b2010-08-17 15:55:45 +0000557/// ParsePragmaPushOrPopMacro - Handle parsing of pragma push_macro/pop_macro.
558/// Return the IdentifierInfo* associated with the macro to push or pop.
559IdentifierInfo *Preprocessor::ParsePragmaPushOrPopMacro(Token &Tok) {
560 // Remember the pragma token location.
561 Token PragmaTok = Tok;
562
563 // Read the '('.
564 Lex(Tok);
565 if (Tok.isNot(tok::l_paren)) {
566 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
567 << getSpelling(PragmaTok);
568 return 0;
569 }
570
571 // Read the macro name string.
572 Lex(Tok);
573 if (Tok.isNot(tok::string_literal)) {
574 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
575 << getSpelling(PragmaTok);
576 return 0;
577 }
578
579 // Remember the macro string.
580 std::string StrVal = getSpelling(Tok);
581
582 // Read the ')'.
583 Lex(Tok);
584 if (Tok.isNot(tok::r_paren)) {
585 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
586 << getSpelling(PragmaTok);
587 return 0;
588 }
589
590 assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
591 "Invalid string token!");
592
593 // Create a Token from the string.
594 Token MacroTok;
595 MacroTok.startToken();
Abramo Bagnarac4bf2b92010-12-22 08:23:18 +0000596 MacroTok.setKind(tok::raw_identifier);
Chris Lattnerf47724b2010-08-17 15:55:45 +0000597 CreateString(&StrVal[1], StrVal.size() - 2, MacroTok);
598
599 // Get the IdentifierInfo of MacroToPushTok.
600 return LookUpIdentifierInfo(MacroTok);
601}
602
603/// HandlePragmaPushMacro - Handle #pragma push_macro.
604/// The syntax is:
605/// #pragma push_macro("macro")
606void Preprocessor::HandlePragmaPushMacro(Token &PushMacroTok) {
607 // Parse the pragma directive and get the macro IdentifierInfo*.
608 IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PushMacroTok);
609 if (!IdentInfo) return;
610
611 // Get the MacroInfo associated with IdentInfo.
612 MacroInfo *MI = getMacroInfo(IdentInfo);
613
614 MacroInfo *MacroCopyToPush = 0;
615 if (MI) {
616 // Make a clone of MI.
617 MacroCopyToPush = CloneMacroInfo(*MI);
618
619 // Allow the original MacroInfo to be redefined later.
620 MI->setIsAllowRedefinitionsWithoutWarning(true);
621 }
622
623 // Push the cloned MacroInfo so we can retrieve it later.
624 PragmaPushMacroInfo[IdentInfo].push_back(MacroCopyToPush);
625}
626
Ted Kremenekb275e3d2010-10-19 17:40:50 +0000627/// HandlePragmaPopMacro - Handle #pragma pop_macro.
Chris Lattnerf47724b2010-08-17 15:55:45 +0000628/// The syntax is:
629/// #pragma pop_macro("macro")
630void Preprocessor::HandlePragmaPopMacro(Token &PopMacroTok) {
631 SourceLocation MessageLoc = PopMacroTok.getLocation();
632
633 // Parse the pragma directive and get the macro IdentifierInfo*.
634 IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PopMacroTok);
635 if (!IdentInfo) return;
636
637 // Find the vector<MacroInfo*> associated with the macro.
638 llvm::DenseMap<IdentifierInfo*, std::vector<MacroInfo*> >::iterator iter =
639 PragmaPushMacroInfo.find(IdentInfo);
640 if (iter != PragmaPushMacroInfo.end()) {
641 // Release the MacroInfo currently associated with IdentInfo.
642 MacroInfo *CurrentMI = getMacroInfo(IdentInfo);
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000643 if (CurrentMI) {
644 if (CurrentMI->isWarnIfUnused())
645 WarnUnusedMacroLocs.erase(CurrentMI->getDefinitionLoc());
646 ReleaseMacroInfo(CurrentMI);
647 }
Chris Lattnerf47724b2010-08-17 15:55:45 +0000648
649 // Get the MacroInfo we want to reinstall.
650 MacroInfo *MacroToReInstall = iter->second.back();
651
652 // Reinstall the previously pushed macro.
653 setMacroInfo(IdentInfo, MacroToReInstall);
654
655 // Pop PragmaPushMacroInfo stack.
656 iter->second.pop_back();
657 if (iter->second.size() == 0)
658 PragmaPushMacroInfo.erase(iter);
659 } else {
660 Diag(MessageLoc, diag::warn_pragma_pop_macro_no_push)
661 << IdentInfo->getName();
662 }
663}
Reid Spencer5f016e22007-07-11 17:01:13 +0000664
665/// AddPragmaHandler - Add the specified pragma handler to the preprocessor.
666/// If 'Namespace' is non-null, then it is a token required to exist on the
667/// pragma line before the pragma string starts, e.g. "STDC" or "GCC".
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000668void Preprocessor::AddPragmaHandler(llvm::StringRef Namespace,
Reid Spencer5f016e22007-07-11 17:01:13 +0000669 PragmaHandler *Handler) {
670 PragmaNamespace *InsertNS = PragmaHandlers;
Mike Stump1eb44332009-09-09 15:08:12 +0000671
Reid Spencer5f016e22007-07-11 17:01:13 +0000672 // If this is specified to be in a namespace, step down into it.
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000673 if (!Namespace.empty()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000674 // If there is already a pragma handler with the name of this namespace,
675 // we either have an error (directive with the same name as a namespace) or
676 // we already have the namespace to insert into.
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000677 if (PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000678 InsertNS = Existing->getIfNamespace();
679 assert(InsertNS != 0 && "Cannot have a pragma namespace and pragma"
680 " handler with the same name!");
681 } else {
682 // Otherwise, this namespace doesn't exist yet, create and insert the
683 // handler for it.
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000684 InsertNS = new PragmaNamespace(Namespace);
Reid Spencer5f016e22007-07-11 17:01:13 +0000685 PragmaHandlers->AddPragma(InsertNS);
686 }
687 }
Mike Stump1eb44332009-09-09 15:08:12 +0000688
Reid Spencer5f016e22007-07-11 17:01:13 +0000689 // Check to make sure we don't already have a pragma for this identifier.
690 assert(!InsertNS->FindHandler(Handler->getName()) &&
691 "Pragma handler already exists for this identifier!");
692 InsertNS->AddPragma(Handler);
693}
694
Daniel Dunbar40950802008-10-04 19:17:46 +0000695/// RemovePragmaHandler - Remove the specific pragma handler from the
696/// preprocessor. If \arg Namespace is non-null, then it should be the
697/// namespace that \arg Handler was added to. It is an error to remove
698/// a handler that has not been registered.
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000699void Preprocessor::RemovePragmaHandler(llvm::StringRef Namespace,
Daniel Dunbar40950802008-10-04 19:17:46 +0000700 PragmaHandler *Handler) {
701 PragmaNamespace *NS = PragmaHandlers;
Mike Stump1eb44332009-09-09 15:08:12 +0000702
Daniel Dunbar40950802008-10-04 19:17:46 +0000703 // If this is specified to be in a namespace, step down into it.
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000704 if (!Namespace.empty()) {
705 PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace);
Daniel Dunbar40950802008-10-04 19:17:46 +0000706 assert(Existing && "Namespace containing handler does not exist!");
707
708 NS = Existing->getIfNamespace();
709 assert(NS && "Invalid namespace, registered as a regular pragma handler!");
710 }
711
712 NS->RemovePragmaHandler(Handler);
Mike Stump1eb44332009-09-09 15:08:12 +0000713
Daniel Dunbar40950802008-10-04 19:17:46 +0000714 // If this is a non-default namespace and it is now empty, remove
715 // it.
716 if (NS != PragmaHandlers && NS->IsEmpty())
717 PragmaHandlers->RemovePragmaHandler(NS);
718}
719
Peter Collingbourne9d3f5f72011-02-14 01:42:24 +0000720bool Preprocessor::LexOnOffSwitch(tok::OnOffSwitch &Result) {
721 Token Tok;
722 LexUnexpandedToken(Tok);
723
724 if (Tok.isNot(tok::identifier)) {
725 Diag(Tok, diag::ext_on_off_switch_syntax);
726 return true;
727 }
728 IdentifierInfo *II = Tok.getIdentifierInfo();
729 if (II->isStr("ON"))
730 Result = tok::OOS_ON;
731 else if (II->isStr("OFF"))
732 Result = tok::OOS_OFF;
733 else if (II->isStr("DEFAULT"))
734 Result = tok::OOS_DEFAULT;
735 else {
736 Diag(Tok, diag::ext_on_off_switch_syntax);
737 return true;
738 }
739
Peter Collingbourne84021552011-02-28 02:37:51 +0000740 // Verify that this is followed by EOD.
Peter Collingbourne9d3f5f72011-02-14 01:42:24 +0000741 LexUnexpandedToken(Tok);
Peter Collingbourne84021552011-02-28 02:37:51 +0000742 if (Tok.isNot(tok::eod))
743 Diag(Tok, diag::ext_pragma_syntax_eod);
Peter Collingbourne9d3f5f72011-02-14 01:42:24 +0000744 return false;
745}
746
Reid Spencer5f016e22007-07-11 17:01:13 +0000747namespace {
Chris Lattner22434492007-12-19 19:38:36 +0000748/// PragmaOnceHandler - "#pragma once" marks the file as atomically included.
Reid Spencer5f016e22007-07-11 17:01:13 +0000749struct PragmaOnceHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000750 PragmaOnceHandler() : PragmaHandler("once") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000751 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
752 Token &OnceTok) {
Chris Lattner35410d52009-04-14 05:07:49 +0000753 PP.CheckEndOfDirective("pragma once");
Reid Spencer5f016e22007-07-11 17:01:13 +0000754 PP.HandlePragmaOnce(OnceTok);
755 }
756};
757
Chris Lattner22434492007-12-19 19:38:36 +0000758/// PragmaMarkHandler - "#pragma mark ..." is ignored by the compiler, and the
759/// rest of the line is not lexed.
760struct PragmaMarkHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000761 PragmaMarkHandler() : PragmaHandler("mark") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000762 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
763 Token &MarkTok) {
Chris Lattner22434492007-12-19 19:38:36 +0000764 PP.HandlePragmaMark();
765 }
766};
767
768/// PragmaPoisonHandler - "#pragma poison x" marks x as not usable.
Reid Spencer5f016e22007-07-11 17:01:13 +0000769struct PragmaPoisonHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000770 PragmaPoisonHandler() : PragmaHandler("poison") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000771 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
772 Token &PoisonTok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000773 PP.HandlePragmaPoison(PoisonTok);
774 }
775};
776
Chris Lattner22434492007-12-19 19:38:36 +0000777/// PragmaSystemHeaderHandler - "#pragma system_header" marks the current file
778/// as a system header, which silences warnings in it.
Reid Spencer5f016e22007-07-11 17:01:13 +0000779struct PragmaSystemHeaderHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000780 PragmaSystemHeaderHandler() : PragmaHandler("system_header") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000781 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
782 Token &SHToken) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000783 PP.HandlePragmaSystemHeader(SHToken);
Chris Lattner35410d52009-04-14 05:07:49 +0000784 PP.CheckEndOfDirective("pragma");
Reid Spencer5f016e22007-07-11 17:01:13 +0000785 }
786};
787struct PragmaDependencyHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000788 PragmaDependencyHandler() : PragmaHandler("dependency") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000789 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
790 Token &DepToken) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000791 PP.HandlePragmaDependency(DepToken);
792 }
793};
Mike Stump1eb44332009-09-09 15:08:12 +0000794
Daniel Dunbarabf7b722010-07-28 15:40:33 +0000795struct PragmaDebugHandler : public PragmaHandler {
796 PragmaDebugHandler() : PragmaHandler("__debug") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000797 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
798 Token &DepToken) {
Daniel Dunbarabf7b722010-07-28 15:40:33 +0000799 Token Tok;
800 PP.LexUnexpandedToken(Tok);
801 if (Tok.isNot(tok::identifier)) {
Douglas Gregor6493a4d2010-08-30 15:15:34 +0000802 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Daniel Dunbarabf7b722010-07-28 15:40:33 +0000803 return;
804 }
805 IdentifierInfo *II = Tok.getIdentifierInfo();
806
Daniel Dunbar55054132010-08-17 22:32:48 +0000807 if (II->isStr("assert")) {
808 assert(0 && "This is an assertion!");
Daniel Dunbarabf7b722010-07-28 15:40:33 +0000809 } else if (II->isStr("crash")) {
Daniel Dunbar55054132010-08-17 22:32:48 +0000810 *(volatile int*) 0x11 = 0;
811 } else if (II->isStr("llvm_fatal_error")) {
812 llvm::report_fatal_error("#pragma clang __debug llvm_fatal_error");
813 } else if (II->isStr("llvm_unreachable")) {
814 llvm_unreachable("#pragma clang __debug llvm_unreachable");
815 } else if (II->isStr("overflow_stack")) {
816 DebugOverflowStack();
Daniel Dunbarff759a62010-08-18 23:09:23 +0000817 } else if (II->isStr("handle_crash")) {
818 llvm::CrashRecoveryContext *CRC =llvm::CrashRecoveryContext::GetCurrent();
819 if (CRC)
820 CRC->HandleCrash();
Daniel Dunbar55054132010-08-17 22:32:48 +0000821 } else {
822 PP.Diag(Tok, diag::warn_pragma_debug_unexpected_command)
823 << II->getName();
Daniel Dunbarabf7b722010-07-28 15:40:33 +0000824 }
825 }
826
Francois Pichet1066c6c2011-05-25 16:15:03 +0000827// Disable MSVC warning about runtime stack overflow.
828#ifdef _MSC_VER
829 #pragma warning(disable : 4717)
830#endif
Daniel Dunbarabf7b722010-07-28 15:40:33 +0000831 void DebugOverflowStack() {
832 DebugOverflowStack();
833 }
Francois Pichet1066c6c2011-05-25 16:15:03 +0000834#ifdef _MSC_VER
835 #pragma warning(default : 4717)
836#endif
837
Daniel Dunbarabf7b722010-07-28 15:40:33 +0000838};
839
Chris Lattneredaf8772009-04-19 23:16:58 +0000840/// PragmaDiagnosticHandler - e.g. '#pragma GCC diagnostic ignored "-Wformat"'
841struct PragmaDiagnosticHandler : public PragmaHandler {
Chris Lattner04ae2df2009-07-12 21:18:45 +0000842public:
Douglas Gregor6493a4d2010-08-30 15:15:34 +0000843 explicit PragmaDiagnosticHandler() : PragmaHandler("diagnostic") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000844 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
845 Token &DiagToken) {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000846 SourceLocation DiagLoc = DiagToken.getLocation();
Chris Lattneredaf8772009-04-19 23:16:58 +0000847 Token Tok;
848 PP.LexUnexpandedToken(Tok);
849 if (Tok.isNot(tok::identifier)) {
Douglas Gregor6493a4d2010-08-30 15:15:34 +0000850 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Chris Lattneredaf8772009-04-19 23:16:58 +0000851 return;
852 }
853 IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000854
Chris Lattneredaf8772009-04-19 23:16:58 +0000855 diag::Mapping Map;
856 if (II->isStr("warning"))
857 Map = diag::MAP_WARNING;
858 else if (II->isStr("error"))
859 Map = diag::MAP_ERROR;
860 else if (II->isStr("ignored"))
861 Map = diag::MAP_IGNORE;
862 else if (II->isStr("fatal"))
863 Map = diag::MAP_FATAL;
Douglas Gregor6493a4d2010-08-30 15:15:34 +0000864 else if (II->isStr("pop")) {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000865 if (!PP.getDiagnostics().popMappings(DiagLoc))
Douglas Gregor6493a4d2010-08-30 15:15:34 +0000866 PP.Diag(Tok, diag::warn_pragma_diagnostic_cannot_pop);
Chris Lattner04ae2df2009-07-12 21:18:45 +0000867
Douglas Gregor6493a4d2010-08-30 15:15:34 +0000868 return;
869 } else if (II->isStr("push")) {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000870 PP.getDiagnostics().pushMappings(DiagLoc);
Chris Lattner04ae2df2009-07-12 21:18:45 +0000871 return;
872 } else {
Douglas Gregor6493a4d2010-08-30 15:15:34 +0000873 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Chris Lattneredaf8772009-04-19 23:16:58 +0000874 return;
875 }
Mike Stump1eb44332009-09-09 15:08:12 +0000876
Chris Lattneredaf8772009-04-19 23:16:58 +0000877 PP.LexUnexpandedToken(Tok);
878
879 // We need at least one string.
880 if (Tok.isNot(tok::string_literal)) {
881 PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token);
882 return;
883 }
Mike Stump1eb44332009-09-09 15:08:12 +0000884
Chris Lattneredaf8772009-04-19 23:16:58 +0000885 // String concatenation allows multiple strings, which can even come from
886 // macro expansion.
887 // "foo " "bar" "Baz"
888 llvm::SmallVector<Token, 4> StrToks;
889 while (Tok.is(tok::string_literal)) {
890 StrToks.push_back(Tok);
891 PP.LexUnexpandedToken(Tok);
892 }
Mike Stump1eb44332009-09-09 15:08:12 +0000893
Peter Collingbourne84021552011-02-28 02:37:51 +0000894 if (Tok.isNot(tok::eod)) {
Chris Lattneredaf8772009-04-19 23:16:58 +0000895 PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token);
896 return;
897 }
Mike Stump1eb44332009-09-09 15:08:12 +0000898
Chris Lattneredaf8772009-04-19 23:16:58 +0000899 // Concatenate and parse the strings.
900 StringLiteralParser Literal(&StrToks[0], StrToks.size(), PP);
901 assert(!Literal.AnyWide && "Didn't allow wide strings in");
902 if (Literal.hadError)
903 return;
904 if (Literal.Pascal) {
Douglas Gregor6493a4d2010-08-30 15:15:34 +0000905 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Chris Lattneredaf8772009-04-19 23:16:58 +0000906 return;
907 }
Chris Lattner04ae2df2009-07-12 21:18:45 +0000908
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000909 llvm::StringRef WarningName(Literal.GetString(), Literal.GetStringLength());
Chris Lattneredaf8772009-04-19 23:16:58 +0000910
911 if (WarningName.size() < 3 || WarningName[0] != '-' ||
912 WarningName[1] != 'W') {
913 PP.Diag(StrToks[0].getLocation(),
914 diag::warn_pragma_diagnostic_invalid_option);
915 return;
916 }
Mike Stump1eb44332009-09-09 15:08:12 +0000917
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000918 if (PP.getDiagnostics().setDiagnosticGroupMapping(WarningName.substr(2),
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000919 Map, DiagLoc))
Chris Lattneredaf8772009-04-19 23:16:58 +0000920 PP.Diag(StrToks[0].getLocation(),
921 diag::warn_pragma_diagnostic_unknown_warning) << WarningName;
922 }
923};
Mike Stump1eb44332009-09-09 15:08:12 +0000924
Chris Lattner636c5ef2009-01-16 08:21:25 +0000925/// PragmaCommentHandler - "#pragma comment ...".
926struct PragmaCommentHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000927 PragmaCommentHandler() : PragmaHandler("comment") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000928 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
929 Token &CommentTok) {
Chris Lattner636c5ef2009-01-16 08:21:25 +0000930 PP.HandlePragmaComment(CommentTok);
931 }
932};
Mike Stump1eb44332009-09-09 15:08:12 +0000933
Chris Lattnerabfe0942010-06-26 17:11:39 +0000934/// PragmaMessageHandler - "#pragma message("...")".
935struct PragmaMessageHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000936 PragmaMessageHandler() : PragmaHandler("message") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000937 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
938 Token &CommentTok) {
Chris Lattnerabfe0942010-06-26 17:11:39 +0000939 PP.HandlePragmaMessage(CommentTok);
940 }
941};
942
Chris Lattnerf47724b2010-08-17 15:55:45 +0000943/// PragmaPushMacroHandler - "#pragma push_macro" saves the value of the
944/// macro on the top of the stack.
945struct PragmaPushMacroHandler : public PragmaHandler {
946 PragmaPushMacroHandler() : PragmaHandler("push_macro") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000947 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
948 Token &PushMacroTok) {
Chris Lattnerf47724b2010-08-17 15:55:45 +0000949 PP.HandlePragmaPushMacro(PushMacroTok);
950 }
951};
952
953
954/// PragmaPopMacroHandler - "#pragma pop_macro" sets the value of the
955/// macro to the value on the top of the stack.
956struct PragmaPopMacroHandler : public PragmaHandler {
957 PragmaPopMacroHandler() : PragmaHandler("pop_macro") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000958 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
959 Token &PopMacroTok) {
Chris Lattnerf47724b2010-08-17 15:55:45 +0000960 PP.HandlePragmaPopMacro(PopMacroTok);
961 }
962};
963
Chris Lattner062f2322009-04-19 21:20:35 +0000964// Pragma STDC implementations.
Chris Lattner6c5cf4a2009-04-19 21:50:08 +0000965
Chris Lattner062f2322009-04-19 21:20:35 +0000966/// PragmaSTDC_FENV_ACCESSHandler - "#pragma STDC FENV_ACCESS ...".
967struct PragmaSTDC_FENV_ACCESSHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000968 PragmaSTDC_FENV_ACCESSHandler() : PragmaHandler("FENV_ACCESS") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000969 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
970 Token &Tok) {
Peter Collingbourne9d3f5f72011-02-14 01:42:24 +0000971 tok::OnOffSwitch OOS;
972 if (PP.LexOnOffSwitch(OOS))
973 return;
974 if (OOS == tok::OOS_ON)
Chris Lattner4d8aac32009-04-19 21:55:32 +0000975 PP.Diag(Tok, diag::warn_stdc_fenv_access_not_supported);
Chris Lattner062f2322009-04-19 21:20:35 +0000976 }
977};
Mike Stump1eb44332009-09-09 15:08:12 +0000978
Chris Lattner062f2322009-04-19 21:20:35 +0000979/// PragmaSTDC_CX_LIMITED_RANGEHandler - "#pragma STDC CX_LIMITED_RANGE ...".
980struct PragmaSTDC_CX_LIMITED_RANGEHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000981 PragmaSTDC_CX_LIMITED_RANGEHandler()
982 : PragmaHandler("CX_LIMITED_RANGE") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000983 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
984 Token &Tok) {
Peter Collingbourne9d3f5f72011-02-14 01:42:24 +0000985 tok::OnOffSwitch OOS;
986 PP.LexOnOffSwitch(OOS);
Chris Lattner062f2322009-04-19 21:20:35 +0000987 }
988};
Mike Stump1eb44332009-09-09 15:08:12 +0000989
Chris Lattner062f2322009-04-19 21:20:35 +0000990/// PragmaSTDC_UnknownHandler - "#pragma STDC ...".
991struct PragmaSTDC_UnknownHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000992 PragmaSTDC_UnknownHandler() {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000993 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
994 Token &UnknownTok) {
Chris Lattner6c5cf4a2009-04-19 21:50:08 +0000995 // C99 6.10.6p2, unknown forms are not allowed.
Chris Lattnerf545be52009-04-19 21:25:37 +0000996 PP.Diag(UnknownTok, diag::ext_stdc_pragma_ignored);
Chris Lattner062f2322009-04-19 21:20:35 +0000997 }
998};
Mike Stump1eb44332009-09-09 15:08:12 +0000999
Reid Spencer5f016e22007-07-11 17:01:13 +00001000} // end anonymous namespace
1001
1002
1003/// RegisterBuiltinPragmas - Install the standard preprocessor pragmas:
1004/// #pragma GCC poison/system_header/dependency and #pragma once.
1005void Preprocessor::RegisterBuiltinPragmas() {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001006 AddPragmaHandler(new PragmaOnceHandler());
1007 AddPragmaHandler(new PragmaMarkHandler());
Chris Lattnerf47724b2010-08-17 15:55:45 +00001008 AddPragmaHandler(new PragmaPushMacroHandler());
1009 AddPragmaHandler(new PragmaPopMacroHandler());
Michael J. Spencer301669b2010-09-27 06:19:02 +00001010 AddPragmaHandler(new PragmaMessageHandler());
Mike Stump1eb44332009-09-09 15:08:12 +00001011
Chris Lattnere8fa06e2009-05-12 18:21:11 +00001012 // #pragma GCC ...
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001013 AddPragmaHandler("GCC", new PragmaPoisonHandler());
1014 AddPragmaHandler("GCC", new PragmaSystemHeaderHandler());
1015 AddPragmaHandler("GCC", new PragmaDependencyHandler());
Douglas Gregor6493a4d2010-08-30 15:15:34 +00001016 AddPragmaHandler("GCC", new PragmaDiagnosticHandler());
Chris Lattnere8fa06e2009-05-12 18:21:11 +00001017 // #pragma clang ...
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001018 AddPragmaHandler("clang", new PragmaPoisonHandler());
1019 AddPragmaHandler("clang", new PragmaSystemHeaderHandler());
Daniel Dunbarabf7b722010-07-28 15:40:33 +00001020 AddPragmaHandler("clang", new PragmaDebugHandler());
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001021 AddPragmaHandler("clang", new PragmaDependencyHandler());
Douglas Gregor6493a4d2010-08-30 15:15:34 +00001022 AddPragmaHandler("clang", new PragmaDiagnosticHandler());
Chris Lattnere8fa06e2009-05-12 18:21:11 +00001023
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001024 AddPragmaHandler("STDC", new PragmaSTDC_FENV_ACCESSHandler());
1025 AddPragmaHandler("STDC", new PragmaSTDC_CX_LIMITED_RANGEHandler());
Chris Lattner062f2322009-04-19 21:20:35 +00001026 AddPragmaHandler("STDC", new PragmaSTDC_UnknownHandler());
Mike Stump1eb44332009-09-09 15:08:12 +00001027
Chris Lattner636c5ef2009-01-16 08:21:25 +00001028 // MS extensions.
Chris Lattnerabfe0942010-06-26 17:11:39 +00001029 if (Features.Microsoft) {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001030 AddPragmaHandler(new PragmaCommentHandler());
Chris Lattnerabfe0942010-06-26 17:11:39 +00001031 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001032}