Skip to content

Commit

Permalink
added ability to merge analyses
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisTimperley committed Jul 13, 2024
1 parent e8e2c82 commit 403c0e2
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/kaskara/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ def with_relative_locations(self, base: str) -> Analysis:
insertions=self.insertions.with_relative_locations(base),
)

def merge(self, other: Analysis) -> Analysis:
"""Merges the results of this analysis and another analysis together."""
return Analysis(
files=self.files.union(other.files),
loops=self.loops.merge(other.loops),
functions=self.functions.merge(other.functions),
statements=self.statements.merge(other.statements),
insertions=self.insertions.merge(other.insertions),
)

def is_inside_loop(self, location: FileLocation) -> bool:
return self.loops.is_within_loop(location)

Expand Down
8 changes: 8 additions & 0 deletions src/kaskara/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
__all__ = ("Function", "ProgramFunctions")

import abc
import itertools
import os
import typing as t
from dataclasses import dataclass
Expand Down Expand Up @@ -82,6 +83,13 @@ def to_dict(self) -> dict[str, t.Any]:
"functions": [f.to_dict() for f in self],
}

def merge(self, other: ProgramFunctions) -> ProgramFunctions:
"""Merges the results of this analysis and another analysis together."""
return self.from_functions(
project_directory=self._project_directory,
functions=itertools.chain(self, other),
)

def with_relative_locations(self, base: str) -> ProgramFunctions:
"""Creates a new instance with relative file locations."""
functions = [f.with_relative_locations(base) for f in self]
Expand Down
3 changes: 3 additions & 0 deletions src/kaskara/insertions.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ def __init__(self, contents: list[InsertionPoint]) -> None:
self.__file_insertions[filename] = []
self.__file_insertions[filename].append(ins)

def merge(self, other: ProgramInsertionPoints) -> ProgramInsertionPoints:
return ProgramInsertionPoints(list(self) + list(other))

def with_relative_locations(self, base: str) -> ProgramInsertionPoints:
return ProgramInsertionPoints([
insertion_point.with_relative_location(base)
Expand Down
9 changes: 9 additions & 0 deletions src/kaskara/loops.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ def from_body_location_ranges(
FileLocationRangeSet(bodies),
)

def merge(self, other: ProgramLoops) -> ProgramLoops:
covered_by_loop_bodies = self._covered_by_loop_bodies.union(
other._covered_by_loop_bodies,
)
return ProgramLoops(
_covered_by_loop_bodies=covered_by_loop_bodies,
_project_directory=self._project_directory,
)

def with_relative_locations(self, base: str) -> ProgramLoops:
"""Creates a new instance with relative file locations."""
covered_by_loop_bodies = self._covered_by_loop_bodies.with_relative_locations(
Expand Down
7 changes: 7 additions & 0 deletions src/kaskara/statements.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
__all__ = ("Statement", "ProgramStatements")

import abc
import itertools
import os
import typing as t
from dataclasses import dataclass, field
Expand Down Expand Up @@ -97,6 +98,12 @@ def __post_init__(self) -> None:
)
logger.debug(f"indexed statements by file:\n{summary}")

def merge(self, other: ProgramStatements) -> ProgramStatements:
return self.build(
project_directory=self._project_directory,
statements=itertools.chain(self, other),
)

def with_relative_locations(self, base: str) -> ProgramStatements:
"""Creates a new instance with relative file locations."""
return self.build(
Expand Down

0 comments on commit 403c0e2

Please sign in to comment.