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