blob: eec1d1d6eea54a6521a2d88fc7335cfc0ba64866 [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"
Douglas Gregorc6d5edd2009-07-02 17:08:52 +000023#include <algorithm>
Chris Lattnerb8761832006-06-24 21:31:03 +000024using namespace clang;
25
26// Out-of-line destructor to provide a home for the class.
27PragmaHandler::~PragmaHandler() {
28}
29
Chris Lattner2e155302006-07-03 05:34:41 +000030//===----------------------------------------------------------------------===//
Daniel Dunbard839e772010-06-11 20:10:12 +000031// EmptyPragmaHandler Implementation.
32//===----------------------------------------------------------------------===//
33
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000034EmptyPragmaHandler::EmptyPragmaHandler() {}
Daniel Dunbard839e772010-06-11 20:10:12 +000035
36void EmptyPragmaHandler::HandlePragma(Preprocessor &PP, Token &FirstToken) {}
37
38//===----------------------------------------------------------------------===//
Chris Lattner2e155302006-07-03 05:34:41 +000039// PragmaNamespace Implementation.
40//===----------------------------------------------------------------------===//
41
42
43PragmaNamespace::~PragmaNamespace() {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000044 for (llvm::StringMap<PragmaHandler*>::iterator
45 I = Handlers.begin(), E = Handlers.end(); I != E; ++I)
46 delete I->second;
Chris Lattner2e155302006-07-03 05:34:41 +000047}
48
49/// FindHandler - Check to see if there is already a handler for the
50/// specified name. If not, return the handler for the null identifier if it
51/// exists, otherwise return null. If IgnoreNull is true (the default) then
52/// the null handler isn't returned on failure to match.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000053PragmaHandler *PragmaNamespace::FindHandler(llvm::StringRef Name,
Chris Lattner2e155302006-07-03 05:34:41 +000054 bool IgnoreNull) const {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000055 if (PragmaHandler *Handler = Handlers.lookup(Name))
56 return Handler;
57 return IgnoreNull ? 0 : Handlers.lookup(llvm::StringRef());
58}
Mike Stump11289f42009-09-09 15:08:12 +000059
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000060void PragmaNamespace::AddPragma(PragmaHandler *Handler) {
61 assert(!Handlers.lookup(Handler->getName()) &&
62 "A handler with this name is already registered in this namespace");
63 llvm::StringMapEntry<PragmaHandler *> &Entry =
64 Handlers.GetOrCreateValue(Handler->getName());
65 Entry.setValue(Handler);
Chris Lattner2e155302006-07-03 05:34:41 +000066}
67
Daniel Dunbar40596532008-10-04 19:17:46 +000068void PragmaNamespace::RemovePragmaHandler(PragmaHandler *Handler) {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000069 assert(Handlers.lookup(Handler->getName()) &&
70 "Handler not registered in this namespace");
71 Handlers.erase(Handler->getName());
Daniel Dunbar40596532008-10-04 19:17:46 +000072}
73
Chris Lattner146762e2007-07-20 16:59:19 +000074void PragmaNamespace::HandlePragma(Preprocessor &PP, Token &Tok) {
Chris Lattnerb8761832006-06-24 21:31:03 +000075 // Read the 'namespace' that the directive is in, e.g. STDC. Do not macro
76 // expand it, the user can have a STDC #define, that should not affect this.
77 PP.LexUnexpandedToken(Tok);
Mike Stump11289f42009-09-09 15:08:12 +000078
Chris Lattnerb8761832006-06-24 21:31:03 +000079 // Get the handler for this token. If there is no handler, ignore the pragma.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000080 PragmaHandler *Handler
81 = FindHandler(Tok.getIdentifierInfo() ? Tok.getIdentifierInfo()->getName()
82 : llvm::StringRef(),
83 /*IgnoreNull=*/false);
Chris Lattner21656f22009-04-19 21:10:26 +000084 if (Handler == 0) {
85 PP.Diag(Tok, diag::warn_pragma_ignored);
86 return;
87 }
Mike Stump11289f42009-09-09 15:08:12 +000088
Chris Lattnerb8761832006-06-24 21:31:03 +000089 // Otherwise, pass it down.
90 Handler->HandlePragma(PP, Tok);
91}
Chris Lattnerb694ba72006-07-02 22:41:36 +000092
Chris Lattnerb694ba72006-07-02 22:41:36 +000093//===----------------------------------------------------------------------===//
94// Preprocessor Pragma Directive Handling.
95//===----------------------------------------------------------------------===//
96
97/// HandlePragmaDirective - The "#pragma" directive has been parsed. Lex the
98/// rest of the pragma, passing it to the registered pragma handlers.
99void Preprocessor::HandlePragmaDirective() {
100 ++NumPragma;
Mike Stump11289f42009-09-09 15:08:12 +0000101
Chris Lattnerb694ba72006-07-02 22:41:36 +0000102 // Invoke the first level of pragma handlers which reads the namespace id.
Chris Lattner146762e2007-07-20 16:59:19 +0000103 Token Tok;
Chris Lattnerb694ba72006-07-02 22:41:36 +0000104 PragmaHandlers->HandlePragma(*this, Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000105
Chris Lattnerb694ba72006-07-02 22:41:36 +0000106 // If the pragma handler didn't read the rest of the line, consume it now.
Chris Lattnere7e65942009-06-18 05:55:53 +0000107 if (CurPPLexer && CurPPLexer->ParsingPreprocessorDirective)
Chris Lattnerb694ba72006-07-02 22:41:36 +0000108 DiscardUntilEndOfDirective();
109}
110
111/// Handle_Pragma - Read a _Pragma directive, slice it up, process it, then
112/// return the first token after the directive. The _Pragma token has just
113/// been read into 'Tok'.
Chris Lattner146762e2007-07-20 16:59:19 +0000114void Preprocessor::Handle_Pragma(Token &Tok) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000115 // Remember the pragma token location.
116 SourceLocation PragmaLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000117
Chris Lattnerb694ba72006-07-02 22:41:36 +0000118 // Read the '('.
119 Lex(Tok);
Chris Lattner907dfe92008-11-18 07:59:24 +0000120 if (Tok.isNot(tok::l_paren)) {
121 Diag(PragmaLoc, diag::err__Pragma_malformed);
122 return;
123 }
Chris Lattnerb694ba72006-07-02 22:41:36 +0000124
125 // Read the '"..."'.
126 Lex(Tok);
Chris Lattner907dfe92008-11-18 07:59:24 +0000127 if (Tok.isNot(tok::string_literal) && Tok.isNot(tok::wide_string_literal)) {
128 Diag(PragmaLoc, diag::err__Pragma_malformed);
129 return;
130 }
Mike Stump11289f42009-09-09 15:08:12 +0000131
Chris Lattnerb694ba72006-07-02 22:41:36 +0000132 // Remember the string.
133 std::string StrVal = getSpelling(Tok);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000134
135 // Read the ')'.
136 Lex(Tok);
Chris Lattner907dfe92008-11-18 07:59:24 +0000137 if (Tok.isNot(tok::r_paren)) {
138 Diag(PragmaLoc, diag::err__Pragma_malformed);
139 return;
140 }
Mike Stump11289f42009-09-09 15:08:12 +0000141
Chris Lattner9dc9c202009-02-15 20:52:18 +0000142 SourceLocation RParenLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000143
Chris Lattner262d4e32009-01-16 18:59:23 +0000144 // The _Pragma is lexically sound. Destringize according to C99 6.10.9.1:
145 // "The string literal is destringized by deleting the L prefix, if present,
146 // deleting the leading and trailing double-quotes, replacing each escape
147 // sequence \" by a double-quote, and replacing each escape sequence \\ by a
148 // single backslash."
Chris Lattnerb694ba72006-07-02 22:41:36 +0000149 if (StrVal[0] == 'L') // Remove L prefix.
150 StrVal.erase(StrVal.begin());
151 assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
152 "Invalid string token!");
Mike Stump11289f42009-09-09 15:08:12 +0000153
Chris Lattnerb694ba72006-07-02 22:41:36 +0000154 // Remove the front quote, replacing it with a space, so that the pragma
155 // contents appear to have a space before them.
156 StrVal[0] = ' ';
Mike Stump11289f42009-09-09 15:08:12 +0000157
Chris Lattnerfa217bd2009-03-08 08:08:45 +0000158 // Replace the terminating quote with a \n.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000159 StrVal[StrVal.size()-1] = '\n';
Mike Stump11289f42009-09-09 15:08:12 +0000160
Chris Lattnerb694ba72006-07-02 22:41:36 +0000161 // Remove escaped quotes and escapes.
162 for (unsigned i = 0, e = StrVal.size(); i != e-1; ++i) {
163 if (StrVal[i] == '\\' &&
164 (StrVal[i+1] == '\\' || StrVal[i+1] == '"')) {
165 // \\ -> '\' and \" -> '"'.
166 StrVal.erase(StrVal.begin()+i);
167 --e;
168 }
169 }
Mike Stump11289f42009-09-09 15:08:12 +0000170
Chris Lattnerf918bf72006-07-19 05:01:18 +0000171 // Plop the string (including the newline and trailing null) into a buffer
172 // where we can lex it.
Chris Lattner5a7971e2009-01-26 19:29:26 +0000173 Token TmpTok;
174 TmpTok.startToken();
175 CreateString(&StrVal[0], StrVal.size(), TmpTok);
176 SourceLocation TokLoc = TmpTok.getLocation();
Chris Lattnerb694ba72006-07-02 22:41:36 +0000177
Chris Lattnerb694ba72006-07-02 22:41:36 +0000178 // Make and enter a lexer object so that we lex and expand the tokens just
179 // like any others.
Chris Lattner9dc9c202009-02-15 20:52:18 +0000180 Lexer *TL = Lexer::Create_PragmaLexer(TokLoc, PragmaLoc, RParenLoc,
Chris Lattnerfa217bd2009-03-08 08:08:45 +0000181 StrVal.size(), *this);
Chris Lattner98a53122006-07-02 23:00:20 +0000182
183 EnterSourceFileWithLexer(TL, 0);
184
Chris Lattnerb694ba72006-07-02 22:41:36 +0000185 // With everything set up, lex this as a #pragma directive.
186 HandlePragmaDirective();
Mike Stump11289f42009-09-09 15:08:12 +0000187
Chris Lattnerb694ba72006-07-02 22:41:36 +0000188 // Finally, return whatever came after the pragma directive.
189 return Lex(Tok);
190}
191
192
193
194/// HandlePragmaOnce - Handle #pragma once. OnceTok is the 'once'.
195///
Chris Lattner146762e2007-07-20 16:59:19 +0000196void Preprocessor::HandlePragmaOnce(Token &OnceTok) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000197 if (isInPrimaryFile()) {
198 Diag(OnceTok, diag::pp_pragma_once_in_main_file);
199 return;
200 }
Mike Stump11289f42009-09-09 15:08:12 +0000201
Chris Lattnerb694ba72006-07-02 22:41:36 +0000202 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000203 // Mark the file as a once-only file now.
Chris Lattnerd32480d2009-01-17 06:22:33 +0000204 HeaderInfo.MarkFileIncludeOnce(getCurrentFileLexer()->getFileEntry());
Chris Lattnerb694ba72006-07-02 22:41:36 +0000205}
206
Chris Lattnerc2383312007-12-19 19:38:36 +0000207void Preprocessor::HandlePragmaMark() {
Ted Kremenek76c34412008-11-19 22:21:33 +0000208 assert(CurPPLexer && "No current lexer?");
Chris Lattnerd9efb6e2009-06-15 05:02:34 +0000209 if (CurLexer)
210 CurLexer->ReadToEndOfLine();
211 else
212 CurPTHLexer->DiscardToEndOfLine();
Chris Lattnerc2383312007-12-19 19:38:36 +0000213}
214
215
Chris Lattnerb694ba72006-07-02 22:41:36 +0000216/// HandlePragmaPoison - Handle #pragma GCC poison. PoisonTok is the 'poison'.
217///
Chris Lattner146762e2007-07-20 16:59:19 +0000218void Preprocessor::HandlePragmaPoison(Token &PoisonTok) {
219 Token Tok;
Chris Lattner538d7f32006-07-20 04:31:52 +0000220
Chris Lattnerb694ba72006-07-02 22:41:36 +0000221 while (1) {
222 // Read the next token to poison. While doing this, pretend that we are
223 // skipping while reading the identifier to poison.
224 // This avoids errors on code like:
225 // #pragma GCC poison X
226 // #pragma GCC poison X
Ted Kremenek551c82a2008-11-18 01:12:54 +0000227 if (CurPPLexer) CurPPLexer->LexingRawMode = true;
Chris Lattnerb694ba72006-07-02 22:41:36 +0000228 LexUnexpandedToken(Tok);
Ted Kremenek551c82a2008-11-18 01:12:54 +0000229 if (CurPPLexer) CurPPLexer->LexingRawMode = false;
Mike Stump11289f42009-09-09 15:08:12 +0000230
Chris Lattnerb694ba72006-07-02 22:41:36 +0000231 // If we reached the end of line, we're done.
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000232 if (Tok.is(tok::eom)) return;
Mike Stump11289f42009-09-09 15:08:12 +0000233
Chris Lattnerb694ba72006-07-02 22:41:36 +0000234 // Can only poison identifiers.
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000235 if (Tok.isNot(tok::identifier)) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000236 Diag(Tok, diag::err_pp_invalid_poison);
237 return;
238 }
Mike Stump11289f42009-09-09 15:08:12 +0000239
Chris Lattnercefc7682006-07-08 08:28:12 +0000240 // Look up the identifier info for the token. We disabled identifier lookup
241 // by saying we're skipping contents, so we need to do this manually.
242 IdentifierInfo *II = LookUpIdentifierInfo(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000243
Chris Lattnerb694ba72006-07-02 22:41:36 +0000244 // Already poisoned.
245 if (II->isPoisoned()) continue;
Mike Stump11289f42009-09-09 15:08:12 +0000246
Chris Lattnerb694ba72006-07-02 22:41:36 +0000247 // If this is a macro identifier, emit a warning.
Chris Lattner259716a2007-10-07 08:04:56 +0000248 if (II->hasMacroDefinition())
Chris Lattnerb694ba72006-07-02 22:41:36 +0000249 Diag(Tok, diag::pp_poisoning_existing_macro);
Mike Stump11289f42009-09-09 15:08:12 +0000250
Chris Lattnerb694ba72006-07-02 22:41:36 +0000251 // Finally, poison it!
252 II->setIsPoisoned();
253 }
254}
255
256/// HandlePragmaSystemHeader - Implement #pragma GCC system_header. We know
257/// that the whole directive has been parsed.
Chris Lattner146762e2007-07-20 16:59:19 +0000258void Preprocessor::HandlePragmaSystemHeader(Token &SysHeaderTok) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000259 if (isInPrimaryFile()) {
260 Diag(SysHeaderTok, diag::pp_pragma_sysheader_in_main_file);
261 return;
262 }
Mike Stump11289f42009-09-09 15:08:12 +0000263
Chris Lattnerb694ba72006-07-02 22:41:36 +0000264 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
Ted Kremenek300590b2008-11-20 01:45:11 +0000265 PreprocessorLexer *TheLexer = getCurrentFileLexer();
Mike Stump11289f42009-09-09 15:08:12 +0000266
Chris Lattnerb694ba72006-07-02 22:41:36 +0000267 // Mark the file as a system header.
Chris Lattnerd32480d2009-01-17 06:22:33 +0000268 HeaderInfo.MarkFileSystemHeader(TheLexer->getFileEntry());
Mike Stump11289f42009-09-09 15:08:12 +0000269
270
Chris Lattnerd9efb6e2009-06-15 05:02:34 +0000271 PresumedLoc PLoc = SourceMgr.getPresumedLoc(SysHeaderTok.getLocation());
272 unsigned FilenameLen = strlen(PLoc.getFilename());
273 unsigned FilenameID = SourceMgr.getLineTableFilenameID(PLoc.getFilename(),
274 FilenameLen);
Mike Stump11289f42009-09-09 15:08:12 +0000275
Chris Lattnerd9efb6e2009-06-15 05:02:34 +0000276 // Emit a line marker. This will change any source locations from this point
277 // forward to realize they are in a system header.
278 // Create a line note with this information.
279 SourceMgr.AddLineNote(SysHeaderTok.getLocation(), PLoc.getLine(), FilenameID,
280 false, false, true, false);
Mike Stump11289f42009-09-09 15:08:12 +0000281
Chris Lattnerb694ba72006-07-02 22:41:36 +0000282 // Notify the client, if desired, that we are in a new source file.
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000283 if (Callbacks)
Ted Kremenek300590b2008-11-20 01:45:11 +0000284 Callbacks->FileChanged(SysHeaderTok.getLocation(),
Chris Lattnerb03dc762008-09-26 21:18:42 +0000285 PPCallbacks::SystemHeaderPragma, SrcMgr::C_System);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000286}
287
288/// HandlePragmaDependency - Handle #pragma GCC dependency "foo" blah.
289///
Chris Lattner146762e2007-07-20 16:59:19 +0000290void Preprocessor::HandlePragmaDependency(Token &DependencyTok) {
291 Token FilenameTok;
Ted Kremenek551c82a2008-11-18 01:12:54 +0000292 CurPPLexer->LexIncludeFilename(FilenameTok);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000293
294 // If the token kind is EOM, the error has already been diagnosed.
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000295 if (FilenameTok.is(tok::eom))
Chris Lattnerb694ba72006-07-02 22:41:36 +0000296 return;
Mike Stump11289f42009-09-09 15:08:12 +0000297
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000298 // Reserve a buffer to get the spelling.
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000299 llvm::SmallString<128> FilenameBuffer;
Douglas Gregordc970f02010-03-16 22:30:13 +0000300 bool Invalid = false;
301 llvm::StringRef Filename = getSpelling(FilenameTok, FilenameBuffer, &Invalid);
302 if (Invalid)
303 return;
Mike Stump11289f42009-09-09 15:08:12 +0000304
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000305 bool isAngled =
306 GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000307 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
308 // error.
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000309 if (Filename.empty())
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000310 return;
Mike Stump11289f42009-09-09 15:08:12 +0000311
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000312 // Search include directories for this file.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000313 const DirectoryLookup *CurDir;
Chris Lattnerfde85352010-01-22 00:14:44 +0000314 const FileEntry *File = LookupFile(Filename, isAngled, 0, CurDir);
Chris Lattner97b8e842008-11-18 08:02:48 +0000315 if (File == 0) {
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000316 Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
Chris Lattner97b8e842008-11-18 08:02:48 +0000317 return;
318 }
Mike Stump11289f42009-09-09 15:08:12 +0000319
Chris Lattnerd32480d2009-01-17 06:22:33 +0000320 const FileEntry *CurFile = getCurrentFileLexer()->getFileEntry();
Chris Lattnerb694ba72006-07-02 22:41:36 +0000321
322 // If this file is older than the file it depends on, emit a diagnostic.
323 if (CurFile && CurFile->getModificationTime() < File->getModificationTime()) {
324 // Lex tokens at the end of the message and include them in the message.
325 std::string Message;
326 Lex(DependencyTok);
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000327 while (DependencyTok.isNot(tok::eom)) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000328 Message += getSpelling(DependencyTok) + " ";
329 Lex(DependencyTok);
330 }
Mike Stump11289f42009-09-09 15:08:12 +0000331
Chris Lattnerb694ba72006-07-02 22:41:36 +0000332 Message.erase(Message.end()-1);
Chris Lattner97b8e842008-11-18 08:02:48 +0000333 Diag(FilenameTok, diag::pp_out_of_date_dependency) << Message;
Chris Lattnerb694ba72006-07-02 22:41:36 +0000334 }
335}
336
Chris Lattner2ff698d2009-01-16 08:21:25 +0000337/// HandlePragmaComment - Handle the microsoft #pragma comment extension. The
338/// syntax is:
339/// #pragma comment(linker, "foo")
340/// 'linker' is one of five identifiers: compiler, exestr, lib, linker, user.
341/// "foo" is a string, which is fully macro expanded, and permits string
Gabor Greif31a082f2009-03-17 11:39:38 +0000342/// concatenation, embedded escape characters etc. See MSDN for more details.
Chris Lattner2ff698d2009-01-16 08:21:25 +0000343void Preprocessor::HandlePragmaComment(Token &Tok) {
344 SourceLocation CommentLoc = Tok.getLocation();
345 Lex(Tok);
346 if (Tok.isNot(tok::l_paren)) {
347 Diag(CommentLoc, diag::err_pragma_comment_malformed);
348 return;
349 }
Mike Stump11289f42009-09-09 15:08:12 +0000350
Chris Lattner2ff698d2009-01-16 08:21:25 +0000351 // Read the identifier.
352 Lex(Tok);
353 if (Tok.isNot(tok::identifier)) {
354 Diag(CommentLoc, diag::err_pragma_comment_malformed);
355 return;
356 }
Mike Stump11289f42009-09-09 15:08:12 +0000357
Chris Lattner2ff698d2009-01-16 08:21:25 +0000358 // Verify that this is one of the 5 whitelisted options.
359 // FIXME: warn that 'exestr' is deprecated.
360 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000361 if (!II->isStr("compiler") && !II->isStr("exestr") && !II->isStr("lib") &&
Chris Lattner2ff698d2009-01-16 08:21:25 +0000362 !II->isStr("linker") && !II->isStr("user")) {
363 Diag(Tok.getLocation(), diag::err_pragma_comment_unknown_kind);
364 return;
365 }
Mike Stump11289f42009-09-09 15:08:12 +0000366
Chris Lattner262d4e32009-01-16 18:59:23 +0000367 // Read the optional string if present.
Chris Lattner2ff698d2009-01-16 08:21:25 +0000368 Lex(Tok);
Chris Lattner262d4e32009-01-16 18:59:23 +0000369 std::string ArgumentString;
Chris Lattner2ff698d2009-01-16 08:21:25 +0000370 if (Tok.is(tok::comma)) {
Chris Lattner262d4e32009-01-16 18:59:23 +0000371 Lex(Tok); // eat the comma.
Chris Lattner2ff698d2009-01-16 08:21:25 +0000372
373 // We need at least one string.
Chris Lattner504af112009-04-19 23:16:58 +0000374 if (Tok.isNot(tok::string_literal)) {
Chris Lattner2ff698d2009-01-16 08:21:25 +0000375 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
376 return;
377 }
378
379 // String concatenation allows multiple strings, which can even come from
380 // macro expansion.
381 // "foo " "bar" "Baz"
Chris Lattner262d4e32009-01-16 18:59:23 +0000382 llvm::SmallVector<Token, 4> StrToks;
Chris Lattner504af112009-04-19 23:16:58 +0000383 while (Tok.is(tok::string_literal)) {
Chris Lattner262d4e32009-01-16 18:59:23 +0000384 StrToks.push_back(Tok);
Chris Lattner2ff698d2009-01-16 08:21:25 +0000385 Lex(Tok);
Chris Lattner262d4e32009-01-16 18:59:23 +0000386 }
387
388 // Concatenate and parse the strings.
389 StringLiteralParser Literal(&StrToks[0], StrToks.size(), *this);
390 assert(!Literal.AnyWide && "Didn't allow wide strings in");
391 if (Literal.hadError)
392 return;
393 if (Literal.Pascal) {
394 Diag(StrToks[0].getLocation(), diag::err_pragma_comment_malformed);
395 return;
396 }
397
398 ArgumentString = std::string(Literal.GetString(),
399 Literal.GetString()+Literal.GetStringLength());
Chris Lattner2ff698d2009-01-16 08:21:25 +0000400 }
Mike Stump11289f42009-09-09 15:08:12 +0000401
Chris Lattner262d4e32009-01-16 18:59:23 +0000402 // FIXME: If the kind is "compiler" warn if the string is present (it is
403 // ignored).
404 // FIXME: 'lib' requires a comment string.
405 // FIXME: 'linker' requires a comment string, and has a specific list of
406 // things that are allowable.
Mike Stump11289f42009-09-09 15:08:12 +0000407
Chris Lattner2ff698d2009-01-16 08:21:25 +0000408 if (Tok.isNot(tok::r_paren)) {
409 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
410 return;
411 }
Chris Lattner262d4e32009-01-16 18:59:23 +0000412 Lex(Tok); // eat the r_paren.
Chris Lattner2ff698d2009-01-16 08:21:25 +0000413
414 if (Tok.isNot(tok::eom)) {
415 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
416 return;
417 }
Mike Stump11289f42009-09-09 15:08:12 +0000418
Chris Lattner262d4e32009-01-16 18:59:23 +0000419 // If the pragma is lexically sound, notify any interested PPCallbacks.
Chris Lattnerf49775d2009-01-16 19:01:46 +0000420 if (Callbacks)
421 Callbacks->PragmaComment(CommentLoc, II, ArgumentString);
Chris Lattner2ff698d2009-01-16 08:21:25 +0000422}
423
Chris Lattner30c924b2010-06-26 17:11:39 +0000424/// HandlePragmaMessage - Handle the microsoft #pragma message extension. The
425/// syntax is:
426/// #pragma message(messagestring)
427/// messagestring is a string, which is fully macro expanded, and permits string
428/// concatenation, embedded escape characters etc. See MSDN for more details.
429void Preprocessor::HandlePragmaMessage(Token &Tok) {
430 SourceLocation MessageLoc = Tok.getLocation();
431 Lex(Tok);
432 if (Tok.isNot(tok::l_paren)) {
433 Diag(MessageLoc, diag::err_pragma_message_malformed);
434 return;
435 }
Chris Lattner2ff698d2009-01-16 08:21:25 +0000436
Chris Lattner30c924b2010-06-26 17:11:39 +0000437 // Read the string.
438 Lex(Tok);
439
440
441 // We need at least one string.
442 if (Tok.isNot(tok::string_literal)) {
443 Diag(Tok.getLocation(), diag::err_pragma_message_malformed);
444 return;
445 }
446
447 // String concatenation allows multiple strings, which can even come from
448 // macro expansion.
449 // "foo " "bar" "Baz"
450 llvm::SmallVector<Token, 4> StrToks;
451 while (Tok.is(tok::string_literal)) {
452 StrToks.push_back(Tok);
453 Lex(Tok);
454 }
455
456 // Concatenate and parse the strings.
457 StringLiteralParser Literal(&StrToks[0], StrToks.size(), *this);
458 assert(!Literal.AnyWide && "Didn't allow wide strings in");
459 if (Literal.hadError)
460 return;
461 if (Literal.Pascal) {
462 Diag(StrToks[0].getLocation(), diag::err_pragma_message_malformed);
463 return;
464 }
465
466 llvm::StringRef MessageString(Literal.GetString(), Literal.GetStringLength());
467
468 if (Tok.isNot(tok::r_paren)) {
469 Diag(Tok.getLocation(), diag::err_pragma_message_malformed);
470 return;
471 }
472 Lex(Tok); // eat the r_paren.
473
474 if (Tok.isNot(tok::eom)) {
475 Diag(Tok.getLocation(), diag::err_pragma_message_malformed);
476 return;
477 }
478
479 // Output the message.
480 Diag(MessageLoc, diag::warn_pragma_message) << MessageString;
481
482 // If the pragma is lexically sound, notify any interested PPCallbacks.
483 if (Callbacks)
484 Callbacks->PragmaMessage(MessageLoc, MessageString);
485}
Chris Lattner2ff698d2009-01-16 08:21:25 +0000486
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000487/// ParsePragmaPushOrPopMacro - Handle parsing of pragma push_macro/pop_macro.
488/// Return the IdentifierInfo* associated with the macro to push or pop.
489IdentifierInfo *Preprocessor::ParsePragmaPushOrPopMacro(Token &Tok) {
490 // Remember the pragma token location.
491 Token PragmaTok = Tok;
492
493 // Read the '('.
494 Lex(Tok);
495 if (Tok.isNot(tok::l_paren)) {
496 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
497 << getSpelling(PragmaTok);
498 return 0;
499 }
500
501 // Read the macro name string.
502 Lex(Tok);
503 if (Tok.isNot(tok::string_literal)) {
504 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
505 << getSpelling(PragmaTok);
506 return 0;
507 }
508
509 // Remember the macro string.
510 std::string StrVal = getSpelling(Tok);
511
512 // Read the ')'.
513 Lex(Tok);
514 if (Tok.isNot(tok::r_paren)) {
515 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
516 << getSpelling(PragmaTok);
517 return 0;
518 }
519
520 assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
521 "Invalid string token!");
522
523 // Create a Token from the string.
524 Token MacroTok;
525 MacroTok.startToken();
526 MacroTok.setKind(tok::identifier);
527 CreateString(&StrVal[1], StrVal.size() - 2, MacroTok);
528
529 // Get the IdentifierInfo of MacroToPushTok.
530 return LookUpIdentifierInfo(MacroTok);
531}
532
533/// HandlePragmaPushMacro - Handle #pragma push_macro.
534/// The syntax is:
535/// #pragma push_macro("macro")
536void Preprocessor::HandlePragmaPushMacro(Token &PushMacroTok) {
537 // Parse the pragma directive and get the macro IdentifierInfo*.
538 IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PushMacroTok);
539 if (!IdentInfo) return;
540
541 // Get the MacroInfo associated with IdentInfo.
542 MacroInfo *MI = getMacroInfo(IdentInfo);
543
544 MacroInfo *MacroCopyToPush = 0;
545 if (MI) {
546 // Make a clone of MI.
547 MacroCopyToPush = CloneMacroInfo(*MI);
548
549 // Allow the original MacroInfo to be redefined later.
550 MI->setIsAllowRedefinitionsWithoutWarning(true);
551 }
552
553 // Push the cloned MacroInfo so we can retrieve it later.
554 PragmaPushMacroInfo[IdentInfo].push_back(MacroCopyToPush);
555}
556
557/// HandlePragmaPopMacro - Handle #pragma push_macro.
558/// The syntax is:
559/// #pragma pop_macro("macro")
560void Preprocessor::HandlePragmaPopMacro(Token &PopMacroTok) {
561 SourceLocation MessageLoc = PopMacroTok.getLocation();
562
563 // Parse the pragma directive and get the macro IdentifierInfo*.
564 IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PopMacroTok);
565 if (!IdentInfo) return;
566
567 // Find the vector<MacroInfo*> associated with the macro.
568 llvm::DenseMap<IdentifierInfo*, std::vector<MacroInfo*> >::iterator iter =
569 PragmaPushMacroInfo.find(IdentInfo);
570 if (iter != PragmaPushMacroInfo.end()) {
571 // Release the MacroInfo currently associated with IdentInfo.
572 MacroInfo *CurrentMI = getMacroInfo(IdentInfo);
573 if (CurrentMI) ReleaseMacroInfo(CurrentMI);
574
575 // Get the MacroInfo we want to reinstall.
576 MacroInfo *MacroToReInstall = iter->second.back();
577
578 // Reinstall the previously pushed macro.
579 setMacroInfo(IdentInfo, MacroToReInstall);
580
581 // Pop PragmaPushMacroInfo stack.
582 iter->second.pop_back();
583 if (iter->second.size() == 0)
584 PragmaPushMacroInfo.erase(iter);
585 } else {
586 Diag(MessageLoc, diag::warn_pragma_pop_macro_no_push)
587 << IdentInfo->getName();
588 }
589}
Chris Lattnerb694ba72006-07-02 22:41:36 +0000590
591/// AddPragmaHandler - Add the specified pragma handler to the preprocessor.
592/// If 'Namespace' is non-null, then it is a token required to exist on the
593/// pragma line before the pragma string starts, e.g. "STDC" or "GCC".
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000594void Preprocessor::AddPragmaHandler(llvm::StringRef Namespace,
Chris Lattnerb694ba72006-07-02 22:41:36 +0000595 PragmaHandler *Handler) {
596 PragmaNamespace *InsertNS = PragmaHandlers;
Mike Stump11289f42009-09-09 15:08:12 +0000597
Chris Lattnerb694ba72006-07-02 22:41:36 +0000598 // If this is specified to be in a namespace, step down into it.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000599 if (!Namespace.empty()) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000600 // If there is already a pragma handler with the name of this namespace,
601 // we either have an error (directive with the same name as a namespace) or
602 // we already have the namespace to insert into.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000603 if (PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace)) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000604 InsertNS = Existing->getIfNamespace();
605 assert(InsertNS != 0 && "Cannot have a pragma namespace and pragma"
606 " handler with the same name!");
607 } else {
608 // Otherwise, this namespace doesn't exist yet, create and insert the
609 // handler for it.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000610 InsertNS = new PragmaNamespace(Namespace);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000611 PragmaHandlers->AddPragma(InsertNS);
612 }
613 }
Mike Stump11289f42009-09-09 15:08:12 +0000614
Chris Lattnerb694ba72006-07-02 22:41:36 +0000615 // Check to make sure we don't already have a pragma for this identifier.
616 assert(!InsertNS->FindHandler(Handler->getName()) &&
617 "Pragma handler already exists for this identifier!");
618 InsertNS->AddPragma(Handler);
619}
620
Daniel Dunbar40596532008-10-04 19:17:46 +0000621/// RemovePragmaHandler - Remove the specific pragma handler from the
622/// preprocessor. If \arg Namespace is non-null, then it should be the
623/// namespace that \arg Handler was added to. It is an error to remove
624/// a handler that has not been registered.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000625void Preprocessor::RemovePragmaHandler(llvm::StringRef Namespace,
Daniel Dunbar40596532008-10-04 19:17:46 +0000626 PragmaHandler *Handler) {
627 PragmaNamespace *NS = PragmaHandlers;
Mike Stump11289f42009-09-09 15:08:12 +0000628
Daniel Dunbar40596532008-10-04 19:17:46 +0000629 // If this is specified to be in a namespace, step down into it.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000630 if (!Namespace.empty()) {
631 PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace);
Daniel Dunbar40596532008-10-04 19:17:46 +0000632 assert(Existing && "Namespace containing handler does not exist!");
633
634 NS = Existing->getIfNamespace();
635 assert(NS && "Invalid namespace, registered as a regular pragma handler!");
636 }
637
638 NS->RemovePragmaHandler(Handler);
Mike Stump11289f42009-09-09 15:08:12 +0000639
Daniel Dunbar40596532008-10-04 19:17:46 +0000640 // If this is a non-default namespace and it is now empty, remove
641 // it.
642 if (NS != PragmaHandlers && NS->IsEmpty())
643 PragmaHandlers->RemovePragmaHandler(NS);
644}
645
Chris Lattnerb694ba72006-07-02 22:41:36 +0000646namespace {
Chris Lattnerc2383312007-12-19 19:38:36 +0000647/// PragmaOnceHandler - "#pragma once" marks the file as atomically included.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000648struct PragmaOnceHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000649 PragmaOnceHandler() : PragmaHandler("once") {}
Chris Lattner146762e2007-07-20 16:59:19 +0000650 virtual void HandlePragma(Preprocessor &PP, Token &OnceTok) {
Chris Lattnerce2ab6f2009-04-14 05:07:49 +0000651 PP.CheckEndOfDirective("pragma once");
Chris Lattnerb694ba72006-07-02 22:41:36 +0000652 PP.HandlePragmaOnce(OnceTok);
653 }
654};
655
Chris Lattnerc2383312007-12-19 19:38:36 +0000656/// PragmaMarkHandler - "#pragma mark ..." is ignored by the compiler, and the
657/// rest of the line is not lexed.
658struct PragmaMarkHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000659 PragmaMarkHandler() : PragmaHandler("mark") {}
Chris Lattnerc2383312007-12-19 19:38:36 +0000660 virtual void HandlePragma(Preprocessor &PP, Token &MarkTok) {
661 PP.HandlePragmaMark();
662 }
663};
664
665/// PragmaPoisonHandler - "#pragma poison x" marks x as not usable.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000666struct PragmaPoisonHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000667 PragmaPoisonHandler() : PragmaHandler("poison") {}
Chris Lattner146762e2007-07-20 16:59:19 +0000668 virtual void HandlePragma(Preprocessor &PP, Token &PoisonTok) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000669 PP.HandlePragmaPoison(PoisonTok);
670 }
671};
672
Chris Lattnerc2383312007-12-19 19:38:36 +0000673/// PragmaSystemHeaderHandler - "#pragma system_header" marks the current file
674/// as a system header, which silences warnings in it.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000675struct PragmaSystemHeaderHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000676 PragmaSystemHeaderHandler() : PragmaHandler("system_header") {}
Chris Lattner146762e2007-07-20 16:59:19 +0000677 virtual void HandlePragma(Preprocessor &PP, Token &SHToken) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000678 PP.HandlePragmaSystemHeader(SHToken);
Chris Lattnerce2ab6f2009-04-14 05:07:49 +0000679 PP.CheckEndOfDirective("pragma");
Chris Lattnerb694ba72006-07-02 22:41:36 +0000680 }
681};
682struct PragmaDependencyHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000683 PragmaDependencyHandler() : PragmaHandler("dependency") {}
Chris Lattner146762e2007-07-20 16:59:19 +0000684 virtual void HandlePragma(Preprocessor &PP, Token &DepToken) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000685 PP.HandlePragmaDependency(DepToken);
686 }
687};
Mike Stump11289f42009-09-09 15:08:12 +0000688
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000689struct PragmaDebugHandler : public PragmaHandler {
690 PragmaDebugHandler() : PragmaHandler("__debug") {}
691 virtual void HandlePragma(Preprocessor &PP, Token &DepToken) {
692 Token Tok;
693 PP.LexUnexpandedToken(Tok);
694 if (Tok.isNot(tok::identifier)) {
695 PP.Diag(Tok, diag::warn_pragma_diagnostic_clang_invalid);
696 return;
697 }
698 IdentifierInfo *II = Tok.getIdentifierInfo();
699
700 if (II->isStr("overflow_stack")) {
701 DebugOverflowStack();
702 } else if (II->isStr("crash")) {
703 DebugCrash();
704 }
705 }
706
707 void DebugOverflowStack() {
708 DebugOverflowStack();
709 }
710
711 void DebugCrash() {
Daniel Dunbar51738ba2010-07-29 02:46:02 +0000712 *(volatile int*) 0x11 = 0;
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000713 }
714};
715
Chris Lattner504af112009-04-19 23:16:58 +0000716/// PragmaDiagnosticHandler - e.g. '#pragma GCC diagnostic ignored "-Wformat"'
Chris Lattnerfb42a182009-07-12 21:18:45 +0000717/// Since clang's diagnostic supports extended functionality beyond GCC's
718/// the constructor takes a clangMode flag to tell it whether or not to allow
719/// clang's extended functionality, or whether to reject it.
Chris Lattner504af112009-04-19 23:16:58 +0000720struct PragmaDiagnosticHandler : public PragmaHandler {
Chris Lattnerfb42a182009-07-12 21:18:45 +0000721private:
722 const bool ClangMode;
723public:
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000724 explicit PragmaDiagnosticHandler(const bool clangMode)
725 : PragmaHandler("diagnostic"), ClangMode(clangMode) {}
726
Chris Lattner504af112009-04-19 23:16:58 +0000727 virtual void HandlePragma(Preprocessor &PP, Token &DiagToken) {
728 Token Tok;
729 PP.LexUnexpandedToken(Tok);
730 if (Tok.isNot(tok::identifier)) {
Chris Lattnerfb42a182009-07-12 21:18:45 +0000731 unsigned Diag = ClangMode ? diag::warn_pragma_diagnostic_clang_invalid
732 : diag::warn_pragma_diagnostic_gcc_invalid;
733 PP.Diag(Tok, Diag);
Chris Lattner504af112009-04-19 23:16:58 +0000734 return;
735 }
736 IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000737
Chris Lattner504af112009-04-19 23:16:58 +0000738 diag::Mapping Map;
739 if (II->isStr("warning"))
740 Map = diag::MAP_WARNING;
741 else if (II->isStr("error"))
742 Map = diag::MAP_ERROR;
743 else if (II->isStr("ignored"))
744 Map = diag::MAP_IGNORE;
745 else if (II->isStr("fatal"))
746 Map = diag::MAP_FATAL;
Chris Lattnerfb42a182009-07-12 21:18:45 +0000747 else if (ClangMode) {
748 if (II->isStr("pop")) {
Mike Stump11289f42009-09-09 15:08:12 +0000749 if (!PP.getDiagnostics().popMappings())
Chris Lattnerfb42a182009-07-12 21:18:45 +0000750 PP.Diag(Tok, diag::warn_pragma_diagnostic_clang_cannot_ppp);
751 return;
752 }
753
754 if (II->isStr("push")) {
755 PP.getDiagnostics().pushMappings();
Mike Stump11289f42009-09-09 15:08:12 +0000756 return;
Chris Lattnerfb42a182009-07-12 21:18:45 +0000757 }
758
759 PP.Diag(Tok, diag::warn_pragma_diagnostic_clang_invalid);
760 return;
761 } else {
762 PP.Diag(Tok, diag::warn_pragma_diagnostic_gcc_invalid);
Chris Lattner504af112009-04-19 23:16:58 +0000763 return;
764 }
Mike Stump11289f42009-09-09 15:08:12 +0000765
Chris Lattner504af112009-04-19 23:16:58 +0000766 PP.LexUnexpandedToken(Tok);
767
768 // We need at least one string.
769 if (Tok.isNot(tok::string_literal)) {
770 PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token);
771 return;
772 }
Mike Stump11289f42009-09-09 15:08:12 +0000773
Chris Lattner504af112009-04-19 23:16:58 +0000774 // String concatenation allows multiple strings, which can even come from
775 // macro expansion.
776 // "foo " "bar" "Baz"
777 llvm::SmallVector<Token, 4> StrToks;
778 while (Tok.is(tok::string_literal)) {
779 StrToks.push_back(Tok);
780 PP.LexUnexpandedToken(Tok);
781 }
Mike Stump11289f42009-09-09 15:08:12 +0000782
Chris Lattner504af112009-04-19 23:16:58 +0000783 if (Tok.isNot(tok::eom)) {
784 PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token);
785 return;
786 }
Mike Stump11289f42009-09-09 15:08:12 +0000787
Chris Lattner504af112009-04-19 23:16:58 +0000788 // Concatenate and parse the strings.
789 StringLiteralParser Literal(&StrToks[0], StrToks.size(), PP);
790 assert(!Literal.AnyWide && "Didn't allow wide strings in");
791 if (Literal.hadError)
792 return;
793 if (Literal.Pascal) {
Chris Lattnerfb42a182009-07-12 21:18:45 +0000794 unsigned Diag = ClangMode ? diag::warn_pragma_diagnostic_clang_invalid
795 : diag::warn_pragma_diagnostic_gcc_invalid;
796 PP.Diag(Tok, Diag);
Chris Lattner504af112009-04-19 23:16:58 +0000797 return;
798 }
Chris Lattnerfb42a182009-07-12 21:18:45 +0000799
Chris Lattner504af112009-04-19 23:16:58 +0000800 std::string WarningName(Literal.GetString(),
801 Literal.GetString()+Literal.GetStringLength());
802
803 if (WarningName.size() < 3 || WarningName[0] != '-' ||
804 WarningName[1] != 'W') {
805 PP.Diag(StrToks[0].getLocation(),
806 diag::warn_pragma_diagnostic_invalid_option);
807 return;
808 }
Mike Stump11289f42009-09-09 15:08:12 +0000809
Chris Lattner504af112009-04-19 23:16:58 +0000810 if (PP.getDiagnostics().setDiagnosticGroupMapping(WarningName.c_str()+2,
811 Map))
812 PP.Diag(StrToks[0].getLocation(),
813 diag::warn_pragma_diagnostic_unknown_warning) << WarningName;
814 }
815};
Mike Stump11289f42009-09-09 15:08:12 +0000816
Chris Lattner2ff698d2009-01-16 08:21:25 +0000817/// PragmaCommentHandler - "#pragma comment ...".
818struct PragmaCommentHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000819 PragmaCommentHandler() : PragmaHandler("comment") {}
Chris Lattner2ff698d2009-01-16 08:21:25 +0000820 virtual void HandlePragma(Preprocessor &PP, Token &CommentTok) {
821 PP.HandlePragmaComment(CommentTok);
822 }
823};
Mike Stump11289f42009-09-09 15:08:12 +0000824
Chris Lattner30c924b2010-06-26 17:11:39 +0000825/// PragmaMessageHandler - "#pragma message("...")".
826struct PragmaMessageHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000827 PragmaMessageHandler() : PragmaHandler("message") {}
Chris Lattner30c924b2010-06-26 17:11:39 +0000828 virtual void HandlePragma(Preprocessor &PP, Token &CommentTok) {
829 PP.HandlePragmaMessage(CommentTok);
830 }
831};
832
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000833/// PragmaPushMacroHandler - "#pragma push_macro" saves the value of the
834/// macro on the top of the stack.
835struct PragmaPushMacroHandler : public PragmaHandler {
836 PragmaPushMacroHandler() : PragmaHandler("push_macro") {}
837 virtual void HandlePragma(Preprocessor &PP, Token &PushMacroTok) {
838 PP.HandlePragmaPushMacro(PushMacroTok);
839 }
840};
841
842
843/// PragmaPopMacroHandler - "#pragma pop_macro" sets the value of the
844/// macro to the value on the top of the stack.
845struct PragmaPopMacroHandler : public PragmaHandler {
846 PragmaPopMacroHandler() : PragmaHandler("pop_macro") {}
847 virtual void HandlePragma(Preprocessor &PP, Token &PopMacroTok) {
848 PP.HandlePragmaPopMacro(PopMacroTok);
849 }
850};
851
Chris Lattner958ee042009-04-19 21:20:35 +0000852// Pragma STDC implementations.
Chris Lattner02ef4e32009-04-19 21:50:08 +0000853
854enum STDCSetting {
855 STDC_ON, STDC_OFF, STDC_DEFAULT, STDC_INVALID
856};
Mike Stump11289f42009-09-09 15:08:12 +0000857
Chris Lattner02ef4e32009-04-19 21:50:08 +0000858static STDCSetting LexOnOffSwitch(Preprocessor &PP) {
859 Token Tok;
860 PP.LexUnexpandedToken(Tok);
861
862 if (Tok.isNot(tok::identifier)) {
863 PP.Diag(Tok, diag::ext_stdc_pragma_syntax);
864 return STDC_INVALID;
865 }
866 IdentifierInfo *II = Tok.getIdentifierInfo();
867 STDCSetting Result;
868 if (II->isStr("ON"))
869 Result = STDC_ON;
870 else if (II->isStr("OFF"))
871 Result = STDC_OFF;
872 else if (II->isStr("DEFAULT"))
873 Result = STDC_DEFAULT;
874 else {
875 PP.Diag(Tok, diag::ext_stdc_pragma_syntax);
876 return STDC_INVALID;
877 }
878
879 // Verify that this is followed by EOM.
880 PP.LexUnexpandedToken(Tok);
881 if (Tok.isNot(tok::eom))
882 PP.Diag(Tok, diag::ext_stdc_pragma_syntax_eom);
883 return Result;
884}
Mike Stump11289f42009-09-09 15:08:12 +0000885
Chris Lattner958ee042009-04-19 21:20:35 +0000886/// PragmaSTDC_FP_CONTRACTHandler - "#pragma STDC FP_CONTRACT ...".
887struct PragmaSTDC_FP_CONTRACTHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000888 PragmaSTDC_FP_CONTRACTHandler() : PragmaHandler("FP_CONTRACT") {}
Chris Lattnera0b1f762009-04-19 21:25:37 +0000889 virtual void HandlePragma(Preprocessor &PP, Token &Tok) {
Chris Lattner02ef4e32009-04-19 21:50:08 +0000890 // We just ignore the setting of FP_CONTRACT. Since we don't do contractions
891 // at all, our default is OFF and setting it to ON is an optimization hint
892 // we can safely ignore. When we support -ffma or something, we would need
893 // to diagnose that we are ignoring FMA.
894 LexOnOffSwitch(PP);
Chris Lattner958ee042009-04-19 21:20:35 +0000895 }
896};
Mike Stump11289f42009-09-09 15:08:12 +0000897
Chris Lattner958ee042009-04-19 21:20:35 +0000898/// PragmaSTDC_FENV_ACCESSHandler - "#pragma STDC FENV_ACCESS ...".
899struct PragmaSTDC_FENV_ACCESSHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000900 PragmaSTDC_FENV_ACCESSHandler() : PragmaHandler("FENV_ACCESS") {}
Chris Lattnera0b1f762009-04-19 21:25:37 +0000901 virtual void HandlePragma(Preprocessor &PP, Token &Tok) {
Chris Lattnerdf222682009-04-19 21:55:32 +0000902 if (LexOnOffSwitch(PP) == STDC_ON)
903 PP.Diag(Tok, diag::warn_stdc_fenv_access_not_supported);
Chris Lattner958ee042009-04-19 21:20:35 +0000904 }
905};
Mike Stump11289f42009-09-09 15:08:12 +0000906
Chris Lattner958ee042009-04-19 21:20:35 +0000907/// PragmaSTDC_CX_LIMITED_RANGEHandler - "#pragma STDC CX_LIMITED_RANGE ...".
908struct PragmaSTDC_CX_LIMITED_RANGEHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000909 PragmaSTDC_CX_LIMITED_RANGEHandler()
910 : PragmaHandler("CX_LIMITED_RANGE") {}
Chris Lattnera0b1f762009-04-19 21:25:37 +0000911 virtual void HandlePragma(Preprocessor &PP, Token &Tok) {
Chris Lattner02ef4e32009-04-19 21:50:08 +0000912 LexOnOffSwitch(PP);
Chris Lattner958ee042009-04-19 21:20:35 +0000913 }
914};
Mike Stump11289f42009-09-09 15:08:12 +0000915
Chris Lattner958ee042009-04-19 21:20:35 +0000916/// PragmaSTDC_UnknownHandler - "#pragma STDC ...".
917struct PragmaSTDC_UnknownHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000918 PragmaSTDC_UnknownHandler() {}
Chris Lattnera0b1f762009-04-19 21:25:37 +0000919 virtual void HandlePragma(Preprocessor &PP, Token &UnknownTok) {
Chris Lattner02ef4e32009-04-19 21:50:08 +0000920 // C99 6.10.6p2, unknown forms are not allowed.
Chris Lattnera0b1f762009-04-19 21:25:37 +0000921 PP.Diag(UnknownTok, diag::ext_stdc_pragma_ignored);
Chris Lattner958ee042009-04-19 21:20:35 +0000922 }
923};
Mike Stump11289f42009-09-09 15:08:12 +0000924
Chris Lattnerb694ba72006-07-02 22:41:36 +0000925} // end anonymous namespace
926
927
928/// RegisterBuiltinPragmas - Install the standard preprocessor pragmas:
929/// #pragma GCC poison/system_header/dependency and #pragma once.
930void Preprocessor::RegisterBuiltinPragmas() {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000931 AddPragmaHandler(new PragmaOnceHandler());
932 AddPragmaHandler(new PragmaMarkHandler());
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000933 AddPragmaHandler(new PragmaPushMacroHandler());
934 AddPragmaHandler(new PragmaPopMacroHandler());
Mike Stump11289f42009-09-09 15:08:12 +0000935
Chris Lattnerb61448d2009-05-12 18:21:11 +0000936 // #pragma GCC ...
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000937 AddPragmaHandler("GCC", new PragmaPoisonHandler());
938 AddPragmaHandler("GCC", new PragmaSystemHeaderHandler());
939 AddPragmaHandler("GCC", new PragmaDependencyHandler());
940 AddPragmaHandler("GCC", new PragmaDiagnosticHandler(false));
Chris Lattnerb61448d2009-05-12 18:21:11 +0000941 // #pragma clang ...
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000942 AddPragmaHandler("clang", new PragmaPoisonHandler());
943 AddPragmaHandler("clang", new PragmaSystemHeaderHandler());
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000944 AddPragmaHandler("clang", new PragmaDebugHandler());
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000945 AddPragmaHandler("clang", new PragmaDependencyHandler());
946 AddPragmaHandler("clang", new PragmaDiagnosticHandler(true));
Chris Lattnerb61448d2009-05-12 18:21:11 +0000947
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000948 AddPragmaHandler("STDC", new PragmaSTDC_FP_CONTRACTHandler());
949 AddPragmaHandler("STDC", new PragmaSTDC_FENV_ACCESSHandler());
950 AddPragmaHandler("STDC", new PragmaSTDC_CX_LIMITED_RANGEHandler());
Chris Lattner958ee042009-04-19 21:20:35 +0000951 AddPragmaHandler("STDC", new PragmaSTDC_UnknownHandler());
Mike Stump11289f42009-09-09 15:08:12 +0000952
Chris Lattner2ff698d2009-01-16 08:21:25 +0000953 // MS extensions.
Chris Lattner30c924b2010-06-26 17:11:39 +0000954 if (Features.Microsoft) {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000955 AddPragmaHandler(new PragmaCommentHandler());
956 AddPragmaHandler(new PragmaMessageHandler());
Chris Lattner30c924b2010-06-26 17:11:39 +0000957 }
Chris Lattnerb694ba72006-07-02 22:41:36 +0000958}