Skip to content
This repository was archived by the owner on Sep 9, 2025. It is now read-only.

Commit 2216fda

Browse files
author
Hendrik van Antwerpen
committed
Use stitcher config only for functions that wrap a stitcher, not for stitcher itself
1 parent 87d312a commit 2216fda

27 files changed

Lines changed: 110 additions & 147 deletions

‎languages/tree-sitter-stack-graphs-java/rust/lib.rs‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use tree_sitter_stack_graphs::loader::FileAnalyzers;
22
use tree_sitter_stack_graphs::loader::LanguageConfiguration;
33
use tree_sitter_stack_graphs::loader::LoadError;
4-
use tree_sitter_stack_graphs::loader::StitcherConfig;
54
use tree_sitter_stack_graphs::CancellationFlag;
65

76
/// The stacks graphs tsg path for this language.
@@ -41,9 +40,6 @@ pub fn try_language_configuration(
4140
)),
4241
Some(STACK_GRAPHS_BUILTINS_CONFIG),
4342
FileAnalyzers::new(),
44-
StitcherConfig {
45-
detect_similar_paths: true,
46-
},
4743
cancellation_flag,
4844
)
4945
}

‎languages/tree-sitter-stack-graphs-typescript/rust/lib.rs‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use tree_sitter_stack_graphs::loader::FileAnalyzers;
99
use tree_sitter_stack_graphs::loader::LanguageConfiguration;
1010
use tree_sitter_stack_graphs::loader::LoadError;
11-
use tree_sitter_stack_graphs::loader::StitcherConfig;
1211
use tree_sitter_stack_graphs::CancellationFlag;
1312

1413
use crate::npm_package::NpmPackageAnalyzer;
@@ -57,7 +56,6 @@ pub fn try_language_configuration(
5756
FileAnalyzers::new()
5857
.add("tsconfig.json".to_string(), TsConfigAnalyzer {})
5958
.add("package.json".to_string(), NpmPackageAnalyzer {}),
60-
StitcherConfig::default(),
6159
cancellation_flag,
6260
)
6361
}

‎stack-graphs/include/stack-graphs.h‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,7 @@ enum sg_result sg_partial_path_arena_find_partial_paths_in_file(const struct sg_
738738
struct sg_partial_path_arena *partials,
739739
sg_file_handle file,
740740
struct sg_partial_path_list *partial_path_list,
741-
struct sg_stitcher_config config,
741+
const struct sg_stitcher_config *stitcher_config,
742742
const size_t *cancellation_flag);
743743

744744
// Finds all complete paths reachable from a set of starting nodes, placing the result into the
@@ -754,7 +754,7 @@ enum sg_result sg_partial_path_arena_find_all_complete_paths(const struct sg_sta
754754
size_t starting_node_count,
755755
const sg_node_handle *starting_nodes,
756756
struct sg_partial_path_list *path_list,
757-
struct sg_stitcher_config config,
757+
const struct sg_stitcher_config *stitcher_config,
758758
const size_t *cancellation_flag);
759759

760760
// Returns a reference to the array of partial path data in this partial path database. The
@@ -812,16 +812,14 @@ struct sg_node_handle_set sg_partial_path_database_local_nodes(const struct sg_p
812812
struct sg_forward_partial_path_stitcher *sg_forward_partial_path_stitcher_from_nodes(const struct sg_stack_graph *graph,
813813
struct sg_partial_path_arena *partials,
814814
size_t count,
815-
const sg_node_handle *starting_nodes,
816-
struct sg_stitcher_config config);
815+
const sg_node_handle *starting_nodes);
817816

818817
// Creates a new forward partial path stitcher that is "seeded" with a set of initial partial
819818
// paths.
820819
struct sg_forward_partial_path_stitcher *sg_forward_partial_path_stitcher_from_partial_paths(const struct sg_stack_graph *graph,
821820
struct sg_partial_path_arena *partials,
822821
size_t count,
823-
const struct sg_partial_path *initial_partial_paths,
824-
struct sg_stitcher_config config);
822+
const struct sg_partial_path *initial_partial_paths);
825823

