blob: 625a204af995b4790e1e6b1e471573af584ee3ab [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.
Chris Lattner100c65e2009-01-26 05:29:08 +00001021 std::string Message = CurLexer->ReadToEndOfLine();
Ted Kremenek7f4bd162012-02-02 00:16:13 +00001022
1023 // Find the first non-whitespace character, so that we can make the
1024 // diagnostic more succinct.
1025 StringRef Msg(Message);
1026 size_t i = Msg.find_first_not_of(' ');
1027 if (i < Msg.size())
1028 Msg = Msg.substr(i);
1029
Chris Lattner100c65e2009-01-26 05:29:08 +00001030 if (isWarning)
Ted Kremenek7f4bd162012-02-02 00:16:13 +00001031 Diag(Tok, diag::pp_hash_warning) << Msg;
Chris Lattner100c65e2009-01-26 05:29:08 +00001032 else
Ted Kremenek7f4bd162012-02-02 00:16:13 +00001033 Diag(Tok, diag::err_pp_hash_error) << Msg;
Chris Lattnerf64b3522008-03-09 01:54:53 +00001034}
1035
1036/// HandleIdentSCCSDirective - Handle a #ident/#sccs directive.
1037///
1038void Preprocessor::HandleIdentSCCSDirective(Token &Tok) {
1039 // Yes, this directive is an extension.
1040 Diag(Tok, diag::ext_pp_ident_directive);
Mike Stump11289f42009-09-09 15:08:12 +00001041
Chris Lattnerf64b3522008-03-09 01:54:53 +00001042 // Read the string argument.
1043 Token StrTok;
1044 Lex(StrTok);
Mike Stump11289f42009-09-09 15:08:12 +00001045
Chris Lattnerf64b3522008-03-09 01:54:53 +00001046 // If the token kind isn't a string, it's a malformed directive.
1047 if (StrTok.isNot(tok::string_literal) &&
Chris Lattner907dfe92008-11-18 07:59:24 +00001048 StrTok.isNot(tok::wide_string_literal)) {
1049 Diag(StrTok, diag::err_pp_malformed_ident);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001050 if (StrTok.isNot(tok::eod))
Chris Lattner38d7fd22009-01-26 05:30:54 +00001051 DiscardUntilEndOfDirective();
Chris Lattner907dfe92008-11-18 07:59:24 +00001052 return;
1053 }
Mike Stump11289f42009-09-09 15:08:12 +00001054
Richard Smithd67aea22012-03-06 03:21:47 +00001055 if (StrTok.hasUDSuffix()) {
1056 Diag(StrTok, diag::err_invalid_string_udl);
1057 return DiscardUntilEndOfDirective();
1058 }
1059
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001060 // Verify that there is nothing after the string, other than EOD.
Chris Lattnerce2ab6f2009-04-14 05:07:49 +00001061 CheckEndOfDirective("ident");
Chris Lattnerf64b3522008-03-09 01:54:53 +00001062
Douglas Gregordc970f02010-03-16 22:30:13 +00001063 if (Callbacks) {
1064 bool Invalid = false;
1065 std::string Str = getSpelling(StrTok, &Invalid);
1066 if (!Invalid)
1067 Callbacks->Ident(Tok.getLocation(), Str);
1068 }
Chris Lattnerf64b3522008-03-09 01:54:53 +00001069}
1070
Douglas Gregor0bf886d2012-01-03 18:24:14 +00001071/// \brief Handle a #public directive.
1072void Preprocessor::HandleMacroPublicDirective(Token &Tok) {
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001073 Token MacroNameTok;
1074 ReadMacroName(MacroNameTok, 2);
1075
1076 // Error reading macro name? If so, diagnostic already issued.
1077 if (MacroNameTok.is(tok::eod))
1078 return;
1079
Douglas Gregor663b48f2012-01-03 19:48:16 +00001080 // Check to see if this is the last token on the #__public_macro line.
1081 CheckEndOfDirective("__public_macro");
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001082
1083 // Okay, we finally have a valid identifier to undef.
1084 MacroInfo *MI = getMacroInfo(MacroNameTok.getIdentifierInfo());
1085
1086 // If the macro is not defined, this is an error.
1087 if (MI == 0) {
Douglas Gregorebf00492011-10-17 15:32:29 +00001088 Diag(MacroNameTok, diag::err_pp_visibility_non_macro)
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001089 << MacroNameTok.getIdentifierInfo();
1090 return;
1091 }
1092
1093 // Note that this macro has now been exported.
Douglas Gregorebf00492011-10-17 15:32:29 +00001094 MI->setVisibility(/*IsPublic=*/true, MacroNameTok.getLocation());
1095
1096 // If this macro definition came from a PCH file, mark it
1097 // as having changed since serialization.
1098 if (MI->isFromAST())
1099 MI->setChangedAfterLoad();
1100}
1101
Douglas Gregor0bf886d2012-01-03 18:24:14 +00001102/// \brief Handle a #private directive.
Douglas Gregorebf00492011-10-17 15:32:29 +00001103void Preprocessor::HandleMacroPrivateDirective(Token &Tok) {
1104 Token MacroNameTok;
1105 ReadMacroName(MacroNameTok, 2);
1106
1107 // Error reading macro name? If so, diagnostic already issued.
1108 if (MacroNameTok.is(tok::eod))
1109 return;
1110
Douglas Gregor663b48f2012-01-03 19:48:16 +00001111 // Check to see if this is the last token on the #__private_macro line.
1112 CheckEndOfDirective("__private_macro");
Douglas Gregorebf00492011-10-17 15:32:29 +00001113
1114 // Okay, we finally have a valid identifier to undef.
1115 MacroInfo *MI = getMacroInfo(MacroNameTok.getIdentifierInfo());
1116
1117 // If the macro is not defined, this is an error.
1118 if (MI == 0) {
1119 Diag(MacroNameTok, diag::err_pp_visibility_non_macro)
1120 << MacroNameTok.getIdentifierInfo();
1121 return;
1122 }
1123
1124 // Note that this macro has now been marked private.
1125 MI->setVisibility(/*IsPublic=*/false, MacroNameTok.getLocation());
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001126
1127 // If this macro definition came from a PCH file, mark it
1128 // as having changed since serialization.
1129 if (MI->isFromAST())
1130 MI->setChangedAfterLoad();
1131}
1132
Chris Lattnerf64b3522008-03-09 01:54:53 +00001133//===----------------------------------------------------------------------===//
1134// Preprocessor Include Directive Handling.
1135//===----------------------------------------------------------------------===//
1136
1137/// GetIncludeFilenameSpelling - Turn the specified lexer token into a fully
1138/// checked and spelled filename, e.g. as an operand of #include. This returns
1139/// true if the input filename was in <>'s or false if it were in ""'s. The
1140/// caller is expected to provide a buffer that is large enough to hold the
1141/// spelling of the filename, but is also expected to handle the case when
1142/// this method decides to use a different buffer.
1143bool Preprocessor::GetIncludeFilenameSpelling(SourceLocation Loc,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001144 StringRef &Buffer) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001145 // Get the text form of the filename.
Chris Lattnerd081f8c2010-01-10 01:35:12 +00001146 assert(!Buffer.empty() && "Can't have tokens with empty spellings!");
Mike Stump11289f42009-09-09 15:08:12 +00001147
Chris Lattnerf64b3522008-03-09 01:54:53 +00001148 // Make sure the filename is <x> or "x".
1149 bool isAngled;
Chris Lattnerd081f8c2010-01-10 01:35:12 +00001150 if (Buffer[0] == '<') {
1151 if (Buffer.back() != '>') {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001152 Diag(Loc, diag::err_pp_expects_filename);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001153 Buffer = StringRef();
Chris Lattnerf64b3522008-03-09 01:54:53 +00001154 return true;
1155 }
1156 isAngled = true;
Chris Lattnerd081f8c2010-01-10 01:35:12 +00001157 } else if (Buffer[0] == '"') {
1158 if (Buffer.back() != '"') {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001159 Diag(Loc, diag::err_pp_expects_filename);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001160 Buffer = StringRef();
Chris Lattnerf64b3522008-03-09 01:54:53 +00001161 return true;
1162 }
1163 isAngled = false;
1164 } else {
1165 Diag(Loc, diag::err_pp_expects_filename);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001166 Buffer = StringRef();
Chris Lattnerf64b3522008-03-09 01:54:53 +00001167 return true;
1168 }
Mike Stump11289f42009-09-09 15:08:12 +00001169
Chris Lattnerf64b3522008-03-09 01:54:53 +00001170 // Diagnose #include "" as invalid.
Chris Lattnerd081f8c2010-01-10 01:35:12 +00001171 if (Buffer.size() <= 2) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001172 Diag(Loc, diag::err_pp_empty_filename);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001173 Buffer = StringRef();
Chris Lattnerd081f8c2010-01-10 01:35:12 +00001174 return true;
Chris Lattnerf64b3522008-03-09 01:54:53 +00001175 }
Mike Stump11289f42009-09-09 15:08:12 +00001176
Chris Lattnerf64b3522008-03-09 01:54:53 +00001177 // Skip the brackets.
Chris Lattnerd081f8c2010-01-10 01:35:12 +00001178 Buffer = Buffer.substr(1, Buffer.size()-2);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001179 return isAngled;
1180}
1181
1182/// ConcatenateIncludeName - Handle cases where the #include name is expanded
1183/// from a macro as multiple tokens, which need to be glued together. This
1184/// occurs for code like:
1185/// #define FOO <a/b.h>
1186/// #include FOO
1187/// because in this case, "<a/b.h>" is returned as 7 tokens, not one.
1188///
1189/// This code concatenates and consumes tokens up to the '>' token. It returns
1190/// false if the > was found, otherwise it returns true if it finds and consumes
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001191/// the EOD marker.
John Thompsonb5353522009-10-30 13:49:06 +00001192bool Preprocessor::ConcatenateIncludeName(
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001193 SmallString<128> &FilenameBuffer,
Douglas Gregor796d76a2010-10-20 22:00:55 +00001194 SourceLocation &End) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001195 Token CurTok;
Mike Stump11289f42009-09-09 15:08:12 +00001196
John Thompsonb5353522009-10-30 13:49:06 +00001197 Lex(CurTok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001198 while (CurTok.isNot(tok::eod)) {
Douglas Gregor796d76a2010-10-20 22:00:55 +00001199 End = CurTok.getLocation();
1200
Douglas Gregor9c7bd2f2010-12-09 23:35:36 +00001201 // FIXME: Provide code completion for #includes.
1202 if (CurTok.is(tok::code_completion)) {
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001203 setCodeCompletionReached();
Douglas Gregor9c7bd2f2010-12-09 23:35:36 +00001204 Lex(CurTok);
1205 continue;
1206 }
1207
Chris Lattnerf64b3522008-03-09 01:54:53 +00001208 // Append the spelling of this token to the buffer. If there was a space
1209 // before it, add it now.
1210 if (CurTok.hasLeadingSpace())
1211 FilenameBuffer.push_back(' ');
Mike Stump11289f42009-09-09 15:08:12 +00001212
Chris Lattnerf64b3522008-03-09 01:54:53 +00001213 // Get the spelling of the token, directly into FilenameBuffer if possible.
1214 unsigned PreAppendSize = FilenameBuffer.size();
1215 FilenameBuffer.resize(PreAppendSize+CurTok.getLength());
Mike Stump11289f42009-09-09 15:08:12 +00001216
Chris Lattnerf64b3522008-03-09 01:54:53 +00001217 const char *BufPtr = &FilenameBuffer[PreAppendSize];
John Thompsonb5353522009-10-30 13:49:06 +00001218 unsigned ActualLen = getSpelling(CurTok, BufPtr);
Mike Stump11289f42009-09-09 15:08:12 +00001219
Chris Lattnerf64b3522008-03-09 01:54:53 +00001220 // If the token was spelled somewhere else, copy it into FilenameBuffer.
1221 if (BufPtr != &FilenameBuffer[PreAppendSize])
1222 memcpy(&FilenameBuffer[PreAppendSize], BufPtr, ActualLen);
Mike Stump11289f42009-09-09 15:08:12 +00001223
Chris Lattnerf64b3522008-03-09 01:54:53 +00001224 // Resize FilenameBuffer to the correct size.
1225 if (CurTok.getLength() != ActualLen)
1226 FilenameBuffer.resize(PreAppendSize+ActualLen);
Mike Stump11289f42009-09-09 15:08:12 +00001227
Chris Lattnerf64b3522008-03-09 01:54:53 +00001228 // If we found the '>' marker, return success.
1229 if (CurTok.is(tok::greater))
1230 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001231
John Thompsonb5353522009-10-30 13:49:06 +00001232 Lex(CurTok);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001233 }
1234
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001235 // If we hit the eod marker, emit an error and return true so that the caller
1236 // knows the EOD has been read.
John Thompsonb5353522009-10-30 13:49:06 +00001237 Diag(CurTok.getLocation(), diag::err_pp_expects_filename);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001238 return true;
1239}
1240
1241/// HandleIncludeDirective - The "#include" tokens have just been read, read the
1242/// file to be included from the lexer, then include it! This is a common
1243/// routine with functionality shared between #include, #include_next and
Chris Lattnerc88a23e2008-09-26 20:12:23 +00001244/// #import. LookupFrom is set when this is a #include_next directive, it
Mike Stump11289f42009-09-09 15:08:12 +00001245/// specifies the file to start searching from.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001246void Preprocessor::HandleIncludeDirective(SourceLocation HashLoc,
1247 Token &IncludeTok,
Chris Lattnerf64b3522008-03-09 01:54:53 +00001248 const DirectoryLookup *LookupFrom,
1249 bool isImport) {
1250
1251 Token FilenameTok;
Ted Kremenek30cd88c2008-11-18 00:34:22 +00001252 CurPPLexer->LexIncludeFilename(FilenameTok);
Mike Stump11289f42009-09-09 15:08:12 +00001253
Chris Lattnerf64b3522008-03-09 01:54:53 +00001254 // Reserve a buffer to get the spelling.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001255 SmallString<128> FilenameBuffer;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001256 StringRef Filename;
Douglas Gregor796d76a2010-10-20 22:00:55 +00001257 SourceLocation End;
Douglas Gregor41e115a2011-11-30 18:02:36 +00001258 SourceLocation CharEnd; // the end of this directive, in characters
Douglas Gregor796d76a2010-10-20 22:00:55 +00001259
Chris Lattnerf64b3522008-03-09 01:54:53 +00001260 switch (FilenameTok.getKind()) {
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001261 case tok::eod:
1262 // If the token kind is EOD, the error has already been diagnosed.
Chris Lattnerf64b3522008-03-09 01:54:53 +00001263 return;
Mike Stump11289f42009-09-09 15:08:12 +00001264
Chris Lattnerf64b3522008-03-09 01:54:53 +00001265 case tok::angle_string_literal:
Benjamin Kramer0a1abd42010-02-27 13:44:12 +00001266 case tok::string_literal:
1267 Filename = getSpelling(FilenameTok, FilenameBuffer);
Douglas Gregor796d76a2010-10-20 22:00:55 +00001268 End = FilenameTok.getLocation();
Douglas Gregor41e115a2011-11-30 18:02:36 +00001269 CharEnd = End.getLocWithOffset(Filename.size());
Chris Lattnerf64b3522008-03-09 01:54:53 +00001270 break;
Mike Stump11289f42009-09-09 15:08:12 +00001271
Chris Lattnerf64b3522008-03-09 01:54:53 +00001272 case tok::less:
1273 // This could be a <foo/bar.h> file coming from a macro expansion. In this
1274 // case, glue the tokens together into FilenameBuffer and interpret those.
1275 FilenameBuffer.push_back('<');
Douglas Gregor796d76a2010-10-20 22:00:55 +00001276 if (ConcatenateIncludeName(FilenameBuffer, End))
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001277 return; // Found <eod> but no ">"? Diagnostic already emitted.
Chris Lattnerd081f8c2010-01-10 01:35:12 +00001278 Filename = FilenameBuffer.str();
Douglas Gregor41e115a2011-11-30 18:02:36 +00001279 CharEnd = getLocForEndOfToken(End);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001280 break;
1281 default:
1282 Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
1283 DiscardUntilEndOfDirective();
1284 return;
1285 }
Mike Stump11289f42009-09-09 15:08:12 +00001286
Aaron Ballman611306e2012-03-02 22:51:54 +00001287 StringRef OriginalFilename = Filename;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001288 bool isAngled =
Chris Lattnerd081f8c2010-01-10 01:35:12 +00001289 GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001290 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
1291 // error.
Chris Lattnerd081f8c2010-01-10 01:35:12 +00001292 if (Filename.empty()) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001293 DiscardUntilEndOfDirective();
1294 return;
1295 }
Mike Stump11289f42009-09-09 15:08:12 +00001296
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001297 // Verify that there is nothing after the filename, other than EOD. Note that
Chris Lattnerb40289b2009-04-17 23:56:52 +00001298 // we allow macros that expand to nothing after the filename, because this
1299 // falls into the category of "#include pp-tokens new-line" specified in
1300 // C99 6.10.2p4.
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00001301 CheckEndOfDirective(IncludeTok.getIdentifierInfo()->getNameStart(), true);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001302
1303 // Check that we don't have infinite #include recursion.
Chris Lattner907dfe92008-11-18 07:59:24 +00001304 if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1) {
1305 Diag(FilenameTok, diag::err_pp_include_too_deep);
1306 return;
1307 }
Mike Stump11289f42009-09-09 15:08:12 +00001308
John McCall32f5fe12011-09-30 05:12:12 +00001309 // Complain about attempts to #include files in an audit pragma.
1310 if (PragmaARCCFCodeAuditedLoc.isValid()) {
1311 Diag(HashLoc, diag::err_pp_include_in_arc_cf_code_audited);
1312 Diag(PragmaARCCFCodeAuditedLoc, diag::note_pragma_entered_here);
1313
1314 // Immediately leave the pragma.
1315 PragmaARCCFCodeAuditedLoc = SourceLocation();
1316 }
1317
Aaron Ballman611306e2012-03-02 22:51:54 +00001318 if (HeaderInfo.HasIncludeAliasMap()) {
1319 // Map the filename with the brackets still attached. If the name doesn't
1320 // map to anything, fall back on the filename we've already gotten the
1321 // spelling for.
1322 StringRef NewName = HeaderInfo.MapHeaderToIncludeAlias(OriginalFilename);
1323 if (!NewName.empty())
1324 Filename = NewName;
1325 }
1326
Chris Lattnerf64b3522008-03-09 01:54:53 +00001327 // Search include directories.
1328 const DirectoryLookup *CurDir;
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001329 SmallString<1024> SearchPath;
1330 SmallString<1024> RelativePath;
Chandler Carruth3cc331a2011-03-16 18:34:36 +00001331 // We get the raw path only if we have 'Callbacks' to which we later pass
1332 // the path.
Douglas Gregorde3ef502011-11-30 23:21:26 +00001333 Module *SuggestedModule = 0;
Chandler Carruth3cc331a2011-03-16 18:34:36 +00001334 const FileEntry *File = LookupFile(
Manuel Klimek0c69fd22011-04-26 21:50:03 +00001335 Filename, isAngled, LookupFrom, CurDir,
Douglas Gregor97eec242011-09-15 22:00:41 +00001336 Callbacks ? &SearchPath : NULL, Callbacks ? &RelativePath : NULL,
David Blaikiebbafb8a2012-03-11 07:00:24 +00001337 getLangOpts().Modules? &SuggestedModule : 0);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001338
Douglas Gregor11729f02011-11-30 18:12:06 +00001339 if (Callbacks) {
1340 if (!File) {
1341 // Give the clients a chance to recover.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001342 SmallString<128> RecoveryPath;
Douglas Gregor11729f02011-11-30 18:12:06 +00001343 if (Callbacks->FileNotFound(Filename, RecoveryPath)) {
1344 if (const DirectoryEntry *DE = FileMgr.getDirectory(RecoveryPath)) {
1345 // Add the recovery path to the list of search paths.
1346 DirectoryLookup DL(DE, SrcMgr::C_User, true, false);
1347 HeaderInfo.AddSearchPath(DL, isAngled);
1348
1349 // Try the lookup again, skipping the cache.
1350 File = LookupFile(Filename, isAngled, LookupFrom, CurDir, 0, 0,
David Blaikiebbafb8a2012-03-11 07:00:24 +00001351 getLangOpts().Modules? &SuggestedModule : 0,
Douglas Gregor11729f02011-11-30 18:12:06 +00001352 /*SkipCache*/true);
1353 }
1354 }
1355 }
1356
1357 // Notify the callback object that we've seen an inclusion directive.
1358 Callbacks->InclusionDirective(HashLoc, IncludeTok, Filename, isAngled, File,
1359 End, SearchPath, RelativePath);
1360 }
1361
1362 if (File == 0) {
1363 if (!SuppressIncludeNotFoundError)
1364 Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
1365 return;
1366 }
1367
Douglas Gregor97eec242011-09-15 22:00:41 +00001368 // If we are supposed to import a module rather than including the header,
1369 // do so now.
Douglas Gregorc04f6442011-11-17 22:44:56 +00001370 if (SuggestedModule) {
Douglas Gregor71944202011-11-30 00:36:36 +00001371 // Compute the module access path corresponding to this module.
1372 // FIXME: Should we have a second loadModule() overload to avoid this
1373 // extra lookup step?
1374 llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Path;
Douglas Gregorde3ef502011-11-30 23:21:26 +00001375 for (Module *Mod = SuggestedModule; Mod; Mod = Mod->Parent)
Douglas Gregor71944202011-11-30 00:36:36 +00001376 Path.push_back(std::make_pair(getIdentifierInfo(Mod->Name),
1377 FilenameTok.getLocation()));
1378 std::reverse(Path.begin(), Path.end());
1379
Douglas Gregor41e115a2011-11-30 18:02:36 +00001380 // Warn that we're replacing the include/import with a module import.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001381 SmallString<128> PathString;
Douglas Gregor41e115a2011-11-30 18:02:36 +00001382 for (unsigned I = 0, N = Path.size(); I != N; ++I) {
1383 if (I)
1384 PathString += '.';
1385 PathString += Path[I].first->getName();
1386 }
1387 int IncludeKind = 0;
1388
1389 switch (IncludeTok.getIdentifierInfo()->getPPKeywordID()) {
1390 case tok::pp_include:
1391 IncludeKind = 0;
1392 break;
1393
1394 case tok::pp_import:
1395 IncludeKind = 1;
1396 break;
1397
Douglas Gregor4401fbe2011-11-30 18:03:26 +00001398 case tok::pp_include_next:
1399 IncludeKind = 2;
1400 break;
Douglas Gregor41e115a2011-11-30 18:02:36 +00001401
1402 case tok::pp___include_macros:
1403 IncludeKind = 3;
1404 break;
1405
1406 default:
1407 llvm_unreachable("unknown include directive kind");
Douglas Gregor41e115a2011-11-30 18:02:36 +00001408 }
1409
Douglas Gregor2537a362011-12-08 17:01:29 +00001410 // Determine whether we are actually building the module that this
1411 // include directive maps to.
1412 bool BuildingImportedModule
David Blaikiebbafb8a2012-03-11 07:00:24 +00001413 = Path[0].first->getName() == getLangOpts().CurrentModule;
Douglas Gregor2537a362011-12-08 17:01:29 +00001414
David Blaikiebbafb8a2012-03-11 07:00:24 +00001415 if (!BuildingImportedModule && getLangOpts().ObjC2) {
Douglas Gregor2537a362011-12-08 17:01:29 +00001416 // If we're not building the imported module, warn that we're going
1417 // to automatically turn this inclusion directive into a module import.
Douglas Gregorda82e702012-01-03 19:32:59 +00001418 // We only do this in Objective-C, where we have a module-import syntax.
Douglas Gregor2537a362011-12-08 17:01:29 +00001419 CharSourceRange ReplaceRange(SourceRange(HashLoc, CharEnd),
1420 /*IsTokenRange=*/false);
1421 Diag(HashLoc, diag::warn_auto_module_import)
1422 << IncludeKind << PathString
1423 << FixItHint::CreateReplacement(ReplaceRange,
Ted Kremenekc1e4dd02012-03-01 22:07:04 +00001424 "@__experimental_modules_import " + PathString.str().str() + ";");
Douglas Gregor2537a362011-12-08 17:01:29 +00001425 }
Douglas Gregor41e115a2011-11-30 18:02:36 +00001426
Douglas Gregor71944202011-11-30 00:36:36 +00001427 // Load the module.
Douglas Gregorff2be532011-12-01 17:11:21 +00001428 // If this was an #__include_macros directive, only make macros visible.
1429 Module::NameVisibilityKind Visibility
1430 = (IncludeKind == 3)? Module::MacrosVisible : Module::AllVisible;
Douglas Gregor98a52db2011-12-20 00:28:52 +00001431 Module *Imported
1432 = TheModuleLoader.loadModule(IncludeTok.getLocation(), Path, Visibility,
1433 /*IsIncludeDirective=*/true);
Douglas Gregor2537a362011-12-08 17:01:29 +00001434
1435 // If this header isn't part of the module we're building, we're done.
Douglas Gregor98a52db2011-12-20 00:28:52 +00001436 if (!BuildingImportedModule && Imported)
Douglas Gregor2537a362011-12-08 17:01:29 +00001437 return;
Douglas Gregor97eec242011-09-15 22:00:41 +00001438 }
1439
Chris Lattnerc88a23e2008-09-26 20:12:23 +00001440 // The #included file will be considered to be a system header if either it is
1441 // in a system include directory, or if the #includer is a system include
1442 // header.
Mike Stump11289f42009-09-09 15:08:12 +00001443 SrcMgr::CharacteristicKind FileCharacter =
Chris Lattnerb03dc762008-09-26 21:18:42 +00001444 std::max(HeaderInfo.getFileDirFlavor(File),
Chris Lattnerc0334162009-01-19 07:59:15 +00001445 SourceMgr.getFileCharacteristic(FilenameTok.getLocation()));
Mike Stump11289f42009-09-09 15:08:12 +00001446
Chris Lattner72286d62010-04-19 20:44:31 +00001447 // Ask HeaderInfo if we should enter this #include file. If not, #including
1448 // this file will have no effect.
1449 if (!HeaderInfo.ShouldEnterIncludeFile(File, isImport)) {
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001450 if (Callbacks)
Chris Lattner72286d62010-04-19 20:44:31 +00001451 Callbacks->FileSkipped(*File, FilenameTok, FileCharacter);
Chris Lattner72286d62010-04-19 20:44:31 +00001452 return;
1453 }
1454
Chris Lattnerf64b3522008-03-09 01:54:53 +00001455 // Look up the file, create a File ID for it.
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +00001456 SourceLocation IncludePos = End;
1457 // If the filename string was the result of macro expansions, set the include
1458 // position on the file where it will be included and after the expansions.
1459 if (IncludePos.isMacroID())
1460 IncludePos = SourceMgr.getExpansionRange(IncludePos).second;
1461 FileID FID = SourceMgr.createFileID(File, IncludePos, FileCharacter);
Peter Collingbourned395b932011-06-30 16:41:03 +00001462 assert(!FID.isInvalid() && "Expected valid file ID");
Chris Lattnerf64b3522008-03-09 01:54:53 +00001463
1464 // Finally, if all is good, enter the new file!
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001465 EnterSourceFile(FID, CurDir, FilenameTok.getLocation());
Chris Lattnerf64b3522008-03-09 01:54:53 +00001466}
1467
1468/// HandleIncludeNextDirective - Implements #include_next.
1469///
Douglas Gregor796d76a2010-10-20 22:00:55 +00001470void Preprocessor::HandleIncludeNextDirective(SourceLocation HashLoc,
1471 Token &IncludeNextTok) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001472 Diag(IncludeNextTok, diag::ext_pp_include_next_directive);
Mike Stump11289f42009-09-09 15:08:12 +00001473
Chris Lattnerf64b3522008-03-09 01:54:53 +00001474 // #include_next is like #include, except that we start searching after
1475 // the current found directory. If we can't do this, issue a
1476 // diagnostic.
1477 const DirectoryLookup *Lookup = CurDirLookup;
1478 if (isInPrimaryFile()) {
1479 Lookup = 0;
1480 Diag(IncludeNextTok, diag::pp_include_next_in_primary);
1481 } else if (Lookup == 0) {
1482 Diag(IncludeNextTok, diag::pp_include_next_absolute_path);
1483 } else {
1484 // Start looking up in the next directory.
1485 ++Lookup;
1486 }
Mike Stump11289f42009-09-09 15:08:12 +00001487
Douglas Gregor796d76a2010-10-20 22:00:55 +00001488 return HandleIncludeDirective(HashLoc, IncludeNextTok, Lookup);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001489}
1490
Aaron Ballman0467f552012-03-18 03:10:37 +00001491/// HandleMicrosoftImportDirective - Implements #import for Microsoft Mode
1492void Preprocessor::HandleMicrosoftImportDirective(Token &Tok) {
1493 // The Microsoft #import directive takes a type library and generates header
1494 // files from it, and includes those. This is beyond the scope of what clang
1495 // does, so we ignore it and error out. However, #import can optionally have
1496 // trailing attributes that span multiple lines. We're going to eat those
1497 // so we can continue processing from there.
1498 Diag(Tok, diag::err_pp_import_directive_ms );
1499
1500 // Read tokens until we get to the end of the directive. Note that the
1501 // directive can be split over multiple lines using the backslash character.
1502 DiscardUntilEndOfDirective();
1503}
1504
Chris Lattnerf64b3522008-03-09 01:54:53 +00001505/// HandleImportDirective - Implements #import.
1506///
Douglas Gregor796d76a2010-10-20 22:00:55 +00001507void Preprocessor::HandleImportDirective(SourceLocation HashLoc,
1508 Token &ImportTok) {
Aaron Ballman0467f552012-03-18 03:10:37 +00001509 if (!LangOpts.ObjC1) { // #import is standard for ObjC.
1510 if (LangOpts.MicrosoftMode)
1511 return HandleMicrosoftImportDirective(ImportTok);
Chris Lattnerd4a96732009-03-06 04:28:03 +00001512 Diag(ImportTok, diag::ext_pp_import_directive);
Aaron Ballman0467f552012-03-18 03:10:37 +00001513 }
Douglas Gregor796d76a2010-10-20 22:00:55 +00001514 return HandleIncludeDirective(HashLoc, ImportTok, 0, true);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001515}
1516
Chris Lattner58a1eb02009-04-08 18:46:40 +00001517/// HandleIncludeMacrosDirective - The -imacros command line option turns into a
1518/// pseudo directive in the predefines buffer. This handles it by sucking all
1519/// tokens through the preprocessor and discarding them (only keeping the side
1520/// effects on the preprocessor).
Douglas Gregor796d76a2010-10-20 22:00:55 +00001521void Preprocessor::HandleIncludeMacrosDirective(SourceLocation HashLoc,
1522 Token &IncludeMacrosTok) {
Chris Lattner58a1eb02009-04-08 18:46:40 +00001523 // This directive should only occur in the predefines buffer. If not, emit an
1524 // error and reject it.
1525 SourceLocation Loc = IncludeMacrosTok.getLocation();
1526 if (strcmp(SourceMgr.getBufferName(Loc), "<built-in>") != 0) {
1527 Diag(IncludeMacrosTok.getLocation(),
1528 diag::pp_include_macros_out_of_predefines);
1529 DiscardUntilEndOfDirective();
1530 return;
1531 }
Mike Stump11289f42009-09-09 15:08:12 +00001532
Chris Lattnere01d82b2009-04-08 20:53:24 +00001533 // Treat this as a normal #include for checking purposes. If this is
1534 // successful, it will push a new lexer onto the include stack.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001535 HandleIncludeDirective(HashLoc, IncludeMacrosTok, 0, false);
Mike Stump11289f42009-09-09 15:08:12 +00001536
Chris Lattnere01d82b2009-04-08 20:53:24 +00001537 Token TmpTok;
1538 do {
1539 Lex(TmpTok);
1540 assert(TmpTok.isNot(tok::eof) && "Didn't find end of -imacros!");
1541 } while (TmpTok.isNot(tok::hashhash));
Chris Lattner58a1eb02009-04-08 18:46:40 +00001542}
1543
Chris Lattnerf64b3522008-03-09 01:54:53 +00001544//===----------------------------------------------------------------------===//
1545// Preprocessor Macro Directive Handling.
1546//===----------------------------------------------------------------------===//
1547
1548/// ReadMacroDefinitionArgList - The ( starting an argument list of a macro
1549/// definition has just been read. Lex the rest of the arguments and the
1550/// closing ), updating MI with what we learn. Return true if an error occurs
1551/// parsing the arg list.
Abramo Bagnarac9e48c02012-03-31 20:17:27 +00001552bool Preprocessor::ReadMacroDefinitionArgList(MacroInfo *MI, Token &Tok) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001553 SmallVector<IdentifierInfo*, 32> Arguments;
Mike Stump11289f42009-09-09 15:08:12 +00001554
Chris Lattnerf64b3522008-03-09 01:54:53 +00001555 while (1) {
1556 LexUnexpandedToken(Tok);
1557 switch (Tok.getKind()) {
1558 case tok::r_paren:
1559 // Found the end of the argument list.
Chris Lattnerf87c5102009-02-20 22:31:31 +00001560 if (Arguments.empty()) // #define FOO()
Chris Lattnerf64b3522008-03-09 01:54:53 +00001561 return false;
Chris Lattnerf64b3522008-03-09 01:54:53 +00001562 // Otherwise we have #define FOO(A,)
1563 Diag(Tok, diag::err_pp_expected_ident_in_arg_list);
1564 return true;
1565 case tok::ellipsis: // #define X(... -> C99 varargs
David Blaikiebbafb8a2012-03-11 07:00:24 +00001566 if (!LangOpts.C99)
1567 Diag(Tok, LangOpts.CPlusPlus0x ?
Richard Smithacd4d3d2011-10-15 01:18:56 +00001568 diag::warn_cxx98_compat_variadic_macro :
1569 diag::ext_variadic_macro);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001570
1571 // Lex the token after the identifier.
1572 LexUnexpandedToken(Tok);
1573 if (Tok.isNot(tok::r_paren)) {
1574 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
1575 return true;
1576 }
1577 // Add the __VA_ARGS__ identifier as an argument.
1578 Arguments.push_back(Ident__VA_ARGS__);
1579 MI->setIsC99Varargs();
Chris Lattner70946da2009-02-20 22:46:43 +00001580 MI->setArgumentList(&Arguments[0], Arguments.size(), BP);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001581 return false;
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001582 case tok::eod: // #define X(
Chris Lattnerf64b3522008-03-09 01:54:53 +00001583 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
1584 return true;
1585 default:
1586 // Handle keywords and identifiers here to accept things like
1587 // #define Foo(for) for.
1588 IdentifierInfo *II = Tok.getIdentifierInfo();
1589 if (II == 0) {
1590 // #define X(1
1591 Diag(Tok, diag::err_pp_invalid_tok_in_arg_list);
1592 return true;
1593 }
1594
1595 // If this is already used as an argument, it is used multiple times (e.g.
1596 // #define X(A,A.
Mike Stump11289f42009-09-09 15:08:12 +00001597 if (std::find(Arguments.begin(), Arguments.end(), II) !=
Chris Lattnerf64b3522008-03-09 01:54:53 +00001598 Arguments.end()) { // C99 6.10.3p6
Chris Lattnerc5cdade2008-11-19 07:33:58 +00001599 Diag(Tok, diag::err_pp_duplicate_name_in_arg_list) << II;
Chris Lattnerf64b3522008-03-09 01:54:53 +00001600 return true;
1601 }
Mike Stump11289f42009-09-09 15:08:12 +00001602
Chris Lattnerf64b3522008-03-09 01:54:53 +00001603 // Add the argument to the macro info.
1604 Arguments.push_back(II);
Mike Stump11289f42009-09-09 15:08:12 +00001605
Chris Lattnerf64b3522008-03-09 01:54:53 +00001606 // Lex the token after the identifier.
1607 LexUnexpandedToken(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001608
Chris Lattnerf64b3522008-03-09 01:54:53 +00001609 switch (Tok.getKind()) {
1610 default: // #define X(A B
1611 Diag(Tok, diag::err_pp_expected_comma_in_arg_list);
1612 return true;
1613 case tok::r_paren: // #define X(A)
Chris Lattner70946da2009-02-20 22:46:43 +00001614 MI->setArgumentList(&Arguments[0], Arguments.size(), BP);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001615 return false;
1616 case tok::comma: // #define X(A,
1617 break;
1618 case tok::ellipsis: // #define X(A... -> GCC extension
1619 // Diagnose extension.
1620 Diag(Tok, diag::ext_named_variadic_macro);
Mike Stump11289f42009-09-09 15:08:12 +00001621
Chris Lattnerf64b3522008-03-09 01:54:53 +00001622 // Lex the token after the identifier.
1623 LexUnexpandedToken(Tok);
1624 if (Tok.isNot(tok::r_paren)) {
1625 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
1626 return true;
1627 }
Mike Stump11289f42009-09-09 15:08:12 +00001628
Chris Lattnerf64b3522008-03-09 01:54:53 +00001629 MI->setIsGNUVarargs();
Chris Lattner70946da2009-02-20 22:46:43 +00001630 MI->setArgumentList(&Arguments[0], Arguments.size(), BP);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001631 return false;
1632 }
1633 }
1634 }
1635}
1636
1637/// HandleDefineDirective - Implements #define. This consumes the entire macro
1638/// line then lets the caller lex the next real token.
1639void Preprocessor::HandleDefineDirective(Token &DefineTok) {
1640 ++NumDefined;
1641
1642 Token MacroNameTok;
1643 ReadMacroName(MacroNameTok, 1);
Mike Stump11289f42009-09-09 15:08:12 +00001644
Chris Lattnerf64b3522008-03-09 01:54:53 +00001645 // Error reading macro name? If so, diagnostic already issued.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001646 if (MacroNameTok.is(tok::eod))
Chris Lattnerf64b3522008-03-09 01:54:53 +00001647 return;
1648
Chris Lattnerd6e97af2009-04-21 04:46:33 +00001649 Token LastTok = MacroNameTok;
1650
Chris Lattnerf64b3522008-03-09 01:54:53 +00001651 // If we are supposed to keep comments in #defines, reenable comment saving
1652 // mode.
Ted Kremenek59e003e2008-11-18 00:43:07 +00001653 if (CurLexer) CurLexer->SetCommentRetentionState(KeepMacroComments);
Mike Stump11289f42009-09-09 15:08:12 +00001654
Chris Lattnerf64b3522008-03-09 01:54:53 +00001655 // Create the new macro.
Ted Kremenek6c7ea112008-12-15 19:56:42 +00001656 MacroInfo *MI = AllocateMacroInfo(MacroNameTok.getLocation());
Mike Stump11289f42009-09-09 15:08:12 +00001657
Chris Lattnerf64b3522008-03-09 01:54:53 +00001658 Token Tok;
1659 LexUnexpandedToken(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001660
Chris Lattnerf64b3522008-03-09 01:54:53 +00001661 // If this is a function-like macro definition, parse the argument list,
1662 // marking each of the identifiers as being used as macro arguments. Also,
1663 // check other constraints on the first token of the macro body.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001664 if (Tok.is(tok::eod)) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001665 // If there is no body to this macro, we have no special handling here.
Chris Lattner2425bcb2009-04-18 02:23:25 +00001666 } else if (Tok.hasLeadingSpace()) {
1667 // This is a normal token with leading space. Clear the leading space
1668 // marker on the first token to get proper expansion.
1669 Tok.clearFlag(Token::LeadingSpace);
1670 } else if (Tok.is(tok::l_paren)) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001671 // This is a function-like macro definition. Read the argument list.
1672 MI->setIsFunctionLike();
Abramo Bagnarac9e48c02012-03-31 20:17:27 +00001673 if (ReadMacroDefinitionArgList(MI, LastTok)) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001674 // Forget about MI.
Ted Kremenek6c7ea112008-12-15 19:56:42 +00001675 ReleaseMacroInfo(MI);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001676 // Throw away the rest of the line.
Ted Kremenek30cd88c2008-11-18 00:34:22 +00001677 if (CurPPLexer->ParsingPreprocessorDirective)
Chris Lattnerf64b3522008-03-09 01:54:53 +00001678 DiscardUntilEndOfDirective();
1679 return;
1680 }
1681
Chris Lattner249c38b2009-04-19 18:26:34 +00001682 // If this is a definition of a variadic C99 function-like macro, not using
1683 // the GNU named varargs extension, enabled __VA_ARGS__.
Mike Stump11289f42009-09-09 15:08:12 +00001684
Chris Lattner249c38b2009-04-19 18:26:34 +00001685 // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro.
1686 // This gets unpoisoned where it is allowed.
1687 assert(Ident__VA_ARGS__->isPoisoned() && "__VA_ARGS__ should be poisoned!");
1688 if (MI->isC99Varargs())
1689 Ident__VA_ARGS__->setIsPoisoned(false);
Mike Stump11289f42009-09-09 15:08:12 +00001690
Chris Lattnerf64b3522008-03-09 01:54:53 +00001691 // Read the first token after the arg list for down below.
1692 LexUnexpandedToken(Tok);
David Blaikiebbafb8a2012-03-11 07:00:24 +00001693 } else if (LangOpts.C99 || LangOpts.CPlusPlus0x) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001694 // C99 requires whitespace between the macro definition and the body. Emit
1695 // a diagnostic for something like "#define X+".
Chris Lattner2425bcb2009-04-18 02:23:25 +00001696 Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001697 } else {
Chris Lattner2425bcb2009-04-18 02:23:25 +00001698 // C90 6.8 TC1 says: "In the definition of an object-like macro, if the
1699 // first character of a replacement list is not a character required by
1700 // subclause 5.2.1, then there shall be white-space separation between the
1701 // identifier and the replacement list.". 5.2.1 lists this set:
1702 // "A-Za-z0-9!"#%&'()*+,_./:;<=>?[\]^_{|}~" as well as whitespace, which
1703 // is irrelevant here.
1704 bool isInvalid = false;
1705 if (Tok.is(tok::at)) // @ is not in the list above.
1706 isInvalid = true;
1707 else if (Tok.is(tok::unknown)) {
1708 // If we have an unknown token, it is something strange like "`". Since
1709 // all of valid characters would have lexed into a single character
1710 // token of some sort, we know this is not a valid case.
1711 isInvalid = true;
1712 }
1713 if (isInvalid)
1714 Diag(Tok, diag::ext_missing_whitespace_after_macro_name);
1715 else
1716 Diag(Tok, diag::warn_missing_whitespace_after_macro_name);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001717 }
Chris Lattnerd6e97af2009-04-21 04:46:33 +00001718
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001719 if (!Tok.is(tok::eod))
Chris Lattnerd6e97af2009-04-21 04:46:33 +00001720 LastTok = Tok;
1721
Chris Lattnerf64b3522008-03-09 01:54:53 +00001722 // Read the rest of the macro body.
1723 if (MI->isObjectLike()) {
1724 // Object-like macros are very simple, just read their body.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001725 while (Tok.isNot(tok::eod)) {
Chris Lattnerd6e97af2009-04-21 04:46:33 +00001726 LastTok = Tok;
Chris Lattnerf64b3522008-03-09 01:54:53 +00001727 MI->AddTokenToBody(Tok);
1728 // Get the next token of the macro.
1729 LexUnexpandedToken(Tok);
1730 }
Mike Stump11289f42009-09-09 15:08:12 +00001731
Chris Lattnerf64b3522008-03-09 01:54:53 +00001732 } else {
Chris Lattner83bd8282009-05-25 17:16:10 +00001733 // Otherwise, read the body of a function-like macro. While we are at it,
1734 // check C99 6.10.3.2p1: ensure that # operators are followed by macro
1735 // parameters in function-like macro expansions.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001736 while (Tok.isNot(tok::eod)) {
Chris Lattnerd6e97af2009-04-21 04:46:33 +00001737 LastTok = Tok;
Chris Lattnerf64b3522008-03-09 01:54:53 +00001738
Chris Lattnerf64b3522008-03-09 01:54:53 +00001739 if (Tok.isNot(tok::hash)) {
Chris Lattner83bd8282009-05-25 17:16:10 +00001740 MI->AddTokenToBody(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001741
Chris Lattnerf64b3522008-03-09 01:54:53 +00001742 // Get the next token of the macro.
1743 LexUnexpandedToken(Tok);
1744 continue;
1745 }
Mike Stump11289f42009-09-09 15:08:12 +00001746
Chris Lattnerf64b3522008-03-09 01:54:53 +00001747 // Get the next token of the macro.
1748 LexUnexpandedToken(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001749
Chris Lattner83bd8282009-05-25 17:16:10 +00001750 // Check for a valid macro arg identifier.
1751 if (Tok.getIdentifierInfo() == 0 ||
1752 MI->getArgumentNum(Tok.getIdentifierInfo()) == -1) {
1753
1754 // If this is assembler-with-cpp mode, we accept random gibberish after
1755 // the '#' because '#' is often a comment character. However, change
1756 // the kind of the token to tok::unknown so that the preprocessor isn't
1757 // confused.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001758 if (getLangOpts().AsmPreprocessor && Tok.isNot(tok::eod)) {
Chris Lattner83bd8282009-05-25 17:16:10 +00001759 LastTok.setKind(tok::unknown);
1760 } else {
1761 Diag(Tok, diag::err_pp_stringize_not_parameter);
1762 ReleaseMacroInfo(MI);
Mike Stump11289f42009-09-09 15:08:12 +00001763
Chris Lattner83bd8282009-05-25 17:16:10 +00001764 // Disable __VA_ARGS__ again.
1765 Ident__VA_ARGS__->setIsPoisoned(true);
1766 return;
1767 }
Chris Lattnerf64b3522008-03-09 01:54:53 +00001768 }
Mike Stump11289f42009-09-09 15:08:12 +00001769
Chris Lattner83bd8282009-05-25 17:16:10 +00001770 // Things look ok, add the '#' and param name tokens to the macro.
1771 MI->AddTokenToBody(LastTok);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001772 MI->AddTokenToBody(Tok);
Chris Lattner83bd8282009-05-25 17:16:10 +00001773 LastTok = Tok;
Mike Stump11289f42009-09-09 15:08:12 +00001774
Chris Lattnerf64b3522008-03-09 01:54:53 +00001775 // Get the next token of the macro.
1776 LexUnexpandedToken(Tok);
1777 }
1778 }
Mike Stump11289f42009-09-09 15:08:12 +00001779
1780
Chris Lattnerf64b3522008-03-09 01:54:53 +00001781 // Disable __VA_ARGS__ again.
1782 Ident__VA_ARGS__->setIsPoisoned(true);
1783
Chris Lattner57540c52011-04-15 05:22:18 +00001784 // Check that there is no paste (##) operator at the beginning or end of the
Chris Lattnerf64b3522008-03-09 01:54:53 +00001785 // replacement list.
1786 unsigned NumTokens = MI->getNumTokens();
1787 if (NumTokens != 0) {
1788 if (MI->getReplacementToken(0).is(tok::hashhash)) {
1789 Diag(MI->getReplacementToken(0), diag::err_paste_at_start);
Ted Kremenek6c7ea112008-12-15 19:56:42 +00001790 ReleaseMacroInfo(MI);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001791 return;
1792 }
1793 if (MI->getReplacementToken(NumTokens-1).is(tok::hashhash)) {
1794 Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end);
Ted Kremenek6c7ea112008-12-15 19:56:42 +00001795 ReleaseMacroInfo(MI);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001796 return;
1797 }
1798 }
Mike Stump11289f42009-09-09 15:08:12 +00001799
Chris Lattnerd6e97af2009-04-21 04:46:33 +00001800 MI->setDefinitionEndLoc(LastTok.getLocation());
Mike Stump11289f42009-09-09 15:08:12 +00001801
Chris Lattnerf64b3522008-03-09 01:54:53 +00001802 // Finally, if this identifier already had a macro defined for it, verify that
1803 // the macro bodies are identical and free the old definition.
1804 if (MacroInfo *OtherMI = getMacroInfo(MacroNameTok.getIdentifierInfo())) {
Chris Lattner5244f342009-01-16 19:50:11 +00001805 // It is very common for system headers to have tons of macro redefinitions
1806 // and for warnings to be disabled in system headers. If this is the case,
1807 // then don't bother calling MacroInfo::isIdenticalTo.
Chris Lattner80c21df2009-03-13 21:17:23 +00001808 if (!getDiagnostics().getSuppressSystemWarnings() ||
Chris Lattner5244f342009-01-16 19:50:11 +00001809 !SourceMgr.isInSystemHeader(DefineTok.getLocation())) {
Argyrios Kyrtzidisb495cc12011-01-18 19:50:15 +00001810 if (!OtherMI->isUsed() && OtherMI->isWarnIfUnused())
Chris Lattner5244f342009-01-16 19:50:11 +00001811 Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001812
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001813 // Macros must be identical. This means all tokens and whitespace
Chris Lattner5244f342009-01-16 19:50:11 +00001814 // separation must be the same. C99 6.10.3.2.
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001815 if (!OtherMI->isAllowRedefinitionsWithoutWarning() &&
Eli Friedman04831922010-08-22 01:00:03 +00001816 !MI->isIdenticalTo(*OtherMI, *this)) {
Chris Lattner5244f342009-01-16 19:50:11 +00001817 Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef)
1818 << MacroNameTok.getIdentifierInfo();
1819 Diag(OtherMI->getDefinitionLoc(), diag::note_previous_definition);
1820 }
Chris Lattnerf64b3522008-03-09 01:54:53 +00001821 }
Argyrios Kyrtzidisb495cc12011-01-18 19:50:15 +00001822 if (OtherMI->isWarnIfUnused())
1823 WarnUnusedMacroLocs.erase(OtherMI->getDefinitionLoc());
Ted Kremenek6c7ea112008-12-15 19:56:42 +00001824 ReleaseMacroInfo(OtherMI);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001825 }
Mike Stump11289f42009-09-09 15:08:12 +00001826
Chris Lattnerf64b3522008-03-09 01:54:53 +00001827 setMacroInfo(MacroNameTok.getIdentifierInfo(), MI);
Mike Stump11289f42009-09-09 15:08:12 +00001828
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +00001829 assert(!MI->isUsed());
1830 // If we need warning for not using the macro, add its location in the
1831 // warn-because-unused-macro set. If it gets used it will be removed from set.
1832 if (isInPrimaryFile() && // don't warn for include'd macros.
1833 Diags->getDiagnosticLevel(diag::pp_macro_not_used,
David Blaikie9c902b52011-09-25 23:23:43 +00001834 MI->getDefinitionLoc()) != DiagnosticsEngine::Ignored) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +00001835 MI->setIsWarnIfUnused(true);
1836 WarnUnusedMacroLocs.insert(MI->getDefinitionLoc());
1837 }
1838
Chris Lattner928e9092009-04-12 01:39:54 +00001839 // If the callbacks want to know, tell them about the macro definition.
1840 if (Callbacks)
Craig Silverstein1a9ca212010-11-19 21:33:15 +00001841 Callbacks->MacroDefined(MacroNameTok, MI);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001842}
1843
1844/// HandleUndefDirective - Implements #undef.
1845///
1846void Preprocessor::HandleUndefDirective(Token &UndefTok) {
1847 ++NumUndefined;
1848
1849 Token MacroNameTok;
1850 ReadMacroName(MacroNameTok, 2);
Mike Stump11289f42009-09-09 15:08:12 +00001851
Chris Lattnerf64b3522008-03-09 01:54:53 +00001852 // Error reading macro name? If so, diagnostic already issued.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001853 if (MacroNameTok.is(tok::eod))
Chris Lattnerf64b3522008-03-09 01:54:53 +00001854 return;
Mike Stump11289f42009-09-09 15:08:12 +00001855
Chris Lattnerf64b3522008-03-09 01:54:53 +00001856 // Check to see if this is the last token on the #undef line.
Chris Lattnerce2ab6f2009-04-14 05:07:49 +00001857 CheckEndOfDirective("undef");
Mike Stump11289f42009-09-09 15:08:12 +00001858
Chris Lattnerf64b3522008-03-09 01:54:53 +00001859 // Okay, we finally have a valid identifier to undef.
1860 MacroInfo *MI = getMacroInfo(MacroNameTok.getIdentifierInfo());
Mike Stump11289f42009-09-09 15:08:12 +00001861
Chris Lattnerf64b3522008-03-09 01:54:53 +00001862 // If the macro is not defined, this is a noop undef, just return.
1863 if (MI == 0) return;
1864
Argyrios Kyrtzidis22998892011-07-11 20:39:47 +00001865 if (!MI->isUsed() && MI->isWarnIfUnused())
Chris Lattnerf64b3522008-03-09 01:54:53 +00001866 Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used);
Chris Lattnercd6d4b12009-04-21 03:42:09 +00001867
1868 // If the callbacks want to know, tell them about the macro #undef.
1869 if (Callbacks)
Craig Silverstein1a9ca212010-11-19 21:33:15 +00001870 Callbacks->MacroUndefined(MacroNameTok, MI);
Chris Lattnercd6d4b12009-04-21 03:42:09 +00001871
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +00001872 if (MI->isWarnIfUnused())
1873 WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());
1874
Chris Lattnerf64b3522008-03-09 01:54:53 +00001875 // Free macro definition.
Ted Kremenek6c7ea112008-12-15 19:56:42 +00001876 ReleaseMacroInfo(MI);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001877 setMacroInfo(MacroNameTok.getIdentifierInfo(), 0);
1878}
1879
1880
1881//===----------------------------------------------------------------------===//
1882// Preprocessor Conditional Directive Handling.
1883//===----------------------------------------------------------------------===//
1884
1885/// HandleIfdefDirective - Implements the #ifdef/#ifndef directive. isIfndef is
1886/// true when this is a #ifndef directive. ReadAnyTokensBeforeDirective is true
1887/// if any tokens have been returned or pp-directives activated before this
1888/// #ifndef has been lexed.
1889///
1890void Preprocessor::HandleIfdefDirective(Token &Result, bool isIfndef,
1891 bool ReadAnyTokensBeforeDirective) {
1892 ++NumIf;
1893 Token DirectiveTok = Result;
1894
1895 Token MacroNameTok;
1896 ReadMacroName(MacroNameTok);
Mike Stump11289f42009-09-09 15:08:12 +00001897
Chris Lattnerf64b3522008-03-09 01:54:53 +00001898 // Error reading macro name? If so, diagnostic already issued.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001899 if (MacroNameTok.is(tok::eod)) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001900 // Skip code until we get to #endif. This helps with recovery by not
1901 // emitting an error when the #endif is reached.
1902 SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
1903 /*Foundnonskip*/false, /*FoundElse*/false);
1904 return;
1905 }
Mike Stump11289f42009-09-09 15:08:12 +00001906
Chris Lattnerf64b3522008-03-09 01:54:53 +00001907 // Check to see if this is the last token on the #if[n]def line.
Chris Lattnerce2ab6f2009-04-14 05:07:49 +00001908 CheckEndOfDirective(isIfndef ? "ifndef" : "ifdef");
Chris Lattnerf64b3522008-03-09 01:54:53 +00001909
Chris Lattneraa1cccbb2010-02-12 08:03:27 +00001910 IdentifierInfo *MII = MacroNameTok.getIdentifierInfo();
1911 MacroInfo *MI = getMacroInfo(MII);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001912
Ted Kremenek30cd88c2008-11-18 00:34:22 +00001913 if (CurPPLexer->getConditionalStackDepth() == 0) {
Chris Lattneraa1cccbb2010-02-12 08:03:27 +00001914 // If the start of a top-level #ifdef and if the macro is not defined,
1915 // inform MIOpt that this might be the start of a proper include guard.
1916 // Otherwise it is some other form of unknown conditional which we can't
1917 // handle.
1918 if (!ReadAnyTokensBeforeDirective && MI == 0) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001919 assert(isIfndef && "#ifdef shouldn't reach here");
Chris Lattneraa1cccbb2010-02-12 08:03:27 +00001920 CurPPLexer->MIOpt.EnterTopLevelIFNDEF(MII);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001921 } else
Ted Kremenek30cd88c2008-11-18 00:34:22 +00001922 CurPPLexer->MIOpt.EnterTopLevelConditional();
Chris Lattnerf64b3522008-03-09 01:54:53 +00001923 }
1924
Chris Lattnerf64b3522008-03-09 01:54:53 +00001925 // If there is a macro, process it.
1926 if (MI) // Mark it used.
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +00001927 markMacroAsUsed(MI);
Mike Stump11289f42009-09-09 15:08:12 +00001928
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +00001929 if (Callbacks) {
1930 if (isIfndef)
1931 Callbacks->Ifndef(DirectiveTok.getLocation(), MacroNameTok);
1932 else
1933 Callbacks->Ifdef(DirectiveTok.getLocation(), MacroNameTok);
1934 }
1935
Chris Lattnerf64b3522008-03-09 01:54:53 +00001936 // Should we include the stuff contained by this directive?
1937 if (!MI == isIfndef) {
1938 // Yes, remember that we are inside a conditional, then lex the next token.
Chris Lattner8cf1f932009-12-14 04:54:40 +00001939 CurPPLexer->pushConditionalLevel(DirectiveTok.getLocation(),
1940 /*wasskip*/false, /*foundnonskip*/true,
1941 /*foundelse*/false);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001942 } else {
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00001943 // No, skip the contents of this block.
Chris Lattnerf64b3522008-03-09 01:54:53 +00001944 SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
Mike Stump11289f42009-09-09 15:08:12 +00001945 /*Foundnonskip*/false,
Chris Lattnerf64b3522008-03-09 01:54:53 +00001946 /*FoundElse*/false);
1947 }
1948}
1949
1950/// HandleIfDirective - Implements the #if directive.
1951///
1952void Preprocessor::HandleIfDirective(Token &IfToken,
1953 bool ReadAnyTokensBeforeDirective) {
1954 ++NumIf;
Mike Stump11289f42009-09-09 15:08:12 +00001955
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00001956 // Parse and evaluate the conditional expression.
Chris Lattnerf64b3522008-03-09 01:54:53 +00001957 IdentifierInfo *IfNDefMacro = 0;
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00001958 const SourceLocation ConditionalBegin = CurPPLexer->getSourceLocation();
1959 const bool ConditionalTrue = EvaluateDirectiveExpression(IfNDefMacro);
1960 const SourceLocation ConditionalEnd = CurPPLexer->getSourceLocation();
Nuno Lopes363212b2008-06-01 18:31:24 +00001961
1962 // If this condition is equivalent to #ifndef X, and if this is the first
1963 // directive seen, handle it for the multiple-include optimization.
Ted Kremenek30cd88c2008-11-18 00:34:22 +00001964 if (CurPPLexer->getConditionalStackDepth() == 0) {
Chris Lattneraa1cccbb2010-02-12 08:03:27 +00001965 if (!ReadAnyTokensBeforeDirective && IfNDefMacro && ConditionalTrue)
Ted Kremenek30cd88c2008-11-18 00:34:22 +00001966 CurPPLexer->MIOpt.EnterTopLevelIFNDEF(IfNDefMacro);
Nuno Lopes363212b2008-06-01 18:31:24 +00001967 else
Ted Kremenek30cd88c2008-11-18 00:34:22 +00001968 CurPPLexer->MIOpt.EnterTopLevelConditional();
Nuno Lopes363212b2008-06-01 18:31:24 +00001969 }
1970
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +00001971 if (Callbacks)
1972 Callbacks->If(IfToken.getLocation(),
1973 SourceRange(ConditionalBegin, ConditionalEnd));
1974
Chris Lattnerf64b3522008-03-09 01:54:53 +00001975 // Should we include the stuff contained by this directive?
1976 if (ConditionalTrue) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001977 // Yes, remember that we are inside a conditional, then lex the next token.
Ted Kremenek30cd88c2008-11-18 00:34:22 +00001978 CurPPLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
Chris Lattnerf64b3522008-03-09 01:54:53 +00001979 /*foundnonskip*/true, /*foundelse*/false);
1980 } else {
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00001981 // No, skip the contents of this block.
Mike Stump11289f42009-09-09 15:08:12 +00001982 SkipExcludedConditionalBlock(IfToken.getLocation(), /*Foundnonskip*/false,
Chris Lattnerf64b3522008-03-09 01:54:53 +00001983 /*FoundElse*/false);
1984 }
1985}
1986
1987/// HandleEndifDirective - Implements the #endif directive.
1988///
1989void Preprocessor::HandleEndifDirective(Token &EndifToken) {
1990 ++NumEndif;
Mike Stump11289f42009-09-09 15:08:12 +00001991
Chris Lattnerf64b3522008-03-09 01:54:53 +00001992 // Check that this is the whole directive.
Chris Lattnerce2ab6f2009-04-14 05:07:49 +00001993 CheckEndOfDirective("endif");
Mike Stump11289f42009-09-09 15:08:12 +00001994
Chris Lattnerf64b3522008-03-09 01:54:53 +00001995 PPConditionalInfo CondInfo;
Ted Kremenek30cd88c2008-11-18 00:34:22 +00001996 if (CurPPLexer->popConditionalLevel(CondInfo)) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001997 // No conditionals on the stack: this is an #endif without an #if.
Chris Lattner907dfe92008-11-18 07:59:24 +00001998 Diag(EndifToken, diag::err_pp_endif_without_if);
1999 return;
Chris Lattnerf64b3522008-03-09 01:54:53 +00002000 }
Mike Stump11289f42009-09-09 15:08:12 +00002001
Chris Lattnerf64b3522008-03-09 01:54:53 +00002002 // If this the end of a top-level #endif, inform MIOpt.
Ted Kremenek30cd88c2008-11-18 00:34:22 +00002003 if (CurPPLexer->getConditionalStackDepth() == 0)
2004 CurPPLexer->MIOpt.ExitTopLevelConditional();
Mike Stump11289f42009-09-09 15:08:12 +00002005
Ted Kremenek30cd88c2008-11-18 00:34:22 +00002006 assert(!CondInfo.WasSkipping && !CurPPLexer->LexingRawMode &&
Chris Lattnerf64b3522008-03-09 01:54:53 +00002007 "This code should only be reachable in the non-skipping case!");
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00002008
2009 if (Callbacks)
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +00002010 Callbacks->Endif(EndifToken.getLocation(), CondInfo.IfLoc);
Chris Lattnerf64b3522008-03-09 01:54:53 +00002011}
2012
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00002013/// HandleElseDirective - Implements the #else directive.
2014///
Chris Lattnerf64b3522008-03-09 01:54:53 +00002015void Preprocessor::HandleElseDirective(Token &Result) {
2016 ++NumElse;
Mike Stump11289f42009-09-09 15:08:12 +00002017
Chris Lattnerf64b3522008-03-09 01:54:53 +00002018 // #else directive in a non-skipping conditional... start skipping.
Chris Lattnerce2ab6f2009-04-14 05:07:49 +00002019 CheckEndOfDirective("else");
Mike Stump11289f42009-09-09 15:08:12 +00002020
Chris Lattnerf64b3522008-03-09 01:54:53 +00002021 PPConditionalInfo CI;
Chris Lattner907dfe92008-11-18 07:59:24 +00002022 if (CurPPLexer->popConditionalLevel(CI)) {
2023 Diag(Result, diag::pp_err_else_without_if);
2024 return;
2025 }
Mike Stump11289f42009-09-09 15:08:12 +00002026
Chris Lattnerf64b3522008-03-09 01:54:53 +00002027 // If this is a top-level #else, inform the MIOpt.
Ted Kremenek30cd88c2008-11-18 00:34:22 +00002028 if (CurPPLexer->getConditionalStackDepth() == 0)
2029 CurPPLexer->MIOpt.EnterTopLevelConditional();
Chris Lattnerf64b3522008-03-09 01:54:53 +00002030
2031 // If this is a #else with a #else before it, report the error.
2032 if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else);
Mike Stump11289f42009-09-09 15:08:12 +00002033
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +00002034 if (Callbacks)
2035 Callbacks->Else(Result.getLocation(), CI.IfLoc);
2036
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00002037 // Finally, skip the rest of the contents of this block.
2038 SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
Argyrios Kyrtzidis18bcfd52011-09-27 17:32:05 +00002039 /*FoundElse*/true, Result.getLocation());
Chris Lattnerf64b3522008-03-09 01:54:53 +00002040}
2041
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00002042/// HandleElifDirective - Implements the #elif directive.
2043///
Chris Lattnerf64b3522008-03-09 01:54:53 +00002044void Preprocessor::HandleElifDirective(Token &ElifToken) {
2045 ++NumElse;
Mike Stump11289f42009-09-09 15:08:12 +00002046
Chris Lattnerf64b3522008-03-09 01:54:53 +00002047 // #elif directive in a non-skipping conditional... start skipping.
2048 // We don't care what the condition is, because we will always skip it (since
2049 // the block immediately before it was included).
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00002050 const SourceLocation ConditionalBegin = CurPPLexer->getSourceLocation();
Chris Lattnerf64b3522008-03-09 01:54:53 +00002051 DiscardUntilEndOfDirective();
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00002052 const SourceLocation ConditionalEnd = CurPPLexer->getSourceLocation();
Chris Lattnerf64b3522008-03-09 01:54:53 +00002053
2054 PPConditionalInfo CI;
Chris Lattner907dfe92008-11-18 07:59:24 +00002055 if (CurPPLexer->popConditionalLevel(CI)) {
2056 Diag(ElifToken, diag::pp_err_elif_without_if);
2057 return;
2058 }
Mike Stump11289f42009-09-09 15:08:12 +00002059
Chris Lattnerf64b3522008-03-09 01:54:53 +00002060 // If this is a top-level #elif, inform the MIOpt.
Ted Kremenek30cd88c2008-11-18 00:34:22 +00002061 if (CurPPLexer->getConditionalStackDepth() == 0)
2062 CurPPLexer->MIOpt.EnterTopLevelConditional();
Mike Stump11289f42009-09-09 15:08:12 +00002063
Chris Lattnerf64b3522008-03-09 01:54:53 +00002064 // If this is a #elif with a #else before it, report the error.
2065 if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else);
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +00002066
2067 if (Callbacks)
2068 Callbacks->Elif(ElifToken.getLocation(),
2069 SourceRange(ConditionalBegin, ConditionalEnd), CI.IfLoc);
Chris Lattnerf64b3522008-03-09 01:54:53 +00002070
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00002071 // Finally, skip the rest of the contents of this block.
2072 SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
Argyrios Kyrtzidis18bcfd52011-09-27 17:32:05 +00002073 /*FoundElse*/CI.FoundElse,
2074 ElifToken.getLocation());
Chris Lattnerf64b3522008-03-09 01:54:53 +00002075}