blob: bb0b71e2268237cbc4162ede11ea5c3378e7423d [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- Pragma.cpp - Pragma registration and handling --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +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"
Chris Lattner4b009652007-07-25 00:24:17 +000016#include "clang/Lex/HeaderSearch.h"
Chris Lattner1055b4f2009-01-16 18:59:23 +000017#include "clang/Lex/LiteralSupport.h"
Chris Lattner4b009652007-07-25 00:24:17 +000018#include "clang/Lex/Preprocessor.h"
Chris Lattner545f39e2009-01-29 05:15:15 +000019#include "clang/Lex/LexDiagnostic.h"
Chris Lattner4b009652007-07-25 00:24:17 +000020#include "clang/Basic/FileManager.h"
21#include "clang/Basic/SourceManager.h"
Douglas Gregora252b232009-07-02 17:08:52 +000022#include <algorithm>
Chris Lattner4b009652007-07-25 00:24:17 +000023using namespace clang;
24
25// Out-of-line destructor to provide a home for the class.
26PragmaHandler::~PragmaHandler() {
27}
28
29//===----------------------------------------------------------------------===//
30// PragmaNamespace Implementation.
31//===----------------------------------------------------------------------===//
32
33
34PragmaNamespace::~PragmaNamespace() {
35 for (unsigned i = 0, e = Handlers.size(); i != e; ++i)
36 delete Handlers[i];
37}
38
39/// FindHandler - Check to see if there is already a handler for the
40/// specified name. If not, return the handler for the null identifier if it
41/// exists, otherwise return null. If IgnoreNull is true (the default) then
42/// the null handler isn't returned on failure to match.
43PragmaHandler *PragmaNamespace::FindHandler(const IdentifierInfo *Name,
44 bool IgnoreNull) const {
45 PragmaHandler *NullHandler = 0;
46 for (unsigned i = 0, e = Handlers.size(); i != e; ++i) {
47 if (Handlers[i]->getName() == Name)
48 return Handlers[i];
49
50 if (Handlers[i]->getName() == 0)
51 NullHandler = Handlers[i];
52 }
53 return IgnoreNull ? 0 : NullHandler;
54}
55
Daniel Dunbara65c00a2008-10-04 19:17:46 +000056void PragmaNamespace::RemovePragmaHandler(PragmaHandler *Handler) {
57 for (unsigned i = 0, e = Handlers.size(); i != e; ++i) {
58 if (Handlers[i] == Handler) {
59 Handlers[i] = Handlers.back();
60 Handlers.pop_back();
61 return;
62 }
63 }
64 assert(0 && "Handler not registered in this namespace");
65}
66
Chris Lattner4b009652007-07-25 00:24:17 +000067void PragmaNamespace::HandlePragma(Preprocessor &PP, Token &Tok) {
68 // Read the 'namespace' that the directive is in, e.g. STDC. Do not macro
69 // expand it, the user can have a STDC #define, that should not affect this.
70 PP.LexUnexpandedToken(Tok);
71
72 // Get the handler for this token. If there is no handler, ignore the pragma.
73 PragmaHandler *Handler = FindHandler(Tok.getIdentifierInfo(), false);
Chris Lattnerd65e0142009-04-19 21:10:26 +000074 if (Handler == 0) {
75 PP.Diag(Tok, diag::warn_pragma_ignored);
76 return;
77 }
Chris Lattner4b009652007-07-25 00:24:17 +000078
79 // Otherwise, pass it down.
80 Handler->HandlePragma(PP, Tok);
81}
82
83//===----------------------------------------------------------------------===//
84// Preprocessor Pragma Directive Handling.
85//===----------------------------------------------------------------------===//
86
87/// HandlePragmaDirective - The "#pragma" directive has been parsed. Lex the
88/// rest of the pragma, passing it to the registered pragma handlers.
89void Preprocessor::HandlePragmaDirective() {
90 ++NumPragma;
91
92 // Invoke the first level of pragma handlers which reads the namespace id.
93 Token Tok;
94 PragmaHandlers->HandlePragma(*this, Tok);
95
96 // If the pragma handler didn't read the rest of the line, consume it now.
Chris Lattneraa6a37b2009-06-18 05:55:53 +000097 if (CurPPLexer && CurPPLexer->ParsingPreprocessorDirective)
Chris Lattner4b009652007-07-25 00:24:17 +000098 DiscardUntilEndOfDirective();
99}
100
101/// Handle_Pragma - Read a _Pragma directive, slice it up, process it, then
102/// return the first token after the directive. The _Pragma token has just
103/// been read into 'Tok'.
104void Preprocessor::Handle_Pragma(Token &Tok) {
105 // Remember the pragma token location.
106 SourceLocation PragmaLoc = Tok.getLocation();
107
108 // Read the '('.
109 Lex(Tok);
Chris Lattner0370d6b2008-11-18 07:59:24 +0000110 if (Tok.isNot(tok::l_paren)) {
111 Diag(PragmaLoc, diag::err__Pragma_malformed);
112 return;
113 }
Chris Lattner4b009652007-07-25 00:24:17 +0000114
115 // Read the '"..."'.
116 Lex(Tok);
Chris Lattner0370d6b2008-11-18 07:59:24 +0000117 if (Tok.isNot(tok::string_literal) && Tok.isNot(tok::wide_string_literal)) {
118 Diag(PragmaLoc, diag::err__Pragma_malformed);
119 return;
120 }
Chris Lattner4b009652007-07-25 00:24:17 +0000121
122 // Remember the string.
123 std::string StrVal = getSpelling(Tok);
Chris Lattner4b009652007-07-25 00:24:17 +0000124
125 // Read the ')'.
126 Lex(Tok);
Chris Lattner0370d6b2008-11-18 07:59:24 +0000127 if (Tok.isNot(tok::r_paren)) {
128 Diag(PragmaLoc, diag::err__Pragma_malformed);
129 return;
130 }
Chris Lattner4b009652007-07-25 00:24:17 +0000131
Chris Lattnerbc7a3cb2009-02-15 20:52:18 +0000132 SourceLocation RParenLoc = Tok.getLocation();
133
Chris Lattner1055b4f2009-01-16 18:59:23 +0000134 // The _Pragma is lexically sound. Destringize according to C99 6.10.9.1:
135 // "The string literal is destringized by deleting the L prefix, if present,
136 // deleting the leading and trailing double-quotes, replacing each escape
137 // sequence \" by a double-quote, and replacing each escape sequence \\ by a
138 // single backslash."
Chris Lattner4b009652007-07-25 00:24:17 +0000139 if (StrVal[0] == 'L') // Remove L prefix.
140 StrVal.erase(StrVal.begin());
141 assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
142 "Invalid string token!");
143
144 // Remove the front quote, replacing it with a space, so that the pragma
145 // contents appear to have a space before them.
146 StrVal[0] = ' ';
147
Chris Lattnerd6d3feb2009-03-08 08:08:45 +0000148 // Replace the terminating quote with a \n.
Chris Lattner4b009652007-07-25 00:24:17 +0000149 StrVal[StrVal.size()-1] = '\n';
Chris Lattner4b009652007-07-25 00:24:17 +0000150
151 // Remove escaped quotes and escapes.
152 for (unsigned i = 0, e = StrVal.size(); i != e-1; ++i) {
153 if (StrVal[i] == '\\' &&
154 (StrVal[i+1] == '\\' || StrVal[i+1] == '"')) {
155 // \\ -> '\' and \" -> '"'.
156 StrVal.erase(StrVal.begin()+i);
157 --e;
158 }
159 }
160
161 // Plop the string (including the newline and trailing null) into a buffer
162 // where we can lex it.
Chris Lattner6ad1f502009-01-26 19:29:26 +0000163 Token TmpTok;
164 TmpTok.startToken();
165 CreateString(&StrVal[0], StrVal.size(), TmpTok);
166 SourceLocation TokLoc = TmpTok.getLocation();
Chris Lattner4b009652007-07-25 00:24:17 +0000167
168 // Make and enter a lexer object so that we lex and expand the tokens just
169 // like any others.
Chris Lattnerbc7a3cb2009-02-15 20:52:18 +0000170 Lexer *TL = Lexer::Create_PragmaLexer(TokLoc, PragmaLoc, RParenLoc,
Chris Lattnerd6d3feb2009-03-08 08:08:45 +0000171 StrVal.size(), *this);
Chris Lattner4b009652007-07-25 00:24:17 +0000172
173 EnterSourceFileWithLexer(TL, 0);
174
175 // With everything set up, lex this as a #pragma directive.
176 HandlePragmaDirective();
177
178 // Finally, return whatever came after the pragma directive.
179 return Lex(Tok);
180}
181
182
183
184/// HandlePragmaOnce - Handle #pragma once. OnceTok is the 'once'.
185///
186void Preprocessor::HandlePragmaOnce(Token &OnceTok) {
187 if (isInPrimaryFile()) {
188 Diag(OnceTok, diag::pp_pragma_once_in_main_file);
189 return;
190 }
191
192 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
Chris Lattner4b009652007-07-25 00:24:17 +0000193 // Mark the file as a once-only file now.
Chris Lattnerf4f776a2009-01-17 06:22:33 +0000194 HeaderInfo.MarkFileIncludeOnce(getCurrentFileLexer()->getFileEntry());
Chris Lattner4b009652007-07-25 00:24:17 +0000195}
196
Chris Lattnerc0f6b222007-12-19 19:38:36 +0000197void Preprocessor::HandlePragmaMark() {
Ted Kremenekb53b1f42008-11-19 22:21:33 +0000198 assert(CurPPLexer && "No current lexer?");
Chris Lattner677f0c42009-06-15 05:02:34 +0000199 if (CurLexer)
200 CurLexer->ReadToEndOfLine();
201 else
202 CurPTHLexer->DiscardToEndOfLine();
Chris Lattnerc0f6b222007-12-19 19:38:36 +0000203}
204
205
Chris Lattner4b009652007-07-25 00:24:17 +0000206/// HandlePragmaPoison - Handle #pragma GCC poison. PoisonTok is the 'poison'.
207///
208void Preprocessor::HandlePragmaPoison(Token &PoisonTok) {
209 Token Tok;
210
211 while (1) {
212 // Read the next token to poison. While doing this, pretend that we are
213 // skipping while reading the identifier to poison.
214 // This avoids errors on code like:
215 // #pragma GCC poison X
216 // #pragma GCC poison X
Ted Kremenek31dd0262008-11-18 01:12:54 +0000217 if (CurPPLexer) CurPPLexer->LexingRawMode = true;
Chris Lattner4b009652007-07-25 00:24:17 +0000218 LexUnexpandedToken(Tok);
Ted Kremenek31dd0262008-11-18 01:12:54 +0000219 if (CurPPLexer) CurPPLexer->LexingRawMode = false;
Chris Lattner4b009652007-07-25 00:24:17 +0000220
221 // If we reached the end of line, we're done.
Chris Lattnercb8e41c2007-10-09 18:02:16 +0000222 if (Tok.is(tok::eom)) return;
Chris Lattner4b009652007-07-25 00:24:17 +0000223
224 // Can only poison identifiers.
Chris Lattnercb8e41c2007-10-09 18:02:16 +0000225 if (Tok.isNot(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000226 Diag(Tok, diag::err_pp_invalid_poison);
227 return;
228 }
229
230 // Look up the identifier info for the token. We disabled identifier lookup
231 // by saying we're skipping contents, so we need to do this manually.
232 IdentifierInfo *II = LookUpIdentifierInfo(Tok);
233
234 // Already poisoned.
235 if (II->isPoisoned()) continue;
236
237 // If this is a macro identifier, emit a warning.
Chris Lattner3b56a012007-10-07 08:04:56 +0000238 if (II->hasMacroDefinition())
Chris Lattner4b009652007-07-25 00:24:17 +0000239 Diag(Tok, diag::pp_poisoning_existing_macro);
240
241 // Finally, poison it!
242 II->setIsPoisoned();
243 }
244}
245
246/// HandlePragmaSystemHeader - Implement #pragma GCC system_header. We know
247/// that the whole directive has been parsed.
248void Preprocessor::HandlePragmaSystemHeader(Token &SysHeaderTok) {
249 if (isInPrimaryFile()) {
250 Diag(SysHeaderTok, diag::pp_pragma_sysheader_in_main_file);
251 return;
252 }
253
254 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
Ted Kremenekf1b062a2008-11-20 01:45:11 +0000255 PreprocessorLexer *TheLexer = getCurrentFileLexer();
Chris Lattner4b009652007-07-25 00:24:17 +0000256
257 // Mark the file as a system header.
Chris Lattnerf4f776a2009-01-17 06:22:33 +0000258 HeaderInfo.MarkFileSystemHeader(TheLexer->getFileEntry());
Chris Lattner4b009652007-07-25 00:24:17 +0000259
Chris Lattner677f0c42009-06-15 05:02:34 +0000260
261 PresumedLoc PLoc = SourceMgr.getPresumedLoc(SysHeaderTok.getLocation());
262 unsigned FilenameLen = strlen(PLoc.getFilename());
263 unsigned FilenameID = SourceMgr.getLineTableFilenameID(PLoc.getFilename(),
264 FilenameLen);
265
266 // Emit a line marker. This will change any source locations from this point
267 // forward to realize they are in a system header.
268 // Create a line note with this information.
269 SourceMgr.AddLineNote(SysHeaderTok.getLocation(), PLoc.getLine(), FilenameID,
270 false, false, true, false);
271
Chris Lattner4b009652007-07-25 00:24:17 +0000272 // Notify the client, if desired, that we are in a new source file.
273 if (Callbacks)
Ted Kremenekf1b062a2008-11-20 01:45:11 +0000274 Callbacks->FileChanged(SysHeaderTok.getLocation(),
Chris Lattner6f044062008-09-26 21:18:42 +0000275 PPCallbacks::SystemHeaderPragma, SrcMgr::C_System);
Chris Lattner4b009652007-07-25 00:24:17 +0000276}
277
278/// HandlePragmaDependency - Handle #pragma GCC dependency "foo" blah.
279///
280void Preprocessor::HandlePragmaDependency(Token &DependencyTok) {
281 Token FilenameTok;
Ted Kremenek31dd0262008-11-18 01:12:54 +0000282 CurPPLexer->LexIncludeFilename(FilenameTok);
Chris Lattner4b009652007-07-25 00:24:17 +0000283
284 // If the token kind is EOM, the error has already been diagnosed.
Chris Lattnercb8e41c2007-10-09 18:02:16 +0000285 if (FilenameTok.is(tok::eom))
Chris Lattner4b009652007-07-25 00:24:17 +0000286 return;
287
288 // Reserve a buffer to get the spelling.
289 llvm::SmallVector<char, 128> FilenameBuffer;
290 FilenameBuffer.resize(FilenameTok.getLength());
291
292 const char *FilenameStart = &FilenameBuffer[0];
293 unsigned Len = getSpelling(FilenameTok, FilenameStart);
294 const char *FilenameEnd = FilenameStart+Len;
295 bool isAngled = GetIncludeFilenameSpelling(FilenameTok.getLocation(),
296 FilenameStart, FilenameEnd);
297 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
298 // error.
299 if (FilenameStart == 0)
300 return;
301
302 // Search include directories for this file.
303 const DirectoryLookup *CurDir;
304 const FileEntry *File = LookupFile(FilenameStart, FilenameEnd,
305 isAngled, 0, CurDir);
Chris Lattnerb64ff5b2008-11-18 08:02:48 +0000306 if (File == 0) {
307 Diag(FilenameTok, diag::err_pp_file_not_found)
308 << std::string(FilenameStart, FilenameEnd);
309 return;
310 }
Chris Lattner4b009652007-07-25 00:24:17 +0000311
Chris Lattnerf4f776a2009-01-17 06:22:33 +0000312 const FileEntry *CurFile = getCurrentFileLexer()->getFileEntry();
Chris Lattner4b009652007-07-25 00:24:17 +0000313
314 // If this file is older than the file it depends on, emit a diagnostic.
315 if (CurFile && CurFile->getModificationTime() < File->getModificationTime()) {
316 // Lex tokens at the end of the message and include them in the message.
317 std::string Message;
318 Lex(DependencyTok);
Chris Lattnercb8e41c2007-10-09 18:02:16 +0000319 while (DependencyTok.isNot(tok::eom)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000320 Message += getSpelling(DependencyTok) + " ";
321 Lex(DependencyTok);
322 }
323
324 Message.erase(Message.end()-1);
Chris Lattnerb64ff5b2008-11-18 08:02:48 +0000325 Diag(FilenameTok, diag::pp_out_of_date_dependency) << Message;
Chris Lattner4b009652007-07-25 00:24:17 +0000326 }
327}
328
Chris Lattner146c5672009-01-16 08:21:25 +0000329/// HandlePragmaComment - Handle the microsoft #pragma comment extension. The
330/// syntax is:
331/// #pragma comment(linker, "foo")
332/// 'linker' is one of five identifiers: compiler, exestr, lib, linker, user.
333/// "foo" is a string, which is fully macro expanded, and permits string
Gabor Greifbe859462009-03-17 11:39:38 +0000334/// concatenation, embedded escape characters etc. See MSDN for more details.
Chris Lattner146c5672009-01-16 08:21:25 +0000335void Preprocessor::HandlePragmaComment(Token &Tok) {
336 SourceLocation CommentLoc = Tok.getLocation();
337 Lex(Tok);
338 if (Tok.isNot(tok::l_paren)) {
339 Diag(CommentLoc, diag::err_pragma_comment_malformed);
340 return;
341 }
342
343 // Read the identifier.
344 Lex(Tok);
345 if (Tok.isNot(tok::identifier)) {
346 Diag(CommentLoc, diag::err_pragma_comment_malformed);
347 return;
348 }
349
350 // Verify that this is one of the 5 whitelisted options.
351 // FIXME: warn that 'exestr' is deprecated.
352 const IdentifierInfo *II = Tok.getIdentifierInfo();
353 if (!II->isStr("compiler") && !II->isStr("exestr") && !II->isStr("lib") &&
354 !II->isStr("linker") && !II->isStr("user")) {
355 Diag(Tok.getLocation(), diag::err_pragma_comment_unknown_kind);
356 return;
357 }
358
Chris Lattner1055b4f2009-01-16 18:59:23 +0000359 // Read the optional string if present.
Chris Lattner146c5672009-01-16 08:21:25 +0000360 Lex(Tok);
Chris Lattner1055b4f2009-01-16 18:59:23 +0000361 std::string ArgumentString;
Chris Lattner146c5672009-01-16 08:21:25 +0000362 if (Tok.is(tok::comma)) {
Chris Lattner1055b4f2009-01-16 18:59:23 +0000363 Lex(Tok); // eat the comma.
Chris Lattner146c5672009-01-16 08:21:25 +0000364
365 // We need at least one string.
Chris Lattner5dd80062009-04-19 23:16:58 +0000366 if (Tok.isNot(tok::string_literal)) {
Chris Lattner146c5672009-01-16 08:21:25 +0000367 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
368 return;
369 }
370
371 // String concatenation allows multiple strings, which can even come from
372 // macro expansion.
373 // "foo " "bar" "Baz"
Chris Lattner1055b4f2009-01-16 18:59:23 +0000374 llvm::SmallVector<Token, 4> StrToks;
Chris Lattner5dd80062009-04-19 23:16:58 +0000375 while (Tok.is(tok::string_literal)) {
Chris Lattner1055b4f2009-01-16 18:59:23 +0000376 StrToks.push_back(Tok);
Chris Lattner146c5672009-01-16 08:21:25 +0000377 Lex(Tok);
Chris Lattner1055b4f2009-01-16 18:59:23 +0000378 }
379
380 // Concatenate and parse the strings.
381 StringLiteralParser Literal(&StrToks[0], StrToks.size(), *this);
382 assert(!Literal.AnyWide && "Didn't allow wide strings in");
383 if (Literal.hadError)
384 return;
385 if (Literal.Pascal) {
386 Diag(StrToks[0].getLocation(), diag::err_pragma_comment_malformed);
387 return;
388 }
389
390 ArgumentString = std::string(Literal.GetString(),
391 Literal.GetString()+Literal.GetStringLength());
Chris Lattner146c5672009-01-16 08:21:25 +0000392 }
393
Chris Lattner1055b4f2009-01-16 18:59:23 +0000394 // FIXME: If the kind is "compiler" warn if the string is present (it is
395 // ignored).
396 // FIXME: 'lib' requires a comment string.
397 // FIXME: 'linker' requires a comment string, and has a specific list of
398 // things that are allowable.
399
Chris Lattner146c5672009-01-16 08:21:25 +0000400 if (Tok.isNot(tok::r_paren)) {
401 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
402 return;
403 }
Chris Lattner1055b4f2009-01-16 18:59:23 +0000404 Lex(Tok); // eat the r_paren.
Chris Lattner146c5672009-01-16 08:21:25 +0000405
406 if (Tok.isNot(tok::eom)) {
407 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
408 return;
409 }
Chris Lattner1055b4f2009-01-16 18:59:23 +0000410
411 // If the pragma is lexically sound, notify any interested PPCallbacks.
Chris Lattner64448002009-01-16 19:01:46 +0000412 if (Callbacks)
413 Callbacks->PragmaComment(CommentLoc, II, ArgumentString);
Chris Lattner146c5672009-01-16 08:21:25 +0000414}
415
416
417
Chris Lattner4b009652007-07-25 00:24:17 +0000418
419/// AddPragmaHandler - Add the specified pragma handler to the preprocessor.
420/// If 'Namespace' is non-null, then it is a token required to exist on the
421/// pragma line before the pragma string starts, e.g. "STDC" or "GCC".
422void Preprocessor::AddPragmaHandler(const char *Namespace,
423 PragmaHandler *Handler) {
424 PragmaNamespace *InsertNS = PragmaHandlers;
425
426 // If this is specified to be in a namespace, step down into it.
427 if (Namespace) {
428 IdentifierInfo *NSID = getIdentifierInfo(Namespace);
429
430 // If there is already a pragma handler with the name of this namespace,
431 // we either have an error (directive with the same name as a namespace) or
432 // we already have the namespace to insert into.
433 if (PragmaHandler *Existing = PragmaHandlers->FindHandler(NSID)) {
434 InsertNS = Existing->getIfNamespace();
435 assert(InsertNS != 0 && "Cannot have a pragma namespace and pragma"
436 " handler with the same name!");
437 } else {
438 // Otherwise, this namespace doesn't exist yet, create and insert the
439 // handler for it.
440 InsertNS = new PragmaNamespace(NSID);
441 PragmaHandlers->AddPragma(InsertNS);
442 }
443 }
444
445 // Check to make sure we don't already have a pragma for this identifier.
446 assert(!InsertNS->FindHandler(Handler->getName()) &&
447 "Pragma handler already exists for this identifier!");
448 InsertNS->AddPragma(Handler);
449}
450
Daniel Dunbara65c00a2008-10-04 19:17:46 +0000451/// RemovePragmaHandler - Remove the specific pragma handler from the
452/// preprocessor. If \arg Namespace is non-null, then it should be the
453/// namespace that \arg Handler was added to. It is an error to remove
454/// a handler that has not been registered.
455void Preprocessor::RemovePragmaHandler(const char *Namespace,
456 PragmaHandler *Handler) {
457 PragmaNamespace *NS = PragmaHandlers;
458
459 // If this is specified to be in a namespace, step down into it.
460 if (Namespace) {
461 IdentifierInfo *NSID = getIdentifierInfo(Namespace);
462 PragmaHandler *Existing = PragmaHandlers->FindHandler(NSID);
463 assert(Existing && "Namespace containing handler does not exist!");
464
465 NS = Existing->getIfNamespace();
466 assert(NS && "Invalid namespace, registered as a regular pragma handler!");
467 }
468
469 NS->RemovePragmaHandler(Handler);
470
471 // If this is a non-default namespace and it is now empty, remove
472 // it.
473 if (NS != PragmaHandlers && NS->IsEmpty())
474 PragmaHandlers->RemovePragmaHandler(NS);
475}
476
Chris Lattner4b009652007-07-25 00:24:17 +0000477namespace {
Chris Lattnerc0f6b222007-12-19 19:38:36 +0000478/// PragmaOnceHandler - "#pragma once" marks the file as atomically included.
Chris Lattner4b009652007-07-25 00:24:17 +0000479struct PragmaOnceHandler : public PragmaHandler {
480 PragmaOnceHandler(const IdentifierInfo *OnceID) : PragmaHandler(OnceID) {}
481 virtual void HandlePragma(Preprocessor &PP, Token &OnceTok) {
Chris Lattner1ac90d42009-04-14 05:07:49 +0000482 PP.CheckEndOfDirective("pragma once");
Chris Lattner4b009652007-07-25 00:24:17 +0000483 PP.HandlePragmaOnce(OnceTok);
484 }
485};
486
Chris Lattnerc0f6b222007-12-19 19:38:36 +0000487/// PragmaMarkHandler - "#pragma mark ..." is ignored by the compiler, and the
488/// rest of the line is not lexed.
489struct PragmaMarkHandler : public PragmaHandler {
490 PragmaMarkHandler(const IdentifierInfo *MarkID) : PragmaHandler(MarkID) {}
491 virtual void HandlePragma(Preprocessor &PP, Token &MarkTok) {
492 PP.HandlePragmaMark();
493 }
494};
495
496/// PragmaPoisonHandler - "#pragma poison x" marks x as not usable.
Chris Lattner4b009652007-07-25 00:24:17 +0000497struct PragmaPoisonHandler : public PragmaHandler {
498 PragmaPoisonHandler(const IdentifierInfo *ID) : PragmaHandler(ID) {}
499 virtual void HandlePragma(Preprocessor &PP, Token &PoisonTok) {
500 PP.HandlePragmaPoison(PoisonTok);
501 }
502};
503
Chris Lattnerc0f6b222007-12-19 19:38:36 +0000504/// PragmaSystemHeaderHandler - "#pragma system_header" marks the current file
505/// as a system header, which silences warnings in it.
Chris Lattner4b009652007-07-25 00:24:17 +0000506struct PragmaSystemHeaderHandler : public PragmaHandler {
507 PragmaSystemHeaderHandler(const IdentifierInfo *ID) : PragmaHandler(ID) {}
508 virtual void HandlePragma(Preprocessor &PP, Token &SHToken) {
509 PP.HandlePragmaSystemHeader(SHToken);
Chris Lattner1ac90d42009-04-14 05:07:49 +0000510 PP.CheckEndOfDirective("pragma");
Chris Lattner4b009652007-07-25 00:24:17 +0000511 }
512};
513struct PragmaDependencyHandler : public PragmaHandler {
514 PragmaDependencyHandler(const IdentifierInfo *ID) : PragmaHandler(ID) {}
515 virtual void HandlePragma(Preprocessor &PP, Token &DepToken) {
516 PP.HandlePragmaDependency(DepToken);
517 }
518};
Chris Lattner146c5672009-01-16 08:21:25 +0000519
Chris Lattner5dd80062009-04-19 23:16:58 +0000520/// PragmaDiagnosticHandler - e.g. '#pragma GCC diagnostic ignored "-Wformat"'
521struct PragmaDiagnosticHandler : public PragmaHandler {
522 PragmaDiagnosticHandler(const IdentifierInfo *ID) : PragmaHandler(ID) {}
523 virtual void HandlePragma(Preprocessor &PP, Token &DiagToken) {
524 Token Tok;
525 PP.LexUnexpandedToken(Tok);
526 if (Tok.isNot(tok::identifier)) {
527 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
528 return;
529 }
530 IdentifierInfo *II = Tok.getIdentifierInfo();
531
532 diag::Mapping Map;
533 if (II->isStr("warning"))
534 Map = diag::MAP_WARNING;
535 else if (II->isStr("error"))
536 Map = diag::MAP_ERROR;
537 else if (II->isStr("ignored"))
538 Map = diag::MAP_IGNORE;
539 else if (II->isStr("fatal"))
540 Map = diag::MAP_FATAL;
541 else {
542 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
543 return;
544 }
545
546 PP.LexUnexpandedToken(Tok);
547
548 // We need at least one string.
549 if (Tok.isNot(tok::string_literal)) {
550 PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token);
551 return;
552 }
553
554 // String concatenation allows multiple strings, which can even come from
555 // macro expansion.
556 // "foo " "bar" "Baz"
557 llvm::SmallVector<Token, 4> StrToks;
558 while (Tok.is(tok::string_literal)) {
559 StrToks.push_back(Tok);
560 PP.LexUnexpandedToken(Tok);
561 }
562
563 if (Tok.isNot(tok::eom)) {
564 PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token);
565 return;
566 }
567
568 // Concatenate and parse the strings.
569 StringLiteralParser Literal(&StrToks[0], StrToks.size(), PP);
570 assert(!Literal.AnyWide && "Didn't allow wide strings in");
571 if (Literal.hadError)
572 return;
573 if (Literal.Pascal) {
574 PP.Diag(StrToks[0].getLocation(), diag::warn_pragma_diagnostic_invalid);
575 return;
576 }
577
578 std::string WarningName(Literal.GetString(),
579 Literal.GetString()+Literal.GetStringLength());
580
581 if (WarningName.size() < 3 || WarningName[0] != '-' ||
582 WarningName[1] != 'W') {
583 PP.Diag(StrToks[0].getLocation(),
584 diag::warn_pragma_diagnostic_invalid_option);
585 return;
586 }
587
588 if (PP.getDiagnostics().setDiagnosticGroupMapping(WarningName.c_str()+2,
589 Map))
590 PP.Diag(StrToks[0].getLocation(),
591 diag::warn_pragma_diagnostic_unknown_warning) << WarningName;
592 }
593};
594
Chris Lattner146c5672009-01-16 08:21:25 +0000595/// PragmaCommentHandler - "#pragma comment ...".
596struct PragmaCommentHandler : public PragmaHandler {
597 PragmaCommentHandler(const IdentifierInfo *ID) : PragmaHandler(ID) {}
598 virtual void HandlePragma(Preprocessor &PP, Token &CommentTok) {
599 PP.HandlePragmaComment(CommentTok);
600 }
601};
Chris Lattner0edd5432009-04-19 21:20:35 +0000602
603// Pragma STDC implementations.
Chris Lattner6f7bb262009-04-19 21:50:08 +0000604
605enum STDCSetting {
606 STDC_ON, STDC_OFF, STDC_DEFAULT, STDC_INVALID
607};
608
609static STDCSetting LexOnOffSwitch(Preprocessor &PP) {
610 Token Tok;
611 PP.LexUnexpandedToken(Tok);
612
613 if (Tok.isNot(tok::identifier)) {
614 PP.Diag(Tok, diag::ext_stdc_pragma_syntax);
615 return STDC_INVALID;
616 }
617 IdentifierInfo *II = Tok.getIdentifierInfo();
618 STDCSetting Result;
619 if (II->isStr("ON"))
620 Result = STDC_ON;
621 else if (II->isStr("OFF"))
622 Result = STDC_OFF;
623 else if (II->isStr("DEFAULT"))
624 Result = STDC_DEFAULT;
625 else {
626 PP.Diag(Tok, diag::ext_stdc_pragma_syntax);
627 return STDC_INVALID;
628 }
629
630 // Verify that this is followed by EOM.
631 PP.LexUnexpandedToken(Tok);
632 if (Tok.isNot(tok::eom))
633 PP.Diag(Tok, diag::ext_stdc_pragma_syntax_eom);
634 return Result;
635}
Chris Lattner0edd5432009-04-19 21:20:35 +0000636
637/// PragmaSTDC_FP_CONTRACTHandler - "#pragma STDC FP_CONTRACT ...".
638struct PragmaSTDC_FP_CONTRACTHandler : public PragmaHandler {
639 PragmaSTDC_FP_CONTRACTHandler(const IdentifierInfo *ID) : PragmaHandler(ID) {}
Chris Lattnercc848422009-04-19 21:25:37 +0000640 virtual void HandlePragma(Preprocessor &PP, Token &Tok) {
Chris Lattner6f7bb262009-04-19 21:50:08 +0000641 // We just ignore the setting of FP_CONTRACT. Since we don't do contractions
642 // at all, our default is OFF and setting it to ON is an optimization hint
643 // we can safely ignore. When we support -ffma or something, we would need
644 // to diagnose that we are ignoring FMA.
645 LexOnOffSwitch(PP);
Chris Lattner0edd5432009-04-19 21:20:35 +0000646 }
647};
648
649/// PragmaSTDC_FENV_ACCESSHandler - "#pragma STDC FENV_ACCESS ...".
650struct PragmaSTDC_FENV_ACCESSHandler : public PragmaHandler {
651 PragmaSTDC_FENV_ACCESSHandler(const IdentifierInfo *ID) : PragmaHandler(ID) {}
Chris Lattnercc848422009-04-19 21:25:37 +0000652 virtual void HandlePragma(Preprocessor &PP, Token &Tok) {
Chris Lattnerfa661e02009-04-19 21:55:32 +0000653 if (LexOnOffSwitch(PP) == STDC_ON)
654 PP.Diag(Tok, diag::warn_stdc_fenv_access_not_supported);
Chris Lattner0edd5432009-04-19 21:20:35 +0000655 }
656};
657
658/// PragmaSTDC_CX_LIMITED_RANGEHandler - "#pragma STDC CX_LIMITED_RANGE ...".
659struct PragmaSTDC_CX_LIMITED_RANGEHandler : public PragmaHandler {
660 PragmaSTDC_CX_LIMITED_RANGEHandler(const IdentifierInfo *ID)
661 : PragmaHandler(ID) {}
Chris Lattnercc848422009-04-19 21:25:37 +0000662 virtual void HandlePragma(Preprocessor &PP, Token &Tok) {
Chris Lattner6f7bb262009-04-19 21:50:08 +0000663 LexOnOffSwitch(PP);
Chris Lattner0edd5432009-04-19 21:20:35 +0000664 }
665};
666
667/// PragmaSTDC_UnknownHandler - "#pragma STDC ...".
668struct PragmaSTDC_UnknownHandler : public PragmaHandler {
669 PragmaSTDC_UnknownHandler() : PragmaHandler(0) {}
Chris Lattnercc848422009-04-19 21:25:37 +0000670 virtual void HandlePragma(Preprocessor &PP, Token &UnknownTok) {
Chris Lattner6f7bb262009-04-19 21:50:08 +0000671 // C99 6.10.6p2, unknown forms are not allowed.
Chris Lattnercc848422009-04-19 21:25:37 +0000672 PP.Diag(UnknownTok, diag::ext_stdc_pragma_ignored);
Chris Lattner0edd5432009-04-19 21:20:35 +0000673 }
674};
675
Chris Lattner4b009652007-07-25 00:24:17 +0000676} // end anonymous namespace
677
678
679/// RegisterBuiltinPragmas - Install the standard preprocessor pragmas:
680/// #pragma GCC poison/system_header/dependency and #pragma once.
681void Preprocessor::RegisterBuiltinPragmas() {
682 AddPragmaHandler(0, new PragmaOnceHandler(getIdentifierInfo("once")));
Chris Lattnerc0f6b222007-12-19 19:38:36 +0000683 AddPragmaHandler(0, new PragmaMarkHandler(getIdentifierInfo("mark")));
Chris Lattner53b0aae2009-05-12 18:21:11 +0000684
685 // #pragma GCC ...
Chris Lattner4b009652007-07-25 00:24:17 +0000686 AddPragmaHandler("GCC", new PragmaPoisonHandler(getIdentifierInfo("poison")));
687 AddPragmaHandler("GCC", new PragmaSystemHeaderHandler(
688 getIdentifierInfo("system_header")));
689 AddPragmaHandler("GCC", new PragmaDependencyHandler(
690 getIdentifierInfo("dependency")));
Chris Lattner5dd80062009-04-19 23:16:58 +0000691 AddPragmaHandler("GCC", new PragmaDiagnosticHandler(
692 getIdentifierInfo("diagnostic")));
Chris Lattner53b0aae2009-05-12 18:21:11 +0000693 // #pragma clang ...
694 AddPragmaHandler("clang", new PragmaPoisonHandler(
695 getIdentifierInfo("poison")));
696 AddPragmaHandler("clang", new PragmaSystemHeaderHandler(
697 getIdentifierInfo("system_header")));
698 AddPragmaHandler("clang", new PragmaDependencyHandler(
699 getIdentifierInfo("dependency")));
700 AddPragmaHandler("clang", new PragmaDiagnosticHandler(
701 getIdentifierInfo("diagnostic")));
702
Chris Lattner0edd5432009-04-19 21:20:35 +0000703 AddPragmaHandler("STDC", new PragmaSTDC_FP_CONTRACTHandler(
704 getIdentifierInfo("FP_CONTRACT")));
705 AddPragmaHandler("STDC", new PragmaSTDC_FENV_ACCESSHandler(
706 getIdentifierInfo("FENV_ACCESS")));
707 AddPragmaHandler("STDC", new PragmaSTDC_CX_LIMITED_RANGEHandler(
708 getIdentifierInfo("CX_LIMITED_RANGE")));
709 AddPragmaHandler("STDC", new PragmaSTDC_UnknownHandler());
710
Chris Lattner146c5672009-01-16 08:21:25 +0000711 // MS extensions.
712 if (Features.Microsoft)
713 AddPragmaHandler(0, new PragmaCommentHandler(getIdentifierInfo("comment")));
Chris Lattner4b009652007-07-25 00:24:17 +0000714}