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