blob: 8d469f609decedd0f385bd3df5d6a71a25b4a4d0 [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());
332 unsigned FilenameLen = strlen(PLoc.getFilename());
333 unsigned FilenameID = SourceMgr.getLineTableFilenameID(PLoc.getFilename(),
334 FilenameLen);
Mike Stump1eb44332009-09-09 15:08:12 +0000335
Chris Lattner6896a372009-06-15 05:02:34 +0000336 // Emit a line marker. This will change any source locations from this point
337 // forward to realize they are in a system header.
338 // Create a line note with this information.
339 SourceMgr.AddLineNote(SysHeaderTok.getLocation(), PLoc.getLine(), FilenameID,
340 false, false, true, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000341
Reid Spencer5f016e22007-07-11 17:01:13 +0000342 // Notify the client, if desired, that we are in a new source file.
343 if (Callbacks)
Ted Kremenek35c10c22008-11-20 01:45:11 +0000344 Callbacks->FileChanged(SysHeaderTok.getLocation(),
Chris Lattner0b9e7362008-09-26 21:18:42 +0000345 PPCallbacks::SystemHeaderPragma, SrcMgr::C_System);
Reid Spencer5f016e22007-07-11 17:01:13 +0000346}
347
348/// HandlePragmaDependency - Handle #pragma GCC dependency "foo" blah.
349///
Chris Lattnerd2177732007-07-20 16:59:19 +0000350void Preprocessor::HandlePragmaDependency(Token &DependencyTok) {
351 Token FilenameTok;
Ted Kremenek68a91d52008-11-18 01:12:54 +0000352 CurPPLexer->LexIncludeFilename(FilenameTok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000353
354 // If the token kind is EOM, the error has already been diagnosed.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000355 if (FilenameTok.is(tok::eom))
Reid Spencer5f016e22007-07-11 17:01:13 +0000356 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000357
Reid Spencer5f016e22007-07-11 17:01:13 +0000358 // Reserve a buffer to get the spelling.
Chris Lattnera1394812010-01-10 01:35:12 +0000359 llvm::SmallString<128> FilenameBuffer;
Douglas Gregor453091c2010-03-16 22:30:13 +0000360 bool Invalid = false;
361 llvm::StringRef Filename = getSpelling(FilenameTok, FilenameBuffer, &Invalid);
362 if (Invalid)
363 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000364
Chris Lattnera1394812010-01-10 01:35:12 +0000365 bool isAngled =
366 GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
Reid Spencer5f016e22007-07-11 17:01:13 +0000367 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
368 // error.
Chris Lattnera1394812010-01-10 01:35:12 +0000369 if (Filename.empty())
Reid Spencer5f016e22007-07-11 17:01:13 +0000370 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000371
Reid Spencer5f016e22007-07-11 17:01:13 +0000372 // Search include directories for this file.
373 const DirectoryLookup *CurDir;
Chris Lattnerf45b6462010-01-22 00:14:44 +0000374 const FileEntry *File = LookupFile(Filename, isAngled, 0, CurDir);
Chris Lattner56b05c82008-11-18 08:02:48 +0000375 if (File == 0) {
Chris Lattnera1394812010-01-10 01:35:12 +0000376 Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
Chris Lattner56b05c82008-11-18 08:02:48 +0000377 return;
378 }
Mike Stump1eb44332009-09-09 15:08:12 +0000379
Chris Lattner2b2453a2009-01-17 06:22:33 +0000380 const FileEntry *CurFile = getCurrentFileLexer()->getFileEntry();
Reid Spencer5f016e22007-07-11 17:01:13 +0000381
382 // If this file is older than the file it depends on, emit a diagnostic.
383 if (CurFile && CurFile->getModificationTime() < File->getModificationTime()) {
384 // Lex tokens at the end of the message and include them in the message.
385 std::string Message;
386 Lex(DependencyTok);
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000387 while (DependencyTok.isNot(tok::eom)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000388 Message += getSpelling(DependencyTok) + " ";
389 Lex(DependencyTok);
390 }
Mike Stump1eb44332009-09-09 15:08:12 +0000391
Chris Lattner96de2592010-09-05 23:16:09 +0000392 // Remove the trailing ' ' if present.
393 if (!Message.empty())
394 Message.erase(Message.end()-1);
Chris Lattner56b05c82008-11-18 08:02:48 +0000395 Diag(FilenameTok, diag::pp_out_of_date_dependency) << Message;
Reid Spencer5f016e22007-07-11 17:01:13 +0000396 }
397}
398
Chris Lattner636c5ef2009-01-16 08:21:25 +0000399/// HandlePragmaComment - Handle the microsoft #pragma comment extension. The
400/// syntax is:
401/// #pragma comment(linker, "foo")
402/// 'linker' is one of five identifiers: compiler, exestr, lib, linker, user.
403/// "foo" is a string, which is fully macro expanded, and permits string
Gabor Greifd7ee3492009-03-17 11:39:38 +0000404/// concatenation, embedded escape characters etc. See MSDN for more details.
Chris Lattner636c5ef2009-01-16 08:21:25 +0000405void Preprocessor::HandlePragmaComment(Token &Tok) {
406 SourceLocation CommentLoc = Tok.getLocation();
407 Lex(Tok);
408 if (Tok.isNot(tok::l_paren)) {
409 Diag(CommentLoc, diag::err_pragma_comment_malformed);
410 return;
411 }
Mike Stump1eb44332009-09-09 15:08:12 +0000412
Chris Lattner636c5ef2009-01-16 08:21:25 +0000413 // Read the identifier.
414 Lex(Tok);
415 if (Tok.isNot(tok::identifier)) {
416 Diag(CommentLoc, diag::err_pragma_comment_malformed);
417 return;
418 }
Mike Stump1eb44332009-09-09 15:08:12 +0000419
Chris Lattner636c5ef2009-01-16 08:21:25 +0000420 // Verify that this is one of the 5 whitelisted options.
421 // FIXME: warn that 'exestr' is deprecated.
422 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000423 if (!II->isStr("compiler") && !II->isStr("exestr") && !II->isStr("lib") &&
Chris Lattner636c5ef2009-01-16 08:21:25 +0000424 !II->isStr("linker") && !II->isStr("user")) {
425 Diag(Tok.getLocation(), diag::err_pragma_comment_unknown_kind);
426 return;
427 }
Mike Stump1eb44332009-09-09 15:08:12 +0000428
Chris Lattnera9d91452009-01-16 18:59:23 +0000429 // Read the optional string if present.
Chris Lattner636c5ef2009-01-16 08:21:25 +0000430 Lex(Tok);
Chris Lattnera9d91452009-01-16 18:59:23 +0000431 std::string ArgumentString;
Chris Lattner636c5ef2009-01-16 08:21:25 +0000432 if (Tok.is(tok::comma)) {
Chris Lattnera9d91452009-01-16 18:59:23 +0000433 Lex(Tok); // eat the comma.
Chris Lattner636c5ef2009-01-16 08:21:25 +0000434
435 // We need at least one string.
Chris Lattneredaf8772009-04-19 23:16:58 +0000436 if (Tok.isNot(tok::string_literal)) {
Chris Lattner636c5ef2009-01-16 08:21:25 +0000437 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
438 return;
439 }
440
441 // String concatenation allows multiple strings, which can even come from
442 // macro expansion.
443 // "foo " "bar" "Baz"
Chris Lattnera9d91452009-01-16 18:59:23 +0000444 llvm::SmallVector<Token, 4> StrToks;
Chris Lattneredaf8772009-04-19 23:16:58 +0000445 while (Tok.is(tok::string_literal)) {
Chris Lattnera9d91452009-01-16 18:59:23 +0000446 StrToks.push_back(Tok);
Chris Lattner636c5ef2009-01-16 08:21:25 +0000447 Lex(Tok);
Chris Lattnera9d91452009-01-16 18:59:23 +0000448 }
449
450 // Concatenate and parse the strings.
451 StringLiteralParser Literal(&StrToks[0], StrToks.size(), *this);
452 assert(!Literal.AnyWide && "Didn't allow wide strings in");
453 if (Literal.hadError)
454 return;
455 if (Literal.Pascal) {
456 Diag(StrToks[0].getLocation(), diag::err_pragma_comment_malformed);
457 return;
458 }
459
460 ArgumentString = std::string(Literal.GetString(),
461 Literal.GetString()+Literal.GetStringLength());
Chris Lattner636c5ef2009-01-16 08:21:25 +0000462 }
Mike Stump1eb44332009-09-09 15:08:12 +0000463
Chris Lattnera9d91452009-01-16 18:59:23 +0000464 // FIXME: If the kind is "compiler" warn if the string is present (it is
465 // ignored).
466 // FIXME: 'lib' requires a comment string.
467 // FIXME: 'linker' requires a comment string, and has a specific list of
468 // things that are allowable.
Mike Stump1eb44332009-09-09 15:08:12 +0000469
Chris Lattner636c5ef2009-01-16 08:21:25 +0000470 if (Tok.isNot(tok::r_paren)) {
471 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
472 return;
473 }
Chris Lattnera9d91452009-01-16 18:59:23 +0000474 Lex(Tok); // eat the r_paren.
Chris Lattner636c5ef2009-01-16 08:21:25 +0000475
476 if (Tok.isNot(tok::eom)) {
477 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
478 return;
479 }
Mike Stump1eb44332009-09-09 15:08:12 +0000480
Chris Lattnera9d91452009-01-16 18:59:23 +0000481 // If the pragma is lexically sound, notify any interested PPCallbacks.
Chris Lattner172e3362009-01-16 19:01:46 +0000482 if (Callbacks)
483 Callbacks->PragmaComment(CommentLoc, II, ArgumentString);
Chris Lattner636c5ef2009-01-16 08:21:25 +0000484}
485
Michael J. Spencer301669b2010-09-27 06:19:02 +0000486/// HandlePragmaMessage - Handle the microsoft and gcc #pragma message
487/// extension. The syntax is:
488/// #pragma message(string)
489/// OR, in GCC mode:
490/// #pragma message string
491/// string is a string, which is fully macro expanded, and permits string
492/// concatenation, embedded escape characters, etc... See MSDN for more details.
Chris Lattnerabfe0942010-06-26 17:11:39 +0000493void Preprocessor::HandlePragmaMessage(Token &Tok) {
494 SourceLocation MessageLoc = Tok.getLocation();
495 Lex(Tok);
Michael J. Spencer301669b2010-09-27 06:19:02 +0000496 bool ExpectClosingParen = false;
Michael J. Spencerd83fc542010-09-27 06:34:47 +0000497 switch (Tok.getKind()) {
Michael J. Spencer301669b2010-09-27 06:19:02 +0000498 case tok::l_paren:
499 // We have a MSVC style pragma message.
500 ExpectClosingParen = true;
501 // Read the string.
502 Lex(Tok);
503 break;
504 case tok::string_literal:
505 // We have a GCC style pragma message, and we just read the string.
506 break;
507 default:
Chris Lattnerabfe0942010-06-26 17:11:39 +0000508 Diag(MessageLoc, diag::err_pragma_message_malformed);
509 return;
510 }
Chris Lattner636c5ef2009-01-16 08:21:25 +0000511
Chris Lattnerabfe0942010-06-26 17:11:39 +0000512 // We need at least one string.
513 if (Tok.isNot(tok::string_literal)) {
514 Diag(Tok.getLocation(), diag::err_pragma_message_malformed);
515 return;
516 }
517
518 // String concatenation allows multiple strings, which can even come from
519 // macro expansion.
520 // "foo " "bar" "Baz"
521 llvm::SmallVector<Token, 4> StrToks;
522 while (Tok.is(tok::string_literal)) {
523 StrToks.push_back(Tok);
524 Lex(Tok);
525 }
526
527 // Concatenate and parse the strings.
528 StringLiteralParser Literal(&StrToks[0], StrToks.size(), *this);
529 assert(!Literal.AnyWide && "Didn't allow wide strings in");
530 if (Literal.hadError)
531 return;
532 if (Literal.Pascal) {
533 Diag(StrToks[0].getLocation(), diag::err_pragma_message_malformed);
534 return;
535 }
536
537 llvm::StringRef MessageString(Literal.GetString(), Literal.GetStringLength());
538
Michael J. Spencer301669b2010-09-27 06:19:02 +0000539 if (ExpectClosingParen) {
540 if (Tok.isNot(tok::r_paren)) {
541 Diag(Tok.getLocation(), diag::err_pragma_message_malformed);
542 return;
543 }
544 Lex(Tok); // eat the r_paren.
Chris Lattnerabfe0942010-06-26 17:11:39 +0000545 }
Chris Lattnerabfe0942010-06-26 17:11:39 +0000546
547 if (Tok.isNot(tok::eom)) {
548 Diag(Tok.getLocation(), diag::err_pragma_message_malformed);
549 return;
550 }
551
552 // Output the message.
553 Diag(MessageLoc, diag::warn_pragma_message) << MessageString;
554
555 // If the pragma is lexically sound, notify any interested PPCallbacks.
556 if (Callbacks)
557 Callbacks->PragmaMessage(MessageLoc, MessageString);
558}
Chris Lattner636c5ef2009-01-16 08:21:25 +0000559
Chris Lattnerf47724b2010-08-17 15:55:45 +0000560/// ParsePragmaPushOrPopMacro - Handle parsing of pragma push_macro/pop_macro.
561/// Return the IdentifierInfo* associated with the macro to push or pop.
562IdentifierInfo *Preprocessor::ParsePragmaPushOrPopMacro(Token &Tok) {
563 // Remember the pragma token location.
564 Token PragmaTok = Tok;
565
566 // Read the '('.
567 Lex(Tok);
568 if (Tok.isNot(tok::l_paren)) {
569 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
570 << getSpelling(PragmaTok);
571 return 0;
572 }
573
574 // Read the macro name string.
575 Lex(Tok);
576 if (Tok.isNot(tok::string_literal)) {
577 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
578 << getSpelling(PragmaTok);
579 return 0;
580 }
581
582 // Remember the macro string.
583 std::string StrVal = getSpelling(Tok);
584
585 // Read the ')'.
586 Lex(Tok);
587 if (Tok.isNot(tok::r_paren)) {
588 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
589 << getSpelling(PragmaTok);
590 return 0;
591 }
592
593 assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
594 "Invalid string token!");
595
596 // Create a Token from the string.
597 Token MacroTok;
598 MacroTok.startToken();
599 MacroTok.setKind(tok::identifier);
600 CreateString(&StrVal[1], StrVal.size() - 2, MacroTok);
601
602 // Get the IdentifierInfo of MacroToPushTok.
603 return LookUpIdentifierInfo(MacroTok);
604}
605
606/// HandlePragmaPushMacro - Handle #pragma push_macro.
607/// The syntax is:
608/// #pragma push_macro("macro")
609void Preprocessor::HandlePragmaPushMacro(Token &PushMacroTok) {
610 // Parse the pragma directive and get the macro IdentifierInfo*.
611 IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PushMacroTok);
612 if (!IdentInfo) return;
613
614 // Get the MacroInfo associated with IdentInfo.
615 MacroInfo *MI = getMacroInfo(IdentInfo);
616
617 MacroInfo *MacroCopyToPush = 0;
618 if (MI) {
619 // Make a clone of MI.
620 MacroCopyToPush = CloneMacroInfo(*MI);
621
622 // Allow the original MacroInfo to be redefined later.
623 MI->setIsAllowRedefinitionsWithoutWarning(true);
624 }
625
626 // Push the cloned MacroInfo so we can retrieve it later.
627 PragmaPushMacroInfo[IdentInfo].push_back(MacroCopyToPush);
628}
629
Ted Kremenekb275e3d2010-10-19 17:40:50 +0000630/// HandlePragmaPopMacro - Handle #pragma pop_macro.
Chris Lattnerf47724b2010-08-17 15:55:45 +0000631/// The syntax is:
632/// #pragma pop_macro("macro")
633void Preprocessor::HandlePragmaPopMacro(Token &PopMacroTok) {
634 SourceLocation MessageLoc = PopMacroTok.getLocation();
635
636 // Parse the pragma directive and get the macro IdentifierInfo*.
637 IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PopMacroTok);
638 if (!IdentInfo) return;
639
640 // Find the vector<MacroInfo*> associated with the macro.
641 llvm::DenseMap<IdentifierInfo*, std::vector<MacroInfo*> >::iterator iter =
642 PragmaPushMacroInfo.find(IdentInfo);
643 if (iter != PragmaPushMacroInfo.end()) {
644 // Release the MacroInfo currently associated with IdentInfo.
645 MacroInfo *CurrentMI = getMacroInfo(IdentInfo);
646 if (CurrentMI) ReleaseMacroInfo(CurrentMI);
647
648 // Get the MacroInfo we want to reinstall.
649 MacroInfo *MacroToReInstall = iter->second.back();
650
651 // Reinstall the previously pushed macro.
652 setMacroInfo(IdentInfo, MacroToReInstall);
653
654 // Pop PragmaPushMacroInfo stack.
655 iter->second.pop_back();
656 if (iter->second.size() == 0)
657 PragmaPushMacroInfo.erase(iter);
658 } else {
659 Diag(MessageLoc, diag::warn_pragma_pop_macro_no_push)
660 << IdentInfo->getName();
661 }
662}
Reid Spencer5f016e22007-07-11 17:01:13 +0000663
664/// AddPragmaHandler - Add the specified pragma handler to the preprocessor.
665/// If 'Namespace' is non-null, then it is a token required to exist on the
666/// pragma line before the pragma string starts, e.g. "STDC" or "GCC".
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000667void Preprocessor::AddPragmaHandler(llvm::StringRef Namespace,
Reid Spencer5f016e22007-07-11 17:01:13 +0000668 PragmaHandler *Handler) {
669 PragmaNamespace *InsertNS = PragmaHandlers;
Mike Stump1eb44332009-09-09 15:08:12 +0000670
Reid Spencer5f016e22007-07-11 17:01:13 +0000671 // If this is specified to be in a namespace, step down into it.
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000672 if (!Namespace.empty()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000673 // If there is already a pragma handler with the name of this namespace,
674 // we either have an error (directive with the same name as a namespace) or
675 // we already have the namespace to insert into.
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000676 if (PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000677 InsertNS = Existing->getIfNamespace();
678 assert(InsertNS != 0 && "Cannot have a pragma namespace and pragma"
679 " handler with the same name!");
680 } else {
681 // Otherwise, this namespace doesn't exist yet, create and insert the
682 // handler for it.
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000683 InsertNS = new PragmaNamespace(Namespace);
Reid Spencer5f016e22007-07-11 17:01:13 +0000684 PragmaHandlers->AddPragma(InsertNS);
685 }
686 }
Mike Stump1eb44332009-09-09 15:08:12 +0000687
Reid Spencer5f016e22007-07-11 17:01:13 +0000688 // Check to make sure we don't already have a pragma for this identifier.
689 assert(!InsertNS->FindHandler(Handler->getName()) &&
690 "Pragma handler already exists for this identifier!");
691 InsertNS->AddPragma(Handler);
692}
693
Daniel Dunbar40950802008-10-04 19:17:46 +0000694/// RemovePragmaHandler - Remove the specific pragma handler from the
695/// preprocessor. If \arg Namespace is non-null, then it should be the
696/// namespace that \arg Handler was added to. It is an error to remove
697/// a handler that has not been registered.
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000698void Preprocessor::RemovePragmaHandler(llvm::StringRef Namespace,
Daniel Dunbar40950802008-10-04 19:17:46 +0000699 PragmaHandler *Handler) {
700 PragmaNamespace *NS = PragmaHandlers;
Mike Stump1eb44332009-09-09 15:08:12 +0000701
Daniel Dunbar40950802008-10-04 19:17:46 +0000702 // If this is specified to be in a namespace, step down into it.
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000703 if (!Namespace.empty()) {
704 PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace);
Daniel Dunbar40950802008-10-04 19:17:46 +0000705 assert(Existing && "Namespace containing handler does not exist!");
706
707 NS = Existing->getIfNamespace();
708 assert(NS && "Invalid namespace, registered as a regular pragma handler!");
709 }
710
711 NS->RemovePragmaHandler(Handler);
Mike Stump1eb44332009-09-09 15:08:12 +0000712
Daniel Dunbar40950802008-10-04 19:17:46 +0000713 // If this is a non-default namespace and it is now empty, remove
714 // it.
715 if (NS != PragmaHandlers && NS->IsEmpty())
716 PragmaHandlers->RemovePragmaHandler(NS);
717}
718
Reid Spencer5f016e22007-07-11 17:01:13 +0000719namespace {
Chris Lattner22434492007-12-19 19:38:36 +0000720/// PragmaOnceHandler - "#pragma once" marks the file as atomically included.
Reid Spencer5f016e22007-07-11 17:01:13 +0000721struct PragmaOnceHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000722 PragmaOnceHandler() : PragmaHandler("once") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000723 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
724 Token &OnceTok) {
Chris Lattner35410d52009-04-14 05:07:49 +0000725 PP.CheckEndOfDirective("pragma once");
Reid Spencer5f016e22007-07-11 17:01:13 +0000726 PP.HandlePragmaOnce(OnceTok);
727 }
728};
729
Chris Lattner22434492007-12-19 19:38:36 +0000730/// PragmaMarkHandler - "#pragma mark ..." is ignored by the compiler, and the
731/// rest of the line is not lexed.
732struct PragmaMarkHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000733 PragmaMarkHandler() : PragmaHandler("mark") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000734 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
735 Token &MarkTok) {
Chris Lattner22434492007-12-19 19:38:36 +0000736 PP.HandlePragmaMark();
737 }
738};
739
740/// PragmaPoisonHandler - "#pragma poison x" marks x as not usable.
Reid Spencer5f016e22007-07-11 17:01:13 +0000741struct PragmaPoisonHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000742 PragmaPoisonHandler() : PragmaHandler("poison") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000743 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
744 Token &PoisonTok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000745 PP.HandlePragmaPoison(PoisonTok);
746 }
747};
748
Chris Lattner22434492007-12-19 19:38:36 +0000749/// PragmaSystemHeaderHandler - "#pragma system_header" marks the current file
750/// as a system header, which silences warnings in it.
Reid Spencer5f016e22007-07-11 17:01:13 +0000751struct PragmaSystemHeaderHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000752 PragmaSystemHeaderHandler() : PragmaHandler("system_header") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000753 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
754 Token &SHToken) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000755 PP.HandlePragmaSystemHeader(SHToken);
Chris Lattner35410d52009-04-14 05:07:49 +0000756 PP.CheckEndOfDirective("pragma");
Reid Spencer5f016e22007-07-11 17:01:13 +0000757 }
758};
759struct PragmaDependencyHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000760 PragmaDependencyHandler() : PragmaHandler("dependency") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000761 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
762 Token &DepToken) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000763 PP.HandlePragmaDependency(DepToken);
764 }
765};
Mike Stump1eb44332009-09-09 15:08:12 +0000766
Daniel Dunbarabf7b722010-07-28 15:40:33 +0000767struct PragmaDebugHandler : public PragmaHandler {
768 PragmaDebugHandler() : PragmaHandler("__debug") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000769 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
770 Token &DepToken) {
Daniel Dunbarabf7b722010-07-28 15:40:33 +0000771 Token Tok;
772 PP.LexUnexpandedToken(Tok);
773 if (Tok.isNot(tok::identifier)) {
Douglas Gregor6493a4d2010-08-30 15:15:34 +0000774 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Daniel Dunbarabf7b722010-07-28 15:40:33 +0000775 return;
776 }
777 IdentifierInfo *II = Tok.getIdentifierInfo();
778
Daniel Dunbar55054132010-08-17 22:32:48 +0000779 if (II->isStr("assert")) {
780 assert(0 && "This is an assertion!");
Daniel Dunbarabf7b722010-07-28 15:40:33 +0000781 } else if (II->isStr("crash")) {
Daniel Dunbar55054132010-08-17 22:32:48 +0000782 *(volatile int*) 0x11 = 0;
783 } else if (II->isStr("llvm_fatal_error")) {
784 llvm::report_fatal_error("#pragma clang __debug llvm_fatal_error");
785 } else if (II->isStr("llvm_unreachable")) {
786 llvm_unreachable("#pragma clang __debug llvm_unreachable");
787 } else if (II->isStr("overflow_stack")) {
788 DebugOverflowStack();
Daniel Dunbarff759a62010-08-18 23:09:23 +0000789 } else if (II->isStr("handle_crash")) {
790 llvm::CrashRecoveryContext *CRC =llvm::CrashRecoveryContext::GetCurrent();
791 if (CRC)
792 CRC->HandleCrash();
Daniel Dunbar55054132010-08-17 22:32:48 +0000793 } else {
794 PP.Diag(Tok, diag::warn_pragma_debug_unexpected_command)
795 << II->getName();
Daniel Dunbarabf7b722010-07-28 15:40:33 +0000796 }
797 }
798
799 void DebugOverflowStack() {
800 DebugOverflowStack();
801 }
Daniel Dunbarabf7b722010-07-28 15:40:33 +0000802};
803
Chris Lattneredaf8772009-04-19 23:16:58 +0000804/// PragmaDiagnosticHandler - e.g. '#pragma GCC diagnostic ignored "-Wformat"'
805struct PragmaDiagnosticHandler : public PragmaHandler {
Chris Lattner04ae2df2009-07-12 21:18:45 +0000806public:
Douglas Gregor6493a4d2010-08-30 15:15:34 +0000807 explicit PragmaDiagnosticHandler() : PragmaHandler("diagnostic") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000808 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
809 Token &DiagToken) {
Chris Lattneredaf8772009-04-19 23:16:58 +0000810 Token Tok;
811 PP.LexUnexpandedToken(Tok);
812 if (Tok.isNot(tok::identifier)) {
Douglas Gregor6493a4d2010-08-30 15:15:34 +0000813 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Chris Lattneredaf8772009-04-19 23:16:58 +0000814 return;
815 }
816 IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000817
Chris Lattneredaf8772009-04-19 23:16:58 +0000818 diag::Mapping Map;
819 if (II->isStr("warning"))
820 Map = diag::MAP_WARNING;
821 else if (II->isStr("error"))
822 Map = diag::MAP_ERROR;
823 else if (II->isStr("ignored"))
824 Map = diag::MAP_IGNORE;
825 else if (II->isStr("fatal"))
826 Map = diag::MAP_FATAL;
Douglas Gregor6493a4d2010-08-30 15:15:34 +0000827 else if (II->isStr("pop")) {
828 if (!PP.getDiagnostics().popMappings())
829 PP.Diag(Tok, diag::warn_pragma_diagnostic_cannot_pop);
Chris Lattner04ae2df2009-07-12 21:18:45 +0000830
Douglas Gregor6493a4d2010-08-30 15:15:34 +0000831 return;
832 } else if (II->isStr("push")) {
833 PP.getDiagnostics().pushMappings();
Chris Lattner04ae2df2009-07-12 21:18:45 +0000834 return;
835 } else {
Douglas Gregor6493a4d2010-08-30 15:15:34 +0000836 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Chris Lattneredaf8772009-04-19 23:16:58 +0000837 return;
838 }
Mike Stump1eb44332009-09-09 15:08:12 +0000839
Chris Lattneredaf8772009-04-19 23:16:58 +0000840 PP.LexUnexpandedToken(Tok);
841
842 // We need at least one string.
843 if (Tok.isNot(tok::string_literal)) {
844 PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token);
845 return;
846 }
Mike Stump1eb44332009-09-09 15:08:12 +0000847
Chris Lattneredaf8772009-04-19 23:16:58 +0000848 // String concatenation allows multiple strings, which can even come from
849 // macro expansion.
850 // "foo " "bar" "Baz"
851 llvm::SmallVector<Token, 4> StrToks;
852 while (Tok.is(tok::string_literal)) {
853 StrToks.push_back(Tok);
854 PP.LexUnexpandedToken(Tok);
855 }
Mike Stump1eb44332009-09-09 15:08:12 +0000856
Chris Lattneredaf8772009-04-19 23:16:58 +0000857 if (Tok.isNot(tok::eom)) {
858 PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token);
859 return;
860 }
Mike Stump1eb44332009-09-09 15:08:12 +0000861
Chris Lattneredaf8772009-04-19 23:16:58 +0000862 // Concatenate and parse the strings.
863 StringLiteralParser Literal(&StrToks[0], StrToks.size(), PP);
864 assert(!Literal.AnyWide && "Didn't allow wide strings in");
865 if (Literal.hadError)
866 return;
867 if (Literal.Pascal) {
Douglas Gregor6493a4d2010-08-30 15:15:34 +0000868 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Chris Lattneredaf8772009-04-19 23:16:58 +0000869 return;
870 }
Chris Lattner04ae2df2009-07-12 21:18:45 +0000871
Chris Lattneredaf8772009-04-19 23:16:58 +0000872 std::string WarningName(Literal.GetString(),
873 Literal.GetString()+Literal.GetStringLength());
874
875 if (WarningName.size() < 3 || WarningName[0] != '-' ||
876 WarningName[1] != 'W') {
877 PP.Diag(StrToks[0].getLocation(),
878 diag::warn_pragma_diagnostic_invalid_option);
879 return;
880 }
Mike Stump1eb44332009-09-09 15:08:12 +0000881
Chris Lattneredaf8772009-04-19 23:16:58 +0000882 if (PP.getDiagnostics().setDiagnosticGroupMapping(WarningName.c_str()+2,
883 Map))
884 PP.Diag(StrToks[0].getLocation(),
885 diag::warn_pragma_diagnostic_unknown_warning) << WarningName;
886 }
887};
Mike Stump1eb44332009-09-09 15:08:12 +0000888
Chris Lattner636c5ef2009-01-16 08:21:25 +0000889/// PragmaCommentHandler - "#pragma comment ...".
890struct PragmaCommentHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000891 PragmaCommentHandler() : PragmaHandler("comment") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000892 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
893 Token &CommentTok) {
Chris Lattner636c5ef2009-01-16 08:21:25 +0000894 PP.HandlePragmaComment(CommentTok);
895 }
896};
Mike Stump1eb44332009-09-09 15:08:12 +0000897
Chris Lattnerabfe0942010-06-26 17:11:39 +0000898/// PragmaMessageHandler - "#pragma message("...")".
899struct PragmaMessageHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000900 PragmaMessageHandler() : PragmaHandler("message") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000901 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
902 Token &CommentTok) {
Chris Lattnerabfe0942010-06-26 17:11:39 +0000903 PP.HandlePragmaMessage(CommentTok);
904 }
905};
906
Chris Lattnerf47724b2010-08-17 15:55:45 +0000907/// PragmaPushMacroHandler - "#pragma push_macro" saves the value of the
908/// macro on the top of the stack.
909struct PragmaPushMacroHandler : public PragmaHandler {
910 PragmaPushMacroHandler() : PragmaHandler("push_macro") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000911 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
912 Token &PushMacroTok) {
Chris Lattnerf47724b2010-08-17 15:55:45 +0000913 PP.HandlePragmaPushMacro(PushMacroTok);
914 }
915};
916
917
918/// PragmaPopMacroHandler - "#pragma pop_macro" sets the value of the
919/// macro to the value on the top of the stack.
920struct PragmaPopMacroHandler : public PragmaHandler {
921 PragmaPopMacroHandler() : PragmaHandler("pop_macro") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000922 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
923 Token &PopMacroTok) {
Chris Lattnerf47724b2010-08-17 15:55:45 +0000924 PP.HandlePragmaPopMacro(PopMacroTok);
925 }
926};
927
Chris Lattner062f2322009-04-19 21:20:35 +0000928// Pragma STDC implementations.
Chris Lattner6c5cf4a2009-04-19 21:50:08 +0000929
930enum STDCSetting {
931 STDC_ON, STDC_OFF, STDC_DEFAULT, STDC_INVALID
932};
Mike Stump1eb44332009-09-09 15:08:12 +0000933
Chris Lattner6c5cf4a2009-04-19 21:50:08 +0000934static STDCSetting LexOnOffSwitch(Preprocessor &PP) {
935 Token Tok;
936 PP.LexUnexpandedToken(Tok);
937
938 if (Tok.isNot(tok::identifier)) {
939 PP.Diag(Tok, diag::ext_stdc_pragma_syntax);
940 return STDC_INVALID;
941 }
942 IdentifierInfo *II = Tok.getIdentifierInfo();
943 STDCSetting Result;
944 if (II->isStr("ON"))
945 Result = STDC_ON;
946 else if (II->isStr("OFF"))
947 Result = STDC_OFF;
948 else if (II->isStr("DEFAULT"))
949 Result = STDC_DEFAULT;
950 else {
951 PP.Diag(Tok, diag::ext_stdc_pragma_syntax);
952 return STDC_INVALID;
953 }
954
955 // Verify that this is followed by EOM.
956 PP.LexUnexpandedToken(Tok);
957 if (Tok.isNot(tok::eom))
958 PP.Diag(Tok, diag::ext_stdc_pragma_syntax_eom);
959 return Result;
960}
Mike Stump1eb44332009-09-09 15:08:12 +0000961
Chris Lattner062f2322009-04-19 21:20:35 +0000962/// PragmaSTDC_FP_CONTRACTHandler - "#pragma STDC FP_CONTRACT ...".
963struct PragmaSTDC_FP_CONTRACTHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000964 PragmaSTDC_FP_CONTRACTHandler() : PragmaHandler("FP_CONTRACT") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000965 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
966 Token &Tok) {
Chris Lattner6c5cf4a2009-04-19 21:50:08 +0000967 // We just ignore the setting of FP_CONTRACT. Since we don't do contractions
968 // at all, our default is OFF and setting it to ON is an optimization hint
969 // we can safely ignore. When we support -ffma or something, we would need
970 // to diagnose that we are ignoring FMA.
971 LexOnOffSwitch(PP);
Chris Lattner062f2322009-04-19 21:20:35 +0000972 }
973};
Mike Stump1eb44332009-09-09 15:08:12 +0000974
Chris Lattner062f2322009-04-19 21:20:35 +0000975/// PragmaSTDC_FENV_ACCESSHandler - "#pragma STDC FENV_ACCESS ...".
976struct PragmaSTDC_FENV_ACCESSHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000977 PragmaSTDC_FENV_ACCESSHandler() : PragmaHandler("FENV_ACCESS") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000978 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
979 Token &Tok) {
Chris Lattner4d8aac32009-04-19 21:55:32 +0000980 if (LexOnOffSwitch(PP) == STDC_ON)
981 PP.Diag(Tok, diag::warn_stdc_fenv_access_not_supported);
Chris Lattner062f2322009-04-19 21:20:35 +0000982 }
983};
Mike Stump1eb44332009-09-09 15:08:12 +0000984
Chris Lattner062f2322009-04-19 21:20:35 +0000985/// PragmaSTDC_CX_LIMITED_RANGEHandler - "#pragma STDC CX_LIMITED_RANGE ...".
986struct PragmaSTDC_CX_LIMITED_RANGEHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000987 PragmaSTDC_CX_LIMITED_RANGEHandler()
988 : PragmaHandler("CX_LIMITED_RANGE") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000989 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
990 Token &Tok) {
Chris Lattner6c5cf4a2009-04-19 21:50:08 +0000991 LexOnOffSwitch(PP);
Chris Lattner062f2322009-04-19 21:20:35 +0000992 }
993};
Mike Stump1eb44332009-09-09 15:08:12 +0000994
Chris Lattner062f2322009-04-19 21:20:35 +0000995/// PragmaSTDC_UnknownHandler - "#pragma STDC ...".
996struct PragmaSTDC_UnknownHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000997 PragmaSTDC_UnknownHandler() {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000998 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
999 Token &UnknownTok) {
Chris Lattner6c5cf4a2009-04-19 21:50:08 +00001000 // C99 6.10.6p2, unknown forms are not allowed.
Chris Lattnerf545be52009-04-19 21:25:37 +00001001 PP.Diag(UnknownTok, diag::ext_stdc_pragma_ignored);
Chris Lattner062f2322009-04-19 21:20:35 +00001002 }
1003};
Mike Stump1eb44332009-09-09 15:08:12 +00001004
Reid Spencer5f016e22007-07-11 17:01:13 +00001005} // end anonymous namespace
1006
1007
1008/// RegisterBuiltinPragmas - Install the standard preprocessor pragmas:
1009/// #pragma GCC poison/system_header/dependency and #pragma once.
1010void Preprocessor::RegisterBuiltinPragmas() {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001011 AddPragmaHandler(new PragmaOnceHandler());
1012 AddPragmaHandler(new PragmaMarkHandler());
Chris Lattnerf47724b2010-08-17 15:55:45 +00001013 AddPragmaHandler(new PragmaPushMacroHandler());
1014 AddPragmaHandler(new PragmaPopMacroHandler());
Michael J. Spencer301669b2010-09-27 06:19:02 +00001015 AddPragmaHandler(new PragmaMessageHandler());
Mike Stump1eb44332009-09-09 15:08:12 +00001016
Chris Lattnere8fa06e2009-05-12 18:21:11 +00001017 // #pragma GCC ...
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001018 AddPragmaHandler("GCC", new PragmaPoisonHandler());
1019 AddPragmaHandler("GCC", new PragmaSystemHeaderHandler());
1020 AddPragmaHandler("GCC", new PragmaDependencyHandler());
Douglas Gregor6493a4d2010-08-30 15:15:34 +00001021 AddPragmaHandler("GCC", new PragmaDiagnosticHandler());
Chris Lattnere8fa06e2009-05-12 18:21:11 +00001022 // #pragma clang ...
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001023 AddPragmaHandler("clang", new PragmaPoisonHandler());
1024 AddPragmaHandler("clang", new PragmaSystemHeaderHandler());
Daniel Dunbarabf7b722010-07-28 15:40:33 +00001025 AddPragmaHandler("clang", new PragmaDebugHandler());
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001026 AddPragmaHandler("clang", new PragmaDependencyHandler());
Douglas Gregor6493a4d2010-08-30 15:15:34 +00001027 AddPragmaHandler("clang", new PragmaDiagnosticHandler());
Chris Lattnere8fa06e2009-05-12 18:21:11 +00001028
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001029 AddPragmaHandler("STDC", new PragmaSTDC_FP_CONTRACTHandler());
1030 AddPragmaHandler("STDC", new PragmaSTDC_FENV_ACCESSHandler());
1031 AddPragmaHandler("STDC", new PragmaSTDC_CX_LIMITED_RANGEHandler());
Chris Lattner062f2322009-04-19 21:20:35 +00001032 AddPragmaHandler("STDC", new PragmaSTDC_UnknownHandler());
Mike Stump1eb44332009-09-09 15:08:12 +00001033
Chris Lattner636c5ef2009-01-16 08:21:25 +00001034 // MS extensions.
Chris Lattnerabfe0942010-06-26 17:11:39 +00001035 if (Features.Microsoft) {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001036 AddPragmaHandler(new PragmaCommentHandler());
Chris Lattnerabfe0942010-06-26 17:11:39 +00001037 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001038}