blob: 31e777604ebe23fc8173eaf6845d55f0ca6792f0 [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.
Peter Collingbourne2c9f9662011-02-22 13:49:00 +0000113 if ((CurTokenLexer && CurTokenLexer->isParsingPreprocessorDirective())
114 || (CurPPLexer && CurPPLexer->ParsingPreprocessorDirective))
Chris Lattnerb694ba72006-07-02 22:41:36 +0000115 DiscardUntilEndOfDirective();
116}
117
118/// Handle_Pragma - Read a _Pragma directive, slice it up, process it, then
119/// return the first token after the directive. The _Pragma token has just
120/// been read into 'Tok'.
Chris Lattner146762e2007-07-20 16:59:19 +0000121void Preprocessor::Handle_Pragma(Token &Tok) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000122 // Remember the pragma token location.
123 SourceLocation PragmaLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000124
Chris Lattnerb694ba72006-07-02 22:41:36 +0000125 // Read the '('.
126 Lex(Tok);
Chris Lattner907dfe92008-11-18 07:59:24 +0000127 if (Tok.isNot(tok::l_paren)) {
128 Diag(PragmaLoc, diag::err__Pragma_malformed);
129 return;
130 }
Chris Lattnerb694ba72006-07-02 22:41:36 +0000131
132 // Read the '"..."'.
133 Lex(Tok);
Chris Lattner907dfe92008-11-18 07:59:24 +0000134 if (Tok.isNot(tok::string_literal) && Tok.isNot(tok::wide_string_literal)) {
135 Diag(PragmaLoc, diag::err__Pragma_malformed);
136 return;
137 }
Mike Stump11289f42009-09-09 15:08:12 +0000138
Chris Lattnerb694ba72006-07-02 22:41:36 +0000139 // Remember the string.
140 std::string StrVal = getSpelling(Tok);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000141
142 // Read the ')'.
143 Lex(Tok);
Chris Lattner907dfe92008-11-18 07:59:24 +0000144 if (Tok.isNot(tok::r_paren)) {
145 Diag(PragmaLoc, diag::err__Pragma_malformed);
146 return;
147 }
Mike Stump11289f42009-09-09 15:08:12 +0000148
Chris Lattner9dc9c202009-02-15 20:52:18 +0000149 SourceLocation RParenLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000150
Chris Lattner262d4e32009-01-16 18:59:23 +0000151 // The _Pragma is lexically sound. Destringize according to C99 6.10.9.1:
152 // "The string literal is destringized by deleting the L prefix, if present,
153 // deleting the leading and trailing double-quotes, replacing each escape
154 // sequence \" by a double-quote, and replacing each escape sequence \\ by a
155 // single backslash."
Chris Lattnerb694ba72006-07-02 22:41:36 +0000156 if (StrVal[0] == 'L') // Remove L prefix.
157 StrVal.erase(StrVal.begin());
158 assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
159 "Invalid string token!");
Mike Stump11289f42009-09-09 15:08:12 +0000160
Chris Lattnerb694ba72006-07-02 22:41:36 +0000161 // Remove the front quote, replacing it with a space, so that the pragma
162 // contents appear to have a space before them.
163 StrVal[0] = ' ';
Mike Stump11289f42009-09-09 15:08:12 +0000164
Chris Lattnerfa217bd2009-03-08 08:08:45 +0000165 // Replace the terminating quote with a \n.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000166 StrVal[StrVal.size()-1] = '\n';
Mike Stump11289f42009-09-09 15:08:12 +0000167
Chris Lattnerb694ba72006-07-02 22:41:36 +0000168 // Remove escaped quotes and escapes.
169 for (unsigned i = 0, e = StrVal.size(); i != e-1; ++i) {
170 if (StrVal[i] == '\\' &&
171 (StrVal[i+1] == '\\' || StrVal[i+1] == '"')) {
172 // \\ -> '\' and \" -> '"'.
173 StrVal.erase(StrVal.begin()+i);
174 --e;
175 }
176 }
John McCall89e925d2010-08-28 22:34:47 +0000177
Douglas Gregorc7d65762010-09-09 22:45:38 +0000178 Handle_Pragma(PIK__Pragma, StrVal, PragmaLoc, RParenLoc);
John McCall89e925d2010-08-28 22:34:47 +0000179
180 // Finally, return whatever came after the pragma directive.
181 return Lex(Tok);
182}
183
184/// HandleMicrosoft__pragma - Like Handle_Pragma except the pragma text
185/// is not enclosed within a string literal.
186void Preprocessor::HandleMicrosoft__pragma(Token &Tok) {
187 // Remember the pragma token location.
188 SourceLocation PragmaLoc = Tok.getLocation();
189
190 // Read the '('.
191 Lex(Tok);
192 if (Tok.isNot(tok::l_paren)) {
193 Diag(PragmaLoc, diag::err__Pragma_malformed);
194 return;
195 }
196
197 // Get the tokens enclosed within the __pragma().
198 llvm::SmallVector<Token, 32> PragmaToks;
199 int NumParens = 0;
200 Lex(Tok);
201 while (Tok.isNot(tok::eof)) {
202 if (Tok.is(tok::l_paren))
203 NumParens++;
204 else if (Tok.is(tok::r_paren) && NumParens-- == 0)
205 break;
206 PragmaToks.push_back(Tok);
207 Lex(Tok);
208 }
209
John McCall49039d42010-08-29 01:09:54 +0000210 if (Tok.is(tok::eof)) {
211 Diag(PragmaLoc, diag::err_unterminated___pragma);
212 return;
213 }
214
John McCall89e925d2010-08-28 22:34:47 +0000215 // Build the pragma string.
216 std::string StrVal = " ";
217 for (llvm::SmallVector<Token, 32>::iterator I =
218 PragmaToks.begin(), E = PragmaToks.end(); I != E; ++I) {
219 StrVal += getSpelling(*I);
220 }
221
222 SourceLocation RParenLoc = Tok.getLocation();
223
Douglas Gregorc7d65762010-09-09 22:45:38 +0000224 Handle_Pragma(PIK___pragma, StrVal, PragmaLoc, RParenLoc);
John McCall89e925d2010-08-28 22:34:47 +0000225
226 // Finally, return whatever came after the pragma directive.
227 return Lex(Tok);
228}
229
Douglas Gregorc7d65762010-09-09 22:45:38 +0000230void Preprocessor::Handle_Pragma(unsigned Introducer,
231 const std::string &StrVal,
John McCall89e925d2010-08-28 22:34:47 +0000232 SourceLocation PragmaLoc,
233 SourceLocation RParenLoc) {
Mike Stump11289f42009-09-09 15:08:12 +0000234
Chris Lattnerf918bf72006-07-19 05:01:18 +0000235 // Plop the string (including the newline and trailing null) into a buffer
236 // where we can lex it.
Chris Lattner5a7971e2009-01-26 19:29:26 +0000237 Token TmpTok;
238 TmpTok.startToken();
239 CreateString(&StrVal[0], StrVal.size(), TmpTok);
240 SourceLocation TokLoc = TmpTok.getLocation();
Chris Lattnerb694ba72006-07-02 22:41:36 +0000241
Chris Lattnerb694ba72006-07-02 22:41:36 +0000242 // Make and enter a lexer object so that we lex and expand the tokens just
243 // like any others.
Chris Lattner9dc9c202009-02-15 20:52:18 +0000244 Lexer *TL = Lexer::Create_PragmaLexer(TokLoc, PragmaLoc, RParenLoc,
Chris Lattnerfa217bd2009-03-08 08:08:45 +0000245 StrVal.size(), *this);
Chris Lattner98a53122006-07-02 23:00:20 +0000246
247 EnterSourceFileWithLexer(TL, 0);
248
Chris Lattnerb694ba72006-07-02 22:41:36 +0000249 // With everything set up, lex this as a #pragma directive.
Douglas Gregorc7d65762010-09-09 22:45:38 +0000250 HandlePragmaDirective(Introducer);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000251}
252
253
254
255/// HandlePragmaOnce - Handle #pragma once. OnceTok is the 'once'.
256///
Chris Lattner146762e2007-07-20 16:59:19 +0000257void Preprocessor::HandlePragmaOnce(Token &OnceTok) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000258 if (isInPrimaryFile()) {
259 Diag(OnceTok, diag::pp_pragma_once_in_main_file);
260 return;
261 }
Mike Stump11289f42009-09-09 15:08:12 +0000262
Chris Lattnerb694ba72006-07-02 22:41:36 +0000263 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000264 // Mark the file as a once-only file now.
Chris Lattnerd32480d2009-01-17 06:22:33 +0000265 HeaderInfo.MarkFileIncludeOnce(getCurrentFileLexer()->getFileEntry());
Chris Lattnerb694ba72006-07-02 22:41:36 +0000266}
267
Chris Lattnerc2383312007-12-19 19:38:36 +0000268void Preprocessor::HandlePragmaMark() {
Ted Kremenek76c34412008-11-19 22:21:33 +0000269 assert(CurPPLexer && "No current lexer?");
Chris Lattnerd9efb6e2009-06-15 05:02:34 +0000270 if (CurLexer)
271 CurLexer->ReadToEndOfLine();
272 else
273 CurPTHLexer->DiscardToEndOfLine();
Chris Lattnerc2383312007-12-19 19:38:36 +0000274}
275
276
Chris Lattnerb694ba72006-07-02 22:41:36 +0000277/// HandlePragmaPoison - Handle #pragma GCC poison. PoisonTok is the 'poison'.
278///
Chris Lattner146762e2007-07-20 16:59:19 +0000279void Preprocessor::HandlePragmaPoison(Token &PoisonTok) {
280 Token Tok;
Chris Lattner538d7f32006-07-20 04:31:52 +0000281
Chris Lattnerb694ba72006-07-02 22:41:36 +0000282 while (1) {
283 // Read the next token to poison. While doing this, pretend that we are
284 // skipping while reading the identifier to poison.
285 // This avoids errors on code like:
286 // #pragma GCC poison X
287 // #pragma GCC poison X
Ted Kremenek551c82a2008-11-18 01:12:54 +0000288 if (CurPPLexer) CurPPLexer->LexingRawMode = true;
Chris Lattnerb694ba72006-07-02 22:41:36 +0000289 LexUnexpandedToken(Tok);
Ted Kremenek551c82a2008-11-18 01:12:54 +0000290 if (CurPPLexer) CurPPLexer->LexingRawMode = false;
Mike Stump11289f42009-09-09 15:08:12 +0000291
Chris Lattnerb694ba72006-07-02 22:41:36 +0000292 // If we reached the end of line, we're done.
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000293 if (Tok.is(tok::eom)) return;
Mike Stump11289f42009-09-09 15:08:12 +0000294
Chris Lattnerb694ba72006-07-02 22:41:36 +0000295 // Can only poison identifiers.
Abramo Bagnaraea4f7c72010-12-22 08:23:18 +0000296 if (Tok.isNot(tok::raw_identifier)) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000297 Diag(Tok, diag::err_pp_invalid_poison);
298 return;
299 }
Mike Stump11289f42009-09-09 15:08:12 +0000300
Chris Lattnercefc7682006-07-08 08:28:12 +0000301 // Look up the identifier info for the token. We disabled identifier lookup
302 // by saying we're skipping contents, so we need to do this manually.
303 IdentifierInfo *II = LookUpIdentifierInfo(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000304
Chris Lattnerb694ba72006-07-02 22:41:36 +0000305 // Already poisoned.
306 if (II->isPoisoned()) continue;
Mike Stump11289f42009-09-09 15:08:12 +0000307
Chris Lattnerb694ba72006-07-02 22:41:36 +0000308 // If this is a macro identifier, emit a warning.
Chris Lattner259716a2007-10-07 08:04:56 +0000309 if (II->hasMacroDefinition())
Chris Lattnerb694ba72006-07-02 22:41:36 +0000310 Diag(Tok, diag::pp_poisoning_existing_macro);
Mike Stump11289f42009-09-09 15:08:12 +0000311
Chris Lattnerb694ba72006-07-02 22:41:36 +0000312 // Finally, poison it!
313 II->setIsPoisoned();
314 }
315}
316
317/// HandlePragmaSystemHeader - Implement #pragma GCC system_header. We know
318/// that the whole directive has been parsed.
Chris Lattner146762e2007-07-20 16:59:19 +0000319void Preprocessor::HandlePragmaSystemHeader(Token &SysHeaderTok) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000320 if (isInPrimaryFile()) {
321 Diag(SysHeaderTok, diag::pp_pragma_sysheader_in_main_file);
322 return;
323 }
Mike Stump11289f42009-09-09 15:08:12 +0000324
Chris Lattnerb694ba72006-07-02 22:41:36 +0000325 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
Ted Kremenek300590b2008-11-20 01:45:11 +0000326 PreprocessorLexer *TheLexer = getCurrentFileLexer();
Mike Stump11289f42009-09-09 15:08:12 +0000327
Chris Lattnerb694ba72006-07-02 22:41:36 +0000328 // Mark the file as a system header.
Chris Lattnerd32480d2009-01-17 06:22:33 +0000329 HeaderInfo.MarkFileSystemHeader(TheLexer->getFileEntry());
Mike Stump11289f42009-09-09 15:08:12 +0000330
331
Chris Lattnerd9efb6e2009-06-15 05:02:34 +0000332 PresumedLoc PLoc = SourceMgr.getPresumedLoc(SysHeaderTok.getLocation());
Douglas Gregor453b0122010-11-12 07:15:47 +0000333 if (PLoc.isInvalid())
334 return;
335
Chris Lattnerd9efb6e2009-06-15 05:02:34 +0000336 unsigned FilenameLen = strlen(PLoc.getFilename());
337 unsigned FilenameID = SourceMgr.getLineTableFilenameID(PLoc.getFilename(),
338 FilenameLen);
Mike Stump11289f42009-09-09 15:08:12 +0000339
Chris Lattnerd9efb6e2009-06-15 05:02:34 +0000340 // Emit a line marker. This will change any source locations from this point
341 // forward to realize they are in a system header.
342 // Create a line note with this information.
343 SourceMgr.AddLineNote(SysHeaderTok.getLocation(), PLoc.getLine(), FilenameID,
344 false, false, true, false);
Mike Stump11289f42009-09-09 15:08:12 +0000345
Chris Lattnerb694ba72006-07-02 22:41:36 +0000346 // Notify the client, if desired, that we are in a new source file.
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000347 if (Callbacks)
Ted Kremenek300590b2008-11-20 01:45:11 +0000348 Callbacks->FileChanged(SysHeaderTok.getLocation(),
Chris Lattnerb03dc762008-09-26 21:18:42 +0000349 PPCallbacks::SystemHeaderPragma, SrcMgr::C_System);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000350}
351
352/// HandlePragmaDependency - Handle #pragma GCC dependency "foo" blah.
353///
Chris Lattner146762e2007-07-20 16:59:19 +0000354void Preprocessor::HandlePragmaDependency(Token &DependencyTok) {
355 Token FilenameTok;
Ted Kremenek551c82a2008-11-18 01:12:54 +0000356 CurPPLexer->LexIncludeFilename(FilenameTok);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000357
358 // If the token kind is EOM, the error has already been diagnosed.
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000359 if (FilenameTok.is(tok::eom))
Chris Lattnerb694ba72006-07-02 22:41:36 +0000360 return;
Mike Stump11289f42009-09-09 15:08:12 +0000361
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000362 // Reserve a buffer to get the spelling.
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000363 llvm::SmallString<128> FilenameBuffer;
Douglas Gregordc970f02010-03-16 22:30:13 +0000364 bool Invalid = false;
365 llvm::StringRef Filename = getSpelling(FilenameTok, FilenameBuffer, &Invalid);
366 if (Invalid)
367 return;
Mike Stump11289f42009-09-09 15:08:12 +0000368
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000369 bool isAngled =
370 GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000371 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
372 // error.
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000373 if (Filename.empty())
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000374 return;
Mike Stump11289f42009-09-09 15:08:12 +0000375
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000376 // Search include directories for this file.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000377 const DirectoryLookup *CurDir;
Chris Lattnerfde85352010-01-22 00:14:44 +0000378 const FileEntry *File = LookupFile(Filename, isAngled, 0, CurDir);
Chris Lattner97b8e842008-11-18 08:02:48 +0000379 if (File == 0) {
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000380 Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
Chris Lattner97b8e842008-11-18 08:02:48 +0000381 return;
382 }
Mike Stump11289f42009-09-09 15:08:12 +0000383
Chris Lattnerd32480d2009-01-17 06:22:33 +0000384 const FileEntry *CurFile = getCurrentFileLexer()->getFileEntry();
Chris Lattnerb694ba72006-07-02 22:41:36 +0000385
386 // If this file is older than the file it depends on, emit a diagnostic.
387 if (CurFile && CurFile->getModificationTime() < File->getModificationTime()) {
388 // Lex tokens at the end of the message and include them in the message.
389 std::string Message;
390 Lex(DependencyTok);
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000391 while (DependencyTok.isNot(tok::eom)) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000392 Message += getSpelling(DependencyTok) + " ";
393 Lex(DependencyTok);
394 }
Mike Stump11289f42009-09-09 15:08:12 +0000395
Chris Lattnerf0b04972010-09-05 23:16:09 +0000396 // Remove the trailing ' ' if present.
397 if (!Message.empty())
398 Message.erase(Message.end()-1);
Chris Lattner97b8e842008-11-18 08:02:48 +0000399 Diag(FilenameTok, diag::pp_out_of_date_dependency) << Message;
Chris Lattnerb694ba72006-07-02 22:41:36 +0000400 }
401}
402
Chris Lattner2ff698d2009-01-16 08:21:25 +0000403/// HandlePragmaComment - Handle the microsoft #pragma comment extension. The
404/// syntax is:
405/// #pragma comment(linker, "foo")
406/// 'linker' is one of five identifiers: compiler, exestr, lib, linker, user.
407/// "foo" is a string, which is fully macro expanded, and permits string
Gabor Greif31a082f2009-03-17 11:39:38 +0000408/// concatenation, embedded escape characters etc. See MSDN for more details.
Chris Lattner2ff698d2009-01-16 08:21:25 +0000409void Preprocessor::HandlePragmaComment(Token &Tok) {
410 SourceLocation CommentLoc = Tok.getLocation();
411 Lex(Tok);
412 if (Tok.isNot(tok::l_paren)) {
413 Diag(CommentLoc, diag::err_pragma_comment_malformed);
414 return;
415 }
Mike Stump11289f42009-09-09 15:08:12 +0000416
Chris Lattner2ff698d2009-01-16 08:21:25 +0000417 // Read the identifier.
418 Lex(Tok);
419 if (Tok.isNot(tok::identifier)) {
420 Diag(CommentLoc, diag::err_pragma_comment_malformed);
421 return;
422 }
Mike Stump11289f42009-09-09 15:08:12 +0000423
Chris Lattner2ff698d2009-01-16 08:21:25 +0000424 // Verify that this is one of the 5 whitelisted options.
425 // FIXME: warn that 'exestr' is deprecated.
426 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000427 if (!II->isStr("compiler") && !II->isStr("exestr") && !II->isStr("lib") &&
Chris Lattner2ff698d2009-01-16 08:21:25 +0000428 !II->isStr("linker") && !II->isStr("user")) {
429 Diag(Tok.getLocation(), diag::err_pragma_comment_unknown_kind);
430 return;
431 }
Mike Stump11289f42009-09-09 15:08:12 +0000432
Chris Lattner262d4e32009-01-16 18:59:23 +0000433 // Read the optional string if present.
Chris Lattner2ff698d2009-01-16 08:21:25 +0000434 Lex(Tok);
Chris Lattner262d4e32009-01-16 18:59:23 +0000435 std::string ArgumentString;
Chris Lattner2ff698d2009-01-16 08:21:25 +0000436 if (Tok.is(tok::comma)) {
Chris Lattner262d4e32009-01-16 18:59:23 +0000437 Lex(Tok); // eat the comma.
Chris Lattner2ff698d2009-01-16 08:21:25 +0000438
439 // We need at least one string.
Chris Lattner504af112009-04-19 23:16:58 +0000440 if (Tok.isNot(tok::string_literal)) {
Chris Lattner2ff698d2009-01-16 08:21:25 +0000441 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
442 return;
443 }
444
445 // String concatenation allows multiple strings, which can even come from
446 // macro expansion.
447 // "foo " "bar" "Baz"
Chris Lattner262d4e32009-01-16 18:59:23 +0000448 llvm::SmallVector<Token, 4> StrToks;
Chris Lattner504af112009-04-19 23:16:58 +0000449 while (Tok.is(tok::string_literal)) {
Chris Lattner262d4e32009-01-16 18:59:23 +0000450 StrToks.push_back(Tok);
Chris Lattner2ff698d2009-01-16 08:21:25 +0000451 Lex(Tok);
Chris Lattner262d4e32009-01-16 18:59:23 +0000452 }
453
454 // Concatenate and parse the strings.
455 StringLiteralParser Literal(&StrToks[0], StrToks.size(), *this);
456 assert(!Literal.AnyWide && "Didn't allow wide strings in");
457 if (Literal.hadError)
458 return;
459 if (Literal.Pascal) {
460 Diag(StrToks[0].getLocation(), diag::err_pragma_comment_malformed);
461 return;
462 }
463
464 ArgumentString = std::string(Literal.GetString(),
465 Literal.GetString()+Literal.GetStringLength());
Chris Lattner2ff698d2009-01-16 08:21:25 +0000466 }
Mike Stump11289f42009-09-09 15:08:12 +0000467
Chris Lattner262d4e32009-01-16 18:59:23 +0000468 // FIXME: If the kind is "compiler" warn if the string is present (it is
469 // ignored).
470 // FIXME: 'lib' requires a comment string.
471 // FIXME: 'linker' requires a comment string, and has a specific list of
472 // things that are allowable.
Mike Stump11289f42009-09-09 15:08:12 +0000473
Chris Lattner2ff698d2009-01-16 08:21:25 +0000474 if (Tok.isNot(tok::r_paren)) {
475 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
476 return;
477 }
Chris Lattner262d4e32009-01-16 18:59:23 +0000478 Lex(Tok); // eat the r_paren.
Chris Lattner2ff698d2009-01-16 08:21:25 +0000479
480 if (Tok.isNot(tok::eom)) {
481 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
482 return;
483 }
Mike Stump11289f42009-09-09 15:08:12 +0000484
Chris Lattner262d4e32009-01-16 18:59:23 +0000485 // If the pragma is lexically sound, notify any interested PPCallbacks.
Chris Lattnerf49775d2009-01-16 19:01:46 +0000486 if (Callbacks)
487 Callbacks->PragmaComment(CommentLoc, II, ArgumentString);
Chris Lattner2ff698d2009-01-16 08:21:25 +0000488}
489
Michael J. Spencera0a820f2010-09-27 06:19:02 +0000490/// HandlePragmaMessage - Handle the microsoft and gcc #pragma message
491/// extension. The syntax is:
492/// #pragma message(string)
493/// OR, in GCC mode:
494/// #pragma message string
495/// string is a string, which is fully macro expanded, and permits string
496/// concatenation, embedded escape characters, etc... See MSDN for more details.
Chris Lattner30c924b2010-06-26 17:11:39 +0000497void Preprocessor::HandlePragmaMessage(Token &Tok) {
498 SourceLocation MessageLoc = Tok.getLocation();
499 Lex(Tok);
Michael J. Spencera0a820f2010-09-27 06:19:02 +0000500 bool ExpectClosingParen = false;
Michael J. Spencer4362a1c2010-09-27 06:34:47 +0000501 switch (Tok.getKind()) {
Michael J. Spencera0a820f2010-09-27 06:19:02 +0000502 case tok::l_paren:
503 // We have a MSVC style pragma message.
504 ExpectClosingParen = true;
505 // Read the string.
506 Lex(Tok);
507 break;
508 case tok::string_literal:
509 // We have a GCC style pragma message, and we just read the string.
510 break;
511 default:
Chris Lattner30c924b2010-06-26 17:11:39 +0000512 Diag(MessageLoc, diag::err_pragma_message_malformed);
513 return;
514 }
Chris Lattner2ff698d2009-01-16 08:21:25 +0000515
Chris Lattner30c924b2010-06-26 17:11:39 +0000516 // We need at least one string.
517 if (Tok.isNot(tok::string_literal)) {
518 Diag(Tok.getLocation(), diag::err_pragma_message_malformed);
519 return;
520 }
521
522 // String concatenation allows multiple strings, which can even come from
523 // macro expansion.
524 // "foo " "bar" "Baz"
525 llvm::SmallVector<Token, 4> StrToks;
526 while (Tok.is(tok::string_literal)) {
527 StrToks.push_back(Tok);
528 Lex(Tok);
529 }
530
531 // Concatenate and parse the strings.
532 StringLiteralParser Literal(&StrToks[0], StrToks.size(), *this);
533 assert(!Literal.AnyWide && "Didn't allow wide strings in");
534 if (Literal.hadError)
535 return;
536 if (Literal.Pascal) {
537 Diag(StrToks[0].getLocation(), diag::err_pragma_message_malformed);
538 return;
539 }
540
541 llvm::StringRef MessageString(Literal.GetString(), Literal.GetStringLength());
542
Michael J. Spencera0a820f2010-09-27 06:19:02 +0000543 if (ExpectClosingParen) {
544 if (Tok.isNot(tok::r_paren)) {
545 Diag(Tok.getLocation(), diag::err_pragma_message_malformed);
546 return;
547 }
548 Lex(Tok); // eat the r_paren.
Chris Lattner30c924b2010-06-26 17:11:39 +0000549 }
Chris Lattner30c924b2010-06-26 17:11:39 +0000550
551 if (Tok.isNot(tok::eom)) {
552 Diag(Tok.getLocation(), diag::err_pragma_message_malformed);
553 return;
554 }
555
556 // Output the message.
557 Diag(MessageLoc, diag::warn_pragma_message) << MessageString;
558
559 // If the pragma is lexically sound, notify any interested PPCallbacks.
560 if (Callbacks)
561 Callbacks->PragmaMessage(MessageLoc, MessageString);
562}
Chris Lattner2ff698d2009-01-16 08:21:25 +0000563
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000564/// ParsePragmaPushOrPopMacro - Handle parsing of pragma push_macro/pop_macro.
565/// Return the IdentifierInfo* associated with the macro to push or pop.
566IdentifierInfo *Preprocessor::ParsePragmaPushOrPopMacro(Token &Tok) {
567 // Remember the pragma token location.
568 Token PragmaTok = Tok;
569
570 // Read the '('.
571 Lex(Tok);
572 if (Tok.isNot(tok::l_paren)) {
573 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
574 << getSpelling(PragmaTok);
575 return 0;
576 }
577
578 // Read the macro name string.
579 Lex(Tok);
580 if (Tok.isNot(tok::string_literal)) {
581 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
582 << getSpelling(PragmaTok);
583 return 0;
584 }
585
586 // Remember the macro string.
587 std::string StrVal = getSpelling(Tok);
588
589 // Read the ')'.
590 Lex(Tok);
591 if (Tok.isNot(tok::r_paren)) {
592 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
593 << getSpelling(PragmaTok);
594 return 0;
595 }
596
597 assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
598 "Invalid string token!");
599
600 // Create a Token from the string.
601 Token MacroTok;
602 MacroTok.startToken();
Abramo Bagnaraea4f7c72010-12-22 08:23:18 +0000603 MacroTok.setKind(tok::raw_identifier);
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000604 CreateString(&StrVal[1], StrVal.size() - 2, MacroTok);
605
606 // Get the IdentifierInfo of MacroToPushTok.
607 return LookUpIdentifierInfo(MacroTok);
608}
609
610/// HandlePragmaPushMacro - Handle #pragma push_macro.
611/// The syntax is:
612/// #pragma push_macro("macro")
613void Preprocessor::HandlePragmaPushMacro(Token &PushMacroTok) {
614 // Parse the pragma directive and get the macro IdentifierInfo*.
615 IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PushMacroTok);
616 if (!IdentInfo) return;
617
618 // Get the MacroInfo associated with IdentInfo.
619 MacroInfo *MI = getMacroInfo(IdentInfo);
620
621 MacroInfo *MacroCopyToPush = 0;
622 if (MI) {
623 // Make a clone of MI.
624 MacroCopyToPush = CloneMacroInfo(*MI);
625
626 // Allow the original MacroInfo to be redefined later.
627 MI->setIsAllowRedefinitionsWithoutWarning(true);
628 }
629
630 // Push the cloned MacroInfo so we can retrieve it later.
631 PragmaPushMacroInfo[IdentInfo].push_back(MacroCopyToPush);
632}
633
Ted Kremenek6339f1c2010-10-19 17:40:50 +0000634/// HandlePragmaPopMacro - Handle #pragma pop_macro.
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000635/// The syntax is:
636/// #pragma pop_macro("macro")
637void Preprocessor::HandlePragmaPopMacro(Token &PopMacroTok) {
638 SourceLocation MessageLoc = PopMacroTok.getLocation();
639
640 // Parse the pragma directive and get the macro IdentifierInfo*.
641 IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PopMacroTok);
642 if (!IdentInfo) return;
643
644 // Find the vector<MacroInfo*> associated with the macro.
645 llvm::DenseMap<IdentifierInfo*, std::vector<MacroInfo*> >::iterator iter =
646 PragmaPushMacroInfo.find(IdentInfo);
647 if (iter != PragmaPushMacroInfo.end()) {
648 // Release the MacroInfo currently associated with IdentInfo.
649 MacroInfo *CurrentMI = getMacroInfo(IdentInfo);
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000650 if (CurrentMI) {
651 if (CurrentMI->isWarnIfUnused())
652 WarnUnusedMacroLocs.erase(CurrentMI->getDefinitionLoc());
653 ReleaseMacroInfo(CurrentMI);
654 }
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000655
656 // Get the MacroInfo we want to reinstall.
657 MacroInfo *MacroToReInstall = iter->second.back();
658
659 // Reinstall the previously pushed macro.
660 setMacroInfo(IdentInfo, MacroToReInstall);
661
662 // Pop PragmaPushMacroInfo stack.
663 iter->second.pop_back();
664 if (iter->second.size() == 0)
665 PragmaPushMacroInfo.erase(iter);
666 } else {
667 Diag(MessageLoc, diag::warn_pragma_pop_macro_no_push)
668 << IdentInfo->getName();
669 }
670}
Chris Lattnerb694ba72006-07-02 22:41:36 +0000671
672/// AddPragmaHandler - Add the specified pragma handler to the preprocessor.
673/// If 'Namespace' is non-null, then it is a token required to exist on the
674/// pragma line before the pragma string starts, e.g. "STDC" or "GCC".
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000675void Preprocessor::AddPragmaHandler(llvm::StringRef Namespace,
Chris Lattnerb694ba72006-07-02 22:41:36 +0000676 PragmaHandler *Handler) {
677 PragmaNamespace *InsertNS = PragmaHandlers;
Mike Stump11289f42009-09-09 15:08:12 +0000678
Chris Lattnerb694ba72006-07-02 22:41:36 +0000679 // If this is specified to be in a namespace, step down into it.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000680 if (!Namespace.empty()) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000681 // If there is already a pragma handler with the name of this namespace,
682 // we either have an error (directive with the same name as a namespace) or
683 // we already have the namespace to insert into.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000684 if (PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace)) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000685 InsertNS = Existing->getIfNamespace();
686 assert(InsertNS != 0 && "Cannot have a pragma namespace and pragma"
687 " handler with the same name!");
688 } else {
689 // Otherwise, this namespace doesn't exist yet, create and insert the
690 // handler for it.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000691 InsertNS = new PragmaNamespace(Namespace);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000692 PragmaHandlers->AddPragma(InsertNS);
693 }
694 }
Mike Stump11289f42009-09-09 15:08:12 +0000695
Chris Lattnerb694ba72006-07-02 22:41:36 +0000696 // Check to make sure we don't already have a pragma for this identifier.
697 assert(!InsertNS->FindHandler(Handler->getName()) &&
698 "Pragma handler already exists for this identifier!");
699 InsertNS->AddPragma(Handler);
700}
701
Daniel Dunbar40596532008-10-04 19:17:46 +0000702/// RemovePragmaHandler - Remove the specific pragma handler from the
703/// preprocessor. If \arg Namespace is non-null, then it should be the
704/// namespace that \arg Handler was added to. It is an error to remove
705/// a handler that has not been registered.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000706void Preprocessor::RemovePragmaHandler(llvm::StringRef Namespace,
Daniel Dunbar40596532008-10-04 19:17:46 +0000707 PragmaHandler *Handler) {
708 PragmaNamespace *NS = PragmaHandlers;
Mike Stump11289f42009-09-09 15:08:12 +0000709
Daniel Dunbar40596532008-10-04 19:17:46 +0000710 // If this is specified to be in a namespace, step down into it.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000711 if (!Namespace.empty()) {
712 PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace);
Daniel Dunbar40596532008-10-04 19:17:46 +0000713 assert(Existing && "Namespace containing handler does not exist!");
714
715 NS = Existing->getIfNamespace();
716 assert(NS && "Invalid namespace, registered as a regular pragma handler!");
717 }
718
719 NS->RemovePragmaHandler(Handler);
Mike Stump11289f42009-09-09 15:08:12 +0000720
Daniel Dunbar40596532008-10-04 19:17:46 +0000721 // If this is a non-default namespace and it is now empty, remove
722 // it.
723 if (NS != PragmaHandlers && NS->IsEmpty())
724 PragmaHandlers->RemovePragmaHandler(NS);
725}
726
Peter Collingbourne3bffa522011-02-14 01:42:24 +0000727bool Preprocessor::LexOnOffSwitch(tok::OnOffSwitch &Result) {
728 Token Tok;
729 LexUnexpandedToken(Tok);
730
731 if (Tok.isNot(tok::identifier)) {
732 Diag(Tok, diag::ext_on_off_switch_syntax);
733 return true;
734 }
735 IdentifierInfo *II = Tok.getIdentifierInfo();
736 if (II->isStr("ON"))
737 Result = tok::OOS_ON;
738 else if (II->isStr("OFF"))
739 Result = tok::OOS_OFF;
740 else if (II->isStr("DEFAULT"))
741 Result = tok::OOS_DEFAULT;
742 else {
743 Diag(Tok, diag::ext_on_off_switch_syntax);
744 return true;
745 }
746
747 // Verify that this is followed by EOM.
748 LexUnexpandedToken(Tok);
749 if (Tok.isNot(tok::eom))
750 Diag(Tok, diag::ext_pragma_syntax_eom);
751 return false;
752}
753
Chris Lattnerb694ba72006-07-02 22:41:36 +0000754namespace {
Chris Lattnerc2383312007-12-19 19:38:36 +0000755/// PragmaOnceHandler - "#pragma once" marks the file as atomically included.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000756struct PragmaOnceHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000757 PragmaOnceHandler() : PragmaHandler("once") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000758 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
759 Token &OnceTok) {
Chris Lattnerce2ab6f2009-04-14 05:07:49 +0000760 PP.CheckEndOfDirective("pragma once");
Chris Lattnerb694ba72006-07-02 22:41:36 +0000761 PP.HandlePragmaOnce(OnceTok);
762 }
763};
764
Chris Lattnerc2383312007-12-19 19:38:36 +0000765/// PragmaMarkHandler - "#pragma mark ..." is ignored by the compiler, and the
766/// rest of the line is not lexed.
767struct PragmaMarkHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000768 PragmaMarkHandler() : PragmaHandler("mark") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000769 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
770 Token &MarkTok) {
Chris Lattnerc2383312007-12-19 19:38:36 +0000771 PP.HandlePragmaMark();
772 }
773};
774
775/// PragmaPoisonHandler - "#pragma poison x" marks x as not usable.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000776struct PragmaPoisonHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000777 PragmaPoisonHandler() : PragmaHandler("poison") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000778 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
779 Token &PoisonTok) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000780 PP.HandlePragmaPoison(PoisonTok);
781 }
782};
783
Chris Lattnerc2383312007-12-19 19:38:36 +0000784/// PragmaSystemHeaderHandler - "#pragma system_header" marks the current file
785/// as a system header, which silences warnings in it.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000786struct PragmaSystemHeaderHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000787 PragmaSystemHeaderHandler() : PragmaHandler("system_header") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000788 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
789 Token &SHToken) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000790 PP.HandlePragmaSystemHeader(SHToken);
Chris Lattnerce2ab6f2009-04-14 05:07:49 +0000791 PP.CheckEndOfDirective("pragma");
Chris Lattnerb694ba72006-07-02 22:41:36 +0000792 }
793};
794struct PragmaDependencyHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000795 PragmaDependencyHandler() : PragmaHandler("dependency") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000796 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
797 Token &DepToken) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000798 PP.HandlePragmaDependency(DepToken);
799 }
800};
Mike Stump11289f42009-09-09 15:08:12 +0000801
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000802struct PragmaDebugHandler : public PragmaHandler {
803 PragmaDebugHandler() : PragmaHandler("__debug") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000804 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
805 Token &DepToken) {
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000806 Token Tok;
807 PP.LexUnexpandedToken(Tok);
808 if (Tok.isNot(tok::identifier)) {
Douglas Gregor3cc26482010-08-30 15:15:34 +0000809 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000810 return;
811 }
812 IdentifierInfo *II = Tok.getIdentifierInfo();
813
Daniel Dunbarf2cf3292010-08-17 22:32:48 +0000814 if (II->isStr("assert")) {
815 assert(0 && "This is an assertion!");
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000816 } else if (II->isStr("crash")) {
Daniel Dunbarf2cf3292010-08-17 22:32:48 +0000817 *(volatile int*) 0x11 = 0;
818 } else if (II->isStr("llvm_fatal_error")) {
819 llvm::report_fatal_error("#pragma clang __debug llvm_fatal_error");
820 } else if (II->isStr("llvm_unreachable")) {
821 llvm_unreachable("#pragma clang __debug llvm_unreachable");
822 } else if (II->isStr("overflow_stack")) {
823 DebugOverflowStack();
Daniel Dunbar211a7872010-08-18 23:09:23 +0000824 } else if (II->isStr("handle_crash")) {
825 llvm::CrashRecoveryContext *CRC =llvm::CrashRecoveryContext::GetCurrent();
826 if (CRC)
827 CRC->HandleCrash();
Daniel Dunbarf2cf3292010-08-17 22:32:48 +0000828 } else {
829 PP.Diag(Tok, diag::warn_pragma_debug_unexpected_command)
830 << II->getName();
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000831 }
832 }
833
834 void DebugOverflowStack() {
835 DebugOverflowStack();
836 }
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000837};
838
Chris Lattner504af112009-04-19 23:16:58 +0000839/// PragmaDiagnosticHandler - e.g. '#pragma GCC diagnostic ignored "-Wformat"'
840struct PragmaDiagnosticHandler : public PragmaHandler {
Chris Lattnerfb42a182009-07-12 21:18:45 +0000841public:
Douglas Gregor3cc26482010-08-30 15:15:34 +0000842 explicit PragmaDiagnosticHandler() : PragmaHandler("diagnostic") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000843 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
844 Token &DiagToken) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000845 SourceLocation DiagLoc = DiagToken.getLocation();
Chris Lattner504af112009-04-19 23:16:58 +0000846 Token Tok;
847 PP.LexUnexpandedToken(Tok);
848 if (Tok.isNot(tok::identifier)) {
Douglas Gregor3cc26482010-08-30 15:15:34 +0000849 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Chris Lattner504af112009-04-19 23:16:58 +0000850 return;
851 }
852 IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000853
Chris Lattner504af112009-04-19 23:16:58 +0000854 diag::Mapping Map;
855 if (II->isStr("warning"))
856 Map = diag::MAP_WARNING;
857 else if (II->isStr("error"))
858 Map = diag::MAP_ERROR;
859 else if (II->isStr("ignored"))
860 Map = diag::MAP_IGNORE;
861 else if (II->isStr("fatal"))
862 Map = diag::MAP_FATAL;
Douglas Gregor3cc26482010-08-30 15:15:34 +0000863 else if (II->isStr("pop")) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000864 if (!PP.getDiagnostics().popMappings(DiagLoc))
Douglas Gregor3cc26482010-08-30 15:15:34 +0000865 PP.Diag(Tok, diag::warn_pragma_diagnostic_cannot_pop);
Chris Lattnerfb42a182009-07-12 21:18:45 +0000866
Douglas Gregor3cc26482010-08-30 15:15:34 +0000867 return;
868 } else if (II->isStr("push")) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000869 PP.getDiagnostics().pushMappings(DiagLoc);
Chris Lattnerfb42a182009-07-12 21:18:45 +0000870 return;
871 } else {
Douglas Gregor3cc26482010-08-30 15:15:34 +0000872 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Chris Lattner504af112009-04-19 23:16:58 +0000873 return;
874 }
Mike Stump11289f42009-09-09 15:08:12 +0000875
Chris Lattner504af112009-04-19 23:16:58 +0000876 PP.LexUnexpandedToken(Tok);
877
878 // We need at least one string.
879 if (Tok.isNot(tok::string_literal)) {
880 PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token);
881 return;
882 }
Mike Stump11289f42009-09-09 15:08:12 +0000883
Chris Lattner504af112009-04-19 23:16:58 +0000884 // String concatenation allows multiple strings, which can even come from
885 // macro expansion.
886 // "foo " "bar" "Baz"
887 llvm::SmallVector<Token, 4> StrToks;
888 while (Tok.is(tok::string_literal)) {
889 StrToks.push_back(Tok);
890 PP.LexUnexpandedToken(Tok);
891 }
Mike Stump11289f42009-09-09 15:08:12 +0000892
Chris Lattner504af112009-04-19 23:16:58 +0000893 if (Tok.isNot(tok::eom)) {
894 PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token);
895 return;
896 }
Mike Stump11289f42009-09-09 15:08:12 +0000897
Chris Lattner504af112009-04-19 23:16:58 +0000898 // Concatenate and parse the strings.
899 StringLiteralParser Literal(&StrToks[0], StrToks.size(), PP);
900 assert(!Literal.AnyWide && "Didn't allow wide strings in");
901 if (Literal.hadError)
902 return;
903 if (Literal.Pascal) {
Douglas Gregor3cc26482010-08-30 15:15:34 +0000904 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Chris Lattner504af112009-04-19 23:16:58 +0000905 return;
906 }
Chris Lattnerfb42a182009-07-12 21:18:45 +0000907
Chris Lattner504af112009-04-19 23:16:58 +0000908 std::string WarningName(Literal.GetString(),
909 Literal.GetString()+Literal.GetStringLength());
910
911 if (WarningName.size() < 3 || WarningName[0] != '-' ||
912 WarningName[1] != 'W') {
913 PP.Diag(StrToks[0].getLocation(),
914 diag::warn_pragma_diagnostic_invalid_option);
915 return;
916 }
Mike Stump11289f42009-09-09 15:08:12 +0000917
Chris Lattner504af112009-04-19 23:16:58 +0000918 if (PP.getDiagnostics().setDiagnosticGroupMapping(WarningName.c_str()+2,
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000919 Map, DiagLoc))
Chris Lattner504af112009-04-19 23:16:58 +0000920 PP.Diag(StrToks[0].getLocation(),
921 diag::warn_pragma_diagnostic_unknown_warning) << WarningName;
922 }
923};
Mike Stump11289f42009-09-09 15:08:12 +0000924
Chris Lattner2ff698d2009-01-16 08:21:25 +0000925/// PragmaCommentHandler - "#pragma comment ...".
926struct PragmaCommentHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000927 PragmaCommentHandler() : PragmaHandler("comment") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000928 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
929 Token &CommentTok) {
Chris Lattner2ff698d2009-01-16 08:21:25 +0000930 PP.HandlePragmaComment(CommentTok);
931 }
932};
Mike Stump11289f42009-09-09 15:08:12 +0000933
Chris Lattner30c924b2010-06-26 17:11:39 +0000934/// PragmaMessageHandler - "#pragma message("...")".
935struct PragmaMessageHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000936 PragmaMessageHandler() : PragmaHandler("message") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000937 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
938 Token &CommentTok) {
Chris Lattner30c924b2010-06-26 17:11:39 +0000939 PP.HandlePragmaMessage(CommentTok);
940 }
941};
942
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000943/// PragmaPushMacroHandler - "#pragma push_macro" saves the value of the
944/// macro on the top of the stack.
945struct PragmaPushMacroHandler : public PragmaHandler {
946 PragmaPushMacroHandler() : PragmaHandler("push_macro") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000947 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
948 Token &PushMacroTok) {
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000949 PP.HandlePragmaPushMacro(PushMacroTok);
950 }
951};
952
953
954/// PragmaPopMacroHandler - "#pragma pop_macro" sets the value of the
955/// macro to the value on the top of the stack.
956struct PragmaPopMacroHandler : public PragmaHandler {
957 PragmaPopMacroHandler() : PragmaHandler("pop_macro") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000958 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
959 Token &PopMacroTok) {
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000960 PP.HandlePragmaPopMacro(PopMacroTok);
961 }
962};
963
Chris Lattner958ee042009-04-19 21:20:35 +0000964// Pragma STDC implementations.
Chris Lattner02ef4e32009-04-19 21:50:08 +0000965
Chris Lattner958ee042009-04-19 21:20:35 +0000966/// PragmaSTDC_FENV_ACCESSHandler - "#pragma STDC FENV_ACCESS ...".
967struct PragmaSTDC_FENV_ACCESSHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000968 PragmaSTDC_FENV_ACCESSHandler() : PragmaHandler("FENV_ACCESS") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000969 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
970 Token &Tok) {
Peter Collingbourne3bffa522011-02-14 01:42:24 +0000971 tok::OnOffSwitch OOS;
972 if (PP.LexOnOffSwitch(OOS))
973 return;
974 if (OOS == tok::OOS_ON)
Chris Lattnerdf222682009-04-19 21:55:32 +0000975 PP.Diag(Tok, diag::warn_stdc_fenv_access_not_supported);
Chris Lattner958ee042009-04-19 21:20:35 +0000976 }
977};
Mike Stump11289f42009-09-09 15:08:12 +0000978
Chris Lattner958ee042009-04-19 21:20:35 +0000979/// PragmaSTDC_CX_LIMITED_RANGEHandler - "#pragma STDC CX_LIMITED_RANGE ...".
980struct PragmaSTDC_CX_LIMITED_RANGEHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000981 PragmaSTDC_CX_LIMITED_RANGEHandler()
982 : PragmaHandler("CX_LIMITED_RANGE") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000983 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
984 Token &Tok) {
Peter Collingbourne3bffa522011-02-14 01:42:24 +0000985 tok::OnOffSwitch OOS;
986 PP.LexOnOffSwitch(OOS);
Chris Lattner958ee042009-04-19 21:20:35 +0000987 }
988};
Mike Stump11289f42009-09-09 15:08:12 +0000989
Chris Lattner958ee042009-04-19 21:20:35 +0000990/// PragmaSTDC_UnknownHandler - "#pragma STDC ...".
991struct PragmaSTDC_UnknownHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000992 PragmaSTDC_UnknownHandler() {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000993 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
994 Token &UnknownTok) {
Chris Lattner02ef4e32009-04-19 21:50:08 +0000995 // C99 6.10.6p2, unknown forms are not allowed.
Chris Lattnera0b1f762009-04-19 21:25:37 +0000996 PP.Diag(UnknownTok, diag::ext_stdc_pragma_ignored);
Chris Lattner958ee042009-04-19 21:20:35 +0000997 }
998};
Mike Stump11289f42009-09-09 15:08:12 +0000999
Chris Lattnerb694ba72006-07-02 22:41:36 +00001000} // end anonymous namespace
1001
1002
1003/// RegisterBuiltinPragmas - Install the standard preprocessor pragmas:
1004/// #pragma GCC poison/system_header/dependency and #pragma once.
1005void Preprocessor::RegisterBuiltinPragmas() {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001006 AddPragmaHandler(new PragmaOnceHandler());
1007 AddPragmaHandler(new PragmaMarkHandler());
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001008 AddPragmaHandler(new PragmaPushMacroHandler());
1009 AddPragmaHandler(new PragmaPopMacroHandler());
Michael J. Spencera0a820f2010-09-27 06:19:02 +00001010 AddPragmaHandler(new PragmaMessageHandler());
Mike Stump11289f42009-09-09 15:08:12 +00001011
Chris Lattnerb61448d2009-05-12 18:21:11 +00001012 // #pragma GCC ...
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001013 AddPragmaHandler("GCC", new PragmaPoisonHandler());
1014 AddPragmaHandler("GCC", new PragmaSystemHeaderHandler());
1015 AddPragmaHandler("GCC", new PragmaDependencyHandler());
Douglas Gregor3cc26482010-08-30 15:15:34 +00001016 AddPragmaHandler("GCC", new PragmaDiagnosticHandler());
Chris Lattnerb61448d2009-05-12 18:21:11 +00001017 // #pragma clang ...
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001018 AddPragmaHandler("clang", new PragmaPoisonHandler());
1019 AddPragmaHandler("clang", new PragmaSystemHeaderHandler());
Daniel Dunbarb8068c32010-07-28 15:40:33 +00001020 AddPragmaHandler("clang", new PragmaDebugHandler());
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001021 AddPragmaHandler("clang", new PragmaDependencyHandler());
Douglas Gregor3cc26482010-08-30 15:15:34 +00001022 AddPragmaHandler("clang", new PragmaDiagnosticHandler());
Chris Lattnerb61448d2009-05-12 18:21:11 +00001023
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001024 AddPragmaHandler("STDC", new PragmaSTDC_FENV_ACCESSHandler());
1025 AddPragmaHandler("STDC", new PragmaSTDC_CX_LIMITED_RANGEHandler());
Chris Lattner958ee042009-04-19 21:20:35 +00001026 AddPragmaHandler("STDC", new PragmaSTDC_UnknownHandler());
Mike Stump11289f42009-09-09 15:08:12 +00001027
Chris Lattner2ff698d2009-01-16 08:21:25 +00001028 // MS extensions.
Chris Lattner30c924b2010-06-26 17:11:39 +00001029 if (Features.Microsoft) {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001030 AddPragmaHandler(new PragmaCommentHandler());
Chris Lattner30c924b2010-06-26 17:11:39 +00001031 }
Chris Lattnerb694ba72006-07-02 22:41:36 +00001032}