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