blob: 58625520a99175f70bbfec78c9e2e461b51e6c04 [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.
Chris Lattner027cff62009-06-18 05:55:53 +0000113 if (CurPPLexer && CurPPLexer->ParsingPreprocessorDirective)
Reid Spencer5f016e22007-07-11 17:01:13 +0000114 DiscardUntilEndOfDirective();
115}
116
117/// Handle_Pragma - Read a _Pragma directive, slice it up, process it, then
118/// return the first token after the directive. The _Pragma token has just
119/// been read into 'Tok'.
Chris Lattnerd2177732007-07-20 16:59:19 +0000120void Preprocessor::Handle_Pragma(Token &Tok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000121 // Remember the pragma token location.
122 SourceLocation PragmaLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000123
Reid Spencer5f016e22007-07-11 17:01:13 +0000124 // Read the '('.
125 Lex(Tok);
Chris Lattner3692b092008-11-18 07:59:24 +0000126 if (Tok.isNot(tok::l_paren)) {
127 Diag(PragmaLoc, diag::err__Pragma_malformed);
128 return;
129 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000130
131 // Read the '"..."'.
132 Lex(Tok);
Chris Lattner3692b092008-11-18 07:59:24 +0000133 if (Tok.isNot(tok::string_literal) && Tok.isNot(tok::wide_string_literal)) {
134 Diag(PragmaLoc, diag::err__Pragma_malformed);
135 return;
136 }
Mike Stump1eb44332009-09-09 15:08:12 +0000137
Reid Spencer5f016e22007-07-11 17:01:13 +0000138 // Remember the string.
139 std::string StrVal = getSpelling(Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000140
141 // Read the ')'.
142 Lex(Tok);
Chris Lattner3692b092008-11-18 07:59:24 +0000143 if (Tok.isNot(tok::r_paren)) {
144 Diag(PragmaLoc, diag::err__Pragma_malformed);
145 return;
146 }
Mike Stump1eb44332009-09-09 15:08:12 +0000147
Chris Lattnere7fb4842009-02-15 20:52:18 +0000148 SourceLocation RParenLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000149
Chris Lattnera9d91452009-01-16 18:59:23 +0000150 // The _Pragma is lexically sound. Destringize according to C99 6.10.9.1:
151 // "The string literal is destringized by deleting the L prefix, if present,
152 // deleting the leading and trailing double-quotes, replacing each escape
153 // sequence \" by a double-quote, and replacing each escape sequence \\ by a
154 // single backslash."
Reid Spencer5f016e22007-07-11 17:01:13 +0000155 if (StrVal[0] == 'L') // Remove L prefix.
156 StrVal.erase(StrVal.begin());
157 assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
158 "Invalid string token!");
Mike Stump1eb44332009-09-09 15:08:12 +0000159
Reid Spencer5f016e22007-07-11 17:01:13 +0000160 // Remove the front quote, replacing it with a space, so that the pragma
161 // contents appear to have a space before them.
162 StrVal[0] = ' ';
Mike Stump1eb44332009-09-09 15:08:12 +0000163
Chris Lattner1fa49532009-03-08 08:08:45 +0000164 // Replace the terminating quote with a \n.
Reid Spencer5f016e22007-07-11 17:01:13 +0000165 StrVal[StrVal.size()-1] = '\n';
Mike Stump1eb44332009-09-09 15:08:12 +0000166
Reid Spencer5f016e22007-07-11 17:01:13 +0000167 // Remove escaped quotes and escapes.
168 for (unsigned i = 0, e = StrVal.size(); i != e-1; ++i) {
169 if (StrVal[i] == '\\' &&
170 (StrVal[i+1] == '\\' || StrVal[i+1] == '"')) {
171 // \\ -> '\' and \" -> '"'.
172 StrVal.erase(StrVal.begin()+i);
173 --e;
174 }
175 }
John McCall1ef8a2e2010-08-28 22:34:47 +0000176
Douglas Gregor80c60f72010-09-09 22:45:38 +0000177 Handle_Pragma(PIK__Pragma, StrVal, PragmaLoc, RParenLoc);
John McCall1ef8a2e2010-08-28 22:34:47 +0000178
179 // Finally, return whatever came after the pragma directive.
180 return Lex(Tok);
181}
182
183/// HandleMicrosoft__pragma - Like Handle_Pragma except the pragma text
184/// is not enclosed within a string literal.
185void Preprocessor::HandleMicrosoft__pragma(Token &Tok) {
186 // Remember the pragma token location.
187 SourceLocation PragmaLoc = Tok.getLocation();
188
189 // Read the '('.
190 Lex(Tok);
191 if (Tok.isNot(tok::l_paren)) {
192 Diag(PragmaLoc, diag::err__Pragma_malformed);
193 return;
194 }
195
196 // Get the tokens enclosed within the __pragma().
197 llvm::SmallVector<Token, 32> PragmaToks;
198 int NumParens = 0;
199 Lex(Tok);
200 while (Tok.isNot(tok::eof)) {
201 if (Tok.is(tok::l_paren))
202 NumParens++;
203 else if (Tok.is(tok::r_paren) && NumParens-- == 0)
204 break;
205 PragmaToks.push_back(Tok);
206 Lex(Tok);
207 }
208
John McCall3da92a92010-08-29 01:09:54 +0000209 if (Tok.is(tok::eof)) {
210 Diag(PragmaLoc, diag::err_unterminated___pragma);
211 return;
212 }
213
John McCall1ef8a2e2010-08-28 22:34:47 +0000214 // Build the pragma string.
215 std::string StrVal = " ";
216 for (llvm::SmallVector<Token, 32>::iterator I =
217 PragmaToks.begin(), E = PragmaToks.end(); I != E; ++I) {
218 StrVal += getSpelling(*I);
219 }
220
221 SourceLocation RParenLoc = Tok.getLocation();
222
Douglas Gregor80c60f72010-09-09 22:45:38 +0000223 Handle_Pragma(PIK___pragma, StrVal, PragmaLoc, RParenLoc);
John McCall1ef8a2e2010-08-28 22:34:47 +0000224
225 // Finally, return whatever came after the pragma directive.
226 return Lex(Tok);
227}
228
Douglas Gregor80c60f72010-09-09 22:45:38 +0000229void Preprocessor::Handle_Pragma(unsigned Introducer,
230 const std::string &StrVal,
John McCall1ef8a2e2010-08-28 22:34:47 +0000231 SourceLocation PragmaLoc,
232 SourceLocation RParenLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +0000233
Reid Spencer5f016e22007-07-11 17:01:13 +0000234 // Plop the string (including the newline and trailing null) into a buffer
235 // where we can lex it.
Chris Lattner47246be2009-01-26 19:29:26 +0000236 Token TmpTok;
237 TmpTok.startToken();
238 CreateString(&StrVal[0], StrVal.size(), TmpTok);
239 SourceLocation TokLoc = TmpTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000240
Reid Spencer5f016e22007-07-11 17:01:13 +0000241 // Make and enter a lexer object so that we lex and expand the tokens just
242 // like any others.
Chris Lattnere7fb4842009-02-15 20:52:18 +0000243 Lexer *TL = Lexer::Create_PragmaLexer(TokLoc, PragmaLoc, RParenLoc,
Chris Lattner1fa49532009-03-08 08:08:45 +0000244 StrVal.size(), *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000245
246 EnterSourceFileWithLexer(TL, 0);
247
248 // With everything set up, lex this as a #pragma directive.
Douglas Gregor80c60f72010-09-09 22:45:38 +0000249 HandlePragmaDirective(Introducer);
Reid Spencer5f016e22007-07-11 17:01:13 +0000250}
251
252
253
254/// HandlePragmaOnce - Handle #pragma once. OnceTok is the 'once'.
255///
Chris Lattnerd2177732007-07-20 16:59:19 +0000256void Preprocessor::HandlePragmaOnce(Token &OnceTok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000257 if (isInPrimaryFile()) {
258 Diag(OnceTok, diag::pp_pragma_once_in_main_file);
259 return;
260 }
Mike Stump1eb44332009-09-09 15:08:12 +0000261
Reid Spencer5f016e22007-07-11 17:01:13 +0000262 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
Reid Spencer5f016e22007-07-11 17:01:13 +0000263 // Mark the file as a once-only file now.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000264 HeaderInfo.MarkFileIncludeOnce(getCurrentFileLexer()->getFileEntry());
Reid Spencer5f016e22007-07-11 17:01:13 +0000265}
266
Chris Lattner22434492007-12-19 19:38:36 +0000267void Preprocessor::HandlePragmaMark() {
Ted Kremenek17ff58a2008-11-19 22:21:33 +0000268 assert(CurPPLexer && "No current lexer?");
Chris Lattner6896a372009-06-15 05:02:34 +0000269 if (CurLexer)
270 CurLexer->ReadToEndOfLine();
271 else
272 CurPTHLexer->DiscardToEndOfLine();
Chris Lattner22434492007-12-19 19:38:36 +0000273}
274
275
Reid Spencer5f016e22007-07-11 17:01:13 +0000276/// HandlePragmaPoison - Handle #pragma GCC poison. PoisonTok is the 'poison'.
277///
Chris Lattnerd2177732007-07-20 16:59:19 +0000278void Preprocessor::HandlePragmaPoison(Token &PoisonTok) {
279 Token Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000280
281 while (1) {
282 // Read the next token to poison. While doing this, pretend that we are
283 // skipping while reading the identifier to poison.
284 // This avoids errors on code like:
285 // #pragma GCC poison X
286 // #pragma GCC poison X
Ted Kremenek68a91d52008-11-18 01:12:54 +0000287 if (CurPPLexer) CurPPLexer->LexingRawMode = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000288 LexUnexpandedToken(Tok);
Ted Kremenek68a91d52008-11-18 01:12:54 +0000289 if (CurPPLexer) CurPPLexer->LexingRawMode = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000290
Reid Spencer5f016e22007-07-11 17:01:13 +0000291 // If we reached the end of line, we're done.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000292 if (Tok.is(tok::eom)) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000293
Reid Spencer5f016e22007-07-11 17:01:13 +0000294 // Can only poison identifiers.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000295 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000296 Diag(Tok, diag::err_pp_invalid_poison);
297 return;
298 }
Mike Stump1eb44332009-09-09 15:08:12 +0000299
Reid Spencer5f016e22007-07-11 17:01:13 +0000300 // Look up the identifier info for the token. We disabled identifier lookup
301 // by saying we're skipping contents, so we need to do this manually.
302 IdentifierInfo *II = LookUpIdentifierInfo(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000303
Reid Spencer5f016e22007-07-11 17:01:13 +0000304 // Already poisoned.
305 if (II->isPoisoned()) continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000306
Reid Spencer5f016e22007-07-11 17:01:13 +0000307 // If this is a macro identifier, emit a warning.
Chris Lattner0edde552007-10-07 08:04:56 +0000308 if (II->hasMacroDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000309 Diag(Tok, diag::pp_poisoning_existing_macro);
Mike Stump1eb44332009-09-09 15:08:12 +0000310
Reid Spencer5f016e22007-07-11 17:01:13 +0000311 // Finally, poison it!
312 II->setIsPoisoned();
313 }
314}
315
316/// HandlePragmaSystemHeader - Implement #pragma GCC system_header. We know
317/// that the whole directive has been parsed.
Chris Lattnerd2177732007-07-20 16:59:19 +0000318void Preprocessor::HandlePragmaSystemHeader(Token &SysHeaderTok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000319 if (isInPrimaryFile()) {
320 Diag(SysHeaderTok, diag::pp_pragma_sysheader_in_main_file);
321 return;
322 }
Mike Stump1eb44332009-09-09 15:08:12 +0000323
Reid Spencer5f016e22007-07-11 17:01:13 +0000324 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
Ted Kremenek35c10c22008-11-20 01:45:11 +0000325 PreprocessorLexer *TheLexer = getCurrentFileLexer();
Mike Stump1eb44332009-09-09 15:08:12 +0000326
Reid Spencer5f016e22007-07-11 17:01:13 +0000327 // Mark the file as a system header.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000328 HeaderInfo.MarkFileSystemHeader(TheLexer->getFileEntry());
Mike Stump1eb44332009-09-09 15:08:12 +0000329
330
Chris Lattner6896a372009-06-15 05:02:34 +0000331 PresumedLoc PLoc = SourceMgr.getPresumedLoc(SysHeaderTok.getLocation());
Douglas Gregorcb7b1e12010-11-12 07:15:47 +0000332 if (PLoc.isInvalid())
333 return;
334
Chris Lattner6896a372009-06-15 05:02:34 +0000335 unsigned FilenameLen = strlen(PLoc.getFilename());
336 unsigned FilenameID = SourceMgr.getLineTableFilenameID(PLoc.getFilename(),
337 FilenameLen);
Mike Stump1eb44332009-09-09 15:08:12 +0000338
Chris Lattner6896a372009-06-15 05:02:34 +0000339 // Emit a line marker. This will change any source locations from this point
340 // forward to realize they are in a system header.
341 // Create a line note with this information.
342 SourceMgr.AddLineNote(SysHeaderTok.getLocation(), PLoc.getLine(), FilenameID,
343 false, false, true, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000344
Reid Spencer5f016e22007-07-11 17:01:13 +0000345 // Notify the client, if desired, that we are in a new source file.
346 if (Callbacks)
Ted Kremenek35c10c22008-11-20 01:45:11 +0000347 Callbacks->FileChanged(SysHeaderTok.getLocation(),
Chris Lattner0b9e7362008-09-26 21:18:42 +0000348 PPCallbacks::SystemHeaderPragma, SrcMgr::C_System);
Reid Spencer5f016e22007-07-11 17:01:13 +0000349}
350
351/// HandlePragmaDependency - Handle #pragma GCC dependency "foo" blah.
352///
Chris Lattnerd2177732007-07-20 16:59:19 +0000353void Preprocessor::HandlePragmaDependency(Token &DependencyTok) {
354 Token FilenameTok;
Ted Kremenek68a91d52008-11-18 01:12:54 +0000355 CurPPLexer->LexIncludeFilename(FilenameTok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000356
357 // If the token kind is EOM, the error has already been diagnosed.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000358 if (FilenameTok.is(tok::eom))
Reid Spencer5f016e22007-07-11 17:01:13 +0000359 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000360
Reid Spencer5f016e22007-07-11 17:01:13 +0000361 // Reserve a buffer to get the spelling.
Chris Lattnera1394812010-01-10 01:35:12 +0000362 llvm::SmallString<128> FilenameBuffer;
Douglas Gregor453091c2010-03-16 22:30:13 +0000363 bool Invalid = false;
364 llvm::StringRef Filename = getSpelling(FilenameTok, FilenameBuffer, &Invalid);
365 if (Invalid)
366 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000367
Chris Lattnera1394812010-01-10 01:35:12 +0000368 bool isAngled =
369 GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
Reid Spencer5f016e22007-07-11 17:01:13 +0000370 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
371 // error.
Chris Lattnera1394812010-01-10 01:35:12 +0000372 if (Filename.empty())
Reid Spencer5f016e22007-07-11 17:01:13 +0000373 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000374
Reid Spencer5f016e22007-07-11 17:01:13 +0000375 // Search include directories for this file.
376 const DirectoryLookup *CurDir;
Chris Lattnerf45b6462010-01-22 00:14:44 +0000377 const FileEntry *File = LookupFile(Filename, isAngled, 0, CurDir);
Chris Lattner56b05c82008-11-18 08:02:48 +0000378 if (File == 0) {
Chris Lattnera1394812010-01-10 01:35:12 +0000379 Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
Chris Lattner56b05c82008-11-18 08:02:48 +0000380 return;
381 }
Mike Stump1eb44332009-09-09 15:08:12 +0000382
Chris Lattner2b2453a2009-01-17 06:22:33 +0000383 const FileEntry *CurFile = getCurrentFileLexer()->getFileEntry();
Reid Spencer5f016e22007-07-11 17:01:13 +0000384
385 // If this file is older than the file it depends on, emit a diagnostic.
386 if (CurFile && CurFile->getModificationTime() < File->getModificationTime()) {
387 // Lex tokens at the end of the message and include them in the message.
388 std::string Message;
389 Lex(DependencyTok);
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000390 while (DependencyTok.isNot(tok::eom)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000391 Message += getSpelling(DependencyTok) + " ";
392 Lex(DependencyTok);
393 }
Mike Stump1eb44332009-09-09 15:08:12 +0000394
Chris Lattner96de2592010-09-05 23:16:09 +0000395 // Remove the trailing ' ' if present.
396 if (!Message.empty())
397 Message.erase(Message.end()-1);
Chris Lattner56b05c82008-11-18 08:02:48 +0000398 Diag(FilenameTok, diag::pp_out_of_date_dependency) << Message;
Reid Spencer5f016e22007-07-11 17:01:13 +0000399 }
400}
401
Chris Lattner636c5ef2009-01-16 08:21:25 +0000402/// HandlePragmaComment - Handle the microsoft #pragma comment extension. The
403/// syntax is:
404/// #pragma comment(linker, "foo")
405/// 'linker' is one of five identifiers: compiler, exestr, lib, linker, user.
406/// "foo" is a string, which is fully macro expanded, and permits string
Gabor Greifd7ee3492009-03-17 11:39:38 +0000407/// concatenation, embedded escape characters etc. See MSDN for more details.
Chris Lattner636c5ef2009-01-16 08:21:25 +0000408void Preprocessor::HandlePragmaComment(Token &Tok) {
409 SourceLocation CommentLoc = Tok.getLocation();
410 Lex(Tok);
411 if (Tok.isNot(tok::l_paren)) {
412 Diag(CommentLoc, diag::err_pragma_comment_malformed);
413 return;
414 }
Mike Stump1eb44332009-09-09 15:08:12 +0000415
Chris Lattner636c5ef2009-01-16 08:21:25 +0000416 // Read the identifier.
417 Lex(Tok);
418 if (Tok.isNot(tok::identifier)) {
419 Diag(CommentLoc, diag::err_pragma_comment_malformed);
420 return;
421 }
Mike Stump1eb44332009-09-09 15:08:12 +0000422
Chris Lattner636c5ef2009-01-16 08:21:25 +0000423 // Verify that this is one of the 5 whitelisted options.
424 // FIXME: warn that 'exestr' is deprecated.
425 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000426 if (!II->isStr("compiler") && !II->isStr("exestr") && !II->isStr("lib") &&
Chris Lattner636c5ef2009-01-16 08:21:25 +0000427 !II->isStr("linker") && !II->isStr("user")) {
428 Diag(Tok.getLocation(), diag::err_pragma_comment_unknown_kind);
429 return;
430 }
Mike Stump1eb44332009-09-09 15:08:12 +0000431
Chris Lattnera9d91452009-01-16 18:59:23 +0000432 // Read the optional string if present.
Chris Lattner636c5ef2009-01-16 08:21:25 +0000433 Lex(Tok);
Chris Lattnera9d91452009-01-16 18:59:23 +0000434 std::string ArgumentString;
Chris Lattner636c5ef2009-01-16 08:21:25 +0000435 if (Tok.is(tok::comma)) {
Chris Lattnera9d91452009-01-16 18:59:23 +0000436 Lex(Tok); // eat the comma.
Chris Lattner636c5ef2009-01-16 08:21:25 +0000437
438 // We need at least one string.
Chris Lattneredaf8772009-04-19 23:16:58 +0000439 if (Tok.isNot(tok::string_literal)) {
Chris Lattner636c5ef2009-01-16 08:21:25 +0000440 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
441 return;
442 }
443
444 // String concatenation allows multiple strings, which can even come from
445 // macro expansion.
446 // "foo " "bar" "Baz"
Chris Lattnera9d91452009-01-16 18:59:23 +0000447 llvm::SmallVector<Token, 4> StrToks;
Chris Lattneredaf8772009-04-19 23:16:58 +0000448 while (Tok.is(tok::string_literal)) {
Chris Lattnera9d91452009-01-16 18:59:23 +0000449 StrToks.push_back(Tok);
Chris Lattner636c5ef2009-01-16 08:21:25 +0000450 Lex(Tok);
Chris Lattnera9d91452009-01-16 18:59:23 +0000451 }
452
453 // Concatenate and parse the strings.
454 StringLiteralParser Literal(&StrToks[0], StrToks.size(), *this);
455 assert(!Literal.AnyWide && "Didn't allow wide strings in");
456 if (Literal.hadError)
457 return;
458 if (Literal.Pascal) {
459 Diag(StrToks[0].getLocation(), diag::err_pragma_comment_malformed);
460 return;
461 }
462
463 ArgumentString = std::string(Literal.GetString(),
464 Literal.GetString()+Literal.GetStringLength());
Chris Lattner636c5ef2009-01-16 08:21:25 +0000465 }
Mike Stump1eb44332009-09-09 15:08:12 +0000466
Chris Lattnera9d91452009-01-16 18:59:23 +0000467 // FIXME: If the kind is "compiler" warn if the string is present (it is
468 // ignored).
469 // FIXME: 'lib' requires a comment string.
470 // FIXME: 'linker' requires a comment string, and has a specific list of
471 // things that are allowable.
Mike Stump1eb44332009-09-09 15:08:12 +0000472
Chris Lattner636c5ef2009-01-16 08:21:25 +0000473 if (Tok.isNot(tok::r_paren)) {
474 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
475 return;
476 }
Chris Lattnera9d91452009-01-16 18:59:23 +0000477 Lex(Tok); // eat the r_paren.
Chris Lattner636c5ef2009-01-16 08:21:25 +0000478
479 if (Tok.isNot(tok::eom)) {
480 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
481 return;
482 }
Mike Stump1eb44332009-09-09 15:08:12 +0000483
Chris Lattnera9d91452009-01-16 18:59:23 +0000484 // If the pragma is lexically sound, notify any interested PPCallbacks.
Chris Lattner172e3362009-01-16 19:01:46 +0000485 if (Callbacks)
486 Callbacks->PragmaComment(CommentLoc, II, ArgumentString);
Chris Lattner636c5ef2009-01-16 08:21:25 +0000487}
488
Michael J. Spencer301669b2010-09-27 06:19:02 +0000489/// HandlePragmaMessage - Handle the microsoft and gcc #pragma message
490/// extension. The syntax is:
491/// #pragma message(string)
492/// OR, in GCC mode:
493/// #pragma message string
494/// string is a string, which is fully macro expanded, and permits string
495/// concatenation, embedded escape characters, etc... See MSDN for more details.
Chris Lattnerabfe0942010-06-26 17:11:39 +0000496void Preprocessor::HandlePragmaMessage(Token &Tok) {
497 SourceLocation MessageLoc = Tok.getLocation();
498 Lex(Tok);
Michael J. Spencer301669b2010-09-27 06:19:02 +0000499 bool ExpectClosingParen = false;
Michael J. Spencerd83fc542010-09-27 06:34:47 +0000500 switch (Tok.getKind()) {
Michael J. Spencer301669b2010-09-27 06:19:02 +0000501 case tok::l_paren:
502 // We have a MSVC style pragma message.
503 ExpectClosingParen = true;
504 // Read the string.
505 Lex(Tok);
506 break;
507 case tok::string_literal:
508 // We have a GCC style pragma message, and we just read the string.
509 break;
510 default:
Chris Lattnerabfe0942010-06-26 17:11:39 +0000511 Diag(MessageLoc, diag::err_pragma_message_malformed);
512 return;
513 }
Chris Lattner636c5ef2009-01-16 08:21:25 +0000514
Chris Lattnerabfe0942010-06-26 17:11:39 +0000515 // We need at least one string.
516 if (Tok.isNot(tok::string_literal)) {
517 Diag(Tok.getLocation(), diag::err_pragma_message_malformed);
518 return;
519 }
520
521 // String concatenation allows multiple strings, which can even come from
522 // macro expansion.
523 // "foo " "bar" "Baz"
524 llvm::SmallVector<Token, 4> StrToks;
525 while (Tok.is(tok::string_literal)) {
526 StrToks.push_back(Tok);
527 Lex(Tok);
528 }
529
530 // Concatenate and parse the strings.
531 StringLiteralParser Literal(&StrToks[0], StrToks.size(), *this);
532 assert(!Literal.AnyWide && "Didn't allow wide strings in");
533 if (Literal.hadError)
534 return;
535 if (Literal.Pascal) {
536 Diag(StrToks[0].getLocation(), diag::err_pragma_message_malformed);
537 return;
538 }
539
540 llvm::StringRef MessageString(Literal.GetString(), Literal.GetStringLength());
541
Michael J. Spencer301669b2010-09-27 06:19:02 +0000542 if (ExpectClosingParen) {
543 if (Tok.isNot(tok::r_paren)) {
544 Diag(Tok.getLocation(), diag::err_pragma_message_malformed);
545 return;
546 }
547 Lex(Tok); // eat the r_paren.
Chris Lattnerabfe0942010-06-26 17:11:39 +0000548 }
Chris Lattnerabfe0942010-06-26 17:11:39 +0000549
550 if (Tok.isNot(tok::eom)) {
551 Diag(Tok.getLocation(), diag::err_pragma_message_malformed);
552 return;
553 }
554
555 // Output the message.
556 Diag(MessageLoc, diag::warn_pragma_message) << MessageString;
557
558 // If the pragma is lexically sound, notify any interested PPCallbacks.
559 if (Callbacks)
560 Callbacks->PragmaMessage(MessageLoc, MessageString);
561}
Chris Lattner636c5ef2009-01-16 08:21:25 +0000562
Chris Lattnerf47724b2010-08-17 15:55:45 +0000563/// ParsePragmaPushOrPopMacro - Handle parsing of pragma push_macro/pop_macro.
564/// Return the IdentifierInfo* associated with the macro to push or pop.
565IdentifierInfo *Preprocessor::ParsePragmaPushOrPopMacro(Token &Tok) {
566 // Remember the pragma token location.
567 Token PragmaTok = Tok;
568
569 // Read the '('.
570 Lex(Tok);
571 if (Tok.isNot(tok::l_paren)) {
572 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
573 << getSpelling(PragmaTok);
574 return 0;
575 }
576
577 // Read the macro name string.
578 Lex(Tok);
579 if (Tok.isNot(tok::string_literal)) {
580 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
581 << getSpelling(PragmaTok);
582 return 0;
583 }
584
585 // Remember the macro string.
586 std::string StrVal = getSpelling(Tok);
587
588 // Read the ')'.
589 Lex(Tok);
590 if (Tok.isNot(tok::r_paren)) {
591 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
592 << getSpelling(PragmaTok);
593 return 0;
594 }
595
596 assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
597 "Invalid string token!");
598
599 // Create a Token from the string.
600 Token MacroTok;
601 MacroTok.startToken();
602 MacroTok.setKind(tok::identifier);
603 CreateString(&StrVal[1], StrVal.size() - 2, MacroTok);
604
605 // Get the IdentifierInfo of MacroToPushTok.
606 return LookUpIdentifierInfo(MacroTok);
607}
608
609/// HandlePragmaPushMacro - Handle #pragma push_macro.
610/// The syntax is:
611/// #pragma push_macro("macro")
612void Preprocessor::HandlePragmaPushMacro(Token &PushMacroTok) {
613 // Parse the pragma directive and get the macro IdentifierInfo*.
614 IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PushMacroTok);
615 if (!IdentInfo) return;
616
617 // Get the MacroInfo associated with IdentInfo.
618 MacroInfo *MI = getMacroInfo(IdentInfo);
619
620 MacroInfo *MacroCopyToPush = 0;
621 if (MI) {
622 // Make a clone of MI.
623 MacroCopyToPush = CloneMacroInfo(*MI);
624
625 // Allow the original MacroInfo to be redefined later.
626 MI->setIsAllowRedefinitionsWithoutWarning(true);
627 }
628
629 // Push the cloned MacroInfo so we can retrieve it later.
630 PragmaPushMacroInfo[IdentInfo].push_back(MacroCopyToPush);
631}
632
Ted Kremenekb275e3d2010-10-19 17:40:50 +0000633/// HandlePragmaPopMacro - Handle #pragma pop_macro.
Chris Lattnerf47724b2010-08-17 15:55:45 +0000634/// The syntax is:
635/// #pragma pop_macro("macro")
636void Preprocessor::HandlePragmaPopMacro(Token &PopMacroTok) {
637 SourceLocation MessageLoc = PopMacroTok.getLocation();
638
639 // Parse the pragma directive and get the macro IdentifierInfo*.
640 IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PopMacroTok);
641 if (!IdentInfo) return;
642
643 // Find the vector<MacroInfo*> associated with the macro.
644 llvm::DenseMap<IdentifierInfo*, std::vector<MacroInfo*> >::iterator iter =
645 PragmaPushMacroInfo.find(IdentInfo);
646 if (iter != PragmaPushMacroInfo.end()) {
647 // Release the MacroInfo currently associated with IdentInfo.
648 MacroInfo *CurrentMI = getMacroInfo(IdentInfo);
649 if (CurrentMI) ReleaseMacroInfo(CurrentMI);
650
651 // Get the MacroInfo we want to reinstall.
652 MacroInfo *MacroToReInstall = iter->second.back();
653
654 // Reinstall the previously pushed macro.
655 setMacroInfo(IdentInfo, MacroToReInstall);
656
657 // Pop PragmaPushMacroInfo stack.
658 iter->second.pop_back();
659 if (iter->second.size() == 0)
660 PragmaPushMacroInfo.erase(iter);
661 } else {
662 Diag(MessageLoc, diag::warn_pragma_pop_macro_no_push)
663 << IdentInfo->getName();
664 }
665}
Reid Spencer5f016e22007-07-11 17:01:13 +0000666
667/// AddPragmaHandler - Add the specified pragma handler to the preprocessor.
668/// If 'Namespace' is non-null, then it is a token required to exist on the
669/// pragma line before the pragma string starts, e.g. "STDC" or "GCC".
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000670void Preprocessor::AddPragmaHandler(llvm::StringRef Namespace,
Reid Spencer5f016e22007-07-11 17:01:13 +0000671 PragmaHandler *Handler) {
672 PragmaNamespace *InsertNS = PragmaHandlers;
Mike Stump1eb44332009-09-09 15:08:12 +0000673
Reid Spencer5f016e22007-07-11 17:01:13 +0000674 // If this is specified to be in a namespace, step down into it.
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000675 if (!Namespace.empty()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000676 // If there is already a pragma handler with the name of this namespace,
677 // we either have an error (directive with the same name as a namespace) or
678 // we already have the namespace to insert into.
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000679 if (PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000680 InsertNS = Existing->getIfNamespace();
681 assert(InsertNS != 0 && "Cannot have a pragma namespace and pragma"
682 " handler with the same name!");
683 } else {
684 // Otherwise, this namespace doesn't exist yet, create and insert the
685 // handler for it.
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000686 InsertNS = new PragmaNamespace(Namespace);
Reid Spencer5f016e22007-07-11 17:01:13 +0000687 PragmaHandlers->AddPragma(InsertNS);
688 }
689 }
Mike Stump1eb44332009-09-09 15:08:12 +0000690
Reid Spencer5f016e22007-07-11 17:01:13 +0000691 // Check to make sure we don't already have a pragma for this identifier.
692 assert(!InsertNS->FindHandler(Handler->getName()) &&
693 "Pragma handler already exists for this identifier!");
694 InsertNS->AddPragma(Handler);
695}
696
Daniel Dunbar40950802008-10-04 19:17:46 +0000697/// RemovePragmaHandler - Remove the specific pragma handler from the
698/// preprocessor. If \arg Namespace is non-null, then it should be the
699/// namespace that \arg Handler was added to. It is an error to remove
700/// a handler that has not been registered.
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000701void Preprocessor::RemovePragmaHandler(llvm::StringRef Namespace,
Daniel Dunbar40950802008-10-04 19:17:46 +0000702 PragmaHandler *Handler) {
703 PragmaNamespace *NS = PragmaHandlers;
Mike Stump1eb44332009-09-09 15:08:12 +0000704
Daniel Dunbar40950802008-10-04 19:17:46 +0000705 // If this is specified to be in a namespace, step down into it.
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000706 if (!Namespace.empty()) {
707 PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace);
Daniel Dunbar40950802008-10-04 19:17:46 +0000708 assert(Existing && "Namespace containing handler does not exist!");
709
710 NS = Existing->getIfNamespace();
711 assert(NS && "Invalid namespace, registered as a regular pragma handler!");
712 }
713
714 NS->RemovePragmaHandler(Handler);
Mike Stump1eb44332009-09-09 15:08:12 +0000715
Daniel Dunbar40950802008-10-04 19:17:46 +0000716 // If this is a non-default namespace and it is now empty, remove
717 // it.
718 if (NS != PragmaHandlers && NS->IsEmpty())
719 PragmaHandlers->RemovePragmaHandler(NS);
720}
721
Reid Spencer5f016e22007-07-11 17:01:13 +0000722namespace {
Chris Lattner22434492007-12-19 19:38:36 +0000723/// PragmaOnceHandler - "#pragma once" marks the file as atomically included.
Reid Spencer5f016e22007-07-11 17:01:13 +0000724struct PragmaOnceHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000725 PragmaOnceHandler() : PragmaHandler("once") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000726 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
727 Token &OnceTok) {
Chris Lattner35410d52009-04-14 05:07:49 +0000728 PP.CheckEndOfDirective("pragma once");
Reid Spencer5f016e22007-07-11 17:01:13 +0000729 PP.HandlePragmaOnce(OnceTok);
730 }
731};
732
Chris Lattner22434492007-12-19 19:38:36 +0000733/// PragmaMarkHandler - "#pragma mark ..." is ignored by the compiler, and the
734/// rest of the line is not lexed.
735struct PragmaMarkHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000736 PragmaMarkHandler() : PragmaHandler("mark") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000737 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
738 Token &MarkTok) {
Chris Lattner22434492007-12-19 19:38:36 +0000739 PP.HandlePragmaMark();
740 }
741};
742
743/// PragmaPoisonHandler - "#pragma poison x" marks x as not usable.
Reid Spencer5f016e22007-07-11 17:01:13 +0000744struct PragmaPoisonHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000745 PragmaPoisonHandler() : PragmaHandler("poison") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000746 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
747 Token &PoisonTok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000748 PP.HandlePragmaPoison(PoisonTok);
749 }
750};
751
Chris Lattner22434492007-12-19 19:38:36 +0000752/// PragmaSystemHeaderHandler - "#pragma system_header" marks the current file
753/// as a system header, which silences warnings in it.
Reid Spencer5f016e22007-07-11 17:01:13 +0000754struct PragmaSystemHeaderHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000755 PragmaSystemHeaderHandler() : PragmaHandler("system_header") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000756 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
757 Token &SHToken) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000758 PP.HandlePragmaSystemHeader(SHToken);
Chris Lattner35410d52009-04-14 05:07:49 +0000759 PP.CheckEndOfDirective("pragma");
Reid Spencer5f016e22007-07-11 17:01:13 +0000760 }
761};
762struct PragmaDependencyHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000763 PragmaDependencyHandler() : PragmaHandler("dependency") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000764 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
765 Token &DepToken) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000766 PP.HandlePragmaDependency(DepToken);
767 }
768};
Mike Stump1eb44332009-09-09 15:08:12 +0000769
Daniel Dunbarabf7b722010-07-28 15:40:33 +0000770struct PragmaDebugHandler : public PragmaHandler {
771 PragmaDebugHandler() : PragmaHandler("__debug") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000772 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
773 Token &DepToken) {
Daniel Dunbarabf7b722010-07-28 15:40:33 +0000774 Token Tok;
775 PP.LexUnexpandedToken(Tok);
776 if (Tok.isNot(tok::identifier)) {
Douglas Gregor6493a4d2010-08-30 15:15:34 +0000777 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Daniel Dunbarabf7b722010-07-28 15:40:33 +0000778 return;
779 }
780 IdentifierInfo *II = Tok.getIdentifierInfo();
781
Daniel Dunbar55054132010-08-17 22:32:48 +0000782 if (II->isStr("assert")) {
783 assert(0 && "This is an assertion!");
Daniel Dunbarabf7b722010-07-28 15:40:33 +0000784 } else if (II->isStr("crash")) {
Daniel Dunbar55054132010-08-17 22:32:48 +0000785 *(volatile int*) 0x11 = 0;
786 } else if (II->isStr("llvm_fatal_error")) {
787 llvm::report_fatal_error("#pragma clang __debug llvm_fatal_error");
788 } else if (II->isStr("llvm_unreachable")) {
789 llvm_unreachable("#pragma clang __debug llvm_unreachable");
790 } else if (II->isStr("overflow_stack")) {
791 DebugOverflowStack();
Daniel Dunbarff759a62010-08-18 23:09:23 +0000792 } else if (II->isStr("handle_crash")) {
793 llvm::CrashRecoveryContext *CRC =llvm::CrashRecoveryContext::GetCurrent();
794 if (CRC)
795 CRC->HandleCrash();
Daniel Dunbar55054132010-08-17 22:32:48 +0000796 } else {
797 PP.Diag(Tok, diag::warn_pragma_debug_unexpected_command)
798 << II->getName();
Daniel Dunbarabf7b722010-07-28 15:40:33 +0000799 }
800 }
801
802 void DebugOverflowStack() {
803 DebugOverflowStack();
804 }
Daniel Dunbarabf7b722010-07-28 15:40:33 +0000805};
806
Chris Lattneredaf8772009-04-19 23:16:58 +0000807/// PragmaDiagnosticHandler - e.g. '#pragma GCC diagnostic ignored "-Wformat"'
808struct PragmaDiagnosticHandler : public PragmaHandler {
Chris Lattner04ae2df2009-07-12 21:18:45 +0000809public:
Douglas Gregor6493a4d2010-08-30 15:15:34 +0000810 explicit PragmaDiagnosticHandler() : PragmaHandler("diagnostic") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000811 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
812 Token &DiagToken) {
Chris Lattneredaf8772009-04-19 23:16:58 +0000813 Token Tok;
814 PP.LexUnexpandedToken(Tok);
815 if (Tok.isNot(tok::identifier)) {
Douglas Gregor6493a4d2010-08-30 15:15:34 +0000816 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Chris Lattneredaf8772009-04-19 23:16:58 +0000817 return;
818 }
819 IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000820
Chris Lattneredaf8772009-04-19 23:16:58 +0000821 diag::Mapping Map;
822 if (II->isStr("warning"))
823 Map = diag::MAP_WARNING;
824 else if (II->isStr("error"))
825 Map = diag::MAP_ERROR;
826 else if (II->isStr("ignored"))
827 Map = diag::MAP_IGNORE;
828 else if (II->isStr("fatal"))
829 Map = diag::MAP_FATAL;
Douglas Gregor6493a4d2010-08-30 15:15:34 +0000830 else if (II->isStr("pop")) {
831 if (!PP.getDiagnostics().popMappings())
832 PP.Diag(Tok, diag::warn_pragma_diagnostic_cannot_pop);
Chris Lattner04ae2df2009-07-12 21:18:45 +0000833
Douglas Gregor6493a4d2010-08-30 15:15:34 +0000834 return;
835 } else if (II->isStr("push")) {
836 PP.getDiagnostics().pushMappings();
Chris Lattner04ae2df2009-07-12 21:18:45 +0000837 return;
838 } else {
Douglas Gregor6493a4d2010-08-30 15:15:34 +0000839 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Chris Lattneredaf8772009-04-19 23:16:58 +0000840 return;
841 }
Mike Stump1eb44332009-09-09 15:08:12 +0000842
Chris Lattneredaf8772009-04-19 23:16:58 +0000843 PP.LexUnexpandedToken(Tok);
844
845 // We need at least one string.
846 if (Tok.isNot(tok::string_literal)) {
847 PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token);
848 return;
849 }
Mike Stump1eb44332009-09-09 15:08:12 +0000850
Chris Lattneredaf8772009-04-19 23:16:58 +0000851 // String concatenation allows multiple strings, which can even come from
852 // macro expansion.
853 // "foo " "bar" "Baz"
854 llvm::SmallVector<Token, 4> StrToks;
855 while (Tok.is(tok::string_literal)) {
856 StrToks.push_back(Tok);
857 PP.LexUnexpandedToken(Tok);
858 }
Mike Stump1eb44332009-09-09 15:08:12 +0000859
Chris Lattneredaf8772009-04-19 23:16:58 +0000860 if (Tok.isNot(tok::eom)) {
861 PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token);
862 return;
863 }
Mike Stump1eb44332009-09-09 15:08:12 +0000864
Chris Lattneredaf8772009-04-19 23:16:58 +0000865 // Concatenate and parse the strings.
866 StringLiteralParser Literal(&StrToks[0], StrToks.size(), PP);
867 assert(!Literal.AnyWide && "Didn't allow wide strings in");
868 if (Literal.hadError)
869 return;
870 if (Literal.Pascal) {
Douglas Gregor6493a4d2010-08-30 15:15:34 +0000871 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Chris Lattneredaf8772009-04-19 23:16:58 +0000872 return;
873 }
Chris Lattner04ae2df2009-07-12 21:18:45 +0000874
Chris Lattneredaf8772009-04-19 23:16:58 +0000875 std::string WarningName(Literal.GetString(),
876 Literal.GetString()+Literal.GetStringLength());
877
878 if (WarningName.size() < 3 || WarningName[0] != '-' ||
879 WarningName[1] != 'W') {
880 PP.Diag(StrToks[0].getLocation(),
881 diag::warn_pragma_diagnostic_invalid_option);
882 return;
883 }
Mike Stump1eb44332009-09-09 15:08:12 +0000884
Chris Lattneredaf8772009-04-19 23:16:58 +0000885 if (PP.getDiagnostics().setDiagnosticGroupMapping(WarningName.c_str()+2,
886 Map))
887 PP.Diag(StrToks[0].getLocation(),
888 diag::warn_pragma_diagnostic_unknown_warning) << WarningName;
889 }
890};
Mike Stump1eb44332009-09-09 15:08:12 +0000891
Chris Lattner636c5ef2009-01-16 08:21:25 +0000892/// PragmaCommentHandler - "#pragma comment ...".
893struct PragmaCommentHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000894 PragmaCommentHandler() : PragmaHandler("comment") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000895 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
896 Token &CommentTok) {
Chris Lattner636c5ef2009-01-16 08:21:25 +0000897 PP.HandlePragmaComment(CommentTok);
898 }
899};
Mike Stump1eb44332009-09-09 15:08:12 +0000900
Chris Lattnerabfe0942010-06-26 17:11:39 +0000901/// PragmaMessageHandler - "#pragma message("...")".
902struct PragmaMessageHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000903 PragmaMessageHandler() : PragmaHandler("message") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000904 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
905 Token &CommentTok) {
Chris Lattnerabfe0942010-06-26 17:11:39 +0000906 PP.HandlePragmaMessage(CommentTok);
907 }
908};
909
Chris Lattnerf47724b2010-08-17 15:55:45 +0000910/// PragmaPushMacroHandler - "#pragma push_macro" saves the value of the
911/// macro on the top of the stack.
912struct PragmaPushMacroHandler : public PragmaHandler {
913 PragmaPushMacroHandler() : PragmaHandler("push_macro") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000914 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
915 Token &PushMacroTok) {
Chris Lattnerf47724b2010-08-17 15:55:45 +0000916 PP.HandlePragmaPushMacro(PushMacroTok);
917 }
918};
919
920
921/// PragmaPopMacroHandler - "#pragma pop_macro" sets the value of the
922/// macro to the value on the top of the stack.
923struct PragmaPopMacroHandler : public PragmaHandler {
924 PragmaPopMacroHandler() : PragmaHandler("pop_macro") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000925 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
926 Token &PopMacroTok) {
Chris Lattnerf47724b2010-08-17 15:55:45 +0000927 PP.HandlePragmaPopMacro(PopMacroTok);
928 }
929};
930
Chris Lattner062f2322009-04-19 21:20:35 +0000931// Pragma STDC implementations.
Chris Lattner6c5cf4a2009-04-19 21:50:08 +0000932
933enum STDCSetting {
934 STDC_ON, STDC_OFF, STDC_DEFAULT, STDC_INVALID
935};
Mike Stump1eb44332009-09-09 15:08:12 +0000936
Chris Lattner6c5cf4a2009-04-19 21:50:08 +0000937static STDCSetting LexOnOffSwitch(Preprocessor &PP) {
938 Token Tok;
939 PP.LexUnexpandedToken(Tok);
940
941 if (Tok.isNot(tok::identifier)) {
942 PP.Diag(Tok, diag::ext_stdc_pragma_syntax);
943 return STDC_INVALID;
944 }
945 IdentifierInfo *II = Tok.getIdentifierInfo();
946 STDCSetting Result;
947 if (II->isStr("ON"))
948 Result = STDC_ON;
949 else if (II->isStr("OFF"))
950 Result = STDC_OFF;
951 else if (II->isStr("DEFAULT"))
952 Result = STDC_DEFAULT;
953 else {
954 PP.Diag(Tok, diag::ext_stdc_pragma_syntax);
955 return STDC_INVALID;
956 }
957
958 // Verify that this is followed by EOM.
959 PP.LexUnexpandedToken(Tok);
960 if (Tok.isNot(tok::eom))
961 PP.Diag(Tok, diag::ext_stdc_pragma_syntax_eom);
962 return Result;
963}
Mike Stump1eb44332009-09-09 15:08:12 +0000964
Chris Lattner062f2322009-04-19 21:20:35 +0000965/// PragmaSTDC_FP_CONTRACTHandler - "#pragma STDC FP_CONTRACT ...".
966struct PragmaSTDC_FP_CONTRACTHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000967 PragmaSTDC_FP_CONTRACTHandler() : PragmaHandler("FP_CONTRACT") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000968 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
969 Token &Tok) {
Chris Lattner6c5cf4a2009-04-19 21:50:08 +0000970 // We just ignore the setting of FP_CONTRACT. Since we don't do contractions
971 // at all, our default is OFF and setting it to ON is an optimization hint
972 // we can safely ignore. When we support -ffma or something, we would need
973 // to diagnose that we are ignoring FMA.
974 LexOnOffSwitch(PP);
Chris Lattner062f2322009-04-19 21:20:35 +0000975 }
976};
Mike Stump1eb44332009-09-09 15:08:12 +0000977
Chris Lattner062f2322009-04-19 21:20:35 +0000978/// PragmaSTDC_FENV_ACCESSHandler - "#pragma STDC FENV_ACCESS ...".
979struct PragmaSTDC_FENV_ACCESSHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000980 PragmaSTDC_FENV_ACCESSHandler() : PragmaHandler("FENV_ACCESS") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000981 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
982 Token &Tok) {
Chris Lattner4d8aac32009-04-19 21:55:32 +0000983 if (LexOnOffSwitch(PP) == STDC_ON)
984 PP.Diag(Tok, diag::warn_stdc_fenv_access_not_supported);
Chris Lattner062f2322009-04-19 21:20:35 +0000985 }
986};
Mike Stump1eb44332009-09-09 15:08:12 +0000987
Chris Lattner062f2322009-04-19 21:20:35 +0000988/// PragmaSTDC_CX_LIMITED_RANGEHandler - "#pragma STDC CX_LIMITED_RANGE ...".
989struct PragmaSTDC_CX_LIMITED_RANGEHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000990 PragmaSTDC_CX_LIMITED_RANGEHandler()
991 : PragmaHandler("CX_LIMITED_RANGE") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000992 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
993 Token &Tok) {
Chris Lattner6c5cf4a2009-04-19 21:50:08 +0000994 LexOnOffSwitch(PP);
Chris Lattner062f2322009-04-19 21:20:35 +0000995 }
996};
Mike Stump1eb44332009-09-09 15:08:12 +0000997
Chris Lattner062f2322009-04-19 21:20:35 +0000998/// PragmaSTDC_UnknownHandler - "#pragma STDC ...".
999struct PragmaSTDC_UnknownHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001000 PragmaSTDC_UnknownHandler() {}
Douglas Gregor80c60f72010-09-09 22:45:38 +00001001 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1002 Token &UnknownTok) {
Chris Lattner6c5cf4a2009-04-19 21:50:08 +00001003 // C99 6.10.6p2, unknown forms are not allowed.
Chris Lattnerf545be52009-04-19 21:25:37 +00001004 PP.Diag(UnknownTok, diag::ext_stdc_pragma_ignored);
Chris Lattner062f2322009-04-19 21:20:35 +00001005 }
1006};
Mike Stump1eb44332009-09-09 15:08:12 +00001007
Reid Spencer5f016e22007-07-11 17:01:13 +00001008} // end anonymous namespace
1009
1010
1011/// RegisterBuiltinPragmas - Install the standard preprocessor pragmas:
1012/// #pragma GCC poison/system_header/dependency and #pragma once.
1013void Preprocessor::RegisterBuiltinPragmas() {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001014 AddPragmaHandler(new PragmaOnceHandler());
1015 AddPragmaHandler(new PragmaMarkHandler());
Chris Lattnerf47724b2010-08-17 15:55:45 +00001016 AddPragmaHandler(new PragmaPushMacroHandler());
1017 AddPragmaHandler(new PragmaPopMacroHandler());
Michael J. Spencer301669b2010-09-27 06:19:02 +00001018 AddPragmaHandler(new PragmaMessageHandler());
Mike Stump1eb44332009-09-09 15:08:12 +00001019
Chris Lattnere8fa06e2009-05-12 18:21:11 +00001020 // #pragma GCC ...
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001021 AddPragmaHandler("GCC", new PragmaPoisonHandler());
1022 AddPragmaHandler("GCC", new PragmaSystemHeaderHandler());
1023 AddPragmaHandler("GCC", new PragmaDependencyHandler());
Douglas Gregor6493a4d2010-08-30 15:15:34 +00001024 AddPragmaHandler("GCC", new PragmaDiagnosticHandler());
Chris Lattnere8fa06e2009-05-12 18:21:11 +00001025 // #pragma clang ...
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001026 AddPragmaHandler("clang", new PragmaPoisonHandler());
1027 AddPragmaHandler("clang", new PragmaSystemHeaderHandler());
Daniel Dunbarabf7b722010-07-28 15:40:33 +00001028 AddPragmaHandler("clang", new PragmaDebugHandler());
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001029 AddPragmaHandler("clang", new PragmaDependencyHandler());
Douglas Gregor6493a4d2010-08-30 15:15:34 +00001030 AddPragmaHandler("clang", new PragmaDiagnosticHandler());
Chris Lattnere8fa06e2009-05-12 18:21:11 +00001031
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001032 AddPragmaHandler("STDC", new PragmaSTDC_FP_CONTRACTHandler());
1033 AddPragmaHandler("STDC", new PragmaSTDC_FENV_ACCESSHandler());
1034 AddPragmaHandler("STDC", new PragmaSTDC_CX_LIMITED_RANGEHandler());
Chris Lattner062f2322009-04-19 21:20:35 +00001035 AddPragmaHandler("STDC", new PragmaSTDC_UnknownHandler());
Mike Stump1eb44332009-09-09 15:08:12 +00001036
Chris Lattner636c5ef2009-01-16 08:21:25 +00001037 // MS extensions.
Chris Lattnerabfe0942010-06-26 17:11:39 +00001038 if (Features.Microsoft) {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001039 AddPragmaHandler(new PragmaCommentHandler());
Chris Lattnerabfe0942010-06-26 17:11:39 +00001040 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001041}