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