blob: e000c54768552a98e1c99e818d40fed9a865061e [file] [log] [blame]
Chris Lattnera3b605e2008-03-09 03:13:06 +00001//===--- PPDirectives.cpp - Directive Handling for Preprocessor -----------===//
Chris Lattner141e71f2008-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 Lattner359cc442009-01-26 05:29:08 +000015#include "clang/Lex/LiteralSupport.h"
Chris Lattner141e71f2008-03-09 01:54:53 +000016#include "clang/Lex/HeaderSearch.h"
17#include "clang/Lex/MacroInfo.h"
Chris Lattner500d3292009-01-29 05:15:15 +000018#include "clang/Lex/LexDiagnostic.h"
Chris Lattner6e290142009-11-30 04:18:44 +000019#include "clang/Basic/FileManager.h"
Chris Lattner141e71f2008-03-09 01:54:53 +000020#include "clang/Basic/SourceManager.h"
Chris Lattner359cc442009-01-26 05:29:08 +000021#include "llvm/ADT/APInt.h"
Chris Lattner141e71f2008-03-09 01:54:53 +000022using namespace clang;
23
24//===----------------------------------------------------------------------===//
25// Utility Methods for Preprocessor Directive Handling.
26//===----------------------------------------------------------------------===//
27
Chris Lattnerf47724b2010-08-17 15:55:45 +000028MacroInfo *Preprocessor::AllocateMacroInfo() {
Ted Kremenek0ea76722008-12-15 19:56:42 +000029 MacroInfo *MI;
Mike Stump1eb44332009-09-09 15:08:12 +000030
Ted Kremenek0ea76722008-12-15 19:56:42 +000031 if (!MICache.empty()) {
32 MI = MICache.back();
33 MICache.pop_back();
Chris Lattner0301b3f2009-02-20 22:19:20 +000034 } else
35 MI = (MacroInfo*) BP.Allocate<MacroInfo>();
Chris Lattnerf47724b2010-08-17 15:55:45 +000036 return MI;
37}
38
39MacroInfo *Preprocessor::AllocateMacroInfo(SourceLocation L) {
40 MacroInfo *MI = AllocateMacroInfo();
Ted Kremenek0ea76722008-12-15 19:56:42 +000041 new (MI) MacroInfo(L);
42 return MI;
43}
44
Chris Lattnerf47724b2010-08-17 15:55:45 +000045MacroInfo *Preprocessor::CloneMacroInfo(const MacroInfo &MacroToClone) {
46 MacroInfo *MI = AllocateMacroInfo();
47 new (MI) MacroInfo(MacroToClone, BP);
48 return MI;
49}
50
Chris Lattner0301b3f2009-02-20 22:19:20 +000051/// ReleaseMacroInfo - Release the specified MacroInfo. This memory will
52/// be reused for allocating new MacroInfo objects.
Chris Lattner2c1ab902010-08-18 16:08:51 +000053void Preprocessor::ReleaseMacroInfo(MacroInfo *MI) {
Chris Lattner0301b3f2009-02-20 22:19:20 +000054 MICache.push_back(MI);
Chris Lattner2c1ab902010-08-18 16:08:51 +000055 MI->FreeArgumentList();
Chris Lattner0301b3f2009-02-20 22:19:20 +000056}
57
58
Chris Lattner141e71f2008-03-09 01:54:53 +000059/// DiscardUntilEndOfDirective - Read and discard all tokens remaining on the
60/// current line until the tok::eom token is found.
61void Preprocessor::DiscardUntilEndOfDirective() {
62 Token Tmp;
63 do {
64 LexUnexpandedToken(Tmp);
65 } while (Tmp.isNot(tok::eom));
66}
67
Chris Lattner141e71f2008-03-09 01:54:53 +000068/// ReadMacroName - Lex and validate a macro name, which occurs after a
69/// #define or #undef. This sets the token kind to eom and discards the rest
70/// of the macro line if the macro name is invalid. isDefineUndef is 1 if
71/// this is due to a a #define, 2 if #undef directive, 0 if it is something
72/// else (e.g. #ifdef).
73void Preprocessor::ReadMacroName(Token &MacroNameTok, char isDefineUndef) {
74 // Read the token, don't allow macro expansion on it.
75 LexUnexpandedToken(MacroNameTok);
Mike Stump1eb44332009-09-09 15:08:12 +000076
Chris Lattner141e71f2008-03-09 01:54:53 +000077 // Missing macro name?
Chris Lattner3692b092008-11-18 07:59:24 +000078 if (MacroNameTok.is(tok::eom)) {
79 Diag(MacroNameTok, diag::err_pp_missing_macro_name);
80 return;
81 }
Mike Stump1eb44332009-09-09 15:08:12 +000082
Chris Lattner141e71f2008-03-09 01:54:53 +000083 IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
84 if (II == 0) {
Douglas Gregor453091c2010-03-16 22:30:13 +000085 bool Invalid = false;
86 std::string Spelling = getSpelling(MacroNameTok, &Invalid);
87 if (Invalid)
88 return;
89
Chris Lattner9485d232008-12-13 20:12:40 +000090 const IdentifierInfo &Info = Identifiers.get(Spelling);
91 if (Info.isCPlusPlusOperatorKeyword())
Chris Lattner141e71f2008-03-09 01:54:53 +000092 // C++ 2.5p2: Alternative tokens behave the same as its primary token
93 // except for their spellings.
Chris Lattner56b05c82008-11-18 08:02:48 +000094 Diag(MacroNameTok, diag::err_pp_operator_used_as_macro_name) << Spelling;
Chris Lattner141e71f2008-03-09 01:54:53 +000095 else
96 Diag(MacroNameTok, diag::err_pp_macro_not_identifier);
97 // Fall through on error.
98 } else if (isDefineUndef && II->getPPKeywordID() == tok::pp_defined) {
99 // Error if defining "defined": C99 6.10.8.4.
100 Diag(MacroNameTok, diag::err_defined_macro_name);
101 } else if (isDefineUndef && II->hasMacroDefinition() &&
102 getMacroInfo(II)->isBuiltinMacro()) {
103 // Error if defining "__LINE__" and other builtins: C99 6.10.8.4.
104 if (isDefineUndef == 1)
105 Diag(MacroNameTok, diag::pp_redef_builtin_macro);
106 else
107 Diag(MacroNameTok, diag::pp_undef_builtin_macro);
108 } else {
109 // Okay, we got a good identifier node. Return it.
110 return;
111 }
Mike Stump1eb44332009-09-09 15:08:12 +0000112
Chris Lattner141e71f2008-03-09 01:54:53 +0000113 // Invalid macro name, read and discard the rest of the line. Then set the
114 // token kind to tok::eom.
115 MacroNameTok.setKind(tok::eom);
116 return DiscardUntilEndOfDirective();
117}
118
119/// CheckEndOfDirective - Ensure that the next token is a tok::eom token. If
Chris Lattnerab82f412009-04-17 23:30:53 +0000120/// not, emit a diagnostic and consume up until the eom. If EnableMacros is
121/// true, then we consider macros that expand to zero tokens as being ok.
122void Preprocessor::CheckEndOfDirective(const char *DirType, bool EnableMacros) {
Chris Lattner141e71f2008-03-09 01:54:53 +0000123 Token Tmp;
Chris Lattnerab82f412009-04-17 23:30:53 +0000124 // Lex unexpanded tokens for most directives: macros might expand to zero
125 // tokens, causing us to miss diagnosing invalid lines. Some directives (like
126 // #line) allow empty macros.
127 if (EnableMacros)
128 Lex(Tmp);
129 else
130 LexUnexpandedToken(Tmp);
Mike Stump1eb44332009-09-09 15:08:12 +0000131
Chris Lattner141e71f2008-03-09 01:54:53 +0000132 // There should be no tokens after the directive, but we allow them as an
133 // extension.
134 while (Tmp.is(tok::comment)) // Skip comments in -C mode.
135 LexUnexpandedToken(Tmp);
Mike Stump1eb44332009-09-09 15:08:12 +0000136
Chris Lattner141e71f2008-03-09 01:54:53 +0000137 if (Tmp.isNot(tok::eom)) {
Chris Lattner959875a2009-04-14 05:15:20 +0000138 // Add a fixit in GNU/C99/C++ mode. Don't offer a fixit for strict-C89,
139 // because it is more trouble than it is worth to insert /**/ and check that
140 // there is no /**/ in the range also.
Douglas Gregor849b2432010-03-31 17:46:05 +0000141 FixItHint Hint;
Chris Lattner959875a2009-04-14 05:15:20 +0000142 if (Features.GNUMode || Features.C99 || Features.CPlusPlus)
Douglas Gregor849b2432010-03-31 17:46:05 +0000143 Hint = FixItHint::CreateInsertion(Tmp.getLocation(),"//");
144 Diag(Tmp, diag::ext_pp_extra_tokens_at_eol) << DirType << Hint;
Chris Lattner141e71f2008-03-09 01:54:53 +0000145 DiscardUntilEndOfDirective();
146 }
147}
148
149
150
151/// SkipExcludedConditionalBlock - We just read a #if or related directive and
152/// decided that the subsequent tokens are in the #if'd out portion of the
153/// file. Lex the rest of the file, until we see an #endif. If
154/// FoundNonSkipPortion is true, then we have already emitted code for part of
155/// this #if directive, so #else/#elif blocks should never be entered. If ElseOk
156/// is true, then #else directives are ok, if not, then we have already seen one
157/// so a #else directive is a duplicate. When this returns, the caller can lex
158/// the first valid token.
159void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc,
160 bool FoundNonSkipPortion,
161 bool FoundElse) {
162 ++NumSkipped;
Ted Kremenekf6452c52008-11-18 01:04:47 +0000163 assert(CurTokenLexer == 0 && CurPPLexer && "Lexing a macro, not a file?");
Chris Lattner141e71f2008-03-09 01:54:53 +0000164
Ted Kremenek60e45d42008-11-18 00:34:22 +0000165 CurPPLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/false,
Chris Lattner141e71f2008-03-09 01:54:53 +0000166 FoundNonSkipPortion, FoundElse);
Mike Stump1eb44332009-09-09 15:08:12 +0000167
Ted Kremenek268ee702008-12-12 18:34:08 +0000168 if (CurPTHLexer) {
169 PTHSkipExcludedConditionalBlock();
170 return;
171 }
Mike Stump1eb44332009-09-09 15:08:12 +0000172
Chris Lattner141e71f2008-03-09 01:54:53 +0000173 // Enter raw mode to disable identifier lookup (and thus macro expansion),
174 // disabling warnings, etc.
Ted Kremenek60e45d42008-11-18 00:34:22 +0000175 CurPPLexer->LexingRawMode = true;
Chris Lattner141e71f2008-03-09 01:54:53 +0000176 Token Tok;
177 while (1) {
Chris Lattner2c6b1932010-01-18 22:33:01 +0000178 CurLexer->Lex(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000179
Chris Lattner141e71f2008-03-09 01:54:53 +0000180 // If this is the end of the buffer, we have an error.
181 if (Tok.is(tok::eof)) {
182 // Emit errors for each unterminated conditional on the stack, including
183 // the current one.
Ted Kremenek60e45d42008-11-18 00:34:22 +0000184 while (!CurPPLexer->ConditionalStack.empty()) {
Douglas Gregor2d474ba2010-08-12 17:04:55 +0000185 if (!isCodeCompletionFile(Tok.getLocation()))
186 Diag(CurPPLexer->ConditionalStack.back().IfLoc,
187 diag::err_pp_unterminated_conditional);
Ted Kremenek60e45d42008-11-18 00:34:22 +0000188 CurPPLexer->ConditionalStack.pop_back();
Mike Stump1eb44332009-09-09 15:08:12 +0000189 }
190
Chris Lattner141e71f2008-03-09 01:54:53 +0000191 // Just return and let the caller lex after this #include.
192 break;
193 }
Mike Stump1eb44332009-09-09 15:08:12 +0000194
Chris Lattner141e71f2008-03-09 01:54:53 +0000195 // If this token is not a preprocessor directive, just skip it.
196 if (Tok.isNot(tok::hash) || !Tok.isAtStartOfLine())
197 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000198
Chris Lattner141e71f2008-03-09 01:54:53 +0000199 // We just parsed a # character at the start of a line, so we're in
200 // directive mode. Tell the lexer this so any newlines we see will be
201 // converted into an EOM token (this terminates the macro).
Ted Kremenek60e45d42008-11-18 00:34:22 +0000202 CurPPLexer->ParsingPreprocessorDirective = true;
Ted Kremenekac6b06d2008-11-18 00:43:07 +0000203 if (CurLexer) CurLexer->SetCommentRetentionState(false);
Chris Lattner141e71f2008-03-09 01:54:53 +0000204
Mike Stump1eb44332009-09-09 15:08:12 +0000205
Chris Lattner141e71f2008-03-09 01:54:53 +0000206 // Read the next token, the directive flavor.
207 LexUnexpandedToken(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000208
Chris Lattner141e71f2008-03-09 01:54:53 +0000209 // If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or
210 // something bogus), skip it.
211 if (Tok.isNot(tok::identifier)) {
Ted Kremenek60e45d42008-11-18 00:34:22 +0000212 CurPPLexer->ParsingPreprocessorDirective = false;
Chris Lattner141e71f2008-03-09 01:54:53 +0000213 // Restore comment saving mode.
Ted Kremenekac6b06d2008-11-18 00:43:07 +0000214 if (CurLexer) CurLexer->SetCommentRetentionState(KeepComments);
Chris Lattner141e71f2008-03-09 01:54:53 +0000215 continue;
216 }
217
218 // If the first letter isn't i or e, it isn't intesting to us. We know that
219 // this is safe in the face of spelling differences, because there is no way
220 // to spell an i/e in a strange way that is another letter. Skipping this
221 // allows us to avoid looking up the identifier info for #define/#undef and
222 // other common directives.
Douglas Gregora5430162010-03-16 20:46:42 +0000223 bool Invalid = false;
224 const char *RawCharData = SourceMgr.getCharacterData(Tok.getLocation(),
225 &Invalid);
226 if (Invalid)
227 return;
228
Chris Lattner141e71f2008-03-09 01:54:53 +0000229 char FirstChar = RawCharData[0];
Mike Stump1eb44332009-09-09 15:08:12 +0000230 if (FirstChar >= 'a' && FirstChar <= 'z' &&
Chris Lattner141e71f2008-03-09 01:54:53 +0000231 FirstChar != 'i' && FirstChar != 'e') {
Ted Kremenek60e45d42008-11-18 00:34:22 +0000232 CurPPLexer->ParsingPreprocessorDirective = false;
Chris Lattner141e71f2008-03-09 01:54:53 +0000233 // Restore comment saving mode.
Ted Kremenekac6b06d2008-11-18 00:43:07 +0000234 if (CurLexer) CurLexer->SetCommentRetentionState(KeepComments);
Chris Lattner141e71f2008-03-09 01:54:53 +0000235 continue;
236 }
Mike Stump1eb44332009-09-09 15:08:12 +0000237
Chris Lattner141e71f2008-03-09 01:54:53 +0000238 // Get the identifier name without trigraphs or embedded newlines. Note
239 // that we can't use Tok.getIdentifierInfo() because its lookup is disabled
240 // when skipping.
Benjamin Kramerb939a4e2009-12-31 13:32:38 +0000241 char DirectiveBuf[20];
242 llvm::StringRef Directive;
Chris Lattner141e71f2008-03-09 01:54:53 +0000243 if (!Tok.needsCleaning() && Tok.getLength() < 20) {
Benjamin Kramerb939a4e2009-12-31 13:32:38 +0000244 Directive = llvm::StringRef(RawCharData, Tok.getLength());
Chris Lattner141e71f2008-03-09 01:54:53 +0000245 } else {
246 std::string DirectiveStr = getSpelling(Tok);
Benjamin Kramerb939a4e2009-12-31 13:32:38 +0000247 unsigned IdLen = DirectiveStr.size();
Chris Lattner141e71f2008-03-09 01:54:53 +0000248 if (IdLen >= 20) {
Ted Kremenek60e45d42008-11-18 00:34:22 +0000249 CurPPLexer->ParsingPreprocessorDirective = false;
Chris Lattner141e71f2008-03-09 01:54:53 +0000250 // Restore comment saving mode.
Ted Kremenekac6b06d2008-11-18 00:43:07 +0000251 if (CurLexer) CurLexer->SetCommentRetentionState(KeepComments);
Chris Lattner141e71f2008-03-09 01:54:53 +0000252 continue;
253 }
Benjamin Kramerb939a4e2009-12-31 13:32:38 +0000254 memcpy(DirectiveBuf, &DirectiveStr[0], IdLen);
255 Directive = llvm::StringRef(DirectiveBuf, IdLen);
Chris Lattner141e71f2008-03-09 01:54:53 +0000256 }
Mike Stump1eb44332009-09-09 15:08:12 +0000257
Benjamin Kramerb939a4e2009-12-31 13:32:38 +0000258 if (Directive.startswith("if")) {
259 llvm::StringRef Sub = Directive.substr(2);
260 if (Sub.empty() || // "if"
261 Sub == "def" || // "ifdef"
262 Sub == "ndef") { // "ifndef"
Chris Lattner141e71f2008-03-09 01:54:53 +0000263 // We know the entire #if/#ifdef/#ifndef block will be skipped, don't
264 // bother parsing the condition.
265 DiscardUntilEndOfDirective();
Ted Kremenek60e45d42008-11-18 00:34:22 +0000266 CurPPLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true,
Chris Lattner141e71f2008-03-09 01:54:53 +0000267 /*foundnonskip*/false,
268 /*fnddelse*/false);
269 }
Benjamin Kramerb939a4e2009-12-31 13:32:38 +0000270 } else if (Directive[0] == 'e') {
271 llvm::StringRef Sub = Directive.substr(1);
272 if (Sub == "ndif") { // "endif"
Chris Lattner35410d52009-04-14 05:07:49 +0000273 CheckEndOfDirective("endif");
Chris Lattner141e71f2008-03-09 01:54:53 +0000274 PPConditionalInfo CondInfo;
275 CondInfo.WasSkipping = true; // Silence bogus warning.
Ted Kremenek60e45d42008-11-18 00:34:22 +0000276 bool InCond = CurPPLexer->popConditionalLevel(CondInfo);
Chris Lattner141e71f2008-03-09 01:54:53 +0000277 InCond = InCond; // Silence warning in no-asserts mode.
278 assert(!InCond && "Can't be skipping if not in a conditional!");
Mike Stump1eb44332009-09-09 15:08:12 +0000279
Chris Lattner141e71f2008-03-09 01:54:53 +0000280 // If we popped the outermost skipping block, we're done skipping!
281 if (!CondInfo.WasSkipping)
282 break;
Benjamin Kramerb939a4e2009-12-31 13:32:38 +0000283 } else if (Sub == "lse") { // "else".
Chris Lattner141e71f2008-03-09 01:54:53 +0000284 // #else directive in a skipping conditional. If not in some other
285 // skipping conditional, and if #else hasn't already been seen, enter it
286 // as a non-skipping conditional.
Chris Lattner8fe00e72009-04-18 01:34:22 +0000287 DiscardUntilEndOfDirective(); // C99 6.10p4.
Ted Kremenek60e45d42008-11-18 00:34:22 +0000288 PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel();
Mike Stump1eb44332009-09-09 15:08:12 +0000289
Chris Lattner141e71f2008-03-09 01:54:53 +0000290 // If this is a #else with a #else before it, report the error.
291 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_else_after_else);
Mike Stump1eb44332009-09-09 15:08:12 +0000292
Chris Lattner141e71f2008-03-09 01:54:53 +0000293 // Note that we've seen a #else in this conditional.
294 CondInfo.FoundElse = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000295
Chris Lattner141e71f2008-03-09 01:54:53 +0000296 // If the conditional is at the top level, and the #if block wasn't
297 // entered, enter the #else block now.
298 if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) {
299 CondInfo.FoundNonSkip = true;
300 break;
301 }
Benjamin Kramerb939a4e2009-12-31 13:32:38 +0000302 } else if (Sub == "lif") { // "elif".
Ted Kremenek60e45d42008-11-18 00:34:22 +0000303 PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel();
Chris Lattner141e71f2008-03-09 01:54:53 +0000304
305 bool ShouldEnter;
306 // If this is in a skipping block or if we're already handled this #if
307 // block, don't bother parsing the condition.
308 if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) {
309 DiscardUntilEndOfDirective();
310 ShouldEnter = false;
311 } else {
312 // Restore the value of LexingRawMode so that identifiers are
313 // looked up, etc, inside the #elif expression.
Ted Kremenek60e45d42008-11-18 00:34:22 +0000314 assert(CurPPLexer->LexingRawMode && "We have to be skipping here!");
315 CurPPLexer->LexingRawMode = false;
Chris Lattner141e71f2008-03-09 01:54:53 +0000316 IdentifierInfo *IfNDefMacro = 0;
317 ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro);
Ted Kremenek60e45d42008-11-18 00:34:22 +0000318 CurPPLexer->LexingRawMode = true;
Chris Lattner141e71f2008-03-09 01:54:53 +0000319 }
Mike Stump1eb44332009-09-09 15:08:12 +0000320
Chris Lattner141e71f2008-03-09 01:54:53 +0000321 // If this is a #elif with a #else before it, report the error.
322 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_elif_after_else);
Mike Stump1eb44332009-09-09 15:08:12 +0000323
Chris Lattner141e71f2008-03-09 01:54:53 +0000324 // If this condition is true, enter it!
325 if (ShouldEnter) {
326 CondInfo.FoundNonSkip = true;
327 break;
328 }
329 }
330 }
Mike Stump1eb44332009-09-09 15:08:12 +0000331
Ted Kremenek60e45d42008-11-18 00:34:22 +0000332 CurPPLexer->ParsingPreprocessorDirective = false;
Chris Lattner141e71f2008-03-09 01:54:53 +0000333 // Restore comment saving mode.
Ted Kremenekac6b06d2008-11-18 00:43:07 +0000334 if (CurLexer) CurLexer->SetCommentRetentionState(KeepComments);
Chris Lattner141e71f2008-03-09 01:54:53 +0000335 }
336
337 // Finally, if we are out of the conditional (saw an #endif or ran off the end
338 // of the file, just stop skipping and return to lexing whatever came after
339 // the #if block.
Ted Kremenek60e45d42008-11-18 00:34:22 +0000340 CurPPLexer->LexingRawMode = false;
Chris Lattner141e71f2008-03-09 01:54:53 +0000341}
342
Ted Kremenek268ee702008-12-12 18:34:08 +0000343void Preprocessor::PTHSkipExcludedConditionalBlock() {
Mike Stump1eb44332009-09-09 15:08:12 +0000344
345 while (1) {
Ted Kremenek268ee702008-12-12 18:34:08 +0000346 assert(CurPTHLexer);
347 assert(CurPTHLexer->LexingRawMode == false);
Mike Stump1eb44332009-09-09 15:08:12 +0000348
Ted Kremenek268ee702008-12-12 18:34:08 +0000349 // Skip to the next '#else', '#elif', or #endif.
350 if (CurPTHLexer->SkipBlock()) {
351 // We have reached an #endif. Both the '#' and 'endif' tokens
352 // have been consumed by the PTHLexer. Just pop off the condition level.
353 PPConditionalInfo CondInfo;
354 bool InCond = CurPTHLexer->popConditionalLevel(CondInfo);
355 InCond = InCond; // Silence warning in no-asserts mode.
356 assert(!InCond && "Can't be skipping if not in a conditional!");
357 break;
358 }
Mike Stump1eb44332009-09-09 15:08:12 +0000359
Ted Kremenek268ee702008-12-12 18:34:08 +0000360 // We have reached a '#else' or '#elif'. Lex the next token to get
361 // the directive flavor.
362 Token Tok;
363 LexUnexpandedToken(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000364
Ted Kremenek268ee702008-12-12 18:34:08 +0000365 // We can actually look up the IdentifierInfo here since we aren't in
366 // raw mode.
367 tok::PPKeywordKind K = Tok.getIdentifierInfo()->getPPKeywordID();
368
369 if (K == tok::pp_else) {
370 // #else: Enter the else condition. We aren't in a nested condition
371 // since we skip those. We're always in the one matching the last
372 // blocked we skipped.
373 PPConditionalInfo &CondInfo = CurPTHLexer->peekConditionalLevel();
374 // Note that we've seen a #else in this conditional.
375 CondInfo.FoundElse = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000376
Ted Kremenek268ee702008-12-12 18:34:08 +0000377 // If the #if block wasn't entered then enter the #else block now.
378 if (!CondInfo.FoundNonSkip) {
379 CondInfo.FoundNonSkip = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000380
Daniel Dunbar8533bd52009-04-13 17:57:49 +0000381 // Scan until the eom token.
Ted Kremeneke5680f32008-12-23 01:30:52 +0000382 CurPTHLexer->ParsingPreprocessorDirective = true;
Daniel Dunbar8533bd52009-04-13 17:57:49 +0000383 DiscardUntilEndOfDirective();
Ted Kremeneke5680f32008-12-23 01:30:52 +0000384 CurPTHLexer->ParsingPreprocessorDirective = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000385
Ted Kremenek268ee702008-12-12 18:34:08 +0000386 break;
387 }
Mike Stump1eb44332009-09-09 15:08:12 +0000388
Ted Kremenek268ee702008-12-12 18:34:08 +0000389 // Otherwise skip this block.
390 continue;
391 }
Mike Stump1eb44332009-09-09 15:08:12 +0000392
Ted Kremenek268ee702008-12-12 18:34:08 +0000393 assert(K == tok::pp_elif);
394 PPConditionalInfo &CondInfo = CurPTHLexer->peekConditionalLevel();
395
396 // If this is a #elif with a #else before it, report the error.
397 if (CondInfo.FoundElse)
398 Diag(Tok, diag::pp_err_elif_after_else);
Mike Stump1eb44332009-09-09 15:08:12 +0000399
Ted Kremenek268ee702008-12-12 18:34:08 +0000400 // If this is in a skipping block or if we're already handled this #if
Mike Stump1eb44332009-09-09 15:08:12 +0000401 // block, don't bother parsing the condition. We just skip this block.
Ted Kremenek268ee702008-12-12 18:34:08 +0000402 if (CondInfo.FoundNonSkip)
403 continue;
404
405 // Evaluate the condition of the #elif.
406 IdentifierInfo *IfNDefMacro = 0;
407 CurPTHLexer->ParsingPreprocessorDirective = true;
408 bool ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro);
409 CurPTHLexer->ParsingPreprocessorDirective = false;
410
411 // If this condition is true, enter it!
412 if (ShouldEnter) {
413 CondInfo.FoundNonSkip = true;
414 break;
415 }
416
417 // Otherwise, skip this block and go to the next one.
418 continue;
419 }
420}
421
Chris Lattner10725092008-03-09 04:17:44 +0000422/// LookupFile - Given a "foo" or <foo> reference, look up the indicated file,
423/// return null on failure. isAngled indicates whether the file reference is
424/// for system #include's or not (i.e. using <> instead of "").
Chris Lattnera1394812010-01-10 01:35:12 +0000425const FileEntry *Preprocessor::LookupFile(llvm::StringRef Filename,
Chris Lattner10725092008-03-09 04:17:44 +0000426 bool isAngled,
427 const DirectoryLookup *FromDir,
428 const DirectoryLookup *&CurDir) {
429 // If the header lookup mechanism may be relative to the current file, pass in
430 // info about where the current file is.
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000431 const FileEntry *CurFileEnt = 0;
Chris Lattner10725092008-03-09 04:17:44 +0000432 if (!FromDir) {
Chris Lattner2b2453a2009-01-17 06:22:33 +0000433 FileID FID = getCurrentFileLexer()->getFileID();
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000434 CurFileEnt = SourceMgr.getFileEntryForID(FID);
Mike Stump1eb44332009-09-09 15:08:12 +0000435
Chris Lattnerbe5c64d2009-02-04 19:45:07 +0000436 // If there is no file entry associated with this file, it must be the
437 // predefines buffer. Any other file is not lexed with a normal lexer, so
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000438 // it won't be scanned for preprocessor directives. If we have the
439 // predefines buffer, resolve #include references (which come from the
440 // -include command line argument) as if they came from the main file, this
441 // affects file lookup etc.
442 if (CurFileEnt == 0) {
Chris Lattnerbe5c64d2009-02-04 19:45:07 +0000443 FID = SourceMgr.getMainFileID();
444 CurFileEnt = SourceMgr.getFileEntryForID(FID);
445 }
Chris Lattner10725092008-03-09 04:17:44 +0000446 }
Mike Stump1eb44332009-09-09 15:08:12 +0000447
Chris Lattner10725092008-03-09 04:17:44 +0000448 // Do a standard file entry lookup.
449 CurDir = CurDirLookup;
450 const FileEntry *FE =
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000451 HeaderInfo.LookupFile(Filename, isAngled, FromDir, CurDir, CurFileEnt);
Chris Lattnerf45b6462010-01-22 00:14:44 +0000452 if (FE) return FE;
Mike Stump1eb44332009-09-09 15:08:12 +0000453
Chris Lattner10725092008-03-09 04:17:44 +0000454 // Otherwise, see if this is a subframework header. If so, this is relative
455 // to one of the headers on the #include stack. Walk the list of the current
456 // headers on the #include stack and pass them to HeaderInfo.
Ted Kremenek81d24e12008-11-20 16:19:53 +0000457 if (IsFileLexer()) {
Ted Kremenek41938c82008-11-19 21:57:25 +0000458 if ((CurFileEnt = SourceMgr.getFileEntryForID(CurPPLexer->getFileID())))
Chris Lattnera1394812010-01-10 01:35:12 +0000459 if ((FE = HeaderInfo.LookupSubframeworkHeader(Filename, CurFileEnt)))
Chris Lattner10725092008-03-09 04:17:44 +0000460 return FE;
461 }
Mike Stump1eb44332009-09-09 15:08:12 +0000462
Chris Lattner10725092008-03-09 04:17:44 +0000463 for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) {
464 IncludeStackInfo &ISEntry = IncludeMacroStack[e-i-1];
Ted Kremenek81d24e12008-11-20 16:19:53 +0000465 if (IsFileLexer(ISEntry)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000466 if ((CurFileEnt =
Ted Kremenek41938c82008-11-19 21:57:25 +0000467 SourceMgr.getFileEntryForID(ISEntry.ThePPLexer->getFileID())))
Chris Lattnera1394812010-01-10 01:35:12 +0000468 if ((FE = HeaderInfo.LookupSubframeworkHeader(Filename, CurFileEnt)))
Chris Lattner10725092008-03-09 04:17:44 +0000469 return FE;
470 }
471 }
Mike Stump1eb44332009-09-09 15:08:12 +0000472
Chris Lattner10725092008-03-09 04:17:44 +0000473 // Otherwise, we really couldn't find the file.
474 return 0;
475}
476
Chris Lattner141e71f2008-03-09 01:54:53 +0000477
478//===----------------------------------------------------------------------===//
479// Preprocessor Directive Handling.
480//===----------------------------------------------------------------------===//
481
482/// HandleDirective - This callback is invoked when the lexer sees a # token
Mike Stump1eb44332009-09-09 15:08:12 +0000483/// at the start of a line. This consumes the directive, modifies the
Chris Lattner141e71f2008-03-09 01:54:53 +0000484/// lexer/preprocessor state, and advances the lexer(s) so that the next token
485/// read is the correct one.
486void Preprocessor::HandleDirective(Token &Result) {
487 // FIXME: Traditional: # with whitespace before it not recognized by K&R?
Mike Stump1eb44332009-09-09 15:08:12 +0000488
Chris Lattner141e71f2008-03-09 01:54:53 +0000489 // We just parsed a # character at the start of a line, so we're in directive
490 // mode. Tell the lexer this so any newlines we see will be converted into an
491 // EOM token (which terminates the directive).
Ted Kremenek60e45d42008-11-18 00:34:22 +0000492 CurPPLexer->ParsingPreprocessorDirective = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000493
Chris Lattner141e71f2008-03-09 01:54:53 +0000494 ++NumDirectives;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000495
Chris Lattner141e71f2008-03-09 01:54:53 +0000496 // We are about to read a token. For the multiple-include optimization FA to
Mike Stump1eb44332009-09-09 15:08:12 +0000497 // work, we have to remember if we had read any tokens *before* this
Chris Lattner141e71f2008-03-09 01:54:53 +0000498 // pp-directive.
Chris Lattner1d9c54d2009-12-14 04:54:40 +0000499 bool ReadAnyTokensBeforeDirective =CurPPLexer->MIOpt.getHasReadAnyTokensVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000500
Chris Lattner42aa16c2009-03-18 21:00:25 +0000501 // Save the '#' token in case we need to return it later.
502 Token SavedHash = Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000503
Chris Lattner141e71f2008-03-09 01:54:53 +0000504 // Read the next token, the directive flavor. This isn't expanded due to
505 // C99 6.10.3p8.
506 LexUnexpandedToken(Result);
Mike Stump1eb44332009-09-09 15:08:12 +0000507
Chris Lattner141e71f2008-03-09 01:54:53 +0000508 // C99 6.10.3p11: Is this preprocessor directive in macro invocation? e.g.:
509 // #define A(x) #x
510 // A(abc
511 // #warning blah
512 // def)
513 // If so, the user is relying on non-portable behavior, emit a diagnostic.
514 if (InMacroArgs)
515 Diag(Result, diag::ext_embedded_directive);
Mike Stump1eb44332009-09-09 15:08:12 +0000516
Chris Lattner141e71f2008-03-09 01:54:53 +0000517TryAgain:
518 switch (Result.getKind()) {
519 case tok::eom:
520 return; // null directive.
521 case tok::comment:
522 // Handle stuff like "# /*foo*/ define X" in -E -C mode.
523 LexUnexpandedToken(Result);
524 goto TryAgain;
525
Chris Lattner478a18e2009-01-26 06:19:46 +0000526 case tok::numeric_constant: // # 7 GNU line marker directive.
Chris Lattner5f607c42009-03-18 20:41:10 +0000527 if (getLangOptions().AsmPreprocessor)
528 break; // # 4 is not a preprocessor directive in .S files.
Chris Lattner478a18e2009-01-26 06:19:46 +0000529 return HandleDigitDirective(Result);
Chris Lattner141e71f2008-03-09 01:54:53 +0000530 default:
531 IdentifierInfo *II = Result.getIdentifierInfo();
532 if (II == 0) break; // Not an identifier.
Mike Stump1eb44332009-09-09 15:08:12 +0000533
Chris Lattner141e71f2008-03-09 01:54:53 +0000534 // Ask what the preprocessor keyword ID is.
535 switch (II->getPPKeywordID()) {
536 default: break;
537 // C99 6.10.1 - Conditional Inclusion.
538 case tok::pp_if:
539 return HandleIfDirective(Result, ReadAnyTokensBeforeDirective);
540 case tok::pp_ifdef:
541 return HandleIfdefDirective(Result, false, true/*not valid for miopt*/);
542 case tok::pp_ifndef:
543 return HandleIfdefDirective(Result, true, ReadAnyTokensBeforeDirective);
544 case tok::pp_elif:
545 return HandleElifDirective(Result);
546 case tok::pp_else:
547 return HandleElseDirective(Result);
548 case tok::pp_endif:
549 return HandleEndifDirective(Result);
Mike Stump1eb44332009-09-09 15:08:12 +0000550
Chris Lattner141e71f2008-03-09 01:54:53 +0000551 // C99 6.10.2 - Source File Inclusion.
552 case tok::pp_include:
Chris Lattnerb8e240e2009-04-08 18:24:34 +0000553 return HandleIncludeDirective(Result); // Handle #include.
554 case tok::pp___include_macros:
Chris Lattnerde076652009-04-08 18:46:40 +0000555 return HandleIncludeMacrosDirective(Result); // Handle -imacros.
Mike Stump1eb44332009-09-09 15:08:12 +0000556
Chris Lattner141e71f2008-03-09 01:54:53 +0000557 // C99 6.10.3 - Macro Replacement.
558 case tok::pp_define:
559 return HandleDefineDirective(Result);
560 case tok::pp_undef:
561 return HandleUndefDirective(Result);
562
563 // C99 6.10.4 - Line Control.
564 case tok::pp_line:
Chris Lattner359cc442009-01-26 05:29:08 +0000565 return HandleLineDirective(Result);
Mike Stump1eb44332009-09-09 15:08:12 +0000566
Chris Lattner141e71f2008-03-09 01:54:53 +0000567 // C99 6.10.5 - Error Directive.
568 case tok::pp_error:
569 return HandleUserDiagnosticDirective(Result, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000570
Chris Lattner141e71f2008-03-09 01:54:53 +0000571 // C99 6.10.6 - Pragma Directive.
572 case tok::pp_pragma:
573 return HandlePragmaDirective();
Mike Stump1eb44332009-09-09 15:08:12 +0000574
Chris Lattner141e71f2008-03-09 01:54:53 +0000575 // GNU Extensions.
576 case tok::pp_import:
577 return HandleImportDirective(Result);
578 case tok::pp_include_next:
579 return HandleIncludeNextDirective(Result);
Mike Stump1eb44332009-09-09 15:08:12 +0000580
Chris Lattner141e71f2008-03-09 01:54:53 +0000581 case tok::pp_warning:
582 Diag(Result, diag::ext_pp_warning_directive);
583 return HandleUserDiagnosticDirective(Result, true);
584 case tok::pp_ident:
585 return HandleIdentSCCSDirective(Result);
586 case tok::pp_sccs:
587 return HandleIdentSCCSDirective(Result);
588 case tok::pp_assert:
589 //isExtension = true; // FIXME: implement #assert
590 break;
591 case tok::pp_unassert:
592 //isExtension = true; // FIXME: implement #unassert
593 break;
594 }
595 break;
596 }
Mike Stump1eb44332009-09-09 15:08:12 +0000597
Chris Lattner42aa16c2009-03-18 21:00:25 +0000598 // If this is a .S file, treat unknown # directives as non-preprocessor
599 // directives. This is important because # may be a comment or introduce
600 // various pseudo-ops. Just return the # token and push back the following
601 // token to be lexed next time.
602 if (getLangOptions().AsmPreprocessor) {
Daniel Dunbar3d399a02009-07-13 21:48:50 +0000603 Token *Toks = new Token[2];
Chris Lattner42aa16c2009-03-18 21:00:25 +0000604 // Return the # and the token after it.
Mike Stump1eb44332009-09-09 15:08:12 +0000605 Toks[0] = SavedHash;
Chris Lattner42aa16c2009-03-18 21:00:25 +0000606 Toks[1] = Result;
607 // Enter this token stream so that we re-lex the tokens. Make sure to
608 // enable macro expansion, in case the token after the # is an identifier
609 // that is expanded.
610 EnterTokenStream(Toks, 2, false, true);
611 return;
612 }
Mike Stump1eb44332009-09-09 15:08:12 +0000613
Chris Lattner141e71f2008-03-09 01:54:53 +0000614 // If we reached here, the preprocessing token is not valid!
615 Diag(Result, diag::err_pp_invalid_directive);
Mike Stump1eb44332009-09-09 15:08:12 +0000616
Chris Lattner141e71f2008-03-09 01:54:53 +0000617 // Read the rest of the PP line.
618 DiscardUntilEndOfDirective();
Mike Stump1eb44332009-09-09 15:08:12 +0000619
Chris Lattner141e71f2008-03-09 01:54:53 +0000620 // Okay, we're done parsing the directive.
621}
622
Chris Lattner478a18e2009-01-26 06:19:46 +0000623/// GetLineValue - Convert a numeric token into an unsigned value, emitting
624/// Diagnostic DiagID if it is invalid, and returning the value in Val.
625static bool GetLineValue(Token &DigitTok, unsigned &Val,
626 unsigned DiagID, Preprocessor &PP) {
627 if (DigitTok.isNot(tok::numeric_constant)) {
628 PP.Diag(DigitTok, DiagID);
Mike Stump1eb44332009-09-09 15:08:12 +0000629
Chris Lattner478a18e2009-01-26 06:19:46 +0000630 if (DigitTok.isNot(tok::eom))
631 PP.DiscardUntilEndOfDirective();
632 return true;
633 }
Mike Stump1eb44332009-09-09 15:08:12 +0000634
Chris Lattner478a18e2009-01-26 06:19:46 +0000635 llvm::SmallString<64> IntegerBuffer;
636 IntegerBuffer.resize(DigitTok.getLength());
637 const char *DigitTokBegin = &IntegerBuffer[0];
Douglas Gregor453091c2010-03-16 22:30:13 +0000638 bool Invalid = false;
639 unsigned ActualLength = PP.getSpelling(DigitTok, DigitTokBegin, &Invalid);
640 if (Invalid)
641 return true;
642
Chris Lattnerdc8c90d2009-04-18 18:35:15 +0000643 // Verify that we have a simple digit-sequence, and compute the value. This
644 // is always a simple digit string computed in decimal, so we do this manually
645 // here.
646 Val = 0;
647 for (unsigned i = 0; i != ActualLength; ++i) {
648 if (!isdigit(DigitTokBegin[i])) {
649 PP.Diag(PP.AdvanceToTokenCharacter(DigitTok.getLocation(), i),
650 diag::err_pp_line_digit_sequence);
651 PP.DiscardUntilEndOfDirective();
652 return true;
653 }
Mike Stump1eb44332009-09-09 15:08:12 +0000654
Chris Lattnerdc8c90d2009-04-18 18:35:15 +0000655 unsigned NextVal = Val*10+(DigitTokBegin[i]-'0');
656 if (NextVal < Val) { // overflow.
657 PP.Diag(DigitTok, DiagID);
658 PP.DiscardUntilEndOfDirective();
659 return true;
660 }
661 Val = NextVal;
Chris Lattner478a18e2009-01-26 06:19:46 +0000662 }
Mike Stump1eb44332009-09-09 15:08:12 +0000663
664 // Reject 0, this is needed both by #line numbers and flags.
Chris Lattner478a18e2009-01-26 06:19:46 +0000665 if (Val == 0) {
666 PP.Diag(DigitTok, DiagID);
667 PP.DiscardUntilEndOfDirective();
668 return true;
669 }
Mike Stump1eb44332009-09-09 15:08:12 +0000670
Chris Lattnerdc8c90d2009-04-18 18:35:15 +0000671 if (DigitTokBegin[0] == '0')
672 PP.Diag(DigitTok.getLocation(), diag::warn_pp_line_decimal);
Mike Stump1eb44332009-09-09 15:08:12 +0000673
Chris Lattner478a18e2009-01-26 06:19:46 +0000674 return false;
675}
676
Mike Stump1eb44332009-09-09 15:08:12 +0000677/// HandleLineDirective - Handle #line directive: C99 6.10.4. The two
Chris Lattner359cc442009-01-26 05:29:08 +0000678/// acceptable forms are:
679/// # line digit-sequence
680/// # line digit-sequence "s-char-sequence"
681void Preprocessor::HandleLineDirective(Token &Tok) {
682 // Read the line # and string argument. Per C99 6.10.4p5, these tokens are
683 // expanded.
684 Token DigitTok;
685 Lex(DigitTok);
686
Chris Lattner359cc442009-01-26 05:29:08 +0000687 // Validate the number and convert it to an unsigned.
Chris Lattner478a18e2009-01-26 06:19:46 +0000688 unsigned LineNo;
Chris Lattnerdc8c90d2009-04-18 18:35:15 +0000689 if (GetLineValue(DigitTok, LineNo, diag::err_pp_line_requires_integer,*this))
Chris Lattner359cc442009-01-26 05:29:08 +0000690 return;
Chris Lattner359cc442009-01-26 05:29:08 +0000691
Chris Lattner478a18e2009-01-26 06:19:46 +0000692 // Enforce C99 6.10.4p3: "The digit sequence shall not specify ... a
693 // number greater than 2147483647". C90 requires that the line # be <= 32767.
Chris Lattner359cc442009-01-26 05:29:08 +0000694 unsigned LineLimit = Features.C99 ? 2147483648U : 32768U;
695 if (LineNo >= LineLimit)
696 Diag(DigitTok, diag::ext_pp_line_too_big) << LineLimit;
Mike Stump1eb44332009-09-09 15:08:12 +0000697
Chris Lattner5b9a5042009-01-26 07:57:50 +0000698 int FilenameID = -1;
Chris Lattner359cc442009-01-26 05:29:08 +0000699 Token StrTok;
700 Lex(StrTok);
701
702 // If the StrTok is "eom", then it wasn't present. Otherwise, it must be a
703 // string followed by eom.
Mike Stump1eb44332009-09-09 15:08:12 +0000704 if (StrTok.is(tok::eom))
Chris Lattner359cc442009-01-26 05:29:08 +0000705 ; // ok
706 else if (StrTok.isNot(tok::string_literal)) {
707 Diag(StrTok, diag::err_pp_line_invalid_filename);
708 DiscardUntilEndOfDirective();
709 return;
710 } else {
Chris Lattner5b9a5042009-01-26 07:57:50 +0000711 // Parse and validate the string, converting it into a unique ID.
712 StringLiteralParser Literal(&StrTok, 1, *this);
713 assert(!Literal.AnyWide && "Didn't allow wide strings in");
714 if (Literal.hadError)
715 return DiscardUntilEndOfDirective();
716 if (Literal.Pascal) {
717 Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
718 return DiscardUntilEndOfDirective();
719 }
720 FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString(),
721 Literal.GetStringLength());
Mike Stump1eb44332009-09-09 15:08:12 +0000722
Chris Lattnerab82f412009-04-17 23:30:53 +0000723 // Verify that there is nothing after the string, other than EOM. Because
724 // of C99 6.10.4p5, macros that expand to empty tokens are ok.
725 CheckEndOfDirective("line", true);
Chris Lattner359cc442009-01-26 05:29:08 +0000726 }
Mike Stump1eb44332009-09-09 15:08:12 +0000727
Chris Lattner4c4ea172009-02-03 21:52:55 +0000728 SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID);
Mike Stump1eb44332009-09-09 15:08:12 +0000729
Chris Lattner16629382009-03-27 17:13:49 +0000730 if (Callbacks)
Chris Lattner86d0ef72010-04-14 04:28:50 +0000731 Callbacks->FileChanged(CurPPLexer->getSourceLocation(),
732 PPCallbacks::RenameFile,
Chris Lattner16629382009-03-27 17:13:49 +0000733 SrcMgr::C_User);
Chris Lattner359cc442009-01-26 05:29:08 +0000734}
735
Chris Lattner478a18e2009-01-26 06:19:46 +0000736/// ReadLineMarkerFlags - Parse and validate any flags at the end of a GNU line
737/// marker directive.
738static bool ReadLineMarkerFlags(bool &IsFileEntry, bool &IsFileExit,
739 bool &IsSystemHeader, bool &IsExternCHeader,
740 Preprocessor &PP) {
741 unsigned FlagVal;
742 Token FlagTok;
743 PP.Lex(FlagTok);
744 if (FlagTok.is(tok::eom)) return false;
745 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP))
746 return true;
747
748 if (FlagVal == 1) {
749 IsFileEntry = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000750
Chris Lattner478a18e2009-01-26 06:19:46 +0000751 PP.Lex(FlagTok);
752 if (FlagTok.is(tok::eom)) return false;
753 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP))
754 return true;
755 } else if (FlagVal == 2) {
756 IsFileExit = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000757
Chris Lattner137b6a62009-02-04 06:25:26 +0000758 SourceManager &SM = PP.getSourceManager();
759 // If we are leaving the current presumed file, check to make sure the
760 // presumed include stack isn't empty!
761 FileID CurFileID =
762 SM.getDecomposedInstantiationLoc(FlagTok.getLocation()).first;
763 PresumedLoc PLoc = SM.getPresumedLoc(FlagTok.getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000764
Chris Lattner137b6a62009-02-04 06:25:26 +0000765 // If there is no include loc (main file) or if the include loc is in a
766 // different physical file, then we aren't in a "1" line marker flag region.
767 SourceLocation IncLoc = PLoc.getIncludeLoc();
768 if (IncLoc.isInvalid() ||
769 SM.getDecomposedInstantiationLoc(IncLoc).first != CurFileID) {
770 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_pop);
771 PP.DiscardUntilEndOfDirective();
772 return true;
773 }
Mike Stump1eb44332009-09-09 15:08:12 +0000774
Chris Lattner478a18e2009-01-26 06:19:46 +0000775 PP.Lex(FlagTok);
776 if (FlagTok.is(tok::eom)) return false;
777 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP))
778 return true;
779 }
780
781 // We must have 3 if there are still flags.
782 if (FlagVal != 3) {
783 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
Chris Lattner5b9a5042009-01-26 07:57:50 +0000784 PP.DiscardUntilEndOfDirective();
Chris Lattner478a18e2009-01-26 06:19:46 +0000785 return true;
786 }
Mike Stump1eb44332009-09-09 15:08:12 +0000787
Chris Lattner478a18e2009-01-26 06:19:46 +0000788 IsSystemHeader = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000789
Chris Lattner478a18e2009-01-26 06:19:46 +0000790 PP.Lex(FlagTok);
791 if (FlagTok.is(tok::eom)) return false;
Chris Lattner9d79eba2009-02-04 05:21:58 +0000792 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP))
Chris Lattner478a18e2009-01-26 06:19:46 +0000793 return true;
794
795 // We must have 4 if there is yet another flag.
796 if (FlagVal != 4) {
797 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
Chris Lattner5b9a5042009-01-26 07:57:50 +0000798 PP.DiscardUntilEndOfDirective();
Chris Lattner478a18e2009-01-26 06:19:46 +0000799 return true;
800 }
Mike Stump1eb44332009-09-09 15:08:12 +0000801
Chris Lattner478a18e2009-01-26 06:19:46 +0000802 IsExternCHeader = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000803
Chris Lattner478a18e2009-01-26 06:19:46 +0000804 PP.Lex(FlagTok);
805 if (FlagTok.is(tok::eom)) return false;
806
807 // There are no more valid flags here.
808 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
Chris Lattner5b9a5042009-01-26 07:57:50 +0000809 PP.DiscardUntilEndOfDirective();
Chris Lattner478a18e2009-01-26 06:19:46 +0000810 return true;
811}
812
813/// HandleDigitDirective - Handle a GNU line marker directive, whose syntax is
814/// one of the following forms:
815///
816/// # 42
Mike Stump1eb44332009-09-09 15:08:12 +0000817/// # 42 "file" ('1' | '2')?
Chris Lattner478a18e2009-01-26 06:19:46 +0000818/// # 42 "file" ('1' | '2')? '3' '4'?
819///
820void Preprocessor::HandleDigitDirective(Token &DigitTok) {
821 // Validate the number and convert it to an unsigned. GNU does not have a
822 // line # limit other than it fit in 32-bits.
823 unsigned LineNo;
824 if (GetLineValue(DigitTok, LineNo, diag::err_pp_linemarker_requires_integer,
825 *this))
826 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000827
Chris Lattner478a18e2009-01-26 06:19:46 +0000828 Token StrTok;
829 Lex(StrTok);
Mike Stump1eb44332009-09-09 15:08:12 +0000830
Chris Lattner478a18e2009-01-26 06:19:46 +0000831 bool IsFileEntry = false, IsFileExit = false;
832 bool IsSystemHeader = false, IsExternCHeader = false;
Chris Lattner5b9a5042009-01-26 07:57:50 +0000833 int FilenameID = -1;
834
Chris Lattner478a18e2009-01-26 06:19:46 +0000835 // If the StrTok is "eom", then it wasn't present. Otherwise, it must be a
836 // string followed by eom.
Mike Stump1eb44332009-09-09 15:08:12 +0000837 if (StrTok.is(tok::eom))
Chris Lattner478a18e2009-01-26 06:19:46 +0000838 ; // ok
839 else if (StrTok.isNot(tok::string_literal)) {
840 Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
Chris Lattner5b9a5042009-01-26 07:57:50 +0000841 return DiscardUntilEndOfDirective();
Chris Lattner478a18e2009-01-26 06:19:46 +0000842 } else {
Chris Lattner5b9a5042009-01-26 07:57:50 +0000843 // Parse and validate the string, converting it into a unique ID.
844 StringLiteralParser Literal(&StrTok, 1, *this);
845 assert(!Literal.AnyWide && "Didn't allow wide strings in");
846 if (Literal.hadError)
847 return DiscardUntilEndOfDirective();
848 if (Literal.Pascal) {
849 Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
850 return DiscardUntilEndOfDirective();
851 }
852 FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString(),
853 Literal.GetStringLength());
Mike Stump1eb44332009-09-09 15:08:12 +0000854
Chris Lattner478a18e2009-01-26 06:19:46 +0000855 // If a filename was present, read any flags that are present.
Mike Stump1eb44332009-09-09 15:08:12 +0000856 if (ReadLineMarkerFlags(IsFileEntry, IsFileExit,
Chris Lattner5b9a5042009-01-26 07:57:50 +0000857 IsSystemHeader, IsExternCHeader, *this))
Chris Lattner478a18e2009-01-26 06:19:46 +0000858 return;
Chris Lattner478a18e2009-01-26 06:19:46 +0000859 }
Mike Stump1eb44332009-09-09 15:08:12 +0000860
Chris Lattner9d79eba2009-02-04 05:21:58 +0000861 // Create a line note with this information.
862 SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID,
Mike Stump1eb44332009-09-09 15:08:12 +0000863 IsFileEntry, IsFileExit,
Chris Lattner9d79eba2009-02-04 05:21:58 +0000864 IsSystemHeader, IsExternCHeader);
Mike Stump1eb44332009-09-09 15:08:12 +0000865
Chris Lattner16629382009-03-27 17:13:49 +0000866 // If the preprocessor has callbacks installed, notify them of the #line
867 // change. This is used so that the line marker comes out in -E mode for
868 // example.
869 if (Callbacks) {
870 PPCallbacks::FileChangeReason Reason = PPCallbacks::RenameFile;
871 if (IsFileEntry)
872 Reason = PPCallbacks::EnterFile;
873 else if (IsFileExit)
874 Reason = PPCallbacks::ExitFile;
875 SrcMgr::CharacteristicKind FileKind = SrcMgr::C_User;
876 if (IsExternCHeader)
877 FileKind = SrcMgr::C_ExternCSystem;
878 else if (IsSystemHeader)
879 FileKind = SrcMgr::C_System;
Mike Stump1eb44332009-09-09 15:08:12 +0000880
Chris Lattner86d0ef72010-04-14 04:28:50 +0000881 Callbacks->FileChanged(CurPPLexer->getSourceLocation(), Reason, FileKind);
Chris Lattner16629382009-03-27 17:13:49 +0000882 }
Chris Lattner478a18e2009-01-26 06:19:46 +0000883}
884
885
Chris Lattner099dd052009-01-26 05:30:54 +0000886/// HandleUserDiagnosticDirective - Handle a #warning or #error directive.
887///
Mike Stump1eb44332009-09-09 15:08:12 +0000888void Preprocessor::HandleUserDiagnosticDirective(Token &Tok,
Chris Lattner141e71f2008-03-09 01:54:53 +0000889 bool isWarning) {
Chris Lattner099dd052009-01-26 05:30:54 +0000890 // PTH doesn't emit #warning or #error directives.
891 if (CurPTHLexer)
Chris Lattner359cc442009-01-26 05:29:08 +0000892 return CurPTHLexer->DiscardToEndOfLine();
893
Chris Lattner141e71f2008-03-09 01:54:53 +0000894 // Read the rest of the line raw. We do this because we don't want macros
895 // to be expanded and we don't require that the tokens be valid preprocessing
896 // tokens. For example, this is allowed: "#warning ` 'foo". GCC does
897 // collapse multiple consequtive white space between tokens, but this isn't
898 // specified by the standard.
Chris Lattner359cc442009-01-26 05:29:08 +0000899 std::string Message = CurLexer->ReadToEndOfLine();
900 if (isWarning)
901 Diag(Tok, diag::pp_hash_warning) << Message;
902 else
903 Diag(Tok, diag::err_pp_hash_error) << Message;
Chris Lattner141e71f2008-03-09 01:54:53 +0000904}
905
906/// HandleIdentSCCSDirective - Handle a #ident/#sccs directive.
907///
908void Preprocessor::HandleIdentSCCSDirective(Token &Tok) {
909 // Yes, this directive is an extension.
910 Diag(Tok, diag::ext_pp_ident_directive);
Mike Stump1eb44332009-09-09 15:08:12 +0000911
Chris Lattner141e71f2008-03-09 01:54:53 +0000912 // Read the string argument.
913 Token StrTok;
914 Lex(StrTok);
Mike Stump1eb44332009-09-09 15:08:12 +0000915
Chris Lattner141e71f2008-03-09 01:54:53 +0000916 // If the token kind isn't a string, it's a malformed directive.
917 if (StrTok.isNot(tok::string_literal) &&
Chris Lattner3692b092008-11-18 07:59:24 +0000918 StrTok.isNot(tok::wide_string_literal)) {
919 Diag(StrTok, diag::err_pp_malformed_ident);
Chris Lattner099dd052009-01-26 05:30:54 +0000920 if (StrTok.isNot(tok::eom))
921 DiscardUntilEndOfDirective();
Chris Lattner3692b092008-11-18 07:59:24 +0000922 return;
923 }
Mike Stump1eb44332009-09-09 15:08:12 +0000924
Chris Lattner141e71f2008-03-09 01:54:53 +0000925 // Verify that there is nothing after the string, other than EOM.
Chris Lattner35410d52009-04-14 05:07:49 +0000926 CheckEndOfDirective("ident");
Chris Lattner141e71f2008-03-09 01:54:53 +0000927
Douglas Gregor453091c2010-03-16 22:30:13 +0000928 if (Callbacks) {
929 bool Invalid = false;
930 std::string Str = getSpelling(StrTok, &Invalid);
931 if (!Invalid)
932 Callbacks->Ident(Tok.getLocation(), Str);
933 }
Chris Lattner141e71f2008-03-09 01:54:53 +0000934}
935
936//===----------------------------------------------------------------------===//
937// Preprocessor Include Directive Handling.
938//===----------------------------------------------------------------------===//
939
940/// GetIncludeFilenameSpelling - Turn the specified lexer token into a fully
941/// checked and spelled filename, e.g. as an operand of #include. This returns
942/// true if the input filename was in <>'s or false if it were in ""'s. The
943/// caller is expected to provide a buffer that is large enough to hold the
944/// spelling of the filename, but is also expected to handle the case when
945/// this method decides to use a different buffer.
946bool Preprocessor::GetIncludeFilenameSpelling(SourceLocation Loc,
Chris Lattnera1394812010-01-10 01:35:12 +0000947 llvm::StringRef &Buffer) {
Chris Lattner141e71f2008-03-09 01:54:53 +0000948 // Get the text form of the filename.
Chris Lattnera1394812010-01-10 01:35:12 +0000949 assert(!Buffer.empty() && "Can't have tokens with empty spellings!");
Mike Stump1eb44332009-09-09 15:08:12 +0000950
Chris Lattner141e71f2008-03-09 01:54:53 +0000951 // Make sure the filename is <x> or "x".
952 bool isAngled;
Chris Lattnera1394812010-01-10 01:35:12 +0000953 if (Buffer[0] == '<') {
954 if (Buffer.back() != '>') {
Chris Lattner141e71f2008-03-09 01:54:53 +0000955 Diag(Loc, diag::err_pp_expects_filename);
Chris Lattnera1394812010-01-10 01:35:12 +0000956 Buffer = llvm::StringRef();
Chris Lattner141e71f2008-03-09 01:54:53 +0000957 return true;
958 }
959 isAngled = true;
Chris Lattnera1394812010-01-10 01:35:12 +0000960 } else if (Buffer[0] == '"') {
961 if (Buffer.back() != '"') {
Chris Lattner141e71f2008-03-09 01:54:53 +0000962 Diag(Loc, diag::err_pp_expects_filename);
Chris Lattnera1394812010-01-10 01:35:12 +0000963 Buffer = llvm::StringRef();
Chris Lattner141e71f2008-03-09 01:54:53 +0000964 return true;
965 }
966 isAngled = false;
967 } else {
968 Diag(Loc, diag::err_pp_expects_filename);
Chris Lattnera1394812010-01-10 01:35:12 +0000969 Buffer = llvm::StringRef();
Chris Lattner141e71f2008-03-09 01:54:53 +0000970 return true;
971 }
Mike Stump1eb44332009-09-09 15:08:12 +0000972
Chris Lattner141e71f2008-03-09 01:54:53 +0000973 // Diagnose #include "" as invalid.
Chris Lattnera1394812010-01-10 01:35:12 +0000974 if (Buffer.size() <= 2) {
Chris Lattner141e71f2008-03-09 01:54:53 +0000975 Diag(Loc, diag::err_pp_empty_filename);
Chris Lattnera1394812010-01-10 01:35:12 +0000976 Buffer = llvm::StringRef();
977 return true;
Chris Lattner141e71f2008-03-09 01:54:53 +0000978 }
Mike Stump1eb44332009-09-09 15:08:12 +0000979
Chris Lattner141e71f2008-03-09 01:54:53 +0000980 // Skip the brackets.
Chris Lattnera1394812010-01-10 01:35:12 +0000981 Buffer = Buffer.substr(1, Buffer.size()-2);
Chris Lattner141e71f2008-03-09 01:54:53 +0000982 return isAngled;
983}
984
985/// ConcatenateIncludeName - Handle cases where the #include name is expanded
986/// from a macro as multiple tokens, which need to be glued together. This
987/// occurs for code like:
988/// #define FOO <a/b.h>
989/// #include FOO
990/// because in this case, "<a/b.h>" is returned as 7 tokens, not one.
991///
992/// This code concatenates and consumes tokens up to the '>' token. It returns
993/// false if the > was found, otherwise it returns true if it finds and consumes
994/// the EOM marker.
John Thompsona28cc092009-10-30 13:49:06 +0000995bool Preprocessor::ConcatenateIncludeName(
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000996 llvm::SmallString<128> &FilenameBuffer) {
Chris Lattner141e71f2008-03-09 01:54:53 +0000997 Token CurTok;
Mike Stump1eb44332009-09-09 15:08:12 +0000998
John Thompsona28cc092009-10-30 13:49:06 +0000999 Lex(CurTok);
Chris Lattner141e71f2008-03-09 01:54:53 +00001000 while (CurTok.isNot(tok::eom)) {
1001 // Append the spelling of this token to the buffer. If there was a space
1002 // before it, add it now.
1003 if (CurTok.hasLeadingSpace())
1004 FilenameBuffer.push_back(' ');
Mike Stump1eb44332009-09-09 15:08:12 +00001005
Chris Lattner141e71f2008-03-09 01:54:53 +00001006 // Get the spelling of the token, directly into FilenameBuffer if possible.
1007 unsigned PreAppendSize = FilenameBuffer.size();
1008 FilenameBuffer.resize(PreAppendSize+CurTok.getLength());
Mike Stump1eb44332009-09-09 15:08:12 +00001009
Chris Lattner141e71f2008-03-09 01:54:53 +00001010 const char *BufPtr = &FilenameBuffer[PreAppendSize];
John Thompsona28cc092009-10-30 13:49:06 +00001011 unsigned ActualLen = getSpelling(CurTok, BufPtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001012
Chris Lattner141e71f2008-03-09 01:54:53 +00001013 // If the token was spelled somewhere else, copy it into FilenameBuffer.
1014 if (BufPtr != &FilenameBuffer[PreAppendSize])
1015 memcpy(&FilenameBuffer[PreAppendSize], BufPtr, ActualLen);
Mike Stump1eb44332009-09-09 15:08:12 +00001016
Chris Lattner141e71f2008-03-09 01:54:53 +00001017 // Resize FilenameBuffer to the correct size.
1018 if (CurTok.getLength() != ActualLen)
1019 FilenameBuffer.resize(PreAppendSize+ActualLen);
Mike Stump1eb44332009-09-09 15:08:12 +00001020
Chris Lattner141e71f2008-03-09 01:54:53 +00001021 // If we found the '>' marker, return success.
1022 if (CurTok.is(tok::greater))
1023 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001024
John Thompsona28cc092009-10-30 13:49:06 +00001025 Lex(CurTok);
Chris Lattner141e71f2008-03-09 01:54:53 +00001026 }
1027
1028 // If we hit the eom marker, emit an error and return true so that the caller
1029 // knows the EOM has been read.
John Thompsona28cc092009-10-30 13:49:06 +00001030 Diag(CurTok.getLocation(), diag::err_pp_expects_filename);
Chris Lattner141e71f2008-03-09 01:54:53 +00001031 return true;
1032}
1033
1034/// HandleIncludeDirective - The "#include" tokens have just been read, read the
1035/// file to be included from the lexer, then include it! This is a common
1036/// routine with functionality shared between #include, #include_next and
Chris Lattner72181832008-09-26 20:12:23 +00001037/// #import. LookupFrom is set when this is a #include_next directive, it
Mike Stump1eb44332009-09-09 15:08:12 +00001038/// specifies the file to start searching from.
Chris Lattner141e71f2008-03-09 01:54:53 +00001039void Preprocessor::HandleIncludeDirective(Token &IncludeTok,
1040 const DirectoryLookup *LookupFrom,
1041 bool isImport) {
1042
1043 Token FilenameTok;
Ted Kremenek60e45d42008-11-18 00:34:22 +00001044 CurPPLexer->LexIncludeFilename(FilenameTok);
Mike Stump1eb44332009-09-09 15:08:12 +00001045
Chris Lattner141e71f2008-03-09 01:54:53 +00001046 // Reserve a buffer to get the spelling.
Chris Lattnera1394812010-01-10 01:35:12 +00001047 llvm::SmallString<128> FilenameBuffer;
1048 llvm::StringRef Filename;
Chris Lattner141e71f2008-03-09 01:54:53 +00001049
1050 switch (FilenameTok.getKind()) {
1051 case tok::eom:
1052 // If the token kind is EOM, the error has already been diagnosed.
1053 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001054
Chris Lattner141e71f2008-03-09 01:54:53 +00001055 case tok::angle_string_literal:
Benjamin Kramerddeea562010-02-27 13:44:12 +00001056 case tok::string_literal:
1057 Filename = getSpelling(FilenameTok, FilenameBuffer);
Chris Lattner141e71f2008-03-09 01:54:53 +00001058 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001059
Chris Lattner141e71f2008-03-09 01:54:53 +00001060 case tok::less:
1061 // This could be a <foo/bar.h> file coming from a macro expansion. In this
1062 // case, glue the tokens together into FilenameBuffer and interpret those.
1063 FilenameBuffer.push_back('<');
John Thompsona28cc092009-10-30 13:49:06 +00001064 if (ConcatenateIncludeName(FilenameBuffer))
Chris Lattner141e71f2008-03-09 01:54:53 +00001065 return; // Found <eom> but no ">"? Diagnostic already emitted.
Chris Lattnera1394812010-01-10 01:35:12 +00001066 Filename = FilenameBuffer.str();
Chris Lattner141e71f2008-03-09 01:54:53 +00001067 break;
1068 default:
1069 Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
1070 DiscardUntilEndOfDirective();
1071 return;
1072 }
Mike Stump1eb44332009-09-09 15:08:12 +00001073
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001074 bool isAngled =
Chris Lattnera1394812010-01-10 01:35:12 +00001075 GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
Chris Lattner141e71f2008-03-09 01:54:53 +00001076 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
1077 // error.
Chris Lattnera1394812010-01-10 01:35:12 +00001078 if (Filename.empty()) {
Chris Lattner141e71f2008-03-09 01:54:53 +00001079 DiscardUntilEndOfDirective();
1080 return;
1081 }
Mike Stump1eb44332009-09-09 15:08:12 +00001082
Chris Lattner9cb51ce2009-04-17 23:56:52 +00001083 // Verify that there is nothing after the filename, other than EOM. Note that
1084 // we allow macros that expand to nothing after the filename, because this
1085 // falls into the category of "#include pp-tokens new-line" specified in
1086 // C99 6.10.2p4.
Daniel Dunbare013d682009-10-18 20:26:12 +00001087 CheckEndOfDirective(IncludeTok.getIdentifierInfo()->getNameStart(), true);
Chris Lattner141e71f2008-03-09 01:54:53 +00001088
1089 // Check that we don't have infinite #include recursion.
Chris Lattner3692b092008-11-18 07:59:24 +00001090 if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1) {
1091 Diag(FilenameTok, diag::err_pp_include_too_deep);
1092 return;
1093 }
Mike Stump1eb44332009-09-09 15:08:12 +00001094
Chris Lattner141e71f2008-03-09 01:54:53 +00001095 // Search include directories.
1096 const DirectoryLookup *CurDir;
Chris Lattnerf45b6462010-01-22 00:14:44 +00001097 const FileEntry *File = LookupFile(Filename, isAngled, LookupFrom, CurDir);
Chris Lattner3692b092008-11-18 07:59:24 +00001098 if (File == 0) {
Chris Lattnera1394812010-01-10 01:35:12 +00001099 Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
Chris Lattner3692b092008-11-18 07:59:24 +00001100 return;
1101 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001102
Chris Lattner72181832008-09-26 20:12:23 +00001103 // The #included file will be considered to be a system header if either it is
1104 // in a system include directory, or if the #includer is a system include
1105 // header.
Mike Stump1eb44332009-09-09 15:08:12 +00001106 SrcMgr::CharacteristicKind FileCharacter =
Chris Lattner0b9e7362008-09-26 21:18:42 +00001107 std::max(HeaderInfo.getFileDirFlavor(File),
Chris Lattner693faa62009-01-19 07:59:15 +00001108 SourceMgr.getFileCharacteristic(FilenameTok.getLocation()));
Mike Stump1eb44332009-09-09 15:08:12 +00001109
Chris Lattner6fbe3eb2010-04-19 20:44:31 +00001110 // Ask HeaderInfo if we should enter this #include file. If not, #including
1111 // this file will have no effect.
1112 if (!HeaderInfo.ShouldEnterIncludeFile(File, isImport)) {
Chris Lattnere127a0d2010-04-20 20:35:58 +00001113 if (Callbacks)
Chris Lattner6fbe3eb2010-04-19 20:44:31 +00001114 Callbacks->FileSkipped(*File, FilenameTok, FileCharacter);
Chris Lattner6fbe3eb2010-04-19 20:44:31 +00001115 return;
1116 }
1117
Chris Lattner141e71f2008-03-09 01:54:53 +00001118 // Look up the file, create a File ID for it.
Chris Lattner2b2453a2009-01-17 06:22:33 +00001119 FileID FID = SourceMgr.createFileID(File, FilenameTok.getLocation(),
1120 FileCharacter);
1121 if (FID.isInvalid()) {
Chris Lattnera1394812010-01-10 01:35:12 +00001122 Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
Chris Lattner56b05c82008-11-18 08:02:48 +00001123 return;
1124 }
Chris Lattner141e71f2008-03-09 01:54:53 +00001125
1126 // Finally, if all is good, enter the new file!
Chris Lattnere127a0d2010-04-20 20:35:58 +00001127 EnterSourceFile(FID, CurDir, FilenameTok.getLocation());
Chris Lattner141e71f2008-03-09 01:54:53 +00001128}
1129
1130/// HandleIncludeNextDirective - Implements #include_next.
1131///
1132void Preprocessor::HandleIncludeNextDirective(Token &IncludeNextTok) {
1133 Diag(IncludeNextTok, diag::ext_pp_include_next_directive);
Mike Stump1eb44332009-09-09 15:08:12 +00001134
Chris Lattner141e71f2008-03-09 01:54:53 +00001135 // #include_next is like #include, except that we start searching after
1136 // the current found directory. If we can't do this, issue a
1137 // diagnostic.
1138 const DirectoryLookup *Lookup = CurDirLookup;
1139 if (isInPrimaryFile()) {
1140 Lookup = 0;
1141 Diag(IncludeNextTok, diag::pp_include_next_in_primary);
1142 } else if (Lookup == 0) {
1143 Diag(IncludeNextTok, diag::pp_include_next_absolute_path);
1144 } else {
1145 // Start looking up in the next directory.
1146 ++Lookup;
1147 }
Mike Stump1eb44332009-09-09 15:08:12 +00001148
Chris Lattner141e71f2008-03-09 01:54:53 +00001149 return HandleIncludeDirective(IncludeNextTok, Lookup);
1150}
1151
1152/// HandleImportDirective - Implements #import.
1153///
1154void Preprocessor::HandleImportDirective(Token &ImportTok) {
Chris Lattnerb627c8d2009-03-06 04:28:03 +00001155 if (!Features.ObjC1) // #import is standard for ObjC.
1156 Diag(ImportTok, diag::ext_pp_import_directive);
Mike Stump1eb44332009-09-09 15:08:12 +00001157
Chris Lattner141e71f2008-03-09 01:54:53 +00001158 return HandleIncludeDirective(ImportTok, 0, true);
1159}
1160
Chris Lattnerde076652009-04-08 18:46:40 +00001161/// HandleIncludeMacrosDirective - The -imacros command line option turns into a
1162/// pseudo directive in the predefines buffer. This handles it by sucking all
1163/// tokens through the preprocessor and discarding them (only keeping the side
1164/// effects on the preprocessor).
1165void Preprocessor::HandleIncludeMacrosDirective(Token &IncludeMacrosTok) {
1166 // This directive should only occur in the predefines buffer. If not, emit an
1167 // error and reject it.
1168 SourceLocation Loc = IncludeMacrosTok.getLocation();
1169 if (strcmp(SourceMgr.getBufferName(Loc), "<built-in>") != 0) {
1170 Diag(IncludeMacrosTok.getLocation(),
1171 diag::pp_include_macros_out_of_predefines);
1172 DiscardUntilEndOfDirective();
1173 return;
1174 }
Mike Stump1eb44332009-09-09 15:08:12 +00001175
Chris Lattnerfd105112009-04-08 20:53:24 +00001176 // Treat this as a normal #include for checking purposes. If this is
1177 // successful, it will push a new lexer onto the include stack.
1178 HandleIncludeDirective(IncludeMacrosTok, 0, false);
Mike Stump1eb44332009-09-09 15:08:12 +00001179
Chris Lattnerfd105112009-04-08 20:53:24 +00001180 Token TmpTok;
1181 do {
1182 Lex(TmpTok);
1183 assert(TmpTok.isNot(tok::eof) && "Didn't find end of -imacros!");
1184 } while (TmpTok.isNot(tok::hashhash));
Chris Lattnerde076652009-04-08 18:46:40 +00001185}
1186
Chris Lattner141e71f2008-03-09 01:54:53 +00001187//===----------------------------------------------------------------------===//
1188// Preprocessor Macro Directive Handling.
1189//===----------------------------------------------------------------------===//
1190
1191/// ReadMacroDefinitionArgList - The ( starting an argument list of a macro
1192/// definition has just been read. Lex the rest of the arguments and the
1193/// closing ), updating MI with what we learn. Return true if an error occurs
1194/// parsing the arg list.
1195bool Preprocessor::ReadMacroDefinitionArgList(MacroInfo *MI) {
1196 llvm::SmallVector<IdentifierInfo*, 32> Arguments;
Mike Stump1eb44332009-09-09 15:08:12 +00001197
Chris Lattner141e71f2008-03-09 01:54:53 +00001198 Token Tok;
1199 while (1) {
1200 LexUnexpandedToken(Tok);
1201 switch (Tok.getKind()) {
1202 case tok::r_paren:
1203 // Found the end of the argument list.
Chris Lattnercf29e072009-02-20 22:31:31 +00001204 if (Arguments.empty()) // #define FOO()
Chris Lattner141e71f2008-03-09 01:54:53 +00001205 return false;
Chris Lattner141e71f2008-03-09 01:54:53 +00001206 // Otherwise we have #define FOO(A,)
1207 Diag(Tok, diag::err_pp_expected_ident_in_arg_list);
1208 return true;
1209 case tok::ellipsis: // #define X(... -> C99 varargs
1210 // Warn if use of C99 feature in non-C99 mode.
1211 if (!Features.C99) Diag(Tok, diag::ext_variadic_macro);
1212
1213 // Lex the token after the identifier.
1214 LexUnexpandedToken(Tok);
1215 if (Tok.isNot(tok::r_paren)) {
1216 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
1217 return true;
1218 }
1219 // Add the __VA_ARGS__ identifier as an argument.
1220 Arguments.push_back(Ident__VA_ARGS__);
1221 MI->setIsC99Varargs();
Chris Lattner685befe2009-02-20 22:46:43 +00001222 MI->setArgumentList(&Arguments[0], Arguments.size(), BP);
Chris Lattner141e71f2008-03-09 01:54:53 +00001223 return false;
1224 case tok::eom: // #define X(
1225 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
1226 return true;
1227 default:
1228 // Handle keywords and identifiers here to accept things like
1229 // #define Foo(for) for.
1230 IdentifierInfo *II = Tok.getIdentifierInfo();
1231 if (II == 0) {
1232 // #define X(1
1233 Diag(Tok, diag::err_pp_invalid_tok_in_arg_list);
1234 return true;
1235 }
1236
1237 // If this is already used as an argument, it is used multiple times (e.g.
1238 // #define X(A,A.
Mike Stump1eb44332009-09-09 15:08:12 +00001239 if (std::find(Arguments.begin(), Arguments.end(), II) !=
Chris Lattner141e71f2008-03-09 01:54:53 +00001240 Arguments.end()) { // C99 6.10.3p6
Chris Lattner6cf3ed72008-11-19 07:33:58 +00001241 Diag(Tok, diag::err_pp_duplicate_name_in_arg_list) << II;
Chris Lattner141e71f2008-03-09 01:54:53 +00001242 return true;
1243 }
Mike Stump1eb44332009-09-09 15:08:12 +00001244
Chris Lattner141e71f2008-03-09 01:54:53 +00001245 // Add the argument to the macro info.
1246 Arguments.push_back(II);
Mike Stump1eb44332009-09-09 15:08:12 +00001247
Chris Lattner141e71f2008-03-09 01:54:53 +00001248 // Lex the token after the identifier.
1249 LexUnexpandedToken(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +00001250
Chris Lattner141e71f2008-03-09 01:54:53 +00001251 switch (Tok.getKind()) {
1252 default: // #define X(A B
1253 Diag(Tok, diag::err_pp_expected_comma_in_arg_list);
1254 return true;
1255 case tok::r_paren: // #define X(A)
Chris Lattner685befe2009-02-20 22:46:43 +00001256 MI->setArgumentList(&Arguments[0], Arguments.size(), BP);
Chris Lattner141e71f2008-03-09 01:54:53 +00001257 return false;
1258 case tok::comma: // #define X(A,
1259 break;
1260 case tok::ellipsis: // #define X(A... -> GCC extension
1261 // Diagnose extension.
1262 Diag(Tok, diag::ext_named_variadic_macro);
Mike Stump1eb44332009-09-09 15:08:12 +00001263
Chris Lattner141e71f2008-03-09 01:54:53 +00001264 // Lex the token after the identifier.
1265 LexUnexpandedToken(Tok);
1266 if (Tok.isNot(tok::r_paren)) {
1267 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
1268 return true;
1269 }
Mike Stump1eb44332009-09-09 15:08:12 +00001270
Chris Lattner141e71f2008-03-09 01:54:53 +00001271 MI->setIsGNUVarargs();
Chris Lattner685befe2009-02-20 22:46:43 +00001272 MI->setArgumentList(&Arguments[0], Arguments.size(), BP);
Chris Lattner141e71f2008-03-09 01:54:53 +00001273 return false;
1274 }
1275 }
1276 }
1277}
1278
1279/// HandleDefineDirective - Implements #define. This consumes the entire macro
1280/// line then lets the caller lex the next real token.
1281void Preprocessor::HandleDefineDirective(Token &DefineTok) {
1282 ++NumDefined;
1283
1284 Token MacroNameTok;
1285 ReadMacroName(MacroNameTok, 1);
Mike Stump1eb44332009-09-09 15:08:12 +00001286
Chris Lattner141e71f2008-03-09 01:54:53 +00001287 // Error reading macro name? If so, diagnostic already issued.
1288 if (MacroNameTok.is(tok::eom))
1289 return;
1290
Chris Lattner2451b522009-04-21 04:46:33 +00001291 Token LastTok = MacroNameTok;
1292
Chris Lattner141e71f2008-03-09 01:54:53 +00001293 // If we are supposed to keep comments in #defines, reenable comment saving
1294 // mode.
Ted Kremenekac6b06d2008-11-18 00:43:07 +00001295 if (CurLexer) CurLexer->SetCommentRetentionState(KeepMacroComments);
Mike Stump1eb44332009-09-09 15:08:12 +00001296
Chris Lattner141e71f2008-03-09 01:54:53 +00001297 // Create the new macro.
Ted Kremenek0ea76722008-12-15 19:56:42 +00001298 MacroInfo *MI = AllocateMacroInfo(MacroNameTok.getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001299
Chris Lattner141e71f2008-03-09 01:54:53 +00001300 Token Tok;
1301 LexUnexpandedToken(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +00001302
Chris Lattner141e71f2008-03-09 01:54:53 +00001303 // If this is a function-like macro definition, parse the argument list,
1304 // marking each of the identifiers as being used as macro arguments. Also,
1305 // check other constraints on the first token of the macro body.
1306 if (Tok.is(tok::eom)) {
1307 // If there is no body to this macro, we have no special handling here.
Chris Lattner6272bcf2009-04-18 02:23:25 +00001308 } else if (Tok.hasLeadingSpace()) {
1309 // This is a normal token with leading space. Clear the leading space
1310 // marker on the first token to get proper expansion.
1311 Tok.clearFlag(Token::LeadingSpace);
1312 } else if (Tok.is(tok::l_paren)) {
Chris Lattner141e71f2008-03-09 01:54:53 +00001313 // This is a function-like macro definition. Read the argument list.
1314 MI->setIsFunctionLike();
1315 if (ReadMacroDefinitionArgList(MI)) {
1316 // Forget about MI.
Ted Kremenek0ea76722008-12-15 19:56:42 +00001317 ReleaseMacroInfo(MI);
Chris Lattner141e71f2008-03-09 01:54:53 +00001318 // Throw away the rest of the line.
Ted Kremenek60e45d42008-11-18 00:34:22 +00001319 if (CurPPLexer->ParsingPreprocessorDirective)
Chris Lattner141e71f2008-03-09 01:54:53 +00001320 DiscardUntilEndOfDirective();
1321 return;
1322 }
1323
Chris Lattner8fde5972009-04-19 18:26:34 +00001324 // If this is a definition of a variadic C99 function-like macro, not using
1325 // the GNU named varargs extension, enabled __VA_ARGS__.
Mike Stump1eb44332009-09-09 15:08:12 +00001326
Chris Lattner8fde5972009-04-19 18:26:34 +00001327 // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro.
1328 // This gets unpoisoned where it is allowed.
1329 assert(Ident__VA_ARGS__->isPoisoned() && "__VA_ARGS__ should be poisoned!");
1330 if (MI->isC99Varargs())
1331 Ident__VA_ARGS__->setIsPoisoned(false);
Mike Stump1eb44332009-09-09 15:08:12 +00001332
Chris Lattner141e71f2008-03-09 01:54:53 +00001333 // Read the first token after the arg list for down below.
1334 LexUnexpandedToken(Tok);
Chris Lattner6272bcf2009-04-18 02:23:25 +00001335 } else if (Features.C99) {
Chris Lattner141e71f2008-03-09 01:54:53 +00001336 // C99 requires whitespace between the macro definition and the body. Emit
1337 // a diagnostic for something like "#define X+".
Chris Lattner6272bcf2009-04-18 02:23:25 +00001338 Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name);
Chris Lattner141e71f2008-03-09 01:54:53 +00001339 } else {
Chris Lattner6272bcf2009-04-18 02:23:25 +00001340 // C90 6.8 TC1 says: "In the definition of an object-like macro, if the
1341 // first character of a replacement list is not a character required by
1342 // subclause 5.2.1, then there shall be white-space separation between the
1343 // identifier and the replacement list.". 5.2.1 lists this set:
1344 // "A-Za-z0-9!"#%&'()*+,_./:;<=>?[\]^_{|}~" as well as whitespace, which
1345 // is irrelevant here.
1346 bool isInvalid = false;
1347 if (Tok.is(tok::at)) // @ is not in the list above.
1348 isInvalid = true;
1349 else if (Tok.is(tok::unknown)) {
1350 // If we have an unknown token, it is something strange like "`". Since
1351 // all of valid characters would have lexed into a single character
1352 // token of some sort, we know this is not a valid case.
1353 isInvalid = true;
1354 }
1355 if (isInvalid)
1356 Diag(Tok, diag::ext_missing_whitespace_after_macro_name);
1357 else
1358 Diag(Tok, diag::warn_missing_whitespace_after_macro_name);
Chris Lattner141e71f2008-03-09 01:54:53 +00001359 }
Chris Lattner2451b522009-04-21 04:46:33 +00001360
1361 if (!Tok.is(tok::eom))
1362 LastTok = Tok;
1363
Chris Lattner141e71f2008-03-09 01:54:53 +00001364 // Read the rest of the macro body.
1365 if (MI->isObjectLike()) {
1366 // Object-like macros are very simple, just read their body.
1367 while (Tok.isNot(tok::eom)) {
Chris Lattner2451b522009-04-21 04:46:33 +00001368 LastTok = Tok;
Chris Lattner141e71f2008-03-09 01:54:53 +00001369 MI->AddTokenToBody(Tok);
1370 // Get the next token of the macro.
1371 LexUnexpandedToken(Tok);
1372 }
Mike Stump1eb44332009-09-09 15:08:12 +00001373
Chris Lattner141e71f2008-03-09 01:54:53 +00001374 } else {
Chris Lattner32404692009-05-25 17:16:10 +00001375 // Otherwise, read the body of a function-like macro. While we are at it,
1376 // check C99 6.10.3.2p1: ensure that # operators are followed by macro
1377 // parameters in function-like macro expansions.
Chris Lattner141e71f2008-03-09 01:54:53 +00001378 while (Tok.isNot(tok::eom)) {
Chris Lattner2451b522009-04-21 04:46:33 +00001379 LastTok = Tok;
Chris Lattner141e71f2008-03-09 01:54:53 +00001380
Chris Lattner141e71f2008-03-09 01:54:53 +00001381 if (Tok.isNot(tok::hash)) {
Chris Lattner32404692009-05-25 17:16:10 +00001382 MI->AddTokenToBody(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +00001383
Chris Lattner141e71f2008-03-09 01:54:53 +00001384 // Get the next token of the macro.
1385 LexUnexpandedToken(Tok);
1386 continue;
1387 }
Mike Stump1eb44332009-09-09 15:08:12 +00001388
Chris Lattner141e71f2008-03-09 01:54:53 +00001389 // Get the next token of the macro.
1390 LexUnexpandedToken(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +00001391
Chris Lattner32404692009-05-25 17:16:10 +00001392 // Check for a valid macro arg identifier.
1393 if (Tok.getIdentifierInfo() == 0 ||
1394 MI->getArgumentNum(Tok.getIdentifierInfo()) == -1) {
1395
1396 // If this is assembler-with-cpp mode, we accept random gibberish after
1397 // the '#' because '#' is often a comment character. However, change
1398 // the kind of the token to tok::unknown so that the preprocessor isn't
1399 // confused.
1400 if (getLangOptions().AsmPreprocessor && Tok.isNot(tok::eom)) {
1401 LastTok.setKind(tok::unknown);
1402 } else {
1403 Diag(Tok, diag::err_pp_stringize_not_parameter);
1404 ReleaseMacroInfo(MI);
Mike Stump1eb44332009-09-09 15:08:12 +00001405
Chris Lattner32404692009-05-25 17:16:10 +00001406 // Disable __VA_ARGS__ again.
1407 Ident__VA_ARGS__->setIsPoisoned(true);
1408 return;
1409 }
Chris Lattner141e71f2008-03-09 01:54:53 +00001410 }
Mike Stump1eb44332009-09-09 15:08:12 +00001411
Chris Lattner32404692009-05-25 17:16:10 +00001412 // Things look ok, add the '#' and param name tokens to the macro.
1413 MI->AddTokenToBody(LastTok);
Chris Lattner141e71f2008-03-09 01:54:53 +00001414 MI->AddTokenToBody(Tok);
Chris Lattner32404692009-05-25 17:16:10 +00001415 LastTok = Tok;
Mike Stump1eb44332009-09-09 15:08:12 +00001416
Chris Lattner141e71f2008-03-09 01:54:53 +00001417 // Get the next token of the macro.
1418 LexUnexpandedToken(Tok);
1419 }
1420 }
Mike Stump1eb44332009-09-09 15:08:12 +00001421
1422
Chris Lattner141e71f2008-03-09 01:54:53 +00001423 // Disable __VA_ARGS__ again.
1424 Ident__VA_ARGS__->setIsPoisoned(true);
1425
1426 // Check that there is no paste (##) operator at the begining or end of the
1427 // replacement list.
1428 unsigned NumTokens = MI->getNumTokens();
1429 if (NumTokens != 0) {
1430 if (MI->getReplacementToken(0).is(tok::hashhash)) {
1431 Diag(MI->getReplacementToken(0), diag::err_paste_at_start);
Ted Kremenek0ea76722008-12-15 19:56:42 +00001432 ReleaseMacroInfo(MI);
Chris Lattner141e71f2008-03-09 01:54:53 +00001433 return;
1434 }
1435 if (MI->getReplacementToken(NumTokens-1).is(tok::hashhash)) {
1436 Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end);
Ted Kremenek0ea76722008-12-15 19:56:42 +00001437 ReleaseMacroInfo(MI);
Chris Lattner141e71f2008-03-09 01:54:53 +00001438 return;
1439 }
1440 }
Mike Stump1eb44332009-09-09 15:08:12 +00001441
Chris Lattner141e71f2008-03-09 01:54:53 +00001442 // If this is the primary source file, remember that this macro hasn't been
1443 // used yet.
1444 if (isInPrimaryFile())
1445 MI->setIsUsed(false);
Chris Lattner2451b522009-04-21 04:46:33 +00001446
1447 MI->setDefinitionEndLoc(LastTok.getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001448
Chris Lattner141e71f2008-03-09 01:54:53 +00001449 // Finally, if this identifier already had a macro defined for it, verify that
1450 // the macro bodies are identical and free the old definition.
1451 if (MacroInfo *OtherMI = getMacroInfo(MacroNameTok.getIdentifierInfo())) {
Chris Lattner41c3ae12009-01-16 19:50:11 +00001452 // It is very common for system headers to have tons of macro redefinitions
1453 // and for warnings to be disabled in system headers. If this is the case,
1454 // then don't bother calling MacroInfo::isIdenticalTo.
Chris Lattner7f549df2009-03-13 21:17:23 +00001455 if (!getDiagnostics().getSuppressSystemWarnings() ||
Chris Lattner41c3ae12009-01-16 19:50:11 +00001456 !SourceMgr.isInSystemHeader(DefineTok.getLocation())) {
1457 if (!OtherMI->isUsed())
1458 Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used);
Chris Lattner141e71f2008-03-09 01:54:53 +00001459
Chris Lattnerf47724b2010-08-17 15:55:45 +00001460 // Macros must be identical. This means all tokens and whitespace
Chris Lattner41c3ae12009-01-16 19:50:11 +00001461 // separation must be the same. C99 6.10.3.2.
Chris Lattnerf47724b2010-08-17 15:55:45 +00001462 if (!OtherMI->isAllowRedefinitionsWithoutWarning() &&
Eli Friedmana7e68452010-08-22 01:00:03 +00001463 !MI->isIdenticalTo(*OtherMI, *this)) {
Chris Lattner41c3ae12009-01-16 19:50:11 +00001464 Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef)
1465 << MacroNameTok.getIdentifierInfo();
1466 Diag(OtherMI->getDefinitionLoc(), diag::note_previous_definition);
1467 }
Chris Lattner141e71f2008-03-09 01:54:53 +00001468 }
Ted Kremenek0ea76722008-12-15 19:56:42 +00001469 ReleaseMacroInfo(OtherMI);
Chris Lattner141e71f2008-03-09 01:54:53 +00001470 }
Mike Stump1eb44332009-09-09 15:08:12 +00001471
Chris Lattner141e71f2008-03-09 01:54:53 +00001472 setMacroInfo(MacroNameTok.getIdentifierInfo(), MI);
Mike Stump1eb44332009-09-09 15:08:12 +00001473
Chris Lattnerf4a72b02009-04-12 01:39:54 +00001474 // If the callbacks want to know, tell them about the macro definition.
1475 if (Callbacks)
1476 Callbacks->MacroDefined(MacroNameTok.getIdentifierInfo(), MI);
Chris Lattner141e71f2008-03-09 01:54:53 +00001477}
1478
1479/// HandleUndefDirective - Implements #undef.
1480///
1481void Preprocessor::HandleUndefDirective(Token &UndefTok) {
1482 ++NumUndefined;
1483
1484 Token MacroNameTok;
1485 ReadMacroName(MacroNameTok, 2);
Mike Stump1eb44332009-09-09 15:08:12 +00001486
Chris Lattner141e71f2008-03-09 01:54:53 +00001487 // Error reading macro name? If so, diagnostic already issued.
1488 if (MacroNameTok.is(tok::eom))
1489 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001490
Chris Lattner141e71f2008-03-09 01:54:53 +00001491 // Check to see if this is the last token on the #undef line.
Chris Lattner35410d52009-04-14 05:07:49 +00001492 CheckEndOfDirective("undef");
Mike Stump1eb44332009-09-09 15:08:12 +00001493
Chris Lattner141e71f2008-03-09 01:54:53 +00001494 // Okay, we finally have a valid identifier to undef.
1495 MacroInfo *MI = getMacroInfo(MacroNameTok.getIdentifierInfo());
Mike Stump1eb44332009-09-09 15:08:12 +00001496
Chris Lattner141e71f2008-03-09 01:54:53 +00001497 // If the macro is not defined, this is a noop undef, just return.
1498 if (MI == 0) return;
1499
1500 if (!MI->isUsed())
1501 Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used);
Chris Lattner41c17472009-04-21 03:42:09 +00001502
1503 // If the callbacks want to know, tell them about the macro #undef.
1504 if (Callbacks)
Benjamin Kramer2f054492010-08-07 22:27:00 +00001505 Callbacks->MacroUndefined(MacroNameTok.getLocation(),
1506 MacroNameTok.getIdentifierInfo(), MI);
Chris Lattner41c17472009-04-21 03:42:09 +00001507
Chris Lattner141e71f2008-03-09 01:54:53 +00001508 // Free macro definition.
Ted Kremenek0ea76722008-12-15 19:56:42 +00001509 ReleaseMacroInfo(MI);
Chris Lattner141e71f2008-03-09 01:54:53 +00001510 setMacroInfo(MacroNameTok.getIdentifierInfo(), 0);
1511}
1512
1513
1514//===----------------------------------------------------------------------===//
1515// Preprocessor Conditional Directive Handling.
1516//===----------------------------------------------------------------------===//
1517
1518/// HandleIfdefDirective - Implements the #ifdef/#ifndef directive. isIfndef is
1519/// true when this is a #ifndef directive. ReadAnyTokensBeforeDirective is true
1520/// if any tokens have been returned or pp-directives activated before this
1521/// #ifndef has been lexed.
1522///
1523void Preprocessor::HandleIfdefDirective(Token &Result, bool isIfndef,
1524 bool ReadAnyTokensBeforeDirective) {
1525 ++NumIf;
1526 Token DirectiveTok = Result;
1527
1528 Token MacroNameTok;
1529 ReadMacroName(MacroNameTok);
Mike Stump1eb44332009-09-09 15:08:12 +00001530
Chris Lattner141e71f2008-03-09 01:54:53 +00001531 // Error reading macro name? If so, diagnostic already issued.
1532 if (MacroNameTok.is(tok::eom)) {
1533 // Skip code until we get to #endif. This helps with recovery by not
1534 // emitting an error when the #endif is reached.
1535 SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
1536 /*Foundnonskip*/false, /*FoundElse*/false);
1537 return;
1538 }
Mike Stump1eb44332009-09-09 15:08:12 +00001539
Chris Lattner141e71f2008-03-09 01:54:53 +00001540 // Check to see if this is the last token on the #if[n]def line.
Chris Lattner35410d52009-04-14 05:07:49 +00001541 CheckEndOfDirective(isIfndef ? "ifndef" : "ifdef");
Chris Lattner141e71f2008-03-09 01:54:53 +00001542
Chris Lattner13d283d2010-02-12 08:03:27 +00001543 IdentifierInfo *MII = MacroNameTok.getIdentifierInfo();
1544 MacroInfo *MI = getMacroInfo(MII);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001545
Ted Kremenek60e45d42008-11-18 00:34:22 +00001546 if (CurPPLexer->getConditionalStackDepth() == 0) {
Chris Lattner13d283d2010-02-12 08:03:27 +00001547 // If the start of a top-level #ifdef and if the macro is not defined,
1548 // inform MIOpt that this might be the start of a proper include guard.
1549 // Otherwise it is some other form of unknown conditional which we can't
1550 // handle.
1551 if (!ReadAnyTokensBeforeDirective && MI == 0) {
Chris Lattner141e71f2008-03-09 01:54:53 +00001552 assert(isIfndef && "#ifdef shouldn't reach here");
Chris Lattner13d283d2010-02-12 08:03:27 +00001553 CurPPLexer->MIOpt.EnterTopLevelIFNDEF(MII);
Chris Lattner141e71f2008-03-09 01:54:53 +00001554 } else
Ted Kremenek60e45d42008-11-18 00:34:22 +00001555 CurPPLexer->MIOpt.EnterTopLevelConditional();
Chris Lattner141e71f2008-03-09 01:54:53 +00001556 }
1557
Chris Lattner141e71f2008-03-09 01:54:53 +00001558 // If there is a macro, process it.
1559 if (MI) // Mark it used.
1560 MI->setIsUsed(true);
Mike Stump1eb44332009-09-09 15:08:12 +00001561
Chris Lattner141e71f2008-03-09 01:54:53 +00001562 // Should we include the stuff contained by this directive?
1563 if (!MI == isIfndef) {
1564 // Yes, remember that we are inside a conditional, then lex the next token.
Chris Lattner1d9c54d2009-12-14 04:54:40 +00001565 CurPPLexer->pushConditionalLevel(DirectiveTok.getLocation(),
1566 /*wasskip*/false, /*foundnonskip*/true,
1567 /*foundelse*/false);
Chris Lattner141e71f2008-03-09 01:54:53 +00001568 } else {
1569 // No, skip the contents of this block and return the first token after it.
1570 SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +00001571 /*Foundnonskip*/false,
Chris Lattner141e71f2008-03-09 01:54:53 +00001572 /*FoundElse*/false);
1573 }
1574}
1575
1576/// HandleIfDirective - Implements the #if directive.
1577///
1578void Preprocessor::HandleIfDirective(Token &IfToken,
1579 bool ReadAnyTokensBeforeDirective) {
1580 ++NumIf;
Mike Stump1eb44332009-09-09 15:08:12 +00001581
Chris Lattner141e71f2008-03-09 01:54:53 +00001582 // Parse and evaluation the conditional expression.
1583 IdentifierInfo *IfNDefMacro = 0;
1584 bool ConditionalTrue = EvaluateDirectiveExpression(IfNDefMacro);
Mike Stump1eb44332009-09-09 15:08:12 +00001585
Nuno Lopes0049db62008-06-01 18:31:24 +00001586
1587 // If this condition is equivalent to #ifndef X, and if this is the first
1588 // directive seen, handle it for the multiple-include optimization.
Ted Kremenek60e45d42008-11-18 00:34:22 +00001589 if (CurPPLexer->getConditionalStackDepth() == 0) {
Chris Lattner13d283d2010-02-12 08:03:27 +00001590 if (!ReadAnyTokensBeforeDirective && IfNDefMacro && ConditionalTrue)
Ted Kremenek60e45d42008-11-18 00:34:22 +00001591 CurPPLexer->MIOpt.EnterTopLevelIFNDEF(IfNDefMacro);
Nuno Lopes0049db62008-06-01 18:31:24 +00001592 else
Ted Kremenek60e45d42008-11-18 00:34:22 +00001593 CurPPLexer->MIOpt.EnterTopLevelConditional();
Nuno Lopes0049db62008-06-01 18:31:24 +00001594 }
1595
Chris Lattner141e71f2008-03-09 01:54:53 +00001596 // Should we include the stuff contained by this directive?
1597 if (ConditionalTrue) {
Chris Lattner141e71f2008-03-09 01:54:53 +00001598 // Yes, remember that we are inside a conditional, then lex the next token.
Ted Kremenek60e45d42008-11-18 00:34:22 +00001599 CurPPLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
Chris Lattner141e71f2008-03-09 01:54:53 +00001600 /*foundnonskip*/true, /*foundelse*/false);
1601 } else {
1602 // No, skip the contents of this block and return the first token after it.
Mike Stump1eb44332009-09-09 15:08:12 +00001603 SkipExcludedConditionalBlock(IfToken.getLocation(), /*Foundnonskip*/false,
Chris Lattner141e71f2008-03-09 01:54:53 +00001604 /*FoundElse*/false);
1605 }
1606}
1607
1608/// HandleEndifDirective - Implements the #endif directive.
1609///
1610void Preprocessor::HandleEndifDirective(Token &EndifToken) {
1611 ++NumEndif;
Mike Stump1eb44332009-09-09 15:08:12 +00001612
Chris Lattner141e71f2008-03-09 01:54:53 +00001613 // Check that this is the whole directive.
Chris Lattner35410d52009-04-14 05:07:49 +00001614 CheckEndOfDirective("endif");
Mike Stump1eb44332009-09-09 15:08:12 +00001615
Chris Lattner141e71f2008-03-09 01:54:53 +00001616 PPConditionalInfo CondInfo;
Ted Kremenek60e45d42008-11-18 00:34:22 +00001617 if (CurPPLexer->popConditionalLevel(CondInfo)) {
Chris Lattner141e71f2008-03-09 01:54:53 +00001618 // No conditionals on the stack: this is an #endif without an #if.
Chris Lattner3692b092008-11-18 07:59:24 +00001619 Diag(EndifToken, diag::err_pp_endif_without_if);
1620 return;
Chris Lattner141e71f2008-03-09 01:54:53 +00001621 }
Mike Stump1eb44332009-09-09 15:08:12 +00001622
Chris Lattner141e71f2008-03-09 01:54:53 +00001623 // If this the end of a top-level #endif, inform MIOpt.
Ted Kremenek60e45d42008-11-18 00:34:22 +00001624 if (CurPPLexer->getConditionalStackDepth() == 0)
1625 CurPPLexer->MIOpt.ExitTopLevelConditional();
Mike Stump1eb44332009-09-09 15:08:12 +00001626
Ted Kremenek60e45d42008-11-18 00:34:22 +00001627 assert(!CondInfo.WasSkipping && !CurPPLexer->LexingRawMode &&
Chris Lattner141e71f2008-03-09 01:54:53 +00001628 "This code should only be reachable in the non-skipping case!");
1629}
1630
1631
1632void Preprocessor::HandleElseDirective(Token &Result) {
1633 ++NumElse;
Mike Stump1eb44332009-09-09 15:08:12 +00001634
Chris Lattner141e71f2008-03-09 01:54:53 +00001635 // #else directive in a non-skipping conditional... start skipping.
Chris Lattner35410d52009-04-14 05:07:49 +00001636 CheckEndOfDirective("else");
Mike Stump1eb44332009-09-09 15:08:12 +00001637
Chris Lattner141e71f2008-03-09 01:54:53 +00001638 PPConditionalInfo CI;
Chris Lattner3692b092008-11-18 07:59:24 +00001639 if (CurPPLexer->popConditionalLevel(CI)) {
1640 Diag(Result, diag::pp_err_else_without_if);
1641 return;
1642 }
Mike Stump1eb44332009-09-09 15:08:12 +00001643
Chris Lattner141e71f2008-03-09 01:54:53 +00001644 // If this is a top-level #else, inform the MIOpt.
Ted Kremenek60e45d42008-11-18 00:34:22 +00001645 if (CurPPLexer->getConditionalStackDepth() == 0)
1646 CurPPLexer->MIOpt.EnterTopLevelConditional();
Chris Lattner141e71f2008-03-09 01:54:53 +00001647
1648 // If this is a #else with a #else before it, report the error.
1649 if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else);
Mike Stump1eb44332009-09-09 15:08:12 +00001650
Chris Lattner141e71f2008-03-09 01:54:53 +00001651 // Finally, skip the rest of the contents of this block and return the first
1652 // token after it.
1653 return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
1654 /*FoundElse*/true);
1655}
1656
1657void Preprocessor::HandleElifDirective(Token &ElifToken) {
1658 ++NumElse;
Mike Stump1eb44332009-09-09 15:08:12 +00001659
Chris Lattner141e71f2008-03-09 01:54:53 +00001660 // #elif directive in a non-skipping conditional... start skipping.
1661 // We don't care what the condition is, because we will always skip it (since
1662 // the block immediately before it was included).
1663 DiscardUntilEndOfDirective();
1664
1665 PPConditionalInfo CI;
Chris Lattner3692b092008-11-18 07:59:24 +00001666 if (CurPPLexer->popConditionalLevel(CI)) {
1667 Diag(ElifToken, diag::pp_err_elif_without_if);
1668 return;
1669 }
Mike Stump1eb44332009-09-09 15:08:12 +00001670
Chris Lattner141e71f2008-03-09 01:54:53 +00001671 // If this is a top-level #elif, inform the MIOpt.
Ted Kremenek60e45d42008-11-18 00:34:22 +00001672 if (CurPPLexer->getConditionalStackDepth() == 0)
1673 CurPPLexer->MIOpt.EnterTopLevelConditional();
Mike Stump1eb44332009-09-09 15:08:12 +00001674
Chris Lattner141e71f2008-03-09 01:54:53 +00001675 // If this is a #elif with a #else before it, report the error.
1676 if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else);
1677
1678 // Finally, skip the rest of the contents of this block and return the first
1679 // token after it.
1680 return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
1681 /*FoundElse*/CI.FoundElse);
1682}