Skip to content

Commit

Permalink
🧹 simplify LoCTracker interface
Browse files Browse the repository at this point in the history
  • Loading branch information
cdstanford committed Aug 27, 2024
1 parent f35bd36 commit 0352cd3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 35 deletions.
5 changes: 2 additions & 3 deletions src/bin/audit_stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ fn compute_stats(
// Collect total lines of code for audited functions
for f in &total_fns {
match results.fn_loc_tracker.get(f) {
Some(tracker) => audited_loc += tracker.get_loc_lb(),
Some(tracker) => audited_loc += tracker.get_loc(),
None => debug!(
"failed to find tracker node for `{:?}`. possibly it is a trait method.",
f.as_str()
Expand All @@ -147,8 +147,7 @@ fn compute_stats(
});
sink_calls.extend(sink_effects.cloned());

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

AuditingStats {
crate_id,
Expand Down
30 changes: 11 additions & 19 deletions src/loc_tracker.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
//! Abstract tracker for calculating total lines of code (LoC)
//! in a sequence of code excerpts.
//! in a sequence of code blocks.
//!
//! Supports a simple interface using add(s) for s: Spanned
//! to add a code excerpt.
//! Code blocks are added through the interface add(s) for s: Spanned.
//!
//! Important note:
//! - Assumes that code excerpts do not overlap.
//! - If multiple code excerpts start and end on the same line, this
//! may result in an overapproximation as get_loc() counts zero
//! sized excerpts as one line each.
//! - The "length" of each block is defined to be the end line, minus the start line,
//! plus one if the excerpt starts and ends on the same line.

use syn::spanned::Spanned;

Expand Down Expand Up @@ -49,23 +46,18 @@ impl LoCTracker {
self.instances
}

/// Get lines of code lower bound
pub fn get_loc_lb(&self) -> usize {
self.lines
}

/// Get lines of code upper bound
pub fn get_loc_ub(&self) -> usize {
/// Get total lines of code added
pub fn get_loc(&self) -> usize {
self.lines + self.zero_size_lines
}

/// Summary as a CSV
pub fn as_csv(&self) -> String {
format!("{}, {}, {}", self.get_instances(), self.get_loc_lb(), self.get_loc_ub())
format!("{}, {}", self.get_instances(), self.get_loc())
}

/// Header for CSV output
pub fn csv_header() -> &'static str {
"Instances, LoC (lower bound), LoC (upper bound)"
}
// Header for CSV output (unused)
// pub fn csv_header() -> &'static str {
// "Instances, LoC"
// }
}
25 changes: 12 additions & 13 deletions src/scan_stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,22 @@ pub struct CrateStats {
impl CrateStats {
pub fn metadata_csv_header() -> &'static str {
"\
num_effects, \
total, loc_lb, loc_ub, \
macros, loc_lb, loc_ub, \
conditional_code, loc_lb, loc_ub, \
skipped_calls, loc_lb, loc_ub, \
skipped_fn_ptrs, loc_lb, loc_ub, \
skipped_other, loc_lb, loc_ub, \
unsafe_trait, loc_lb, loc_ub, \
unsafe_impl, loc_lb, loc_ub, \
pub_fns, pub_fns_with_effects, pub_total_effects, \
audited_fns, audited_loc\
effects, \
macros, macro LoC, \
conditional blocks, conditional LoC, \
skipped calls, skipped call LoC, \
skipped fn pointers, skipped pointers LoC, \
skipped other, skipped other LoC, \
unsafe traits, unsafe trait LoC, \
unsafe impls, unsafe impl LoC, \
public fns, public fns with effects, public total effects, \
audited fns, audited LoC, total LoC\
"
}
pub fn metadata_csv(&self) -> String {
format!(
"{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}",
self.effects.len(),
self.total_loc.as_csv(),
self.skipped_macros.as_csv(),
self.skipped_conditional_code.as_csv(),
self.skipped_fn_calls.as_csv(),
Expand All @@ -74,6 +72,7 @@ impl CrateStats {
self.pub_total_effects,
self.audited_fns,
self.audited_loc,
self.total_loc.get_loc(),
)
}
}
Expand Down Expand Up @@ -143,7 +142,7 @@ fn get_auditing_metrics(audit: &AuditFile, results: &ScanResults) -> (usize, usi

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

0 comments on commit 0352cd3

Please sign in to comment.