blob: c6290170892adc6751c200940dce9b00475de289 [file] [log] [blame]
Chris Lattner89620152008-03-09 03:13:06 +00001//===--- PPDirectives.cpp - Directive Handling for Preprocessor -----------===//
Chris Lattnerf64b3522008-03-09 01:54:53 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements # directive processing for the Preprocessor.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Lex/Preprocessor.h"
Chris Lattner100c65e2009-01-26 05:29:08 +000015#include "clang/Lex/LiteralSupport.h"
Chris Lattnerf64b3522008-03-09 01:54:53 +000016#include "clang/Lex/HeaderSearch.h"
17#include "clang/Lex/MacroInfo.h"
Chris Lattner60f36222009-01-29 05:15:15 +000018#include "clang/Lex/LexDiagnostic.h"
Douglas Gregor3a7ad252010-08-24 19:08:16 +000019#include "clang/Lex/CodeCompletionHandler.h"
Douglas Gregor97eec242011-09-15 22:00:41 +000020#include "clang/Lex/ModuleLoader.h"
Douglas Gregorc7d65762010-09-09 22:45:38 +000021#include "clang/Lex/Pragma.h"
Chris Lattner710bb872009-11-30 04:18:44 +000022#include "clang/Basic/FileManager.h"
Chris Lattnerf64b3522008-03-09 01:54:53 +000023#include "clang/Basic/SourceManager.h"
Chris Lattner100c65e2009-01-26 05:29:08 +000024#include "llvm/ADT/APInt.h"
Douglas Gregor41e115a2011-11-30 18:02:36 +000025#include "llvm/Support/ErrorHandling.h"
Chris Lattnerf64b3522008-03-09 01:54:53 +000026using namespace clang;
27
28//===----------------------------------------------------------------------===//
29// Utility Methods for Preprocessor Directive Handling.
30//===----------------------------------------------------------------------===//
31
Chris Lattnerc0a585d2010-08-17 15:55:45 +000032MacroInfo *Preprocessor::AllocateMacroInfo() {
Ted Kremenekc8456f82010-10-19 22:15:20 +000033 MacroInfoChain *MIChain;
Mike Stump11289f42009-09-09 15:08:12 +000034
Ted Kremenekc8456f82010-10-19 22:15:20 +000035 if (MICache) {
36 MIChain = MICache;
37 MICache = MICache->Next;
Ted Kremenek1f1e4bd2010-10-19 18:16:54 +000038 }
Ted Kremenekc8456f82010-10-19 22:15:20 +000039 else {
40 MIChain = BP.Allocate<MacroInfoChain>();
41 }
42
43 MIChain->Next = MIChainHead;
44 MIChain->Prev = 0;
45 if (MIChainHead)
46 MIChainHead->Prev = MIChain;
47 MIChainHead = MIChain;
48
49 return &(MIChain->MI);
Chris Lattnerc0a585d2010-08-17 15:55:45 +000050}
51
52MacroInfo *Preprocessor::AllocateMacroInfo(SourceLocation L) {
53 MacroInfo *MI = AllocateMacroInfo();
Ted Kremenek6c7ea112008-12-15 19:56:42 +000054 new (MI) MacroInfo(L);
55 return MI;
56}
57
Chris Lattnerc0a585d2010-08-17 15:55:45 +000058MacroInfo *Preprocessor::CloneMacroInfo(const MacroInfo &MacroToClone) {
59 MacroInfo *MI = AllocateMacroInfo();
60 new (MI) MacroInfo(MacroToClone, BP);
61 return MI;
62}
63
Chris Lattner666f7a42009-02-20 22:19:20 +000064/// ReleaseMacroInfo - Release the specified MacroInfo. This memory will
65/// be reused for allocating new MacroInfo objects.
Chris Lattner66b67d22010-08-18 16:08:51 +000066void Preprocessor::ReleaseMacroInfo(MacroInfo *MI) {
Ted Kremenekc8456f82010-10-19 22:15:20 +000067 MacroInfoChain *MIChain = (MacroInfoChain*) MI;
68 if (MacroInfoChain *Prev = MIChain->Prev) {
69 MacroInfoChain *Next = MIChain->Next;
70 Prev->Next = Next;
71 if (Next)
72 Next->Prev = Prev;
73 }
74 else {
75 assert(MIChainHead == MIChain);
76 MIChainHead = MIChain->Next;
77 MIChainHead->Prev = 0;
78 }
79 MIChain->Next = MICache;
80 MICache = MIChain;
Chris Lattner666f7a42009-02-20 22:19:20 +000081
Ted Kremenekc8456f82010-10-19 22:15:20 +000082 MI->Destroy();
83}
Chris Lattner666f7a42009-02-20 22:19:20 +000084
Chris Lattnerf64b3522008-03-09 01:54:53 +000085/// DiscardUntilEndOfDirective - Read and discard all tokens remaining on the
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +000086/// current line until the tok::eod token is found.
Chris Lattnerf64b3522008-03-09 01:54:53 +000087void Preprocessor::DiscardUntilEndOfDirective() {
88 Token Tmp;
89 do {
90 LexUnexpandedToken(Tmp);
Peter Collingbournef29ce972011-02-22 13:49:06 +000091 assert(Tmp.isNot(tok::eof) && "EOF seen while discarding directive tokens");
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +000092 } while (Tmp.isNot(tok::eod));
Chris Lattnerf64b3522008-03-09 01:54:53 +000093}
94
Chris Lattnerf64b3522008-03-09 01:54:53 +000095/// ReadMacroName - Lex and validate a macro name, which occurs after a
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +000096/// #define or #undef. This sets the token kind to eod and discards the rest
Chris Lattnerf64b3522008-03-09 01:54:53 +000097/// of the macro line if the macro name is invalid. isDefineUndef is 1 if
98/// this is due to a a #define, 2 if #undef directive, 0 if it is something
99/// else (e.g. #ifdef).
100void Preprocessor::ReadMacroName(Token &MacroNameTok, char isDefineUndef) {
101 // Read the token, don't allow macro expansion on it.
102 LexUnexpandedToken(MacroNameTok);
Mike Stump11289f42009-09-09 15:08:12 +0000103
Douglas Gregor12785102010-08-24 20:21:13 +0000104 if (MacroNameTok.is(tok::code_completion)) {
105 if (CodeComplete)
106 CodeComplete->CodeCompleteMacroName(isDefineUndef == 1);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000107 setCodeCompletionReached();
Douglas Gregor12785102010-08-24 20:21:13 +0000108 LexUnexpandedToken(MacroNameTok);
Douglas Gregor12785102010-08-24 20:21:13 +0000109 }
110
Chris Lattnerf64b3522008-03-09 01:54:53 +0000111 // Missing macro name?
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000112 if (MacroNameTok.is(tok::eod)) {
Chris Lattner907dfe92008-11-18 07:59:24 +0000113 Diag(MacroNameTok, diag::err_pp_missing_macro_name);
114 return;
115 }
Mike Stump11289f42009-09-09 15:08:12 +0000116
Chris Lattnerf64b3522008-03-09 01:54:53 +0000117 IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
118 if (II == 0) {
Douglas Gregordc970f02010-03-16 22:30:13 +0000119 bool Invalid = false;
120 std::string Spelling = getSpelling(MacroNameTok, &Invalid);
121 if (Invalid)
122 return;
Nico Weber2e686202012-02-29 22:54:43 +0000123
Chris Lattner77c76ae2008-12-13 20:12:40 +0000124 const IdentifierInfo &Info = Identifiers.get(Spelling);
Nico Weber2e686202012-02-29 22:54:43 +0000125
126 // Allow #defining |and| and friends in microsoft mode.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000127 if (Info.isCPlusPlusOperatorKeyword() && getLangOpts().MicrosoftMode) {
Nico Weber2e686202012-02-29 22:54:43 +0000128 MacroNameTok.setIdentifierInfo(getIdentifierInfo(Spelling));
129 return;
130 }
131
Chris Lattner77c76ae2008-12-13 20:12:40 +0000132 if (Info.isCPlusPlusOperatorKeyword())
Chris Lattnerf64b3522008-03-09 01:54:53 +0000133 // C++ 2.5p2: Alternative tokens behave the same as its primary token
134 // except for their spellings.
Chris Lattner97b8e842008-11-18 08:02:48 +0000135 Diag(MacroNameTok, diag::err_pp_operator_used_as_macro_name) << Spelling;
Chris Lattnerf64b3522008-03-09 01:54:53 +0000136 else
137 Diag(MacroNameTok, diag::err_pp_macro_not_identifier);
138 // Fall through on error.
139 } else if (isDefineUndef && II->getPPKeywordID() == tok::pp_defined) {
140 // Error if defining "defined": C99 6.10.8.4.
141 Diag(MacroNameTok, diag::err_defined_macro_name);
142 } else if (isDefineUndef && II->hasMacroDefinition() &&
143 getMacroInfo(II)->isBuiltinMacro()) {
144 // Error if defining "__LINE__" and other builtins: C99 6.10.8.4.
145 if (isDefineUndef == 1)
146 Diag(MacroNameTok, diag::pp_redef_builtin_macro);
147 else
148 Diag(MacroNameTok, diag::pp_undef_builtin_macro);
149 } else {
150 // Okay, we got a good identifier node. Return it.
151 return;
152 }
Mike Stump11289f42009-09-09 15:08:12 +0000153
Chris Lattnerf64b3522008-03-09 01:54:53 +0000154 // Invalid macro name, read and discard the rest of the line. Then set the
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000155 // token kind to tok::eod.
156 MacroNameTok.setKind(tok::eod);
Chris Lattnerf64b3522008-03-09 01:54:53 +0000157 return DiscardUntilEndOfDirective();
158}
159
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000160/// CheckEndOfDirective - Ensure that the next token is a tok::eod token. If
161/// not, emit a diagnostic and consume up until the eod. If EnableMacros is
Chris Lattner0003c272009-04-17 23:30:53 +0000162/// true, then we consider macros that expand to zero tokens as being ok.
163void Preprocessor::CheckEndOfDirective(const char *DirType, bool EnableMacros) {
Chris Lattnerf64b3522008-03-09 01:54:53 +0000164 Token Tmp;
Chris Lattner0003c272009-04-17 23:30:53 +0000165 // Lex unexpanded tokens for most directives: macros might expand to zero
166 // tokens, causing us to miss diagnosing invalid lines. Some directives (like
167 // #line) allow empty macros.
168 if (EnableMacros)
169 Lex(Tmp);
170 else
171 LexUnexpandedToken(Tmp);
Mike Stump11289f42009-09-09 15:08:12 +0000172
Chris Lattnerf64b3522008-03-09 01:54:53 +0000173 // There should be no tokens after the directive, but we allow them as an
174 // extension.
175 while (Tmp.is(tok::comment)) // Skip comments in -C mode.
176 LexUnexpandedToken(Tmp);
Mike Stump11289f42009-09-09 15:08:12 +0000177
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000178 if (Tmp.isNot(tok::eod)) {
Chris Lattner825676a2009-04-14 05:15:20 +0000179 // Add a fixit in GNU/C99/C++ mode. Don't offer a fixit for strict-C89,
Peter Collingbourne2c9f9662011-02-22 13:49:00 +0000180 // or if this is a macro-style preprocessing directive, because it is more
181 // trouble than it is worth to insert /**/ and check that there is no /**/
182 // in the range also.
Douglas Gregora771f462010-03-31 17:46:05 +0000183 FixItHint Hint;
David Blaikiebbafb8a2012-03-11 07:00:24 +0000184 if ((LangOpts.GNUMode || LangOpts.C99 || LangOpts.CPlusPlus) &&
Peter Collingbourne2c9f9662011-02-22 13:49:00 +0000185 !CurTokenLexer)
Douglas Gregora771f462010-03-31 17:46:05 +0000186 Hint = FixItHint::CreateInsertion(Tmp.getLocation(),"//");
187 Diag(Tmp, diag::ext_pp_extra_tokens_at_eol) << DirType << Hint;
Chris Lattnerf64b3522008-03-09 01:54:53 +0000188 DiscardUntilEndOfDirective();
189 }
190}
191
192
193
194/// SkipExcludedConditionalBlock - We just read a #if or related directive and
195/// decided that the subsequent tokens are in the #if'd out portion of the
196/// file. Lex the rest of the file, until we see an #endif. If
197/// FoundNonSkipPortion is true, then we have already emitted code for part of
198/// this #if directive, so #else/#elif blocks should never be entered. If ElseOk
199/// is true, then #else directives are ok, if not, then we have already seen one
200/// so a #else directive is a duplicate. When this returns, the caller can lex
201/// the first valid token.
202void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc,
203 bool FoundNonSkipPortion,
Argyrios Kyrtzidis18bcfd52011-09-27 17:32:05 +0000204 bool FoundElse,
205 SourceLocation ElseLoc) {
Chris Lattnerf64b3522008-03-09 01:54:53 +0000206 ++NumSkipped;
Ted Kremenek6b732912008-11-18 01:04:47 +0000207 assert(CurTokenLexer == 0 && CurPPLexer && "Lexing a macro, not a file?");
Chris Lattnerf64b3522008-03-09 01:54:53 +0000208
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000209 CurPPLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/false,
Chris Lattnerf64b3522008-03-09 01:54:53 +0000210 FoundNonSkipPortion, FoundElse);
Mike Stump11289f42009-09-09 15:08:12 +0000211
Ted Kremenek56572ab2008-12-12 18:34:08 +0000212 if (CurPTHLexer) {
213 PTHSkipExcludedConditionalBlock();
214 return;
215 }
Mike Stump11289f42009-09-09 15:08:12 +0000216
Chris Lattnerf64b3522008-03-09 01:54:53 +0000217 // Enter raw mode to disable identifier lookup (and thus macro expansion),
218 // disabling warnings, etc.
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000219 CurPPLexer->LexingRawMode = true;
Chris Lattnerf64b3522008-03-09 01:54:53 +0000220 Token Tok;
221 while (1) {
Chris Lattnerf406b242010-01-18 22:33:01 +0000222 CurLexer->Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000223
Douglas Gregor3a7ad252010-08-24 19:08:16 +0000224 if (Tok.is(tok::code_completion)) {
225 if (CodeComplete)
226 CodeComplete->CodeCompleteInConditionalExclusion();
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000227 setCodeCompletionReached();
Douglas Gregor3a7ad252010-08-24 19:08:16 +0000228 continue;
229 }
230
Chris Lattnerf64b3522008-03-09 01:54:53 +0000231 // If this is the end of the buffer, we have an error.
232 if (Tok.is(tok::eof)) {
233 // Emit errors for each unterminated conditional on the stack, including
234 // the current one.
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000235 while (!CurPPLexer->ConditionalStack.empty()) {
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000236 if (CurLexer->getFileLoc() != CodeCompletionFileLoc)
Douglas Gregor02690ba2010-08-12 17:04:55 +0000237 Diag(CurPPLexer->ConditionalStack.back().IfLoc,
238 diag::err_pp_unterminated_conditional);
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000239 CurPPLexer->ConditionalStack.pop_back();
Mike Stump11289f42009-09-09 15:08:12 +0000240 }
241
Chris Lattnerf64b3522008-03-09 01:54:53 +0000242 // Just return and let the caller lex after this #include.
243 break;
244 }
Mike Stump11289f42009-09-09 15:08:12 +0000245
Chris Lattnerf64b3522008-03-09 01:54:53 +0000246 // If this token is not a preprocessor directive, just skip it.
247 if (Tok.isNot(tok::hash) || !Tok.isAtStartOfLine())
248 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000249
Chris Lattnerf64b3522008-03-09 01:54:53 +0000250 // We just parsed a # character at the start of a line, so we're in
251 // directive mode. Tell the lexer this so any newlines we see will be
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000252 // converted into an EOD token (this terminates the macro).
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000253 CurPPLexer->ParsingPreprocessorDirective = true;
Ted Kremenek59e003e2008-11-18 00:43:07 +0000254 if (CurLexer) CurLexer->SetCommentRetentionState(false);
Chris Lattnerf64b3522008-03-09 01:54:53 +0000255
Mike Stump11289f42009-09-09 15:08:12 +0000256
Chris Lattnerf64b3522008-03-09 01:54:53 +0000257 // Read the next token, the directive flavor.
258 LexUnexpandedToken(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000259
Chris Lattnerf64b3522008-03-09 01:54:53 +0000260 // If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or
261 // something bogus), skip it.
Abramo Bagnaraea4f7c72010-12-22 08:23:18 +0000262 if (Tok.isNot(tok::raw_identifier)) {
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000263 CurPPLexer->ParsingPreprocessorDirective = false;
Chris Lattnerf64b3522008-03-09 01:54:53 +0000264 // Restore comment saving mode.
Ted Kremenek59e003e2008-11-18 00:43:07 +0000265 if (CurLexer) CurLexer->SetCommentRetentionState(KeepComments);
Chris Lattnerf64b3522008-03-09 01:54:53 +0000266 continue;
267 }
268
269 // If the first letter isn't i or e, it isn't intesting to us. We know that
270 // this is safe in the face of spelling differences, because there is no way
271 // to spell an i/e in a strange way that is another letter. Skipping this
272 // allows us to avoid looking up the identifier info for #define/#undef and
273 // other common directives.
Abramo Bagnaraea4f7c72010-12-22 08:23:18 +0000274 const char *RawCharData = Tok.getRawIdentifierData();
275
Chris Lattnerf64b3522008-03-09 01:54:53 +0000276 char FirstChar = RawCharData[0];
Mike Stump11289f42009-09-09 15:08:12 +0000277 if (FirstChar >= 'a' && FirstChar <= 'z' &&
Chris Lattnerf64b3522008-03-09 01:54:53 +0000278 FirstChar != 'i' && FirstChar != 'e') {
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000279 CurPPLexer->ParsingPreprocessorDirective = false;
Chris Lattnerf64b3522008-03-09 01:54:53 +0000280 // Restore comment saving mode.
Ted Kremenek59e003e2008-11-18 00:43:07 +0000281 if (CurLexer) CurLexer->SetCommentRetentionState(KeepComments);
Chris Lattnerf64b3522008-03-09 01:54:53 +0000282 continue;
283 }
Mike Stump11289f42009-09-09 15:08:12 +0000284
Chris Lattnerf64b3522008-03-09 01:54:53 +0000285 // Get the identifier name without trigraphs or embedded newlines. Note
286 // that we can't use Tok.getIdentifierInfo() because its lookup is disabled
287 // when skipping.
Benjamin Kramer144884642009-12-31 13:32:38 +0000288 char DirectiveBuf[20];
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000289 StringRef Directive;
Chris Lattnerf64b3522008-03-09 01:54:53 +0000290 if (!Tok.needsCleaning() && Tok.getLength() < 20) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000291 Directive = StringRef(RawCharData, Tok.getLength());
Chris Lattnerf64b3522008-03-09 01:54:53 +0000292 } else {
293 std::string DirectiveStr = getSpelling(Tok);
Benjamin Kramer144884642009-12-31 13:32:38 +0000294 unsigned IdLen = DirectiveStr.size();
Chris Lattnerf64b3522008-03-09 01:54:53 +0000295 if (IdLen >= 20) {
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000296 CurPPLexer->ParsingPreprocessorDirective = false;
Chris Lattnerf64b3522008-03-09 01:54:53 +0000297 // Restore comment saving mode.
Ted Kremenek59e003e2008-11-18 00:43:07 +0000298 if (CurLexer) CurLexer->SetCommentRetentionState(KeepComments);
Chris Lattnerf64b3522008-03-09 01:54:53 +0000299 continue;
300 }
Benjamin Kramer144884642009-12-31 13:32:38 +0000301 memcpy(DirectiveBuf, &DirectiveStr[0], IdLen);
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000302 Directive = StringRef(DirectiveBuf, IdLen);
Chris Lattnerf64b3522008-03-09 01:54:53 +0000303 }
Mike Stump11289f42009-09-09 15:08:12 +0000304
Benjamin Kramer144884642009-12-31 13:32:38 +0000305 if (Directive.startswith("if")) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000306 StringRef Sub = Directive.substr(2);
Benjamin Kramer144884642009-12-31 13:32:38 +0000307 if (Sub.empty() || // "if"
308 Sub == "def" || // "ifdef"
309 Sub == "ndef") { // "ifndef"
Chris Lattnerf64b3522008-03-09 01:54:53 +0000310 // We know the entire #if/#ifdef/#ifndef block will be skipped, don't
311 // bother parsing the condition.
312 DiscardUntilEndOfDirective();
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000313 CurPPLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true,
Chris Lattnerf64b3522008-03-09 01:54:53 +0000314 /*foundnonskip*/false,
Chandler Carruth540960f2011-01-03 17:40:17 +0000315 /*foundelse*/false);
Chris Lattnerf64b3522008-03-09 01:54:53 +0000316 }
Benjamin Kramer144884642009-12-31 13:32:38 +0000317 } else if (Directive[0] == 'e') {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000318 StringRef Sub = Directive.substr(1);
Benjamin Kramer144884642009-12-31 13:32:38 +0000319 if (Sub == "ndif") { // "endif"
Chris Lattnerce2ab6f2009-04-14 05:07:49 +0000320 CheckEndOfDirective("endif");
Chris Lattnerf64b3522008-03-09 01:54:53 +0000321 PPConditionalInfo CondInfo;
322 CondInfo.WasSkipping = true; // Silence bogus warning.
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000323 bool InCond = CurPPLexer->popConditionalLevel(CondInfo);
Jeffrey Yasskinb3321532010-12-23 01:01:28 +0000324 (void)InCond; // Silence warning in no-asserts mode.
Chris Lattnerf64b3522008-03-09 01:54:53 +0000325 assert(!InCond && "Can't be skipping if not in a conditional!");
Mike Stump11289f42009-09-09 15:08:12 +0000326
Chris Lattnerf64b3522008-03-09 01:54:53 +0000327 // If we popped the outermost skipping block, we're done skipping!
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +0000328 if (!CondInfo.WasSkipping) {
329 if (Callbacks)
330 Callbacks->Endif(Tok.getLocation(), CondInfo.IfLoc);
Chris Lattnerf64b3522008-03-09 01:54:53 +0000331 break;
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +0000332 }
Benjamin Kramer144884642009-12-31 13:32:38 +0000333 } else if (Sub == "lse") { // "else".
Chris Lattnerf64b3522008-03-09 01:54:53 +0000334 // #else directive in a skipping conditional. If not in some other
335 // skipping conditional, and if #else hasn't already been seen, enter it
336 // as a non-skipping conditional.
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000337 PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel();
Mike Stump11289f42009-09-09 15:08:12 +0000338
Chris Lattnerf64b3522008-03-09 01:54:53 +0000339 // If this is a #else with a #else before it, report the error.
340 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_else_after_else);
Mike Stump11289f42009-09-09 15:08:12 +0000341
Chris Lattnerf64b3522008-03-09 01:54:53 +0000342 // Note that we've seen a #else in this conditional.
343 CondInfo.FoundElse = true;
Mike Stump11289f42009-09-09 15:08:12 +0000344
Chris Lattnerf64b3522008-03-09 01:54:53 +0000345 // If the conditional is at the top level, and the #if block wasn't
346 // entered, enter the #else block now.
347 if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) {
348 CondInfo.FoundNonSkip = true;
Argyrios Kyrtzidis627c14a2011-05-21 04:26:04 +0000349 CheckEndOfDirective("else");
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +0000350 if (Callbacks)
351 Callbacks->Else(Tok.getLocation(), CondInfo.IfLoc);
Chris Lattnerf64b3522008-03-09 01:54:53 +0000352 break;
Argyrios Kyrtzidis627c14a2011-05-21 04:26:04 +0000353 } else {
354 DiscardUntilEndOfDirective(); // C99 6.10p4.
Chris Lattnerf64b3522008-03-09 01:54:53 +0000355 }
Benjamin Kramer144884642009-12-31 13:32:38 +0000356 } else if (Sub == "lif") { // "elif".
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000357 PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel();
Chris Lattnerf64b3522008-03-09 01:54:53 +0000358
359 bool ShouldEnter;
Chandler Carruth540960f2011-01-03 17:40:17 +0000360 const SourceLocation ConditionalBegin = CurPPLexer->getSourceLocation();
Chris Lattnerf64b3522008-03-09 01:54:53 +0000361 // If this is in a skipping block or if we're already handled this #if
362 // block, don't bother parsing the condition.
363 if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) {
364 DiscardUntilEndOfDirective();
365 ShouldEnter = false;
366 } else {
367 // Restore the value of LexingRawMode so that identifiers are
368 // looked up, etc, inside the #elif expression.
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000369 assert(CurPPLexer->LexingRawMode && "We have to be skipping here!");
370 CurPPLexer->LexingRawMode = false;
Chris Lattnerf64b3522008-03-09 01:54:53 +0000371 IdentifierInfo *IfNDefMacro = 0;
372 ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro);
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000373 CurPPLexer->LexingRawMode = true;
Chris Lattnerf64b3522008-03-09 01:54:53 +0000374 }
Chandler Carruth540960f2011-01-03 17:40:17 +0000375 const SourceLocation ConditionalEnd = CurPPLexer->getSourceLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000376
Chris Lattnerf64b3522008-03-09 01:54:53 +0000377 // If this is a #elif with a #else before it, report the error.
378 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_elif_after_else);
Mike Stump11289f42009-09-09 15:08:12 +0000379
Chris Lattnerf64b3522008-03-09 01:54:53 +0000380 // If this condition is true, enter it!
381 if (ShouldEnter) {
382 CondInfo.FoundNonSkip = true;
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +0000383 if (Callbacks)
384 Callbacks->Elif(Tok.getLocation(),
385 SourceRange(ConditionalBegin, ConditionalEnd),
386 CondInfo.IfLoc);
Chris Lattnerf64b3522008-03-09 01:54:53 +0000387 break;
388 }
389 }
390 }
Mike Stump11289f42009-09-09 15:08:12 +0000391
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000392 CurPPLexer->ParsingPreprocessorDirective = false;
Chris Lattnerf64b3522008-03-09 01:54:53 +0000393 // Restore comment saving mode.
Ted Kremenek59e003e2008-11-18 00:43:07 +0000394 if (CurLexer) CurLexer->SetCommentRetentionState(KeepComments);
Chris Lattnerf64b3522008-03-09 01:54:53 +0000395 }
396
397 // Finally, if we are out of the conditional (saw an #endif or ran off the end
398 // of the file, just stop skipping and return to lexing whatever came after
399 // the #if block.
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000400 CurPPLexer->LexingRawMode = false;
Argyrios Kyrtzidis18bcfd52011-09-27 17:32:05 +0000401
402 if (Callbacks) {
403 SourceLocation BeginLoc = ElseLoc.isValid() ? ElseLoc : IfTokenLoc;
404 Callbacks->SourceRangeSkipped(SourceRange(BeginLoc, Tok.getLocation()));
405 }
Chris Lattnerf64b3522008-03-09 01:54:53 +0000406}
407
Ted Kremenek56572ab2008-12-12 18:34:08 +0000408void Preprocessor::PTHSkipExcludedConditionalBlock() {
Mike Stump11289f42009-09-09 15:08:12 +0000409
410 while (1) {
Ted Kremenek56572ab2008-12-12 18:34:08 +0000411 assert(CurPTHLexer);
412 assert(CurPTHLexer->LexingRawMode == false);
Mike Stump11289f42009-09-09 15:08:12 +0000413
Ted Kremenek56572ab2008-12-12 18:34:08 +0000414 // Skip to the next '#else', '#elif', or #endif.
415 if (CurPTHLexer->SkipBlock()) {
416 // We have reached an #endif. Both the '#' and 'endif' tokens
417 // have been consumed by the PTHLexer. Just pop off the condition level.
418 PPConditionalInfo CondInfo;
419 bool InCond = CurPTHLexer->popConditionalLevel(CondInfo);
Jeffrey Yasskinb3321532010-12-23 01:01:28 +0000420 (void)InCond; // Silence warning in no-asserts mode.
Ted Kremenek56572ab2008-12-12 18:34:08 +0000421 assert(!InCond && "Can't be skipping if not in a conditional!");
422 break;
423 }
Mike Stump11289f42009-09-09 15:08:12 +0000424
Ted Kremenek56572ab2008-12-12 18:34:08 +0000425 // We have reached a '#else' or '#elif'. Lex the next token to get
426 // the directive flavor.
427 Token Tok;
428 LexUnexpandedToken(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000429
Ted Kremenek56572ab2008-12-12 18:34:08 +0000430 // We can actually look up the IdentifierInfo here since we aren't in
431 // raw mode.
432 tok::PPKeywordKind K = Tok.getIdentifierInfo()->getPPKeywordID();
433
434 if (K == tok::pp_else) {
435 // #else: Enter the else condition. We aren't in a nested condition
436 // since we skip those. We're always in the one matching the last
437 // blocked we skipped.
438 PPConditionalInfo &CondInfo = CurPTHLexer->peekConditionalLevel();
439 // Note that we've seen a #else in this conditional.
440 CondInfo.FoundElse = true;
Mike Stump11289f42009-09-09 15:08:12 +0000441
Ted Kremenek56572ab2008-12-12 18:34:08 +0000442 // If the #if block wasn't entered then enter the #else block now.
443 if (!CondInfo.FoundNonSkip) {
444 CondInfo.FoundNonSkip = true;
Mike Stump11289f42009-09-09 15:08:12 +0000445
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000446 // Scan until the eod token.
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000447 CurPTHLexer->ParsingPreprocessorDirective = true;
Daniel Dunbar2cba6be2009-04-13 17:57:49 +0000448 DiscardUntilEndOfDirective();
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000449 CurPTHLexer->ParsingPreprocessorDirective = false;
Mike Stump11289f42009-09-09 15:08:12 +0000450
Ted Kremenek56572ab2008-12-12 18:34:08 +0000451 break;
452 }
Mike Stump11289f42009-09-09 15:08:12 +0000453
Ted Kremenek56572ab2008-12-12 18:34:08 +0000454 // Otherwise skip this block.
455 continue;
456 }
Mike Stump11289f42009-09-09 15:08:12 +0000457
Ted Kremenek56572ab2008-12-12 18:34:08 +0000458 assert(K == tok::pp_elif);
459 PPConditionalInfo &CondInfo = CurPTHLexer->peekConditionalLevel();
460
461 // If this is a #elif with a #else before it, report the error.
462 if (CondInfo.FoundElse)
463 Diag(Tok, diag::pp_err_elif_after_else);
Mike Stump11289f42009-09-09 15:08:12 +0000464
Ted Kremenek56572ab2008-12-12 18:34:08 +0000465 // If this is in a skipping block or if we're already handled this #if
Mike Stump11289f42009-09-09 15:08:12 +0000466 // block, don't bother parsing the condition. We just skip this block.
Ted Kremenek56572ab2008-12-12 18:34:08 +0000467 if (CondInfo.FoundNonSkip)
468 continue;
469
470 // Evaluate the condition of the #elif.
471 IdentifierInfo *IfNDefMacro = 0;
472 CurPTHLexer->ParsingPreprocessorDirective = true;
473 bool ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro);
474 CurPTHLexer->ParsingPreprocessorDirective = false;
475
476 // If this condition is true, enter it!
477 if (ShouldEnter) {
478 CondInfo.FoundNonSkip = true;
479 break;
480 }
481
482 // Otherwise, skip this block and go to the next one.
483 continue;
484 }
485}
486
Chris Lattnerf7ad82d2008-03-09 04:17:44 +0000487/// LookupFile - Given a "foo" or <foo> reference, look up the indicated file,
488/// return null on failure. isAngled indicates whether the file reference is
489/// for system #include's or not (i.e. using <> instead of "").
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000490const FileEntry *Preprocessor::LookupFile(
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000491 StringRef Filename,
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000492 bool isAngled,
493 const DirectoryLookup *FromDir,
494 const DirectoryLookup *&CurDir,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000495 SmallVectorImpl<char> *SearchPath,
Douglas Gregor97eec242011-09-15 22:00:41 +0000496 SmallVectorImpl<char> *RelativePath,
Douglas Gregorde3ef502011-11-30 23:21:26 +0000497 Module **SuggestedModule,
Douglas Gregor8ad31c22011-11-20 17:46:46 +0000498 bool SkipCache) {
Chris Lattnerf7ad82d2008-03-09 04:17:44 +0000499 // If the header lookup mechanism may be relative to the current file, pass in
500 // info about where the current file is.
Douglas Gregor618e64a2010-08-08 07:49:23 +0000501 const FileEntry *CurFileEnt = 0;
Chris Lattnerf7ad82d2008-03-09 04:17:44 +0000502 if (!FromDir) {
Chris Lattnerd32480d2009-01-17 06:22:33 +0000503 FileID FID = getCurrentFileLexer()->getFileID();
Douglas Gregor618e64a2010-08-08 07:49:23 +0000504 CurFileEnt = SourceMgr.getFileEntryForID(FID);
Mike Stump11289f42009-09-09 15:08:12 +0000505
Chris Lattner022923a2009-02-04 19:45:07 +0000506 // If there is no file entry associated with this file, it must be the
507 // predefines buffer. Any other file is not lexed with a normal lexer, so
Douglas Gregor618e64a2010-08-08 07:49:23 +0000508 // it won't be scanned for preprocessor directives. If we have the
509 // predefines buffer, resolve #include references (which come from the
510 // -include command line argument) as if they came from the main file, this
511 // affects file lookup etc.
512 if (CurFileEnt == 0) {
Chris Lattner022923a2009-02-04 19:45:07 +0000513 FID = SourceMgr.getMainFileID();
514 CurFileEnt = SourceMgr.getFileEntryForID(FID);
515 }
Chris Lattnerf7ad82d2008-03-09 04:17:44 +0000516 }
Mike Stump11289f42009-09-09 15:08:12 +0000517
Chris Lattnerf7ad82d2008-03-09 04:17:44 +0000518 // Do a standard file entry lookup.
519 CurDir = CurDirLookup;
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000520 const FileEntry *FE = HeaderInfo.LookupFile(
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000521 Filename, isAngled, FromDir, CurDir, CurFileEnt,
Douglas Gregor8ad31c22011-11-20 17:46:46 +0000522 SearchPath, RelativePath, SuggestedModule, SkipCache);
Chris Lattnerfde85352010-01-22 00:14:44 +0000523 if (FE) return FE;
Mike Stump11289f42009-09-09 15:08:12 +0000524
Chris Lattnerf7ad82d2008-03-09 04:17:44 +0000525 // Otherwise, see if this is a subframework header. If so, this is relative
526 // to one of the headers on the #include stack. Walk the list of the current
527 // headers on the #include stack and pass them to HeaderInfo.
Douglas Gregor97eec242011-09-15 22:00:41 +0000528 // FIXME: SuggestedModule!
Ted Kremenek6bc5f3e2008-11-20 16:19:53 +0000529 if (IsFileLexer()) {
Ted Kremenek45245212008-11-19 21:57:25 +0000530 if ((CurFileEnt = SourceMgr.getFileEntryForID(CurPPLexer->getFileID())))
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000531 if ((FE = HeaderInfo.LookupSubframeworkHeader(Filename, CurFileEnt,
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000532 SearchPath, RelativePath)))
Chris Lattnerf7ad82d2008-03-09 04:17:44 +0000533 return FE;
534 }
Mike Stump11289f42009-09-09 15:08:12 +0000535
Chris Lattnerf7ad82d2008-03-09 04:17:44 +0000536 for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) {
537 IncludeStackInfo &ISEntry = IncludeMacroStack[e-i-1];
Ted Kremenek6bc5f3e2008-11-20 16:19:53 +0000538 if (IsFileLexer(ISEntry)) {
Mike Stump11289f42009-09-09 15:08:12 +0000539 if ((CurFileEnt =
Ted Kremenek45245212008-11-19 21:57:25 +0000540 SourceMgr.getFileEntryForID(ISEntry.ThePPLexer->getFileID())))
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000541 if ((FE = HeaderInfo.LookupSubframeworkHeader(
542 Filename, CurFileEnt, SearchPath, RelativePath)))
Chris Lattnerf7ad82d2008-03-09 04:17:44 +0000543 return FE;
544 }
545 }
Mike Stump11289f42009-09-09 15:08:12 +0000546
Chris Lattnerf7ad82d2008-03-09 04:17:44 +0000547 // Otherwise, we really couldn't find the file.
548 return 0;
549}
550
Chris Lattnerf64b3522008-03-09 01:54:53 +0000551
552//===----------------------------------------------------------------------===//
553// Preprocessor Directive Handling.
554//===----------------------------------------------------------------------===//
555
556/// HandleDirective - This callback is invoked when the lexer sees a # token
Mike Stump11289f42009-09-09 15:08:12 +0000557/// at the start of a line. This consumes the directive, modifies the
Chris Lattnerf64b3522008-03-09 01:54:53 +0000558/// lexer/preprocessor state, and advances the lexer(s) so that the next token
559/// read is the correct one.
560void Preprocessor::HandleDirective(Token &Result) {
561 // FIXME: Traditional: # with whitespace before it not recognized by K&R?
Mike Stump11289f42009-09-09 15:08:12 +0000562
Chris Lattnerf64b3522008-03-09 01:54:53 +0000563 // We just parsed a # character at the start of a line, so we're in directive
564 // mode. Tell the lexer this so any newlines we see will be converted into an
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000565 // EOD token (which terminates the directive).
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000566 CurPPLexer->ParsingPreprocessorDirective = true;
Mike Stump11289f42009-09-09 15:08:12 +0000567
Chris Lattnerf64b3522008-03-09 01:54:53 +0000568 ++NumDirectives;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000569
Chris Lattnerf64b3522008-03-09 01:54:53 +0000570 // We are about to read a token. For the multiple-include optimization FA to
Mike Stump11289f42009-09-09 15:08:12 +0000571 // work, we have to remember if we had read any tokens *before* this
Chris Lattnerf64b3522008-03-09 01:54:53 +0000572 // pp-directive.
Chris Lattner8cf1f932009-12-14 04:54:40 +0000573 bool ReadAnyTokensBeforeDirective =CurPPLexer->MIOpt.getHasReadAnyTokensVal();
Mike Stump11289f42009-09-09 15:08:12 +0000574
Chris Lattner2d17ab72009-03-18 21:00:25 +0000575 // Save the '#' token in case we need to return it later.
576 Token SavedHash = Result;
Mike Stump11289f42009-09-09 15:08:12 +0000577
Chris Lattnerf64b3522008-03-09 01:54:53 +0000578 // Read the next token, the directive flavor. This isn't expanded due to
579 // C99 6.10.3p8.
580 LexUnexpandedToken(Result);
Mike Stump11289f42009-09-09 15:08:12 +0000581
Chris Lattnerf64b3522008-03-09 01:54:53 +0000582 // C99 6.10.3p11: Is this preprocessor directive in macro invocation? e.g.:
583 // #define A(x) #x
584 // A(abc
585 // #warning blah
586 // def)
Richard Smitheb3ce7c2011-12-16 22:50:01 +0000587 // If so, the user is relying on undefined behavior, emit a diagnostic. Do
588 // not support this for #include-like directives, since that can result in
589 // terrible diagnostics, and does not work in GCC.
590 if (InMacroArgs) {
591 if (IdentifierInfo *II = Result.getIdentifierInfo()) {
592 switch (II->getPPKeywordID()) {
593 case tok::pp_include:
594 case tok::pp_import:
595 case tok::pp_include_next:
596 case tok::pp___include_macros:
597 Diag(Result, diag::err_embedded_include) << II->getName();
598 DiscardUntilEndOfDirective();
599 return;
600 default:
601 break;
602 }
603 }
Chris Lattnerf64b3522008-03-09 01:54:53 +0000604 Diag(Result, diag::ext_embedded_directive);
Richard Smitheb3ce7c2011-12-16 22:50:01 +0000605 }
Mike Stump11289f42009-09-09 15:08:12 +0000606
Chris Lattnerf64b3522008-03-09 01:54:53 +0000607TryAgain:
608 switch (Result.getKind()) {
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000609 case tok::eod:
Chris Lattnerf64b3522008-03-09 01:54:53 +0000610 return; // null directive.
611 case tok::comment:
612 // Handle stuff like "# /*foo*/ define X" in -E -C mode.
613 LexUnexpandedToken(Result);
614 goto TryAgain;
Douglas Gregor3a7ad252010-08-24 19:08:16 +0000615 case tok::code_completion:
616 if (CodeComplete)
617 CodeComplete->CodeCompleteDirective(
618 CurPPLexer->getConditionalStackDepth() > 0);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000619 setCodeCompletionReached();
Douglas Gregor3a7ad252010-08-24 19:08:16 +0000620 return;
Chris Lattner76e68962009-01-26 06:19:46 +0000621 case tok::numeric_constant: // # 7 GNU line marker directive.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000622 if (getLangOpts().AsmPreprocessor)
Chris Lattner5eb8ae22009-03-18 20:41:10 +0000623 break; // # 4 is not a preprocessor directive in .S files.
Chris Lattner76e68962009-01-26 06:19:46 +0000624 return HandleDigitDirective(Result);
Chris Lattnerf64b3522008-03-09 01:54:53 +0000625 default:
626 IdentifierInfo *II = Result.getIdentifierInfo();
627 if (II == 0) break; // Not an identifier.
Mike Stump11289f42009-09-09 15:08:12 +0000628
Chris Lattnerf64b3522008-03-09 01:54:53 +0000629 // Ask what the preprocessor keyword ID is.
630 switch (II->getPPKeywordID()) {
631 default: break;
632 // C99 6.10.1 - Conditional Inclusion.
633 case tok::pp_if:
634 return HandleIfDirective(Result, ReadAnyTokensBeforeDirective);
635 case tok::pp_ifdef:
636 return HandleIfdefDirective(Result, false, true/*not valid for miopt*/);
637 case tok::pp_ifndef:
638 return HandleIfdefDirective(Result, true, ReadAnyTokensBeforeDirective);
639 case tok::pp_elif:
640 return HandleElifDirective(Result);
641 case tok::pp_else:
642 return HandleElseDirective(Result);
643 case tok::pp_endif:
644 return HandleEndifDirective(Result);
Mike Stump11289f42009-09-09 15:08:12 +0000645
Chris Lattnerf64b3522008-03-09 01:54:53 +0000646 // C99 6.10.2 - Source File Inclusion.
647 case tok::pp_include:
Douglas Gregor796d76a2010-10-20 22:00:55 +0000648 // Handle #include.
649 return HandleIncludeDirective(SavedHash.getLocation(), Result);
Chris Lattner14a7f392009-04-08 18:24:34 +0000650 case tok::pp___include_macros:
Douglas Gregor796d76a2010-10-20 22:00:55 +0000651 // Handle -imacros.
652 return HandleIncludeMacrosDirective(SavedHash.getLocation(), Result);
Mike Stump11289f42009-09-09 15:08:12 +0000653
Chris Lattnerf64b3522008-03-09 01:54:53 +0000654 // C99 6.10.3 - Macro Replacement.
655 case tok::pp_define:
656 return HandleDefineDirective(Result);
657 case tok::pp_undef:
658 return HandleUndefDirective(Result);
659
660 // C99 6.10.4 - Line Control.
661 case tok::pp_line:
Chris Lattner100c65e2009-01-26 05:29:08 +0000662 return HandleLineDirective(Result);
Mike Stump11289f42009-09-09 15:08:12 +0000663
Chris Lattnerf64b3522008-03-09 01:54:53 +0000664 // C99 6.10.5 - Error Directive.
665 case tok::pp_error:
666 return HandleUserDiagnosticDirective(Result, false);
Mike Stump11289f42009-09-09 15:08:12 +0000667
Chris Lattnerf64b3522008-03-09 01:54:53 +0000668 // C99 6.10.6 - Pragma Directive.
669 case tok::pp_pragma:
Douglas Gregorc7d65762010-09-09 22:45:38 +0000670 return HandlePragmaDirective(PIK_HashPragma);
Mike Stump11289f42009-09-09 15:08:12 +0000671
Chris Lattnerf64b3522008-03-09 01:54:53 +0000672 // GNU Extensions.
673 case tok::pp_import:
Douglas Gregor796d76a2010-10-20 22:00:55 +0000674 return HandleImportDirective(SavedHash.getLocation(), Result);
Chris Lattnerf64b3522008-03-09 01:54:53 +0000675 case tok::pp_include_next:
Douglas Gregor796d76a2010-10-20 22:00:55 +0000676 return HandleIncludeNextDirective(SavedHash.getLocation(), Result);
Mike Stump11289f42009-09-09 15:08:12 +0000677
Chris Lattnerf64b3522008-03-09 01:54:53 +0000678 case tok::pp_warning:
679 Diag(Result, diag::ext_pp_warning_directive);
680 return HandleUserDiagnosticDirective(Result, true);
681 case tok::pp_ident:
682 return HandleIdentSCCSDirective(Result);
683 case tok::pp_sccs:
684 return HandleIdentSCCSDirective(Result);
685 case tok::pp_assert:
686 //isExtension = true; // FIXME: implement #assert
687 break;
688 case tok::pp_unassert:
689 //isExtension = true; // FIXME: implement #unassert
690 break;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +0000691
Douglas Gregor663b48f2012-01-03 19:48:16 +0000692 case tok::pp___public_macro:
David Blaikiebbafb8a2012-03-11 07:00:24 +0000693 if (getLangOpts().Modules)
Douglas Gregor0bf886d2012-01-03 18:24:14 +0000694 return HandleMacroPublicDirective(Result);
695 break;
696
Douglas Gregor663b48f2012-01-03 19:48:16 +0000697 case tok::pp___private_macro:
David Blaikiebbafb8a2012-03-11 07:00:24 +0000698 if (getLangOpts().Modules)
Douglas Gregor0bf886d2012-01-03 18:24:14 +0000699 return HandleMacroPrivateDirective(Result);
700 break;
Chris Lattnerf64b3522008-03-09 01:54:53 +0000701 }
702 break;
703 }
Mike Stump11289f42009-09-09 15:08:12 +0000704
Chris Lattner2d17ab72009-03-18 21:00:25 +0000705 // If this is a .S file, treat unknown # directives as non-preprocessor
706 // directives. This is important because # may be a comment or introduce
707 // various pseudo-ops. Just return the # token and push back the following
708 // token to be lexed next time.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000709 if (getLangOpts().AsmPreprocessor) {
Daniel Dunbar48b4d1e2009-07-13 21:48:50 +0000710 Token *Toks = new Token[2];
Chris Lattner2d17ab72009-03-18 21:00:25 +0000711 // Return the # and the token after it.
Mike Stump11289f42009-09-09 15:08:12 +0000712 Toks[0] = SavedHash;
Chris Lattner2d17ab72009-03-18 21:00:25 +0000713 Toks[1] = Result;
Chris Lattner56f64c12011-01-06 05:01:51 +0000714
715 // If the second token is a hashhash token, then we need to translate it to
716 // unknown so the token lexer doesn't try to perform token pasting.
717 if (Result.is(tok::hashhash))
718 Toks[1].setKind(tok::unknown);
719
Chris Lattner2d17ab72009-03-18 21:00:25 +0000720 // Enter this token stream so that we re-lex the tokens. Make sure to
721 // enable macro expansion, in case the token after the # is an identifier
722 // that is expanded.
723 EnterTokenStream(Toks, 2, false, true);
724 return;
725 }
Mike Stump11289f42009-09-09 15:08:12 +0000726
Chris Lattnerf64b3522008-03-09 01:54:53 +0000727 // If we reached here, the preprocessing token is not valid!
728 Diag(Result, diag::err_pp_invalid_directive);
Mike Stump11289f42009-09-09 15:08:12 +0000729
Chris Lattnerf64b3522008-03-09 01:54:53 +0000730 // Read the rest of the PP line.
731 DiscardUntilEndOfDirective();
Mike Stump11289f42009-09-09 15:08:12 +0000732
Chris Lattnerf64b3522008-03-09 01:54:53 +0000733 // Okay, we're done parsing the directive.
734}
735
Chris Lattner76e68962009-01-26 06:19:46 +0000736/// GetLineValue - Convert a numeric token into an unsigned value, emitting
737/// Diagnostic DiagID if it is invalid, and returning the value in Val.
738static bool GetLineValue(Token &DigitTok, unsigned &Val,
739 unsigned DiagID, Preprocessor &PP) {
740 if (DigitTok.isNot(tok::numeric_constant)) {
741 PP.Diag(DigitTok, DiagID);
Mike Stump11289f42009-09-09 15:08:12 +0000742
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000743 if (DigitTok.isNot(tok::eod))
Chris Lattner76e68962009-01-26 06:19:46 +0000744 PP.DiscardUntilEndOfDirective();
745 return true;
746 }
Mike Stump11289f42009-09-09 15:08:12 +0000747
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000748 SmallString<64> IntegerBuffer;
Chris Lattner76e68962009-01-26 06:19:46 +0000749 IntegerBuffer.resize(DigitTok.getLength());
750 const char *DigitTokBegin = &IntegerBuffer[0];
Douglas Gregordc970f02010-03-16 22:30:13 +0000751 bool Invalid = false;
752 unsigned ActualLength = PP.getSpelling(DigitTok, DigitTokBegin, &Invalid);
753 if (Invalid)
754 return true;
755
Chris Lattnerd66f1722009-04-18 18:35:15 +0000756 // Verify that we have a simple digit-sequence, and compute the value. This
757 // is always a simple digit string computed in decimal, so we do this manually
758 // here.
759 Val = 0;
760 for (unsigned i = 0; i != ActualLength; ++i) {
761 if (!isdigit(DigitTokBegin[i])) {
762 PP.Diag(PP.AdvanceToTokenCharacter(DigitTok.getLocation(), i),
763 diag::err_pp_line_digit_sequence);
764 PP.DiscardUntilEndOfDirective();
765 return true;
766 }
Mike Stump11289f42009-09-09 15:08:12 +0000767
Chris Lattnerd66f1722009-04-18 18:35:15 +0000768 unsigned NextVal = Val*10+(DigitTokBegin[i]-'0');
769 if (NextVal < Val) { // overflow.
770 PP.Diag(DigitTok, DiagID);
771 PP.DiscardUntilEndOfDirective();
772 return true;
773 }
774 Val = NextVal;
Chris Lattner76e68962009-01-26 06:19:46 +0000775 }
Mike Stump11289f42009-09-09 15:08:12 +0000776
777 // Reject 0, this is needed both by #line numbers and flags.
Chris Lattner76e68962009-01-26 06:19:46 +0000778 if (Val == 0) {
779 PP.Diag(DigitTok, DiagID);
780 PP.DiscardUntilEndOfDirective();
781 return true;
782 }
Mike Stump11289f42009-09-09 15:08:12 +0000783
Chris Lattnerd66f1722009-04-18 18:35:15 +0000784 if (DigitTokBegin[0] == '0')
785 PP.Diag(DigitTok.getLocation(), diag::warn_pp_line_decimal);
Mike Stump11289f42009-09-09 15:08:12 +0000786
Chris Lattner76e68962009-01-26 06:19:46 +0000787 return false;
788}
789
Mike Stump11289f42009-09-09 15:08:12 +0000790/// HandleLineDirective - Handle #line directive: C99 6.10.4. The two
Chris Lattner100c65e2009-01-26 05:29:08 +0000791/// acceptable forms are:
792/// # line digit-sequence
793/// # line digit-sequence "s-char-sequence"
794void Preprocessor::HandleLineDirective(Token &Tok) {
795 // Read the line # and string argument. Per C99 6.10.4p5, these tokens are
796 // expanded.
797 Token DigitTok;
798 Lex(DigitTok);
799
Chris Lattner100c65e2009-01-26 05:29:08 +0000800 // Validate the number and convert it to an unsigned.
Chris Lattner76e68962009-01-26 06:19:46 +0000801 unsigned LineNo;
Chris Lattnerd66f1722009-04-18 18:35:15 +0000802 if (GetLineValue(DigitTok, LineNo, diag::err_pp_line_requires_integer,*this))
Chris Lattner100c65e2009-01-26 05:29:08 +0000803 return;
Chris Lattner100c65e2009-01-26 05:29:08 +0000804
Chris Lattner76e68962009-01-26 06:19:46 +0000805 // Enforce C99 6.10.4p3: "The digit sequence shall not specify ... a
806 // number greater than 2147483647". C90 requires that the line # be <= 32767.
Eli Friedman192e0342011-10-10 23:35:28 +0000807 unsigned LineLimit = 32768U;
David Blaikiebbafb8a2012-03-11 07:00:24 +0000808 if (LangOpts.C99 || LangOpts.CPlusPlus0x)
Eli Friedman192e0342011-10-10 23:35:28 +0000809 LineLimit = 2147483648U;
Chris Lattner100c65e2009-01-26 05:29:08 +0000810 if (LineNo >= LineLimit)
811 Diag(DigitTok, diag::ext_pp_line_too_big) << LineLimit;
David Blaikiebbafb8a2012-03-11 07:00:24 +0000812 else if (LangOpts.CPlusPlus0x && LineNo >= 32768U)
Richard Smithacd4d3d2011-10-15 01:18:56 +0000813 Diag(DigitTok, diag::warn_cxx98_compat_pp_line_too_big);
Mike Stump11289f42009-09-09 15:08:12 +0000814
Chris Lattnerb5fba6f2009-01-26 07:57:50 +0000815 int FilenameID = -1;
Chris Lattner100c65e2009-01-26 05:29:08 +0000816 Token StrTok;
817 Lex(StrTok);
818
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000819 // If the StrTok is "eod", then it wasn't present. Otherwise, it must be a
820 // string followed by eod.
821 if (StrTok.is(tok::eod))
Chris Lattner100c65e2009-01-26 05:29:08 +0000822 ; // ok
823 else if (StrTok.isNot(tok::string_literal)) {
824 Diag(StrTok, diag::err_pp_line_invalid_filename);
Richard Smithd67aea22012-03-06 03:21:47 +0000825 return DiscardUntilEndOfDirective();
826 } else if (StrTok.hasUDSuffix()) {
827 Diag(StrTok, diag::err_invalid_string_udl);
828 return DiscardUntilEndOfDirective();
Chris Lattner100c65e2009-01-26 05:29:08 +0000829 } else {
Chris Lattnerb5fba6f2009-01-26 07:57:50 +0000830 // Parse and validate the string, converting it into a unique ID.
831 StringLiteralParser Literal(&StrTok, 1, *this);
Douglas Gregorfb65e592011-07-27 05:40:30 +0000832 assert(Literal.isAscii() && "Didn't allow wide strings in");
Chris Lattnerb5fba6f2009-01-26 07:57:50 +0000833 if (Literal.hadError)
834 return DiscardUntilEndOfDirective();
835 if (Literal.Pascal) {
836 Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
837 return DiscardUntilEndOfDirective();
838 }
Jay Foad9a6b0982011-06-21 15:13:30 +0000839 FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString());
Mike Stump11289f42009-09-09 15:08:12 +0000840
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000841 // Verify that there is nothing after the string, other than EOD. Because
Chris Lattner0003c272009-04-17 23:30:53 +0000842 // of C99 6.10.4p5, macros that expand to empty tokens are ok.
843 CheckEndOfDirective("line", true);
Chris Lattner100c65e2009-01-26 05:29:08 +0000844 }
Mike Stump11289f42009-09-09 15:08:12 +0000845
Chris Lattner1eaa70a2009-02-03 21:52:55 +0000846 SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID);
Mike Stump11289f42009-09-09 15:08:12 +0000847
Chris Lattner839150e2009-03-27 17:13:49 +0000848 if (Callbacks)
Chris Lattnerc745cec2010-04-14 04:28:50 +0000849 Callbacks->FileChanged(CurPPLexer->getSourceLocation(),
850 PPCallbacks::RenameFile,
Chris Lattner839150e2009-03-27 17:13:49 +0000851 SrcMgr::C_User);
Chris Lattner100c65e2009-01-26 05:29:08 +0000852}
853
Chris Lattner76e68962009-01-26 06:19:46 +0000854/// ReadLineMarkerFlags - Parse and validate any flags at the end of a GNU line
855/// marker directive.
856static bool ReadLineMarkerFlags(bool &IsFileEntry, bool &IsFileExit,
857 bool &IsSystemHeader, bool &IsExternCHeader,
858 Preprocessor &PP) {
859 unsigned FlagVal;
860 Token FlagTok;
861 PP.Lex(FlagTok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000862 if (FlagTok.is(tok::eod)) return false;
Chris Lattner76e68962009-01-26 06:19:46 +0000863 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP))
864 return true;
865
866 if (FlagVal == 1) {
867 IsFileEntry = true;
Mike Stump11289f42009-09-09 15:08:12 +0000868
Chris Lattner76e68962009-01-26 06:19:46 +0000869 PP.Lex(FlagTok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000870 if (FlagTok.is(tok::eod)) return false;
Chris Lattner76e68962009-01-26 06:19:46 +0000871 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP))
872 return true;
873 } else if (FlagVal == 2) {
874 IsFileExit = true;
Mike Stump11289f42009-09-09 15:08:12 +0000875
Chris Lattner1c967782009-02-04 06:25:26 +0000876 SourceManager &SM = PP.getSourceManager();
877 // If we are leaving the current presumed file, check to make sure the
878 // presumed include stack isn't empty!
879 FileID CurFileID =
Chandler Carruthc7ca5212011-07-25 20:52:32 +0000880 SM.getDecomposedExpansionLoc(FlagTok.getLocation()).first;
Chris Lattner1c967782009-02-04 06:25:26 +0000881 PresumedLoc PLoc = SM.getPresumedLoc(FlagTok.getLocation());
Douglas Gregor453b0122010-11-12 07:15:47 +0000882 if (PLoc.isInvalid())
883 return true;
884
Chris Lattner1c967782009-02-04 06:25:26 +0000885 // If there is no include loc (main file) or if the include loc is in a
886 // different physical file, then we aren't in a "1" line marker flag region.
887 SourceLocation IncLoc = PLoc.getIncludeLoc();
888 if (IncLoc.isInvalid() ||
Chandler Carruthc7ca5212011-07-25 20:52:32 +0000889 SM.getDecomposedExpansionLoc(IncLoc).first != CurFileID) {
Chris Lattner1c967782009-02-04 06:25:26 +0000890 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_pop);
891 PP.DiscardUntilEndOfDirective();
892 return true;
893 }
Mike Stump11289f42009-09-09 15:08:12 +0000894
Chris Lattner76e68962009-01-26 06:19:46 +0000895 PP.Lex(FlagTok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000896 if (FlagTok.is(tok::eod)) return false;
Chris Lattner76e68962009-01-26 06:19:46 +0000897 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP))
898 return true;
899 }
900
901 // We must have 3 if there are still flags.
902 if (FlagVal != 3) {
903 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
Chris Lattnerb5fba6f2009-01-26 07:57:50 +0000904 PP.DiscardUntilEndOfDirective();
Chris Lattner76e68962009-01-26 06:19:46 +0000905 return true;
906 }
Mike Stump11289f42009-09-09 15:08:12 +0000907
Chris Lattner76e68962009-01-26 06:19:46 +0000908 IsSystemHeader = true;
Mike Stump11289f42009-09-09 15:08:12 +0000909
Chris Lattner76e68962009-01-26 06:19:46 +0000910 PP.Lex(FlagTok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000911 if (FlagTok.is(tok::eod)) return false;
Chris Lattner0a1a8d82009-02-04 05:21:58 +0000912 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP))
Chris Lattner76e68962009-01-26 06:19:46 +0000913 return true;
914
915 // We must have 4 if there is yet another flag.
916 if (FlagVal != 4) {
917 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
Chris Lattnerb5fba6f2009-01-26 07:57:50 +0000918 PP.DiscardUntilEndOfDirective();
Chris Lattner76e68962009-01-26 06:19:46 +0000919 return true;
920 }
Mike Stump11289f42009-09-09 15:08:12 +0000921
Chris Lattner76e68962009-01-26 06:19:46 +0000922 IsExternCHeader = true;
Mike Stump11289f42009-09-09 15:08:12 +0000923
Chris Lattner76e68962009-01-26 06:19:46 +0000924 PP.Lex(FlagTok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000925 if (FlagTok.is(tok::eod)) return false;
Chris Lattner76e68962009-01-26 06:19:46 +0000926
927 // There are no more valid flags here.
928 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
Chris Lattnerb5fba6f2009-01-26 07:57:50 +0000929 PP.DiscardUntilEndOfDirective();
Chris Lattner76e68962009-01-26 06:19:46 +0000930 return true;
931}
932
933/// HandleDigitDirective - Handle a GNU line marker directive, whose syntax is
934/// one of the following forms:
935///
936/// # 42
Mike Stump11289f42009-09-09 15:08:12 +0000937/// # 42 "file" ('1' | '2')?
Chris Lattner76e68962009-01-26 06:19:46 +0000938/// # 42 "file" ('1' | '2')? '3' '4'?
939///
940void Preprocessor::HandleDigitDirective(Token &DigitTok) {
941 // Validate the number and convert it to an unsigned. GNU does not have a
942 // line # limit other than it fit in 32-bits.
943 unsigned LineNo;
944 if (GetLineValue(DigitTok, LineNo, diag::err_pp_linemarker_requires_integer,
945 *this))
946 return;
Mike Stump11289f42009-09-09 15:08:12 +0000947
Chris Lattner76e68962009-01-26 06:19:46 +0000948 Token StrTok;
949 Lex(StrTok);
Mike Stump11289f42009-09-09 15:08:12 +0000950
Chris Lattner76e68962009-01-26 06:19:46 +0000951 bool IsFileEntry = false, IsFileExit = false;
952 bool IsSystemHeader = false, IsExternCHeader = false;
Chris Lattnerb5fba6f2009-01-26 07:57:50 +0000953 int FilenameID = -1;
954
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000955 // If the StrTok is "eod", then it wasn't present. Otherwise, it must be a
956 // string followed by eod.
957 if (StrTok.is(tok::eod))
Chris Lattner76e68962009-01-26 06:19:46 +0000958 ; // ok
959 else if (StrTok.isNot(tok::string_literal)) {
960 Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
Chris Lattnerb5fba6f2009-01-26 07:57:50 +0000961 return DiscardUntilEndOfDirective();
Richard Smithd67aea22012-03-06 03:21:47 +0000962 } else if (StrTok.hasUDSuffix()) {
963 Diag(StrTok, diag::err_invalid_string_udl);
964 return DiscardUntilEndOfDirective();
Chris Lattner76e68962009-01-26 06:19:46 +0000965 } else {
Chris Lattnerb5fba6f2009-01-26 07:57:50 +0000966 // Parse and validate the string, converting it into a unique ID.
967 StringLiteralParser Literal(&StrTok, 1, *this);
Douglas Gregorfb65e592011-07-27 05:40:30 +0000968 assert(Literal.isAscii() && "Didn't allow wide strings in");
Chris Lattnerb5fba6f2009-01-26 07:57:50 +0000969 if (Literal.hadError)
970 return DiscardUntilEndOfDirective();
971 if (Literal.Pascal) {
972 Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
973 return DiscardUntilEndOfDirective();
974 }
Jay Foad9a6b0982011-06-21 15:13:30 +0000975 FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString());
Mike Stump11289f42009-09-09 15:08:12 +0000976
Chris Lattner76e68962009-01-26 06:19:46 +0000977 // If a filename was present, read any flags that are present.
Mike Stump11289f42009-09-09 15:08:12 +0000978 if (ReadLineMarkerFlags(IsFileEntry, IsFileExit,
Chris Lattnerb5fba6f2009-01-26 07:57:50 +0000979 IsSystemHeader, IsExternCHeader, *this))
Chris Lattner76e68962009-01-26 06:19:46 +0000980 return;
Chris Lattner76e68962009-01-26 06:19:46 +0000981 }
Mike Stump11289f42009-09-09 15:08:12 +0000982
Chris Lattner0a1a8d82009-02-04 05:21:58 +0000983 // Create a line note with this information.
984 SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID,
Mike Stump11289f42009-09-09 15:08:12 +0000985 IsFileEntry, IsFileExit,
Chris Lattner0a1a8d82009-02-04 05:21:58 +0000986 IsSystemHeader, IsExternCHeader);
Mike Stump11289f42009-09-09 15:08:12 +0000987
Chris Lattner839150e2009-03-27 17:13:49 +0000988 // If the preprocessor has callbacks installed, notify them of the #line
989 // change. This is used so that the line marker comes out in -E mode for
990 // example.
991 if (Callbacks) {
992 PPCallbacks::FileChangeReason Reason = PPCallbacks::RenameFile;
993 if (IsFileEntry)
994 Reason = PPCallbacks::EnterFile;
995 else if (IsFileExit)
996 Reason = PPCallbacks::ExitFile;
997 SrcMgr::CharacteristicKind FileKind = SrcMgr::C_User;
998 if (IsExternCHeader)
999 FileKind = SrcMgr::C_ExternCSystem;
1000 else if (IsSystemHeader)
1001 FileKind = SrcMgr::C_System;
Mike Stump11289f42009-09-09 15:08:12 +00001002
Chris Lattnerc745cec2010-04-14 04:28:50 +00001003 Callbacks->FileChanged(CurPPLexer->getSourceLocation(), Reason, FileKind);
Chris Lattner839150e2009-03-27 17:13:49 +00001004 }
Chris Lattner76e68962009-01-26 06:19:46 +00001005}
1006
1007
Chris Lattner38d7fd22009-01-26 05:30:54 +00001008/// HandleUserDiagnosticDirective - Handle a #warning or #error directive.
1009///
Mike Stump11289f42009-09-09 15:08:12 +00001010void Preprocessor::HandleUserDiagnosticDirective(Token &Tok,
Chris Lattnerf64b3522008-03-09 01:54:53 +00001011 bool isWarning) {
Chris Lattner38d7fd22009-01-26 05:30:54 +00001012 // PTH doesn't emit #warning or #error directives.
1013 if (CurPTHLexer)
Chris Lattner100c65e2009-01-26 05:29:08 +00001014 return CurPTHLexer->DiscardToEndOfLine();
1015
Chris Lattnerf64b3522008-03-09 01:54:53 +00001016 // Read the rest of the line raw. We do this because we don't want macros
1017 // to be expanded and we don't require that the tokens be valid preprocessing
1018 // tokens. For example, this is allowed: "#warning ` 'foo". GCC does
1019 // collapse multiple consequtive white space between tokens, but this isn't
1020 // specified by the standard.
Benjamin Kramere5fbc6c2012-05-18 19:32:16 +00001021 SmallString<128> Message;
1022 CurLexer->ReadToEndOfLine(&Message);
Ted Kremenek7f4bd162012-02-02 00:16:13 +00001023
1024 // Find the first non-whitespace character, so that we can make the
1025 // diagnostic more succinct.
Benjamin Kramere5fbc6c2012-05-18 19:32:16 +00001026 StringRef Msg = Message.str().ltrim(" ");
1027
Chris Lattner100c65e2009-01-26 05:29:08 +00001028 if (isWarning)
Ted Kremenek7f4bd162012-02-02 00:16:13 +00001029 Diag(Tok, diag::pp_hash_warning) << Msg;
Chris Lattner100c65e2009-01-26 05:29:08 +00001030 else
Ted Kremenek7f4bd162012-02-02 00:16:13 +00001031 Diag(Tok, diag::err_pp_hash_error) << Msg;
Chris Lattnerf64b3522008-03-09 01:54:53 +00001032}
1033
1034/// HandleIdentSCCSDirective - Handle a #ident/#sccs directive.
1035///
1036void Preprocessor::HandleIdentSCCSDirective(Token &Tok) {
1037 // Yes, this directive is an extension.
1038 Diag(Tok, diag::ext_pp_ident_directive);
Mike Stump11289f42009-09-09 15:08:12 +00001039
Chris Lattnerf64b3522008-03-09 01:54:53 +00001040 // Read the string argument.
1041 Token StrTok;
1042 Lex(StrTok);
Mike Stump11289f42009-09-09 15:08:12 +00001043
Chris Lattnerf64b3522008-03-09 01:54:53 +00001044 // If the token kind isn't a string, it's a malformed directive.
1045 if (StrTok.isNot(tok::string_literal) &&
Chris Lattner907dfe92008-11-18 07:59:24 +00001046 StrTok.isNot(tok::wide_string_literal)) {
1047 Diag(StrTok, diag::err_pp_malformed_ident);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001048 if (StrTok.isNot(tok::eod))
Chris Lattner38d7fd22009-01-26 05:30:54 +00001049 DiscardUntilEndOfDirective();
Chris Lattner907dfe92008-11-18 07:59:24 +00001050 return;
1051 }
Mike Stump11289f42009-09-09 15:08:12 +00001052
Richard Smithd67aea22012-03-06 03:21:47 +00001053 if (StrTok.hasUDSuffix()) {
1054 Diag(StrTok, diag::err_invalid_string_udl);
1055 return DiscardUntilEndOfDirective();
1056 }
1057
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001058 // Verify that there is nothing after the string, other than EOD.
Chris Lattnerce2ab6f2009-04-14 05:07:49 +00001059 CheckEndOfDirective("ident");
Chris Lattnerf64b3522008-03-09 01:54:53 +00001060
Douglas Gregordc970f02010-03-16 22:30:13 +00001061 if (Callbacks) {
1062 bool Invalid = false;
1063 std::string Str = getSpelling(StrTok, &Invalid);
1064 if (!Invalid)
1065 Callbacks->Ident(Tok.getLocation(), Str);
1066 }
Chris Lattnerf64b3522008-03-09 01:54:53 +00001067}
1068
Douglas Gregor0bf886d2012-01-03 18:24:14 +00001069/// \brief Handle a #public directive.
1070void Preprocessor::HandleMacroPublicDirective(Token &Tok) {
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001071 Token MacroNameTok;
1072 ReadMacroName(MacroNameTok, 2);
1073
1074 // Error reading macro name? If so, diagnostic already issued.
1075 if (MacroNameTok.is(tok::eod))
1076 return;
1077
Douglas Gregor663b48f2012-01-03 19:48:16 +00001078 // Check to see if this is the last token on the #__public_macro line.
1079 CheckEndOfDirective("__public_macro");
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001080
1081 // Okay, we finally have a valid identifier to undef.
1082 MacroInfo *MI = getMacroInfo(MacroNameTok.getIdentifierInfo());
1083
1084 // If the macro is not defined, this is an error.
1085 if (MI == 0) {
Douglas Gregorebf00492011-10-17 15:32:29 +00001086 Diag(MacroNameTok, diag::err_pp_visibility_non_macro)
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001087 << MacroNameTok.getIdentifierInfo();
1088 return;
1089 }
1090
1091 // Note that this macro has now been exported.
Douglas Gregorebf00492011-10-17 15:32:29 +00001092 MI->setVisibility(/*IsPublic=*/true, MacroNameTok.getLocation());
1093
1094 // If this macro definition came from a PCH file, mark it
1095 // as having changed since serialization.
1096 if (MI->isFromAST())
1097 MI->setChangedAfterLoad();
1098}
1099
Douglas Gregor0bf886d2012-01-03 18:24:14 +00001100/// \brief Handle a #private directive.
Douglas Gregorebf00492011-10-17 15:32:29 +00001101void Preprocessor::HandleMacroPrivateDirective(Token &Tok) {
1102 Token MacroNameTok;
1103 ReadMacroName(MacroNameTok, 2);
1104
1105 // Error reading macro name? If so, diagnostic already issued.
1106 if (MacroNameTok.is(tok::eod))
1107 return;
1108
Douglas Gregor663b48f2012-01-03 19:48:16 +00001109 // Check to see if this is the last token on the #__private_macro line.
1110 CheckEndOfDirective("__private_macro");
Douglas Gregorebf00492011-10-17 15:32:29 +00001111
1112 // Okay, we finally have a valid identifier to undef.
1113 MacroInfo *MI = getMacroInfo(MacroNameTok.getIdentifierInfo());
1114
1115 // If the macro is not defined, this is an error.
1116 if (MI == 0) {
1117 Diag(MacroNameTok, diag::err_pp_visibility_non_macro)
1118 << MacroNameTok.getIdentifierInfo();
1119 return;
1120 }
1121
1122 // Note that this macro has now been marked private.
1123 MI->setVisibility(/*IsPublic=*/false, MacroNameTok.getLocation());
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001124
1125 // If this macro definition came from a PCH file, mark it
1126 // as having changed since serialization.
1127 if (MI->isFromAST())
1128 MI->setChangedAfterLoad();
1129}
1130
Chris Lattnerf64b3522008-03-09 01:54:53 +00001131//===----------------------------------------------------------------------===//
1132// Preprocessor Include Directive Handling.
1133//===----------------------------------------------------------------------===//
1134
1135/// GetIncludeFilenameSpelling - Turn the specified lexer token into a fully
1136/// checked and spelled filename, e.g. as an operand of #include. This returns
1137/// true if the input filename was in <>'s or false if it were in ""'s. The
1138/// caller is expected to provide a buffer that is large enough to hold the
1139/// spelling of the filename, but is also expected to handle the case when
1140/// this method decides to use a different buffer.
1141bool Preprocessor::GetIncludeFilenameSpelling(SourceLocation Loc,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001142 StringRef &Buffer) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001143 // Get the text form of the filename.
Chris Lattnerd081f8c2010-01-10 01:35:12 +00001144 assert(!Buffer.empty() && "Can't have tokens with empty spellings!");
Mike Stump11289f42009-09-09 15:08:12 +00001145
Chris Lattnerf64b3522008-03-09 01:54:53 +00001146 // Make sure the filename is <x> or "x".
1147 bool isAngled;
Chris Lattnerd081f8c2010-01-10 01:35:12 +00001148 if (Buffer[0] == '<') {
1149 if (Buffer.back() != '>') {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001150 Diag(Loc, diag::err_pp_expects_filename);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001151 Buffer = StringRef();
Chris Lattnerf64b3522008-03-09 01:54:53 +00001152 return true;
1153 }
1154 isAngled = true;
Chris Lattnerd081f8c2010-01-10 01:35:12 +00001155 } else if (Buffer[0] == '"') {
1156 if (Buffer.back() != '"') {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001157 Diag(Loc, diag::err_pp_expects_filename);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001158 Buffer = StringRef();
Chris Lattnerf64b3522008-03-09 01:54:53 +00001159 return true;
1160 }
1161 isAngled = false;
1162 } else {
1163 Diag(Loc, diag::err_pp_expects_filename);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001164 Buffer = StringRef();
Chris Lattnerf64b3522008-03-09 01:54:53 +00001165 return true;
1166 }
Mike Stump11289f42009-09-09 15:08:12 +00001167
Chris Lattnerf64b3522008-03-09 01:54:53 +00001168 // Diagnose #include "" as invalid.
Chris Lattnerd081f8c2010-01-10 01:35:12 +00001169 if (Buffer.size() <= 2) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001170 Diag(Loc, diag::err_pp_empty_filename);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001171 Buffer = StringRef();
Chris Lattnerd081f8c2010-01-10 01:35:12 +00001172 return true;
Chris Lattnerf64b3522008-03-09 01:54:53 +00001173 }
Mike Stump11289f42009-09-09 15:08:12 +00001174
Chris Lattnerf64b3522008-03-09 01:54:53 +00001175 // Skip the brackets.
Chris Lattnerd081f8c2010-01-10 01:35:12 +00001176 Buffer = Buffer.substr(1, Buffer.size()-2);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001177 return isAngled;
1178}
1179
1180/// ConcatenateIncludeName - Handle cases where the #include name is expanded
1181/// from a macro as multiple tokens, which need to be glued together. This
1182/// occurs for code like:
1183/// #define FOO <a/b.h>
1184/// #include FOO
1185/// because in this case, "<a/b.h>" is returned as 7 tokens, not one.
1186///
1187/// This code concatenates and consumes tokens up to the '>' token. It returns
1188/// false if the > was found, otherwise it returns true if it finds and consumes
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001189/// the EOD marker.
John Thompsonb5353522009-10-30 13:49:06 +00001190bool Preprocessor::ConcatenateIncludeName(
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001191 SmallString<128> &FilenameBuffer,
Douglas Gregor796d76a2010-10-20 22:00:55 +00001192 SourceLocation &End) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001193 Token CurTok;
Mike Stump11289f42009-09-09 15:08:12 +00001194
John Thompsonb5353522009-10-30 13:49:06 +00001195 Lex(CurTok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001196 while (CurTok.isNot(tok::eod)) {
Douglas Gregor796d76a2010-10-20 22:00:55 +00001197 End = CurTok.getLocation();
1198
Douglas Gregor9c7bd2f2010-12-09 23:35:36 +00001199 // FIXME: Provide code completion for #includes.
1200 if (CurTok.is(tok::code_completion)) {
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001201 setCodeCompletionReached();
Douglas Gregor9c7bd2f2010-12-09 23:35:36 +00001202 Lex(CurTok);
1203 continue;
1204 }
1205
Chris Lattnerf64b3522008-03-09 01:54:53 +00001206 // Append the spelling of this token to the buffer. If there was a space
1207 // before it, add it now.
1208 if (CurTok.hasLeadingSpace())
1209 FilenameBuffer.push_back(' ');
Mike Stump11289f42009-09-09 15:08:12 +00001210
Chris Lattnerf64b3522008-03-09 01:54:53 +00001211 // Get the spelling of the token, directly into FilenameBuffer if possible.
1212 unsigned PreAppendSize = FilenameBuffer.size();
1213 FilenameBuffer.resize(PreAppendSize+CurTok.getLength());
Mike Stump11289f42009-09-09 15:08:12 +00001214
Chris Lattnerf64b3522008-03-09 01:54:53 +00001215 const char *BufPtr = &FilenameBuffer[PreAppendSize];
John Thompsonb5353522009-10-30 13:49:06 +00001216 unsigned ActualLen = getSpelling(CurTok, BufPtr);
Mike Stump11289f42009-09-09 15:08:12 +00001217
Chris Lattnerf64b3522008-03-09 01:54:53 +00001218 // If the token was spelled somewhere else, copy it into FilenameBuffer.
1219 if (BufPtr != &FilenameBuffer[PreAppendSize])
1220 memcpy(&FilenameBuffer[PreAppendSize], BufPtr, ActualLen);
Mike Stump11289f42009-09-09 15:08:12 +00001221
Chris Lattnerf64b3522008-03-09 01:54:53 +00001222 // Resize FilenameBuffer to the correct size.
1223 if (CurTok.getLength() != ActualLen)
1224 FilenameBuffer.resize(PreAppendSize+ActualLen);
Mike Stump11289f42009-09-09 15:08:12 +00001225
Chris Lattnerf64b3522008-03-09 01:54:53 +00001226 // If we found the '>' marker, return success.
1227 if (CurTok.is(tok::greater))
1228 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001229
John Thompsonb5353522009-10-30 13:49:06 +00001230 Lex(CurTok);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001231 }
1232
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001233 // If we hit the eod marker, emit an error and return true so that the caller
1234 // knows the EOD has been read.
John Thompsonb5353522009-10-30 13:49:06 +00001235 Diag(CurTok.getLocation(), diag::err_pp_expects_filename);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001236 return true;
1237}
1238
1239/// HandleIncludeDirective - The "#include" tokens have just been read, read the
1240/// file to be included from the lexer, then include it! This is a common
1241/// routine with functionality shared between #include, #include_next and
Chris Lattnerc88a23e2008-09-26 20:12:23 +00001242/// #import. LookupFrom is set when this is a #include_next directive, it
Mike Stump11289f42009-09-09 15:08:12 +00001243/// specifies the file to start searching from.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001244void Preprocessor::HandleIncludeDirective(SourceLocation HashLoc,
1245 Token &IncludeTok,
Chris Lattnerf64b3522008-03-09 01:54:53 +00001246 const DirectoryLookup *LookupFrom,
1247 bool isImport) {
1248
1249 Token FilenameTok;
Ted Kremenek30cd88c2008-11-18 00:34:22 +00001250 CurPPLexer->LexIncludeFilename(FilenameTok);
Mike Stump11289f42009-09-09 15:08:12 +00001251
Chris Lattnerf64b3522008-03-09 01:54:53 +00001252 // Reserve a buffer to get the spelling.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001253 SmallString<128> FilenameBuffer;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001254 StringRef Filename;
Douglas Gregor796d76a2010-10-20 22:00:55 +00001255 SourceLocation End;
Douglas Gregor41e115a2011-11-30 18:02:36 +00001256 SourceLocation CharEnd; // the end of this directive, in characters
Douglas Gregor796d76a2010-10-20 22:00:55 +00001257
Chris Lattnerf64b3522008-03-09 01:54:53 +00001258 switch (FilenameTok.getKind()) {
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001259 case tok::eod:
1260 // If the token kind is EOD, the error has already been diagnosed.
Chris Lattnerf64b3522008-03-09 01:54:53 +00001261 return;
Mike Stump11289f42009-09-09 15:08:12 +00001262
Chris Lattnerf64b3522008-03-09 01:54:53 +00001263 case tok::angle_string_literal:
Benjamin Kramer0a1abd42010-02-27 13:44:12 +00001264 case tok::string_literal:
1265 Filename = getSpelling(FilenameTok, FilenameBuffer);
Douglas Gregor796d76a2010-10-20 22:00:55 +00001266 End = FilenameTok.getLocation();
Douglas Gregor41e115a2011-11-30 18:02:36 +00001267 CharEnd = End.getLocWithOffset(Filename.size());
Chris Lattnerf64b3522008-03-09 01:54:53 +00001268 break;
Mike Stump11289f42009-09-09 15:08:12 +00001269
Chris Lattnerf64b3522008-03-09 01:54:53 +00001270 case tok::less:
1271 // This could be a <foo/bar.h> file coming from a macro expansion. In this
1272 // case, glue the tokens together into FilenameBuffer and interpret those.
1273 FilenameBuffer.push_back('<');
Douglas Gregor796d76a2010-10-20 22:00:55 +00001274 if (ConcatenateIncludeName(FilenameBuffer, End))
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001275 return; // Found <eod> but no ">"? Diagnostic already emitted.
Chris Lattnerd081f8c2010-01-10 01:35:12 +00001276 Filename = FilenameBuffer.str();
Douglas Gregor41e115a2011-11-30 18:02:36 +00001277 CharEnd = getLocForEndOfToken(End);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001278 break;
1279 default:
1280 Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
1281 DiscardUntilEndOfDirective();
1282 return;
1283 }
Mike Stump11289f42009-09-09 15:08:12 +00001284
Aaron Ballman611306e2012-03-02 22:51:54 +00001285 StringRef OriginalFilename = Filename;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001286 bool isAngled =
Chris Lattnerd081f8c2010-01-10 01:35:12 +00001287 GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001288 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
1289 // error.
Chris Lattnerd081f8c2010-01-10 01:35:12 +00001290 if (Filename.empty()) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001291 DiscardUntilEndOfDirective();
1292 return;
1293 }
Mike Stump11289f42009-09-09 15:08:12 +00001294
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001295 // Verify that there is nothing after the filename, other than EOD. Note that
Chris Lattnerb40289b2009-04-17 23:56:52 +00001296 // we allow macros that expand to nothing after the filename, because this
1297 // falls into the category of "#include pp-tokens new-line" specified in
1298 // C99 6.10.2p4.
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00001299 CheckEndOfDirective(IncludeTok.getIdentifierInfo()->getNameStart(), true);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001300
1301 // Check that we don't have infinite #include recursion.
Chris Lattner907dfe92008-11-18 07:59:24 +00001302 if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1) {
1303 Diag(FilenameTok, diag::err_pp_include_too_deep);
1304 return;
1305 }
Mike Stump11289f42009-09-09 15:08:12 +00001306
John McCall32f5fe12011-09-30 05:12:12 +00001307 // Complain about attempts to #include files in an audit pragma.
1308 if (PragmaARCCFCodeAuditedLoc.isValid()) {
1309 Diag(HashLoc, diag::err_pp_include_in_arc_cf_code_audited);
1310 Diag(PragmaARCCFCodeAuditedLoc, diag::note_pragma_entered_here);
1311
1312 // Immediately leave the pragma.
1313 PragmaARCCFCodeAuditedLoc = SourceLocation();
1314 }
1315
Aaron Ballman611306e2012-03-02 22:51:54 +00001316 if (HeaderInfo.HasIncludeAliasMap()) {
1317 // Map the filename with the brackets still attached. If the name doesn't
1318 // map to anything, fall back on the filename we've already gotten the
1319 // spelling for.
1320 StringRef NewName = HeaderInfo.MapHeaderToIncludeAlias(OriginalFilename);
1321 if (!NewName.empty())
1322 Filename = NewName;
1323 }
1324
Chris Lattnerf64b3522008-03-09 01:54:53 +00001325 // Search include directories.
1326 const DirectoryLookup *CurDir;
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001327 SmallString<1024> SearchPath;
1328 SmallString<1024> RelativePath;
Chandler Carruth3cc331a2011-03-16 18:34:36 +00001329 // We get the raw path only if we have 'Callbacks' to which we later pass
1330 // the path.
Douglas Gregorde3ef502011-11-30 23:21:26 +00001331 Module *SuggestedModule = 0;
Chandler Carruth3cc331a2011-03-16 18:34:36 +00001332 const FileEntry *File = LookupFile(
Manuel Klimek0c69fd22011-04-26 21:50:03 +00001333 Filename, isAngled, LookupFrom, CurDir,
Douglas Gregor97eec242011-09-15 22:00:41 +00001334 Callbacks ? &SearchPath : NULL, Callbacks ? &RelativePath : NULL,
David Blaikiebbafb8a2012-03-11 07:00:24 +00001335 getLangOpts().Modules? &SuggestedModule : 0);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001336
Douglas Gregor11729f02011-11-30 18:12:06 +00001337 if (Callbacks) {
1338 if (!File) {
1339 // Give the clients a chance to recover.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001340 SmallString<128> RecoveryPath;
Douglas Gregor11729f02011-11-30 18:12:06 +00001341 if (Callbacks->FileNotFound(Filename, RecoveryPath)) {
1342 if (const DirectoryEntry *DE = FileMgr.getDirectory(RecoveryPath)) {
1343 // Add the recovery path to the list of search paths.
1344 DirectoryLookup DL(DE, SrcMgr::C_User, true, false);
1345 HeaderInfo.AddSearchPath(DL, isAngled);
1346
1347 // Try the lookup again, skipping the cache.
1348 File = LookupFile(Filename, isAngled, LookupFrom, CurDir, 0, 0,
David Blaikiebbafb8a2012-03-11 07:00:24 +00001349 getLangOpts().Modules? &SuggestedModule : 0,
Douglas Gregor11729f02011-11-30 18:12:06 +00001350 /*SkipCache*/true);
1351 }
1352 }
1353 }
1354
1355 // Notify the callback object that we've seen an inclusion directive.
1356 Callbacks->InclusionDirective(HashLoc, IncludeTok, Filename, isAngled, File,
1357 End, SearchPath, RelativePath);
1358 }
1359
1360 if (File == 0) {
1361 if (!SuppressIncludeNotFoundError)
1362 Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
1363 return;
1364 }
1365
Douglas Gregor97eec242011-09-15 22:00:41 +00001366 // If we are supposed to import a module rather than including the header,
1367 // do so now.
Douglas Gregorc04f6442011-11-17 22:44:56 +00001368 if (SuggestedModule) {
Douglas Gregor71944202011-11-30 00:36:36 +00001369 // Compute the module access path corresponding to this module.
1370 // FIXME: Should we have a second loadModule() overload to avoid this
1371 // extra lookup step?
1372 llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Path;
Douglas Gregorde3ef502011-11-30 23:21:26 +00001373 for (Module *Mod = SuggestedModule; Mod; Mod = Mod->Parent)
Douglas Gregor71944202011-11-30 00:36:36 +00001374 Path.push_back(std::make_pair(getIdentifierInfo(Mod->Name),
1375 FilenameTok.getLocation()));
1376 std::reverse(Path.begin(), Path.end());
1377
Douglas Gregor41e115a2011-11-30 18:02:36 +00001378 // Warn that we're replacing the include/import with a module import.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001379 SmallString<128> PathString;
Douglas Gregor41e115a2011-11-30 18:02:36 +00001380 for (unsigned I = 0, N = Path.size(); I != N; ++I) {
1381 if (I)
1382 PathString += '.';
1383 PathString += Path[I].first->getName();
1384 }
1385 int IncludeKind = 0;
1386
1387 switch (IncludeTok.getIdentifierInfo()->getPPKeywordID()) {
1388 case tok::pp_include:
1389 IncludeKind = 0;
1390 break;
1391
1392 case tok::pp_import:
1393 IncludeKind = 1;
1394 break;
1395
Douglas Gregor4401fbe2011-11-30 18:03:26 +00001396 case tok::pp_include_next:
1397 IncludeKind = 2;
1398 break;
Douglas Gregor41e115a2011-11-30 18:02:36 +00001399
1400 case tok::pp___include_macros:
1401 IncludeKind = 3;
1402 break;
1403
1404 default:
1405 llvm_unreachable("unknown include directive kind");
Douglas Gregor41e115a2011-11-30 18:02:36 +00001406 }
1407
Douglas Gregor2537a362011-12-08 17:01:29 +00001408 // Determine whether we are actually building the module that this
1409 // include directive maps to.
1410 bool BuildingImportedModule
David Blaikiebbafb8a2012-03-11 07:00:24 +00001411 = Path[0].first->getName() == getLangOpts().CurrentModule;
Douglas Gregor2537a362011-12-08 17:01:29 +00001412
David Blaikiebbafb8a2012-03-11 07:00:24 +00001413 if (!BuildingImportedModule && getLangOpts().ObjC2) {
Douglas Gregor2537a362011-12-08 17:01:29 +00001414 // If we're not building the imported module, warn that we're going
1415 // to automatically turn this inclusion directive into a module import.
Douglas Gregorda82e702012-01-03 19:32:59 +00001416 // We only do this in Objective-C, where we have a module-import syntax.
Douglas Gregor2537a362011-12-08 17:01:29 +00001417 CharSourceRange ReplaceRange(SourceRange(HashLoc, CharEnd),
1418 /*IsTokenRange=*/false);
1419 Diag(HashLoc, diag::warn_auto_module_import)
1420 << IncludeKind << PathString
1421 << FixItHint::CreateReplacement(ReplaceRange,
Ted Kremenekc1e4dd02012-03-01 22:07:04 +00001422 "@__experimental_modules_import " + PathString.str().str() + ";");
Douglas Gregor2537a362011-12-08 17:01:29 +00001423 }
Douglas Gregor41e115a2011-11-30 18:02:36 +00001424
Douglas Gregor71944202011-11-30 00:36:36 +00001425 // Load the module.
Douglas Gregorff2be532011-12-01 17:11:21 +00001426 // If this was an #__include_macros directive, only make macros visible.
1427 Module::NameVisibilityKind Visibility
1428 = (IncludeKind == 3)? Module::MacrosVisible : Module::AllVisible;
Douglas Gregor98a52db2011-12-20 00:28:52 +00001429 Module *Imported
1430 = TheModuleLoader.loadModule(IncludeTok.getLocation(), Path, Visibility,
1431 /*IsIncludeDirective=*/true);
Douglas Gregor2537a362011-12-08 17:01:29 +00001432
1433 // If this header isn't part of the module we're building, we're done.
Douglas Gregor98a52db2011-12-20 00:28:52 +00001434 if (!BuildingImportedModule && Imported)
Douglas Gregor2537a362011-12-08 17:01:29 +00001435 return;
Douglas Gregor97eec242011-09-15 22:00:41 +00001436 }
1437
Chris Lattnerc88a23e2008-09-26 20:12:23 +00001438 // The #included file will be considered to be a system header if either it is
1439 // in a system include directory, or if the #includer is a system include
1440 // header.
Mike Stump11289f42009-09-09 15:08:12 +00001441 SrcMgr::CharacteristicKind FileCharacter =
Chris Lattnerb03dc762008-09-26 21:18:42 +00001442 std::max(HeaderInfo.getFileDirFlavor(File),
Chris Lattnerc0334162009-01-19 07:59:15 +00001443 SourceMgr.getFileCharacteristic(FilenameTok.getLocation()));
Mike Stump11289f42009-09-09 15:08:12 +00001444
Chris Lattner72286d62010-04-19 20:44:31 +00001445 // Ask HeaderInfo if we should enter this #include file. If not, #including
1446 // this file will have no effect.
1447 if (!HeaderInfo.ShouldEnterIncludeFile(File, isImport)) {
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001448 if (Callbacks)
Chris Lattner72286d62010-04-19 20:44:31 +00001449 Callbacks->FileSkipped(*File, FilenameTok, FileCharacter);
Chris Lattner72286d62010-04-19 20:44:31 +00001450 return;
1451 }
1452
Chris Lattnerf64b3522008-03-09 01:54:53 +00001453 // Look up the file, create a File ID for it.
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +00001454 SourceLocation IncludePos = End;
1455 // If the filename string was the result of macro expansions, set the include
1456 // position on the file where it will be included and after the expansions.
1457 if (IncludePos.isMacroID())
1458 IncludePos = SourceMgr.getExpansionRange(IncludePos).second;
1459 FileID FID = SourceMgr.createFileID(File, IncludePos, FileCharacter);
Peter Collingbourned395b932011-06-30 16:41:03 +00001460 assert(!FID.isInvalid() && "Expected valid file ID");
Chris Lattnerf64b3522008-03-09 01:54:53 +00001461
1462 // Finally, if all is good, enter the new file!
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001463 EnterSourceFile(FID, CurDir, FilenameTok.getLocation());
Chris Lattnerf64b3522008-03-09 01:54:53 +00001464}
1465
1466/// HandleIncludeNextDirective - Implements #include_next.
1467///
Douglas Gregor796d76a2010-10-20 22:00:55 +00001468void Preprocessor::HandleIncludeNextDirective(SourceLocation HashLoc,
1469 Token &IncludeNextTok) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001470 Diag(IncludeNextTok, diag::ext_pp_include_next_directive);
Mike Stump11289f42009-09-09 15:08:12 +00001471
Chris Lattnerf64b3522008-03-09 01:54:53 +00001472 // #include_next is like #include, except that we start searching after
1473 // the current found directory. If we can't do this, issue a
1474 // diagnostic.
1475 const DirectoryLookup *Lookup = CurDirLookup;
1476 if (isInPrimaryFile()) {
1477 Lookup = 0;
1478 Diag(IncludeNextTok, diag::pp_include_next_in_primary);
1479 } else if (Lookup == 0) {
1480 Diag(IncludeNextTok, diag::pp_include_next_absolute_path);
1481 } else {
1482 // Start looking up in the next directory.
1483 ++Lookup;
1484 }
Mike Stump11289f42009-09-09 15:08:12 +00001485
Douglas Gregor796d76a2010-10-20 22:00:55 +00001486 return HandleIncludeDirective(HashLoc, IncludeNextTok, Lookup);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001487}
1488
Aaron Ballman0467f552012-03-18 03:10:37 +00001489/// HandleMicrosoftImportDirective - Implements #import for Microsoft Mode
1490void Preprocessor::HandleMicrosoftImportDirective(Token &Tok) {
1491 // The Microsoft #import directive takes a type library and generates header
1492 // files from it, and includes those. This is beyond the scope of what clang
1493 // does, so we ignore it and error out. However, #import can optionally have
1494 // trailing attributes that span multiple lines. We're going to eat those
1495 // so we can continue processing from there.
1496 Diag(Tok, diag::err_pp_import_directive_ms );
1497
1498 // Read tokens until we get to the end of the directive. Note that the
1499 // directive can be split over multiple lines using the backslash character.
1500 DiscardUntilEndOfDirective();
1501}
1502
Chris Lattnerf64b3522008-03-09 01:54:53 +00001503/// HandleImportDirective - Implements #import.
1504///
Douglas Gregor796d76a2010-10-20 22:00:55 +00001505void Preprocessor::HandleImportDirective(SourceLocation HashLoc,
1506 Token &ImportTok) {
Aaron Ballman0467f552012-03-18 03:10:37 +00001507 if (!LangOpts.ObjC1) { // #import is standard for ObjC.
1508 if (LangOpts.MicrosoftMode)
1509 return HandleMicrosoftImportDirective(ImportTok);
Chris Lattnerd4a96732009-03-06 04:28:03 +00001510 Diag(ImportTok, diag::ext_pp_import_directive);
Aaron Ballman0467f552012-03-18 03:10:37 +00001511 }
Douglas Gregor796d76a2010-10-20 22:00:55 +00001512 return HandleIncludeDirective(HashLoc, ImportTok, 0, true);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001513}
1514
Chris Lattner58a1eb02009-04-08 18:46:40 +00001515/// HandleIncludeMacrosDirective - The -imacros command line option turns into a
1516/// pseudo directive in the predefines buffer. This handles it by sucking all
1517/// tokens through the preprocessor and discarding them (only keeping the side
1518/// effects on the preprocessor).
Douglas Gregor796d76a2010-10-20 22:00:55 +00001519void Preprocessor::HandleIncludeMacrosDirective(SourceLocation HashLoc,
1520 Token &IncludeMacrosTok) {
Chris Lattner58a1eb02009-04-08 18:46:40 +00001521 // This directive should only occur in the predefines buffer. If not, emit an
1522 // error and reject it.
1523 SourceLocation Loc = IncludeMacrosTok.getLocation();
1524 if (strcmp(SourceMgr.getBufferName(Loc), "<built-in>") != 0) {
1525 Diag(IncludeMacrosTok.getLocation(),
1526 diag::pp_include_macros_out_of_predefines);
1527 DiscardUntilEndOfDirective();
1528 return;
1529 }
Mike Stump11289f42009-09-09 15:08:12 +00001530
Chris Lattnere01d82b2009-04-08 20:53:24 +00001531 // Treat this as a normal #include for checking purposes. If this is
1532 // successful, it will push a new lexer onto the include stack.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001533 HandleIncludeDirective(HashLoc, IncludeMacrosTok, 0, false);
Mike Stump11289f42009-09-09 15:08:12 +00001534
Chris Lattnere01d82b2009-04-08 20:53:24 +00001535 Token TmpTok;
1536 do {
1537 Lex(TmpTok);
1538 assert(TmpTok.isNot(tok::eof) && "Didn't find end of -imacros!");
1539 } while (TmpTok.isNot(tok::hashhash));
Chris Lattner58a1eb02009-04-08 18:46:40 +00001540}
1541
Chris Lattnerf64b3522008-03-09 01:54:53 +00001542//===----------------------------------------------------------------------===//
1543// Preprocessor Macro Directive Handling.
1544//===----------------------------------------------------------------------===//
1545
1546/// ReadMacroDefinitionArgList - The ( starting an argument list of a macro
1547/// definition has just been read. Lex the rest of the arguments and the
1548/// closing ), updating MI with what we learn. Return true if an error occurs
1549/// parsing the arg list.
Abramo Bagnarac9e48c02012-03-31 20:17:27 +00001550bool Preprocessor::ReadMacroDefinitionArgList(MacroInfo *MI, Token &Tok) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001551 SmallVector<IdentifierInfo*, 32> Arguments;
Mike Stump11289f42009-09-09 15:08:12 +00001552
Chris Lattnerf64b3522008-03-09 01:54:53 +00001553 while (1) {
1554 LexUnexpandedToken(Tok);
1555 switch (Tok.getKind()) {
1556 case tok::r_paren:
1557 // Found the end of the argument list.
Chris Lattnerf87c5102009-02-20 22:31:31 +00001558 if (Arguments.empty()) // #define FOO()
Chris Lattnerf64b3522008-03-09 01:54:53 +00001559 return false;
Chris Lattnerf64b3522008-03-09 01:54:53 +00001560 // Otherwise we have #define FOO(A,)
1561 Diag(Tok, diag::err_pp_expected_ident_in_arg_list);
1562 return true;
1563 case tok::ellipsis: // #define X(... -> C99 varargs
David Blaikiebbafb8a2012-03-11 07:00:24 +00001564 if (!LangOpts.C99)
1565 Diag(Tok, LangOpts.CPlusPlus0x ?
Richard Smithacd4d3d2011-10-15 01:18:56 +00001566 diag::warn_cxx98_compat_variadic_macro :
1567 diag::ext_variadic_macro);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001568
1569 // Lex the token after the identifier.
1570 LexUnexpandedToken(Tok);
1571 if (Tok.isNot(tok::r_paren)) {
1572 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
1573 return true;
1574 }
1575 // Add the __VA_ARGS__ identifier as an argument.
1576 Arguments.push_back(Ident__VA_ARGS__);
1577 MI->setIsC99Varargs();
Chris Lattner70946da2009-02-20 22:46:43 +00001578 MI->setArgumentList(&Arguments[0], Arguments.size(), BP);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001579 return false;
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001580 case tok::eod: // #define X(
Chris Lattnerf64b3522008-03-09 01:54:53 +00001581 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
1582 return true;
1583 default:
1584 // Handle keywords and identifiers here to accept things like
1585 // #define Foo(for) for.
1586 IdentifierInfo *II = Tok.getIdentifierInfo();
1587 if (II == 0) {
1588 // #define X(1
1589 Diag(Tok, diag::err_pp_invalid_tok_in_arg_list);
1590 return true;
1591 }
1592
1593 // If this is already used as an argument, it is used multiple times (e.g.
1594 // #define X(A,A.
Mike Stump11289f42009-09-09 15:08:12 +00001595 if (std::find(Arguments.begin(), Arguments.end(), II) !=
Chris Lattnerf64b3522008-03-09 01:54:53 +00001596 Arguments.end()) { // C99 6.10.3p6
Chris Lattnerc5cdade2008-11-19 07:33:58 +00001597 Diag(Tok, diag::err_pp_duplicate_name_in_arg_list) << II;
Chris Lattnerf64b3522008-03-09 01:54:53 +00001598 return true;
1599 }
Mike Stump11289f42009-09-09 15:08:12 +00001600
Chris Lattnerf64b3522008-03-09 01:54:53 +00001601 // Add the argument to the macro info.
1602 Arguments.push_back(II);
Mike Stump11289f42009-09-09 15:08:12 +00001603
Chris Lattnerf64b3522008-03-09 01:54:53 +00001604 // Lex the token after the identifier.
1605 LexUnexpandedToken(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001606
Chris Lattnerf64b3522008-03-09 01:54:53 +00001607 switch (Tok.getKind()) {
1608 default: // #define X(A B
1609 Diag(Tok, diag::err_pp_expected_comma_in_arg_list);
1610 return true;
1611 case tok::r_paren: // #define X(A)
Chris Lattner70946da2009-02-20 22:46:43 +00001612 MI->setArgumentList(&Arguments[0], Arguments.size(), BP);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001613 return false;
1614 case tok::comma: // #define X(A,
1615 break;
1616 case tok::ellipsis: // #define X(A... -> GCC extension
1617 // Diagnose extension.
1618 Diag(Tok, diag::ext_named_variadic_macro);
Mike Stump11289f42009-09-09 15:08:12 +00001619
Chris Lattnerf64b3522008-03-09 01:54:53 +00001620 // Lex the token after the identifier.
1621 LexUnexpandedToken(Tok);
1622 if (Tok.isNot(tok::r_paren)) {
1623 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
1624 return true;
1625 }
Mike Stump11289f42009-09-09 15:08:12 +00001626
Chris Lattnerf64b3522008-03-09 01:54:53 +00001627 MI->setIsGNUVarargs();
Chris Lattner70946da2009-02-20 22:46:43 +00001628 MI->setArgumentList(&Arguments[0], Arguments.size(), BP);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001629 return false;
1630 }
1631 }
1632 }
1633}
1634
1635/// HandleDefineDirective - Implements #define. This consumes the entire macro
1636/// line then lets the caller lex the next real token.
1637void Preprocessor::HandleDefineDirective(Token &DefineTok) {
1638 ++NumDefined;
1639
1640 Token MacroNameTok;
1641 ReadMacroName(MacroNameTok, 1);
Mike Stump11289f42009-09-09 15:08:12 +00001642
Chris Lattnerf64b3522008-03-09 01:54:53 +00001643 // Error reading macro name? If so, diagnostic already issued.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001644 if (MacroNameTok.is(tok::eod))
Chris Lattnerf64b3522008-03-09 01:54:53 +00001645 return;
1646
Chris Lattnerd6e97af2009-04-21 04:46:33 +00001647 Token LastTok = MacroNameTok;
1648
Chris Lattnerf64b3522008-03-09 01:54:53 +00001649 // If we are supposed to keep comments in #defines, reenable comment saving
1650 // mode.
Ted Kremenek59e003e2008-11-18 00:43:07 +00001651 if (CurLexer) CurLexer->SetCommentRetentionState(KeepMacroComments);
Mike Stump11289f42009-09-09 15:08:12 +00001652
Chris Lattnerf64b3522008-03-09 01:54:53 +00001653 // Create the new macro.
Ted Kremenek6c7ea112008-12-15 19:56:42 +00001654 MacroInfo *MI = AllocateMacroInfo(MacroNameTok.getLocation());
Mike Stump11289f42009-09-09 15:08:12 +00001655
Chris Lattnerf64b3522008-03-09 01:54:53 +00001656 Token Tok;
1657 LexUnexpandedToken(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001658
Chris Lattnerf64b3522008-03-09 01:54:53 +00001659 // If this is a function-like macro definition, parse the argument list,
1660 // marking each of the identifiers as being used as macro arguments. Also,
1661 // check other constraints on the first token of the macro body.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001662 if (Tok.is(tok::eod)) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001663 // If there is no body to this macro, we have no special handling here.
Chris Lattner2425bcb2009-04-18 02:23:25 +00001664 } else if (Tok.hasLeadingSpace()) {
1665 // This is a normal token with leading space. Clear the leading space
1666 // marker on the first token to get proper expansion.
1667 Tok.clearFlag(Token::LeadingSpace);
1668 } else if (Tok.is(tok::l_paren)) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001669 // This is a function-like macro definition. Read the argument list.
1670 MI->setIsFunctionLike();
Abramo Bagnarac9e48c02012-03-31 20:17:27 +00001671 if (ReadMacroDefinitionArgList(MI, LastTok)) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001672 // Forget about MI.
Ted Kremenek6c7ea112008-12-15 19:56:42 +00001673 ReleaseMacroInfo(MI);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001674 // Throw away the rest of the line.
Ted Kremenek30cd88c2008-11-18 00:34:22 +00001675 if (CurPPLexer->ParsingPreprocessorDirective)
Chris Lattnerf64b3522008-03-09 01:54:53 +00001676 DiscardUntilEndOfDirective();
1677 return;
1678 }
1679
Chris Lattner249c38b2009-04-19 18:26:34 +00001680 // If this is a definition of a variadic C99 function-like macro, not using
1681 // the GNU named varargs extension, enabled __VA_ARGS__.
Mike Stump11289f42009-09-09 15:08:12 +00001682
Chris Lattner249c38b2009-04-19 18:26:34 +00001683 // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro.
1684 // This gets unpoisoned where it is allowed.
1685 assert(Ident__VA_ARGS__->isPoisoned() && "__VA_ARGS__ should be poisoned!");
1686 if (MI->isC99Varargs())
1687 Ident__VA_ARGS__->setIsPoisoned(false);
Mike Stump11289f42009-09-09 15:08:12 +00001688
Chris Lattnerf64b3522008-03-09 01:54:53 +00001689 // Read the first token after the arg list for down below.
1690 LexUnexpandedToken(Tok);
David Blaikiebbafb8a2012-03-11 07:00:24 +00001691 } else if (LangOpts.C99 || LangOpts.CPlusPlus0x) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001692 // C99 requires whitespace between the macro definition and the body. Emit
1693 // a diagnostic for something like "#define X+".
Chris Lattner2425bcb2009-04-18 02:23:25 +00001694 Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001695 } else {
Chris Lattner2425bcb2009-04-18 02:23:25 +00001696 // C90 6.8 TC1 says: "In the definition of an object-like macro, if the
1697 // first character of a replacement list is not a character required by
1698 // subclause 5.2.1, then there shall be white-space separation between the
1699 // identifier and the replacement list.". 5.2.1 lists this set:
1700 // "A-Za-z0-9!"#%&'()*+,_./:;<=>?[\]^_{|}~" as well as whitespace, which
1701 // is irrelevant here.
1702 bool isInvalid = false;
1703 if (Tok.is(tok::at)) // @ is not in the list above.
1704 isInvalid = true;
1705 else if (Tok.is(tok::unknown)) {
1706 // If we have an unknown token, it is something strange like "`". Since
1707 // all of valid characters would have lexed into a single character
1708 // token of some sort, we know this is not a valid case.
1709 isInvalid = true;
1710 }
1711 if (isInvalid)
1712 Diag(Tok, diag::ext_missing_whitespace_after_macro_name);
1713 else
1714 Diag(Tok, diag::warn_missing_whitespace_after_macro_name);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001715 }
Chris Lattnerd6e97af2009-04-21 04:46:33 +00001716
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001717 if (!Tok.is(tok::eod))
Chris Lattnerd6e97af2009-04-21 04:46:33 +00001718 LastTok = Tok;
1719
Chris Lattnerf64b3522008-03-09 01:54:53 +00001720 // Read the rest of the macro body.
1721 if (MI->isObjectLike()) {
1722 // Object-like macros are very simple, just read their body.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001723 while (Tok.isNot(tok::eod)) {
Chris Lattnerd6e97af2009-04-21 04:46:33 +00001724 LastTok = Tok;
Chris Lattnerf64b3522008-03-09 01:54:53 +00001725 MI->AddTokenToBody(Tok);
1726 // Get the next token of the macro.
1727 LexUnexpandedToken(Tok);
1728 }
Mike Stump11289f42009-09-09 15:08:12 +00001729
Chris Lattnerf64b3522008-03-09 01:54:53 +00001730 } else {
Chris Lattner83bd8282009-05-25 17:16:10 +00001731 // Otherwise, read the body of a function-like macro. While we are at it,
1732 // check C99 6.10.3.2p1: ensure that # operators are followed by macro
1733 // parameters in function-like macro expansions.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001734 while (Tok.isNot(tok::eod)) {
Chris Lattnerd6e97af2009-04-21 04:46:33 +00001735 LastTok = Tok;
Chris Lattnerf64b3522008-03-09 01:54:53 +00001736
Chris Lattnerf64b3522008-03-09 01:54:53 +00001737 if (Tok.isNot(tok::hash)) {
Chris Lattner83bd8282009-05-25 17:16:10 +00001738 MI->AddTokenToBody(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001739
Chris Lattnerf64b3522008-03-09 01:54:53 +00001740 // Get the next token of the macro.
1741 LexUnexpandedToken(Tok);
1742 continue;
1743 }
Mike Stump11289f42009-09-09 15:08:12 +00001744
Chris Lattnerf64b3522008-03-09 01:54:53 +00001745 // Get the next token of the macro.
1746 LexUnexpandedToken(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001747
Chris Lattner83bd8282009-05-25 17:16:10 +00001748 // Check for a valid macro arg identifier.
1749 if (Tok.getIdentifierInfo() == 0 ||
1750 MI->getArgumentNum(Tok.getIdentifierInfo()) == -1) {
1751
1752 // If this is assembler-with-cpp mode, we accept random gibberish after
1753 // the '#' because '#' is often a comment character. However, change
1754 // the kind of the token to tok::unknown so that the preprocessor isn't
1755 // confused.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001756 if (getLangOpts().AsmPreprocessor && Tok.isNot(tok::eod)) {
Chris Lattner83bd8282009-05-25 17:16:10 +00001757 LastTok.setKind(tok::unknown);
1758 } else {
1759 Diag(Tok, diag::err_pp_stringize_not_parameter);
1760 ReleaseMacroInfo(MI);
Mike Stump11289f42009-09-09 15:08:12 +00001761
Chris Lattner83bd8282009-05-25 17:16:10 +00001762 // Disable __VA_ARGS__ again.
1763 Ident__VA_ARGS__->setIsPoisoned(true);
1764 return;
1765 }
Chris Lattnerf64b3522008-03-09 01:54:53 +00001766 }
Mike Stump11289f42009-09-09 15:08:12 +00001767
Chris Lattner83bd8282009-05-25 17:16:10 +00001768 // Things look ok, add the '#' and param name tokens to the macro.
1769 MI->AddTokenToBody(LastTok);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001770 MI->AddTokenToBody(Tok);
Chris Lattner83bd8282009-05-25 17:16:10 +00001771 LastTok = Tok;
Mike Stump11289f42009-09-09 15:08:12 +00001772
Chris Lattnerf64b3522008-03-09 01:54:53 +00001773 // Get the next token of the macro.
1774 LexUnexpandedToken(Tok);
1775 }
1776 }
Mike Stump11289f42009-09-09 15:08:12 +00001777
1778
Chris Lattnerf64b3522008-03-09 01:54:53 +00001779 // Disable __VA_ARGS__ again.
1780 Ident__VA_ARGS__->setIsPoisoned(true);
1781
Chris Lattner57540c52011-04-15 05:22:18 +00001782 // Check that there is no paste (##) operator at the beginning or end of the
Chris Lattnerf64b3522008-03-09 01:54:53 +00001783 // replacement list.
1784 unsigned NumTokens = MI->getNumTokens();
1785 if (NumTokens != 0) {
1786 if (MI->getReplacementToken(0).is(tok::hashhash)) {
1787 Diag(MI->getReplacementToken(0), diag::err_paste_at_start);
Ted Kremenek6c7ea112008-12-15 19:56:42 +00001788 ReleaseMacroInfo(MI);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001789 return;
1790 }
1791 if (MI->getReplacementToken(NumTokens-1).is(tok::hashhash)) {
1792 Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end);
Ted Kremenek6c7ea112008-12-15 19:56:42 +00001793 ReleaseMacroInfo(MI);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001794 return;
1795 }
1796 }
Mike Stump11289f42009-09-09 15:08:12 +00001797
Chris Lattnerd6e97af2009-04-21 04:46:33 +00001798 MI->setDefinitionEndLoc(LastTok.getLocation());
Mike Stump11289f42009-09-09 15:08:12 +00001799
Chris Lattnerf64b3522008-03-09 01:54:53 +00001800 // Finally, if this identifier already had a macro defined for it, verify that
1801 // the macro bodies are identical and free the old definition.
1802 if (MacroInfo *OtherMI = getMacroInfo(MacroNameTok.getIdentifierInfo())) {
Chris Lattner5244f342009-01-16 19:50:11 +00001803 // It is very common for system headers to have tons of macro redefinitions
1804 // and for warnings to be disabled in system headers. If this is the case,
1805 // then don't bother calling MacroInfo::isIdenticalTo.
Chris Lattner80c21df2009-03-13 21:17:23 +00001806 if (!getDiagnostics().getSuppressSystemWarnings() ||
Chris Lattner5244f342009-01-16 19:50:11 +00001807 !SourceMgr.isInSystemHeader(DefineTok.getLocation())) {
Argyrios Kyrtzidisb495cc12011-01-18 19:50:15 +00001808 if (!OtherMI->isUsed() && OtherMI->isWarnIfUnused())
Chris Lattner5244f342009-01-16 19:50:11 +00001809 Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001810
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001811 // Macros must be identical. This means all tokens and whitespace
Chris Lattner5244f342009-01-16 19:50:11 +00001812 // separation must be the same. C99 6.10.3.2.
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001813 if (!OtherMI->isAllowRedefinitionsWithoutWarning() &&
Eli Friedman04831922010-08-22 01:00:03 +00001814 !MI->isIdenticalTo(*OtherMI, *this)) {
Chris Lattner5244f342009-01-16 19:50:11 +00001815 Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef)
1816 << MacroNameTok.getIdentifierInfo();
1817 Diag(OtherMI->getDefinitionLoc(), diag::note_previous_definition);
1818 }
Chris Lattnerf64b3522008-03-09 01:54:53 +00001819 }
Argyrios Kyrtzidisb495cc12011-01-18 19:50:15 +00001820 if (OtherMI->isWarnIfUnused())
1821 WarnUnusedMacroLocs.erase(OtherMI->getDefinitionLoc());
Ted Kremenek6c7ea112008-12-15 19:56:42 +00001822 ReleaseMacroInfo(OtherMI);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001823 }
Mike Stump11289f42009-09-09 15:08:12 +00001824
Chris Lattnerf64b3522008-03-09 01:54:53 +00001825 setMacroInfo(MacroNameTok.getIdentifierInfo(), MI);
Mike Stump11289f42009-09-09 15:08:12 +00001826
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +00001827 assert(!MI->isUsed());
1828 // If we need warning for not using the macro, add its location in the
1829 // warn-because-unused-macro set. If it gets used it will be removed from set.
1830 if (isInPrimaryFile() && // don't warn for include'd macros.
1831 Diags->getDiagnosticLevel(diag::pp_macro_not_used,
David Blaikie9c902b52011-09-25 23:23:43 +00001832 MI->getDefinitionLoc()) != DiagnosticsEngine::Ignored) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +00001833 MI->setIsWarnIfUnused(true);
1834 WarnUnusedMacroLocs.insert(MI->getDefinitionLoc());
1835 }
1836
Chris Lattner928e9092009-04-12 01:39:54 +00001837 // If the callbacks want to know, tell them about the macro definition.
1838 if (Callbacks)
Craig Silverstein1a9ca212010-11-19 21:33:15 +00001839 Callbacks->MacroDefined(MacroNameTok, MI);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001840}
1841
1842/// HandleUndefDirective - Implements #undef.
1843///
1844void Preprocessor::HandleUndefDirective(Token &UndefTok) {
1845 ++NumUndefined;
1846
1847 Token MacroNameTok;
1848 ReadMacroName(MacroNameTok, 2);
Mike Stump11289f42009-09-09 15:08:12 +00001849
Chris Lattnerf64b3522008-03-09 01:54:53 +00001850 // Error reading macro name? If so, diagnostic already issued.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001851 if (MacroNameTok.is(tok::eod))
Chris Lattnerf64b3522008-03-09 01:54:53 +00001852 return;
Mike Stump11289f42009-09-09 15:08:12 +00001853
Chris Lattnerf64b3522008-03-09 01:54:53 +00001854 // Check to see if this is the last token on the #undef line.
Chris Lattnerce2ab6f2009-04-14 05:07:49 +00001855 CheckEndOfDirective("undef");
Mike Stump11289f42009-09-09 15:08:12 +00001856
Chris Lattnerf64b3522008-03-09 01:54:53 +00001857 // Okay, we finally have a valid identifier to undef.
1858 MacroInfo *MI = getMacroInfo(MacroNameTok.getIdentifierInfo());
Mike Stump11289f42009-09-09 15:08:12 +00001859
Chris Lattnerf64b3522008-03-09 01:54:53 +00001860 // If the macro is not defined, this is a noop undef, just return.
1861 if (MI == 0) return;
1862
Argyrios Kyrtzidis22998892011-07-11 20:39:47 +00001863 if (!MI->isUsed() && MI->isWarnIfUnused())
Chris Lattnerf64b3522008-03-09 01:54:53 +00001864 Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used);
Chris Lattnercd6d4b12009-04-21 03:42:09 +00001865
1866 // If the callbacks want to know, tell them about the macro #undef.
1867 if (Callbacks)
Craig Silverstein1a9ca212010-11-19 21:33:15 +00001868 Callbacks->MacroUndefined(MacroNameTok, MI);
Chris Lattnercd6d4b12009-04-21 03:42:09 +00001869
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +00001870 if (MI->isWarnIfUnused())
1871 WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());
1872
Chris Lattnerf64b3522008-03-09 01:54:53 +00001873 // Free macro definition.
Ted Kremenek6c7ea112008-12-15 19:56:42 +00001874 ReleaseMacroInfo(MI);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001875 setMacroInfo(MacroNameTok.getIdentifierInfo(), 0);
1876}
1877
1878
1879//===----------------------------------------------------------------------===//
1880// Preprocessor Conditional Directive Handling.
1881//===----------------------------------------------------------------------===//
1882
1883/// HandleIfdefDirective - Implements the #ifdef/#ifndef directive. isIfndef is
1884/// true when this is a #ifndef directive. ReadAnyTokensBeforeDirective is true
1885/// if any tokens have been returned or pp-directives activated before this
1886/// #ifndef has been lexed.
1887///
1888void Preprocessor::HandleIfdefDirective(Token &Result, bool isIfndef,
1889 bool ReadAnyTokensBeforeDirective) {
1890 ++NumIf;
1891 Token DirectiveTok = Result;
1892
1893 Token MacroNameTok;
1894 ReadMacroName(MacroNameTok);
Mike Stump11289f42009-09-09 15:08:12 +00001895
Chris Lattnerf64b3522008-03-09 01:54:53 +00001896 // Error reading macro name? If so, diagnostic already issued.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001897 if (MacroNameTok.is(tok::eod)) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001898 // Skip code until we get to #endif. This helps with recovery by not
1899 // emitting an error when the #endif is reached.
1900 SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
1901 /*Foundnonskip*/false, /*FoundElse*/false);
1902 return;
1903 }
Mike Stump11289f42009-09-09 15:08:12 +00001904
Chris Lattnerf64b3522008-03-09 01:54:53 +00001905 // Check to see if this is the last token on the #if[n]def line.
Chris Lattnerce2ab6f2009-04-14 05:07:49 +00001906 CheckEndOfDirective(isIfndef ? "ifndef" : "ifdef");
Chris Lattnerf64b3522008-03-09 01:54:53 +00001907
Chris Lattneraa1cccbb2010-02-12 08:03:27 +00001908 IdentifierInfo *MII = MacroNameTok.getIdentifierInfo();
1909 MacroInfo *MI = getMacroInfo(MII);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001910
Ted Kremenek30cd88c2008-11-18 00:34:22 +00001911 if (CurPPLexer->getConditionalStackDepth() == 0) {
Chris Lattneraa1cccbb2010-02-12 08:03:27 +00001912 // If the start of a top-level #ifdef and if the macro is not defined,
1913 // inform MIOpt that this might be the start of a proper include guard.
1914 // Otherwise it is some other form of unknown conditional which we can't
1915 // handle.
1916 if (!ReadAnyTokensBeforeDirective && MI == 0) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001917 assert(isIfndef && "#ifdef shouldn't reach here");
Chris Lattneraa1cccbb2010-02-12 08:03:27 +00001918 CurPPLexer->MIOpt.EnterTopLevelIFNDEF(MII);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001919 } else
Ted Kremenek30cd88c2008-11-18 00:34:22 +00001920 CurPPLexer->MIOpt.EnterTopLevelConditional();
Chris Lattnerf64b3522008-03-09 01:54:53 +00001921 }
1922
Chris Lattnerf64b3522008-03-09 01:54:53 +00001923 // If there is a macro, process it.
1924 if (MI) // Mark it used.
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +00001925 markMacroAsUsed(MI);
Mike Stump11289f42009-09-09 15:08:12 +00001926
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +00001927 if (Callbacks) {
1928 if (isIfndef)
1929 Callbacks->Ifndef(DirectiveTok.getLocation(), MacroNameTok);
1930 else
1931 Callbacks->Ifdef(DirectiveTok.getLocation(), MacroNameTok);
1932 }
1933
Chris Lattnerf64b3522008-03-09 01:54:53 +00001934 // Should we include the stuff contained by this directive?
1935 if (!MI == isIfndef) {
1936 // Yes, remember that we are inside a conditional, then lex the next token.
Chris Lattner8cf1f932009-12-14 04:54:40 +00001937 CurPPLexer->pushConditionalLevel(DirectiveTok.getLocation(),
1938 /*wasskip*/false, /*foundnonskip*/true,
1939 /*foundelse*/false);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001940 } else {
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00001941 // No, skip the contents of this block.
Chris Lattnerf64b3522008-03-09 01:54:53 +00001942 SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
Mike Stump11289f42009-09-09 15:08:12 +00001943 /*Foundnonskip*/false,
Chris Lattnerf64b3522008-03-09 01:54:53 +00001944 /*FoundElse*/false);
1945 }
1946}
1947
1948/// HandleIfDirective - Implements the #if directive.
1949///
1950void Preprocessor::HandleIfDirective(Token &IfToken,
1951 bool ReadAnyTokensBeforeDirective) {
1952 ++NumIf;
Mike Stump11289f42009-09-09 15:08:12 +00001953
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00001954 // Parse and evaluate the conditional expression.
Chris Lattnerf64b3522008-03-09 01:54:53 +00001955 IdentifierInfo *IfNDefMacro = 0;
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00001956 const SourceLocation ConditionalBegin = CurPPLexer->getSourceLocation();
1957 const bool ConditionalTrue = EvaluateDirectiveExpression(IfNDefMacro);
1958 const SourceLocation ConditionalEnd = CurPPLexer->getSourceLocation();
Nuno Lopes363212b2008-06-01 18:31:24 +00001959
1960 // If this condition is equivalent to #ifndef X, and if this is the first
1961 // directive seen, handle it for the multiple-include optimization.
Ted Kremenek30cd88c2008-11-18 00:34:22 +00001962 if (CurPPLexer->getConditionalStackDepth() == 0) {
Chris Lattneraa1cccbb2010-02-12 08:03:27 +00001963 if (!ReadAnyTokensBeforeDirective && IfNDefMacro && ConditionalTrue)
Ted Kremenek30cd88c2008-11-18 00:34:22 +00001964 CurPPLexer->MIOpt.EnterTopLevelIFNDEF(IfNDefMacro);
Nuno Lopes363212b2008-06-01 18:31:24 +00001965 else
Ted Kremenek30cd88c2008-11-18 00:34:22 +00001966 CurPPLexer->MIOpt.EnterTopLevelConditional();
Nuno Lopes363212b2008-06-01 18:31:24 +00001967 }
1968
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +00001969 if (Callbacks)
1970 Callbacks->If(IfToken.getLocation(),
1971 SourceRange(ConditionalBegin, ConditionalEnd));
1972
Chris Lattnerf64b3522008-03-09 01:54:53 +00001973 // Should we include the stuff contained by this directive?
1974 if (ConditionalTrue) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001975 // Yes, remember that we are inside a conditional, then lex the next token.
Ted Kremenek30cd88c2008-11-18 00:34:22 +00001976 CurPPLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
Chris Lattnerf64b3522008-03-09 01:54:53 +00001977 /*foundnonskip*/true, /*foundelse*/false);
1978 } else {
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00001979 // No, skip the contents of this block.
Mike Stump11289f42009-09-09 15:08:12 +00001980 SkipExcludedConditionalBlock(IfToken.getLocation(), /*Foundnonskip*/false,
Chris Lattnerf64b3522008-03-09 01:54:53 +00001981 /*FoundElse*/false);
1982 }
1983}
1984
1985/// HandleEndifDirective - Implements the #endif directive.
1986///
1987void Preprocessor::HandleEndifDirective(Token &EndifToken) {
1988 ++NumEndif;
Mike Stump11289f42009-09-09 15:08:12 +00001989
Chris Lattnerf64b3522008-03-09 01:54:53 +00001990 // Check that this is the whole directive.
Chris Lattnerce2ab6f2009-04-14 05:07:49 +00001991 CheckEndOfDirective("endif");
Mike Stump11289f42009-09-09 15:08:12 +00001992
Chris Lattnerf64b3522008-03-09 01:54:53 +00001993 PPConditionalInfo CondInfo;
Ted Kremenek30cd88c2008-11-18 00:34:22 +00001994 if (CurPPLexer->popConditionalLevel(CondInfo)) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001995 // No conditionals on the stack: this is an #endif without an #if.
Chris Lattner907dfe92008-11-18 07:59:24 +00001996 Diag(EndifToken, diag::err_pp_endif_without_if);
1997 return;
Chris Lattnerf64b3522008-03-09 01:54:53 +00001998 }
Mike Stump11289f42009-09-09 15:08:12 +00001999
Chris Lattnerf64b3522008-03-09 01:54:53 +00002000 // If this the end of a top-level #endif, inform MIOpt.
Ted Kremenek30cd88c2008-11-18 00:34:22 +00002001 if (CurPPLexer->getConditionalStackDepth() == 0)
2002 CurPPLexer->MIOpt.ExitTopLevelConditional();
Mike Stump11289f42009-09-09 15:08:12 +00002003
Ted Kremenek30cd88c2008-11-18 00:34:22 +00002004 assert(!CondInfo.WasSkipping && !CurPPLexer->LexingRawMode &&
Chris Lattnerf64b3522008-03-09 01:54:53 +00002005 "This code should only be reachable in the non-skipping case!");
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00002006
2007 if (Callbacks)
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +00002008 Callbacks->Endif(EndifToken.getLocation(), CondInfo.IfLoc);
Chris Lattnerf64b3522008-03-09 01:54:53 +00002009}
2010
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00002011/// HandleElseDirective - Implements the #else directive.
2012///
Chris Lattnerf64b3522008-03-09 01:54:53 +00002013void Preprocessor::HandleElseDirective(Token &Result) {
2014 ++NumElse;
Mike Stump11289f42009-09-09 15:08:12 +00002015
Chris Lattnerf64b3522008-03-09 01:54:53 +00002016 // #else directive in a non-skipping conditional... start skipping.
Chris Lattnerce2ab6f2009-04-14 05:07:49 +00002017 CheckEndOfDirective("else");
Mike Stump11289f42009-09-09 15:08:12 +00002018
Chris Lattnerf64b3522008-03-09 01:54:53 +00002019 PPConditionalInfo CI;
Chris Lattner907dfe92008-11-18 07:59:24 +00002020 if (CurPPLexer->popConditionalLevel(CI)) {
2021 Diag(Result, diag::pp_err_else_without_if);
2022 return;
2023 }
Mike Stump11289f42009-09-09 15:08:12 +00002024
Chris Lattnerf64b3522008-03-09 01:54:53 +00002025 // If this is a top-level #else, inform the MIOpt.
Ted Kremenek30cd88c2008-11-18 00:34:22 +00002026 if (CurPPLexer->getConditionalStackDepth() == 0)
2027 CurPPLexer->MIOpt.EnterTopLevelConditional();
Chris Lattnerf64b3522008-03-09 01:54:53 +00002028
2029 // If this is a #else with a #else before it, report the error.
2030 if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else);
Mike Stump11289f42009-09-09 15:08:12 +00002031
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +00002032 if (Callbacks)
2033 Callbacks->Else(Result.getLocation(), CI.IfLoc);
2034
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00002035 // Finally, skip the rest of the contents of this block.
2036 SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
Argyrios Kyrtzidis18bcfd52011-09-27 17:32:05 +00002037 /*FoundElse*/true, Result.getLocation());
Chris Lattnerf64b3522008-03-09 01:54:53 +00002038}
2039
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00002040/// HandleElifDirective - Implements the #elif directive.
2041///
Chris Lattnerf64b3522008-03-09 01:54:53 +00002042void Preprocessor::HandleElifDirective(Token &ElifToken) {
2043 ++NumElse;
Mike Stump11289f42009-09-09 15:08:12 +00002044
Chris Lattnerf64b3522008-03-09 01:54:53 +00002045 // #elif directive in a non-skipping conditional... start skipping.
2046 // We don't care what the condition is, because we will always skip it (since
2047 // the block immediately before it was included).
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00002048 const SourceLocation ConditionalBegin = CurPPLexer->getSourceLocation();
Chris Lattnerf64b3522008-03-09 01:54:53 +00002049 DiscardUntilEndOfDirective();
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00002050 const SourceLocation ConditionalEnd = CurPPLexer->getSourceLocation();
Chris Lattnerf64b3522008-03-09 01:54:53 +00002051
2052 PPConditionalInfo CI;
Chris Lattner907dfe92008-11-18 07:59:24 +00002053 if (CurPPLexer->popConditionalLevel(CI)) {
2054 Diag(ElifToken, diag::pp_err_elif_without_if);
2055 return;
2056 }
Mike Stump11289f42009-09-09 15:08:12 +00002057
Chris Lattnerf64b3522008-03-09 01:54:53 +00002058 // If this is a top-level #elif, inform the MIOpt.
Ted Kremenek30cd88c2008-11-18 00:34:22 +00002059 if (CurPPLexer->getConditionalStackDepth() == 0)
2060 CurPPLexer->MIOpt.EnterTopLevelConditional();
Mike Stump11289f42009-09-09 15:08:12 +00002061
Chris Lattnerf64b3522008-03-09 01:54:53 +00002062 // If this is a #elif with a #else before it, report the error.
2063 if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else);
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +00002064
2065 if (Callbacks)
2066 Callbacks->Elif(ElifToken.getLocation(),
2067 SourceRange(ConditionalBegin, ConditionalEnd), CI.IfLoc);
Chris Lattnerf64b3522008-03-09 01:54:53 +00002068
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00002069 // Finally, skip the rest of the contents of this block.
2070 SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
Argyrios Kyrtzidis18bcfd52011-09-27 17:32:05 +00002071 /*FoundElse*/CI.FoundElse,
2072 ElifToken.getLocation());
Chris Lattnerf64b3522008-03-09 01:54:53 +00002073}