Skip to content

Commit 0352cd3

Browse files
committed
🧹 simplify LoCTracker interface
1 parent f35bd36 commit 0352cd3

File tree

3 files changed

+25
-35
lines changed

3 files changed

+25
-35
lines changed

src/bin/audit_stats.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ fn compute_stats(
132132
// Collect total lines of code for audited functions
133133
for f in &total_fns {
134134
match results.fn_loc_tracker.get(f) {
135-
Some(tracker) => audited_loc += tracker.get_loc_lb(),
135+
Some(tracker) => audited_loc += tracker.get_loc(),
136136
None => debug!(
137137
"failed to find tracker node for `{:?}`. possibly it is a trait method.",
138138
f.as_str()
@@ -147,8 +147,7 @@ fn compute_stats(
147147
});
148148
sink_calls.extend(sink_effects.cloned());
149149

150-
let total_loc =
151-
results.fn_loc_tracker.values().fold(0, |acc, x| acc + x.get_loc_lb());
150+
let total_loc = results.fn_loc_tracker.values().fold(0, |acc, x| acc + x.get_loc());
152151

153152
AuditingStats {
154153
crate_id,

src/loc_tracker.rs

+11-19
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
//! Abstract tracker for calculating total lines of code (LoC)
2-
//! in a sequence of code excerpts.
2+
//! in a sequence of code blocks.
33
//!
4-
//! Supports a simple interface using add(s) for s: Spanned
5-
//! to add a code excerpt.
4+
//! Code blocks are added through the interface add(s) for s: Spanned.
65
//!
76
//! Important note:
8-
//! - Assumes that code excerpts do not overlap.
9-
//! - If multiple code excerpts start and end on the same line, this
10-
//! may result in an overapproximation as get_loc() counts zero
11-
//! sized excerpts as one line each.
7+
//! - The "length" of each block is defined to be the end line, minus the start line,
8+
//! plus one if the excerpt starts and ends on the same line.
129
1310
use syn::spanned::Spanned;
1411

@@ -49,23 +46,18 @@ impl LoCTracker {
4946
self.instances
5047
}
5148

52-
/// Get lines of code lower bound
53-
pub fn get_loc_lb(&self) -> usize {
54-
self.lines
55-
}
56-
57-
/// Get lines of code upper bound
58-
pub fn get_loc_ub(&self) -> usize {
49+
/// Get total lines of code added
50+
pub fn get_loc(&self) -> usize {
5951
self.lines + self.zero_size_lines
6052
}
6153

6254
/// Summary as a CSV
6355
pub fn as_csv(&self) -> String {
64-
format!("{}, {}, {}", self.get_instances(), self.get_loc_lb(), self.get_loc_ub())
56+
format!("{}, {}", self.get_instances(), self.get_loc())
6557
}
6658

67-
/// Header for CSV output
68-
pub fn csv_header() -> &'static str {
69-
"Instances, LoC (lower bound), LoC (upper bound)"
70-
}
59+
// Header for CSV output (unused)
60+
// pub fn csv_header() -> &'static str {
61+
// "Instances, LoC"
62+
// }
7163
}

src/scan_stats.rs

+12-13
Original file line numberDiff line numberDiff line change
@@ -44,24 +44,22 @@ pub struct CrateStats {
4444
impl CrateStats {
4545
pub fn metadata_csv_header() -> &'static str {
4646
"\
47-
num_effects, \
48-
total, loc_lb, loc_ub, \
49-
macros, loc_lb, loc_ub, \
50-
conditional_code, loc_lb, loc_ub, \
51-
skipped_calls, loc_lb, loc_ub, \
52-
skipped_fn_ptrs, loc_lb, loc_ub, \
53-
skipped_other, loc_lb, loc_ub, \
54-
unsafe_trait, loc_lb, loc_ub, \
55-
unsafe_impl, loc_lb, loc_ub, \
56-
pub_fns, pub_fns_with_effects, pub_total_effects, \
57-
audited_fns, audited_loc\
47+
effects, \
48+
macros, macro LoC, \
49+
conditional blocks, conditional LoC, \
50+
skipped calls, skipped call LoC, \
51+
skipped fn pointers, skipped pointers LoC, \
52+
skipped other, skipped other LoC, \
53+
unsafe traits, unsafe trait LoC, \
54+
unsafe impls, unsafe impl LoC, \
55+
public fns, public fns with effects, public total effects, \
56+
audited fns, audited LoC, total LoC\
5857
"
5958
}
6059
pub fn metadata_csv(&self) -> String {
6160
format!(
6261
"{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}",
6362
self.effects.len(),
64-
self.total_loc.as_csv(),
6563
self.skipped_macros.as_csv(),
6664
self.skipped_conditional_code.as_csv(),
6765
self.skipped_fn_calls.as_csv(),
@@ -74,6 +72,7 @@ impl CrateStats {
7472
self.pub_total_effects,
7573
self.audited_fns,
7674
self.audited_loc,
75+
self.total_loc.get_loc(),
7776
)
7877
}
7978
}
@@ -143,7 +142,7 @@ fn get_auditing_metrics(audit: &AuditFile, results: &ScanResults) -> (usize, usi
143142

144143
for f in &total_fns {
145144
if let Some(tracker) = results.fn_loc_tracker.get(f) {
146-
total_loc += tracker.get_loc_lb();
145+
total_loc += tracker.get_loc();
147146
} else {
148147
// This case happens in the case of abstract trait method nodes
149148
debug!("no tracker found for a method -- possibly an abstract trait method");

0 commit comments

Comments
 (0)