Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 1 | //===--- PPDirectives.cpp - Directive Handling for Preprocessor -----------===// |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 2 | // |
| 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" |
| 15 | #include "clang/Lex/HeaderSearch.h" |
| 16 | #include "clang/Lex/MacroInfo.h" |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 17 | #include "clang/Basic/Diagnostic.h" |
| 18 | #include "clang/Basic/SourceManager.h" |
| 19 | using namespace clang; |
| 20 | |
| 21 | //===----------------------------------------------------------------------===// |
| 22 | // Utility Methods for Preprocessor Directive Handling. |
| 23 | //===----------------------------------------------------------------------===// |
| 24 | |
Ted Kremenek | 0ea7672 | 2008-12-15 19:56:42 +0000 | [diff] [blame] | 25 | MacroInfo* Preprocessor::AllocateMacroInfo(SourceLocation L) { |
| 26 | MacroInfo *MI; |
| 27 | |
| 28 | if (!MICache.empty()) { |
| 29 | MI = MICache.back(); |
| 30 | MICache.pop_back(); |
| 31 | } |
| 32 | else MI = (MacroInfo*) BP.Allocate<MacroInfo>(); |
| 33 | new (MI) MacroInfo(L); |
| 34 | return MI; |
| 35 | } |
| 36 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 37 | /// DiscardUntilEndOfDirective - Read and discard all tokens remaining on the |
| 38 | /// current line until the tok::eom token is found. |
| 39 | void Preprocessor::DiscardUntilEndOfDirective() { |
| 40 | Token Tmp; |
| 41 | do { |
| 42 | LexUnexpandedToken(Tmp); |
| 43 | } while (Tmp.isNot(tok::eom)); |
| 44 | } |
| 45 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 46 | /// ReadMacroName - Lex and validate a macro name, which occurs after a |
| 47 | /// #define or #undef. This sets the token kind to eom and discards the rest |
| 48 | /// of the macro line if the macro name is invalid. isDefineUndef is 1 if |
| 49 | /// this is due to a a #define, 2 if #undef directive, 0 if it is something |
| 50 | /// else (e.g. #ifdef). |
| 51 | void Preprocessor::ReadMacroName(Token &MacroNameTok, char isDefineUndef) { |
| 52 | // Read the token, don't allow macro expansion on it. |
| 53 | LexUnexpandedToken(MacroNameTok); |
| 54 | |
| 55 | // Missing macro name? |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 56 | if (MacroNameTok.is(tok::eom)) { |
| 57 | Diag(MacroNameTok, diag::err_pp_missing_macro_name); |
| 58 | return; |
| 59 | } |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 60 | |
| 61 | IdentifierInfo *II = MacroNameTok.getIdentifierInfo(); |
| 62 | if (II == 0) { |
| 63 | std::string Spelling = getSpelling(MacroNameTok); |
Chris Lattner | 9485d23 | 2008-12-13 20:12:40 +0000 | [diff] [blame] | 64 | const IdentifierInfo &Info = Identifiers.get(Spelling); |
| 65 | if (Info.isCPlusPlusOperatorKeyword()) |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 66 | // C++ 2.5p2: Alternative tokens behave the same as its primary token |
| 67 | // except for their spellings. |
Chris Lattner | 56b05c8 | 2008-11-18 08:02:48 +0000 | [diff] [blame] | 68 | Diag(MacroNameTok, diag::err_pp_operator_used_as_macro_name) << Spelling; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 69 | else |
| 70 | Diag(MacroNameTok, diag::err_pp_macro_not_identifier); |
| 71 | // Fall through on error. |
| 72 | } else if (isDefineUndef && II->getPPKeywordID() == tok::pp_defined) { |
| 73 | // Error if defining "defined": C99 6.10.8.4. |
| 74 | Diag(MacroNameTok, diag::err_defined_macro_name); |
| 75 | } else if (isDefineUndef && II->hasMacroDefinition() && |
| 76 | getMacroInfo(II)->isBuiltinMacro()) { |
| 77 | // Error if defining "__LINE__" and other builtins: C99 6.10.8.4. |
| 78 | if (isDefineUndef == 1) |
| 79 | Diag(MacroNameTok, diag::pp_redef_builtin_macro); |
| 80 | else |
| 81 | Diag(MacroNameTok, diag::pp_undef_builtin_macro); |
| 82 | } else { |
| 83 | // Okay, we got a good identifier node. Return it. |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | // Invalid macro name, read and discard the rest of the line. Then set the |
| 88 | // token kind to tok::eom. |
| 89 | MacroNameTok.setKind(tok::eom); |
| 90 | return DiscardUntilEndOfDirective(); |
| 91 | } |
| 92 | |
| 93 | /// CheckEndOfDirective - Ensure that the next token is a tok::eom token. If |
| 94 | /// not, emit a diagnostic and consume up until the eom. |
| 95 | void Preprocessor::CheckEndOfDirective(const char *DirType) { |
| 96 | Token Tmp; |
| 97 | // Lex unexpanded tokens: macros might expand to zero tokens, causing us to |
| 98 | // miss diagnosing invalid lines. |
| 99 | LexUnexpandedToken(Tmp); |
| 100 | |
| 101 | // There should be no tokens after the directive, but we allow them as an |
| 102 | // extension. |
| 103 | while (Tmp.is(tok::comment)) // Skip comments in -C mode. |
| 104 | LexUnexpandedToken(Tmp); |
| 105 | |
| 106 | if (Tmp.isNot(tok::eom)) { |
Chris Lattner | 56b05c8 | 2008-11-18 08:02:48 +0000 | [diff] [blame] | 107 | Diag(Tmp, diag::ext_pp_extra_tokens_at_eol) << DirType; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 108 | DiscardUntilEndOfDirective(); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | |
| 113 | |
| 114 | /// SkipExcludedConditionalBlock - We just read a #if or related directive and |
| 115 | /// decided that the subsequent tokens are in the #if'd out portion of the |
| 116 | /// file. Lex the rest of the file, until we see an #endif. If |
| 117 | /// FoundNonSkipPortion is true, then we have already emitted code for part of |
| 118 | /// this #if directive, so #else/#elif blocks should never be entered. If ElseOk |
| 119 | /// is true, then #else directives are ok, if not, then we have already seen one |
| 120 | /// so a #else directive is a duplicate. When this returns, the caller can lex |
| 121 | /// the first valid token. |
| 122 | void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc, |
| 123 | bool FoundNonSkipPortion, |
| 124 | bool FoundElse) { |
| 125 | ++NumSkipped; |
Ted Kremenek | f6452c5 | 2008-11-18 01:04:47 +0000 | [diff] [blame] | 126 | assert(CurTokenLexer == 0 && CurPPLexer && "Lexing a macro, not a file?"); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 127 | |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 128 | CurPPLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/false, |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 129 | FoundNonSkipPortion, FoundElse); |
| 130 | |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 131 | if (CurPTHLexer) { |
| 132 | PTHSkipExcludedConditionalBlock(); |
| 133 | return; |
| 134 | } |
| 135 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 136 | // Enter raw mode to disable identifier lookup (and thus macro expansion), |
| 137 | // disabling warnings, etc. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 138 | CurPPLexer->LexingRawMode = true; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 139 | Token Tok; |
| 140 | while (1) { |
Ted Kremenek | f6452c5 | 2008-11-18 01:04:47 +0000 | [diff] [blame] | 141 | if (CurLexer) |
| 142 | CurLexer->Lex(Tok); |
| 143 | else |
| 144 | CurPTHLexer->Lex(Tok); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 145 | |
| 146 | // If this is the end of the buffer, we have an error. |
| 147 | if (Tok.is(tok::eof)) { |
| 148 | // Emit errors for each unterminated conditional on the stack, including |
| 149 | // the current one. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 150 | while (!CurPPLexer->ConditionalStack.empty()) { |
| 151 | Diag(CurPPLexer->ConditionalStack.back().IfLoc, |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 152 | diag::err_pp_unterminated_conditional); |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 153 | CurPPLexer->ConditionalStack.pop_back(); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | // Just return and let the caller lex after this #include. |
| 157 | break; |
| 158 | } |
| 159 | |
| 160 | // If this token is not a preprocessor directive, just skip it. |
| 161 | if (Tok.isNot(tok::hash) || !Tok.isAtStartOfLine()) |
| 162 | continue; |
| 163 | |
| 164 | // We just parsed a # character at the start of a line, so we're in |
| 165 | // directive mode. Tell the lexer this so any newlines we see will be |
| 166 | // converted into an EOM token (this terminates the macro). |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 167 | CurPPLexer->ParsingPreprocessorDirective = true; |
Ted Kremenek | ac6b06d | 2008-11-18 00:43:07 +0000 | [diff] [blame] | 168 | if (CurLexer) CurLexer->SetCommentRetentionState(false); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 169 | |
| 170 | |
| 171 | // Read the next token, the directive flavor. |
| 172 | LexUnexpandedToken(Tok); |
| 173 | |
| 174 | // If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or |
| 175 | // something bogus), skip it. |
| 176 | if (Tok.isNot(tok::identifier)) { |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 177 | CurPPLexer->ParsingPreprocessorDirective = false; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 178 | // Restore comment saving mode. |
Ted Kremenek | ac6b06d | 2008-11-18 00:43:07 +0000 | [diff] [blame] | 179 | if (CurLexer) CurLexer->SetCommentRetentionState(KeepComments); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 180 | continue; |
| 181 | } |
| 182 | |
| 183 | // If the first letter isn't i or e, it isn't intesting to us. We know that |
| 184 | // this is safe in the face of spelling differences, because there is no way |
| 185 | // to spell an i/e in a strange way that is another letter. Skipping this |
| 186 | // allows us to avoid looking up the identifier info for #define/#undef and |
| 187 | // other common directives. |
| 188 | const char *RawCharData = SourceMgr.getCharacterData(Tok.getLocation()); |
| 189 | char FirstChar = RawCharData[0]; |
| 190 | if (FirstChar >= 'a' && FirstChar <= 'z' && |
| 191 | FirstChar != 'i' && FirstChar != 'e') { |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 192 | CurPPLexer->ParsingPreprocessorDirective = false; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 193 | // Restore comment saving mode. |
Ted Kremenek | ac6b06d | 2008-11-18 00:43:07 +0000 | [diff] [blame] | 194 | if (CurLexer) CurLexer->SetCommentRetentionState(KeepComments); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 195 | continue; |
| 196 | } |
| 197 | |
| 198 | // Get the identifier name without trigraphs or embedded newlines. Note |
| 199 | // that we can't use Tok.getIdentifierInfo() because its lookup is disabled |
| 200 | // when skipping. |
| 201 | // TODO: could do this with zero copies in the no-clean case by using |
| 202 | // strncmp below. |
| 203 | char Directive[20]; |
| 204 | unsigned IdLen; |
| 205 | if (!Tok.needsCleaning() && Tok.getLength() < 20) { |
| 206 | IdLen = Tok.getLength(); |
| 207 | memcpy(Directive, RawCharData, IdLen); |
| 208 | Directive[IdLen] = 0; |
| 209 | } else { |
| 210 | std::string DirectiveStr = getSpelling(Tok); |
| 211 | IdLen = DirectiveStr.size(); |
| 212 | if (IdLen >= 20) { |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 213 | CurPPLexer->ParsingPreprocessorDirective = false; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 214 | // Restore comment saving mode. |
Ted Kremenek | ac6b06d | 2008-11-18 00:43:07 +0000 | [diff] [blame] | 215 | if (CurLexer) CurLexer->SetCommentRetentionState(KeepComments); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 216 | continue; |
| 217 | } |
| 218 | memcpy(Directive, &DirectiveStr[0], IdLen); |
| 219 | Directive[IdLen] = 0; |
| 220 | } |
| 221 | |
| 222 | if (FirstChar == 'i' && Directive[1] == 'f') { |
| 223 | if ((IdLen == 2) || // "if" |
| 224 | (IdLen == 5 && !strcmp(Directive+2, "def")) || // "ifdef" |
| 225 | (IdLen == 6 && !strcmp(Directive+2, "ndef"))) { // "ifndef" |
| 226 | // We know the entire #if/#ifdef/#ifndef block will be skipped, don't |
| 227 | // bother parsing the condition. |
| 228 | DiscardUntilEndOfDirective(); |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 229 | CurPPLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true, |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 230 | /*foundnonskip*/false, |
| 231 | /*fnddelse*/false); |
| 232 | } |
| 233 | } else if (FirstChar == 'e') { |
| 234 | if (IdLen == 5 && !strcmp(Directive+1, "ndif")) { // "endif" |
| 235 | CheckEndOfDirective("#endif"); |
| 236 | PPConditionalInfo CondInfo; |
| 237 | CondInfo.WasSkipping = true; // Silence bogus warning. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 238 | bool InCond = CurPPLexer->popConditionalLevel(CondInfo); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 239 | InCond = InCond; // Silence warning in no-asserts mode. |
| 240 | assert(!InCond && "Can't be skipping if not in a conditional!"); |
| 241 | |
| 242 | // If we popped the outermost skipping block, we're done skipping! |
| 243 | if (!CondInfo.WasSkipping) |
| 244 | break; |
| 245 | } else if (IdLen == 4 && !strcmp(Directive+1, "lse")) { // "else". |
| 246 | // #else directive in a skipping conditional. If not in some other |
| 247 | // skipping conditional, and if #else hasn't already been seen, enter it |
| 248 | // as a non-skipping conditional. |
| 249 | CheckEndOfDirective("#else"); |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 250 | PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel(); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 251 | |
| 252 | // If this is a #else with a #else before it, report the error. |
| 253 | if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_else_after_else); |
| 254 | |
| 255 | // Note that we've seen a #else in this conditional. |
| 256 | CondInfo.FoundElse = true; |
| 257 | |
| 258 | // If the conditional is at the top level, and the #if block wasn't |
| 259 | // entered, enter the #else block now. |
| 260 | if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) { |
| 261 | CondInfo.FoundNonSkip = true; |
| 262 | break; |
| 263 | } |
| 264 | } else if (IdLen == 4 && !strcmp(Directive+1, "lif")) { // "elif". |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 265 | PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel(); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 266 | |
| 267 | bool ShouldEnter; |
| 268 | // If this is in a skipping block or if we're already handled this #if |
| 269 | // block, don't bother parsing the condition. |
| 270 | if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) { |
| 271 | DiscardUntilEndOfDirective(); |
| 272 | ShouldEnter = false; |
| 273 | } else { |
| 274 | // Restore the value of LexingRawMode so that identifiers are |
| 275 | // looked up, etc, inside the #elif expression. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 276 | assert(CurPPLexer->LexingRawMode && "We have to be skipping here!"); |
| 277 | CurPPLexer->LexingRawMode = false; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 278 | IdentifierInfo *IfNDefMacro = 0; |
| 279 | ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro); |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 280 | CurPPLexer->LexingRawMode = true; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | // If this is a #elif with a #else before it, report the error. |
| 284 | if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_elif_after_else); |
| 285 | |
| 286 | // If this condition is true, enter it! |
| 287 | if (ShouldEnter) { |
| 288 | CondInfo.FoundNonSkip = true; |
| 289 | break; |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 294 | CurPPLexer->ParsingPreprocessorDirective = false; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 295 | // Restore comment saving mode. |
Ted Kremenek | ac6b06d | 2008-11-18 00:43:07 +0000 | [diff] [blame] | 296 | if (CurLexer) CurLexer->SetCommentRetentionState(KeepComments); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | // Finally, if we are out of the conditional (saw an #endif or ran off the end |
| 300 | // of the file, just stop skipping and return to lexing whatever came after |
| 301 | // the #if block. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 302 | CurPPLexer->LexingRawMode = false; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 303 | } |
| 304 | |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 305 | void Preprocessor::PTHSkipExcludedConditionalBlock() { |
| 306 | |
| 307 | while(1) { |
| 308 | assert(CurPTHLexer); |
| 309 | assert(CurPTHLexer->LexingRawMode == false); |
| 310 | |
| 311 | // Skip to the next '#else', '#elif', or #endif. |
| 312 | if (CurPTHLexer->SkipBlock()) { |
| 313 | // We have reached an #endif. Both the '#' and 'endif' tokens |
| 314 | // have been consumed by the PTHLexer. Just pop off the condition level. |
| 315 | PPConditionalInfo CondInfo; |
| 316 | bool InCond = CurPTHLexer->popConditionalLevel(CondInfo); |
| 317 | InCond = InCond; // Silence warning in no-asserts mode. |
| 318 | assert(!InCond && "Can't be skipping if not in a conditional!"); |
| 319 | break; |
| 320 | } |
| 321 | |
| 322 | // We have reached a '#else' or '#elif'. Lex the next token to get |
| 323 | // the directive flavor. |
| 324 | Token Tok; |
| 325 | LexUnexpandedToken(Tok); |
| 326 | |
| 327 | // We can actually look up the IdentifierInfo here since we aren't in |
| 328 | // raw mode. |
| 329 | tok::PPKeywordKind K = Tok.getIdentifierInfo()->getPPKeywordID(); |
| 330 | |
| 331 | if (K == tok::pp_else) { |
| 332 | // #else: Enter the else condition. We aren't in a nested condition |
| 333 | // since we skip those. We're always in the one matching the last |
| 334 | // blocked we skipped. |
| 335 | PPConditionalInfo &CondInfo = CurPTHLexer->peekConditionalLevel(); |
| 336 | // Note that we've seen a #else in this conditional. |
| 337 | CondInfo.FoundElse = true; |
| 338 | |
| 339 | // If the #if block wasn't entered then enter the #else block now. |
| 340 | if (!CondInfo.FoundNonSkip) { |
| 341 | CondInfo.FoundNonSkip = true; |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 342 | |
| 343 | // Consume the eom token. |
| 344 | CurPTHLexer->ParsingPreprocessorDirective = true; |
| 345 | LexUnexpandedToken(Tok); |
| 346 | assert(Tok.is(tok::eom)); |
| 347 | CurPTHLexer->ParsingPreprocessorDirective = false; |
| 348 | |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 349 | break; |
| 350 | } |
| 351 | |
| 352 | // Otherwise skip this block. |
| 353 | continue; |
| 354 | } |
| 355 | |
| 356 | assert(K == tok::pp_elif); |
| 357 | PPConditionalInfo &CondInfo = CurPTHLexer->peekConditionalLevel(); |
| 358 | |
| 359 | // If this is a #elif with a #else before it, report the error. |
| 360 | if (CondInfo.FoundElse) |
| 361 | Diag(Tok, diag::pp_err_elif_after_else); |
| 362 | |
| 363 | // If this is in a skipping block or if we're already handled this #if |
| 364 | // block, don't bother parsing the condition. We just skip this block. |
| 365 | if (CondInfo.FoundNonSkip) |
| 366 | continue; |
| 367 | |
| 368 | // Evaluate the condition of the #elif. |
| 369 | IdentifierInfo *IfNDefMacro = 0; |
| 370 | CurPTHLexer->ParsingPreprocessorDirective = true; |
| 371 | bool ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro); |
| 372 | CurPTHLexer->ParsingPreprocessorDirective = false; |
| 373 | |
| 374 | // If this condition is true, enter it! |
| 375 | if (ShouldEnter) { |
| 376 | CondInfo.FoundNonSkip = true; |
| 377 | break; |
| 378 | } |
| 379 | |
| 380 | // Otherwise, skip this block and go to the next one. |
| 381 | continue; |
| 382 | } |
| 383 | } |
| 384 | |
Chris Lattner | 1072509 | 2008-03-09 04:17:44 +0000 | [diff] [blame] | 385 | /// LookupFile - Given a "foo" or <foo> reference, look up the indicated file, |
| 386 | /// return null on failure. isAngled indicates whether the file reference is |
| 387 | /// for system #include's or not (i.e. using <> instead of ""). |
| 388 | const FileEntry *Preprocessor::LookupFile(const char *FilenameStart, |
| 389 | const char *FilenameEnd, |
| 390 | bool isAngled, |
| 391 | const DirectoryLookup *FromDir, |
| 392 | const DirectoryLookup *&CurDir) { |
| 393 | // If the header lookup mechanism may be relative to the current file, pass in |
| 394 | // info about where the current file is. |
| 395 | const FileEntry *CurFileEnt = 0; |
| 396 | if (!FromDir) { |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 397 | FileID FID = getCurrentFileLexer()->getFileID(); |
| 398 | CurFileEnt = SourceMgr.getFileEntryForID(FID); |
Chris Lattner | 1072509 | 2008-03-09 04:17:44 +0000 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | // Do a standard file entry lookup. |
| 402 | CurDir = CurDirLookup; |
| 403 | const FileEntry *FE = |
| 404 | HeaderInfo.LookupFile(FilenameStart, FilenameEnd, |
| 405 | isAngled, FromDir, CurDir, CurFileEnt); |
| 406 | if (FE) return FE; |
| 407 | |
| 408 | // Otherwise, see if this is a subframework header. If so, this is relative |
| 409 | // to one of the headers on the #include stack. Walk the list of the current |
| 410 | // headers on the #include stack and pass them to HeaderInfo. |
Ted Kremenek | 81d24e1 | 2008-11-20 16:19:53 +0000 | [diff] [blame] | 411 | if (IsFileLexer()) { |
Ted Kremenek | 41938c8 | 2008-11-19 21:57:25 +0000 | [diff] [blame] | 412 | if ((CurFileEnt = SourceMgr.getFileEntryForID(CurPPLexer->getFileID()))) |
Chris Lattner | 1072509 | 2008-03-09 04:17:44 +0000 | [diff] [blame] | 413 | if ((FE = HeaderInfo.LookupSubframeworkHeader(FilenameStart, FilenameEnd, |
| 414 | CurFileEnt))) |
| 415 | return FE; |
| 416 | } |
| 417 | |
| 418 | for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) { |
| 419 | IncludeStackInfo &ISEntry = IncludeMacroStack[e-i-1]; |
Ted Kremenek | 81d24e1 | 2008-11-20 16:19:53 +0000 | [diff] [blame] | 420 | if (IsFileLexer(ISEntry)) { |
Chris Lattner | 1072509 | 2008-03-09 04:17:44 +0000 | [diff] [blame] | 421 | if ((CurFileEnt = |
Ted Kremenek | 41938c8 | 2008-11-19 21:57:25 +0000 | [diff] [blame] | 422 | SourceMgr.getFileEntryForID(ISEntry.ThePPLexer->getFileID()))) |
Chris Lattner | 1072509 | 2008-03-09 04:17:44 +0000 | [diff] [blame] | 423 | if ((FE = HeaderInfo.LookupSubframeworkHeader(FilenameStart, |
| 424 | FilenameEnd, CurFileEnt))) |
| 425 | return FE; |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | // Otherwise, we really couldn't find the file. |
| 430 | return 0; |
| 431 | } |
| 432 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 433 | |
| 434 | //===----------------------------------------------------------------------===// |
| 435 | // Preprocessor Directive Handling. |
| 436 | //===----------------------------------------------------------------------===// |
| 437 | |
| 438 | /// HandleDirective - This callback is invoked when the lexer sees a # token |
| 439 | /// at the start of a line. This consumes the directive, modifies the |
| 440 | /// lexer/preprocessor state, and advances the lexer(s) so that the next token |
| 441 | /// read is the correct one. |
| 442 | void Preprocessor::HandleDirective(Token &Result) { |
| 443 | // FIXME: Traditional: # with whitespace before it not recognized by K&R? |
| 444 | |
| 445 | // We just parsed a # character at the start of a line, so we're in directive |
| 446 | // mode. Tell the lexer this so any newlines we see will be converted into an |
| 447 | // EOM token (which terminates the directive). |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 448 | CurPPLexer->ParsingPreprocessorDirective = true; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 449 | |
| 450 | ++NumDirectives; |
| 451 | |
| 452 | // We are about to read a token. For the multiple-include optimization FA to |
| 453 | // work, we have to remember if we had read any tokens *before* this |
| 454 | // pp-directive. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 455 | bool ReadAnyTokensBeforeDirective = CurPPLexer->MIOpt.getHasReadAnyTokensVal(); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 456 | |
| 457 | // Read the next token, the directive flavor. This isn't expanded due to |
| 458 | // C99 6.10.3p8. |
| 459 | LexUnexpandedToken(Result); |
| 460 | |
| 461 | // C99 6.10.3p11: Is this preprocessor directive in macro invocation? e.g.: |
| 462 | // #define A(x) #x |
| 463 | // A(abc |
| 464 | // #warning blah |
| 465 | // def) |
| 466 | // If so, the user is relying on non-portable behavior, emit a diagnostic. |
| 467 | if (InMacroArgs) |
| 468 | Diag(Result, diag::ext_embedded_directive); |
| 469 | |
| 470 | TryAgain: |
| 471 | switch (Result.getKind()) { |
| 472 | case tok::eom: |
| 473 | return; // null directive. |
| 474 | case tok::comment: |
| 475 | // Handle stuff like "# /*foo*/ define X" in -E -C mode. |
| 476 | LexUnexpandedToken(Result); |
| 477 | goto TryAgain; |
| 478 | |
| 479 | case tok::numeric_constant: |
| 480 | // FIXME: implement # 7 line numbers! |
| 481 | DiscardUntilEndOfDirective(); |
| 482 | return; |
| 483 | default: |
| 484 | IdentifierInfo *II = Result.getIdentifierInfo(); |
| 485 | if (II == 0) break; // Not an identifier. |
| 486 | |
| 487 | // Ask what the preprocessor keyword ID is. |
| 488 | switch (II->getPPKeywordID()) { |
| 489 | default: break; |
| 490 | // C99 6.10.1 - Conditional Inclusion. |
| 491 | case tok::pp_if: |
| 492 | return HandleIfDirective(Result, ReadAnyTokensBeforeDirective); |
| 493 | case tok::pp_ifdef: |
| 494 | return HandleIfdefDirective(Result, false, true/*not valid for miopt*/); |
| 495 | case tok::pp_ifndef: |
| 496 | return HandleIfdefDirective(Result, true, ReadAnyTokensBeforeDirective); |
| 497 | case tok::pp_elif: |
| 498 | return HandleElifDirective(Result); |
| 499 | case tok::pp_else: |
| 500 | return HandleElseDirective(Result); |
| 501 | case tok::pp_endif: |
| 502 | return HandleEndifDirective(Result); |
| 503 | |
| 504 | // C99 6.10.2 - Source File Inclusion. |
| 505 | case tok::pp_include: |
| 506 | return HandleIncludeDirective(Result); // Handle #include. |
| 507 | |
| 508 | // C99 6.10.3 - Macro Replacement. |
| 509 | case tok::pp_define: |
| 510 | return HandleDefineDirective(Result); |
| 511 | case tok::pp_undef: |
| 512 | return HandleUndefDirective(Result); |
| 513 | |
| 514 | // C99 6.10.4 - Line Control. |
| 515 | case tok::pp_line: |
| 516 | // FIXME: implement #line |
| 517 | DiscardUntilEndOfDirective(); |
| 518 | return; |
| 519 | |
| 520 | // C99 6.10.5 - Error Directive. |
| 521 | case tok::pp_error: |
| 522 | return HandleUserDiagnosticDirective(Result, false); |
| 523 | |
| 524 | // C99 6.10.6 - Pragma Directive. |
| 525 | case tok::pp_pragma: |
| 526 | return HandlePragmaDirective(); |
| 527 | |
| 528 | // GNU Extensions. |
| 529 | case tok::pp_import: |
| 530 | return HandleImportDirective(Result); |
| 531 | case tok::pp_include_next: |
| 532 | return HandleIncludeNextDirective(Result); |
| 533 | |
| 534 | case tok::pp_warning: |
| 535 | Diag(Result, diag::ext_pp_warning_directive); |
| 536 | return HandleUserDiagnosticDirective(Result, true); |
| 537 | case tok::pp_ident: |
| 538 | return HandleIdentSCCSDirective(Result); |
| 539 | case tok::pp_sccs: |
| 540 | return HandleIdentSCCSDirective(Result); |
| 541 | case tok::pp_assert: |
| 542 | //isExtension = true; // FIXME: implement #assert |
| 543 | break; |
| 544 | case tok::pp_unassert: |
| 545 | //isExtension = true; // FIXME: implement #unassert |
| 546 | break; |
| 547 | } |
| 548 | break; |
| 549 | } |
| 550 | |
| 551 | // If we reached here, the preprocessing token is not valid! |
| 552 | Diag(Result, diag::err_pp_invalid_directive); |
| 553 | |
| 554 | // Read the rest of the PP line. |
| 555 | DiscardUntilEndOfDirective(); |
| 556 | |
| 557 | // Okay, we're done parsing the directive. |
| 558 | } |
| 559 | |
| 560 | void Preprocessor::HandleUserDiagnosticDirective(Token &Tok, |
| 561 | bool isWarning) { |
| 562 | // Read the rest of the line raw. We do this because we don't want macros |
| 563 | // to be expanded and we don't require that the tokens be valid preprocessing |
| 564 | // tokens. For example, this is allowed: "#warning ` 'foo". GCC does |
| 565 | // collapse multiple consequtive white space between tokens, but this isn't |
| 566 | // specified by the standard. |
Ted Kremenek | 17ff58a | 2008-11-19 22:21:33 +0000 | [diff] [blame] | 567 | |
| 568 | if (CurLexer) { |
| 569 | std::string Message = CurLexer->ReadToEndOfLine(); |
| 570 | unsigned DiagID = isWarning ? diag::pp_hash_warning : diag::err_pp_hash_error; |
| 571 | Diag(Tok, DiagID) << Message; |
| 572 | } |
| 573 | else { |
| 574 | CurPTHLexer->DiscardToEndOfLine(); |
| 575 | } |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | /// HandleIdentSCCSDirective - Handle a #ident/#sccs directive. |
| 579 | /// |
| 580 | void Preprocessor::HandleIdentSCCSDirective(Token &Tok) { |
| 581 | // Yes, this directive is an extension. |
| 582 | Diag(Tok, diag::ext_pp_ident_directive); |
| 583 | |
| 584 | // Read the string argument. |
| 585 | Token StrTok; |
| 586 | Lex(StrTok); |
| 587 | |
| 588 | // If the token kind isn't a string, it's a malformed directive. |
| 589 | if (StrTok.isNot(tok::string_literal) && |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 590 | StrTok.isNot(tok::wide_string_literal)) { |
| 591 | Diag(StrTok, diag::err_pp_malformed_ident); |
| 592 | return; |
| 593 | } |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 594 | |
| 595 | // Verify that there is nothing after the string, other than EOM. |
| 596 | CheckEndOfDirective("#ident"); |
| 597 | |
| 598 | if (Callbacks) |
| 599 | Callbacks->Ident(Tok.getLocation(), getSpelling(StrTok)); |
| 600 | } |
| 601 | |
| 602 | //===----------------------------------------------------------------------===// |
| 603 | // Preprocessor Include Directive Handling. |
| 604 | //===----------------------------------------------------------------------===// |
| 605 | |
| 606 | /// GetIncludeFilenameSpelling - Turn the specified lexer token into a fully |
| 607 | /// checked and spelled filename, e.g. as an operand of #include. This returns |
| 608 | /// true if the input filename was in <>'s or false if it were in ""'s. The |
| 609 | /// caller is expected to provide a buffer that is large enough to hold the |
| 610 | /// spelling of the filename, but is also expected to handle the case when |
| 611 | /// this method decides to use a different buffer. |
| 612 | bool Preprocessor::GetIncludeFilenameSpelling(SourceLocation Loc, |
| 613 | const char *&BufStart, |
| 614 | const char *&BufEnd) { |
| 615 | // Get the text form of the filename. |
| 616 | assert(BufStart != BufEnd && "Can't have tokens with empty spellings!"); |
| 617 | |
| 618 | // Make sure the filename is <x> or "x". |
| 619 | bool isAngled; |
| 620 | if (BufStart[0] == '<') { |
| 621 | if (BufEnd[-1] != '>') { |
| 622 | Diag(Loc, diag::err_pp_expects_filename); |
| 623 | BufStart = 0; |
| 624 | return true; |
| 625 | } |
| 626 | isAngled = true; |
| 627 | } else if (BufStart[0] == '"') { |
| 628 | if (BufEnd[-1] != '"') { |
| 629 | Diag(Loc, diag::err_pp_expects_filename); |
| 630 | BufStart = 0; |
| 631 | return true; |
| 632 | } |
| 633 | isAngled = false; |
| 634 | } else { |
| 635 | Diag(Loc, diag::err_pp_expects_filename); |
| 636 | BufStart = 0; |
| 637 | return true; |
| 638 | } |
| 639 | |
| 640 | // Diagnose #include "" as invalid. |
| 641 | if (BufEnd-BufStart <= 2) { |
| 642 | Diag(Loc, diag::err_pp_empty_filename); |
| 643 | BufStart = 0; |
| 644 | return ""; |
| 645 | } |
| 646 | |
| 647 | // Skip the brackets. |
| 648 | ++BufStart; |
| 649 | --BufEnd; |
| 650 | return isAngled; |
| 651 | } |
| 652 | |
| 653 | /// ConcatenateIncludeName - Handle cases where the #include name is expanded |
| 654 | /// from a macro as multiple tokens, which need to be glued together. This |
| 655 | /// occurs for code like: |
| 656 | /// #define FOO <a/b.h> |
| 657 | /// #include FOO |
| 658 | /// because in this case, "<a/b.h>" is returned as 7 tokens, not one. |
| 659 | /// |
| 660 | /// This code concatenates and consumes tokens up to the '>' token. It returns |
| 661 | /// false if the > was found, otherwise it returns true if it finds and consumes |
| 662 | /// the EOM marker. |
| 663 | static bool ConcatenateIncludeName(llvm::SmallVector<char, 128> &FilenameBuffer, |
| 664 | Preprocessor &PP) { |
| 665 | Token CurTok; |
| 666 | |
| 667 | PP.Lex(CurTok); |
| 668 | while (CurTok.isNot(tok::eom)) { |
| 669 | // Append the spelling of this token to the buffer. If there was a space |
| 670 | // before it, add it now. |
| 671 | if (CurTok.hasLeadingSpace()) |
| 672 | FilenameBuffer.push_back(' '); |
| 673 | |
| 674 | // Get the spelling of the token, directly into FilenameBuffer if possible. |
| 675 | unsigned PreAppendSize = FilenameBuffer.size(); |
| 676 | FilenameBuffer.resize(PreAppendSize+CurTok.getLength()); |
| 677 | |
| 678 | const char *BufPtr = &FilenameBuffer[PreAppendSize]; |
| 679 | unsigned ActualLen = PP.getSpelling(CurTok, BufPtr); |
| 680 | |
| 681 | // If the token was spelled somewhere else, copy it into FilenameBuffer. |
| 682 | if (BufPtr != &FilenameBuffer[PreAppendSize]) |
| 683 | memcpy(&FilenameBuffer[PreAppendSize], BufPtr, ActualLen); |
| 684 | |
| 685 | // Resize FilenameBuffer to the correct size. |
| 686 | if (CurTok.getLength() != ActualLen) |
| 687 | FilenameBuffer.resize(PreAppendSize+ActualLen); |
| 688 | |
| 689 | // If we found the '>' marker, return success. |
| 690 | if (CurTok.is(tok::greater)) |
| 691 | return false; |
| 692 | |
| 693 | PP.Lex(CurTok); |
| 694 | } |
| 695 | |
| 696 | // If we hit the eom marker, emit an error and return true so that the caller |
| 697 | // knows the EOM has been read. |
| 698 | PP.Diag(CurTok.getLocation(), diag::err_pp_expects_filename); |
| 699 | return true; |
| 700 | } |
| 701 | |
| 702 | /// HandleIncludeDirective - The "#include" tokens have just been read, read the |
| 703 | /// file to be included from the lexer, then include it! This is a common |
| 704 | /// routine with functionality shared between #include, #include_next and |
Chris Lattner | 7218183 | 2008-09-26 20:12:23 +0000 | [diff] [blame] | 705 | /// #import. LookupFrom is set when this is a #include_next directive, it |
| 706 | /// specifies the file to start searching from. |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 707 | void Preprocessor::HandleIncludeDirective(Token &IncludeTok, |
| 708 | const DirectoryLookup *LookupFrom, |
| 709 | bool isImport) { |
| 710 | |
| 711 | Token FilenameTok; |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 712 | CurPPLexer->LexIncludeFilename(FilenameTok); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 713 | |
| 714 | // Reserve a buffer to get the spelling. |
| 715 | llvm::SmallVector<char, 128> FilenameBuffer; |
| 716 | const char *FilenameStart, *FilenameEnd; |
| 717 | |
| 718 | switch (FilenameTok.getKind()) { |
| 719 | case tok::eom: |
| 720 | // If the token kind is EOM, the error has already been diagnosed. |
| 721 | return; |
| 722 | |
| 723 | case tok::angle_string_literal: |
| 724 | case tok::string_literal: { |
| 725 | FilenameBuffer.resize(FilenameTok.getLength()); |
| 726 | FilenameStart = &FilenameBuffer[0]; |
| 727 | unsigned Len = getSpelling(FilenameTok, FilenameStart); |
| 728 | FilenameEnd = FilenameStart+Len; |
| 729 | break; |
| 730 | } |
| 731 | |
| 732 | case tok::less: |
| 733 | // This could be a <foo/bar.h> file coming from a macro expansion. In this |
| 734 | // case, glue the tokens together into FilenameBuffer and interpret those. |
| 735 | FilenameBuffer.push_back('<'); |
| 736 | if (ConcatenateIncludeName(FilenameBuffer, *this)) |
| 737 | return; // Found <eom> but no ">"? Diagnostic already emitted. |
| 738 | FilenameStart = &FilenameBuffer[0]; |
| 739 | FilenameEnd = &FilenameBuffer[FilenameBuffer.size()]; |
| 740 | break; |
| 741 | default: |
| 742 | Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename); |
| 743 | DiscardUntilEndOfDirective(); |
| 744 | return; |
| 745 | } |
| 746 | |
| 747 | bool isAngled = GetIncludeFilenameSpelling(FilenameTok.getLocation(), |
| 748 | FilenameStart, FilenameEnd); |
| 749 | // If GetIncludeFilenameSpelling set the start ptr to null, there was an |
| 750 | // error. |
| 751 | if (FilenameStart == 0) { |
| 752 | DiscardUntilEndOfDirective(); |
| 753 | return; |
| 754 | } |
| 755 | |
| 756 | // Verify that there is nothing after the filename, other than EOM. Use the |
| 757 | // preprocessor to lex this in case lexing the filename entered a macro. |
| 758 | CheckEndOfDirective("#include"); |
| 759 | |
| 760 | // Check that we don't have infinite #include recursion. |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 761 | if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1) { |
| 762 | Diag(FilenameTok, diag::err_pp_include_too_deep); |
| 763 | return; |
| 764 | } |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 765 | |
| 766 | // Search include directories. |
| 767 | const DirectoryLookup *CurDir; |
| 768 | const FileEntry *File = LookupFile(FilenameStart, FilenameEnd, |
| 769 | isAngled, LookupFrom, CurDir); |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 770 | if (File == 0) { |
| 771 | Diag(FilenameTok, diag::err_pp_file_not_found) |
| 772 | << std::string(FilenameStart, FilenameEnd); |
| 773 | return; |
| 774 | } |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 775 | |
Chris Lattner | 7218183 | 2008-09-26 20:12:23 +0000 | [diff] [blame] | 776 | // Ask HeaderInfo if we should enter this #include file. If not, #including |
| 777 | // this file will have no effect. |
| 778 | if (!HeaderInfo.ShouldEnterIncludeFile(File, isImport)) |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 779 | return; |
Chris Lattner | 7218183 | 2008-09-26 20:12:23 +0000 | [diff] [blame] | 780 | |
| 781 | // The #included file will be considered to be a system header if either it is |
| 782 | // in a system include directory, or if the #includer is a system include |
| 783 | // header. |
Chris Lattner | 9d72851 | 2008-10-27 01:19:25 +0000 | [diff] [blame] | 784 | SrcMgr::CharacteristicKind FileCharacter = |
Chris Lattner | 0b9e736 | 2008-09-26 21:18:42 +0000 | [diff] [blame] | 785 | std::max(HeaderInfo.getFileDirFlavor(File), |
Chris Lattner | 693faa6 | 2009-01-19 07:59:15 +0000 | [diff] [blame^] | 786 | SourceMgr.getFileCharacteristic(FilenameTok.getLocation())); |
Chris Lattner | 7218183 | 2008-09-26 20:12:23 +0000 | [diff] [blame] | 787 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 788 | // Look up the file, create a File ID for it. |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 789 | FileID FID = SourceMgr.createFileID(File, FilenameTok.getLocation(), |
| 790 | FileCharacter); |
| 791 | if (FID.isInvalid()) { |
Chris Lattner | 56b05c8 | 2008-11-18 08:02:48 +0000 | [diff] [blame] | 792 | Diag(FilenameTok, diag::err_pp_file_not_found) |
| 793 | << std::string(FilenameStart, FilenameEnd); |
| 794 | return; |
| 795 | } |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 796 | |
| 797 | // Finally, if all is good, enter the new file! |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 798 | EnterSourceFile(FID, CurDir); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | /// HandleIncludeNextDirective - Implements #include_next. |
| 802 | /// |
| 803 | void Preprocessor::HandleIncludeNextDirective(Token &IncludeNextTok) { |
| 804 | Diag(IncludeNextTok, diag::ext_pp_include_next_directive); |
| 805 | |
| 806 | // #include_next is like #include, except that we start searching after |
| 807 | // the current found directory. If we can't do this, issue a |
| 808 | // diagnostic. |
| 809 | const DirectoryLookup *Lookup = CurDirLookup; |
| 810 | if (isInPrimaryFile()) { |
| 811 | Lookup = 0; |
| 812 | Diag(IncludeNextTok, diag::pp_include_next_in_primary); |
| 813 | } else if (Lookup == 0) { |
| 814 | Diag(IncludeNextTok, diag::pp_include_next_absolute_path); |
| 815 | } else { |
| 816 | // Start looking up in the next directory. |
| 817 | ++Lookup; |
| 818 | } |
| 819 | |
| 820 | return HandleIncludeDirective(IncludeNextTok, Lookup); |
| 821 | } |
| 822 | |
| 823 | /// HandleImportDirective - Implements #import. |
| 824 | /// |
| 825 | void Preprocessor::HandleImportDirective(Token &ImportTok) { |
| 826 | Diag(ImportTok, diag::ext_pp_import_directive); |
| 827 | |
| 828 | return HandleIncludeDirective(ImportTok, 0, true); |
| 829 | } |
| 830 | |
| 831 | //===----------------------------------------------------------------------===// |
| 832 | // Preprocessor Macro Directive Handling. |
| 833 | //===----------------------------------------------------------------------===// |
| 834 | |
| 835 | /// ReadMacroDefinitionArgList - The ( starting an argument list of a macro |
| 836 | /// definition has just been read. Lex the rest of the arguments and the |
| 837 | /// closing ), updating MI with what we learn. Return true if an error occurs |
| 838 | /// parsing the arg list. |
| 839 | bool Preprocessor::ReadMacroDefinitionArgList(MacroInfo *MI) { |
| 840 | llvm::SmallVector<IdentifierInfo*, 32> Arguments; |
| 841 | |
| 842 | Token Tok; |
| 843 | while (1) { |
| 844 | LexUnexpandedToken(Tok); |
| 845 | switch (Tok.getKind()) { |
| 846 | case tok::r_paren: |
| 847 | // Found the end of the argument list. |
| 848 | if (Arguments.empty()) { // #define FOO() |
| 849 | MI->setArgumentList(Arguments.begin(), Arguments.end()); |
| 850 | return false; |
| 851 | } |
| 852 | // Otherwise we have #define FOO(A,) |
| 853 | Diag(Tok, diag::err_pp_expected_ident_in_arg_list); |
| 854 | return true; |
| 855 | case tok::ellipsis: // #define X(... -> C99 varargs |
| 856 | // Warn if use of C99 feature in non-C99 mode. |
| 857 | if (!Features.C99) Diag(Tok, diag::ext_variadic_macro); |
| 858 | |
| 859 | // Lex the token after the identifier. |
| 860 | LexUnexpandedToken(Tok); |
| 861 | if (Tok.isNot(tok::r_paren)) { |
| 862 | Diag(Tok, diag::err_pp_missing_rparen_in_macro_def); |
| 863 | return true; |
| 864 | } |
| 865 | // Add the __VA_ARGS__ identifier as an argument. |
| 866 | Arguments.push_back(Ident__VA_ARGS__); |
| 867 | MI->setIsC99Varargs(); |
| 868 | MI->setArgumentList(Arguments.begin(), Arguments.end()); |
| 869 | return false; |
| 870 | case tok::eom: // #define X( |
| 871 | Diag(Tok, diag::err_pp_missing_rparen_in_macro_def); |
| 872 | return true; |
| 873 | default: |
| 874 | // Handle keywords and identifiers here to accept things like |
| 875 | // #define Foo(for) for. |
| 876 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 877 | if (II == 0) { |
| 878 | // #define X(1 |
| 879 | Diag(Tok, diag::err_pp_invalid_tok_in_arg_list); |
| 880 | return true; |
| 881 | } |
| 882 | |
| 883 | // If this is already used as an argument, it is used multiple times (e.g. |
| 884 | // #define X(A,A. |
| 885 | if (std::find(Arguments.begin(), Arguments.end(), II) != |
| 886 | Arguments.end()) { // C99 6.10.3p6 |
Chris Lattner | 6cf3ed7 | 2008-11-19 07:33:58 +0000 | [diff] [blame] | 887 | Diag(Tok, diag::err_pp_duplicate_name_in_arg_list) << II; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 888 | return true; |
| 889 | } |
| 890 | |
| 891 | // Add the argument to the macro info. |
| 892 | Arguments.push_back(II); |
| 893 | |
| 894 | // Lex the token after the identifier. |
| 895 | LexUnexpandedToken(Tok); |
| 896 | |
| 897 | switch (Tok.getKind()) { |
| 898 | default: // #define X(A B |
| 899 | Diag(Tok, diag::err_pp_expected_comma_in_arg_list); |
| 900 | return true; |
| 901 | case tok::r_paren: // #define X(A) |
| 902 | MI->setArgumentList(Arguments.begin(), Arguments.end()); |
| 903 | return false; |
| 904 | case tok::comma: // #define X(A, |
| 905 | break; |
| 906 | case tok::ellipsis: // #define X(A... -> GCC extension |
| 907 | // Diagnose extension. |
| 908 | Diag(Tok, diag::ext_named_variadic_macro); |
| 909 | |
| 910 | // Lex the token after the identifier. |
| 911 | LexUnexpandedToken(Tok); |
| 912 | if (Tok.isNot(tok::r_paren)) { |
| 913 | Diag(Tok, diag::err_pp_missing_rparen_in_macro_def); |
| 914 | return true; |
| 915 | } |
| 916 | |
| 917 | MI->setIsGNUVarargs(); |
| 918 | MI->setArgumentList(Arguments.begin(), Arguments.end()); |
| 919 | return false; |
| 920 | } |
| 921 | } |
| 922 | } |
| 923 | } |
| 924 | |
| 925 | /// HandleDefineDirective - Implements #define. This consumes the entire macro |
| 926 | /// line then lets the caller lex the next real token. |
| 927 | void Preprocessor::HandleDefineDirective(Token &DefineTok) { |
| 928 | ++NumDefined; |
| 929 | |
| 930 | Token MacroNameTok; |
| 931 | ReadMacroName(MacroNameTok, 1); |
| 932 | |
| 933 | // Error reading macro name? If so, diagnostic already issued. |
| 934 | if (MacroNameTok.is(tok::eom)) |
| 935 | return; |
| 936 | |
| 937 | // If we are supposed to keep comments in #defines, reenable comment saving |
| 938 | // mode. |
Ted Kremenek | ac6b06d | 2008-11-18 00:43:07 +0000 | [diff] [blame] | 939 | if (CurLexer) CurLexer->SetCommentRetentionState(KeepMacroComments); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 940 | |
| 941 | // Create the new macro. |
Ted Kremenek | 0ea7672 | 2008-12-15 19:56:42 +0000 | [diff] [blame] | 942 | MacroInfo *MI = AllocateMacroInfo(MacroNameTok.getLocation()); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 943 | |
| 944 | Token Tok; |
| 945 | LexUnexpandedToken(Tok); |
| 946 | |
| 947 | // If this is a function-like macro definition, parse the argument list, |
| 948 | // marking each of the identifiers as being used as macro arguments. Also, |
| 949 | // check other constraints on the first token of the macro body. |
| 950 | if (Tok.is(tok::eom)) { |
| 951 | // If there is no body to this macro, we have no special handling here. |
| 952 | } else if (Tok.is(tok::l_paren) && !Tok.hasLeadingSpace()) { |
| 953 | // This is a function-like macro definition. Read the argument list. |
| 954 | MI->setIsFunctionLike(); |
| 955 | if (ReadMacroDefinitionArgList(MI)) { |
| 956 | // Forget about MI. |
Ted Kremenek | 0ea7672 | 2008-12-15 19:56:42 +0000 | [diff] [blame] | 957 | ReleaseMacroInfo(MI); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 958 | // Throw away the rest of the line. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 959 | if (CurPPLexer->ParsingPreprocessorDirective) |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 960 | DiscardUntilEndOfDirective(); |
| 961 | return; |
| 962 | } |
| 963 | |
| 964 | // Read the first token after the arg list for down below. |
| 965 | LexUnexpandedToken(Tok); |
| 966 | } else if (!Tok.hasLeadingSpace()) { |
| 967 | // C99 requires whitespace between the macro definition and the body. Emit |
| 968 | // a diagnostic for something like "#define X+". |
| 969 | if (Features.C99) { |
| 970 | Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name); |
| 971 | } else { |
| 972 | // FIXME: C90/C++ do not get this diagnostic, but it does get a similar |
| 973 | // one in some cases! |
| 974 | } |
| 975 | } else { |
| 976 | // This is a normal token with leading space. Clear the leading space |
| 977 | // marker on the first token to get proper expansion. |
| 978 | Tok.clearFlag(Token::LeadingSpace); |
| 979 | } |
| 980 | |
| 981 | // If this is a definition of a variadic C99 function-like macro, not using |
| 982 | // the GNU named varargs extension, enabled __VA_ARGS__. |
| 983 | |
| 984 | // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro. |
| 985 | // This gets unpoisoned where it is allowed. |
| 986 | assert(Ident__VA_ARGS__->isPoisoned() && "__VA_ARGS__ should be poisoned!"); |
| 987 | if (MI->isC99Varargs()) |
| 988 | Ident__VA_ARGS__->setIsPoisoned(false); |
| 989 | |
| 990 | // Read the rest of the macro body. |
| 991 | if (MI->isObjectLike()) { |
| 992 | // Object-like macros are very simple, just read their body. |
| 993 | while (Tok.isNot(tok::eom)) { |
| 994 | MI->AddTokenToBody(Tok); |
| 995 | // Get the next token of the macro. |
| 996 | LexUnexpandedToken(Tok); |
| 997 | } |
| 998 | |
| 999 | } else { |
| 1000 | // Otherwise, read the body of a function-like macro. This has to validate |
| 1001 | // the # (stringize) operator. |
| 1002 | while (Tok.isNot(tok::eom)) { |
| 1003 | MI->AddTokenToBody(Tok); |
| 1004 | |
| 1005 | // Check C99 6.10.3.2p1: ensure that # operators are followed by macro |
| 1006 | // parameters in function-like macro expansions. |
| 1007 | if (Tok.isNot(tok::hash)) { |
| 1008 | // Get the next token of the macro. |
| 1009 | LexUnexpandedToken(Tok); |
| 1010 | continue; |
| 1011 | } |
| 1012 | |
| 1013 | // Get the next token of the macro. |
| 1014 | LexUnexpandedToken(Tok); |
| 1015 | |
| 1016 | // Not a macro arg identifier? |
| 1017 | if (!Tok.getIdentifierInfo() || |
| 1018 | MI->getArgumentNum(Tok.getIdentifierInfo()) == -1) { |
| 1019 | Diag(Tok, diag::err_pp_stringize_not_parameter); |
Ted Kremenek | 0ea7672 | 2008-12-15 19:56:42 +0000 | [diff] [blame] | 1020 | ReleaseMacroInfo(MI); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1021 | |
| 1022 | // Disable __VA_ARGS__ again. |
| 1023 | Ident__VA_ARGS__->setIsPoisoned(true); |
| 1024 | return; |
| 1025 | } |
| 1026 | |
| 1027 | // Things look ok, add the param name token to the macro. |
| 1028 | MI->AddTokenToBody(Tok); |
| 1029 | |
| 1030 | // Get the next token of the macro. |
| 1031 | LexUnexpandedToken(Tok); |
| 1032 | } |
| 1033 | } |
| 1034 | |
| 1035 | |
| 1036 | // Disable __VA_ARGS__ again. |
| 1037 | Ident__VA_ARGS__->setIsPoisoned(true); |
| 1038 | |
| 1039 | // Check that there is no paste (##) operator at the begining or end of the |
| 1040 | // replacement list. |
| 1041 | unsigned NumTokens = MI->getNumTokens(); |
| 1042 | if (NumTokens != 0) { |
| 1043 | if (MI->getReplacementToken(0).is(tok::hashhash)) { |
| 1044 | Diag(MI->getReplacementToken(0), diag::err_paste_at_start); |
Ted Kremenek | 0ea7672 | 2008-12-15 19:56:42 +0000 | [diff] [blame] | 1045 | ReleaseMacroInfo(MI); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1046 | return; |
| 1047 | } |
| 1048 | if (MI->getReplacementToken(NumTokens-1).is(tok::hashhash)) { |
| 1049 | Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end); |
Ted Kremenek | 0ea7672 | 2008-12-15 19:56:42 +0000 | [diff] [blame] | 1050 | ReleaseMacroInfo(MI); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1051 | return; |
| 1052 | } |
| 1053 | } |
| 1054 | |
| 1055 | // If this is the primary source file, remember that this macro hasn't been |
| 1056 | // used yet. |
| 1057 | if (isInPrimaryFile()) |
| 1058 | MI->setIsUsed(false); |
| 1059 | |
| 1060 | // Finally, if this identifier already had a macro defined for it, verify that |
| 1061 | // the macro bodies are identical and free the old definition. |
| 1062 | if (MacroInfo *OtherMI = getMacroInfo(MacroNameTok.getIdentifierInfo())) { |
Chris Lattner | 41c3ae1 | 2009-01-16 19:50:11 +0000 | [diff] [blame] | 1063 | // It is very common for system headers to have tons of macro redefinitions |
| 1064 | // and for warnings to be disabled in system headers. If this is the case, |
| 1065 | // then don't bother calling MacroInfo::isIdenticalTo. |
| 1066 | if (!Diags.getSuppressSystemWarnings() || |
| 1067 | !SourceMgr.isInSystemHeader(DefineTok.getLocation())) { |
| 1068 | if (!OtherMI->isUsed()) |
| 1069 | Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1070 | |
Chris Lattner | 41c3ae1 | 2009-01-16 19:50:11 +0000 | [diff] [blame] | 1071 | // Macros must be identical. This means all tokes and whitespace |
| 1072 | // separation must be the same. C99 6.10.3.2. |
| 1073 | if (!MI->isIdenticalTo(*OtherMI, *this)) { |
| 1074 | Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef) |
| 1075 | << MacroNameTok.getIdentifierInfo(); |
| 1076 | Diag(OtherMI->getDefinitionLoc(), diag::note_previous_definition); |
| 1077 | } |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1078 | } |
Chris Lattner | 41c3ae1 | 2009-01-16 19:50:11 +0000 | [diff] [blame] | 1079 | |
Ted Kremenek | 0ea7672 | 2008-12-15 19:56:42 +0000 | [diff] [blame] | 1080 | ReleaseMacroInfo(OtherMI); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1081 | } |
| 1082 | |
| 1083 | setMacroInfo(MacroNameTok.getIdentifierInfo(), MI); |
| 1084 | } |
| 1085 | |
| 1086 | /// HandleUndefDirective - Implements #undef. |
| 1087 | /// |
| 1088 | void Preprocessor::HandleUndefDirective(Token &UndefTok) { |
| 1089 | ++NumUndefined; |
| 1090 | |
| 1091 | Token MacroNameTok; |
| 1092 | ReadMacroName(MacroNameTok, 2); |
| 1093 | |
| 1094 | // Error reading macro name? If so, diagnostic already issued. |
| 1095 | if (MacroNameTok.is(tok::eom)) |
| 1096 | return; |
| 1097 | |
| 1098 | // Check to see if this is the last token on the #undef line. |
| 1099 | CheckEndOfDirective("#undef"); |
| 1100 | |
| 1101 | // Okay, we finally have a valid identifier to undef. |
| 1102 | MacroInfo *MI = getMacroInfo(MacroNameTok.getIdentifierInfo()); |
| 1103 | |
| 1104 | // If the macro is not defined, this is a noop undef, just return. |
| 1105 | if (MI == 0) return; |
| 1106 | |
| 1107 | if (!MI->isUsed()) |
| 1108 | Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used); |
| 1109 | |
| 1110 | // Free macro definition. |
Ted Kremenek | 0ea7672 | 2008-12-15 19:56:42 +0000 | [diff] [blame] | 1111 | ReleaseMacroInfo(MI); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1112 | setMacroInfo(MacroNameTok.getIdentifierInfo(), 0); |
| 1113 | } |
| 1114 | |
| 1115 | |
| 1116 | //===----------------------------------------------------------------------===// |
| 1117 | // Preprocessor Conditional Directive Handling. |
| 1118 | //===----------------------------------------------------------------------===// |
| 1119 | |
| 1120 | /// HandleIfdefDirective - Implements the #ifdef/#ifndef directive. isIfndef is |
| 1121 | /// true when this is a #ifndef directive. ReadAnyTokensBeforeDirective is true |
| 1122 | /// if any tokens have been returned or pp-directives activated before this |
| 1123 | /// #ifndef has been lexed. |
| 1124 | /// |
| 1125 | void Preprocessor::HandleIfdefDirective(Token &Result, bool isIfndef, |
| 1126 | bool ReadAnyTokensBeforeDirective) { |
| 1127 | ++NumIf; |
| 1128 | Token DirectiveTok = Result; |
| 1129 | |
| 1130 | Token MacroNameTok; |
| 1131 | ReadMacroName(MacroNameTok); |
| 1132 | |
| 1133 | // Error reading macro name? If so, diagnostic already issued. |
| 1134 | if (MacroNameTok.is(tok::eom)) { |
| 1135 | // Skip code until we get to #endif. This helps with recovery by not |
| 1136 | // emitting an error when the #endif is reached. |
| 1137 | SkipExcludedConditionalBlock(DirectiveTok.getLocation(), |
| 1138 | /*Foundnonskip*/false, /*FoundElse*/false); |
| 1139 | return; |
| 1140 | } |
| 1141 | |
| 1142 | // Check to see if this is the last token on the #if[n]def line. |
| 1143 | CheckEndOfDirective(isIfndef ? "#ifndef" : "#ifdef"); |
| 1144 | |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1145 | if (CurPPLexer->getConditionalStackDepth() == 0) { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1146 | // If the start of a top-level #ifdef, inform MIOpt. |
| 1147 | if (!ReadAnyTokensBeforeDirective) { |
| 1148 | assert(isIfndef && "#ifdef shouldn't reach here"); |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1149 | CurPPLexer->MIOpt.EnterTopLevelIFNDEF(MacroNameTok.getIdentifierInfo()); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1150 | } else |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1151 | CurPPLexer->MIOpt.EnterTopLevelConditional(); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1152 | } |
| 1153 | |
| 1154 | IdentifierInfo *MII = MacroNameTok.getIdentifierInfo(); |
| 1155 | MacroInfo *MI = getMacroInfo(MII); |
| 1156 | |
| 1157 | // If there is a macro, process it. |
| 1158 | if (MI) // Mark it used. |
| 1159 | MI->setIsUsed(true); |
| 1160 | |
| 1161 | // Should we include the stuff contained by this directive? |
| 1162 | if (!MI == isIfndef) { |
| 1163 | // Yes, remember that we are inside a conditional, then lex the next token. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1164 | CurPPLexer->pushConditionalLevel(DirectiveTok.getLocation(), /*wasskip*/false, |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1165 | /*foundnonskip*/true, /*foundelse*/false); |
| 1166 | } else { |
| 1167 | // No, skip the contents of this block and return the first token after it. |
| 1168 | SkipExcludedConditionalBlock(DirectiveTok.getLocation(), |
| 1169 | /*Foundnonskip*/false, |
| 1170 | /*FoundElse*/false); |
| 1171 | } |
| 1172 | } |
| 1173 | |
| 1174 | /// HandleIfDirective - Implements the #if directive. |
| 1175 | /// |
| 1176 | void Preprocessor::HandleIfDirective(Token &IfToken, |
| 1177 | bool ReadAnyTokensBeforeDirective) { |
| 1178 | ++NumIf; |
| 1179 | |
| 1180 | // Parse and evaluation the conditional expression. |
| 1181 | IdentifierInfo *IfNDefMacro = 0; |
| 1182 | bool ConditionalTrue = EvaluateDirectiveExpression(IfNDefMacro); |
| 1183 | |
Nuno Lopes | 0049db6 | 2008-06-01 18:31:24 +0000 | [diff] [blame] | 1184 | |
| 1185 | // If this condition is equivalent to #ifndef X, and if this is the first |
| 1186 | // directive seen, handle it for the multiple-include optimization. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1187 | if (CurPPLexer->getConditionalStackDepth() == 0) { |
Nuno Lopes | 0049db6 | 2008-06-01 18:31:24 +0000 | [diff] [blame] | 1188 | if (!ReadAnyTokensBeforeDirective && IfNDefMacro) |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1189 | CurPPLexer->MIOpt.EnterTopLevelIFNDEF(IfNDefMacro); |
Nuno Lopes | 0049db6 | 2008-06-01 18:31:24 +0000 | [diff] [blame] | 1190 | else |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1191 | CurPPLexer->MIOpt.EnterTopLevelConditional(); |
Nuno Lopes | 0049db6 | 2008-06-01 18:31:24 +0000 | [diff] [blame] | 1192 | } |
| 1193 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1194 | // Should we include the stuff contained by this directive? |
| 1195 | if (ConditionalTrue) { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1196 | // Yes, remember that we are inside a conditional, then lex the next token. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1197 | CurPPLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false, |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1198 | /*foundnonskip*/true, /*foundelse*/false); |
| 1199 | } else { |
| 1200 | // No, skip the contents of this block and return the first token after it. |
| 1201 | SkipExcludedConditionalBlock(IfToken.getLocation(), /*Foundnonskip*/false, |
| 1202 | /*FoundElse*/false); |
| 1203 | } |
| 1204 | } |
| 1205 | |
| 1206 | /// HandleEndifDirective - Implements the #endif directive. |
| 1207 | /// |
| 1208 | void Preprocessor::HandleEndifDirective(Token &EndifToken) { |
| 1209 | ++NumEndif; |
| 1210 | |
| 1211 | // Check that this is the whole directive. |
| 1212 | CheckEndOfDirective("#endif"); |
| 1213 | |
| 1214 | PPConditionalInfo CondInfo; |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1215 | if (CurPPLexer->popConditionalLevel(CondInfo)) { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1216 | // No conditionals on the stack: this is an #endif without an #if. |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 1217 | Diag(EndifToken, diag::err_pp_endif_without_if); |
| 1218 | return; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1219 | } |
| 1220 | |
| 1221 | // If this the end of a top-level #endif, inform MIOpt. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1222 | if (CurPPLexer->getConditionalStackDepth() == 0) |
| 1223 | CurPPLexer->MIOpt.ExitTopLevelConditional(); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1224 | |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1225 | assert(!CondInfo.WasSkipping && !CurPPLexer->LexingRawMode && |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1226 | "This code should only be reachable in the non-skipping case!"); |
| 1227 | } |
| 1228 | |
| 1229 | |
| 1230 | void Preprocessor::HandleElseDirective(Token &Result) { |
| 1231 | ++NumElse; |
| 1232 | |
| 1233 | // #else directive in a non-skipping conditional... start skipping. |
| 1234 | CheckEndOfDirective("#else"); |
| 1235 | |
| 1236 | PPConditionalInfo CI; |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 1237 | if (CurPPLexer->popConditionalLevel(CI)) { |
| 1238 | Diag(Result, diag::pp_err_else_without_if); |
| 1239 | return; |
| 1240 | } |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1241 | |
| 1242 | // If this is a top-level #else, inform the MIOpt. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1243 | if (CurPPLexer->getConditionalStackDepth() == 0) |
| 1244 | CurPPLexer->MIOpt.EnterTopLevelConditional(); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1245 | |
| 1246 | // If this is a #else with a #else before it, report the error. |
| 1247 | if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else); |
| 1248 | |
| 1249 | // Finally, skip the rest of the contents of this block and return the first |
| 1250 | // token after it. |
| 1251 | return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true, |
| 1252 | /*FoundElse*/true); |
| 1253 | } |
| 1254 | |
| 1255 | void Preprocessor::HandleElifDirective(Token &ElifToken) { |
| 1256 | ++NumElse; |
| 1257 | |
| 1258 | // #elif directive in a non-skipping conditional... start skipping. |
| 1259 | // We don't care what the condition is, because we will always skip it (since |
| 1260 | // the block immediately before it was included). |
| 1261 | DiscardUntilEndOfDirective(); |
| 1262 | |
| 1263 | PPConditionalInfo CI; |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 1264 | if (CurPPLexer->popConditionalLevel(CI)) { |
| 1265 | Diag(ElifToken, diag::pp_err_elif_without_if); |
| 1266 | return; |
| 1267 | } |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1268 | |
| 1269 | // If this is a top-level #elif, inform the MIOpt. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1270 | if (CurPPLexer->getConditionalStackDepth() == 0) |
| 1271 | CurPPLexer->MIOpt.EnterTopLevelConditional(); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1272 | |
| 1273 | // If this is a #elif with a #else before it, report the error. |
| 1274 | if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else); |
| 1275 | |
| 1276 | // Finally, skip the rest of the contents of this block and return the first |
| 1277 | // token after it. |
| 1278 | return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true, |
| 1279 | /*FoundElse*/CI.FoundElse); |
| 1280 | } |
| 1281 | |