826824
// Sets whether similar path detection should be enabled during path stitching. Paths are similar
827825
// if start and end node, and pre- and postconditions are the same. The presence of similar paths

‎stack-graphs/src/assert.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ impl Assertion {
150150
graph: &StackGraph,
151151
partials: &mut PartialPaths,
152152
db: &mut Database,
153-
stitcher_config: &StitcherConfig,
153+
stitcher_config: StitcherConfig,
154154
cancellation_flag: &dyn CancellationFlag,
155155
) -> Result<(), AssertionError> {
156156
match self {
@@ -175,7 +175,7 @@ impl Assertion {
175175
db: &mut Database,
176176
source: &AssertionSource,
177177
expected_targets: &Vec<AssertionTarget>,
178-
stitcher_config: &StitcherConfig,
178+
stitcher_config: StitcherConfig,
179179
cancellation_flag: &dyn CancellationFlag,
180180
) -> Result<(), AssertionError> {
181181
let references = source.iter_references(graph).collect::<Vec<_>>();

‎stack-graphs/src/c.rs‎

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,20 +1174,21 @@ pub extern "C" fn sg_partial_path_arena_find_partial_paths_in_file(
11741174
partials: *mut sg_partial_path_arena,
11751175
file: sg_file_handle,
11761176
partial_path_list: *mut sg_partial_path_list,
1177-
config: sg_stitcher_config,
1177+
stitcher_config: *const sg_stitcher_config,
11781178
cancellation_flag: *const usize,
11791179
) -> sg_result {
11801180
let graph = unsafe { &(*graph).inner };
11811181
let partials = unsafe { &mut (*partials).inner };
11821182
let file = file.into();
11831183
let partial_path_list = unsafe { &mut *partial_path_list };
1184+
let stitcher_config = unsafe { *stitcher_config };
11841185
let cancellation_flag: Option<&AtomicUsize> =
11851186
unsafe { std::mem::transmute(cancellation_flag.as_ref()) };
11861187
ForwardPartialPathStitcher::find_minimal_partial_path_set_in_file(
11871188
graph,
11881189
partials,
11891190
file,
1190-
&config.into(),
1191+
stitcher_config.into(),
11911192
&AtomicUsizeCancellationFlag(cancellation_flag),
11921193
|_graph, partials, path| {
11931194
let mut path = path.clone();
@@ -1213,19 +1214,20 @@ pub extern "C" fn sg_partial_path_arena_find_all_complete_paths(
12131214
starting_node_count: usize,
12141215
starting_nodes: *const sg_node_handle,
12151216
path_list: *mut sg_partial_path_list,
1216-
config: sg_stitcher_config,
1217+
stitcher_config: *const sg_stitcher_config,
12171218
cancellation_flag: *const usize,
12181219
) -> sg_result {
12191220
let graph = unsafe { &(*graph).inner };
12201221
let partials = unsafe { &mut (*partials).inner };
12211222
let starting_nodes = unsafe { std::slice::from_raw_parts(starting_nodes, starting_node_count) };
1223+
let stitcher_config = unsafe { *stitcher_config };
12221224
let path_list = unsafe { &mut *path_list };
12231225
let cancellation_flag: Option<&AtomicUsize> =
12241226
unsafe { std::mem::transmute(cancellation_flag.as_ref()) };
12251227
ForwardPartialPathStitcher::find_all_complete_partial_paths(
12261228
&mut GraphEdgeCandidates::new(graph, partials, None),
12271229
starting_nodes.iter().copied().map(sg_node_handle::into),
1228-
&config.into(),
1230+
stitcher_config.into(),
12291231
&AtomicUsizeCancellationFlag(cancellation_flag),
12301232
|graph, _partials, path| {
12311233
if path.is_complete(graph) {
@@ -1398,6 +1400,7 @@ pub struct sg_forward_partial_path_stitcher {
13981400

13991401
// Configuration for partial path stitchers.
14001402
#[repr(C)]
1403+
#[derive(Clone, Copy)]
14011404
pub struct sg_stitcher_config {
14021405
/// Enables similar path detection during stiching.
14031406
pub detect_similar_paths: bool,
@@ -1461,7 +1464,6 @@ pub extern "C" fn sg_forward_partial_path_stitcher_from_nodes(
14611464
partials: *mut sg_partial_path_arena,
14621465
count: usize,
14631466
starting_nodes: *const sg_node_handle,
1464-
config: sg_stitcher_config,
14651467
) -> *mut sg_forward_partial_path_stitcher {
14661468
let graph = unsafe { &(*graph).inner };
14671469
let partials = unsafe { &mut (*partials).inner };
@@ -1476,12 +1478,7 @@ pub extern "C" fn sg_forward_partial_path_stitcher_from_nodes(
14761478
p
14771479
})
14781480
.collect::<Vec<_>>();
1479-
let stitcher = ForwardPartialPathStitcher::from_partial_paths(
1480-
graph,
1481-
partials,
1482-
initial_paths,
1483-
&config.into(),
1484-
);
1481+
let stitcher = ForwardPartialPathStitcher::from_partial_paths(graph, partials, initial_paths);
14851482
Box::into_raw(Box::new(InternalForwardPartialPathStitcher::new(
14861483
stitcher, partials,
14871484
))) as *mut _
@@ -1495,7 +1492,6 @@ pub extern "C" fn sg_forward_partial_path_stitcher_from_partial_paths(
14951492
partials: *mut sg_partial_path_arena,
14961493
count: usize,
14971494
initial_partial_paths: *const sg_partial_path,
1498-
config: sg_stitcher_config,
14991495
) -> *mut sg_forward_partial_path_stitcher {
15001496
let graph = unsafe { &(*graph).inner };
15011497
let partials = unsafe { &mut (*partials).inner };
@@ -1505,7 +1501,6 @@ pub extern "C" fn sg_forward_partial_path_stitcher_from_partial_paths(
15051501
graph,
15061502
partials,
15071503
initial_partial_paths.to_vec(),
1508-
&config.into(),
15091504
);
15101505
Box::into_raw(Box::new(InternalForwardPartialPathStitcher::new(
15111506
stitcher, partials,

‎stack-graphs/src/stitching.rs‎

Lines changed: 63 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -795,13 +795,6 @@ impl<'a> Display for DisplaySymbolStackKey<'a> {
795795
//-------------------------------------------------------------------------------------------------
796796
// Stitching partial paths together
797797

798-
/// Configuration for partial path stitchers.
799-
#[derive(Clone, Debug, Default)]
800-
pub struct StitcherConfig {
801-
/// Enables similar path detection during path stitching.
802-
pub detect_similar_paths: bool,
803-
}
804-
805798
/// Implements a phased forward partial path stitching algorithm.
806799
///
807800
/// Our overall goal is to start with a set of _seed_ partial paths, and to repeatedly extend each
@@ -858,7 +851,6 @@ impl<H> ForwardPartialPathStitcher<H> {
858851
_graph: &StackGraph,
859852
_partials: &mut PartialPaths,
860853
initial_partial_paths: I,
861-
config: &StitcherConfig,
862854
) -> Self
863855
where
864856
I: IntoIterator<Item = PartialPath>,
@@ -871,16 +863,14 @@ impl<H> ForwardPartialPathStitcher<H> {
871863
(p, c, false)
872864
})
873865
.multiunzip();
874-
let similar_path_detector =
875-
Some(SimilarPathDetector::new()).filter(|_| config.detect_similar_paths);
876-
ForwardPartialPathStitcher {
866+
Self {
877867
candidates: Vec::new(),
878868
extensions: Vec::new(),
879869
queue: VecDeque::new(),
880870
initial_paths: next_iteration.0.len(),
881871
next_iteration,
882872
appended_paths,
883-
similar_path_detector,
873+
similar_path_detector: Some(SimilarPathDetector::new()),
884874
// By default, all nodes are checked for cycles and (if enabled) similarity
885875
check_only_join_nodes: false,
886876
// By default, there's no artificial bound on the amount of work done per phase
@@ -889,28 +879,6 @@ impl<H> ForwardPartialPathStitcher<H> {
889879
phase_number: 1,
890880
}
891881
}
892-
}
893-
894-
impl<H: Clone> ForwardPartialPathStitcher<H> {
895-
/// Returns an iterator of all of the (possibly incomplete) partial paths that were encountered
896-
/// during the most recent phase of the algorithm.
897-
pub fn previous_phase_partial_paths(&self) -> impl Iterator<Item = &PartialPath> + '_ {
898-
self.next_iteration.0.iter()
899-
}
900-
901-
/// Returns a slice of all of the (possibly incomplete) partial paths that were encountered
902-
/// during the most recent phase of the algorithm.
903-
pub fn previous_phase_partial_paths_slice(&mut self) -> &[PartialPath] {
904-
self.next_iteration.0.make_contiguous();
905-
self.next_iteration.0.as_slices().0
906-
}
907-
908-
/// Returns a mutable slice of all of the (possibly incomplete) partial paths that were
909-
/// encountered during the most recent phase of the algorithm.
910-
pub fn previous_phase_partial_paths_slice_mut(&mut self) -> &mut [PartialPath] {
911-
self.next_iteration.0.make_contiguous();
912-
self.next_iteration.0.as_mut_slices().0
913-
}
914882

915883
/// Sets whether similar path detection should be enabled during path stitching. Paths are similar
916884
/// if start and end node, and pre- and postconditions are the same. The presence of similar paths
@@ -940,6 +908,28 @@ impl<H: Clone> ForwardPartialPathStitcher<H> {
940908
pub fn set_max_work_per_phase(&mut self, max_work_per_phase: usize) {
941909
self.max_work_per_phase = max_work_per_phase;
942910
}
911+
}
912+
913+
impl<H: Clone> ForwardPartialPathStitcher<H> {
914+
/// Returns an iterator of all of the (possibly incomplete) partial paths that were encountered
915+
/// during the most recent phase of the algorithm.
916+
pub fn previous_phase_partial_paths(&self) -> impl Iterator<Item = &PartialPath> + '_ {
917+
self.next_iteration.0.iter()
918+
}
919+
920+
/// Returns a slice of all of the (possibly incomplete) partial paths that were encountered
921+
/// during the most recent phase of the algorithm.
922+
pub fn previous_phase_partial_paths_slice(&mut self) -> &[PartialPath] {
923+
self.next_iteration.0.make_contiguous();
924+
self.next_iteration.0.as_slices().0
925+
}
926+
927+
/// Returns a mutable slice of all of the (possibly incomplete) partial paths that were
928+
/// encountered during the most recent phase of the algorithm.
929+
pub fn previous_phase_partial_paths_slice_mut(&mut self) -> &mut [PartialPath] {
930+
self.next_iteration.0.make_contiguous();
931+
self.next_iteration.0.as_mut_slices().0
932+
}
943933

944934
/// Attempts to extend one partial path as part of the algorithm. When calling this function,
945935
/// you are responsible for ensuring that `db` already contains all of the possible appendables
@@ -1155,7 +1145,7 @@ impl ForwardPartialPathStitcher<Edge> {
11551145
graph: &StackGraph,
11561146
partials: &mut PartialPaths,
11571147
file: Handle<File>,
1158-
config: &StitcherConfig,
1148+
config: StitcherConfig,
11591149
cancellation_flag: &dyn CancellationFlag,
11601150
mut visit: F,
11611151
) -> Result<(), CancellationError>
@@ -1174,7 +1164,8 @@ impl ForwardPartialPathStitcher<Edge> {
11741164
.map(|node| PartialPath::from_node(graph, partials, node))
11751165
.collect::<Vec<_>>();
11761166
let mut stitcher =
1177-
ForwardPartialPathStitcher::from_partial_paths(graph, partials, initial_paths, config);
1167+
ForwardPartialPathStitcher::from_partial_paths(graph, partials, initial_paths);
1168+
config.apply(&mut stitcher);
11781169
stitcher.set_check_only_join_nodes(true);
11791170
while !stitcher.is_complete() {
11801171
cancellation_flag.check("finding complete partial paths")?;
@@ -1207,7 +1198,7 @@ impl<H: Clone> ForwardPartialPathStitcher<H> {
12071198
pub fn find_all_complete_partial_paths<I, F, A, Db, C, Err>(
12081199
candidates: &mut C,
12091200
starting_nodes: I,
1210-
config: &StitcherConfig,
1201+
config: StitcherConfig,
12111202
cancellation_flag: &dyn CancellationFlag,
12121203
mut visit: F,
12131204
) -> Result<(), Err>
@@ -1219,19 +1210,19 @@ impl<H: Clone> ForwardPartialPathStitcher<H> {
12191210
F: FnMut(&StackGraph, &mut PartialPaths, &PartialPath),
12201211
Err: std::convert::From<CancellationError>,
12211212
{
1222-
let mut stitcher = {
1223-
let (graph, partials, _) = candidates.get_graph_partials_and_db();
1224-
let initial_paths = starting_nodes
1225-
.into_iter()
1226-
.filter(|n| graph[*n].is_reference())
1227-
.map(|n| {
1228-
let mut p = PartialPath::from_node(graph, partials, n);
1229-
p.eliminate_precondition_stack_variables(partials);
1230-
p
1231-
})
1232-
.collect::<Vec<_>>();
1233-
ForwardPartialPathStitcher::from_partial_paths(graph, partials, initial_paths, config)
1234-
};
1213+
let (graph, partials, _) = candidates.get_graph_partials_and_db();
1214+
let initial_paths = starting_nodes
1215+
.into_iter()
1216+
.filter(|n| graph[*n].is_reference())
1217+
.map(|n| {
1218+
let mut p = PartialPath::from_node(graph, partials, n);
1219+
p.eliminate_precondition_stack_variables(partials);
1220+
p
1221+
})
1222+
.collect::<Vec<_>>();
1223+
let mut stitcher =
1224+
ForwardPartialPathStitcher::from_partial_paths(graph, partials, initial_paths);
1225+
config.apply(&mut stitcher);
12351226
stitcher.set_check_only_join_nodes(true);
12361227
while !stitcher.is_complete() {
12371228
cancellation_flag.check("finding complete partial paths")?;
@@ -1249,3 +1240,25 @@ impl<H: Clone> ForwardPartialPathStitcher<H> {
12491240
Ok(())
12501241
}
12511242
}
1243+
1244+
/// Configuration for partial path stitchers.
1245+
#[repr(C)]
1246+
#[derive(Clone, Copy, Debug)]
1247+
pub struct StitcherConfig {
1248+
/// Enables similar path detection during path stitching.
1249+
pub detect_similar_paths: bool,
1250+
}
1251+
1252+
impl StitcherConfig {
1253+
fn apply<H>(&self, stitcher: &mut ForwardPartialPathStitcher<H>) {
1254+
stitcher.set_similar_path_detection(self.detect_similar_paths);
1255+
}
1256+
}
1257+
1258+
impl Default for StitcherConfig {
1259+
fn default() -> Self {
1260+
Self {
1261+
detect_similar_paths: true,
1262+
}
1263+
}
1264+
}

‎stack-graphs/tests/it/c/can_find_local_nodes.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ fn check_local_nodes(graph: &TestGraph, file: &str, expected_local_nodes: &[&str
3434

3535
let partials = sg_partial_path_arena_new();
3636
let path_list = sg_partial_path_list_new();
37-
let config = sg_stitcher_config {
37+
let stitcher_config = sg_stitcher_config {
3838
detect_similar_paths: false,
3939
};
4040
sg_partial_path_arena_find_partial_paths_in_file(
4141
graph.graph,
4242
partials,
4343
file.as_u32(),
4444
path_list,
45-
config,
45+
&stitcher_config,
4646
std::ptr::null(),
4747
);
4848

0 commit comments

Comments
 (0)