blob: 0ef87b96d3bad56377aa22d3bbe8d0b387285633 [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 Dunbarf2cf3292010-08-17 22:32:48 +000023#include "llvm/Support/ErrorHandling.h"
Douglas Gregorc6d5edd2009-07-02 17:08:52 +000024#include <algorithm>
Chris Lattnerb8761832006-06-24 21:31:03 +000025using namespace clang;
26
27// Out-of-line destructor to provide a home for the class.
28PragmaHandler::~PragmaHandler() {
29}
30
Chris Lattner2e155302006-07-03 05:34:41 +000031//===----------------------------------------------------------------------===//
Daniel Dunbard839e772010-06-11 20:10:12 +000032// EmptyPragmaHandler Implementation.
33//===----------------------------------------------------------------------===//
34
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000035EmptyPragmaHandler::EmptyPragmaHandler() {}
Daniel Dunbard839e772010-06-11 20:10:12 +000036
37void EmptyPragmaHandler::HandlePragma(Preprocessor &PP, Token &FirstToken) {}
38
39//===----------------------------------------------------------------------===//
Chris Lattner2e155302006-07-03 05:34:41 +000040// PragmaNamespace Implementation.
41//===----------------------------------------------------------------------===//
42
43
44PragmaNamespace::~PragmaNamespace() {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000045 for (llvm::StringMap<PragmaHandler*>::iterator
46 I = Handlers.begin(), E = Handlers.end(); I != E; ++I)
47 delete I->second;
Chris Lattner2e155302006-07-03 05:34:41 +000048}
49
50/// FindHandler - Check to see if there is already a handler for the
51/// specified name. If not, return the handler for the null identifier if it
52/// exists, otherwise return null. If IgnoreNull is true (the default) then
53/// the null handler isn't returned on failure to match.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000054PragmaHandler *PragmaNamespace::FindHandler(llvm::StringRef Name,
Chris Lattner2e155302006-07-03 05:34:41 +000055 bool IgnoreNull) const {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000056 if (PragmaHandler *Handler = Handlers.lookup(Name))
57 return Handler;
58 return IgnoreNull ? 0 : Handlers.lookup(llvm::StringRef());
59}
Mike Stump11289f42009-09-09 15:08:12 +000060
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000061void PragmaNamespace::AddPragma(PragmaHandler *Handler) {
62 assert(!Handlers.lookup(Handler->getName()) &&
63 "A handler with this name is already registered in this namespace");
64 llvm::StringMapEntry<PragmaHandler *> &Entry =
65 Handlers.GetOrCreateValue(Handler->getName());
66 Entry.setValue(Handler);
Chris Lattner2e155302006-07-03 05:34:41 +000067}
68
Daniel Dunbar40596532008-10-04 19:17:46 +000069void PragmaNamespace::RemovePragmaHandler(PragmaHandler *Handler) {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000070 assert(Handlers.lookup(Handler->getName()) &&
71 "Handler not registered in this namespace");
72 Handlers.erase(Handler->getName());
Daniel Dunbar40596532008-10-04 19:17:46 +000073}
74
Chris Lattner146762e2007-07-20 16:59:19 +000075void PragmaNamespace::HandlePragma(Preprocessor &PP, Token &Tok) {
Chris Lattnerb8761832006-06-24 21:31:03 +000076 // Read the 'namespace' that the directive is in, e.g. STDC. Do not macro
77 // expand it, the user can have a STDC #define, that should not affect this.
78 PP.LexUnexpandedToken(Tok);
Mike Stump11289f42009-09-09 15:08:12 +000079
Chris Lattnerb8761832006-06-24 21:31:03 +000080 // Get the handler for this token. If there is no handler, ignore the pragma.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000081 PragmaHandler *Handler
82 = FindHandler(Tok.getIdentifierInfo() ? Tok.getIdentifierInfo()->getName()
83 : llvm::StringRef(),
84 /*IgnoreNull=*/false);
Chris Lattner21656f22009-04-19 21:10:26 +000085 if (Handler == 0) {
86 PP.Diag(Tok, diag::warn_pragma_ignored);
87 return;
88 }
Mike Stump11289f42009-09-09 15:08:12 +000089
Chris Lattnerb8761832006-06-24 21:31:03 +000090 // Otherwise, pass it down.
91 Handler->HandlePragma(PP, Tok);
92}
Chris Lattnerb694ba72006-07-02 22:41:36 +000093
Chris Lattnerb694ba72006-07-02 22:41:36 +000094//===----------------------------------------------------------------------===//
95// Preprocessor Pragma Directive Handling.
96//===----------------------------------------------------------------------===//
97
98/// HandlePragmaDirective - The "#pragma" directive has been parsed. Lex the
99/// rest of the pragma, passing it to the registered pragma handlers.
100void Preprocessor::HandlePragmaDirective() {
101 ++NumPragma;
Mike Stump11289f42009-09-09 15:08:12 +0000102
Chris Lattnerb694ba72006-07-02 22:41:36 +0000103 // Invoke the first level of pragma handlers which reads the namespace id.
Chris Lattner146762e2007-07-20 16:59:19 +0000104 Token Tok;
Chris Lattnerb694ba72006-07-02 22:41:36 +0000105 PragmaHandlers->HandlePragma(*this, Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000106
Chris Lattnerb694ba72006-07-02 22:41:36 +0000107 // If the pragma handler didn't read the rest of the line, consume it now.
Chris Lattnere7e65942009-06-18 05:55:53 +0000108 if (CurPPLexer && CurPPLexer->ParsingPreprocessorDirective)
Chris Lattnerb694ba72006-07-02 22:41:36 +0000109 DiscardUntilEndOfDirective();
110}
111
112/// Handle_Pragma - Read a _Pragma directive, slice it up, process it, then
113/// return the first token after the directive. The _Pragma token has just
114/// been read into 'Tok'.
Chris Lattner146762e2007-07-20 16:59:19 +0000115void Preprocessor::Handle_Pragma(Token &Tok) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000116 // Remember the pragma token location.
117 SourceLocation PragmaLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000118
Chris Lattnerb694ba72006-07-02 22:41:36 +0000119 // Read the '('.
120 Lex(Tok);
Chris Lattner907dfe92008-11-18 07:59:24 +0000121 if (Tok.isNot(tok::l_paren)) {
122 Diag(PragmaLoc, diag::err__Pragma_malformed);
123 return;
124 }
Chris Lattnerb694ba72006-07-02 22:41:36 +0000125
126 // Read the '"..."'.
127 Lex(Tok);
Chris Lattner907dfe92008-11-18 07:59:24 +0000128 if (Tok.isNot(tok::string_literal) && Tok.isNot(tok::wide_string_literal)) {
129 Diag(PragmaLoc, diag::err__Pragma_malformed);
130 return;
131 }
Mike Stump11289f42009-09-09 15:08:12 +0000132
Chris Lattnerb694ba72006-07-02 22:41:36 +0000133 // Remember the string.
134 std::string StrVal = getSpelling(Tok);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000135
136 // Read the ')'.
137 Lex(Tok);
Chris Lattner907dfe92008-11-18 07:59:24 +0000138 if (Tok.isNot(tok::r_paren)) {
139 Diag(PragmaLoc, diag::err__Pragma_malformed);
140 return;
141 }
Mike Stump11289f42009-09-09 15:08:12 +0000142
Chris Lattner9dc9c202009-02-15 20:52:18 +0000143 SourceLocation RParenLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000144
Chris Lattner262d4e32009-01-16 18:59:23 +0000145 // The _Pragma is lexically sound. Destringize according to C99 6.10.9.1:
146 // "The string literal is destringized by deleting the L prefix, if present,
147 // deleting the leading and trailing double-quotes, replacing each escape
148 // sequence \" by a double-quote, and replacing each escape sequence \\ by a
149 // single backslash."
Chris Lattnerb694ba72006-07-02 22:41:36 +0000150 if (StrVal[0] == 'L') // Remove L prefix.
151 StrVal.erase(StrVal.begin());
152 assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
153 "Invalid string token!");
Mike Stump11289f42009-09-09 15:08:12 +0000154
Chris Lattnerb694ba72006-07-02 22:41:36 +0000155 // Remove the front quote, replacing it with a space, so that the pragma
156 // contents appear to have a space before them.
157 StrVal[0] = ' ';
Mike Stump11289f42009-09-09 15:08:12 +0000158
Chris Lattnerfa217bd2009-03-08 08:08:45 +0000159 // Replace the terminating quote with a \n.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000160 StrVal[StrVal.size()-1] = '\n';
Mike Stump11289f42009-09-09 15:08:12 +0000161
Chris Lattnerb694ba72006-07-02 22:41:36 +0000162 // Remove escaped quotes and escapes.
163 for (unsigned i = 0, e = StrVal.size(); i != e-1; ++i) {
164 if (StrVal[i] == '\\' &&
165 (StrVal[i+1] == '\\' || StrVal[i+1] == '"')) {
166 // \\ -> '\' and \" -> '"'.
167 StrVal.erase(StrVal.begin()+i);
168 --e;
169 }
170 }
Mike Stump11289f42009-09-09 15:08:12 +0000171
Chris Lattnerf918bf72006-07-19 05:01:18 +0000172 // Plop the string (including the newline and trailing null) into a buffer
173 // where we can lex it.
Chris Lattner5a7971e2009-01-26 19:29:26 +0000174 Token TmpTok;
175 TmpTok.startToken();
176 CreateString(&StrVal[0], StrVal.size(), TmpTok);
177 SourceLocation TokLoc = TmpTok.getLocation();
Chris Lattnerb694ba72006-07-02 22:41:36 +0000178
Chris Lattnerb694ba72006-07-02 22:41:36 +0000179 // Make and enter a lexer object so that we lex and expand the tokens just
180 // like any others.
Chris Lattner9dc9c202009-02-15 20:52:18 +0000181 Lexer *TL = Lexer::Create_PragmaLexer(TokLoc, PragmaLoc, RParenLoc,
Chris Lattnerfa217bd2009-03-08 08:08:45 +0000182 StrVal.size(), *this);
Chris Lattner98a53122006-07-02 23:00:20 +0000183
184 EnterSourceFileWithLexer(TL, 0);
185
Chris Lattnerb694ba72006-07-02 22:41:36 +0000186 // With everything set up, lex this as a #pragma directive.
187 HandlePragmaDirective();
Mike Stump11289f42009-09-09 15:08:12 +0000188
Chris Lattnerb694ba72006-07-02 22:41:36 +0000189 // Finally, return whatever came after the pragma directive.
190 return Lex(Tok);
191}
192
193
194
195/// HandlePragmaOnce - Handle #pragma once. OnceTok is the 'once'.
196///
Chris Lattner146762e2007-07-20 16:59:19 +0000197void Preprocessor::HandlePragmaOnce(Token &OnceTok) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000198 if (isInPrimaryFile()) {
199 Diag(OnceTok, diag::pp_pragma_once_in_main_file);
200 return;
201 }
Mike Stump11289f42009-09-09 15:08:12 +0000202
Chris Lattnerb694ba72006-07-02 22:41:36 +0000203 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000204 // Mark the file as a once-only file now.
Chris Lattnerd32480d2009-01-17 06:22:33 +0000205 HeaderInfo.MarkFileIncludeOnce(getCurrentFileLexer()->getFileEntry());
Chris Lattnerb694ba72006-07-02 22:41:36 +0000206}
207
Chris Lattnerc2383312007-12-19 19:38:36 +0000208void Preprocessor::HandlePragmaMark() {
Ted Kremenek76c34412008-11-19 22:21:33 +0000209 assert(CurPPLexer && "No current lexer?");
Chris Lattnerd9efb6e2009-06-15 05:02:34 +0000210 if (CurLexer)
211 CurLexer->ReadToEndOfLine();
212 else
213 CurPTHLexer->DiscardToEndOfLine();
Chris Lattnerc2383312007-12-19 19:38:36 +0000214}
215
216
Chris Lattnerb694ba72006-07-02 22:41:36 +0000217/// HandlePragmaPoison - Handle #pragma GCC poison. PoisonTok is the 'poison'.
218///
Chris Lattner146762e2007-07-20 16:59:19 +0000219void Preprocessor::HandlePragmaPoison(Token &PoisonTok) {
220 Token Tok;
Chris Lattner538d7f32006-07-20 04:31:52 +0000221
Chris Lattnerb694ba72006-07-02 22:41:36 +0000222 while (1) {
223 // Read the next token to poison. While doing this, pretend that we are
224 // skipping while reading the identifier to poison.
225 // This avoids errors on code like:
226 // #pragma GCC poison X
227 // #pragma GCC poison X
Ted Kremenek551c82a2008-11-18 01:12:54 +0000228 if (CurPPLexer) CurPPLexer->LexingRawMode = true;
Chris Lattnerb694ba72006-07-02 22:41:36 +0000229 LexUnexpandedToken(Tok);
Ted Kremenek551c82a2008-11-18 01:12:54 +0000230 if (CurPPLexer) CurPPLexer->LexingRawMode = false;
Mike Stump11289f42009-09-09 15:08:12 +0000231
Chris Lattnerb694ba72006-07-02 22:41:36 +0000232 // If we reached the end of line, we're done.
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000233 if (Tok.is(tok::eom)) return;
Mike Stump11289f42009-09-09 15:08:12 +0000234
Chris Lattnerb694ba72006-07-02 22:41:36 +0000235 // Can only poison identifiers.
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000236 if (Tok.isNot(tok::identifier)) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000237 Diag(Tok, diag::err_pp_invalid_poison);
238 return;
239 }
Mike Stump11289f42009-09-09 15:08:12 +0000240
Chris Lattnercefc7682006-07-08 08:28:12 +0000241 // Look up the identifier info for the token. We disabled identifier lookup
242 // by saying we're skipping contents, so we need to do this manually.
243 IdentifierInfo *II = LookUpIdentifierInfo(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000244
Chris Lattnerb694ba72006-07-02 22:41:36 +0000245 // Already poisoned.
246 if (II->isPoisoned()) continue;
Mike Stump11289f42009-09-09 15:08:12 +0000247
Chris Lattnerb694ba72006-07-02 22:41:36 +0000248 // If this is a macro identifier, emit a warning.
Chris Lattner259716a2007-10-07 08:04:56 +0000249 if (II->hasMacroDefinition())
Chris Lattnerb694ba72006-07-02 22:41:36 +0000250 Diag(Tok, diag::pp_poisoning_existing_macro);
Mike Stump11289f42009-09-09 15:08:12 +0000251
Chris Lattnerb694ba72006-07-02 22:41:36 +0000252 // Finally, poison it!
253 II->setIsPoisoned();
254 }
255}
256
257/// HandlePragmaSystemHeader - Implement #pragma GCC system_header. We know
258/// that the whole directive has been parsed.
Chris Lattner146762e2007-07-20 16:59:19 +0000259void Preprocessor::HandlePragmaSystemHeader(Token &SysHeaderTok) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000260 if (isInPrimaryFile()) {
261 Diag(SysHeaderTok, diag::pp_pragma_sysheader_in_main_file);
262 return;
263 }
Mike Stump11289f42009-09-09 15:08:12 +0000264
Chris Lattnerb694ba72006-07-02 22:41:36 +0000265 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
Ted Kremenek300590b2008-11-20 01:45:11 +0000266 PreprocessorLexer *TheLexer = getCurrentFileLexer();
Mike Stump11289f42009-09-09 15:08:12 +0000267
Chris Lattnerb694ba72006-07-02 22:41:36 +0000268 // Mark the file as a system header.
Chris Lattnerd32480d2009-01-17 06:22:33 +0000269 HeaderInfo.MarkFileSystemHeader(TheLexer->getFileEntry());
Mike Stump11289f42009-09-09 15:08:12 +0000270
271
Chris Lattnerd9efb6e2009-06-15 05:02:34 +0000272 PresumedLoc PLoc = SourceMgr.getPresumedLoc(SysHeaderTok.getLocation());
273 unsigned FilenameLen = strlen(PLoc.getFilename());
274 unsigned FilenameID = SourceMgr.getLineTableFilenameID(PLoc.getFilename(),
275 FilenameLen);
Mike Stump11289f42009-09-09 15:08:12 +0000276
Chris Lattnerd9efb6e2009-06-15 05:02:34 +0000277 // Emit a line marker. This will change any source locations from this point
278 // forward to realize they are in a system header.
279 // Create a line note with this information.
280 SourceMgr.AddLineNote(SysHeaderTok.getLocation(), PLoc.getLine(), FilenameID,
281 false, false, true, false);
Mike Stump11289f42009-09-09 15:08:12 +0000282
Chris Lattnerb694ba72006-07-02 22:41:36 +0000283 // Notify the client, if desired, that we are in a new source file.
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000284 if (Callbacks)
Ted Kremenek300590b2008-11-20 01:45:11 +0000285 Callbacks->FileChanged(SysHeaderTok.getLocation(),
Chris Lattnerb03dc762008-09-26 21:18:42 +0000286 PPCallbacks::SystemHeaderPragma, SrcMgr::C_System);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000287}
288
289/// HandlePragmaDependency - Handle #pragma GCC dependency "foo" blah.
290///
Chris Lattner146762e2007-07-20 16:59:19 +0000291void Preprocessor::HandlePragmaDependency(Token &DependencyTok) {
292 Token FilenameTok;
Ted Kremenek551c82a2008-11-18 01:12:54 +0000293 CurPPLexer->LexIncludeFilename(FilenameTok);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000294
295 // If the token kind is EOM, the error has already been diagnosed.
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000296 if (FilenameTok.is(tok::eom))
Chris Lattnerb694ba72006-07-02 22:41:36 +0000297 return;
Mike Stump11289f42009-09-09 15:08:12 +0000298
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000299 // Reserve a buffer to get the spelling.
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000300 llvm::SmallString<128> FilenameBuffer;
Douglas Gregordc970f02010-03-16 22:30:13 +0000301 bool Invalid = false;
302 llvm::StringRef Filename = getSpelling(FilenameTok, FilenameBuffer, &Invalid);
303 if (Invalid)
304 return;
Mike Stump11289f42009-09-09 15:08:12 +0000305
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000306 bool isAngled =
307 GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000308 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
309 // error.
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000310 if (Filename.empty())
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000311 return;
Mike Stump11289f42009-09-09 15:08:12 +0000312
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000313 // Search include directories for this file.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000314 const DirectoryLookup *CurDir;
Chris Lattnerfde85352010-01-22 00:14:44 +0000315 const FileEntry *File = LookupFile(Filename, isAngled, 0, CurDir);
Chris Lattner97b8e842008-11-18 08:02:48 +0000316 if (File == 0) {
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000317 Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
Chris Lattner97b8e842008-11-18 08:02:48 +0000318 return;
319 }
Mike Stump11289f42009-09-09 15:08:12 +0000320
Chris Lattnerd32480d2009-01-17 06:22:33 +0000321 const FileEntry *CurFile = getCurrentFileLexer()->getFileEntry();
Chris Lattnerb694ba72006-07-02 22:41:36 +0000322
323 // If this file is older than the file it depends on, emit a diagnostic.
324 if (CurFile && CurFile->getModificationTime() < File->getModificationTime()) {
325 // Lex tokens at the end of the message and include them in the message.
326 std::string Message;
327 Lex(DependencyTok);
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000328 while (DependencyTok.isNot(tok::eom)) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000329 Message += getSpelling(DependencyTok) + " ";
330 Lex(DependencyTok);
331 }
Mike Stump11289f42009-09-09 15:08:12 +0000332
Chris Lattnerb694ba72006-07-02 22:41:36 +0000333 Message.erase(Message.end()-1);
Chris Lattner97b8e842008-11-18 08:02:48 +0000334 Diag(FilenameTok, diag::pp_out_of_date_dependency) << Message;
Chris Lattnerb694ba72006-07-02 22:41:36 +0000335 }
336}
337
Chris Lattner2ff698d2009-01-16 08:21:25 +0000338/// HandlePragmaComment - Handle the microsoft #pragma comment extension. The
339/// syntax is:
340/// #pragma comment(linker, "foo")
341/// 'linker' is one of five identifiers: compiler, exestr, lib, linker, user.
342/// "foo" is a string, which is fully macro expanded, and permits string
Gabor Greif31a082f2009-03-17 11:39:38 +0000343/// concatenation, embedded escape characters etc. See MSDN for more details.
Chris Lattner2ff698d2009-01-16 08:21:25 +0000344void Preprocessor::HandlePragmaComment(Token &Tok) {
345 SourceLocation CommentLoc = Tok.getLocation();
346 Lex(Tok);
347 if (Tok.isNot(tok::l_paren)) {
348 Diag(CommentLoc, diag::err_pragma_comment_malformed);
349 return;
350 }
Mike Stump11289f42009-09-09 15:08:12 +0000351
Chris Lattner2ff698d2009-01-16 08:21:25 +0000352 // Read the identifier.
353 Lex(Tok);
354 if (Tok.isNot(tok::identifier)) {
355 Diag(CommentLoc, diag::err_pragma_comment_malformed);
356 return;
357 }
Mike Stump11289f42009-09-09 15:08:12 +0000358
Chris Lattner2ff698d2009-01-16 08:21:25 +0000359 // Verify that this is one of the 5 whitelisted options.
360 // FIXME: warn that 'exestr' is deprecated.
361 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000362 if (!II->isStr("compiler") && !II->isStr("exestr") && !II->isStr("lib") &&
Chris Lattner2ff698d2009-01-16 08:21:25 +0000363 !II->isStr("linker") && !II->isStr("user")) {
364 Diag(Tok.getLocation(), diag::err_pragma_comment_unknown_kind);
365 return;
366 }
Mike Stump11289f42009-09-09 15:08:12 +0000367
Chris Lattner262d4e32009-01-16 18:59:23 +0000368 // Read the optional string if present.
Chris Lattner2ff698d2009-01-16 08:21:25 +0000369 Lex(Tok);
Chris Lattner262d4e32009-01-16 18:59:23 +0000370 std::string ArgumentString;
Chris Lattner2ff698d2009-01-16 08:21:25 +0000371 if (Tok.is(tok::comma)) {
Chris Lattner262d4e32009-01-16 18:59:23 +0000372 Lex(Tok); // eat the comma.
Chris Lattner2ff698d2009-01-16 08:21:25 +0000373
374 // We need at least one string.
Chris Lattner504af112009-04-19 23:16:58 +0000375 if (Tok.isNot(tok::string_literal)) {
Chris Lattner2ff698d2009-01-16 08:21:25 +0000376 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
377 return;
378 }
379
380 // String concatenation allows multiple strings, which can even come from
381 // macro expansion.
382 // "foo " "bar" "Baz"
Chris Lattner262d4e32009-01-16 18:59:23 +0000383 llvm::SmallVector<Token, 4> StrToks;
Chris Lattner504af112009-04-19 23:16:58 +0000384 while (Tok.is(tok::string_literal)) {
Chris Lattner262d4e32009-01-16 18:59:23 +0000385 StrToks.push_back(Tok);
Chris Lattner2ff698d2009-01-16 08:21:25 +0000386 Lex(Tok);
Chris Lattner262d4e32009-01-16 18:59:23 +0000387 }
388
389 // Concatenate and parse the strings.
390 StringLiteralParser Literal(&StrToks[0], StrToks.size(), *this);
391 assert(!Literal.AnyWide && "Didn't allow wide strings in");
392 if (Literal.hadError)
393 return;
394 if (Literal.Pascal) {
395 Diag(StrToks[0].getLocation(), diag::err_pragma_comment_malformed);
396 return;
397 }
398
399 ArgumentString = std::string(Literal.GetString(),
400 Literal.GetString()+Literal.GetStringLength());
Chris Lattner2ff698d2009-01-16 08:21:25 +0000401 }
Mike Stump11289f42009-09-09 15:08:12 +0000402
Chris Lattner262d4e32009-01-16 18:59:23 +0000403 // FIXME: If the kind is "compiler" warn if the string is present (it is
404 // ignored).
405 // FIXME: 'lib' requires a comment string.
406 // FIXME: 'linker' requires a comment string, and has a specific list of
407 // things that are allowable.
Mike Stump11289f42009-09-09 15:08:12 +0000408
Chris Lattner2ff698d2009-01-16 08:21:25 +0000409 if (Tok.isNot(tok::r_paren)) {
410 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
411 return;
412 }
Chris Lattner262d4e32009-01-16 18:59:23 +0000413 Lex(Tok); // eat the r_paren.
Chris Lattner2ff698d2009-01-16 08:21:25 +0000414
415 if (Tok.isNot(tok::eom)) {
416 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
417 return;
418 }
Mike Stump11289f42009-09-09 15:08:12 +0000419
Chris Lattner262d4e32009-01-16 18:59:23 +0000420 // If the pragma is lexically sound, notify any interested PPCallbacks.
Chris Lattnerf49775d2009-01-16 19:01:46 +0000421 if (Callbacks)
422 Callbacks->PragmaComment(CommentLoc, II, ArgumentString);
Chris Lattner2ff698d2009-01-16 08:21:25 +0000423}
424
Chris Lattner30c924b2010-06-26 17:11:39 +0000425/// HandlePragmaMessage - Handle the microsoft #pragma message extension. The
426/// syntax is:
427/// #pragma message(messagestring)
428/// messagestring is a string, which is fully macro expanded, and permits string
429/// concatenation, embedded escape characters etc. See MSDN for more details.
430void Preprocessor::HandlePragmaMessage(Token &Tok) {
431 SourceLocation MessageLoc = Tok.getLocation();
432 Lex(Tok);
433 if (Tok.isNot(tok::l_paren)) {
434 Diag(MessageLoc, diag::err_pragma_message_malformed);
435 return;
436 }
Chris Lattner2ff698d2009-01-16 08:21:25 +0000437
Chris Lattner30c924b2010-06-26 17:11:39 +0000438 // Read the string.
439 Lex(Tok);
440
441
442 // We need at least one string.
443 if (Tok.isNot(tok::string_literal)) {
444 Diag(Tok.getLocation(), diag::err_pragma_message_malformed);
445 return;
446 }
447
448 // String concatenation allows multiple strings, which can even come from
449 // macro expansion.
450 // "foo " "bar" "Baz"
451 llvm::SmallVector<Token, 4> StrToks;
452 while (Tok.is(tok::string_literal)) {
453 StrToks.push_back(Tok);
454 Lex(Tok);
455 }
456
457 // Concatenate and parse the strings.
458 StringLiteralParser Literal(&StrToks[0], StrToks.size(), *this);
459 assert(!Literal.AnyWide && "Didn't allow wide strings in");
460 if (Literal.hadError)
461 return;
462 if (Literal.Pascal) {
463 Diag(StrToks[0].getLocation(), diag::err_pragma_message_malformed);
464 return;
465 }
466
467 llvm::StringRef MessageString(Literal.GetString(), Literal.GetStringLength());
468
469 if (Tok.isNot(tok::r_paren)) {
470 Diag(Tok.getLocation(), diag::err_pragma_message_malformed);
471 return;
472 }
473 Lex(Tok); // eat the r_paren.
474
475 if (Tok.isNot(tok::eom)) {
476 Diag(Tok.getLocation(), diag::err_pragma_message_malformed);
477 return;
478 }
479
480 // Output the message.
481 Diag(MessageLoc, diag::warn_pragma_message) << MessageString;
482
483 // If the pragma is lexically sound, notify any interested PPCallbacks.
484 if (Callbacks)
485 Callbacks->PragmaMessage(MessageLoc, MessageString);
486}
Chris Lattner2ff698d2009-01-16 08:21:25 +0000487
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000488/// ParsePragmaPushOrPopMacro - Handle parsing of pragma push_macro/pop_macro.
489/// Return the IdentifierInfo* associated with the macro to push or pop.
490IdentifierInfo *Preprocessor::ParsePragmaPushOrPopMacro(Token &Tok) {
491 // Remember the pragma token location.
492 Token PragmaTok = Tok;
493
494 // Read the '('.
495 Lex(Tok);
496 if (Tok.isNot(tok::l_paren)) {
497 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
498 << getSpelling(PragmaTok);
499 return 0;
500 }
501
502 // Read the macro name string.
503 Lex(Tok);
504 if (Tok.isNot(tok::string_literal)) {
505 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
506 << getSpelling(PragmaTok);
507 return 0;
508 }
509
510 // Remember the macro string.
511 std::string StrVal = getSpelling(Tok);
512
513 // Read the ')'.
514 Lex(Tok);
515 if (Tok.isNot(tok::r_paren)) {
516 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
517 << getSpelling(PragmaTok);
518 return 0;
519 }
520
521 assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
522 "Invalid string token!");
523
524 // Create a Token from the string.
525 Token MacroTok;
526 MacroTok.startToken();
527 MacroTok.setKind(tok::identifier);
528 CreateString(&StrVal[1], StrVal.size() - 2, MacroTok);
529
530 // Get the IdentifierInfo of MacroToPushTok.
531 return LookUpIdentifierInfo(MacroTok);
532}
533
534/// HandlePragmaPushMacro - Handle #pragma push_macro.
535/// The syntax is:
536/// #pragma push_macro("macro")
537void Preprocessor::HandlePragmaPushMacro(Token &PushMacroTok) {
538 // Parse the pragma directive and get the macro IdentifierInfo*.
539 IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PushMacroTok);
540 if (!IdentInfo) return;
541
542 // Get the MacroInfo associated with IdentInfo.
543 MacroInfo *MI = getMacroInfo(IdentInfo);
544
545 MacroInfo *MacroCopyToPush = 0;
546 if (MI) {
547 // Make a clone of MI.
548 MacroCopyToPush = CloneMacroInfo(*MI);
549
550 // Allow the original MacroInfo to be redefined later.
551 MI->setIsAllowRedefinitionsWithoutWarning(true);
552 }
553
554 // Push the cloned MacroInfo so we can retrieve it later.
555 PragmaPushMacroInfo[IdentInfo].push_back(MacroCopyToPush);
556}
557
558/// HandlePragmaPopMacro - Handle #pragma push_macro.
559/// The syntax is:
560/// #pragma pop_macro("macro")
561void Preprocessor::HandlePragmaPopMacro(Token &PopMacroTok) {
562 SourceLocation MessageLoc = PopMacroTok.getLocation();
563
564 // Parse the pragma directive and get the macro IdentifierInfo*.
565 IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PopMacroTok);
566 if (!IdentInfo) return;
567
568 // Find the vector<MacroInfo*> associated with the macro.
569 llvm::DenseMap<IdentifierInfo*, std::vector<MacroInfo*> >::iterator iter =
570 PragmaPushMacroInfo.find(IdentInfo);
571 if (iter != PragmaPushMacroInfo.end()) {
572 // Release the MacroInfo currently associated with IdentInfo.
573 MacroInfo *CurrentMI = getMacroInfo(IdentInfo);
574 if (CurrentMI) ReleaseMacroInfo(CurrentMI);
575
576 // Get the MacroInfo we want to reinstall.
577 MacroInfo *MacroToReInstall = iter->second.back();
578
579 // Reinstall the previously pushed macro.
580 setMacroInfo(IdentInfo, MacroToReInstall);
581
582 // Pop PragmaPushMacroInfo stack.
583 iter->second.pop_back();
584 if (iter->second.size() == 0)
585 PragmaPushMacroInfo.erase(iter);
586 } else {
587 Diag(MessageLoc, diag::warn_pragma_pop_macro_no_push)
588 << IdentInfo->getName();
589 }
590}
Chris Lattnerb694ba72006-07-02 22:41:36 +0000591
592/// AddPragmaHandler - Add the specified pragma handler to the preprocessor.
593/// If 'Namespace' is non-null, then it is a token required to exist on the
594/// pragma line before the pragma string starts, e.g. "STDC" or "GCC".
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000595void Preprocessor::AddPragmaHandler(llvm::StringRef Namespace,
Chris Lattnerb694ba72006-07-02 22:41:36 +0000596 PragmaHandler *Handler) {
597 PragmaNamespace *InsertNS = PragmaHandlers;
Mike Stump11289f42009-09-09 15:08:12 +0000598
Chris Lattnerb694ba72006-07-02 22:41:36 +0000599 // If this is specified to be in a namespace, step down into it.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000600 if (!Namespace.empty()) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000601 // If there is already a pragma handler with the name of this namespace,
602 // we either have an error (directive with the same name as a namespace) or
603 // we already have the namespace to insert into.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000604 if (PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace)) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000605 InsertNS = Existing->getIfNamespace();
606 assert(InsertNS != 0 && "Cannot have a pragma namespace and pragma"
607 " handler with the same name!");
608 } else {
609 // Otherwise, this namespace doesn't exist yet, create and insert the
610 // handler for it.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000611 InsertNS = new PragmaNamespace(Namespace);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000612 PragmaHandlers->AddPragma(InsertNS);
613 }
614 }
Mike Stump11289f42009-09-09 15:08:12 +0000615
Chris Lattnerb694ba72006-07-02 22:41:36 +0000616 // Check to make sure we don't already have a pragma for this identifier.
617 assert(!InsertNS->FindHandler(Handler->getName()) &&
618 "Pragma handler already exists for this identifier!");
619 InsertNS->AddPragma(Handler);
620}
621
Daniel Dunbar40596532008-10-04 19:17:46 +0000622/// RemovePragmaHandler - Remove the specific pragma handler from the
623/// preprocessor. If \arg Namespace is non-null, then it should be the
624/// namespace that \arg Handler was added to. It is an error to remove
625/// a handler that has not been registered.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000626void Preprocessor::RemovePragmaHandler(llvm::StringRef Namespace,
Daniel Dunbar40596532008-10-04 19:17:46 +0000627 PragmaHandler *Handler) {
628 PragmaNamespace *NS = PragmaHandlers;
Mike Stump11289f42009-09-09 15:08:12 +0000629
Daniel Dunbar40596532008-10-04 19:17:46 +0000630 // If this is specified to be in a namespace, step down into it.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000631 if (!Namespace.empty()) {
632 PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace);
Daniel Dunbar40596532008-10-04 19:17:46 +0000633 assert(Existing && "Namespace containing handler does not exist!");
634
635 NS = Existing->getIfNamespace();
636 assert(NS && "Invalid namespace, registered as a regular pragma handler!");
637 }
638
639 NS->RemovePragmaHandler(Handler);
Mike Stump11289f42009-09-09 15:08:12 +0000640
Daniel Dunbar40596532008-10-04 19:17:46 +0000641 // If this is a non-default namespace and it is now empty, remove
642 // it.
643 if (NS != PragmaHandlers && NS->IsEmpty())
644 PragmaHandlers->RemovePragmaHandler(NS);
645}
646
Chris Lattnerb694ba72006-07-02 22:41:36 +0000647namespace {
Chris Lattnerc2383312007-12-19 19:38:36 +0000648/// PragmaOnceHandler - "#pragma once" marks the file as atomically included.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000649struct PragmaOnceHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000650 PragmaOnceHandler() : PragmaHandler("once") {}
Chris Lattner146762e2007-07-20 16:59:19 +0000651 virtual void HandlePragma(Preprocessor &PP, Token &OnceTok) {
Chris Lattnerce2ab6f2009-04-14 05:07:49 +0000652 PP.CheckEndOfDirective("pragma once");
Chris Lattnerb694ba72006-07-02 22:41:36 +0000653 PP.HandlePragmaOnce(OnceTok);
654 }
655};
656
Chris Lattnerc2383312007-12-19 19:38:36 +0000657/// PragmaMarkHandler - "#pragma mark ..." is ignored by the compiler, and the
658/// rest of the line is not lexed.
659struct PragmaMarkHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000660 PragmaMarkHandler() : PragmaHandler("mark") {}
Chris Lattnerc2383312007-12-19 19:38:36 +0000661 virtual void HandlePragma(Preprocessor &PP, Token &MarkTok) {
662 PP.HandlePragmaMark();
663 }
664};
665
666/// PragmaPoisonHandler - "#pragma poison x" marks x as not usable.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000667struct PragmaPoisonHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000668 PragmaPoisonHandler() : PragmaHandler("poison") {}
Chris Lattner146762e2007-07-20 16:59:19 +0000669 virtual void HandlePragma(Preprocessor &PP, Token &PoisonTok) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000670 PP.HandlePragmaPoison(PoisonTok);
671 }
672};
673
Chris Lattnerc2383312007-12-19 19:38:36 +0000674/// PragmaSystemHeaderHandler - "#pragma system_header" marks the current file
675/// as a system header, which silences warnings in it.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000676struct PragmaSystemHeaderHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000677 PragmaSystemHeaderHandler() : PragmaHandler("system_header") {}
Chris Lattner146762e2007-07-20 16:59:19 +0000678 virtual void HandlePragma(Preprocessor &PP, Token &SHToken) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000679 PP.HandlePragmaSystemHeader(SHToken);
Chris Lattnerce2ab6f2009-04-14 05:07:49 +0000680 PP.CheckEndOfDirective("pragma");
Chris Lattnerb694ba72006-07-02 22:41:36 +0000681 }
682};
683struct PragmaDependencyHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000684 PragmaDependencyHandler() : PragmaHandler("dependency") {}
Chris Lattner146762e2007-07-20 16:59:19 +0000685 virtual void HandlePragma(Preprocessor &PP, Token &DepToken) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000686 PP.HandlePragmaDependency(DepToken);
687 }
688};
Mike Stump11289f42009-09-09 15:08:12 +0000689
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000690struct PragmaDebugHandler : public PragmaHandler {
691 PragmaDebugHandler() : PragmaHandler("__debug") {}
692 virtual void HandlePragma(Preprocessor &PP, Token &DepToken) {
693 Token Tok;
694 PP.LexUnexpandedToken(Tok);
695 if (Tok.isNot(tok::identifier)) {
696 PP.Diag(Tok, diag::warn_pragma_diagnostic_clang_invalid);
697 return;
698 }
699 IdentifierInfo *II = Tok.getIdentifierInfo();
700
Daniel Dunbarf2cf3292010-08-17 22:32:48 +0000701 if (II->isStr("assert")) {
702 assert(0 && "This is an assertion!");
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000703 } else if (II->isStr("crash")) {
Daniel Dunbarf2cf3292010-08-17 22:32:48 +0000704 *(volatile int*) 0x11 = 0;
705 } else if (II->isStr("llvm_fatal_error")) {
706 llvm::report_fatal_error("#pragma clang __debug llvm_fatal_error");
707 } else if (II->isStr("llvm_unreachable")) {
708 llvm_unreachable("#pragma clang __debug llvm_unreachable");
709 } else if (II->isStr("overflow_stack")) {
710 DebugOverflowStack();
711 } else {
712 PP.Diag(Tok, diag::warn_pragma_debug_unexpected_command)
713 << II->getName();
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000714 }
715 }
716
717 void DebugOverflowStack() {
718 DebugOverflowStack();
719 }
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000720};
721
Chris Lattner504af112009-04-19 23:16:58 +0000722/// PragmaDiagnosticHandler - e.g. '#pragma GCC diagnostic ignored "-Wformat"'
Chris Lattnerfb42a182009-07-12 21:18:45 +0000723/// Since clang's diagnostic supports extended functionality beyond GCC's
724/// the constructor takes a clangMode flag to tell it whether or not to allow
725/// clang's extended functionality, or whether to reject it.
Chris Lattner504af112009-04-19 23:16:58 +0000726struct PragmaDiagnosticHandler : public PragmaHandler {
Chris Lattnerfb42a182009-07-12 21:18:45 +0000727private:
728 const bool ClangMode;
729public:
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000730 explicit PragmaDiagnosticHandler(const bool clangMode)
731 : PragmaHandler("diagnostic"), ClangMode(clangMode) {}
732
Chris Lattner504af112009-04-19 23:16:58 +0000733 virtual void HandlePragma(Preprocessor &PP, Token &DiagToken) {
734 Token Tok;
735 PP.LexUnexpandedToken(Tok);
736 if (Tok.isNot(tok::identifier)) {
Chris Lattnerfb42a182009-07-12 21:18:45 +0000737 unsigned Diag = ClangMode ? diag::warn_pragma_diagnostic_clang_invalid
738 : diag::warn_pragma_diagnostic_gcc_invalid;
739 PP.Diag(Tok, Diag);
Chris Lattner504af112009-04-19 23:16:58 +0000740 return;
741 }
742 IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000743
Chris Lattner504af112009-04-19 23:16:58 +0000744 diag::Mapping Map;
745 if (II->isStr("warning"))
746 Map = diag::MAP_WARNING;
747 else if (II->isStr("error"))
748 Map = diag::MAP_ERROR;
749 else if (II->isStr("ignored"))
750 Map = diag::MAP_IGNORE;
751 else if (II->isStr("fatal"))
752 Map = diag::MAP_FATAL;
Chris Lattnerfb42a182009-07-12 21:18:45 +0000753 else if (ClangMode) {
754 if (II->isStr("pop")) {
Mike Stump11289f42009-09-09 15:08:12 +0000755 if (!PP.getDiagnostics().popMappings())
Chris Lattnerfb42a182009-07-12 21:18:45 +0000756 PP.Diag(Tok, diag::warn_pragma_diagnostic_clang_cannot_ppp);
757 return;
758 }
759
760 if (II->isStr("push")) {
761 PP.getDiagnostics().pushMappings();
Mike Stump11289f42009-09-09 15:08:12 +0000762 return;
Chris Lattnerfb42a182009-07-12 21:18:45 +0000763 }
764
765 PP.Diag(Tok, diag::warn_pragma_diagnostic_clang_invalid);
766 return;
767 } else {
768 PP.Diag(Tok, diag::warn_pragma_diagnostic_gcc_invalid);
Chris Lattner504af112009-04-19 23:16:58 +0000769 return;
770 }
Mike Stump11289f42009-09-09 15:08:12 +0000771
Chris Lattner504af112009-04-19 23:16:58 +0000772 PP.LexUnexpandedToken(Tok);
773
774 // We need at least one string.
775 if (Tok.isNot(tok::string_literal)) {
776 PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token);
777 return;
778 }
Mike Stump11289f42009-09-09 15:08:12 +0000779
Chris Lattner504af112009-04-19 23:16:58 +0000780 // String concatenation allows multiple strings, which can even come from
781 // macro expansion.
782 // "foo " "bar" "Baz"
783 llvm::SmallVector<Token, 4> StrToks;
784 while (Tok.is(tok::string_literal)) {
785 StrToks.push_back(Tok);
786 PP.LexUnexpandedToken(Tok);
787 }
Mike Stump11289f42009-09-09 15:08:12 +0000788
Chris Lattner504af112009-04-19 23:16:58 +0000789 if (Tok.isNot(tok::eom)) {
790 PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token);
791 return;
792 }
Mike Stump11289f42009-09-09 15:08:12 +0000793
Chris Lattner504af112009-04-19 23:16:58 +0000794 // Concatenate and parse the strings.
795 StringLiteralParser Literal(&StrToks[0], StrToks.size(), PP);
796 assert(!Literal.AnyWide && "Didn't allow wide strings in");
797 if (Literal.hadError)
798 return;
799 if (Literal.Pascal) {
Chris Lattnerfb42a182009-07-12 21:18:45 +0000800 unsigned Diag = ClangMode ? diag::warn_pragma_diagnostic_clang_invalid
801 : diag::warn_pragma_diagnostic_gcc_invalid;
802 PP.Diag(Tok, Diag);
Chris Lattner504af112009-04-19 23:16:58 +0000803 return;
804 }
Chris Lattnerfb42a182009-07-12 21:18:45 +0000805
Chris Lattner504af112009-04-19 23:16:58 +0000806 std::string WarningName(Literal.GetString(),
807 Literal.GetString()+Literal.GetStringLength());
808
809 if (WarningName.size() < 3 || WarningName[0] != '-' ||
810 WarningName[1] != 'W') {
811 PP.Diag(StrToks[0].getLocation(),
812 diag::warn_pragma_diagnostic_invalid_option);
813 return;
814 }
Mike Stump11289f42009-09-09 15:08:12 +0000815
Chris Lattner504af112009-04-19 23:16:58 +0000816 if (PP.getDiagnostics().setDiagnosticGroupMapping(WarningName.c_str()+2,
817 Map))
818 PP.Diag(StrToks[0].getLocation(),
819 diag::warn_pragma_diagnostic_unknown_warning) << WarningName;
820 }
821};
Mike Stump11289f42009-09-09 15:08:12 +0000822
Chris Lattner2ff698d2009-01-16 08:21:25 +0000823/// PragmaCommentHandler - "#pragma comment ...".
824struct PragmaCommentHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000825 PragmaCommentHandler() : PragmaHandler("comment") {}
Chris Lattner2ff698d2009-01-16 08:21:25 +0000826 virtual void HandlePragma(Preprocessor &PP, Token &CommentTok) {
827 PP.HandlePragmaComment(CommentTok);
828 }
829};
Mike Stump11289f42009-09-09 15:08:12 +0000830
Chris Lattner30c924b2010-06-26 17:11:39 +0000831/// PragmaMessageHandler - "#pragma message("...")".
832struct PragmaMessageHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000833 PragmaMessageHandler() : PragmaHandler("message") {}
Chris Lattner30c924b2010-06-26 17:11:39 +0000834 virtual void HandlePragma(Preprocessor &PP, Token &CommentTok) {
835 PP.HandlePragmaMessage(CommentTok);
836 }
837};
838
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000839/// PragmaPushMacroHandler - "#pragma push_macro" saves the value of the
840/// macro on the top of the stack.
841struct PragmaPushMacroHandler : public PragmaHandler {
842 PragmaPushMacroHandler() : PragmaHandler("push_macro") {}
843 virtual void HandlePragma(Preprocessor &PP, Token &PushMacroTok) {
844 PP.HandlePragmaPushMacro(PushMacroTok);
845 }
846};
847
848
849/// PragmaPopMacroHandler - "#pragma pop_macro" sets the value of the
850/// macro to the value on the top of the stack.
851struct PragmaPopMacroHandler : public PragmaHandler {
852 PragmaPopMacroHandler() : PragmaHandler("pop_macro") {}
853 virtual void HandlePragma(Preprocessor &PP, Token &PopMacroTok) {
854 PP.HandlePragmaPopMacro(PopMacroTok);
855 }
856};
857
Chris Lattner958ee042009-04-19 21:20:35 +0000858// Pragma STDC implementations.
Chris Lattner02ef4e32009-04-19 21:50:08 +0000859
860enum STDCSetting {
861 STDC_ON, STDC_OFF, STDC_DEFAULT, STDC_INVALID
862};
Mike Stump11289f42009-09-09 15:08:12 +0000863
Chris Lattner02ef4e32009-04-19 21:50:08 +0000864static STDCSetting LexOnOffSwitch(Preprocessor &PP) {
865 Token Tok;
866 PP.LexUnexpandedToken(Tok);
867
868 if (Tok.isNot(tok::identifier)) {
869 PP.Diag(Tok, diag::ext_stdc_pragma_syntax);
870 return STDC_INVALID;
871 }
872 IdentifierInfo *II = Tok.getIdentifierInfo();
873 STDCSetting Result;
874 if (II->isStr("ON"))
875 Result = STDC_ON;
876 else if (II->isStr("OFF"))
877 Result = STDC_OFF;
878 else if (II->isStr("DEFAULT"))
879 Result = STDC_DEFAULT;
880 else {
881 PP.Diag(Tok, diag::ext_stdc_pragma_syntax);
882 return STDC_INVALID;
883 }
884
885 // Verify that this is followed by EOM.
886 PP.LexUnexpandedToken(Tok);
887 if (Tok.isNot(tok::eom))
888 PP.Diag(Tok, diag::ext_stdc_pragma_syntax_eom);
889 return Result;
890}
Mike Stump11289f42009-09-09 15:08:12 +0000891
Chris Lattner958ee042009-04-19 21:20:35 +0000892/// PragmaSTDC_FP_CONTRACTHandler - "#pragma STDC FP_CONTRACT ...".
893struct PragmaSTDC_FP_CONTRACTHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000894 PragmaSTDC_FP_CONTRACTHandler() : PragmaHandler("FP_CONTRACT") {}
Chris Lattnera0b1f762009-04-19 21:25:37 +0000895 virtual void HandlePragma(Preprocessor &PP, Token &Tok) {
Chris Lattner02ef4e32009-04-19 21:50:08 +0000896 // We just ignore the setting of FP_CONTRACT. Since we don't do contractions
897 // at all, our default is OFF and setting it to ON is an optimization hint
898 // we can safely ignore. When we support -ffma or something, we would need
899 // to diagnose that we are ignoring FMA.
900 LexOnOffSwitch(PP);
Chris Lattner958ee042009-04-19 21:20:35 +0000901 }
902};
Mike Stump11289f42009-09-09 15:08:12 +0000903
Chris Lattner958ee042009-04-19 21:20:35 +0000904/// PragmaSTDC_FENV_ACCESSHandler - "#pragma STDC FENV_ACCESS ...".
905struct PragmaSTDC_FENV_ACCESSHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000906 PragmaSTDC_FENV_ACCESSHandler() : PragmaHandler("FENV_ACCESS") {}
Chris Lattnera0b1f762009-04-19 21:25:37 +0000907 virtual void HandlePragma(Preprocessor &PP, Token &Tok) {
Chris Lattnerdf222682009-04-19 21:55:32 +0000908 if (LexOnOffSwitch(PP) == STDC_ON)
909 PP.Diag(Tok, diag::warn_stdc_fenv_access_not_supported);
Chris Lattner958ee042009-04-19 21:20:35 +0000910 }
911};
Mike Stump11289f42009-09-09 15:08:12 +0000912
Chris Lattner958ee042009-04-19 21:20:35 +0000913/// PragmaSTDC_CX_LIMITED_RANGEHandler - "#pragma STDC CX_LIMITED_RANGE ...".
914struct PragmaSTDC_CX_LIMITED_RANGEHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000915 PragmaSTDC_CX_LIMITED_RANGEHandler()
916 : PragmaHandler("CX_LIMITED_RANGE") {}
Chris Lattnera0b1f762009-04-19 21:25:37 +0000917 virtual void HandlePragma(Preprocessor &PP, Token &Tok) {
Chris Lattner02ef4e32009-04-19 21:50:08 +0000918 LexOnOffSwitch(PP);
Chris Lattner958ee042009-04-19 21:20:35 +0000919 }
920};
Mike Stump11289f42009-09-09 15:08:12 +0000921
Chris Lattner958ee042009-04-19 21:20:35 +0000922/// PragmaSTDC_UnknownHandler - "#pragma STDC ...".
923struct PragmaSTDC_UnknownHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000924 PragmaSTDC_UnknownHandler() {}
Chris Lattnera0b1f762009-04-19 21:25:37 +0000925 virtual void HandlePragma(Preprocessor &PP, Token &UnknownTok) {
Chris Lattner02ef4e32009-04-19 21:50:08 +0000926 // C99 6.10.6p2, unknown forms are not allowed.
Chris Lattnera0b1f762009-04-19 21:25:37 +0000927 PP.Diag(UnknownTok, diag::ext_stdc_pragma_ignored);
Chris Lattner958ee042009-04-19 21:20:35 +0000928 }
929};
Mike Stump11289f42009-09-09 15:08:12 +0000930
Chris Lattnerb694ba72006-07-02 22:41:36 +0000931} // end anonymous namespace
932
933
934/// RegisterBuiltinPragmas - Install the standard preprocessor pragmas:
935/// #pragma GCC poison/system_header/dependency and #pragma once.
936void Preprocessor::RegisterBuiltinPragmas() {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000937 AddPragmaHandler(new PragmaOnceHandler());
938 AddPragmaHandler(new PragmaMarkHandler());
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000939 AddPragmaHandler(new PragmaPushMacroHandler());
940 AddPragmaHandler(new PragmaPopMacroHandler());
Mike Stump11289f42009-09-09 15:08:12 +0000941
Chris Lattnerb61448d2009-05-12 18:21:11 +0000942 // #pragma GCC ...
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000943 AddPragmaHandler("GCC", new PragmaPoisonHandler());
944 AddPragmaHandler("GCC", new PragmaSystemHeaderHandler());
945 AddPragmaHandler("GCC", new PragmaDependencyHandler());
946 AddPragmaHandler("GCC", new PragmaDiagnosticHandler(false));
Chris Lattnerb61448d2009-05-12 18:21:11 +0000947 // #pragma clang ...
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000948 AddPragmaHandler("clang", new PragmaPoisonHandler());
949 AddPragmaHandler("clang", new PragmaSystemHeaderHandler());
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000950 AddPragmaHandler("clang", new PragmaDebugHandler());
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000951 AddPragmaHandler("clang", new PragmaDependencyHandler());
952 AddPragmaHandler("clang", new PragmaDiagnosticHandler(true));
Chris Lattnerb61448d2009-05-12 18:21:11 +0000953
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000954 AddPragmaHandler("STDC", new PragmaSTDC_FP_CONTRACTHandler());
955 AddPragmaHandler("STDC", new PragmaSTDC_FENV_ACCESSHandler());
956 AddPragmaHandler("STDC", new PragmaSTDC_CX_LIMITED_RANGEHandler());
Chris Lattner958ee042009-04-19 21:20:35 +0000957 AddPragmaHandler("STDC", new PragmaSTDC_UnknownHandler());
Mike Stump11289f42009-09-09 15:08:12 +0000958
Chris Lattner2ff698d2009-01-16 08:21:25 +0000959 // MS extensions.
Chris Lattner30c924b2010-06-26 17:11:39 +0000960 if (Features.Microsoft) {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000961 AddPragmaHandler(new PragmaCommentHandler());
962 AddPragmaHandler(new PragmaMessageHandler());
Chris Lattner30c924b2010-06-26 17:11:39 +0000963 }
Chris Lattnerb694ba72006-07-02 22:41:36 +0000964}