Skip to content

Commit 1e54b34

Browse files
authored
feat/ absorb full context (#907)
1 parent 4170c50 commit 1e54b34

File tree

16 files changed

+1645
-134
lines changed

16 files changed

+1645
-134
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ petgraph = "0"
4444

4545
# Uncomment in Production (just before merging to dev)
4646
# Cyfrin managed github repository
47-
solidity_ast = { git = "https://github.com/Cyfrin/solidity-ast-rs", tag = "v0.0.1-alpha.beta.3", package = "solidity-ast" }
47+
solidity_ast = { git = "https://github.com/Cyfrin/solidity-ast-rs", tag = "v0.0.1-alpha.beta.4", package = "solidity-ast" }
4848

4949
# Uncomment when debugging in branch (PR)
5050
# solidity_ast = { git = "https://github.com/Cyfrin/solidity-ast-rs", branch = "main", package = "solidity-ast" }

aderyn_core/src/context/workspace_context.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
use solidity_ast::EvmVersion;
22

33
use crate::{ast::*, fscloc::cloc::IgnoreLine};
4-
use std::{cmp::Ordering, collections::HashMap};
4+
use std::{
5+
cmp::Ordering,
6+
collections::{HashMap, HashSet},
7+
path::PathBuf,
8+
};
59

610
use super::{browser::GetImmediateParent, capturable::Capturable, graph::WorkspaceCallGraph};
711
pub use crate::ast::ASTNode;
@@ -35,6 +39,9 @@ pub struct WorkspaceContext {
3539
// Source units
3640
pub source_units_context: Vec<SourceUnit>,
3741

42+
// In-scope files
43+
pub included: HashSet<PathBuf>,
44+
3845
// Hashmaps of all nodes => source_unit_id
3946
pub(crate) array_type_names_context: HashMap<ArrayTypeName, NodeContext>,
4047
pub(crate) assignments_context: HashMap<Assignment, NodeContext>,

aderyn_core/src/detect/report.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ use std::{
22
collections::{btree_map::Entry, BTreeMap, HashMap},
33
error::Error,
44
ops::Add,
5-
path::Path,
5+
path::{Path, PathBuf},
6+
str::FromStr,
67
};
78

89
use crate::{
@@ -139,10 +140,20 @@ pub fn get_report(
139140
if found {
140141
let instances = d.instances();
141142
let hints = d.hints();
142-
return (instances, hints, context.src_filepaths.clone());
143+
return (
144+
instances,
145+
hints,
146+
context.src_filepaths.clone(),
147+
context.included.clone(),
148+
);
143149
}
144150
}
145-
(Default::default(), Default::default(), context.src_filepaths.clone())
151+
(
152+
Default::default(),
153+
Default::default(),
154+
context.src_filepaths.clone(),
155+
context.included.clone(),
156+
)
146157
})
147158
.collect::<Vec<_>>();
148159

@@ -175,7 +186,7 @@ pub fn get_report(
175186
Vec<BTreeMap<(String, usize, String), i64>>,
176187
> = Default::default();
177188

178-
for (instances, hints, src_filepaths) in collection_of_instances {
189+
for (instances, hints, src_filepaths, included) in collection_of_instances {
179190
let mut grouped_instances_context: BTreeMap<
180191
String,
181192
BTreeMap<(String, usize, String), i64>,
@@ -201,6 +212,9 @@ pub fn get_report(
201212
}
202213

203214
for (key, value) in grouped_instances_context {
215+
if !included.contains(&PathBuf::from_str(&key).unwrap()) {
216+
continue;
217+
}
204218
match grouped_instances.entry(key.clone()) {
205219
Entry::Vacant(v) => {
206220
v.insert(vec![value]);

aderyn_core/src/fscloc/engine.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
use crate::fscloc::cloc;
22
use ignore::{DirEntry, WalkBuilder, WalkState::Continue};
33
use rayon::prelude::*;
4-
use std::{collections::HashMap, path::Path, sync::Mutex};
4+
use std::{
5+
collections::{HashMap, HashSet},
6+
path::{Path, PathBuf},
7+
sync::Mutex,
8+
};
59

610
use super::cloc::Stats;
711

812
pub fn count_lines_of_code_and_collect_line_numbers_to_ignore(
913
src: &Path,
1014
src_filepaths: &[String],
1115
skip_cloc: bool,
16+
included: &HashSet<PathBuf>,
1217
) -> Mutex<HashMap<String, Stats>> {
1318
let (tx, rx) = crossbeam_channel::unbounded();
1419

@@ -29,7 +34,11 @@ pub fn count_lines_of_code_and_collect_line_numbers_to_ignore(
2934
}
3035
};
3136

32-
let src_filepaths_as_paths = src_filepaths.iter().map(form_path).collect::<Vec<_>>();
37+
let src_filepaths_as_paths = src_filepaths
38+
.iter()
39+
.map(form_path)
40+
.filter(|s| included.contains(s.strip_prefix(src).unwrap()))
41+
.collect::<Vec<_>>();
3342

3443
if !src_filepaths_as_paths.is_empty() {
3544
// Only add the paths to WalkBuilder that we want to do analysis on.

aderyn_core/src/fscloc/mod.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub struct CodeLines {
1313

1414
#[cfg(test)]
1515
mod cloc_tests {
16-
use std::path::PathBuf;
16+
use std::{collections::HashSet, path::PathBuf, str::FromStr};
1717

1818
use crate::fscloc::engine;
1919

@@ -23,10 +23,15 @@ mod cloc_tests {
2323
"HeavilyCommentedContract.sol".to_string(),
2424
"AnotherHeavilyCommentedContract.sol".to_string(),
2525
];
26+
let included = HashSet::from_iter(vec![
27+
PathBuf::from_str("HeavilyCommentedContract.sol").unwrap(),
28+
PathBuf::from_str("AnotherHeavilyCommentedContract.sol").unwrap(),
29+
]);
2630
let sol = engine::count_lines_of_code_and_collect_line_numbers_to_ignore(
2731
PathBuf::from("../tests/contract-playground/src/cloc").as_path(),
2832
&src_filepaths,
2933
false,
34+
&included,
3035
);
3136
let result = sol.lock().unwrap();
3237
result.iter().for_each(|element| println!("{} - {}", element.0, element.1.code));
@@ -49,10 +54,13 @@ mod cloc_tests {
4954
#[test]
5055
fn test_print_loc_specific_file() {
5156
let src_filepaths = vec!["HeavilyCommentedContract.sol".to_string()];
57+
let included =
58+
HashSet::from_iter(vec![PathBuf::from_str("HeavilyCommentedContract.sol").unwrap()]);
5259
let sol = engine::count_lines_of_code_and_collect_line_numbers_to_ignore(
5360
PathBuf::from("../tests/contract-playground/src/cloc").as_path(),
5461
&src_filepaths,
5562
false,
63+
&included,
5664
);
5765
let result = sol.lock().unwrap();
5866
assert_eq!(result.len(), 1);

aderyn_driver/src/compile.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,13 @@ pub fn project(
6565
display_ingesting_message(&sources_ast, &included, &ast_info.version.to_string());
6666
}
6767
for (source_path, ast_source_file) in sources_ast {
68-
if included.contains(&source_path) {
69-
let content = sources.get(&source_path).cloned().expect("content not found");
70-
absorb_ast_content_into_context(ast_source_file, &mut context, content);
71-
context.src_filepaths.push(source_path.display().to_string());
72-
}
68+
let content = sources.get(&source_path).expect("content not found");
69+
absorb_ast_content_into_context(ast_source_file, &mut context, content.clone());
70+
context.src_filepaths.push(source_path.display().to_string());
7371
}
7472

7573
context.evm_version = derived_ast_evm_info.evm_version;
74+
context.included = included;
7675

7776
Some(context)
7877
})

aderyn_driver/src/process.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ pub fn make_context(
5959
absolute_root_path.as_path(),
6060
&context.src_filepaths,
6161
common.skip_cloc,
62+
&context.included,
6263
);
6364
let sloc_stats: HashMap<String, usize> = stats
6465
.lock()

reports/ccip-functions-report.md

Lines changed: 110 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

reports/nft-report.md

Lines changed: 1 addition & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)