Skip to content

Usage example: Delete statements containing "print" using the XML engine

Gabin An edited this page Aug 30, 2019 · 1 revision
import os
from pyggi.base import Patch
from pyggi.tree import TreeProgram, XmlEngine
from pyggi.tree import StmtReplacement, StmtInsertion, StmtDeletion

class MyXmlEngine(XmlEngine):
    @classmethod
    def process_tree(cls, tree):
        stmt_tags = ['if', 'decl_stmt', 'expr_stmt', 'return', 'try']
        cls.select_tags(tree, keep=stmt_tags)

class MyTreeProgram(TreeProgram):
    def setup(self):
        if not os.path.exists(os.path.join(self.tmp_path, "Triangle.java.xml")):
            self.exec_cmd("srcml Triangle.java -o Triangle.java.xml")

    @classmethod
    def get_engine(cls, file_name):
        return MyXmlEngine

config = {
    "target_files": ["Triangle.java.xml"],
    "test_command": "./run.sh"
}

program = MyTreeProgram("[path_to_pyggi_repo]/sample/Triangle_bug_java", 
						config=config)

patch = Patch(program)
for i, mp in enumerate(program.modification_points["Triangle.java.xml"]):
	# print(i, mp)
	source = program.get_source("Triangle.java.xml", i)
	# print(source)
	if "print" in source:
		patch.add(StmtDeletion(("Triangle.java.xml", i)))
program.apply(patch)
print(program.diff(patch))
Clone this wiki locally