blob: bde3fbc0e87080856ffe211086cadff5e9f2f401 [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"
Reid Spencer5f016e22007-07-11 17:01:13 +000022using namespace clang;
23
24// Out-of-line destructor to provide a home for the class.
25PragmaHandler::~PragmaHandler() {
26}
27
28//===----------------------------------------------------------------------===//
29// PragmaNamespace Implementation.
30//===----------------------------------------------------------------------===//
31
32
33PragmaNamespace::~PragmaNamespace() {
34 for (unsigned i = 0, e = Handlers.size(); i != e; ++i)
35 delete Handlers[i];
36}
37
38/// FindHandler - Check to see if there is already a handler for the
39/// specified name. If not, return the handler for the null identifier if it
40/// exists, otherwise return null. If IgnoreNull is true (the default) then
41/// the null handler isn't returned on failure to match.
42PragmaHandler *PragmaNamespace::FindHandler(const IdentifierInfo *Name,
43 bool IgnoreNull) const {
44 PragmaHandler *NullHandler = 0;
45 for (unsigned i = 0, e = Handlers.size(); i != e; ++i) {
46 if (Handlers[i]->getName() == Name)
47 return Handlers[i];
48
49 if (Handlers[i]->getName() == 0)
50 NullHandler = Handlers[i];
51 }
52 return IgnoreNull ? 0 : NullHandler;
53}
54
Daniel Dunbar40950802008-10-04 19:17:46 +000055void PragmaNamespace::RemovePragmaHandler(PragmaHandler *Handler) {
56 for (unsigned i = 0, e = Handlers.size(); i != e; ++i) {
57 if (Handlers[i] == Handler) {
58 Handlers[i] = Handlers.back();
59 Handlers.pop_back();
60 return;
61 }
62 }
63 assert(0 && "Handler not registered in this namespace");
64}
65
Chris Lattnerd2177732007-07-20 16:59:19 +000066void PragmaNamespace::HandlePragma(Preprocessor &PP, Token &Tok) {
Reid Spencer5f016e22007-07-11 17:01:13 +000067 // Read the 'namespace' that the directive is in, e.g. STDC. Do not macro
68 // expand it, the user can have a STDC #define, that should not affect this.
69 PP.LexUnexpandedToken(Tok);
70
71 // Get the handler for this token. If there is no handler, ignore the pragma.
72 PragmaHandler *Handler = FindHandler(Tok.getIdentifierInfo(), false);
Chris Lattneraf7cdf42009-04-19 21:10:26 +000073 if (Handler == 0) {
74 PP.Diag(Tok, diag::warn_pragma_ignored);
75 return;
76 }
Reid Spencer5f016e22007-07-11 17:01:13 +000077
78 // Otherwise, pass it down.
79 Handler->HandlePragma(PP, Tok);
80}
81
82//===----------------------------------------------------------------------===//
83// Preprocessor Pragma Directive Handling.
84//===----------------------------------------------------------------------===//
85
86/// HandlePragmaDirective - The "#pragma" directive has been parsed. Lex the
87/// rest of the pragma, passing it to the registered pragma handlers.
88void Preprocessor::HandlePragmaDirective() {
89 ++NumPragma;
90
91 // Invoke the first level of pragma handlers which reads the namespace id.
Chris Lattnerd2177732007-07-20 16:59:19 +000092 Token Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +000093 PragmaHandlers->HandlePragma(*this, Tok);
94
95 // If the pragma handler didn't read the rest of the line, consume it now.
Ted Kremenek68a91d52008-11-18 01:12:54 +000096 if (CurPPLexer->ParsingPreprocessorDirective)
Reid Spencer5f016e22007-07-11 17:01:13 +000097 DiscardUntilEndOfDirective();
98}
99
100/// Handle_Pragma - Read a _Pragma directive, slice it up, process it, then
101/// return the first token after the directive. The _Pragma token has just
102/// been read into 'Tok'.
Chris Lattnerd2177732007-07-20 16:59:19 +0000103void Preprocessor::Handle_Pragma(Token &Tok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000104 // Remember the pragma token location.
105 SourceLocation PragmaLoc = Tok.getLocation();
106
107 // Read the '('.
108 Lex(Tok);
Chris Lattner3692b092008-11-18 07:59:24 +0000109 if (Tok.isNot(tok::l_paren)) {
110 Diag(PragmaLoc, diag::err__Pragma_malformed);
111 return;
112 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000113
114 // Read the '"..."'.
115 Lex(Tok);
Chris Lattner3692b092008-11-18 07:59:24 +0000116 if (Tok.isNot(tok::string_literal) && Tok.isNot(tok::wide_string_literal)) {
117 Diag(PragmaLoc, diag::err__Pragma_malformed);
118 return;
119 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000120
121 // Remember the string.
122 std::string StrVal = getSpelling(Tok);
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::r_paren)) {
127 Diag(PragmaLoc, diag::err__Pragma_malformed);
128 return;
129 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000130
Chris Lattnere7fb4842009-02-15 20:52:18 +0000131 SourceLocation RParenLoc = Tok.getLocation();
132
Chris Lattnera9d91452009-01-16 18:59:23 +0000133 // The _Pragma is lexically sound. Destringize according to C99 6.10.9.1:
134 // "The string literal is destringized by deleting the L prefix, if present,
135 // deleting the leading and trailing double-quotes, replacing each escape
136 // sequence \" by a double-quote, and replacing each escape sequence \\ by a
137 // single backslash."
Reid Spencer5f016e22007-07-11 17:01:13 +0000138 if (StrVal[0] == 'L') // Remove L prefix.
139 StrVal.erase(StrVal.begin());
140 assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
141 "Invalid string token!");
142
143 // Remove the front quote, replacing it with a space, so that the pragma
144 // contents appear to have a space before them.
145 StrVal[0] = ' ';
146
Chris Lattner1fa49532009-03-08 08:08:45 +0000147 // Replace the terminating quote with a \n.
Reid Spencer5f016e22007-07-11 17:01:13 +0000148 StrVal[StrVal.size()-1] = '\n';
Reid Spencer5f016e22007-07-11 17:01:13 +0000149
150 // Remove escaped quotes and escapes.
151 for (unsigned i = 0, e = StrVal.size(); i != e-1; ++i) {
152 if (StrVal[i] == '\\' &&
153 (StrVal[i+1] == '\\' || StrVal[i+1] == '"')) {
154 // \\ -> '\' and \" -> '"'.
155 StrVal.erase(StrVal.begin()+i);
156 --e;
157 }
158 }
159
160 // Plop the string (including the newline and trailing null) into a buffer
161 // where we can lex it.
Chris Lattner47246be2009-01-26 19:29:26 +0000162 Token TmpTok;
163 TmpTok.startToken();
164 CreateString(&StrVal[0], StrVal.size(), TmpTok);
165 SourceLocation TokLoc = TmpTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000166
Reid Spencer5f016e22007-07-11 17:01:13 +0000167 // Make and enter a lexer object so that we lex and expand the tokens just
168 // like any others.
Chris Lattnere7fb4842009-02-15 20:52:18 +0000169 Lexer *TL = Lexer::Create_PragmaLexer(TokLoc, PragmaLoc, RParenLoc,
Chris Lattner1fa49532009-03-08 08:08:45 +0000170 StrVal.size(), *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000171
172 EnterSourceFileWithLexer(TL, 0);
173
174 // With everything set up, lex this as a #pragma directive.
175 HandlePragmaDirective();
176
177 // Finally, return whatever came after the pragma directive.
178 return Lex(Tok);
179}
180
181
182
183/// HandlePragmaOnce - Handle #pragma once. OnceTok is the 'once'.
184///
Chris Lattnerd2177732007-07-20 16:59:19 +0000185void Preprocessor::HandlePragmaOnce(Token &OnceTok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000186 if (isInPrimaryFile()) {
187 Diag(OnceTok, diag::pp_pragma_once_in_main_file);
188 return;
189 }
190
191 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
Reid Spencer5f016e22007-07-11 17:01:13 +0000192 // Mark the file as a once-only file now.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000193 HeaderInfo.MarkFileIncludeOnce(getCurrentFileLexer()->getFileEntry());
Reid Spencer5f016e22007-07-11 17:01:13 +0000194}
195
Chris Lattner22434492007-12-19 19:38:36 +0000196void Preprocessor::HandlePragmaMark() {
Ted Kremenek17ff58a2008-11-19 22:21:33 +0000197 assert(CurPPLexer && "No current lexer?");
198 if (CurLexer) CurLexer->ReadToEndOfLine();
199 else CurPTHLexer->DiscardToEndOfLine();
Chris Lattner22434492007-12-19 19:38:36 +0000200}
201
202
Reid Spencer5f016e22007-07-11 17:01:13 +0000203/// HandlePragmaPoison - Handle #pragma GCC poison. PoisonTok is the 'poison'.
204///
Chris Lattnerd2177732007-07-20 16:59:19 +0000205void Preprocessor::HandlePragmaPoison(Token &PoisonTok) {
206 Token Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000207
208 while (1) {
209 // Read the next token to poison. While doing this, pretend that we are
210 // skipping while reading the identifier to poison.
211 // This avoids errors on code like:
212 // #pragma GCC poison X
213 // #pragma GCC poison X
Ted Kremenek68a91d52008-11-18 01:12:54 +0000214 if (CurPPLexer) CurPPLexer->LexingRawMode = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000215 LexUnexpandedToken(Tok);
Ted Kremenek68a91d52008-11-18 01:12:54 +0000216 if (CurPPLexer) CurPPLexer->LexingRawMode = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000217
218 // If we reached the end of line, we're done.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000219 if (Tok.is(tok::eom)) return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000220
221 // Can only poison identifiers.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000222 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000223 Diag(Tok, diag::err_pp_invalid_poison);
224 return;
225 }
226
227 // Look up the identifier info for the token. We disabled identifier lookup
228 // by saying we're skipping contents, so we need to do this manually.
229 IdentifierInfo *II = LookUpIdentifierInfo(Tok);
230
231 // Already poisoned.
232 if (II->isPoisoned()) continue;
233
234 // If this is a macro identifier, emit a warning.
Chris Lattner0edde552007-10-07 08:04:56 +0000235 if (II->hasMacroDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000236 Diag(Tok, diag::pp_poisoning_existing_macro);
237
238 // Finally, poison it!
239 II->setIsPoisoned();
240 }
241}
242
243/// HandlePragmaSystemHeader - Implement #pragma GCC system_header. We know
244/// that the whole directive has been parsed.
Chris Lattnerd2177732007-07-20 16:59:19 +0000245void Preprocessor::HandlePragmaSystemHeader(Token &SysHeaderTok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000246 if (isInPrimaryFile()) {
247 Diag(SysHeaderTok, diag::pp_pragma_sysheader_in_main_file);
248 return;
249 }
250
251 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
Ted Kremenek35c10c22008-11-20 01:45:11 +0000252 PreprocessorLexer *TheLexer = getCurrentFileLexer();
Reid Spencer5f016e22007-07-11 17:01:13 +0000253
254 // Mark the file as a system header.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000255 HeaderInfo.MarkFileSystemHeader(TheLexer->getFileEntry());
Reid Spencer5f016e22007-07-11 17:01:13 +0000256
257 // Notify the client, if desired, that we are in a new source file.
258 if (Callbacks)
Ted Kremenek35c10c22008-11-20 01:45:11 +0000259 Callbacks->FileChanged(SysHeaderTok.getLocation(),
Chris Lattner0b9e7362008-09-26 21:18:42 +0000260 PPCallbacks::SystemHeaderPragma, SrcMgr::C_System);
Reid Spencer5f016e22007-07-11 17:01:13 +0000261}
262
263/// HandlePragmaDependency - Handle #pragma GCC dependency "foo" blah.
264///
Chris Lattnerd2177732007-07-20 16:59:19 +0000265void Preprocessor::HandlePragmaDependency(Token &DependencyTok) {
266 Token FilenameTok;
Ted Kremenek68a91d52008-11-18 01:12:54 +0000267 CurPPLexer->LexIncludeFilename(FilenameTok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000268
269 // If the token kind is EOM, the error has already been diagnosed.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000270 if (FilenameTok.is(tok::eom))
Reid Spencer5f016e22007-07-11 17:01:13 +0000271 return;
272
273 // Reserve a buffer to get the spelling.
274 llvm::SmallVector<char, 128> FilenameBuffer;
275 FilenameBuffer.resize(FilenameTok.getLength());
276
Chris Lattnerf1c99ac2007-07-23 04:15:27 +0000277 const char *FilenameStart = &FilenameBuffer[0];
278 unsigned Len = getSpelling(FilenameTok, FilenameStart);
279 const char *FilenameEnd = FilenameStart+Len;
280 bool isAngled = GetIncludeFilenameSpelling(FilenameTok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000281 FilenameStart, FilenameEnd);
282 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
283 // error.
284 if (FilenameStart == 0)
285 return;
286
287 // Search include directories for this file.
288 const DirectoryLookup *CurDir;
289 const FileEntry *File = LookupFile(FilenameStart, FilenameEnd,
290 isAngled, 0, CurDir);
Chris Lattner56b05c82008-11-18 08:02:48 +0000291 if (File == 0) {
292 Diag(FilenameTok, diag::err_pp_file_not_found)
293 << std::string(FilenameStart, FilenameEnd);
294 return;
295 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000296
Chris Lattner2b2453a2009-01-17 06:22:33 +0000297 const FileEntry *CurFile = getCurrentFileLexer()->getFileEntry();
Reid Spencer5f016e22007-07-11 17:01:13 +0000298
299 // If this file is older than the file it depends on, emit a diagnostic.
300 if (CurFile && CurFile->getModificationTime() < File->getModificationTime()) {
301 // Lex tokens at the end of the message and include them in the message.
302 std::string Message;
303 Lex(DependencyTok);
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000304 while (DependencyTok.isNot(tok::eom)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000305 Message += getSpelling(DependencyTok) + " ";
306 Lex(DependencyTok);
307 }
308
309 Message.erase(Message.end()-1);
Chris Lattner56b05c82008-11-18 08:02:48 +0000310 Diag(FilenameTok, diag::pp_out_of_date_dependency) << Message;
Reid Spencer5f016e22007-07-11 17:01:13 +0000311 }
312}
313
Chris Lattner636c5ef2009-01-16 08:21:25 +0000314/// HandlePragmaComment - Handle the microsoft #pragma comment extension. The
315/// syntax is:
316/// #pragma comment(linker, "foo")
317/// 'linker' is one of five identifiers: compiler, exestr, lib, linker, user.
318/// "foo" is a string, which is fully macro expanded, and permits string
Gabor Greifd7ee3492009-03-17 11:39:38 +0000319/// concatenation, embedded escape characters etc. See MSDN for more details.
Chris Lattner636c5ef2009-01-16 08:21:25 +0000320void Preprocessor::HandlePragmaComment(Token &Tok) {
321 SourceLocation CommentLoc = Tok.getLocation();
322 Lex(Tok);
323 if (Tok.isNot(tok::l_paren)) {
324 Diag(CommentLoc, diag::err_pragma_comment_malformed);
325 return;
326 }
327
328 // Read the identifier.
329 Lex(Tok);
330 if (Tok.isNot(tok::identifier)) {
331 Diag(CommentLoc, diag::err_pragma_comment_malformed);
332 return;
333 }
334
335 // Verify that this is one of the 5 whitelisted options.
336 // FIXME: warn that 'exestr' is deprecated.
337 const IdentifierInfo *II = Tok.getIdentifierInfo();
338 if (!II->isStr("compiler") && !II->isStr("exestr") && !II->isStr("lib") &&
339 !II->isStr("linker") && !II->isStr("user")) {
340 Diag(Tok.getLocation(), diag::err_pragma_comment_unknown_kind);
341 return;
342 }
343
Chris Lattnera9d91452009-01-16 18:59:23 +0000344 // Read the optional string if present.
Chris Lattner636c5ef2009-01-16 08:21:25 +0000345 Lex(Tok);
Chris Lattnera9d91452009-01-16 18:59:23 +0000346 std::string ArgumentString;
Chris Lattner636c5ef2009-01-16 08:21:25 +0000347 if (Tok.is(tok::comma)) {
Chris Lattnera9d91452009-01-16 18:59:23 +0000348 Lex(Tok); // eat the comma.
Chris Lattner636c5ef2009-01-16 08:21:25 +0000349
350 // We need at least one string.
Chris Lattneredaf8772009-04-19 23:16:58 +0000351 if (Tok.isNot(tok::string_literal)) {
Chris Lattner636c5ef2009-01-16 08:21:25 +0000352 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
353 return;
354 }
355
356 // String concatenation allows multiple strings, which can even come from
357 // macro expansion.
358 // "foo " "bar" "Baz"
Chris Lattnera9d91452009-01-16 18:59:23 +0000359 llvm::SmallVector<Token, 4> StrToks;
Chris Lattneredaf8772009-04-19 23:16:58 +0000360 while (Tok.is(tok::string_literal)) {
Chris Lattnera9d91452009-01-16 18:59:23 +0000361 StrToks.push_back(Tok);
Chris Lattner636c5ef2009-01-16 08:21:25 +0000362 Lex(Tok);
Chris Lattnera9d91452009-01-16 18:59:23 +0000363 }
364
365 // Concatenate and parse the strings.
366 StringLiteralParser Literal(&StrToks[0], StrToks.size(), *this);
367 assert(!Literal.AnyWide && "Didn't allow wide strings in");
368 if (Literal.hadError)
369 return;
370 if (Literal.Pascal) {
371 Diag(StrToks[0].getLocation(), diag::err_pragma_comment_malformed);
372 return;
373 }
374
375 ArgumentString = std::string(Literal.GetString(),
376 Literal.GetString()+Literal.GetStringLength());
Chris Lattner636c5ef2009-01-16 08:21:25 +0000377 }
378
Chris Lattnera9d91452009-01-16 18:59:23 +0000379 // FIXME: If the kind is "compiler" warn if the string is present (it is
380 // ignored).
381 // FIXME: 'lib' requires a comment string.
382 // FIXME: 'linker' requires a comment string, and has a specific list of
383 // things that are allowable.
384
Chris Lattner636c5ef2009-01-16 08:21:25 +0000385 if (Tok.isNot(tok::r_paren)) {
386 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
387 return;
388 }
Chris Lattnera9d91452009-01-16 18:59:23 +0000389 Lex(Tok); // eat the r_paren.
Chris Lattner636c5ef2009-01-16 08:21:25 +0000390
391 if (Tok.isNot(tok::eom)) {
392 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
393 return;
394 }
Chris Lattnera9d91452009-01-16 18:59:23 +0000395
396 // If the pragma is lexically sound, notify any interested PPCallbacks.
Chris Lattner172e3362009-01-16 19:01:46 +0000397 if (Callbacks)
398 Callbacks->PragmaComment(CommentLoc, II, ArgumentString);
Chris Lattner636c5ef2009-01-16 08:21:25 +0000399}
400
401
402
Reid Spencer5f016e22007-07-11 17:01:13 +0000403
404/// AddPragmaHandler - Add the specified pragma handler to the preprocessor.
405/// If 'Namespace' is non-null, then it is a token required to exist on the
406/// pragma line before the pragma string starts, e.g. "STDC" or "GCC".
407void Preprocessor::AddPragmaHandler(const char *Namespace,
408 PragmaHandler *Handler) {
409 PragmaNamespace *InsertNS = PragmaHandlers;
410
411 // If this is specified to be in a namespace, step down into it.
412 if (Namespace) {
413 IdentifierInfo *NSID = getIdentifierInfo(Namespace);
414
415 // If there is already a pragma handler with the name of this namespace,
416 // we either have an error (directive with the same name as a namespace) or
417 // we already have the namespace to insert into.
418 if (PragmaHandler *Existing = PragmaHandlers->FindHandler(NSID)) {
419 InsertNS = Existing->getIfNamespace();
420 assert(InsertNS != 0 && "Cannot have a pragma namespace and pragma"
421 " handler with the same name!");
422 } else {
423 // Otherwise, this namespace doesn't exist yet, create and insert the
424 // handler for it.
425 InsertNS = new PragmaNamespace(NSID);
426 PragmaHandlers->AddPragma(InsertNS);
427 }
428 }
429
430 // Check to make sure we don't already have a pragma for this identifier.
431 assert(!InsertNS->FindHandler(Handler->getName()) &&
432 "Pragma handler already exists for this identifier!");
433 InsertNS->AddPragma(Handler);
434}
435
Daniel Dunbar40950802008-10-04 19:17:46 +0000436/// RemovePragmaHandler - Remove the specific pragma handler from the
437/// preprocessor. If \arg Namespace is non-null, then it should be the
438/// namespace that \arg Handler was added to. It is an error to remove
439/// a handler that has not been registered.
440void Preprocessor::RemovePragmaHandler(const char *Namespace,
441 PragmaHandler *Handler) {
442 PragmaNamespace *NS = PragmaHandlers;
443
444 // If this is specified to be in a namespace, step down into it.
445 if (Namespace) {
446 IdentifierInfo *NSID = getIdentifierInfo(Namespace);
447 PragmaHandler *Existing = PragmaHandlers->FindHandler(NSID);
448 assert(Existing && "Namespace containing handler does not exist!");
449
450 NS = Existing->getIfNamespace();
451 assert(NS && "Invalid namespace, registered as a regular pragma handler!");
452 }
453
454 NS->RemovePragmaHandler(Handler);
455
456 // If this is a non-default namespace and it is now empty, remove
457 // it.
458 if (NS != PragmaHandlers && NS->IsEmpty())
459 PragmaHandlers->RemovePragmaHandler(NS);
460}
461
Reid Spencer5f016e22007-07-11 17:01:13 +0000462namespace {
Chris Lattner22434492007-12-19 19:38:36 +0000463/// PragmaOnceHandler - "#pragma once" marks the file as atomically included.
Reid Spencer5f016e22007-07-11 17:01:13 +0000464struct PragmaOnceHandler : public PragmaHandler {
465 PragmaOnceHandler(const IdentifierInfo *OnceID) : PragmaHandler(OnceID) {}
Chris Lattnerd2177732007-07-20 16:59:19 +0000466 virtual void HandlePragma(Preprocessor &PP, Token &OnceTok) {
Chris Lattner35410d52009-04-14 05:07:49 +0000467 PP.CheckEndOfDirective("pragma once");
Reid Spencer5f016e22007-07-11 17:01:13 +0000468 PP.HandlePragmaOnce(OnceTok);
469 }
470};
471
Chris Lattner22434492007-12-19 19:38:36 +0000472/// PragmaMarkHandler - "#pragma mark ..." is ignored by the compiler, and the
473/// rest of the line is not lexed.
474struct PragmaMarkHandler : public PragmaHandler {
475 PragmaMarkHandler(const IdentifierInfo *MarkID) : PragmaHandler(MarkID) {}
476 virtual void HandlePragma(Preprocessor &PP, Token &MarkTok) {
477 PP.HandlePragmaMark();
478 }
479};
480
481/// PragmaPoisonHandler - "#pragma poison x" marks x as not usable.
Reid Spencer5f016e22007-07-11 17:01:13 +0000482struct PragmaPoisonHandler : public PragmaHandler {
483 PragmaPoisonHandler(const IdentifierInfo *ID) : PragmaHandler(ID) {}
Chris Lattnerd2177732007-07-20 16:59:19 +0000484 virtual void HandlePragma(Preprocessor &PP, Token &PoisonTok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000485 PP.HandlePragmaPoison(PoisonTok);
486 }
487};
488
Chris Lattner22434492007-12-19 19:38:36 +0000489/// PragmaSystemHeaderHandler - "#pragma system_header" marks the current file
490/// as a system header, which silences warnings in it.
Reid Spencer5f016e22007-07-11 17:01:13 +0000491struct PragmaSystemHeaderHandler : public PragmaHandler {
492 PragmaSystemHeaderHandler(const IdentifierInfo *ID) : PragmaHandler(ID) {}
Chris Lattnerd2177732007-07-20 16:59:19 +0000493 virtual void HandlePragma(Preprocessor &PP, Token &SHToken) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000494 PP.HandlePragmaSystemHeader(SHToken);
Chris Lattner35410d52009-04-14 05:07:49 +0000495 PP.CheckEndOfDirective("pragma");
Reid Spencer5f016e22007-07-11 17:01:13 +0000496 }
497};
498struct PragmaDependencyHandler : public PragmaHandler {
499 PragmaDependencyHandler(const IdentifierInfo *ID) : PragmaHandler(ID) {}
Chris Lattnerd2177732007-07-20 16:59:19 +0000500 virtual void HandlePragma(Preprocessor &PP, Token &DepToken) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000501 PP.HandlePragmaDependency(DepToken);
502 }
503};
Chris Lattner636c5ef2009-01-16 08:21:25 +0000504
Chris Lattneredaf8772009-04-19 23:16:58 +0000505/// PragmaDiagnosticHandler - e.g. '#pragma GCC diagnostic ignored "-Wformat"'
506struct PragmaDiagnosticHandler : public PragmaHandler {
507 PragmaDiagnosticHandler(const IdentifierInfo *ID) : PragmaHandler(ID) {}
508 virtual void HandlePragma(Preprocessor &PP, Token &DiagToken) {
509 Token Tok;
510 PP.LexUnexpandedToken(Tok);
511 if (Tok.isNot(tok::identifier)) {
512 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
513 return;
514 }
515 IdentifierInfo *II = Tok.getIdentifierInfo();
516
517 diag::Mapping Map;
518 if (II->isStr("warning"))
519 Map = diag::MAP_WARNING;
520 else if (II->isStr("error"))
521 Map = diag::MAP_ERROR;
522 else if (II->isStr("ignored"))
523 Map = diag::MAP_IGNORE;
524 else if (II->isStr("fatal"))
525 Map = diag::MAP_FATAL;
526 else {
527 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
528 return;
529 }
530
531 PP.LexUnexpandedToken(Tok);
532
533 // We need at least one string.
534 if (Tok.isNot(tok::string_literal)) {
535 PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token);
536 return;
537 }
538
539 // String concatenation allows multiple strings, which can even come from
540 // macro expansion.
541 // "foo " "bar" "Baz"
542 llvm::SmallVector<Token, 4> StrToks;
543 while (Tok.is(tok::string_literal)) {
544 StrToks.push_back(Tok);
545 PP.LexUnexpandedToken(Tok);
546 }
547
548 if (Tok.isNot(tok::eom)) {
549 PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token);
550 return;
551 }
552
553 // Concatenate and parse the strings.
554 StringLiteralParser Literal(&StrToks[0], StrToks.size(), PP);
555 assert(!Literal.AnyWide && "Didn't allow wide strings in");
556 if (Literal.hadError)
557 return;
558 if (Literal.Pascal) {
559 PP.Diag(StrToks[0].getLocation(), diag::warn_pragma_diagnostic_invalid);
560 return;
561 }
562
563 std::string WarningName(Literal.GetString(),
564 Literal.GetString()+Literal.GetStringLength());
565
566 if (WarningName.size() < 3 || WarningName[0] != '-' ||
567 WarningName[1] != 'W') {
568 PP.Diag(StrToks[0].getLocation(),
569 diag::warn_pragma_diagnostic_invalid_option);
570 return;
571 }
572
573 if (PP.getDiagnostics().setDiagnosticGroupMapping(WarningName.c_str()+2,
574 Map))
575 PP.Diag(StrToks[0].getLocation(),
576 diag::warn_pragma_diagnostic_unknown_warning) << WarningName;
577 }
578};
579
Chris Lattner636c5ef2009-01-16 08:21:25 +0000580/// PragmaCommentHandler - "#pragma comment ...".
581struct PragmaCommentHandler : public PragmaHandler {
582 PragmaCommentHandler(const IdentifierInfo *ID) : PragmaHandler(ID) {}
583 virtual void HandlePragma(Preprocessor &PP, Token &CommentTok) {
584 PP.HandlePragmaComment(CommentTok);
585 }
586};
Chris Lattner062f2322009-04-19 21:20:35 +0000587
588// Pragma STDC implementations.
Chris Lattner6c5cf4a2009-04-19 21:50:08 +0000589
590enum STDCSetting {
591 STDC_ON, STDC_OFF, STDC_DEFAULT, STDC_INVALID
592};
593
594static STDCSetting LexOnOffSwitch(Preprocessor &PP) {
595 Token Tok;
596 PP.LexUnexpandedToken(Tok);
597
598 if (Tok.isNot(tok::identifier)) {
599 PP.Diag(Tok, diag::ext_stdc_pragma_syntax);
600 return STDC_INVALID;
601 }
602 IdentifierInfo *II = Tok.getIdentifierInfo();
603 STDCSetting Result;
604 if (II->isStr("ON"))
605 Result = STDC_ON;
606 else if (II->isStr("OFF"))
607 Result = STDC_OFF;
608 else if (II->isStr("DEFAULT"))
609 Result = STDC_DEFAULT;
610 else {
611 PP.Diag(Tok, diag::ext_stdc_pragma_syntax);
612 return STDC_INVALID;
613 }
614
615 // Verify that this is followed by EOM.
616 PP.LexUnexpandedToken(Tok);
617 if (Tok.isNot(tok::eom))
618 PP.Diag(Tok, diag::ext_stdc_pragma_syntax_eom);
619 return Result;
620}
Chris Lattner062f2322009-04-19 21:20:35 +0000621
622/// PragmaSTDC_FP_CONTRACTHandler - "#pragma STDC FP_CONTRACT ...".
623struct PragmaSTDC_FP_CONTRACTHandler : public PragmaHandler {
624 PragmaSTDC_FP_CONTRACTHandler(const IdentifierInfo *ID) : PragmaHandler(ID) {}
Chris Lattnerf545be52009-04-19 21:25:37 +0000625 virtual void HandlePragma(Preprocessor &PP, Token &Tok) {
Chris Lattner6c5cf4a2009-04-19 21:50:08 +0000626 // We just ignore the setting of FP_CONTRACT. Since we don't do contractions
627 // at all, our default is OFF and setting it to ON is an optimization hint
628 // we can safely ignore. When we support -ffma or something, we would need
629 // to diagnose that we are ignoring FMA.
630 LexOnOffSwitch(PP);
Chris Lattner062f2322009-04-19 21:20:35 +0000631 }
632};
633
634/// PragmaSTDC_FENV_ACCESSHandler - "#pragma STDC FENV_ACCESS ...".
635struct PragmaSTDC_FENV_ACCESSHandler : public PragmaHandler {
636 PragmaSTDC_FENV_ACCESSHandler(const IdentifierInfo *ID) : PragmaHandler(ID) {}
Chris Lattnerf545be52009-04-19 21:25:37 +0000637 virtual void HandlePragma(Preprocessor &PP, Token &Tok) {
Chris Lattner4d8aac32009-04-19 21:55:32 +0000638 if (LexOnOffSwitch(PP) == STDC_ON)
639 PP.Diag(Tok, diag::warn_stdc_fenv_access_not_supported);
Chris Lattner062f2322009-04-19 21:20:35 +0000640 }
641};
642
643/// PragmaSTDC_CX_LIMITED_RANGEHandler - "#pragma STDC CX_LIMITED_RANGE ...".
644struct PragmaSTDC_CX_LIMITED_RANGEHandler : public PragmaHandler {
645 PragmaSTDC_CX_LIMITED_RANGEHandler(const IdentifierInfo *ID)
646 : PragmaHandler(ID) {}
Chris Lattnerf545be52009-04-19 21:25:37 +0000647 virtual void HandlePragma(Preprocessor &PP, Token &Tok) {
Chris Lattner6c5cf4a2009-04-19 21:50:08 +0000648 LexOnOffSwitch(PP);
Chris Lattner062f2322009-04-19 21:20:35 +0000649 }
650};
651
652/// PragmaSTDC_UnknownHandler - "#pragma STDC ...".
653struct PragmaSTDC_UnknownHandler : public PragmaHandler {
654 PragmaSTDC_UnknownHandler() : PragmaHandler(0) {}
Chris Lattnerf545be52009-04-19 21:25:37 +0000655 virtual void HandlePragma(Preprocessor &PP, Token &UnknownTok) {
Chris Lattner6c5cf4a2009-04-19 21:50:08 +0000656 // C99 6.10.6p2, unknown forms are not allowed.
Chris Lattnerf545be52009-04-19 21:25:37 +0000657 PP.Diag(UnknownTok, diag::ext_stdc_pragma_ignored);
Chris Lattner062f2322009-04-19 21:20:35 +0000658 }
659};
660
Reid Spencer5f016e22007-07-11 17:01:13 +0000661} // end anonymous namespace
662
663
664/// RegisterBuiltinPragmas - Install the standard preprocessor pragmas:
665/// #pragma GCC poison/system_header/dependency and #pragma once.
666void Preprocessor::RegisterBuiltinPragmas() {
667 AddPragmaHandler(0, new PragmaOnceHandler(getIdentifierInfo("once")));
Chris Lattner22434492007-12-19 19:38:36 +0000668 AddPragmaHandler(0, new PragmaMarkHandler(getIdentifierInfo("mark")));
Reid Spencer5f016e22007-07-11 17:01:13 +0000669 AddPragmaHandler("GCC", new PragmaPoisonHandler(getIdentifierInfo("poison")));
670 AddPragmaHandler("GCC", new PragmaSystemHeaderHandler(
671 getIdentifierInfo("system_header")));
672 AddPragmaHandler("GCC", new PragmaDependencyHandler(
673 getIdentifierInfo("dependency")));
Chris Lattneredaf8772009-04-19 23:16:58 +0000674 AddPragmaHandler("GCC", new PragmaDiagnosticHandler(
675 getIdentifierInfo("diagnostic")));
Chris Lattner636c5ef2009-01-16 08:21:25 +0000676
Chris Lattner062f2322009-04-19 21:20:35 +0000677 AddPragmaHandler("STDC", new PragmaSTDC_FP_CONTRACTHandler(
678 getIdentifierInfo("FP_CONTRACT")));
679 AddPragmaHandler("STDC", new PragmaSTDC_FENV_ACCESSHandler(
680 getIdentifierInfo("FENV_ACCESS")));
681 AddPragmaHandler("STDC", new PragmaSTDC_CX_LIMITED_RANGEHandler(
682 getIdentifierInfo("CX_LIMITED_RANGE")));
683 AddPragmaHandler("STDC", new PragmaSTDC_UnknownHandler());
684
Chris Lattner636c5ef2009-01-16 08:21:25 +0000685 // MS extensions.
686 if (Features.Microsoft)
687 AddPragmaHandler(0, new PragmaCommentHandler(getIdentifierInfo("comment")));
Reid Spencer5f016e22007-07-11 17:01:13 +0000688}