blob: de713ebae60917ded8b2db344d25ed1a011a144f [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
38void EmptyPragmaHandler::HandlePragma(Preprocessor &PP, Token &FirstToken) {}
39
40//===----------------------------------------------------------------------===//
Chris Lattner2e155302006-07-03 05:34:41 +000041// PragmaNamespace Implementation.
42//===----------------------------------------------------------------------===//
43
44
45PragmaNamespace::~PragmaNamespace() {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000046 for (llvm::StringMap<PragmaHandler*>::iterator
47 I = Handlers.begin(), E = Handlers.end(); I != E; ++I)
48 delete I->second;
Chris Lattner2e155302006-07-03 05:34:41 +000049}
50
51/// FindHandler - Check to see if there is already a handler for the
52/// specified name. If not, return the handler for the null identifier if it
53/// exists, otherwise return null. If IgnoreNull is true (the default) then
54/// the null handler isn't returned on failure to match.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000055PragmaHandler *PragmaNamespace::FindHandler(llvm::StringRef Name,
Chris Lattner2e155302006-07-03 05:34:41 +000056 bool IgnoreNull) const {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000057 if (PragmaHandler *Handler = Handlers.lookup(Name))
58 return Handler;
59 return IgnoreNull ? 0 : Handlers.lookup(llvm::StringRef());
60}
Mike Stump11289f42009-09-09 15:08:12 +000061
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000062void PragmaNamespace::AddPragma(PragmaHandler *Handler) {
63 assert(!Handlers.lookup(Handler->getName()) &&
64 "A handler with this name is already registered in this namespace");
65 llvm::StringMapEntry<PragmaHandler *> &Entry =
66 Handlers.GetOrCreateValue(Handler->getName());
67 Entry.setValue(Handler);
Chris Lattner2e155302006-07-03 05:34:41 +000068}
69
Daniel Dunbar40596532008-10-04 19:17:46 +000070void PragmaNamespace::RemovePragmaHandler(PragmaHandler *Handler) {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000071 assert(Handlers.lookup(Handler->getName()) &&
72 "Handler not registered in this namespace");
73 Handlers.erase(Handler->getName());
Daniel Dunbar40596532008-10-04 19:17:46 +000074}
75
Chris Lattner146762e2007-07-20 16:59:19 +000076void PragmaNamespace::HandlePragma(Preprocessor &PP, Token &Tok) {
Chris Lattnerb8761832006-06-24 21:31:03 +000077 // Read the 'namespace' that the directive is in, e.g. STDC. Do not macro
78 // expand it, the user can have a STDC #define, that should not affect this.
79 PP.LexUnexpandedToken(Tok);
Mike Stump11289f42009-09-09 15:08:12 +000080
Chris Lattnerb8761832006-06-24 21:31:03 +000081 // Get the handler for this token. If there is no handler, ignore the pragma.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000082 PragmaHandler *Handler
83 = FindHandler(Tok.getIdentifierInfo() ? Tok.getIdentifierInfo()->getName()
84 : llvm::StringRef(),
85 /*IgnoreNull=*/false);
Chris Lattner21656f22009-04-19 21:10:26 +000086 if (Handler == 0) {
87 PP.Diag(Tok, diag::warn_pragma_ignored);
88 return;
89 }
Mike Stump11289f42009-09-09 15:08:12 +000090
Chris Lattnerb8761832006-06-24 21:31:03 +000091 // Otherwise, pass it down.
92 Handler->HandlePragma(PP, Tok);
93}
Chris Lattnerb694ba72006-07-02 22:41:36 +000094
Chris Lattnerb694ba72006-07-02 22:41:36 +000095//===----------------------------------------------------------------------===//
96// Preprocessor Pragma Directive Handling.
97//===----------------------------------------------------------------------===//
98
99/// HandlePragmaDirective - The "#pragma" directive has been parsed. Lex the
100/// rest of the pragma, passing it to the registered pragma handlers.
101void Preprocessor::HandlePragmaDirective() {
102 ++NumPragma;
Mike Stump11289f42009-09-09 15:08:12 +0000103
Chris Lattnerb694ba72006-07-02 22:41:36 +0000104 // Invoke the first level of pragma handlers which reads the namespace id.
Chris Lattner146762e2007-07-20 16:59:19 +0000105 Token Tok;
Chris Lattnerb694ba72006-07-02 22:41:36 +0000106 PragmaHandlers->HandlePragma(*this, Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000107
Chris Lattnerb694ba72006-07-02 22:41:36 +0000108 // If the pragma handler didn't read the rest of the line, consume it now.
Chris Lattnere7e65942009-06-18 05:55:53 +0000109 if (CurPPLexer && CurPPLexer->ParsingPreprocessorDirective)
Chris Lattnerb694ba72006-07-02 22:41:36 +0000110 DiscardUntilEndOfDirective();
111}
112
113/// Handle_Pragma - Read a _Pragma directive, slice it up, process it, then
114/// return the first token after the directive. The _Pragma token has just
115/// been read into 'Tok'.
Chris Lattner146762e2007-07-20 16:59:19 +0000116void Preprocessor::Handle_Pragma(Token &Tok) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000117 // Remember the pragma token location.
118 SourceLocation PragmaLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000119
Chris Lattnerb694ba72006-07-02 22:41:36 +0000120 // Read the '('.
121 Lex(Tok);
Chris Lattner907dfe92008-11-18 07:59:24 +0000122 if (Tok.isNot(tok::l_paren)) {
123 Diag(PragmaLoc, diag::err__Pragma_malformed);
124 return;
125 }
Chris Lattnerb694ba72006-07-02 22:41:36 +0000126
127 // Read the '"..."'.
128 Lex(Tok);
Chris Lattner907dfe92008-11-18 07:59:24 +0000129 if (Tok.isNot(tok::string_literal) && Tok.isNot(tok::wide_string_literal)) {
130 Diag(PragmaLoc, diag::err__Pragma_malformed);
131 return;
132 }
Mike Stump11289f42009-09-09 15:08:12 +0000133
Chris Lattnerb694ba72006-07-02 22:41:36 +0000134 // Remember the string.
135 std::string StrVal = getSpelling(Tok);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000136
137 // Read the ')'.
138 Lex(Tok);
Chris Lattner907dfe92008-11-18 07:59:24 +0000139 if (Tok.isNot(tok::r_paren)) {
140 Diag(PragmaLoc, diag::err__Pragma_malformed);
141 return;
142 }
Mike Stump11289f42009-09-09 15:08:12 +0000143
Chris Lattner9dc9c202009-02-15 20:52:18 +0000144 SourceLocation RParenLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000145
Chris Lattner262d4e32009-01-16 18:59:23 +0000146 // The _Pragma is lexically sound. Destringize according to C99 6.10.9.1:
147 // "The string literal is destringized by deleting the L prefix, if present,
148 // deleting the leading and trailing double-quotes, replacing each escape
149 // sequence \" by a double-quote, and replacing each escape sequence \\ by a
150 // single backslash."
Chris Lattnerb694ba72006-07-02 22:41:36 +0000151 if (StrVal[0] == 'L') // Remove L prefix.
152 StrVal.erase(StrVal.begin());
153 assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
154 "Invalid string token!");
Mike Stump11289f42009-09-09 15:08:12 +0000155
Chris Lattnerb694ba72006-07-02 22:41:36 +0000156 // Remove the front quote, replacing it with a space, so that the pragma
157 // contents appear to have a space before them.
158 StrVal[0] = ' ';
Mike Stump11289f42009-09-09 15:08:12 +0000159
Chris Lattnerfa217bd2009-03-08 08:08:45 +0000160 // Replace the terminating quote with a \n.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000161 StrVal[StrVal.size()-1] = '\n';
Mike Stump11289f42009-09-09 15:08:12 +0000162
Chris Lattnerb694ba72006-07-02 22:41:36 +0000163 // Remove escaped quotes and escapes.
164 for (unsigned i = 0, e = StrVal.size(); i != e-1; ++i) {
165 if (StrVal[i] == '\\' &&
166 (StrVal[i+1] == '\\' || StrVal[i+1] == '"')) {
167 // \\ -> '\' and \" -> '"'.
168 StrVal.erase(StrVal.begin()+i);
169 --e;
170 }
171 }
John McCall89e925d2010-08-28 22:34:47 +0000172
173 Handle_Pragma(StrVal, PragmaLoc, RParenLoc);
174
175 // Finally, return whatever came after the pragma directive.
176 return Lex(Tok);
177}
178
179/// HandleMicrosoft__pragma - Like Handle_Pragma except the pragma text
180/// is not enclosed within a string literal.
181void Preprocessor::HandleMicrosoft__pragma(Token &Tok) {
182 // Remember the pragma token location.
183 SourceLocation PragmaLoc = Tok.getLocation();
184
185 // Read the '('.
186 Lex(Tok);
187 if (Tok.isNot(tok::l_paren)) {
188 Diag(PragmaLoc, diag::err__Pragma_malformed);
189 return;
190 }
191
192 // Get the tokens enclosed within the __pragma().
193 llvm::SmallVector<Token, 32> PragmaToks;
194 int NumParens = 0;
195 Lex(Tok);
196 while (Tok.isNot(tok::eof)) {
197 if (Tok.is(tok::l_paren))
198 NumParens++;
199 else if (Tok.is(tok::r_paren) && NumParens-- == 0)
200 break;
201 PragmaToks.push_back(Tok);
202 Lex(Tok);
203 }
204
John McCall49039d42010-08-29 01:09:54 +0000205 if (Tok.is(tok::eof)) {
206 Diag(PragmaLoc, diag::err_unterminated___pragma);
207 return;
208 }
209
John McCall89e925d2010-08-28 22:34:47 +0000210 // Build the pragma string.
211 std::string StrVal = " ";
212 for (llvm::SmallVector<Token, 32>::iterator I =
213 PragmaToks.begin(), E = PragmaToks.end(); I != E; ++I) {
214 StrVal += getSpelling(*I);
215 }
216
217 SourceLocation RParenLoc = Tok.getLocation();
218
219 Handle_Pragma(StrVal, PragmaLoc, RParenLoc);
220
221 // Finally, return whatever came after the pragma directive.
222 return Lex(Tok);
223}
224
225void Preprocessor::Handle_Pragma(const std::string &StrVal,
226 SourceLocation PragmaLoc,
227 SourceLocation RParenLoc) {
Mike Stump11289f42009-09-09 15:08:12 +0000228
Chris Lattnerf918bf72006-07-19 05:01:18 +0000229 // Plop the string (including the newline and trailing null) into a buffer
230 // where we can lex it.
Chris Lattner5a7971e2009-01-26 19:29:26 +0000231 Token TmpTok;
232 TmpTok.startToken();
233 CreateString(&StrVal[0], StrVal.size(), TmpTok);
234 SourceLocation TokLoc = TmpTok.getLocation();
Chris Lattnerb694ba72006-07-02 22:41:36 +0000235
Chris Lattnerb694ba72006-07-02 22:41:36 +0000236 // Make and enter a lexer object so that we lex and expand the tokens just
237 // like any others.
Chris Lattner9dc9c202009-02-15 20:52:18 +0000238 Lexer *TL = Lexer::Create_PragmaLexer(TokLoc, PragmaLoc, RParenLoc,
Chris Lattnerfa217bd2009-03-08 08:08:45 +0000239 StrVal.size(), *this);
Chris Lattner98a53122006-07-02 23:00:20 +0000240
241 EnterSourceFileWithLexer(TL, 0);
242
Chris Lattnerb694ba72006-07-02 22:41:36 +0000243 // With everything set up, lex this as a #pragma directive.
244 HandlePragmaDirective();
Chris Lattnerb694ba72006-07-02 22:41:36 +0000245}
246
247
248
249/// HandlePragmaOnce - Handle #pragma once. OnceTok is the 'once'.
250///
Chris Lattner146762e2007-07-20 16:59:19 +0000251void Preprocessor::HandlePragmaOnce(Token &OnceTok) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000252 if (isInPrimaryFile()) {
253 Diag(OnceTok, diag::pp_pragma_once_in_main_file);
254 return;
255 }
Mike Stump11289f42009-09-09 15:08:12 +0000256
Chris Lattnerb694ba72006-07-02 22:41:36 +0000257 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000258 // Mark the file as a once-only file now.
Chris Lattnerd32480d2009-01-17 06:22:33 +0000259 HeaderInfo.MarkFileIncludeOnce(getCurrentFileLexer()->getFileEntry());
Chris Lattnerb694ba72006-07-02 22:41:36 +0000260}
261
Chris Lattnerc2383312007-12-19 19:38:36 +0000262void Preprocessor::HandlePragmaMark() {
Ted Kremenek76c34412008-11-19 22:21:33 +0000263 assert(CurPPLexer && "No current lexer?");
Chris Lattnerd9efb6e2009-06-15 05:02:34 +0000264 if (CurLexer)
265 CurLexer->ReadToEndOfLine();
266 else
267 CurPTHLexer->DiscardToEndOfLine();
Chris Lattnerc2383312007-12-19 19:38:36 +0000268}
269
270
Chris Lattnerb694ba72006-07-02 22:41:36 +0000271/// HandlePragmaPoison - Handle #pragma GCC poison. PoisonTok is the 'poison'.
272///
Chris Lattner146762e2007-07-20 16:59:19 +0000273void Preprocessor::HandlePragmaPoison(Token &PoisonTok) {
274 Token Tok;
Chris Lattner538d7f32006-07-20 04:31:52 +0000275
Chris Lattnerb694ba72006-07-02 22:41:36 +0000276 while (1) {
277 // Read the next token to poison. While doing this, pretend that we are
278 // skipping while reading the identifier to poison.
279 // This avoids errors on code like:
280 // #pragma GCC poison X
281 // #pragma GCC poison X
Ted Kremenek551c82a2008-11-18 01:12:54 +0000282 if (CurPPLexer) CurPPLexer->LexingRawMode = true;
Chris Lattnerb694ba72006-07-02 22:41:36 +0000283 LexUnexpandedToken(Tok);
Ted Kremenek551c82a2008-11-18 01:12:54 +0000284 if (CurPPLexer) CurPPLexer->LexingRawMode = false;
Mike Stump11289f42009-09-09 15:08:12 +0000285
Chris Lattnerb694ba72006-07-02 22:41:36 +0000286 // If we reached the end of line, we're done.
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000287 if (Tok.is(tok::eom)) return;
Mike Stump11289f42009-09-09 15:08:12 +0000288
Chris Lattnerb694ba72006-07-02 22:41:36 +0000289 // Can only poison identifiers.
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000290 if (Tok.isNot(tok::identifier)) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000291 Diag(Tok, diag::err_pp_invalid_poison);
292 return;
293 }
Mike Stump11289f42009-09-09 15:08:12 +0000294
Chris Lattnercefc7682006-07-08 08:28:12 +0000295 // Look up the identifier info for the token. We disabled identifier lookup
296 // by saying we're skipping contents, so we need to do this manually.
297 IdentifierInfo *II = LookUpIdentifierInfo(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000298
Chris Lattnerb694ba72006-07-02 22:41:36 +0000299 // Already poisoned.
300 if (II->isPoisoned()) continue;
Mike Stump11289f42009-09-09 15:08:12 +0000301
Chris Lattnerb694ba72006-07-02 22:41:36 +0000302 // If this is a macro identifier, emit a warning.
Chris Lattner259716a2007-10-07 08:04:56 +0000303 if (II->hasMacroDefinition())
Chris Lattnerb694ba72006-07-02 22:41:36 +0000304 Diag(Tok, diag::pp_poisoning_existing_macro);
Mike Stump11289f42009-09-09 15:08:12 +0000305
Chris Lattnerb694ba72006-07-02 22:41:36 +0000306 // Finally, poison it!
307 II->setIsPoisoned();
308 }
309}
310
311/// HandlePragmaSystemHeader - Implement #pragma GCC system_header. We know
312/// that the whole directive has been parsed.
Chris Lattner146762e2007-07-20 16:59:19 +0000313void Preprocessor::HandlePragmaSystemHeader(Token &SysHeaderTok) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000314 if (isInPrimaryFile()) {
315 Diag(SysHeaderTok, diag::pp_pragma_sysheader_in_main_file);
316 return;
317 }
Mike Stump11289f42009-09-09 15:08:12 +0000318
Chris Lattnerb694ba72006-07-02 22:41:36 +0000319 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
Ted Kremenek300590b2008-11-20 01:45:11 +0000320 PreprocessorLexer *TheLexer = getCurrentFileLexer();
Mike Stump11289f42009-09-09 15:08:12 +0000321
Chris Lattnerb694ba72006-07-02 22:41:36 +0000322 // Mark the file as a system header.
Chris Lattnerd32480d2009-01-17 06:22:33 +0000323 HeaderInfo.MarkFileSystemHeader(TheLexer->getFileEntry());
Mike Stump11289f42009-09-09 15:08:12 +0000324
325
Chris Lattnerd9efb6e2009-06-15 05:02:34 +0000326 PresumedLoc PLoc = SourceMgr.getPresumedLoc(SysHeaderTok.getLocation());
327 unsigned FilenameLen = strlen(PLoc.getFilename());
328 unsigned FilenameID = SourceMgr.getLineTableFilenameID(PLoc.getFilename(),
329 FilenameLen);
Mike Stump11289f42009-09-09 15:08:12 +0000330
Chris Lattnerd9efb6e2009-06-15 05:02:34 +0000331 // Emit a line marker. This will change any source locations from this point
332 // forward to realize they are in a system header.
333 // Create a line note with this information.
334 SourceMgr.AddLineNote(SysHeaderTok.getLocation(), PLoc.getLine(), FilenameID,
335 false, false, true, false);
Mike Stump11289f42009-09-09 15:08:12 +0000336
Chris Lattnerb694ba72006-07-02 22:41:36 +0000337 // Notify the client, if desired, that we are in a new source file.
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000338 if (Callbacks)
Ted Kremenek300590b2008-11-20 01:45:11 +0000339 Callbacks->FileChanged(SysHeaderTok.getLocation(),
Chris Lattnerb03dc762008-09-26 21:18:42 +0000340 PPCallbacks::SystemHeaderPragma, SrcMgr::C_System);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000341}
342
343/// HandlePragmaDependency - Handle #pragma GCC dependency "foo" blah.
344///
Chris Lattner146762e2007-07-20 16:59:19 +0000345void Preprocessor::HandlePragmaDependency(Token &DependencyTok) {
346 Token FilenameTok;
Ted Kremenek551c82a2008-11-18 01:12:54 +0000347 CurPPLexer->LexIncludeFilename(FilenameTok);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000348
349 // If the token kind is EOM, the error has already been diagnosed.
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000350 if (FilenameTok.is(tok::eom))
Chris Lattnerb694ba72006-07-02 22:41:36 +0000351 return;
Mike Stump11289f42009-09-09 15:08:12 +0000352
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000353 // Reserve a buffer to get the spelling.
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000354 llvm::SmallString<128> FilenameBuffer;
Douglas Gregordc970f02010-03-16 22:30:13 +0000355 bool Invalid = false;
356 llvm::StringRef Filename = getSpelling(FilenameTok, FilenameBuffer, &Invalid);
357 if (Invalid)
358 return;
Mike Stump11289f42009-09-09 15:08:12 +0000359
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000360 bool isAngled =
361 GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000362 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
363 // error.
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000364 if (Filename.empty())
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000365 return;
Mike Stump11289f42009-09-09 15:08:12 +0000366
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000367 // Search include directories for this file.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000368 const DirectoryLookup *CurDir;
Chris Lattnerfde85352010-01-22 00:14:44 +0000369 const FileEntry *File = LookupFile(Filename, isAngled, 0, CurDir);
Chris Lattner97b8e842008-11-18 08:02:48 +0000370 if (File == 0) {
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000371 Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
Chris Lattner97b8e842008-11-18 08:02:48 +0000372 return;
373 }
Mike Stump11289f42009-09-09 15:08:12 +0000374
Chris Lattnerd32480d2009-01-17 06:22:33 +0000375 const FileEntry *CurFile = getCurrentFileLexer()->getFileEntry();
Chris Lattnerb694ba72006-07-02 22:41:36 +0000376
377 // If this file is older than the file it depends on, emit a diagnostic.
378 if (CurFile && CurFile->getModificationTime() < File->getModificationTime()) {
379 // Lex tokens at the end of the message and include them in the message.
380 std::string Message;
381 Lex(DependencyTok);
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000382 while (DependencyTok.isNot(tok::eom)) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000383 Message += getSpelling(DependencyTok) + " ";
384 Lex(DependencyTok);
385 }
Mike Stump11289f42009-09-09 15:08:12 +0000386
Chris Lattnerb694ba72006-07-02 22:41:36 +0000387 Message.erase(Message.end()-1);
Chris Lattner97b8e842008-11-18 08:02:48 +0000388 Diag(FilenameTok, diag::pp_out_of_date_dependency) << Message;
Chris Lattnerb694ba72006-07-02 22:41:36 +0000389 }
390}
391
Chris Lattner2ff698d2009-01-16 08:21:25 +0000392/// HandlePragmaComment - Handle the microsoft #pragma comment extension. The
393/// syntax is:
394/// #pragma comment(linker, "foo")
395/// 'linker' is one of five identifiers: compiler, exestr, lib, linker, user.
396/// "foo" is a string, which is fully macro expanded, and permits string
Gabor Greif31a082f2009-03-17 11:39:38 +0000397/// concatenation, embedded escape characters etc. See MSDN for more details.
Chris Lattner2ff698d2009-01-16 08:21:25 +0000398void Preprocessor::HandlePragmaComment(Token &Tok) {
399 SourceLocation CommentLoc = Tok.getLocation();
400 Lex(Tok);
401 if (Tok.isNot(tok::l_paren)) {
402 Diag(CommentLoc, diag::err_pragma_comment_malformed);
403 return;
404 }
Mike Stump11289f42009-09-09 15:08:12 +0000405
Chris Lattner2ff698d2009-01-16 08:21:25 +0000406 // Read the identifier.
407 Lex(Tok);
408 if (Tok.isNot(tok::identifier)) {
409 Diag(CommentLoc, diag::err_pragma_comment_malformed);
410 return;
411 }
Mike Stump11289f42009-09-09 15:08:12 +0000412
Chris Lattner2ff698d2009-01-16 08:21:25 +0000413 // Verify that this is one of the 5 whitelisted options.
414 // FIXME: warn that 'exestr' is deprecated.
415 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000416 if (!II->isStr("compiler") && !II->isStr("exestr") && !II->isStr("lib") &&
Chris Lattner2ff698d2009-01-16 08:21:25 +0000417 !II->isStr("linker") && !II->isStr("user")) {
418 Diag(Tok.getLocation(), diag::err_pragma_comment_unknown_kind);
419 return;
420 }
Mike Stump11289f42009-09-09 15:08:12 +0000421
Chris Lattner262d4e32009-01-16 18:59:23 +0000422 // Read the optional string if present.
Chris Lattner2ff698d2009-01-16 08:21:25 +0000423 Lex(Tok);
Chris Lattner262d4e32009-01-16 18:59:23 +0000424 std::string ArgumentString;
Chris Lattner2ff698d2009-01-16 08:21:25 +0000425 if (Tok.is(tok::comma)) {
Chris Lattner262d4e32009-01-16 18:59:23 +0000426 Lex(Tok); // eat the comma.
Chris Lattner2ff698d2009-01-16 08:21:25 +0000427
428 // We need at least one string.
Chris Lattner504af112009-04-19 23:16:58 +0000429 if (Tok.isNot(tok::string_literal)) {
Chris Lattner2ff698d2009-01-16 08:21:25 +0000430 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
431 return;
432 }
433
434 // String concatenation allows multiple strings, which can even come from
435 // macro expansion.
436 // "foo " "bar" "Baz"
Chris Lattner262d4e32009-01-16 18:59:23 +0000437 llvm::SmallVector<Token, 4> StrToks;
Chris Lattner504af112009-04-19 23:16:58 +0000438 while (Tok.is(tok::string_literal)) {
Chris Lattner262d4e32009-01-16 18:59:23 +0000439 StrToks.push_back(Tok);
Chris Lattner2ff698d2009-01-16 08:21:25 +0000440 Lex(Tok);
Chris Lattner262d4e32009-01-16 18:59:23 +0000441 }
442
443 // Concatenate and parse the strings.
444 StringLiteralParser Literal(&StrToks[0], StrToks.size(), *this);
445 assert(!Literal.AnyWide && "Didn't allow wide strings in");
446 if (Literal.hadError)
447 return;
448 if (Literal.Pascal) {
449 Diag(StrToks[0].getLocation(), diag::err_pragma_comment_malformed);
450 return;
451 }
452
453 ArgumentString = std::string(Literal.GetString(),
454 Literal.GetString()+Literal.GetStringLength());
Chris Lattner2ff698d2009-01-16 08:21:25 +0000455 }
Mike Stump11289f42009-09-09 15:08:12 +0000456
Chris Lattner262d4e32009-01-16 18:59:23 +0000457 // FIXME: If the kind is "compiler" warn if the string is present (it is
458 // ignored).
459 // FIXME: 'lib' requires a comment string.
460 // FIXME: 'linker' requires a comment string, and has a specific list of
461 // things that are allowable.
Mike Stump11289f42009-09-09 15:08:12 +0000462
Chris Lattner2ff698d2009-01-16 08:21:25 +0000463 if (Tok.isNot(tok::r_paren)) {
464 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
465 return;
466 }
Chris Lattner262d4e32009-01-16 18:59:23 +0000467 Lex(Tok); // eat the r_paren.
Chris Lattner2ff698d2009-01-16 08:21:25 +0000468
469 if (Tok.isNot(tok::eom)) {
470 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
471 return;
472 }
Mike Stump11289f42009-09-09 15:08:12 +0000473
Chris Lattner262d4e32009-01-16 18:59:23 +0000474 // If the pragma is lexically sound, notify any interested PPCallbacks.
Chris Lattnerf49775d2009-01-16 19:01:46 +0000475 if (Callbacks)
476 Callbacks->PragmaComment(CommentLoc, II, ArgumentString);
Chris Lattner2ff698d2009-01-16 08:21:25 +0000477}
478
Chris Lattner30c924b2010-06-26 17:11:39 +0000479/// HandlePragmaMessage - Handle the microsoft #pragma message extension. The
480/// syntax is:
481/// #pragma message(messagestring)
482/// messagestring is a string, which is fully macro expanded, and permits string
483/// concatenation, embedded escape characters etc. See MSDN for more details.
484void Preprocessor::HandlePragmaMessage(Token &Tok) {
485 SourceLocation MessageLoc = Tok.getLocation();
486 Lex(Tok);
487 if (Tok.isNot(tok::l_paren)) {
488 Diag(MessageLoc, diag::err_pragma_message_malformed);
489 return;
490 }
Chris Lattner2ff698d2009-01-16 08:21:25 +0000491
Chris Lattner30c924b2010-06-26 17:11:39 +0000492 // Read the string.
493 Lex(Tok);
494
495
496 // We need at least one string.
497 if (Tok.isNot(tok::string_literal)) {
498 Diag(Tok.getLocation(), diag::err_pragma_message_malformed);
499 return;
500 }
501
502 // String concatenation allows multiple strings, which can even come from
503 // macro expansion.
504 // "foo " "bar" "Baz"
505 llvm::SmallVector<Token, 4> StrToks;
506 while (Tok.is(tok::string_literal)) {
507 StrToks.push_back(Tok);
508 Lex(Tok);
509 }
510
511 // Concatenate and parse the strings.
512 StringLiteralParser Literal(&StrToks[0], StrToks.size(), *this);
513 assert(!Literal.AnyWide && "Didn't allow wide strings in");
514 if (Literal.hadError)
515 return;
516 if (Literal.Pascal) {
517 Diag(StrToks[0].getLocation(), diag::err_pragma_message_malformed);
518 return;
519 }
520
521 llvm::StringRef MessageString(Literal.GetString(), Literal.GetStringLength());
522
523 if (Tok.isNot(tok::r_paren)) {
524 Diag(Tok.getLocation(), diag::err_pragma_message_malformed);
525 return;
526 }
527 Lex(Tok); // eat the r_paren.
528
529 if (Tok.isNot(tok::eom)) {
530 Diag(Tok.getLocation(), diag::err_pragma_message_malformed);
531 return;
532 }
533
534 // Output the message.
535 Diag(MessageLoc, diag::warn_pragma_message) << MessageString;
536
537 // If the pragma is lexically sound, notify any interested PPCallbacks.
538 if (Callbacks)
539 Callbacks->PragmaMessage(MessageLoc, MessageString);
540}
Chris Lattner2ff698d2009-01-16 08:21:25 +0000541
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000542/// ParsePragmaPushOrPopMacro - Handle parsing of pragma push_macro/pop_macro.
543/// Return the IdentifierInfo* associated with the macro to push or pop.
544IdentifierInfo *Preprocessor::ParsePragmaPushOrPopMacro(Token &Tok) {
545 // Remember the pragma token location.
546 Token PragmaTok = Tok;
547
548 // Read the '('.
549 Lex(Tok);
550 if (Tok.isNot(tok::l_paren)) {
551 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
552 << getSpelling(PragmaTok);
553 return 0;
554 }
555
556 // Read the macro name string.
557 Lex(Tok);
558 if (Tok.isNot(tok::string_literal)) {
559 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
560 << getSpelling(PragmaTok);
561 return 0;
562 }
563
564 // Remember the macro string.
565 std::string StrVal = getSpelling(Tok);
566
567 // Read the ')'.
568 Lex(Tok);
569 if (Tok.isNot(tok::r_paren)) {
570 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
571 << getSpelling(PragmaTok);
572 return 0;
573 }
574
575 assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
576 "Invalid string token!");
577
578 // Create a Token from the string.
579 Token MacroTok;
580 MacroTok.startToken();
581 MacroTok.setKind(tok::identifier);
582 CreateString(&StrVal[1], StrVal.size() - 2, MacroTok);
583
584 // Get the IdentifierInfo of MacroToPushTok.
585 return LookUpIdentifierInfo(MacroTok);
586}
587
588/// HandlePragmaPushMacro - Handle #pragma push_macro.
589/// The syntax is:
590/// #pragma push_macro("macro")
591void Preprocessor::HandlePragmaPushMacro(Token &PushMacroTok) {
592 // Parse the pragma directive and get the macro IdentifierInfo*.
593 IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PushMacroTok);
594 if (!IdentInfo) return;
595
596 // Get the MacroInfo associated with IdentInfo.
597 MacroInfo *MI = getMacroInfo(IdentInfo);
598
599 MacroInfo *MacroCopyToPush = 0;
600 if (MI) {
601 // Make a clone of MI.
602 MacroCopyToPush = CloneMacroInfo(*MI);
603
604 // Allow the original MacroInfo to be redefined later.
605 MI->setIsAllowRedefinitionsWithoutWarning(true);
606 }
607
608 // Push the cloned MacroInfo so we can retrieve it later.
609 PragmaPushMacroInfo[IdentInfo].push_back(MacroCopyToPush);
610}
611
612/// HandlePragmaPopMacro - Handle #pragma push_macro.
613/// The syntax is:
614/// #pragma pop_macro("macro")
615void Preprocessor::HandlePragmaPopMacro(Token &PopMacroTok) {
616 SourceLocation MessageLoc = PopMacroTok.getLocation();
617
618 // Parse the pragma directive and get the macro IdentifierInfo*.
619 IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PopMacroTok);
620 if (!IdentInfo) return;
621
622 // Find the vector<MacroInfo*> associated with the macro.
623 llvm::DenseMap<IdentifierInfo*, std::vector<MacroInfo*> >::iterator iter =
624 PragmaPushMacroInfo.find(IdentInfo);
625 if (iter != PragmaPushMacroInfo.end()) {
626 // Release the MacroInfo currently associated with IdentInfo.
627 MacroInfo *CurrentMI = getMacroInfo(IdentInfo);
628 if (CurrentMI) ReleaseMacroInfo(CurrentMI);
629
630 // Get the MacroInfo we want to reinstall.
631 MacroInfo *MacroToReInstall = iter->second.back();
632
633 // Reinstall the previously pushed macro.
634 setMacroInfo(IdentInfo, MacroToReInstall);
635
636 // Pop PragmaPushMacroInfo stack.
637 iter->second.pop_back();
638 if (iter->second.size() == 0)
639 PragmaPushMacroInfo.erase(iter);
640 } else {
641 Diag(MessageLoc, diag::warn_pragma_pop_macro_no_push)
642 << IdentInfo->getName();
643 }
644}
Chris Lattnerb694ba72006-07-02 22:41:36 +0000645
646/// AddPragmaHandler - Add the specified pragma handler to the preprocessor.
647/// If 'Namespace' is non-null, then it is a token required to exist on the
648/// pragma line before the pragma string starts, e.g. "STDC" or "GCC".
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000649void Preprocessor::AddPragmaHandler(llvm::StringRef Namespace,
Chris Lattnerb694ba72006-07-02 22:41:36 +0000650 PragmaHandler *Handler) {
651 PragmaNamespace *InsertNS = PragmaHandlers;
Mike Stump11289f42009-09-09 15:08:12 +0000652
Chris Lattnerb694ba72006-07-02 22:41:36 +0000653 // If this is specified to be in a namespace, step down into it.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000654 if (!Namespace.empty()) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000655 // If there is already a pragma handler with the name of this namespace,
656 // we either have an error (directive with the same name as a namespace) or
657 // we already have the namespace to insert into.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000658 if (PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace)) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000659 InsertNS = Existing->getIfNamespace();
660 assert(InsertNS != 0 && "Cannot have a pragma namespace and pragma"
661 " handler with the same name!");
662 } else {
663 // Otherwise, this namespace doesn't exist yet, create and insert the
664 // handler for it.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000665 InsertNS = new PragmaNamespace(Namespace);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000666 PragmaHandlers->AddPragma(InsertNS);
667 }
668 }
Mike Stump11289f42009-09-09 15:08:12 +0000669
Chris Lattnerb694ba72006-07-02 22:41:36 +0000670 // Check to make sure we don't already have a pragma for this identifier.
671 assert(!InsertNS->FindHandler(Handler->getName()) &&
672 "Pragma handler already exists for this identifier!");
673 InsertNS->AddPragma(Handler);
674}
675
Daniel Dunbar40596532008-10-04 19:17:46 +0000676/// RemovePragmaHandler - Remove the specific pragma handler from the
677/// preprocessor. If \arg Namespace is non-null, then it should be the
678/// namespace that \arg Handler was added to. It is an error to remove
679/// a handler that has not been registered.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000680void Preprocessor::RemovePragmaHandler(llvm::StringRef Namespace,
Daniel Dunbar40596532008-10-04 19:17:46 +0000681 PragmaHandler *Handler) {
682 PragmaNamespace *NS = PragmaHandlers;
Mike Stump11289f42009-09-09 15:08:12 +0000683
Daniel Dunbar40596532008-10-04 19:17:46 +0000684 // If this is specified to be in a namespace, step down into it.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000685 if (!Namespace.empty()) {
686 PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace);
Daniel Dunbar40596532008-10-04 19:17:46 +0000687 assert(Existing && "Namespace containing handler does not exist!");
688
689 NS = Existing->getIfNamespace();
690 assert(NS && "Invalid namespace, registered as a regular pragma handler!");
691 }
692
693 NS->RemovePragmaHandler(Handler);
Mike Stump11289f42009-09-09 15:08:12 +0000694
Daniel Dunbar40596532008-10-04 19:17:46 +0000695 // If this is a non-default namespace and it is now empty, remove
696 // it.
697 if (NS != PragmaHandlers && NS->IsEmpty())
698 PragmaHandlers->RemovePragmaHandler(NS);
699}
700
Chris Lattnerb694ba72006-07-02 22:41:36 +0000701namespace {
Chris Lattnerc2383312007-12-19 19:38:36 +0000702/// PragmaOnceHandler - "#pragma once" marks the file as atomically included.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000703struct PragmaOnceHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000704 PragmaOnceHandler() : PragmaHandler("once") {}
Chris Lattner146762e2007-07-20 16:59:19 +0000705 virtual void HandlePragma(Preprocessor &PP, Token &OnceTok) {
Chris Lattnerce2ab6f2009-04-14 05:07:49 +0000706 PP.CheckEndOfDirective("pragma once");
Chris Lattnerb694ba72006-07-02 22:41:36 +0000707 PP.HandlePragmaOnce(OnceTok);
708 }
709};
710
Chris Lattnerc2383312007-12-19 19:38:36 +0000711/// PragmaMarkHandler - "#pragma mark ..." is ignored by the compiler, and the
712/// rest of the line is not lexed.
713struct PragmaMarkHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000714 PragmaMarkHandler() : PragmaHandler("mark") {}
Chris Lattnerc2383312007-12-19 19:38:36 +0000715 virtual void HandlePragma(Preprocessor &PP, Token &MarkTok) {
716 PP.HandlePragmaMark();
717 }
718};
719
720/// PragmaPoisonHandler - "#pragma poison x" marks x as not usable.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000721struct PragmaPoisonHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000722 PragmaPoisonHandler() : PragmaHandler("poison") {}
Chris Lattner146762e2007-07-20 16:59:19 +0000723 virtual void HandlePragma(Preprocessor &PP, Token &PoisonTok) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000724 PP.HandlePragmaPoison(PoisonTok);
725 }
726};
727
Chris Lattnerc2383312007-12-19 19:38:36 +0000728/// PragmaSystemHeaderHandler - "#pragma system_header" marks the current file
729/// as a system header, which silences warnings in it.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000730struct PragmaSystemHeaderHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000731 PragmaSystemHeaderHandler() : PragmaHandler("system_header") {}
Chris Lattner146762e2007-07-20 16:59:19 +0000732 virtual void HandlePragma(Preprocessor &PP, Token &SHToken) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000733 PP.HandlePragmaSystemHeader(SHToken);
Chris Lattnerce2ab6f2009-04-14 05:07:49 +0000734 PP.CheckEndOfDirective("pragma");
Chris Lattnerb694ba72006-07-02 22:41:36 +0000735 }
736};
737struct PragmaDependencyHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000738 PragmaDependencyHandler() : PragmaHandler("dependency") {}
Chris Lattner146762e2007-07-20 16:59:19 +0000739 virtual void HandlePragma(Preprocessor &PP, Token &DepToken) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000740 PP.HandlePragmaDependency(DepToken);
741 }
742};
Mike Stump11289f42009-09-09 15:08:12 +0000743
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000744struct PragmaDebugHandler : public PragmaHandler {
745 PragmaDebugHandler() : PragmaHandler("__debug") {}
746 virtual void HandlePragma(Preprocessor &PP, Token &DepToken) {
747 Token Tok;
748 PP.LexUnexpandedToken(Tok);
749 if (Tok.isNot(tok::identifier)) {
750 PP.Diag(Tok, diag::warn_pragma_diagnostic_clang_invalid);
751 return;
752 }
753 IdentifierInfo *II = Tok.getIdentifierInfo();
754
Daniel Dunbarf2cf3292010-08-17 22:32:48 +0000755 if (II->isStr("assert")) {
756 assert(0 && "This is an assertion!");
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000757 } else if (II->isStr("crash")) {
Daniel Dunbarf2cf3292010-08-17 22:32:48 +0000758 *(volatile int*) 0x11 = 0;
759 } else if (II->isStr("llvm_fatal_error")) {
760 llvm::report_fatal_error("#pragma clang __debug llvm_fatal_error");
761 } else if (II->isStr("llvm_unreachable")) {
762 llvm_unreachable("#pragma clang __debug llvm_unreachable");
763 } else if (II->isStr("overflow_stack")) {
764 DebugOverflowStack();
Daniel Dunbar211a7872010-08-18 23:09:23 +0000765 } else if (II->isStr("handle_crash")) {
766 llvm::CrashRecoveryContext *CRC =llvm::CrashRecoveryContext::GetCurrent();
767 if (CRC)
768 CRC->HandleCrash();
Daniel Dunbarf2cf3292010-08-17 22:32:48 +0000769 } else {
770 PP.Diag(Tok, diag::warn_pragma_debug_unexpected_command)
771 << II->getName();
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000772 }
773 }
774
775 void DebugOverflowStack() {
776 DebugOverflowStack();
777 }
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000778};
779
Chris Lattner504af112009-04-19 23:16:58 +0000780/// PragmaDiagnosticHandler - e.g. '#pragma GCC diagnostic ignored "-Wformat"'
Chris Lattnerfb42a182009-07-12 21:18:45 +0000781/// Since clang's diagnostic supports extended functionality beyond GCC's
782/// the constructor takes a clangMode flag to tell it whether or not to allow
783/// clang's extended functionality, or whether to reject it.
Chris Lattner504af112009-04-19 23:16:58 +0000784struct PragmaDiagnosticHandler : public PragmaHandler {
Chris Lattnerfb42a182009-07-12 21:18:45 +0000785private:
786 const bool ClangMode;
787public:
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000788 explicit PragmaDiagnosticHandler(const bool clangMode)
789 : PragmaHandler("diagnostic"), ClangMode(clangMode) {}
790
Chris Lattner504af112009-04-19 23:16:58 +0000791 virtual void HandlePragma(Preprocessor &PP, Token &DiagToken) {
792 Token Tok;
793 PP.LexUnexpandedToken(Tok);
794 if (Tok.isNot(tok::identifier)) {
Chris Lattnerfb42a182009-07-12 21:18:45 +0000795 unsigned Diag = ClangMode ? diag::warn_pragma_diagnostic_clang_invalid
796 : diag::warn_pragma_diagnostic_gcc_invalid;
797 PP.Diag(Tok, Diag);
Chris Lattner504af112009-04-19 23:16:58 +0000798 return;
799 }
800 IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000801
Chris Lattner504af112009-04-19 23:16:58 +0000802 diag::Mapping Map;
803 if (II->isStr("warning"))
804 Map = diag::MAP_WARNING;
805 else if (II->isStr("error"))
806 Map = diag::MAP_ERROR;
807 else if (II->isStr("ignored"))
808 Map = diag::MAP_IGNORE;
809 else if (II->isStr("fatal"))
810 Map = diag::MAP_FATAL;
Chris Lattnerfb42a182009-07-12 21:18:45 +0000811 else if (ClangMode) {
812 if (II->isStr("pop")) {
Mike Stump11289f42009-09-09 15:08:12 +0000813 if (!PP.getDiagnostics().popMappings())
Chris Lattnerfb42a182009-07-12 21:18:45 +0000814 PP.Diag(Tok, diag::warn_pragma_diagnostic_clang_cannot_ppp);
815 return;
816 }
817
818 if (II->isStr("push")) {
819 PP.getDiagnostics().pushMappings();
Mike Stump11289f42009-09-09 15:08:12 +0000820 return;
Chris Lattnerfb42a182009-07-12 21:18:45 +0000821 }
822
823 PP.Diag(Tok, diag::warn_pragma_diagnostic_clang_invalid);
824 return;
825 } else {
826 PP.Diag(Tok, diag::warn_pragma_diagnostic_gcc_invalid);
Chris Lattner504af112009-04-19 23:16:58 +0000827 return;
828 }
Mike Stump11289f42009-09-09 15:08:12 +0000829
Chris Lattner504af112009-04-19 23:16:58 +0000830 PP.LexUnexpandedToken(Tok);
831
832 // We need at least one string.
833 if (Tok.isNot(tok::string_literal)) {
834 PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token);
835 return;
836 }
Mike Stump11289f42009-09-09 15:08:12 +0000837
Chris Lattner504af112009-04-19 23:16:58 +0000838 // String concatenation allows multiple strings, which can even come from
839 // macro expansion.
840 // "foo " "bar" "Baz"
841 llvm::SmallVector<Token, 4> StrToks;
842 while (Tok.is(tok::string_literal)) {
843 StrToks.push_back(Tok);
844 PP.LexUnexpandedToken(Tok);
845 }
Mike Stump11289f42009-09-09 15:08:12 +0000846
Chris Lattner504af112009-04-19 23:16:58 +0000847 if (Tok.isNot(tok::eom)) {
848 PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token);
849 return;
850 }
Mike Stump11289f42009-09-09 15:08:12 +0000851
Chris Lattner504af112009-04-19 23:16:58 +0000852 // Concatenate and parse the strings.
853 StringLiteralParser Literal(&StrToks[0], StrToks.size(), PP);
854 assert(!Literal.AnyWide && "Didn't allow wide strings in");
855 if (Literal.hadError)
856 return;
857 if (Literal.Pascal) {
Chris Lattnerfb42a182009-07-12 21:18:45 +0000858 unsigned Diag = ClangMode ? diag::warn_pragma_diagnostic_clang_invalid
859 : diag::warn_pragma_diagnostic_gcc_invalid;
860 PP.Diag(Tok, Diag);
Chris Lattner504af112009-04-19 23:16:58 +0000861 return;
862 }
Chris Lattnerfb42a182009-07-12 21:18:45 +0000863
Chris Lattner504af112009-04-19 23:16:58 +0000864 std::string WarningName(Literal.GetString(),
865 Literal.GetString()+Literal.GetStringLength());
866
867 if (WarningName.size() < 3 || WarningName[0] != '-' ||
868 WarningName[1] != 'W') {
869 PP.Diag(StrToks[0].getLocation(),
870 diag::warn_pragma_diagnostic_invalid_option);
871 return;
872 }
Mike Stump11289f42009-09-09 15:08:12 +0000873
Chris Lattner504af112009-04-19 23:16:58 +0000874 if (PP.getDiagnostics().setDiagnosticGroupMapping(WarningName.c_str()+2,
875 Map))
876 PP.Diag(StrToks[0].getLocation(),
877 diag::warn_pragma_diagnostic_unknown_warning) << WarningName;
878 }
879};
Mike Stump11289f42009-09-09 15:08:12 +0000880
Chris Lattner2ff698d2009-01-16 08:21:25 +0000881/// PragmaCommentHandler - "#pragma comment ...".
882struct PragmaCommentHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000883 PragmaCommentHandler() : PragmaHandler("comment") {}
Chris Lattner2ff698d2009-01-16 08:21:25 +0000884 virtual void HandlePragma(Preprocessor &PP, Token &CommentTok) {
885 PP.HandlePragmaComment(CommentTok);
886 }
887};
Mike Stump11289f42009-09-09 15:08:12 +0000888
Chris Lattner30c924b2010-06-26 17:11:39 +0000889/// PragmaMessageHandler - "#pragma message("...")".
890struct PragmaMessageHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000891 PragmaMessageHandler() : PragmaHandler("message") {}
Chris Lattner30c924b2010-06-26 17:11:39 +0000892 virtual void HandlePragma(Preprocessor &PP, Token &CommentTok) {
893 PP.HandlePragmaMessage(CommentTok);
894 }
895};
896
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000897/// PragmaPushMacroHandler - "#pragma push_macro" saves the value of the
898/// macro on the top of the stack.
899struct PragmaPushMacroHandler : public PragmaHandler {
900 PragmaPushMacroHandler() : PragmaHandler("push_macro") {}
901 virtual void HandlePragma(Preprocessor &PP, Token &PushMacroTok) {
902 PP.HandlePragmaPushMacro(PushMacroTok);
903 }
904};
905
906
907/// PragmaPopMacroHandler - "#pragma pop_macro" sets the value of the
908/// macro to the value on the top of the stack.
909struct PragmaPopMacroHandler : public PragmaHandler {
910 PragmaPopMacroHandler() : PragmaHandler("pop_macro") {}
911 virtual void HandlePragma(Preprocessor &PP, Token &PopMacroTok) {
912 PP.HandlePragmaPopMacro(PopMacroTok);
913 }
914};
915
Chris Lattner958ee042009-04-19 21:20:35 +0000916// Pragma STDC implementations.
Chris Lattner02ef4e32009-04-19 21:50:08 +0000917
918enum STDCSetting {
919 STDC_ON, STDC_OFF, STDC_DEFAULT, STDC_INVALID
920};
Mike Stump11289f42009-09-09 15:08:12 +0000921
Chris Lattner02ef4e32009-04-19 21:50:08 +0000922static STDCSetting LexOnOffSwitch(Preprocessor &PP) {
923 Token Tok;
924 PP.LexUnexpandedToken(Tok);
925
926 if (Tok.isNot(tok::identifier)) {
927 PP.Diag(Tok, diag::ext_stdc_pragma_syntax);
928 return STDC_INVALID;
929 }
930 IdentifierInfo *II = Tok.getIdentifierInfo();
931 STDCSetting Result;
932 if (II->isStr("ON"))
933 Result = STDC_ON;
934 else if (II->isStr("OFF"))
935 Result = STDC_OFF;
936 else if (II->isStr("DEFAULT"))
937 Result = STDC_DEFAULT;
938 else {
939 PP.Diag(Tok, diag::ext_stdc_pragma_syntax);
940 return STDC_INVALID;
941 }
942
943 // Verify that this is followed by EOM.
944 PP.LexUnexpandedToken(Tok);
945 if (Tok.isNot(tok::eom))
946 PP.Diag(Tok, diag::ext_stdc_pragma_syntax_eom);
947 return Result;
948}
Mike Stump11289f42009-09-09 15:08:12 +0000949
Chris Lattner958ee042009-04-19 21:20:35 +0000950/// PragmaSTDC_FP_CONTRACTHandler - "#pragma STDC FP_CONTRACT ...".
951struct PragmaSTDC_FP_CONTRACTHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000952 PragmaSTDC_FP_CONTRACTHandler() : PragmaHandler("FP_CONTRACT") {}
Chris Lattnera0b1f762009-04-19 21:25:37 +0000953 virtual void HandlePragma(Preprocessor &PP, Token &Tok) {
Chris Lattner02ef4e32009-04-19 21:50:08 +0000954 // We just ignore the setting of FP_CONTRACT. Since we don't do contractions
955 // at all, our default is OFF and setting it to ON is an optimization hint
956 // we can safely ignore. When we support -ffma or something, we would need
957 // to diagnose that we are ignoring FMA.
958 LexOnOffSwitch(PP);
Chris Lattner958ee042009-04-19 21:20:35 +0000959 }
960};
Mike Stump11289f42009-09-09 15:08:12 +0000961
Chris Lattner958ee042009-04-19 21:20:35 +0000962/// PragmaSTDC_FENV_ACCESSHandler - "#pragma STDC FENV_ACCESS ...".
963struct PragmaSTDC_FENV_ACCESSHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000964 PragmaSTDC_FENV_ACCESSHandler() : PragmaHandler("FENV_ACCESS") {}
Chris Lattnera0b1f762009-04-19 21:25:37 +0000965 virtual void HandlePragma(Preprocessor &PP, Token &Tok) {
Chris Lattnerdf222682009-04-19 21:55:32 +0000966 if (LexOnOffSwitch(PP) == STDC_ON)
967 PP.Diag(Tok, diag::warn_stdc_fenv_access_not_supported);
Chris Lattner958ee042009-04-19 21:20:35 +0000968 }
969};
Mike Stump11289f42009-09-09 15:08:12 +0000970
Chris Lattner958ee042009-04-19 21:20:35 +0000971/// PragmaSTDC_CX_LIMITED_RANGEHandler - "#pragma STDC CX_LIMITED_RANGE ...".
972struct PragmaSTDC_CX_LIMITED_RANGEHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000973 PragmaSTDC_CX_LIMITED_RANGEHandler()
974 : PragmaHandler("CX_LIMITED_RANGE") {}
Chris Lattnera0b1f762009-04-19 21:25:37 +0000975 virtual void HandlePragma(Preprocessor &PP, Token &Tok) {
Chris Lattner02ef4e32009-04-19 21:50:08 +0000976 LexOnOffSwitch(PP);
Chris Lattner958ee042009-04-19 21:20:35 +0000977 }
978};
Mike Stump11289f42009-09-09 15:08:12 +0000979
Chris Lattner958ee042009-04-19 21:20:35 +0000980/// PragmaSTDC_UnknownHandler - "#pragma STDC ...".
981struct PragmaSTDC_UnknownHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000982 PragmaSTDC_UnknownHandler() {}
Chris Lattnera0b1f762009-04-19 21:25:37 +0000983 virtual void HandlePragma(Preprocessor &PP, Token &UnknownTok) {
Chris Lattner02ef4e32009-04-19 21:50:08 +0000984 // C99 6.10.6p2, unknown forms are not allowed.
Chris Lattnera0b1f762009-04-19 21:25:37 +0000985 PP.Diag(UnknownTok, diag::ext_stdc_pragma_ignored);
Chris Lattner958ee042009-04-19 21:20:35 +0000986 }
987};
Mike Stump11289f42009-09-09 15:08:12 +0000988
Chris Lattnerb694ba72006-07-02 22:41:36 +0000989} // end anonymous namespace
990
991
992/// RegisterBuiltinPragmas - Install the standard preprocessor pragmas:
993/// #pragma GCC poison/system_header/dependency and #pragma once.
994void Preprocessor::RegisterBuiltinPragmas() {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000995 AddPragmaHandler(new PragmaOnceHandler());
996 AddPragmaHandler(new PragmaMarkHandler());
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000997 AddPragmaHandler(new PragmaPushMacroHandler());
998 AddPragmaHandler(new PragmaPopMacroHandler());
Mike Stump11289f42009-09-09 15:08:12 +0000999
Chris Lattnerb61448d2009-05-12 18:21:11 +00001000 // #pragma GCC ...
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001001 AddPragmaHandler("GCC", new PragmaPoisonHandler());
1002 AddPragmaHandler("GCC", new PragmaSystemHeaderHandler());
1003 AddPragmaHandler("GCC", new PragmaDependencyHandler());
1004 AddPragmaHandler("GCC", new PragmaDiagnosticHandler(false));
Chris Lattnerb61448d2009-05-12 18:21:11 +00001005 // #pragma clang ...
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001006 AddPragmaHandler("clang", new PragmaPoisonHandler());
1007 AddPragmaHandler("clang", new PragmaSystemHeaderHandler());
Daniel Dunbarb8068c32010-07-28 15:40:33 +00001008 AddPragmaHandler("clang", new PragmaDebugHandler());
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001009 AddPragmaHandler("clang", new PragmaDependencyHandler());
1010 AddPragmaHandler("clang", new PragmaDiagnosticHandler(true));
Chris Lattnerb61448d2009-05-12 18:21:11 +00001011
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001012 AddPragmaHandler("STDC", new PragmaSTDC_FP_CONTRACTHandler());
1013 AddPragmaHandler("STDC", new PragmaSTDC_FENV_ACCESSHandler());
1014 AddPragmaHandler("STDC", new PragmaSTDC_CX_LIMITED_RANGEHandler());
Chris Lattner958ee042009-04-19 21:20:35 +00001015 AddPragmaHandler("STDC", new PragmaSTDC_UnknownHandler());
Mike Stump11289f42009-09-09 15:08:12 +00001016
Chris Lattner2ff698d2009-01-16 08:21:25 +00001017 // MS extensions.
Chris Lattner30c924b2010-06-26 17:11:39 +00001018 if (Features.Microsoft) {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001019 AddPragmaHandler(new PragmaCommentHandler());
1020 AddPragmaHandler(new PragmaMessageHandler());
Chris Lattner30c924b2010-06-26 17:11:39 +00001021 }
Chris Lattnerb694ba72006-07-02 22:41:36 +00001022}