Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

- **`cora dead-code`, `callers` and `impact` see functions passed as values and functions named in serde attributes (#802).** The Rust extractor recorded a call only for a `call_expression` and inside macros, so `.map(helper)`, `.and_then(Type::make)` and `#[serde(default = "default_true")]` made the function look unused. On this repository `cora dead-code` went from 43 findings, none of them dead code, to the 5 that are (the unwired bundling engine of #792), and `callers` now lists the function that passes another one as a value. A variable that merely has the name of a function (a parameter, a `let`, a pattern binding) is not recorded. The first `cora index` after the upgrade re-extracts every file once (#803).

- **`cora index` re-extracts every file once after an extractor change (#803).** A file was skipped when its `mtime:size` matched, so an upgrade that improved an extractor (the Ruby nesting of #791, for example) never reached the files that had not changed, until someone ran `cora index --rebuild`. The fingerprint now carries an extractor version, so the first `cora index` after such an upgrade re-extracts all files once; later runs skip unchanged files as before.

- **Ruby: methods in modules and classes nested in modules are indexed (#791).** Two gaps in the Tree-sitter extractor: a second `"module"` arm that also extracts the methods was unreachable, and only the direct children of a file were visited, so the usual `module Outer; class Inner; def run` layout indexed nothing inside `Outer`. The superclass edge of a Ruby class also carried the `<` of `< Base` in its target, so it never matched the symbol `Base`. On Sinatra (147 Ruby files) the index grows from 184 to 853 symbols, the methods from 79 to 622, and `cora explore halt` finds `Sinatra::Base#halt`. Projects with Ruby code need one `cora index` to benefit.
Expand Down
237 changes: 235 additions & 2 deletions src/index/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
use crate::index::extract::CallSite;
use crate::index::extract::ExtractedDef;
use crate::index::symbols::SymbolKind;
use regex::Regex;
use std::collections::HashSet;
use std::sync::LazyLock;

/// A node in the code graph — a definition extracted from source.
#[derive(Debug, Clone)]
Expand Down Expand Up @@ -322,6 +325,7 @@ fn extract_rust(
"struct_item" => {
let name = node_name(&child, source);
if !name.is_empty() {
serde_attribute_calls(&child, source, file_path, &name, &mut edges);
nodes.push(AstNode {
name,
kind: SymbolKind::Struct,
Expand All @@ -335,6 +339,7 @@ fn extract_rust(
"enum_item" => {
let name = node_name(&child, source);
if !name.is_empty() {
serde_attribute_calls(&child, source, file_path, &name, &mut edges);
nodes.push(AstNode {
name,
kind: SymbolKind::Enum,
Expand Down Expand Up @@ -563,6 +568,7 @@ fn extract_calls_in_impl(
file_path: &str,
caller: &str,
self_type: Option<&str>,
locals: &HashSet<String>,
edges: &mut Vec<AstEdge>,
) {
if node.kind() == "call_expression" {
Expand All @@ -582,6 +588,11 @@ fn extract_calls_in_impl(
}
}
}
// `.map(helper)`, `.map(Type::make)`: a function passed as a value is called
// by the function that passes it, although there is no `call_expression`.
if node.kind() == "arguments" {
function_references(node, source, file_path, caller, self_type, locals, edges);
}
// The arguments of a macro are a raw `token_tree`, not parsed
// expressions, so a call inside `json!(..)`, `vec![..]`, `format!(..)`,
// `assert!(..)` has no `call_expression` node (#715).
Expand All @@ -591,14 +602,136 @@ fn extract_calls_in_impl(
let mut cursor = node.walk();
if cursor.goto_first_child() {
loop {
walk_calls(&cursor.node(), source, file_path, caller, self_type, edges);
walk_calls(
&cursor.node(),
source,
file_path,
caller,
self_type,
locals,
edges,
);
if !cursor.goto_next_sibling() {
break;
}
}
}
}
walk_calls(node, source, file_path, caller, self_type, edges);
let locals = local_bindings(&node_text(node, source));
walk_calls(node, source, file_path, caller, self_type, &locals, edges);
}

/// Names a function body binds itself (parameters, `let`, `for`, closure
/// parameters). A bare identifier with one of these names that is passed as an
/// argument is a variable, not a function used as a value.
fn local_bindings(body: &str) -> HashSet<String> {
// Every lowercase word of a pattern is a binding or a harmless keyword.
static WORD: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\b[a-z_]\w*").unwrap());
// `let (a, b) =`, `let mut x =`, `if let Some(v) =`, `for (i, x) in`
static AFTER_KEYWORD: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"(?s)\b(?:let|for)\b([^=;{]*?)(?:=|\bin\b)").unwrap());
// `Some(v) =>`, `Ok(value) if cond =>`
static ARM: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"\b[A-Z]\w*\s*\(([^()]*)\)\s*(?:if\b[^=\n]*)?=>").unwrap());
// `Command::Callers { symbol, limit } =>`, `if let Cfg { name, .. } =`
static STRUCT_PATTERN: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"\b[A-Z][\w:]*\s*\{([^{}]*)\}\s*(?:=>|=)").unwrap());
// `name: Type` (parameters, closure parameters with a type)
static TYPED: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\b([a-z_]\w*)\s*:").unwrap());
// `|a, b|`
static CLOSURE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\|([^|]*)\|").unwrap());
let mut names = HashSet::new();
for re in [&*AFTER_KEYWORD, &*ARM, &*STRUCT_PATTERN, &*CLOSURE] {
for cap in re.captures_iter(body) {
names.extend(WORD.find_iter(&cap[1]).map(|m| m.as_str().to_string()));
}
}
names.extend(TYPED.captures_iter(body).map(|c| c[1].to_string()));
names
}

/// Function paths passed as arguments: a bare snake_case identifier that the
/// function does not bind itself, or a `Type::f` / `module::f` path. Constants,
/// enum variants and types start uppercase and are skipped.
fn function_references(
args: &tree_sitter::Node,
source: &str,
file_path: &str,
caller: &str,
self_type: Option<&str>,
locals: &HashSet<String>,
edges: &mut Vec<AstEdge>,
) {
let mut cursor = args.walk();
for arg in args.children(&mut cursor) {
if !matches!(arg.kind(), "identifier" | "scoped_identifier") {
continue;
}
let name = normalize_callee_name(&node_text(&arg, source)).to_string();
if !name.starts_with(|c: char| c.is_ascii_lowercase() || c == '_') {
continue;
}
if arg.kind() == "identifier" && locals.contains(&name) {
continue;
}
edges.push(AstEdge {
source: caller.to_string(),
kind: EdgeKind::Calls,
target: name,
file: file_path.to_string(),
line: (arg.start_position().row + 1) as u32,
qualifier: callee_qualifier(&arg, source, self_type),
caller_type: self_type.map(str::to_string),
});
}
}

/// Functions named in the `serde` attributes of a struct or enum and of its
/// fields (`default = "f"`, `deserialize_with = "path::f"`, ...): serde calls
/// them, so the item is their caller.
fn serde_attribute_calls(
item: &tree_sitter::Node,
source: &str,
file_path: &str,
owner: &str,
edges: &mut Vec<AstEdge>,
) {
static SERDE_FN: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(
r#"\b(?:default|deserialize_with|serialize_with|skip_serializing_if|with)\s*=\s*"([A-Za-z_][\w:]*)""#,
)
.unwrap()
});
if item.kind() == "attribute_item" {
let text = node_text(item, source);
if text.contains("serde") {
for cap in SERDE_FN.captures_iter(&text) {
let path = &cap[1];
let name = path.rsplit("::").next().unwrap_or(path);
if name.is_empty() {
continue;
}
let qualifier = path.rsplit_once("::").and_then(|(prefix, _)| {
path_qualifier(prefix)
.filter(|q| !matches!(q.as_str(), "crate" | "super" | "self"))
});
edges.push(AstEdge {
source: owner.to_string(),
kind: EdgeKind::Calls,
target: name.to_string(),
file: file_path.to_string(),
line: (item.start_position().row + 1) as u32,
qualifier,
caller_type: None,
});
}
}
return;
}
let mut cursor = item.walk();
for child in item.children(&mut cursor) {
serde_attribute_calls(&child, source, file_path, owner, edges);
}
}

/// Items that a token tree can declare; an identifier after one of these
Expand Down Expand Up @@ -2704,6 +2837,106 @@ func (s *Server) Start() error {
assert!(names.contains(&"Start"));
}

fn call_targets(edges: &[AstEdge], caller: &str) -> Vec<String> {
let mut t: Vec<String> = edges
.iter()
.filter(|e| e.kind == EdgeKind::Calls && e.source == caller)
.map(|e| e.target.clone())
.collect();
t.sort();
t
}

#[test]
fn test_rust_function_passed_as_argument_is_a_call() {
// A function used as a value is called by the function that passes it:
// `cora callers`, `impact` and `dead-code` must see it (no parentheses, so
// no `call_expression`).
let code = r#"
fn helper(x: u32) -> u32 { x }
struct S;
impl S { fn make(v: u32) -> S { S } }
fn run(items: Vec<u32>) {
let _a: Vec<u32> = items.iter().copied().map(helper).collect();
let _b: Vec<S> = items.into_iter().map(S::make).collect();
let _c = other(0, MAX_LEN, None, Mode::Fast, "text", 7, &items);
let (conn, project_id) = setup();
use_both(conn, project_id);
if let Some(inner) = lookup() {
take(inner);
}
match find() {
Ok(value) => keep(value),
Err(_) => {}
}
for (index, entry) in items.iter().enumerate() {
visit(index, entry);
}
match command {
Command::Callers { symbol, limit } => callers(symbol, limit),
_ => {}
}
}
"#;
let (_nodes, edges) = extract(code, "rs", "run.rs");
let targets = call_targets(&edges, "run");
assert!(targets.contains(&"helper".to_string()), "{targets:?}");
assert!(targets.contains(&"make".to_string()), "{targets:?}");
let make = edges.iter().find(|e| e.target == "make").unwrap();
assert_eq!(make.qualifier.as_deref(), Some("S"));
for noise in [
"MAX_LEN",
"None",
"Fast",
"Mode",
"text",
"items",
"conn",
"project_id",
"inner",
"value",
"index",
"entry",
"symbol",
"limit",
] {
assert!(
!targets.contains(&noise.to_string()),
"{noise} is not a function: {targets:?}"
);
}
}

#[test]
fn test_rust_serde_attribute_functions_are_calls_of_the_struct() {
let code = r#"
#[derive(Deserialize)]
struct Cfg {
#[serde(default = "default_true")]
on: bool,
#[serde(skip_serializing_if = "is_default", deserialize_with = "crate::de::lenient")]
n: u32,
/// default = "not_an_attribute"
note: String,
}
fn default_true() -> bool { true }
"#;
let (_nodes, edges) = extract(code, "rs", "cfg.rs");
let targets = call_targets(&edges, "Cfg");
for want in ["default_true", "is_default", "lenient"] {
assert!(
targets.contains(&want.to_string()),
"missing {want}: {targets:?}"
);
}
assert!(
!targets.contains(&"not_an_attribute".to_string()),
"{targets:?}"
);
let lenient = edges.iter().find(|e| e.target == "lenient").unwrap();
assert_eq!(lenient.qualifier.as_deref(), Some("de"));
}

#[test]
fn test_extract_ruby_module_methods() {
// #791: the methods of a Ruby module were never indexed because the
Expand Down
5 changes: 4 additions & 1 deletion src/index/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,10 @@ fn file_fingerprint(content: &str) -> String {
/// so bumping it makes the next `cora index` re-extract every file once, including
/// files that did not change (#803). Bump it whenever an extractor starts to
/// record symbols or edges it did not record before, or stops recording wrong ones.
const EXTRACTOR_VERSION: u32 = 1;
///
/// History: 2 records function values passed as arguments and the functions named in
/// serde attributes as calls (#802).
const EXTRACTOR_VERSION: u32 = 2;

/// Statistics from an index build run.
#[derive(Debug, Clone, Default)]
Expand Down
Loading