Skip to content

Commit 6aee16d

Browse files
committed
integrate macro scanning with vscode extension
1 parent 040a1c6 commit 6aee16d

File tree

9 files changed

+74
-21
lines changed

9 files changed

+74
-21
lines changed

lang_server/src/request.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ impl ScanCommandResponse {
133133

134134
/// Scan crate in root path and get crate stats
135135
fn get_simple_scan_results(path: &Path) -> CrateStats {
136-
let res = get_crate_stats_default(path.to_path_buf(), false, false);
136+
let res = get_crate_stats_default(path.to_path_buf(), false, true);
137137
info!("Finished scanning. Found {} effects.", res.effects.len());
138138

139139
res

lang_server/src/server.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,8 @@ fn runner(
112112
let root_crate_path = std::path::PathBuf::from_str(root_uri.path())?;
113113
info!("Crate path received in cargo-scan LSP server: {}", root_crate_path.display());
114114

115-
let scan_res = scanner::scan_crate(
116-
&root_crate_path,
117-
effect::DEFAULT_EFFECT_TYPES,
118-
false,
119-
false,
120-
)?;
115+
let scan_res =
116+
scanner::scan_crate(&root_crate_path, effect::DEFAULT_EFFECT_TYPES, false, true)?;
121117

122118
info!("Starting main server loop\n");
123119
let mut audit_file: Option<AuditFile> = None;

src/audit_chain.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,7 @@ fn make_new_audit_file(
455455
audit_type: DefaultAuditType,
456456
relevant_effects: &[EffectType],
457457
quick_mode: bool,
458+
expand_macro: bool,
458459
) -> Result<()> {
459460
let audit_file_path = PathBuf::from(format!(
460461
"{}/{}-{}.audit",
@@ -503,6 +504,7 @@ fn make_new_audit_file(
503504
audit_type,
504505
relevant_effects,
505506
quick_mode,
507+
expand_macro,
506508
)?;
507509
audit_file.save_to_file(audit_file_path.clone())?;
508510

@@ -515,6 +517,7 @@ pub fn create_new_audit_chain(
515517
args: Create,
516518
crate_download_path: &str,
517519
quick_mode: bool,
520+
expand_macro: bool,
518521
) -> Result<AuditChain> {
519522
info!("Creating audit chain");
520523
let mut chain = AuditChain::new(
@@ -572,6 +575,7 @@ pub fn create_new_audit_chain(
572575
audit_type,
573576
&args.effect_types,
574577
quick_mode,
578+
expand_macro,
575579
)?;
576580
}
577581

src/audit_file.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -538,12 +538,14 @@ impl AuditFile {
538538
crate_path: &FilePath,
539539
relevant_effects: &[EffectType],
540540
quick_mode: bool,
541+
expand_macro: bool,
541542
) -> Result<AuditFile> {
542543
Self::new_caller_checked_default_with_sinks(
543544
crate_path,
544545
HashSet::new(),
545546
relevant_effects,
546547
quick_mode,
548+
expand_macro,
547549
)
548550
}
549551

@@ -567,13 +569,14 @@ impl AuditFile {
567569
sinks: HashSet<CanonicalPath>,
568570
relevant_effects: &[EffectType],
569571
quick: bool,
572+
expand_macro: bool,
570573
) -> Result<AuditFile> {
571574
Self::new_caller_checked_default_with_sinks_and_results(
572575
crate_path,
573576
sinks,
574577
relevant_effects,
575578
quick,
576-
false,
579+
expand_macro,
577580
)
578581
.map(|x| x.0)
579582
}
@@ -621,6 +624,7 @@ impl AuditFile {
621624
sinks: HashSet<CanonicalPath>,
622625
relevant_effects: &[EffectType],
623626
quick: bool,
627+
expand_macro: bool,
624628
) -> Result<AuditFile> {
625629
let mut audit_file =
626630
AuditFile::empty(crate_path.to_path_buf(), relevant_effects.to_vec())?;
@@ -631,7 +635,7 @@ impl AuditFile {
631635
ident_sinks,
632636
relevant_effects,
633637
quick,
634-
false,
638+
expand_macro,
635639
)?;
636640
audit_file.set_base_audit_trees(scan_res.effects_set());
637641

@@ -643,9 +647,15 @@ impl AuditFile {
643647
sinks: HashSet<CanonicalPath>,
644648
relevant_effects: &[EffectType],
645649
quick: bool,
650+
expand_macro: bool,
646651
) -> Result<AuditFile> {
647-
let (mut audit_file, _scan_res) =
648-
Self::scan_with_sinks(crate_path, sinks, relevant_effects, quick, false)?;
652+
let (mut audit_file, _scan_res) = Self::scan_with_sinks(
653+
crate_path,
654+
sinks,
655+
relevant_effects,
656+
quick,
657+
expand_macro,
658+
)?;
649659
for (_, mut t) in audit_file.audit_trees.iter_mut() {
650660
if let EffectTree::Leaf(_, a) = &mut t {
651661
*a = SafetyAnnotation::Safe;
@@ -661,6 +671,7 @@ impl AuditFile {
661671
audit_type: DefaultAuditType,
662672
relevant_effects: &[EffectType],
663673
quick: bool,
674+
expand_macro: bool,
664675
) -> Result<AuditFile> {
665676
match audit_type {
666677
DefaultAuditType::CallerChecked => {
@@ -669,19 +680,22 @@ impl AuditFile {
669680
sinks,
670681
relevant_effects,
671682
quick,
683+
expand_macro,
672684
)
673685
}
674686
DefaultAuditType::Empty => Self::new_empty_default_with_sinks(
675687
crate_path,
676688
sinks,
677689
relevant_effects,
678690
quick,
691+
expand_macro,
679692
),
680693
DefaultAuditType::Safe => Self::new_safe_default_with_sinks(
681694
crate_path,
682695
sinks,
683696
relevant_effects,
684697
quick,
698+
expand_macro,
685699
),
686700
}
687701
}

src/auditing/audit.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,7 @@ pub fn audit_pub_fn(
468468
sink_ident: &Sink,
469469
config: &Config,
470470
quick_mode: bool,
471+
expand_macro: bool,
471472
) -> Result<HashSet<CanonicalPath>> {
472473
let sink_crate = sink_ident
473474
.first_ident()
@@ -484,7 +485,7 @@ pub fn audit_pub_fn(
484485
&new_audit_file.base_dir,
485486
&prev_audit_file.scanned_effects,
486487
quick_mode,
487-
false,
488+
expand_macro,
488489
)?;
489490
let sink_fn = CanonicalPath::new(sink_ident.as_str());
490491
loop {
@@ -515,7 +516,7 @@ pub fn audit_pub_fn(
515516
))
516517
}
517518
};
518-
audit_pub_fn(chain, child_sink, config, quick_mode)?;
519+
audit_pub_fn(chain, child_sink, config, quick_mode, expand_macro)?;
519520
// We have to reload the new audit file because auditing child
520521
// effects may have removed some base effects from the current
521522
// crate

src/auditing/chain.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,18 @@ pub struct OuterArgs {
2121

2222
#[clap(long, default_value_t = false)]
2323
pub quick_mode: bool,
24+
25+
#[clap(long, default_value_t = true)]
26+
pub expand_macro: bool,
2427
}
2528

2629
impl Default for OuterArgs {
2730
fn default() -> Self {
28-
Self { crate_download_path: ".audit_crates".to_string(), quick_mode: false }
31+
Self {
32+
crate_download_path: ".audit_crates".to_string(),
33+
quick_mode: false,
34+
expand_macro: true,
35+
}
2936
}
3037
}
3138

@@ -89,7 +96,12 @@ impl CommandRunner for Create {
8996
std::fs::rename(&tmp_path, &self.crate_path)?;
9097
}
9198

92-
let chain = create_new_audit_chain(self, &args.crate_download_path, false)?;
99+
let chain = create_new_audit_chain(
100+
self,
101+
&args.crate_download_path,
102+
args.quick_mode,
103+
args.expand_macro,
104+
)?;
93105
chain.save_to_file()?;
94106
Ok(())
95107
}
@@ -205,8 +217,8 @@ impl CommandRunner for Audit {
205217
let scan_res = scanner::scan_crate(
206218
&crate_path,
207219
&orig_audit_file.scanned_effects,
208-
false,
209-
false,
220+
args.quick_mode,
221+
args.expand_macro,
210222
)?;
211223

212224
let mut audit_config = AuditConfig::default();
@@ -229,6 +241,7 @@ impl CommandRunner for Audit {
229241
sink_ident,
230242
&audit_config,
231243
args.quick_mode,
244+
args.expand_macro,
232245
)?,
233246
_ => {
234247
return Err(anyhow!(

src/bin/audit.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ struct Args {
102102
/// TESTING ONLY: Use the quick-mode scan option
103103
#[clap(long, default_value_t = false)]
104104
quick_mode: bool,
105+
106+
/// Whether to analyze macro expanded code for effects
107+
#[clap(long, default_value_t = true)]
108+
expand_macros: bool,
105109
}
106110

107111
#[derive(ValueEnum, Clone, Copy, Debug, PartialEq, Eq)]
@@ -250,7 +254,7 @@ fn audit_crate(args: Args, audit_file: Option<AuditFile>) -> Result<()> {
250254
sinks,
251255
relevant_effects,
252256
args.quick_mode,
253-
false,
257+
args.expand_macros,
254258
)?
255259
};
256260
let scan_effects = scan_res.effects_set();
@@ -334,8 +338,12 @@ fn runner(args: Args) -> Result<()> {
334338
println!("Previewing crate effects.");
335339
println!("Scanning crate...");
336340

337-
let res =
338-
scan_crate(&args.crate_path, &args.effect_types, args.quick_mode, false)?;
341+
let res = scan_crate(
342+
&args.crate_path,
343+
&args.effect_types,
344+
args.quick_mode,
345+
args.expand_macros,
346+
)?;
339347
for effect in res.effects {
340348
println!("{}", effect.to_csv());
341349
}

src/bin/default_audit.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ struct Args {
3434
/// Run in quick mode (turns off RustAnalyzer)
3535
#[clap(long, default_value_t = false)]
3636
quick_mode: bool,
37+
38+
/// Whether to analyze macro expansions for effects
39+
#[clap(long, default_value_t = true)]
40+
expand_macro: bool,
3741
}
3842

3943
// TODO: Combine this with DefaultAuditType once we implement every version
@@ -56,12 +60,14 @@ fn runner(args: Args) -> Result<()> {
5660
&args.crate_path,
5761
&EffectType::unsafe_effects(),
5862
args.quick_mode,
63+
args.expand_macro,
5964
)?,
6065
AuditType::Safe => AuditFile::new_safe_default_with_sinks(
6166
&args.crate_path,
6267
HashSet::new(),
6368
&EffectType::unsafe_effects(),
6469
args.quick_mode,
70+
args.expand_macro,
6571
)?,
6672
};
6773

src/bin/stat.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ struct Args {
5555
EffectType::ClosureCreation,
5656
])]
5757
effect_types: Vec<EffectType>,
58+
59+
#[clap(long, default_value_t = false)]
60+
pub quick_mode: bool,
61+
62+
#[clap(long, default_value_t = true)]
63+
pub expand_macro: bool,
5864
}
5965

6066
#[derive(ValueEnum, Clone, Copy, Debug)]
@@ -128,7 +134,12 @@ fn main() -> Result<()> {
128134
args.effect_types,
129135
);
130136

131-
let mut chain = create_new_audit_chain(create, &args.audit_file_path, false)?;
137+
let mut chain = create_new_audit_chain(
138+
create,
139+
&args.audit_file_path,
140+
args.quick_mode,
141+
args.expand_macro,
142+
)?;
132143
let root_crate = chain.root_crate()?;
133144
let root_audit_file = chain
134145
.read_audit_file(&root_crate)?

0 commit comments

Comments
 (0)