blob: a7b289e137ebf73dd7a05b87e2fcaa90b5a59b65 [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 Lattnerf47724b2010-08-17 15:55:45 +000019#include "clang/Lex/MacroInfo.h"
Chris Lattner500d3292009-01-29 05:15:15 +000020#include "clang/Lex/LexDiagnostic.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000021#include "clang/Basic/FileManager.h"
22#include "clang/Basic/SourceManager.h"
Daniel Dunbarff759a62010-08-18 23:09:23 +000023#include "llvm/Support/CrashRecoveryContext.h"
Daniel Dunbar55054132010-08-17 22:32:48 +000024#include "llvm/Support/ErrorHandling.h"
Douglas Gregor2e222532009-07-02 17:08:52 +000025#include <algorithm>
Reid Spencer5f016e22007-07-11 17:01:13 +000026using namespace clang;
27
28// Out-of-line destructor to provide a home for the class.
29PragmaHandler::~PragmaHandler() {
30}
31
32//===----------------------------------------------------------------------===//
Daniel Dunbarc72cc502010-06-11 20:10:12 +000033// EmptyPragmaHandler Implementation.
34//===----------------------------------------------------------------------===//
35
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +000036EmptyPragmaHandler::EmptyPragmaHandler() {}
Daniel Dunbarc72cc502010-06-11 20:10:12 +000037
38void EmptyPragmaHandler::HandlePragma(Preprocessor &PP, Token &FirstToken) {}
39
40//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +000041// PragmaNamespace Implementation.
42//===----------------------------------------------------------------------===//
43
44
45PragmaNamespace::~PragmaNamespace() {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +000046 for (llvm::StringMap<PragmaHandler*>::iterator
47 I = Handlers.begin(), E = Handlers.end(); I != E; ++I)
48 delete I->second;
Reid Spencer5f016e22007-07-11 17:01:13 +000049}
50
51/// FindHandler - Check to see if there is already a handler for the
52/// specified name. If not, return the handler for the null identifier if it
53/// exists, otherwise return null. If IgnoreNull is true (the default) then
54/// the null handler isn't returned on failure to match.
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +000055PragmaHandler *PragmaNamespace::FindHandler(llvm::StringRef Name,
Reid Spencer5f016e22007-07-11 17:01:13 +000056 bool IgnoreNull) const {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +000057 if (PragmaHandler *Handler = Handlers.lookup(Name))
58 return Handler;
59 return IgnoreNull ? 0 : Handlers.lookup(llvm::StringRef());
60}
Mike Stump1eb44332009-09-09 15:08:12 +000061
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +000062void PragmaNamespace::AddPragma(PragmaHandler *Handler) {
63 assert(!Handlers.lookup(Handler->getName()) &&
64 "A handler with this name is already registered in this namespace");
65 llvm::StringMapEntry<PragmaHandler *> &Entry =
66 Handlers.GetOrCreateValue(Handler->getName());
67 Entry.setValue(Handler);
Reid Spencer5f016e22007-07-11 17:01:13 +000068}
69
Daniel Dunbar40950802008-10-04 19:17:46 +000070void PragmaNamespace::RemovePragmaHandler(PragmaHandler *Handler) {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +000071 assert(Handlers.lookup(Handler->getName()) &&
72 "Handler not registered in this namespace");
73 Handlers.erase(Handler->getName());
Daniel Dunbar40950802008-10-04 19:17:46 +000074}
75
Chris Lattnerd2177732007-07-20 16:59:19 +000076void PragmaNamespace::HandlePragma(Preprocessor &PP, Token &Tok) {
Reid Spencer5f016e22007-07-11 17:01:13 +000077 // Read the 'namespace' that the directive is in, e.g. STDC. Do not macro
78 // expand it, the user can have a STDC #define, that should not affect this.
79 PP.LexUnexpandedToken(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +000080
Reid Spencer5f016e22007-07-11 17:01:13 +000081 // Get the handler for this token. If there is no handler, ignore the pragma.
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +000082 PragmaHandler *Handler
83 = FindHandler(Tok.getIdentifierInfo() ? Tok.getIdentifierInfo()->getName()
84 : llvm::StringRef(),
85 /*IgnoreNull=*/false);
Chris Lattneraf7cdf42009-04-19 21:10:26 +000086 if (Handler == 0) {
87 PP.Diag(Tok, diag::warn_pragma_ignored);
88 return;
89 }
Mike Stump1eb44332009-09-09 15:08:12 +000090
Reid Spencer5f016e22007-07-11 17:01:13 +000091 // Otherwise, pass it down.
92 Handler->HandlePragma(PP, Tok);
93}
94
95//===----------------------------------------------------------------------===//
96// Preprocessor Pragma Directive Handling.
97//===----------------------------------------------------------------------===//
98
99/// HandlePragmaDirective - The "#pragma" directive has been parsed. Lex the
100/// rest of the pragma, passing it to the registered pragma handlers.
101void Preprocessor::HandlePragmaDirective() {
102 ++NumPragma;
Mike Stump1eb44332009-09-09 15:08:12 +0000103
Reid Spencer5f016e22007-07-11 17:01:13 +0000104 // Invoke the first level of pragma handlers which reads the namespace id.
Chris Lattnerd2177732007-07-20 16:59:19 +0000105 Token Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000106 PragmaHandlers->HandlePragma(*this, Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000107
Reid Spencer5f016e22007-07-11 17:01:13 +0000108 // If the pragma handler didn't read the rest of the line, consume it now.
Chris Lattner027cff62009-06-18 05:55:53 +0000109 if (CurPPLexer && CurPPLexer->ParsingPreprocessorDirective)
Reid Spencer5f016e22007-07-11 17:01:13 +0000110 DiscardUntilEndOfDirective();
111}
112
113/// Handle_Pragma - Read a _Pragma directive, slice it up, process it, then
114/// return the first token after the directive. The _Pragma token has just
115/// been read into 'Tok'.
Chris Lattnerd2177732007-07-20 16:59:19 +0000116void Preprocessor::Handle_Pragma(Token &Tok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000117 // Remember the pragma token location.
118 SourceLocation PragmaLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000119
Reid Spencer5f016e22007-07-11 17:01:13 +0000120 // Read the '('.
121 Lex(Tok);
Chris Lattner3692b092008-11-18 07:59:24 +0000122 if (Tok.isNot(tok::l_paren)) {
123 Diag(PragmaLoc, diag::err__Pragma_malformed);
124 return;
125 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000126
127 // Read the '"..."'.
128 Lex(Tok);
Chris Lattner3692b092008-11-18 07:59:24 +0000129 if (Tok.isNot(tok::string_literal) && Tok.isNot(tok::wide_string_literal)) {
130 Diag(PragmaLoc, diag::err__Pragma_malformed);
131 return;
132 }
Mike Stump1eb44332009-09-09 15:08:12 +0000133
Reid Spencer5f016e22007-07-11 17:01:13 +0000134 // Remember the string.
135 std::string StrVal = getSpelling(Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000136
137 // Read the ')'.
138 Lex(Tok);
Chris Lattner3692b092008-11-18 07:59:24 +0000139 if (Tok.isNot(tok::r_paren)) {
140 Diag(PragmaLoc, diag::err__Pragma_malformed);
141 return;
142 }
Mike Stump1eb44332009-09-09 15:08:12 +0000143
Chris Lattnere7fb4842009-02-15 20:52:18 +0000144 SourceLocation RParenLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000145
Chris Lattnera9d91452009-01-16 18:59:23 +0000146 // The _Pragma is lexically sound. Destringize according to C99 6.10.9.1:
147 // "The string literal is destringized by deleting the L prefix, if present,
148 // deleting the leading and trailing double-quotes, replacing each escape
149 // sequence \" by a double-quote, and replacing each escape sequence \\ by a
150 // single backslash."
Reid Spencer5f016e22007-07-11 17:01:13 +0000151 if (StrVal[0] == 'L') // Remove L prefix.
152 StrVal.erase(StrVal.begin());
153 assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
154 "Invalid string token!");
Mike Stump1eb44332009-09-09 15:08:12 +0000155
Reid Spencer5f016e22007-07-11 17:01:13 +0000156 // Remove the front quote, replacing it with a space, so that the pragma
157 // contents appear to have a space before them.
158 StrVal[0] = ' ';
Mike Stump1eb44332009-09-09 15:08:12 +0000159
Chris Lattner1fa49532009-03-08 08:08:45 +0000160 // Replace the terminating quote with a \n.
Reid Spencer5f016e22007-07-11 17:01:13 +0000161 StrVal[StrVal.size()-1] = '\n';
Mike Stump1eb44332009-09-09 15:08:12 +0000162
Reid Spencer5f016e22007-07-11 17:01:13 +0000163 // Remove escaped quotes and escapes.
164 for (unsigned i = 0, e = StrVal.size(); i != e-1; ++i) {
165 if (StrVal[i] == '\\' &&
166 (StrVal[i+1] == '\\' || StrVal[i+1] == '"')) {
167 // \\ -> '\' and \" -> '"'.
168 StrVal.erase(StrVal.begin()+i);
169 --e;
170 }
171 }
John McCall1ef8a2e2010-08-28 22:34:47 +0000172
173 Handle_Pragma(StrVal, PragmaLoc, RParenLoc);
174
175 // Finally, return whatever came after the pragma directive.
176 return Lex(Tok);
177}
178
179/// HandleMicrosoft__pragma - Like Handle_Pragma except the pragma text
180/// is not enclosed within a string literal.
181void Preprocessor::HandleMicrosoft__pragma(Token &Tok) {
182 // Remember the pragma token location.
183 SourceLocation PragmaLoc = Tok.getLocation();
184
185 // Read the '('.
186 Lex(Tok);
187 if (Tok.isNot(tok::l_paren)) {
188 Diag(PragmaLoc, diag::err__Pragma_malformed);
189 return;
190 }
191
192 // Get the tokens enclosed within the __pragma().
193 llvm::SmallVector<Token, 32> PragmaToks;
194 int NumParens = 0;
195 Lex(Tok);
196 while (Tok.isNot(tok::eof)) {
197 if (Tok.is(tok::l_paren))
198 NumParens++;
199 else if (Tok.is(tok::r_paren) && NumParens-- == 0)
200 break;
201 PragmaToks.push_back(Tok);
202 Lex(Tok);
203 }
204
John McCall3da92a92010-08-29 01:09:54 +0000205 if (Tok.is(tok::eof)) {
206 Diag(PragmaLoc, diag::err_unterminated___pragma);
207 return;
208 }
209
John McCall1ef8a2e2010-08-28 22:34:47 +0000210 // Build the pragma string.
211 std::string StrVal = " ";
212 for (llvm::SmallVector<Token, 32>::iterator I =
213 PragmaToks.begin(), E = PragmaToks.end(); I != E; ++I) {
214 StrVal += getSpelling(*I);
215 }
216
217 SourceLocation RParenLoc = Tok.getLocation();
218
219 Handle_Pragma(StrVal, PragmaLoc, RParenLoc);
220
221 // Finally, return whatever came after the pragma directive.
222 return Lex(Tok);
223}
224
225void Preprocessor::Handle_Pragma(const std::string &StrVal,
226 SourceLocation PragmaLoc,
227 SourceLocation RParenLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +0000228
Reid Spencer5f016e22007-07-11 17:01:13 +0000229 // Plop the string (including the newline and trailing null) into a buffer
230 // where we can lex it.
Chris Lattner47246be2009-01-26 19:29:26 +0000231 Token TmpTok;
232 TmpTok.startToken();
233 CreateString(&StrVal[0], StrVal.size(), TmpTok);
234 SourceLocation TokLoc = TmpTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000235
Reid Spencer5f016e22007-07-11 17:01:13 +0000236 // Make and enter a lexer object so that we lex and expand the tokens just
237 // like any others.
Chris Lattnere7fb4842009-02-15 20:52:18 +0000238 Lexer *TL = Lexer::Create_PragmaLexer(TokLoc, PragmaLoc, RParenLoc,
Chris Lattner1fa49532009-03-08 08:08:45 +0000239 StrVal.size(), *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000240
241 EnterSourceFileWithLexer(TL, 0);
242
243 // With everything set up, lex this as a #pragma directive.
244 HandlePragmaDirective();
Reid Spencer5f016e22007-07-11 17:01:13 +0000245}
246
247
248
249/// HandlePragmaOnce - Handle #pragma once. OnceTok is the 'once'.
250///
Chris Lattnerd2177732007-07-20 16:59:19 +0000251void Preprocessor::HandlePragmaOnce(Token &OnceTok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000252 if (isInPrimaryFile()) {
253 Diag(OnceTok, diag::pp_pragma_once_in_main_file);
254 return;
255 }
Mike Stump1eb44332009-09-09 15:08:12 +0000256
Reid Spencer5f016e22007-07-11 17:01:13 +0000257 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
Reid Spencer5f016e22007-07-11 17:01:13 +0000258 // Mark the file as a once-only file now.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000259 HeaderInfo.MarkFileIncludeOnce(getCurrentFileLexer()->getFileEntry());
Reid Spencer5f016e22007-07-11 17:01:13 +0000260}
261
Chris Lattner22434492007-12-19 19:38:36 +0000262void Preprocessor::HandlePragmaMark() {
Ted Kremenek17ff58a2008-11-19 22:21:33 +0000263 assert(CurPPLexer && "No current lexer?");
Chris Lattner6896a372009-06-15 05:02:34 +0000264 if (CurLexer)
265 CurLexer->ReadToEndOfLine();
266 else
267 CurPTHLexer->DiscardToEndOfLine();
Chris Lattner22434492007-12-19 19:38:36 +0000268}
269
270
Reid Spencer5f016e22007-07-11 17:01:13 +0000271/// HandlePragmaPoison - Handle #pragma GCC poison. PoisonTok is the 'poison'.
272///
Chris Lattnerd2177732007-07-20 16:59:19 +0000273void Preprocessor::HandlePragmaPoison(Token &PoisonTok) {
274 Token Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000275
276 while (1) {
277 // Read the next token to poison. While doing this, pretend that we are
278 // skipping while reading the identifier to poison.
279 // This avoids errors on code like:
280 // #pragma GCC poison X
281 // #pragma GCC poison X
Ted Kremenek68a91d52008-11-18 01:12:54 +0000282 if (CurPPLexer) CurPPLexer->LexingRawMode = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000283 LexUnexpandedToken(Tok);
Ted Kremenek68a91d52008-11-18 01:12:54 +0000284 if (CurPPLexer) CurPPLexer->LexingRawMode = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000285
Reid Spencer5f016e22007-07-11 17:01:13 +0000286 // If we reached the end of line, we're done.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000287 if (Tok.is(tok::eom)) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000288
Reid Spencer5f016e22007-07-11 17:01:13 +0000289 // Can only poison identifiers.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000290 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000291 Diag(Tok, diag::err_pp_invalid_poison);
292 return;
293 }
Mike Stump1eb44332009-09-09 15:08:12 +0000294
Reid Spencer5f016e22007-07-11 17:01:13 +0000295 // Look up the identifier info for the token. We disabled identifier lookup
296 // by saying we're skipping contents, so we need to do this manually.
297 IdentifierInfo *II = LookUpIdentifierInfo(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000298
Reid Spencer5f016e22007-07-11 17:01:13 +0000299 // Already poisoned.
300 if (II->isPoisoned()) continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000301
Reid Spencer5f016e22007-07-11 17:01:13 +0000302 // If this is a macro identifier, emit a warning.
Chris Lattner0edde552007-10-07 08:04:56 +0000303 if (II->hasMacroDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000304 Diag(Tok, diag::pp_poisoning_existing_macro);
Mike Stump1eb44332009-09-09 15:08:12 +0000305
Reid Spencer5f016e22007-07-11 17:01:13 +0000306 // Finally, poison it!
307 II->setIsPoisoned();
308 }
309}
310
311/// HandlePragmaSystemHeader - Implement #pragma GCC system_header. We know
312/// that the whole directive has been parsed.
Chris Lattnerd2177732007-07-20 16:59:19 +0000313void Preprocessor::HandlePragmaSystemHeader(Token &SysHeaderTok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000314 if (isInPrimaryFile()) {
315 Diag(SysHeaderTok, diag::pp_pragma_sysheader_in_main_file);
316 return;
317 }
Mike Stump1eb44332009-09-09 15:08:12 +0000318
Reid Spencer5f016e22007-07-11 17:01:13 +0000319 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
Ted Kremenek35c10c22008-11-20 01:45:11 +0000320 PreprocessorLexer *TheLexer = getCurrentFileLexer();
Mike Stump1eb44332009-09-09 15:08:12 +0000321
Reid Spencer5f016e22007-07-11 17:01:13 +0000322 // Mark the file as a system header.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000323 HeaderInfo.MarkFileSystemHeader(TheLexer->getFileEntry());
Mike Stump1eb44332009-09-09 15:08:12 +0000324
325
Chris Lattner6896a372009-06-15 05:02:34 +0000326 PresumedLoc PLoc = SourceMgr.getPresumedLoc(SysHeaderTok.getLocation());
327 unsigned FilenameLen = strlen(PLoc.getFilename());
328 unsigned FilenameID = SourceMgr.getLineTableFilenameID(PLoc.getFilename(),
329 FilenameLen);
Mike Stump1eb44332009-09-09 15:08:12 +0000330
Chris Lattner6896a372009-06-15 05:02:34 +0000331 // Emit a line marker. This will change any source locations from this point
332 // forward to realize they are in a system header.
333 // Create a line note with this information.
334 SourceMgr.AddLineNote(SysHeaderTok.getLocation(), PLoc.getLine(), FilenameID,
335 false, false, true, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000336
Reid Spencer5f016e22007-07-11 17:01:13 +0000337 // Notify the client, if desired, that we are in a new source file.
338 if (Callbacks)
Ted Kremenek35c10c22008-11-20 01:45:11 +0000339 Callbacks->FileChanged(SysHeaderTok.getLocation(),
Chris Lattner0b9e7362008-09-26 21:18:42 +0000340 PPCallbacks::SystemHeaderPragma, SrcMgr::C_System);
Reid Spencer5f016e22007-07-11 17:01:13 +0000341}
342
343/// HandlePragmaDependency - Handle #pragma GCC dependency "foo" blah.
344///
Chris Lattnerd2177732007-07-20 16:59:19 +0000345void Preprocessor::HandlePragmaDependency(Token &DependencyTok) {
346 Token FilenameTok;
Ted Kremenek68a91d52008-11-18 01:12:54 +0000347 CurPPLexer->LexIncludeFilename(FilenameTok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000348
349 // If the token kind is EOM, the error has already been diagnosed.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000350 if (FilenameTok.is(tok::eom))
Reid Spencer5f016e22007-07-11 17:01:13 +0000351 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000352
Reid Spencer5f016e22007-07-11 17:01:13 +0000353 // Reserve a buffer to get the spelling.
Chris Lattnera1394812010-01-10 01:35:12 +0000354 llvm::SmallString<128> FilenameBuffer;
Douglas Gregor453091c2010-03-16 22:30:13 +0000355 bool Invalid = false;
356 llvm::StringRef Filename = getSpelling(FilenameTok, FilenameBuffer, &Invalid);
357 if (Invalid)
358 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000359
Chris Lattnera1394812010-01-10 01:35:12 +0000360 bool isAngled =
361 GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
Reid Spencer5f016e22007-07-11 17:01:13 +0000362 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
363 // error.
Chris Lattnera1394812010-01-10 01:35:12 +0000364 if (Filename.empty())
Reid Spencer5f016e22007-07-11 17:01:13 +0000365 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000366
Reid Spencer5f016e22007-07-11 17:01:13 +0000367 // Search include directories for this file.
368 const DirectoryLookup *CurDir;
Chris Lattnerf45b6462010-01-22 00:14:44 +0000369 const FileEntry *File = LookupFile(Filename, isAngled, 0, CurDir);
Chris Lattner56b05c82008-11-18 08:02:48 +0000370 if (File == 0) {
Chris Lattnera1394812010-01-10 01:35:12 +0000371 Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
Chris Lattner56b05c82008-11-18 08:02:48 +0000372 return;
373 }
Mike Stump1eb44332009-09-09 15:08:12 +0000374
Chris Lattner2b2453a2009-01-17 06:22:33 +0000375 const FileEntry *CurFile = getCurrentFileLexer()->getFileEntry();
Reid Spencer5f016e22007-07-11 17:01:13 +0000376
377 // If this file is older than the file it depends on, emit a diagnostic.
378 if (CurFile && CurFile->getModificationTime() < File->getModificationTime()) {
379 // Lex tokens at the end of the message and include them in the message.
380 std::string Message;
381 Lex(DependencyTok);
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000382 while (DependencyTok.isNot(tok::eom)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000383 Message += getSpelling(DependencyTok) + " ";
384 Lex(DependencyTok);
385 }
Mike Stump1eb44332009-09-09 15:08:12 +0000386
Chris Lattner96de2592010-09-05 23:16:09 +0000387 // Remove the trailing ' ' if present.
388 if (!Message.empty())
389 Message.erase(Message.end()-1);
Chris Lattner56b05c82008-11-18 08:02:48 +0000390 Diag(FilenameTok, diag::pp_out_of_date_dependency) << Message;
Reid Spencer5f016e22007-07-11 17:01:13 +0000391 }
392}
393
Chris Lattner636c5ef2009-01-16 08:21:25 +0000394/// HandlePragmaComment - Handle the microsoft #pragma comment extension. The
395/// syntax is:
396/// #pragma comment(linker, "foo")
397/// 'linker' is one of five identifiers: compiler, exestr, lib, linker, user.
398/// "foo" is a string, which is fully macro expanded, and permits string
Gabor Greifd7ee3492009-03-17 11:39:38 +0000399/// concatenation, embedded escape characters etc. See MSDN for more details.
Chris Lattner636c5ef2009-01-16 08:21:25 +0000400void Preprocessor::HandlePragmaComment(Token &Tok) {
401 SourceLocation CommentLoc = Tok.getLocation();
402 Lex(Tok);
403 if (Tok.isNot(tok::l_paren)) {
404 Diag(CommentLoc, diag::err_pragma_comment_malformed);
405 return;
406 }
Mike Stump1eb44332009-09-09 15:08:12 +0000407
Chris Lattner636c5ef2009-01-16 08:21:25 +0000408 // Read the identifier.
409 Lex(Tok);
410 if (Tok.isNot(tok::identifier)) {
411 Diag(CommentLoc, diag::err_pragma_comment_malformed);
412 return;
413 }
Mike Stump1eb44332009-09-09 15:08:12 +0000414
Chris Lattner636c5ef2009-01-16 08:21:25 +0000415 // Verify that this is one of the 5 whitelisted options.
416 // FIXME: warn that 'exestr' is deprecated.
417 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000418 if (!II->isStr("compiler") && !II->isStr("exestr") && !II->isStr("lib") &&
Chris Lattner636c5ef2009-01-16 08:21:25 +0000419 !II->isStr("linker") && !II->isStr("user")) {
420 Diag(Tok.getLocation(), diag::err_pragma_comment_unknown_kind);
421 return;
422 }
Mike Stump1eb44332009-09-09 15:08:12 +0000423
Chris Lattnera9d91452009-01-16 18:59:23 +0000424 // Read the optional string if present.
Chris Lattner636c5ef2009-01-16 08:21:25 +0000425 Lex(Tok);
Chris Lattnera9d91452009-01-16 18:59:23 +0000426 std::string ArgumentString;
Chris Lattner636c5ef2009-01-16 08:21:25 +0000427 if (Tok.is(tok::comma)) {
Chris Lattnera9d91452009-01-16 18:59:23 +0000428 Lex(Tok); // eat the comma.
Chris Lattner636c5ef2009-01-16 08:21:25 +0000429
430 // We need at least one string.
Chris Lattneredaf8772009-04-19 23:16:58 +0000431 if (Tok.isNot(tok::string_literal)) {
Chris Lattner636c5ef2009-01-16 08:21:25 +0000432 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
433 return;
434 }
435
436 // String concatenation allows multiple strings, which can even come from
437 // macro expansion.
438 // "foo " "bar" "Baz"
Chris Lattnera9d91452009-01-16 18:59:23 +0000439 llvm::SmallVector<Token, 4> StrToks;
Chris Lattneredaf8772009-04-19 23:16:58 +0000440 while (Tok.is(tok::string_literal)) {
Chris Lattnera9d91452009-01-16 18:59:23 +0000441 StrToks.push_back(Tok);
Chris Lattner636c5ef2009-01-16 08:21:25 +0000442 Lex(Tok);
Chris Lattnera9d91452009-01-16 18:59:23 +0000443 }
444
445 // Concatenate and parse the strings.
446 StringLiteralParser Literal(&StrToks[0], StrToks.size(), *this);
447 assert(!Literal.AnyWide && "Didn't allow wide strings in");
448 if (Literal.hadError)
449 return;
450 if (Literal.Pascal) {
451 Diag(StrToks[0].getLocation(), diag::err_pragma_comment_malformed);
452 return;
453 }
454
455 ArgumentString = std::string(Literal.GetString(),
456 Literal.GetString()+Literal.GetStringLength());
Chris Lattner636c5ef2009-01-16 08:21:25 +0000457 }
Mike Stump1eb44332009-09-09 15:08:12 +0000458
Chris Lattnera9d91452009-01-16 18:59:23 +0000459 // FIXME: If the kind is "compiler" warn if the string is present (it is
460 // ignored).
461 // FIXME: 'lib' requires a comment string.
462 // FIXME: 'linker' requires a comment string, and has a specific list of
463 // things that are allowable.
Mike Stump1eb44332009-09-09 15:08:12 +0000464
Chris Lattner636c5ef2009-01-16 08:21:25 +0000465 if (Tok.isNot(tok::r_paren)) {
466 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
467 return;
468 }
Chris Lattnera9d91452009-01-16 18:59:23 +0000469 Lex(Tok); // eat the r_paren.
Chris Lattner636c5ef2009-01-16 08:21:25 +0000470
471 if (Tok.isNot(tok::eom)) {
472 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
473 return;
474 }
Mike Stump1eb44332009-09-09 15:08:12 +0000475
Chris Lattnera9d91452009-01-16 18:59:23 +0000476 // If the pragma is lexically sound, notify any interested PPCallbacks.
Chris Lattner172e3362009-01-16 19:01:46 +0000477 if (Callbacks)
478 Callbacks->PragmaComment(CommentLoc, II, ArgumentString);
Chris Lattner636c5ef2009-01-16 08:21:25 +0000479}
480
Chris Lattnerabfe0942010-06-26 17:11:39 +0000481/// HandlePragmaMessage - Handle the microsoft #pragma message extension. The
482/// syntax is:
483/// #pragma message(messagestring)
484/// messagestring is a string, which is fully macro expanded, and permits string
485/// concatenation, embedded escape characters etc. See MSDN for more details.
486void Preprocessor::HandlePragmaMessage(Token &Tok) {
487 SourceLocation MessageLoc = Tok.getLocation();
488 Lex(Tok);
489 if (Tok.isNot(tok::l_paren)) {
490 Diag(MessageLoc, diag::err_pragma_message_malformed);
491 return;
492 }
Chris Lattner636c5ef2009-01-16 08:21:25 +0000493
Chris Lattnerabfe0942010-06-26 17:11:39 +0000494 // Read the string.
495 Lex(Tok);
496
497
498 // We need at least one string.
499 if (Tok.isNot(tok::string_literal)) {
500 Diag(Tok.getLocation(), diag::err_pragma_message_malformed);
501 return;
502 }
503
504 // String concatenation allows multiple strings, which can even come from
505 // macro expansion.
506 // "foo " "bar" "Baz"
507 llvm::SmallVector<Token, 4> StrToks;
508 while (Tok.is(tok::string_literal)) {
509 StrToks.push_back(Tok);
510 Lex(Tok);
511 }
512
513 // Concatenate and parse the strings.
514 StringLiteralParser Literal(&StrToks[0], StrToks.size(), *this);
515 assert(!Literal.AnyWide && "Didn't allow wide strings in");
516 if (Literal.hadError)
517 return;
518 if (Literal.Pascal) {
519 Diag(StrToks[0].getLocation(), diag::err_pragma_message_malformed);
520 return;
521 }
522
523 llvm::StringRef MessageString(Literal.GetString(), Literal.GetStringLength());
524
525 if (Tok.isNot(tok::r_paren)) {
526 Diag(Tok.getLocation(), diag::err_pragma_message_malformed);
527 return;
528 }
529 Lex(Tok); // eat the r_paren.
530
531 if (Tok.isNot(tok::eom)) {
532 Diag(Tok.getLocation(), diag::err_pragma_message_malformed);
533 return;
534 }
535
536 // Output the message.
537 Diag(MessageLoc, diag::warn_pragma_message) << MessageString;
538
539 // If the pragma is lexically sound, notify any interested PPCallbacks.
540 if (Callbacks)
541 Callbacks->PragmaMessage(MessageLoc, MessageString);
542}
Chris Lattner636c5ef2009-01-16 08:21:25 +0000543
Chris Lattnerf47724b2010-08-17 15:55:45 +0000544/// ParsePragmaPushOrPopMacro - Handle parsing of pragma push_macro/pop_macro.
545/// Return the IdentifierInfo* associated with the macro to push or pop.
546IdentifierInfo *Preprocessor::ParsePragmaPushOrPopMacro(Token &Tok) {
547 // Remember the pragma token location.
548 Token PragmaTok = Tok;
549
550 // Read the '('.
551 Lex(Tok);
552 if (Tok.isNot(tok::l_paren)) {
553 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
554 << getSpelling(PragmaTok);
555 return 0;
556 }
557
558 // Read the macro name string.
559 Lex(Tok);
560 if (Tok.isNot(tok::string_literal)) {
561 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
562 << getSpelling(PragmaTok);
563 return 0;
564 }
565
566 // Remember the macro string.
567 std::string StrVal = getSpelling(Tok);
568
569 // Read the ')'.
570 Lex(Tok);
571 if (Tok.isNot(tok::r_paren)) {
572 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
573 << getSpelling(PragmaTok);
574 return 0;
575 }
576
577 assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
578 "Invalid string token!");
579
580 // Create a Token from the string.
581 Token MacroTok;
582 MacroTok.startToken();
583 MacroTok.setKind(tok::identifier);
584 CreateString(&StrVal[1], StrVal.size() - 2, MacroTok);
585
586 // Get the IdentifierInfo of MacroToPushTok.
587 return LookUpIdentifierInfo(MacroTok);
588}
589
590/// HandlePragmaPushMacro - Handle #pragma push_macro.
591/// The syntax is:
592/// #pragma push_macro("macro")
593void Preprocessor::HandlePragmaPushMacro(Token &PushMacroTok) {
594 // Parse the pragma directive and get the macro IdentifierInfo*.
595 IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PushMacroTok);
596 if (!IdentInfo) return;
597
598 // Get the MacroInfo associated with IdentInfo.
599 MacroInfo *MI = getMacroInfo(IdentInfo);
600
601 MacroInfo *MacroCopyToPush = 0;
602 if (MI) {
603 // Make a clone of MI.
604 MacroCopyToPush = CloneMacroInfo(*MI);
605
606 // Allow the original MacroInfo to be redefined later.
607 MI->setIsAllowRedefinitionsWithoutWarning(true);
608 }
609
610 // Push the cloned MacroInfo so we can retrieve it later.
611 PragmaPushMacroInfo[IdentInfo].push_back(MacroCopyToPush);
612}
613
614/// HandlePragmaPopMacro - Handle #pragma push_macro.
615/// The syntax is:
616/// #pragma pop_macro("macro")
617void Preprocessor::HandlePragmaPopMacro(Token &PopMacroTok) {
618 SourceLocation MessageLoc = PopMacroTok.getLocation();
619
620 // Parse the pragma directive and get the macro IdentifierInfo*.
621 IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PopMacroTok);
622 if (!IdentInfo) return;
623
624 // Find the vector<MacroInfo*> associated with the macro.
625 llvm::DenseMap<IdentifierInfo*, std::vector<MacroInfo*> >::iterator iter =
626 PragmaPushMacroInfo.find(IdentInfo);
627 if (iter != PragmaPushMacroInfo.end()) {
628 // Release the MacroInfo currently associated with IdentInfo.
629 MacroInfo *CurrentMI = getMacroInfo(IdentInfo);
630 if (CurrentMI) ReleaseMacroInfo(CurrentMI);
631
632 // Get the MacroInfo we want to reinstall.
633 MacroInfo *MacroToReInstall = iter->second.back();
634
635 // Reinstall the previously pushed macro.
636 setMacroInfo(IdentInfo, MacroToReInstall);
637
638 // Pop PragmaPushMacroInfo stack.
639 iter->second.pop_back();
640 if (iter->second.size() == 0)
641 PragmaPushMacroInfo.erase(iter);
642 } else {
643 Diag(MessageLoc, diag::warn_pragma_pop_macro_no_push)
644 << IdentInfo->getName();
645 }
646}
Reid Spencer5f016e22007-07-11 17:01:13 +0000647
648/// AddPragmaHandler - Add the specified pragma handler to the preprocessor.
649/// If 'Namespace' is non-null, then it is a token required to exist on the
650/// pragma line before the pragma string starts, e.g. "STDC" or "GCC".
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000651void Preprocessor::AddPragmaHandler(llvm::StringRef Namespace,
Reid Spencer5f016e22007-07-11 17:01:13 +0000652 PragmaHandler *Handler) {
653 PragmaNamespace *InsertNS = PragmaHandlers;
Mike Stump1eb44332009-09-09 15:08:12 +0000654
Reid Spencer5f016e22007-07-11 17:01:13 +0000655 // If this is specified to be in a namespace, step down into it.
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000656 if (!Namespace.empty()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000657 // If there is already a pragma handler with the name of this namespace,
658 // we either have an error (directive with the same name as a namespace) or
659 // we already have the namespace to insert into.
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000660 if (PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000661 InsertNS = Existing->getIfNamespace();
662 assert(InsertNS != 0 && "Cannot have a pragma namespace and pragma"
663 " handler with the same name!");
664 } else {
665 // Otherwise, this namespace doesn't exist yet, create and insert the
666 // handler for it.
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000667 InsertNS = new PragmaNamespace(Namespace);
Reid Spencer5f016e22007-07-11 17:01:13 +0000668 PragmaHandlers->AddPragma(InsertNS);
669 }
670 }
Mike Stump1eb44332009-09-09 15:08:12 +0000671
Reid Spencer5f016e22007-07-11 17:01:13 +0000672 // Check to make sure we don't already have a pragma for this identifier.
673 assert(!InsertNS->FindHandler(Handler->getName()) &&
674 "Pragma handler already exists for this identifier!");
675 InsertNS->AddPragma(Handler);
676}
677
Daniel Dunbar40950802008-10-04 19:17:46 +0000678/// RemovePragmaHandler - Remove the specific pragma handler from the
679/// preprocessor. If \arg Namespace is non-null, then it should be the
680/// namespace that \arg Handler was added to. It is an error to remove
681/// a handler that has not been registered.
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000682void Preprocessor::RemovePragmaHandler(llvm::StringRef Namespace,
Daniel Dunbar40950802008-10-04 19:17:46 +0000683 PragmaHandler *Handler) {
684 PragmaNamespace *NS = PragmaHandlers;
Mike Stump1eb44332009-09-09 15:08:12 +0000685
Daniel Dunbar40950802008-10-04 19:17:46 +0000686 // If this is specified to be in a namespace, step down into it.
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000687 if (!Namespace.empty()) {
688 PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace);
Daniel Dunbar40950802008-10-04 19:17:46 +0000689 assert(Existing && "Namespace containing handler does not exist!");
690
691 NS = Existing->getIfNamespace();
692 assert(NS && "Invalid namespace, registered as a regular pragma handler!");
693 }
694
695 NS->RemovePragmaHandler(Handler);
Mike Stump1eb44332009-09-09 15:08:12 +0000696
Daniel Dunbar40950802008-10-04 19:17:46 +0000697 // If this is a non-default namespace and it is now empty, remove
698 // it.
699 if (NS != PragmaHandlers && NS->IsEmpty())
700 PragmaHandlers->RemovePragmaHandler(NS);
701}
702
Reid Spencer5f016e22007-07-11 17:01:13 +0000703namespace {
Chris Lattner22434492007-12-19 19:38:36 +0000704/// PragmaOnceHandler - "#pragma once" marks the file as atomically included.
Reid Spencer5f016e22007-07-11 17:01:13 +0000705struct PragmaOnceHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000706 PragmaOnceHandler() : PragmaHandler("once") {}
Chris Lattnerd2177732007-07-20 16:59:19 +0000707 virtual void HandlePragma(Preprocessor &PP, Token &OnceTok) {
Chris Lattner35410d52009-04-14 05:07:49 +0000708 PP.CheckEndOfDirective("pragma once");
Reid Spencer5f016e22007-07-11 17:01:13 +0000709 PP.HandlePragmaOnce(OnceTok);
710 }
711};
712
Chris Lattner22434492007-12-19 19:38:36 +0000713/// PragmaMarkHandler - "#pragma mark ..." is ignored by the compiler, and the
714/// rest of the line is not lexed.
715struct PragmaMarkHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000716 PragmaMarkHandler() : PragmaHandler("mark") {}
Chris Lattner22434492007-12-19 19:38:36 +0000717 virtual void HandlePragma(Preprocessor &PP, Token &MarkTok) {
718 PP.HandlePragmaMark();
719 }
720};
721
722/// PragmaPoisonHandler - "#pragma poison x" marks x as not usable.
Reid Spencer5f016e22007-07-11 17:01:13 +0000723struct PragmaPoisonHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000724 PragmaPoisonHandler() : PragmaHandler("poison") {}
Chris Lattnerd2177732007-07-20 16:59:19 +0000725 virtual void HandlePragma(Preprocessor &PP, Token &PoisonTok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000726 PP.HandlePragmaPoison(PoisonTok);
727 }
728};
729
Chris Lattner22434492007-12-19 19:38:36 +0000730/// PragmaSystemHeaderHandler - "#pragma system_header" marks the current file
731/// as a system header, which silences warnings in it.
Reid Spencer5f016e22007-07-11 17:01:13 +0000732struct PragmaSystemHeaderHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000733 PragmaSystemHeaderHandler() : PragmaHandler("system_header") {}
Chris Lattnerd2177732007-07-20 16:59:19 +0000734 virtual void HandlePragma(Preprocessor &PP, Token &SHToken) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000735 PP.HandlePragmaSystemHeader(SHToken);
Chris Lattner35410d52009-04-14 05:07:49 +0000736 PP.CheckEndOfDirective("pragma");
Reid Spencer5f016e22007-07-11 17:01:13 +0000737 }
738};
739struct PragmaDependencyHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000740 PragmaDependencyHandler() : PragmaHandler("dependency") {}
Chris Lattnerd2177732007-07-20 16:59:19 +0000741 virtual void HandlePragma(Preprocessor &PP, Token &DepToken) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000742 PP.HandlePragmaDependency(DepToken);
743 }
744};
Mike Stump1eb44332009-09-09 15:08:12 +0000745
Daniel Dunbarabf7b722010-07-28 15:40:33 +0000746struct PragmaDebugHandler : public PragmaHandler {
747 PragmaDebugHandler() : PragmaHandler("__debug") {}
748 virtual void HandlePragma(Preprocessor &PP, Token &DepToken) {
749 Token Tok;
750 PP.LexUnexpandedToken(Tok);
751 if (Tok.isNot(tok::identifier)) {
Douglas Gregor6493a4d2010-08-30 15:15:34 +0000752 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Daniel Dunbarabf7b722010-07-28 15:40:33 +0000753 return;
754 }
755 IdentifierInfo *II = Tok.getIdentifierInfo();
756
Daniel Dunbar55054132010-08-17 22:32:48 +0000757 if (II->isStr("assert")) {
758 assert(0 && "This is an assertion!");
Daniel Dunbarabf7b722010-07-28 15:40:33 +0000759 } else if (II->isStr("crash")) {
Daniel Dunbar55054132010-08-17 22:32:48 +0000760 *(volatile int*) 0x11 = 0;
761 } else if (II->isStr("llvm_fatal_error")) {
762 llvm::report_fatal_error("#pragma clang __debug llvm_fatal_error");
763 } else if (II->isStr("llvm_unreachable")) {
764 llvm_unreachable("#pragma clang __debug llvm_unreachable");
765 } else if (II->isStr("overflow_stack")) {
766 DebugOverflowStack();
Daniel Dunbarff759a62010-08-18 23:09:23 +0000767 } else if (II->isStr("handle_crash")) {
768 llvm::CrashRecoveryContext *CRC =llvm::CrashRecoveryContext::GetCurrent();
769 if (CRC)
770 CRC->HandleCrash();
Daniel Dunbar55054132010-08-17 22:32:48 +0000771 } else {
772 PP.Diag(Tok, diag::warn_pragma_debug_unexpected_command)
773 << II->getName();
Daniel Dunbarabf7b722010-07-28 15:40:33 +0000774 }
775 }
776
777 void DebugOverflowStack() {
778 DebugOverflowStack();
779 }
Daniel Dunbarabf7b722010-07-28 15:40:33 +0000780};
781
Chris Lattneredaf8772009-04-19 23:16:58 +0000782/// PragmaDiagnosticHandler - e.g. '#pragma GCC diagnostic ignored "-Wformat"'
783struct PragmaDiagnosticHandler : public PragmaHandler {
Chris Lattner04ae2df2009-07-12 21:18:45 +0000784public:
Douglas Gregor6493a4d2010-08-30 15:15:34 +0000785 explicit PragmaDiagnosticHandler() : PragmaHandler("diagnostic") {}
Chris Lattneredaf8772009-04-19 23:16:58 +0000786 virtual void HandlePragma(Preprocessor &PP, Token &DiagToken) {
787 Token Tok;
788 PP.LexUnexpandedToken(Tok);
789 if (Tok.isNot(tok::identifier)) {
Douglas Gregor6493a4d2010-08-30 15:15:34 +0000790 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Chris Lattneredaf8772009-04-19 23:16:58 +0000791 return;
792 }
793 IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000794
Chris Lattneredaf8772009-04-19 23:16:58 +0000795 diag::Mapping Map;
796 if (II->isStr("warning"))
797 Map = diag::MAP_WARNING;
798 else if (II->isStr("error"))
799 Map = diag::MAP_ERROR;
800 else if (II->isStr("ignored"))
801 Map = diag::MAP_IGNORE;
802 else if (II->isStr("fatal"))
803 Map = diag::MAP_FATAL;
Douglas Gregor6493a4d2010-08-30 15:15:34 +0000804 else if (II->isStr("pop")) {
805 if (!PP.getDiagnostics().popMappings())
806 PP.Diag(Tok, diag::warn_pragma_diagnostic_cannot_pop);
Chris Lattner04ae2df2009-07-12 21:18:45 +0000807
Douglas Gregor6493a4d2010-08-30 15:15:34 +0000808 return;
809 } else if (II->isStr("push")) {
810 PP.getDiagnostics().pushMappings();
Chris Lattner04ae2df2009-07-12 21:18:45 +0000811 return;
812 } else {
Douglas Gregor6493a4d2010-08-30 15:15:34 +0000813 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Chris Lattneredaf8772009-04-19 23:16:58 +0000814 return;
815 }
Mike Stump1eb44332009-09-09 15:08:12 +0000816
Chris Lattneredaf8772009-04-19 23:16:58 +0000817 PP.LexUnexpandedToken(Tok);
818
819 // We need at least one string.
820 if (Tok.isNot(tok::string_literal)) {
821 PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token);
822 return;
823 }
Mike Stump1eb44332009-09-09 15:08:12 +0000824
Chris Lattneredaf8772009-04-19 23:16:58 +0000825 // String concatenation allows multiple strings, which can even come from
826 // macro expansion.
827 // "foo " "bar" "Baz"
828 llvm::SmallVector<Token, 4> StrToks;
829 while (Tok.is(tok::string_literal)) {
830 StrToks.push_back(Tok);
831 PP.LexUnexpandedToken(Tok);
832 }
Mike Stump1eb44332009-09-09 15:08:12 +0000833
Chris Lattneredaf8772009-04-19 23:16:58 +0000834 if (Tok.isNot(tok::eom)) {
835 PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token);
836 return;
837 }
Mike Stump1eb44332009-09-09 15:08:12 +0000838
Chris Lattneredaf8772009-04-19 23:16:58 +0000839 // Concatenate and parse the strings.
840 StringLiteralParser Literal(&StrToks[0], StrToks.size(), PP);
841 assert(!Literal.AnyWide && "Didn't allow wide strings in");
842 if (Literal.hadError)
843 return;
844 if (Literal.Pascal) {
Douglas Gregor6493a4d2010-08-30 15:15:34 +0000845 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Chris Lattneredaf8772009-04-19 23:16:58 +0000846 return;
847 }
Chris Lattner04ae2df2009-07-12 21:18:45 +0000848
Chris Lattneredaf8772009-04-19 23:16:58 +0000849 std::string WarningName(Literal.GetString(),
850 Literal.GetString()+Literal.GetStringLength());
851
852 if (WarningName.size() < 3 || WarningName[0] != '-' ||
853 WarningName[1] != 'W') {
854 PP.Diag(StrToks[0].getLocation(),
855 diag::warn_pragma_diagnostic_invalid_option);
856 return;
857 }
Mike Stump1eb44332009-09-09 15:08:12 +0000858
Chris Lattneredaf8772009-04-19 23:16:58 +0000859 if (PP.getDiagnostics().setDiagnosticGroupMapping(WarningName.c_str()+2,
860 Map))
861 PP.Diag(StrToks[0].getLocation(),
862 diag::warn_pragma_diagnostic_unknown_warning) << WarningName;
863 }
864};
Mike Stump1eb44332009-09-09 15:08:12 +0000865
Chris Lattner636c5ef2009-01-16 08:21:25 +0000866/// PragmaCommentHandler - "#pragma comment ...".
867struct PragmaCommentHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000868 PragmaCommentHandler() : PragmaHandler("comment") {}
Chris Lattner636c5ef2009-01-16 08:21:25 +0000869 virtual void HandlePragma(Preprocessor &PP, Token &CommentTok) {
870 PP.HandlePragmaComment(CommentTok);
871 }
872};
Mike Stump1eb44332009-09-09 15:08:12 +0000873
Chris Lattnerabfe0942010-06-26 17:11:39 +0000874/// PragmaMessageHandler - "#pragma message("...")".
875struct PragmaMessageHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000876 PragmaMessageHandler() : PragmaHandler("message") {}
Chris Lattnerabfe0942010-06-26 17:11:39 +0000877 virtual void HandlePragma(Preprocessor &PP, Token &CommentTok) {
878 PP.HandlePragmaMessage(CommentTok);
879 }
880};
881
Chris Lattnerf47724b2010-08-17 15:55:45 +0000882/// PragmaPushMacroHandler - "#pragma push_macro" saves the value of the
883/// macro on the top of the stack.
884struct PragmaPushMacroHandler : public PragmaHandler {
885 PragmaPushMacroHandler() : PragmaHandler("push_macro") {}
886 virtual void HandlePragma(Preprocessor &PP, Token &PushMacroTok) {
887 PP.HandlePragmaPushMacro(PushMacroTok);
888 }
889};
890
891
892/// PragmaPopMacroHandler - "#pragma pop_macro" sets the value of the
893/// macro to the value on the top of the stack.
894struct PragmaPopMacroHandler : public PragmaHandler {
895 PragmaPopMacroHandler() : PragmaHandler("pop_macro") {}
896 virtual void HandlePragma(Preprocessor &PP, Token &PopMacroTok) {
897 PP.HandlePragmaPopMacro(PopMacroTok);
898 }
899};
900
Chris Lattner062f2322009-04-19 21:20:35 +0000901// Pragma STDC implementations.
Chris Lattner6c5cf4a2009-04-19 21:50:08 +0000902
903enum STDCSetting {
904 STDC_ON, STDC_OFF, STDC_DEFAULT, STDC_INVALID
905};
Mike Stump1eb44332009-09-09 15:08:12 +0000906
Chris Lattner6c5cf4a2009-04-19 21:50:08 +0000907static STDCSetting LexOnOffSwitch(Preprocessor &PP) {
908 Token Tok;
909 PP.LexUnexpandedToken(Tok);
910
911 if (Tok.isNot(tok::identifier)) {
912 PP.Diag(Tok, diag::ext_stdc_pragma_syntax);
913 return STDC_INVALID;
914 }
915 IdentifierInfo *II = Tok.getIdentifierInfo();
916 STDCSetting Result;
917 if (II->isStr("ON"))
918 Result = STDC_ON;
919 else if (II->isStr("OFF"))
920 Result = STDC_OFF;
921 else if (II->isStr("DEFAULT"))
922 Result = STDC_DEFAULT;
923 else {
924 PP.Diag(Tok, diag::ext_stdc_pragma_syntax);
925 return STDC_INVALID;
926 }
927
928 // Verify that this is followed by EOM.
929 PP.LexUnexpandedToken(Tok);
930 if (Tok.isNot(tok::eom))
931 PP.Diag(Tok, diag::ext_stdc_pragma_syntax_eom);
932 return Result;
933}
Mike Stump1eb44332009-09-09 15:08:12 +0000934
Chris Lattner062f2322009-04-19 21:20:35 +0000935/// PragmaSTDC_FP_CONTRACTHandler - "#pragma STDC FP_CONTRACT ...".
936struct PragmaSTDC_FP_CONTRACTHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000937 PragmaSTDC_FP_CONTRACTHandler() : PragmaHandler("FP_CONTRACT") {}
Chris Lattnerf545be52009-04-19 21:25:37 +0000938 virtual void HandlePragma(Preprocessor &PP, Token &Tok) {
Chris Lattner6c5cf4a2009-04-19 21:50:08 +0000939 // We just ignore the setting of FP_CONTRACT. Since we don't do contractions
940 // at all, our default is OFF and setting it to ON is an optimization hint
941 // we can safely ignore. When we support -ffma or something, we would need
942 // to diagnose that we are ignoring FMA.
943 LexOnOffSwitch(PP);
Chris Lattner062f2322009-04-19 21:20:35 +0000944 }
945};
Mike Stump1eb44332009-09-09 15:08:12 +0000946
Chris Lattner062f2322009-04-19 21:20:35 +0000947/// PragmaSTDC_FENV_ACCESSHandler - "#pragma STDC FENV_ACCESS ...".
948struct PragmaSTDC_FENV_ACCESSHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000949 PragmaSTDC_FENV_ACCESSHandler() : PragmaHandler("FENV_ACCESS") {}
Chris Lattnerf545be52009-04-19 21:25:37 +0000950 virtual void HandlePragma(Preprocessor &PP, Token &Tok) {
Chris Lattner4d8aac32009-04-19 21:55:32 +0000951 if (LexOnOffSwitch(PP) == STDC_ON)
952 PP.Diag(Tok, diag::warn_stdc_fenv_access_not_supported);
Chris Lattner062f2322009-04-19 21:20:35 +0000953 }
954};
Mike Stump1eb44332009-09-09 15:08:12 +0000955
Chris Lattner062f2322009-04-19 21:20:35 +0000956/// PragmaSTDC_CX_LIMITED_RANGEHandler - "#pragma STDC CX_LIMITED_RANGE ...".
957struct PragmaSTDC_CX_LIMITED_RANGEHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000958 PragmaSTDC_CX_LIMITED_RANGEHandler()
959 : PragmaHandler("CX_LIMITED_RANGE") {}
Chris Lattnerf545be52009-04-19 21:25:37 +0000960 virtual void HandlePragma(Preprocessor &PP, Token &Tok) {
Chris Lattner6c5cf4a2009-04-19 21:50:08 +0000961 LexOnOffSwitch(PP);
Chris Lattner062f2322009-04-19 21:20:35 +0000962 }
963};
Mike Stump1eb44332009-09-09 15:08:12 +0000964
Chris Lattner062f2322009-04-19 21:20:35 +0000965/// PragmaSTDC_UnknownHandler - "#pragma STDC ...".
966struct PragmaSTDC_UnknownHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000967 PragmaSTDC_UnknownHandler() {}
Chris Lattnerf545be52009-04-19 21:25:37 +0000968 virtual void HandlePragma(Preprocessor &PP, Token &UnknownTok) {
Chris Lattner6c5cf4a2009-04-19 21:50:08 +0000969 // C99 6.10.6p2, unknown forms are not allowed.
Chris Lattnerf545be52009-04-19 21:25:37 +0000970 PP.Diag(UnknownTok, diag::ext_stdc_pragma_ignored);
Chris Lattner062f2322009-04-19 21:20:35 +0000971 }
972};
Mike Stump1eb44332009-09-09 15:08:12 +0000973
Reid Spencer5f016e22007-07-11 17:01:13 +0000974} // end anonymous namespace
975
976
977/// RegisterBuiltinPragmas - Install the standard preprocessor pragmas:
978/// #pragma GCC poison/system_header/dependency and #pragma once.
979void Preprocessor::RegisterBuiltinPragmas() {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000980 AddPragmaHandler(new PragmaOnceHandler());
981 AddPragmaHandler(new PragmaMarkHandler());
Chris Lattnerf47724b2010-08-17 15:55:45 +0000982 AddPragmaHandler(new PragmaPushMacroHandler());
983 AddPragmaHandler(new PragmaPopMacroHandler());
Mike Stump1eb44332009-09-09 15:08:12 +0000984
Chris Lattnere8fa06e2009-05-12 18:21:11 +0000985 // #pragma GCC ...
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000986 AddPragmaHandler("GCC", new PragmaPoisonHandler());
987 AddPragmaHandler("GCC", new PragmaSystemHeaderHandler());
988 AddPragmaHandler("GCC", new PragmaDependencyHandler());
Douglas Gregor6493a4d2010-08-30 15:15:34 +0000989 AddPragmaHandler("GCC", new PragmaDiagnosticHandler());
Chris Lattnere8fa06e2009-05-12 18:21:11 +0000990 // #pragma clang ...
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000991 AddPragmaHandler("clang", new PragmaPoisonHandler());
992 AddPragmaHandler("clang", new PragmaSystemHeaderHandler());
Daniel Dunbarabf7b722010-07-28 15:40:33 +0000993 AddPragmaHandler("clang", new PragmaDebugHandler());
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000994 AddPragmaHandler("clang", new PragmaDependencyHandler());
Douglas Gregor6493a4d2010-08-30 15:15:34 +0000995 AddPragmaHandler("clang", new PragmaDiagnosticHandler());
Chris Lattnere8fa06e2009-05-12 18:21:11 +0000996
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000997 AddPragmaHandler("STDC", new PragmaSTDC_FP_CONTRACTHandler());
998 AddPragmaHandler("STDC", new PragmaSTDC_FENV_ACCESSHandler());
999 AddPragmaHandler("STDC", new PragmaSTDC_CX_LIMITED_RANGEHandler());
Chris Lattner062f2322009-04-19 21:20:35 +00001000 AddPragmaHandler("STDC", new PragmaSTDC_UnknownHandler());
Mike Stump1eb44332009-09-09 15:08:12 +00001001
Chris Lattner636c5ef2009-01-16 08:21:25 +00001002 // MS extensions.
Chris Lattnerabfe0942010-06-26 17:11:39 +00001003 if (Features.Microsoft) {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001004 AddPragmaHandler(new PragmaCommentHandler());
1005 AddPragmaHandler(new PragmaMessageHandler());
Chris Lattnerabfe0942010-06-26 17:11:39 +00001006 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001007}