blob: f0475bc0cb2016df369de237f450c8312a06dea7 [file] [log] [blame]
Chris Lattnerb8761832006-06-24 21:31:03 +00001//===--- Pragma.cpp - Pragma registration and handling --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerb8761832006-06-24 21:31:03 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattnerb694ba72006-07-02 22:41:36 +000010// This file implements the PragmaHandler/PragmaTable interfaces and implements
11// pragma related methods of the Preprocessor class.
Chris Lattnerb8761832006-06-24 21:31:03 +000012//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Lex/Pragma.h"
Chris Lattner07b019a2006-10-22 07:28:56 +000016#include "clang/Lex/HeaderSearch.h"
Chris Lattner262d4e32009-01-16 18:59:23 +000017#include "clang/Lex/LiteralSupport.h"
Chris Lattnerb8761832006-06-24 21:31:03 +000018#include "clang/Lex/Preprocessor.h"
Chris Lattnerc0a585d2010-08-17 15:55:45 +000019#include "clang/Lex/MacroInfo.h"
Chris Lattner60f36222009-01-29 05:15:15 +000020#include "clang/Lex/LexDiagnostic.h"
Chris Lattnerb694ba72006-07-02 22:41:36 +000021#include "clang/Basic/FileManager.h"
22#include "clang/Basic/SourceManager.h"
Daniel Dunbar211a7872010-08-18 23:09:23 +000023#include "llvm/Support/CrashRecoveryContext.h"
Daniel Dunbarf2cf3292010-08-17 22:32:48 +000024#include "llvm/Support/ErrorHandling.h"
Douglas Gregorc6d5edd2009-07-02 17:08:52 +000025#include <algorithm>
Chris Lattnerb8761832006-06-24 21:31:03 +000026using namespace clang;
27
28// Out-of-line destructor to provide a home for the class.
29PragmaHandler::~PragmaHandler() {
30}
31
Chris Lattner2e155302006-07-03 05:34:41 +000032//===----------------------------------------------------------------------===//
Daniel Dunbard839e772010-06-11 20:10:12 +000033// EmptyPragmaHandler Implementation.
34//===----------------------------------------------------------------------===//
35
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000036EmptyPragmaHandler::EmptyPragmaHandler() {}
Daniel Dunbard839e772010-06-11 20:10:12 +000037
Douglas Gregorc7d65762010-09-09 22:45:38 +000038void EmptyPragmaHandler::HandlePragma(Preprocessor &PP,
39 PragmaIntroducerKind Introducer,
40 Token &FirstToken) {}
Daniel Dunbard839e772010-06-11 20:10:12 +000041
42//===----------------------------------------------------------------------===//
Chris Lattner2e155302006-07-03 05:34:41 +000043// PragmaNamespace Implementation.
44//===----------------------------------------------------------------------===//
45
46
47PragmaNamespace::~PragmaNamespace() {
Argyrios Kyrtzidis36745fd2010-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;
Chris Lattner2e155302006-07-03 05:34:41 +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 Kyrtzidis36745fd2010-07-13 09:07:17 +000057PragmaHandler *PragmaNamespace::FindHandler(llvm::StringRef Name,
Chris Lattner2e155302006-07-03 05:34:41 +000058 bool IgnoreNull) const {
Argyrios Kyrtzidis36745fd2010-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 Stump11289f42009-09-09 15:08:12 +000063
Argyrios Kyrtzidis36745fd2010-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);
Chris Lattner2e155302006-07-03 05:34:41 +000070}
71
Daniel Dunbar40596532008-10-04 19:17:46 +000072void PragmaNamespace::RemovePragmaHandler(PragmaHandler *Handler) {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000073 assert(Handlers.lookup(Handler->getName()) &&
74 "Handler not registered in this namespace");
75 Handlers.erase(Handler->getName());
Daniel Dunbar40596532008-10-04 19:17:46 +000076}
77
Douglas Gregorc7d65762010-09-09 22:45:38 +000078void PragmaNamespace::HandlePragma(Preprocessor &PP,
79 PragmaIntroducerKind Introducer,
80 Token &Tok) {
Chris Lattnerb8761832006-06-24 21:31:03 +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 Stump11289f42009-09-09 15:08:12 +000084
Chris Lattnerb8761832006-06-24 21:31:03 +000085 // Get the handler for this token. If there is no handler, ignore the pragma.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000086 PragmaHandler *Handler
87 = FindHandler(Tok.getIdentifierInfo() ? Tok.getIdentifierInfo()->getName()
88 : llvm::StringRef(),
89 /*IgnoreNull=*/false);
Chris Lattner21656f22009-04-19 21:10:26 +000090 if (Handler == 0) {
91 PP.Diag(Tok, diag::warn_pragma_ignored);
92 return;
93 }
Mike Stump11289f42009-09-09 15:08:12 +000094
Chris Lattnerb8761832006-06-24 21:31:03 +000095 // Otherwise, pass it down.
Douglas Gregorc7d65762010-09-09 22:45:38 +000096 Handler->HandlePragma(PP, Introducer, Tok);
Chris Lattnerb8761832006-06-24 21:31:03 +000097}
Chris Lattnerb694ba72006-07-02 22:41:36 +000098
Chris Lattnerb694ba72006-07-02 22:41:36 +000099//===----------------------------------------------------------------------===//
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 Gregorc7d65762010-09-09 22:45:38 +0000105void Preprocessor::HandlePragmaDirective(unsigned Introducer) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000106 ++NumPragma;
Mike Stump11289f42009-09-09 15:08:12 +0000107
Chris Lattnerb694ba72006-07-02 22:41:36 +0000108 // Invoke the first level of pragma handlers which reads the namespace id.
Chris Lattner146762e2007-07-20 16:59:19 +0000109 Token Tok;
Douglas Gregorc7d65762010-09-09 22:45:38 +0000110 PragmaHandlers->HandlePragma(*this, PragmaIntroducerKind(Introducer), Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000111
Chris Lattnerb694ba72006-07-02 22:41:36 +0000112 // If the pragma handler didn't read the rest of the line, consume it now.
Chris Lattnere7e65942009-06-18 05:55:53 +0000113 if (CurPPLexer && CurPPLexer->ParsingPreprocessorDirective)
Chris Lattnerb694ba72006-07-02 22:41:36 +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 Lattner146762e2007-07-20 16:59:19 +0000120void Preprocessor::Handle_Pragma(Token &Tok) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000121 // Remember the pragma token location.
122 SourceLocation PragmaLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000123
Chris Lattnerb694ba72006-07-02 22:41:36 +0000124 // Read the '('.
125 Lex(Tok);
Chris Lattner907dfe92008-11-18 07:59:24 +0000126 if (Tok.isNot(tok::l_paren)) {
127 Diag(PragmaLoc, diag::err__Pragma_malformed);
128 return;
129 }
Chris Lattnerb694ba72006-07-02 22:41:36 +0000130
131 // Read the '"..."'.
132 Lex(Tok);
Chris Lattner907dfe92008-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 Stump11289f42009-09-09 15:08:12 +0000137
Chris Lattnerb694ba72006-07-02 22:41:36 +0000138 // Remember the string.
139 std::string StrVal = getSpelling(Tok);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000140
141 // Read the ')'.
142 Lex(Tok);
Chris Lattner907dfe92008-11-18 07:59:24 +0000143 if (Tok.isNot(tok::r_paren)) {
144 Diag(PragmaLoc, diag::err__Pragma_malformed);
145 return;
146 }
Mike Stump11289f42009-09-09 15:08:12 +0000147
Chris Lattner9dc9c202009-02-15 20:52:18 +0000148 SourceLocation RParenLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000149
Chris Lattner262d4e32009-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."
Chris Lattnerb694ba72006-07-02 22:41:36 +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 Stump11289f42009-09-09 15:08:12 +0000159
Chris Lattnerb694ba72006-07-02 22:41:36 +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 Stump11289f42009-09-09 15:08:12 +0000163
Chris Lattnerfa217bd2009-03-08 08:08:45 +0000164 // Replace the terminating quote with a \n.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000165 StrVal[StrVal.size()-1] = '\n';
Mike Stump11289f42009-09-09 15:08:12 +0000166
Chris Lattnerb694ba72006-07-02 22:41:36 +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 McCall89e925d2010-08-28 22:34:47 +0000176
Douglas Gregorc7d65762010-09-09 22:45:38 +0000177 Handle_Pragma(PIK__Pragma, StrVal, PragmaLoc, RParenLoc);
John McCall89e925d2010-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 McCall49039d42010-08-29 01:09:54 +0000209 if (Tok.is(tok::eof)) {
210 Diag(PragmaLoc, diag::err_unterminated___pragma);
211 return;
212 }
213
John McCall89e925d2010-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 Gregorc7d65762010-09-09 22:45:38 +0000223 Handle_Pragma(PIK___pragma, StrVal, PragmaLoc, RParenLoc);
John McCall89e925d2010-08-28 22:34:47 +0000224
225 // Finally, return whatever came after the pragma directive.
226 return Lex(Tok);
227}
228
Douglas Gregorc7d65762010-09-09 22:45:38 +0000229void Preprocessor::Handle_Pragma(unsigned Introducer,
230 const std::string &StrVal,
John McCall89e925d2010-08-28 22:34:47 +0000231 SourceLocation PragmaLoc,
232 SourceLocation RParenLoc) {
Mike Stump11289f42009-09-09 15:08:12 +0000233
Chris Lattnerf918bf72006-07-19 05:01:18 +0000234 // Plop the string (including the newline and trailing null) into a buffer
235 // where we can lex it.
Chris Lattner5a7971e2009-01-26 19:29:26 +0000236 Token TmpTok;
237 TmpTok.startToken();
238 CreateString(&StrVal[0], StrVal.size(), TmpTok);
239 SourceLocation TokLoc = TmpTok.getLocation();
Chris Lattnerb694ba72006-07-02 22:41:36 +0000240
Chris Lattnerb694ba72006-07-02 22:41:36 +0000241 // Make and enter a lexer object so that we lex and expand the tokens just
242 // like any others.
Chris Lattner9dc9c202009-02-15 20:52:18 +0000243 Lexer *TL = Lexer::Create_PragmaLexer(TokLoc, PragmaLoc, RParenLoc,
Chris Lattnerfa217bd2009-03-08 08:08:45 +0000244 StrVal.size(), *this);
Chris Lattner98a53122006-07-02 23:00:20 +0000245
246 EnterSourceFileWithLexer(TL, 0);
247
Chris Lattnerb694ba72006-07-02 22:41:36 +0000248 // With everything set up, lex this as a #pragma directive.
Douglas Gregorc7d65762010-09-09 22:45:38 +0000249 HandlePragmaDirective(Introducer);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000250}
251
252
253
254/// HandlePragmaOnce - Handle #pragma once. OnceTok is the 'once'.
255///
Chris Lattner146762e2007-07-20 16:59:19 +0000256void Preprocessor::HandlePragmaOnce(Token &OnceTok) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000257 if (isInPrimaryFile()) {
258 Diag(OnceTok, diag::pp_pragma_once_in_main_file);
259 return;
260 }
Mike Stump11289f42009-09-09 15:08:12 +0000261
Chris Lattnerb694ba72006-07-02 22:41:36 +0000262 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000263 // Mark the file as a once-only file now.
Chris Lattnerd32480d2009-01-17 06:22:33 +0000264 HeaderInfo.MarkFileIncludeOnce(getCurrentFileLexer()->getFileEntry());
Chris Lattnerb694ba72006-07-02 22:41:36 +0000265}
266
Chris Lattnerc2383312007-12-19 19:38:36 +0000267void Preprocessor::HandlePragmaMark() {
Ted Kremenek76c34412008-11-19 22:21:33 +0000268 assert(CurPPLexer && "No current lexer?");
Chris Lattnerd9efb6e2009-06-15 05:02:34 +0000269 if (CurLexer)
270 CurLexer->ReadToEndOfLine();
271 else
272 CurPTHLexer->DiscardToEndOfLine();
Chris Lattnerc2383312007-12-19 19:38:36 +0000273}
274
275
Chris Lattnerb694ba72006-07-02 22:41:36 +0000276/// HandlePragmaPoison - Handle #pragma GCC poison. PoisonTok is the 'poison'.
277///
Chris Lattner146762e2007-07-20 16:59:19 +0000278void Preprocessor::HandlePragmaPoison(Token &PoisonTok) {
279 Token Tok;
Chris Lattner538d7f32006-07-20 04:31:52 +0000280
Chris Lattnerb694ba72006-07-02 22:41:36 +0000281 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 Kremenek551c82a2008-11-18 01:12:54 +0000287 if (CurPPLexer) CurPPLexer->LexingRawMode = true;
Chris Lattnerb694ba72006-07-02 22:41:36 +0000288 LexUnexpandedToken(Tok);
Ted Kremenek551c82a2008-11-18 01:12:54 +0000289 if (CurPPLexer) CurPPLexer->LexingRawMode = false;
Mike Stump11289f42009-09-09 15:08:12 +0000290
Chris Lattnerb694ba72006-07-02 22:41:36 +0000291 // If we reached the end of line, we're done.
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000292 if (Tok.is(tok::eom)) return;
Mike Stump11289f42009-09-09 15:08:12 +0000293
Chris Lattnerb694ba72006-07-02 22:41:36 +0000294 // Can only poison identifiers.
Abramo Bagnaraea4f7c72010-12-22 08:23:18 +0000295 if (Tok.isNot(tok::raw_identifier)) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000296 Diag(Tok, diag::err_pp_invalid_poison);
297 return;
298 }
Mike Stump11289f42009-09-09 15:08:12 +0000299
Chris Lattnercefc7682006-07-08 08:28:12 +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 Stump11289f42009-09-09 15:08:12 +0000303
Chris Lattnerb694ba72006-07-02 22:41:36 +0000304 // Already poisoned.
305 if (II->isPoisoned()) continue;
Mike Stump11289f42009-09-09 15:08:12 +0000306
Chris Lattnerb694ba72006-07-02 22:41:36 +0000307 // If this is a macro identifier, emit a warning.
Chris Lattner259716a2007-10-07 08:04:56 +0000308 if (II->hasMacroDefinition())
Chris Lattnerb694ba72006-07-02 22:41:36 +0000309 Diag(Tok, diag::pp_poisoning_existing_macro);
Mike Stump11289f42009-09-09 15:08:12 +0000310
Chris Lattnerb694ba72006-07-02 22:41:36 +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 Lattner146762e2007-07-20 16:59:19 +0000318void Preprocessor::HandlePragmaSystemHeader(Token &SysHeaderTok) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000319 if (isInPrimaryFile()) {
320 Diag(SysHeaderTok, diag::pp_pragma_sysheader_in_main_file);
321 return;
322 }
Mike Stump11289f42009-09-09 15:08:12 +0000323
Chris Lattnerb694ba72006-07-02 22:41:36 +0000324 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
Ted Kremenek300590b2008-11-20 01:45:11 +0000325 PreprocessorLexer *TheLexer = getCurrentFileLexer();
Mike Stump11289f42009-09-09 15:08:12 +0000326
Chris Lattnerb694ba72006-07-02 22:41:36 +0000327 // Mark the file as a system header.
Chris Lattnerd32480d2009-01-17 06:22:33 +0000328 HeaderInfo.MarkFileSystemHeader(TheLexer->getFileEntry());
Mike Stump11289f42009-09-09 15:08:12 +0000329
330
Chris Lattnerd9efb6e2009-06-15 05:02:34 +0000331 PresumedLoc PLoc = SourceMgr.getPresumedLoc(SysHeaderTok.getLocation());
Douglas Gregor453b0122010-11-12 07:15:47 +0000332 if (PLoc.isInvalid())
333 return;
334
Chris Lattnerd9efb6e2009-06-15 05:02:34 +0000335 unsigned FilenameLen = strlen(PLoc.getFilename());
336 unsigned FilenameID = SourceMgr.getLineTableFilenameID(PLoc.getFilename(),
337 FilenameLen);
Mike Stump11289f42009-09-09 15:08:12 +0000338
Chris Lattnerd9efb6e2009-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 Stump11289f42009-09-09 15:08:12 +0000344
Chris Lattnerb694ba72006-07-02 22:41:36 +0000345 // Notify the client, if desired, that we are in a new source file.
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000346 if (Callbacks)
Ted Kremenek300590b2008-11-20 01:45:11 +0000347 Callbacks->FileChanged(SysHeaderTok.getLocation(),
Chris Lattnerb03dc762008-09-26 21:18:42 +0000348 PPCallbacks::SystemHeaderPragma, SrcMgr::C_System);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000349}
350
351/// HandlePragmaDependency - Handle #pragma GCC dependency "foo" blah.
352///
Chris Lattner146762e2007-07-20 16:59:19 +0000353void Preprocessor::HandlePragmaDependency(Token &DependencyTok) {
354 Token FilenameTok;
Ted Kremenek551c82a2008-11-18 01:12:54 +0000355 CurPPLexer->LexIncludeFilename(FilenameTok);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000356
357 // If the token kind is EOM, the error has already been diagnosed.
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000358 if (FilenameTok.is(tok::eom))
Chris Lattnerb694ba72006-07-02 22:41:36 +0000359 return;
Mike Stump11289f42009-09-09 15:08:12 +0000360
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000361 // Reserve a buffer to get the spelling.
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000362 llvm::SmallString<128> FilenameBuffer;
Douglas Gregordc970f02010-03-16 22:30:13 +0000363 bool Invalid = false;
364 llvm::StringRef Filename = getSpelling(FilenameTok, FilenameBuffer, &Invalid);
365 if (Invalid)
366 return;
Mike Stump11289f42009-09-09 15:08:12 +0000367
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000368 bool isAngled =
369 GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000370 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
371 // error.
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000372 if (Filename.empty())
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000373 return;
Mike Stump11289f42009-09-09 15:08:12 +0000374
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000375 // Search include directories for this file.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000376 const DirectoryLookup *CurDir;
Chris Lattnerfde85352010-01-22 00:14:44 +0000377 const FileEntry *File = LookupFile(Filename, isAngled, 0, CurDir);
Chris Lattner97b8e842008-11-18 08:02:48 +0000378 if (File == 0) {
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000379 Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
Chris Lattner97b8e842008-11-18 08:02:48 +0000380 return;
381 }
Mike Stump11289f42009-09-09 15:08:12 +0000382
Chris Lattnerd32480d2009-01-17 06:22:33 +0000383 const FileEntry *CurFile = getCurrentFileLexer()->getFileEntry();
Chris Lattnerb694ba72006-07-02 22:41:36 +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 Lattner98c1f7c2007-10-09 18:02:16 +0000390 while (DependencyTok.isNot(tok::eom)) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000391 Message += getSpelling(DependencyTok) + " ";
392 Lex(DependencyTok);
393 }
Mike Stump11289f42009-09-09 15:08:12 +0000394
Chris Lattnerf0b04972010-09-05 23:16:09 +0000395 // Remove the trailing ' ' if present.
396 if (!Message.empty())
397 Message.erase(Message.end()-1);
Chris Lattner97b8e842008-11-18 08:02:48 +0000398 Diag(FilenameTok, diag::pp_out_of_date_dependency) << Message;
Chris Lattnerb694ba72006-07-02 22:41:36 +0000399 }
400}
401
Chris Lattner2ff698d2009-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 Greif31a082f2009-03-17 11:39:38 +0000407/// concatenation, embedded escape characters etc. See MSDN for more details.
Chris Lattner2ff698d2009-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 Stump11289f42009-09-09 15:08:12 +0000415
Chris Lattner2ff698d2009-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 Stump11289f42009-09-09 15:08:12 +0000422
Chris Lattner2ff698d2009-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 Stump11289f42009-09-09 15:08:12 +0000426 if (!II->isStr("compiler") && !II->isStr("exestr") && !II->isStr("lib") &&
Chris Lattner2ff698d2009-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 Stump11289f42009-09-09 15:08:12 +0000431
Chris Lattner262d4e32009-01-16 18:59:23 +0000432 // Read the optional string if present.
Chris Lattner2ff698d2009-01-16 08:21:25 +0000433 Lex(Tok);
Chris Lattner262d4e32009-01-16 18:59:23 +0000434 std::string ArgumentString;
Chris Lattner2ff698d2009-01-16 08:21:25 +0000435 if (Tok.is(tok::comma)) {
Chris Lattner262d4e32009-01-16 18:59:23 +0000436 Lex(Tok); // eat the comma.
Chris Lattner2ff698d2009-01-16 08:21:25 +0000437
438 // We need at least one string.
Chris Lattner504af112009-04-19 23:16:58 +0000439 if (Tok.isNot(tok::string_literal)) {
Chris Lattner2ff698d2009-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 Lattner262d4e32009-01-16 18:59:23 +0000447 llvm::SmallVector<Token, 4> StrToks;
Chris Lattner504af112009-04-19 23:16:58 +0000448 while (Tok.is(tok::string_literal)) {
Chris Lattner262d4e32009-01-16 18:59:23 +0000449 StrToks.push_back(Tok);
Chris Lattner2ff698d2009-01-16 08:21:25 +0000450 Lex(Tok);
Chris Lattner262d4e32009-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 Lattner2ff698d2009-01-16 08:21:25 +0000465 }
Mike Stump11289f42009-09-09 15:08:12 +0000466
Chris Lattner262d4e32009-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 Stump11289f42009-09-09 15:08:12 +0000472
Chris Lattner2ff698d2009-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 Lattner262d4e32009-01-16 18:59:23 +0000477 Lex(Tok); // eat the r_paren.
Chris Lattner2ff698d2009-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 Stump11289f42009-09-09 15:08:12 +0000483
Chris Lattner262d4e32009-01-16 18:59:23 +0000484 // If the pragma is lexically sound, notify any interested PPCallbacks.
Chris Lattnerf49775d2009-01-16 19:01:46 +0000485 if (Callbacks)
486 Callbacks->PragmaComment(CommentLoc, II, ArgumentString);
Chris Lattner2ff698d2009-01-16 08:21:25 +0000487}
488
Michael J. Spencera0a820f2010-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 Lattner30c924b2010-06-26 17:11:39 +0000496void Preprocessor::HandlePragmaMessage(Token &Tok) {
497 SourceLocation MessageLoc = Tok.getLocation();
498 Lex(Tok);
Michael J. Spencera0a820f2010-09-27 06:19:02 +0000499 bool ExpectClosingParen = false;
Michael J. Spencer4362a1c2010-09-27 06:34:47 +0000500 switch (Tok.getKind()) {
Michael J. Spencera0a820f2010-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 Lattner30c924b2010-06-26 17:11:39 +0000511 Diag(MessageLoc, diag::err_pragma_message_malformed);
512 return;
513 }
Chris Lattner2ff698d2009-01-16 08:21:25 +0000514
Chris Lattner30c924b2010-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. Spencera0a820f2010-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 Lattner30c924b2010-06-26 17:11:39 +0000548 }
Chris Lattner30c924b2010-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 Lattner2ff698d2009-01-16 08:21:25 +0000562
Chris Lattnerc0a585d2010-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();
Abramo Bagnaraea4f7c72010-12-22 08:23:18 +0000602 MacroTok.setKind(tok::raw_identifier);
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000603 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 Kremenek6339f1c2010-10-19 17:40:50 +0000633/// HandlePragmaPopMacro - Handle #pragma pop_macro.
Chris Lattnerc0a585d2010-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);
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000649 if (CurrentMI) {
650 if (CurrentMI->isWarnIfUnused())
651 WarnUnusedMacroLocs.erase(CurrentMI->getDefinitionLoc());
652 ReleaseMacroInfo(CurrentMI);
653 }
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000654
655 // Get the MacroInfo we want to reinstall.
656 MacroInfo *MacroToReInstall = iter->second.back();
657
658 // Reinstall the previously pushed macro.
659 setMacroInfo(IdentInfo, MacroToReInstall);
660
661 // Pop PragmaPushMacroInfo stack.
662 iter->second.pop_back();
663 if (iter->second.size() == 0)
664 PragmaPushMacroInfo.erase(iter);
665 } else {
666 Diag(MessageLoc, diag::warn_pragma_pop_macro_no_push)
667 << IdentInfo->getName();
668 }
669}
Chris Lattnerb694ba72006-07-02 22:41:36 +0000670
671/// AddPragmaHandler - Add the specified pragma handler to the preprocessor.
672/// If 'Namespace' is non-null, then it is a token required to exist on the
673/// pragma line before the pragma string starts, e.g. "STDC" or "GCC".
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000674void Preprocessor::AddPragmaHandler(llvm::StringRef Namespace,
Chris Lattnerb694ba72006-07-02 22:41:36 +0000675 PragmaHandler *Handler) {
676 PragmaNamespace *InsertNS = PragmaHandlers;
Mike Stump11289f42009-09-09 15:08:12 +0000677
Chris Lattnerb694ba72006-07-02 22:41:36 +0000678 // If this is specified to be in a namespace, step down into it.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000679 if (!Namespace.empty()) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000680 // If there is already a pragma handler with the name of this namespace,
681 // we either have an error (directive with the same name as a namespace) or
682 // we already have the namespace to insert into.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000683 if (PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace)) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000684 InsertNS = Existing->getIfNamespace();
685 assert(InsertNS != 0 && "Cannot have a pragma namespace and pragma"
686 " handler with the same name!");
687 } else {
688 // Otherwise, this namespace doesn't exist yet, create and insert the
689 // handler for it.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000690 InsertNS = new PragmaNamespace(Namespace);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000691 PragmaHandlers->AddPragma(InsertNS);
692 }
693 }
Mike Stump11289f42009-09-09 15:08:12 +0000694
Chris Lattnerb694ba72006-07-02 22:41:36 +0000695 // Check to make sure we don't already have a pragma for this identifier.
696 assert(!InsertNS->FindHandler(Handler->getName()) &&
697 "Pragma handler already exists for this identifier!");
698 InsertNS->AddPragma(Handler);
699}
700
Daniel Dunbar40596532008-10-04 19:17:46 +0000701/// RemovePragmaHandler - Remove the specific pragma handler from the
702/// preprocessor. If \arg Namespace is non-null, then it should be the
703/// namespace that \arg Handler was added to. It is an error to remove
704/// a handler that has not been registered.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000705void Preprocessor::RemovePragmaHandler(llvm::StringRef Namespace,
Daniel Dunbar40596532008-10-04 19:17:46 +0000706 PragmaHandler *Handler) {
707 PragmaNamespace *NS = PragmaHandlers;
Mike Stump11289f42009-09-09 15:08:12 +0000708
Daniel Dunbar40596532008-10-04 19:17:46 +0000709 // If this is specified to be in a namespace, step down into it.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000710 if (!Namespace.empty()) {
711 PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace);
Daniel Dunbar40596532008-10-04 19:17:46 +0000712 assert(Existing && "Namespace containing handler does not exist!");
713
714 NS = Existing->getIfNamespace();
715 assert(NS && "Invalid namespace, registered as a regular pragma handler!");
716 }
717
718 NS->RemovePragmaHandler(Handler);
Mike Stump11289f42009-09-09 15:08:12 +0000719
Daniel Dunbar40596532008-10-04 19:17:46 +0000720 // If this is a non-default namespace and it is now empty, remove
721 // it.
722 if (NS != PragmaHandlers && NS->IsEmpty())
723 PragmaHandlers->RemovePragmaHandler(NS);
724}
725
Peter Collingbourne3bffa522011-02-14 01:42:24 +0000726bool Preprocessor::LexOnOffSwitch(tok::OnOffSwitch &Result) {
727 Token Tok;
728 LexUnexpandedToken(Tok);
729
730 if (Tok.isNot(tok::identifier)) {
731 Diag(Tok, diag::ext_on_off_switch_syntax);
732 return true;
733 }
734 IdentifierInfo *II = Tok.getIdentifierInfo();
735 if (II->isStr("ON"))
736 Result = tok::OOS_ON;
737 else if (II->isStr("OFF"))
738 Result = tok::OOS_OFF;
739 else if (II->isStr("DEFAULT"))
740 Result = tok::OOS_DEFAULT;
741 else {
742 Diag(Tok, diag::ext_on_off_switch_syntax);
743 return true;
744 }
745
746 // Verify that this is followed by EOM.
747 LexUnexpandedToken(Tok);
748 if (Tok.isNot(tok::eom))
749 Diag(Tok, diag::ext_pragma_syntax_eom);
750 return false;
751}
752
Chris Lattnerb694ba72006-07-02 22:41:36 +0000753namespace {
Chris Lattnerc2383312007-12-19 19:38:36 +0000754/// PragmaOnceHandler - "#pragma once" marks the file as atomically included.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000755struct PragmaOnceHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000756 PragmaOnceHandler() : PragmaHandler("once") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000757 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
758 Token &OnceTok) {
Chris Lattnerce2ab6f2009-04-14 05:07:49 +0000759 PP.CheckEndOfDirective("pragma once");
Chris Lattnerb694ba72006-07-02 22:41:36 +0000760 PP.HandlePragmaOnce(OnceTok);
761 }
762};
763
Chris Lattnerc2383312007-12-19 19:38:36 +0000764/// PragmaMarkHandler - "#pragma mark ..." is ignored by the compiler, and the
765/// rest of the line is not lexed.
766struct PragmaMarkHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000767 PragmaMarkHandler() : PragmaHandler("mark") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000768 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
769 Token &MarkTok) {
Chris Lattnerc2383312007-12-19 19:38:36 +0000770 PP.HandlePragmaMark();
771 }
772};
773
774/// PragmaPoisonHandler - "#pragma poison x" marks x as not usable.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000775struct PragmaPoisonHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000776 PragmaPoisonHandler() : PragmaHandler("poison") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000777 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
778 Token &PoisonTok) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000779 PP.HandlePragmaPoison(PoisonTok);
780 }
781};
782
Chris Lattnerc2383312007-12-19 19:38:36 +0000783/// PragmaSystemHeaderHandler - "#pragma system_header" marks the current file
784/// as a system header, which silences warnings in it.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000785struct PragmaSystemHeaderHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000786 PragmaSystemHeaderHandler() : PragmaHandler("system_header") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000787 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
788 Token &SHToken) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000789 PP.HandlePragmaSystemHeader(SHToken);
Chris Lattnerce2ab6f2009-04-14 05:07:49 +0000790 PP.CheckEndOfDirective("pragma");
Chris Lattnerb694ba72006-07-02 22:41:36 +0000791 }
792};
793struct PragmaDependencyHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000794 PragmaDependencyHandler() : PragmaHandler("dependency") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000795 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
796 Token &DepToken) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000797 PP.HandlePragmaDependency(DepToken);
798 }
799};
Mike Stump11289f42009-09-09 15:08:12 +0000800
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000801struct PragmaDebugHandler : public PragmaHandler {
802 PragmaDebugHandler() : PragmaHandler("__debug") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000803 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
804 Token &DepToken) {
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000805 Token Tok;
806 PP.LexUnexpandedToken(Tok);
807 if (Tok.isNot(tok::identifier)) {
Douglas Gregor3cc26482010-08-30 15:15:34 +0000808 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000809 return;
810 }
811 IdentifierInfo *II = Tok.getIdentifierInfo();
812
Daniel Dunbarf2cf3292010-08-17 22:32:48 +0000813 if (II->isStr("assert")) {
814 assert(0 && "This is an assertion!");
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000815 } else if (II->isStr("crash")) {
Daniel Dunbarf2cf3292010-08-17 22:32:48 +0000816 *(volatile int*) 0x11 = 0;
817 } else if (II->isStr("llvm_fatal_error")) {
818 llvm::report_fatal_error("#pragma clang __debug llvm_fatal_error");
819 } else if (II->isStr("llvm_unreachable")) {
820 llvm_unreachable("#pragma clang __debug llvm_unreachable");
821 } else if (II->isStr("overflow_stack")) {
822 DebugOverflowStack();
Daniel Dunbar211a7872010-08-18 23:09:23 +0000823 } else if (II->isStr("handle_crash")) {
824 llvm::CrashRecoveryContext *CRC =llvm::CrashRecoveryContext::GetCurrent();
825 if (CRC)
826 CRC->HandleCrash();
Daniel Dunbarf2cf3292010-08-17 22:32:48 +0000827 } else {
828 PP.Diag(Tok, diag::warn_pragma_debug_unexpected_command)
829 << II->getName();
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000830 }
831 }
832
833 void DebugOverflowStack() {
834 DebugOverflowStack();
835 }
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000836};
837
Chris Lattner504af112009-04-19 23:16:58 +0000838/// PragmaDiagnosticHandler - e.g. '#pragma GCC diagnostic ignored "-Wformat"'
839struct PragmaDiagnosticHandler : public PragmaHandler {
Chris Lattnerfb42a182009-07-12 21:18:45 +0000840public:
Douglas Gregor3cc26482010-08-30 15:15:34 +0000841 explicit PragmaDiagnosticHandler() : PragmaHandler("diagnostic") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000842 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
843 Token &DiagToken) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000844 SourceLocation DiagLoc = DiagToken.getLocation();
Chris Lattner504af112009-04-19 23:16:58 +0000845 Token Tok;
846 PP.LexUnexpandedToken(Tok);
847 if (Tok.isNot(tok::identifier)) {
Douglas Gregor3cc26482010-08-30 15:15:34 +0000848 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Chris Lattner504af112009-04-19 23:16:58 +0000849 return;
850 }
851 IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000852
Chris Lattner504af112009-04-19 23:16:58 +0000853 diag::Mapping Map;
854 if (II->isStr("warning"))
855 Map = diag::MAP_WARNING;
856 else if (II->isStr("error"))
857 Map = diag::MAP_ERROR;
858 else if (II->isStr("ignored"))
859 Map = diag::MAP_IGNORE;
860 else if (II->isStr("fatal"))
861 Map = diag::MAP_FATAL;
Douglas Gregor3cc26482010-08-30 15:15:34 +0000862 else if (II->isStr("pop")) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000863 if (!PP.getDiagnostics().popMappings(DiagLoc))
Douglas Gregor3cc26482010-08-30 15:15:34 +0000864 PP.Diag(Tok, diag::warn_pragma_diagnostic_cannot_pop);
Chris Lattnerfb42a182009-07-12 21:18:45 +0000865
Douglas Gregor3cc26482010-08-30 15:15:34 +0000866 return;
867 } else if (II->isStr("push")) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000868 PP.getDiagnostics().pushMappings(DiagLoc);
Chris Lattnerfb42a182009-07-12 21:18:45 +0000869 return;
870 } else {
Douglas Gregor3cc26482010-08-30 15:15:34 +0000871 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Chris Lattner504af112009-04-19 23:16:58 +0000872 return;
873 }
Mike Stump11289f42009-09-09 15:08:12 +0000874
Chris Lattner504af112009-04-19 23:16:58 +0000875 PP.LexUnexpandedToken(Tok);
876
877 // We need at least one string.
878 if (Tok.isNot(tok::string_literal)) {
879 PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token);
880 return;
881 }
Mike Stump11289f42009-09-09 15:08:12 +0000882
Chris Lattner504af112009-04-19 23:16:58 +0000883 // String concatenation allows multiple strings, which can even come from
884 // macro expansion.
885 // "foo " "bar" "Baz"
886 llvm::SmallVector<Token, 4> StrToks;
887 while (Tok.is(tok::string_literal)) {
888 StrToks.push_back(Tok);
889 PP.LexUnexpandedToken(Tok);
890 }
Mike Stump11289f42009-09-09 15:08:12 +0000891
Chris Lattner504af112009-04-19 23:16:58 +0000892 if (Tok.isNot(tok::eom)) {
893 PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token);
894 return;
895 }
Mike Stump11289f42009-09-09 15:08:12 +0000896
Chris Lattner504af112009-04-19 23:16:58 +0000897 // Concatenate and parse the strings.
898 StringLiteralParser Literal(&StrToks[0], StrToks.size(), PP);
899 assert(!Literal.AnyWide && "Didn't allow wide strings in");
900 if (Literal.hadError)
901 return;
902 if (Literal.Pascal) {
Douglas Gregor3cc26482010-08-30 15:15:34 +0000903 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Chris Lattner504af112009-04-19 23:16:58 +0000904 return;
905 }
Chris Lattnerfb42a182009-07-12 21:18:45 +0000906
Chris Lattner504af112009-04-19 23:16:58 +0000907 std::string WarningName(Literal.GetString(),
908 Literal.GetString()+Literal.GetStringLength());
909
910 if (WarningName.size() < 3 || WarningName[0] != '-' ||
911 WarningName[1] != 'W') {
912 PP.Diag(StrToks[0].getLocation(),
913 diag::warn_pragma_diagnostic_invalid_option);
914 return;
915 }
Mike Stump11289f42009-09-09 15:08:12 +0000916
Chris Lattner504af112009-04-19 23:16:58 +0000917 if (PP.getDiagnostics().setDiagnosticGroupMapping(WarningName.c_str()+2,
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000918 Map, DiagLoc))
Chris Lattner504af112009-04-19 23:16:58 +0000919 PP.Diag(StrToks[0].getLocation(),
920 diag::warn_pragma_diagnostic_unknown_warning) << WarningName;
921 }
922};
Mike Stump11289f42009-09-09 15:08:12 +0000923
Chris Lattner2ff698d2009-01-16 08:21:25 +0000924/// PragmaCommentHandler - "#pragma comment ...".
925struct PragmaCommentHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000926 PragmaCommentHandler() : PragmaHandler("comment") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000927 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
928 Token &CommentTok) {
Chris Lattner2ff698d2009-01-16 08:21:25 +0000929 PP.HandlePragmaComment(CommentTok);
930 }
931};
Mike Stump11289f42009-09-09 15:08:12 +0000932
Chris Lattner30c924b2010-06-26 17:11:39 +0000933/// PragmaMessageHandler - "#pragma message("...")".
934struct PragmaMessageHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000935 PragmaMessageHandler() : PragmaHandler("message") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000936 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
937 Token &CommentTok) {
Chris Lattner30c924b2010-06-26 17:11:39 +0000938 PP.HandlePragmaMessage(CommentTok);
939 }
940};
941
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000942/// PragmaPushMacroHandler - "#pragma push_macro" saves the value of the
943/// macro on the top of the stack.
944struct PragmaPushMacroHandler : public PragmaHandler {
945 PragmaPushMacroHandler() : PragmaHandler("push_macro") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000946 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
947 Token &PushMacroTok) {
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000948 PP.HandlePragmaPushMacro(PushMacroTok);
949 }
950};
951
952
953/// PragmaPopMacroHandler - "#pragma pop_macro" sets the value of the
954/// macro to the value on the top of the stack.
955struct PragmaPopMacroHandler : public PragmaHandler {
956 PragmaPopMacroHandler() : PragmaHandler("pop_macro") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000957 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
958 Token &PopMacroTok) {
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000959 PP.HandlePragmaPopMacro(PopMacroTok);
960 }
961};
962
Chris Lattner958ee042009-04-19 21:20:35 +0000963// Pragma STDC implementations.
Chris Lattner02ef4e32009-04-19 21:50:08 +0000964
Chris Lattner958ee042009-04-19 21:20:35 +0000965/// PragmaSTDC_FENV_ACCESSHandler - "#pragma STDC FENV_ACCESS ...".
966struct PragmaSTDC_FENV_ACCESSHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000967 PragmaSTDC_FENV_ACCESSHandler() : PragmaHandler("FENV_ACCESS") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000968 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
969 Token &Tok) {
Peter Collingbourne3bffa522011-02-14 01:42:24 +0000970 tok::OnOffSwitch OOS;
971 if (PP.LexOnOffSwitch(OOS))
972 return;
973 if (OOS == tok::OOS_ON)
Chris Lattnerdf222682009-04-19 21:55:32 +0000974 PP.Diag(Tok, diag::warn_stdc_fenv_access_not_supported);
Chris Lattner958ee042009-04-19 21:20:35 +0000975 }
976};
Mike Stump11289f42009-09-09 15:08:12 +0000977
Chris Lattner958ee042009-04-19 21:20:35 +0000978/// PragmaSTDC_CX_LIMITED_RANGEHandler - "#pragma STDC CX_LIMITED_RANGE ...".
979struct PragmaSTDC_CX_LIMITED_RANGEHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000980 PragmaSTDC_CX_LIMITED_RANGEHandler()
981 : PragmaHandler("CX_LIMITED_RANGE") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000982 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
983 Token &Tok) {
Peter Collingbourne3bffa522011-02-14 01:42:24 +0000984 tok::OnOffSwitch OOS;
985 PP.LexOnOffSwitch(OOS);
Chris Lattner958ee042009-04-19 21:20:35 +0000986 }
987};
Mike Stump11289f42009-09-09 15:08:12 +0000988
Chris Lattner958ee042009-04-19 21:20:35 +0000989/// PragmaSTDC_UnknownHandler - "#pragma STDC ...".
990struct PragmaSTDC_UnknownHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000991 PragmaSTDC_UnknownHandler() {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000992 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
993 Token &UnknownTok) {
Chris Lattner02ef4e32009-04-19 21:50:08 +0000994 // C99 6.10.6p2, unknown forms are not allowed.
Chris Lattnera0b1f762009-04-19 21:25:37 +0000995 PP.Diag(UnknownTok, diag::ext_stdc_pragma_ignored);
Chris Lattner958ee042009-04-19 21:20:35 +0000996 }
997};
Mike Stump11289f42009-09-09 15:08:12 +0000998
Chris Lattnerb694ba72006-07-02 22:41:36 +0000999} // end anonymous namespace
1000
1001
1002/// RegisterBuiltinPragmas - Install the standard preprocessor pragmas:
1003/// #pragma GCC poison/system_header/dependency and #pragma once.
1004void Preprocessor::RegisterBuiltinPragmas() {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001005 AddPragmaHandler(new PragmaOnceHandler());
1006 AddPragmaHandler(new PragmaMarkHandler());
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001007 AddPragmaHandler(new PragmaPushMacroHandler());
1008 AddPragmaHandler(new PragmaPopMacroHandler());
Michael J. Spencera0a820f2010-09-27 06:19:02 +00001009 AddPragmaHandler(new PragmaMessageHandler());
Mike Stump11289f42009-09-09 15:08:12 +00001010
Chris Lattnerb61448d2009-05-12 18:21:11 +00001011 // #pragma GCC ...
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001012 AddPragmaHandler("GCC", new PragmaPoisonHandler());
1013 AddPragmaHandler("GCC", new PragmaSystemHeaderHandler());
1014 AddPragmaHandler("GCC", new PragmaDependencyHandler());
Douglas Gregor3cc26482010-08-30 15:15:34 +00001015 AddPragmaHandler("GCC", new PragmaDiagnosticHandler());
Chris Lattnerb61448d2009-05-12 18:21:11 +00001016 // #pragma clang ...
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001017 AddPragmaHandler("clang", new PragmaPoisonHandler());
1018 AddPragmaHandler("clang", new PragmaSystemHeaderHandler());
Daniel Dunbarb8068c32010-07-28 15:40:33 +00001019 AddPragmaHandler("clang", new PragmaDebugHandler());
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001020 AddPragmaHandler("clang", new PragmaDependencyHandler());
Douglas Gregor3cc26482010-08-30 15:15:34 +00001021 AddPragmaHandler("clang", new PragmaDiagnosticHandler());
Chris Lattnerb61448d2009-05-12 18:21:11 +00001022
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001023 AddPragmaHandler("STDC", new PragmaSTDC_FENV_ACCESSHandler());
1024 AddPragmaHandler("STDC", new PragmaSTDC_CX_LIMITED_RANGEHandler());
Chris Lattner958ee042009-04-19 21:20:35 +00001025 AddPragmaHandler("STDC", new PragmaSTDC_UnknownHandler());
Mike Stump11289f42009-09-09 15:08:12 +00001026
Chris Lattner2ff698d2009-01-16 08:21:25 +00001027 // MS extensions.
Chris Lattner30c924b2010-06-26 17:11:39 +00001028 if (Features.Microsoft) {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001029 AddPragmaHandler(new PragmaCommentHandler());
Chris Lattner30c924b2010-06-26 17:11:39 +00001030 }
Chris Lattnerb694ba72006-07-02 22:41:36 +00001031}