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