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" |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 15 | #include "clang/Lex/LiteralSupport.h" |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 16 | #include "clang/Lex/HeaderSearch.h" |
| 17 | #include "clang/Lex/MacroInfo.h" |
Chris Lattner | 500d329 | 2009-01-29 05:15:15 +0000 | [diff] [blame] | 18 | #include "clang/Lex/LexDiagnostic.h" |
Chris Lattner | 6e29014 | 2009-11-30 04:18:44 +0000 | [diff] [blame] | 19 | #include "clang/Basic/FileManager.h" |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 20 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/APInt.h" |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 22 | using namespace clang; |
| 23 | |
| 24 | //===----------------------------------------------------------------------===// |
| 25 | // Utility Methods for Preprocessor Directive Handling. |
| 26 | //===----------------------------------------------------------------------===// |
| 27 | |
Chris Lattner | 0301b3f | 2009-02-20 22:19:20 +0000 | [diff] [blame] | 28 | MacroInfo *Preprocessor::AllocateMacroInfo(SourceLocation L) { |
Ted Kremenek | 0ea7672 | 2008-12-15 19:56:42 +0000 | [diff] [blame] | 29 | MacroInfo *MI; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 30 | |
Ted Kremenek | 0ea7672 | 2008-12-15 19:56:42 +0000 | [diff] [blame] | 31 | if (!MICache.empty()) { |
| 32 | MI = MICache.back(); |
| 33 | MICache.pop_back(); |
Chris Lattner | 0301b3f | 2009-02-20 22:19:20 +0000 | [diff] [blame] | 34 | } else |
| 35 | MI = (MacroInfo*) BP.Allocate<MacroInfo>(); |
Ted Kremenek | 0ea7672 | 2008-12-15 19:56:42 +0000 | [diff] [blame] | 36 | new (MI) MacroInfo(L); |
| 37 | return MI; |
| 38 | } |
| 39 | |
Chris Lattner | 0301b3f | 2009-02-20 22:19:20 +0000 | [diff] [blame] | 40 | /// ReleaseMacroInfo - Release the specified MacroInfo. This memory will |
| 41 | /// be reused for allocating new MacroInfo objects. |
| 42 | void Preprocessor::ReleaseMacroInfo(MacroInfo* MI) { |
| 43 | MICache.push_back(MI); |
Chris Lattner | 685befe | 2009-02-20 22:46:43 +0000 | [diff] [blame] | 44 | MI->FreeArgumentList(BP); |
Chris Lattner | 0301b3f | 2009-02-20 22:19:20 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 48 | /// DiscardUntilEndOfDirective - Read and discard all tokens remaining on the |
| 49 | /// current line until the tok::eom token is found. |
| 50 | void Preprocessor::DiscardUntilEndOfDirective() { |
| 51 | Token Tmp; |
| 52 | do { |
| 53 | LexUnexpandedToken(Tmp); |
| 54 | } while (Tmp.isNot(tok::eom)); |
| 55 | } |
| 56 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 57 | /// ReadMacroName - Lex and validate a macro name, which occurs after a |
| 58 | /// #define or #undef. This sets the token kind to eom and discards the rest |
| 59 | /// of the macro line if the macro name is invalid. isDefineUndef is 1 if |
| 60 | /// this is due to a a #define, 2 if #undef directive, 0 if it is something |
| 61 | /// else (e.g. #ifdef). |
| 62 | void Preprocessor::ReadMacroName(Token &MacroNameTok, char isDefineUndef) { |
| 63 | // Read the token, don't allow macro expansion on it. |
| 64 | LexUnexpandedToken(MacroNameTok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 65 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 66 | // Missing macro name? |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 67 | if (MacroNameTok.is(tok::eom)) { |
| 68 | Diag(MacroNameTok, diag::err_pp_missing_macro_name); |
| 69 | return; |
| 70 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 71 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 72 | IdentifierInfo *II = MacroNameTok.getIdentifierInfo(); |
| 73 | if (II == 0) { |
| 74 | std::string Spelling = getSpelling(MacroNameTok); |
Chris Lattner | 9485d23 | 2008-12-13 20:12:40 +0000 | [diff] [blame] | 75 | const IdentifierInfo &Info = Identifiers.get(Spelling); |
| 76 | if (Info.isCPlusPlusOperatorKeyword()) |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 77 | // C++ 2.5p2: Alternative tokens behave the same as its primary token |
| 78 | // except for their spellings. |
Chris Lattner | 56b05c8 | 2008-11-18 08:02:48 +0000 | [diff] [blame] | 79 | Diag(MacroNameTok, diag::err_pp_operator_used_as_macro_name) << Spelling; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 80 | else |
| 81 | Diag(MacroNameTok, diag::err_pp_macro_not_identifier); |
| 82 | // Fall through on error. |
| 83 | } else if (isDefineUndef && II->getPPKeywordID() == tok::pp_defined) { |
| 84 | // Error if defining "defined": C99 6.10.8.4. |
| 85 | Diag(MacroNameTok, diag::err_defined_macro_name); |
| 86 | } else if (isDefineUndef && II->hasMacroDefinition() && |
| 87 | getMacroInfo(II)->isBuiltinMacro()) { |
| 88 | // Error if defining "__LINE__" and other builtins: C99 6.10.8.4. |
| 89 | if (isDefineUndef == 1) |
| 90 | Diag(MacroNameTok, diag::pp_redef_builtin_macro); |
| 91 | else |
| 92 | Diag(MacroNameTok, diag::pp_undef_builtin_macro); |
| 93 | } else { |
| 94 | // Okay, we got a good identifier node. Return it. |
| 95 | return; |
| 96 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 97 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 98 | // Invalid macro name, read and discard the rest of the line. Then set the |
| 99 | // token kind to tok::eom. |
| 100 | MacroNameTok.setKind(tok::eom); |
| 101 | return DiscardUntilEndOfDirective(); |
| 102 | } |
| 103 | |
| 104 | /// CheckEndOfDirective - Ensure that the next token is a tok::eom token. If |
Chris Lattner | ab82f41 | 2009-04-17 23:30:53 +0000 | [diff] [blame] | 105 | /// not, emit a diagnostic and consume up until the eom. If EnableMacros is |
| 106 | /// true, then we consider macros that expand to zero tokens as being ok. |
| 107 | void Preprocessor::CheckEndOfDirective(const char *DirType, bool EnableMacros) { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 108 | Token Tmp; |
Chris Lattner | ab82f41 | 2009-04-17 23:30:53 +0000 | [diff] [blame] | 109 | // Lex unexpanded tokens for most directives: macros might expand to zero |
| 110 | // tokens, causing us to miss diagnosing invalid lines. Some directives (like |
| 111 | // #line) allow empty macros. |
| 112 | if (EnableMacros) |
| 113 | Lex(Tmp); |
| 114 | else |
| 115 | LexUnexpandedToken(Tmp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 116 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 117 | // There should be no tokens after the directive, but we allow them as an |
| 118 | // extension. |
| 119 | while (Tmp.is(tok::comment)) // Skip comments in -C mode. |
| 120 | LexUnexpandedToken(Tmp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 121 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 122 | if (Tmp.isNot(tok::eom)) { |
Chris Lattner | 959875a | 2009-04-14 05:15:20 +0000 | [diff] [blame] | 123 | // Add a fixit in GNU/C99/C++ mode. Don't offer a fixit for strict-C89, |
| 124 | // because it is more trouble than it is worth to insert /**/ and check that |
| 125 | // there is no /**/ in the range also. |
| 126 | CodeModificationHint FixItHint; |
| 127 | if (Features.GNUMode || Features.C99 || Features.CPlusPlus) |
| 128 | FixItHint = CodeModificationHint::CreateInsertion(Tmp.getLocation(),"//"); |
| 129 | Diag(Tmp, diag::ext_pp_extra_tokens_at_eol) << DirType << FixItHint; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 130 | DiscardUntilEndOfDirective(); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | |
| 135 | |
| 136 | /// SkipExcludedConditionalBlock - We just read a #if or related directive and |
| 137 | /// decided that the subsequent tokens are in the #if'd out portion of the |
| 138 | /// file. Lex the rest of the file, until we see an #endif. If |
| 139 | /// FoundNonSkipPortion is true, then we have already emitted code for part of |
| 140 | /// this #if directive, so #else/#elif blocks should never be entered. If ElseOk |
| 141 | /// is true, then #else directives are ok, if not, then we have already seen one |
| 142 | /// so a #else directive is a duplicate. When this returns, the caller can lex |
| 143 | /// the first valid token. |
| 144 | void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc, |
| 145 | bool FoundNonSkipPortion, |
| 146 | bool FoundElse) { |
| 147 | ++NumSkipped; |
Ted Kremenek | f6452c5 | 2008-11-18 01:04:47 +0000 | [diff] [blame] | 148 | assert(CurTokenLexer == 0 && CurPPLexer && "Lexing a macro, not a file?"); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 149 | |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 150 | CurPPLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/false, |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 151 | FoundNonSkipPortion, FoundElse); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 152 | |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 153 | if (CurPTHLexer) { |
| 154 | PTHSkipExcludedConditionalBlock(); |
| 155 | return; |
| 156 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 157 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 158 | // Enter raw mode to disable identifier lookup (and thus macro expansion), |
| 159 | // disabling warnings, etc. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 160 | CurPPLexer->LexingRawMode = true; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 161 | Token Tok; |
| 162 | while (1) { |
Ted Kremenek | f6452c5 | 2008-11-18 01:04:47 +0000 | [diff] [blame] | 163 | if (CurLexer) |
| 164 | CurLexer->Lex(Tok); |
| 165 | else |
| 166 | CurPTHLexer->Lex(Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 167 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 168 | // If this is the end of the buffer, we have an error. |
| 169 | if (Tok.is(tok::eof)) { |
| 170 | // Emit errors for each unterminated conditional on the stack, including |
| 171 | // the current one. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 172 | while (!CurPPLexer->ConditionalStack.empty()) { |
| 173 | Diag(CurPPLexer->ConditionalStack.back().IfLoc, |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 174 | diag::err_pp_unterminated_conditional); |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 175 | CurPPLexer->ConditionalStack.pop_back(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 178 | // Just return and let the caller lex after this #include. |
| 179 | break; |
| 180 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 181 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 182 | // If this token is not a preprocessor directive, just skip it. |
| 183 | if (Tok.isNot(tok::hash) || !Tok.isAtStartOfLine()) |
| 184 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 185 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 186 | // We just parsed a # character at the start of a line, so we're in |
| 187 | // directive mode. Tell the lexer this so any newlines we see will be |
| 188 | // converted into an EOM token (this terminates the macro). |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 189 | CurPPLexer->ParsingPreprocessorDirective = true; |
Ted Kremenek | ac6b06d | 2008-11-18 00:43:07 +0000 | [diff] [blame] | 190 | if (CurLexer) CurLexer->SetCommentRetentionState(false); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 191 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 192 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 193 | // Read the next token, the directive flavor. |
| 194 | LexUnexpandedToken(Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 195 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 196 | // If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or |
| 197 | // something bogus), skip it. |
| 198 | if (Tok.isNot(tok::identifier)) { |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 199 | CurPPLexer->ParsingPreprocessorDirective = false; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 200 | // Restore comment saving mode. |
Ted Kremenek | ac6b06d | 2008-11-18 00:43:07 +0000 | [diff] [blame] | 201 | if (CurLexer) CurLexer->SetCommentRetentionState(KeepComments); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 202 | continue; |
| 203 | } |
| 204 | |
| 205 | // If the first letter isn't i or e, it isn't intesting to us. We know that |
| 206 | // this is safe in the face of spelling differences, because there is no way |
| 207 | // to spell an i/e in a strange way that is another letter. Skipping this |
| 208 | // allows us to avoid looking up the identifier info for #define/#undef and |
| 209 | // other common directives. |
| 210 | const char *RawCharData = SourceMgr.getCharacterData(Tok.getLocation()); |
| 211 | char FirstChar = RawCharData[0]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 212 | if (FirstChar >= 'a' && FirstChar <= 'z' && |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 213 | FirstChar != 'i' && FirstChar != 'e') { |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 214 | CurPPLexer->ParsingPreprocessorDirective = false; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 215 | // Restore comment saving mode. |
Ted Kremenek | ac6b06d | 2008-11-18 00:43:07 +0000 | [diff] [blame] | 216 | if (CurLexer) CurLexer->SetCommentRetentionState(KeepComments); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 217 | continue; |
| 218 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 219 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 220 | // Get the identifier name without trigraphs or embedded newlines. Note |
| 221 | // that we can't use Tok.getIdentifierInfo() because its lookup is disabled |
| 222 | // when skipping. |
Benjamin Kramer | b939a4e | 2009-12-31 13:32:38 +0000 | [diff] [blame] | 223 | char DirectiveBuf[20]; |
| 224 | llvm::StringRef Directive; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 225 | if (!Tok.needsCleaning() && Tok.getLength() < 20) { |
Benjamin Kramer | b939a4e | 2009-12-31 13:32:38 +0000 | [diff] [blame] | 226 | Directive = llvm::StringRef(RawCharData, Tok.getLength()); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 227 | } else { |
| 228 | std::string DirectiveStr = getSpelling(Tok); |
Benjamin Kramer | b939a4e | 2009-12-31 13:32:38 +0000 | [diff] [blame] | 229 | unsigned IdLen = DirectiveStr.size(); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 230 | if (IdLen >= 20) { |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 231 | CurPPLexer->ParsingPreprocessorDirective = false; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 232 | // Restore comment saving mode. |
Ted Kremenek | ac6b06d | 2008-11-18 00:43:07 +0000 | [diff] [blame] | 233 | if (CurLexer) CurLexer->SetCommentRetentionState(KeepComments); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 234 | continue; |
| 235 | } |
Benjamin Kramer | b939a4e | 2009-12-31 13:32:38 +0000 | [diff] [blame] | 236 | memcpy(DirectiveBuf, &DirectiveStr[0], IdLen); |
| 237 | Directive = llvm::StringRef(DirectiveBuf, IdLen); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 238 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 239 | |
Benjamin Kramer | b939a4e | 2009-12-31 13:32:38 +0000 | [diff] [blame] | 240 | if (Directive.startswith("if")) { |
| 241 | llvm::StringRef Sub = Directive.substr(2); |
| 242 | if (Sub.empty() || // "if" |
| 243 | Sub == "def" || // "ifdef" |
| 244 | Sub == "ndef") { // "ifndef" |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 245 | // We know the entire #if/#ifdef/#ifndef block will be skipped, don't |
| 246 | // bother parsing the condition. |
| 247 | DiscardUntilEndOfDirective(); |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 248 | CurPPLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true, |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 249 | /*foundnonskip*/false, |
| 250 | /*fnddelse*/false); |
| 251 | } |
Benjamin Kramer | b939a4e | 2009-12-31 13:32:38 +0000 | [diff] [blame] | 252 | } else if (Directive[0] == 'e') { |
| 253 | llvm::StringRef Sub = Directive.substr(1); |
| 254 | if (Sub == "ndif") { // "endif" |
Chris Lattner | 35410d5 | 2009-04-14 05:07:49 +0000 | [diff] [blame] | 255 | CheckEndOfDirective("endif"); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 256 | PPConditionalInfo CondInfo; |
| 257 | CondInfo.WasSkipping = true; // Silence bogus warning. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 258 | bool InCond = CurPPLexer->popConditionalLevel(CondInfo); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 259 | InCond = InCond; // Silence warning in no-asserts mode. |
| 260 | assert(!InCond && "Can't be skipping if not in a conditional!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 261 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 262 | // If we popped the outermost skipping block, we're done skipping! |
| 263 | if (!CondInfo.WasSkipping) |
| 264 | break; |
Benjamin Kramer | b939a4e | 2009-12-31 13:32:38 +0000 | [diff] [blame] | 265 | } else if (Sub == "lse") { // "else". |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 266 | // #else directive in a skipping conditional. If not in some other |
| 267 | // skipping conditional, and if #else hasn't already been seen, enter it |
| 268 | // as a non-skipping conditional. |
Chris Lattner | 8fe00e7 | 2009-04-18 01:34:22 +0000 | [diff] [blame] | 269 | DiscardUntilEndOfDirective(); // C99 6.10p4. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 270 | PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 271 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 272 | // If this is a #else with a #else before it, report the error. |
| 273 | if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_else_after_else); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 274 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 275 | // Note that we've seen a #else in this conditional. |
| 276 | CondInfo.FoundElse = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 277 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 278 | // If the conditional is at the top level, and the #if block wasn't |
| 279 | // entered, enter the #else block now. |
| 280 | if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) { |
| 281 | CondInfo.FoundNonSkip = true; |
| 282 | break; |
| 283 | } |
Benjamin Kramer | b939a4e | 2009-12-31 13:32:38 +0000 | [diff] [blame] | 284 | } else if (Sub == "lif") { // "elif". |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 285 | PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel(); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 286 | |
| 287 | bool ShouldEnter; |
| 288 | // If this is in a skipping block or if we're already handled this #if |
| 289 | // block, don't bother parsing the condition. |
| 290 | if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) { |
| 291 | DiscardUntilEndOfDirective(); |
| 292 | ShouldEnter = false; |
| 293 | } else { |
| 294 | // Restore the value of LexingRawMode so that identifiers are |
| 295 | // looked up, etc, inside the #elif expression. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 296 | assert(CurPPLexer->LexingRawMode && "We have to be skipping here!"); |
| 297 | CurPPLexer->LexingRawMode = false; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 298 | IdentifierInfo *IfNDefMacro = 0; |
| 299 | ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro); |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 300 | CurPPLexer->LexingRawMode = true; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 301 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 302 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 303 | // If this is a #elif with a #else before it, report the error. |
| 304 | if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_elif_after_else); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 305 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 306 | // If this condition is true, enter it! |
| 307 | if (ShouldEnter) { |
| 308 | CondInfo.FoundNonSkip = true; |
| 309 | break; |
| 310 | } |
| 311 | } |
| 312 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 313 | |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 314 | CurPPLexer->ParsingPreprocessorDirective = false; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 315 | // Restore comment saving mode. |
Ted Kremenek | ac6b06d | 2008-11-18 00:43:07 +0000 | [diff] [blame] | 316 | if (CurLexer) CurLexer->SetCommentRetentionState(KeepComments); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | // Finally, if we are out of the conditional (saw an #endif or ran off the end |
| 320 | // of the file, just stop skipping and return to lexing whatever came after |
| 321 | // the #if block. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 322 | CurPPLexer->LexingRawMode = false; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 323 | } |
| 324 | |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 325 | void Preprocessor::PTHSkipExcludedConditionalBlock() { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 326 | |
| 327 | while (1) { |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 328 | assert(CurPTHLexer); |
| 329 | assert(CurPTHLexer->LexingRawMode == false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 330 | |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 331 | // Skip to the next '#else', '#elif', or #endif. |
| 332 | if (CurPTHLexer->SkipBlock()) { |
| 333 | // We have reached an #endif. Both the '#' and 'endif' tokens |
| 334 | // have been consumed by the PTHLexer. Just pop off the condition level. |
| 335 | PPConditionalInfo CondInfo; |
| 336 | bool InCond = CurPTHLexer->popConditionalLevel(CondInfo); |
| 337 | InCond = InCond; // Silence warning in no-asserts mode. |
| 338 | assert(!InCond && "Can't be skipping if not in a conditional!"); |
| 339 | break; |
| 340 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 341 | |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 342 | // We have reached a '#else' or '#elif'. Lex the next token to get |
| 343 | // the directive flavor. |
| 344 | Token Tok; |
| 345 | LexUnexpandedToken(Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 346 | |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 347 | // We can actually look up the IdentifierInfo here since we aren't in |
| 348 | // raw mode. |
| 349 | tok::PPKeywordKind K = Tok.getIdentifierInfo()->getPPKeywordID(); |
| 350 | |
| 351 | if (K == tok::pp_else) { |
| 352 | // #else: Enter the else condition. We aren't in a nested condition |
| 353 | // since we skip those. We're always in the one matching the last |
| 354 | // blocked we skipped. |
| 355 | PPConditionalInfo &CondInfo = CurPTHLexer->peekConditionalLevel(); |
| 356 | // Note that we've seen a #else in this conditional. |
| 357 | CondInfo.FoundElse = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 358 | |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 359 | // If the #if block wasn't entered then enter the #else block now. |
| 360 | if (!CondInfo.FoundNonSkip) { |
| 361 | CondInfo.FoundNonSkip = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 362 | |
Daniel Dunbar | 8533bd5 | 2009-04-13 17:57:49 +0000 | [diff] [blame] | 363 | // Scan until the eom token. |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 364 | CurPTHLexer->ParsingPreprocessorDirective = true; |
Daniel Dunbar | 8533bd5 | 2009-04-13 17:57:49 +0000 | [diff] [blame] | 365 | DiscardUntilEndOfDirective(); |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 366 | CurPTHLexer->ParsingPreprocessorDirective = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 367 | |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 368 | break; |
| 369 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 370 | |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 371 | // Otherwise skip this block. |
| 372 | continue; |
| 373 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 374 | |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 375 | assert(K == tok::pp_elif); |
| 376 | PPConditionalInfo &CondInfo = CurPTHLexer->peekConditionalLevel(); |
| 377 | |
| 378 | // If this is a #elif with a #else before it, report the error. |
| 379 | if (CondInfo.FoundElse) |
| 380 | Diag(Tok, diag::pp_err_elif_after_else); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 381 | |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 382 | // If this is in a skipping block or if we're already handled this #if |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 383 | // block, don't bother parsing the condition. We just skip this block. |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 384 | if (CondInfo.FoundNonSkip) |
| 385 | continue; |
| 386 | |
| 387 | // Evaluate the condition of the #elif. |
| 388 | IdentifierInfo *IfNDefMacro = 0; |
| 389 | CurPTHLexer->ParsingPreprocessorDirective = true; |
| 390 | bool ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro); |
| 391 | CurPTHLexer->ParsingPreprocessorDirective = false; |
| 392 | |
| 393 | // If this condition is true, enter it! |
| 394 | if (ShouldEnter) { |
| 395 | CondInfo.FoundNonSkip = true; |
| 396 | break; |
| 397 | } |
| 398 | |
| 399 | // Otherwise, skip this block and go to the next one. |
| 400 | continue; |
| 401 | } |
| 402 | } |
| 403 | |
Chris Lattner | 1072509 | 2008-03-09 04:17:44 +0000 | [diff] [blame] | 404 | /// LookupFile - Given a "foo" or <foo> reference, look up the indicated file, |
| 405 | /// return null on failure. isAngled indicates whether the file reference is |
| 406 | /// for system #include's or not (i.e. using <> instead of ""). |
| 407 | const FileEntry *Preprocessor::LookupFile(const char *FilenameStart, |
| 408 | const char *FilenameEnd, |
Chris Lattner | 804f652 | 2010-01-10 00:24:58 +0000 | [diff] [blame^] | 409 | SourceLocation FilenameTokLoc, |
Chris Lattner | 1072509 | 2008-03-09 04:17:44 +0000 | [diff] [blame] | 410 | bool isAngled, |
| 411 | const DirectoryLookup *FromDir, |
| 412 | const DirectoryLookup *&CurDir) { |
| 413 | // If the header lookup mechanism may be relative to the current file, pass in |
| 414 | // info about where the current file is. |
| 415 | const FileEntry *CurFileEnt = 0; |
| 416 | if (!FromDir) { |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 417 | FileID FID = getCurrentFileLexer()->getFileID(); |
| 418 | CurFileEnt = SourceMgr.getFileEntryForID(FID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 419 | |
Chris Lattner | be5c64d | 2009-02-04 19:45:07 +0000 | [diff] [blame] | 420 | // If there is no file entry associated with this file, it must be the |
| 421 | // predefines buffer. Any other file is not lexed with a normal lexer, so |
| 422 | // it won't be scanned for preprocessor directives. If we have the |
| 423 | // predefines buffer, resolve #include references (which come from the |
| 424 | // -include command line argument) as if they came from the main file, this |
| 425 | // affects file lookup etc. |
| 426 | if (CurFileEnt == 0) { |
| 427 | FID = SourceMgr.getMainFileID(); |
| 428 | CurFileEnt = SourceMgr.getFileEntryForID(FID); |
| 429 | } |
Chris Lattner | 1072509 | 2008-03-09 04:17:44 +0000 | [diff] [blame] | 430 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 431 | |
Chris Lattner | 1072509 | 2008-03-09 04:17:44 +0000 | [diff] [blame] | 432 | // Do a standard file entry lookup. |
| 433 | CurDir = CurDirLookup; |
| 434 | const FileEntry *FE = |
Chris Lattner | be5c64d | 2009-02-04 19:45:07 +0000 | [diff] [blame] | 435 | HeaderInfo.LookupFile(FilenameStart, FilenameEnd, |
| 436 | isAngled, FromDir, CurDir, CurFileEnt); |
Chris Lattner | 804f652 | 2010-01-10 00:24:58 +0000 | [diff] [blame^] | 437 | if (FE) { |
| 438 | // Warn about normal quoted #include from framework headers. Since |
| 439 | // framework headers are published (both public and private ones) they |
| 440 | // should not do relative searches, they should do an include relative to |
| 441 | // their framework. |
| 442 | if (!isAngled && CurDir && FilenameTokLoc.isValid() && |
| 443 | CurDir->isFramework() && CurDir == CurDirLookup) |
| 444 | Diag(FilenameTokLoc, diag::warn_pp_relative_include_from_framework); |
| 445 | return FE; |
| 446 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 447 | |
Chris Lattner | 1072509 | 2008-03-09 04:17:44 +0000 | [diff] [blame] | 448 | // Otherwise, see if this is a subframework header. If so, this is relative |
| 449 | // to one of the headers on the #include stack. Walk the list of the current |
| 450 | // headers on the #include stack and pass them to HeaderInfo. |
Ted Kremenek | 81d24e1 | 2008-11-20 16:19:53 +0000 | [diff] [blame] | 451 | if (IsFileLexer()) { |
Ted Kremenek | 41938c8 | 2008-11-19 21:57:25 +0000 | [diff] [blame] | 452 | if ((CurFileEnt = SourceMgr.getFileEntryForID(CurPPLexer->getFileID()))) |
Chris Lattner | 1072509 | 2008-03-09 04:17:44 +0000 | [diff] [blame] | 453 | if ((FE = HeaderInfo.LookupSubframeworkHeader(FilenameStart, FilenameEnd, |
| 454 | CurFileEnt))) |
| 455 | return FE; |
| 456 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 457 | |
Chris Lattner | 1072509 | 2008-03-09 04:17:44 +0000 | [diff] [blame] | 458 | for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) { |
| 459 | IncludeStackInfo &ISEntry = IncludeMacroStack[e-i-1]; |
Ted Kremenek | 81d24e1 | 2008-11-20 16:19:53 +0000 | [diff] [blame] | 460 | if (IsFileLexer(ISEntry)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 461 | if ((CurFileEnt = |
Ted Kremenek | 41938c8 | 2008-11-19 21:57:25 +0000 | [diff] [blame] | 462 | SourceMgr.getFileEntryForID(ISEntry.ThePPLexer->getFileID()))) |
Chris Lattner | 1072509 | 2008-03-09 04:17:44 +0000 | [diff] [blame] | 463 | if ((FE = HeaderInfo.LookupSubframeworkHeader(FilenameStart, |
| 464 | FilenameEnd, CurFileEnt))) |
| 465 | return FE; |
| 466 | } |
| 467 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 468 | |
Chris Lattner | 1072509 | 2008-03-09 04:17:44 +0000 | [diff] [blame] | 469 | // Otherwise, we really couldn't find the file. |
| 470 | return 0; |
| 471 | } |
| 472 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 473 | |
| 474 | //===----------------------------------------------------------------------===// |
| 475 | // Preprocessor Directive Handling. |
| 476 | //===----------------------------------------------------------------------===// |
| 477 | |
| 478 | /// HandleDirective - This callback is invoked when the lexer sees a # token |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 479 | /// at the start of a line. This consumes the directive, modifies the |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 480 | /// lexer/preprocessor state, and advances the lexer(s) so that the next token |
| 481 | /// read is the correct one. |
| 482 | void Preprocessor::HandleDirective(Token &Result) { |
| 483 | // FIXME: Traditional: # with whitespace before it not recognized by K&R? |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 484 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 485 | // We just parsed a # character at the start of a line, so we're in directive |
| 486 | // mode. Tell the lexer this so any newlines we see will be converted into an |
| 487 | // EOM token (which terminates the directive). |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 488 | CurPPLexer->ParsingPreprocessorDirective = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 489 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 490 | ++NumDirectives; |
Chris Lattner | 1d9c54d | 2009-12-14 04:54:40 +0000 | [diff] [blame] | 491 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 492 | // We are about to read a token. For the multiple-include optimization FA to |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 493 | // work, we have to remember if we had read any tokens *before* this |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 494 | // pp-directive. |
Chris Lattner | 1d9c54d | 2009-12-14 04:54:40 +0000 | [diff] [blame] | 495 | bool ReadAnyTokensBeforeDirective =CurPPLexer->MIOpt.getHasReadAnyTokensVal(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 496 | |
Chris Lattner | 42aa16c | 2009-03-18 21:00:25 +0000 | [diff] [blame] | 497 | // Save the '#' token in case we need to return it later. |
| 498 | Token SavedHash = Result; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 499 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 500 | // Read the next token, the directive flavor. This isn't expanded due to |
| 501 | // C99 6.10.3p8. |
| 502 | LexUnexpandedToken(Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 503 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 504 | // C99 6.10.3p11: Is this preprocessor directive in macro invocation? e.g.: |
| 505 | // #define A(x) #x |
| 506 | // A(abc |
| 507 | // #warning blah |
| 508 | // def) |
| 509 | // If so, the user is relying on non-portable behavior, emit a diagnostic. |
| 510 | if (InMacroArgs) |
| 511 | Diag(Result, diag::ext_embedded_directive); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 512 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 513 | TryAgain: |
| 514 | switch (Result.getKind()) { |
| 515 | case tok::eom: |
| 516 | return; // null directive. |
| 517 | case tok::comment: |
| 518 | // Handle stuff like "# /*foo*/ define X" in -E -C mode. |
| 519 | LexUnexpandedToken(Result); |
| 520 | goto TryAgain; |
| 521 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 522 | case tok::numeric_constant: // # 7 GNU line marker directive. |
Chris Lattner | 5f607c4 | 2009-03-18 20:41:10 +0000 | [diff] [blame] | 523 | if (getLangOptions().AsmPreprocessor) |
| 524 | break; // # 4 is not a preprocessor directive in .S files. |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 525 | return HandleDigitDirective(Result); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 526 | default: |
| 527 | IdentifierInfo *II = Result.getIdentifierInfo(); |
| 528 | if (II == 0) break; // Not an identifier. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 529 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 530 | // Ask what the preprocessor keyword ID is. |
| 531 | switch (II->getPPKeywordID()) { |
| 532 | default: break; |
| 533 | // C99 6.10.1 - Conditional Inclusion. |
| 534 | case tok::pp_if: |
| 535 | return HandleIfDirective(Result, ReadAnyTokensBeforeDirective); |
| 536 | case tok::pp_ifdef: |
| 537 | return HandleIfdefDirective(Result, false, true/*not valid for miopt*/); |
| 538 | case tok::pp_ifndef: |
| 539 | return HandleIfdefDirective(Result, true, ReadAnyTokensBeforeDirective); |
| 540 | case tok::pp_elif: |
| 541 | return HandleElifDirective(Result); |
| 542 | case tok::pp_else: |
| 543 | return HandleElseDirective(Result); |
| 544 | case tok::pp_endif: |
| 545 | return HandleEndifDirective(Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 546 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 547 | // C99 6.10.2 - Source File Inclusion. |
| 548 | case tok::pp_include: |
Chris Lattner | b8e240e | 2009-04-08 18:24:34 +0000 | [diff] [blame] | 549 | return HandleIncludeDirective(Result); // Handle #include. |
| 550 | case tok::pp___include_macros: |
Chris Lattner | de07665 | 2009-04-08 18:46:40 +0000 | [diff] [blame] | 551 | return HandleIncludeMacrosDirective(Result); // Handle -imacros. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 552 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 553 | // C99 6.10.3 - Macro Replacement. |
| 554 | case tok::pp_define: |
| 555 | return HandleDefineDirective(Result); |
| 556 | case tok::pp_undef: |
| 557 | return HandleUndefDirective(Result); |
| 558 | |
| 559 | // C99 6.10.4 - Line Control. |
| 560 | case tok::pp_line: |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 561 | return HandleLineDirective(Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 562 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 563 | // C99 6.10.5 - Error Directive. |
| 564 | case tok::pp_error: |
| 565 | return HandleUserDiagnosticDirective(Result, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 566 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 567 | // C99 6.10.6 - Pragma Directive. |
| 568 | case tok::pp_pragma: |
| 569 | return HandlePragmaDirective(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 570 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 571 | // GNU Extensions. |
| 572 | case tok::pp_import: |
| 573 | return HandleImportDirective(Result); |
| 574 | case tok::pp_include_next: |
| 575 | return HandleIncludeNextDirective(Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 576 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 577 | case tok::pp_warning: |
| 578 | Diag(Result, diag::ext_pp_warning_directive); |
| 579 | return HandleUserDiagnosticDirective(Result, true); |
| 580 | case tok::pp_ident: |
| 581 | return HandleIdentSCCSDirective(Result); |
| 582 | case tok::pp_sccs: |
| 583 | return HandleIdentSCCSDirective(Result); |
| 584 | case tok::pp_assert: |
| 585 | //isExtension = true; // FIXME: implement #assert |
| 586 | break; |
| 587 | case tok::pp_unassert: |
| 588 | //isExtension = true; // FIXME: implement #unassert |
| 589 | break; |
| 590 | } |
| 591 | break; |
| 592 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 593 | |
Chris Lattner | 42aa16c | 2009-03-18 21:00:25 +0000 | [diff] [blame] | 594 | // If this is a .S file, treat unknown # directives as non-preprocessor |
| 595 | // directives. This is important because # may be a comment or introduce |
| 596 | // various pseudo-ops. Just return the # token and push back the following |
| 597 | // token to be lexed next time. |
| 598 | if (getLangOptions().AsmPreprocessor) { |
Daniel Dunbar | 3d399a0 | 2009-07-13 21:48:50 +0000 | [diff] [blame] | 599 | Token *Toks = new Token[2]; |
Chris Lattner | 42aa16c | 2009-03-18 21:00:25 +0000 | [diff] [blame] | 600 | // Return the # and the token after it. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 601 | Toks[0] = SavedHash; |
Chris Lattner | 42aa16c | 2009-03-18 21:00:25 +0000 | [diff] [blame] | 602 | Toks[1] = Result; |
| 603 | // Enter this token stream so that we re-lex the tokens. Make sure to |
| 604 | // enable macro expansion, in case the token after the # is an identifier |
| 605 | // that is expanded. |
| 606 | EnterTokenStream(Toks, 2, false, true); |
| 607 | return; |
| 608 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 609 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 610 | // If we reached here, the preprocessing token is not valid! |
| 611 | Diag(Result, diag::err_pp_invalid_directive); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 612 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 613 | // Read the rest of the PP line. |
| 614 | DiscardUntilEndOfDirective(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 615 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 616 | // Okay, we're done parsing the directive. |
| 617 | } |
| 618 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 619 | /// GetLineValue - Convert a numeric token into an unsigned value, emitting |
| 620 | /// Diagnostic DiagID if it is invalid, and returning the value in Val. |
| 621 | static bool GetLineValue(Token &DigitTok, unsigned &Val, |
| 622 | unsigned DiagID, Preprocessor &PP) { |
| 623 | if (DigitTok.isNot(tok::numeric_constant)) { |
| 624 | PP.Diag(DigitTok, DiagID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 625 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 626 | if (DigitTok.isNot(tok::eom)) |
| 627 | PP.DiscardUntilEndOfDirective(); |
| 628 | return true; |
| 629 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 630 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 631 | llvm::SmallString<64> IntegerBuffer; |
| 632 | IntegerBuffer.resize(DigitTok.getLength()); |
| 633 | const char *DigitTokBegin = &IntegerBuffer[0]; |
| 634 | unsigned ActualLength = PP.getSpelling(DigitTok, DigitTokBegin); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 635 | |
Chris Lattner | dc8c90d | 2009-04-18 18:35:15 +0000 | [diff] [blame] | 636 | // Verify that we have a simple digit-sequence, and compute the value. This |
| 637 | // is always a simple digit string computed in decimal, so we do this manually |
| 638 | // here. |
| 639 | Val = 0; |
| 640 | for (unsigned i = 0; i != ActualLength; ++i) { |
| 641 | if (!isdigit(DigitTokBegin[i])) { |
| 642 | PP.Diag(PP.AdvanceToTokenCharacter(DigitTok.getLocation(), i), |
| 643 | diag::err_pp_line_digit_sequence); |
| 644 | PP.DiscardUntilEndOfDirective(); |
| 645 | return true; |
| 646 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 647 | |
Chris Lattner | dc8c90d | 2009-04-18 18:35:15 +0000 | [diff] [blame] | 648 | unsigned NextVal = Val*10+(DigitTokBegin[i]-'0'); |
| 649 | if (NextVal < Val) { // overflow. |
| 650 | PP.Diag(DigitTok, DiagID); |
| 651 | PP.DiscardUntilEndOfDirective(); |
| 652 | return true; |
| 653 | } |
| 654 | Val = NextVal; |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 655 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 656 | |
| 657 | // Reject 0, this is needed both by #line numbers and flags. |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 658 | if (Val == 0) { |
| 659 | PP.Diag(DigitTok, DiagID); |
| 660 | PP.DiscardUntilEndOfDirective(); |
| 661 | return true; |
| 662 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 663 | |
Chris Lattner | dc8c90d | 2009-04-18 18:35:15 +0000 | [diff] [blame] | 664 | if (DigitTokBegin[0] == '0') |
| 665 | PP.Diag(DigitTok.getLocation(), diag::warn_pp_line_decimal); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 666 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 667 | return false; |
| 668 | } |
| 669 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 670 | /// HandleLineDirective - Handle #line directive: C99 6.10.4. The two |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 671 | /// acceptable forms are: |
| 672 | /// # line digit-sequence |
| 673 | /// # line digit-sequence "s-char-sequence" |
| 674 | void Preprocessor::HandleLineDirective(Token &Tok) { |
| 675 | // Read the line # and string argument. Per C99 6.10.4p5, these tokens are |
| 676 | // expanded. |
| 677 | Token DigitTok; |
| 678 | Lex(DigitTok); |
| 679 | |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 680 | // Validate the number and convert it to an unsigned. |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 681 | unsigned LineNo; |
Chris Lattner | dc8c90d | 2009-04-18 18:35:15 +0000 | [diff] [blame] | 682 | if (GetLineValue(DigitTok, LineNo, diag::err_pp_line_requires_integer,*this)) |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 683 | return; |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 684 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 685 | // Enforce C99 6.10.4p3: "The digit sequence shall not specify ... a |
| 686 | // number greater than 2147483647". C90 requires that the line # be <= 32767. |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 687 | unsigned LineLimit = Features.C99 ? 2147483648U : 32768U; |
| 688 | if (LineNo >= LineLimit) |
| 689 | Diag(DigitTok, diag::ext_pp_line_too_big) << LineLimit; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 690 | |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 691 | int FilenameID = -1; |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 692 | Token StrTok; |
| 693 | Lex(StrTok); |
| 694 | |
| 695 | // If the StrTok is "eom", then it wasn't present. Otherwise, it must be a |
| 696 | // string followed by eom. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 697 | if (StrTok.is(tok::eom)) |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 698 | ; // ok |
| 699 | else if (StrTok.isNot(tok::string_literal)) { |
| 700 | Diag(StrTok, diag::err_pp_line_invalid_filename); |
| 701 | DiscardUntilEndOfDirective(); |
| 702 | return; |
| 703 | } else { |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 704 | // Parse and validate the string, converting it into a unique ID. |
| 705 | StringLiteralParser Literal(&StrTok, 1, *this); |
| 706 | assert(!Literal.AnyWide && "Didn't allow wide strings in"); |
| 707 | if (Literal.hadError) |
| 708 | return DiscardUntilEndOfDirective(); |
| 709 | if (Literal.Pascal) { |
| 710 | Diag(StrTok, diag::err_pp_linemarker_invalid_filename); |
| 711 | return DiscardUntilEndOfDirective(); |
| 712 | } |
| 713 | FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString(), |
| 714 | Literal.GetStringLength()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 715 | |
Chris Lattner | ab82f41 | 2009-04-17 23:30:53 +0000 | [diff] [blame] | 716 | // Verify that there is nothing after the string, other than EOM. Because |
| 717 | // of C99 6.10.4p5, macros that expand to empty tokens are ok. |
| 718 | CheckEndOfDirective("line", true); |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 719 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 720 | |
Chris Lattner | 4c4ea17 | 2009-02-03 21:52:55 +0000 | [diff] [blame] | 721 | SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 722 | |
Chris Lattner | 1662938 | 2009-03-27 17:13:49 +0000 | [diff] [blame] | 723 | if (Callbacks) |
| 724 | Callbacks->FileChanged(DigitTok.getLocation(), PPCallbacks::RenameFile, |
| 725 | SrcMgr::C_User); |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 726 | } |
| 727 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 728 | /// ReadLineMarkerFlags - Parse and validate any flags at the end of a GNU line |
| 729 | /// marker directive. |
| 730 | static bool ReadLineMarkerFlags(bool &IsFileEntry, bool &IsFileExit, |
| 731 | bool &IsSystemHeader, bool &IsExternCHeader, |
| 732 | Preprocessor &PP) { |
| 733 | unsigned FlagVal; |
| 734 | Token FlagTok; |
| 735 | PP.Lex(FlagTok); |
| 736 | if (FlagTok.is(tok::eom)) return false; |
| 737 | if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP)) |
| 738 | return true; |
| 739 | |
| 740 | if (FlagVal == 1) { |
| 741 | IsFileEntry = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 742 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 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 | } else if (FlagVal == 2) { |
| 748 | IsFileExit = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 749 | |
Chris Lattner | 137b6a6 | 2009-02-04 06:25:26 +0000 | [diff] [blame] | 750 | SourceManager &SM = PP.getSourceManager(); |
| 751 | // If we are leaving the current presumed file, check to make sure the |
| 752 | // presumed include stack isn't empty! |
| 753 | FileID CurFileID = |
| 754 | SM.getDecomposedInstantiationLoc(FlagTok.getLocation()).first; |
| 755 | PresumedLoc PLoc = SM.getPresumedLoc(FlagTok.getLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 756 | |
Chris Lattner | 137b6a6 | 2009-02-04 06:25:26 +0000 | [diff] [blame] | 757 | // If there is no include loc (main file) or if the include loc is in a |
| 758 | // different physical file, then we aren't in a "1" line marker flag region. |
| 759 | SourceLocation IncLoc = PLoc.getIncludeLoc(); |
| 760 | if (IncLoc.isInvalid() || |
| 761 | SM.getDecomposedInstantiationLoc(IncLoc).first != CurFileID) { |
| 762 | PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_pop); |
| 763 | PP.DiscardUntilEndOfDirective(); |
| 764 | return true; |
| 765 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 766 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 767 | PP.Lex(FlagTok); |
| 768 | if (FlagTok.is(tok::eom)) return false; |
| 769 | if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP)) |
| 770 | return true; |
| 771 | } |
| 772 | |
| 773 | // We must have 3 if there are still flags. |
| 774 | if (FlagVal != 3) { |
| 775 | PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag); |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 776 | PP.DiscardUntilEndOfDirective(); |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 777 | return true; |
| 778 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 779 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 780 | IsSystemHeader = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 781 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 782 | PP.Lex(FlagTok); |
| 783 | if (FlagTok.is(tok::eom)) return false; |
Chris Lattner | 9d79eba | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 784 | if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP)) |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 785 | return true; |
| 786 | |
| 787 | // We must have 4 if there is yet another flag. |
| 788 | if (FlagVal != 4) { |
| 789 | PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag); |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 790 | PP.DiscardUntilEndOfDirective(); |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 791 | return true; |
| 792 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 793 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 794 | IsExternCHeader = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 795 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 796 | PP.Lex(FlagTok); |
| 797 | if (FlagTok.is(tok::eom)) return false; |
| 798 | |
| 799 | // There are no more valid flags here. |
| 800 | PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag); |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 801 | PP.DiscardUntilEndOfDirective(); |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 802 | return true; |
| 803 | } |
| 804 | |
| 805 | /// HandleDigitDirective - Handle a GNU line marker directive, whose syntax is |
| 806 | /// one of the following forms: |
| 807 | /// |
| 808 | /// # 42 |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 809 | /// # 42 "file" ('1' | '2')? |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 810 | /// # 42 "file" ('1' | '2')? '3' '4'? |
| 811 | /// |
| 812 | void Preprocessor::HandleDigitDirective(Token &DigitTok) { |
| 813 | // Validate the number and convert it to an unsigned. GNU does not have a |
| 814 | // line # limit other than it fit in 32-bits. |
| 815 | unsigned LineNo; |
| 816 | if (GetLineValue(DigitTok, LineNo, diag::err_pp_linemarker_requires_integer, |
| 817 | *this)) |
| 818 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 819 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 820 | Token StrTok; |
| 821 | Lex(StrTok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 822 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 823 | bool IsFileEntry = false, IsFileExit = false; |
| 824 | bool IsSystemHeader = false, IsExternCHeader = false; |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 825 | int FilenameID = -1; |
| 826 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 827 | // If the StrTok is "eom", then it wasn't present. Otherwise, it must be a |
| 828 | // string followed by eom. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 829 | if (StrTok.is(tok::eom)) |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 830 | ; // ok |
| 831 | else if (StrTok.isNot(tok::string_literal)) { |
| 832 | Diag(StrTok, diag::err_pp_linemarker_invalid_filename); |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 833 | return DiscardUntilEndOfDirective(); |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 834 | } else { |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 835 | // Parse and validate the string, converting it into a unique ID. |
| 836 | StringLiteralParser Literal(&StrTok, 1, *this); |
| 837 | assert(!Literal.AnyWide && "Didn't allow wide strings in"); |
| 838 | if (Literal.hadError) |
| 839 | return DiscardUntilEndOfDirective(); |
| 840 | if (Literal.Pascal) { |
| 841 | Diag(StrTok, diag::err_pp_linemarker_invalid_filename); |
| 842 | return DiscardUntilEndOfDirective(); |
| 843 | } |
| 844 | FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString(), |
| 845 | Literal.GetStringLength()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 846 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 847 | // If a filename was present, read any flags that are present. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 848 | if (ReadLineMarkerFlags(IsFileEntry, IsFileExit, |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 849 | IsSystemHeader, IsExternCHeader, *this)) |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 850 | return; |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 851 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 852 | |
Chris Lattner | 9d79eba | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 853 | // Create a line note with this information. |
| 854 | SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 855 | IsFileEntry, IsFileExit, |
Chris Lattner | 9d79eba | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 856 | IsSystemHeader, IsExternCHeader); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 857 | |
Chris Lattner | 1662938 | 2009-03-27 17:13:49 +0000 | [diff] [blame] | 858 | // If the preprocessor has callbacks installed, notify them of the #line |
| 859 | // change. This is used so that the line marker comes out in -E mode for |
| 860 | // example. |
| 861 | if (Callbacks) { |
| 862 | PPCallbacks::FileChangeReason Reason = PPCallbacks::RenameFile; |
| 863 | if (IsFileEntry) |
| 864 | Reason = PPCallbacks::EnterFile; |
| 865 | else if (IsFileExit) |
| 866 | Reason = PPCallbacks::ExitFile; |
| 867 | SrcMgr::CharacteristicKind FileKind = SrcMgr::C_User; |
| 868 | if (IsExternCHeader) |
| 869 | FileKind = SrcMgr::C_ExternCSystem; |
| 870 | else if (IsSystemHeader) |
| 871 | FileKind = SrcMgr::C_System; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 872 | |
Chris Lattner | 1662938 | 2009-03-27 17:13:49 +0000 | [diff] [blame] | 873 | Callbacks->FileChanged(DigitTok.getLocation(), Reason, FileKind); |
| 874 | } |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | |
Chris Lattner | 099dd05 | 2009-01-26 05:30:54 +0000 | [diff] [blame] | 878 | /// HandleUserDiagnosticDirective - Handle a #warning or #error directive. |
| 879 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 880 | void Preprocessor::HandleUserDiagnosticDirective(Token &Tok, |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 881 | bool isWarning) { |
Chris Lattner | 099dd05 | 2009-01-26 05:30:54 +0000 | [diff] [blame] | 882 | // PTH doesn't emit #warning or #error directives. |
| 883 | if (CurPTHLexer) |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 884 | return CurPTHLexer->DiscardToEndOfLine(); |
| 885 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 886 | // Read the rest of the line raw. We do this because we don't want macros |
| 887 | // to be expanded and we don't require that the tokens be valid preprocessing |
| 888 | // tokens. For example, this is allowed: "#warning ` 'foo". GCC does |
| 889 | // collapse multiple consequtive white space between tokens, but this isn't |
| 890 | // specified by the standard. |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 891 | std::string Message = CurLexer->ReadToEndOfLine(); |
| 892 | if (isWarning) |
| 893 | Diag(Tok, diag::pp_hash_warning) << Message; |
| 894 | else |
| 895 | Diag(Tok, diag::err_pp_hash_error) << Message; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 896 | } |
| 897 | |
| 898 | /// HandleIdentSCCSDirective - Handle a #ident/#sccs directive. |
| 899 | /// |
| 900 | void Preprocessor::HandleIdentSCCSDirective(Token &Tok) { |
| 901 | // Yes, this directive is an extension. |
| 902 | Diag(Tok, diag::ext_pp_ident_directive); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 903 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 904 | // Read the string argument. |
| 905 | Token StrTok; |
| 906 | Lex(StrTok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 907 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 908 | // If the token kind isn't a string, it's a malformed directive. |
| 909 | if (StrTok.isNot(tok::string_literal) && |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 910 | StrTok.isNot(tok::wide_string_literal)) { |
| 911 | Diag(StrTok, diag::err_pp_malformed_ident); |
Chris Lattner | 099dd05 | 2009-01-26 05:30:54 +0000 | [diff] [blame] | 912 | if (StrTok.isNot(tok::eom)) |
| 913 | DiscardUntilEndOfDirective(); |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 914 | return; |
| 915 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 916 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 917 | // Verify that there is nothing after the string, other than EOM. |
Chris Lattner | 35410d5 | 2009-04-14 05:07:49 +0000 | [diff] [blame] | 918 | CheckEndOfDirective("ident"); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 919 | |
| 920 | if (Callbacks) |
| 921 | Callbacks->Ident(Tok.getLocation(), getSpelling(StrTok)); |
| 922 | } |
| 923 | |
| 924 | //===----------------------------------------------------------------------===// |
| 925 | // Preprocessor Include Directive Handling. |
| 926 | //===----------------------------------------------------------------------===// |
| 927 | |
| 928 | /// GetIncludeFilenameSpelling - Turn the specified lexer token into a fully |
| 929 | /// checked and spelled filename, e.g. as an operand of #include. This returns |
| 930 | /// true if the input filename was in <>'s or false if it were in ""'s. The |
| 931 | /// caller is expected to provide a buffer that is large enough to hold the |
| 932 | /// spelling of the filename, but is also expected to handle the case when |
| 933 | /// this method decides to use a different buffer. |
| 934 | bool Preprocessor::GetIncludeFilenameSpelling(SourceLocation Loc, |
| 935 | const char *&BufStart, |
| 936 | const char *&BufEnd) { |
| 937 | // Get the text form of the filename. |
| 938 | assert(BufStart != BufEnd && "Can't have tokens with empty spellings!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 939 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 940 | // Make sure the filename is <x> or "x". |
| 941 | bool isAngled; |
| 942 | if (BufStart[0] == '<') { |
| 943 | if (BufEnd[-1] != '>') { |
| 944 | Diag(Loc, diag::err_pp_expects_filename); |
| 945 | BufStart = 0; |
| 946 | return true; |
| 947 | } |
| 948 | isAngled = true; |
| 949 | } else if (BufStart[0] == '"') { |
| 950 | if (BufEnd[-1] != '"') { |
| 951 | Diag(Loc, diag::err_pp_expects_filename); |
| 952 | BufStart = 0; |
| 953 | return true; |
| 954 | } |
| 955 | isAngled = false; |
| 956 | } else { |
| 957 | Diag(Loc, diag::err_pp_expects_filename); |
| 958 | BufStart = 0; |
| 959 | return true; |
| 960 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 961 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 962 | // Diagnose #include "" as invalid. |
| 963 | if (BufEnd-BufStart <= 2) { |
| 964 | Diag(Loc, diag::err_pp_empty_filename); |
| 965 | BufStart = 0; |
| 966 | return ""; |
| 967 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 968 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 969 | // Skip the brackets. |
| 970 | ++BufStart; |
| 971 | --BufEnd; |
| 972 | return isAngled; |
| 973 | } |
| 974 | |
| 975 | /// ConcatenateIncludeName - Handle cases where the #include name is expanded |
| 976 | /// from a macro as multiple tokens, which need to be glued together. This |
| 977 | /// occurs for code like: |
| 978 | /// #define FOO <a/b.h> |
| 979 | /// #include FOO |
| 980 | /// because in this case, "<a/b.h>" is returned as 7 tokens, not one. |
| 981 | /// |
| 982 | /// This code concatenates and consumes tokens up to the '>' token. It returns |
| 983 | /// false if the > was found, otherwise it returns true if it finds and consumes |
| 984 | /// the EOM marker. |
John Thompson | a28cc09 | 2009-10-30 13:49:06 +0000 | [diff] [blame] | 985 | bool Preprocessor::ConcatenateIncludeName( |
| 986 | llvm::SmallVector<char, 128> &FilenameBuffer) { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 987 | Token CurTok; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 988 | |
John Thompson | a28cc09 | 2009-10-30 13:49:06 +0000 | [diff] [blame] | 989 | Lex(CurTok); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 990 | while (CurTok.isNot(tok::eom)) { |
| 991 | // Append the spelling of this token to the buffer. If there was a space |
| 992 | // before it, add it now. |
| 993 | if (CurTok.hasLeadingSpace()) |
| 994 | FilenameBuffer.push_back(' '); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 995 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 996 | // Get the spelling of the token, directly into FilenameBuffer if possible. |
| 997 | unsigned PreAppendSize = FilenameBuffer.size(); |
| 998 | FilenameBuffer.resize(PreAppendSize+CurTok.getLength()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 999 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1000 | const char *BufPtr = &FilenameBuffer[PreAppendSize]; |
John Thompson | a28cc09 | 2009-10-30 13:49:06 +0000 | [diff] [blame] | 1001 | unsigned ActualLen = getSpelling(CurTok, BufPtr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1002 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1003 | // If the token was spelled somewhere else, copy it into FilenameBuffer. |
| 1004 | if (BufPtr != &FilenameBuffer[PreAppendSize]) |
| 1005 | memcpy(&FilenameBuffer[PreAppendSize], BufPtr, ActualLen); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1006 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1007 | // Resize FilenameBuffer to the correct size. |
| 1008 | if (CurTok.getLength() != ActualLen) |
| 1009 | FilenameBuffer.resize(PreAppendSize+ActualLen); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1010 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1011 | // If we found the '>' marker, return success. |
| 1012 | if (CurTok.is(tok::greater)) |
| 1013 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1014 | |
John Thompson | a28cc09 | 2009-10-30 13:49:06 +0000 | [diff] [blame] | 1015 | Lex(CurTok); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1016 | } |
| 1017 | |
| 1018 | // If we hit the eom marker, emit an error and return true so that the caller |
| 1019 | // knows the EOM has been read. |
John Thompson | a28cc09 | 2009-10-30 13:49:06 +0000 | [diff] [blame] | 1020 | Diag(CurTok.getLocation(), diag::err_pp_expects_filename); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1021 | return true; |
| 1022 | } |
| 1023 | |
| 1024 | /// HandleIncludeDirective - The "#include" tokens have just been read, read the |
| 1025 | /// file to be included from the lexer, then include it! This is a common |
| 1026 | /// routine with functionality shared between #include, #include_next and |
Chris Lattner | 7218183 | 2008-09-26 20:12:23 +0000 | [diff] [blame] | 1027 | /// #import. LookupFrom is set when this is a #include_next directive, it |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1028 | /// specifies the file to start searching from. |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1029 | void Preprocessor::HandleIncludeDirective(Token &IncludeTok, |
| 1030 | const DirectoryLookup *LookupFrom, |
| 1031 | bool isImport) { |
| 1032 | |
| 1033 | Token FilenameTok; |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1034 | CurPPLexer->LexIncludeFilename(FilenameTok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1035 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1036 | // Reserve a buffer to get the spelling. |
| 1037 | llvm::SmallVector<char, 128> FilenameBuffer; |
| 1038 | const char *FilenameStart, *FilenameEnd; |
| 1039 | |
| 1040 | switch (FilenameTok.getKind()) { |
| 1041 | case tok::eom: |
| 1042 | // If the token kind is EOM, the error has already been diagnosed. |
| 1043 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1044 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1045 | case tok::angle_string_literal: |
| 1046 | case tok::string_literal: { |
| 1047 | FilenameBuffer.resize(FilenameTok.getLength()); |
| 1048 | FilenameStart = &FilenameBuffer[0]; |
| 1049 | unsigned Len = getSpelling(FilenameTok, FilenameStart); |
| 1050 | FilenameEnd = FilenameStart+Len; |
| 1051 | break; |
| 1052 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1053 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1054 | case tok::less: |
| 1055 | // This could be a <foo/bar.h> file coming from a macro expansion. In this |
| 1056 | // case, glue the tokens together into FilenameBuffer and interpret those. |
| 1057 | FilenameBuffer.push_back('<'); |
John Thompson | a28cc09 | 2009-10-30 13:49:06 +0000 | [diff] [blame] | 1058 | if (ConcatenateIncludeName(FilenameBuffer)) |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1059 | return; // Found <eom> but no ">"? Diagnostic already emitted. |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 1060 | FilenameStart = FilenameBuffer.data(); |
| 1061 | FilenameEnd = FilenameStart + FilenameBuffer.size(); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1062 | break; |
| 1063 | default: |
| 1064 | Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename); |
| 1065 | DiscardUntilEndOfDirective(); |
| 1066 | return; |
| 1067 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1068 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1069 | bool isAngled = GetIncludeFilenameSpelling(FilenameTok.getLocation(), |
| 1070 | FilenameStart, FilenameEnd); |
| 1071 | // If GetIncludeFilenameSpelling set the start ptr to null, there was an |
| 1072 | // error. |
| 1073 | if (FilenameStart == 0) { |
| 1074 | DiscardUntilEndOfDirective(); |
| 1075 | return; |
| 1076 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1077 | |
Chris Lattner | 9cb51ce | 2009-04-17 23:56:52 +0000 | [diff] [blame] | 1078 | // Verify that there is nothing after the filename, other than EOM. Note that |
| 1079 | // we allow macros that expand to nothing after the filename, because this |
| 1080 | // falls into the category of "#include pp-tokens new-line" specified in |
| 1081 | // C99 6.10.2p4. |
Daniel Dunbar | e013d68 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 1082 | CheckEndOfDirective(IncludeTok.getIdentifierInfo()->getNameStart(), true); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1083 | |
| 1084 | // Check that we don't have infinite #include recursion. |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 1085 | if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1) { |
| 1086 | Diag(FilenameTok, diag::err_pp_include_too_deep); |
| 1087 | return; |
| 1088 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1089 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1090 | // Search include directories. |
| 1091 | const DirectoryLookup *CurDir; |
| 1092 | const FileEntry *File = LookupFile(FilenameStart, FilenameEnd, |
Chris Lattner | 804f652 | 2010-01-10 00:24:58 +0000 | [diff] [blame^] | 1093 | FilenameTok.getLocation(), |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1094 | isAngled, LookupFrom, CurDir); |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 1095 | if (File == 0) { |
| 1096 | Diag(FilenameTok, diag::err_pp_file_not_found) |
| 1097 | << std::string(FilenameStart, FilenameEnd); |
| 1098 | return; |
| 1099 | } |
Chris Lattner | 804f652 | 2010-01-10 00:24:58 +0000 | [diff] [blame^] | 1100 | |
Chris Lattner | 7218183 | 2008-09-26 20:12:23 +0000 | [diff] [blame] | 1101 | // Ask HeaderInfo if we should enter this #include file. If not, #including |
| 1102 | // this file will have no effect. |
| 1103 | if (!HeaderInfo.ShouldEnterIncludeFile(File, isImport)) |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1104 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1105 | |
Chris Lattner | 7218183 | 2008-09-26 20:12:23 +0000 | [diff] [blame] | 1106 | // The #included file will be considered to be a system header if either it is |
| 1107 | // in a system include directory, or if the #includer is a system include |
| 1108 | // header. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1109 | SrcMgr::CharacteristicKind FileCharacter = |
Chris Lattner | 0b9e736 | 2008-09-26 21:18:42 +0000 | [diff] [blame] | 1110 | std::max(HeaderInfo.getFileDirFlavor(File), |
Chris Lattner | 693faa6 | 2009-01-19 07:59:15 +0000 | [diff] [blame] | 1111 | SourceMgr.getFileCharacteristic(FilenameTok.getLocation())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1112 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1113 | // Look up the file, create a File ID for it. |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 1114 | FileID FID = SourceMgr.createFileID(File, FilenameTok.getLocation(), |
| 1115 | FileCharacter); |
| 1116 | if (FID.isInvalid()) { |
Chris Lattner | 56b05c8 | 2008-11-18 08:02:48 +0000 | [diff] [blame] | 1117 | Diag(FilenameTok, diag::err_pp_file_not_found) |
| 1118 | << std::string(FilenameStart, FilenameEnd); |
| 1119 | return; |
| 1120 | } |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1121 | |
| 1122 | // Finally, if all is good, enter the new file! |
Chris Lattner | 39d9841 | 2009-12-01 22:52:33 +0000 | [diff] [blame] | 1123 | std::string ErrorStr; |
Daniel Dunbar | 63ceaa3 | 2009-12-06 09:19:12 +0000 | [diff] [blame] | 1124 | if (EnterSourceFile(FID, CurDir, ErrorStr)) |
Chris Lattner | 6e29014 | 2009-11-30 04:18:44 +0000 | [diff] [blame] | 1125 | Diag(FilenameTok, diag::err_pp_error_opening_file) |
Chris Lattner | 39d9841 | 2009-12-01 22:52:33 +0000 | [diff] [blame] | 1126 | << std::string(SourceMgr.getFileEntryForID(FID)->getName()) << ErrorStr; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1127 | } |
| 1128 | |
| 1129 | /// HandleIncludeNextDirective - Implements #include_next. |
| 1130 | /// |
| 1131 | void Preprocessor::HandleIncludeNextDirective(Token &IncludeNextTok) { |
| 1132 | Diag(IncludeNextTok, diag::ext_pp_include_next_directive); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1133 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1134 | // #include_next is like #include, except that we start searching after |
| 1135 | // the current found directory. If we can't do this, issue a |
| 1136 | // diagnostic. |
| 1137 | const DirectoryLookup *Lookup = CurDirLookup; |
| 1138 | if (isInPrimaryFile()) { |
| 1139 | Lookup = 0; |
| 1140 | Diag(IncludeNextTok, diag::pp_include_next_in_primary); |
| 1141 | } else if (Lookup == 0) { |
| 1142 | Diag(IncludeNextTok, diag::pp_include_next_absolute_path); |
| 1143 | } else { |
| 1144 | // Start looking up in the next directory. |
| 1145 | ++Lookup; |
| 1146 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1147 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1148 | return HandleIncludeDirective(IncludeNextTok, Lookup); |
| 1149 | } |
| 1150 | |
| 1151 | /// HandleImportDirective - Implements #import. |
| 1152 | /// |
| 1153 | void Preprocessor::HandleImportDirective(Token &ImportTok) { |
Chris Lattner | b627c8d | 2009-03-06 04:28:03 +0000 | [diff] [blame] | 1154 | if (!Features.ObjC1) // #import is standard for ObjC. |
| 1155 | Diag(ImportTok, diag::ext_pp_import_directive); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1156 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1157 | return HandleIncludeDirective(ImportTok, 0, true); |
| 1158 | } |
| 1159 | |
Chris Lattner | de07665 | 2009-04-08 18:46:40 +0000 | [diff] [blame] | 1160 | /// HandleIncludeMacrosDirective - The -imacros command line option turns into a |
| 1161 | /// pseudo directive in the predefines buffer. This handles it by sucking all |
| 1162 | /// tokens through the preprocessor and discarding them (only keeping the side |
| 1163 | /// effects on the preprocessor). |
| 1164 | void Preprocessor::HandleIncludeMacrosDirective(Token &IncludeMacrosTok) { |
| 1165 | // This directive should only occur in the predefines buffer. If not, emit an |
| 1166 | // error and reject it. |
| 1167 | SourceLocation Loc = IncludeMacrosTok.getLocation(); |
| 1168 | if (strcmp(SourceMgr.getBufferName(Loc), "<built-in>") != 0) { |
| 1169 | Diag(IncludeMacrosTok.getLocation(), |
| 1170 | diag::pp_include_macros_out_of_predefines); |
| 1171 | DiscardUntilEndOfDirective(); |
| 1172 | return; |
| 1173 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1174 | |
Chris Lattner | fd10511 | 2009-04-08 20:53:24 +0000 | [diff] [blame] | 1175 | // Treat this as a normal #include for checking purposes. If this is |
| 1176 | // successful, it will push a new lexer onto the include stack. |
| 1177 | HandleIncludeDirective(IncludeMacrosTok, 0, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1178 | |
Chris Lattner | fd10511 | 2009-04-08 20:53:24 +0000 | [diff] [blame] | 1179 | Token TmpTok; |
| 1180 | do { |
| 1181 | Lex(TmpTok); |
| 1182 | assert(TmpTok.isNot(tok::eof) && "Didn't find end of -imacros!"); |
| 1183 | } while (TmpTok.isNot(tok::hashhash)); |
Chris Lattner | de07665 | 2009-04-08 18:46:40 +0000 | [diff] [blame] | 1184 | } |
| 1185 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1186 | //===----------------------------------------------------------------------===// |
| 1187 | // Preprocessor Macro Directive Handling. |
| 1188 | //===----------------------------------------------------------------------===// |
| 1189 | |
| 1190 | /// ReadMacroDefinitionArgList - The ( starting an argument list of a macro |
| 1191 | /// definition has just been read. Lex the rest of the arguments and the |
| 1192 | /// closing ), updating MI with what we learn. Return true if an error occurs |
| 1193 | /// parsing the arg list. |
| 1194 | bool Preprocessor::ReadMacroDefinitionArgList(MacroInfo *MI) { |
| 1195 | llvm::SmallVector<IdentifierInfo*, 32> Arguments; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1196 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1197 | Token Tok; |
| 1198 | while (1) { |
| 1199 | LexUnexpandedToken(Tok); |
| 1200 | switch (Tok.getKind()) { |
| 1201 | case tok::r_paren: |
| 1202 | // Found the end of the argument list. |
Chris Lattner | cf29e07 | 2009-02-20 22:31:31 +0000 | [diff] [blame] | 1203 | if (Arguments.empty()) // #define FOO() |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1204 | return false; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1205 | // Otherwise we have #define FOO(A,) |
| 1206 | Diag(Tok, diag::err_pp_expected_ident_in_arg_list); |
| 1207 | return true; |
| 1208 | case tok::ellipsis: // #define X(... -> C99 varargs |
| 1209 | // Warn if use of C99 feature in non-C99 mode. |
| 1210 | if (!Features.C99) Diag(Tok, diag::ext_variadic_macro); |
| 1211 | |
| 1212 | // Lex the token after the identifier. |
| 1213 | LexUnexpandedToken(Tok); |
| 1214 | if (Tok.isNot(tok::r_paren)) { |
| 1215 | Diag(Tok, diag::err_pp_missing_rparen_in_macro_def); |
| 1216 | return true; |
| 1217 | } |
| 1218 | // Add the __VA_ARGS__ identifier as an argument. |
| 1219 | Arguments.push_back(Ident__VA_ARGS__); |
| 1220 | MI->setIsC99Varargs(); |
Chris Lattner | 685befe | 2009-02-20 22:46:43 +0000 | [diff] [blame] | 1221 | MI->setArgumentList(&Arguments[0], Arguments.size(), BP); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1222 | return false; |
| 1223 | case tok::eom: // #define X( |
| 1224 | Diag(Tok, diag::err_pp_missing_rparen_in_macro_def); |
| 1225 | return true; |
| 1226 | default: |
| 1227 | // Handle keywords and identifiers here to accept things like |
| 1228 | // #define Foo(for) for. |
| 1229 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 1230 | if (II == 0) { |
| 1231 | // #define X(1 |
| 1232 | Diag(Tok, diag::err_pp_invalid_tok_in_arg_list); |
| 1233 | return true; |
| 1234 | } |
| 1235 | |
| 1236 | // If this is already used as an argument, it is used multiple times (e.g. |
| 1237 | // #define X(A,A. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1238 | if (std::find(Arguments.begin(), Arguments.end(), II) != |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1239 | Arguments.end()) { // C99 6.10.3p6 |
Chris Lattner | 6cf3ed7 | 2008-11-19 07:33:58 +0000 | [diff] [blame] | 1240 | Diag(Tok, diag::err_pp_duplicate_name_in_arg_list) << II; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1241 | return true; |
| 1242 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1243 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1244 | // Add the argument to the macro info. |
| 1245 | Arguments.push_back(II); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1246 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1247 | // Lex the token after the identifier. |
| 1248 | LexUnexpandedToken(Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1249 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1250 | switch (Tok.getKind()) { |
| 1251 | default: // #define X(A B |
| 1252 | Diag(Tok, diag::err_pp_expected_comma_in_arg_list); |
| 1253 | return true; |
| 1254 | case tok::r_paren: // #define X(A) |
Chris Lattner | 685befe | 2009-02-20 22:46:43 +0000 | [diff] [blame] | 1255 | MI->setArgumentList(&Arguments[0], Arguments.size(), BP); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1256 | return false; |
| 1257 | case tok::comma: // #define X(A, |
| 1258 | break; |
| 1259 | case tok::ellipsis: // #define X(A... -> GCC extension |
| 1260 | // Diagnose extension. |
| 1261 | Diag(Tok, diag::ext_named_variadic_macro); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1262 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1263 | // Lex the token after the identifier. |
| 1264 | LexUnexpandedToken(Tok); |
| 1265 | if (Tok.isNot(tok::r_paren)) { |
| 1266 | Diag(Tok, diag::err_pp_missing_rparen_in_macro_def); |
| 1267 | return true; |
| 1268 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1269 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1270 | MI->setIsGNUVarargs(); |
Chris Lattner | 685befe | 2009-02-20 22:46:43 +0000 | [diff] [blame] | 1271 | MI->setArgumentList(&Arguments[0], Arguments.size(), BP); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1272 | return false; |
| 1273 | } |
| 1274 | } |
| 1275 | } |
| 1276 | } |
| 1277 | |
| 1278 | /// HandleDefineDirective - Implements #define. This consumes the entire macro |
| 1279 | /// line then lets the caller lex the next real token. |
| 1280 | void Preprocessor::HandleDefineDirective(Token &DefineTok) { |
| 1281 | ++NumDefined; |
| 1282 | |
| 1283 | Token MacroNameTok; |
| 1284 | ReadMacroName(MacroNameTok, 1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1285 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1286 | // Error reading macro name? If so, diagnostic already issued. |
| 1287 | if (MacroNameTok.is(tok::eom)) |
| 1288 | return; |
| 1289 | |
Chris Lattner | 2451b52 | 2009-04-21 04:46:33 +0000 | [diff] [blame] | 1290 | Token LastTok = MacroNameTok; |
| 1291 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1292 | // If we are supposed to keep comments in #defines, reenable comment saving |
| 1293 | // mode. |
Ted Kremenek | ac6b06d | 2008-11-18 00:43:07 +0000 | [diff] [blame] | 1294 | if (CurLexer) CurLexer->SetCommentRetentionState(KeepMacroComments); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1295 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1296 | // Create the new macro. |
Ted Kremenek | 0ea7672 | 2008-12-15 19:56:42 +0000 | [diff] [blame] | 1297 | MacroInfo *MI = AllocateMacroInfo(MacroNameTok.getLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1298 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1299 | Token Tok; |
| 1300 | LexUnexpandedToken(Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1301 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1302 | // If this is a function-like macro definition, parse the argument list, |
| 1303 | // marking each of the identifiers as being used as macro arguments. Also, |
| 1304 | // check other constraints on the first token of the macro body. |
| 1305 | if (Tok.is(tok::eom)) { |
| 1306 | // If there is no body to this macro, we have no special handling here. |
Chris Lattner | 6272bcf | 2009-04-18 02:23:25 +0000 | [diff] [blame] | 1307 | } else if (Tok.hasLeadingSpace()) { |
| 1308 | // This is a normal token with leading space. Clear the leading space |
| 1309 | // marker on the first token to get proper expansion. |
| 1310 | Tok.clearFlag(Token::LeadingSpace); |
| 1311 | } else if (Tok.is(tok::l_paren)) { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1312 | // This is a function-like macro definition. Read the argument list. |
| 1313 | MI->setIsFunctionLike(); |
| 1314 | if (ReadMacroDefinitionArgList(MI)) { |
| 1315 | // Forget about MI. |
Ted Kremenek | 0ea7672 | 2008-12-15 19:56:42 +0000 | [diff] [blame] | 1316 | ReleaseMacroInfo(MI); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1317 | // Throw away the rest of the line. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1318 | if (CurPPLexer->ParsingPreprocessorDirective) |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1319 | DiscardUntilEndOfDirective(); |
| 1320 | return; |
| 1321 | } |
| 1322 | |
Chris Lattner | 8fde597 | 2009-04-19 18:26:34 +0000 | [diff] [blame] | 1323 | // If this is a definition of a variadic C99 function-like macro, not using |
| 1324 | // the GNU named varargs extension, enabled __VA_ARGS__. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1325 | |
Chris Lattner | 8fde597 | 2009-04-19 18:26:34 +0000 | [diff] [blame] | 1326 | // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro. |
| 1327 | // This gets unpoisoned where it is allowed. |
| 1328 | assert(Ident__VA_ARGS__->isPoisoned() && "__VA_ARGS__ should be poisoned!"); |
| 1329 | if (MI->isC99Varargs()) |
| 1330 | Ident__VA_ARGS__->setIsPoisoned(false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1331 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1332 | // Read the first token after the arg list for down below. |
| 1333 | LexUnexpandedToken(Tok); |
Chris Lattner | 6272bcf | 2009-04-18 02:23:25 +0000 | [diff] [blame] | 1334 | } else if (Features.C99) { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1335 | // C99 requires whitespace between the macro definition and the body. Emit |
| 1336 | // a diagnostic for something like "#define X+". |
Chris Lattner | 6272bcf | 2009-04-18 02:23:25 +0000 | [diff] [blame] | 1337 | Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1338 | } else { |
Chris Lattner | 6272bcf | 2009-04-18 02:23:25 +0000 | [diff] [blame] | 1339 | // C90 6.8 TC1 says: "In the definition of an object-like macro, if the |
| 1340 | // first character of a replacement list is not a character required by |
| 1341 | // subclause 5.2.1, then there shall be white-space separation between the |
| 1342 | // identifier and the replacement list.". 5.2.1 lists this set: |
| 1343 | // "A-Za-z0-9!"#%&'()*+,_./:;<=>?[\]^_{|}~" as well as whitespace, which |
| 1344 | // is irrelevant here. |
| 1345 | bool isInvalid = false; |
| 1346 | if (Tok.is(tok::at)) // @ is not in the list above. |
| 1347 | isInvalid = true; |
| 1348 | else if (Tok.is(tok::unknown)) { |
| 1349 | // If we have an unknown token, it is something strange like "`". Since |
| 1350 | // all of valid characters would have lexed into a single character |
| 1351 | // token of some sort, we know this is not a valid case. |
| 1352 | isInvalid = true; |
| 1353 | } |
| 1354 | if (isInvalid) |
| 1355 | Diag(Tok, diag::ext_missing_whitespace_after_macro_name); |
| 1356 | else |
| 1357 | Diag(Tok, diag::warn_missing_whitespace_after_macro_name); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1358 | } |
Chris Lattner | 2451b52 | 2009-04-21 04:46:33 +0000 | [diff] [blame] | 1359 | |
| 1360 | if (!Tok.is(tok::eom)) |
| 1361 | LastTok = Tok; |
| 1362 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1363 | // Read the rest of the macro body. |
| 1364 | if (MI->isObjectLike()) { |
| 1365 | // Object-like macros are very simple, just read their body. |
| 1366 | while (Tok.isNot(tok::eom)) { |
Chris Lattner | 2451b52 | 2009-04-21 04:46:33 +0000 | [diff] [blame] | 1367 | LastTok = Tok; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1368 | MI->AddTokenToBody(Tok); |
| 1369 | // Get the next token of the macro. |
| 1370 | LexUnexpandedToken(Tok); |
| 1371 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1372 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1373 | } else { |
Chris Lattner | 3240469 | 2009-05-25 17:16:10 +0000 | [diff] [blame] | 1374 | // Otherwise, read the body of a function-like macro. While we are at it, |
| 1375 | // check C99 6.10.3.2p1: ensure that # operators are followed by macro |
| 1376 | // parameters in function-like macro expansions. |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1377 | while (Tok.isNot(tok::eom)) { |
Chris Lattner | 2451b52 | 2009-04-21 04:46:33 +0000 | [diff] [blame] | 1378 | LastTok = Tok; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1379 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1380 | if (Tok.isNot(tok::hash)) { |
Chris Lattner | 3240469 | 2009-05-25 17:16:10 +0000 | [diff] [blame] | 1381 | MI->AddTokenToBody(Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1382 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1383 | // Get the next token of the macro. |
| 1384 | LexUnexpandedToken(Tok); |
| 1385 | continue; |
| 1386 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1387 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1388 | // Get the next token of the macro. |
| 1389 | LexUnexpandedToken(Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1390 | |
Chris Lattner | 3240469 | 2009-05-25 17:16:10 +0000 | [diff] [blame] | 1391 | // Check for a valid macro arg identifier. |
| 1392 | if (Tok.getIdentifierInfo() == 0 || |
| 1393 | MI->getArgumentNum(Tok.getIdentifierInfo()) == -1) { |
| 1394 | |
| 1395 | // If this is assembler-with-cpp mode, we accept random gibberish after |
| 1396 | // the '#' because '#' is often a comment character. However, change |
| 1397 | // the kind of the token to tok::unknown so that the preprocessor isn't |
| 1398 | // confused. |
| 1399 | if (getLangOptions().AsmPreprocessor && Tok.isNot(tok::eom)) { |
| 1400 | LastTok.setKind(tok::unknown); |
| 1401 | } else { |
| 1402 | Diag(Tok, diag::err_pp_stringize_not_parameter); |
| 1403 | ReleaseMacroInfo(MI); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1404 | |
Chris Lattner | 3240469 | 2009-05-25 17:16:10 +0000 | [diff] [blame] | 1405 | // Disable __VA_ARGS__ again. |
| 1406 | Ident__VA_ARGS__->setIsPoisoned(true); |
| 1407 | return; |
| 1408 | } |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1409 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1410 | |
Chris Lattner | 3240469 | 2009-05-25 17:16:10 +0000 | [diff] [blame] | 1411 | // Things look ok, add the '#' and param name tokens to the macro. |
| 1412 | MI->AddTokenToBody(LastTok); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1413 | MI->AddTokenToBody(Tok); |
Chris Lattner | 3240469 | 2009-05-25 17:16:10 +0000 | [diff] [blame] | 1414 | LastTok = Tok; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1415 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1416 | // Get the next token of the macro. |
| 1417 | LexUnexpandedToken(Tok); |
| 1418 | } |
| 1419 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1420 | |
| 1421 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1422 | // Disable __VA_ARGS__ again. |
| 1423 | Ident__VA_ARGS__->setIsPoisoned(true); |
| 1424 | |
| 1425 | // Check that there is no paste (##) operator at the begining or end of the |
| 1426 | // replacement list. |
| 1427 | unsigned NumTokens = MI->getNumTokens(); |
| 1428 | if (NumTokens != 0) { |
| 1429 | if (MI->getReplacementToken(0).is(tok::hashhash)) { |
| 1430 | Diag(MI->getReplacementToken(0), diag::err_paste_at_start); |
Ted Kremenek | 0ea7672 | 2008-12-15 19:56:42 +0000 | [diff] [blame] | 1431 | ReleaseMacroInfo(MI); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1432 | return; |
| 1433 | } |
| 1434 | if (MI->getReplacementToken(NumTokens-1).is(tok::hashhash)) { |
| 1435 | Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end); |
Ted Kremenek | 0ea7672 | 2008-12-15 19:56:42 +0000 | [diff] [blame] | 1436 | ReleaseMacroInfo(MI); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1437 | return; |
| 1438 | } |
| 1439 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1440 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1441 | // If this is the primary source file, remember that this macro hasn't been |
| 1442 | // used yet. |
| 1443 | if (isInPrimaryFile()) |
| 1444 | MI->setIsUsed(false); |
Chris Lattner | 2451b52 | 2009-04-21 04:46:33 +0000 | [diff] [blame] | 1445 | |
| 1446 | MI->setDefinitionEndLoc(LastTok.getLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1447 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1448 | // Finally, if this identifier already had a macro defined for it, verify that |
| 1449 | // the macro bodies are identical and free the old definition. |
| 1450 | if (MacroInfo *OtherMI = getMacroInfo(MacroNameTok.getIdentifierInfo())) { |
Chris Lattner | 41c3ae1 | 2009-01-16 19:50:11 +0000 | [diff] [blame] | 1451 | // It is very common for system headers to have tons of macro redefinitions |
| 1452 | // and for warnings to be disabled in system headers. If this is the case, |
| 1453 | // then don't bother calling MacroInfo::isIdenticalTo. |
Chris Lattner | 7f549df | 2009-03-13 21:17:23 +0000 | [diff] [blame] | 1454 | if (!getDiagnostics().getSuppressSystemWarnings() || |
Chris Lattner | 41c3ae1 | 2009-01-16 19:50:11 +0000 | [diff] [blame] | 1455 | !SourceMgr.isInSystemHeader(DefineTok.getLocation())) { |
| 1456 | if (!OtherMI->isUsed()) |
| 1457 | Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1458 | |
Chris Lattner | 41c3ae1 | 2009-01-16 19:50:11 +0000 | [diff] [blame] | 1459 | // Macros must be identical. This means all tokes and whitespace |
| 1460 | // separation must be the same. C99 6.10.3.2. |
| 1461 | if (!MI->isIdenticalTo(*OtherMI, *this)) { |
| 1462 | Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef) |
| 1463 | << MacroNameTok.getIdentifierInfo(); |
| 1464 | Diag(OtherMI->getDefinitionLoc(), diag::note_previous_definition); |
| 1465 | } |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1466 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1467 | |
Ted Kremenek | 0ea7672 | 2008-12-15 19:56:42 +0000 | [diff] [blame] | 1468 | ReleaseMacroInfo(OtherMI); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1469 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1470 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1471 | setMacroInfo(MacroNameTok.getIdentifierInfo(), MI); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1472 | |
Chris Lattner | f4a72b0 | 2009-04-12 01:39:54 +0000 | [diff] [blame] | 1473 | // If the callbacks want to know, tell them about the macro definition. |
| 1474 | if (Callbacks) |
| 1475 | Callbacks->MacroDefined(MacroNameTok.getIdentifierInfo(), MI); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1476 | } |
| 1477 | |
| 1478 | /// HandleUndefDirective - Implements #undef. |
| 1479 | /// |
| 1480 | void Preprocessor::HandleUndefDirective(Token &UndefTok) { |
| 1481 | ++NumUndefined; |
| 1482 | |
| 1483 | Token MacroNameTok; |
| 1484 | ReadMacroName(MacroNameTok, 2); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1485 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1486 | // Error reading macro name? If so, diagnostic already issued. |
| 1487 | if (MacroNameTok.is(tok::eom)) |
| 1488 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1489 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1490 | // Check to see if this is the last token on the #undef line. |
Chris Lattner | 35410d5 | 2009-04-14 05:07:49 +0000 | [diff] [blame] | 1491 | CheckEndOfDirective("undef"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1492 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1493 | // Okay, we finally have a valid identifier to undef. |
| 1494 | MacroInfo *MI = getMacroInfo(MacroNameTok.getIdentifierInfo()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1495 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1496 | // If the macro is not defined, this is a noop undef, just return. |
| 1497 | if (MI == 0) return; |
| 1498 | |
| 1499 | if (!MI->isUsed()) |
| 1500 | Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used); |
Chris Lattner | 41c1747 | 2009-04-21 03:42:09 +0000 | [diff] [blame] | 1501 | |
| 1502 | // If the callbacks want to know, tell them about the macro #undef. |
| 1503 | if (Callbacks) |
| 1504 | Callbacks->MacroUndefined(MacroNameTok.getIdentifierInfo(), MI); |
| 1505 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1506 | // Free macro definition. |
Ted Kremenek | 0ea7672 | 2008-12-15 19:56:42 +0000 | [diff] [blame] | 1507 | ReleaseMacroInfo(MI); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1508 | setMacroInfo(MacroNameTok.getIdentifierInfo(), 0); |
| 1509 | } |
| 1510 | |
| 1511 | |
| 1512 | //===----------------------------------------------------------------------===// |
| 1513 | // Preprocessor Conditional Directive Handling. |
| 1514 | //===----------------------------------------------------------------------===// |
| 1515 | |
| 1516 | /// HandleIfdefDirective - Implements the #ifdef/#ifndef directive. isIfndef is |
| 1517 | /// true when this is a #ifndef directive. ReadAnyTokensBeforeDirective is true |
| 1518 | /// if any tokens have been returned or pp-directives activated before this |
| 1519 | /// #ifndef has been lexed. |
| 1520 | /// |
| 1521 | void Preprocessor::HandleIfdefDirective(Token &Result, bool isIfndef, |
| 1522 | bool ReadAnyTokensBeforeDirective) { |
| 1523 | ++NumIf; |
| 1524 | Token DirectiveTok = Result; |
| 1525 | |
| 1526 | Token MacroNameTok; |
| 1527 | ReadMacroName(MacroNameTok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1528 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1529 | // Error reading macro name? If so, diagnostic already issued. |
| 1530 | if (MacroNameTok.is(tok::eom)) { |
| 1531 | // Skip code until we get to #endif. This helps with recovery by not |
| 1532 | // emitting an error when the #endif is reached. |
| 1533 | SkipExcludedConditionalBlock(DirectiveTok.getLocation(), |
| 1534 | /*Foundnonskip*/false, /*FoundElse*/false); |
| 1535 | return; |
| 1536 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1537 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1538 | // Check to see if this is the last token on the #if[n]def line. |
Chris Lattner | 35410d5 | 2009-04-14 05:07:49 +0000 | [diff] [blame] | 1539 | CheckEndOfDirective(isIfndef ? "ifndef" : "ifdef"); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1540 | |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1541 | if (CurPPLexer->getConditionalStackDepth() == 0) { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1542 | // If the start of a top-level #ifdef, inform MIOpt. |
| 1543 | if (!ReadAnyTokensBeforeDirective) { |
| 1544 | assert(isIfndef && "#ifdef shouldn't reach here"); |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1545 | CurPPLexer->MIOpt.EnterTopLevelIFNDEF(MacroNameTok.getIdentifierInfo()); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1546 | } else |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1547 | CurPPLexer->MIOpt.EnterTopLevelConditional(); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1548 | } |
| 1549 | |
| 1550 | IdentifierInfo *MII = MacroNameTok.getIdentifierInfo(); |
| 1551 | MacroInfo *MI = getMacroInfo(MII); |
| 1552 | |
| 1553 | // If there is a macro, process it. |
| 1554 | if (MI) // Mark it used. |
| 1555 | MI->setIsUsed(true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1556 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1557 | // Should we include the stuff contained by this directive? |
| 1558 | if (!MI == isIfndef) { |
| 1559 | // Yes, remember that we are inside a conditional, then lex the next token. |
Chris Lattner | 1d9c54d | 2009-12-14 04:54:40 +0000 | [diff] [blame] | 1560 | CurPPLexer->pushConditionalLevel(DirectiveTok.getLocation(), |
| 1561 | /*wasskip*/false, /*foundnonskip*/true, |
| 1562 | /*foundelse*/false); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1563 | } else { |
| 1564 | // No, skip the contents of this block and return the first token after it. |
| 1565 | SkipExcludedConditionalBlock(DirectiveTok.getLocation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1566 | /*Foundnonskip*/false, |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1567 | /*FoundElse*/false); |
| 1568 | } |
| 1569 | } |
| 1570 | |
| 1571 | /// HandleIfDirective - Implements the #if directive. |
| 1572 | /// |
| 1573 | void Preprocessor::HandleIfDirective(Token &IfToken, |
| 1574 | bool ReadAnyTokensBeforeDirective) { |
| 1575 | ++NumIf; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1576 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1577 | // Parse and evaluation the conditional expression. |
| 1578 | IdentifierInfo *IfNDefMacro = 0; |
| 1579 | bool ConditionalTrue = EvaluateDirectiveExpression(IfNDefMacro); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1580 | |
Nuno Lopes | 0049db6 | 2008-06-01 18:31:24 +0000 | [diff] [blame] | 1581 | |
| 1582 | // If this condition is equivalent to #ifndef X, and if this is the first |
| 1583 | // directive seen, handle it for the multiple-include optimization. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1584 | if (CurPPLexer->getConditionalStackDepth() == 0) { |
Nuno Lopes | 0049db6 | 2008-06-01 18:31:24 +0000 | [diff] [blame] | 1585 | if (!ReadAnyTokensBeforeDirective && IfNDefMacro) |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1586 | CurPPLexer->MIOpt.EnterTopLevelIFNDEF(IfNDefMacro); |
Nuno Lopes | 0049db6 | 2008-06-01 18:31:24 +0000 | [diff] [blame] | 1587 | else |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1588 | CurPPLexer->MIOpt.EnterTopLevelConditional(); |
Nuno Lopes | 0049db6 | 2008-06-01 18:31:24 +0000 | [diff] [blame] | 1589 | } |
| 1590 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1591 | // Should we include the stuff contained by this directive? |
| 1592 | if (ConditionalTrue) { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1593 | // 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] | 1594 | CurPPLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false, |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1595 | /*foundnonskip*/true, /*foundelse*/false); |
| 1596 | } else { |
| 1597 | // No, skip the contents of this block and return the first token after it. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1598 | SkipExcludedConditionalBlock(IfToken.getLocation(), /*Foundnonskip*/false, |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1599 | /*FoundElse*/false); |
| 1600 | } |
| 1601 | } |
| 1602 | |
| 1603 | /// HandleEndifDirective - Implements the #endif directive. |
| 1604 | /// |
| 1605 | void Preprocessor::HandleEndifDirective(Token &EndifToken) { |
| 1606 | ++NumEndif; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1607 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1608 | // Check that this is the whole directive. |
Chris Lattner | 35410d5 | 2009-04-14 05:07:49 +0000 | [diff] [blame] | 1609 | CheckEndOfDirective("endif"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1610 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1611 | PPConditionalInfo CondInfo; |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1612 | if (CurPPLexer->popConditionalLevel(CondInfo)) { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1613 | // No conditionals on the stack: this is an #endif without an #if. |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 1614 | Diag(EndifToken, diag::err_pp_endif_without_if); |
| 1615 | return; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1616 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1617 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1618 | // If this the end of a top-level #endif, inform MIOpt. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1619 | if (CurPPLexer->getConditionalStackDepth() == 0) |
| 1620 | CurPPLexer->MIOpt.ExitTopLevelConditional(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1621 | |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1622 | assert(!CondInfo.WasSkipping && !CurPPLexer->LexingRawMode && |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1623 | "This code should only be reachable in the non-skipping case!"); |
| 1624 | } |
| 1625 | |
| 1626 | |
| 1627 | void Preprocessor::HandleElseDirective(Token &Result) { |
| 1628 | ++NumElse; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1629 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1630 | // #else directive in a non-skipping conditional... start skipping. |
Chris Lattner | 35410d5 | 2009-04-14 05:07:49 +0000 | [diff] [blame] | 1631 | CheckEndOfDirective("else"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1632 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1633 | PPConditionalInfo CI; |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 1634 | if (CurPPLexer->popConditionalLevel(CI)) { |
| 1635 | Diag(Result, diag::pp_err_else_without_if); |
| 1636 | return; |
| 1637 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1638 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1639 | // If this is a top-level #else, inform the MIOpt. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1640 | if (CurPPLexer->getConditionalStackDepth() == 0) |
| 1641 | CurPPLexer->MIOpt.EnterTopLevelConditional(); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1642 | |
| 1643 | // If this is a #else with a #else before it, report the error. |
| 1644 | if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1645 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1646 | // Finally, skip the rest of the contents of this block and return the first |
| 1647 | // token after it. |
| 1648 | return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true, |
| 1649 | /*FoundElse*/true); |
| 1650 | } |
| 1651 | |
| 1652 | void Preprocessor::HandleElifDirective(Token &ElifToken) { |
| 1653 | ++NumElse; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1654 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1655 | // #elif directive in a non-skipping conditional... start skipping. |
| 1656 | // We don't care what the condition is, because we will always skip it (since |
| 1657 | // the block immediately before it was included). |
| 1658 | DiscardUntilEndOfDirective(); |
| 1659 | |
| 1660 | PPConditionalInfo CI; |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 1661 | if (CurPPLexer->popConditionalLevel(CI)) { |
| 1662 | Diag(ElifToken, diag::pp_err_elif_without_if); |
| 1663 | return; |
| 1664 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1665 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1666 | // If this is a top-level #elif, inform the MIOpt. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1667 | if (CurPPLexer->getConditionalStackDepth() == 0) |
| 1668 | CurPPLexer->MIOpt.EnterTopLevelConditional(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1669 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1670 | // If this is a #elif with a #else before it, report the error. |
| 1671 | if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else); |
| 1672 | |
| 1673 | // Finally, skip the rest of the contents of this block and return the first |
| 1674 | // token after it. |
| 1675 | return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true, |
| 1676 | /*FoundElse*/CI.FoundElse); |
| 1677 | } |
| 1678 | |