blob: 7bf409405ab138129bd945be4fd0dbef5b01cde5 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Pragma.cpp - Pragma registration and handling --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the PragmaHandler/PragmaTable interfaces and implements
11// pragma related methods of the Preprocessor class.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Lex/Pragma.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000016#include "clang/Lex/HeaderSearch.h"
Chris Lattnera9d91452009-01-16 18:59:23 +000017#include "clang/Lex/LiteralSupport.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000018#include "clang/Lex/Preprocessor.h"
Chris Lattner500d3292009-01-29 05:15:15 +000019#include "clang/Lex/LexDiagnostic.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020#include "clang/Basic/FileManager.h"
21#include "clang/Basic/SourceManager.h"
Douglas Gregor2e222532009-07-02 17:08:52 +000022#include <algorithm>
Reid Spencer5f016e22007-07-11 17:01:13 +000023using namespace clang;
24
25// Out-of-line destructor to provide a home for the class.
26PragmaHandler::~PragmaHandler() {
27}
28
29//===----------------------------------------------------------------------===//
Daniel Dunbarc72cc502010-06-11 20:10:12 +000030// EmptyPragmaHandler Implementation.
31//===----------------------------------------------------------------------===//
32
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +000033EmptyPragmaHandler::EmptyPragmaHandler() {}
Daniel Dunbarc72cc502010-06-11 20:10:12 +000034
35void EmptyPragmaHandler::HandlePragma(Preprocessor &PP, Token &FirstToken) {}
36
37//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +000038// PragmaNamespace Implementation.
39//===----------------------------------------------------------------------===//
40
41
42PragmaNamespace::~PragmaNamespace() {
Argyrios Kyrtzidis9b36c3f2010-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;
Reid Spencer5f016e22007-07-11 17:01:13 +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 Kyrtzidis9b36c3f2010-07-13 09:07:17 +000052PragmaHandler *PragmaNamespace::FindHandler(llvm::StringRef Name,
Reid Spencer5f016e22007-07-11 17:01:13 +000053 bool IgnoreNull) const {
Argyrios Kyrtzidis9b36c3f2010-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 Stump1eb44332009-09-09 15:08:12 +000058
Argyrios Kyrtzidis9b36c3f2010-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);
Reid Spencer5f016e22007-07-11 17:01:13 +000065}
66
Daniel Dunbar40950802008-10-04 19:17:46 +000067void PragmaNamespace::RemovePragmaHandler(PragmaHandler *Handler) {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +000068 assert(Handlers.lookup(Handler->getName()) &&
69 "Handler not registered in this namespace");
70 Handlers.erase(Handler->getName());
Daniel Dunbar40950802008-10-04 19:17:46 +000071}
72
Chris Lattnerd2177732007-07-20 16:59:19 +000073void PragmaNamespace::HandlePragma(Preprocessor &PP, Token &Tok) {
Reid Spencer5f016e22007-07-11 17:01:13 +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 Stump1eb44332009-09-09 15:08:12 +000077
Reid Spencer5f016e22007-07-11 17:01:13 +000078 // Get the handler for this token. If there is no handler, ignore the pragma.
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +000079 PragmaHandler *Handler
80 = FindHandler(Tok.getIdentifierInfo() ? Tok.getIdentifierInfo()->getName()
81 : llvm::StringRef(),
82 /*IgnoreNull=*/false);
Chris Lattneraf7cdf42009-04-19 21:10:26 +000083 if (Handler == 0) {
84 PP.Diag(Tok, diag::warn_pragma_ignored);
85 return;
86 }
Mike Stump1eb44332009-09-09 15:08:12 +000087
Reid Spencer5f016e22007-07-11 17:01:13 +000088 // Otherwise, pass it down.
89 Handler->HandlePragma(PP, Tok);
90}
91
92//===----------------------------------------------------------------------===//
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 Stump1eb44332009-09-09 15:08:12 +0000100
Reid Spencer5f016e22007-07-11 17:01:13 +0000101 // Invoke the first level of pragma handlers which reads the namespace id.
Chris Lattnerd2177732007-07-20 16:59:19 +0000102 Token Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000103 PragmaHandlers->HandlePragma(*this, Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000104
Reid Spencer5f016e22007-07-11 17:01:13 +0000105 // If the pragma handler didn't read the rest of the line, consume it now.
Chris Lattner027cff62009-06-18 05:55:53 +0000106 if (CurPPLexer && CurPPLexer->ParsingPreprocessorDirective)
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattnerd2177732007-07-20 16:59:19 +0000113void Preprocessor::Handle_Pragma(Token &Tok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000114 // Remember the pragma token location.
115 SourceLocation PragmaLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000116
Reid Spencer5f016e22007-07-11 17:01:13 +0000117 // Read the '('.
118 Lex(Tok);
Chris Lattner3692b092008-11-18 07:59:24 +0000119 if (Tok.isNot(tok::l_paren)) {
120 Diag(PragmaLoc, diag::err__Pragma_malformed);
121 return;
122 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000123
124 // Read the '"..."'.
125 Lex(Tok);
Chris Lattner3692b092008-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 Stump1eb44332009-09-09 15:08:12 +0000130
Reid Spencer5f016e22007-07-11 17:01:13 +0000131 // Remember the string.
132 std::string StrVal = getSpelling(Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000133
134 // Read the ')'.
135 Lex(Tok);
Chris Lattner3692b092008-11-18 07:59:24 +0000136 if (Tok.isNot(tok::r_paren)) {
137 Diag(PragmaLoc, diag::err__Pragma_malformed);
138 return;
139 }
Mike Stump1eb44332009-09-09 15:08:12 +0000140
Chris Lattnere7fb4842009-02-15 20:52:18 +0000141 SourceLocation RParenLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000142
Chris Lattnera9d91452009-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."
Reid Spencer5f016e22007-07-11 17:01:13 +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 Stump1eb44332009-09-09 15:08:12 +0000152
Reid Spencer5f016e22007-07-11 17:01:13 +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 Stump1eb44332009-09-09 15:08:12 +0000156
Chris Lattner1fa49532009-03-08 08:08:45 +0000157 // Replace the terminating quote with a \n.
Reid Spencer5f016e22007-07-11 17:01:13 +0000158 StrVal[StrVal.size()-1] = '\n';
Mike Stump1eb44332009-09-09 15:08:12 +0000159
Reid Spencer5f016e22007-07-11 17:01:13 +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 Stump1eb44332009-09-09 15:08:12 +0000169
Reid Spencer5f016e22007-07-11 17:01:13 +0000170 // Plop the string (including the newline and trailing null) into a buffer
171 // where we can lex it.
Chris Lattner47246be2009-01-26 19:29:26 +0000172 Token TmpTok;
173 TmpTok.startToken();
174 CreateString(&StrVal[0], StrVal.size(), TmpTok);
175 SourceLocation TokLoc = TmpTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000176
Reid Spencer5f016e22007-07-11 17:01:13 +0000177 // Make and enter a lexer object so that we lex and expand the tokens just
178 // like any others.
Chris Lattnere7fb4842009-02-15 20:52:18 +0000179 Lexer *TL = Lexer::Create_PragmaLexer(TokLoc, PragmaLoc, RParenLoc,
Chris Lattner1fa49532009-03-08 08:08:45 +0000180 StrVal.size(), *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000181
182 EnterSourceFileWithLexer(TL, 0);
183
184 // With everything set up, lex this as a #pragma directive.
185 HandlePragmaDirective();
Mike Stump1eb44332009-09-09 15:08:12 +0000186
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattnerd2177732007-07-20 16:59:19 +0000195void Preprocessor::HandlePragmaOnce(Token &OnceTok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000196 if (isInPrimaryFile()) {
197 Diag(OnceTok, diag::pp_pragma_once_in_main_file);
198 return;
199 }
Mike Stump1eb44332009-09-09 15:08:12 +0000200
Reid Spencer5f016e22007-07-11 17:01:13 +0000201 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
Reid Spencer5f016e22007-07-11 17:01:13 +0000202 // Mark the file as a once-only file now.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000203 HeaderInfo.MarkFileIncludeOnce(getCurrentFileLexer()->getFileEntry());
Reid Spencer5f016e22007-07-11 17:01:13 +0000204}
205
Chris Lattner22434492007-12-19 19:38:36 +0000206void Preprocessor::HandlePragmaMark() {
Ted Kremenek17ff58a2008-11-19 22:21:33 +0000207 assert(CurPPLexer && "No current lexer?");
Chris Lattner6896a372009-06-15 05:02:34 +0000208 if (CurLexer)
209 CurLexer->ReadToEndOfLine();
210 else
211 CurPTHLexer->DiscardToEndOfLine();
Chris Lattner22434492007-12-19 19:38:36 +0000212}
213
214
Reid Spencer5f016e22007-07-11 17:01:13 +0000215/// HandlePragmaPoison - Handle #pragma GCC poison. PoisonTok is the 'poison'.
216///
Chris Lattnerd2177732007-07-20 16:59:19 +0000217void Preprocessor::HandlePragmaPoison(Token &PoisonTok) {
218 Token Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000219
220 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 Kremenek68a91d52008-11-18 01:12:54 +0000226 if (CurPPLexer) CurPPLexer->LexingRawMode = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000227 LexUnexpandedToken(Tok);
Ted Kremenek68a91d52008-11-18 01:12:54 +0000228 if (CurPPLexer) CurPPLexer->LexingRawMode = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000229
Reid Spencer5f016e22007-07-11 17:01:13 +0000230 // If we reached the end of line, we're done.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000231 if (Tok.is(tok::eom)) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000232
Reid Spencer5f016e22007-07-11 17:01:13 +0000233 // Can only poison identifiers.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000234 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000235 Diag(Tok, diag::err_pp_invalid_poison);
236 return;
237 }
Mike Stump1eb44332009-09-09 15:08:12 +0000238
Reid Spencer5f016e22007-07-11 17:01:13 +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 Stump1eb44332009-09-09 15:08:12 +0000242
Reid Spencer5f016e22007-07-11 17:01:13 +0000243 // Already poisoned.
244 if (II->isPoisoned()) continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000245
Reid Spencer5f016e22007-07-11 17:01:13 +0000246 // If this is a macro identifier, emit a warning.
Chris Lattner0edde552007-10-07 08:04:56 +0000247 if (II->hasMacroDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000248 Diag(Tok, diag::pp_poisoning_existing_macro);
Mike Stump1eb44332009-09-09 15:08:12 +0000249
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattnerd2177732007-07-20 16:59:19 +0000257void Preprocessor::HandlePragmaSystemHeader(Token &SysHeaderTok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000258 if (isInPrimaryFile()) {
259 Diag(SysHeaderTok, diag::pp_pragma_sysheader_in_main_file);
260 return;
261 }
Mike Stump1eb44332009-09-09 15:08:12 +0000262
Reid Spencer5f016e22007-07-11 17:01:13 +0000263 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
Ted Kremenek35c10c22008-11-20 01:45:11 +0000264 PreprocessorLexer *TheLexer = getCurrentFileLexer();
Mike Stump1eb44332009-09-09 15:08:12 +0000265
Reid Spencer5f016e22007-07-11 17:01:13 +0000266 // Mark the file as a system header.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000267 HeaderInfo.MarkFileSystemHeader(TheLexer->getFileEntry());
Mike Stump1eb44332009-09-09 15:08:12 +0000268
269
Chris Lattner6896a372009-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 Stump1eb44332009-09-09 15:08:12 +0000274
Chris Lattner6896a372009-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 Stump1eb44332009-09-09 15:08:12 +0000280
Reid Spencer5f016e22007-07-11 17:01:13 +0000281 // Notify the client, if desired, that we are in a new source file.
282 if (Callbacks)
Ted Kremenek35c10c22008-11-20 01:45:11 +0000283 Callbacks->FileChanged(SysHeaderTok.getLocation(),
Chris Lattner0b9e7362008-09-26 21:18:42 +0000284 PPCallbacks::SystemHeaderPragma, SrcMgr::C_System);
Reid Spencer5f016e22007-07-11 17:01:13 +0000285}
286
287/// HandlePragmaDependency - Handle #pragma GCC dependency "foo" blah.
288///
Chris Lattnerd2177732007-07-20 16:59:19 +0000289void Preprocessor::HandlePragmaDependency(Token &DependencyTok) {
290 Token FilenameTok;
Ted Kremenek68a91d52008-11-18 01:12:54 +0000291 CurPPLexer->LexIncludeFilename(FilenameTok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000292
293 // If the token kind is EOM, the error has already been diagnosed.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000294 if (FilenameTok.is(tok::eom))
Reid Spencer5f016e22007-07-11 17:01:13 +0000295 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000296
Reid Spencer5f016e22007-07-11 17:01:13 +0000297 // Reserve a buffer to get the spelling.
Chris Lattnera1394812010-01-10 01:35:12 +0000298 llvm::SmallString<128> FilenameBuffer;
Douglas Gregor453091c2010-03-16 22:30:13 +0000299 bool Invalid = false;
300 llvm::StringRef Filename = getSpelling(FilenameTok, FilenameBuffer, &Invalid);
301 if (Invalid)
302 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000303
Chris Lattnera1394812010-01-10 01:35:12 +0000304 bool isAngled =
305 GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
Reid Spencer5f016e22007-07-11 17:01:13 +0000306 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
307 // error.
Chris Lattnera1394812010-01-10 01:35:12 +0000308 if (Filename.empty())
Reid Spencer5f016e22007-07-11 17:01:13 +0000309 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000310
Reid Spencer5f016e22007-07-11 17:01:13 +0000311 // Search include directories for this file.
312 const DirectoryLookup *CurDir;
Chris Lattnerf45b6462010-01-22 00:14:44 +0000313 const FileEntry *File = LookupFile(Filename, isAngled, 0, CurDir);
Chris Lattner56b05c82008-11-18 08:02:48 +0000314 if (File == 0) {
Chris Lattnera1394812010-01-10 01:35:12 +0000315 Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
Chris Lattner56b05c82008-11-18 08:02:48 +0000316 return;
317 }
Mike Stump1eb44332009-09-09 15:08:12 +0000318
Chris Lattner2b2453a2009-01-17 06:22:33 +0000319 const FileEntry *CurFile = getCurrentFileLexer()->getFileEntry();
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner22f6bbc2007-10-09 18:02:16 +0000326 while (DependencyTok.isNot(tok::eom)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000327 Message += getSpelling(DependencyTok) + " ";
328 Lex(DependencyTok);
329 }
Mike Stump1eb44332009-09-09 15:08:12 +0000330
Reid Spencer5f016e22007-07-11 17:01:13 +0000331 Message.erase(Message.end()-1);
Chris Lattner56b05c82008-11-18 08:02:48 +0000332 Diag(FilenameTok, diag::pp_out_of_date_dependency) << Message;
Reid Spencer5f016e22007-07-11 17:01:13 +0000333 }
334}
335
Chris Lattner636c5ef2009-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 Greifd7ee3492009-03-17 11:39:38 +0000341/// concatenation, embedded escape characters etc. See MSDN for more details.
Chris Lattner636c5ef2009-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 Stump1eb44332009-09-09 15:08:12 +0000349
Chris Lattner636c5ef2009-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 Stump1eb44332009-09-09 15:08:12 +0000356
Chris Lattner636c5ef2009-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 Stump1eb44332009-09-09 15:08:12 +0000360 if (!II->isStr("compiler") && !II->isStr("exestr") && !II->isStr("lib") &&
Chris Lattner636c5ef2009-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 Stump1eb44332009-09-09 15:08:12 +0000365
Chris Lattnera9d91452009-01-16 18:59:23 +0000366 // Read the optional string if present.
Chris Lattner636c5ef2009-01-16 08:21:25 +0000367 Lex(Tok);
Chris Lattnera9d91452009-01-16 18:59:23 +0000368 std::string ArgumentString;
Chris Lattner636c5ef2009-01-16 08:21:25 +0000369 if (Tok.is(tok::comma)) {
Chris Lattnera9d91452009-01-16 18:59:23 +0000370 Lex(Tok); // eat the comma.
Chris Lattner636c5ef2009-01-16 08:21:25 +0000371
372 // We need at least one string.
Chris Lattneredaf8772009-04-19 23:16:58 +0000373 if (Tok.isNot(tok::string_literal)) {
Chris Lattner636c5ef2009-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 Lattnera9d91452009-01-16 18:59:23 +0000381 llvm::SmallVector<Token, 4> StrToks;
Chris Lattneredaf8772009-04-19 23:16:58 +0000382 while (Tok.is(tok::string_literal)) {
Chris Lattnera9d91452009-01-16 18:59:23 +0000383 StrToks.push_back(Tok);
Chris Lattner636c5ef2009-01-16 08:21:25 +0000384 Lex(Tok);
Chris Lattnera9d91452009-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 Lattner636c5ef2009-01-16 08:21:25 +0000399 }
Mike Stump1eb44332009-09-09 15:08:12 +0000400
Chris Lattnera9d91452009-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 Stump1eb44332009-09-09 15:08:12 +0000406
Chris Lattner636c5ef2009-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 Lattnera9d91452009-01-16 18:59:23 +0000411 Lex(Tok); // eat the r_paren.
Chris Lattner636c5ef2009-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 Stump1eb44332009-09-09 15:08:12 +0000417
Chris Lattnera9d91452009-01-16 18:59:23 +0000418 // If the pragma is lexically sound, notify any interested PPCallbacks.
Chris Lattner172e3362009-01-16 19:01:46 +0000419 if (Callbacks)
420 Callbacks->PragmaComment(CommentLoc, II, ArgumentString);
Chris Lattner636c5ef2009-01-16 08:21:25 +0000421}
422
Chris Lattnerabfe0942010-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 Lattner636c5ef2009-01-16 08:21:25 +0000435
Chris Lattnerabfe0942010-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 Lattner636c5ef2009-01-16 08:21:25 +0000485
Reid Spencer5f016e22007-07-11 17:01:13 +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 Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000490void Preprocessor::AddPragmaHandler(llvm::StringRef Namespace,
Reid Spencer5f016e22007-07-11 17:01:13 +0000491 PragmaHandler *Handler) {
492 PragmaNamespace *InsertNS = PragmaHandlers;
Mike Stump1eb44332009-09-09 15:08:12 +0000493
Reid Spencer5f016e22007-07-11 17:01:13 +0000494 // If this is specified to be in a namespace, step down into it.
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000495 if (!Namespace.empty()) {
Reid Spencer5f016e22007-07-11 17:01:13 +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 Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000499 if (PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +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 Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000506 InsertNS = new PragmaNamespace(Namespace);
Reid Spencer5f016e22007-07-11 17:01:13 +0000507 PragmaHandlers->AddPragma(InsertNS);
508 }
509 }
Mike Stump1eb44332009-09-09 15:08:12 +0000510
Reid Spencer5f016e22007-07-11 17:01:13 +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 Dunbar40950802008-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 Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000521void Preprocessor::RemovePragmaHandler(llvm::StringRef Namespace,
Daniel Dunbar40950802008-10-04 19:17:46 +0000522 PragmaHandler *Handler) {
523 PragmaNamespace *NS = PragmaHandlers;
Mike Stump1eb44332009-09-09 15:08:12 +0000524
Daniel Dunbar40950802008-10-04 19:17:46 +0000525 // If this is specified to be in a namespace, step down into it.
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000526 if (!Namespace.empty()) {
527 PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace);
Daniel Dunbar40950802008-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 Stump1eb44332009-09-09 15:08:12 +0000535
Daniel Dunbar40950802008-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
Reid Spencer5f016e22007-07-11 17:01:13 +0000542namespace {
Chris Lattner22434492007-12-19 19:38:36 +0000543/// PragmaOnceHandler - "#pragma once" marks the file as atomically included.
Reid Spencer5f016e22007-07-11 17:01:13 +0000544struct PragmaOnceHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000545 PragmaOnceHandler() : PragmaHandler("once") {}
Chris Lattnerd2177732007-07-20 16:59:19 +0000546 virtual void HandlePragma(Preprocessor &PP, Token &OnceTok) {
Chris Lattner35410d52009-04-14 05:07:49 +0000547 PP.CheckEndOfDirective("pragma once");
Reid Spencer5f016e22007-07-11 17:01:13 +0000548 PP.HandlePragmaOnce(OnceTok);
549 }
550};
551
Chris Lattner22434492007-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 Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000555 PragmaMarkHandler() : PragmaHandler("mark") {}
Chris Lattner22434492007-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.
Reid Spencer5f016e22007-07-11 17:01:13 +0000562struct PragmaPoisonHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000563 PragmaPoisonHandler() : PragmaHandler("poison") {}
Chris Lattnerd2177732007-07-20 16:59:19 +0000564 virtual void HandlePragma(Preprocessor &PP, Token &PoisonTok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000565 PP.HandlePragmaPoison(PoisonTok);
566 }
567};
568
Chris Lattner22434492007-12-19 19:38:36 +0000569/// PragmaSystemHeaderHandler - "#pragma system_header" marks the current file
570/// as a system header, which silences warnings in it.
Reid Spencer5f016e22007-07-11 17:01:13 +0000571struct PragmaSystemHeaderHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000572 PragmaSystemHeaderHandler() : PragmaHandler("system_header") {}
Chris Lattnerd2177732007-07-20 16:59:19 +0000573 virtual void HandlePragma(Preprocessor &PP, Token &SHToken) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000574 PP.HandlePragmaSystemHeader(SHToken);
Chris Lattner35410d52009-04-14 05:07:49 +0000575 PP.CheckEndOfDirective("pragma");
Reid Spencer5f016e22007-07-11 17:01:13 +0000576 }
577};
578struct PragmaDependencyHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000579 PragmaDependencyHandler() : PragmaHandler("dependency") {}
Chris Lattnerd2177732007-07-20 16:59:19 +0000580 virtual void HandlePragma(Preprocessor &PP, Token &DepToken) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000581 PP.HandlePragmaDependency(DepToken);
582 }
583};
Mike Stump1eb44332009-09-09 15:08:12 +0000584
Chris Lattneredaf8772009-04-19 23:16:58 +0000585/// PragmaDiagnosticHandler - e.g. '#pragma GCC diagnostic ignored "-Wformat"'
Chris Lattner04ae2df2009-07-12 21:18:45 +0000586/// Since clang's diagnostic supports extended functionality beyond GCC's
587/// the constructor takes a clangMode flag to tell it whether or not to allow
588/// clang's extended functionality, or whether to reject it.
Chris Lattneredaf8772009-04-19 23:16:58 +0000589struct PragmaDiagnosticHandler : public PragmaHandler {
Chris Lattner04ae2df2009-07-12 21:18:45 +0000590private:
591 const bool ClangMode;
592public:
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000593 explicit PragmaDiagnosticHandler(const bool clangMode)
594 : PragmaHandler("diagnostic"), ClangMode(clangMode) {}
595
Chris Lattneredaf8772009-04-19 23:16:58 +0000596 virtual void HandlePragma(Preprocessor &PP, Token &DiagToken) {
597 Token Tok;
598 PP.LexUnexpandedToken(Tok);
599 if (Tok.isNot(tok::identifier)) {
Chris Lattner04ae2df2009-07-12 21:18:45 +0000600 unsigned Diag = ClangMode ? diag::warn_pragma_diagnostic_clang_invalid
601 : diag::warn_pragma_diagnostic_gcc_invalid;
602 PP.Diag(Tok, Diag);
Chris Lattneredaf8772009-04-19 23:16:58 +0000603 return;
604 }
605 IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000606
Chris Lattneredaf8772009-04-19 23:16:58 +0000607 diag::Mapping Map;
608 if (II->isStr("warning"))
609 Map = diag::MAP_WARNING;
610 else if (II->isStr("error"))
611 Map = diag::MAP_ERROR;
612 else if (II->isStr("ignored"))
613 Map = diag::MAP_IGNORE;
614 else if (II->isStr("fatal"))
615 Map = diag::MAP_FATAL;
Chris Lattner04ae2df2009-07-12 21:18:45 +0000616 else if (ClangMode) {
617 if (II->isStr("pop")) {
Mike Stump1eb44332009-09-09 15:08:12 +0000618 if (!PP.getDiagnostics().popMappings())
Chris Lattner04ae2df2009-07-12 21:18:45 +0000619 PP.Diag(Tok, diag::warn_pragma_diagnostic_clang_cannot_ppp);
620 return;
621 }
622
623 if (II->isStr("push")) {
624 PP.getDiagnostics().pushMappings();
Mike Stump1eb44332009-09-09 15:08:12 +0000625 return;
Chris Lattner04ae2df2009-07-12 21:18:45 +0000626 }
627
628 PP.Diag(Tok, diag::warn_pragma_diagnostic_clang_invalid);
629 return;
630 } else {
631 PP.Diag(Tok, diag::warn_pragma_diagnostic_gcc_invalid);
Chris Lattneredaf8772009-04-19 23:16:58 +0000632 return;
633 }
Mike Stump1eb44332009-09-09 15:08:12 +0000634
Chris Lattneredaf8772009-04-19 23:16:58 +0000635 PP.LexUnexpandedToken(Tok);
636
637 // We need at least one string.
638 if (Tok.isNot(tok::string_literal)) {
639 PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token);
640 return;
641 }
Mike Stump1eb44332009-09-09 15:08:12 +0000642
Chris Lattneredaf8772009-04-19 23:16:58 +0000643 // String concatenation allows multiple strings, which can even come from
644 // macro expansion.
645 // "foo " "bar" "Baz"
646 llvm::SmallVector<Token, 4> StrToks;
647 while (Tok.is(tok::string_literal)) {
648 StrToks.push_back(Tok);
649 PP.LexUnexpandedToken(Tok);
650 }
Mike Stump1eb44332009-09-09 15:08:12 +0000651
Chris Lattneredaf8772009-04-19 23:16:58 +0000652 if (Tok.isNot(tok::eom)) {
653 PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token);
654 return;
655 }
Mike Stump1eb44332009-09-09 15:08:12 +0000656
Chris Lattneredaf8772009-04-19 23:16:58 +0000657 // Concatenate and parse the strings.
658 StringLiteralParser Literal(&StrToks[0], StrToks.size(), PP);
659 assert(!Literal.AnyWide && "Didn't allow wide strings in");
660 if (Literal.hadError)
661 return;
662 if (Literal.Pascal) {
Chris Lattner04ae2df2009-07-12 21:18:45 +0000663 unsigned Diag = ClangMode ? diag::warn_pragma_diagnostic_clang_invalid
664 : diag::warn_pragma_diagnostic_gcc_invalid;
665 PP.Diag(Tok, Diag);
Chris Lattneredaf8772009-04-19 23:16:58 +0000666 return;
667 }
Chris Lattner04ae2df2009-07-12 21:18:45 +0000668
Chris Lattneredaf8772009-04-19 23:16:58 +0000669 std::string WarningName(Literal.GetString(),
670 Literal.GetString()+Literal.GetStringLength());
671
672 if (WarningName.size() < 3 || WarningName[0] != '-' ||
673 WarningName[1] != 'W') {
674 PP.Diag(StrToks[0].getLocation(),
675 diag::warn_pragma_diagnostic_invalid_option);
676 return;
677 }
Mike Stump1eb44332009-09-09 15:08:12 +0000678
Chris Lattneredaf8772009-04-19 23:16:58 +0000679 if (PP.getDiagnostics().setDiagnosticGroupMapping(WarningName.c_str()+2,
680 Map))
681 PP.Diag(StrToks[0].getLocation(),
682 diag::warn_pragma_diagnostic_unknown_warning) << WarningName;
683 }
684};
Mike Stump1eb44332009-09-09 15:08:12 +0000685
Chris Lattner636c5ef2009-01-16 08:21:25 +0000686/// PragmaCommentHandler - "#pragma comment ...".
687struct PragmaCommentHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000688 PragmaCommentHandler() : PragmaHandler("comment") {}
Chris Lattner636c5ef2009-01-16 08:21:25 +0000689 virtual void HandlePragma(Preprocessor &PP, Token &CommentTok) {
690 PP.HandlePragmaComment(CommentTok);
691 }
692};
Mike Stump1eb44332009-09-09 15:08:12 +0000693
Chris Lattnerabfe0942010-06-26 17:11:39 +0000694/// PragmaMessageHandler - "#pragma message("...")".
695struct PragmaMessageHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000696 PragmaMessageHandler() : PragmaHandler("message") {}
Chris Lattnerabfe0942010-06-26 17:11:39 +0000697 virtual void HandlePragma(Preprocessor &PP, Token &CommentTok) {
698 PP.HandlePragmaMessage(CommentTok);
699 }
700};
701
Chris Lattner062f2322009-04-19 21:20:35 +0000702// Pragma STDC implementations.
Chris Lattner6c5cf4a2009-04-19 21:50:08 +0000703
704enum STDCSetting {
705 STDC_ON, STDC_OFF, STDC_DEFAULT, STDC_INVALID
706};
Mike Stump1eb44332009-09-09 15:08:12 +0000707
Chris Lattner6c5cf4a2009-04-19 21:50:08 +0000708static STDCSetting LexOnOffSwitch(Preprocessor &PP) {
709 Token Tok;
710 PP.LexUnexpandedToken(Tok);
711
712 if (Tok.isNot(tok::identifier)) {
713 PP.Diag(Tok, diag::ext_stdc_pragma_syntax);
714 return STDC_INVALID;
715 }
716 IdentifierInfo *II = Tok.getIdentifierInfo();
717 STDCSetting Result;
718 if (II->isStr("ON"))
719 Result = STDC_ON;
720 else if (II->isStr("OFF"))
721 Result = STDC_OFF;
722 else if (II->isStr("DEFAULT"))
723 Result = STDC_DEFAULT;
724 else {
725 PP.Diag(Tok, diag::ext_stdc_pragma_syntax);
726 return STDC_INVALID;
727 }
728
729 // Verify that this is followed by EOM.
730 PP.LexUnexpandedToken(Tok);
731 if (Tok.isNot(tok::eom))
732 PP.Diag(Tok, diag::ext_stdc_pragma_syntax_eom);
733 return Result;
734}
Mike Stump1eb44332009-09-09 15:08:12 +0000735
Chris Lattner062f2322009-04-19 21:20:35 +0000736/// PragmaSTDC_FP_CONTRACTHandler - "#pragma STDC FP_CONTRACT ...".
737struct PragmaSTDC_FP_CONTRACTHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000738 PragmaSTDC_FP_CONTRACTHandler() : PragmaHandler("FP_CONTRACT") {}
Chris Lattnerf545be52009-04-19 21:25:37 +0000739 virtual void HandlePragma(Preprocessor &PP, Token &Tok) {
Chris Lattner6c5cf4a2009-04-19 21:50:08 +0000740 // We just ignore the setting of FP_CONTRACT. Since we don't do contractions
741 // at all, our default is OFF and setting it to ON is an optimization hint
742 // we can safely ignore. When we support -ffma or something, we would need
743 // to diagnose that we are ignoring FMA.
744 LexOnOffSwitch(PP);
Chris Lattner062f2322009-04-19 21:20:35 +0000745 }
746};
Mike Stump1eb44332009-09-09 15:08:12 +0000747
Chris Lattner062f2322009-04-19 21:20:35 +0000748/// PragmaSTDC_FENV_ACCESSHandler - "#pragma STDC FENV_ACCESS ...".
749struct PragmaSTDC_FENV_ACCESSHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000750 PragmaSTDC_FENV_ACCESSHandler() : PragmaHandler("FENV_ACCESS") {}
Chris Lattnerf545be52009-04-19 21:25:37 +0000751 virtual void HandlePragma(Preprocessor &PP, Token &Tok) {
Chris Lattner4d8aac32009-04-19 21:55:32 +0000752 if (LexOnOffSwitch(PP) == STDC_ON)
753 PP.Diag(Tok, diag::warn_stdc_fenv_access_not_supported);
Chris Lattner062f2322009-04-19 21:20:35 +0000754 }
755};
Mike Stump1eb44332009-09-09 15:08:12 +0000756
Chris Lattner062f2322009-04-19 21:20:35 +0000757/// PragmaSTDC_CX_LIMITED_RANGEHandler - "#pragma STDC CX_LIMITED_RANGE ...".
758struct PragmaSTDC_CX_LIMITED_RANGEHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000759 PragmaSTDC_CX_LIMITED_RANGEHandler()
760 : PragmaHandler("CX_LIMITED_RANGE") {}
Chris Lattnerf545be52009-04-19 21:25:37 +0000761 virtual void HandlePragma(Preprocessor &PP, Token &Tok) {
Chris Lattner6c5cf4a2009-04-19 21:50:08 +0000762 LexOnOffSwitch(PP);
Chris Lattner062f2322009-04-19 21:20:35 +0000763 }
764};
Mike Stump1eb44332009-09-09 15:08:12 +0000765
Chris Lattner062f2322009-04-19 21:20:35 +0000766/// PragmaSTDC_UnknownHandler - "#pragma STDC ...".
767struct PragmaSTDC_UnknownHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000768 PragmaSTDC_UnknownHandler() {}
Chris Lattnerf545be52009-04-19 21:25:37 +0000769 virtual void HandlePragma(Preprocessor &PP, Token &UnknownTok) {
Chris Lattner6c5cf4a2009-04-19 21:50:08 +0000770 // C99 6.10.6p2, unknown forms are not allowed.
Chris Lattnerf545be52009-04-19 21:25:37 +0000771 PP.Diag(UnknownTok, diag::ext_stdc_pragma_ignored);
Chris Lattner062f2322009-04-19 21:20:35 +0000772 }
773};
Mike Stump1eb44332009-09-09 15:08:12 +0000774
Reid Spencer5f016e22007-07-11 17:01:13 +0000775} // end anonymous namespace
776
777
778/// RegisterBuiltinPragmas - Install the standard preprocessor pragmas:
779/// #pragma GCC poison/system_header/dependency and #pragma once.
780void Preprocessor::RegisterBuiltinPragmas() {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000781 AddPragmaHandler(new PragmaOnceHandler());
782 AddPragmaHandler(new PragmaMarkHandler());
Mike Stump1eb44332009-09-09 15:08:12 +0000783
Chris Lattnere8fa06e2009-05-12 18:21:11 +0000784 // #pragma GCC ...
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000785 AddPragmaHandler("GCC", new PragmaPoisonHandler());
786 AddPragmaHandler("GCC", new PragmaSystemHeaderHandler());
787 AddPragmaHandler("GCC", new PragmaDependencyHandler());
788 AddPragmaHandler("GCC", new PragmaDiagnosticHandler(false));
Chris Lattnere8fa06e2009-05-12 18:21:11 +0000789 // #pragma clang ...
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000790 AddPragmaHandler("clang", new PragmaPoisonHandler());
791 AddPragmaHandler("clang", new PragmaSystemHeaderHandler());
792 AddPragmaHandler("clang", new PragmaDependencyHandler());
793 AddPragmaHandler("clang", new PragmaDiagnosticHandler(true));
Chris Lattnere8fa06e2009-05-12 18:21:11 +0000794
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000795 AddPragmaHandler("STDC", new PragmaSTDC_FP_CONTRACTHandler());
796 AddPragmaHandler("STDC", new PragmaSTDC_FENV_ACCESSHandler());
797 AddPragmaHandler("STDC", new PragmaSTDC_CX_LIMITED_RANGEHandler());
Chris Lattner062f2322009-04-19 21:20:35 +0000798 AddPragmaHandler("STDC", new PragmaSTDC_UnknownHandler());
Mike Stump1eb44332009-09-09 15:08:12 +0000799
Chris Lattner636c5ef2009-01-16 08:21:25 +0000800 // MS extensions.
Chris Lattnerabfe0942010-06-26 17:11:39 +0000801 if (Features.Microsoft) {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000802 AddPragmaHandler(new PragmaCommentHandler());
803 AddPragmaHandler(new PragmaMessageHandler());
Chris Lattnerabfe0942010-06-26 17:11:39 +0000804 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000805}