Skip to content

Commit

Permalink
Bugfix for #71.
Browse files Browse the repository at this point in the history
  • Loading branch information
anderejd committed Nov 5, 2019
1 parent 9b3b9b6 commit 89119f8
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
created: "2019-08-04T18:33:42.673360579Z"
creator: insta@0.10.0
created: "2019-11-05T20:25:00.717945Z"
creator: insta@0.10.1
source: cargo-geiger/tests/mod.rs
expression: stdout
---
Expand All @@ -16,5 +16,5 @@ Symbols:

Functions Expressions Impls Traits Methods Dependency

1/1 1/1 0/0 0/0 0/0 ! test1_package_with_no_deps 0.1.0
1/1 2/2 0/0 0/0 0/0 ! test1_package_with_no_deps 0.1.0

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
created: "2019-08-04T18:52:10.690069132Z"
creator: insta@0.10.0
created: "2019-11-05T20:25:00.881602Z"
creator: insta@0.10.1
source: cargo-geiger/tests/mod.rs
expression: stdout
---
Expand All @@ -16,7 +16,7 @@ Symbols:

Functions Expressions Impls Traits Methods Dependency

1/1 0/0 0/0 0/0 0/0 ! test2_package_with_shallow_deps 0.1.0
1/1 4/4 0/0 0/0 0/0 ! test2_package_with_shallow_deps 0.1.0
0/0 2/2 0/0 0/0 0/0 ! |-- ref_slice 1.1.1
1/1 1/1 0/0 0/0 0/0 ! `-- test1_package_with_no_deps 0.1.0
1/1 2/2 0/0 0/0 0/0 ! `-- test1_package_with_no_deps 0.1.0
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
created: "2019-08-04T19:19:52.253818844Z"
creator: insta@0.10.0
created: "2019-11-05T20:25:03.792379Z"
creator: insta@0.10.1
source: cargo-geiger/tests/mod.rs
expression: stdout
---
Expand All @@ -18,9 +18,9 @@ Functions Expressions Impls Traits Methods Dependency

0/0 1/1 0/0 0/0 0/0 ! test3_package_with_nested_deps 0.1.0
0/0 0/0 0/0 0/0 0/0 ? |-- doc-comment 0.3.1
0/0 0/55 0/3 0/1 0/3 ? |-- itertools 0.8.0
0/0 0/72 0/3 0/1 0/3 ? |-- itertools 0.8.0
0/0 0/0 0/0 0/0 0/0 ? | `-- either 1.5.2
1/1 0/0 0/0 0/0 0/0 ! `-- test2_package_with_shallow_deps 0.1.0
1/1 4/4 0/0 0/0 0/0 ! `-- test2_package_with_shallow_deps 0.1.0
0/0 2/2 0/0 0/0 0/0 ! |-- ref_slice 1.1.1
1/1 1/1 0/0 0/0 0/0 ! `-- test1_package_with_no_deps 0.1.0
1/1 2/2 0/0 0/0 0/0 ! `-- test1_package_with_no_deps 0.1.0
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
created: "2019-08-04T18:52:10.256237005Z"
creator: insta@0.10.0
created: "2019-11-05T20:25:00.871051Z"
creator: insta@0.10.1
source: cargo-geiger/tests/mod.rs
expression: stdout
---
Expand All @@ -17,5 +17,5 @@ Symbols:
Functions Expressions Impls Traits Methods Dependency

0/0 0/1 0/0 0/0 0/0 ? test4_workspace_with_virtual_manifest 0.1.0
1/1 1/1 0/0 0/0 0/0 ! `-- test1_package_with_no_deps 0.1.0
1/1 2/2 0/0 0/0 0/0 ! `-- test1_package_with_no_deps 0.1.0
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
created: "2019-08-04T17:26:25.848181323Z"
creator: insta@0.10.0
created: "2019-11-05T20:25:00.366323Z"
creator: insta@0.10.1
source: cargo-geiger/tests/mod.rs
expression: stderr
---
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
created: "2019-08-04T17:27:53.034149454Z"
creator: insta@0.10.0
created: "2019-11-05T20:30:26.212139Z"
creator: insta@0.10.1
source: cargo-geiger/tests/mod.rs
expression: stdout
---
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
created: "2019-09-24T17:02:24.314541942Z"
created: "2019-11-05T20:25:10.736895Z"
creator: insta@0.10.1
source: cargo-geiger/tests/mod.rs
expression: stdout
Expand All @@ -24,5 +24,5 @@ Functions Expressions Impls Traits Methods Dependency
0/0 0/0 0/0 0/0 0/0 :) |-- unicode-bidi 0.3.4
0/0 0/0 0/0 0/0 0/0 ? | `-- matches 0.1.8
0/0 20/20 0/0 0/0 0/0 ! `-- unicode-normalization 0.1.8
2/2 318/318 4/4 1/1 13/13 ! `-- smallvec 0.6.9
2/2 354/354 4/4 1/1 13/13 ! `-- smallvec 0.6.9
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
created: "2019-09-24T17:32:03.899959253Z"
created: "2019-11-05T20:25:00.986247Z"
creator: insta@0.10.1
source: cargo-geiger/tests/mod.rs
expression: stdout
Expand All @@ -18,5 +18,5 @@ Functions Expressions Impls Traits Methods Dependency

0/0 0/0 0/0 0/0 0/0 :) test7_package_with_patched_dep 0.1.0
0/0 0/0 0/0 0/0 0/0 ? `-- num_cpus 1.10.1
1/1 1/1 0/0 0/0 0/0 ! `-- test1_package_with_no_deps 0.1.0
1/1 2/2 0/0 0/0 0/0 ! `-- test1_package_with_no_deps 0.1.0

