blob: 60877743531d3c857d1b673825d059105db27b6a [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"
19#include "clang/Basic/Diagnostic.h"
20#include "clang/Basic/FileManager.h"
21#include "clang/Basic/SourceManager.h"
Chris Lattner4b009652007-07-25 00:24:17 +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 Dunbara65c00a2008-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 Lattner4b009652007-07-25 00:24:17 +000066void PragmaNamespace::HandlePragma(Preprocessor &PP, Token &Tok) {
67 // 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);
73 if (Handler == 0) return;
74
75 // Otherwise, pass it down.
76 Handler->HandlePragma(PP, Tok);
77}
78
79//===----------------------------------------------------------------------===//
80// Preprocessor Pragma Directive Handling.
81//===----------------------------------------------------------------------===//
82
83/// HandlePragmaDirective - The "#pragma" directive has been parsed. Lex the
84/// rest of the pragma, passing it to the registered pragma handlers.
85void Preprocessor::HandlePragmaDirective() {
86 ++NumPragma;
87
88 // Invoke the first level of pragma handlers which reads the namespace id.
89 Token Tok;
90 PragmaHandlers->HandlePragma(*this, Tok);
91
92 // If the pragma handler didn't read the rest of the line, consume it now.
Ted Kremenek31dd0262008-11-18 01:12:54 +000093 if (CurPPLexer->ParsingPreprocessorDirective)
Chris Lattner4b009652007-07-25 00:24:17 +000094 DiscardUntilEndOfDirective();
95}
96
97/// Handle_Pragma - Read a _Pragma directive, slice it up, process it, then
98/// return the first token after the directive. The _Pragma token has just
99/// been read into 'Tok'.
100void Preprocessor::Handle_Pragma(Token &Tok) {
101 // Remember the pragma token location.
102 SourceLocation PragmaLoc = Tok.getLocation();
103
104 // Read the '('.
105 Lex(Tok);
Chris Lattner0370d6b2008-11-18 07:59:24 +0000106 if (Tok.isNot(tok::l_paren)) {
107 Diag(PragmaLoc, diag::err__Pragma_malformed);
108 return;
109 }
Chris Lattner4b009652007-07-25 00:24:17 +0000110
111 // Read the '"..."'.
112 Lex(Tok);
Chris Lattner0370d6b2008-11-18 07:59:24 +0000113 if (Tok.isNot(tok::string_literal) && Tok.isNot(tok::wide_string_literal)) {
114 Diag(PragmaLoc, diag::err__Pragma_malformed);
115 return;
116 }
Chris Lattner4b009652007-07-25 00:24:17 +0000117
118 // Remember the string.
119 std::string StrVal = getSpelling(Tok);
120 SourceLocation StrLoc = Tok.getLocation();
121
122 // Read the ')'.
123 Lex(Tok);
Chris Lattner0370d6b2008-11-18 07:59:24 +0000124 if (Tok.isNot(tok::r_paren)) {
125 Diag(PragmaLoc, diag::err__Pragma_malformed);
126 return;
127 }
Chris Lattner4b009652007-07-25 00:24:17 +0000128
Chris Lattner1055b4f2009-01-16 18:59:23 +0000129 // The _Pragma is lexically sound. Destringize according to C99 6.10.9.1:
130 // "The string literal is destringized by deleting the L prefix, if present,
131 // deleting the leading and trailing double-quotes, replacing each escape
132 // sequence \" by a double-quote, and replacing each escape sequence \\ by a
133 // single backslash."
Chris Lattner4b009652007-07-25 00:24:17 +0000134 if (StrVal[0] == 'L') // Remove L prefix.
135 StrVal.erase(StrVal.begin());
136 assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
137 "Invalid string token!");
138
139 // Remove the front quote, replacing it with a space, so that the pragma
140 // contents appear to have a space before them.
141 StrVal[0] = ' ';
142
143 // Replace the terminating quote with a \n\0.
144 StrVal[StrVal.size()-1] = '\n';
145 StrVal += '\0';
146
147 // Remove escaped quotes and escapes.
148 for (unsigned i = 0, e = StrVal.size(); i != e-1; ++i) {
149 if (StrVal[i] == '\\' &&
150 (StrVal[i+1] == '\\' || StrVal[i+1] == '"')) {
151 // \\ -> '\' and \" -> '"'.
152 StrVal.erase(StrVal.begin()+i);
153 --e;
154 }
155 }
156
157 // Plop the string (including the newline and trailing null) into a buffer
158 // where we can lex it.
159 SourceLocation TokLoc = CreateString(&StrVal[0], StrVal.size(), StrLoc);
160 const char *StrData = SourceMgr.getCharacterData(TokLoc);
161
162 // Make and enter a lexer object so that we lex and expand the tokens just
163 // like any others.
164 Lexer *TL = new Lexer(TokLoc, *this,
165 StrData, StrData+StrVal.size()-1 /* no null */);
166
167 // Ensure that the lexer thinks it is inside a directive, so that end \n will
168 // return an EOM token.
169 TL->ParsingPreprocessorDirective = true;
170
171 // This lexer really is for _Pragma.
172 TL->Is_PragmaLexer = true;
173
174 EnterSourceFileWithLexer(TL, 0);
175
176 // With everything set up, lex this as a #pragma directive.
177 HandlePragmaDirective();
178
179 // Finally, return whatever came after the pragma directive.
180 return Lex(Tok);
181}
182
183
184
185/// HandlePragmaOnce - Handle #pragma once. OnceTok is the 'once'.
186///
187void Preprocessor::HandlePragmaOnce(Token &OnceTok) {
188 if (isInPrimaryFile()) {
189 Diag(OnceTok, diag::pp_pragma_once_in_main_file);
190 return;
191 }
192
193 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
Ted Kremenek03467f62008-11-19 22:55:25 +0000194 unsigned FileID = getCurrentFileLexer()->getFileID();
Chris Lattner4b009652007-07-25 00:24:17 +0000195
196 // Mark the file as a once-only file now.
Ted Kremenek03467f62008-11-19 22:55:25 +0000197 HeaderInfo.MarkFileIncludeOnce(SourceMgr.getFileEntryForID(FileID));
Chris Lattner4b009652007-07-25 00:24:17 +0000198}
199
Chris Lattnerc0f6b222007-12-19 19:38:36 +0000200void Preprocessor::HandlePragmaMark() {
Ted Kremenekb53b1f42008-11-19 22:21:33 +0000201 assert(CurPPLexer && "No current lexer?");
202 if (CurLexer) CurLexer->ReadToEndOfLine();
203 else CurPTHLexer->DiscardToEndOfLine();
Chris Lattnerc0f6b222007-12-19 19:38:36 +0000204}
205
206
Chris Lattner4b009652007-07-25 00:24:17 +0000207/// HandlePragmaPoison - Handle #pragma GCC poison. PoisonTok is the 'poison'.
208///
209void Preprocessor::HandlePragmaPoison(Token &PoisonTok) {
210 Token Tok;
211
212 while (1) {
213 // Read the next token to poison. While doing this, pretend that we are
214 // skipping while reading the identifier to poison.
215 // This avoids errors on code like:
216 // #pragma GCC poison X
217 // #pragma GCC poison X
Ted Kremenek31dd0262008-11-18 01:12:54 +0000218 if (CurPPLexer) CurPPLexer->LexingRawMode = true;
Chris Lattner4b009652007-07-25 00:24:17 +0000219 LexUnexpandedToken(Tok);
Ted Kremenek31dd0262008-11-18 01:12:54 +0000220 if (CurPPLexer) CurPPLexer->LexingRawMode = false;
Chris Lattner4b009652007-07-25 00:24:17 +0000221
222 // If we reached the end of line, we're done.
Chris Lattnercb8e41c2007-10-09 18:02:16 +0000223 if (Tok.is(tok::eom)) return;
Chris Lattner4b009652007-07-25 00:24:17 +0000224
225 // Can only poison identifiers.
Chris Lattnercb8e41c2007-10-09 18:02:16 +0000226 if (Tok.isNot(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000227 Diag(Tok, diag::err_pp_invalid_poison);
228 return;
229 }
230
231 // Look up the identifier info for the token. We disabled identifier lookup
232 // by saying we're skipping contents, so we need to do this manually.
233 IdentifierInfo *II = LookUpIdentifierInfo(Tok);
234
235 // Already poisoned.
236 if (II->isPoisoned()) continue;
237
238 // If this is a macro identifier, emit a warning.
Chris Lattner3b56a012007-10-07 08:04:56 +0000239 if (II->hasMacroDefinition())
Chris Lattner4b009652007-07-25 00:24:17 +0000240 Diag(Tok, diag::pp_poisoning_existing_macro);
241
242 // Finally, poison it!
243 II->setIsPoisoned();
244 }
245}
246
247/// HandlePragmaSystemHeader - Implement #pragma GCC system_header. We know
248/// that the whole directive has been parsed.
249void Preprocessor::HandlePragmaSystemHeader(Token &SysHeaderTok) {
250 if (isInPrimaryFile()) {
251 Diag(SysHeaderTok, diag::pp_pragma_sysheader_in_main_file);
252 return;
253 }
254
255 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
Ted Kremenekf1b062a2008-11-20 01:45:11 +0000256 PreprocessorLexer *TheLexer = getCurrentFileLexer();
Chris Lattner4b009652007-07-25 00:24:17 +0000257
258 // Mark the file as a system header.
Ted Kremenek03467f62008-11-19 22:55:25 +0000259 const FileEntry *File = SourceMgr.getFileEntryForID(TheLexer->getFileID());
Chris Lattner4b009652007-07-25 00:24:17 +0000260 HeaderInfo.MarkFileSystemHeader(File);
261
262 // Notify the client, if desired, that we are in a new source file.
263 if (Callbacks)
Ted Kremenekf1b062a2008-11-20 01:45:11 +0000264 Callbacks->FileChanged(SysHeaderTok.getLocation(),
Chris Lattner6f044062008-09-26 21:18:42 +0000265 PPCallbacks::SystemHeaderPragma, SrcMgr::C_System);
Chris Lattner4b009652007-07-25 00:24:17 +0000266}
267
268/// HandlePragmaDependency - Handle #pragma GCC dependency "foo" blah.
269///
270void Preprocessor::HandlePragmaDependency(Token &DependencyTok) {
271 Token FilenameTok;
Ted Kremenek31dd0262008-11-18 01:12:54 +0000272 CurPPLexer->LexIncludeFilename(FilenameTok);
Chris Lattner4b009652007-07-25 00:24:17 +0000273
274 // If the token kind is EOM, the error has already been diagnosed.
Chris Lattnercb8e41c2007-10-09 18:02:16 +0000275 if (FilenameTok.is(tok::eom))
Chris Lattner4b009652007-07-25 00:24:17 +0000276 return;
277
278 // Reserve a buffer to get the spelling.
279 llvm::SmallVector<char, 128> FilenameBuffer;
280 FilenameBuffer.resize(FilenameTok.getLength());
281
282 const char *FilenameStart = &FilenameBuffer[0];
283 unsigned Len = getSpelling(FilenameTok, FilenameStart);
284 const char *FilenameEnd = FilenameStart+Len;
285 bool isAngled = GetIncludeFilenameSpelling(FilenameTok.getLocation(),
286 FilenameStart, FilenameEnd);
287 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
288 // error.
289 if (FilenameStart == 0)
290 return;
291
292 // Search include directories for this file.
293 const DirectoryLookup *CurDir;
294 const FileEntry *File = LookupFile(FilenameStart, FilenameEnd,
295 isAngled, 0, CurDir);
Chris Lattnerb64ff5b2008-11-18 08:02:48 +0000296 if (File == 0) {
297 Diag(FilenameTok, diag::err_pp_file_not_found)
298 << std::string(FilenameStart, FilenameEnd);
299 return;
300 }
Chris Lattner4b009652007-07-25 00:24:17 +0000301
Ted Kremenek03467f62008-11-19 22:55:25 +0000302 unsigned FileID = getCurrentFileLexer()->getFileID();
303 const FileEntry *CurFile = SourceMgr.getFileEntryForID(FileID);
Chris Lattner4b009652007-07-25 00:24:17 +0000304
305 // If this file is older than the file it depends on, emit a diagnostic.
306 if (CurFile && CurFile->getModificationTime() < File->getModificationTime()) {
307 // Lex tokens at the end of the message and include them in the message.
308 std::string Message;
309 Lex(DependencyTok);
Chris Lattnercb8e41c2007-10-09 18:02:16 +0000310 while (DependencyTok.isNot(tok::eom)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000311 Message += getSpelling(DependencyTok) + " ";
312 Lex(DependencyTok);
313 }
314
315 Message.erase(Message.end()-1);
Chris Lattnerb64ff5b2008-11-18 08:02:48 +0000316 Diag(FilenameTok, diag::pp_out_of_date_dependency) << Message;
Chris Lattner4b009652007-07-25 00:24:17 +0000317 }
318}
319
Chris Lattner146c5672009-01-16 08:21:25 +0000320/// HandlePragmaComment - Handle the microsoft #pragma comment extension. The
321/// syntax is:
322/// #pragma comment(linker, "foo")
323/// 'linker' is one of five identifiers: compiler, exestr, lib, linker, user.
324/// "foo" is a string, which is fully macro expanded, and permits string
325/// concatenation, embeded escape characters etc. See MSDN for more details.
326void Preprocessor::HandlePragmaComment(Token &Tok) {
327 SourceLocation CommentLoc = Tok.getLocation();
328 Lex(Tok);
329 if (Tok.isNot(tok::l_paren)) {
330 Diag(CommentLoc, diag::err_pragma_comment_malformed);
331 return;
332 }
333
334 // Read the identifier.
335 Lex(Tok);
336 if (Tok.isNot(tok::identifier)) {
337 Diag(CommentLoc, diag::err_pragma_comment_malformed);
338 return;
339 }
340
341 // Verify that this is one of the 5 whitelisted options.
342 // FIXME: warn that 'exestr' is deprecated.
343 const IdentifierInfo *II = Tok.getIdentifierInfo();
344 if (!II->isStr("compiler") && !II->isStr("exestr") && !II->isStr("lib") &&
345 !II->isStr("linker") && !II->isStr("user")) {
346 Diag(Tok.getLocation(), diag::err_pragma_comment_unknown_kind);
347 return;
348 }
349
Chris Lattner1055b4f2009-01-16 18:59:23 +0000350 // Read the optional string if present.
Chris Lattner146c5672009-01-16 08:21:25 +0000351 Lex(Tok);
Chris Lattner1055b4f2009-01-16 18:59:23 +0000352 std::string ArgumentString;
Chris Lattner146c5672009-01-16 08:21:25 +0000353 if (Tok.is(tok::comma)) {
Chris Lattner1055b4f2009-01-16 18:59:23 +0000354 Lex(Tok); // eat the comma.
Chris Lattner146c5672009-01-16 08:21:25 +0000355
356 // We need at least one string.
357 if (Tok.getKind() != tok::string_literal) {
358 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
359 return;
360 }
361
362 // String concatenation allows multiple strings, which can even come from
363 // macro expansion.
364 // "foo " "bar" "Baz"
Chris Lattner1055b4f2009-01-16 18:59:23 +0000365 llvm::SmallVector<Token, 4> StrToks;
366 while (Tok.getKind() == tok::string_literal) {
367 StrToks.push_back(Tok);
Chris Lattner146c5672009-01-16 08:21:25 +0000368 Lex(Tok);
Chris Lattner1055b4f2009-01-16 18:59:23 +0000369 }
370
371 // Concatenate and parse the strings.
372 StringLiteralParser Literal(&StrToks[0], StrToks.size(), *this);
373 assert(!Literal.AnyWide && "Didn't allow wide strings in");
374 if (Literal.hadError)
375 return;
376 if (Literal.Pascal) {
377 Diag(StrToks[0].getLocation(), diag::err_pragma_comment_malformed);
378 return;
379 }
380
381 ArgumentString = std::string(Literal.GetString(),
382 Literal.GetString()+Literal.GetStringLength());
Chris Lattner146c5672009-01-16 08:21:25 +0000383 }
384
Chris Lattner1055b4f2009-01-16 18:59:23 +0000385 // FIXME: If the kind is "compiler" warn if the string is present (it is
386 // ignored).
387 // FIXME: 'lib' requires a comment string.
388 // FIXME: 'linker' requires a comment string, and has a specific list of
389 // things that are allowable.
390
Chris Lattner146c5672009-01-16 08:21:25 +0000391 if (Tok.isNot(tok::r_paren)) {
392 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
393 return;
394 }
Chris Lattner1055b4f2009-01-16 18:59:23 +0000395 Lex(Tok); // eat the r_paren.
Chris Lattner146c5672009-01-16 08:21:25 +0000396
397 if (Tok.isNot(tok::eom)) {
398 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
399 return;
400 }
Chris Lattner1055b4f2009-01-16 18:59:23 +0000401
402 // If the pragma is lexically sound, notify any interested PPCallbacks.
403 Callbacks->PragmaComment(CommentLoc, II, ArgumentString);
Chris Lattner146c5672009-01-16 08:21:25 +0000404}
405
406
407
Chris Lattner4b009652007-07-25 00:24:17 +0000408
409/// AddPragmaHandler - Add the specified pragma handler to the preprocessor.
410/// If 'Namespace' is non-null, then it is a token required to exist on the
411/// pragma line before the pragma string starts, e.g. "STDC" or "GCC".
412void Preprocessor::AddPragmaHandler(const char *Namespace,
413 PragmaHandler *Handler) {
414 PragmaNamespace *InsertNS = PragmaHandlers;
415
416 // If this is specified to be in a namespace, step down into it.
417 if (Namespace) {
418 IdentifierInfo *NSID = getIdentifierInfo(Namespace);
419
420 // If there is already a pragma handler with the name of this namespace,
421 // we either have an error (directive with the same name as a namespace) or
422 // we already have the namespace to insert into.
423 if (PragmaHandler *Existing = PragmaHandlers->FindHandler(NSID)) {
424 InsertNS = Existing->getIfNamespace();
425 assert(InsertNS != 0 && "Cannot have a pragma namespace and pragma"
426 " handler with the same name!");
427 } else {
428 // Otherwise, this namespace doesn't exist yet, create and insert the
429 // handler for it.
430 InsertNS = new PragmaNamespace(NSID);
431 PragmaHandlers->AddPragma(InsertNS);
432 }
433 }
434
435 // Check to make sure we don't already have a pragma for this identifier.
436 assert(!InsertNS->FindHandler(Handler->getName()) &&
437 "Pragma handler already exists for this identifier!");
438 InsertNS->AddPragma(Handler);
439}
440
Daniel Dunbara65c00a2008-10-04 19:17:46 +0000441/// RemovePragmaHandler - Remove the specific pragma handler from the
442/// preprocessor. If \arg Namespace is non-null, then it should be the
443/// namespace that \arg Handler was added to. It is an error to remove
444/// a handler that has not been registered.
445void Preprocessor::RemovePragmaHandler(const char *Namespace,
446 PragmaHandler *Handler) {
447 PragmaNamespace *NS = PragmaHandlers;
448
449 // If this is specified to be in a namespace, step down into it.
450 if (Namespace) {
451 IdentifierInfo *NSID = getIdentifierInfo(Namespace);
452 PragmaHandler *Existing = PragmaHandlers->FindHandler(NSID);
453 assert(Existing && "Namespace containing handler does not exist!");
454
455 NS = Existing->getIfNamespace();
456 assert(NS && "Invalid namespace, registered as a regular pragma handler!");
457 }
458
459 NS->RemovePragmaHandler(Handler);
460
461 // If this is a non-default namespace and it is now empty, remove
462 // it.
463 if (NS != PragmaHandlers && NS->IsEmpty())
464 PragmaHandlers->RemovePragmaHandler(NS);
465}
466
Chris Lattner4b009652007-07-25 00:24:17 +0000467namespace {
Chris Lattnerc0f6b222007-12-19 19:38:36 +0000468/// PragmaOnceHandler - "#pragma once" marks the file as atomically included.
Chris Lattner4b009652007-07-25 00:24:17 +0000469struct PragmaOnceHandler : public PragmaHandler {
470 PragmaOnceHandler(const IdentifierInfo *OnceID) : PragmaHandler(OnceID) {}
471 virtual void HandlePragma(Preprocessor &PP, Token &OnceTok) {
472 PP.CheckEndOfDirective("#pragma once");
473 PP.HandlePragmaOnce(OnceTok);
474 }
475};
476
Chris Lattnerc0f6b222007-12-19 19:38:36 +0000477/// PragmaMarkHandler - "#pragma mark ..." is ignored by the compiler, and the
478/// rest of the line is not lexed.
479struct PragmaMarkHandler : public PragmaHandler {
480 PragmaMarkHandler(const IdentifierInfo *MarkID) : PragmaHandler(MarkID) {}
481 virtual void HandlePragma(Preprocessor &PP, Token &MarkTok) {
482 PP.HandlePragmaMark();
483 }
484};
485
486/// PragmaPoisonHandler - "#pragma poison x" marks x as not usable.
Chris Lattner4b009652007-07-25 00:24:17 +0000487struct PragmaPoisonHandler : public PragmaHandler {
488 PragmaPoisonHandler(const IdentifierInfo *ID) : PragmaHandler(ID) {}
489 virtual void HandlePragma(Preprocessor &PP, Token &PoisonTok) {
490 PP.HandlePragmaPoison(PoisonTok);
491 }
492};
493
Chris Lattnerc0f6b222007-12-19 19:38:36 +0000494/// PragmaSystemHeaderHandler - "#pragma system_header" marks the current file
495/// as a system header, which silences warnings in it.
Chris Lattner4b009652007-07-25 00:24:17 +0000496struct PragmaSystemHeaderHandler : public PragmaHandler {
497 PragmaSystemHeaderHandler(const IdentifierInfo *ID) : PragmaHandler(ID) {}
498 virtual void HandlePragma(Preprocessor &PP, Token &SHToken) {
499 PP.HandlePragmaSystemHeader(SHToken);
500 PP.CheckEndOfDirective("#pragma");
501 }
502};
503struct PragmaDependencyHandler : public PragmaHandler {
504 PragmaDependencyHandler(const IdentifierInfo *ID) : PragmaHandler(ID) {}
505 virtual void HandlePragma(Preprocessor &PP, Token &DepToken) {
506 PP.HandlePragmaDependency(DepToken);
507 }
508};
Chris Lattner146c5672009-01-16 08:21:25 +0000509
510/// PragmaCommentHandler - "#pragma comment ...".
511struct PragmaCommentHandler : public PragmaHandler {
512 PragmaCommentHandler(const IdentifierInfo *ID) : PragmaHandler(ID) {}
513 virtual void HandlePragma(Preprocessor &PP, Token &CommentTok) {
514 PP.HandlePragmaComment(CommentTok);
515 }
516};
Chris Lattner4b009652007-07-25 00:24:17 +0000517} // end anonymous namespace
518
519
520/// RegisterBuiltinPragmas - Install the standard preprocessor pragmas:
521/// #pragma GCC poison/system_header/dependency and #pragma once.
522void Preprocessor::RegisterBuiltinPragmas() {
523 AddPragmaHandler(0, new PragmaOnceHandler(getIdentifierInfo("once")));
Chris Lattnerc0f6b222007-12-19 19:38:36 +0000524 AddPragmaHandler(0, new PragmaMarkHandler(getIdentifierInfo("mark")));
Chris Lattner4b009652007-07-25 00:24:17 +0000525 AddPragmaHandler("GCC", new PragmaPoisonHandler(getIdentifierInfo("poison")));
526 AddPragmaHandler("GCC", new PragmaSystemHeaderHandler(
527 getIdentifierInfo("system_header")));
528 AddPragmaHandler("GCC", new PragmaDependencyHandler(
529 getIdentifierInfo("dependency")));
Chris Lattner146c5672009-01-16 08:21:25 +0000530
531 // MS extensions.
532 if (Features.Microsoft)
533 AddPragmaHandler(0, new PragmaCommentHandler(getIdentifierInfo("comment")));
Chris Lattner4b009652007-07-25 00:24:17 +0000534}