Skip to content

Commit 4921589

Browse files
Use stitcher config only for functions that wrap a stitcher, not for stitcher itself
1 parent c4c3b20 commit 4921589

27 files changed

+110
-133
lines changed

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
@@ -733,13 +733,6 @@ impl<'a> Display for DisplaySymbolStackKey<'a> {
733733
//-------------------------------------------------------------------------------------------------
734734
// Stitching partial paths together
735735

736-
/// Configuration for partial path stitchers.
737-
#[derive(Clone, Debug, Default)]
738-
pub struct StitcherConfig {
739-
/// Enables similar path detection during path stitching.
740-
pub detect_similar_paths: bool,
741-
}
742-
743736
/// Implements a phased forward partial path stitching algorithm.
744737
///
745738
/// Our overall goal is to start with a set of _seed_ partial paths, and to repeatedly extend each
@@ -796,7 +789,6 @@ impl<H> ForwardPartialPathStitcher<H> {
796789
_graph: &StackGraph,
797790
_partials: &mut PartialPaths,
798791
initial_partial_paths: I,
799-
config: &StitcherConfig,
800792
) -> Self
801793
where
802794
I: IntoIterator<Item = PartialPath>,
@@ -809,16 +801,14 @@ impl<H> ForwardPartialPathStitcher<H> {
809801
(p, c, false)
810802
})
811803
.multiunzip();
812-
let similar_path_detector =
813-
Some(SimilarPathDetector::new()).filter(|_| config.detect_similar_paths);
814-
ForwardPartialPathStitcher {
804+
Self {
815805
candidates: Vec::new(),
816806
extensions: Vec::new(),
817807
queue: VecDeque::new(),
818808
initial_paths: next_iteration.0.len(),
819809
next_iteration,
820810
appended_paths,
821-
similar_path_detector,
811+
similar_path_detector: Some(SimilarPathDetector::new()),
822812
// By default, all nodes are checked for cycles and (if enabled) similarity
823813
check_only_join_nodes: false,
824814
// By default, there's no artificial bound on the amount of work done per phase
@@ -827,28 +817,6 @@ impl<H> ForwardPartialPathStitcher<H> {
827817
phase_number: 1,
828818
}
829819
}
830-
}
831-
832-
impl<H: Clone> ForwardPartialPathStitcher<H> {
833-
/// Returns an iterator of all of the (possibly incomplete) partial paths that were encountered
834-
/// during the most recent phase of the algorithm.
835-
pub fn previous_phase_partial_paths(&self) -> impl Iterator<Item = &PartialPath> + '_ {
836-
self.next_iteration.0.iter()
837-
}
838-
839-
/// Returns a slice of all of the (possibly incomplete) partial paths that were encountered
840-
/// during the most recent phase of the algorithm.
841-
pub fn previous_phase_partial_paths_slice(&mut self) -> &[PartialPath] {
842-
self.next_iteration.0.make_contiguous();
843-
self.next_iteration.0.as_slices().0
844-
}
845-
846-
/// Returns a mutable slice of all of the (possibly incomplete) partial paths that were
847-
/// encountered during the most recent phase of the algorithm.
848-
pub fn previous_phase_partial_paths_slice_mut(&mut self) -> &mut [PartialPath] {
849-
self.next_iteration.0.make_contiguous();
850-
self.next_iteration.0.as_mut_slices().0
851-
}
852820

853821
/// Sets whether similar path detection should be enabled during path stitching. Paths are similar
854822
/// if start and end node, and pre- and postconditions are the same. The presence of similar paths
@@ -878,6 +846,28 @@ impl<H: Clone> ForwardPartialPathStitcher<H> {
878846
pub fn set_max_work_per_phase(&mut self, max_work_per_phase: usize) {
879847
self.max_work_per_phase = max_work_per_phase;
880848
}
849+
}
850+
851+
impl<H: Clone> ForwardPartialPathStitcher<H> {
852+
/// Returns an iterator of all of the (possibly incomplete) partial paths that were encountered
853+
/// during the most recent phase of the algorithm.
854+
pub fn previous_phase_partial_paths(&self) -> impl Iterator<Item = &PartialPath> + '_ {
855+
self.next_iteration.0.iter()
856+
}
857+
858+
/// Returns a slice of all of the (possibly incomplete) partial paths that were encountered
859+
/// during the most recent phase of the algorithm.
860+
pub fn previous_phase_partial_paths_slice(&mut self) -> &[PartialPath] {
861+
self.next_iteration.0.make_contiguous();
862+
self.next_iteration.0.as_slices().0
863+
}
864+
865+
/// Returns a mutable slice of all of the (possibly incomplete) partial paths that were
866+
/// encountered during the most recent phase of the algorithm.
867+
pub fn previous_phase_partial_paths_slice_mut(&mut self) -> &mut [PartialPath] {
868+
self.next_iteration.0.make_contiguous();
869+
self.next_iteration.0.as_mut_slices().0
870+
}
881871

