blob: 783900de467be85bb4e4b7aa54101edc7b759c96 [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 Lattner60f36222009-01-29 05:15:15 +000019#include "clang/Lex/LexDiagnostic.h"
Chris Lattnerb694ba72006-07-02 22:41:36 +000020#include "clang/Basic/FileManager.h"
21#include "clang/Basic/SourceManager.h"
Douglas Gregorc6d5edd2009-07-02 17:08:52 +000022#include <algorithm>
Chris Lattnerb8761832006-06-24 21:31:03 +000023using namespace clang;
24
25// Out-of-line destructor to provide a home for the class.
26PragmaHandler::~PragmaHandler() {
27}
28
Chris Lattner2e155302006-07-03 05:34:41 +000029//===----------------------------------------------------------------------===//
Daniel Dunbard839e772010-06-11 20:10:12 +000030// EmptyPragmaHandler Implementation.
31//===----------------------------------------------------------------------===//
32
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000033EmptyPragmaHandler::EmptyPragmaHandler() {}
Daniel Dunbard839e772010-06-11 20:10:12 +000034
35void EmptyPragmaHandler::HandlePragma(Preprocessor &PP, Token &FirstToken) {}
36
37//===----------------------------------------------------------------------===//
Chris Lattner2e155302006-07-03 05:34:41 +000038// PragmaNamespace Implementation.
39//===----------------------------------------------------------------------===//
40
41
42PragmaNamespace::~PragmaNamespace() {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000043 for (llvm::StringMap<PragmaHandler*>::iterator
44 I = Handlers.begin(), E = Handlers.end(); I != E; ++I)
45 delete I->second;
Chris Lattner2e155302006-07-03 05:34:41 +000046}
47
48/// FindHandler - Check to see if there is already a handler for the
49/// specified name. If not, return the handler for the null identifier if it
50/// exists, otherwise return null. If IgnoreNull is true (the default) then
51/// the null handler isn't returned on failure to match.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000052PragmaHandler *PragmaNamespace::FindHandler(llvm::StringRef Name,
Chris Lattner2e155302006-07-03 05:34:41 +000053 bool IgnoreNull) const {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000054 if (PragmaHandler *Handler = Handlers.lookup(Name))
55 return Handler;
56 return IgnoreNull ? 0 : Handlers.lookup(llvm::StringRef());
57}
Mike Stump11289f42009-09-09 15:08:12 +000058
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000059void PragmaNamespace::AddPragma(PragmaHandler *Handler) {
60 assert(!Handlers.lookup(Handler->getName()) &&
61 "A handler with this name is already registered in this namespace");
62 llvm::StringMapEntry<PragmaHandler *> &Entry =
63 Handlers.GetOrCreateValue(Handler->getName());
64 Entry.setValue(Handler);
Chris Lattner2e155302006-07-03 05:34:41 +000065}
66
Daniel Dunbar40596532008-10-04 19:17:46 +000067void PragmaNamespace::RemovePragmaHandler(PragmaHandler *Handler) {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000068 assert(Handlers.lookup(Handler->getName()) &&
69 "Handler not registered in this namespace");
70 Handlers.erase(Handler->getName());
Daniel Dunbar40596532008-10-04 19:17:46 +000071}
72
Chris Lattner146762e2007-07-20 16:59:19 +000073void PragmaNamespace::HandlePragma(Preprocessor &PP, Token &Tok) {
Chris Lattnerb8761832006-06-24 21:31:03 +000074 // Read the 'namespace' that the directive is in, e.g. STDC. Do not macro
75 // expand it, the user can have a STDC #define, that should not affect this.
76 PP.LexUnexpandedToken(Tok);
Mike Stump11289f42009-09-09 15:08:12 +000077
Chris Lattnerb8761832006-06-24 21:31:03 +000078 // Get the handler for this token. If there is no handler, ignore the pragma.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000079 PragmaHandler *Handler
80 = FindHandler(Tok.getIdentifierInfo() ? Tok.getIdentifierInfo()->getName()
81 : llvm::StringRef(),
82 /*IgnoreNull=*/false);
Chris Lattner21656f22009-04-19 21:10:26 +000083 if (Handler == 0) {
84 PP.Diag(Tok, diag::warn_pragma_ignored);
85 return;
86 }
Mike Stump11289f42009-09-09 15:08:12 +000087
Chris Lattnerb8761832006-06-24 21:31:03 +000088 // Otherwise, pass it down.
89 Handler->HandlePragma(PP, Tok);
90}
Chris Lattnerb694ba72006-07-02 22:41:36 +000091
Chris Lattnerb694ba72006-07-02 22:41:36 +000092//===----------------------------------------------------------------------===//
93// Preprocessor Pragma Directive Handling.
94//===----------------------------------------------------------------------===//
95
96/// HandlePragmaDirective - The "#pragma" directive has been parsed. Lex the
97/// rest of the pragma, passing it to the registered pragma handlers.
98void Preprocessor::HandlePragmaDirective() {
99 ++NumPragma;
Mike Stump11289f42009-09-09 15:08:12 +0000100
Chris Lattnerb694ba72006-07-02 22:41:36 +0000101 // Invoke the first level of pragma handlers which reads the namespace id.
Chris Lattner146762e2007-07-20 16:59:19 +0000102 Token Tok;
Chris Lattnerb694ba72006-07-02 22:41:36 +0000103 PragmaHandlers->HandlePragma(*this, Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000104
Chris Lattnerb694ba72006-07-02 22:41:36 +0000105 // If the pragma handler didn't read the rest of the line, consume it now.
Chris Lattnere7e65942009-06-18 05:55:53 +0000106 if (CurPPLexer && CurPPLexer->ParsingPreprocessorDirective)
Chris Lattnerb694ba72006-07-02 22:41:36 +0000107 DiscardUntilEndOfDirective();
108}
109
110/// Handle_Pragma - Read a _Pragma directive, slice it up, process it, then
111/// return the first token after the directive. The _Pragma token has just
112/// been read into 'Tok'.
Chris Lattner146762e2007-07-20 16:59:19 +0000113void Preprocessor::Handle_Pragma(Token &Tok) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000114 // Remember the pragma token location.
115 SourceLocation PragmaLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000116
Chris Lattnerb694ba72006-07-02 22:41:36 +0000117 // Read the '('.
118 Lex(Tok);
Chris Lattner907dfe92008-11-18 07:59:24 +0000119 if (Tok.isNot(tok::l_paren)) {
120 Diag(PragmaLoc, diag::err__Pragma_malformed);
121 return;
122 }
Chris Lattnerb694ba72006-07-02 22:41:36 +0000123
124 // Read the '"..."'.
125 Lex(Tok);
Chris Lattner907dfe92008-11-18 07:59:24 +0000126 if (Tok.isNot(tok::string_literal) && Tok.isNot(tok::wide_string_literal)) {
127 Diag(PragmaLoc, diag::err__Pragma_malformed);
128 return;
129 }
Mike Stump11289f42009-09-09 15:08:12 +0000130
Chris Lattnerb694ba72006-07-02 22:41:36 +0000131 // Remember the string.
132 std::string StrVal = getSpelling(Tok);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000133
134 // Read the ')'.
135 Lex(Tok);
Chris Lattner907dfe92008-11-18 07:59:24 +0000136 if (Tok.isNot(tok::r_paren)) {
137 Diag(PragmaLoc, diag::err__Pragma_malformed);
138 return;
139 }
Mike Stump11289f42009-09-09 15:08:12 +0000140
Chris Lattner9dc9c202009-02-15 20:52:18 +0000141 SourceLocation RParenLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000142
Chris Lattner262d4e32009-01-16 18:59:23 +0000143 // The _Pragma is lexically sound. Destringize according to C99 6.10.9.1:
144 // "The string literal is destringized by deleting the L prefix, if present,
145 // deleting the leading and trailing double-quotes, replacing each escape
146 // sequence \" by a double-quote, and replacing each escape sequence \\ by a
147 // single backslash."
Chris Lattnerb694ba72006-07-02 22:41:36 +0000148 if (StrVal[0] == 'L') // Remove L prefix.
149 StrVal.erase(StrVal.begin());
150 assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
151 "Invalid string token!");
Mike Stump11289f42009-09-09 15:08:12 +0000152
Chris Lattnerb694ba72006-07-02 22:41:36 +0000153 // Remove the front quote, replacing it with a space, so that the pragma
154 // contents appear to have a space before them.
155 StrVal[0] = ' ';
Mike Stump11289f42009-09-09 15:08:12 +0000156
Chris Lattnerfa217bd2009-03-08 08:08:45 +0000157 // Replace the terminating quote with a \n.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000158 StrVal[StrVal.size()-1] = '\n';
Mike Stump11289f42009-09-09 15:08:12 +0000159
Chris Lattnerb694ba72006-07-02 22:41:36 +0000160 // Remove escaped quotes and escapes.
161 for (unsigned i = 0, e = StrVal.size(); i != e-1; ++i) {
162 if (StrVal[i] == '\\' &&
163 (StrVal[i+1] == '\\' || StrVal[i+1] == '"')) {
164 // \\ -> '\' and \" -> '"'.
165 StrVal.erase(StrVal.begin()+i);
166 --e;
167 }
168 }
Mike Stump11289f42009-09-09 15:08:12 +0000169
Chris Lattnerf918bf72006-07-19 05:01:18 +0000170 // Plop the string (including the newline and trailing null) into a buffer
171 // where we can lex it.
Chris Lattner5a7971e2009-01-26 19:29:26 +0000172 Token TmpTok;
173 TmpTok.startToken();
174 CreateString(&StrVal[0], StrVal.size(), TmpTok);
175 SourceLocation TokLoc = TmpTok.getLocation();
Chris Lattnerb694ba72006-07-02 22:41:36 +0000176
Chris Lattnerb694ba72006-07-02 22:41:36 +0000177 // Make and enter a lexer object so that we lex and expand the tokens just
178 // like any others.
Chris Lattner9dc9c202009-02-15 20:52:18 +0000179 Lexer *TL = Lexer::Create_PragmaLexer(TokLoc, PragmaLoc, RParenLoc,
Chris Lattnerfa217bd2009-03-08 08:08:45 +0000180 StrVal.size(), *this);
Chris Lattner98a53122006-07-02 23:00:20 +0000181
182 EnterSourceFileWithLexer(TL, 0);
183
Chris Lattnerb694ba72006-07-02 22:41:36 +0000184 // With everything set up, lex this as a #pragma directive.
185 HandlePragmaDirective();
Mike Stump11289f42009-09-09 15:08:12 +0000186
Chris Lattnerb694ba72006-07-02 22:41:36 +0000187 // Finally, return whatever came after the pragma directive.
188 return Lex(Tok);
189}
190
191
192
193/// HandlePragmaOnce - Handle #pragma once. OnceTok is the 'once'.
194///
Chris Lattner146762e2007-07-20 16:59:19 +0000195void Preprocessor::HandlePragmaOnce(Token &OnceTok) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000196 if (isInPrimaryFile()) {
197 Diag(OnceTok, diag::pp_pragma_once_in_main_file);
198 return;
199 }
Mike Stump11289f42009-09-09 15:08:12 +0000200
Chris Lattnerb694ba72006-07-02 22:41:36 +0000201 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000202 // Mark the file as a once-only file now.
Chris Lattnerd32480d2009-01-17 06:22:33 +0000203 HeaderInfo.MarkFileIncludeOnce(getCurrentFileLexer()->getFileEntry());
Chris Lattnerb694ba72006-07-02 22:41:36 +0000204}
205
Chris Lattnerc2383312007-12-19 19:38:36 +0000206void Preprocessor::HandlePragmaMark() {
Ted Kremenek76c34412008-11-19 22:21:33 +0000207 assert(CurPPLexer && "No current lexer?");
Chris Lattnerd9efb6e2009-06-15 05:02:34 +0000208 if (CurLexer)
209 CurLexer->ReadToEndOfLine();
210 else
211 CurPTHLexer->DiscardToEndOfLine();
Chris Lattnerc2383312007-12-19 19:38:36 +0000212}
213
214
Chris Lattnerb694ba72006-07-02 22:41:36 +0000215/// HandlePragmaPoison - Handle #pragma GCC poison. PoisonTok is the 'poison'.
216///
Chris Lattner146762e2007-07-20 16:59:19 +0000217void Preprocessor::HandlePragmaPoison(Token &PoisonTok) {
218 Token Tok;
Chris Lattner538d7f32006-07-20 04:31:52 +0000219
Chris Lattnerb694ba72006-07-02 22:41:36 +0000220 while (1) {
221 // Read the next token to poison. While doing this, pretend that we are
222 // skipping while reading the identifier to poison.
223 // This avoids errors on code like:
224 // #pragma GCC poison X
225 // #pragma GCC poison X
Ted Kremenek551c82a2008-11-18 01:12:54 +0000226 if (CurPPLexer) CurPPLexer->LexingRawMode = true;
Chris Lattnerb694ba72006-07-02 22:41:36 +0000227 LexUnexpandedToken(Tok);
Ted Kremenek551c82a2008-11-18 01:12:54 +0000228 if (CurPPLexer) CurPPLexer->LexingRawMode = false;
Mike Stump11289f42009-09-09 15:08:12 +0000229
Chris Lattnerb694ba72006-07-02 22:41:36 +0000230 // If we reached the end of line, we're done.
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000231 if (Tok.is(tok::eom)) return;
Mike Stump11289f42009-09-09 15:08:12 +0000232
Chris Lattnerb694ba72006-07-02 22:41:36 +0000233 // Can only poison identifiers.
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000234 if (Tok.isNot(tok::identifier)) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000235 Diag(Tok, diag::err_pp_invalid_poison);
236 return;
237 }
Mike Stump11289f42009-09-09 15:08:12 +0000238
Chris Lattnercefc7682006-07-08 08:28:12 +0000239 // Look up the identifier info for the token. We disabled identifier lookup
240 // by saying we're skipping contents, so we need to do this manually.
241 IdentifierInfo *II = LookUpIdentifierInfo(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000242
Chris Lattnerb694ba72006-07-02 22:41:36 +0000243 // Already poisoned.
244 if (II->isPoisoned()) continue;
Mike Stump11289f42009-09-09 15:08:12 +0000245
Chris Lattnerb694ba72006-07-02 22:41:36 +0000246 // If this is a macro identifier, emit a warning.
Chris Lattner259716a2007-10-07 08:04:56 +0000247 if (II->hasMacroDefinition())
Chris Lattnerb694ba72006-07-02 22:41:36 +0000248 Diag(Tok, diag::pp_poisoning_existing_macro);
Mike Stump11289f42009-09-09 15:08:12 +0000249
Chris Lattnerb694ba72006-07-02 22:41:36 +0000250 // Finally, poison it!
251 II->setIsPoisoned();
252 }
253}
254
255/// HandlePragmaSystemHeader - Implement #pragma GCC system_header. We know
256/// that the whole directive has been parsed.
Chris Lattner146762e2007-07-20 16:59:19 +0000257void Preprocessor::HandlePragmaSystemHeader(Token &SysHeaderTok) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000258 if (isInPrimaryFile()) {
259 Diag(SysHeaderTok, diag::pp_pragma_sysheader_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.
Ted Kremenek300590b2008-11-20 01:45:11 +0000264 PreprocessorLexer *TheLexer = getCurrentFileLexer();
Mike Stump11289f42009-09-09 15:08:12 +0000265
Chris Lattnerb694ba72006-07-02 22:41:36 +0000266 // Mark the file as a system header.
Chris Lattnerd32480d2009-01-17 06:22:33 +0000267 HeaderInfo.MarkFileSystemHeader(TheLexer->getFileEntry());
Mike Stump11289f42009-09-09 15:08:12 +0000268
269
Chris Lattnerd9efb6e2009-06-15 05:02:34 +0000270 PresumedLoc PLoc = SourceMgr.getPresumedLoc(SysHeaderTok.getLocation());
271 unsigned FilenameLen = strlen(PLoc.getFilename());
272 unsigned FilenameID = SourceMgr.getLineTableFilenameID(PLoc.getFilename(),
273 FilenameLen);
Mike Stump11289f42009-09-09 15:08:12 +0000274
Chris Lattnerd9efb6e2009-06-15 05:02:34 +0000275 // Emit a line marker. This will change any source locations from this point
276 // forward to realize they are in a system header.
277 // Create a line note with this information.
278 SourceMgr.AddLineNote(SysHeaderTok.getLocation(), PLoc.getLine(), FilenameID,
279 false, false, true, false);
Mike Stump11289f42009-09-09 15:08:12 +0000280
Chris Lattnerb694ba72006-07-02 22:41:36 +0000281 // Notify the client, if desired, that we are in a new source file.
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000282 if (Callbacks)
Ted Kremenek300590b2008-11-20 01:45:11 +0000283 Callbacks->FileChanged(SysHeaderTok.getLocation(),
Chris Lattnerb03dc762008-09-26 21:18:42 +0000284 PPCallbacks::SystemHeaderPragma, SrcMgr::C_System);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000285}
286
287/// HandlePragmaDependency - Handle #pragma GCC dependency "foo" blah.
288///
Chris Lattner146762e2007-07-20 16:59:19 +0000289void Preprocessor::HandlePragmaDependency(Token &DependencyTok) {
290 Token FilenameTok;
Ted Kremenek551c82a2008-11-18 01:12:54 +0000291 CurPPLexer->LexIncludeFilename(FilenameTok);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000292
293 // If the token kind is EOM, the error has already been diagnosed.
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000294 if (FilenameTok.is(tok::eom))
Chris Lattnerb694ba72006-07-02 22:41:36 +0000295 return;
Mike Stump11289f42009-09-09 15:08:12 +0000296
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000297 // Reserve a buffer to get the spelling.
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000298 llvm::SmallString<128> FilenameBuffer;
Douglas Gregordc970f02010-03-16 22:30:13 +0000299 bool Invalid = false;
300 llvm::StringRef Filename = getSpelling(FilenameTok, FilenameBuffer, &Invalid);
301 if (Invalid)
302 return;
Mike Stump11289f42009-09-09 15:08:12 +0000303
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000304 bool isAngled =
305 GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000306 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
307 // error.
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000308 if (Filename.empty())
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000309 return;
Mike Stump11289f42009-09-09 15:08:12 +0000310
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000311 // Search include directories for this file.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000312 const DirectoryLookup *CurDir;
Chris Lattnerfde85352010-01-22 00:14:44 +0000313 const FileEntry *File = LookupFile(Filename, isAngled, 0, CurDir);
Chris Lattner97b8e842008-11-18 08:02:48 +0000314 if (File == 0) {
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000315 Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
Chris Lattner97b8e842008-11-18 08:02:48 +0000316 return;
317 }
Mike Stump11289f42009-09-09 15:08:12 +0000318
Chris Lattnerd32480d2009-01-17 06:22:33 +0000319 const FileEntry *CurFile = getCurrentFileLexer()->getFileEntry();
Chris Lattnerb694ba72006-07-02 22:41:36 +0000320
321 // If this file is older than the file it depends on, emit a diagnostic.
322 if (CurFile && CurFile->getModificationTime() < File->getModificationTime()) {
323 // Lex tokens at the end of the message and include them in the message.
324 std::string Message;
325 Lex(DependencyTok);
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000326 while (DependencyTok.isNot(tok::eom)) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000327 Message += getSpelling(DependencyTok) + " ";
328 Lex(DependencyTok);
329 }
Mike Stump11289f42009-09-09 15:08:12 +0000330
Chris Lattnerb694ba72006-07-02 22:41:36 +0000331 Message.erase(Message.end()-1);
Chris Lattner97b8e842008-11-18 08:02:48 +0000332 Diag(FilenameTok, diag::pp_out_of_date_dependency) << Message;
Chris Lattnerb694ba72006-07-02 22:41:36 +0000333 }
334}
335
Chris Lattner2ff698d2009-01-16 08:21:25 +0000336/// HandlePragmaComment - Handle the microsoft #pragma comment extension. The
337/// syntax is:
338/// #pragma comment(linker, "foo")
339/// 'linker' is one of five identifiers: compiler, exestr, lib, linker, user.
340/// "foo" is a string, which is fully macro expanded, and permits string
Gabor Greif31a082f2009-03-17 11:39:38 +0000341/// concatenation, embedded escape characters etc. See MSDN for more details.
Chris Lattner2ff698d2009-01-16 08:21:25 +0000342void Preprocessor::HandlePragmaComment(Token &Tok) {
343 SourceLocation CommentLoc = Tok.getLocation();
344 Lex(Tok);
345 if (Tok.isNot(tok::l_paren)) {
346 Diag(CommentLoc, diag::err_pragma_comment_malformed);
347 return;
348 }
Mike Stump11289f42009-09-09 15:08:12 +0000349
Chris Lattner2ff698d2009-01-16 08:21:25 +0000350 // Read the identifier.
351 Lex(Tok);
352 if (Tok.isNot(tok::identifier)) {
353 Diag(CommentLoc, diag::err_pragma_comment_malformed);
354 return;
355 }
Mike Stump11289f42009-09-09 15:08:12 +0000356
Chris Lattner2ff698d2009-01-16 08:21:25 +0000357 // Verify that this is one of the 5 whitelisted options.
358 // FIXME: warn that 'exestr' is deprecated.
359 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000360 if (!II->isStr("compiler") && !II->isStr("exestr") && !II->isStr("lib") &&
Chris Lattner2ff698d2009-01-16 08:21:25 +0000361 !II->isStr("linker") && !II->isStr("user")) {
362 Diag(Tok.getLocation(), diag::err_pragma_comment_unknown_kind);
363 return;
364 }
Mike Stump11289f42009-09-09 15:08:12 +0000365
Chris Lattner262d4e32009-01-16 18:59:23 +0000366 // Read the optional string if present.
Chris Lattner2ff698d2009-01-16 08:21:25 +0000367 Lex(Tok);
Chris Lattner262d4e32009-01-16 18:59:23 +0000368 std::string ArgumentString;
Chris Lattner2ff698d2009-01-16 08:21:25 +0000369 if (Tok.is(tok::comma)) {
Chris Lattner262d4e32009-01-16 18:59:23 +0000370 Lex(Tok); // eat the comma.
Chris Lattner2ff698d2009-01-16 08:21:25 +0000371
372 // We need at least one string.
Chris Lattner504af112009-04-19 23:16:58 +0000373 if (Tok.isNot(tok::string_literal)) {
Chris Lattner2ff698d2009-01-16 08:21:25 +0000374 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
375 return;
376 }
377
378 // String concatenation allows multiple strings, which can even come from
379 // macro expansion.
380 // "foo " "bar" "Baz"
Chris Lattner262d4e32009-01-16 18:59:23 +0000381 llvm::SmallVector<Token, 4> StrToks;
Chris Lattner504af112009-04-19 23:16:58 +0000382 while (Tok.is(tok::string_literal)) {
Chris Lattner262d4e32009-01-16 18:59:23 +0000383 StrToks.push_back(Tok);
Chris Lattner2ff698d2009-01-16 08:21:25 +0000384 Lex(Tok);
Chris Lattner262d4e32009-01-16 18:59:23 +0000385 }
386
387 // Concatenate and parse the strings.
388 StringLiteralParser Literal(&StrToks[0], StrToks.size(), *this);
389 assert(!Literal.AnyWide && "Didn't allow wide strings in");
390 if (Literal.hadError)
391 return;
392 if (Literal.Pascal) {
393 Diag(StrToks[0].getLocation(), diag::err_pragma_comment_malformed);
394 return;
395 }
396
397 ArgumentString = std::string(Literal.GetString(),
398 Literal.GetString()+Literal.GetStringLength());
Chris Lattner2ff698d2009-01-16 08:21:25 +0000399 }
Mike Stump11289f42009-09-09 15:08:12 +0000400
Chris Lattner262d4e32009-01-16 18:59:23 +0000401 // FIXME: If the kind is "compiler" warn if the string is present (it is
402 // ignored).
403 // FIXME: 'lib' requires a comment string.
404 // FIXME: 'linker' requires a comment string, and has a specific list of
405 // things that are allowable.
Mike Stump11289f42009-09-09 15:08:12 +0000406
Chris Lattner2ff698d2009-01-16 08:21:25 +0000407 if (Tok.isNot(tok::r_paren)) {
408 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
409 return;
410 }
Chris Lattner262d4e32009-01-16 18:59:23 +0000411 Lex(Tok); // eat the r_paren.
Chris Lattner2ff698d2009-01-16 08:21:25 +0000412
413 if (Tok.isNot(tok::eom)) {
414 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
415 return;
416 }
Mike Stump11289f42009-09-09 15:08:12 +0000417
Chris Lattner262d4e32009-01-16 18:59:23 +0000418 // If the pragma is lexically sound, notify any interested PPCallbacks.
Chris Lattnerf49775d2009-01-16 19:01:46 +0000419 if (Callbacks)
420 Callbacks->PragmaComment(CommentLoc, II, ArgumentString);
Chris Lattner2ff698d2009-01-16 08:21:25 +0000421}
422
Chris Lattner30c924b2010-06-26 17:11:39 +0000423/// HandlePragmaMessage - Handle the microsoft #pragma message extension. The
424/// syntax is:
425/// #pragma message(messagestring)
426/// messagestring is a string, which is fully macro expanded, and permits string
427/// concatenation, embedded escape characters etc. See MSDN for more details.
428void Preprocessor::HandlePragmaMessage(Token &Tok) {
429 SourceLocation MessageLoc = Tok.getLocation();
430 Lex(Tok);
431 if (Tok.isNot(tok::l_paren)) {
432 Diag(MessageLoc, diag::err_pragma_message_malformed);
433 return;
434 }
Chris Lattner2ff698d2009-01-16 08:21:25 +0000435
Chris Lattner30c924b2010-06-26 17:11:39 +0000436 // Read the string.
437 Lex(Tok);
438
439
440 // We need at least one string.
441 if (Tok.isNot(tok::string_literal)) {
442 Diag(Tok.getLocation(), diag::err_pragma_message_malformed);
443 return;
444 }
445
446 // String concatenation allows multiple strings, which can even come from
447 // macro expansion.
448 // "foo " "bar" "Baz"
449 llvm::SmallVector<Token, 4> StrToks;
450 while (Tok.is(tok::string_literal)) {
451 StrToks.push_back(Tok);
452 Lex(Tok);
453 }
454
455 // Concatenate and parse the strings.
456 StringLiteralParser Literal(&StrToks[0], StrToks.size(), *this);
457 assert(!Literal.AnyWide && "Didn't allow wide strings in");
458 if (Literal.hadError)
459 return;
460 if (Literal.Pascal) {
461 Diag(StrToks[0].getLocation(), diag::err_pragma_message_malformed);
462 return;
463 }
464
465 llvm::StringRef MessageString(Literal.GetString(), Literal.GetStringLength());
466
467 if (Tok.isNot(tok::r_paren)) {
468 Diag(Tok.getLocation(), diag::err_pragma_message_malformed);
469 return;
470 }
471 Lex(Tok); // eat the r_paren.
472
473 if (Tok.isNot(tok::eom)) {
474 Diag(Tok.getLocation(), diag::err_pragma_message_malformed);
475 return;
476 }
477
478 // Output the message.
479 Diag(MessageLoc, diag::warn_pragma_message) << MessageString;
480
481 // If the pragma is lexically sound, notify any interested PPCallbacks.
482 if (Callbacks)
483 Callbacks->PragmaMessage(MessageLoc, MessageString);
484}
Chris Lattner2ff698d2009-01-16 08:21:25 +0000485
Chris Lattnerb694ba72006-07-02 22:41:36 +0000486
487/// AddPragmaHandler - Add the specified pragma handler to the preprocessor.
488/// If 'Namespace' is non-null, then it is a token required to exist on the
489/// pragma line before the pragma string starts, e.g. "STDC" or "GCC".
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000490void Preprocessor::AddPragmaHandler(llvm::StringRef Namespace,
Chris Lattnerb694ba72006-07-02 22:41:36 +0000491 PragmaHandler *Handler) {
492 PragmaNamespace *InsertNS = PragmaHandlers;
Mike Stump11289f42009-09-09 15:08:12 +0000493
Chris Lattnerb694ba72006-07-02 22:41:36 +0000494 // If this is specified to be in a namespace, step down into it.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000495 if (!Namespace.empty()) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000496 // If there is already a pragma handler with the name of this namespace,
497 // we either have an error (directive with the same name as a namespace) or
498 // we already have the namespace to insert into.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000499 if (PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace)) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000500 InsertNS = Existing->getIfNamespace();
501 assert(InsertNS != 0 && "Cannot have a pragma namespace and pragma"
502 " handler with the same name!");
503 } else {
504 // Otherwise, this namespace doesn't exist yet, create and insert the
505 // handler for it.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000506 InsertNS = new PragmaNamespace(Namespace);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000507 PragmaHandlers->AddPragma(InsertNS);
508 }
509 }
Mike Stump11289f42009-09-09 15:08:12 +0000510
Chris Lattnerb694ba72006-07-02 22:41:36 +0000511 // Check to make sure we don't already have a pragma for this identifier.
512 assert(!InsertNS->FindHandler(Handler->getName()) &&
513 "Pragma handler already exists for this identifier!");
514 InsertNS->AddPragma(Handler);
515}
516
Daniel Dunbar40596532008-10-04 19:17:46 +0000517/// RemovePragmaHandler - Remove the specific pragma handler from the
518/// preprocessor. If \arg Namespace is non-null, then it should be the
519/// namespace that \arg Handler was added to. It is an error to remove
520/// a handler that has not been registered.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000521void Preprocessor::RemovePragmaHandler(llvm::StringRef Namespace,
Daniel Dunbar40596532008-10-04 19:17:46 +0000522 PragmaHandler *Handler) {
523 PragmaNamespace *NS = PragmaHandlers;
Mike Stump11289f42009-09-09 15:08:12 +0000524
Daniel Dunbar40596532008-10-04 19:17:46 +0000525 // If this is specified to be in a namespace, step down into it.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000526 if (!Namespace.empty()) {
527 PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace);
Daniel Dunbar40596532008-10-04 19:17:46 +0000528 assert(Existing && "Namespace containing handler does not exist!");
529
530 NS = Existing->getIfNamespace();
531 assert(NS && "Invalid namespace, registered as a regular pragma handler!");
532 }
533
534 NS->RemovePragmaHandler(Handler);
Mike Stump11289f42009-09-09 15:08:12 +0000535
Daniel Dunbar40596532008-10-04 19:17:46 +0000536 // If this is a non-default namespace and it is now empty, remove
537 // it.
538 if (NS != PragmaHandlers && NS->IsEmpty())
539 PragmaHandlers->RemovePragmaHandler(NS);
540}
541
Chris Lattnerb694ba72006-07-02 22:41:36 +0000542namespace {
Chris Lattnerc2383312007-12-19 19:38:36 +0000543/// PragmaOnceHandler - "#pragma once" marks the file as atomically included.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000544struct PragmaOnceHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000545 PragmaOnceHandler() : PragmaHandler("once") {}
Chris Lattner146762e2007-07-20 16:59:19 +0000546 virtual void HandlePragma(Preprocessor &PP, Token &OnceTok) {
Chris Lattnerce2ab6f2009-04-14 05:07:49 +0000547 PP.CheckEndOfDirective("pragma once");
Chris Lattnerb694ba72006-07-02 22:41:36 +0000548 PP.HandlePragmaOnce(OnceTok);
549 }
550};
551
Chris Lattnerc2383312007-12-19 19:38:36 +0000552/// PragmaMarkHandler - "#pragma mark ..." is ignored by the compiler, and the
553/// rest of the line is not lexed.
554struct PragmaMarkHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000555 PragmaMarkHandler() : PragmaHandler("mark") {}
Chris Lattnerc2383312007-12-19 19:38:36 +0000556 virtual void HandlePragma(Preprocessor &PP, Token &MarkTok) {
557 PP.HandlePragmaMark();
558 }
559};
560
561/// PragmaPoisonHandler - "#pragma poison x" marks x as not usable.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000562struct PragmaPoisonHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000563 PragmaPoisonHandler() : PragmaHandler("poison") {}
Chris Lattner146762e2007-07-20 16:59:19 +0000564 virtual void HandlePragma(Preprocessor &PP, Token &PoisonTok) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000565 PP.HandlePragmaPoison(PoisonTok);
566 }
567};
568
Chris Lattnerc2383312007-12-19 19:38:36 +0000569/// PragmaSystemHeaderHandler - "#pragma system_header" marks the current file
570/// as a system header, which silences warnings in it.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000571struct PragmaSystemHeaderHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000572 PragmaSystemHeaderHandler() : PragmaHandler("system_header") {}
Chris Lattner146762e2007-07-20 16:59:19 +0000573 virtual void HandlePragma(Preprocessor &PP, Token &SHToken) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000574 PP.HandlePragmaSystemHeader(SHToken);
Chris Lattnerce2ab6f2009-04-14 05:07:49 +0000575 PP.CheckEndOfDirective("pragma");
Chris Lattnerb694ba72006-07-02 22:41:36 +0000576 }
577};
578struct PragmaDependencyHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000579 PragmaDependencyHandler() : PragmaHandler("dependency") {}
Chris Lattner146762e2007-07-20 16:59:19 +0000580 virtual void HandlePragma(Preprocessor &PP, Token &DepToken) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000581 PP.HandlePragmaDependency(DepToken);
582 }
583};
Mike Stump11289f42009-09-09 15:08:12 +0000584
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000585struct PragmaDebugHandler : public PragmaHandler {
586 PragmaDebugHandler() : PragmaHandler("__debug") {}
587 virtual void HandlePragma(Preprocessor &PP, Token &DepToken) {
588 Token Tok;
589 PP.LexUnexpandedToken(Tok);
590 if (Tok.isNot(tok::identifier)) {
591 PP.Diag(Tok, diag::warn_pragma_diagnostic_clang_invalid);
592 return;
593 }
594 IdentifierInfo *II = Tok.getIdentifierInfo();
595
596 if (II->isStr("overflow_stack")) {
597 DebugOverflowStack();
598 } else if (II->isStr("crash")) {
599 DebugCrash();
600 }
601 }
602
603 void DebugOverflowStack() {
604 DebugOverflowStack();
605 }
606
607 void DebugCrash() {
Daniel Dunbar51738ba2010-07-29 02:46:02 +0000608 *(volatile int*) 0x11 = 0;
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000609 }
610};
611
Chris Lattner504af112009-04-19 23:16:58 +0000612/// PragmaDiagnosticHandler - e.g. '#pragma GCC diagnostic ignored "-Wformat"'
Chris Lattnerfb42a182009-07-12 21:18:45 +0000613/// Since clang's diagnostic supports extended functionality beyond GCC's
614/// the constructor takes a clangMode flag to tell it whether or not to allow
615/// clang's extended functionality, or whether to reject it.
Chris Lattner504af112009-04-19 23:16:58 +0000616struct PragmaDiagnosticHandler : public PragmaHandler {
Chris Lattnerfb42a182009-07-12 21:18:45 +0000617private:
618 const bool ClangMode;
619public:
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000620 explicit PragmaDiagnosticHandler(const bool clangMode)
621 : PragmaHandler("diagnostic"), ClangMode(clangMode) {}
622
Chris Lattner504af112009-04-19 23:16:58 +0000623 virtual void HandlePragma(Preprocessor &PP, Token &DiagToken) {
624 Token Tok;
625 PP.LexUnexpandedToken(Tok);
626 if (Tok.isNot(tok::identifier)) {
Chris Lattnerfb42a182009-07-12 21:18:45 +0000627 unsigned Diag = ClangMode ? diag::warn_pragma_diagnostic_clang_invalid
628 : diag::warn_pragma_diagnostic_gcc_invalid;
629 PP.Diag(Tok, Diag);
Chris Lattner504af112009-04-19 23:16:58 +0000630 return;
631 }
632 IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000633
Chris Lattner504af112009-04-19 23:16:58 +0000634 diag::Mapping Map;
635 if (II->isStr("warning"))
636 Map = diag::MAP_WARNING;
637 else if (II->isStr("error"))
638 Map = diag::MAP_ERROR;
639 else if (II->isStr("ignored"))
640 Map = diag::MAP_IGNORE;
641 else if (II->isStr("fatal"))
642 Map = diag::MAP_FATAL;
Chris Lattnerfb42a182009-07-12 21:18:45 +0000643 else if (ClangMode) {
644 if (II->isStr("pop")) {
Mike Stump11289f42009-09-09 15:08:12 +0000645 if (!PP.getDiagnostics().popMappings())
Chris Lattnerfb42a182009-07-12 21:18:45 +0000646 PP.Diag(Tok, diag::warn_pragma_diagnostic_clang_cannot_ppp);
647 return;
648 }
649
650 if (II->isStr("push")) {
651 PP.getDiagnostics().pushMappings();
Mike Stump11289f42009-09-09 15:08:12 +0000652 return;
Chris Lattnerfb42a182009-07-12 21:18:45 +0000653 }
654
655 PP.Diag(Tok, diag::warn_pragma_diagnostic_clang_invalid);
656 return;
657 } else {
658 PP.Diag(Tok, diag::warn_pragma_diagnostic_gcc_invalid);
Chris Lattner504af112009-04-19 23:16:58 +0000659 return;
660 }
Mike Stump11289f42009-09-09 15:08:12 +0000661
Chris Lattner504af112009-04-19 23:16:58 +0000662 PP.LexUnexpandedToken(Tok);
663
664 // We need at least one string.
665 if (Tok.isNot(tok::string_literal)) {
666 PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token);
667 return;
668 }
Mike Stump11289f42009-09-09 15:08:12 +0000669
Chris Lattner504af112009-04-19 23:16:58 +0000670 // String concatenation allows multiple strings, which can even come from
671 // macro expansion.
672 // "foo " "bar" "Baz"
673 llvm::SmallVector<Token, 4> StrToks;
674 while (Tok.is(tok::string_literal)) {
675 StrToks.push_back(Tok);
676 PP.LexUnexpandedToken(Tok);
677 }
Mike Stump11289f42009-09-09 15:08:12 +0000678
Chris Lattner504af112009-04-19 23:16:58 +0000679 if (Tok.isNot(tok::eom)) {
680 PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token);
681 return;
682 }
Mike Stump11289f42009-09-09 15:08:12 +0000683
Chris Lattner504af112009-04-19 23:16:58 +0000684 // Concatenate and parse the strings.
685 StringLiteralParser Literal(&StrToks[0], StrToks.size(), PP);
686 assert(!Literal.AnyWide && "Didn't allow wide strings in");
687 if (Literal.hadError)
688 return;
689 if (Literal.Pascal) {
Chris Lattnerfb42a182009-07-12 21:18:45 +0000690 unsigned Diag = ClangMode ? diag::warn_pragma_diagnostic_clang_invalid
691 : diag::warn_pragma_diagnostic_gcc_invalid;
692 PP.Diag(Tok, Diag);
Chris Lattner504af112009-04-19 23:16:58 +0000693 return;
694 }
Chris Lattnerfb42a182009-07-12 21:18:45 +0000695
Chris Lattner504af112009-04-19 23:16:58 +0000696 std::string WarningName(Literal.GetString(),
697 Literal.GetString()+Literal.GetStringLength());
698
699 if (WarningName.size() < 3 || WarningName[0] != '-' ||
700 WarningName[1] != 'W') {
701 PP.Diag(StrToks[0].getLocation(),
702 diag::warn_pragma_diagnostic_invalid_option);
703 return;
704 }
Mike Stump11289f42009-09-09 15:08:12 +0000705
Chris Lattner504af112009-04-19 23:16:58 +0000706 if (PP.getDiagnostics().setDiagnosticGroupMapping(WarningName.c_str()+2,
707 Map))
708 PP.Diag(StrToks[0].getLocation(),
709 diag::warn_pragma_diagnostic_unknown_warning) << WarningName;
710 }
711};
Mike Stump11289f42009-09-09 15:08:12 +0000712
Chris Lattner2ff698d2009-01-16 08:21:25 +0000713/// PragmaCommentHandler - "#pragma comment ...".
714struct PragmaCommentHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000715 PragmaCommentHandler() : PragmaHandler("comment") {}
Chris Lattner2ff698d2009-01-16 08:21:25 +0000716 virtual void HandlePragma(Preprocessor &PP, Token &CommentTok) {
717 PP.HandlePragmaComment(CommentTok);
718 }
719};
Mike Stump11289f42009-09-09 15:08:12 +0000720
Chris Lattner30c924b2010-06-26 17:11:39 +0000721/// PragmaMessageHandler - "#pragma message("...")".
722struct PragmaMessageHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000723 PragmaMessageHandler() : PragmaHandler("message") {}
Chris Lattner30c924b2010-06-26 17:11:39 +0000724 virtual void HandlePragma(Preprocessor &PP, Token &CommentTok) {
725 PP.HandlePragmaMessage(CommentTok);
726 }
727};
728
Chris Lattner958ee042009-04-19 21:20:35 +0000729// Pragma STDC implementations.
Chris Lattner02ef4e32009-04-19 21:50:08 +0000730
731enum STDCSetting {
732 STDC_ON, STDC_OFF, STDC_DEFAULT, STDC_INVALID
733};
Mike Stump11289f42009-09-09 15:08:12 +0000734
Chris Lattner02ef4e32009-04-19 21:50:08 +0000735static STDCSetting LexOnOffSwitch(Preprocessor &PP) {
736 Token Tok;
737 PP.LexUnexpandedToken(Tok);
738
739 if (Tok.isNot(tok::identifier)) {
740 PP.Diag(Tok, diag::ext_stdc_pragma_syntax);
741 return STDC_INVALID;
742 }
743 IdentifierInfo *II = Tok.getIdentifierInfo();
744 STDCSetting Result;
745 if (II->isStr("ON"))
746 Result = STDC_ON;
747 else if (II->isStr("OFF"))
748 Result = STDC_OFF;
749 else if (II->isStr("DEFAULT"))
750 Result = STDC_DEFAULT;
751 else {
752 PP.Diag(Tok, diag::ext_stdc_pragma_syntax);
753 return STDC_INVALID;
754 }
755
756 // Verify that this is followed by EOM.
757 PP.LexUnexpandedToken(Tok);
758 if (Tok.isNot(tok::eom))
759 PP.Diag(Tok, diag::ext_stdc_pragma_syntax_eom);
760 return Result;
761}
Mike Stump11289f42009-09-09 15:08:12 +0000762
Chris Lattner958ee042009-04-19 21:20:35 +0000763/// PragmaSTDC_FP_CONTRACTHandler - "#pragma STDC FP_CONTRACT ...".
764struct PragmaSTDC_FP_CONTRACTHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000765 PragmaSTDC_FP_CONTRACTHandler() : PragmaHandler("FP_CONTRACT") {}
Chris Lattnera0b1f762009-04-19 21:25:37 +0000766 virtual void HandlePragma(Preprocessor &PP, Token &Tok) {
Chris Lattner02ef4e32009-04-19 21:50:08 +0000767 // We just ignore the setting of FP_CONTRACT. Since we don't do contractions
768 // at all, our default is OFF and setting it to ON is an optimization hint
769 // we can safely ignore. When we support -ffma or something, we would need
770 // to diagnose that we are ignoring FMA.
771 LexOnOffSwitch(PP);
Chris Lattner958ee042009-04-19 21:20:35 +0000772 }
773};
Mike Stump11289f42009-09-09 15:08:12 +0000774
Chris Lattner958ee042009-04-19 21:20:35 +0000775/// PragmaSTDC_FENV_ACCESSHandler - "#pragma STDC FENV_ACCESS ...".
776struct PragmaSTDC_FENV_ACCESSHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000777 PragmaSTDC_FENV_ACCESSHandler() : PragmaHandler("FENV_ACCESS") {}
Chris Lattnera0b1f762009-04-19 21:25:37 +0000778 virtual void HandlePragma(Preprocessor &PP, Token &Tok) {
Chris Lattnerdf222682009-04-19 21:55:32 +0000779 if (LexOnOffSwitch(PP) == STDC_ON)
780 PP.Diag(Tok, diag::warn_stdc_fenv_access_not_supported);
Chris Lattner958ee042009-04-19 21:20:35 +0000781 }
782};
Mike Stump11289f42009-09-09 15:08:12 +0000783
Chris Lattner958ee042009-04-19 21:20:35 +0000784/// PragmaSTDC_CX_LIMITED_RANGEHandler - "#pragma STDC CX_LIMITED_RANGE ...".
785struct PragmaSTDC_CX_LIMITED_RANGEHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000786 PragmaSTDC_CX_LIMITED_RANGEHandler()
787 : PragmaHandler("CX_LIMITED_RANGE") {}
Chris Lattnera0b1f762009-04-19 21:25:37 +0000788 virtual void HandlePragma(Preprocessor &PP, Token &Tok) {
Chris Lattner02ef4e32009-04-19 21:50:08 +0000789 LexOnOffSwitch(PP);
Chris Lattner958ee042009-04-19 21:20:35 +0000790 }
791};
Mike Stump11289f42009-09-09 15:08:12 +0000792
Chris Lattner958ee042009-04-19 21:20:35 +0000793/// PragmaSTDC_UnknownHandler - "#pragma STDC ...".
794struct PragmaSTDC_UnknownHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000795 PragmaSTDC_UnknownHandler() {}
Chris Lattnera0b1f762009-04-19 21:25:37 +0000796 virtual void HandlePragma(Preprocessor &PP, Token &UnknownTok) {
Chris Lattner02ef4e32009-04-19 21:50:08 +0000797 // C99 6.10.6p2, unknown forms are not allowed.
Chris Lattnera0b1f762009-04-19 21:25:37 +0000798 PP.Diag(UnknownTok, diag::ext_stdc_pragma_ignored);
Chris Lattner958ee042009-04-19 21:20:35 +0000799 }
800};
Mike Stump11289f42009-09-09 15:08:12 +0000801
Chris Lattnerb694ba72006-07-02 22:41:36 +0000802} // end anonymous namespace
803
804
805/// RegisterBuiltinPragmas - Install the standard preprocessor pragmas:
806/// #pragma GCC poison/system_header/dependency and #pragma once.
807void Preprocessor::RegisterBuiltinPragmas() {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000808 AddPragmaHandler(new PragmaOnceHandler());
809 AddPragmaHandler(new PragmaMarkHandler());
Mike Stump11289f42009-09-09 15:08:12 +0000810
Chris Lattnerb61448d2009-05-12 18:21:11 +0000811 // #pragma GCC ...
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000812 AddPragmaHandler("GCC", new PragmaPoisonHandler());
813 AddPragmaHandler("GCC", new PragmaSystemHeaderHandler());
814 AddPragmaHandler("GCC", new PragmaDependencyHandler());
815 AddPragmaHandler("GCC", new PragmaDiagnosticHandler(false));
Chris Lattnerb61448d2009-05-12 18:21:11 +0000816 // #pragma clang ...
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000817 AddPragmaHandler("clang", new PragmaPoisonHandler());
818 AddPragmaHandler("clang", new PragmaSystemHeaderHandler());
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000819 AddPragmaHandler("clang", new PragmaDebugHandler());
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000820 AddPragmaHandler("clang", new PragmaDependencyHandler());
821 AddPragmaHandler("clang", new PragmaDiagnosticHandler(true));
Chris Lattnerb61448d2009-05-12 18:21:11 +0000822
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000823 AddPragmaHandler("STDC", new PragmaSTDC_FP_CONTRACTHandler());
824 AddPragmaHandler("STDC", new PragmaSTDC_FENV_ACCESSHandler());
825 AddPragmaHandler("STDC", new PragmaSTDC_CX_LIMITED_RANGEHandler());
Chris Lattner958ee042009-04-19 21:20:35 +0000826 AddPragmaHandler("STDC", new PragmaSTDC_UnknownHandler());
Mike Stump11289f42009-09-09 15:08:12 +0000827
Chris Lattner2ff698d2009-01-16 08:21:25 +0000828 // MS extensions.
Chris Lattner30c924b2010-06-26 17:11:39 +0000829 if (Features.Microsoft) {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000830 AddPragmaHandler(new PragmaCommentHandler());
831 AddPragmaHandler(new PragmaMessageHandler());
Chris Lattner30c924b2010-06-26 17:11:39 +0000832 }
Chris Lattnerb694ba72006-07-02 22:41:36 +0000833}