blob: 5d65cc4f23b125161032128b803012ef33e6d73f [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Pragma.cpp - Pragma registration and handling --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the PragmaHandler/PragmaTable interfaces and implements
11// pragma related methods of the Preprocessor class.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Lex/Pragma.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000016#include "clang/Lex/HeaderSearch.h"
Chris Lattnera9d91452009-01-16 18:59:23 +000017#include "clang/Lex/LiteralSupport.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000018#include "clang/Lex/Preprocessor.h"
Chris Lattnerf47724b2010-08-17 15:55:45 +000019#include "clang/Lex/MacroInfo.h"
Chris Lattner500d3292009-01-29 05:15:15 +000020#include "clang/Lex/LexDiagnostic.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000021#include "clang/Basic/FileManager.h"
22#include "clang/Basic/SourceManager.h"
Daniel Dunbarff759a62010-08-18 23:09:23 +000023#include "llvm/Support/CrashRecoveryContext.h"
Daniel Dunbar55054132010-08-17 22:32:48 +000024#include "llvm/Support/ErrorHandling.h"
Douglas Gregor2e222532009-07-02 17:08:52 +000025#include <algorithm>
Reid Spencer5f016e22007-07-11 17:01:13 +000026using namespace clang;
27
28// Out-of-line destructor to provide a home for the class.
29PragmaHandler::~PragmaHandler() {
30}
31
32//===----------------------------------------------------------------------===//
Daniel Dunbarc72cc502010-06-11 20:10:12 +000033// EmptyPragmaHandler Implementation.
34//===----------------------------------------------------------------------===//
35
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +000036EmptyPragmaHandler::EmptyPragmaHandler() {}
Daniel Dunbarc72cc502010-06-11 20:10:12 +000037
Douglas Gregor80c60f72010-09-09 22:45:38 +000038void EmptyPragmaHandler::HandlePragma(Preprocessor &PP,
39 PragmaIntroducerKind Introducer,
40 Token &FirstToken) {}
Daniel Dunbarc72cc502010-06-11 20:10:12 +000041
42//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +000043// PragmaNamespace Implementation.
44//===----------------------------------------------------------------------===//
45
46
47PragmaNamespace::~PragmaNamespace() {
Argyrios Kyrtzidis9b36c3f2010-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;
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner5f9e2722011-07-23 10:55:15 +000057PragmaHandler *PragmaNamespace::FindHandler(StringRef Name,
Reid Spencer5f016e22007-07-11 17:01:13 +000058 bool IgnoreNull) const {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +000059 if (PragmaHandler *Handler = Handlers.lookup(Name))
60 return Handler;
Chris Lattner5f9e2722011-07-23 10:55:15 +000061 return IgnoreNull ? 0 : Handlers.lookup(StringRef());
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +000062}
Mike Stump1eb44332009-09-09 15:08:12 +000063
Argyrios Kyrtzidis9b36c3f2010-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);
Reid Spencer5f016e22007-07-11 17:01:13 +000070}
71
Daniel Dunbar40950802008-10-04 19:17:46 +000072void PragmaNamespace::RemovePragmaHandler(PragmaHandler *Handler) {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +000073 assert(Handlers.lookup(Handler->getName()) &&
74 "Handler not registered in this namespace");
75 Handlers.erase(Handler->getName());
Daniel Dunbar40950802008-10-04 19:17:46 +000076}
77
Douglas Gregor80c60f72010-09-09 22:45:38 +000078void PragmaNamespace::HandlePragma(Preprocessor &PP,
79 PragmaIntroducerKind Introducer,
80 Token &Tok) {
Reid Spencer5f016e22007-07-11 17:01:13 +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 Stump1eb44332009-09-09 15:08:12 +000084
Reid Spencer5f016e22007-07-11 17:01:13 +000085 // Get the handler for this token. If there is no handler, ignore the pragma.
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +000086 PragmaHandler *Handler
87 = FindHandler(Tok.getIdentifierInfo() ? Tok.getIdentifierInfo()->getName()
Chris Lattner5f9e2722011-07-23 10:55:15 +000088 : StringRef(),
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +000089 /*IgnoreNull=*/false);
Chris Lattneraf7cdf42009-04-19 21:10:26 +000090 if (Handler == 0) {
91 PP.Diag(Tok, diag::warn_pragma_ignored);
92 return;
93 }
Mike Stump1eb44332009-09-09 15:08:12 +000094
Reid Spencer5f016e22007-07-11 17:01:13 +000095 // Otherwise, pass it down.
Douglas Gregor80c60f72010-09-09 22:45:38 +000096 Handler->HandlePragma(PP, Introducer, Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +000097}
98
99//===----------------------------------------------------------------------===//
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 Gregor80c60f72010-09-09 22:45:38 +0000105void Preprocessor::HandlePragmaDirective(unsigned Introducer) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000106 ++NumPragma;
Mike Stump1eb44332009-09-09 15:08:12 +0000107
Reid Spencer5f016e22007-07-11 17:01:13 +0000108 // Invoke the first level of pragma handlers which reads the namespace id.
Chris Lattnerd2177732007-07-20 16:59:19 +0000109 Token Tok;
Douglas Gregor80c60f72010-09-09 22:45:38 +0000110 PragmaHandlers->HandlePragma(*this, PragmaIntroducerKind(Introducer), Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000111
Reid Spencer5f016e22007-07-11 17:01:13 +0000112 // If the pragma handler didn't read the rest of the line, consume it now.
Peter Collingbourneb2eb53d2011-02-22 13:49:00 +0000113 if ((CurTokenLexer && CurTokenLexer->isParsingPreprocessorDirective())
114 || (CurPPLexer && CurPPLexer->ParsingPreprocessorDirective))
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattnerd2177732007-07-20 16:59:19 +0000121void Preprocessor::Handle_Pragma(Token &Tok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000122 // Remember the pragma token location.
123 SourceLocation PragmaLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000124
Reid Spencer5f016e22007-07-11 17:01:13 +0000125 // Read the '('.
126 Lex(Tok);
Chris Lattner3692b092008-11-18 07:59:24 +0000127 if (Tok.isNot(tok::l_paren)) {
128 Diag(PragmaLoc, diag::err__Pragma_malformed);
129 return;
130 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000131
132 // Read the '"..."'.
133 Lex(Tok);
Chris Lattner3692b092008-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);
Richard Smith99831e42012-03-06 03:21:47 +0000136 // Skip this token, and the ')', if present.
137 if (Tok.isNot(tok::r_paren))
138 Lex(Tok);
139 if (Tok.is(tok::r_paren))
140 Lex(Tok);
141 return;
142 }
143
144 if (Tok.hasUDSuffix()) {
145 Diag(Tok, diag::err_invalid_string_udl);
146 // Skip this token, and the ')', if present.
147 Lex(Tok);
148 if (Tok.is(tok::r_paren))
149 Lex(Tok);
Chris Lattner3692b092008-11-18 07:59:24 +0000150 return;
151 }
Mike Stump1eb44332009-09-09 15:08:12 +0000152
Reid Spencer5f016e22007-07-11 17:01:13 +0000153 // Remember the string.
154 std::string StrVal = getSpelling(Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000155
156 // Read the ')'.
157 Lex(Tok);
Chris Lattner3692b092008-11-18 07:59:24 +0000158 if (Tok.isNot(tok::r_paren)) {
159 Diag(PragmaLoc, diag::err__Pragma_malformed);
160 return;
161 }
Mike Stump1eb44332009-09-09 15:08:12 +0000162
Chris Lattnere7fb4842009-02-15 20:52:18 +0000163 SourceLocation RParenLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000164
Chris Lattnera9d91452009-01-16 18:59:23 +0000165 // The _Pragma is lexically sound. Destringize according to C99 6.10.9.1:
166 // "The string literal is destringized by deleting the L prefix, if present,
167 // deleting the leading and trailing double-quotes, replacing each escape
168 // sequence \" by a double-quote, and replacing each escape sequence \\ by a
169 // single backslash."
Reid Spencer5f016e22007-07-11 17:01:13 +0000170 if (StrVal[0] == 'L') // Remove L prefix.
171 StrVal.erase(StrVal.begin());
172 assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
173 "Invalid string token!");
Mike Stump1eb44332009-09-09 15:08:12 +0000174
Reid Spencer5f016e22007-07-11 17:01:13 +0000175 // Remove the front quote, replacing it with a space, so that the pragma
176 // contents appear to have a space before them.
177 StrVal[0] = ' ';
Mike Stump1eb44332009-09-09 15:08:12 +0000178
Chris Lattner1fa49532009-03-08 08:08:45 +0000179 // Replace the terminating quote with a \n.
Reid Spencer5f016e22007-07-11 17:01:13 +0000180 StrVal[StrVal.size()-1] = '\n';
Mike Stump1eb44332009-09-09 15:08:12 +0000181
Reid Spencer5f016e22007-07-11 17:01:13 +0000182 // Remove escaped quotes and escapes.
183 for (unsigned i = 0, e = StrVal.size(); i != e-1; ++i) {
184 if (StrVal[i] == '\\' &&
185 (StrVal[i+1] == '\\' || StrVal[i+1] == '"')) {
186 // \\ -> '\' and \" -> '"'.
187 StrVal.erase(StrVal.begin()+i);
188 --e;
189 }
190 }
John McCall1ef8a2e2010-08-28 22:34:47 +0000191
Peter Collingbournea5ef5842011-02-22 13:49:06 +0000192 // Plop the string (including the newline and trailing null) into a buffer
193 // where we can lex it.
194 Token TmpTok;
195 TmpTok.startToken();
196 CreateString(&StrVal[0], StrVal.size(), TmpTok);
197 SourceLocation TokLoc = TmpTok.getLocation();
198
199 // Make and enter a lexer object so that we lex and expand the tokens just
200 // like any others.
201 Lexer *TL = Lexer::Create_PragmaLexer(TokLoc, PragmaLoc, RParenLoc,
202 StrVal.size(), *this);
203
204 EnterSourceFileWithLexer(TL, 0);
205
206 // With everything set up, lex this as a #pragma directive.
207 HandlePragmaDirective(PIK__Pragma);
John McCall1ef8a2e2010-08-28 22:34:47 +0000208
209 // Finally, return whatever came after the pragma directive.
210 return Lex(Tok);
211}
212
213/// HandleMicrosoft__pragma - Like Handle_Pragma except the pragma text
214/// is not enclosed within a string literal.
215void Preprocessor::HandleMicrosoft__pragma(Token &Tok) {
216 // Remember the pragma token location.
217 SourceLocation PragmaLoc = Tok.getLocation();
218
219 // Read the '('.
220 Lex(Tok);
221 if (Tok.isNot(tok::l_paren)) {
222 Diag(PragmaLoc, diag::err__Pragma_malformed);
223 return;
224 }
225
Peter Collingbournea5ef5842011-02-22 13:49:06 +0000226 // Get the tokens enclosed within the __pragma(), as well as the final ')'.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000227 SmallVector<Token, 32> PragmaToks;
John McCall1ef8a2e2010-08-28 22:34:47 +0000228 int NumParens = 0;
229 Lex(Tok);
230 while (Tok.isNot(tok::eof)) {
Peter Collingbournea5ef5842011-02-22 13:49:06 +0000231 PragmaToks.push_back(Tok);
John McCall1ef8a2e2010-08-28 22:34:47 +0000232 if (Tok.is(tok::l_paren))
233 NumParens++;
234 else if (Tok.is(tok::r_paren) && NumParens-- == 0)
235 break;
John McCall1ef8a2e2010-08-28 22:34:47 +0000236 Lex(Tok);
237 }
238
John McCall3da92a92010-08-29 01:09:54 +0000239 if (Tok.is(tok::eof)) {
240 Diag(PragmaLoc, diag::err_unterminated___pragma);
241 return;
242 }
243
Peter Collingbournea5ef5842011-02-22 13:49:06 +0000244 PragmaToks.front().setFlag(Token::LeadingSpace);
John McCall1ef8a2e2010-08-28 22:34:47 +0000245
Peter Collingbourne84021552011-02-28 02:37:51 +0000246 // Replace the ')' with an EOD to mark the end of the pragma.
247 PragmaToks.back().setKind(tok::eod);
Peter Collingbournea5ef5842011-02-22 13:49:06 +0000248
249 Token *TokArray = new Token[PragmaToks.size()];
250 std::copy(PragmaToks.begin(), PragmaToks.end(), TokArray);
251
252 // Push the tokens onto the stack.
253 EnterTokenStream(TokArray, PragmaToks.size(), true, true);
254
255 // With everything set up, lex this as a #pragma directive.
256 HandlePragmaDirective(PIK___pragma);
John McCall1ef8a2e2010-08-28 22:34:47 +0000257
258 // Finally, return whatever came after the pragma directive.
259 return Lex(Tok);
260}
261
Reid Spencer5f016e22007-07-11 17:01:13 +0000262/// HandlePragmaOnce - Handle #pragma once. OnceTok is the 'once'.
263///
Chris Lattnerd2177732007-07-20 16:59:19 +0000264void Preprocessor::HandlePragmaOnce(Token &OnceTok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000265 if (isInPrimaryFile()) {
266 Diag(OnceTok, diag::pp_pragma_once_in_main_file);
267 return;
268 }
Mike Stump1eb44332009-09-09 15:08:12 +0000269
Reid Spencer5f016e22007-07-11 17:01:13 +0000270 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
Reid Spencer5f016e22007-07-11 17:01:13 +0000271 // Mark the file as a once-only file now.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000272 HeaderInfo.MarkFileIncludeOnce(getCurrentFileLexer()->getFileEntry());
Reid Spencer5f016e22007-07-11 17:01:13 +0000273}
274
Chris Lattner22434492007-12-19 19:38:36 +0000275void Preprocessor::HandlePragmaMark() {
Ted Kremenek17ff58a2008-11-19 22:21:33 +0000276 assert(CurPPLexer && "No current lexer?");
Chris Lattner6896a372009-06-15 05:02:34 +0000277 if (CurLexer)
278 CurLexer->ReadToEndOfLine();
279 else
280 CurPTHLexer->DiscardToEndOfLine();
Chris Lattner22434492007-12-19 19:38:36 +0000281}
282
283
Reid Spencer5f016e22007-07-11 17:01:13 +0000284/// HandlePragmaPoison - Handle #pragma GCC poison. PoisonTok is the 'poison'.
285///
Chris Lattnerd2177732007-07-20 16:59:19 +0000286void Preprocessor::HandlePragmaPoison(Token &PoisonTok) {
287 Token Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000288
289 while (1) {
290 // Read the next token to poison. While doing this, pretend that we are
291 // skipping while reading the identifier to poison.
292 // This avoids errors on code like:
293 // #pragma GCC poison X
294 // #pragma GCC poison X
Ted Kremenek68a91d52008-11-18 01:12:54 +0000295 if (CurPPLexer) CurPPLexer->LexingRawMode = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000296 LexUnexpandedToken(Tok);
Ted Kremenek68a91d52008-11-18 01:12:54 +0000297 if (CurPPLexer) CurPPLexer->LexingRawMode = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000298
Reid Spencer5f016e22007-07-11 17:01:13 +0000299 // If we reached the end of line, we're done.
Peter Collingbourne84021552011-02-28 02:37:51 +0000300 if (Tok.is(tok::eod)) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000301
Reid Spencer5f016e22007-07-11 17:01:13 +0000302 // Can only poison identifiers.
Abramo Bagnarac4bf2b92010-12-22 08:23:18 +0000303 if (Tok.isNot(tok::raw_identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000304 Diag(Tok, diag::err_pp_invalid_poison);
305 return;
306 }
Mike Stump1eb44332009-09-09 15:08:12 +0000307
Reid Spencer5f016e22007-07-11 17:01:13 +0000308 // Look up the identifier info for the token. We disabled identifier lookup
309 // by saying we're skipping contents, so we need to do this manually.
310 IdentifierInfo *II = LookUpIdentifierInfo(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000311
Reid Spencer5f016e22007-07-11 17:01:13 +0000312 // Already poisoned.
313 if (II->isPoisoned()) continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000314
Reid Spencer5f016e22007-07-11 17:01:13 +0000315 // If this is a macro identifier, emit a warning.
Chris Lattner0edde552007-10-07 08:04:56 +0000316 if (II->hasMacroDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000317 Diag(Tok, diag::pp_poisoning_existing_macro);
Mike Stump1eb44332009-09-09 15:08:12 +0000318
Reid Spencer5f016e22007-07-11 17:01:13 +0000319 // Finally, poison it!
320 II->setIsPoisoned();
Douglas Gregoreee242f2011-10-27 09:33:13 +0000321 if (II->isFromAST())
322 II->setChangedSinceDeserialization();
Reid Spencer5f016e22007-07-11 17:01:13 +0000323 }
324}
325
326/// HandlePragmaSystemHeader - Implement #pragma GCC system_header. We know
327/// that the whole directive has been parsed.
Chris Lattnerd2177732007-07-20 16:59:19 +0000328void Preprocessor::HandlePragmaSystemHeader(Token &SysHeaderTok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000329 if (isInPrimaryFile()) {
330 Diag(SysHeaderTok, diag::pp_pragma_sysheader_in_main_file);
331 return;
332 }
Mike Stump1eb44332009-09-09 15:08:12 +0000333
Reid Spencer5f016e22007-07-11 17:01:13 +0000334 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
Ted Kremenek35c10c22008-11-20 01:45:11 +0000335 PreprocessorLexer *TheLexer = getCurrentFileLexer();
Mike Stump1eb44332009-09-09 15:08:12 +0000336
Reid Spencer5f016e22007-07-11 17:01:13 +0000337 // Mark the file as a system header.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000338 HeaderInfo.MarkFileSystemHeader(TheLexer->getFileEntry());
Mike Stump1eb44332009-09-09 15:08:12 +0000339
340
Chris Lattner6896a372009-06-15 05:02:34 +0000341 PresumedLoc PLoc = SourceMgr.getPresumedLoc(SysHeaderTok.getLocation());
Douglas Gregorcb7b1e12010-11-12 07:15:47 +0000342 if (PLoc.isInvalid())
343 return;
344
Jay Foad65aa6882011-06-21 15:13:30 +0000345 unsigned FilenameID = SourceMgr.getLineTableFilenameID(PLoc.getFilename());
Mike Stump1eb44332009-09-09 15:08:12 +0000346
Chris Lattner784c2572011-05-22 22:10:16 +0000347 // Notify the client, if desired, that we are in a new source file.
348 if (Callbacks)
349 Callbacks->FileChanged(SysHeaderTok.getLocation(),
350 PPCallbacks::SystemHeaderPragma, SrcMgr::C_System);
351
Chris Lattner6896a372009-06-15 05:02:34 +0000352 // Emit a line marker. This will change any source locations from this point
353 // forward to realize they are in a system header.
354 // Create a line note with this information.
355 SourceMgr.AddLineNote(SysHeaderTok.getLocation(), PLoc.getLine(), FilenameID,
356 false, false, true, false);
Reid Spencer5f016e22007-07-11 17:01:13 +0000357}
358
359/// HandlePragmaDependency - Handle #pragma GCC dependency "foo" blah.
360///
Chris Lattnerd2177732007-07-20 16:59:19 +0000361void Preprocessor::HandlePragmaDependency(Token &DependencyTok) {
362 Token FilenameTok;
Ted Kremenek68a91d52008-11-18 01:12:54 +0000363 CurPPLexer->LexIncludeFilename(FilenameTok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000364
Peter Collingbourne84021552011-02-28 02:37:51 +0000365 // If the token kind is EOD, the error has already been diagnosed.
366 if (FilenameTok.is(tok::eod))
Reid Spencer5f016e22007-07-11 17:01:13 +0000367 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000368
Reid Spencer5f016e22007-07-11 17:01:13 +0000369 // Reserve a buffer to get the spelling.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000370 SmallString<128> FilenameBuffer;
Douglas Gregor453091c2010-03-16 22:30:13 +0000371 bool Invalid = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000372 StringRef Filename = getSpelling(FilenameTok, FilenameBuffer, &Invalid);
Douglas Gregor453091c2010-03-16 22:30:13 +0000373 if (Invalid)
374 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000375
Chris Lattnera1394812010-01-10 01:35:12 +0000376 bool isAngled =
377 GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
Reid Spencer5f016e22007-07-11 17:01:13 +0000378 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
379 // error.
Chris Lattnera1394812010-01-10 01:35:12 +0000380 if (Filename.empty())
Reid Spencer5f016e22007-07-11 17:01:13 +0000381 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000382
Reid Spencer5f016e22007-07-11 17:01:13 +0000383 // Search include directories for this file.
384 const DirectoryLookup *CurDir;
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000385 const FileEntry *File = LookupFile(Filename, isAngled, 0, CurDir, NULL, NULL,
386 NULL);
Chris Lattner56b05c82008-11-18 08:02:48 +0000387 if (File == 0) {
Eli Friedmanf84139a2011-08-30 23:07:51 +0000388 if (!SuppressIncludeNotFoundError)
389 Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
Chris Lattner56b05c82008-11-18 08:02:48 +0000390 return;
391 }
Mike Stump1eb44332009-09-09 15:08:12 +0000392
Chris Lattner2b2453a2009-01-17 06:22:33 +0000393 const FileEntry *CurFile = getCurrentFileLexer()->getFileEntry();
Reid Spencer5f016e22007-07-11 17:01:13 +0000394
395 // If this file is older than the file it depends on, emit a diagnostic.
396 if (CurFile && CurFile->getModificationTime() < File->getModificationTime()) {
397 // Lex tokens at the end of the message and include them in the message.
398 std::string Message;
399 Lex(DependencyTok);
Peter Collingbourne84021552011-02-28 02:37:51 +0000400 while (DependencyTok.isNot(tok::eod)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000401 Message += getSpelling(DependencyTok) + " ";
402 Lex(DependencyTok);
403 }
Mike Stump1eb44332009-09-09 15:08:12 +0000404
Chris Lattner96de2592010-09-05 23:16:09 +0000405 // Remove the trailing ' ' if present.
406 if (!Message.empty())
407 Message.erase(Message.end()-1);
Chris Lattner56b05c82008-11-18 08:02:48 +0000408 Diag(FilenameTok, diag::pp_out_of_date_dependency) << Message;
Reid Spencer5f016e22007-07-11 17:01:13 +0000409 }
410}
411
Chris Lattner636c5ef2009-01-16 08:21:25 +0000412/// HandlePragmaComment - Handle the microsoft #pragma comment extension. The
413/// syntax is:
414/// #pragma comment(linker, "foo")
415/// 'linker' is one of five identifiers: compiler, exestr, lib, linker, user.
416/// "foo" is a string, which is fully macro expanded, and permits string
Gabor Greifd7ee3492009-03-17 11:39:38 +0000417/// concatenation, embedded escape characters etc. See MSDN for more details.
Chris Lattner636c5ef2009-01-16 08:21:25 +0000418void Preprocessor::HandlePragmaComment(Token &Tok) {
419 SourceLocation CommentLoc = Tok.getLocation();
420 Lex(Tok);
421 if (Tok.isNot(tok::l_paren)) {
422 Diag(CommentLoc, diag::err_pragma_comment_malformed);
423 return;
424 }
Mike Stump1eb44332009-09-09 15:08:12 +0000425
Chris Lattner636c5ef2009-01-16 08:21:25 +0000426 // Read the identifier.
427 Lex(Tok);
428 if (Tok.isNot(tok::identifier)) {
429 Diag(CommentLoc, diag::err_pragma_comment_malformed);
430 return;
431 }
Mike Stump1eb44332009-09-09 15:08:12 +0000432
Chris Lattner636c5ef2009-01-16 08:21:25 +0000433 // Verify that this is one of the 5 whitelisted options.
434 // FIXME: warn that 'exestr' is deprecated.
435 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000436 if (!II->isStr("compiler") && !II->isStr("exestr") && !II->isStr("lib") &&
Chris Lattner636c5ef2009-01-16 08:21:25 +0000437 !II->isStr("linker") && !II->isStr("user")) {
438 Diag(Tok.getLocation(), diag::err_pragma_comment_unknown_kind);
439 return;
440 }
Mike Stump1eb44332009-09-09 15:08:12 +0000441
Chris Lattnera9d91452009-01-16 18:59:23 +0000442 // Read the optional string if present.
Chris Lattner636c5ef2009-01-16 08:21:25 +0000443 Lex(Tok);
Chris Lattnera9d91452009-01-16 18:59:23 +0000444 std::string ArgumentString;
Chris Lattner636c5ef2009-01-16 08:21:25 +0000445 if (Tok.is(tok::comma)) {
Chris Lattnera9d91452009-01-16 18:59:23 +0000446 Lex(Tok); // eat the comma.
Chris Lattner636c5ef2009-01-16 08:21:25 +0000447
448 // We need at least one string.
Chris Lattneredaf8772009-04-19 23:16:58 +0000449 if (Tok.isNot(tok::string_literal)) {
Chris Lattner636c5ef2009-01-16 08:21:25 +0000450 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
451 return;
452 }
453
454 // String concatenation allows multiple strings, which can even come from
455 // macro expansion.
456 // "foo " "bar" "Baz"
Chris Lattner5f9e2722011-07-23 10:55:15 +0000457 SmallVector<Token, 4> StrToks;
Chris Lattneredaf8772009-04-19 23:16:58 +0000458 while (Tok.is(tok::string_literal)) {
Richard Smith99831e42012-03-06 03:21:47 +0000459 if (Tok.hasUDSuffix())
460 Diag(Tok, diag::err_invalid_string_udl);
Chris Lattnera9d91452009-01-16 18:59:23 +0000461 StrToks.push_back(Tok);
Chris Lattner636c5ef2009-01-16 08:21:25 +0000462 Lex(Tok);
Chris Lattnera9d91452009-01-16 18:59:23 +0000463 }
464
465 // Concatenate and parse the strings.
466 StringLiteralParser Literal(&StrToks[0], StrToks.size(), *this);
Douglas Gregor5cee1192011-07-27 05:40:30 +0000467 assert(Literal.isAscii() && "Didn't allow wide strings in");
Chris Lattnera9d91452009-01-16 18:59:23 +0000468 if (Literal.hadError)
469 return;
470 if (Literal.Pascal) {
471 Diag(StrToks[0].getLocation(), diag::err_pragma_comment_malformed);
472 return;
473 }
474
Jay Foad65aa6882011-06-21 15:13:30 +0000475 ArgumentString = Literal.GetString();
Chris Lattner636c5ef2009-01-16 08:21:25 +0000476 }
Mike Stump1eb44332009-09-09 15:08:12 +0000477
Chris Lattnera9d91452009-01-16 18:59:23 +0000478 // FIXME: If the kind is "compiler" warn if the string is present (it is
479 // ignored).
480 // FIXME: 'lib' requires a comment string.
481 // FIXME: 'linker' requires a comment string, and has a specific list of
482 // things that are allowable.
Mike Stump1eb44332009-09-09 15:08:12 +0000483
Chris Lattner636c5ef2009-01-16 08:21:25 +0000484 if (Tok.isNot(tok::r_paren)) {
485 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
486 return;
487 }
Chris Lattnera9d91452009-01-16 18:59:23 +0000488 Lex(Tok); // eat the r_paren.
Chris Lattner636c5ef2009-01-16 08:21:25 +0000489
Peter Collingbourne84021552011-02-28 02:37:51 +0000490 if (Tok.isNot(tok::eod)) {
Chris Lattner636c5ef2009-01-16 08:21:25 +0000491 Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
492 return;
493 }
Mike Stump1eb44332009-09-09 15:08:12 +0000494
Chris Lattnera9d91452009-01-16 18:59:23 +0000495 // If the pragma is lexically sound, notify any interested PPCallbacks.
Chris Lattner172e3362009-01-16 19:01:46 +0000496 if (Callbacks)
497 Callbacks->PragmaComment(CommentLoc, II, ArgumentString);
Chris Lattner636c5ef2009-01-16 08:21:25 +0000498}
499
Michael J. Spencer301669b2010-09-27 06:19:02 +0000500/// HandlePragmaMessage - Handle the microsoft and gcc #pragma message
501/// extension. The syntax is:
502/// #pragma message(string)
503/// OR, in GCC mode:
504/// #pragma message string
505/// string is a string, which is fully macro expanded, and permits string
506/// concatenation, embedded escape characters, etc... See MSDN for more details.
Chris Lattnerabfe0942010-06-26 17:11:39 +0000507void Preprocessor::HandlePragmaMessage(Token &Tok) {
508 SourceLocation MessageLoc = Tok.getLocation();
509 Lex(Tok);
Michael J. Spencer301669b2010-09-27 06:19:02 +0000510 bool ExpectClosingParen = false;
Michael J. Spencerd83fc542010-09-27 06:34:47 +0000511 switch (Tok.getKind()) {
Michael J. Spencer301669b2010-09-27 06:19:02 +0000512 case tok::l_paren:
513 // We have a MSVC style pragma message.
514 ExpectClosingParen = true;
515 // Read the string.
516 Lex(Tok);
517 break;
518 case tok::string_literal:
519 // We have a GCC style pragma message, and we just read the string.
520 break;
521 default:
Chris Lattnerabfe0942010-06-26 17:11:39 +0000522 Diag(MessageLoc, diag::err_pragma_message_malformed);
523 return;
524 }
Chris Lattner636c5ef2009-01-16 08:21:25 +0000525
Chris Lattnerabfe0942010-06-26 17:11:39 +0000526 // We need at least one string.
527 if (Tok.isNot(tok::string_literal)) {
528 Diag(Tok.getLocation(), diag::err_pragma_message_malformed);
529 return;
530 }
531
532 // String concatenation allows multiple strings, which can even come from
533 // macro expansion.
534 // "foo " "bar" "Baz"
Chris Lattner5f9e2722011-07-23 10:55:15 +0000535 SmallVector<Token, 4> StrToks;
Chris Lattnerabfe0942010-06-26 17:11:39 +0000536 while (Tok.is(tok::string_literal)) {
Richard Smith99831e42012-03-06 03:21:47 +0000537 if (Tok.hasUDSuffix())
538 Diag(Tok, diag::err_invalid_string_udl);
Chris Lattnerabfe0942010-06-26 17:11:39 +0000539 StrToks.push_back(Tok);
540 Lex(Tok);
541 }
542
543 // Concatenate and parse the strings.
544 StringLiteralParser Literal(&StrToks[0], StrToks.size(), *this);
Douglas Gregor5cee1192011-07-27 05:40:30 +0000545 assert(Literal.isAscii() && "Didn't allow wide strings in");
Chris Lattnerabfe0942010-06-26 17:11:39 +0000546 if (Literal.hadError)
547 return;
548 if (Literal.Pascal) {
549 Diag(StrToks[0].getLocation(), diag::err_pragma_message_malformed);
550 return;
551 }
552
Chris Lattner5f9e2722011-07-23 10:55:15 +0000553 StringRef MessageString(Literal.GetString());
Chris Lattnerabfe0942010-06-26 17:11:39 +0000554
Michael J. Spencer301669b2010-09-27 06:19:02 +0000555 if (ExpectClosingParen) {
556 if (Tok.isNot(tok::r_paren)) {
557 Diag(Tok.getLocation(), diag::err_pragma_message_malformed);
558 return;
559 }
560 Lex(Tok); // eat the r_paren.
Chris Lattnerabfe0942010-06-26 17:11:39 +0000561 }
Chris Lattnerabfe0942010-06-26 17:11:39 +0000562
Peter Collingbourne84021552011-02-28 02:37:51 +0000563 if (Tok.isNot(tok::eod)) {
Chris Lattnerabfe0942010-06-26 17:11:39 +0000564 Diag(Tok.getLocation(), diag::err_pragma_message_malformed);
565 return;
566 }
567
568 // Output the message.
569 Diag(MessageLoc, diag::warn_pragma_message) << MessageString;
570
571 // If the pragma is lexically sound, notify any interested PPCallbacks.
572 if (Callbacks)
573 Callbacks->PragmaMessage(MessageLoc, MessageString);
574}
Chris Lattner636c5ef2009-01-16 08:21:25 +0000575
Chris Lattnerf47724b2010-08-17 15:55:45 +0000576/// ParsePragmaPushOrPopMacro - Handle parsing of pragma push_macro/pop_macro.
577/// Return the IdentifierInfo* associated with the macro to push or pop.
578IdentifierInfo *Preprocessor::ParsePragmaPushOrPopMacro(Token &Tok) {
579 // Remember the pragma token location.
580 Token PragmaTok = Tok;
581
582 // Read the '('.
583 Lex(Tok);
584 if (Tok.isNot(tok::l_paren)) {
585 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
586 << getSpelling(PragmaTok);
587 return 0;
588 }
589
590 // Read the macro name string.
591 Lex(Tok);
592 if (Tok.isNot(tok::string_literal)) {
593 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
594 << getSpelling(PragmaTok);
595 return 0;
596 }
597
Richard Smith99831e42012-03-06 03:21:47 +0000598 if (Tok.hasUDSuffix()) {
599 Diag(Tok, diag::err_invalid_string_udl);
600 return 0;
601 }
602
Chris Lattnerf47724b2010-08-17 15:55:45 +0000603 // Remember the macro string.
604 std::string StrVal = getSpelling(Tok);
605
606 // Read the ')'.
607 Lex(Tok);
608 if (Tok.isNot(tok::r_paren)) {
609 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
610 << getSpelling(PragmaTok);
611 return 0;
612 }
613
614 assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
615 "Invalid string token!");
616
617 // Create a Token from the string.
618 Token MacroTok;
619 MacroTok.startToken();
Abramo Bagnarac4bf2b92010-12-22 08:23:18 +0000620 MacroTok.setKind(tok::raw_identifier);
Chris Lattnerf47724b2010-08-17 15:55:45 +0000621 CreateString(&StrVal[1], StrVal.size() - 2, MacroTok);
622
623 // Get the IdentifierInfo of MacroToPushTok.
624 return LookUpIdentifierInfo(MacroTok);
625}
626
627/// HandlePragmaPushMacro - Handle #pragma push_macro.
628/// The syntax is:
629/// #pragma push_macro("macro")
630void Preprocessor::HandlePragmaPushMacro(Token &PushMacroTok) {
631 // Parse the pragma directive and get the macro IdentifierInfo*.
632 IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PushMacroTok);
633 if (!IdentInfo) return;
634
635 // Get the MacroInfo associated with IdentInfo.
636 MacroInfo *MI = getMacroInfo(IdentInfo);
637
638 MacroInfo *MacroCopyToPush = 0;
639 if (MI) {
640 // Make a clone of MI.
641 MacroCopyToPush = CloneMacroInfo(*MI);
642
643 // Allow the original MacroInfo to be redefined later.
644 MI->setIsAllowRedefinitionsWithoutWarning(true);
645 }
646
647 // Push the cloned MacroInfo so we can retrieve it later.
648 PragmaPushMacroInfo[IdentInfo].push_back(MacroCopyToPush);
649}
650
Ted Kremenekb275e3d2010-10-19 17:40:50 +0000651/// HandlePragmaPopMacro - Handle #pragma pop_macro.
Chris Lattnerf47724b2010-08-17 15:55:45 +0000652/// The syntax is:
653/// #pragma pop_macro("macro")
654void Preprocessor::HandlePragmaPopMacro(Token &PopMacroTok) {
655 SourceLocation MessageLoc = PopMacroTok.getLocation();
656
657 // Parse the pragma directive and get the macro IdentifierInfo*.
658 IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PopMacroTok);
659 if (!IdentInfo) return;
660
661 // Find the vector<MacroInfo*> associated with the macro.
662 llvm::DenseMap<IdentifierInfo*, std::vector<MacroInfo*> >::iterator iter =
663 PragmaPushMacroInfo.find(IdentInfo);
664 if (iter != PragmaPushMacroInfo.end()) {
665 // Release the MacroInfo currently associated with IdentInfo.
666 MacroInfo *CurrentMI = getMacroInfo(IdentInfo);
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000667 if (CurrentMI) {
668 if (CurrentMI->isWarnIfUnused())
669 WarnUnusedMacroLocs.erase(CurrentMI->getDefinitionLoc());
670 ReleaseMacroInfo(CurrentMI);
671 }
Chris Lattnerf47724b2010-08-17 15:55:45 +0000672
673 // Get the MacroInfo we want to reinstall.
674 MacroInfo *MacroToReInstall = iter->second.back();
675
676 // Reinstall the previously pushed macro.
677 setMacroInfo(IdentInfo, MacroToReInstall);
678
679 // Pop PragmaPushMacroInfo stack.
680 iter->second.pop_back();
681 if (iter->second.size() == 0)
682 PragmaPushMacroInfo.erase(iter);
683 } else {
684 Diag(MessageLoc, diag::warn_pragma_pop_macro_no_push)
685 << IdentInfo->getName();
686 }
687}
Reid Spencer5f016e22007-07-11 17:01:13 +0000688
Aaron Ballman4c55c542012-03-02 22:51:54 +0000689void Preprocessor::HandlePragmaIncludeAlias(Token &Tok) {
690 // We will either get a quoted filename or a bracketed filename, and we
691 // have to track which we got. The first filename is the source name,
692 // and the second name is the mapped filename. If the first is quoted,
693 // the second must be as well (cannot mix and match quotes and brackets).
Aaron Ballman4c55c542012-03-02 22:51:54 +0000694
695 // Get the open paren
696 Lex(Tok);
697 if (Tok.isNot(tok::l_paren)) {
698 Diag(Tok, diag::warn_pragma_include_alias_expected) << "(";
699 return;
700 }
701
702 // We expect either a quoted string literal, or a bracketed name
703 Token SourceFilenameTok;
704 CurPPLexer->LexIncludeFilename(SourceFilenameTok);
705 if (SourceFilenameTok.is(tok::eod)) {
706 // The diagnostic has already been handled
707 return;
708 }
709
710 StringRef SourceFileName;
711 SmallString<128> FileNameBuffer;
712 if (SourceFilenameTok.is(tok::string_literal) ||
713 SourceFilenameTok.is(tok::angle_string_literal)) {
714 SourceFileName = getSpelling(SourceFilenameTok, FileNameBuffer);
715 } else if (SourceFilenameTok.is(tok::less)) {
716 // This could be a path instead of just a name
717 FileNameBuffer.push_back('<');
718 SourceLocation End;
719 if (ConcatenateIncludeName(FileNameBuffer, End))
720 return; // Diagnostic already emitted
721 SourceFileName = FileNameBuffer.str();
722 } else {
723 Diag(Tok, diag::warn_pragma_include_alias_expected_filename);
724 return;
725 }
726 FileNameBuffer.clear();
727
728 // Now we expect a comma, followed by another include name
729 Lex(Tok);
730 if (Tok.isNot(tok::comma)) {
731 Diag(Tok, diag::warn_pragma_include_alias_expected) << ",";
732 return;
733 }
734
735 Token ReplaceFilenameTok;
736 CurPPLexer->LexIncludeFilename(ReplaceFilenameTok);
737 if (ReplaceFilenameTok.is(tok::eod)) {
738 // The diagnostic has already been handled
739 return;
740 }
741
742 StringRef ReplaceFileName;
743 if (ReplaceFilenameTok.is(tok::string_literal) ||
744 ReplaceFilenameTok.is(tok::angle_string_literal)) {
745 ReplaceFileName = getSpelling(ReplaceFilenameTok, FileNameBuffer);
746 } else if (ReplaceFilenameTok.is(tok::less)) {
747 // This could be a path instead of just a name
748 FileNameBuffer.push_back('<');
749 SourceLocation End;
750 if (ConcatenateIncludeName(FileNameBuffer, End))
751 return; // Diagnostic already emitted
752 ReplaceFileName = FileNameBuffer.str();
753 } else {
754 Diag(Tok, diag::warn_pragma_include_alias_expected_filename);
755 return;
756 }
757
758 // Finally, we expect the closing paren
759 Lex(Tok);
760 if (Tok.isNot(tok::r_paren)) {
761 Diag(Tok, diag::warn_pragma_include_alias_expected) << ")";
762 return;
763 }
764
765 // Now that we have the source and target filenames, we need to make sure
766 // they're both of the same type (angled vs non-angled)
767 StringRef OriginalSource = SourceFileName;
768
769 bool SourceIsAngled =
770 GetIncludeFilenameSpelling(SourceFilenameTok.getLocation(),
771 SourceFileName);
772 bool ReplaceIsAngled =
773 GetIncludeFilenameSpelling(ReplaceFilenameTok.getLocation(),
774 ReplaceFileName);
775 if (!SourceFileName.empty() && !ReplaceFileName.empty() &&
776 (SourceIsAngled != ReplaceIsAngled)) {
777 unsigned int DiagID;
778 if (SourceIsAngled)
779 DiagID = diag::warn_pragma_include_alias_mismatch_angle;
780 else
781 DiagID = diag::warn_pragma_include_alias_mismatch_quote;
782
783 Diag(SourceFilenameTok.getLocation(), DiagID)
784 << SourceFileName
785 << ReplaceFileName;
786
787 return;
788 }
789
790 // Now we can let the include handler know about this mapping
791 getHeaderSearchInfo().AddIncludeAlias(OriginalSource, ReplaceFileName);
792}
793
Reid Spencer5f016e22007-07-11 17:01:13 +0000794/// AddPragmaHandler - Add the specified pragma handler to the preprocessor.
795/// If 'Namespace' is non-null, then it is a token required to exist on the
796/// pragma line before the pragma string starts, e.g. "STDC" or "GCC".
Chris Lattner5f9e2722011-07-23 10:55:15 +0000797void Preprocessor::AddPragmaHandler(StringRef Namespace,
Reid Spencer5f016e22007-07-11 17:01:13 +0000798 PragmaHandler *Handler) {
799 PragmaNamespace *InsertNS = PragmaHandlers;
Mike Stump1eb44332009-09-09 15:08:12 +0000800
Reid Spencer5f016e22007-07-11 17:01:13 +0000801 // If this is specified to be in a namespace, step down into it.
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000802 if (!Namespace.empty()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000803 // If there is already a pragma handler with the name of this namespace,
804 // we either have an error (directive with the same name as a namespace) or
805 // we already have the namespace to insert into.
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000806 if (PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000807 InsertNS = Existing->getIfNamespace();
808 assert(InsertNS != 0 && "Cannot have a pragma namespace and pragma"
809 " handler with the same name!");
810 } else {
811 // Otherwise, this namespace doesn't exist yet, create and insert the
812 // handler for it.
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000813 InsertNS = new PragmaNamespace(Namespace);
Reid Spencer5f016e22007-07-11 17:01:13 +0000814 PragmaHandlers->AddPragma(InsertNS);
815 }
816 }
Mike Stump1eb44332009-09-09 15:08:12 +0000817
Reid Spencer5f016e22007-07-11 17:01:13 +0000818 // Check to make sure we don't already have a pragma for this identifier.
819 assert(!InsertNS->FindHandler(Handler->getName()) &&
820 "Pragma handler already exists for this identifier!");
821 InsertNS->AddPragma(Handler);
822}
823
Daniel Dunbar40950802008-10-04 19:17:46 +0000824/// RemovePragmaHandler - Remove the specific pragma handler from the
825/// preprocessor. If \arg Namespace is non-null, then it should be the
826/// namespace that \arg Handler was added to. It is an error to remove
827/// a handler that has not been registered.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000828void Preprocessor::RemovePragmaHandler(StringRef Namespace,
Daniel Dunbar40950802008-10-04 19:17:46 +0000829 PragmaHandler *Handler) {
830 PragmaNamespace *NS = PragmaHandlers;
Mike Stump1eb44332009-09-09 15:08:12 +0000831
Daniel Dunbar40950802008-10-04 19:17:46 +0000832 // If this is specified to be in a namespace, step down into it.
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000833 if (!Namespace.empty()) {
834 PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace);
Daniel Dunbar40950802008-10-04 19:17:46 +0000835 assert(Existing && "Namespace containing handler does not exist!");
836
837 NS = Existing->getIfNamespace();
838 assert(NS && "Invalid namespace, registered as a regular pragma handler!");
839 }
840
841 NS->RemovePragmaHandler(Handler);
Mike Stump1eb44332009-09-09 15:08:12 +0000842
Daniel Dunbar40950802008-10-04 19:17:46 +0000843 // If this is a non-default namespace and it is now empty, remove
844 // it.
Argyrios Kyrtzidisce52bb32012-01-06 00:22:09 +0000845 if (NS != PragmaHandlers && NS->IsEmpty()) {
Daniel Dunbar40950802008-10-04 19:17:46 +0000846 PragmaHandlers->RemovePragmaHandler(NS);
Argyrios Kyrtzidisce52bb32012-01-06 00:22:09 +0000847 delete NS;
848 }
Daniel Dunbar40950802008-10-04 19:17:46 +0000849}
850
Peter Collingbourne9d3f5f72011-02-14 01:42:24 +0000851bool Preprocessor::LexOnOffSwitch(tok::OnOffSwitch &Result) {
852 Token Tok;
853 LexUnexpandedToken(Tok);
854
855 if (Tok.isNot(tok::identifier)) {
856 Diag(Tok, diag::ext_on_off_switch_syntax);
857 return true;
858 }
859 IdentifierInfo *II = Tok.getIdentifierInfo();
860 if (II->isStr("ON"))
861 Result = tok::OOS_ON;
862 else if (II->isStr("OFF"))
863 Result = tok::OOS_OFF;
864 else if (II->isStr("DEFAULT"))
865 Result = tok::OOS_DEFAULT;
866 else {
867 Diag(Tok, diag::ext_on_off_switch_syntax);
868 return true;
869 }
870
Peter Collingbourne84021552011-02-28 02:37:51 +0000871 // Verify that this is followed by EOD.
Peter Collingbourne9d3f5f72011-02-14 01:42:24 +0000872 LexUnexpandedToken(Tok);
Peter Collingbourne84021552011-02-28 02:37:51 +0000873 if (Tok.isNot(tok::eod))
874 Diag(Tok, diag::ext_pragma_syntax_eod);
Peter Collingbourne9d3f5f72011-02-14 01:42:24 +0000875 return false;
876}
877
Reid Spencer5f016e22007-07-11 17:01:13 +0000878namespace {
Chris Lattner22434492007-12-19 19:38:36 +0000879/// PragmaOnceHandler - "#pragma once" marks the file as atomically included.
Reid Spencer5f016e22007-07-11 17:01:13 +0000880struct PragmaOnceHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000881 PragmaOnceHandler() : PragmaHandler("once") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000882 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
883 Token &OnceTok) {
Chris Lattner35410d52009-04-14 05:07:49 +0000884 PP.CheckEndOfDirective("pragma once");
Reid Spencer5f016e22007-07-11 17:01:13 +0000885 PP.HandlePragmaOnce(OnceTok);
886 }
887};
888
Chris Lattner22434492007-12-19 19:38:36 +0000889/// PragmaMarkHandler - "#pragma mark ..." is ignored by the compiler, and the
890/// rest of the line is not lexed.
891struct PragmaMarkHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000892 PragmaMarkHandler() : PragmaHandler("mark") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000893 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
894 Token &MarkTok) {
Chris Lattner22434492007-12-19 19:38:36 +0000895 PP.HandlePragmaMark();
896 }
897};
898
899/// PragmaPoisonHandler - "#pragma poison x" marks x as not usable.
Reid Spencer5f016e22007-07-11 17:01:13 +0000900struct PragmaPoisonHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000901 PragmaPoisonHandler() : PragmaHandler("poison") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000902 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
903 Token &PoisonTok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000904 PP.HandlePragmaPoison(PoisonTok);
905 }
906};
907
Chris Lattner22434492007-12-19 19:38:36 +0000908/// PragmaSystemHeaderHandler - "#pragma system_header" marks the current file
909/// as a system header, which silences warnings in it.
Reid Spencer5f016e22007-07-11 17:01:13 +0000910struct PragmaSystemHeaderHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000911 PragmaSystemHeaderHandler() : PragmaHandler("system_header") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000912 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
913 Token &SHToken) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000914 PP.HandlePragmaSystemHeader(SHToken);
Chris Lattner35410d52009-04-14 05:07:49 +0000915 PP.CheckEndOfDirective("pragma");
Reid Spencer5f016e22007-07-11 17:01:13 +0000916 }
917};
918struct PragmaDependencyHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000919 PragmaDependencyHandler() : PragmaHandler("dependency") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000920 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
921 Token &DepToken) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000922 PP.HandlePragmaDependency(DepToken);
923 }
924};
Mike Stump1eb44332009-09-09 15:08:12 +0000925
Daniel Dunbarabf7b722010-07-28 15:40:33 +0000926struct PragmaDebugHandler : public PragmaHandler {
927 PragmaDebugHandler() : PragmaHandler("__debug") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000928 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
929 Token &DepToken) {
Daniel Dunbarabf7b722010-07-28 15:40:33 +0000930 Token Tok;
931 PP.LexUnexpandedToken(Tok);
932 if (Tok.isNot(tok::identifier)) {
Douglas Gregor6493a4d2010-08-30 15:15:34 +0000933 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Daniel Dunbarabf7b722010-07-28 15:40:33 +0000934 return;
935 }
936 IdentifierInfo *II = Tok.getIdentifierInfo();
937
Daniel Dunbar55054132010-08-17 22:32:48 +0000938 if (II->isStr("assert")) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000939 llvm_unreachable("This is an assertion!");
Daniel Dunbarabf7b722010-07-28 15:40:33 +0000940 } else if (II->isStr("crash")) {
Daniel Dunbar55054132010-08-17 22:32:48 +0000941 *(volatile int*) 0x11 = 0;
942 } else if (II->isStr("llvm_fatal_error")) {
943 llvm::report_fatal_error("#pragma clang __debug llvm_fatal_error");
944 } else if (II->isStr("llvm_unreachable")) {
945 llvm_unreachable("#pragma clang __debug llvm_unreachable");
946 } else if (II->isStr("overflow_stack")) {
947 DebugOverflowStack();
Daniel Dunbarff759a62010-08-18 23:09:23 +0000948 } else if (II->isStr("handle_crash")) {
949 llvm::CrashRecoveryContext *CRC =llvm::CrashRecoveryContext::GetCurrent();
950 if (CRC)
951 CRC->HandleCrash();
Daniel Dunbar55054132010-08-17 22:32:48 +0000952 } else {
953 PP.Diag(Tok, diag::warn_pragma_debug_unexpected_command)
954 << II->getName();
Daniel Dunbarabf7b722010-07-28 15:40:33 +0000955 }
956 }
957
Francois Pichet1066c6c2011-05-25 16:15:03 +0000958// Disable MSVC warning about runtime stack overflow.
959#ifdef _MSC_VER
960 #pragma warning(disable : 4717)
961#endif
Daniel Dunbarabf7b722010-07-28 15:40:33 +0000962 void DebugOverflowStack() {
963 DebugOverflowStack();
964 }
Francois Pichet1066c6c2011-05-25 16:15:03 +0000965#ifdef _MSC_VER
966 #pragma warning(default : 4717)
967#endif
968
Daniel Dunbarabf7b722010-07-28 15:40:33 +0000969};
970
Chris Lattneredaf8772009-04-19 23:16:58 +0000971/// PragmaDiagnosticHandler - e.g. '#pragma GCC diagnostic ignored "-Wformat"'
972struct PragmaDiagnosticHandler : public PragmaHandler {
Douglas Gregorc09ce122011-06-22 19:41:48 +0000973private:
974 const char *Namespace;
Chris Lattner04ae2df2009-07-12 21:18:45 +0000975public:
Douglas Gregorc09ce122011-06-22 19:41:48 +0000976 explicit PragmaDiagnosticHandler(const char *NS) :
977 PragmaHandler("diagnostic"), Namespace(NS) {}
Douglas Gregor80c60f72010-09-09 22:45:38 +0000978 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
979 Token &DiagToken) {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000980 SourceLocation DiagLoc = DiagToken.getLocation();
Chris Lattneredaf8772009-04-19 23:16:58 +0000981 Token Tok;
982 PP.LexUnexpandedToken(Tok);
983 if (Tok.isNot(tok::identifier)) {
Douglas Gregor6493a4d2010-08-30 15:15:34 +0000984 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Chris Lattneredaf8772009-04-19 23:16:58 +0000985 return;
986 }
987 IdentifierInfo *II = Tok.getIdentifierInfo();
Douglas Gregorc09ce122011-06-22 19:41:48 +0000988 PPCallbacks *Callbacks = PP.getPPCallbacks();
Mike Stump1eb44332009-09-09 15:08:12 +0000989
Chris Lattneredaf8772009-04-19 23:16:58 +0000990 diag::Mapping Map;
991 if (II->isStr("warning"))
992 Map = diag::MAP_WARNING;
993 else if (II->isStr("error"))
994 Map = diag::MAP_ERROR;
995 else if (II->isStr("ignored"))
996 Map = diag::MAP_IGNORE;
997 else if (II->isStr("fatal"))
998 Map = diag::MAP_FATAL;
Douglas Gregor6493a4d2010-08-30 15:15:34 +0000999 else if (II->isStr("pop")) {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001000 if (!PP.getDiagnostics().popMappings(DiagLoc))
Douglas Gregor6493a4d2010-08-30 15:15:34 +00001001 PP.Diag(Tok, diag::warn_pragma_diagnostic_cannot_pop);
Douglas Gregorc09ce122011-06-22 19:41:48 +00001002 else if (Callbacks)
1003 Callbacks->PragmaDiagnosticPop(DiagLoc, Namespace);
Douglas Gregor6493a4d2010-08-30 15:15:34 +00001004 return;
1005 } else if (II->isStr("push")) {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001006 PP.getDiagnostics().pushMappings(DiagLoc);
Douglas Gregorc09ce122011-06-22 19:41:48 +00001007 if (Callbacks)
1008 Callbacks->PragmaDiagnosticPush(DiagLoc, Namespace);
Chris Lattner04ae2df2009-07-12 21:18:45 +00001009 return;
1010 } else {
Douglas Gregor6493a4d2010-08-30 15:15:34 +00001011 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Chris Lattneredaf8772009-04-19 23:16:58 +00001012 return;
1013 }
Mike Stump1eb44332009-09-09 15:08:12 +00001014
Chris Lattneredaf8772009-04-19 23:16:58 +00001015 PP.LexUnexpandedToken(Tok);
1016
1017 // We need at least one string.
1018 if (Tok.isNot(tok::string_literal)) {
1019 PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token);
1020 return;
1021 }
Mike Stump1eb44332009-09-09 15:08:12 +00001022
Chris Lattneredaf8772009-04-19 23:16:58 +00001023 // String concatenation allows multiple strings, which can even come from
1024 // macro expansion.
1025 // "foo " "bar" "Baz"
Chris Lattner5f9e2722011-07-23 10:55:15 +00001026 SmallVector<Token, 4> StrToks;
Chris Lattneredaf8772009-04-19 23:16:58 +00001027 while (Tok.is(tok::string_literal)) {
1028 StrToks.push_back(Tok);
1029 PP.LexUnexpandedToken(Tok);
1030 }
Mike Stump1eb44332009-09-09 15:08:12 +00001031
Peter Collingbourne84021552011-02-28 02:37:51 +00001032 if (Tok.isNot(tok::eod)) {
Chris Lattneredaf8772009-04-19 23:16:58 +00001033 PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token);
1034 return;
1035 }
Mike Stump1eb44332009-09-09 15:08:12 +00001036
Chris Lattneredaf8772009-04-19 23:16:58 +00001037 // Concatenate and parse the strings.
1038 StringLiteralParser Literal(&StrToks[0], StrToks.size(), PP);
Douglas Gregor5cee1192011-07-27 05:40:30 +00001039 assert(Literal.isAscii() && "Didn't allow wide strings in");
Chris Lattneredaf8772009-04-19 23:16:58 +00001040 if (Literal.hadError)
1041 return;
1042 if (Literal.Pascal) {
Douglas Gregor6493a4d2010-08-30 15:15:34 +00001043 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Chris Lattneredaf8772009-04-19 23:16:58 +00001044 return;
1045 }
Chris Lattner04ae2df2009-07-12 21:18:45 +00001046
Chris Lattner5f9e2722011-07-23 10:55:15 +00001047 StringRef WarningName(Literal.GetString());
Chris Lattneredaf8772009-04-19 23:16:58 +00001048
1049 if (WarningName.size() < 3 || WarningName[0] != '-' ||
1050 WarningName[1] != 'W') {
1051 PP.Diag(StrToks[0].getLocation(),
1052 diag::warn_pragma_diagnostic_invalid_option);
1053 return;
1054 }
Mike Stump1eb44332009-09-09 15:08:12 +00001055
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +00001056 if (PP.getDiagnostics().setDiagnosticGroupMapping(WarningName.substr(2),
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001057 Map, DiagLoc))
Chris Lattneredaf8772009-04-19 23:16:58 +00001058 PP.Diag(StrToks[0].getLocation(),
1059 diag::warn_pragma_diagnostic_unknown_warning) << WarningName;
Douglas Gregorc09ce122011-06-22 19:41:48 +00001060 else if (Callbacks)
1061 Callbacks->PragmaDiagnostic(DiagLoc, Namespace, Map, WarningName);
Chris Lattneredaf8772009-04-19 23:16:58 +00001062 }
1063};
Mike Stump1eb44332009-09-09 15:08:12 +00001064
Chris Lattner636c5ef2009-01-16 08:21:25 +00001065/// PragmaCommentHandler - "#pragma comment ...".
1066struct PragmaCommentHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001067 PragmaCommentHandler() : PragmaHandler("comment") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +00001068 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1069 Token &CommentTok) {
Chris Lattner636c5ef2009-01-16 08:21:25 +00001070 PP.HandlePragmaComment(CommentTok);
1071 }
1072};
Mike Stump1eb44332009-09-09 15:08:12 +00001073
Aaron Ballman4c55c542012-03-02 22:51:54 +00001074/// PragmaIncludeAliasHandler - "#pragma include_alias("...")".
1075struct PragmaIncludeAliasHandler : public PragmaHandler {
1076 PragmaIncludeAliasHandler() : PragmaHandler("include_alias") {}
1077 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1078 Token &IncludeAliasTok) {
1079 PP.HandlePragmaIncludeAlias(IncludeAliasTok);
1080 }
1081};
1082
Chris Lattnerabfe0942010-06-26 17:11:39 +00001083/// PragmaMessageHandler - "#pragma message("...")".
1084struct PragmaMessageHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001085 PragmaMessageHandler() : PragmaHandler("message") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +00001086 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1087 Token &CommentTok) {
Chris Lattnerabfe0942010-06-26 17:11:39 +00001088 PP.HandlePragmaMessage(CommentTok);
1089 }
1090};
1091
Chris Lattnerf47724b2010-08-17 15:55:45 +00001092/// PragmaPushMacroHandler - "#pragma push_macro" saves the value of the
1093/// macro on the top of the stack.
1094struct PragmaPushMacroHandler : public PragmaHandler {
1095 PragmaPushMacroHandler() : PragmaHandler("push_macro") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +00001096 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1097 Token &PushMacroTok) {
Chris Lattnerf47724b2010-08-17 15:55:45 +00001098 PP.HandlePragmaPushMacro(PushMacroTok);
1099 }
1100};
1101
1102
1103/// PragmaPopMacroHandler - "#pragma pop_macro" sets the value of the
1104/// macro to the value on the top of the stack.
1105struct PragmaPopMacroHandler : public PragmaHandler {
1106 PragmaPopMacroHandler() : PragmaHandler("pop_macro") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +00001107 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1108 Token &PopMacroTok) {
Chris Lattnerf47724b2010-08-17 15:55:45 +00001109 PP.HandlePragmaPopMacro(PopMacroTok);
1110 }
1111};
1112
Chris Lattner062f2322009-04-19 21:20:35 +00001113// Pragma STDC implementations.
Chris Lattner6c5cf4a2009-04-19 21:50:08 +00001114
Chris Lattner062f2322009-04-19 21:20:35 +00001115/// PragmaSTDC_FENV_ACCESSHandler - "#pragma STDC FENV_ACCESS ...".
1116struct PragmaSTDC_FENV_ACCESSHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001117 PragmaSTDC_FENV_ACCESSHandler() : PragmaHandler("FENV_ACCESS") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +00001118 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1119 Token &Tok) {
Peter Collingbourne9d3f5f72011-02-14 01:42:24 +00001120 tok::OnOffSwitch OOS;
1121 if (PP.LexOnOffSwitch(OOS))
1122 return;
1123 if (OOS == tok::OOS_ON)
Chris Lattner4d8aac32009-04-19 21:55:32 +00001124 PP.Diag(Tok, diag::warn_stdc_fenv_access_not_supported);
Chris Lattner062f2322009-04-19 21:20:35 +00001125 }
1126};
Mike Stump1eb44332009-09-09 15:08:12 +00001127
Chris Lattner062f2322009-04-19 21:20:35 +00001128/// PragmaSTDC_CX_LIMITED_RANGEHandler - "#pragma STDC CX_LIMITED_RANGE ...".
1129struct PragmaSTDC_CX_LIMITED_RANGEHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001130 PragmaSTDC_CX_LIMITED_RANGEHandler()
1131 : PragmaHandler("CX_LIMITED_RANGE") {}
Douglas Gregor80c60f72010-09-09 22:45:38 +00001132 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1133 Token &Tok) {
Peter Collingbourne9d3f5f72011-02-14 01:42:24 +00001134 tok::OnOffSwitch OOS;
1135 PP.LexOnOffSwitch(OOS);
Chris Lattner062f2322009-04-19 21:20:35 +00001136 }
1137};
Mike Stump1eb44332009-09-09 15:08:12 +00001138
Chris Lattner062f2322009-04-19 21:20:35 +00001139/// PragmaSTDC_UnknownHandler - "#pragma STDC ...".
1140struct PragmaSTDC_UnknownHandler : public PragmaHandler {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001141 PragmaSTDC_UnknownHandler() {}
Douglas Gregor80c60f72010-09-09 22:45:38 +00001142 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1143 Token &UnknownTok) {
Chris Lattner6c5cf4a2009-04-19 21:50:08 +00001144 // C99 6.10.6p2, unknown forms are not allowed.
Chris Lattnerf545be52009-04-19 21:25:37 +00001145 PP.Diag(UnknownTok, diag::ext_stdc_pragma_ignored);
Chris Lattner062f2322009-04-19 21:20:35 +00001146 }
1147};
Mike Stump1eb44332009-09-09 15:08:12 +00001148
John McCall8dfac0b2011-09-30 05:12:12 +00001149/// PragmaARCCFCodeAuditedHandler -
1150/// #pragma clang arc_cf_code_audited begin/end
1151struct PragmaARCCFCodeAuditedHandler : public PragmaHandler {
1152 PragmaARCCFCodeAuditedHandler() : PragmaHandler("arc_cf_code_audited") {}
1153 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1154 Token &NameTok) {
1155 SourceLocation Loc = NameTok.getLocation();
1156 bool IsBegin;
1157
1158 Token Tok;
1159
1160 // Lex the 'begin' or 'end'.
1161 PP.LexUnexpandedToken(Tok);
1162 const IdentifierInfo *BeginEnd = Tok.getIdentifierInfo();
1163 if (BeginEnd && BeginEnd->isStr("begin")) {
1164 IsBegin = true;
1165 } else if (BeginEnd && BeginEnd->isStr("end")) {
1166 IsBegin = false;
1167 } else {
1168 PP.Diag(Tok.getLocation(), diag::err_pp_arc_cf_code_audited_syntax);
1169 return;
1170 }
1171
1172 // Verify that this is followed by EOD.
1173 PP.LexUnexpandedToken(Tok);
1174 if (Tok.isNot(tok::eod))
1175 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
1176
1177 // The start location of the active audit.
1178 SourceLocation BeginLoc = PP.getPragmaARCCFCodeAuditedLoc();
1179
1180 // The start location we want after processing this.
1181 SourceLocation NewLoc;
1182
1183 if (IsBegin) {
1184 // Complain about attempts to re-enter an audit.
1185 if (BeginLoc.isValid()) {
1186 PP.Diag(Loc, diag::err_pp_double_begin_of_arc_cf_code_audited);
1187 PP.Diag(BeginLoc, diag::note_pragma_entered_here);
1188 }
1189 NewLoc = Loc;
1190 } else {
1191 // Complain about attempts to leave an audit that doesn't exist.
1192 if (!BeginLoc.isValid()) {
1193 PP.Diag(Loc, diag::err_pp_unmatched_end_of_arc_cf_code_audited);
1194 return;
1195 }
1196 NewLoc = SourceLocation();
1197 }
1198
1199 PP.setPragmaARCCFCodeAuditedLoc(NewLoc);
1200 }
1201};
1202
Reid Spencer5f016e22007-07-11 17:01:13 +00001203} // end anonymous namespace
1204
1205
1206/// RegisterBuiltinPragmas - Install the standard preprocessor pragmas:
1207/// #pragma GCC poison/system_header/dependency and #pragma once.
1208void Preprocessor::RegisterBuiltinPragmas() {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001209 AddPragmaHandler(new PragmaOnceHandler());
1210 AddPragmaHandler(new PragmaMarkHandler());
Chris Lattnerf47724b2010-08-17 15:55:45 +00001211 AddPragmaHandler(new PragmaPushMacroHandler());
1212 AddPragmaHandler(new PragmaPopMacroHandler());
Michael J. Spencer301669b2010-09-27 06:19:02 +00001213 AddPragmaHandler(new PragmaMessageHandler());
Mike Stump1eb44332009-09-09 15:08:12 +00001214
Chris Lattnere8fa06e2009-05-12 18:21:11 +00001215 // #pragma GCC ...
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001216 AddPragmaHandler("GCC", new PragmaPoisonHandler());
1217 AddPragmaHandler("GCC", new PragmaSystemHeaderHandler());
1218 AddPragmaHandler("GCC", new PragmaDependencyHandler());
Douglas Gregorc09ce122011-06-22 19:41:48 +00001219 AddPragmaHandler("GCC", new PragmaDiagnosticHandler("GCC"));
Chris Lattnere8fa06e2009-05-12 18:21:11 +00001220 // #pragma clang ...
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001221 AddPragmaHandler("clang", new PragmaPoisonHandler());
1222 AddPragmaHandler("clang", new PragmaSystemHeaderHandler());
Daniel Dunbarabf7b722010-07-28 15:40:33 +00001223 AddPragmaHandler("clang", new PragmaDebugHandler());
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001224 AddPragmaHandler("clang", new PragmaDependencyHandler());
Douglas Gregorc09ce122011-06-22 19:41:48 +00001225 AddPragmaHandler("clang", new PragmaDiagnosticHandler("clang"));
John McCall8dfac0b2011-09-30 05:12:12 +00001226 AddPragmaHandler("clang", new PragmaARCCFCodeAuditedHandler());
Chris Lattnere8fa06e2009-05-12 18:21:11 +00001227
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001228 AddPragmaHandler("STDC", new PragmaSTDC_FENV_ACCESSHandler());
1229 AddPragmaHandler("STDC", new PragmaSTDC_CX_LIMITED_RANGEHandler());
Chris Lattner062f2322009-04-19 21:20:35 +00001230 AddPragmaHandler("STDC", new PragmaSTDC_UnknownHandler());
Mike Stump1eb44332009-09-09 15:08:12 +00001231
Chris Lattner636c5ef2009-01-16 08:21:25 +00001232 // MS extensions.
David Blaikie4e4d0842012-03-11 07:00:24 +00001233 if (LangOpts.MicrosoftExt) {
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +00001234 AddPragmaHandler(new PragmaCommentHandler());
Aaron Ballman4c55c542012-03-02 22:51:54 +00001235 AddPragmaHandler(new PragmaIncludeAliasHandler());
Chris Lattnerabfe0942010-06-26 17:11:39 +00001236 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001237}