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