2021-03-22 15:49:18 +00:00
|
|
|
import os
|
|
|
|
|
import libadalang as lal
|
2022-02-02 09:34:13 +00:00
|
|
|
from ada_reducer.types import Buffer, infer_or_equal
|
|
|
|
|
from ada_reducer.interfaces import ChunkInterface, StrategyInterface
|
|
|
|
|
from ada_reducer.dichotomy import to_tree, dichototree
|
2021-03-22 15:49:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class RemoveSubprogram(ChunkInterface):
|
2021-03-25 09:40:11 +00:00
|
|
|
def __init__(self, file, node, buffers):
|
2021-03-22 15:49:18 +00:00
|
|
|
self.file = file
|
|
|
|
|
self.node = node
|
|
|
|
|
self.buffers = buffers
|
|
|
|
|
|
|
|
|
|
# Resolve node
|
|
|
|
|
self.decl = node.p_decl_part()
|
|
|
|
|
self.other_file = None
|
|
|
|
|
|
|
|
|
|
def do(self):
|
|
|
|
|
num_lines = self.node.sloc_range.end.line - self.node.sloc_range.start.line + 1
|
|
|
|
|
new_text = [""] * num_lines
|
|
|
|
|
self.body_range, self.body_lines = self.buffers[self.file].replace(
|
|
|
|
|
self.node.sloc_range, new_text
|
|
|
|
|
)
|
|
|
|
|
if self.decl is None or not os.path.exists(self.decl.unit.filename):
|
|
|
|
|
return
|
|
|
|
|
self.other_file = self.decl.unit.filename
|
|
|
|
|
if self.other_file not in self.buffers:
|
|
|
|
|
self.buffers[self.other_file] = Buffer(self.other_file)
|
|
|
|
|
|
|
|
|
|
num_lines = self.decl.sloc_range.end.line - self.decl.sloc_range.start.line + 1
|
|
|
|
|
new_text = [""] * num_lines
|
|
|
|
|
self.spec_range, self.spec_lines = self.buffers[self.other_file].replace(
|
|
|
|
|
self.decl.sloc_range, new_text
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def undo(self):
|
|
|
|
|
self.buffers[self.file].replace(self.body_range, self.body_lines)
|
|
|
|
|
if self.other_file is not None:
|
|
|
|
|
self.buffers[self.other_file].replace(self.spec_range, self.spec_lines)
|
|
|
|
|
|
|
|
|
|
def is_in(self, other):
|
|
|
|
|
return infer_or_equal(
|
|
|
|
|
other.node.sloc_range.start, self.node.sloc_range.start
|
|
|
|
|
) and infer_or_equal(self.node.sloc_range.end, other.node.sloc_range.end)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class RemoveSubprograms(StrategyInterface):
|
|
|
|
|
""" Remove subprograms """
|
|
|
|
|
|
2021-03-24 22:03:50 +00:00
|
|
|
def save(self):
|
2021-03-22 15:49:18 +00:00
|
|
|
for file in self.buffers:
|
|
|
|
|
self.buffers[file].save()
|
|
|
|
|
|
|
|
|
|
def run_on_file(self, context, file, predicate):
|
|
|
|
|
self.context = context
|
|
|
|
|
|
|
|
|
|
self.buffers = {file: Buffer(file)}
|
2021-03-25 09:40:11 +00:00
|
|
|
unit = self.context.get_from_file(file)
|
|
|
|
|
|
|
|
|
|
if unit.root is None:
|
|
|
|
|
return
|
2021-03-22 15:49:18 +00:00
|
|
|
|
|
|
|
|
# List all subprograms in the file
|
|
|
|
|
|
|
|
|
|
chunks = []
|
2021-03-25 09:40:11 +00:00
|
|
|
for subp in unit.root.findall(
|
2021-03-24 15:07:50 +00:00
|
|
|
lambda x: x.is_a(lal.SubpBody) or x.is_a(lal.ExprFunction)
|
|
|
|
|
):
|
2021-03-22 15:49:18 +00:00
|
|
|
# Create a chunk for each subprogram
|
2021-03-25 09:40:11 +00:00
|
|
|
chunks.append(RemoveSubprogram(file, subp, self.buffers))
|
2021-03-22 15:49:18 +00:00
|
|
|
|
|
|
|
|
t = to_tree(chunks)
|
2021-03-24 22:03:50 +00:00
|
|
|
return dichototree(t, predicate, self.save)
|