blob: f0f3bce008d247ea3d44c46d1a217cec18503a5f [file] [log] [blame]
Chris Lattnerb8761832006-06-24 21:31:03 +00001//===--- Pragma.cpp - Pragma registration and handling --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerb8761832006-06-24 21:31:03 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattnerb694ba72006-07-02 22:41:36 +000010// This file implements the PragmaHandler/PragmaTable interfaces and implements
11// pragma related methods of the Preprocessor class.
Chris Lattnerb8761832006-06-24 21:31:03 +000012//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Lex/Pragma.h"
Chris Lattner07b019a2006-10-22 07:28:56 +000016#include "clang/Lex/HeaderSearch.h"
Chris Lattner262d4e32009-01-16 18:59:23 +000017#include "clang/Lex/LiteralSupport.h"
Chris Lattnerb8761832006-06-24 21:31:03 +000018#include "clang/Lex/Preprocessor.h"
Chris Lattnerc0a585d2010-08-17 15:55:45 +000019#include "clang/Lex/MacroInfo.h"
Chris Lattner60f36222009-01-29 05:15:15 +000020#include "clang/Lex/LexDiagnostic.h"
Chris Lattnerb694ba72006-07-02 22:41:36 +000021#include "clang/Basic/FileManager.h"
22#include "clang/Basic/SourceManager.h"
Daniel Dunbar211a7872010-08-18 23:09:23 +000023#include "llvm/Support/CrashRecoveryContext.h"
Daniel Dunbarf2cf3292010-08-17 22:32:48 +000024#include "llvm/Support/ErrorHandling.h"
Douglas Gregorc6d5edd2009-07-02 17:08:52 +000025#include <algorithm>
Chris Lattnerb8761832006-06-24 21:31:03 +000026using namespace clang;
27
28// Out-of-line destructor to provide a home for the class.
29PragmaHandler::~PragmaHandler() {
30}
31
Chris Lattner2e155302006-07-03 05:34:41 +000032//===----------------------------------------------------------------------===//
Daniel Dunbard839e772010-06-11 20:10:12 +000033// EmptyPragmaHandler Implementation.
34//===----------------------------------------------------------------------===//
35
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000036EmptyPragmaHandler::EmptyPragmaHandler() {}
Daniel Dunbard839e772010-06-11 20:10:12 +000037
38void EmptyPragmaHandler::HandlePragma(Preprocessor &PP, Token &FirstToken) {}
39
40//===----------------------------------------------------------------------===//
Chris Lattner2e155302006-07-03 05:34:41 +000041// PragmaNamespace Implementation.
42//===----------------------------------------------------------------------===//
43
44
45PragmaNamespace::~PragmaNamespace() {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000046 for (llvm::StringMap<PragmaHandler*>::iterator
47 I = Handlers.begin(), E = Handlers.end(); I != E; ++I)
48 delete I->second;
Chris Lattner2e155302006-07-03 05:34:41 +000049}
50
51/// FindHandler - Check to see if there is already a handler for the
52/// specified name. If not, return the handler for the null identifier if it
53/// exists, otherwise return null. If IgnoreNull is true (the default) then
54/// the null handler isn't returned on failure to match.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000055PragmaHandler *PragmaNamespace::FindHandler(llvm::StringRef Name,
Chris Lattner2e155302006-07-03 05:34:41 +000056 bool IgnoreNull) const {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000057 if (PragmaHandler *Handler = Handlers.lookup(Name))
58 return Handler;
59 return IgnoreNull ? 0 : Handlers.lookup(llvm::StringRef());
60}
Mike Stump11289f42009-09-09 15:08:12 +000061
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000062void PragmaNamespace::AddPragma(PragmaHandler *Handler) {
63 assert(!Handlers.lookup(Handler->getName()) &&
64 "A handler with this name is already registered in this namespace");
65 llvm::StringMapEntry<PragmaHandler *> &Entry =
66 Handlers.GetOrCreateValue(Handler->getName());
67 Entry.setValue(Handler);
Chris Lattner2e155302006-07-03 05:34:41 +000068}
69
Daniel Dunbar40596532008-10-04 19:17:46 +000070void PragmaNamespace::RemovePragmaHandler(PragmaHandler *Handler) {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000071 assert(Handlers.lookup(Handler->getName()) &&
72 "Handler not registered in this namespace");
73 Handlers.erase(Handler->getName());
Daniel Dunbar40596532008-10-04 19:17:46 +000074}
75
Chris Lattner146762e2007-07-20 16:59:19 +000076void PragmaNamespace::HandlePragma(Preprocessor &PP, Token &Tok) {
Chris Lattnerb8761832006-06-24 21:31:03 +000077 // Read the 'namespace' that the directive is in, e.g. STDC. Do not macro
78 // expand it, the user can have a STDC #define, that should not affect this.
79 PP.LexUnexpandedToken(Tok);
Mike Stump11289f42009-09-09 15:08:12 +000080
Chris Lattnerb8761832006-06-24 21:31:03 +000081 // Get the handler for this token. If there is no handler, ignore the pragma.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000082 PragmaHandler *Handler
83 = FindHandler(Tok.getIdentifierInfo() ? Tok.getIdentifierInfo()->getName()
84 : llvm::StringRef(),
85 /*IgnoreNull=*/false);
Chris Lattner21656f22009-04-19 21:10:26 +000086 if (Handler == 0) {
87 PP.Diag(Tok, diag::warn_pragma_ignored);
88 return;
89 }
Mike Stump11289f42009-09-09 15:08:12 +000090
Chris Lattnerb8761832006-06-24 21:31:03 +000091 // Otherwise, pass it down.
92 Handler->HandlePragma(PP, Tok);
93}
Chris Lattnerb694ba72006-07-02 22:41:36 +000094
Chris Lattnerb694ba72006-07-02 22:41:36 +000095//===----------------------------------------------------------------------===//
96// Preprocessor Pragma Directive Handling.
97//===----------------------------------------------------------------------===//
98
99/// HandlePragmaDirective - The "#pragma" directive has been parsed. Lex the
100/// rest of the pragma, passing it to the registered pragma handlers.
101void Preprocessor::HandlePragmaDirective() {
102 ++NumPragma;
Mike Stump11289f42009-09-09 15:08:12 +0000103
Chris Lattnerb694ba72006-07-02 22:41:36 +0000104 // Invoke the first level of pragma handlers which reads the namespace id.
Chris Lattner146762e2007-07-20 16:59:19 +0000105 Token Tok;
Chris Lattnerb694ba72006-07-02 22:41:36 +0000106 PragmaHandlers->HandlePragma(*this, Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000107
Chris Lattnerb694ba72006-07-02 22:41:36 +0000108 // If the pragma handler didn't read the rest of the line, consume it now.
Chris Lattnere7e65942009-06-18 05:55:53 +0000109 if (CurPPLexer && CurPPLexer->ParsingPreprocessorDirective)
Chris Lattnerb694ba72006-07-02 22:41:36 +0000110 DiscardUntilEndOfDirective();
111}
112
113/// Handle_Pragma - Read a _Pragma directive, slice it up, process it, then
114/// return the first token after the directive. The _Pragma token has just
115/// been read into 'Tok'.
Chris Lattner146762e2007-07-20 16:59:19 +0000116void Preprocessor::Handle_Pragma(Token &Tok) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000117 // Remember the pragma token location.
118 SourceLocation PragmaLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000119
Chris Lattnerb694ba72006-07-02 22:41:36 +0000120 // Read the '('.
121 Lex(Tok);
Chris Lattner907dfe92008-11-18 07:59:24 +0000122 if (Tok.isNot(tok::l_paren)) {
123 Diag(PragmaLoc, diag::err__Pragma_malformed);
124 return;
125 }
Chris Lattnerb694ba72006-07-02 22:41:36 +0000126
127 // Read the '"..."'.
128 Lex(Tok);
Chris Lattner907dfe92008-11-18 07:59:24 +0000129 if (Tok.isNot(tok::string_literal) && Tok.isNot(tok::wide_string_literal)) {
130 Diag(PragmaLoc, diag::err__Pragma_malformed);
131 return;
132 }
Mike Stump11289f42009-09-09 15:08:12 +0000133
Chris Lattnerb694ba72006-07-02 22:41:36 +0000134 // Remember the string.
135 std::string StrVal = getSpelling(Tok);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000136
137 // Read the ')'.
138 Lex(Tok);
Chris Lattner907dfe92008-11-18 07:59:24 +0000139 if (Tok.isNot(tok::r_paren)) {
140 Diag(PragmaLoc, diag::err__Pragma_malformed);
141 return;
142 }
Mike Stump11289f42009-09-09 15:08:12 +0000143
Chris Lattner9dc9c202009-02-15 20:52:18 +0000144 SourceLocation RParenLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000145
Chris Lattner262d4e32009-01-16 18:59:23 +0000146 // The _Pragma is lexically sound. Destringize according to C99 6.10.9.1:
147 // "The string literal is destringized by deleting the L prefix, if present,
148 // deleting the leading and trailing double-quotes, replacing each escape
149 // sequence \" by a double-quote, and replacing each escape sequence \\ by a
150 // single backslash."
Chris Lattnerb694ba72006-07-02 22:41:36 +0000151 if (StrVal[0] == 'L') // Remove L prefix.
152 StrVal.erase(StrVal.begin());
153 assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
154 "Invalid string token!");
Mike Stump11289f42009-09-09 15:08:12 +0000155
Chris Lattnerb694ba72006-07-02 22:41:36 +0000156 // Remove the front quote, replacing it with a space, so that the pragma
157 // contents appear to have a space before them.
158 StrVal[0] = ' ';
Mike Stump11289f42009-09-09 15:08:12 +0000159
Chris Lattnerfa217bd2009-03-08 08:08:45 +0000160 // Replace the terminating quote with a \n.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000161 StrVal[StrVal.size()-1] = '\n';
Mike Stump11289f42009-09-09 15:08:12 +0000162
Chris Lattnerb694ba72006-07-02 22:41:36 +0000163 // Remove escaped quotes and escapes.
164 for (unsigned i = 0, e = StrVal.size(); i != e-1; ++i) {
165 if (StrVal[i] == '\\' &&
166 (StrVal[i+1] == '\\' || StrVal[i+1] == '"')) {
167 // \\ -> '\' and \" -> '"'.
168 StrVal.erase(StrVal.begin()+i);
169 --e;
170 }
171 }
Mike Stump11289f42009-09-09 15:08:12 +0000172
Chris Lattnerf918bf72006-07-19 05:01:18 +0000173 // Plop the string (including the newline and trailing null) into a buffer
174 // where we can lex it.
Chris Lattner5a7971e2009-01-26 19:29:26 +0000175 Token TmpTok;
176 TmpTok.startToken();
177 CreateString(&StrVal[0], StrVal.size(), TmpTok);
178 SourceLocation TokLoc = TmpTok.getLocation();
Chris Lattnerb694ba72006-07-02 22:41:36 +0000179
Chris Lattnerb694ba72006-07-02 22:41:36 +0000180 // Make and enter a lexer object so that we lex and expand the tokens just
181 // like any others.
Chris Lattner9dc9c202009-02-15 20:52:18 +0000182 Lexer *TL = Lexer::Create_PragmaLexer(TokLoc, PragmaLoc, RParenLoc,
Chris Lattnerfa217bd2009-03-08 08:08:45 +0000183 StrVal.size(), *this);
Chris Lattner98a53122006-07-02 23:00:20 +0000184
185 EnterSourceFileWithLexer(TL, 0);
186
Chris Lattnerb694ba72006-07-02 22:41:36 +0000187 // With everything set up, lex this as a #pragma directive.
188 HandlePragmaDirective();
Mike Stump11289f42009-09-09 15:08:12 +0000189
Chris Lattnerb694ba72006-07-02 22:41:36 +0000190 // Finally, return whatever came after the pragma directive.
191 return Lex(Tok);
192}
193
194
195
196/// HandlePragmaOnce - Handle #pragma once. OnceTok is the 'once'.
197///
Chris Lattner146762e2007-07-20 16:59:19 +0000198void Preprocessor::HandlePragmaOnce(Token &OnceTok) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000199 if (isInPrimaryFile()) {
200 Diag(OnceTok, diag::pp_pragma_once_in_main_file);
201 return;
202 }
Mike Stump11289f42009-09-09 15:08:12 +0000203
Chris Lattnerb694ba72006-07-02 22:41:36 +0000204 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000205 // Mark the file as a once-only file now.
Chris Lattnerd32480d2009-01-17 06:22:33 +0000206 HeaderInfo.MarkFileIncludeOnce(getCurrentFileLexer()->getFileEntry());
Chris Lattnerb694ba72006-07-02 22:41:36 +0000207}
208
Chris Lattnerc2383312007-12-19 19:38:36 +0000209void Preprocessor::HandlePragmaMark() {
Ted Kremenek76c34412008-11-19 22:21:33 +0000210 assert(CurPPLexer && "No current lexer?");
Chris Lattnerd9efb6e2009-06-15 05:02:34 +0000211 if (CurLexer)
212 CurLexer->ReadToEndOfLine();
213 else
214 CurPTHLexer->DiscardToEndOfLine();
Chris Lattnerc2383312007-12-19 19:38:36 +0000215}
216
217
Chris Lattnerb694ba72006-07-02 22:41:36 +0000218/// HandlePragmaPoison - Handle #pragma GCC poison. PoisonTok is the 'poison'.
219///
Chris Lattner146762e2007-07-20 16:59:19 +0000220void Preprocessor::HandlePragmaPoison(Token &PoisonTok) {
221 Token Tok;
Chris Lattner538d7f32006-07-20 04:31:52 +0000222
Chris Lattnerb694ba72006-07-02 22:41:36 +0000223 while (1) {
224 // Read the next token to poison. While doing this, pretend that we are
225 // skipping while reading the identifier to poison.
226 // This avoids errors on code like:
227 // #pragma GCC poison X
228 // #pragma GCC poison X
Ted Kremenek551c82a2008-11-18 01:12:54 +0000229 if (CurPPLexer) CurPPLexer->LexingRawMode = true;
Chris Lattnerb694ba72006-07-02 22:41:36 +0000230 LexUnexpandedToken(Tok);
Ted Kremenek551c82a2008-11-18 01:12:54 +0000231 if (CurPPLexer) CurPPLexer->LexingRawMode = false;
Mike Stump11289f42009-09-09 15:08:12 +0000232
Chris Lattnerb694ba72006-07-02 22:41:36 +0000233 // If we reached the end of line, we're done.
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000234 if (Tok.is(tok::eom)) return;
Mike Stump11289f42009-09-09 15:08:12 +0000235
Chris Lattnerb694ba72006-07-02 22:41:36 +0000236 // Can only poison identifiers.
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000237 if (Tok.isNot(tok::identifier)) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000238 Diag(Tok, diag::err_pp_invalid_poison);
239 return;
240 }
Mike Stump11289f42009-09-09 15:08:12 +0000241
Chris Lattnercefc7682006-07-08 08:28:12 +0000242 // Look up the identifier info for the token. We disabled identifier lookup
243 // by saying we're skipping contents, so we need to do this manually.
244 IdentifierInfo *II = LookUpIdentifierInfo(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000245
Chris Lattnerb694ba72006-07-02 22:41:36 +0000246 // Already poisoned.
247 if (II->isPoisoned()) continue;
Mike Stump11289f42009-09-09 15:08:12 +0000248
Chris Lattnerb694ba72006-07-02 22:41:36 +0000249 // If this is a macro identifier, emit a warning.
Chris Lattner259716a2007-10-07 08:04:56 +0000250 if (II->hasMacroDefinition())
Chris Lattnerb694ba72006-07-02 22:41:36 +0000251 Diag(Tok, diag::pp_poisoning_existing_macro);
Mike Stump11289f42009-09-09 15:08:12 +0000252
Chris Lattnerb694ba72006-07-02 22:41:36 +0000253 // Finally, poison it!
254 II->setIsPoisoned();
255 }
256}
257
258/// HandlePragmaSystemHeader - Implement #pragma GCC system_header. We know
259/// that the whole directive has been parsed.
Chris Lattner146762e2007-07-20 16:59:19 +0000260void Preprocessor::HandlePragmaSystemHeader(Token &SysHeaderTok) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000261 if (isInPrimaryFile()) {
262 Diag(SysHeaderTok, diag::pp_pragma_sysheader_in_main_file);
263 return;
264 }
Mike Stump11289f42009-09-09 15:08:12 +0000265
Chris Lattnerb694ba72006-07-02 22:41:36 +0000266 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
Ted Kremenek300590b2008-11-20 01:45:11 +0000267 PreprocessorLexer *TheLexer = getCurrentFileLexer();
Mike Stump11289f42009-09-09 15:08:12 +0000268
Chris Lattnerb694ba72006-07-02 22:41:36 +0000269 // Mark the file as a system header.
Chris Lattnerd32480d2009-01-17 06:22:33 +0000270 HeaderInfo.MarkFileSystemHeader(TheLexer->getFileEntry());
Mike Stump11289f42009-09-09 15:08:12 +0000271
272
Chris Lattnerd9efb6e2009-06-15 05:02:34 +0000273 PresumedLoc PLoc = SourceMgr.getPresumedLoc(SysHeaderTok.getLocation());
274 unsigned FilenameLen = strlen(PLoc.getFilename());
275 unsigned FilenameID = SourceMgr.getLineTableFilenameID(PLoc.getFilename(),
276 FilenameLen);
Mike Stump11289f42009-09-09 15:08:12 +0000277
Chris Lattnerd9efb6e2009-06-15 05:02:34 +0000278 // Emit a line marker. This will change any source locations from this point
279 // forward to realize they are in a system header.
280 // Create a line note with this information.
281 SourceMgr.AddLineNote(SysHeaderTok.getLocation(), PLoc.getLine(), FilenameID,
282 false, false, true, false);
Mike Stump11289f42009-09-09 15:08:12 +0000283
Chris Lattnerb694ba72006-07-02 22:41:36 +0000284 // Notify the client, if desired, that we are in a new source file.
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000285 if (Callbacks)
Ted Kremenek300590b2008-11-20 01:45:11 +0000286 Callbacks->FileChanged(SysHeaderTok.getLocation(),
Chris Lattnerb03dc762008-09-26 21:18:42 +0000287 PPCallbacks::SystemHeaderPragma, SrcMgr::C_System);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000288}
289
290/// HandlePragmaDependency - Handle #pragma GCC dependency "foo" blah.
291///
Chris Lattner146762e2007-07-20 16:59:19 +0000292void Preprocessor::HandlePragmaDependency(Token &DependencyTok) {
293 Token FilenameTok;
Ted Kremenek551c82a2008-11-18 01:12:54 +0000294 CurPPLexer->LexIncludeFilename(FilenameTok);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000295
296 // If the token kind is EOM, the error has already been diagnosed.
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000297 if (FilenameTok.is(tok::eom))
Chris Lattnerb694ba72006-07-02 22:41:36 +0000298 return;
Mike Stump11289f42009-09-09 15:08:12 +0000299
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000300 // Reserve a buffer to get the spelling.
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000301 llvm::SmallString<128> FilenameBuffer;
Douglas Gregordc970f02010-03-16 22:30:13 +0000302 bool Invalid = false;
303 llvm::StringRef Filename = getSpelling(FilenameTok, FilenameBuffer, &Invalid);
304 if (Invalid)
305 return;
Mike Stump11289f42009-09-09 15:08:12 +0000306
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000307 bool isAngled =
308 GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000309 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
310 // error.
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000311 if (Filename.empty())
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000312 return;
Mike Stump11289f42009-09-09 15:08:12 +0000313
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000314 // Search include directories for this file.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000315 const DirectoryLookup *CurDir;
Chris Lattnerfde85352010-01-22 00:14:44 +0000316 const FileEntry *File = LookupFile(Filename, isAngled, 0, CurDir);
Chris Lattner97b8e842008-11-18 08:02:48 +0000317 if (File == 0) {
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000318 Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
Chris Lattner97b8e842008-11-18 08:02:48 +0000319 return;
320 }
Mike Stump11289f42009-09-09 15:08:12 +0000321
Chris Lattnerd32480d2009-01-17 06:22:33 +0000322 const FileEntry *CurFile = getCurrentFileLexer()->getFileEntry();
Chris Lattnerb694ba72006-07-02 22:41:36 +0000323
324 // If this file is older than the file it depends on, emit a diagnostic.
325 if (CurFile && CurFile->getModificationTime() < File->getModificationTime()) {
326 // Lex tokens at the end of the message and include them in the message.
327 std::string Message;
328 Lex(DependencyTok);
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000329 while (DependencyTok.isNot(tok::eom)) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000330 Message += getSpelling(DependencyTok) + " ";
331 Lex(DependencyTok);
332 }
Mike Stump11289f42009-09-09 15:08:12 +0000333
Chris Lattnerb694ba72006-07-02 22:41:36 +0000334 Message.erase(Message.end()-1);
Chris Lattner97b8e842008-11-18 08:02:48 +0000335 Diag(FilenameTok, diag::pp_out_of_date_dependency) << Message;
Chris Lattnerb694ba72006-07-02 22:41:36 +0000336 }
337}
338
Chris Lattner2ff698d2009-01-16 08:21:25 +0000339/// HandlePragmaComment - Handle the microsoft #pragma comment extension. The
340/// syntax is:
341/// #pragma comment(linker, "foo")
342/// 'linker' is one of five identifiers: compiler, exestr, lib, linker, user.
343/// "foo" is a string, which is fully macro expanded, and permits string
Gabor Greif31a082f2009-03-17 11:39:38 +0000344/// concatenation, embedded escape characters etc. See MSDN for more details.
Chris Lattner2ff698d2009-01-16 08:21:25 +0000345void Preprocessor::HandlePragmaComment(Token &Tok) {
346 SourceLocation CommentLoc = Tok.getLocation();
347 Lex(Tok);
348 if (Tok.isNot(tok::l_paren)) {
349 Diag(CommentLoc, diag::err_pragma_comment_malformed);
350 return;
351 }
Mike Stump11289f42009-09-09 15:08:12 +0000352
Chris Lattner2ff698d2009-01-16 08:21:25 +0000353 // Read the identifier.
354 Lex(Tok);
355 if (Tok.isNot(tok::identifier)) {
356 Diag(CommentLoc, diag::err_pragma_comment_malformed);
357 return;
358 }
Mike Stump11289f42009-09-09 15:08:12 +0000359
Chris Lattner2ff698d2009-01-16 08:21:25 +0000360 // Verify that this is one of the 5 whitelisted options.
361 // FIXME: warn that 'exestr' is deprecated.
362 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000363 if (!II->isStr("compiler") && !II->isStr("exestr") && !II->isStr("lib") &&
Chris Lattner2ff698d2009-01-16 08:21:25 +0000364 !II->isStr("linker") && !II->isStr("user")) {
365 Diag(Tok.getLocation(), diag::err_pragma_comment_unknown_kind);
366 return;
367 }
Mike Stump11289f42009-09-09 15:08:12 +0000368
Chris Lattner262d4e32009-01-16 18:59:23 +0000369 // Read the optional string if present.
Chris Lattner2ff698d2009-01-16 08:21:25 +0000370 Lex(Tok);
Chris Lattner262d4e32009-01-16 18:59:23 +0000371 std::string ArgumentString;
Chris Lattner2ff698d2009-01-16 08:21:25 +0000372 if (Tok.is(tok::comma)) {
Chris Lattner262d4e32009-01-16 18:59:23 +0000373 Lex(Tok); // eat the comma.
Chris Lattner2ff698d2009-01-16 08:21:25 +0000374
375 // We need at least one string.
Chris Lattner504af112009-04-19 23:16:58 +0000376 if (Tok.isNot(tok::string_literal)) {
Chris Lattner2ff698d2009-01-16 08:21:25 +0000377 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
378 return;
379 }
380
381 // String concatenation allows multiple strings, which can even come from
382 // macro expansion.
383 // "foo " "bar" "Baz"
Chris Lattner262d4e32009-01-16 18:59:23 +0000384 llvm::SmallVector<Token, 4> StrToks;
Chris Lattner504af112009-04-19 23:16:58 +0000385 while (Tok.is(tok::string_literal)) {
Chris Lattner262d4e32009-01-16 18:59:23 +0000386 StrToks.push_back(Tok);
Chris Lattner2ff698d2009-01-16 08:21:25 +0000387 Lex(Tok);
Chris Lattner262d4e32009-01-16 18:59:23 +0000388 }
389
390 // Concatenate and parse the strings.
391 StringLiteralParser Literal(&StrToks[0], StrToks.size(), *this);
392 assert(!Literal.AnyWide && "Didn't allow wide strings in");
393 if (Literal.hadError)
394 return;
395 if (Literal.Pascal) {
396 Diag(StrToks[0].getLocation(), diag::err_pragma_comment_malformed);
397 return;
398 }
399
400 ArgumentString = std::string(Literal.GetString(),
401 Literal.GetString()+Literal.GetStringLength());
Chris Lattner2ff698d2009-01-16 08:21:25 +0000402 }
Mike Stump11289f42009-09-09 15:08:12 +0000403
Chris Lattner262d4e32009-01-16 18:59:23 +0000404 // FIXME: If the kind is "compiler" warn if the string is present (it is
405 // ignored).
406 // FIXME: 'lib' requires a comment string.
407 // FIXME: 'linker' requires a comment string, and has a specific list of
408 // things that are allowable.
Mike Stump11289f42009-09-09 15:08:12 +0000409
Chris Lattner2ff698d2009-01-16 08:21:25 +0000410 if (Tok.isNot(tok::r_paren)) {
411 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
412 return;
413 }
Chris Lattner262d4e32009-01-16 18:59:23 +0000414 Lex(Tok); // eat the r_paren.
Chris Lattner2ff698d2009-01-16 08:21:25 +0000415
416 if (Tok.isNot(tok::eom)) {
417 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
418 return;
419 }
Mike Stump11289f42009-09-09 15:08:12 +0000420
Chris Lattner262d4e32009-01-16 18:59:23 +0000421 // If the pragma is lexically sound, notify any interested PPCallbacks.
Chris Lattnerf49775d2009-01-16 19:01:46 +0000422 if (Callbacks)
423 Callbacks->PragmaComment(CommentLoc, II, ArgumentString);
Chris Lattner2ff698d2009-01-16 08:21:25 +0000424}
425
Chris Lattner30c924b2010-06-26 17:11:39 +0000426/// HandlePragmaMessage - Handle the microsoft #pragma message extension. The
427/// syntax is:
428/// #pragma message(messagestring)
429/// messagestring is a string, which is fully macro expanded, and permits string
430/// concatenation, embedded escape characters etc. See MSDN for more details.
431void Preprocessor::HandlePragmaMessage(Token &Tok) {
432 SourceLocation MessageLoc = Tok.getLocation();
433 Lex(Tok);
434 if (Tok.isNot(tok::l_paren)) {
435 Diag(MessageLoc, diag::err_pragma_message_malformed);
436 return;
437 }
Chris Lattner2ff698d2009-01-16 08:21:25 +0000438
Chris Lattner30c924b2010-06-26 17:11:39 +0000439 // Read the string.
440 Lex(Tok);
441
442
443 // We need at least one string.
444 if (Tok.isNot(tok::string_literal)) {
445 Diag(Tok.getLocation(), diag::err_pragma_message_malformed);
446 return;
447 }
448
449 // String concatenation allows multiple strings, which can even come from
450 // macro expansion.
451 // "foo " "bar" "Baz"
452 llvm::SmallVector<Token, 4> StrToks;
453 while (Tok.is(tok::string_literal)) {
454 StrToks.push_back(Tok);
455 Lex(Tok);
456 }
457
458 // Concatenate and parse the strings.
459 StringLiteralParser Literal(&StrToks[0], StrToks.size(), *this);
460 assert(!Literal.AnyWide && "Didn't allow wide strings in");
461 if (Literal.hadError)
462 return;
463 if (Literal.Pascal) {
464 Diag(StrToks[0].getLocation(), diag::err_pragma_message_malformed);
465 return;
466 }
467
468 llvm::StringRef MessageString(Literal.GetString(), Literal.GetStringLength());
469
470 if (Tok.isNot(tok::r_paren)) {
471 Diag(Tok.getLocation(), diag::err_pragma_message_malformed);
472 return;
473 }
474 Lex(Tok); // eat the r_paren.
475
476 if (Tok.isNot(tok::eom)) {
477 Diag(Tok.getLocation(), diag::err_pragma_message_malformed);
478 return;
479 }
480
481 // Output the message.
482 Diag(MessageLoc, diag::warn_pragma_message) << MessageString;
483
484 // If the pragma is lexically sound, notify any interested PPCallbacks.
485 if (Callbacks)
486 Callbacks->PragmaMessage(MessageLoc, MessageString);
487}
Chris Lattner2ff698d2009-01-16 08:21:25 +0000488
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000489/// ParsePragmaPushOrPopMacro - Handle parsing of pragma push_macro/pop_macro.
490/// Return the IdentifierInfo* associated with the macro to push or pop.
491IdentifierInfo *Preprocessor::ParsePragmaPushOrPopMacro(Token &Tok) {
492 // Remember the pragma token location.
493 Token PragmaTok = Tok;
494
495 // Read the '('.
496 Lex(Tok);
497 if (Tok.isNot(tok::l_paren)) {
498 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
499 << getSpelling(PragmaTok);
500 return 0;
501 }
502
503 // Read the macro name string.
504 Lex(Tok);
505 if (Tok.isNot(tok::string_literal)) {
506 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
507 << getSpelling(PragmaTok);
508 return 0;
509 }
510
511 // Remember the macro string.
512 std::string StrVal = getSpelling(Tok);
513
514 // Read the ')'.
515 Lex(Tok);
516 if (Tok.isNot(tok::r_paren)) {
517 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
518 << getSpelling(PragmaTok);
519 return 0;
520 }
521
522 assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
523 "Invalid string token!");
524
525 // Create a Token from the string.
526 Token MacroTok;
527 MacroTok.startToken();
528 MacroTok.setKind(tok::identifier);
529 CreateString(&StrVal[1], StrVal.size() - 2, MacroTok);
530
531 // Get the IdentifierInfo of MacroToPushTok.
532 return LookUpIdentifierInfo(MacroTok);
533}
534
535/// HandlePragmaPushMacro - Handle #pragma push_macro.
536/// The syntax is:
537/// #pragma push_macro("macro")
538void Preprocessor::HandlePragmaPushMacro(Token &PushMacroTok) {
539 // Parse the pragma directive and get the macro IdentifierInfo*.
540 IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PushMacroTok);
541 if (!IdentInfo) return;
542
543 // Get the MacroInfo associated with IdentInfo.
544 MacroInfo *MI = getMacroInfo(IdentInfo);
545
546 MacroInfo *MacroCopyToPush = 0;
547 if (MI) {
548 // Make a clone of MI.
549 MacroCopyToPush = CloneMacroInfo(*MI);
550
551 // Allow the original MacroInfo to be redefined later.
552 MI->setIsAllowRedefinitionsWithoutWarning(true);
553 }
554
555 // Push the cloned MacroInfo so we can retrieve it later.
556 PragmaPushMacroInfo[IdentInfo].push_back(MacroCopyToPush);
557}
558
559/// HandlePragmaPopMacro - Handle #pragma push_macro.
560/// The syntax is:
561/// #pragma pop_macro("macro")
562void Preprocessor::HandlePragmaPopMacro(Token &PopMacroTok) {
563 SourceLocation MessageLoc = PopMacroTok.getLocation();
564
565 // Parse the pragma directive and get the macro IdentifierInfo*.
566 IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PopMacroTok);
567 if (!IdentInfo) return;
568
569 // Find the vector<MacroInfo*> associated with the macro.
570 llvm::DenseMap<IdentifierInfo*, std::vector<MacroInfo*> >::iterator iter =
571 PragmaPushMacroInfo.find(IdentInfo);
572 if (iter != PragmaPushMacroInfo.end()) {
573 // Release the MacroInfo currently associated with IdentInfo.
574 MacroInfo *CurrentMI = getMacroInfo(IdentInfo);
575 if (CurrentMI) ReleaseMacroInfo(CurrentMI);
576
577 // Get the MacroInfo we want to reinstall.
578 MacroInfo *MacroToReInstall = iter->second.back();
579
580 // Reinstall the previously pushed macro.
581 setMacroInfo(IdentInfo, MacroToReInstall);
582
583 // Pop PragmaPushMacroInfo stack.
584 iter->second.pop_back();
585 if (iter->second.size() == 0)
586 PragmaPushMacroInfo.erase(iter);
587 } else {
588 Diag(MessageLoc, diag::warn_pragma_pop_macro_no_push)
589 << IdentInfo->getName();
590 }
591}
Chris Lattnerb694ba72006-07-02 22:41:36 +0000592
593/// AddPragmaHandler - Add the specified pragma handler to the preprocessor.
594/// If 'Namespace' is non-null, then it is a token required to exist on the
595/// pragma line before the pragma string starts, e.g. "STDC" or "GCC".
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000596void Preprocessor::AddPragmaHandler(llvm::StringRef Namespace,
Chris Lattnerb694ba72006-07-02 22:41:36 +0000597 PragmaHandler *Handler) {
598 PragmaNamespace *InsertNS = PragmaHandlers;
Mike Stump11289f42009-09-09 15:08:12 +0000599
Chris Lattnerb694ba72006-07-02 22:41:36 +0000600 // If this is specified to be in a namespace, step down into it.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000601 if (!Namespace.empty()) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000602 // If there is already a pragma handler with the name of this namespace,
603 // we either have an error (directive with the same name as a namespace) or
604 // we already have the namespace to insert into.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000605 if (PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace)) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000606 InsertNS = Existing->getIfNamespace();
607 assert(InsertNS != 0 && "Cannot have a pragma namespace and pragma"
608 " handler with the same name!");
609 } else {
610 // Otherwise, this namespace doesn't exist yet, create and insert the
611 // handler for it.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000612 InsertNS = new PragmaNamespace(Namespace);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000613 PragmaHandlers->AddPragma(InsertNS);
614 }
615 }
Mike Stump11289f42009-09-09 15:08:12 +0000616
Chris Lattnerb694ba72006-07-02 22:41:36 +0000617 // Check to make sure we don't already have a pragma for this identifier.
618 assert(!InsertNS->FindHandler(Handler->getName()) &&
619 "Pragma handler already exists for this identifier!");
620 InsertNS->AddPragma(Handler);
621}
622
Daniel Dunbar40596532008-10-04 19:17:46 +0000623/// RemovePragmaHandler - Remove the specific pragma handler from the
624/// preprocessor. If \arg Namespace is non-null, then it should be the
625/// namespace that \arg Handler was added to. It is an error to remove
626/// a handler that has not been registered.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000627void Preprocessor::RemovePragmaHandler(llvm::StringRef Namespace,
Daniel Dunbar40596532008-10-04 19:17:46 +0000628 PragmaHandler *Handler) {
629 PragmaNamespace *NS = PragmaHandlers;
Mike Stump11289f42009-09-09 15:08:12 +0000630
Daniel Dunbar40596532008-10-04 19:17:46 +0000631 // If this is specified to be in a namespace, step down into it.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000632 if (!Namespace.empty()) {
633 PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace);
Daniel Dunbar40596532008-10-04 19:17:46 +0000634 assert(Existing && "Namespace containing handler does not exist!");
635
636 NS = Existing->getIfNamespace();
637 assert(NS && "Invalid namespace, registered as a regular pragma handler!");
638 }
639
640 NS->RemovePragmaHandler(Handler);
Mike Stump11289f42009-09-09 15:08:12 +0000641
Daniel Dunbar40596532008-10-04 19:17:46 +0000642 // If this is a non-default namespace and it is now empty, remove
643 // it.
644 if (NS != PragmaHandlers && NS->IsEmpty())
645 PragmaHandlers->RemovePragmaHandler(NS);
646}
647
Chris Lattnerb694ba72006-07-02 22:41:36 +0000648namespace {
Chris Lattnerc2383312007-12-19 19:38:36 +0000649/// PragmaOnceHandler - "#pragma once" marks the file as atomically included.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000650struct PragmaOnceHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000651 PragmaOnceHandler() : PragmaHandler("once") {}
Chris Lattner146762e2007-07-20 16:59:19 +0000652 virtual void HandlePragma(Preprocessor &PP, Token &OnceTok) {
Chris Lattnerce2ab6f2009-04-14 05:07:49 +0000653 PP.CheckEndOfDirective("pragma once");
Chris Lattnerb694ba72006-07-02 22:41:36 +0000654 PP.HandlePragmaOnce(OnceTok);
655 }
656};
657
Chris Lattnerc2383312007-12-19 19:38:36 +0000658/// PragmaMarkHandler - "#pragma mark ..." is ignored by the compiler, and the
659/// rest of the line is not lexed.
660struct PragmaMarkHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000661 PragmaMarkHandler() : PragmaHandler("mark") {}
Chris Lattnerc2383312007-12-19 19:38:36 +0000662 virtual void HandlePragma(Preprocessor &PP, Token &MarkTok) {
663 PP.HandlePragmaMark();
664 }
665};
666
667/// PragmaPoisonHandler - "#pragma poison x" marks x as not usable.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000668struct PragmaPoisonHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000669 PragmaPoisonHandler() : PragmaHandler("poison") {}
Chris Lattner146762e2007-07-20 16:59:19 +0000670 virtual void HandlePragma(Preprocessor &PP, Token &PoisonTok) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000671 PP.HandlePragmaPoison(PoisonTok);
672 }
673};
674
Chris Lattnerc2383312007-12-19 19:38:36 +0000675/// PragmaSystemHeaderHandler - "#pragma system_header" marks the current file
676/// as a system header, which silences warnings in it.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000677struct PragmaSystemHeaderHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000678 PragmaSystemHeaderHandler() : PragmaHandler("system_header") {}
Chris Lattner146762e2007-07-20 16:59:19 +0000679 virtual void HandlePragma(Preprocessor &PP, Token &SHToken) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000680 PP.HandlePragmaSystemHeader(SHToken);
Chris Lattnerce2ab6f2009-04-14 05:07:49 +0000681 PP.CheckEndOfDirective("pragma");
Chris Lattnerb694ba72006-07-02 22:41:36 +0000682 }
683};
684struct PragmaDependencyHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000685 PragmaDependencyHandler() : PragmaHandler("dependency") {}
Chris Lattner146762e2007-07-20 16:59:19 +0000686 virtual void HandlePragma(Preprocessor &PP, Token &DepToken) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000687 PP.HandlePragmaDependency(DepToken);
688 }
689};
Mike Stump11289f42009-09-09 15:08:12 +0000690
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000691struct PragmaDebugHandler : public PragmaHandler {
692 PragmaDebugHandler() : PragmaHandler("__debug") {}
693 virtual void HandlePragma(Preprocessor &PP, Token &DepToken) {
694 Token Tok;
695 PP.LexUnexpandedToken(Tok);
696 if (Tok.isNot(tok::identifier)) {
697 PP.Diag(Tok, diag::warn_pragma_diagnostic_clang_invalid);
698 return;
699 }
700 IdentifierInfo *II = Tok.getIdentifierInfo();
701
Daniel Dunbarf2cf3292010-08-17 22:32:48 +0000702 if (II->isStr("assert")) {
703 assert(0 && "This is an assertion!");
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000704 } else if (II->isStr("crash")) {
Daniel Dunbarf2cf3292010-08-17 22:32:48 +0000705 *(volatile int*) 0x11 = 0;
706 } else if (II->isStr("llvm_fatal_error")) {
707 llvm::report_fatal_error("#pragma clang __debug llvm_fatal_error");
708 } else if (II->isStr("llvm_unreachable")) {
709 llvm_unreachable("#pragma clang __debug llvm_unreachable");
710 } else if (II->isStr("overflow_stack")) {
711 DebugOverflowStack();
Daniel Dunbar211a7872010-08-18 23:09:23 +0000712 } else if (II->isStr("handle_crash")) {
713 llvm::CrashRecoveryContext *CRC =llvm::CrashRecoveryContext::GetCurrent();
714 if (CRC)
715 CRC->HandleCrash();
Daniel Dunbarf2cf3292010-08-17 22:32:48 +0000716 } else {
717 PP.Diag(Tok, diag::warn_pragma_debug_unexpected_command)
718 << II->getName();
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000719 }
720 }
721
722 void DebugOverflowStack() {
723 DebugOverflowStack();
724 }
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000725};
726
Chris Lattner504af112009-04-19 23:16:58 +0000727/// PragmaDiagnosticHandler - e.g. '#pragma GCC diagnostic ignored "-Wformat"'
Chris Lattnerfb42a182009-07-12 21:18:45 +0000728/// Since clang's diagnostic supports extended functionality beyond GCC's
729/// the constructor takes a clangMode flag to tell it whether or not to allow
730/// clang's extended functionality, or whether to reject it.
Chris Lattner504af112009-04-19 23:16:58 +0000731struct PragmaDiagnosticHandler : public PragmaHandler {
Chris Lattnerfb42a182009-07-12 21:18:45 +0000732private:
733 const bool ClangMode;
734public:
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000735 explicit PragmaDiagnosticHandler(const bool clangMode)
736 : PragmaHandler("diagnostic"), ClangMode(clangMode) {}
737
Chris Lattner504af112009-04-19 23:16:58 +0000738 virtual void HandlePragma(Preprocessor &PP, Token &DiagToken) {
739 Token Tok;
740 PP.LexUnexpandedToken(Tok);
741 if (Tok.isNot(tok::identifier)) {
Chris Lattnerfb42a182009-07-12 21:18:45 +0000742 unsigned Diag = ClangMode ? diag::warn_pragma_diagnostic_clang_invalid
743 : diag::warn_pragma_diagnostic_gcc_invalid;
744 PP.Diag(Tok, Diag);
Chris Lattner504af112009-04-19 23:16:58 +0000745 return;
746 }
747 IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000748
Chris Lattner504af112009-04-19 23:16:58 +0000749 diag::Mapping Map;
750 if (II->isStr("warning"))
751 Map = diag::MAP_WARNING;
752 else if (II->isStr("error"))
753 Map = diag::MAP_ERROR;
754 else if (II->isStr("ignored"))
755 Map = diag::MAP_IGNORE;
756 else if (II->isStr("fatal"))
757 Map = diag::MAP_FATAL;
Chris Lattnerfb42a182009-07-12 21:18:45 +0000758 else if (ClangMode) {
759 if (II->isStr("pop")) {
Mike Stump11289f42009-09-09 15:08:12 +0000760 if (!PP.getDiagnostics().popMappings())
Chris Lattnerfb42a182009-07-12 21:18:45 +0000761 PP.Diag(Tok, diag::warn_pragma_diagnostic_clang_cannot_ppp);
762 return;
763 }
764
765 if (II->isStr("push")) {
766 PP.getDiagnostics().pushMappings();
Mike Stump11289f42009-09-09 15:08:12 +0000767 return;
Chris Lattnerfb42a182009-07-12 21:18:45 +0000768 }
769
770 PP.Diag(Tok, diag::warn_pragma_diagnostic_clang_invalid);
771 return;
772 } else {
773 PP.Diag(Tok, diag::warn_pragma_diagnostic_gcc_invalid);
Chris Lattner504af112009-04-19 23:16:58 +0000774 return;
775 }
Mike Stump11289f42009-09-09 15:08:12 +0000776
Chris Lattner504af112009-04-19 23:16:58 +0000777 PP.LexUnexpandedToken(Tok);
778
779 // We need at least one string.
780 if (Tok.isNot(tok::string_literal)) {
781 PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token);
782 return;
783 }
Mike Stump11289f42009-09-09 15:08:12 +0000784
Chris Lattner504af112009-04-19 23:16:58 +0000785 // String concatenation allows multiple strings, which can even come from
786 // macro expansion.
787 // "foo " "bar" "Baz"
788 llvm::SmallVector<Token, 4> StrToks;
789 while (Tok.is(tok::string_literal)) {
790 StrToks.push_back(Tok);
791 PP.LexUnexpandedToken(Tok);
792 }
Mike Stump11289f42009-09-09 15:08:12 +0000793
Chris Lattner504af112009-04-19 23:16:58 +0000794 if (Tok.isNot(tok::eom)) {
795 PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token);
796 return;
797 }
Mike Stump11289f42009-09-09 15:08:12 +0000798
Chris Lattner504af112009-04-19 23:16:58 +0000799 // Concatenate and parse the strings.
800 StringLiteralParser Literal(&StrToks[0], StrToks.size(), PP);
801 assert(!Literal.AnyWide && "Didn't allow wide strings in");
802 if (Literal.hadError)
803 return;
804 if (Literal.Pascal) {
Chris Lattnerfb42a182009-07-12 21:18:45 +0000805 unsigned Diag = ClangMode ? diag::warn_pragma_diagnostic_clang_invalid
806 : diag::warn_pragma_diagnostic_gcc_invalid;
807 PP.Diag(Tok, Diag);
Chris Lattner504af112009-04-19 23:16:58 +0000808 return;
809 }
Chris Lattnerfb42a182009-07-12 21:18:45 +0000810
Chris Lattner504af112009-04-19 23:16:58 +0000811 std::string WarningName(Literal.GetString(),
812 Literal.GetString()+Literal.GetStringLength());
813
814 if (WarningName.size() < 3 || WarningName[0] != '-' ||
815 WarningName[1] != 'W') {
816 PP.Diag(StrToks[0].getLocation(),
817 diag::warn_pragma_diagnostic_invalid_option);
818 return;
819 }
Mike Stump11289f42009-09-09 15:08:12 +0000820
Chris Lattner504af112009-04-19 23:16:58 +0000821 if (PP.getDiagnostics().setDiagnosticGroupMapping(WarningName.c_str()+2,
822 Map))
823 PP.Diag(StrToks[0].getLocation(),
824 diag::warn_pragma_diagnostic_unknown_warning) << WarningName;
825 }
826};
Mike Stump11289f42009-09-09 15:08:12 +0000827
Chris Lattner2ff698d2009-01-16 08:21:25 +0000828/// PragmaCommentHandler - "#pragma comment ...".
829struct PragmaCommentHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000830 PragmaCommentHandler() : PragmaHandler("comment") {}
Chris Lattner2ff698d2009-01-16 08:21:25 +0000831 virtual void HandlePragma(Preprocessor &PP, Token &CommentTok) {
832 PP.HandlePragmaComment(CommentTok);
833 }
834};
Mike Stump11289f42009-09-09 15:08:12 +0000835
Chris Lattner30c924b2010-06-26 17:11:39 +0000836/// PragmaMessageHandler - "#pragma message("...")".
837struct PragmaMessageHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000838 PragmaMessageHandler() : PragmaHandler("message") {}
Chris Lattner30c924b2010-06-26 17:11:39 +0000839 virtual void HandlePragma(Preprocessor &PP, Token &CommentTok) {
840 PP.HandlePragmaMessage(CommentTok);
841 }
842};
843
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000844/// PragmaPushMacroHandler - "#pragma push_macro" saves the value of the
845/// macro on the top of the stack.
846struct PragmaPushMacroHandler : public PragmaHandler {
847 PragmaPushMacroHandler() : PragmaHandler("push_macro") {}
848 virtual void HandlePragma(Preprocessor &PP, Token &PushMacroTok) {
849 PP.HandlePragmaPushMacro(PushMacroTok);
850 }
851};
852
853
854/// PragmaPopMacroHandler - "#pragma pop_macro" sets the value of the
855/// macro to the value on the top of the stack.
856struct PragmaPopMacroHandler : public PragmaHandler {
857 PragmaPopMacroHandler() : PragmaHandler("pop_macro") {}
858 virtual void HandlePragma(Preprocessor &PP, Token &PopMacroTok) {
859 PP.HandlePragmaPopMacro(PopMacroTok);
860 }
861};
862
Chris Lattner958ee042009-04-19 21:20:35 +0000863// Pragma STDC implementations.
Chris Lattner02ef4e32009-04-19 21:50:08 +0000864
865enum STDCSetting {
866 STDC_ON, STDC_OFF, STDC_DEFAULT, STDC_INVALID
867};
Mike Stump11289f42009-09-09 15:08:12 +0000868
Chris Lattner02ef4e32009-04-19 21:50:08 +0000869static STDCSetting LexOnOffSwitch(Preprocessor &PP) {
870 Token Tok;
871 PP.LexUnexpandedToken(Tok);
872
873 if (Tok.isNot(tok::identifier)) {
874 PP.Diag(Tok, diag::ext_stdc_pragma_syntax);
875 return STDC_INVALID;
876 }
877 IdentifierInfo *II = Tok.getIdentifierInfo();
878 STDCSetting Result;
879 if (II->isStr("ON"))
880 Result = STDC_ON;
881 else if (II->isStr("OFF"))
882 Result = STDC_OFF;
883 else if (II->isStr("DEFAULT"))
884 Result = STDC_DEFAULT;
885 else {
886 PP.Diag(Tok, diag::ext_stdc_pragma_syntax);
887 return STDC_INVALID;
888 }
889
890 // Verify that this is followed by EOM.
891 PP.LexUnexpandedToken(Tok);
892 if (Tok.isNot(tok::eom))
893 PP.Diag(Tok, diag::ext_stdc_pragma_syntax_eom);
894 return Result;
895}
Mike Stump11289f42009-09-09 15:08:12 +0000896
Chris Lattner958ee042009-04-19 21:20:35 +0000897/// PragmaSTDC_FP_CONTRACTHandler - "#pragma STDC FP_CONTRACT ...".
898struct PragmaSTDC_FP_CONTRACTHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000899 PragmaSTDC_FP_CONTRACTHandler() : PragmaHandler("FP_CONTRACT") {}
Chris Lattnera0b1f762009-04-19 21:25:37 +0000900 virtual void HandlePragma(Preprocessor &PP, Token &Tok) {
Chris Lattner02ef4e32009-04-19 21:50:08 +0000901 // We just ignore the setting of FP_CONTRACT. Since we don't do contractions
902 // at all, our default is OFF and setting it to ON is an optimization hint
903 // we can safely ignore. When we support -ffma or something, we would need
904 // to diagnose that we are ignoring FMA.
905 LexOnOffSwitch(PP);
Chris Lattner958ee042009-04-19 21:20:35 +0000906 }
907};
Mike Stump11289f42009-09-09 15:08:12 +0000908
Chris Lattner958ee042009-04-19 21:20:35 +0000909/// PragmaSTDC_FENV_ACCESSHandler - "#pragma STDC FENV_ACCESS ...".
910struct PragmaSTDC_FENV_ACCESSHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000911 PragmaSTDC_FENV_ACCESSHandler() : PragmaHandler("FENV_ACCESS") {}
Chris Lattnera0b1f762009-04-19 21:25:37 +0000912 virtual void HandlePragma(Preprocessor &PP, Token &Tok) {
Chris Lattnerdf222682009-04-19 21:55:32 +0000913 if (LexOnOffSwitch(PP) == STDC_ON)
914 PP.Diag(Tok, diag::warn_stdc_fenv_access_not_supported);
Chris Lattner958ee042009-04-19 21:20:35 +0000915 }
916};
Mike Stump11289f42009-09-09 15:08:12 +0000917
Chris Lattner958ee042009-04-19 21:20:35 +0000918/// PragmaSTDC_CX_LIMITED_RANGEHandler - "#pragma STDC CX_LIMITED_RANGE ...".
919struct PragmaSTDC_CX_LIMITED_RANGEHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000920 PragmaSTDC_CX_LIMITED_RANGEHandler()
921 : PragmaHandler("CX_LIMITED_RANGE") {}
Chris Lattnera0b1f762009-04-19 21:25:37 +0000922 virtual void HandlePragma(Preprocessor &PP, Token &Tok) {
Chris Lattner02ef4e32009-04-19 21:50:08 +0000923 LexOnOffSwitch(PP);
Chris Lattner958ee042009-04-19 21:20:35 +0000924 }
925};
Mike Stump11289f42009-09-09 15:08:12 +0000926
Chris Lattner958ee042009-04-19 21:20:35 +0000927/// PragmaSTDC_UnknownHandler - "#pragma STDC ...".
928struct PragmaSTDC_UnknownHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000929 PragmaSTDC_UnknownHandler() {}
Chris Lattnera0b1f762009-04-19 21:25:37 +0000930 virtual void HandlePragma(Preprocessor &PP, Token &UnknownTok) {
Chris Lattner02ef4e32009-04-19 21:50:08 +0000931 // C99 6.10.6p2, unknown forms are not allowed.
Chris Lattnera0b1f762009-04-19 21:25:37 +0000932 PP.Diag(UnknownTok, diag::ext_stdc_pragma_ignored);
Chris Lattner958ee042009-04-19 21:20:35 +0000933 }
934};
Mike Stump11289f42009-09-09 15:08:12 +0000935
Chris Lattnerb694ba72006-07-02 22:41:36 +0000936} // end anonymous namespace
937
938
939/// RegisterBuiltinPragmas - Install the standard preprocessor pragmas:
940/// #pragma GCC poison/system_header/dependency and #pragma once.
941void Preprocessor::RegisterBuiltinPragmas() {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000942 AddPragmaHandler(new PragmaOnceHandler());
943 AddPragmaHandler(new PragmaMarkHandler());
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000944 AddPragmaHandler(new PragmaPushMacroHandler());
945 AddPragmaHandler(new PragmaPopMacroHandler());
Mike Stump11289f42009-09-09 15:08:12 +0000946
Chris Lattnerb61448d2009-05-12 18:21:11 +0000947 // #pragma GCC ...
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000948 AddPragmaHandler("GCC", new PragmaPoisonHandler());
949 AddPragmaHandler("GCC", new PragmaSystemHeaderHandler());
950 AddPragmaHandler("GCC", new PragmaDependencyHandler());
951 AddPragmaHandler("GCC", new PragmaDiagnosticHandler(false));
Chris Lattnerb61448d2009-05-12 18:21:11 +0000952 // #pragma clang ...
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000953 AddPragmaHandler("clang", new PragmaPoisonHandler());
954 AddPragmaHandler("clang", new PragmaSystemHeaderHandler());
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000955 AddPragmaHandler("clang", new PragmaDebugHandler());
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000956 AddPragmaHandler("clang", new PragmaDependencyHandler());
957 AddPragmaHandler("clang", new PragmaDiagnosticHandler(true));
Chris Lattnerb61448d2009-05-12 18:21:11 +0000958
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000959 AddPragmaHandler("STDC", new PragmaSTDC_FP_CONTRACTHandler());
960 AddPragmaHandler("STDC", new PragmaSTDC_FENV_ACCESSHandler());
961 AddPragmaHandler("STDC", new PragmaSTDC_CX_LIMITED_RANGEHandler());
Chris Lattner958ee042009-04-19 21:20:35 +0000962 AddPragmaHandler("STDC", new PragmaSTDC_UnknownHandler());
Mike Stump11289f42009-09-09 15:08:12 +0000963
Chris Lattner2ff698d2009-01-16 08:21:25 +0000964 // MS extensions.
Chris Lattner30c924b2010-06-26 17:11:39 +0000965 if (Features.Microsoft) {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000966 AddPragmaHandler(new PragmaCommentHandler());
967 AddPragmaHandler(new PragmaMessageHandler());
Chris Lattner30c924b2010-06-26 17:11:39 +0000968 }
Chris Lattnerb694ba72006-07-02 22:41:36 +0000969}