Skip to content

Commit f3322eb

Browse files
authored
Chore/ cleanup 8 (#905)
1 parent 27b46d4 commit f3322eb

File tree

7 files changed

+86
-85
lines changed

7 files changed

+86
-85
lines changed

aderyn/src/lsp.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,10 @@ pub fn spin_up_language_server(args: Args) {
105105

106106
// Watch for file changes
107107
file_system_watcher
108-
.watch(PathBuf::from(args.root.clone()).as_path(), RecursiveMode::Recursive)
108+
.watch(
109+
PathBuf::from(args.input_config.root.clone()).as_path(),
110+
RecursiveMode::Recursive,
111+
)
109112
.expect("unable to watch for file changes");
110113

111114
// Most editor's LSP clients communicate through stdout/stdin channels. Theefore use

aderyn/src/main.rs

+20-13
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use aderyn::{
44
aderyn_is_currently_running_newest_version, create_aderyn_toml_file_at, initialize_niceties,
55
lsp::spin_up_language_server, print_all_detectors_view, print_detail_view,
66
};
7-
use aderyn_driver::driver::{self, Args};
7+
use aderyn_driver::driver::{self, Args, CliArgsOutputConfig};
88

99
use clap::{ArgGroup, Parser, Subcommand};
1010
#[derive(Parser, Debug)]
@@ -137,23 +137,30 @@ fn main() {
137137
return;
138138
}
139139

140-
let mut args: Args = Args {
141-
root: cmd_args.root,
142-
output: cmd_args.output,
143-
src: cmd_args.src,
144-
path_includes: cmd_args.path_includes,
145-
path_excludes: cmd_args.path_excludes,
146-
no_snippets: cmd_args.no_snippets,
147-
skip_cloc: cmd_args.skip_cloc,
148-
stdout: cmd_args.stdout,
140+
let mut args = Args {
149141
auditor_mode: cmd_args.auditor_mode,
150-
highs_only: cmd_args.highs_only,
151-
lsp: cmd_args.lsp,
142+
input_config: driver::CliArgsInputConfig {
143+
root: cmd_args.root,
144+
src: cmd_args.src,
145+
path_excludes: cmd_args.path_excludes,
146+
path_includes: cmd_args.path_includes,
147+
},
148+
output_config: CliArgsOutputConfig {
149+
output: cmd_args.output,
150+
stdout: cmd_args.stdout,
151+
no_snippets: cmd_args.no_snippets,
152+
},
153+
common_config: driver::CliArgsCommonConfig {
154+
lsp: cmd_args.lsp,
155+
skip_cloc: cmd_args.skip_cloc,
156+
highs_only: cmd_args.highs_only,
157+
},
152158
};
153159

154160
// Run watcher is watch mode is engaged
155161
if cmd_args.lsp {
156-
args.skip_cloc = true;
162+
// FORCE skip cloc
163+
args.common_config.skip_cloc = true;
157164
spin_up_language_server(args);
158165
} else {
159166
driver::kick_off_report_creation(args.clone());

aderyn_driver/benches/detectors.rs

+2-29
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
use std::path::PathBuf;
22

3-
use aderyn_driver::{
4-
detector::get_all_issue_detectors,
5-
driver::{kick_off_report_creation, Args},
6-
process::PreprocessedConfig,
7-
};
3+
use aderyn_driver::{detector::get_all_issue_detectors, process::PreprocessedConfig};
84

95
use criterion::{criterion_group, criterion_main, Criterion};
106
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
@@ -27,31 +23,8 @@ fn bench_individual_detectors_on_contract_playground(c: &mut Criterion) {
2723
}
2824
}
2925

30-
fn bench_aderyn_on_contract_playground(c: &mut Criterion) {
31-
let root_path = PathBuf::from("../tests/contract-playground");
32-
33-
c.bench_function("aderyn", |b| {
34-
b.iter(|| {
35-
kick_off_report_creation(Args {
36-
root: root_path.to_string_lossy().to_string(),
37-
output: String::from("aderyn-report-for-bench.md"),
38-
no_snippets: false,
39-
skip_cloc: true,
40-
path_excludes: None,
41-
path_includes: None,
42-
src: None,
43-
stdout: false,
44-
auditor_mode: false,
45-
highs_only: false,
46-
lsp: false,
47-
});
48-
});
49-
});
50-
}
51-
5226
criterion_group!(
5327
detectors, // Group name is the first argument
54-
bench_aderyn_on_contract_playground, // Group 1
55-
bench_individual_detectors_on_contract_playground, // Group 2
28+
bench_individual_detectors_on_contract_playground, // Group 1
5629
);
5730
criterion_main!(detectors);

aderyn_driver/src/driver.rs

+27-14
Original file line numberDiff line numberDiff line change
@@ -10,38 +10,52 @@ use tokio::sync::Mutex;
1010

1111
#[derive(Clone, FieldAccess)]
1212
pub struct Args {
13+
pub auditor_mode: bool,
14+
pub input_config: CliArgsInputConfig,
15+
pub output_config: CliArgsOutputConfig,
16+
pub common_config: CliArgsCommonConfig,
17+
}
18+
19+
#[derive(Debug, Clone)]
20+
pub struct CliArgsInputConfig {
1321
pub root: String,
14-
pub output: String,
1522
pub src: Option<String>,
1623
pub path_excludes: Option<Vec<String>>,
1724
pub path_includes: Option<Vec<String>>,
25+
}
26+
27+
#[derive(Debug, Clone)]
28+
pub struct CliArgsOutputConfig {
29+
pub output: String,
30+
pub stdout: bool,
1831
pub no_snippets: bool,
32+
}
33+
34+
#[derive(Debug, Clone)]
35+
pub struct CliArgsCommonConfig {
36+
pub lsp: bool,
1937
pub skip_cloc: bool,
20-
pub stdout: bool,
21-
pub auditor_mode: bool,
2238
pub highs_only: bool,
23-
pub lsp: bool,
2439
}
2540

2641
/// One way pipeline. Used by CLI
2742
pub fn kick_off_report_creation(args: Args) {
28-
// Choose the detectors
2943
let detectors = detector_list(&args);
3044

3145
let run_pipeline = || -> Result<(), Box<dyn std::error::Error>> {
32-
let cx_wrapper = make_context(&args).unwrap_or_else(|e| {
33-
eprintln!("Error making context: {}", e);
34-
std::process::exit(1);
35-
});
46+
let cx_wrapper =
47+
make_context(&args.input_config, &args.common_config).unwrap_or_else(|e| {
48+
eprintln!("Error making context: {}", e);
49+
std::process::exit(1);
50+
});
3651

3752
if args.auditor_mode {
3853
run_auditor_mode(&cx_wrapper.contexts)?;
3954
} else {
4055
let root_rel_path = cx_wrapper.root_path;
41-
let output = args.output.clone();
4256

4357
// Load the workspace context into the run function, which runs the detectors
44-
run_detector_mode(&cx_wrapper.contexts, output, root_rel_path, detectors, &args)?;
58+
run_detector_mode(&cx_wrapper.contexts, root_rel_path, detectors, &args.output_config)?;
4559
}
4660
Ok(())
4761
};
@@ -55,10 +69,9 @@ pub fn kick_off_report_creation(args: Args) {
5569

5670
/// Drives and returns results. Used by LSP
5771
pub fn fetch_report_for_lsp(args: Args) -> Arc<Mutex<Option<LspReport>>> {
58-
// Choose the detectors
5972
let detectors = detector_list(&args);
6073

61-
let ctx_wrapper = match make_context(&args) {
74+
let ctx_wrapper = match make_context(&args.input_config, &args.common_config) {
6275
Ok(ctx_wrapper) => ctx_wrapper,
6376
Err(_) => {
6477
return Arc::new(tokio::sync::Mutex::new(None));
@@ -74,6 +87,6 @@ pub fn fetch_report_for_lsp(args: Args) -> Arc<Mutex<Option<LspReport>>> {
7487
fn detector_list(args: &Args) -> Vec<Box<dyn IssueDetector>> {
7588
get_all_issue_detectors()
7689
.into_iter()
77-
.filter(|d| !args.highs_only || d.severity() == IssueSeverity::High)
90+
.filter(|d| !args.common_config.highs_only || d.severity() == IssueSeverity::High)
7891
.collect()
7992
}

aderyn_driver/src/interface/mod.rs

+13-12
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use std::{
1212

1313
use aderyn_core::{context::workspace_context::WorkspaceContext, report::Report};
1414

15-
use crate::driver::Args;
15+
use crate::driver::CliArgsOutputConfig;
1616

1717
#[derive(Default)]
1818
pub enum OutputInterface {
@@ -27,10 +27,8 @@ pub fn output_interface_router(
2727
report: &Report,
2828
contexts: &[WorkspaceContext],
2929
root_rel_path: PathBuf,
30-
output_file_path: String, /* you writer 'W' may or may not be writing a file. Eg:
31-
* it can simply consume and forget :P */
3230
detectors_used: &[(String, String)],
33-
args: &Args,
31+
output_config: &CliArgsOutputConfig,
3432
) -> Result<()> {
3533
let get_writer = |filename: &str| -> io::Result<File> {
3634
let file_path = Path::new(filename);
@@ -45,30 +43,33 @@ pub fn output_interface_router(
4543

4644
println!("Detectors run, printing report.");
4745

48-
let mut b: Box<dyn Write> =
49-
if args.stdout { Box::new(io::stdout()) } else { Box::new(get_writer(&output_file_path)?) };
46+
let mut b: Box<dyn Write> = if output_config.stdout {
47+
Box::new(io::stdout())
48+
} else {
49+
Box::new(get_writer(&output_config.output)?)
50+
};
5051

5152
match output_interface {
5253
OutputInterface::Json => {
53-
json::print_report(&mut b, report, contexts, args.stdout, detectors_used)?;
54+
json::print_report(&mut b, report, contexts, output_config.stdout, detectors_used)?;
5455
}
5556
OutputInterface::Markdown => {
5657
markdown::print_report(
5758
&mut b,
5859
report,
5960
contexts,
6061
root_rel_path,
61-
output_file_path.clone(),
62-
args.no_snippets,
62+
output_config.output.clone(),
63+
output_config.no_snippets,
6364
)?;
6465
}
6566
OutputInterface::Sarif => {
66-
sarif::print_report(&mut b, report, args.stdout)?;
67+
sarif::print_report(&mut b, report, output_config.stdout)?;
6768
}
6869
}
6970

70-
if !args.stdout {
71-
println!("Report printed to {}", output_file_path);
71+
if !output_config.stdout {
72+
println!("Report printed to {}", output_config.output);
7273
}
7374

7475
Ok(())

aderyn_driver/src/process.rs

+13-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
use crate::{compile, config::supplement_values_from_aderyn_toml, driver::Args};
1+
use crate::{
2+
compile,
3+
config::supplement_values_from_aderyn_toml,
4+
driver::{CliArgsCommonConfig, CliArgsInputConfig},
5+
};
26
use aderyn_core::{
37
context::{
48
graph::{Transpose, WorkspaceCallGraph},
@@ -24,21 +28,22 @@ pub struct PreprocessedConfig {
2428
}
2529

2630
pub fn make_context(
27-
args: &Args,
31+
args: &CliArgsInputConfig,
32+
common: &CliArgsCommonConfig,
2833
) -> Result<WorkspaceContextWrapper, Box<dyn std::error::Error + Send + Sync>> {
2934
// Preprocess config by supplementing CLI args with aderyn.toml if exists
3035
let preprocessed_config = obtain_config_values(args.clone())?;
3136

32-
let is_lsp_mode = args.lsp;
3337
let root_path = preprocessed_config.root_path.clone();
3438

3539
// Compilation steps:
36-
// 1. Processes the config by translating them to runtime values (Cyfrin/solidity-ast-rs)
40+
// 1. Processes the above preprocessed config by translating them to runtime values Thanks to
41+
// Cyfrin/solidity-ast-rs
3742
// 2. Parse those files and prepare ASTs.
38-
let mut contexts: Vec<WorkspaceContext> = compile::project(preprocessed_config, is_lsp_mode)?;
43+
let mut contexts: Vec<WorkspaceContext> = compile::project(preprocessed_config, common.lsp)?;
3944

4045
// Only make this an error when it's not in LSP mode
41-
if !is_lsp_mode && contexts.iter().all(|c| c.src_filepaths.is_empty()) {
46+
if !common.lsp && contexts.iter().all(|c| c.src_filepaths.is_empty()) {
4247
let error = "No solidity files found in given scope!";
4348
eprintln!("{}", error);
4449
return Err(error.into());
@@ -53,7 +58,7 @@ pub fn make_context(
5358
let stats = fscloc::engine::count_lines_of_code_and_collect_line_numbers_to_ignore(
5459
absolute_root_path.as_path(),
5560
&context.src_filepaths,
56-
args.skip_cloc,
61+
common.skip_cloc,
5762
);
5863
let sloc_stats: HashMap<String, usize> = stats
5964
.lock()
@@ -85,7 +90,7 @@ pub fn make_context(
8590

8691
/// Supplement the CLI arguments with values from aderyn.toml
8792
fn obtain_config_values(
88-
args: Args,
93+
args: CliArgsInputConfig,
8994
) -> Result<PreprocessedConfig, Box<dyn std::error::Error + Send + Sync>> {
9095
let root_path = PathBuf::from(&args.root);
9196
let aderyn_path = root_path.join("aderyn.toml");

aderyn_driver/src/runner.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,30 @@ use aderyn_core::{context::workspace_context::WorkspaceContext, detect::detector
22
use std::{error::Error, path::PathBuf};
33

44
use crate::{
5-
driver::Args,
5+
driver::CliArgsOutputConfig,
66
interface::{lsp::LspReport, output_interface_router, OutputInterface},
77
};
88
use aderyn_core::report::*;
99

1010
pub fn run_detector_mode(
1111
contexts: &[WorkspaceContext],
12-
output_file_path: String,
1312
root_rel_path: PathBuf,
1413
detectors: Vec<Box<dyn IssueDetector>>,
15-
args: &Args,
14+
output_config: &CliArgsOutputConfig,
1615
) -> Result<(), Box<dyn Error>> {
1716
println!("Running {} detectors", detectors.len());
1817

1918
let detectors_used =
2019
&detectors.iter().map(|d| (d.name(), d.severity().to_string())).collect::<Vec<_>>();
2120

2221
let report = get_report(contexts, &root_rel_path, detectors)?;
22+
let output_file_path = output_config.output.clone();
2323

24-
let output_interface = if args.output.ends_with(".json") {
24+
let output_interface = if output_file_path.ends_with(".json") {
2525
OutputInterface::Json
26-
} else if args.output.ends_with(".sarif") {
26+
} else if output_file_path.ends_with(".sarif") {
2727
OutputInterface::Sarif
28-
} else if args.output.ends_with(".md") {
28+
} else if output_file_path.ends_with(".md") {
2929
OutputInterface::Markdown
3030
} else {
3131
println!("Warning: Output file extension is unrecognized. Reverting to markdown..");
@@ -37,9 +37,8 @@ pub fn run_detector_mode(
3737
&report,
3838
contexts,
3939
root_rel_path,
40-
output_file_path.clone(),
4140
detectors_used,
42-
args,
41+
output_config,
4342
)?;
4443

4544
Ok(())

0 commit comments

Comments
 (0)