clang API Documentation
00001 //===--- PPExpressions.cpp - Preprocessor Expression Evaluation -----------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements the Preprocessor::EvaluateDirectiveExpression method, 00011 // which parses and evaluates integer constant expressions for #if directives. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 // 00015 // FIXME: implement testing for #assert's. 00016 // 00017 //===----------------------------------------------------------------------===// 00018 00019 #include "clang/Lex/Preprocessor.h" 00020 #include "clang/Lex/MacroInfo.h" 00021 #include "clang/Lex/LiteralSupport.h" 00022 #include "clang/Lex/CodeCompletionHandler.h" 00023 #include "clang/Basic/TargetInfo.h" 00024 #include "clang/Lex/LexDiagnostic.h" 00025 #include "llvm/ADT/APSInt.h" 00026 #include "llvm/Support/ErrorHandling.h" 00027 using namespace clang; 00028 00029 namespace { 00030 00031 /// PPValue - Represents the value of a subexpression of a preprocessor 00032 /// conditional and the source range covered by it. 00033 class PPValue { 00034 SourceRange Range; 00035 public: 00036 llvm::APSInt Val; 00037 00038 // Default ctor - Construct an 'invalid' PPValue. 00039 PPValue(unsigned BitWidth) : Val(BitWidth) {} 00040 00041 unsigned getBitWidth() const { return Val.getBitWidth(); } 00042 bool isUnsigned() const { return Val.isUnsigned(); } 00043 00044 const SourceRange &getRange() const { return Range; } 00045 00046 void setRange(SourceLocation L) { Range.setBegin(L); Range.setEnd(L); } 00047 void setRange(SourceLocation B, SourceLocation E) { 00048 Range.setBegin(B); Range.setEnd(E); 00049 } 00050 void setBegin(SourceLocation L) { Range.setBegin(L); } 00051 void setEnd(SourceLocation L) { Range.setEnd(L); } 00052 }; 00053 00054 } 00055 00056 static bool EvaluateDirectiveSubExpr(PPValue &LHS, unsigned MinPrec, 00057 Token &PeekTok, bool ValueLive, 00058 Preprocessor &PP); 00059 00060 /// DefinedTracker - This struct is used while parsing expressions to keep track 00061 /// of whether !defined(X) has been seen. 00062 /// 00063 /// With this simple scheme, we handle the basic forms: 00064 /// !defined(X) and !defined X 00065 /// but we also trivially handle (silly) stuff like: 00066 /// !!!defined(X) and +!defined(X) and !+!+!defined(X) and !(defined(X)). 00067 struct DefinedTracker { 00068 /// Each time a Value is evaluated, it returns information about whether the 00069 /// parsed value is of the form defined(X), !defined(X) or is something else. 00070 enum TrackerState { 00071 DefinedMacro, // defined(X) 00072 NotDefinedMacro, // !defined(X) 00073 Unknown // Something else. 00074 } State; 00075 /// TheMacro - When the state is DefinedMacro or NotDefinedMacro, this 00076 /// indicates the macro that was checked. 00077 IdentifierInfo *TheMacro; 00078 }; 00079 00080 /// EvaluateDefined - Process a 'defined(sym)' expression. 00081 static bool EvaluateDefined(PPValue &Result, Token &PeekTok, DefinedTracker &DT, 00082 bool ValueLive, Preprocessor &PP) { 00083 IdentifierInfo *II; 00084 Result.setBegin(PeekTok.getLocation()); 00085 00086 // Get the next token, don't expand it. 00087 PP.LexUnexpandedNonComment(PeekTok); 00088 00089 // Two options, it can either be a pp-identifier or a (. 00090 SourceLocation LParenLoc; 00091 if (PeekTok.is(tok::l_paren)) { 00092 // Found a paren, remember we saw it and skip it. 00093 LParenLoc = PeekTok.getLocation(); 00094 PP.LexUnexpandedNonComment(PeekTok); 00095 } 00096 00097 if (PeekTok.is(tok::code_completion)) { 00098 if (PP.getCodeCompletionHandler()) 00099 PP.getCodeCompletionHandler()->CodeCompleteMacroName(false); 00100 PP.setCodeCompletionReached(); 00101 PP.LexUnexpandedNonComment(PeekTok); 00102 } 00103 00104 // If we don't have a pp-identifier now, this is an error. 00105 if ((II = PeekTok.getIdentifierInfo()) == 0) { 00106 PP.Diag(PeekTok, diag::err_pp_defined_requires_identifier); 00107 return true; 00108 } 00109 00110 // Otherwise, we got an identifier, is it defined to something? 00111 Result.Val = II->hasMacroDefinition(); 00112 Result.Val.setIsUnsigned(false); // Result is signed intmax_t. 00113 00114 // If there is a macro, mark it used. 00115 if (Result.Val != 0 && ValueLive) { 00116 MacroInfo *Macro = PP.getMacroInfo(II); 00117 PP.markMacroAsUsed(Macro); 00118 } 00119 00120 // Invoke the 'defined' callback. 00121 if (PPCallbacks *Callbacks = PP.getPPCallbacks()) 00122 Callbacks->Defined(PeekTok); 00123 00124 // If we are in parens, ensure we have a trailing ). 00125 if (LParenLoc.isValid()) { 00126 // Consume identifier. 00127 Result.setEnd(PeekTok.getLocation()); 00128 PP.LexUnexpandedNonComment(PeekTok); 00129 00130 if (PeekTok.isNot(tok::r_paren)) { 00131 PP.Diag(PeekTok.getLocation(), diag::err_pp_missing_rparen) << "defined"; 00132 PP.Diag(LParenLoc, diag::note_matching) << "("; 00133 return true; 00134 } 00135 // Consume the ). 00136 Result.setEnd(PeekTok.getLocation()); 00137 PP.LexNonComment(PeekTok); 00138 } else { 00139 // Consume identifier. 00140 Result.setEnd(PeekTok.getLocation()); 00141 PP.LexNonComment(PeekTok); 00142 } 00143 00144 // Success, remember that we saw defined(X). 00145 DT.State = DefinedTracker::DefinedMacro; 00146 DT.TheMacro = II; 00147 return false; 00148 } 00149 00150 /// EvaluateValue - Evaluate the token PeekTok (and any others needed) and 00151 /// return the computed value in Result. Return true if there was an error 00152 /// parsing. This function also returns information about the form of the 00153 /// expression in DT. See above for information on what DT means. 00154 /// 00155 /// If ValueLive is false, then this value is being evaluated in a context where 00156 /// the result is not used. As such, avoid diagnostics that relate to 00157 /// evaluation. 00158 static bool EvaluateValue(PPValue &Result, Token &PeekTok, DefinedTracker &DT, 00159 bool ValueLive, Preprocessor &PP) { 00160 DT.State = DefinedTracker::Unknown; 00161 00162 if (PeekTok.is(tok::code_completion)) { 00163 if (PP.getCodeCompletionHandler()) 00164 PP.getCodeCompletionHandler()->CodeCompletePreprocessorExpression(); 00165 PP.setCodeCompletionReached(); 00166 PP.LexNonComment(PeekTok); 00167 } 00168 00169 // If this token's spelling is a pp-identifier, check to see if it is 00170 // 'defined' or if it is a macro. Note that we check here because many 00171 // keywords are pp-identifiers, so we can't check the kind. 00172 if (IdentifierInfo *II = PeekTok.getIdentifierInfo()) { 00173 // Handle "defined X" and "defined(X)". 00174 if (II->isStr("defined")) 00175 return(EvaluateDefined(Result, PeekTok, DT, ValueLive, PP)); 00176 00177 // If this identifier isn't 'defined' or one of the special 00178 // preprocessor keywords and it wasn't macro expanded, it turns 00179 // into a simple 0, unless it is the C++ keyword "true", in which case it 00180 // turns into "1". 00181 if (ValueLive) 00182 PP.Diag(PeekTok, diag::warn_pp_undef_identifier) << II; 00183 Result.Val = II->getTokenID() == tok::kw_true; 00184 Result.Val.setIsUnsigned(false); // "0" is signed intmax_t 0. 00185 Result.setRange(PeekTok.getLocation()); 00186 PP.LexNonComment(PeekTok); 00187 return false; 00188 } 00189 00190 switch (PeekTok.getKind()) { 00191 default: // Non-value token. 00192 PP.Diag(PeekTok, diag::err_pp_expr_bad_token_start_expr); 00193 return true; 00194 case tok::eod: 00195 case tok::r_paren: 00196 // If there is no expression, report and exit. 00197 PP.Diag(PeekTok, diag::err_pp_expected_value_in_expr); 00198 return true; 00199 case tok::numeric_constant: { 00200 SmallString<64> IntegerBuffer; 00201 bool NumberInvalid = false; 00202 StringRef Spelling = PP.getSpelling(PeekTok, IntegerBuffer, 00203 &NumberInvalid); 00204 if (NumberInvalid) 00205 return true; // a diagnostic was already reported 00206 00207 NumericLiteralParser Literal(Spelling.begin(), Spelling.end(), 00208 PeekTok.getLocation(), PP); 00209 if (Literal.hadError) 00210 return true; // a diagnostic was already reported. 00211 00212 if (Literal.isFloatingLiteral() || Literal.isImaginary) { 00213 PP.Diag(PeekTok, diag::err_pp_illegal_floating_literal); 00214 return true; 00215 } 00216 assert(Literal.isIntegerLiteral() && "Unknown ppnumber"); 00217 00218 // Complain about, and drop, any ud-suffix. 00219 if (Literal.hasUDSuffix()) 00220 PP.Diag(PeekTok, diag::err_pp_invalid_udl) << /*integer*/1; 00221 00222 // long long is a C99 feature. 00223 if (!PP.getLangOpts().C99 && Literal.isLongLong) 00224 PP.Diag(PeekTok, PP.getLangOpts().CPlusPlus0x ? 00225 diag::warn_cxx98_compat_longlong : diag::ext_longlong); 00226 00227 // Parse the integer literal into Result. 00228 if (Literal.GetIntegerValue(Result.Val)) { 00229 // Overflow parsing integer literal. 00230 if (ValueLive) PP.Diag(PeekTok, diag::warn_integer_too_large); 00231 Result.Val.setIsUnsigned(true); 00232 } else { 00233 // Set the signedness of the result to match whether there was a U suffix 00234 // or not. 00235 Result.Val.setIsUnsigned(Literal.isUnsigned); 00236 00237 // Detect overflow based on whether the value is signed. If signed 00238 // and if the value is too large, emit a warning "integer constant is so 00239 // large that it is unsigned" e.g. on 12345678901234567890 where intmax_t 00240 // is 64-bits. 00241 if (!Literal.isUnsigned && Result.Val.isNegative()) { 00242 // Don't warn for a hex literal: 0x8000..0 shouldn't warn. 00243 if (ValueLive && Literal.getRadix() != 16) 00244 PP.Diag(PeekTok, diag::warn_integer_too_large_for_signed); 00245 Result.Val.setIsUnsigned(true); 00246 } 00247 } 00248 00249 // Consume the token. 00250 Result.setRange(PeekTok.getLocation()); 00251 PP.LexNonComment(PeekTok); 00252 return false; 00253 } 00254 case tok::char_constant: // 'x' 00255 case tok::wide_char_constant: { // L'x' 00256 case tok::utf16_char_constant: // u'x' 00257 case tok::utf32_char_constant: // U'x' 00258 // Complain about, and drop, any ud-suffix. 00259 if (PeekTok.hasUDSuffix()) 00260 PP.Diag(PeekTok, diag::err_pp_invalid_udl) << /*character*/0; 00261 00262 SmallString<32> CharBuffer; 00263 bool CharInvalid = false; 00264 StringRef ThisTok = PP.getSpelling(PeekTok, CharBuffer, &CharInvalid); 00265 if (CharInvalid) 00266 return true; 00267 00268 CharLiteralParser Literal(ThisTok.begin(), ThisTok.end(), 00269 PeekTok.getLocation(), PP, PeekTok.getKind()); 00270 if (Literal.hadError()) 00271 return true; // A diagnostic was already emitted. 00272 00273 // Character literals are always int or wchar_t, expand to intmax_t. 00274 const TargetInfo &TI = PP.getTargetInfo(); 00275 unsigned NumBits; 00276 if (Literal.isMultiChar()) 00277 NumBits = TI.getIntWidth(); 00278 else if (Literal.isWide()) 00279 NumBits = TI.getWCharWidth(); 00280 else if (Literal.isUTF16()) 00281 NumBits = TI.getChar16Width(); 00282 else if (Literal.isUTF32()) 00283 NumBits = TI.getChar32Width(); 00284 else 00285 NumBits = TI.getCharWidth(); 00286 00287 // Set the width. 00288 llvm::APSInt Val(NumBits); 00289 // Set the value. 00290 Val = Literal.getValue(); 00291 // Set the signedness. UTF-16 and UTF-32 are always unsigned 00292 if (!Literal.isUTF16() && !Literal.isUTF32()) 00293 Val.setIsUnsigned(!PP.getLangOpts().CharIsSigned); 00294 00295 if (Result.Val.getBitWidth() > Val.getBitWidth()) { 00296 Result.Val = Val.extend(Result.Val.getBitWidth()); 00297 } else { 00298 assert(Result.Val.getBitWidth() == Val.getBitWidth() && 00299 "intmax_t smaller than char/wchar_t?"); 00300 Result.Val = Val; 00301 } 00302 00303 // Consume the token. 00304 Result.setRange(PeekTok.getLocation()); 00305 PP.LexNonComment(PeekTok); 00306 return false; 00307 } 00308 case tok::l_paren: { 00309 SourceLocation Start = PeekTok.getLocation(); 00310 PP.LexNonComment(PeekTok); // Eat the (. 00311 // Parse the value and if there are any binary operators involved, parse 00312 // them. 00313 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true; 00314 00315 // If this is a silly value like (X), which doesn't need parens, check for 00316 // !(defined X). 00317 if (PeekTok.is(tok::r_paren)) { 00318 // Just use DT unmodified as our result. 00319 } else { 00320 // Otherwise, we have something like (x+y), and we consumed '(x'. 00321 if (EvaluateDirectiveSubExpr(Result, 1, PeekTok, ValueLive, PP)) 00322 return true; 00323 00324 if (PeekTok.isNot(tok::r_paren)) { 00325 PP.Diag(PeekTok.getLocation(), diag::err_pp_expected_rparen) 00326 << Result.getRange(); 00327 PP.Diag(Start, diag::note_matching) << "("; 00328 return true; 00329 } 00330 DT.State = DefinedTracker::Unknown; 00331 } 00332 Result.setRange(Start, PeekTok.getLocation()); 00333 PP.LexNonComment(PeekTok); // Eat the ). 00334 return false; 00335 } 00336 case tok::plus: { 00337 SourceLocation Start = PeekTok.getLocation(); 00338 // Unary plus doesn't modify the value. 00339 PP.LexNonComment(PeekTok); 00340 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true; 00341 Result.setBegin(Start); 00342 return false; 00343 } 00344 case tok::minus: { 00345 SourceLocation Loc = PeekTok.getLocation(); 00346 PP.LexNonComment(PeekTok); 00347 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true; 00348 Result.setBegin(Loc); 00349 00350 // C99 6.5.3.3p3: The sign of the result matches the sign of the operand. 00351 Result.Val = -Result.Val; 00352 00353 // -MININT is the only thing that overflows. Unsigned never overflows. 00354 bool Overflow = !Result.isUnsigned() && Result.Val.isMinSignedValue(); 00355 00356 // If this operator is live and overflowed, report the issue. 00357 if (Overflow && ValueLive) 00358 PP.Diag(Loc, diag::warn_pp_expr_overflow) << Result.getRange(); 00359 00360 DT.State = DefinedTracker::Unknown; 00361 return false; 00362 } 00363 00364 case tok::tilde: { 00365 SourceLocation Start = PeekTok.getLocation(); 00366 PP.LexNonComment(PeekTok); 00367 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true; 00368 Result.setBegin(Start); 00369 00370 // C99 6.5.3.3p4: The sign of the result matches the sign of the operand. 00371 Result.Val = ~Result.Val; 00372 DT.State = DefinedTracker::Unknown; 00373 return false; 00374 } 00375 00376 case tok::exclaim: { 00377 SourceLocation Start = PeekTok.getLocation(); 00378 PP.LexNonComment(PeekTok); 00379 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true; 00380 Result.setBegin(Start); 00381 Result.Val = !Result.Val; 00382 // C99 6.5.3.3p5: The sign of the result is 'int', aka it is signed. 00383 Result.Val.setIsUnsigned(false); 00384 00385 if (DT.State == DefinedTracker::DefinedMacro) 00386 DT.State = DefinedTracker::NotDefinedMacro; 00387 else if (DT.State == DefinedTracker::NotDefinedMacro) 00388 DT.State = DefinedTracker::DefinedMacro; 00389 return false; 00390 } 00391 00392 // FIXME: Handle #assert 00393 } 00394 } 00395 00396 00397 00398 /// getPrecedence - Return the precedence of the specified binary operator 00399 /// token. This returns: 00400 /// ~0 - Invalid token. 00401 /// 14 -> 3 - various operators. 00402 /// 0 - 'eod' or ')' 00403 static unsigned getPrecedence(tok::TokenKind Kind) { 00404 switch (Kind) { 00405 default: return ~0U; 00406 case tok::percent: 00407 case tok::slash: 00408 case tok::star: return 14; 00409 case tok::plus: 00410 case tok::minus: return 13; 00411 case tok::lessless: 00412 case tok::greatergreater: return 12; 00413 case tok::lessequal: 00414 case tok::less: 00415 case tok::greaterequal: 00416 case tok::greater: return 11; 00417 case tok::exclaimequal: 00418 case tok::equalequal: return 10; 00419 case tok::amp: return 9; 00420 case tok::caret: return 8; 00421 case tok::pipe: return 7; 00422 case tok::ampamp: return 6; 00423 case tok::pipepipe: return 5; 00424 case tok::question: return 4; 00425 case tok::comma: return 3; 00426 case tok::colon: return 2; 00427 case tok::r_paren: return 0;// Lowest priority, end of expr. 00428 case tok::eod: return 0;// Lowest priority, end of directive. 00429 } 00430 } 00431 00432 00433 /// EvaluateDirectiveSubExpr - Evaluate the subexpression whose first token is 00434 /// PeekTok, and whose precedence is PeekPrec. This returns the result in LHS. 00435 /// 00436 /// If ValueLive is false, then this value is being evaluated in a context where 00437 /// the result is not used. As such, avoid diagnostics that relate to 00438 /// evaluation, such as division by zero warnings. 00439 static bool EvaluateDirectiveSubExpr(PPValue &LHS, unsigned MinPrec, 00440 Token &PeekTok, bool ValueLive, 00441 Preprocessor &PP) { 00442 unsigned PeekPrec = getPrecedence(PeekTok.getKind()); 00443 // If this token isn't valid, report the error. 00444 if (PeekPrec == ~0U) { 00445 PP.Diag(PeekTok.getLocation(), diag::err_pp_expr_bad_token_binop) 00446 << LHS.getRange(); 00447 return true; 00448 } 00449 00450 while (1) { 00451 // If this token has a lower precedence than we are allowed to parse, return 00452 // it so that higher levels of the recursion can parse it. 00453 if (PeekPrec < MinPrec) 00454 return false; 00455 00456 tok::TokenKind Operator = PeekTok.getKind(); 00457 00458 // If this is a short-circuiting operator, see if the RHS of the operator is 00459 // dead. Note that this cannot just clobber ValueLive. Consider 00460 // "0 && 1 ? 4 : 1 / 0", which is parsed as "(0 && 1) ? 4 : (1 / 0)". In 00461 // this example, the RHS of the && being dead does not make the rest of the 00462 // expr dead. 00463 bool RHSIsLive; 00464 if (Operator == tok::ampamp && LHS.Val == 0) 00465 RHSIsLive = false; // RHS of "0 && x" is dead. 00466 else if (Operator == tok::pipepipe && LHS.Val != 0) 00467 RHSIsLive = false; // RHS of "1 || x" is dead. 00468 else if (Operator == tok::question && LHS.Val == 0) 00469 RHSIsLive = false; // RHS (x) of "0 ? x : y" is dead. 00470 else 00471 RHSIsLive = ValueLive; 00472 00473 // Consume the operator, remembering the operator's location for reporting. 00474 SourceLocation OpLoc = PeekTok.getLocation(); 00475 PP.LexNonComment(PeekTok); 00476 00477 PPValue RHS(LHS.getBitWidth()); 00478 // Parse the RHS of the operator. 00479 DefinedTracker DT; 00480 if (EvaluateValue(RHS, PeekTok, DT, RHSIsLive, PP)) return true; 00481 00482 // Remember the precedence of this operator and get the precedence of the 00483 // operator immediately to the right of the RHS. 00484 unsigned ThisPrec = PeekPrec; 00485 PeekPrec = getPrecedence(PeekTok.getKind()); 00486 00487 // If this token isn't valid, report the error. 00488 if (PeekPrec == ~0U) { 00489 PP.Diag(PeekTok.getLocation(), diag::err_pp_expr_bad_token_binop) 00490 << RHS.getRange(); 00491 return true; 00492 } 00493 00494 // Decide whether to include the next binop in this subexpression. For 00495 // example, when parsing x+y*z and looking at '*', we want to recursively 00496 // handle y*z as a single subexpression. We do this because the precedence 00497 // of * is higher than that of +. The only strange case we have to handle 00498 // here is for the ?: operator, where the precedence is actually lower than 00499 // the LHS of the '?'. The grammar rule is: 00500 // 00501 // conditional-expression ::= 00502 // logical-OR-expression ? expression : conditional-expression 00503 // where 'expression' is actually comma-expression. 00504 unsigned RHSPrec; 00505 if (Operator == tok::question) 00506 // The RHS of "?" should be maximally consumed as an expression. 00507 RHSPrec = getPrecedence(tok::comma); 00508 else // All others should munch while higher precedence. 00509 RHSPrec = ThisPrec+1; 00510 00511 if (PeekPrec >= RHSPrec) { 00512 if (EvaluateDirectiveSubExpr(RHS, RHSPrec, PeekTok, RHSIsLive, PP)) 00513 return true; 00514 PeekPrec = getPrecedence(PeekTok.getKind()); 00515 } 00516 assert(PeekPrec <= ThisPrec && "Recursion didn't work!"); 00517 00518 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if 00519 // either operand is unsigned. 00520 llvm::APSInt Res(LHS.getBitWidth()); 00521 switch (Operator) { 00522 case tok::question: // No UAC for x and y in "x ? y : z". 00523 case tok::lessless: // Shift amount doesn't UAC with shift value. 00524 case tok::greatergreater: // Shift amount doesn't UAC with shift value. 00525 case tok::comma: // Comma operands are not subject to UACs. 00526 case tok::pipepipe: // Logical || does not do UACs. 00527 case tok::ampamp: // Logical && does not do UACs. 00528 break; // No UAC 00529 default: 00530 Res.setIsUnsigned(LHS.isUnsigned()|RHS.isUnsigned()); 00531 // If this just promoted something from signed to unsigned, and if the 00532 // value was negative, warn about it. 00533 if (ValueLive && Res.isUnsigned()) { 00534 if (!LHS.isUnsigned() && LHS.Val.isNegative()) 00535 PP.Diag(OpLoc, diag::warn_pp_convert_lhs_to_positive) 00536 << LHS.Val.toString(10, true) + " to " + 00537 LHS.Val.toString(10, false) 00538 << LHS.getRange() << RHS.getRange(); 00539 if (!RHS.isUnsigned() && RHS.Val.isNegative()) 00540 PP.Diag(OpLoc, diag::warn_pp_convert_rhs_to_positive) 00541 << RHS.Val.toString(10, true) + " to " + 00542 RHS.Val.toString(10, false) 00543 << LHS.getRange() << RHS.getRange(); 00544 } 00545 LHS.Val.setIsUnsigned(Res.isUnsigned()); 00546 RHS.Val.setIsUnsigned(Res.isUnsigned()); 00547 } 00548 00549 bool Overflow = false; 00550 switch (Operator) { 00551 default: llvm_unreachable("Unknown operator token!"); 00552 case tok::percent: 00553 if (RHS.Val != 0) 00554 Res = LHS.Val % RHS.Val; 00555 else if (ValueLive) { 00556 PP.Diag(OpLoc, diag::err_pp_remainder_by_zero) 00557 << LHS.getRange() << RHS.getRange(); 00558 return true; 00559 } 00560 break; 00561 case tok::slash: 00562 if (RHS.Val != 0) { 00563 if (LHS.Val.isSigned()) 00564 Res = llvm::APSInt(LHS.Val.sdiv_ov(RHS.Val, Overflow), false); 00565 else 00566 Res = LHS.Val / RHS.Val; 00567 } else if (ValueLive) { 00568 PP.Diag(OpLoc, diag::err_pp_division_by_zero) 00569 << LHS.getRange() << RHS.getRange(); 00570 return true; 00571 } 00572 break; 00573 00574 case tok::star: 00575 if (Res.isSigned()) 00576 Res = llvm::APSInt(LHS.Val.smul_ov(RHS.Val, Overflow), false); 00577 else 00578 Res = LHS.Val * RHS.Val; 00579 break; 00580 case tok::lessless: { 00581 // Determine whether overflow is about to happen. 00582 unsigned ShAmt = static_cast<unsigned>(RHS.Val.getLimitedValue()); 00583 if (LHS.isUnsigned()) { 00584 Overflow = ShAmt >= LHS.Val.getBitWidth(); 00585 if (Overflow) 00586 ShAmt = LHS.Val.getBitWidth()-1; 00587 Res = LHS.Val << ShAmt; 00588 } else { 00589 Res = llvm::APSInt(LHS.Val.sshl_ov(ShAmt, Overflow), false); 00590 } 00591 break; 00592 } 00593 case tok::greatergreater: { 00594 // Determine whether overflow is about to happen. 00595 unsigned ShAmt = static_cast<unsigned>(RHS.Val.getLimitedValue()); 00596 if (ShAmt >= LHS.getBitWidth()) 00597 Overflow = true, ShAmt = LHS.getBitWidth()-1; 00598 Res = LHS.Val >> ShAmt; 00599 break; 00600 } 00601 case tok::plus: 00602 if (LHS.isUnsigned()) 00603 Res = LHS.Val + RHS.Val; 00604 else 00605 Res = llvm::APSInt(LHS.Val.sadd_ov(RHS.Val, Overflow), false); 00606 break; 00607 case tok::minus: 00608 if (LHS.isUnsigned()) 00609 Res = LHS.Val - RHS.Val; 00610 else 00611 Res = llvm::APSInt(LHS.Val.ssub_ov(RHS.Val, Overflow), false); 00612 break; 00613 case tok::lessequal: 00614 Res = LHS.Val <= RHS.Val; 00615 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed) 00616 break; 00617 case tok::less: 00618 Res = LHS.Val < RHS.Val; 00619 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed) 00620 break; 00621 case tok::greaterequal: 00622 Res = LHS.Val >= RHS.Val; 00623 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed) 00624 break; 00625 case tok::greater: 00626 Res = LHS.Val > RHS.Val; 00627 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed) 00628 break; 00629 case tok::exclaimequal: 00630 Res = LHS.Val != RHS.Val; 00631 Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed) 00632 break; 00633 case tok::equalequal: 00634 Res = LHS.Val == RHS.Val; 00635 Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed) 00636 break; 00637 case tok::amp: 00638 Res = LHS.Val & RHS.Val; 00639 break; 00640 case tok::caret: 00641 Res = LHS.Val ^ RHS.Val; 00642 break; 00643 case tok::pipe: 00644 Res = LHS.Val | RHS.Val; 00645 break; 00646 case tok::ampamp: 00647 Res = (LHS.Val != 0 && RHS.Val != 0); 00648 Res.setIsUnsigned(false); // C99 6.5.13p3, result is always int (signed) 00649 break; 00650 case tok::pipepipe: 00651 Res = (LHS.Val != 0 || RHS.Val != 0); 00652 Res.setIsUnsigned(false); // C99 6.5.14p3, result is always int (signed) 00653 break; 00654 case tok::comma: 00655 // Comma is invalid in pp expressions in c89/c++ mode, but is valid in C99 00656 // if not being evaluated. 00657 if (!PP.getLangOpts().C99 || ValueLive) 00658 PP.Diag(OpLoc, diag::ext_pp_comma_expr) 00659 << LHS.getRange() << RHS.getRange(); 00660 Res = RHS.Val; // LHS = LHS,RHS -> RHS. 00661 break; 00662 case tok::question: { 00663 // Parse the : part of the expression. 00664 if (PeekTok.isNot(tok::colon)) { 00665 PP.Diag(PeekTok.getLocation(), diag::err_expected_colon) 00666 << LHS.getRange(), RHS.getRange(); 00667 PP.Diag(OpLoc, diag::note_matching) << "?"; 00668 return true; 00669 } 00670 // Consume the :. 00671 PP.LexNonComment(PeekTok); 00672 00673 // Evaluate the value after the :. 00674 bool AfterColonLive = ValueLive && LHS.Val == 0; 00675 PPValue AfterColonVal(LHS.getBitWidth()); 00676 DefinedTracker DT; 00677 if (EvaluateValue(AfterColonVal, PeekTok, DT, AfterColonLive, PP)) 00678 return true; 00679 00680 // Parse anything after the : with the same precedence as ?. We allow 00681 // things of equal precedence because ?: is right associative. 00682 if (EvaluateDirectiveSubExpr(AfterColonVal, ThisPrec, 00683 PeekTok, AfterColonLive, PP)) 00684 return true; 00685 00686 // Now that we have the condition, the LHS and the RHS of the :, evaluate. 00687 Res = LHS.Val != 0 ? RHS.Val : AfterColonVal.Val; 00688 RHS.setEnd(AfterColonVal.getRange().getEnd()); 00689 00690 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if 00691 // either operand is unsigned. 00692 Res.setIsUnsigned(RHS.isUnsigned() | AfterColonVal.isUnsigned()); 00693 00694 // Figure out the precedence of the token after the : part. 00695 PeekPrec = getPrecedence(PeekTok.getKind()); 00696 break; 00697 } 00698 case tok::colon: 00699 // Don't allow :'s to float around without being part of ?: exprs. 00700 PP.Diag(OpLoc, diag::err_pp_colon_without_question) 00701 << LHS.getRange() << RHS.getRange(); 00702 return true; 00703 } 00704 00705 // If this operator is live and overflowed, report the issue. 00706 if (Overflow && ValueLive) 00707 PP.Diag(OpLoc, diag::warn_pp_expr_overflow) 00708 << LHS.getRange() << RHS.getRange(); 00709 00710 // Put the result back into 'LHS' for our next iteration. 00711 LHS.Val = Res; 00712 LHS.setEnd(RHS.getRange().getEnd()); 00713 } 00714 } 00715 00716 /// EvaluateDirectiveExpression - Evaluate an integer constant expression that 00717 /// may occur after a #if or #elif directive. If the expression is equivalent 00718 /// to "!defined(X)" return X in IfNDefMacro. 00719 bool Preprocessor:: 00720 EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro) { 00721 // Save the current state of 'DisableMacroExpansion' and reset it to false. If 00722 // 'DisableMacroExpansion' is true, then we must be in a macro argument list 00723 // in which case a directive is undefined behavior. We want macros to be able 00724 // to recursively expand in order to get more gcc-list behavior, so we force 00725 // DisableMacroExpansion to false and restore it when we're done parsing the 00726 // expression. 00727 bool DisableMacroExpansionAtStartOfDirective = DisableMacroExpansion; 00728 DisableMacroExpansion = false; 00729 00730 // Peek ahead one token. 00731 Token Tok; 00732 LexNonComment(Tok); 00733 00734 // C99 6.10.1p3 - All expressions are evaluated as intmax_t or uintmax_t. 00735 unsigned BitWidth = getTargetInfo().getIntMaxTWidth(); 00736 00737 PPValue ResVal(BitWidth); 00738 DefinedTracker DT; 00739 if (EvaluateValue(ResVal, Tok, DT, true, *this)) { 00740 // Parse error, skip the rest of the macro line. 00741 if (Tok.isNot(tok::eod)) 00742 DiscardUntilEndOfDirective(); 00743 00744 // Restore 'DisableMacroExpansion'. 00745 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective; 00746 return false; 00747 } 00748 00749 // If we are at the end of the expression after just parsing a value, there 00750 // must be no (unparenthesized) binary operators involved, so we can exit 00751 // directly. 00752 if (Tok.is(tok::eod)) { 00753 // If the expression we parsed was of the form !defined(macro), return the 00754 // macro in IfNDefMacro. 00755 if (DT.State == DefinedTracker::NotDefinedMacro) 00756 IfNDefMacro = DT.TheMacro; 00757 00758 // Restore 'DisableMacroExpansion'. 00759 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective; 00760 return ResVal.Val != 0; 00761 } 00762 00763 // Otherwise, we must have a binary operator (e.g. "#if 1 < 2"), so parse the 00764 // operator and the stuff after it. 00765 if (EvaluateDirectiveSubExpr(ResVal, getPrecedence(tok::question), 00766 Tok, true, *this)) { 00767 // Parse error, skip the rest of the macro line. 00768 if (Tok.isNot(tok::eod)) 00769 DiscardUntilEndOfDirective(); 00770 00771 // Restore 'DisableMacroExpansion'. 00772 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective; 00773 return false; 00774 } 00775 00776 // If we aren't at the tok::eod token, something bad happened, like an extra 00777 // ')' token. 00778 if (Tok.isNot(tok::eod)) { 00779 Diag(Tok, diag::err_pp_expected_eol); 00780 DiscardUntilEndOfDirective(); 00781 } 00782 00783 // Restore 'DisableMacroExpansion'. 00784 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective; 00785 return ResVal.Val != 0; 00786 }