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