blob: 856b3bd76d802bfa02feda6885e7c1edc4924dee [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"
Douglas Gregor2e222532009-07-02 17:08:52 +000022#include <algorithm>
Reid Spencer5f016e22007-07-11 17:01:13 +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) {
Mike Stump1eb44332009-09-09 15:08:12 +000047 if (Handlers[i]->getName() == Name)
Reid Spencer5f016e22007-07-11 17:01:13 +000048 return Handlers[i];
Mike Stump1eb44332009-09-09 15:08:12 +000049
Reid Spencer5f016e22007-07-11 17:01:13 +000050 if (Handlers[i]->getName() == 0)
51 NullHandler = Handlers[i];
52 }
53 return IgnoreNull ? 0 : NullHandler;
54}
55
Daniel Dunbar40950802008-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 Lattnerd2177732007-07-20 16:59:19 +000067void PragmaNamespace::HandlePragma(Preprocessor &PP, Token &Tok) {
Reid Spencer5f016e22007-07-11 17:01:13 +000068 // 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);
Mike Stump1eb44332009-09-09 15:08:12 +000071
Reid Spencer5f016e22007-07-11 17:01:13 +000072 // Get the handler for this token. If there is no handler, ignore the pragma.
73 PragmaHandler *Handler = FindHandler(Tok.getIdentifierInfo(), false);
Chris Lattneraf7cdf42009-04-19 21:10:26 +000074 if (Handler == 0) {
75 PP.Diag(Tok, diag::warn_pragma_ignored);
76 return;
77 }
Mike Stump1eb44332009-09-09 15:08:12 +000078
Reid Spencer5f016e22007-07-11 17:01:13 +000079 // 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;
Mike Stump1eb44332009-09-09 15:08:12 +000091
Reid Spencer5f016e22007-07-11 17:01:13 +000092 // Invoke the first level of pragma handlers which reads the namespace id.
Chris Lattnerd2177732007-07-20 16:59:19 +000093 Token Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +000094 PragmaHandlers->HandlePragma(*this, Tok);
Mike Stump1eb44332009-09-09 15:08:12 +000095
Reid Spencer5f016e22007-07-11 17:01:13 +000096 // If the pragma handler didn't read the rest of the line, consume it now.
Chris Lattner027cff62009-06-18 05:55:53 +000097 if (CurPPLexer && CurPPLexer->ParsingPreprocessorDirective)
Reid Spencer5f016e22007-07-11 17:01:13 +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'.
Chris Lattnerd2177732007-07-20 16:59:19 +0000104void Preprocessor::Handle_Pragma(Token &Tok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000105 // Remember the pragma token location.
106 SourceLocation PragmaLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000107
Reid Spencer5f016e22007-07-11 17:01:13 +0000108 // Read the '('.
109 Lex(Tok);
Chris Lattner3692b092008-11-18 07:59:24 +0000110 if (Tok.isNot(tok::l_paren)) {
111 Diag(PragmaLoc, diag::err__Pragma_malformed);
112 return;
113 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000114
115 // Read the '"..."'.
116 Lex(Tok);
Chris Lattner3692b092008-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 }
Mike Stump1eb44332009-09-09 15:08:12 +0000121
Reid Spencer5f016e22007-07-11 17:01:13 +0000122 // Remember the string.
123 std::string StrVal = getSpelling(Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000124
125 // Read the ')'.
126 Lex(Tok);
Chris Lattner3692b092008-11-18 07:59:24 +0000127 if (Tok.isNot(tok::r_paren)) {
128 Diag(PragmaLoc, diag::err__Pragma_malformed);
129 return;
130 }
Mike Stump1eb44332009-09-09 15:08:12 +0000131
Chris Lattnere7fb4842009-02-15 20:52:18 +0000132 SourceLocation RParenLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000133
Chris Lattnera9d91452009-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."
Reid Spencer5f016e22007-07-11 17:01:13 +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!");
Mike Stump1eb44332009-09-09 15:08:12 +0000143
Reid Spencer5f016e22007-07-11 17:01:13 +0000144 // 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] = ' ';
Mike Stump1eb44332009-09-09 15:08:12 +0000147
Chris Lattner1fa49532009-03-08 08:08:45 +0000148 // Replace the terminating quote with a \n.
Reid Spencer5f016e22007-07-11 17:01:13 +0000149 StrVal[StrVal.size()-1] = '\n';
Mike Stump1eb44332009-09-09 15:08:12 +0000150
Reid Spencer5f016e22007-07-11 17:01:13 +0000151 // 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 }
Mike Stump1eb44332009-09-09 15:08:12 +0000160
Reid Spencer5f016e22007-07-11 17:01:13 +0000161 // Plop the string (including the newline and trailing null) into a buffer
162 // where we can lex it.
Chris Lattner47246be2009-01-26 19:29:26 +0000163 Token TmpTok;
164 TmpTok.startToken();
165 CreateString(&StrVal[0], StrVal.size(), TmpTok);
166 SourceLocation TokLoc = TmpTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000167
Reid Spencer5f016e22007-07-11 17:01:13 +0000168 // Make and enter a lexer object so that we lex and expand the tokens just
169 // like any others.
Chris Lattnere7fb4842009-02-15 20:52:18 +0000170 Lexer *TL = Lexer::Create_PragmaLexer(TokLoc, PragmaLoc, RParenLoc,
Chris Lattner1fa49532009-03-08 08:08:45 +0000171 StrVal.size(), *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000172
173 EnterSourceFileWithLexer(TL, 0);
174
175 // With everything set up, lex this as a #pragma directive.
176 HandlePragmaDirective();
Mike Stump1eb44332009-09-09 15:08:12 +0000177
Reid Spencer5f016e22007-07-11 17:01:13 +0000178 // 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///
Chris Lattnerd2177732007-07-20 16:59:19 +0000186void Preprocessor::HandlePragmaOnce(Token &OnceTok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000187 if (isInPrimaryFile()) {
188 Diag(OnceTok, diag::pp_pragma_once_in_main_file);
189 return;
190 }
Mike Stump1eb44332009-09-09 15:08:12 +0000191
Reid Spencer5f016e22007-07-11 17:01:13 +0000192 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
Reid Spencer5f016e22007-07-11 17:01:13 +0000193 // Mark the file as a once-only file now.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000194 HeaderInfo.MarkFileIncludeOnce(getCurrentFileLexer()->getFileEntry());
Reid Spencer5f016e22007-07-11 17:01:13 +0000195}
196
Chris Lattner22434492007-12-19 19:38:36 +0000197void Preprocessor::HandlePragmaMark() {
Ted Kremenek17ff58a2008-11-19 22:21:33 +0000198 assert(CurPPLexer && "No current lexer?");
Chris Lattner6896a372009-06-15 05:02:34 +0000199 if (CurLexer)
200 CurLexer->ReadToEndOfLine();
201 else
202 CurPTHLexer->DiscardToEndOfLine();
Chris Lattner22434492007-12-19 19:38:36 +0000203}
204
205
Reid Spencer5f016e22007-07-11 17:01:13 +0000206/// HandlePragmaPoison - Handle #pragma GCC poison. PoisonTok is the 'poison'.
207///
Chris Lattnerd2177732007-07-20 16:59:19 +0000208void Preprocessor::HandlePragmaPoison(Token &PoisonTok) {
209 Token Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000210
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 Kremenek68a91d52008-11-18 01:12:54 +0000217 if (CurPPLexer) CurPPLexer->LexingRawMode = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000218 LexUnexpandedToken(Tok);
Ted Kremenek68a91d52008-11-18 01:12:54 +0000219 if (CurPPLexer) CurPPLexer->LexingRawMode = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000220
Reid Spencer5f016e22007-07-11 17:01:13 +0000221 // If we reached the end of line, we're done.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000222 if (Tok.is(tok::eom)) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000223
Reid Spencer5f016e22007-07-11 17:01:13 +0000224 // Can only poison identifiers.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000225 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000226 Diag(Tok, diag::err_pp_invalid_poison);
227 return;
228 }
Mike Stump1eb44332009-09-09 15:08:12 +0000229
Reid Spencer5f016e22007-07-11 17:01:13 +0000230 // 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);
Mike Stump1eb44332009-09-09 15:08:12 +0000233
Reid Spencer5f016e22007-07-11 17:01:13 +0000234 // Already poisoned.
235 if (II->isPoisoned()) continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000236
Reid Spencer5f016e22007-07-11 17:01:13 +0000237 // If this is a macro identifier, emit a warning.
Chris Lattner0edde552007-10-07 08:04:56 +0000238 if (II->hasMacroDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000239 Diag(Tok, diag::pp_poisoning_existing_macro);
Mike Stump1eb44332009-09-09 15:08:12 +0000240
Reid Spencer5f016e22007-07-11 17:01:13 +0000241 // 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.
Chris Lattnerd2177732007-07-20 16:59:19 +0000248void Preprocessor::HandlePragmaSystemHeader(Token &SysHeaderTok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000249 if (isInPrimaryFile()) {
250 Diag(SysHeaderTok, diag::pp_pragma_sysheader_in_main_file);
251 return;
252 }
Mike Stump1eb44332009-09-09 15:08:12 +0000253
Reid Spencer5f016e22007-07-11 17:01:13 +0000254 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
Ted Kremenek35c10c22008-11-20 01:45:11 +0000255 PreprocessorLexer *TheLexer = getCurrentFileLexer();
Mike Stump1eb44332009-09-09 15:08:12 +0000256
Reid Spencer5f016e22007-07-11 17:01:13 +0000257 // Mark the file as a system header.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000258 HeaderInfo.MarkFileSystemHeader(TheLexer->getFileEntry());
Mike Stump1eb44332009-09-09 15:08:12 +0000259
260
Chris Lattner6896a372009-06-15 05:02:34 +0000261 PresumedLoc PLoc = SourceMgr.getPresumedLoc(SysHeaderTok.getLocation());
262 unsigned FilenameLen = strlen(PLoc.getFilename());
263 unsigned FilenameID = SourceMgr.getLineTableFilenameID(PLoc.getFilename(),
264 FilenameLen);
Mike Stump1eb44332009-09-09 15:08:12 +0000265
Chris Lattner6896a372009-06-15 05:02:34 +0000266 // 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);
Mike Stump1eb44332009-09-09 15:08:12 +0000271
Reid Spencer5f016e22007-07-11 17:01:13 +0000272 // Notify the client, if desired, that we are in a new source file.
273 if (Callbacks)
Ted Kremenek35c10c22008-11-20 01:45:11 +0000274 Callbacks->FileChanged(SysHeaderTok.getLocation(),
Chris Lattner0b9e7362008-09-26 21:18:42 +0000275 PPCallbacks::SystemHeaderPragma, SrcMgr::C_System);
Reid Spencer5f016e22007-07-11 17:01:13 +0000276}
277
278/// HandlePragmaDependency - Handle #pragma GCC dependency "foo" blah.
279///
Chris Lattnerd2177732007-07-20 16:59:19 +0000280void Preprocessor::HandlePragmaDependency(Token &DependencyTok) {
281 Token FilenameTok;
Ted Kremenek68a91d52008-11-18 01:12:54 +0000282 CurPPLexer->LexIncludeFilename(FilenameTok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000283
284 // If the token kind is EOM, the error has already been diagnosed.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000285 if (FilenameTok.is(tok::eom))
Reid Spencer5f016e22007-07-11 17:01:13 +0000286 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000287
Reid Spencer5f016e22007-07-11 17:01:13 +0000288 // Reserve a buffer to get the spelling.
Chris Lattnera1394812010-01-10 01:35:12 +0000289 llvm::SmallString<128> FilenameBuffer;
Reid Spencer5f016e22007-07-11 17:01:13 +0000290 FilenameBuffer.resize(FilenameTok.getLength());
Mike Stump1eb44332009-09-09 15:08:12 +0000291
Chris Lattnerf1c99ac2007-07-23 04:15:27 +0000292 const char *FilenameStart = &FilenameBuffer[0];
293 unsigned Len = getSpelling(FilenameTok, FilenameStart);
Chris Lattnera1394812010-01-10 01:35:12 +0000294 llvm::StringRef Filename(FilenameStart, Len);
295 bool isAngled =
296 GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
Reid Spencer5f016e22007-07-11 17:01:13 +0000297 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
298 // error.
Chris Lattnera1394812010-01-10 01:35:12 +0000299 if (Filename.empty())
Reid Spencer5f016e22007-07-11 17:01:13 +0000300 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000301
Reid Spencer5f016e22007-07-11 17:01:13 +0000302 // Search include directories for this file.
303 const DirectoryLookup *CurDir;
Chris Lattnera1394812010-01-10 01:35:12 +0000304 const FileEntry *File = LookupFile(Filename, FilenameTok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000305 isAngled, 0, CurDir);
Chris Lattner56b05c82008-11-18 08:02:48 +0000306 if (File == 0) {
Chris Lattnera1394812010-01-10 01:35:12 +0000307 Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
Chris Lattner56b05c82008-11-18 08:02:48 +0000308 return;
309 }
Mike Stump1eb44332009-09-09 15:08:12 +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 }
Mike Stump1eb44332009-09-09 15:08:12 +0000322
Reid Spencer5f016e22007-07-11 17:01:13 +0000323 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 }
Mike Stump1eb44332009-09-09 15:08:12 +0000341
Chris Lattner636c5ef2009-01-16 08:21:25 +0000342 // Read the identifier.
343 Lex(Tok);
344 if (Tok.isNot(tok::identifier)) {
345 Diag(CommentLoc, diag::err_pragma_comment_malformed);
346 return;
347 }
Mike Stump1eb44332009-09-09 15:08:12 +0000348
Chris Lattner636c5ef2009-01-16 08:21:25 +0000349 // Verify that this is one of the 5 whitelisted options.
350 // FIXME: warn that 'exestr' is deprecated.
351 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000352 if (!II->isStr("compiler") && !II->isStr("exestr") && !II->isStr("lib") &&
Chris Lattner636c5ef2009-01-16 08:21:25 +0000353 !II->isStr("linker") && !II->isStr("user")) {
354 Diag(Tok.getLocation(), diag::err_pragma_comment_unknown_kind);
355 return;
356 }
Mike Stump1eb44332009-09-09 15:08:12 +0000357
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 }
Mike Stump1eb44332009-09-09 15:08:12 +0000392
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.
Mike Stump1eb44332009-09-09 15:08:12 +0000398
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 }
Mike Stump1eb44332009-09-09 15:08:12 +0000409
Chris Lattnera9d91452009-01-16 18:59:23 +0000410 // 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".
Mike Stump1eb44332009-09-09 15:08:12 +0000421void Preprocessor::AddPragmaHandler(const char *Namespace,
Reid Spencer5f016e22007-07-11 17:01:13 +0000422 PragmaHandler *Handler) {
423 PragmaNamespace *InsertNS = PragmaHandlers;
Mike Stump1eb44332009-09-09 15:08:12 +0000424
Reid Spencer5f016e22007-07-11 17:01:13 +0000425 // If this is specified to be in a namespace, step down into it.
426 if (Namespace) {
427 IdentifierInfo *NSID = getIdentifierInfo(Namespace);
Mike Stump1eb44332009-09-09 15:08:12 +0000428
Reid Spencer5f016e22007-07-11 17:01:13 +0000429 // 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 }
Mike Stump1eb44332009-09-09 15:08:12 +0000443
Reid Spencer5f016e22007-07-11 17:01:13 +0000444 // 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;
Mike Stump1eb44332009-09-09 15:08:12 +0000457
Daniel Dunbar40950802008-10-04 19:17:46 +0000458 // 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);
Mike Stump1eb44332009-09-09 15:08:12 +0000469
Daniel Dunbar40950802008-10-04 19:17:46 +0000470 // 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};
Mike Stump1eb44332009-09-09 15:08:12 +0000518
Chris Lattneredaf8772009-04-19 23:16:58 +0000519/// PragmaDiagnosticHandler - e.g. '#pragma GCC diagnostic ignored "-Wformat"'
Chris Lattner04ae2df2009-07-12 21:18:45 +0000520/// Since clang's diagnostic supports extended functionality beyond GCC's
521/// the constructor takes a clangMode flag to tell it whether or not to allow
522/// clang's extended functionality, or whether to reject it.
Chris Lattneredaf8772009-04-19 23:16:58 +0000523struct PragmaDiagnosticHandler : public PragmaHandler {
Chris Lattner04ae2df2009-07-12 21:18:45 +0000524private:
525 const bool ClangMode;
526public:
527 PragmaDiagnosticHandler(const IdentifierInfo *ID,
528 const bool clangMode) : PragmaHandler(ID),
529 ClangMode(clangMode) {}
Chris Lattneredaf8772009-04-19 23:16:58 +0000530 virtual void HandlePragma(Preprocessor &PP, Token &DiagToken) {
531 Token Tok;
532 PP.LexUnexpandedToken(Tok);
533 if (Tok.isNot(tok::identifier)) {
Chris Lattner04ae2df2009-07-12 21:18:45 +0000534 unsigned Diag = ClangMode ? diag::warn_pragma_diagnostic_clang_invalid
535 : diag::warn_pragma_diagnostic_gcc_invalid;
536 PP.Diag(Tok, Diag);
Chris Lattneredaf8772009-04-19 23:16:58 +0000537 return;
538 }
539 IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000540
Chris Lattneredaf8772009-04-19 23:16:58 +0000541 diag::Mapping Map;
542 if (II->isStr("warning"))
543 Map = diag::MAP_WARNING;
544 else if (II->isStr("error"))
545 Map = diag::MAP_ERROR;
546 else if (II->isStr("ignored"))
547 Map = diag::MAP_IGNORE;
548 else if (II->isStr("fatal"))
549 Map = diag::MAP_FATAL;
Chris Lattner04ae2df2009-07-12 21:18:45 +0000550 else if (ClangMode) {
551 if (II->isStr("pop")) {
Mike Stump1eb44332009-09-09 15:08:12 +0000552 if (!PP.getDiagnostics().popMappings())
Chris Lattner04ae2df2009-07-12 21:18:45 +0000553 PP.Diag(Tok, diag::warn_pragma_diagnostic_clang_cannot_ppp);
554 return;
555 }
556
557 if (II->isStr("push")) {
558 PP.getDiagnostics().pushMappings();
Mike Stump1eb44332009-09-09 15:08:12 +0000559 return;
Chris Lattner04ae2df2009-07-12 21:18:45 +0000560 }
561
562 PP.Diag(Tok, diag::warn_pragma_diagnostic_clang_invalid);
563 return;
564 } else {
565 PP.Diag(Tok, diag::warn_pragma_diagnostic_gcc_invalid);
Chris Lattneredaf8772009-04-19 23:16:58 +0000566 return;
567 }
Mike Stump1eb44332009-09-09 15:08:12 +0000568
Chris Lattneredaf8772009-04-19 23:16:58 +0000569 PP.LexUnexpandedToken(Tok);
570
571 // We need at least one string.
572 if (Tok.isNot(tok::string_literal)) {
573 PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token);
574 return;
575 }
Mike Stump1eb44332009-09-09 15:08:12 +0000576
Chris Lattneredaf8772009-04-19 23:16:58 +0000577 // String concatenation allows multiple strings, which can even come from
578 // macro expansion.
579 // "foo " "bar" "Baz"
580 llvm::SmallVector<Token, 4> StrToks;
581 while (Tok.is(tok::string_literal)) {
582 StrToks.push_back(Tok);
583 PP.LexUnexpandedToken(Tok);
584 }
Mike Stump1eb44332009-09-09 15:08:12 +0000585
Chris Lattneredaf8772009-04-19 23:16:58 +0000586 if (Tok.isNot(tok::eom)) {
587 PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token);
588 return;
589 }
Mike Stump1eb44332009-09-09 15:08:12 +0000590
Chris Lattneredaf8772009-04-19 23:16:58 +0000591 // Concatenate and parse the strings.
592 StringLiteralParser Literal(&StrToks[0], StrToks.size(), PP);
593 assert(!Literal.AnyWide && "Didn't allow wide strings in");
594 if (Literal.hadError)
595 return;
596 if (Literal.Pascal) {
Chris Lattner04ae2df2009-07-12 21:18:45 +0000597 unsigned Diag = ClangMode ? diag::warn_pragma_diagnostic_clang_invalid
598 : diag::warn_pragma_diagnostic_gcc_invalid;
599 PP.Diag(Tok, Diag);
Chris Lattneredaf8772009-04-19 23:16:58 +0000600 return;
601 }
Chris Lattner04ae2df2009-07-12 21:18:45 +0000602
Chris Lattneredaf8772009-04-19 23:16:58 +0000603 std::string WarningName(Literal.GetString(),
604 Literal.GetString()+Literal.GetStringLength());
605
606 if (WarningName.size() < 3 || WarningName[0] != '-' ||
607 WarningName[1] != 'W') {
608 PP.Diag(StrToks[0].getLocation(),
609 diag::warn_pragma_diagnostic_invalid_option);
610 return;
611 }
Mike Stump1eb44332009-09-09 15:08:12 +0000612
Chris Lattneredaf8772009-04-19 23:16:58 +0000613 if (PP.getDiagnostics().setDiagnosticGroupMapping(WarningName.c_str()+2,
614 Map))
615 PP.Diag(StrToks[0].getLocation(),
616 diag::warn_pragma_diagnostic_unknown_warning) << WarningName;
617 }
618};
Mike Stump1eb44332009-09-09 15:08:12 +0000619
Chris Lattner636c5ef2009-01-16 08:21:25 +0000620/// PragmaCommentHandler - "#pragma comment ...".
621struct PragmaCommentHandler : public PragmaHandler {
622 PragmaCommentHandler(const IdentifierInfo *ID) : PragmaHandler(ID) {}
623 virtual void HandlePragma(Preprocessor &PP, Token &CommentTok) {
624 PP.HandlePragmaComment(CommentTok);
625 }
626};
Mike Stump1eb44332009-09-09 15:08:12 +0000627
Chris Lattner062f2322009-04-19 21:20:35 +0000628// Pragma STDC implementations.
Chris Lattner6c5cf4a2009-04-19 21:50:08 +0000629
630enum STDCSetting {
631 STDC_ON, STDC_OFF, STDC_DEFAULT, STDC_INVALID
632};
Mike Stump1eb44332009-09-09 15:08:12 +0000633
Chris Lattner6c5cf4a2009-04-19 21:50:08 +0000634static STDCSetting LexOnOffSwitch(Preprocessor &PP) {
635 Token Tok;
636 PP.LexUnexpandedToken(Tok);
637
638 if (Tok.isNot(tok::identifier)) {
639 PP.Diag(Tok, diag::ext_stdc_pragma_syntax);
640 return STDC_INVALID;
641 }
642 IdentifierInfo *II = Tok.getIdentifierInfo();
643 STDCSetting Result;
644 if (II->isStr("ON"))
645 Result = STDC_ON;
646 else if (II->isStr("OFF"))
647 Result = STDC_OFF;
648 else if (II->isStr("DEFAULT"))
649 Result = STDC_DEFAULT;
650 else {
651 PP.Diag(Tok, diag::ext_stdc_pragma_syntax);
652 return STDC_INVALID;
653 }
654
655 // Verify that this is followed by EOM.
656 PP.LexUnexpandedToken(Tok);
657 if (Tok.isNot(tok::eom))
658 PP.Diag(Tok, diag::ext_stdc_pragma_syntax_eom);
659 return Result;
660}
Mike Stump1eb44332009-09-09 15:08:12 +0000661
Chris Lattner062f2322009-04-19 21:20:35 +0000662/// PragmaSTDC_FP_CONTRACTHandler - "#pragma STDC FP_CONTRACT ...".
663struct PragmaSTDC_FP_CONTRACTHandler : public PragmaHandler {
664 PragmaSTDC_FP_CONTRACTHandler(const IdentifierInfo *ID) : PragmaHandler(ID) {}
Chris Lattnerf545be52009-04-19 21:25:37 +0000665 virtual void HandlePragma(Preprocessor &PP, Token &Tok) {
Chris Lattner6c5cf4a2009-04-19 21:50:08 +0000666 // We just ignore the setting of FP_CONTRACT. Since we don't do contractions
667 // at all, our default is OFF and setting it to ON is an optimization hint
668 // we can safely ignore. When we support -ffma or something, we would need
669 // to diagnose that we are ignoring FMA.
670 LexOnOffSwitch(PP);
Chris Lattner062f2322009-04-19 21:20:35 +0000671 }
672};
Mike Stump1eb44332009-09-09 15:08:12 +0000673
Chris Lattner062f2322009-04-19 21:20:35 +0000674/// PragmaSTDC_FENV_ACCESSHandler - "#pragma STDC FENV_ACCESS ...".
675struct PragmaSTDC_FENV_ACCESSHandler : public PragmaHandler {
676 PragmaSTDC_FENV_ACCESSHandler(const IdentifierInfo *ID) : PragmaHandler(ID) {}
Chris Lattnerf545be52009-04-19 21:25:37 +0000677 virtual void HandlePragma(Preprocessor &PP, Token &Tok) {
Chris Lattner4d8aac32009-04-19 21:55:32 +0000678 if (LexOnOffSwitch(PP) == STDC_ON)
679 PP.Diag(Tok, diag::warn_stdc_fenv_access_not_supported);
Chris Lattner062f2322009-04-19 21:20:35 +0000680 }
681};
Mike Stump1eb44332009-09-09 15:08:12 +0000682
Chris Lattner062f2322009-04-19 21:20:35 +0000683/// PragmaSTDC_CX_LIMITED_RANGEHandler - "#pragma STDC CX_LIMITED_RANGE ...".
684struct PragmaSTDC_CX_LIMITED_RANGEHandler : public PragmaHandler {
685 PragmaSTDC_CX_LIMITED_RANGEHandler(const IdentifierInfo *ID)
686 : PragmaHandler(ID) {}
Chris Lattnerf545be52009-04-19 21:25:37 +0000687 virtual void HandlePragma(Preprocessor &PP, Token &Tok) {
Chris Lattner6c5cf4a2009-04-19 21:50:08 +0000688 LexOnOffSwitch(PP);
Chris Lattner062f2322009-04-19 21:20:35 +0000689 }
690};
Mike Stump1eb44332009-09-09 15:08:12 +0000691
Chris Lattner062f2322009-04-19 21:20:35 +0000692/// PragmaSTDC_UnknownHandler - "#pragma STDC ...".
693struct PragmaSTDC_UnknownHandler : public PragmaHandler {
694 PragmaSTDC_UnknownHandler() : PragmaHandler(0) {}
Chris Lattnerf545be52009-04-19 21:25:37 +0000695 virtual void HandlePragma(Preprocessor &PP, Token &UnknownTok) {
Chris Lattner6c5cf4a2009-04-19 21:50:08 +0000696 // C99 6.10.6p2, unknown forms are not allowed.
Chris Lattnerf545be52009-04-19 21:25:37 +0000697 PP.Diag(UnknownTok, diag::ext_stdc_pragma_ignored);
Chris Lattner062f2322009-04-19 21:20:35 +0000698 }
699};
Mike Stump1eb44332009-09-09 15:08:12 +0000700
Reid Spencer5f016e22007-07-11 17:01:13 +0000701} // end anonymous namespace
702
703
704/// RegisterBuiltinPragmas - Install the standard preprocessor pragmas:
705/// #pragma GCC poison/system_header/dependency and #pragma once.
706void Preprocessor::RegisterBuiltinPragmas() {
707 AddPragmaHandler(0, new PragmaOnceHandler(getIdentifierInfo("once")));
Chris Lattner22434492007-12-19 19:38:36 +0000708 AddPragmaHandler(0, new PragmaMarkHandler(getIdentifierInfo("mark")));
Mike Stump1eb44332009-09-09 15:08:12 +0000709
Chris Lattnere8fa06e2009-05-12 18:21:11 +0000710 // #pragma GCC ...
Reid Spencer5f016e22007-07-11 17:01:13 +0000711 AddPragmaHandler("GCC", new PragmaPoisonHandler(getIdentifierInfo("poison")));
712 AddPragmaHandler("GCC", new PragmaSystemHeaderHandler(
713 getIdentifierInfo("system_header")));
714 AddPragmaHandler("GCC", new PragmaDependencyHandler(
715 getIdentifierInfo("dependency")));
Chris Lattneredaf8772009-04-19 23:16:58 +0000716 AddPragmaHandler("GCC", new PragmaDiagnosticHandler(
Chris Lattner04ae2df2009-07-12 21:18:45 +0000717 getIdentifierInfo("diagnostic"),
718 false));
Chris Lattnere8fa06e2009-05-12 18:21:11 +0000719 // #pragma clang ...
720 AddPragmaHandler("clang", new PragmaPoisonHandler(
721 getIdentifierInfo("poison")));
722 AddPragmaHandler("clang", new PragmaSystemHeaderHandler(
723 getIdentifierInfo("system_header")));
724 AddPragmaHandler("clang", new PragmaDependencyHandler(
725 getIdentifierInfo("dependency")));
726 AddPragmaHandler("clang", new PragmaDiagnosticHandler(
Chris Lattner04ae2df2009-07-12 21:18:45 +0000727 getIdentifierInfo("diagnostic"),
728 true));
Chris Lattnere8fa06e2009-05-12 18:21:11 +0000729
Chris Lattner062f2322009-04-19 21:20:35 +0000730 AddPragmaHandler("STDC", new PragmaSTDC_FP_CONTRACTHandler(
731 getIdentifierInfo("FP_CONTRACT")));
732 AddPragmaHandler("STDC", new PragmaSTDC_FENV_ACCESSHandler(
733 getIdentifierInfo("FENV_ACCESS")));
734 AddPragmaHandler("STDC", new PragmaSTDC_CX_LIMITED_RANGEHandler(
735 getIdentifierInfo("CX_LIMITED_RANGE")));
736 AddPragmaHandler("STDC", new PragmaSTDC_UnknownHandler());
Mike Stump1eb44332009-09-09 15:08:12 +0000737
Chris Lattner636c5ef2009-01-16 08:21:25 +0000738 // MS extensions.
739 if (Features.Microsoft)
740 AddPragmaHandler(0, new PragmaCommentHandler(getIdentifierInfo("comment")));
Reid Spencer5f016e22007-07-11 17:01:13 +0000741}