40 changes: 33 additions & 7 deletions geiger/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,18 +125,32 @@ struct GeigerSynVisitor {
/// The resulting data from a single file scan.
metrics: RsFileMetrics,

/// Used by the Visit trait implementation to track the traversal state.
in_unsafe_block: bool,
/// The number of nested unsafe scopes that the GeigerSynVisitor are
/// currently in. For example, if the visitor is inside an unsafe function
/// and inside an unnecessary unsafe block inside that function, then this
/// number should be 2. If the visitor is outside unsafe scopes, in a safe
/// scope, this number should be 0.
/// This is needed since unsafe scopes can be nested and we need to know
/// when we leave the outmost unsafe scope and get back into a safe scope.
unsafe_scopes: u32,
}

impl GeigerSynVisitor {
fn new(include_tests: IncludeTests) -> Self {
GeigerSynVisitor {
include_tests,
metrics: Default::default(),
in_unsafe_block: false,
unsafe_scopes: 0,
}
}

fn enter_unsafe_scope(&mut self) {
self.unsafe_scopes += 1;
}

fn exit_unsafe_scope(&mut self) {
self.unsafe_scopes -= 1;
}
}

/// Will return true for #[cfg(test)] decodated modules.
Expand Down Expand Up @@ -245,20 +259,26 @@ impl<'ast> visit::Visit<'ast> for GeigerSynVisitor {
if IncludeTests::No == self.include_tests && is_test_fn(i) {
return;
}
if i.sig.unsafety.is_some() {
self.enter_unsafe_scope()
}
self.metrics
.counters
.functions
.count(i.sig.unsafety.is_some());
visit::visit_item_fn(self, i);
if i.sig.unsafety.is_some() {
self.exit_unsafe_scope()
}
}

fn visit_expr(&mut self, i: &Expr) {
// Total number of expressions of any type
match i {
Expr::Unsafe(i) => {
self.in_unsafe_block = true;
self.enter_unsafe_scope();
visit::visit_expr_unsafe(self, i);
self.in_unsafe_block = false;
self.exit_unsafe_scope();
}
Expr::Path(_) | Expr::Lit(_) => {
// Do not count. The expression `f(x)` should count as one
Expand All @@ -267,10 +287,10 @@ impl<'ast> visit::Visit<'ast> for GeigerSynVisitor {
other => {
// TODO: Print something pretty here or gather the data for later
// printing.
// if self.verbosity == Verbosity::Verbose && self.in_unsafe_block {
// if self.verbosity == Verbosity::Verbose && self.unsafe_scopes > 0 {
// println!("{:#?}", other);
// }
self.metrics.counters.exprs.count(self.in_unsafe_block);
self.metrics.counters.exprs.count(self.unsafe_scopes > 0);
visit::visit_expr(self, other);
}
}
Expand Down Expand Up @@ -299,11 +319,17 @@ impl<'ast> visit::Visit<'ast> for GeigerSynVisitor {
}

fn visit_impl_item_method(&mut self, i: &ImplItemMethod) {
if i.sig.unsafety.is_some() {
self.enter_unsafe_scope()
}
self.metrics
.counters
.methods
.count(i.sig.unsafety.is_some());
visit::visit_impl_item_method(self, i);
if i.sig.unsafety.is_some() {
self.exit_unsafe_scope()
}
}

// TODO: Visit macros.
Expand Down

0 comments on commit 89119f8

Please sign in to comment.