blob: af676670d1fac9fe1a82da7bdd15ed7197b0d9b8 [file] [log] [blame]
Chris Lattnerb8761832006-06-24 21:31:03 +00001//===--- Pragma.cpp - Pragma registration and handling --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerb8761832006-06-24 21:31:03 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattnerb694ba72006-07-02 22:41:36 +000010// This file implements the PragmaHandler/PragmaTable interfaces and implements
11// pragma related methods of the Preprocessor class.
Chris Lattnerb8761832006-06-24 21:31:03 +000012//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Lex/Pragma.h"
Chris Lattner07b019a2006-10-22 07:28:56 +000016#include "clang/Lex/HeaderSearch.h"
Chris Lattner262d4e32009-01-16 18:59:23 +000017#include "clang/Lex/LiteralSupport.h"
Chris Lattnerb8761832006-06-24 21:31:03 +000018#include "clang/Lex/Preprocessor.h"
Chris Lattnerc0a585d2010-08-17 15:55:45 +000019#include "clang/Lex/MacroInfo.h"
Chris Lattner60f36222009-01-29 05:15:15 +000020#include "clang/Lex/LexDiagnostic.h"
Chris Lattnerb694ba72006-07-02 22:41:36 +000021#include "clang/Basic/FileManager.h"
22#include "clang/Basic/SourceManager.h"
Daniel Dunbar211a7872010-08-18 23:09:23 +000023#include "llvm/Support/CrashRecoveryContext.h"
Daniel Dunbarf2cf3292010-08-17 22:32:48 +000024#include "llvm/Support/ErrorHandling.h"
Douglas Gregorc6d5edd2009-07-02 17:08:52 +000025#include <algorithm>
Chris Lattnerb8761832006-06-24 21:31:03 +000026using namespace clang;
27
28// Out-of-line destructor to provide a home for the class.
29PragmaHandler::~PragmaHandler() {
30}
31
Chris Lattner2e155302006-07-03 05:34:41 +000032//===----------------------------------------------------------------------===//
Daniel Dunbard839e772010-06-11 20:10:12 +000033// EmptyPragmaHandler Implementation.
34//===----------------------------------------------------------------------===//
35
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000036EmptyPragmaHandler::EmptyPragmaHandler() {}
Daniel Dunbard839e772010-06-11 20:10:12 +000037
38void EmptyPragmaHandler::HandlePragma(Preprocessor &PP, Token &FirstToken) {}
39
40//===----------------------------------------------------------------------===//
Chris Lattner2e155302006-07-03 05:34:41 +000041// PragmaNamespace Implementation.
42//===----------------------------------------------------------------------===//
43
44
45PragmaNamespace::~PragmaNamespace() {
Argyrios Kyrtzidis36745fd2010-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;
Chris Lattner2e155302006-07-03 05:34:41 +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 Kyrtzidis36745fd2010-07-13 09:07:17 +000055PragmaHandler *PragmaNamespace::FindHandler(llvm::StringRef Name,
Chris Lattner2e155302006-07-03 05:34:41 +000056 bool IgnoreNull) const {
Argyrios Kyrtzidis36745fd2010-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 Stump11289f42009-09-09 15:08:12 +000061
Argyrios Kyrtzidis36745fd2010-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);
Chris Lattner2e155302006-07-03 05:34:41 +000068}
69
Daniel Dunbar40596532008-10-04 19:17:46 +000070void PragmaNamespace::RemovePragmaHandler(PragmaHandler *Handler) {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000071 assert(Handlers.lookup(Handler->getName()) &&
72 "Handler not registered in this namespace");
73 Handlers.erase(Handler->getName());
Daniel Dunbar40596532008-10-04 19:17:46 +000074}
75
Chris Lattner146762e2007-07-20 16:59:19 +000076void PragmaNamespace::HandlePragma(Preprocessor &PP, Token &Tok) {
Chris Lattnerb8761832006-06-24 21:31:03 +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 Stump11289f42009-09-09 15:08:12 +000080
Chris Lattnerb8761832006-06-24 21:31:03 +000081 // Get the handler for this token. If there is no handler, ignore the pragma.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000082 PragmaHandler *Handler
83 = FindHandler(Tok.getIdentifierInfo() ? Tok.getIdentifierInfo()->getName()
84 : llvm::StringRef(),
85 /*IgnoreNull=*/false);
Chris Lattner21656f22009-04-19 21:10:26 +000086 if (Handler == 0) {
87 PP.Diag(Tok, diag::warn_pragma_ignored);
88 return;
89 }
Mike Stump11289f42009-09-09 15:08:12 +000090
Chris Lattnerb8761832006-06-24 21:31:03 +000091 // Otherwise, pass it down.
92 Handler->HandlePragma(PP, Tok);
93}
Chris Lattnerb694ba72006-07-02 22:41:36 +000094
Chris Lattnerb694ba72006-07-02 22:41:36 +000095//===----------------------------------------------------------------------===//
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 Stump11289f42009-09-09 15:08:12 +0000103
Chris Lattnerb694ba72006-07-02 22:41:36 +0000104 // Invoke the first level of pragma handlers which reads the namespace id.
Chris Lattner146762e2007-07-20 16:59:19 +0000105 Token Tok;
Chris Lattnerb694ba72006-07-02 22:41:36 +0000106 PragmaHandlers->HandlePragma(*this, Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000107
Chris Lattnerb694ba72006-07-02 22:41:36 +0000108 // If the pragma handler didn't read the rest of the line, consume it now.
Chris Lattnere7e65942009-06-18 05:55:53 +0000109 if (CurPPLexer && CurPPLexer->ParsingPreprocessorDirective)
Chris Lattnerb694ba72006-07-02 22:41:36 +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 Lattner146762e2007-07-20 16:59:19 +0000116void Preprocessor::Handle_Pragma(Token &Tok) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000117 // Remember the pragma token location.
118 SourceLocation PragmaLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000119
Chris Lattnerb694ba72006-07-02 22:41:36 +0000120 // Read the '('.
121 Lex(Tok);
Chris Lattner907dfe92008-11-18 07:59:24 +0000122 if (Tok.isNot(tok::l_paren)) {
123 Diag(PragmaLoc, diag::err__Pragma_malformed);
124 return;
125 }
Chris Lattnerb694ba72006-07-02 22:41:36 +0000126
127 // Read the '"..."'.
128 Lex(Tok);
Chris Lattner907dfe92008-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 Stump11289f42009-09-09 15:08:12 +0000133
Chris Lattnerb694ba72006-07-02 22:41:36 +0000134 // Remember the string.
135 std::string StrVal = getSpelling(Tok);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000136
137 // Read the ')'.
138 Lex(Tok);
Chris Lattner907dfe92008-11-18 07:59:24 +0000139 if (Tok.isNot(tok::r_paren)) {
140 Diag(PragmaLoc, diag::err__Pragma_malformed);
141 return;
142 }
Mike Stump11289f42009-09-09 15:08:12 +0000143
Chris Lattner9dc9c202009-02-15 20:52:18 +0000144 SourceLocation RParenLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000145
Chris Lattner262d4e32009-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."
Chris Lattnerb694ba72006-07-02 22:41:36 +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 Stump11289f42009-09-09 15:08:12 +0000155
Chris Lattnerb694ba72006-07-02 22:41:36 +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 Stump11289f42009-09-09 15:08:12 +0000159
Chris Lattnerfa217bd2009-03-08 08:08:45 +0000160 // Replace the terminating quote with a \n.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000161 StrVal[StrVal.size()-1] = '\n';
Mike Stump11289f42009-09-09 15:08:12 +0000162
Chris Lattnerb694ba72006-07-02 22:41:36 +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 McCall89e925d2010-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
205 // Build the pragma string.
206 std::string StrVal = " ";
207 for (llvm::SmallVector<Token, 32>::iterator I =
208 PragmaToks.begin(), E = PragmaToks.end(); I != E; ++I) {
209 StrVal += getSpelling(*I);
210 }
211
212 SourceLocation RParenLoc = Tok.getLocation();
213
214 Handle_Pragma(StrVal, PragmaLoc, RParenLoc);
215
216 // Finally, return whatever came after the pragma directive.
217 return Lex(Tok);
218}
219
220void Preprocessor::Handle_Pragma(const std::string &StrVal,
221 SourceLocation PragmaLoc,
222 SourceLocation RParenLoc) {
Mike Stump11289f42009-09-09 15:08:12 +0000223
Chris Lattnerf918bf72006-07-19 05:01:18 +0000224 // Plop the string (including the newline and trailing null) into a buffer
225 // where we can lex it.
Chris Lattner5a7971e2009-01-26 19:29:26 +0000226 Token TmpTok;
227 TmpTok.startToken();
228 CreateString(&StrVal[0], StrVal.size(), TmpTok);
229 SourceLocation TokLoc = TmpTok.getLocation();
Chris Lattnerb694ba72006-07-02 22:41:36 +0000230
Chris Lattnerb694ba72006-07-02 22:41:36 +0000231 // Make and enter a lexer object so that we lex and expand the tokens just
232 // like any others.
Chris Lattner9dc9c202009-02-15 20:52:18 +0000233 Lexer *TL = Lexer::Create_PragmaLexer(TokLoc, PragmaLoc, RParenLoc,
Chris Lattnerfa217bd2009-03-08 08:08:45 +0000234 StrVal.size(), *this);
Chris Lattner98a53122006-07-02 23:00:20 +0000235
236 EnterSourceFileWithLexer(TL, 0);
237
Chris Lattnerb694ba72006-07-02 22:41:36 +0000238 // With everything set up, lex this as a #pragma directive.
239 HandlePragmaDirective();
Chris Lattnerb694ba72006-07-02 22:41:36 +0000240}
241
242
243
244/// HandlePragmaOnce - Handle #pragma once. OnceTok is the 'once'.
245///
Chris Lattner146762e2007-07-20 16:59:19 +0000246void Preprocessor::HandlePragmaOnce(Token &OnceTok) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000247 if (isInPrimaryFile()) {
248 Diag(OnceTok, diag::pp_pragma_once_in_main_file);
249 return;
250 }
Mike Stump11289f42009-09-09 15:08:12 +0000251
Chris Lattnerb694ba72006-07-02 22:41:36 +0000252 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000253 // Mark the file as a once-only file now.
Chris Lattnerd32480d2009-01-17 06:22:33 +0000254 HeaderInfo.MarkFileIncludeOnce(getCurrentFileLexer()->getFileEntry());
Chris Lattnerb694ba72006-07-02 22:41:36 +0000255}
256
Chris Lattnerc2383312007-12-19 19:38:36 +0000257void Preprocessor::HandlePragmaMark() {
Ted Kremenek76c34412008-11-19 22:21:33 +0000258 assert(CurPPLexer && "No current lexer?");
Chris Lattnerd9efb6e2009-06-15 05:02:34 +0000259 if (CurLexer)
260 CurLexer->ReadToEndOfLine();
261 else
262 CurPTHLexer->DiscardToEndOfLine();
Chris Lattnerc2383312007-12-19 19:38:36 +0000263}
264
265
Chris Lattnerb694ba72006-07-02 22:41:36 +0000266/// HandlePragmaPoison - Handle #pragma GCC poison. PoisonTok is the 'poison'.
267///
Chris Lattner146762e2007-07-20 16:59:19 +0000268void Preprocessor::HandlePragmaPoison(Token &PoisonTok) {
269 Token Tok;
Chris Lattner538d7f32006-07-20 04:31:52 +0000270
Chris Lattnerb694ba72006-07-02 22:41:36 +0000271 while (1) {
272 // Read the next token to poison. While doing this, pretend that we are
273 // skipping while reading the identifier to poison.
274 // This avoids errors on code like:
275 // #pragma GCC poison X
276 // #pragma GCC poison X
Ted Kremenek551c82a2008-11-18 01:12:54 +0000277 if (CurPPLexer) CurPPLexer->LexingRawMode = true;
Chris Lattnerb694ba72006-07-02 22:41:36 +0000278 LexUnexpandedToken(Tok);
Ted Kremenek551c82a2008-11-18 01:12:54 +0000279 if (CurPPLexer) CurPPLexer->LexingRawMode = false;
Mike Stump11289f42009-09-09 15:08:12 +0000280
Chris Lattnerb694ba72006-07-02 22:41:36 +0000281 // If we reached the end of line, we're done.
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000282 if (Tok.is(tok::eom)) return;
Mike Stump11289f42009-09-09 15:08:12 +0000283
Chris Lattnerb694ba72006-07-02 22:41:36 +0000284 // Can only poison identifiers.
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000285 if (Tok.isNot(tok::identifier)) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000286 Diag(Tok, diag::err_pp_invalid_poison);
287 return;
288 }
Mike Stump11289f42009-09-09 15:08:12 +0000289
Chris Lattnercefc7682006-07-08 08:28:12 +0000290 // Look up the identifier info for the token. We disabled identifier lookup
291 // by saying we're skipping contents, so we need to do this manually.
292 IdentifierInfo *II = LookUpIdentifierInfo(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000293
Chris Lattnerb694ba72006-07-02 22:41:36 +0000294 // Already poisoned.
295 if (II->isPoisoned()) continue;
Mike Stump11289f42009-09-09 15:08:12 +0000296
Chris Lattnerb694ba72006-07-02 22:41:36 +0000297 // If this is a macro identifier, emit a warning.
Chris Lattner259716a2007-10-07 08:04:56 +0000298 if (II->hasMacroDefinition())
Chris Lattnerb694ba72006-07-02 22:41:36 +0000299 Diag(Tok, diag::pp_poisoning_existing_macro);
Mike Stump11289f42009-09-09 15:08:12 +0000300
Chris Lattnerb694ba72006-07-02 22:41:36 +0000301 // Finally, poison it!
302 II->setIsPoisoned();
303 }
304}
305
306/// HandlePragmaSystemHeader - Implement #pragma GCC system_header. We know
307/// that the whole directive has been parsed.
Chris Lattner146762e2007-07-20 16:59:19 +0000308void Preprocessor::HandlePragmaSystemHeader(Token &SysHeaderTok) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000309 if (isInPrimaryFile()) {
310 Diag(SysHeaderTok, diag::pp_pragma_sysheader_in_main_file);
311 return;
312 }
Mike Stump11289f42009-09-09 15:08:12 +0000313
Chris Lattnerb694ba72006-07-02 22:41:36 +0000314 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
Ted Kremenek300590b2008-11-20 01:45:11 +0000315 PreprocessorLexer *TheLexer = getCurrentFileLexer();
Mike Stump11289f42009-09-09 15:08:12 +0000316
Chris Lattnerb694ba72006-07-02 22:41:36 +0000317 // Mark the file as a system header.
Chris Lattnerd32480d2009-01-17 06:22:33 +0000318 HeaderInfo.MarkFileSystemHeader(TheLexer->getFileEntry());
Mike Stump11289f42009-09-09 15:08:12 +0000319
320
Chris Lattnerd9efb6e2009-06-15 05:02:34 +0000321 PresumedLoc PLoc = SourceMgr.getPresumedLoc(SysHeaderTok.getLocation());
322 unsigned FilenameLen = strlen(PLoc.getFilename());
323 unsigned FilenameID = SourceMgr.getLineTableFilenameID(PLoc.getFilename(),
324 FilenameLen);
Mike Stump11289f42009-09-09 15:08:12 +0000325
Chris Lattnerd9efb6e2009-06-15 05:02:34 +0000326 // Emit a line marker. This will change any source locations from this point
327 // forward to realize they are in a system header.
328 // Create a line note with this information.
329 SourceMgr.AddLineNote(SysHeaderTok.getLocation(), PLoc.getLine(), FilenameID,
330 false, false, true, false);
Mike Stump11289f42009-09-09 15:08:12 +0000331
Chris Lattnerb694ba72006-07-02 22:41:36 +0000332 // Notify the client, if desired, that we are in a new source file.
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000333 if (Callbacks)
Ted Kremenek300590b2008-11-20 01:45:11 +0000334 Callbacks->FileChanged(SysHeaderTok.getLocation(),
Chris Lattnerb03dc762008-09-26 21:18:42 +0000335 PPCallbacks::SystemHeaderPragma, SrcMgr::C_System);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000336}
337
338/// HandlePragmaDependency - Handle #pragma GCC dependency "foo" blah.
339///
Chris Lattner146762e2007-07-20 16:59:19 +0000340void Preprocessor::HandlePragmaDependency(Token &DependencyTok) {
341 Token FilenameTok;
Ted Kremenek551c82a2008-11-18 01:12:54 +0000342 CurPPLexer->LexIncludeFilename(FilenameTok);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000343
344 // If the token kind is EOM, the error has already been diagnosed.
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000345 if (FilenameTok.is(tok::eom))
Chris Lattnerb694ba72006-07-02 22:41:36 +0000346 return;
Mike Stump11289f42009-09-09 15:08:12 +0000347
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000348 // Reserve a buffer to get the spelling.
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000349 llvm::SmallString<128> FilenameBuffer;
Douglas Gregordc970f02010-03-16 22:30:13 +0000350 bool Invalid = false;
351 llvm::StringRef Filename = getSpelling(FilenameTok, FilenameBuffer, &Invalid);
352 if (Invalid)
353 return;
Mike Stump11289f42009-09-09 15:08:12 +0000354
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000355 bool isAngled =
356 GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000357 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
358 // error.
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000359 if (Filename.empty())
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000360 return;
Mike Stump11289f42009-09-09 15:08:12 +0000361
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000362 // Search include directories for this file.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000363 const DirectoryLookup *CurDir;
Chris Lattnerfde85352010-01-22 00:14:44 +0000364 const FileEntry *File = LookupFile(Filename, isAngled, 0, CurDir);
Chris Lattner97b8e842008-11-18 08:02:48 +0000365 if (File == 0) {
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000366 Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
Chris Lattner97b8e842008-11-18 08:02:48 +0000367 return;
368 }
Mike Stump11289f42009-09-09 15:08:12 +0000369
Chris Lattnerd32480d2009-01-17 06:22:33 +0000370 const FileEntry *CurFile = getCurrentFileLexer()->getFileEntry();
Chris Lattnerb694ba72006-07-02 22:41:36 +0000371
372 // If this file is older than the file it depends on, emit a diagnostic.
373 if (CurFile && CurFile->getModificationTime() < File->getModificationTime()) {
374 // Lex tokens at the end of the message and include them in the message.
375 std::string Message;
376 Lex(DependencyTok);
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000377 while (DependencyTok.isNot(tok::eom)) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000378 Message += getSpelling(DependencyTok) + " ";
379 Lex(DependencyTok);
380 }
Mike Stump11289f42009-09-09 15:08:12 +0000381
Chris Lattnerb694ba72006-07-02 22:41:36 +0000382 Message.erase(Message.end()-1);
Chris Lattner97b8e842008-11-18 08:02:48 +0000383 Diag(FilenameTok, diag::pp_out_of_date_dependency) << Message;
Chris Lattnerb694ba72006-07-02 22:41:36 +0000384 }
385}
386
Chris Lattner2ff698d2009-01-16 08:21:25 +0000387/// HandlePragmaComment - Handle the microsoft #pragma comment extension. The
388/// syntax is:
389/// #pragma comment(linker, "foo")
390/// 'linker' is one of five identifiers: compiler, exestr, lib, linker, user.
391/// "foo" is a string, which is fully macro expanded, and permits string
Gabor Greif31a082f2009-03-17 11:39:38 +0000392/// concatenation, embedded escape characters etc. See MSDN for more details.
Chris Lattner2ff698d2009-01-16 08:21:25 +0000393void Preprocessor::HandlePragmaComment(Token &Tok) {
394 SourceLocation CommentLoc = Tok.getLocation();
395 Lex(Tok);
396 if (Tok.isNot(tok::l_paren)) {
397 Diag(CommentLoc, diag::err_pragma_comment_malformed);
398 return;
399 }
Mike Stump11289f42009-09-09 15:08:12 +0000400
Chris Lattner2ff698d2009-01-16 08:21:25 +0000401 // Read the identifier.
402 Lex(Tok);
403 if (Tok.isNot(tok::identifier)) {
404 Diag(CommentLoc, diag::err_pragma_comment_malformed);
405 return;
406 }
Mike Stump11289f42009-09-09 15:08:12 +0000407
Chris Lattner2ff698d2009-01-16 08:21:25 +0000408 // Verify that this is one of the 5 whitelisted options.
409 // FIXME: warn that 'exestr' is deprecated.
410 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000411 if (!II->isStr("compiler") && !II->isStr("exestr") && !II->isStr("lib") &&
Chris Lattner2ff698d2009-01-16 08:21:25 +0000412 !II->isStr("linker") && !II->isStr("user")) {
413 Diag(Tok.getLocation(), diag::err_pragma_comment_unknown_kind);
414 return;
415 }
Mike Stump11289f42009-09-09 15:08:12 +0000416
Chris Lattner262d4e32009-01-16 18:59:23 +0000417 // Read the optional string if present.
Chris Lattner2ff698d2009-01-16 08:21:25 +0000418 Lex(Tok);
Chris Lattner262d4e32009-01-16 18:59:23 +0000419 std::string ArgumentString;
Chris Lattner2ff698d2009-01-16 08:21:25 +0000420 if (Tok.is(tok::comma)) {
Chris Lattner262d4e32009-01-16 18:59:23 +0000421 Lex(Tok); // eat the comma.
Chris Lattner2ff698d2009-01-16 08:21:25 +0000422
423 // We need at least one string.
Chris Lattner504af112009-04-19 23:16:58 +0000424 if (Tok.isNot(tok::string_literal)) {
Chris Lattner2ff698d2009-01-16 08:21:25 +0000425 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
426 return;
427 }
428
429 // String concatenation allows multiple strings, which can even come from
430 // macro expansion.
431 // "foo " "bar" "Baz"
Chris Lattner262d4e32009-01-16 18:59:23 +0000432 llvm::SmallVector<Token, 4> StrToks;
Chris Lattner504af112009-04-19 23:16:58 +0000433 while (Tok.is(tok::string_literal)) {
Chris Lattner262d4e32009-01-16 18:59:23 +0000434 StrToks.push_back(Tok);
Chris Lattner2ff698d2009-01-16 08:21:25 +0000435 Lex(Tok);
Chris Lattner262d4e32009-01-16 18:59:23 +0000436 }
437
438 // Concatenate and parse the strings.
439 StringLiteralParser Literal(&StrToks[0], StrToks.size(), *this);
440 assert(!Literal.AnyWide && "Didn't allow wide strings in");
441 if (Literal.hadError)
442 return;
443 if (Literal.Pascal) {
444 Diag(StrToks[0].getLocation(), diag::err_pragma_comment_malformed);
445 return;
446 }
447
448 ArgumentString = std::string(Literal.GetString(),
449 Literal.GetString()+Literal.GetStringLength());
Chris Lattner2ff698d2009-01-16 08:21:25 +0000450 }
Mike Stump11289f42009-09-09 15:08:12 +0000451
Chris Lattner262d4e32009-01-16 18:59:23 +0000452 // FIXME: If the kind is "compiler" warn if the string is present (it is
453 // ignored).
454 // FIXME: 'lib' requires a comment string.
455 // FIXME: 'linker' requires a comment string, and has a specific list of
456 // things that are allowable.
Mike Stump11289f42009-09-09 15:08:12 +0000457
Chris Lattner2ff698d2009-01-16 08:21:25 +0000458 if (Tok.isNot(tok::r_paren)) {
459 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
460 return;
461 }
Chris Lattner262d4e32009-01-16 18:59:23 +0000462 Lex(Tok); // eat the r_paren.
Chris Lattner2ff698d2009-01-16 08:21:25 +0000463
464 if (Tok.isNot(tok::eom)) {
465 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
466 return;
467 }
Mike Stump11289f42009-09-09 15:08:12 +0000468
Chris Lattner262d4e32009-01-16 18:59:23 +0000469 // If the pragma is lexically sound, notify any interested PPCallbacks.
Chris Lattnerf49775d2009-01-16 19:01:46 +0000470 if (Callbacks)
471 Callbacks->PragmaComment(CommentLoc, II, ArgumentString);
Chris Lattner2ff698d2009-01-16 08:21:25 +0000472}
473
Chris Lattner30c924b2010-06-26 17:11:39 +0000474/// HandlePragmaMessage - Handle the microsoft #pragma message extension. The
475/// syntax is:
476/// #pragma message(messagestring)
477/// messagestring is a string, which is fully macro expanded, and permits string
478/// concatenation, embedded escape characters etc. See MSDN for more details.
479void Preprocessor::HandlePragmaMessage(Token &Tok) {
480 SourceLocation MessageLoc = Tok.getLocation();
481 Lex(Tok);
482 if (Tok.isNot(tok::l_paren)) {
483 Diag(MessageLoc, diag::err_pragma_message_malformed);
484 return;
485 }
Chris Lattner2ff698d2009-01-16 08:21:25 +0000486
Chris Lattner30c924b2010-06-26 17:11:39 +0000487 // Read the string.
488 Lex(Tok);
489
490
491 // We need at least one string.
492 if (Tok.isNot(tok::string_literal)) {
493 Diag(Tok.getLocation(), diag::err_pragma_message_malformed);
494 return;
495 }
496
497 // String concatenation allows multiple strings, which can even come from
498 // macro expansion.
499 // "foo " "bar" "Baz"
500 llvm::SmallVector<Token, 4> StrToks;
501 while (Tok.is(tok::string_literal)) {
502 StrToks.push_back(Tok);
503 Lex(Tok);
504 }
505
506 // Concatenate and parse the strings.
507 StringLiteralParser Literal(&StrToks[0], StrToks.size(), *this);
508 assert(!Literal.AnyWide && "Didn't allow wide strings in");
509 if (Literal.hadError)
510 return;
511 if (Literal.Pascal) {
512 Diag(StrToks[0].getLocation(), diag::err_pragma_message_malformed);
513 return;
514 }
515
516 llvm::StringRef MessageString(Literal.GetString(), Literal.GetStringLength());
517
518 if (Tok.isNot(tok::r_paren)) {
519 Diag(Tok.getLocation(), diag::err_pragma_message_malformed);
520 return;
521 }
522 Lex(Tok); // eat the r_paren.
523
524 if (Tok.isNot(tok::eom)) {
525 Diag(Tok.getLocation(), diag::err_pragma_message_malformed);
526 return;
527 }
528
529 // Output the message.
530 Diag(MessageLoc, diag::warn_pragma_message) << MessageString;
531
532 // If the pragma is lexically sound, notify any interested PPCallbacks.
533 if (Callbacks)
534 Callbacks->PragmaMessage(MessageLoc, MessageString);
535}
Chris Lattner2ff698d2009-01-16 08:21:25 +0000536
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000537/// ParsePragmaPushOrPopMacro - Handle parsing of pragma push_macro/pop_macro.
538/// Return the IdentifierInfo* associated with the macro to push or pop.
539IdentifierInfo *Preprocessor::ParsePragmaPushOrPopMacro(Token &Tok) {
540 // Remember the pragma token location.
541 Token PragmaTok = Tok;
542
543 // Read the '('.
544 Lex(Tok);
545 if (Tok.isNot(tok::l_paren)) {
546 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
547 << getSpelling(PragmaTok);
548 return 0;
549 }
550
551 // Read the macro name string.
552 Lex(Tok);
553 if (Tok.isNot(tok::string_literal)) {
554 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
555 << getSpelling(PragmaTok);
556 return 0;
557 }
558
559 // Remember the macro string.
560 std::string StrVal = getSpelling(Tok);
561
562 // Read the ')'.
563 Lex(Tok);
564 if (Tok.isNot(tok::r_paren)) {
565 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
566 << getSpelling(PragmaTok);
567 return 0;
568 }
569
570 assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
571 "Invalid string token!");
572
573 // Create a Token from the string.
574 Token MacroTok;
575 MacroTok.startToken();
576 MacroTok.setKind(tok::identifier);
577 CreateString(&StrVal[1], StrVal.size() - 2, MacroTok);
578
579 // Get the IdentifierInfo of MacroToPushTok.
580 return LookUpIdentifierInfo(MacroTok);
581}
582
583/// HandlePragmaPushMacro - Handle #pragma push_macro.
584/// The syntax is:
585/// #pragma push_macro("macro")
586void Preprocessor::HandlePragmaPushMacro(Token &PushMacroTok) {
587 // Parse the pragma directive and get the macro IdentifierInfo*.
588 IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PushMacroTok);
589 if (!IdentInfo) return;
590
591 // Get the MacroInfo associated with IdentInfo.
592 MacroInfo *MI = getMacroInfo(IdentInfo);
593
594 MacroInfo *MacroCopyToPush = 0;
595 if (MI) {
596 // Make a clone of MI.
597 MacroCopyToPush = CloneMacroInfo(*MI);
598
599 // Allow the original MacroInfo to be redefined later.
600 MI->setIsAllowRedefinitionsWithoutWarning(true);
601 }
602
603 // Push the cloned MacroInfo so we can retrieve it later.
604 PragmaPushMacroInfo[IdentInfo].push_back(MacroCopyToPush);
605}
606
607/// HandlePragmaPopMacro - Handle #pragma push_macro.
608/// The syntax is:
609/// #pragma pop_macro("macro")
610void Preprocessor::HandlePragmaPopMacro(Token &PopMacroTok) {
611 SourceLocation MessageLoc = PopMacroTok.getLocation();
612
613 // Parse the pragma directive and get the macro IdentifierInfo*.
614 IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PopMacroTok);
615 if (!IdentInfo) return;
616
617 // Find the vector<MacroInfo*> associated with the macro.
618 llvm::DenseMap<IdentifierInfo*, std::vector<MacroInfo*> >::iterator iter =
619 PragmaPushMacroInfo.find(IdentInfo);
620 if (iter != PragmaPushMacroInfo.end()) {
621 // Release the MacroInfo currently associated with IdentInfo.
622 MacroInfo *CurrentMI = getMacroInfo(IdentInfo);
623 if (CurrentMI) ReleaseMacroInfo(CurrentMI);
624
625 // Get the MacroInfo we want to reinstall.
626 MacroInfo *MacroToReInstall = iter->second.back();
627
628 // Reinstall the previously pushed macro.
629 setMacroInfo(IdentInfo, MacroToReInstall);
630
631 // Pop PragmaPushMacroInfo stack.
632 iter->second.pop_back();
633 if (iter->second.size() == 0)
634 PragmaPushMacroInfo.erase(iter);
635 } else {
636 Diag(MessageLoc, diag::warn_pragma_pop_macro_no_push)
637 << IdentInfo->getName();
638 }
639}
Chris Lattnerb694ba72006-07-02 22:41:36 +0000640
641/// AddPragmaHandler - Add the specified pragma handler to the preprocessor.
642/// If 'Namespace' is non-null, then it is a token required to exist on the
643/// pragma line before the pragma string starts, e.g. "STDC" or "GCC".
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000644void Preprocessor::AddPragmaHandler(llvm::StringRef Namespace,
Chris Lattnerb694ba72006-07-02 22:41:36 +0000645 PragmaHandler *Handler) {
646 PragmaNamespace *InsertNS = PragmaHandlers;
Mike Stump11289f42009-09-09 15:08:12 +0000647
Chris Lattnerb694ba72006-07-02 22:41:36 +0000648 // If this is specified to be in a namespace, step down into it.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000649 if (!Namespace.empty()) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000650 // If there is already a pragma handler with the name of this namespace,
651 // we either have an error (directive with the same name as a namespace) or
652 // we already have the namespace to insert into.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000653 if (PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace)) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000654 InsertNS = Existing->getIfNamespace();
655 assert(InsertNS != 0 && "Cannot have a pragma namespace and pragma"
656 " handler with the same name!");
657 } else {
658 // Otherwise, this namespace doesn't exist yet, create and insert the
659 // handler for it.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000660 InsertNS = new PragmaNamespace(Namespace);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000661 PragmaHandlers->AddPragma(InsertNS);
662 }
663 }
Mike Stump11289f42009-09-09 15:08:12 +0000664
Chris Lattnerb694ba72006-07-02 22:41:36 +0000665 // Check to make sure we don't already have a pragma for this identifier.
666 assert(!InsertNS->FindHandler(Handler->getName()) &&
667 "Pragma handler already exists for this identifier!");
668 InsertNS->AddPragma(Handler);
669}
670
Daniel Dunbar40596532008-10-04 19:17:46 +0000671/// RemovePragmaHandler - Remove the specific pragma handler from the
672/// preprocessor. If \arg Namespace is non-null, then it should be the
673/// namespace that \arg Handler was added to. It is an error to remove
674/// a handler that has not been registered.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000675void Preprocessor::RemovePragmaHandler(llvm::StringRef Namespace,
Daniel Dunbar40596532008-10-04 19:17:46 +0000676 PragmaHandler *Handler) {
677 PragmaNamespace *NS = PragmaHandlers;
Mike Stump11289f42009-09-09 15:08:12 +0000678
Daniel Dunbar40596532008-10-04 19:17:46 +0000679 // If this is specified to be in a namespace, step down into it.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000680 if (!Namespace.empty()) {
681 PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace);
Daniel Dunbar40596532008-10-04 19:17:46 +0000682 assert(Existing && "Namespace containing handler does not exist!");
683
684 NS = Existing->getIfNamespace();
685 assert(NS && "Invalid namespace, registered as a regular pragma handler!");
686 }
687
688 NS->RemovePragmaHandler(Handler);
Mike Stump11289f42009-09-09 15:08:12 +0000689
Daniel Dunbar40596532008-10-04 19:17:46 +0000690 // If this is a non-default namespace and it is now empty, remove
691 // it.
692 if (NS != PragmaHandlers && NS->IsEmpty())
693 PragmaHandlers->RemovePragmaHandler(NS);
694}
695
Chris Lattnerb694ba72006-07-02 22:41:36 +0000696namespace {
Chris Lattnerc2383312007-12-19 19:38:36 +0000697/// PragmaOnceHandler - "#pragma once" marks the file as atomically included.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000698struct PragmaOnceHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000699 PragmaOnceHandler() : PragmaHandler("once") {}
Chris Lattner146762e2007-07-20 16:59:19 +0000700 virtual void HandlePragma(Preprocessor &PP, Token &OnceTok) {
Chris Lattnerce2ab6f2009-04-14 05:07:49 +0000701 PP.CheckEndOfDirective("pragma once");
Chris Lattnerb694ba72006-07-02 22:41:36 +0000702 PP.HandlePragmaOnce(OnceTok);
703 }
704};
705
Chris Lattnerc2383312007-12-19 19:38:36 +0000706/// PragmaMarkHandler - "#pragma mark ..." is ignored by the compiler, and the
707/// rest of the line is not lexed.
708struct PragmaMarkHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000709 PragmaMarkHandler() : PragmaHandler("mark") {}
Chris Lattnerc2383312007-12-19 19:38:36 +0000710 virtual void HandlePragma(Preprocessor &PP, Token &MarkTok) {
711 PP.HandlePragmaMark();
712 }
713};
714
715/// PragmaPoisonHandler - "#pragma poison x" marks x as not usable.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000716struct PragmaPoisonHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000717 PragmaPoisonHandler() : PragmaHandler("poison") {}
Chris Lattner146762e2007-07-20 16:59:19 +0000718 virtual void HandlePragma(Preprocessor &PP, Token &PoisonTok) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000719 PP.HandlePragmaPoison(PoisonTok);
720 }
721};
722
Chris Lattnerc2383312007-12-19 19:38:36 +0000723/// PragmaSystemHeaderHandler - "#pragma system_header" marks the current file
724/// as a system header, which silences warnings in it.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000725struct PragmaSystemHeaderHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000726 PragmaSystemHeaderHandler() : PragmaHandler("system_header") {}
Chris Lattner146762e2007-07-20 16:59:19 +0000727 virtual void HandlePragma(Preprocessor &PP, Token &SHToken) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000728 PP.HandlePragmaSystemHeader(SHToken);
Chris Lattnerce2ab6f2009-04-14 05:07:49 +0000729 PP.CheckEndOfDirective("pragma");
Chris Lattnerb694ba72006-07-02 22:41:36 +0000730 }
731};
732struct PragmaDependencyHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000733 PragmaDependencyHandler() : PragmaHandler("dependency") {}
Chris Lattner146762e2007-07-20 16:59:19 +0000734 virtual void HandlePragma(Preprocessor &PP, Token &DepToken) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000735 PP.HandlePragmaDependency(DepToken);
736 }
737};
Mike Stump11289f42009-09-09 15:08:12 +0000738
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000739struct PragmaDebugHandler : public PragmaHandler {
740 PragmaDebugHandler() : PragmaHandler("__debug") {}
741 virtual void HandlePragma(Preprocessor &PP, Token &DepToken) {
742 Token Tok;
743 PP.LexUnexpandedToken(Tok);
744 if (Tok.isNot(tok::identifier)) {
745 PP.Diag(Tok, diag::warn_pragma_diagnostic_clang_invalid);
746 return;
747 }
748 IdentifierInfo *II = Tok.getIdentifierInfo();
749
Daniel Dunbarf2cf3292010-08-17 22:32:48 +0000750 if (II->isStr("assert")) {
751 assert(0 && "This is an assertion!");
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000752 } else if (II->isStr("crash")) {
Daniel Dunbarf2cf3292010-08-17 22:32:48 +0000753 *(volatile int*) 0x11 = 0;
754 } else if (II->isStr("llvm_fatal_error")) {
755 llvm::report_fatal_error("#pragma clang __debug llvm_fatal_error");
756 } else if (II->isStr("llvm_unreachable")) {
757 llvm_unreachable("#pragma clang __debug llvm_unreachable");
758 } else if (II->isStr("overflow_stack")) {
759 DebugOverflowStack();
Daniel Dunbar211a7872010-08-18 23:09:23 +0000760 } else if (II->isStr("handle_crash")) {
761 llvm::CrashRecoveryContext *CRC =llvm::CrashRecoveryContext::GetCurrent();
762 if (CRC)
763 CRC->HandleCrash();
Daniel Dunbarf2cf3292010-08-17 22:32:48 +0000764 } else {
765 PP.Diag(Tok, diag::warn_pragma_debug_unexpected_command)
766 << II->getName();
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000767 }
768 }
769
770 void DebugOverflowStack() {
771 DebugOverflowStack();
772 }
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000773};
774
Chris Lattner504af112009-04-19 23:16:58 +0000775/// PragmaDiagnosticHandler - e.g. '#pragma GCC diagnostic ignored "-Wformat"'
Chris Lattnerfb42a182009-07-12 21:18:45 +0000776/// Since clang's diagnostic supports extended functionality beyond GCC's
777/// the constructor takes a clangMode flag to tell it whether or not to allow
778/// clang's extended functionality, or whether to reject it.
Chris Lattner504af112009-04-19 23:16:58 +0000779struct PragmaDiagnosticHandler : public PragmaHandler {
Chris Lattnerfb42a182009-07-12 21:18:45 +0000780private:
781 const bool ClangMode;
782public:
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000783 explicit PragmaDiagnosticHandler(const bool clangMode)
784 : PragmaHandler("diagnostic"), ClangMode(clangMode) {}
785
Chris Lattner504af112009-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)) {
Chris Lattnerfb42a182009-07-12 21:18:45 +0000790 unsigned Diag = ClangMode ? diag::warn_pragma_diagnostic_clang_invalid
791 : diag::warn_pragma_diagnostic_gcc_invalid;
792 PP.Diag(Tok, Diag);
Chris Lattner504af112009-04-19 23:16:58 +0000793 return;
794 }
795 IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000796
Chris Lattner504af112009-04-19 23:16:58 +0000797 diag::Mapping Map;
798 if (II->isStr("warning"))
799 Map = diag::MAP_WARNING;
800 else if (II->isStr("error"))
801 Map = diag::MAP_ERROR;
802 else if (II->isStr("ignored"))
803 Map = diag::MAP_IGNORE;
804 else if (II->isStr("fatal"))
805 Map = diag::MAP_FATAL;
Chris Lattnerfb42a182009-07-12 21:18:45 +0000806 else if (ClangMode) {
807 if (II->isStr("pop")) {
Mike Stump11289f42009-09-09 15:08:12 +0000808 if (!PP.getDiagnostics().popMappings())
Chris Lattnerfb42a182009-07-12 21:18:45 +0000809 PP.Diag(Tok, diag::warn_pragma_diagnostic_clang_cannot_ppp);
810 return;
811 }
812
813 if (II->isStr("push")) {
814 PP.getDiagnostics().pushMappings();
Mike Stump11289f42009-09-09 15:08:12 +0000815 return;
Chris Lattnerfb42a182009-07-12 21:18:45 +0000816 }
817
818 PP.Diag(Tok, diag::warn_pragma_diagnostic_clang_invalid);
819 return;
820 } else {
821 PP.Diag(Tok, diag::warn_pragma_diagnostic_gcc_invalid);
Chris Lattner504af112009-04-19 23:16:58 +0000822 return;
823 }
Mike Stump11289f42009-09-09 15:08:12 +0000824
Chris Lattner504af112009-04-19 23:16:58 +0000825 PP.LexUnexpandedToken(Tok);
826
827 // We need at least one string.
828 if (Tok.isNot(tok::string_literal)) {
829 PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token);
830 return;
831 }
Mike Stump11289f42009-09-09 15:08:12 +0000832
Chris Lattner504af112009-04-19 23:16:58 +0000833 // String concatenation allows multiple strings, which can even come from
834 // macro expansion.
835 // "foo " "bar" "Baz"
836 llvm::SmallVector<Token, 4> StrToks;
837 while (Tok.is(tok::string_literal)) {
838 StrToks.push_back(Tok);
839 PP.LexUnexpandedToken(Tok);
840 }
Mike Stump11289f42009-09-09 15:08:12 +0000841
Chris Lattner504af112009-04-19 23:16:58 +0000842 if (Tok.isNot(tok::eom)) {
843 PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token);
844 return;
845 }
Mike Stump11289f42009-09-09 15:08:12 +0000846
Chris Lattner504af112009-04-19 23:16:58 +0000847 // Concatenate and parse the strings.
848 StringLiteralParser Literal(&StrToks[0], StrToks.size(), PP);
849 assert(!Literal.AnyWide && "Didn't allow wide strings in");
850 if (Literal.hadError)
851 return;
852 if (Literal.Pascal) {
Chris Lattnerfb42a182009-07-12 21:18:45 +0000853 unsigned Diag = ClangMode ? diag::warn_pragma_diagnostic_clang_invalid
854 : diag::warn_pragma_diagnostic_gcc_invalid;
855 PP.Diag(Tok, Diag);
Chris Lattner504af112009-04-19 23:16:58 +0000856 return;
857 }
Chris Lattnerfb42a182009-07-12 21:18:45 +0000858
Chris Lattner504af112009-04-19 23:16:58 +0000859 std::string WarningName(Literal.GetString(),
860 Literal.GetString()+Literal.GetStringLength());
861
862 if (WarningName.size() < 3 || WarningName[0] != '-' ||
863 WarningName[1] != 'W') {
864 PP.Diag(StrToks[0].getLocation(),
865 diag::warn_pragma_diagnostic_invalid_option);
866 return;
867 }
Mike Stump11289f42009-09-09 15:08:12 +0000868
Chris Lattner504af112009-04-19 23:16:58 +0000869 if (PP.getDiagnostics().setDiagnosticGroupMapping(WarningName.c_str()+2,
870 Map))
871 PP.Diag(StrToks[0].getLocation(),
872 diag::warn_pragma_diagnostic_unknown_warning) << WarningName;
873 }
874};
Mike Stump11289f42009-09-09 15:08:12 +0000875
Chris Lattner2ff698d2009-01-16 08:21:25 +0000876/// PragmaCommentHandler - "#pragma comment ...".
877struct PragmaCommentHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000878 PragmaCommentHandler() : PragmaHandler("comment") {}
Chris Lattner2ff698d2009-01-16 08:21:25 +0000879 virtual void HandlePragma(Preprocessor &PP, Token &CommentTok) {
880 PP.HandlePragmaComment(CommentTok);
881 }
882};
Mike Stump11289f42009-09-09 15:08:12 +0000883
Chris Lattner30c924b2010-06-26 17:11:39 +0000884/// PragmaMessageHandler - "#pragma message("...")".
885struct PragmaMessageHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000886 PragmaMessageHandler() : PragmaHandler("message") {}
Chris Lattner30c924b2010-06-26 17:11:39 +0000887 virtual void HandlePragma(Preprocessor &PP, Token &CommentTok) {
888 PP.HandlePragmaMessage(CommentTok);
889 }
890};
891
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000892/// PragmaPushMacroHandler - "#pragma push_macro" saves the value of the
893/// macro on the top of the stack.
894struct PragmaPushMacroHandler : public PragmaHandler {
895 PragmaPushMacroHandler() : PragmaHandler("push_macro") {}
896 virtual void HandlePragma(Preprocessor &PP, Token &PushMacroTok) {
897 PP.HandlePragmaPushMacro(PushMacroTok);
898 }
899};
900
901
902/// PragmaPopMacroHandler - "#pragma pop_macro" sets the value of the
903/// macro to the value on the top of the stack.
904struct PragmaPopMacroHandler : public PragmaHandler {
905 PragmaPopMacroHandler() : PragmaHandler("pop_macro") {}
906 virtual void HandlePragma(Preprocessor &PP, Token &PopMacroTok) {
907 PP.HandlePragmaPopMacro(PopMacroTok);
908 }
909};
910
Chris Lattner958ee042009-04-19 21:20:35 +0000911// Pragma STDC implementations.
Chris Lattner02ef4e32009-04-19 21:50:08 +0000912
913enum STDCSetting {
914 STDC_ON, STDC_OFF, STDC_DEFAULT, STDC_INVALID
915};
Mike Stump11289f42009-09-09 15:08:12 +0000916
Chris Lattner02ef4e32009-04-19 21:50:08 +0000917static STDCSetting LexOnOffSwitch(Preprocessor &PP) {
918 Token Tok;
919 PP.LexUnexpandedToken(Tok);
920
921 if (Tok.isNot(tok::identifier)) {
922 PP.Diag(Tok, diag::ext_stdc_pragma_syntax);
923 return STDC_INVALID;
924 }
925 IdentifierInfo *II = Tok.getIdentifierInfo();
926 STDCSetting Result;
927 if (II->isStr("ON"))
928 Result = STDC_ON;
929 else if (II->isStr("OFF"))
930 Result = STDC_OFF;
931 else if (II->isStr("DEFAULT"))
932 Result = STDC_DEFAULT;
933 else {
934 PP.Diag(Tok, diag::ext_stdc_pragma_syntax);
935 return STDC_INVALID;
936 }
937
938 // Verify that this is followed by EOM.
939 PP.LexUnexpandedToken(Tok);
940 if (Tok.isNot(tok::eom))
941 PP.Diag(Tok, diag::ext_stdc_pragma_syntax_eom);
942 return Result;
943}
Mike Stump11289f42009-09-09 15:08:12 +0000944
Chris Lattner958ee042009-04-19 21:20:35 +0000945/// PragmaSTDC_FP_CONTRACTHandler - "#pragma STDC FP_CONTRACT ...".
946struct PragmaSTDC_FP_CONTRACTHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000947 PragmaSTDC_FP_CONTRACTHandler() : PragmaHandler("FP_CONTRACT") {}
Chris Lattnera0b1f762009-04-19 21:25:37 +0000948 virtual void HandlePragma(Preprocessor &PP, Token &Tok) {
Chris Lattner02ef4e32009-04-19 21:50:08 +0000949 // We just ignore the setting of FP_CONTRACT. Since we don't do contractions
950 // at all, our default is OFF and setting it to ON is an optimization hint
951 // we can safely ignore. When we support -ffma or something, we would need
952 // to diagnose that we are ignoring FMA.
953 LexOnOffSwitch(PP);
Chris Lattner958ee042009-04-19 21:20:35 +0000954 }
955};
Mike Stump11289f42009-09-09 15:08:12 +0000956
Chris Lattner958ee042009-04-19 21:20:35 +0000957/// PragmaSTDC_FENV_ACCESSHandler - "#pragma STDC FENV_ACCESS ...".
958struct PragmaSTDC_FENV_ACCESSHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000959 PragmaSTDC_FENV_ACCESSHandler() : PragmaHandler("FENV_ACCESS") {}
Chris Lattnera0b1f762009-04-19 21:25:37 +0000960 virtual void HandlePragma(Preprocessor &PP, Token &Tok) {
Chris Lattnerdf222682009-04-19 21:55:32 +0000961 if (LexOnOffSwitch(PP) == STDC_ON)
962 PP.Diag(Tok, diag::warn_stdc_fenv_access_not_supported);
Chris Lattner958ee042009-04-19 21:20:35 +0000963 }
964};
Mike Stump11289f42009-09-09 15:08:12 +0000965
Chris Lattner958ee042009-04-19 21:20:35 +0000966/// PragmaSTDC_CX_LIMITED_RANGEHandler - "#pragma STDC CX_LIMITED_RANGE ...".
967struct PragmaSTDC_CX_LIMITED_RANGEHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000968 PragmaSTDC_CX_LIMITED_RANGEHandler()
969 : PragmaHandler("CX_LIMITED_RANGE") {}
Chris Lattnera0b1f762009-04-19 21:25:37 +0000970 virtual void HandlePragma(Preprocessor &PP, Token &Tok) {
Chris Lattner02ef4e32009-04-19 21:50:08 +0000971 LexOnOffSwitch(PP);
Chris Lattner958ee042009-04-19 21:20:35 +0000972 }
973};
Mike Stump11289f42009-09-09 15:08:12 +0000974
Chris Lattner958ee042009-04-19 21:20:35 +0000975/// PragmaSTDC_UnknownHandler - "#pragma STDC ...".
976struct PragmaSTDC_UnknownHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000977 PragmaSTDC_UnknownHandler() {}
Chris Lattnera0b1f762009-04-19 21:25:37 +0000978 virtual void HandlePragma(Preprocessor &PP, Token &UnknownTok) {
Chris Lattner02ef4e32009-04-19 21:50:08 +0000979 // C99 6.10.6p2, unknown forms are not allowed.
Chris Lattnera0b1f762009-04-19 21:25:37 +0000980 PP.Diag(UnknownTok, diag::ext_stdc_pragma_ignored);
Chris Lattner958ee042009-04-19 21:20:35 +0000981 }
982};
Mike Stump11289f42009-09-09 15:08:12 +0000983
Chris Lattnerb694ba72006-07-02 22:41:36 +0000984} // end anonymous namespace
985
986
987/// RegisterBuiltinPragmas - Install the standard preprocessor pragmas:
988/// #pragma GCC poison/system_header/dependency and #pragma once.
989void Preprocessor::RegisterBuiltinPragmas() {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000990 AddPragmaHandler(new PragmaOnceHandler());
991 AddPragmaHandler(new PragmaMarkHandler());
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000992 AddPragmaHandler(new PragmaPushMacroHandler());
993 AddPragmaHandler(new PragmaPopMacroHandler());
Mike Stump11289f42009-09-09 15:08:12 +0000994
Chris Lattnerb61448d2009-05-12 18:21:11 +0000995 // #pragma GCC ...
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000996 AddPragmaHandler("GCC", new PragmaPoisonHandler());
997 AddPragmaHandler("GCC", new PragmaSystemHeaderHandler());
998 AddPragmaHandler("GCC", new PragmaDependencyHandler());
999 AddPragmaHandler("GCC", new PragmaDiagnosticHandler(false));
Chris Lattnerb61448d2009-05-12 18:21:11 +00001000 // #pragma clang ...
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001001 AddPragmaHandler("clang", new PragmaPoisonHandler());
1002 AddPragmaHandler("clang", new PragmaSystemHeaderHandler());
Daniel Dunbarb8068c32010-07-28 15:40:33 +00001003 AddPragmaHandler("clang", new PragmaDebugHandler());
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001004 AddPragmaHandler("clang", new PragmaDependencyHandler());
1005 AddPragmaHandler("clang", new PragmaDiagnosticHandler(true));
Chris Lattnerb61448d2009-05-12 18:21:11 +00001006
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001007 AddPragmaHandler("STDC", new PragmaSTDC_FP_CONTRACTHandler());
1008 AddPragmaHandler("STDC", new PragmaSTDC_FENV_ACCESSHandler());
1009 AddPragmaHandler("STDC", new PragmaSTDC_CX_LIMITED_RANGEHandler());
Chris Lattner958ee042009-04-19 21:20:35 +00001010 AddPragmaHandler("STDC", new PragmaSTDC_UnknownHandler());
Mike Stump11289f42009-09-09 15:08:12 +00001011
Chris Lattner2ff698d2009-01-16 08:21:25 +00001012 // MS extensions.
Chris Lattner30c924b2010-06-26 17:11:39 +00001013 if (Features.Microsoft) {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001014 AddPragmaHandler(new PragmaCommentHandler());
1015 AddPragmaHandler(new PragmaMessageHandler());
Chris Lattner30c924b2010-06-26 17:11:39 +00001016 }
Chris Lattnerb694ba72006-07-02 22:41:36 +00001017}