2008-01-31 19:34:24 +00:00
|
|
|
//= RValues.cpp - Abstract RValues for Path-Sens. Value Tracking -*- C++ -*-==//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This files defines RValue, LValue, and NonLValue, classes that represent
|
|
|
|
// abstract r-values for use with path-sensitive value tracking.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-02-14 17:30:51 +00:00
|
|
|
#include "clang/Analysis/PathSensitive/RValues.h"
|
2008-02-16 01:12:31 +00:00
|
|
|
#include "llvm/Support/Streams.h"
|
2008-01-31 19:34:24 +00:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using llvm::dyn_cast;
|
|
|
|
using llvm::cast;
|
|
|
|
using llvm::APSInt;
|
|
|
|
|
2008-02-14 23:25:54 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Symbol Iteration.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
RValue::symbol_iterator RValue::symbol_begin() const {
|
|
|
|
if (isa<LValue>(this)) {
|
|
|
|
if (isa<lval::SymbolVal>(this))
|
|
|
|
return (symbol_iterator) (&Data);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (isa<nonlval::SymbolVal>(this))
|
|
|
|
return (symbol_iterator) (&Data);
|
|
|
|
else if (isa<nonlval::SymIntConstraintVal>(this)) {
|
|
|
|
const SymIntConstraint& C =
|
|
|
|
cast<nonlval::SymIntConstraintVal>(this)->getConstraint();
|
|
|
|
return (symbol_iterator) &C.getSymbol();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-01-31 19:34:24 +00:00
|
|
|
|
2008-02-14 23:25:54 +00:00
|
|
|
RValue::symbol_iterator RValue::symbol_end() const {
|
|
|
|
symbol_iterator X = symbol_begin();
|
|
|
|
return X ? X+1 : NULL;
|
|
|
|
}
|
2008-02-06 22:50:25 +00:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Transfer function dispatch for Non-LValues.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
nonlval::ConcreteInt
|
|
|
|
nonlval::ConcreteInt::EvalBinaryOp(ValueManager& ValMgr,
|
|
|
|
BinaryOperator::Opcode Op,
|
|
|
|
const nonlval::ConcreteInt& RHS) const {
|
|
|
|
|
2008-02-16 01:12:31 +00:00
|
|
|
return ValMgr.EvaluateAPSInt(Op, getValue(), RHS.getValue());
|
2008-02-06 22:50:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Bitwise-Complement.
|
|
|
|
|
2008-01-31 19:34:24 +00:00
|
|
|
|
2008-02-06 22:50:25 +00:00
|
|
|
nonlval::ConcreteInt
|
|
|
|
nonlval::ConcreteInt::EvalComplement(ValueManager& ValMgr) const {
|
|
|
|
return ValMgr.getValue(~getValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unary Minus.
|
2008-02-01 06:36:40 +00:00
|
|
|
|
2008-02-06 22:50:25 +00:00
|
|
|
nonlval::ConcreteInt
|
|
|
|
nonlval::ConcreteInt::EvalMinus(ValueManager& ValMgr, UnaryOperator* U) const {
|
|
|
|
assert (U->getType() == U->getSubExpr()->getType());
|
|
|
|
assert (U->getType()->isIntegerType());
|
|
|
|
return ValMgr.getValue(-getValue());
|
2008-02-04 16:58:30 +00:00
|
|
|
}
|
|
|
|
|
2008-02-06 22:50:25 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Transfer function dispatch for LValues.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2008-02-04 16:58:30 +00:00
|
|
|
|
2008-02-06 22:50:25 +00:00
|
|
|
lval::ConcreteInt
|
|
|
|
lval::ConcreteInt::EvalBinaryOp(ValueManager& ValMgr,
|
|
|
|
BinaryOperator::Opcode Op,
|
|
|
|
const lval::ConcreteInt& RHS) const {
|
|
|
|
|
|
|
|
assert (Op == BinaryOperator::Add || Op == BinaryOperator::Sub ||
|
|
|
|
(Op >= BinaryOperator::LT && Op <= BinaryOperator::NE));
|
|
|
|
|
2008-02-16 01:12:31 +00:00
|
|
|
return ValMgr.EvaluateAPSInt(Op, getValue(), RHS.getValue());
|
2008-01-31 19:34:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NonLValue LValue::EQ(ValueManager& ValMgr, const LValue& RHS) const {
|
|
|
|
switch (getSubKind()) {
|
|
|
|
default:
|
|
|
|
assert(false && "EQ not implemented for this LValue.");
|
2008-02-08 02:57:34 +00:00
|
|
|
return cast<NonLValue>(UnknownVal());
|
2008-02-06 22:50:25 +00:00
|
|
|
|
2008-02-05 23:08:41 +00:00
|
|
|
case lval::ConcreteIntKind:
|
|
|
|
if (isa<lval::ConcreteInt>(RHS)) {
|
|
|
|
bool b = cast<lval::ConcreteInt>(this)->getValue() ==
|
2008-02-06 22:50:25 +00:00
|
|
|
cast<lval::ConcreteInt>(RHS).getValue();
|
|
|
|
|
2008-02-05 23:08:41 +00:00
|
|
|
return NonLValue::GetIntTruthValue(ValMgr, b);
|
|
|
|
}
|
|
|
|
else if (isa<lval::SymbolVal>(RHS)) {
|
2008-02-06 22:50:25 +00:00
|
|
|
|
2008-02-05 23:08:41 +00:00
|
|
|
const SymIntConstraint& C =
|
2008-02-06 22:50:25 +00:00
|
|
|
ValMgr.getConstraint(cast<lval::SymbolVal>(RHS).getSymbol(),
|
|
|
|
BinaryOperator::EQ,
|
|
|
|
cast<lval::ConcreteInt>(this)->getValue());
|
2008-02-05 23:08:41 +00:00
|
|
|
|
|
|
|
return nonlval::SymIntConstraintVal(C);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2008-02-06 22:50:25 +00:00
|
|
|
case lval::SymbolValKind: {
|
|
|
|
if (isa<lval::ConcreteInt>(RHS)) {
|
|
|
|
|
|
|
|
const SymIntConstraint& C =
|
2008-02-05 23:08:41 +00:00
|
|
|
ValMgr.getConstraint(cast<lval::SymbolVal>(this)->getSymbol(),
|
|
|
|
BinaryOperator::EQ,
|
|
|
|
cast<lval::ConcreteInt>(RHS).getValue());
|
2008-02-06 22:50:25 +00:00
|
|
|
|
|
|
|
return nonlval::SymIntConstraintVal(C);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert (!isa<lval::SymbolVal>(RHS) && "FIXME: Implement unification.");
|
2008-02-05 23:08:41 +00:00
|
|
|
|
2008-02-06 22:50:25 +00:00
|
|
|
break;
|
2008-02-05 23:08:41 +00:00
|
|
|
}
|
2008-01-31 19:34:24 +00:00
|
|
|
|
2008-02-06 22:50:25 +00:00
|
|
|
case lval::DeclValKind:
|
2008-02-05 23:08:41 +00:00
|
|
|
if (isa<lval::DeclVal>(RHS)) {
|
|
|
|
bool b = cast<lval::DeclVal>(*this) == cast<lval::DeclVal>(RHS);
|
|
|
|
return NonLValue::GetIntTruthValue(ValMgr, b);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2008-01-31 19:34:24 +00:00
|
|
|
}
|
2008-02-05 23:08:41 +00:00
|
|
|
|
|
|
|
return NonLValue::GetIntTruthValue(ValMgr, false);
|
2008-01-31 19:34:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NonLValue LValue::NE(ValueManager& ValMgr, const LValue& RHS) const {
|
|
|
|
switch (getSubKind()) {
|
|
|
|
default:
|
2008-02-05 23:08:41 +00:00
|
|
|
assert(false && "NE not implemented for this LValue.");
|
2008-02-08 02:57:34 +00:00
|
|
|
return cast<NonLValue>(UnknownVal());
|
2008-01-31 19:34:24 +00:00
|
|
|
|
2008-02-05 23:08:41 +00:00
|
|
|
case lval::ConcreteIntKind:
|
|
|
|
if (isa<lval::ConcreteInt>(RHS)) {
|
|
|
|
bool b = cast<lval::ConcreteInt>(this)->getValue() !=
|
2008-02-06 22:50:25 +00:00
|
|
|
cast<lval::ConcreteInt>(RHS).getValue();
|
2008-02-05 23:08:41 +00:00
|
|
|
|
|
|
|
return NonLValue::GetIntTruthValue(ValMgr, b);
|
|
|
|
}
|
|
|
|
else if (isa<lval::SymbolVal>(RHS)) {
|
|
|
|
|
|
|
|
const SymIntConstraint& C =
|
|
|
|
ValMgr.getConstraint(cast<lval::SymbolVal>(RHS).getSymbol(),
|
|
|
|
BinaryOperator::NE,
|
|
|
|
cast<lval::ConcreteInt>(this)->getValue());
|
|
|
|
|
|
|
|
return nonlval::SymIntConstraintVal(C);
|
|
|
|
}
|
2008-02-01 06:36:40 +00:00
|
|
|
|
2008-02-05 23:08:41 +00:00
|
|
|
break;
|
2008-02-01 06:36:40 +00:00
|
|
|
|
2008-02-05 23:08:41 +00:00
|
|
|
case lval::SymbolValKind: {
|
|
|
|
if (isa<lval::ConcreteInt>(RHS)) {
|
|
|
|
|
|
|
|
const SymIntConstraint& C =
|
|
|
|
ValMgr.getConstraint(cast<lval::SymbolVal>(this)->getSymbol(),
|
|
|
|
BinaryOperator::NE,
|
|
|
|
cast<lval::ConcreteInt>(RHS).getValue());
|
|
|
|
|
|
|
|
return nonlval::SymIntConstraintVal(C);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert (!isa<lval::SymbolVal>(RHS) && "FIXME: Implement sym !=.");
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case lval::DeclValKind:
|
|
|
|
if (isa<lval::DeclVal>(RHS)) {
|
|
|
|
bool b = cast<lval::DeclVal>(*this) == cast<lval::DeclVal>(RHS);
|
|
|
|
return NonLValue::GetIntTruthValue(ValMgr, b);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2008-01-31 19:34:24 +00:00
|
|
|
}
|
2008-02-05 23:08:41 +00:00
|
|
|
|
|
|
|
return NonLValue::GetIntTruthValue(ValMgr, true);
|
2008-01-31 19:34:24 +00:00
|
|
|
}
|
|
|
|
|
2008-02-06 22:50:25 +00:00
|
|
|
|
2008-01-31 19:34:24 +00:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Utility methods for constructing Non-LValues.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
NonLValue NonLValue::GetValue(ValueManager& ValMgr, uint64_t X, QualType T,
|
|
|
|
SourceLocation Loc) {
|
|
|
|
|
2008-02-05 21:52:21 +00:00
|
|
|
return nonlval::ConcreteInt(ValMgr.getValue(X, T, Loc));
|
2008-01-31 19:34:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NonLValue NonLValue::GetValue(ValueManager& ValMgr, IntegerLiteral* I) {
|
2008-02-05 21:52:21 +00:00
|
|
|
return nonlval::ConcreteInt(ValMgr.getValue(APSInt(I->getValue(),
|
|
|
|
I->getType()->isUnsignedIntegerType())));
|
2008-01-31 19:34:24 +00:00
|
|
|
}
|
|
|
|
|
2008-02-06 22:50:25 +00:00
|
|
|
NonLValue NonLValue::GetIntTruthValue(ValueManager& ValMgr, bool b) {
|
|
|
|
return nonlval::ConcreteInt(ValMgr.getTruthValue(b));
|
|
|
|
}
|
|
|
|
|
2008-01-31 19:34:24 +00:00
|
|
|
RValue RValue::GetSymbolValue(SymbolManager& SymMgr, ParmVarDecl* D) {
|
|
|
|
QualType T = D->getType();
|
|
|
|
|
|
|
|
if (T->isPointerType() || T->isReferenceType())
|
2008-02-05 21:52:21 +00:00
|
|
|
return lval::SymbolVal(SymMgr.getSymbol(D));
|
2008-01-31 19:34:24 +00:00
|
|
|
else
|
2008-02-05 21:52:21 +00:00
|
|
|
return nonlval::SymbolVal(SymMgr.getSymbol(D));
|
2008-01-31 19:34:24 +00:00
|
|
|
}
|
|
|
|
|
2008-02-12 21:37:56 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Utility methods for constructing LValues.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
LValue LValue::GetValue(AddrLabelExpr* E) {
|
|
|
|
return lval::GotoLabel(E->getLabel());
|
2008-02-07 01:08:27 +00:00
|
|
|
}
|
2008-02-06 22:50:25 +00:00
|
|
|
|
2008-01-31 19:34:24 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Pretty-Printing.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-02-12 21:37:56 +00:00
|
|
|
void RValue::print() const {
|
|
|
|
print(*llvm::cerr.stream());
|
|
|
|
}
|
|
|
|
|
2008-01-31 19:34:24 +00:00
|
|
|
void RValue::print(std::ostream& Out) const {
|
|
|
|
switch (getBaseKind()) {
|
2008-02-08 03:02:48 +00:00
|
|
|
case UnknownKind:
|
2008-01-31 19:34:24 +00:00
|
|
|
Out << "Invalid";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NonLValueKind:
|
|
|
|
cast<NonLValue>(this)->print(Out);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LValueKind:
|
|
|
|
cast<LValue>(this)->print(Out);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UninitializedKind:
|
|
|
|
Out << "Uninitialized";
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert (false && "Invalid RValue.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-05 23:08:41 +00:00
|
|
|
static void printOpcode(std::ostream& Out, BinaryOperator::Opcode Op) {
|
2008-02-15 22:09:30 +00:00
|
|
|
switch (Op) {
|
|
|
|
case BinaryOperator::Mul: Out << "*"; break;
|
|
|
|
case BinaryOperator::Div: Out << "/"; break;
|
|
|
|
case BinaryOperator::Rem: Out << "%" ; break;
|
2008-02-07 15:20:13 +00:00
|
|
|
case BinaryOperator::Add: Out << "+" ; break;
|
|
|
|
case BinaryOperator::Sub: Out << "-" ; break;
|
2008-02-15 22:09:30 +00:00
|
|
|
case BinaryOperator::Shl: Out << "<<" ; break;
|
|
|
|
case BinaryOperator::Shr: Out << ">>" ; break;
|
|
|
|
case BinaryOperator::LT: Out << "<" ; break;
|
|
|
|
case BinaryOperator::GT: Out << ">" ; break;
|
|
|
|
case BinaryOperator::LE: Out << "<=" ; break;
|
|
|
|
case BinaryOperator::GE: Out << ">=" ; break;
|
2008-02-05 23:08:41 +00:00
|
|
|
case BinaryOperator::EQ: Out << "=="; break;
|
|
|
|
case BinaryOperator::NE: Out << "!="; break;
|
2008-02-15 22:09:30 +00:00
|
|
|
case BinaryOperator::And: Out << "&" ; break;
|
|
|
|
case BinaryOperator::Xor: Out << "^" ; break;
|
|
|
|
case BinaryOperator::Or: Out << "|" ; break;
|
2008-02-05 23:08:41 +00:00
|
|
|
default: assert(false && "Not yet implemented.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-31 19:34:24 +00:00
|
|
|
void NonLValue::print(std::ostream& Out) const {
|
|
|
|
switch (getSubKind()) {
|
2008-02-05 21:52:21 +00:00
|
|
|
case nonlval::ConcreteIntKind:
|
|
|
|
Out << cast<nonlval::ConcreteInt>(this)->getValue().toString();
|
2008-02-06 22:50:25 +00:00
|
|
|
|
|
|
|
if (cast<nonlval::ConcreteInt>(this)->getValue().isUnsigned())
|
|
|
|
Out << 'U';
|
|
|
|
|
2008-01-31 19:34:24 +00:00
|
|
|
break;
|
|
|
|
|
2008-02-05 21:52:21 +00:00
|
|
|
case nonlval::SymbolValKind:
|
2008-02-05 23:08:41 +00:00
|
|
|
Out << '$' << cast<nonlval::SymbolVal>(this)->getSymbol();
|
2008-01-31 19:34:24 +00:00
|
|
|
break;
|
2008-02-05 23:08:41 +00:00
|
|
|
|
|
|
|
case nonlval::SymIntConstraintValKind: {
|
|
|
|
const nonlval::SymIntConstraintVal& C =
|
|
|
|
*cast<nonlval::SymIntConstraintVal>(this);
|
|
|
|
|
|
|
|
Out << '$' << C.getConstraint().getSymbol() << ' ';
|
|
|
|
printOpcode(Out, C.getConstraint().getOpcode());
|
|
|
|
Out << ' ' << C.getConstraint().getInt().toString();
|
2008-02-06 22:50:25 +00:00
|
|
|
|
|
|
|
if (C.getConstraint().getInt().isUnsigned())
|
|
|
|
Out << 'U';
|
|
|
|
|
2008-02-05 23:08:41 +00:00
|
|
|
break;
|
|
|
|
}
|
2008-01-31 19:34:24 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
assert (false && "Pretty-printed not implemented for this NonLValue.");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-07 05:48:01 +00:00
|
|
|
|
2008-01-31 19:34:24 +00:00
|
|
|
void LValue::print(std::ostream& Out) const {
|
2008-02-01 06:36:40 +00:00
|
|
|
switch (getSubKind()) {
|
2008-02-05 21:52:21 +00:00
|
|
|
case lval::ConcreteIntKind:
|
|
|
|
Out << cast<lval::ConcreteInt>(this)->getValue().toString()
|
2008-02-01 06:36:40 +00:00
|
|
|
<< " (LValue)";
|
|
|
|
break;
|
|
|
|
|
2008-02-05 21:52:21 +00:00
|
|
|
case lval::SymbolValKind:
|
2008-02-05 23:08:41 +00:00
|
|
|
Out << '$' << cast<lval::SymbolVal>(this)->getSymbol();
|
2008-01-31 19:34:24 +00:00
|
|
|
break;
|
2008-02-12 21:37:56 +00:00
|
|
|
|
|
|
|
case lval::GotoLabelKind:
|
|
|
|
Out << "&&"
|
|
|
|
<< cast<lval::GotoLabel>(this)->getLabel()->getID()->getName();
|
|
|
|
break;
|
2008-02-06 04:31:33 +00:00
|
|
|
|
2008-02-05 21:52:21 +00:00
|
|
|
case lval::DeclValKind:
|
2008-01-31 19:34:24 +00:00
|
|
|
Out << '&'
|
2008-02-05 21:52:21 +00:00
|
|
|
<< cast<lval::DeclVal>(this)->getDecl()->getIdentifier()->getName();
|
2008-01-31 19:34:24 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert (false && "Pretty-printed not implemented for this LValue.");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-02-07 05:48:01 +00:00
|
|
|
|