blob: 9776c2cfbd9eac72f3f4dc2794ea9338be99d433 [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?");
Chris Lattner6896a372009-06-15 05:02:34 +0000198 if (CurLexer)
199 CurLexer->ReadToEndOfLine();
200 else
201 CurPTHLexer->DiscardToEndOfLine();
Chris Lattner22434492007-12-19 19:38:36 +0000202}
203
204
Reid Spencer5f016e22007-07-11 17:01:13 +0000205/// HandlePragmaPoison - Handle #pragma GCC poison. PoisonTok is the 'poison'.
206///
Chris Lattnerd2177732007-07-20 16:59:19 +0000207void Preprocessor::HandlePragmaPoison(Token &PoisonTok) {
208 Token Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000209
210 while (1) {
211 // Read the next token to poison. While doing this, pretend that we are
212 // skipping while reading the identifier to poison.
213 // This avoids errors on code like:
214 // #pragma GCC poison X
215 // #pragma GCC poison X
Ted Kremenek68a91d52008-11-18 01:12:54 +0000216 if (CurPPLexer) CurPPLexer->LexingRawMode = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000217 LexUnexpandedToken(Tok);
Ted Kremenek68a91d52008-11-18 01:12:54 +0000218 if (CurPPLexer) CurPPLexer->LexingRawMode = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000219
220 // If we reached the end of line, we're done.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000221 if (Tok.is(tok::eom)) return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000222
223 // Can only poison identifiers.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000224 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000225 Diag(Tok, diag::err_pp_invalid_poison);
226 return;
227 }
228
229 // Look up the identifier info for the token. We disabled identifier lookup
230 // by saying we're skipping contents, so we need to do this manually.
231 IdentifierInfo *II = LookUpIdentifierInfo(Tok);
232
233 // Already poisoned.
234 if (II->isPoisoned()) continue;
235
236 // If this is a macro identifier, emit a warning.
Chris Lattner0edde552007-10-07 08:04:56 +0000237 if (II->hasMacroDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000238 Diag(Tok, diag::pp_poisoning_existing_macro);
239
240 // Finally, poison it!
241 II->setIsPoisoned();
242 }
243}
244
245/// HandlePragmaSystemHeader - Implement #pragma GCC system_header. We know
246/// that the whole directive has been parsed.
Chris Lattnerd2177732007-07-20 16:59:19 +0000247void Preprocessor::HandlePragmaSystemHeader(Token &SysHeaderTok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000248 if (isInPrimaryFile()) {
249 Diag(SysHeaderTok, diag::pp_pragma_sysheader_in_main_file);
250 return;
251 }
252
253 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
Ted Kremenek35c10c22008-11-20 01:45:11 +0000254 PreprocessorLexer *TheLexer = getCurrentFileLexer();
Reid Spencer5f016e22007-07-11 17:01:13 +0000255
256 // Mark the file as a system header.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000257 HeaderInfo.MarkFileSystemHeader(TheLexer->getFileEntry());
Reid Spencer5f016e22007-07-11 17:01:13 +0000258
Chris Lattner6896a372009-06-15 05:02:34 +0000259
260 PresumedLoc PLoc = SourceMgr.getPresumedLoc(SysHeaderTok.getLocation());
261 unsigned FilenameLen = strlen(PLoc.getFilename());
262 unsigned FilenameID = SourceMgr.getLineTableFilenameID(PLoc.getFilename(),
263 FilenameLen);
264
265 // Emit a line marker. This will change any source locations from this point
266 // forward to realize they are in a system header.
267 // Create a line note with this information.
268 SourceMgr.AddLineNote(SysHeaderTok.getLocation(), PLoc.getLine(), FilenameID,
269 false, false, true, false);
270
Reid Spencer5f016e22007-07-11 17:01:13 +0000271 // Notify the client, if desired, that we are in a new source file.
272 if (Callbacks)
Ted Kremenek35c10c22008-11-20 01:45:11 +0000273 Callbacks->FileChanged(SysHeaderTok.getLocation(),
Chris Lattner0b9e7362008-09-26 21:18:42 +0000274 PPCallbacks::SystemHeaderPragma, SrcMgr::C_System);
Reid Spencer5f016e22007-07-11 17:01:13 +0000275}
276
277/// HandlePragmaDependency - Handle #pragma GCC dependency "foo" blah.
278///
Chris Lattnerd2177732007-07-20 16:59:19 +0000279void Preprocessor::HandlePragmaDependency(Token &DependencyTok) {
280 Token FilenameTok;
Ted Kremenek68a91d52008-11-18 01:12:54 +0000281 CurPPLexer->LexIncludeFilename(FilenameTok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000282
283 // If the token kind is EOM, the error has already been diagnosed.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000284 if (FilenameTok.is(tok::eom))
Reid Spencer5f016e22007-07-11 17:01:13 +0000285 return;
286
287 // Reserve a buffer to get the spelling.
288 llvm::SmallVector<char, 128> FilenameBuffer;
289 FilenameBuffer.resize(FilenameTok.getLength());
290
Chris Lattnerf1c99ac2007-07-23 04:15:27 +0000291 const char *FilenameStart = &FilenameBuffer[0];
292 unsigned Len = getSpelling(FilenameTok, FilenameStart);
293 const char *FilenameEnd = FilenameStart+Len;
294 bool isAngled = GetIncludeFilenameSpelling(FilenameTok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000295 FilenameStart, FilenameEnd);
296 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
297 // error.
298 if (FilenameStart == 0)
299 return;
300
301 // Search include directories for this file.
302 const DirectoryLookup *CurDir;
303 const FileEntry *File = LookupFile(FilenameStart, FilenameEnd,
304 isAngled, 0, CurDir);
Chris Lattner56b05c82008-11-18 08:02:48 +0000305 if (File == 0) {
306 Diag(FilenameTok, diag::err_pp_file_not_found)
307 << std::string(FilenameStart, FilenameEnd);
308 return;
309 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000310
Chris Lattner2b2453a2009-01-17 06:22:33 +0000311 const FileEntry *CurFile = getCurrentFileLexer()->getFileEntry();
Reid Spencer5f016e22007-07-11 17:01:13 +0000312
313 // If this file is older than the file it depends on, emit a diagnostic.
314 if (CurFile && CurFile->getModificationTime() < File->getModificationTime()) {
315 // Lex tokens at the end of the message and include them in the message.
316 std::string Message;
317 Lex(DependencyTok);
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000318 while (DependencyTok.isNot(tok::eom)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000319 Message += getSpelling(DependencyTok) + " ";
320 Lex(DependencyTok);
321 }
322
323 Message.erase(Message.end()-1);
Chris Lattner56b05c82008-11-18 08:02:48 +0000324 Diag(FilenameTok, diag::pp_out_of_date_dependency) << Message;
Reid Spencer5f016e22007-07-11 17:01:13 +0000325 }
326}
327
Chris Lattner636c5ef2009-01-16 08:21:25 +0000328/// HandlePragmaComment - Handle the microsoft #pragma comment extension. The
329/// syntax is:
330/// #pragma comment(linker, "foo")
331/// 'linker' is one of five identifiers: compiler, exestr, lib, linker, user.
332/// "foo" is a string, which is fully macro expanded, and permits string
Gabor Greifd7ee3492009-03-17 11:39:38 +0000333/// concatenation, embedded escape characters etc. See MSDN for more details.
Chris Lattner636c5ef2009-01-16 08:21:25 +0000334void Preprocessor::HandlePragmaComment(Token &Tok) {
335 SourceLocation CommentLoc = Tok.getLocation();
336 Lex(Tok);
337 if (Tok.isNot(tok::l_paren)) {
338 Diag(CommentLoc, diag::err_pragma_comment_malformed);
339 return;
340 }
341
342 // Read the identifier.
343 Lex(Tok);
344 if (Tok.isNot(tok::identifier)) {
345 Diag(CommentLoc, diag::err_pragma_comment_malformed);
346 return;
347 }
348
349 // Verify that this is one of the 5 whitelisted options.
350 // FIXME: warn that 'exestr' is deprecated.
351 const IdentifierInfo *II = Tok.getIdentifierInfo();
352 if (!II->isStr("compiler") && !II->isStr("exestr") && !II->isStr("lib") &&
353 !II->isStr("linker") && !II->isStr("user")) {
354 Diag(Tok.getLocation(), diag::err_pragma_comment_unknown_kind);
355 return;
356 }
357
Chris Lattnera9d91452009-01-16 18:59:23 +0000358 // Read the optional string if present.
Chris Lattner636c5ef2009-01-16 08:21:25 +0000359 Lex(Tok);
Chris Lattnera9d91452009-01-16 18:59:23 +0000360 std::string ArgumentString;
Chris Lattner636c5ef2009-01-16 08:21:25 +0000361 if (Tok.is(tok::comma)) {
Chris Lattnera9d91452009-01-16 18:59:23 +0000362 Lex(Tok); // eat the comma.
Chris Lattner636c5ef2009-01-16 08:21:25 +0000363
364 // We need at least one string.
Chris Lattneredaf8772009-04-19 23:16:58 +0000365 if (Tok.isNot(tok::string_literal)) {
Chris Lattner636c5ef2009-01-16 08:21:25 +0000366 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
367 return;
368 }
369
370 // String concatenation allows multiple strings, which can even come from
371 // macro expansion.
372 // "foo " "bar" "Baz"
Chris Lattnera9d91452009-01-16 18:59:23 +0000373 llvm::SmallVector<Token, 4> StrToks;
Chris Lattneredaf8772009-04-19 23:16:58 +0000374 while (Tok.is(tok::string_literal)) {
Chris Lattnera9d91452009-01-16 18:59:23 +0000375 StrToks.push_back(Tok);
Chris Lattner636c5ef2009-01-16 08:21:25 +0000376 Lex(Tok);
Chris Lattnera9d91452009-01-16 18:59:23 +0000377 }
378
379 // Concatenate and parse the strings.
380 StringLiteralParser Literal(&StrToks[0], StrToks.size(), *this);
381 assert(!Literal.AnyWide && "Didn't allow wide strings in");
382 if (Literal.hadError)
383 return;
384 if (Literal.Pascal) {
385 Diag(StrToks[0].getLocation(), diag::err_pragma_comment_malformed);
386 return;
387 }
388
389 ArgumentString = std::string(Literal.GetString(),
390 Literal.GetString()+Literal.GetStringLength());
Chris Lattner636c5ef2009-01-16 08:21:25 +0000391 }
392
Chris Lattnera9d91452009-01-16 18:59:23 +0000393 // FIXME: If the kind is "compiler" warn if the string is present (it is
394 // ignored).
395 // FIXME: 'lib' requires a comment string.
396 // FIXME: 'linker' requires a comment string, and has a specific list of
397 // things that are allowable.
398
Chris Lattner636c5ef2009-01-16 08:21:25 +0000399 if (Tok.isNot(tok::r_paren)) {
400 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
401 return;
402 }
Chris Lattnera9d91452009-01-16 18:59:23 +0000403 Lex(Tok); // eat the r_paren.
Chris Lattner636c5ef2009-01-16 08:21:25 +0000404
405 if (Tok.isNot(tok::eom)) {
406 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
407 return;
408 }
Chris Lattnera9d91452009-01-16 18:59:23 +0000409
410 // If the pragma is lexically sound, notify any interested PPCallbacks.
Chris Lattner172e3362009-01-16 19:01:46 +0000411 if (Callbacks)
412 Callbacks->PragmaComment(CommentLoc, II, ArgumentString);
Chris Lattner636c5ef2009-01-16 08:21:25 +0000413}
414
415
416
Reid Spencer5f016e22007-07-11 17:01:13 +0000417
418/// AddPragmaHandler - Add the specified pragma handler to the preprocessor.
419/// If 'Namespace' is non-null, then it is a token required to exist on the
420/// pragma line before the pragma string starts, e.g. "STDC" or "GCC".
421void Preprocessor::AddPragmaHandler(const char *Namespace,
422 PragmaHandler *Handler) {
423 PragmaNamespace *InsertNS = PragmaHandlers;
424
425 // If this is specified to be in a namespace, step down into it.
426 if (Namespace) {
427 IdentifierInfo *NSID = getIdentifierInfo(Namespace);
428
429 // If there is already a pragma handler with the name of this namespace,
430 // we either have an error (directive with the same name as a namespace) or
431 // we already have the namespace to insert into.
432 if (PragmaHandler *Existing = PragmaHandlers->FindHandler(NSID)) {
433 InsertNS = Existing->getIfNamespace();
434 assert(InsertNS != 0 && "Cannot have a pragma namespace and pragma"
435 " handler with the same name!");
436 } else {
437 // Otherwise, this namespace doesn't exist yet, create and insert the
438 // handler for it.
439 InsertNS = new PragmaNamespace(NSID);
440 PragmaHandlers->AddPragma(InsertNS);
441 }
442 }
443
444 // Check to make sure we don't already have a pragma for this identifier.
445 assert(!InsertNS->FindHandler(Handler->getName()) &&
446 "Pragma handler already exists for this identifier!");
447 InsertNS->AddPragma(Handler);
448}
449
Daniel Dunbar40950802008-10-04 19:17:46 +0000450/// RemovePragmaHandler - Remove the specific pragma handler from the
451/// preprocessor. If \arg Namespace is non-null, then it should be the
452/// namespace that \arg Handler was added to. It is an error to remove
453/// a handler that has not been registered.
454void Preprocessor::RemovePragmaHandler(const char *Namespace,
455 PragmaHandler *Handler) {
456 PragmaNamespace *NS = PragmaHandlers;
457
458 // If this is specified to be in a namespace, step down into it.
459 if (Namespace) {
460 IdentifierInfo *NSID = getIdentifierInfo(Namespace);
461 PragmaHandler *Existing = PragmaHandlers->FindHandler(NSID);
462 assert(Existing && "Namespace containing handler does not exist!");
463
464 NS = Existing->getIfNamespace();
465 assert(NS && "Invalid namespace, registered as a regular pragma handler!");
466 }
467
468 NS->RemovePragmaHandler(Handler);
469
470 // If this is a non-default namespace and it is now empty, remove
471 // it.
472 if (NS != PragmaHandlers && NS->IsEmpty())
473 PragmaHandlers->RemovePragmaHandler(NS);
474}
475
Reid Spencer5f016e22007-07-11 17:01:13 +0000476namespace {
Chris Lattner22434492007-12-19 19:38:36 +0000477/// PragmaOnceHandler - "#pragma once" marks the file as atomically included.
Reid Spencer5f016e22007-07-11 17:01:13 +0000478struct PragmaOnceHandler : public PragmaHandler {
479 PragmaOnceHandler(const IdentifierInfo *OnceID) : PragmaHandler(OnceID) {}
Chris Lattnerd2177732007-07-20 16:59:19 +0000480 virtual void HandlePragma(Preprocessor &PP, Token &OnceTok) {
Chris Lattner35410d52009-04-14 05:07:49 +0000481 PP.CheckEndOfDirective("pragma once");
Reid Spencer5f016e22007-07-11 17:01:13 +0000482 PP.HandlePragmaOnce(OnceTok);
483 }
484};
485
Chris Lattner22434492007-12-19 19:38:36 +0000486/// PragmaMarkHandler - "#pragma mark ..." is ignored by the compiler, and the
487/// rest of the line is not lexed.
488struct PragmaMarkHandler : public PragmaHandler {
489 PragmaMarkHandler(const IdentifierInfo *MarkID) : PragmaHandler(MarkID) {}
490 virtual void HandlePragma(Preprocessor &PP, Token &MarkTok) {
491 PP.HandlePragmaMark();
492 }
493};
494
495/// PragmaPoisonHandler - "#pragma poison x" marks x as not usable.
Reid Spencer5f016e22007-07-11 17:01:13 +0000496struct PragmaPoisonHandler : public PragmaHandler {
497 PragmaPoisonHandler(const IdentifierInfo *ID) : PragmaHandler(ID) {}
Chris Lattnerd2177732007-07-20 16:59:19 +0000498 virtual void HandlePragma(Preprocessor &PP, Token &PoisonTok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000499 PP.HandlePragmaPoison(PoisonTok);
500 }
501};
502
Chris Lattner22434492007-12-19 19:38:36 +0000503/// PragmaSystemHeaderHandler - "#pragma system_header" marks the current file
504/// as a system header, which silences warnings in it.
Reid Spencer5f016e22007-07-11 17:01:13 +0000505struct PragmaSystemHeaderHandler : public PragmaHandler {
506 PragmaSystemHeaderHandler(const IdentifierInfo *ID) : PragmaHandler(ID) {}
Chris Lattnerd2177732007-07-20 16:59:19 +0000507 virtual void HandlePragma(Preprocessor &PP, Token &SHToken) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000508 PP.HandlePragmaSystemHeader(SHToken);
Chris Lattner35410d52009-04-14 05:07:49 +0000509 PP.CheckEndOfDirective("pragma");
Reid Spencer5f016e22007-07-11 17:01:13 +0000510 }
511};
512struct PragmaDependencyHandler : public PragmaHandler {
513 PragmaDependencyHandler(const IdentifierInfo *ID) : PragmaHandler(ID) {}
Chris Lattnerd2177732007-07-20 16:59:19 +0000514 virtual void HandlePragma(Preprocessor &PP, Token &DepToken) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000515 PP.HandlePragmaDependency(DepToken);
516 }
517};
Chris Lattner636c5ef2009-01-16 08:21:25 +0000518
Chris Lattneredaf8772009-04-19 23:16:58 +0000519/// PragmaDiagnosticHandler - e.g. '#pragma GCC diagnostic ignored "-Wformat"'
520struct PragmaDiagnosticHandler : public PragmaHandler {
521 PragmaDiagnosticHandler(const IdentifierInfo *ID) : PragmaHandler(ID) {}
522 virtual void HandlePragma(Preprocessor &PP, Token &DiagToken) {
523 Token Tok;
524 PP.LexUnexpandedToken(Tok);
525 if (Tok.isNot(tok::identifier)) {
526 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
527 return;
528 }
529 IdentifierInfo *II = Tok.getIdentifierInfo();
530
531 diag::Mapping Map;
532 if (II->isStr("warning"))
533 Map = diag::MAP_WARNING;
534 else if (II->isStr("error"))
535 Map = diag::MAP_ERROR;
536 else if (II->isStr("ignored"))
537 Map = diag::MAP_IGNORE;
538 else if (II->isStr("fatal"))
539 Map = diag::MAP_FATAL;
540 else {
541 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
542 return;
543 }
544
545 PP.LexUnexpandedToken(Tok);
546
547 // We need at least one string.
548 if (Tok.isNot(tok::string_literal)) {
549 PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token);
550 return;
551 }
552
553 // String concatenation allows multiple strings, which can even come from
554 // macro expansion.
555 // "foo " "bar" "Baz"
556 llvm::SmallVector<Token, 4> StrToks;
557 while (Tok.is(tok::string_literal)) {
558 StrToks.push_back(Tok);
559 PP.LexUnexpandedToken(Tok);
560 }
561
562 if (Tok.isNot(tok::eom)) {
563 PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token);
564 return;
565 }
566
567 // Concatenate and parse the strings.
568 StringLiteralParser Literal(&StrToks[0], StrToks.size(), PP);
569 assert(!Literal.AnyWide && "Didn't allow wide strings in");
570 if (Literal.hadError)
571 return;
572 if (Literal.Pascal) {
573 PP.Diag(StrToks[0].getLocation(), diag::warn_pragma_diagnostic_invalid);
574 return;
575 }
576
577 std::string WarningName(Literal.GetString(),
578 Literal.GetString()+Literal.GetStringLength());
579
580 if (WarningName.size() < 3 || WarningName[0] != '-' ||
581 WarningName[1] != 'W') {
582 PP.Diag(StrToks[0].getLocation(),
583 diag::warn_pragma_diagnostic_invalid_option);
584 return;
585 }
586
587 if (PP.getDiagnostics().setDiagnosticGroupMapping(WarningName.c_str()+2,
588 Map))
589 PP.Diag(StrToks[0].getLocation(),
590 diag::warn_pragma_diagnostic_unknown_warning) << WarningName;
591 }
592};
593
Chris Lattner636c5ef2009-01-16 08:21:25 +0000594/// PragmaCommentHandler - "#pragma comment ...".
595struct PragmaCommentHandler : public PragmaHandler {
596 PragmaCommentHandler(const IdentifierInfo *ID) : PragmaHandler(ID) {}
597 virtual void HandlePragma(Preprocessor &PP, Token &CommentTok) {
598 PP.HandlePragmaComment(CommentTok);
599 }
600};
Chris Lattner062f2322009-04-19 21:20:35 +0000601
602// Pragma STDC implementations.
Chris Lattner6c5cf4a2009-04-19 21:50:08 +0000603
604enum STDCSetting {
605 STDC_ON, STDC_OFF, STDC_DEFAULT, STDC_INVALID
606};
607
608static STDCSetting LexOnOffSwitch(Preprocessor &PP) {
609 Token Tok;
610 PP.LexUnexpandedToken(Tok);
611
612 if (Tok.isNot(tok::identifier)) {
613 PP.Diag(Tok, diag::ext_stdc_pragma_syntax);
614 return STDC_INVALID;
615 }
616 IdentifierInfo *II = Tok.getIdentifierInfo();
617 STDCSetting Result;
618 if (II->isStr("ON"))
619 Result = STDC_ON;
620 else if (II->isStr("OFF"))
621 Result = STDC_OFF;
622 else if (II->isStr("DEFAULT"))
623 Result = STDC_DEFAULT;
624 else {
625 PP.Diag(Tok, diag::ext_stdc_pragma_syntax);
626 return STDC_INVALID;
627 }
628
629 // Verify that this is followed by EOM.
630 PP.LexUnexpandedToken(Tok);
631 if (Tok.isNot(tok::eom))
632 PP.Diag(Tok, diag::ext_stdc_pragma_syntax_eom);
633 return Result;
634}
Chris Lattner062f2322009-04-19 21:20:35 +0000635
636/// PragmaSTDC_FP_CONTRACTHandler - "#pragma STDC FP_CONTRACT ...".
637struct PragmaSTDC_FP_CONTRACTHandler : public PragmaHandler {
638 PragmaSTDC_FP_CONTRACTHandler(const IdentifierInfo *ID) : PragmaHandler(ID) {}
Chris Lattnerf545be52009-04-19 21:25:37 +0000639 virtual void HandlePragma(Preprocessor &PP, Token &Tok) {
Chris Lattner6c5cf4a2009-04-19 21:50:08 +0000640 // We just ignore the setting of FP_CONTRACT. Since we don't do contractions
641 // at all, our default is OFF and setting it to ON is an optimization hint
642 // we can safely ignore. When we support -ffma or something, we would need
643 // to diagnose that we are ignoring FMA.
644 LexOnOffSwitch(PP);
Chris Lattner062f2322009-04-19 21:20:35 +0000645 }
646};
647
648/// PragmaSTDC_FENV_ACCESSHandler - "#pragma STDC FENV_ACCESS ...".
649struct PragmaSTDC_FENV_ACCESSHandler : public PragmaHandler {
650 PragmaSTDC_FENV_ACCESSHandler(const IdentifierInfo *ID) : PragmaHandler(ID) {}
Chris Lattnerf545be52009-04-19 21:25:37 +0000651 virtual void HandlePragma(Preprocessor &PP, Token &Tok) {
Chris Lattner4d8aac32009-04-19 21:55:32 +0000652 if (LexOnOffSwitch(PP) == STDC_ON)
653 PP.Diag(Tok, diag::warn_stdc_fenv_access_not_supported);
Chris Lattner062f2322009-04-19 21:20:35 +0000654 }
655};
656
657/// PragmaSTDC_CX_LIMITED_RANGEHandler - "#pragma STDC CX_LIMITED_RANGE ...".
658struct PragmaSTDC_CX_LIMITED_RANGEHandler : public PragmaHandler {
659 PragmaSTDC_CX_LIMITED_RANGEHandler(const IdentifierInfo *ID)
660 : PragmaHandler(ID) {}
Chris Lattnerf545be52009-04-19 21:25:37 +0000661 virtual void HandlePragma(Preprocessor &PP, Token &Tok) {
Chris Lattner6c5cf4a2009-04-19 21:50:08 +0000662 LexOnOffSwitch(PP);
Chris Lattner062f2322009-04-19 21:20:35 +0000663 }
664};
665
666/// PragmaSTDC_UnknownHandler - "#pragma STDC ...".
667struct PragmaSTDC_UnknownHandler : public PragmaHandler {
668 PragmaSTDC_UnknownHandler() : PragmaHandler(0) {}
Chris Lattnerf545be52009-04-19 21:25:37 +0000669 virtual void HandlePragma(Preprocessor &PP, Token &UnknownTok) {
Chris Lattner6c5cf4a2009-04-19 21:50:08 +0000670 // C99 6.10.6p2, unknown forms are not allowed.
Chris Lattnerf545be52009-04-19 21:25:37 +0000671 PP.Diag(UnknownTok, diag::ext_stdc_pragma_ignored);
Chris Lattner062f2322009-04-19 21:20:35 +0000672 }
673};
674
Reid Spencer5f016e22007-07-11 17:01:13 +0000675} // end anonymous namespace
676
677
678/// RegisterBuiltinPragmas - Install the standard preprocessor pragmas:
679/// #pragma GCC poison/system_header/dependency and #pragma once.
680void Preprocessor::RegisterBuiltinPragmas() {
681 AddPragmaHandler(0, new PragmaOnceHandler(getIdentifierInfo("once")));
Chris Lattner22434492007-12-19 19:38:36 +0000682 AddPragmaHandler(0, new PragmaMarkHandler(getIdentifierInfo("mark")));
Chris Lattnere8fa06e2009-05-12 18:21:11 +0000683
684 // #pragma GCC ...
Reid Spencer5f016e22007-07-11 17:01:13 +0000685 AddPragmaHandler("GCC", new PragmaPoisonHandler(getIdentifierInfo("poison")));
686 AddPragmaHandler("GCC", new PragmaSystemHeaderHandler(
687 getIdentifierInfo("system_header")));
688 AddPragmaHandler("GCC", new PragmaDependencyHandler(
689 getIdentifierInfo("dependency")));
Chris Lattneredaf8772009-04-19 23:16:58 +0000690 AddPragmaHandler("GCC", new PragmaDiagnosticHandler(
691 getIdentifierInfo("diagnostic")));
Chris Lattnere8fa06e2009-05-12 18:21:11 +0000692 // #pragma clang ...
693 AddPragmaHandler("clang", new PragmaPoisonHandler(
694 getIdentifierInfo("poison")));
695 AddPragmaHandler("clang", new PragmaSystemHeaderHandler(
696 getIdentifierInfo("system_header")));
697 AddPragmaHandler("clang", new PragmaDependencyHandler(
698 getIdentifierInfo("dependency")));
699 AddPragmaHandler("clang", new PragmaDiagnosticHandler(
700 getIdentifierInfo("diagnostic")));
701
Chris Lattner062f2322009-04-19 21:20:35 +0000702 AddPragmaHandler("STDC", new PragmaSTDC_FP_CONTRACTHandler(
703 getIdentifierInfo("FP_CONTRACT")));
704 AddPragmaHandler("STDC", new PragmaSTDC_FENV_ACCESSHandler(
705 getIdentifierInfo("FENV_ACCESS")));
706 AddPragmaHandler("STDC", new PragmaSTDC_CX_LIMITED_RANGEHandler(
707 getIdentifierInfo("CX_LIMITED_RANGE")));
708 AddPragmaHandler("STDC", new PragmaSTDC_UnknownHandler());
709
Chris Lattner636c5ef2009-01-16 08:21:25 +0000710 // MS extensions.
711 if (Features.Microsoft)
712 AddPragmaHandler(0, new PragmaCommentHandler(getIdentifierInfo("comment")));
Reid Spencer5f016e22007-07-11 17:01:13 +0000713}