blob: 046a4d02f0ab46e4363d53408ff8126284eb88dc [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
Douglas Gregorc7d65762010-09-09 22:45:38 +000038void EmptyPragmaHandler::HandlePragma(Preprocessor &PP,
39 PragmaIntroducerKind Introducer,
40 Token &FirstToken) {}
Daniel Dunbard839e772010-06-11 20:10:12 +000041
42//===----------------------------------------------------------------------===//
Chris Lattner2e155302006-07-03 05:34:41 +000043// PragmaNamespace Implementation.
44//===----------------------------------------------------------------------===//
45
46
47PragmaNamespace::~PragmaNamespace() {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000048 for (llvm::StringMap<PragmaHandler*>::iterator
49 I = Handlers.begin(), E = Handlers.end(); I != E; ++I)
50 delete I->second;
Chris Lattner2e155302006-07-03 05:34:41 +000051}
52
53/// FindHandler - Check to see if there is already a handler for the
54/// specified name. If not, return the handler for the null identifier if it
55/// exists, otherwise return null. If IgnoreNull is true (the default) then
56/// the null handler isn't returned on failure to match.
Chris Lattner0e62c1c2011-07-23 10:55:15 +000057PragmaHandler *PragmaNamespace::FindHandler(StringRef Name,
Chris Lattner2e155302006-07-03 05:34:41 +000058 bool IgnoreNull) const {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000059 if (PragmaHandler *Handler = Handlers.lookup(Name))
60 return Handler;
Chris Lattner0e62c1c2011-07-23 10:55:15 +000061 return IgnoreNull ? 0 : Handlers.lookup(StringRef());
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000062}
Mike Stump11289f42009-09-09 15:08:12 +000063
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000064void PragmaNamespace::AddPragma(PragmaHandler *Handler) {
65 assert(!Handlers.lookup(Handler->getName()) &&
66 "A handler with this name is already registered in this namespace");
67 llvm::StringMapEntry<PragmaHandler *> &Entry =
68 Handlers.GetOrCreateValue(Handler->getName());
69 Entry.setValue(Handler);
Chris Lattner2e155302006-07-03 05:34:41 +000070}
71
Daniel Dunbar40596532008-10-04 19:17:46 +000072void PragmaNamespace::RemovePragmaHandler(PragmaHandler *Handler) {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000073 assert(Handlers.lookup(Handler->getName()) &&
74 "Handler not registered in this namespace");
75 Handlers.erase(Handler->getName());
Daniel Dunbar40596532008-10-04 19:17:46 +000076}
77
Douglas Gregorc7d65762010-09-09 22:45:38 +000078void PragmaNamespace::HandlePragma(Preprocessor &PP,
79 PragmaIntroducerKind Introducer,
80 Token &Tok) {
Chris Lattnerb8761832006-06-24 21:31:03 +000081 // Read the 'namespace' that the directive is in, e.g. STDC. Do not macro
82 // expand it, the user can have a STDC #define, that should not affect this.
83 PP.LexUnexpandedToken(Tok);
Mike Stump11289f42009-09-09 15:08:12 +000084
Chris Lattnerb8761832006-06-24 21:31:03 +000085 // Get the handler for this token. If there is no handler, ignore the pragma.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000086 PragmaHandler *Handler
87 = FindHandler(Tok.getIdentifierInfo() ? Tok.getIdentifierInfo()->getName()
Chris Lattner0e62c1c2011-07-23 10:55:15 +000088 : StringRef(),
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000089 /*IgnoreNull=*/false);
Chris Lattner21656f22009-04-19 21:10:26 +000090 if (Handler == 0) {
91 PP.Diag(Tok, diag::warn_pragma_ignored);
92 return;
93 }
Mike Stump11289f42009-09-09 15:08:12 +000094
Chris Lattnerb8761832006-06-24 21:31:03 +000095 // Otherwise, pass it down.
Douglas Gregorc7d65762010-09-09 22:45:38 +000096 Handler->HandlePragma(PP, Introducer, Tok);
Chris Lattnerb8761832006-06-24 21:31:03 +000097}
Chris Lattnerb694ba72006-07-02 22:41:36 +000098
Chris Lattnerb694ba72006-07-02 22:41:36 +000099//===----------------------------------------------------------------------===//
100// Preprocessor Pragma Directive Handling.
101//===----------------------------------------------------------------------===//
102
103/// HandlePragmaDirective - The "#pragma" directive has been parsed. Lex the
104/// rest of the pragma, passing it to the registered pragma handlers.
Douglas Gregorc7d65762010-09-09 22:45:38 +0000105void Preprocessor::HandlePragmaDirective(unsigned Introducer) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000106 ++NumPragma;
Mike Stump11289f42009-09-09 15:08:12 +0000107
Chris Lattnerb694ba72006-07-02 22:41:36 +0000108 // Invoke the first level of pragma handlers which reads the namespace id.
Chris Lattner146762e2007-07-20 16:59:19 +0000109 Token Tok;
Douglas Gregorc7d65762010-09-09 22:45:38 +0000110 PragmaHandlers->HandlePragma(*this, PragmaIntroducerKind(Introducer), Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000111
Chris Lattnerb694ba72006-07-02 22:41:36 +0000112 // If the pragma handler didn't read the rest of the line, consume it now.
Peter Collingbourne2c9f9662011-02-22 13:49:00 +0000113 if ((CurTokenLexer && CurTokenLexer->isParsingPreprocessorDirective())
114 || (CurPPLexer && CurPPLexer->ParsingPreprocessorDirective))
Chris Lattnerb694ba72006-07-02 22:41:36 +0000115 DiscardUntilEndOfDirective();
116}
117
118/// Handle_Pragma - Read a _Pragma directive, slice it up, process it, then
119/// return the first token after the directive. The _Pragma token has just
120/// been read into 'Tok'.
Chris Lattner146762e2007-07-20 16:59:19 +0000121void Preprocessor::Handle_Pragma(Token &Tok) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000122 // Remember the pragma token location.
123 SourceLocation PragmaLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000124
Chris Lattnerb694ba72006-07-02 22:41:36 +0000125 // Read the '('.
126 Lex(Tok);
Chris Lattner907dfe92008-11-18 07:59:24 +0000127 if (Tok.isNot(tok::l_paren)) {
128 Diag(PragmaLoc, diag::err__Pragma_malformed);
129 return;
130 }
Chris Lattnerb694ba72006-07-02 22:41:36 +0000131
132 // Read the '"..."'.
133 Lex(Tok);
Chris Lattner907dfe92008-11-18 07:59:24 +0000134 if (Tok.isNot(tok::string_literal) && Tok.isNot(tok::wide_string_literal)) {
135 Diag(PragmaLoc, diag::err__Pragma_malformed);
136 return;
137 }
Mike Stump11289f42009-09-09 15:08:12 +0000138
Chris Lattnerb694ba72006-07-02 22:41:36 +0000139 // Remember the string.
140 std::string StrVal = getSpelling(Tok);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000141
142 // Read the ')'.
143 Lex(Tok);
Chris Lattner907dfe92008-11-18 07:59:24 +0000144 if (Tok.isNot(tok::r_paren)) {
145 Diag(PragmaLoc, diag::err__Pragma_malformed);
146 return;
147 }
Mike Stump11289f42009-09-09 15:08:12 +0000148
Chris Lattner9dc9c202009-02-15 20:52:18 +0000149 SourceLocation RParenLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000150
Chris Lattner262d4e32009-01-16 18:59:23 +0000151 // The _Pragma is lexically sound. Destringize according to C99 6.10.9.1:
152 // "The string literal is destringized by deleting the L prefix, if present,
153 // deleting the leading and trailing double-quotes, replacing each escape
154 // sequence \" by a double-quote, and replacing each escape sequence \\ by a
155 // single backslash."
Chris Lattnerb694ba72006-07-02 22:41:36 +0000156 if (StrVal[0] == 'L') // Remove L prefix.
157 StrVal.erase(StrVal.begin());
158 assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
159 "Invalid string token!");
Mike Stump11289f42009-09-09 15:08:12 +0000160
Chris Lattnerb694ba72006-07-02 22:41:36 +0000161 // Remove the front quote, replacing it with a space, so that the pragma
162 // contents appear to have a space before them.
163 StrVal[0] = ' ';
Mike Stump11289f42009-09-09 15:08:12 +0000164
Chris Lattnerfa217bd2009-03-08 08:08:45 +0000165 // Replace the terminating quote with a \n.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000166 StrVal[StrVal.size()-1] = '\n';
Mike Stump11289f42009-09-09 15:08:12 +0000167
Chris Lattnerb694ba72006-07-02 22:41:36 +0000168 // Remove escaped quotes and escapes.
169 for (unsigned i = 0, e = StrVal.size(); i != e-1; ++i) {
170 if (StrVal[i] == '\\' &&
171 (StrVal[i+1] == '\\' || StrVal[i+1] == '"')) {
172 // \\ -> '\' and \" -> '"'.
173 StrVal.erase(StrVal.begin()+i);
174 --e;
175 }
176 }
John McCall89e925d2010-08-28 22:34:47 +0000177
Peter Collingbournef29ce972011-02-22 13:49:06 +0000178 // Plop the string (including the newline and trailing null) into a buffer
179 // where we can lex it.
180 Token TmpTok;
181 TmpTok.startToken();
182 CreateString(&StrVal[0], StrVal.size(), TmpTok);
183 SourceLocation TokLoc = TmpTok.getLocation();
184
185 // Make and enter a lexer object so that we lex and expand the tokens just
186 // like any others.
187 Lexer *TL = Lexer::Create_PragmaLexer(TokLoc, PragmaLoc, RParenLoc,
188 StrVal.size(), *this);
189
190 EnterSourceFileWithLexer(TL, 0);
191
192 // With everything set up, lex this as a #pragma directive.
193 HandlePragmaDirective(PIK__Pragma);
John McCall89e925d2010-08-28 22:34:47 +0000194
195 // Finally, return whatever came after the pragma directive.
196 return Lex(Tok);
197}
198
199/// HandleMicrosoft__pragma - Like Handle_Pragma except the pragma text
200/// is not enclosed within a string literal.
201void Preprocessor::HandleMicrosoft__pragma(Token &Tok) {
202 // Remember the pragma token location.
203 SourceLocation PragmaLoc = Tok.getLocation();
204
205 // Read the '('.
206 Lex(Tok);
207 if (Tok.isNot(tok::l_paren)) {
208 Diag(PragmaLoc, diag::err__Pragma_malformed);
209 return;
210 }
211
Peter Collingbournef29ce972011-02-22 13:49:06 +0000212 // Get the tokens enclosed within the __pragma(), as well as the final ')'.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000213 SmallVector<Token, 32> PragmaToks;
John McCall89e925d2010-08-28 22:34:47 +0000214 int NumParens = 0;
215 Lex(Tok);
216 while (Tok.isNot(tok::eof)) {
Peter Collingbournef29ce972011-02-22 13:49:06 +0000217 PragmaToks.push_back(Tok);
John McCall89e925d2010-08-28 22:34:47 +0000218 if (Tok.is(tok::l_paren))
219 NumParens++;
220 else if (Tok.is(tok::r_paren) && NumParens-- == 0)
221 break;
John McCall89e925d2010-08-28 22:34:47 +0000222 Lex(Tok);
223 }
224
John McCall49039d42010-08-29 01:09:54 +0000225 if (Tok.is(tok::eof)) {
226 Diag(PragmaLoc, diag::err_unterminated___pragma);
227 return;
228 }
229
Peter Collingbournef29ce972011-02-22 13:49:06 +0000230 PragmaToks.front().setFlag(Token::LeadingSpace);
John McCall89e925d2010-08-28 22:34:47 +0000231
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000232 // Replace the ')' with an EOD to mark the end of the pragma.
233 PragmaToks.back().setKind(tok::eod);
Peter Collingbournef29ce972011-02-22 13:49:06 +0000234
235 Token *TokArray = new Token[PragmaToks.size()];
236 std::copy(PragmaToks.begin(), PragmaToks.end(), TokArray);
237
238 // Push the tokens onto the stack.
239 EnterTokenStream(TokArray, PragmaToks.size(), true, true);
240
241 // With everything set up, lex this as a #pragma directive.
242 HandlePragmaDirective(PIK___pragma);
John McCall89e925d2010-08-28 22:34:47 +0000243
244 // Finally, return whatever came after the pragma directive.
245 return Lex(Tok);
246}
247
Chris Lattnerb694ba72006-07-02 22:41:36 +0000248/// HandlePragmaOnce - Handle #pragma once. OnceTok is the 'once'.
249///
Chris Lattner146762e2007-07-20 16:59:19 +0000250void Preprocessor::HandlePragmaOnce(Token &OnceTok) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000251 if (isInPrimaryFile()) {
252 Diag(OnceTok, diag::pp_pragma_once_in_main_file);
253 return;
254 }
Mike Stump11289f42009-09-09 15:08:12 +0000255
Chris Lattnerb694ba72006-07-02 22:41:36 +0000256 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000257 // Mark the file as a once-only file now.
Chris Lattnerd32480d2009-01-17 06:22:33 +0000258 HeaderInfo.MarkFileIncludeOnce(getCurrentFileLexer()->getFileEntry());
Chris Lattnerb694ba72006-07-02 22:41:36 +0000259}
260
Chris Lattnerc2383312007-12-19 19:38:36 +0000261void Preprocessor::HandlePragmaMark() {
Ted Kremenek76c34412008-11-19 22:21:33 +0000262 assert(CurPPLexer && "No current lexer?");
Chris Lattnerd9efb6e2009-06-15 05:02:34 +0000263 if (CurLexer)
264 CurLexer->ReadToEndOfLine();
265 else
266 CurPTHLexer->DiscardToEndOfLine();
Chris Lattnerc2383312007-12-19 19:38:36 +0000267}
268
269
Chris Lattnerb694ba72006-07-02 22:41:36 +0000270/// HandlePragmaPoison - Handle #pragma GCC poison. PoisonTok is the 'poison'.
271///
Chris Lattner146762e2007-07-20 16:59:19 +0000272void Preprocessor::HandlePragmaPoison(Token &PoisonTok) {
273 Token Tok;
Chris Lattner538d7f32006-07-20 04:31:52 +0000274
Chris Lattnerb694ba72006-07-02 22:41:36 +0000275 while (1) {
276 // Read the next token to poison. While doing this, pretend that we are
277 // skipping while reading the identifier to poison.
278 // This avoids errors on code like:
279 // #pragma GCC poison X
280 // #pragma GCC poison X
Ted Kremenek551c82a2008-11-18 01:12:54 +0000281 if (CurPPLexer) CurPPLexer->LexingRawMode = true;
Chris Lattnerb694ba72006-07-02 22:41:36 +0000282 LexUnexpandedToken(Tok);
Ted Kremenek551c82a2008-11-18 01:12:54 +0000283 if (CurPPLexer) CurPPLexer->LexingRawMode = false;
Mike Stump11289f42009-09-09 15:08:12 +0000284
Chris Lattnerb694ba72006-07-02 22:41:36 +0000285 // If we reached the end of line, we're done.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000286 if (Tok.is(tok::eod)) return;
Mike Stump11289f42009-09-09 15:08:12 +0000287
Chris Lattnerb694ba72006-07-02 22:41:36 +0000288 // Can only poison identifiers.
Abramo Bagnaraea4f7c72010-12-22 08:23:18 +0000289 if (Tok.isNot(tok::raw_identifier)) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000290 Diag(Tok, diag::err_pp_invalid_poison);
291 return;
292 }
Mike Stump11289f42009-09-09 15:08:12 +0000293
Chris Lattnercefc7682006-07-08 08:28:12 +0000294 // Look up the identifier info for the token. We disabled identifier lookup
295 // by saying we're skipping contents, so we need to do this manually.
296 IdentifierInfo *II = LookUpIdentifierInfo(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000297
Chris Lattnerb694ba72006-07-02 22:41:36 +0000298 // Already poisoned.
299 if (II->isPoisoned()) continue;
Mike Stump11289f42009-09-09 15:08:12 +0000300
Chris Lattnerb694ba72006-07-02 22:41:36 +0000301 // If this is a macro identifier, emit a warning.
Chris Lattner259716a2007-10-07 08:04:56 +0000302 if (II->hasMacroDefinition())
Chris Lattnerb694ba72006-07-02 22:41:36 +0000303 Diag(Tok, diag::pp_poisoning_existing_macro);
Mike Stump11289f42009-09-09 15:08:12 +0000304
Chris Lattnerb694ba72006-07-02 22:41:36 +0000305 // Finally, poison it!
306 II->setIsPoisoned();
Douglas Gregor935bc7a22011-10-27 09:33:13 +0000307 if (II->isFromAST())
308 II->setChangedSinceDeserialization();
Chris Lattnerb694ba72006-07-02 22:41:36 +0000309 }
310}
311
312/// HandlePragmaSystemHeader - Implement #pragma GCC system_header. We know
313/// that the whole directive has been parsed.
Chris Lattner146762e2007-07-20 16:59:19 +0000314void Preprocessor::HandlePragmaSystemHeader(Token &SysHeaderTok) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000315 if (isInPrimaryFile()) {
316 Diag(SysHeaderTok, diag::pp_pragma_sysheader_in_main_file);
317 return;
318 }
Mike Stump11289f42009-09-09 15:08:12 +0000319
Chris Lattnerb694ba72006-07-02 22:41:36 +0000320 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
Ted Kremenek300590b2008-11-20 01:45:11 +0000321 PreprocessorLexer *TheLexer = getCurrentFileLexer();
Mike Stump11289f42009-09-09 15:08:12 +0000322
Chris Lattnerb694ba72006-07-02 22:41:36 +0000323 // Mark the file as a system header.
Chris Lattnerd32480d2009-01-17 06:22:33 +0000324 HeaderInfo.MarkFileSystemHeader(TheLexer->getFileEntry());
Mike Stump11289f42009-09-09 15:08:12 +0000325
326
Chris Lattnerd9efb6e2009-06-15 05:02:34 +0000327 PresumedLoc PLoc = SourceMgr.getPresumedLoc(SysHeaderTok.getLocation());
Douglas Gregor453b0122010-11-12 07:15:47 +0000328 if (PLoc.isInvalid())
329 return;
330
Jay Foad9a6b0982011-06-21 15:13:30 +0000331 unsigned FilenameID = SourceMgr.getLineTableFilenameID(PLoc.getFilename());
Mike Stump11289f42009-09-09 15:08:12 +0000332
Chris Lattner3bdc7672011-05-22 22:10:16 +0000333 // Notify the client, if desired, that we are in a new source file.
334 if (Callbacks)
335 Callbacks->FileChanged(SysHeaderTok.getLocation(),
336 PPCallbacks::SystemHeaderPragma, SrcMgr::C_System);
337
Chris Lattnerd9efb6e2009-06-15 05:02:34 +0000338 // Emit a line marker. This will change any source locations from this point
339 // forward to realize they are in a system header.
340 // Create a line note with this information.
341 SourceMgr.AddLineNote(SysHeaderTok.getLocation(), PLoc.getLine(), FilenameID,
342 false, false, true, false);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000343}
344
345/// HandlePragmaDependency - Handle #pragma GCC dependency "foo" blah.
346///
Chris Lattner146762e2007-07-20 16:59:19 +0000347void Preprocessor::HandlePragmaDependency(Token &DependencyTok) {
348 Token FilenameTok;
Ted Kremenek551c82a2008-11-18 01:12:54 +0000349 CurPPLexer->LexIncludeFilename(FilenameTok);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000350
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000351 // If the token kind is EOD, the error has already been diagnosed.
352 if (FilenameTok.is(tok::eod))
Chris Lattnerb694ba72006-07-02 22:41:36 +0000353 return;
Mike Stump11289f42009-09-09 15:08:12 +0000354
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000355 // Reserve a buffer to get the spelling.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000356 SmallString<128> FilenameBuffer;
Douglas Gregordc970f02010-03-16 22:30:13 +0000357 bool Invalid = false;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000358 StringRef Filename = getSpelling(FilenameTok, FilenameBuffer, &Invalid);
Douglas Gregordc970f02010-03-16 22:30:13 +0000359 if (Invalid)
360 return;
Mike Stump11289f42009-09-09 15:08:12 +0000361
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000362 bool isAngled =
363 GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000364 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
365 // error.
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000366 if (Filename.empty())
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000367 return;
Mike Stump11289f42009-09-09 15:08:12 +0000368
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000369 // Search include directories for this file.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000370 const DirectoryLookup *CurDir;
Douglas Gregor97eec242011-09-15 22:00:41 +0000371 const FileEntry *File = LookupFile(Filename, isAngled, 0, CurDir, NULL, NULL,
372 NULL);
Chris Lattner97b8e842008-11-18 08:02:48 +0000373 if (File == 0) {
Eli Friedman3781a362011-08-30 23:07:51 +0000374 if (!SuppressIncludeNotFoundError)
375 Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
Chris Lattner97b8e842008-11-18 08:02:48 +0000376 return;
377 }
Mike Stump11289f42009-09-09 15:08:12 +0000378
Chris Lattnerd32480d2009-01-17 06:22:33 +0000379 const FileEntry *CurFile = getCurrentFileLexer()->getFileEntry();
Chris Lattnerb694ba72006-07-02 22:41:36 +0000380
381 // If this file is older than the file it depends on, emit a diagnostic.
382 if (CurFile && CurFile->getModificationTime() < File->getModificationTime()) {
383 // Lex tokens at the end of the message and include them in the message.
384 std::string Message;
385 Lex(DependencyTok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000386 while (DependencyTok.isNot(tok::eod)) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000387 Message += getSpelling(DependencyTok) + " ";
388 Lex(DependencyTok);
389 }
Mike Stump11289f42009-09-09 15:08:12 +0000390
Chris Lattnerf0b04972010-09-05 23:16:09 +0000391 // Remove the trailing ' ' if present.
392 if (!Message.empty())
393 Message.erase(Message.end()-1);
Chris Lattner97b8e842008-11-18 08:02:48 +0000394 Diag(FilenameTok, diag::pp_out_of_date_dependency) << Message;
Chris Lattnerb694ba72006-07-02 22:41:36 +0000395 }
396}
397
Chris Lattner2ff698d2009-01-16 08:21:25 +0000398/// HandlePragmaComment - Handle the microsoft #pragma comment extension. The
399/// syntax is:
400/// #pragma comment(linker, "foo")
401/// 'linker' is one of five identifiers: compiler, exestr, lib, linker, user.
402/// "foo" is a string, which is fully macro expanded, and permits string
Gabor Greif31a082f2009-03-17 11:39:38 +0000403/// concatenation, embedded escape characters etc. See MSDN for more details.
Chris Lattner2ff698d2009-01-16 08:21:25 +0000404void Preprocessor::HandlePragmaComment(Token &Tok) {
405 SourceLocation CommentLoc = Tok.getLocation();
406 Lex(Tok);
407 if (Tok.isNot(tok::l_paren)) {
408 Diag(CommentLoc, diag::err_pragma_comment_malformed);
409 return;
410 }
Mike Stump11289f42009-09-09 15:08:12 +0000411
Chris Lattner2ff698d2009-01-16 08:21:25 +0000412 // Read the identifier.
413 Lex(Tok);
414 if (Tok.isNot(tok::identifier)) {
415 Diag(CommentLoc, diag::err_pragma_comment_malformed);
416 return;
417 }
Mike Stump11289f42009-09-09 15:08:12 +0000418
Chris Lattner2ff698d2009-01-16 08:21:25 +0000419 // Verify that this is one of the 5 whitelisted options.
420 // FIXME: warn that 'exestr' is deprecated.
421 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000422 if (!II->isStr("compiler") && !II->isStr("exestr") && !II->isStr("lib") &&
Chris Lattner2ff698d2009-01-16 08:21:25 +0000423 !II->isStr("linker") && !II->isStr("user")) {
424 Diag(Tok.getLocation(), diag::err_pragma_comment_unknown_kind);
425 return;
426 }
Mike Stump11289f42009-09-09 15:08:12 +0000427
Chris Lattner262d4e32009-01-16 18:59:23 +0000428 // Read the optional string if present.
Chris Lattner2ff698d2009-01-16 08:21:25 +0000429 Lex(Tok);
Chris Lattner262d4e32009-01-16 18:59:23 +0000430 std::string ArgumentString;
Chris Lattner2ff698d2009-01-16 08:21:25 +0000431 if (Tok.is(tok::comma)) {
Chris Lattner262d4e32009-01-16 18:59:23 +0000432 Lex(Tok); // eat the comma.
Chris Lattner2ff698d2009-01-16 08:21:25 +0000433
434 // We need at least one string.
Chris Lattner504af112009-04-19 23:16:58 +0000435 if (Tok.isNot(tok::string_literal)) {
Chris Lattner2ff698d2009-01-16 08:21:25 +0000436 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
437 return;
438 }
439
440 // String concatenation allows multiple strings, which can even come from
441 // macro expansion.
442 // "foo " "bar" "Baz"
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000443 SmallVector<Token, 4> StrToks;
Chris Lattner504af112009-04-19 23:16:58 +0000444 while (Tok.is(tok::string_literal)) {
Chris Lattner262d4e32009-01-16 18:59:23 +0000445 StrToks.push_back(Tok);
Chris Lattner2ff698d2009-01-16 08:21:25 +0000446 Lex(Tok);
Chris Lattner262d4e32009-01-16 18:59:23 +0000447 }
448
449 // Concatenate and parse the strings.
450 StringLiteralParser Literal(&StrToks[0], StrToks.size(), *this);
Douglas Gregorfb65e592011-07-27 05:40:30 +0000451 assert(Literal.isAscii() && "Didn't allow wide strings in");
Chris Lattner262d4e32009-01-16 18:59:23 +0000452 if (Literal.hadError)
453 return;
454 if (Literal.Pascal) {
455 Diag(StrToks[0].getLocation(), diag::err_pragma_comment_malformed);
456 return;
457 }
458
Jay Foad9a6b0982011-06-21 15:13:30 +0000459 ArgumentString = Literal.GetString();
Chris Lattner2ff698d2009-01-16 08:21:25 +0000460 }
Mike Stump11289f42009-09-09 15:08:12 +0000461
Chris Lattner262d4e32009-01-16 18:59:23 +0000462 // FIXME: If the kind is "compiler" warn if the string is present (it is
463 // ignored).
464 // FIXME: 'lib' requires a comment string.
465 // FIXME: 'linker' requires a comment string, and has a specific list of
466 // things that are allowable.
Mike Stump11289f42009-09-09 15:08:12 +0000467
Chris Lattner2ff698d2009-01-16 08:21:25 +0000468 if (Tok.isNot(tok::r_paren)) {
469 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
470 return;
471 }
Chris Lattner262d4e32009-01-16 18:59:23 +0000472 Lex(Tok); // eat the r_paren.
Chris Lattner2ff698d2009-01-16 08:21:25 +0000473
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000474 if (Tok.isNot(tok::eod)) {
Chris Lattner2ff698d2009-01-16 08:21:25 +0000475 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
476 return;
477 }
Mike Stump11289f42009-09-09 15:08:12 +0000478
Chris Lattner262d4e32009-01-16 18:59:23 +0000479 // If the pragma is lexically sound, notify any interested PPCallbacks.
Chris Lattnerf49775d2009-01-16 19:01:46 +0000480 if (Callbacks)
481 Callbacks->PragmaComment(CommentLoc, II, ArgumentString);
Chris Lattner2ff698d2009-01-16 08:21:25 +0000482}
483
Michael J. Spencera0a820f2010-09-27 06:19:02 +0000484/// HandlePragmaMessage - Handle the microsoft and gcc #pragma message
485/// extension. The syntax is:
486/// #pragma message(string)
487/// OR, in GCC mode:
488/// #pragma message string
489/// string is a string, which is fully macro expanded, and permits string
490/// concatenation, embedded escape characters, etc... See MSDN for more details.
Chris Lattner30c924b2010-06-26 17:11:39 +0000491void Preprocessor::HandlePragmaMessage(Token &Tok) {
492 SourceLocation MessageLoc = Tok.getLocation();
493 Lex(Tok);
Michael J. Spencera0a820f2010-09-27 06:19:02 +0000494 bool ExpectClosingParen = false;
Michael J. Spencer4362a1c2010-09-27 06:34:47 +0000495 switch (Tok.getKind()) {
Michael J. Spencera0a820f2010-09-27 06:19:02 +0000496 case tok::l_paren:
497 // We have a MSVC style pragma message.
498 ExpectClosingParen = true;
499 // Read the string.
500 Lex(Tok);
501 break;
502 case tok::string_literal:
503 // We have a GCC style pragma message, and we just read the string.
504 break;
505 default:
Chris Lattner30c924b2010-06-26 17:11:39 +0000506 Diag(MessageLoc, diag::err_pragma_message_malformed);
507 return;
508 }
Chris Lattner2ff698d2009-01-16 08:21:25 +0000509
Chris Lattner30c924b2010-06-26 17:11:39 +0000510 // We need at least one string.
511 if (Tok.isNot(tok::string_literal)) {
512 Diag(Tok.getLocation(), diag::err_pragma_message_malformed);
513 return;
514 }
515
516 // String concatenation allows multiple strings, which can even come from
517 // macro expansion.
518 // "foo " "bar" "Baz"
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000519 SmallVector<Token, 4> StrToks;
Chris Lattner30c924b2010-06-26 17:11:39 +0000520 while (Tok.is(tok::string_literal)) {
521 StrToks.push_back(Tok);
522 Lex(Tok);
523 }
524
525 // Concatenate and parse the strings.
526 StringLiteralParser Literal(&StrToks[0], StrToks.size(), *this);
Douglas Gregorfb65e592011-07-27 05:40:30 +0000527 assert(Literal.isAscii() && "Didn't allow wide strings in");
Chris Lattner30c924b2010-06-26 17:11:39 +0000528 if (Literal.hadError)
529 return;
530 if (Literal.Pascal) {
531 Diag(StrToks[0].getLocation(), diag::err_pragma_message_malformed);
532 return;
533 }
534
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000535 StringRef MessageString(Literal.GetString());
Chris Lattner30c924b2010-06-26 17:11:39 +0000536
Michael J. Spencera0a820f2010-09-27 06:19:02 +0000537 if (ExpectClosingParen) {
538 if (Tok.isNot(tok::r_paren)) {
539 Diag(Tok.getLocation(), diag::err_pragma_message_malformed);
540 return;
541 }
542 Lex(Tok); // eat the r_paren.
Chris Lattner30c924b2010-06-26 17:11:39 +0000543 }
Chris Lattner30c924b2010-06-26 17:11:39 +0000544
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000545 if (Tok.isNot(tok::eod)) {
Chris Lattner30c924b2010-06-26 17:11:39 +0000546 Diag(Tok.getLocation(), diag::err_pragma_message_malformed);
547 return;
548 }
549
550 // Output the message.
551 Diag(MessageLoc, diag::warn_pragma_message) << MessageString;
552
553 // If the pragma is lexically sound, notify any interested PPCallbacks.
554 if (Callbacks)
555 Callbacks->PragmaMessage(MessageLoc, MessageString);
556}
Chris Lattner2ff698d2009-01-16 08:21:25 +0000557
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000558/// ParsePragmaPushOrPopMacro - Handle parsing of pragma push_macro/pop_macro.
559/// Return the IdentifierInfo* associated with the macro to push or pop.
560IdentifierInfo *Preprocessor::ParsePragmaPushOrPopMacro(Token &Tok) {
561 // Remember the pragma token location.
562 Token PragmaTok = Tok;
563
564 // Read the '('.
565 Lex(Tok);
566 if (Tok.isNot(tok::l_paren)) {
567 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
568 << getSpelling(PragmaTok);
569 return 0;
570 }
571
572 // Read the macro name string.
573 Lex(Tok);
574 if (Tok.isNot(tok::string_literal)) {
575 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
576 << getSpelling(PragmaTok);
577 return 0;
578 }
579
580 // Remember the macro string.
581 std::string StrVal = getSpelling(Tok);
582
583 // Read the ')'.
584 Lex(Tok);
585 if (Tok.isNot(tok::r_paren)) {
586 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
587 << getSpelling(PragmaTok);
588 return 0;
589 }
590
591 assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
592 "Invalid string token!");
593
594 // Create a Token from the string.
595 Token MacroTok;
596 MacroTok.startToken();
Abramo Bagnaraea4f7c72010-12-22 08:23:18 +0000597 MacroTok.setKind(tok::raw_identifier);
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000598 CreateString(&StrVal[1], StrVal.size() - 2, MacroTok);
599
600 // Get the IdentifierInfo of MacroToPushTok.
601 return LookUpIdentifierInfo(MacroTok);
602}
603
604/// HandlePragmaPushMacro - Handle #pragma push_macro.
605/// The syntax is:
606/// #pragma push_macro("macro")
607void Preprocessor::HandlePragmaPushMacro(Token &PushMacroTok) {
608 // Parse the pragma directive and get the macro IdentifierInfo*.
609 IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PushMacroTok);
610 if (!IdentInfo) return;
611
612 // Get the MacroInfo associated with IdentInfo.
613 MacroInfo *MI = getMacroInfo(IdentInfo);
614
615 MacroInfo *MacroCopyToPush = 0;
616 if (MI) {
617 // Make a clone of MI.
618 MacroCopyToPush = CloneMacroInfo(*MI);
619
620 // Allow the original MacroInfo to be redefined later.
621 MI->setIsAllowRedefinitionsWithoutWarning(true);
622 }
623
624 // Push the cloned MacroInfo so we can retrieve it later.
625 PragmaPushMacroInfo[IdentInfo].push_back(MacroCopyToPush);
626}
627
Ted Kremenek6339f1c2010-10-19 17:40:50 +0000628/// HandlePragmaPopMacro - Handle #pragma pop_macro.
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000629/// The syntax is:
630/// #pragma pop_macro("macro")
631void Preprocessor::HandlePragmaPopMacro(Token &PopMacroTok) {
632 SourceLocation MessageLoc = PopMacroTok.getLocation();
633
634 // Parse the pragma directive and get the macro IdentifierInfo*.
635 IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PopMacroTok);
636 if (!IdentInfo) return;
637
638 // Find the vector<MacroInfo*> associated with the macro.
639 llvm::DenseMap<IdentifierInfo*, std::vector<MacroInfo*> >::iterator iter =
640 PragmaPushMacroInfo.find(IdentInfo);
641 if (iter != PragmaPushMacroInfo.end()) {
642 // Release the MacroInfo currently associated with IdentInfo.
643 MacroInfo *CurrentMI = getMacroInfo(IdentInfo);
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000644 if (CurrentMI) {
645 if (CurrentMI->isWarnIfUnused())
646 WarnUnusedMacroLocs.erase(CurrentMI->getDefinitionLoc());
647 ReleaseMacroInfo(CurrentMI);
648 }
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000649
650 // Get the MacroInfo we want to reinstall.
651 MacroInfo *MacroToReInstall = iter->second.back();
652
653 // Reinstall the previously pushed macro.
654 setMacroInfo(IdentInfo, MacroToReInstall);
655
656 // Pop PragmaPushMacroInfo stack.
657 iter->second.pop_back();
658 if (iter->second.size() == 0)
659 PragmaPushMacroInfo.erase(iter);
660 } else {
661 Diag(MessageLoc, diag::warn_pragma_pop_macro_no_push)
662 << IdentInfo->getName();
663 }
664}
Chris Lattnerb694ba72006-07-02 22:41:36 +0000665
Aaron Ballman611306e2012-03-02 22:51:54 +0000666void Preprocessor::HandlePragmaIncludeAlias(Token &Tok) {
667 // We will either get a quoted filename or a bracketed filename, and we
668 // have to track which we got. The first filename is the source name,
669 // and the second name is the mapped filename. If the first is quoted,
670 // the second must be as well (cannot mix and match quotes and brackets).
Aaron Ballman611306e2012-03-02 22:51:54 +0000671
672 // Get the open paren
673 Lex(Tok);
674 if (Tok.isNot(tok::l_paren)) {
675 Diag(Tok, diag::warn_pragma_include_alias_expected) << "(";
676 return;
677 }
678
679 // We expect either a quoted string literal, or a bracketed name
680 Token SourceFilenameTok;
681 CurPPLexer->LexIncludeFilename(SourceFilenameTok);
682 if (SourceFilenameTok.is(tok::eod)) {
683 // The diagnostic has already been handled
684 return;
685 }
686
687 StringRef SourceFileName;
688 SmallString<128> FileNameBuffer;
689 if (SourceFilenameTok.is(tok::string_literal) ||
690 SourceFilenameTok.is(tok::angle_string_literal)) {
691 SourceFileName = getSpelling(SourceFilenameTok, FileNameBuffer);
692 } else if (SourceFilenameTok.is(tok::less)) {
693 // This could be a path instead of just a name
694 FileNameBuffer.push_back('<');
695 SourceLocation End;
696 if (ConcatenateIncludeName(FileNameBuffer, End))
697 return; // Diagnostic already emitted
698 SourceFileName = FileNameBuffer.str();
699 } else {
700 Diag(Tok, diag::warn_pragma_include_alias_expected_filename);
701 return;
702 }
703 FileNameBuffer.clear();
704
705 // Now we expect a comma, followed by another include name
706 Lex(Tok);
707 if (Tok.isNot(tok::comma)) {
708 Diag(Tok, diag::warn_pragma_include_alias_expected) << ",";
709 return;
710 }
711
712 Token ReplaceFilenameTok;
713 CurPPLexer->LexIncludeFilename(ReplaceFilenameTok);
714 if (ReplaceFilenameTok.is(tok::eod)) {
715 // The diagnostic has already been handled
716 return;
717 }
718
719 StringRef ReplaceFileName;
720 if (ReplaceFilenameTok.is(tok::string_literal) ||
721 ReplaceFilenameTok.is(tok::angle_string_literal)) {
722 ReplaceFileName = getSpelling(ReplaceFilenameTok, FileNameBuffer);
723 } else if (ReplaceFilenameTok.is(tok::less)) {
724 // This could be a path instead of just a name
725 FileNameBuffer.push_back('<');
726 SourceLocation End;
727 if (ConcatenateIncludeName(FileNameBuffer, End))
728 return; // Diagnostic already emitted
729 ReplaceFileName = FileNameBuffer.str();
730 } else {
731 Diag(Tok, diag::warn_pragma_include_alias_expected_filename);
732 return;
733 }
734
735 // Finally, we expect the closing paren
736 Lex(Tok);
737 if (Tok.isNot(tok::r_paren)) {
738 Diag(Tok, diag::warn_pragma_include_alias_expected) << ")";
739 return;
740 }
741
742 // Now that we have the source and target filenames, we need to make sure
743 // they're both of the same type (angled vs non-angled)
744 StringRef OriginalSource = SourceFileName;
745
746 bool SourceIsAngled =
747 GetIncludeFilenameSpelling(SourceFilenameTok.getLocation(),
748 SourceFileName);
749 bool ReplaceIsAngled =
750 GetIncludeFilenameSpelling(ReplaceFilenameTok.getLocation(),
751 ReplaceFileName);
752 if (!SourceFileName.empty() && !ReplaceFileName.empty() &&
753 (SourceIsAngled != ReplaceIsAngled)) {
754 unsigned int DiagID;
755 if (SourceIsAngled)
756 DiagID = diag::warn_pragma_include_alias_mismatch_angle;
757 else
758 DiagID = diag::warn_pragma_include_alias_mismatch_quote;
759
760 Diag(SourceFilenameTok.getLocation(), DiagID)
761 << SourceFileName
762 << ReplaceFileName;
763
764 return;
765 }
766
767 // Now we can let the include handler know about this mapping
768 getHeaderSearchInfo().AddIncludeAlias(OriginalSource, ReplaceFileName);
769}
770
Chris Lattnerb694ba72006-07-02 22:41:36 +0000771/// AddPragmaHandler - Add the specified pragma handler to the preprocessor.
772/// If 'Namespace' is non-null, then it is a token required to exist on the
773/// pragma line before the pragma string starts, e.g. "STDC" or "GCC".
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000774void Preprocessor::AddPragmaHandler(StringRef Namespace,
Chris Lattnerb694ba72006-07-02 22:41:36 +0000775 PragmaHandler *Handler) {
776 PragmaNamespace *InsertNS = PragmaHandlers;
Mike Stump11289f42009-09-09 15:08:12 +0000777
Chris Lattnerb694ba72006-07-02 22:41:36 +0000778 // If this is specified to be in a namespace, step down into it.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000779 if (!Namespace.empty()) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000780 // If there is already a pragma handler with the name of this namespace,
781 // we either have an error (directive with the same name as a namespace) or
782 // we already have the namespace to insert into.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000783 if (PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace)) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000784 InsertNS = Existing->getIfNamespace();
785 assert(InsertNS != 0 && "Cannot have a pragma namespace and pragma"
786 " handler with the same name!");
787 } else {
788 // Otherwise, this namespace doesn't exist yet, create and insert the
789 // handler for it.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000790 InsertNS = new PragmaNamespace(Namespace);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000791 PragmaHandlers->AddPragma(InsertNS);
792 }
793 }
Mike Stump11289f42009-09-09 15:08:12 +0000794
Chris Lattnerb694ba72006-07-02 22:41:36 +0000795 // Check to make sure we don't already have a pragma for this identifier.
796 assert(!InsertNS->FindHandler(Handler->getName()) &&
797 "Pragma handler already exists for this identifier!");
798 InsertNS->AddPragma(Handler);
799}
800
Daniel Dunbar40596532008-10-04 19:17:46 +0000801/// RemovePragmaHandler - Remove the specific pragma handler from the
802/// preprocessor. If \arg Namespace is non-null, then it should be the
803/// namespace that \arg Handler was added to. It is an error to remove
804/// a handler that has not been registered.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000805void Preprocessor::RemovePragmaHandler(StringRef Namespace,
Daniel Dunbar40596532008-10-04 19:17:46 +0000806 PragmaHandler *Handler) {
807 PragmaNamespace *NS = PragmaHandlers;
Mike Stump11289f42009-09-09 15:08:12 +0000808
Daniel Dunbar40596532008-10-04 19:17:46 +0000809 // If this is specified to be in a namespace, step down into it.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000810 if (!Namespace.empty()) {
811 PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace);
Daniel Dunbar40596532008-10-04 19:17:46 +0000812 assert(Existing && "Namespace containing handler does not exist!");
813
814 NS = Existing->getIfNamespace();
815 assert(NS && "Invalid namespace, registered as a regular pragma handler!");
816 }
817
818 NS->RemovePragmaHandler(Handler);
Mike Stump11289f42009-09-09 15:08:12 +0000819
Daniel Dunbar40596532008-10-04 19:17:46 +0000820 // If this is a non-default namespace and it is now empty, remove
821 // it.
Argyrios Kyrtzidis7ce75262012-01-06 00:22:09 +0000822 if (NS != PragmaHandlers && NS->IsEmpty()) {
Daniel Dunbar40596532008-10-04 19:17:46 +0000823 PragmaHandlers->RemovePragmaHandler(NS);
Argyrios Kyrtzidis7ce75262012-01-06 00:22:09 +0000824 delete NS;
825 }
Daniel Dunbar40596532008-10-04 19:17:46 +0000826}
827
Peter Collingbourne3bffa522011-02-14 01:42:24 +0000828bool Preprocessor::LexOnOffSwitch(tok::OnOffSwitch &Result) {
829 Token Tok;
830 LexUnexpandedToken(Tok);
831
832 if (Tok.isNot(tok::identifier)) {
833 Diag(Tok, diag::ext_on_off_switch_syntax);
834 return true;
835 }
836 IdentifierInfo *II = Tok.getIdentifierInfo();
837 if (II->isStr("ON"))
838 Result = tok::OOS_ON;
839 else if (II->isStr("OFF"))
840 Result = tok::OOS_OFF;
841 else if (II->isStr("DEFAULT"))
842 Result = tok::OOS_DEFAULT;
843 else {
844 Diag(Tok, diag::ext_on_off_switch_syntax);
845 return true;
846 }
847
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000848 // Verify that this is followed by EOD.
Peter Collingbourne3bffa522011-02-14 01:42:24 +0000849 LexUnexpandedToken(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000850 if (Tok.isNot(tok::eod))
851 Diag(Tok, diag::ext_pragma_syntax_eod);
Peter Collingbourne3bffa522011-02-14 01:42:24 +0000852 return false;
853}
854
Chris Lattnerb694ba72006-07-02 22:41:36 +0000855namespace {
Chris Lattnerc2383312007-12-19 19:38:36 +0000856/// PragmaOnceHandler - "#pragma once" marks the file as atomically included.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000857struct PragmaOnceHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000858 PragmaOnceHandler() : PragmaHandler("once") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000859 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
860 Token &OnceTok) {
Chris Lattnerce2ab6f2009-04-14 05:07:49 +0000861 PP.CheckEndOfDirective("pragma once");
Chris Lattnerb694ba72006-07-02 22:41:36 +0000862 PP.HandlePragmaOnce(OnceTok);
863 }
864};
865
Chris Lattnerc2383312007-12-19 19:38:36 +0000866/// PragmaMarkHandler - "#pragma mark ..." is ignored by the compiler, and the
867/// rest of the line is not lexed.
868struct PragmaMarkHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000869 PragmaMarkHandler() : PragmaHandler("mark") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000870 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
871 Token &MarkTok) {
Chris Lattnerc2383312007-12-19 19:38:36 +0000872 PP.HandlePragmaMark();
873 }
874};
875
876/// PragmaPoisonHandler - "#pragma poison x" marks x as not usable.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000877struct PragmaPoisonHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000878 PragmaPoisonHandler() : PragmaHandler("poison") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000879 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
880 Token &PoisonTok) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000881 PP.HandlePragmaPoison(PoisonTok);
882 }
883};
884
Chris Lattnerc2383312007-12-19 19:38:36 +0000885/// PragmaSystemHeaderHandler - "#pragma system_header" marks the current file
886/// as a system header, which silences warnings in it.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000887struct PragmaSystemHeaderHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000888 PragmaSystemHeaderHandler() : PragmaHandler("system_header") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000889 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
890 Token &SHToken) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000891 PP.HandlePragmaSystemHeader(SHToken);
Chris Lattnerce2ab6f2009-04-14 05:07:49 +0000892 PP.CheckEndOfDirective("pragma");
Chris Lattnerb694ba72006-07-02 22:41:36 +0000893 }
894};
895struct PragmaDependencyHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000896 PragmaDependencyHandler() : PragmaHandler("dependency") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000897 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
898 Token &DepToken) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000899 PP.HandlePragmaDependency(DepToken);
900 }
901};
Mike Stump11289f42009-09-09 15:08:12 +0000902
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000903struct PragmaDebugHandler : public PragmaHandler {
904 PragmaDebugHandler() : PragmaHandler("__debug") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000905 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
906 Token &DepToken) {
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000907 Token Tok;
908 PP.LexUnexpandedToken(Tok);
909 if (Tok.isNot(tok::identifier)) {
Douglas Gregor3cc26482010-08-30 15:15:34 +0000910 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000911 return;
912 }
913 IdentifierInfo *II = Tok.getIdentifierInfo();
914
Daniel Dunbarf2cf3292010-08-17 22:32:48 +0000915 if (II->isStr("assert")) {
David Blaikie83d382b2011-09-23 05:06:16 +0000916 llvm_unreachable("This is an assertion!");
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000917 } else if (II->isStr("crash")) {
Daniel Dunbarf2cf3292010-08-17 22:32:48 +0000918 *(volatile int*) 0x11 = 0;
919 } else if (II->isStr("llvm_fatal_error")) {
920 llvm::report_fatal_error("#pragma clang __debug llvm_fatal_error");
921 } else if (II->isStr("llvm_unreachable")) {
922 llvm_unreachable("#pragma clang __debug llvm_unreachable");
923 } else if (II->isStr("overflow_stack")) {
924 DebugOverflowStack();
Daniel Dunbar211a7872010-08-18 23:09:23 +0000925 } else if (II->isStr("handle_crash")) {
926 llvm::CrashRecoveryContext *CRC =llvm::CrashRecoveryContext::GetCurrent();
927 if (CRC)
928 CRC->HandleCrash();
Daniel Dunbarf2cf3292010-08-17 22:32:48 +0000929 } else {
930 PP.Diag(Tok, diag::warn_pragma_debug_unexpected_command)
931 << II->getName();
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000932 }
933 }
934
Francois Pichet2e11f5d2011-05-25 16:15:03 +0000935// Disable MSVC warning about runtime stack overflow.
936#ifdef _MSC_VER
937 #pragma warning(disable : 4717)
938#endif
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000939 void DebugOverflowStack() {
940 DebugOverflowStack();
941 }
Francois Pichet2e11f5d2011-05-25 16:15:03 +0000942#ifdef _MSC_VER
943 #pragma warning(default : 4717)
944#endif
945
Daniel Dunbarb8068c32010-07-28 15:40:33 +0000946};
947
Chris Lattner504af112009-04-19 23:16:58 +0000948/// PragmaDiagnosticHandler - e.g. '#pragma GCC diagnostic ignored "-Wformat"'
949struct PragmaDiagnosticHandler : public PragmaHandler {
Douglas Gregor3bde9b12011-06-22 19:41:48 +0000950private:
951 const char *Namespace;
Chris Lattnerfb42a182009-07-12 21:18:45 +0000952public:
Douglas Gregor3bde9b12011-06-22 19:41:48 +0000953 explicit PragmaDiagnosticHandler(const char *NS) :
954 PragmaHandler("diagnostic"), Namespace(NS) {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000955 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
956 Token &DiagToken) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000957 SourceLocation DiagLoc = DiagToken.getLocation();
Chris Lattner504af112009-04-19 23:16:58 +0000958 Token Tok;
959 PP.LexUnexpandedToken(Tok);
960 if (Tok.isNot(tok::identifier)) {
Douglas Gregor3cc26482010-08-30 15:15:34 +0000961 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Chris Lattner504af112009-04-19 23:16:58 +0000962 return;
963 }
964 IdentifierInfo *II = Tok.getIdentifierInfo();
Douglas Gregor3bde9b12011-06-22 19:41:48 +0000965 PPCallbacks *Callbacks = PP.getPPCallbacks();
Mike Stump11289f42009-09-09 15:08:12 +0000966
Chris Lattner504af112009-04-19 23:16:58 +0000967 diag::Mapping Map;
968 if (II->isStr("warning"))
969 Map = diag::MAP_WARNING;
970 else if (II->isStr("error"))
971 Map = diag::MAP_ERROR;
972 else if (II->isStr("ignored"))
973 Map = diag::MAP_IGNORE;
974 else if (II->isStr("fatal"))
975 Map = diag::MAP_FATAL;
Douglas Gregor3cc26482010-08-30 15:15:34 +0000976 else if (II->isStr("pop")) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000977 if (!PP.getDiagnostics().popMappings(DiagLoc))
Douglas Gregor3cc26482010-08-30 15:15:34 +0000978 PP.Diag(Tok, diag::warn_pragma_diagnostic_cannot_pop);
Douglas Gregor3bde9b12011-06-22 19:41:48 +0000979 else if (Callbacks)
980 Callbacks->PragmaDiagnosticPop(DiagLoc, Namespace);
Douglas Gregor3cc26482010-08-30 15:15:34 +0000981 return;
982 } else if (II->isStr("push")) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000983 PP.getDiagnostics().pushMappings(DiagLoc);
Douglas Gregor3bde9b12011-06-22 19:41:48 +0000984 if (Callbacks)
985 Callbacks->PragmaDiagnosticPush(DiagLoc, Namespace);
Chris Lattnerfb42a182009-07-12 21:18:45 +0000986 return;
987 } else {
Douglas Gregor3cc26482010-08-30 15:15:34 +0000988 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Chris Lattner504af112009-04-19 23:16:58 +0000989 return;
990 }
Mike Stump11289f42009-09-09 15:08:12 +0000991
Chris Lattner504af112009-04-19 23:16:58 +0000992 PP.LexUnexpandedToken(Tok);
993
994 // We need at least one string.
995 if (Tok.isNot(tok::string_literal)) {
996 PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token);
997 return;
998 }
Mike Stump11289f42009-09-09 15:08:12 +0000999
Chris Lattner504af112009-04-19 23:16:58 +00001000 // String concatenation allows multiple strings, which can even come from
1001 // macro expansion.
1002 // "foo " "bar" "Baz"
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001003 SmallVector<Token, 4> StrToks;
Chris Lattner504af112009-04-19 23:16:58 +00001004 while (Tok.is(tok::string_literal)) {
1005 StrToks.push_back(Tok);
1006 PP.LexUnexpandedToken(Tok);
1007 }
Mike Stump11289f42009-09-09 15:08:12 +00001008
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001009 if (Tok.isNot(tok::eod)) {
Chris Lattner504af112009-04-19 23:16:58 +00001010 PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token);
1011 return;
1012 }
Mike Stump11289f42009-09-09 15:08:12 +00001013
Chris Lattner504af112009-04-19 23:16:58 +00001014 // Concatenate and parse the strings.
1015 StringLiteralParser Literal(&StrToks[0], StrToks.size(), PP);
Douglas Gregorfb65e592011-07-27 05:40:30 +00001016 assert(Literal.isAscii() && "Didn't allow wide strings in");
Chris Lattner504af112009-04-19 23:16:58 +00001017 if (Literal.hadError)
1018 return;
1019 if (Literal.Pascal) {
Douglas Gregor3cc26482010-08-30 15:15:34 +00001020 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Chris Lattner504af112009-04-19 23:16:58 +00001021 return;
1022 }
Chris Lattnerfb42a182009-07-12 21:18:45 +00001023
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001024 StringRef WarningName(Literal.GetString());
Chris Lattner504af112009-04-19 23:16:58 +00001025
1026 if (WarningName.size() < 3 || WarningName[0] != '-' ||
1027 WarningName[1] != 'W') {
1028 PP.Diag(StrToks[0].getLocation(),
1029 diag::warn_pragma_diagnostic_invalid_option);
1030 return;
1031 }
Mike Stump11289f42009-09-09 15:08:12 +00001032
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +00001033 if (PP.getDiagnostics().setDiagnosticGroupMapping(WarningName.substr(2),
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +00001034 Map, DiagLoc))
Chris Lattner504af112009-04-19 23:16:58 +00001035 PP.Diag(StrToks[0].getLocation(),
1036 diag::warn_pragma_diagnostic_unknown_warning) << WarningName;
Douglas Gregor3bde9b12011-06-22 19:41:48 +00001037 else if (Callbacks)
1038 Callbacks->PragmaDiagnostic(DiagLoc, Namespace, Map, WarningName);
Chris Lattner504af112009-04-19 23:16:58 +00001039 }
1040};
Mike Stump11289f42009-09-09 15:08:12 +00001041
Chris Lattner2ff698d2009-01-16 08:21:25 +00001042/// PragmaCommentHandler - "#pragma comment ...".
1043struct PragmaCommentHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001044 PragmaCommentHandler() : PragmaHandler("comment") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +00001045 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1046 Token &CommentTok) {
Chris Lattner2ff698d2009-01-16 08:21:25 +00001047 PP.HandlePragmaComment(CommentTok);
1048 }
1049};
Mike Stump11289f42009-09-09 15:08:12 +00001050
Aaron Ballman611306e2012-03-02 22:51:54 +00001051/// PragmaIncludeAliasHandler - "#pragma include_alias("...")".
1052struct PragmaIncludeAliasHandler : public PragmaHandler {
1053 PragmaIncludeAliasHandler() : PragmaHandler("include_alias") {}
1054 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1055 Token &IncludeAliasTok) {
1056 PP.HandlePragmaIncludeAlias(IncludeAliasTok);
1057 }
1058};
1059
Chris Lattner30c924b2010-06-26 17:11:39 +00001060/// PragmaMessageHandler - "#pragma message("...")".
1061struct PragmaMessageHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001062 PragmaMessageHandler() : PragmaHandler("message") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +00001063 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1064 Token &CommentTok) {
Chris Lattner30c924b2010-06-26 17:11:39 +00001065 PP.HandlePragmaMessage(CommentTok);
1066 }
1067};
1068
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001069/// PragmaPushMacroHandler - "#pragma push_macro" saves the value of the
1070/// macro on the top of the stack.
1071struct PragmaPushMacroHandler : public PragmaHandler {
1072 PragmaPushMacroHandler() : PragmaHandler("push_macro") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +00001073 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1074 Token &PushMacroTok) {
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001075 PP.HandlePragmaPushMacro(PushMacroTok);
1076 }
1077};
1078
1079
1080/// PragmaPopMacroHandler - "#pragma pop_macro" sets the value of the
1081/// macro to the value on the top of the stack.
1082struct PragmaPopMacroHandler : public PragmaHandler {
1083 PragmaPopMacroHandler() : PragmaHandler("pop_macro") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +00001084 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1085 Token &PopMacroTok) {
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001086 PP.HandlePragmaPopMacro(PopMacroTok);
1087 }
1088};
1089
Chris Lattner958ee042009-04-19 21:20:35 +00001090// Pragma STDC implementations.
Chris Lattner02ef4e32009-04-19 21:50:08 +00001091
Chris Lattner958ee042009-04-19 21:20:35 +00001092/// PragmaSTDC_FENV_ACCESSHandler - "#pragma STDC FENV_ACCESS ...".
1093struct PragmaSTDC_FENV_ACCESSHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001094 PragmaSTDC_FENV_ACCESSHandler() : PragmaHandler("FENV_ACCESS") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +00001095 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1096 Token &Tok) {
Peter Collingbourne3bffa522011-02-14 01:42:24 +00001097 tok::OnOffSwitch OOS;
1098 if (PP.LexOnOffSwitch(OOS))
1099 return;
1100 if (OOS == tok::OOS_ON)
Chris Lattnerdf222682009-04-19 21:55:32 +00001101 PP.Diag(Tok, diag::warn_stdc_fenv_access_not_supported);
Chris Lattner958ee042009-04-19 21:20:35 +00001102 }
1103};
Mike Stump11289f42009-09-09 15:08:12 +00001104
Chris Lattner958ee042009-04-19 21:20:35 +00001105/// PragmaSTDC_CX_LIMITED_RANGEHandler - "#pragma STDC CX_LIMITED_RANGE ...".
1106struct PragmaSTDC_CX_LIMITED_RANGEHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001107 PragmaSTDC_CX_LIMITED_RANGEHandler()
1108 : PragmaHandler("CX_LIMITED_RANGE") {}
Douglas Gregorc7d65762010-09-09 22:45:38 +00001109 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1110 Token &Tok) {
Peter Collingbourne3bffa522011-02-14 01:42:24 +00001111 tok::OnOffSwitch OOS;
1112 PP.LexOnOffSwitch(OOS);
Chris Lattner958ee042009-04-19 21:20:35 +00001113 }
1114};
Mike Stump11289f42009-09-09 15:08:12 +00001115
Chris Lattner958ee042009-04-19 21:20:35 +00001116/// PragmaSTDC_UnknownHandler - "#pragma STDC ...".
1117struct PragmaSTDC_UnknownHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001118 PragmaSTDC_UnknownHandler() {}
Douglas Gregorc7d65762010-09-09 22:45:38 +00001119 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1120 Token &UnknownTok) {
Chris Lattner02ef4e32009-04-19 21:50:08 +00001121 // C99 6.10.6p2, unknown forms are not allowed.
Chris Lattnera0b1f762009-04-19 21:25:37 +00001122 PP.Diag(UnknownTok, diag::ext_stdc_pragma_ignored);
Chris Lattner958ee042009-04-19 21:20:35 +00001123 }
1124};
Mike Stump11289f42009-09-09 15:08:12 +00001125
John McCall32f5fe12011-09-30 05:12:12 +00001126/// PragmaARCCFCodeAuditedHandler -
1127/// #pragma clang arc_cf_code_audited begin/end
1128struct PragmaARCCFCodeAuditedHandler : public PragmaHandler {
1129 PragmaARCCFCodeAuditedHandler() : PragmaHandler("arc_cf_code_audited") {}
1130 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1131 Token &NameTok) {
1132 SourceLocation Loc = NameTok.getLocation();
1133 bool IsBegin;
1134
1135 Token Tok;
1136
1137 // Lex the 'begin' or 'end'.
1138 PP.LexUnexpandedToken(Tok);
1139 const IdentifierInfo *BeginEnd = Tok.getIdentifierInfo();
1140 if (BeginEnd && BeginEnd->isStr("begin")) {
1141 IsBegin = true;
1142 } else if (BeginEnd && BeginEnd->isStr("end")) {
1143 IsBegin = false;
1144 } else {
1145 PP.Diag(Tok.getLocation(), diag::err_pp_arc_cf_code_audited_syntax);
1146 return;
1147 }
1148
1149 // Verify that this is followed by EOD.
1150 PP.LexUnexpandedToken(Tok);
1151 if (Tok.isNot(tok::eod))
1152 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
1153
1154 // The start location of the active audit.
1155 SourceLocation BeginLoc = PP.getPragmaARCCFCodeAuditedLoc();
1156
1157 // The start location we want after processing this.
1158 SourceLocation NewLoc;
1159
1160 if (IsBegin) {
1161 // Complain about attempts to re-enter an audit.
1162 if (BeginLoc.isValid()) {
1163 PP.Diag(Loc, diag::err_pp_double_begin_of_arc_cf_code_audited);
1164 PP.Diag(BeginLoc, diag::note_pragma_entered_here);
1165 }
1166 NewLoc = Loc;
1167 } else {
1168 // Complain about attempts to leave an audit that doesn't exist.
1169 if (!BeginLoc.isValid()) {
1170 PP.Diag(Loc, diag::err_pp_unmatched_end_of_arc_cf_code_audited);
1171 return;
1172 }
1173 NewLoc = SourceLocation();
1174 }
1175
1176 PP.setPragmaARCCFCodeAuditedLoc(NewLoc);
1177 }
1178};
1179
Chris Lattnerb694ba72006-07-02 22:41:36 +00001180} // end anonymous namespace
1181
1182
1183/// RegisterBuiltinPragmas - Install the standard preprocessor pragmas:
1184/// #pragma GCC poison/system_header/dependency and #pragma once.
1185void Preprocessor::RegisterBuiltinPragmas() {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001186 AddPragmaHandler(new PragmaOnceHandler());
1187 AddPragmaHandler(new PragmaMarkHandler());
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001188 AddPragmaHandler(new PragmaPushMacroHandler());
1189 AddPragmaHandler(new PragmaPopMacroHandler());
Michael J. Spencera0a820f2010-09-27 06:19:02 +00001190 AddPragmaHandler(new PragmaMessageHandler());
Mike Stump11289f42009-09-09 15:08:12 +00001191
Chris Lattnerb61448d2009-05-12 18:21:11 +00001192 // #pragma GCC ...
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001193 AddPragmaHandler("GCC", new PragmaPoisonHandler());
1194 AddPragmaHandler("GCC", new PragmaSystemHeaderHandler());
1195 AddPragmaHandler("GCC", new PragmaDependencyHandler());
Douglas Gregor3bde9b12011-06-22 19:41:48 +00001196 AddPragmaHandler("GCC", new PragmaDiagnosticHandler("GCC"));
Chris Lattnerb61448d2009-05-12 18:21:11 +00001197 // #pragma clang ...
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001198 AddPragmaHandler("clang", new PragmaPoisonHandler());
1199 AddPragmaHandler("clang", new PragmaSystemHeaderHandler());
Daniel Dunbarb8068c32010-07-28 15:40:33 +00001200 AddPragmaHandler("clang", new PragmaDebugHandler());
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001201 AddPragmaHandler("clang", new PragmaDependencyHandler());
Douglas Gregor3bde9b12011-06-22 19:41:48 +00001202 AddPragmaHandler("clang", new PragmaDiagnosticHandler("clang"));
John McCall32f5fe12011-09-30 05:12:12 +00001203 AddPragmaHandler("clang", new PragmaARCCFCodeAuditedHandler());
Chris Lattnerb61448d2009-05-12 18:21:11 +00001204
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001205 AddPragmaHandler("STDC", new PragmaSTDC_FENV_ACCESSHandler());
1206 AddPragmaHandler("STDC", new PragmaSTDC_CX_LIMITED_RANGEHandler());
Chris Lattner958ee042009-04-19 21:20:35 +00001207 AddPragmaHandler("STDC", new PragmaSTDC_UnknownHandler());
Mike Stump11289f42009-09-09 15:08:12 +00001208
Chris Lattner2ff698d2009-01-16 08:21:25 +00001209 // MS extensions.
Francois Pichet0706d202011-09-17 17:15:52 +00001210 if (Features.MicrosoftExt) {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001211 AddPragmaHandler(new PragmaCommentHandler());
Aaron Ballman611306e2012-03-02 22:51:54 +00001212 AddPragmaHandler(new PragmaIncludeAliasHandler());
Chris Lattner30c924b2010-06-26 17:11:39 +00001213 }
Chris Lattnerb694ba72006-07-02 22:41:36 +00001214}