882872
/// Attempts to extend one partial path as part of the algorithm. When calling this function,
883873
/// you are responsible for ensuring that `db` already contains all of the possible appendables
@@ -1092,7 +1082,7 @@ impl ForwardPartialPathStitcher<Edge> {
10921082
graph: &StackGraph,
10931083
partials: &mut PartialPaths,
10941084
file: Handle<File>,
1095-
config: &StitcherConfig,
1085+
config: StitcherConfig,
10961086
cancellation_flag: &dyn CancellationFlag,
10971087
mut visit: F,
10981088
) -> Result<(), CancellationError>
@@ -1111,7 +1101,8 @@ impl ForwardPartialPathStitcher<Edge> {
11111101
.map(|node| PartialPath::from_node(graph, partials, node))
11121102
.collect::<Vec<_>>();
11131103
let mut stitcher =
1114-
ForwardPartialPathStitcher::from_partial_paths(graph, partials, initial_paths, config);
1104+
ForwardPartialPathStitcher::from_partial_paths(graph, partials, initial_paths);
1105+
config.apply(&mut stitcher);
11151106
stitcher.set_check_only_join_nodes(true);
11161107
while !stitcher.is_complete() {
11171108
cancellation_flag.check("finding complete partial paths")?;
@@ -1144,7 +1135,7 @@ impl<H: Clone> ForwardPartialPathStitcher<H> {
11441135
pub fn find_all_complete_partial_paths<I, F, A, Db, C, Err>(
11451136
candidates: &mut C,
11461137
starting_nodes: I,
1147-
config: &StitcherConfig,
1138+
config: StitcherConfig,
11481139
cancellation_flag: &dyn CancellationFlag,
11491140
mut visit: F,
11501141
) -> Result<(), Err>
@@ -1156,19 +1147,19 @@ impl<H: Clone> ForwardPartialPathStitcher<H> {
11561147
F: FnMut(&StackGraph, &mut PartialPaths, &PartialPath),
11571148
Err: std::convert::From<CancellationError>,
11581149
{
1159-
let mut stitcher = {
1160-
let (graph, partials, _) = candidates.get_graph_partials_and_db();
1161-
let initial_paths = starting_nodes
1162-
.into_iter()
1163-
.filter(|n| graph[*n].is_reference())
1164-
.map(|n| {
1165-
let mut p = PartialPath::from_node(graph, partials, n);
1166-
p.eliminate_precondition_stack_variables(partials);
1167-
p
1168-
})
1169-
.collect::<Vec<_>>();
1170-
ForwardPartialPathStitcher::from_partial_paths(graph, partials, initial_paths, config)
1171-
};
1150+
let (graph, partials, _) = candidates.get_graph_partials_and_db();
1151+
let initial_paths = starting_nodes
1152+
.into_iter()
1153+
.filter(|n| graph[*n].is_reference())
1154+
.map(|n| {
1155+
let mut p = PartialPath::from_node(graph, partials, n);
1156+
p.eliminate_precondition_stack_variables(partials);
1157+
p
1158+
})
1159+
.collect::<Vec<_>>();
1160+
let mut stitcher =
1161+
ForwardPartialPathStitcher::from_partial_paths(graph, partials, initial_paths);
1162+
config.apply(&mut stitcher);
11721163
stitcher.set_check_only_join_nodes(true);
11731164
while !stitcher.is_complete() {
11741165
cancellation_flag.check("finding complete partial paths")?;
@@ -1186,3 +1177,25 @@ impl<H: Clone> ForwardPartialPathStitcher<H> {
11861177
Ok(())
11871178
}
11881179
}
1180+
1181+
/// Configuration for partial path stitchers.
1182+
#[repr(C)]
1183+
#[derive(Clone, Copy, Debug)]
1184+
pub struct StitcherConfig {
1185+
/// Enables similar path detection during path stitching.
1186+
pub detect_similar_paths: bool,
1187+
}
1188+
1189+
impl StitcherConfig {
1190+
fn apply<H>(&self, stitcher: &mut ForwardPartialPathStitcher<H>) {
1191+
stitcher.set_similar_path_detection(self.detect_similar_paths);
1192+
}
1193+
}
1194+
1195+
impl Default for StitcherConfig {
1196+
fn default() -> Self {
1197+
Self {
1198+
detect_similar_paths: true,
1199+
}
1200+
}
1201+
}

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)