Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unused variable detection #429

Merged
merged 2 commits into from
Jul 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
Cargo.lock
/target
**/*.rs.bk

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ serde = "1.0"
serde_derive = { version = "1.0" }
inkwell = { version = "0.1.0-beta.2", features = ["target-webassembly", "target-bpf", "llvm11-0"] }
blake2-rfc = "0.2.18"
phf = { version = "0.8", features = ["macros"] }
phf = { version = "0.9", features = ["macros"] }
unicode-xid = "0.2.0"
handlebars = "3.4"
contract-metadata = "0.2.0"
Expand All @@ -54,7 +54,7 @@ ethereum-types = "0.10"
wasmi = "0.9"
rand = "0.8"
sha2 = "0.9"
solana_rbpf = "0.2"
solana_rbpf = "^0.2.11"
byteorder = "1.3"
assert_cmd = "1.0"
bincode = "1.3"
Expand Down
2 changes: 1 addition & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fn main() {
let cxxflags = Command::new("llvm-config")
.args(&["--cxxflags"])
.output()
.unwrap();
.expect("could not execute llvm-config");

let cxxflags = String::from_utf8(cxxflags.stdout).unwrap();

Expand Down
6 changes: 6 additions & 0 deletions src/codegen/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,12 @@ impl ControlFlowGraph {
ty.to_string(ns),
self.expr_to_string(contract, ns, e)
),
Expression::BytesCast(_, ty, from, e) => format!(
"{} from:{} ({})",
ty.to_string(ns),
from.to_string(ns),
self.expr_to_string(contract, ns, e)
),
Expression::Builtin(_, _, builtin, args) => format!(
"(builtin {:?} ({}))",
builtin,
Expand Down
2 changes: 1 addition & 1 deletion src/codegen/constant_folding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1062,7 +1062,7 @@ fn expression(
| Expression::FormatString { .. }
| Expression::InternalFunctionCfg(_) => (expr.clone(), false),
// nothing else is permitted in cfg
_ => unreachable!(),
_ => panic!("expr should not be in cfg: {:?}", expr),
}
}

Expand Down
17 changes: 12 additions & 5 deletions src/codegen/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,12 @@ pub fn expression(
)
}
}
Expression::BytesCast(loc, ty, from, e) => Expression::BytesCast(
*loc,
ty.clone(),
from.clone(),
Box::new(expression(e, cfg, contract_no, ns, vartab)),
),
Expression::Load(loc, ty, e) => Expression::Load(
*loc,
ty.clone(),
Expand Down Expand Up @@ -1784,24 +1790,25 @@ fn array_subscript(
Type::Bytes(array_length) => {
let res_ty = Type::Bytes(1);
let from_ty = Type::Bytes(*array_length);
let index_ty = Type::Uint(*array_length as u16 * 8);

Expression::Trunc(
*loc,
res_ty,
Box::new(Expression::ShiftRight(
*loc,
from_ty.clone(),
from_ty,
Box::new(array),
// shift by (array_length - 1 - index) * 8
Box::new(Expression::ShiftLeft(
*loc,
from_ty.clone(),
index_ty.clone(),
Box::new(Expression::Subtract(
*loc,
from_ty.clone(),
index_ty.clone(),
Box::new(Expression::NumberLiteral(
*loc,
from_ty.clone(),
index_ty.clone(),
BigInt::from_u8(array_length - 1).unwrap(),
)),
Box::new(cast_shift_arg(
Expand All @@ -1814,7 +1821,7 @@ fn array_subscript(
)),
Box::new(Expression::NumberLiteral(
*loc,
from_ty,
index_ty,
BigInt::from_u8(3).unwrap(),
)),
)),
Expand Down
3 changes: 3 additions & 0 deletions src/sema/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ pub struct EventDecl {
pub fields: Vec<Parameter>,
pub signature: String,
pub anonymous: bool,
pub used: bool,
}

impl fmt::Display for EventDecl {
Expand Down Expand Up @@ -284,6 +285,8 @@ pub struct Variable {
pub visibility: pt::Visibility,
pub constant: bool,
pub initializer: Option<Expression>,
pub assigned: bool,
pub read: bool,
}

#[derive(Clone, PartialEq)]
Expand Down
4 changes: 2 additions & 2 deletions src/sema/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ pub fn resolve_call(
args: &[pt::Expression],
contract_no: Option<usize>,
ns: &mut Namespace,
symtable: &Symtable,
symtable: &mut Symtable,
is_constant: bool,
diagnostics: &mut Vec<Diagnostic>,
) -> Result<Expression, ()> {
Expand Down Expand Up @@ -590,7 +590,7 @@ pub fn resolve_method_call(
args: &[pt::Expression],
contract_no: Option<usize>,
ns: &mut Namespace,
symtable: &Symtable,
symtable: &mut Symtable,
diagnostics: &mut Vec<Diagnostic>,
) -> Result<Expression, ()> {
// The abi.* functions need special handling, others do not
Expand Down
11 changes: 9 additions & 2 deletions src/sema/contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use super::statements;
use super::symtable::Symtable;
use super::variables;
use super::{ast, SOLANA_FIRST_OFFSET};
use crate::sema::unused_variable::emit_warning_local_variable;
use crate::{emit, Target};

impl ast::Contract {
Expand Down Expand Up @@ -249,7 +250,7 @@ fn resolve_base_args(
.position(|e| e.contract_no == base_no)
{
if let Some(args) = &base.args {
let symtable = Symtable::new();
let mut symtable = Symtable::new();

// find constructor which matches this
if let Ok((Some(constructor_no), args)) = match_constructor_to_args(
Expand All @@ -259,7 +260,7 @@ fn resolve_base_args(
base_no,
*contract_no,
ns,
&symtable,
&mut symtable,
&mut diagnostics,
) {
ns.contracts[*contract_no].bases[pos].constructor =
Expand Down Expand Up @@ -868,6 +869,12 @@ fn resolve_bodies(
.is_err()
{
broken = true;
} else {
for variable in ns.functions[function_no].symtable.vars.values() {
if let Some(warning) = emit_warning_local_variable(&variable) {
ns.diagnostics.push(warning);
}
}
}
}

Expand Down
Loading