blob: 87410f9ff4ab907071983067cdc916af342f8ad2 [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);
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.
Chris Lattnerd2177732007-07-20 16:59:19 +000089 Token Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +000090 PragmaHandlers->HandlePragma(*this, Tok);
91
92 // If the pragma handler didn't read the rest of the line, consume it now.
Ted Kremenek68a91d52008-11-18 01:12:54 +000093 if (CurPPLexer->ParsingPreprocessorDirective)
Reid Spencer5f016e22007-07-11 17:01:13 +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'.
Chris Lattnerd2177732007-07-20 16:59:19 +0000100void Preprocessor::Handle_Pragma(Token &Tok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000101 // Remember the pragma token location.
102 SourceLocation PragmaLoc = Tok.getLocation();
103
104 // Read the '('.
105 Lex(Tok);
Chris Lattner3692b092008-11-18 07:59:24 +0000106 if (Tok.isNot(tok::l_paren)) {
107 Diag(PragmaLoc, diag::err__Pragma_malformed);
108 return;
109 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000110
111 // Read the '"..."'.
112 Lex(Tok);
Chris Lattner3692b092008-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 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000117
118 // Remember the string.
119 std::string StrVal = getSpelling(Tok);
120 SourceLocation StrLoc = Tok.getLocation();
121
122 // Read the ')'.
123 Lex(Tok);
Chris Lattner3692b092008-11-18 07:59:24 +0000124 if (Tok.isNot(tok::r_paren)) {
125 Diag(PragmaLoc, diag::err__Pragma_malformed);
126 return;
127 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000128
Chris Lattnera9d91452009-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."
Reid Spencer5f016e22007-07-11 17:01:13 +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.
Chris Lattner47246be2009-01-26 19:29:26 +0000159 Token TmpTok;
160 TmpTok.startToken();
161 CreateString(&StrVal[0], StrVal.size(), TmpTok);
162 SourceLocation TokLoc = TmpTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000163
Reid Spencer5f016e22007-07-11 17:01:13 +0000164 // Make and enter a lexer object so that we lex and expand the tokens just
165 // like any others.
Chris Lattnerbcc2a672009-01-19 06:46:35 +0000166 Lexer *TL = Lexer::Create_PragmaLexer(TokLoc, StrLoc,
Chris Lattner42e00d12009-01-17 08:27:52 +0000167 // do not include the null in the count.
168 StrVal.size()-1, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000169
170 EnterSourceFileWithLexer(TL, 0);
171
172 // With everything set up, lex this as a #pragma directive.
173 HandlePragmaDirective();
174
175 // Finally, return whatever came after the pragma directive.
176 return Lex(Tok);
177}
178
179
180
181/// HandlePragmaOnce - Handle #pragma once. OnceTok is the 'once'.
182///
Chris Lattnerd2177732007-07-20 16:59:19 +0000183void Preprocessor::HandlePragmaOnce(Token &OnceTok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000184 if (isInPrimaryFile()) {
185 Diag(OnceTok, diag::pp_pragma_once_in_main_file);
186 return;
187 }
188
189 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
Reid Spencer5f016e22007-07-11 17:01:13 +0000190 // Mark the file as a once-only file now.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000191 HeaderInfo.MarkFileIncludeOnce(getCurrentFileLexer()->getFileEntry());
Reid Spencer5f016e22007-07-11 17:01:13 +0000192}
193
Chris Lattner22434492007-12-19 19:38:36 +0000194void Preprocessor::HandlePragmaMark() {
Ted Kremenek17ff58a2008-11-19 22:21:33 +0000195 assert(CurPPLexer && "No current lexer?");
196 if (CurLexer) CurLexer->ReadToEndOfLine();
197 else CurPTHLexer->DiscardToEndOfLine();
Chris Lattner22434492007-12-19 19:38:36 +0000198}
199
200
Reid Spencer5f016e22007-07-11 17:01:13 +0000201/// HandlePragmaPoison - Handle #pragma GCC poison. PoisonTok is the 'poison'.
202///
Chris Lattnerd2177732007-07-20 16:59:19 +0000203void Preprocessor::HandlePragmaPoison(Token &PoisonTok) {
204 Token Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000205
206 while (1) {
207 // Read the next token to poison. While doing this, pretend that we are
208 // skipping while reading the identifier to poison.
209 // This avoids errors on code like:
210 // #pragma GCC poison X
211 // #pragma GCC poison X
Ted Kremenek68a91d52008-11-18 01:12:54 +0000212 if (CurPPLexer) CurPPLexer->LexingRawMode = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000213 LexUnexpandedToken(Tok);
Ted Kremenek68a91d52008-11-18 01:12:54 +0000214 if (CurPPLexer) CurPPLexer->LexingRawMode = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000215
216 // If we reached the end of line, we're done.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000217 if (Tok.is(tok::eom)) return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000218
219 // Can only poison identifiers.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000220 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000221 Diag(Tok, diag::err_pp_invalid_poison);
222 return;
223 }
224
225 // Look up the identifier info for the token. We disabled identifier lookup
226 // by saying we're skipping contents, so we need to do this manually.
227 IdentifierInfo *II = LookUpIdentifierInfo(Tok);
228
229 // Already poisoned.
230 if (II->isPoisoned()) continue;
231
232 // If this is a macro identifier, emit a warning.
Chris Lattner0edde552007-10-07 08:04:56 +0000233 if (II->hasMacroDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000234 Diag(Tok, diag::pp_poisoning_existing_macro);
235
236 // Finally, poison it!
237 II->setIsPoisoned();
238 }
239}
240
241/// HandlePragmaSystemHeader - Implement #pragma GCC system_header. We know
242/// that the whole directive has been parsed.
Chris Lattnerd2177732007-07-20 16:59:19 +0000243void Preprocessor::HandlePragmaSystemHeader(Token &SysHeaderTok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000244 if (isInPrimaryFile()) {
245 Diag(SysHeaderTok, diag::pp_pragma_sysheader_in_main_file);
246 return;
247 }
248
249 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
Ted Kremenek35c10c22008-11-20 01:45:11 +0000250 PreprocessorLexer *TheLexer = getCurrentFileLexer();
Reid Spencer5f016e22007-07-11 17:01:13 +0000251
252 // Mark the file as a system header.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000253 HeaderInfo.MarkFileSystemHeader(TheLexer->getFileEntry());
Reid Spencer5f016e22007-07-11 17:01:13 +0000254
255 // Notify the client, if desired, that we are in a new source file.
256 if (Callbacks)
Ted Kremenek35c10c22008-11-20 01:45:11 +0000257 Callbacks->FileChanged(SysHeaderTok.getLocation(),
Chris Lattner0b9e7362008-09-26 21:18:42 +0000258 PPCallbacks::SystemHeaderPragma, SrcMgr::C_System);
Reid Spencer5f016e22007-07-11 17:01:13 +0000259}
260
261/// HandlePragmaDependency - Handle #pragma GCC dependency "foo" blah.
262///
Chris Lattnerd2177732007-07-20 16:59:19 +0000263void Preprocessor::HandlePragmaDependency(Token &DependencyTok) {
264 Token FilenameTok;
Ted Kremenek68a91d52008-11-18 01:12:54 +0000265 CurPPLexer->LexIncludeFilename(FilenameTok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000266
267 // If the token kind is EOM, the error has already been diagnosed.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000268 if (FilenameTok.is(tok::eom))
Reid Spencer5f016e22007-07-11 17:01:13 +0000269 return;
270
271 // Reserve a buffer to get the spelling.
272 llvm::SmallVector<char, 128> FilenameBuffer;
273 FilenameBuffer.resize(FilenameTok.getLength());
274
Chris Lattnerf1c99ac2007-07-23 04:15:27 +0000275 const char *FilenameStart = &FilenameBuffer[0];
276 unsigned Len = getSpelling(FilenameTok, FilenameStart);
277 const char *FilenameEnd = FilenameStart+Len;
278 bool isAngled = GetIncludeFilenameSpelling(FilenameTok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000279 FilenameStart, FilenameEnd);
280 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
281 // error.
282 if (FilenameStart == 0)
283 return;
284
285 // Search include directories for this file.
286 const DirectoryLookup *CurDir;
287 const FileEntry *File = LookupFile(FilenameStart, FilenameEnd,
288 isAngled, 0, CurDir);
Chris Lattner56b05c82008-11-18 08:02:48 +0000289 if (File == 0) {
290 Diag(FilenameTok, diag::err_pp_file_not_found)
291 << std::string(FilenameStart, FilenameEnd);
292 return;
293 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000294
Chris Lattner2b2453a2009-01-17 06:22:33 +0000295 const FileEntry *CurFile = getCurrentFileLexer()->getFileEntry();
Reid Spencer5f016e22007-07-11 17:01:13 +0000296
297 // If this file is older than the file it depends on, emit a diagnostic.
298 if (CurFile && CurFile->getModificationTime() < File->getModificationTime()) {
299 // Lex tokens at the end of the message and include them in the message.
300 std::string Message;
301 Lex(DependencyTok);
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000302 while (DependencyTok.isNot(tok::eom)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000303 Message += getSpelling(DependencyTok) + " ";
304 Lex(DependencyTok);
305 }
306
307 Message.erase(Message.end()-1);
Chris Lattner56b05c82008-11-18 08:02:48 +0000308 Diag(FilenameTok, diag::pp_out_of_date_dependency) << Message;
Reid Spencer5f016e22007-07-11 17:01:13 +0000309 }
310}
311
Chris Lattner636c5ef2009-01-16 08:21:25 +0000312/// HandlePragmaComment - Handle the microsoft #pragma comment extension. The
313/// syntax is:
314/// #pragma comment(linker, "foo")
315/// 'linker' is one of five identifiers: compiler, exestr, lib, linker, user.
316/// "foo" is a string, which is fully macro expanded, and permits string
317/// concatenation, embeded escape characters etc. See MSDN for more details.
318void Preprocessor::HandlePragmaComment(Token &Tok) {
319 SourceLocation CommentLoc = Tok.getLocation();
320 Lex(Tok);
321 if (Tok.isNot(tok::l_paren)) {
322 Diag(CommentLoc, diag::err_pragma_comment_malformed);
323 return;
324 }
325
326 // Read the identifier.
327 Lex(Tok);
328 if (Tok.isNot(tok::identifier)) {
329 Diag(CommentLoc, diag::err_pragma_comment_malformed);
330 return;
331 }
332
333 // Verify that this is one of the 5 whitelisted options.
334 // FIXME: warn that 'exestr' is deprecated.
335 const IdentifierInfo *II = Tok.getIdentifierInfo();
336 if (!II->isStr("compiler") && !II->isStr("exestr") && !II->isStr("lib") &&
337 !II->isStr("linker") && !II->isStr("user")) {
338 Diag(Tok.getLocation(), diag::err_pragma_comment_unknown_kind);
339 return;
340 }
341
Chris Lattnera9d91452009-01-16 18:59:23 +0000342 // Read the optional string if present.
Chris Lattner636c5ef2009-01-16 08:21:25 +0000343 Lex(Tok);
Chris Lattnera9d91452009-01-16 18:59:23 +0000344 std::string ArgumentString;
Chris Lattner636c5ef2009-01-16 08:21:25 +0000345 if (Tok.is(tok::comma)) {
Chris Lattnera9d91452009-01-16 18:59:23 +0000346 Lex(Tok); // eat the comma.
Chris Lattner636c5ef2009-01-16 08:21:25 +0000347
348 // We need at least one string.
349 if (Tok.getKind() != tok::string_literal) {
350 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
351 return;
352 }
353
354 // String concatenation allows multiple strings, which can even come from
355 // macro expansion.
356 // "foo " "bar" "Baz"
Chris Lattnera9d91452009-01-16 18:59:23 +0000357 llvm::SmallVector<Token, 4> StrToks;
358 while (Tok.getKind() == tok::string_literal) {
359 StrToks.push_back(Tok);
Chris Lattner636c5ef2009-01-16 08:21:25 +0000360 Lex(Tok);
Chris Lattnera9d91452009-01-16 18:59:23 +0000361 }
362
363 // Concatenate and parse the strings.
364 StringLiteralParser Literal(&StrToks[0], StrToks.size(), *this);
365 assert(!Literal.AnyWide && "Didn't allow wide strings in");
366 if (Literal.hadError)
367 return;
368 if (Literal.Pascal) {
369 Diag(StrToks[0].getLocation(), diag::err_pragma_comment_malformed);
370 return;
371 }
372
373 ArgumentString = std::string(Literal.GetString(),
374 Literal.GetString()+Literal.GetStringLength());
Chris Lattner636c5ef2009-01-16 08:21:25 +0000375 }
376
Chris Lattnera9d91452009-01-16 18:59:23 +0000377 // FIXME: If the kind is "compiler" warn if the string is present (it is
378 // ignored).
379 // FIXME: 'lib' requires a comment string.
380 // FIXME: 'linker' requires a comment string, and has a specific list of
381 // things that are allowable.
382
Chris Lattner636c5ef2009-01-16 08:21:25 +0000383 if (Tok.isNot(tok::r_paren)) {
384 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
385 return;
386 }
Chris Lattnera9d91452009-01-16 18:59:23 +0000387 Lex(Tok); // eat the r_paren.
Chris Lattner636c5ef2009-01-16 08:21:25 +0000388
389 if (Tok.isNot(tok::eom)) {
390 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
391 return;
392 }
Chris Lattnera9d91452009-01-16 18:59:23 +0000393
394 // If the pragma is lexically sound, notify any interested PPCallbacks.
Chris Lattner172e3362009-01-16 19:01:46 +0000395 if (Callbacks)
396 Callbacks->PragmaComment(CommentLoc, II, ArgumentString);
Chris Lattner636c5ef2009-01-16 08:21:25 +0000397}
398
399
400
Reid Spencer5f016e22007-07-11 17:01:13 +0000401
402/// AddPragmaHandler - Add the specified pragma handler to the preprocessor.
403/// If 'Namespace' is non-null, then it is a token required to exist on the
404/// pragma line before the pragma string starts, e.g. "STDC" or "GCC".
405void Preprocessor::AddPragmaHandler(const char *Namespace,
406 PragmaHandler *Handler) {
407 PragmaNamespace *InsertNS = PragmaHandlers;
408
409 // If this is specified to be in a namespace, step down into it.
410 if (Namespace) {
411 IdentifierInfo *NSID = getIdentifierInfo(Namespace);
412
413 // If there is already a pragma handler with the name of this namespace,
414 // we either have an error (directive with the same name as a namespace) or
415 // we already have the namespace to insert into.
416 if (PragmaHandler *Existing = PragmaHandlers->FindHandler(NSID)) {
417 InsertNS = Existing->getIfNamespace();
418 assert(InsertNS != 0 && "Cannot have a pragma namespace and pragma"
419 " handler with the same name!");
420 } else {
421 // Otherwise, this namespace doesn't exist yet, create and insert the
422 // handler for it.
423 InsertNS = new PragmaNamespace(NSID);
424 PragmaHandlers->AddPragma(InsertNS);
425 }
426 }
427
428 // Check to make sure we don't already have a pragma for this identifier.
429 assert(!InsertNS->FindHandler(Handler->getName()) &&
430 "Pragma handler already exists for this identifier!");
431 InsertNS->AddPragma(Handler);
432}
433
Daniel Dunbar40950802008-10-04 19:17:46 +0000434/// RemovePragmaHandler - Remove the specific pragma handler from the
435/// preprocessor. If \arg Namespace is non-null, then it should be the
436/// namespace that \arg Handler was added to. It is an error to remove
437/// a handler that has not been registered.
438void Preprocessor::RemovePragmaHandler(const char *Namespace,
439 PragmaHandler *Handler) {
440 PragmaNamespace *NS = PragmaHandlers;
441
442 // If this is specified to be in a namespace, step down into it.
443 if (Namespace) {
444 IdentifierInfo *NSID = getIdentifierInfo(Namespace);
445 PragmaHandler *Existing = PragmaHandlers->FindHandler(NSID);
446 assert(Existing && "Namespace containing handler does not exist!");
447
448 NS = Existing->getIfNamespace();
449 assert(NS && "Invalid namespace, registered as a regular pragma handler!");
450 }
451
452 NS->RemovePragmaHandler(Handler);
453
454 // If this is a non-default namespace and it is now empty, remove
455 // it.
456 if (NS != PragmaHandlers && NS->IsEmpty())
457 PragmaHandlers->RemovePragmaHandler(NS);
458}
459
Reid Spencer5f016e22007-07-11 17:01:13 +0000460namespace {
Chris Lattner22434492007-12-19 19:38:36 +0000461/// PragmaOnceHandler - "#pragma once" marks the file as atomically included.
Reid Spencer5f016e22007-07-11 17:01:13 +0000462struct PragmaOnceHandler : public PragmaHandler {
463 PragmaOnceHandler(const IdentifierInfo *OnceID) : PragmaHandler(OnceID) {}
Chris Lattnerd2177732007-07-20 16:59:19 +0000464 virtual void HandlePragma(Preprocessor &PP, Token &OnceTok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000465 PP.CheckEndOfDirective("#pragma once");
466 PP.HandlePragmaOnce(OnceTok);
467 }
468};
469
Chris Lattner22434492007-12-19 19:38:36 +0000470/// PragmaMarkHandler - "#pragma mark ..." is ignored by the compiler, and the
471/// rest of the line is not lexed.
472struct PragmaMarkHandler : public PragmaHandler {
473 PragmaMarkHandler(const IdentifierInfo *MarkID) : PragmaHandler(MarkID) {}
474 virtual void HandlePragma(Preprocessor &PP, Token &MarkTok) {
475 PP.HandlePragmaMark();
476 }
477};
478
479/// PragmaPoisonHandler - "#pragma poison x" marks x as not usable.
Reid Spencer5f016e22007-07-11 17:01:13 +0000480struct PragmaPoisonHandler : public PragmaHandler {
481 PragmaPoisonHandler(const IdentifierInfo *ID) : PragmaHandler(ID) {}
Chris Lattnerd2177732007-07-20 16:59:19 +0000482 virtual void HandlePragma(Preprocessor &PP, Token &PoisonTok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000483 PP.HandlePragmaPoison(PoisonTok);
484 }
485};
486
Chris Lattner22434492007-12-19 19:38:36 +0000487/// PragmaSystemHeaderHandler - "#pragma system_header" marks the current file
488/// as a system header, which silences warnings in it.
Reid Spencer5f016e22007-07-11 17:01:13 +0000489struct PragmaSystemHeaderHandler : public PragmaHandler {
490 PragmaSystemHeaderHandler(const IdentifierInfo *ID) : PragmaHandler(ID) {}
Chris Lattnerd2177732007-07-20 16:59:19 +0000491 virtual void HandlePragma(Preprocessor &PP, Token &SHToken) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000492 PP.HandlePragmaSystemHeader(SHToken);
493 PP.CheckEndOfDirective("#pragma");
494 }
495};
496struct PragmaDependencyHandler : public PragmaHandler {
497 PragmaDependencyHandler(const IdentifierInfo *ID) : PragmaHandler(ID) {}
Chris Lattnerd2177732007-07-20 16:59:19 +0000498 virtual void HandlePragma(Preprocessor &PP, Token &DepToken) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000499 PP.HandlePragmaDependency(DepToken);
500 }
501};
Chris Lattner636c5ef2009-01-16 08:21:25 +0000502
503/// PragmaCommentHandler - "#pragma comment ...".
504struct PragmaCommentHandler : public PragmaHandler {
505 PragmaCommentHandler(const IdentifierInfo *ID) : PragmaHandler(ID) {}
506 virtual void HandlePragma(Preprocessor &PP, Token &CommentTok) {
507 PP.HandlePragmaComment(CommentTok);
508 }
509};
Reid Spencer5f016e22007-07-11 17:01:13 +0000510} // end anonymous namespace
511
512
513/// RegisterBuiltinPragmas - Install the standard preprocessor pragmas:
514/// #pragma GCC poison/system_header/dependency and #pragma once.
515void Preprocessor::RegisterBuiltinPragmas() {
516 AddPragmaHandler(0, new PragmaOnceHandler(getIdentifierInfo("once")));
Chris Lattner22434492007-12-19 19:38:36 +0000517 AddPragmaHandler(0, new PragmaMarkHandler(getIdentifierInfo("mark")));
Reid Spencer5f016e22007-07-11 17:01:13 +0000518 AddPragmaHandler("GCC", new PragmaPoisonHandler(getIdentifierInfo("poison")));
519 AddPragmaHandler("GCC", new PragmaSystemHeaderHandler(
520 getIdentifierInfo("system_header")));
521 AddPragmaHandler("GCC", new PragmaDependencyHandler(
522 getIdentifierInfo("dependency")));
Chris Lattner636c5ef2009-01-16 08:21:25 +0000523
524 // MS extensions.
525 if (Features.Microsoft)
526 AddPragmaHandler(0, new PragmaCommentHandler(getIdentifierInfo("comment")));
Reid Spencer5f016e22007-07-11 17:01:13 +0000527}