From ed90b1f665fc894c4672ccca260a7bc0ac1ba602 Mon Sep 17 00:00:00 2001 From: Carlos Galindo Date: Mon, 1 Jun 2020 19:04:03 +0200 Subject: [PATCH 01/10] Moved the project to a module and created sdg-cli module --- .gitignore | 174 +++++++++++++++++- .gitmodules | 6 +- .idea/.gitignore | 3 + sdg-cli/pom.xml | 28 +++ .../src/main/java/tfm/cli}/CFGLog.java | 2 +- .../src/main/java/tfm/cli}/GraphLog.java | 2 +- .../src/main/java/tfm/cli}/Main.java | 8 +- .../main/java/tfm/cli}/MethodResolver.java | 4 +- .../src/main/java/tfm/cli}/PDGLog.java | 2 +- .../src/main/java/tfm/cli}/SDGLog.java | 2 +- {cfg => sdg-core/cfg}/Eval_1_main.ff3 | Bin pom.xml => sdg-core/pom.xml | 28 +-- .../src}/main/java/tfm/arcs/Arc.java | 0 .../java/tfm/arcs/cfg/ControlFlowArc.java | 0 .../tfm/arcs/pdg/ControlDependencyArc.java | 0 .../java/tfm/arcs/pdg/DataDependencyArc.java | 0 .../src}/main/java/tfm/arcs/sdg/CallArc.java | 0 .../java/tfm/arcs/sdg/ParameterInOutArc.java | 0 .../main/java/tfm/arcs/sdg/SummaryArc.java | 0 .../src}/main/java/tfm/graphs/Buildable.java | 0 .../src}/main/java/tfm/graphs/Graph.java | 3 +- .../java/tfm/graphs/GraphWithRootNode.java | 0 .../src}/main/java/tfm/graphs/Sliceable.java | 0 .../main/java/tfm/graphs/augmented/ACFG.java | 0 .../tfm/graphs/augmented/ACFGBuilder.java | 0 .../main/java/tfm/graphs/augmented/APDG.java | 0 .../main/java/tfm/graphs/augmented/PPDG.java | 0 .../src}/main/java/tfm/graphs/cfg/CFG.java | 0 .../main/java/tfm/graphs/cfg/CFGBuilder.java | 0 .../graphs/pdg/ControlDependencyBuilder.java | 0 .../tfm/graphs/pdg/DataDependencyBuilder.java | 0 .../src}/main/java/tfm/graphs/pdg/PDG.java | 0 .../main/java/tfm/graphs/pdg/PDGBuilder.java | 0 .../tfm/graphs/sdg/MethodCallReplacer.java | 5 - .../graphs/sdg/MethodCallReplacerVisitor.java | 0 .../graphs/sdg/OutNodeVariableVisitor.java | 0 .../src}/main/java/tfm/graphs/sdg/SDG.java | 0 .../main/java/tfm/graphs/sdg/SDGBuilder.java | 0 .../sdg/sumarcs/NaiveSummaryArcsBuilder.java | 0 .../sdg/sumarcs/SummaryArcsBuilder.java | 0 .../src}/main/java/tfm/nodes/GraphNode.java | 0 .../src}/main/java/tfm/nodes/IdHelper.java | 0 .../src}/main/java/tfm/nodes/NodeFactory.java | 1 - .../main/java/tfm/nodes/TypeNodeFactory.java | 0 .../main/java/tfm/nodes/type/NodeType.java | 0 .../java/tfm/slicing/GraphNodeCriterion.java | 0 .../java/tfm/slicing/LineNumberCriterion.java | 0 .../src}/main/java/tfm/slicing/Slice.java | 0 .../java/tfm/slicing/SliceAstVisitor.java | 0 .../src}/main/java/tfm/slicing/Sliceable.java | 0 .../java/tfm/slicing/SlicingCriterion.java | 0 .../src}/main/java/tfm/utils/ASTUtils.java | 1 - .../src}/main/java/tfm/utils/Context.java | 0 .../src}/main/java/tfm/utils/FileUtil.java | 0 .../src}/main/java/tfm/utils/Logger.java | 0 .../tfm/utils/MethodDeclarationSolver.java | 0 .../java/tfm/utils/NodeNotFoundException.java | 0 .../src}/main/java/tfm/utils/Utils.java | 0 .../java/tfm/variables/VariableExtractor.java | 1 - .../java/tfm/variables/VariableVisitor.java | 2 - .../tfm/variables/actions/VariableAction.java | 0 .../actions/VariableDeclaration.java | 0 .../variables/actions/VariableDefinition.java | 0 .../tfm/variables/actions/VariableUse.java | 0 .../tfm/graphs/pdg/HandCraftedGraphs.java | 0 .../test/java/tfm/graphs/pdg/PDGTests.java | 4 +- .../src}/test/java/tfm/utils/FileFinder.java | 0 .../src}/test/res/carlos/Problem1.java | 0 .../src}/test/res/carlos/Problem2.java | 0 .../src}/test/res/carlos/Problem3.java | 0 .../src}/test/res/carlos/Test1.java | 0 .../src}/test/res/java-slicing-benchmarks | 0 .../src}/test/res/ltd-samples/BasicBreak.java | 0 .../test/res/ltd-samples/BasicContinue.java | 0 .../test/res/ltd-samples/BasicForeach.java | 0 .../src}/test/res/ltd-samples/BasicIf.java | 0 .../test/res/ltd-samples/BasicIfElse.java | 0 .../test/res/ltd-samples/BasicSwitch.java | 0 .../res/ltd-samples/BasicSwitchDefault.java | 0 .../res/ltd-samples/BasicSwitchNoBreak.java | 0 .../src}/test/res/ltd-samples/Bucles_1.java | 0 .../src}/test/res/ltd-samples/Bucles_2.java | 0 .../src}/test/res/ltd-samples/Bucles_3.java | 0 .../src}/test/res/ltd-samples/Bucles_4.java | 0 .../src}/test/res/ltd-samples/Bucles_5.java | 0 .../src}/test/res/ltd-samples/Bucles_6.java | 0 .../test/res/ltd-samples/Bucles_Josep.java | 0 .../src}/test/res/ltd-samples/ReturnTest.java | 0 .../src}/test/res/ltd-samples/Test_1.java | 0 .../src}/test/res/ltd-samples/Test_2.java | 0 .../src}/test/res/ltd-samples/Test_3.java | 0 .../src}/test/res/ltd-samples/Test_4.java | 0 .../src}/test/res/ltd-samples/Test_5.java | 0 .../src}/test/res/ltd-samples/Test_6.java | 0 .../src}/test/res/ltd-samples/Test_7.java | 0 .../src}/test/res/ltd-samples/Test_8.java | 0 .../src}/test/res/ltd-samples/Test_9.java | 0 .../res/papers/Example1_Horwitz_PPDG.java | 0 .../src}/test/res/programs/WhileLoop.java | 0 .../src}/test/res/programs/cfg/CFG_Test1.java | 0 .../src}/test/res/programs/cfg/CFG_Test2.java | 0 .../src}/test/res/programs/cfg/CFG_Test3.java | 0 .../src}/test/res/programs/cfg/CFG_Test4.java | 0 .../src}/test/res/programs/cfg/CFG_Test5.java | 0 .../src}/test/res/programs/cfg/Eval_1.java | 0 .../src}/test/res/programs/cfg/Eval_2.java | 0 .../src}/test/res/programs/cfg/Eval_3.java | 0 .../src}/test/res/programs/cfg/Eval_4.java | 0 .../src}/test/res/programs/pdg/Example1.java | 0 .../src}/test/res/programs/pdg/Example2.java | 0 .../src}/test/res/programs/pdg/Example3.java | 0 .../src}/test/res/programs/pdg/Test.java | 0 .../src}/test/res/programs/sdg/Example1.java | 0 .../src}/test/res/programs/sdg/Example2.java | 0 114 files changed, 230 insertions(+), 46 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 sdg-cli/pom.xml rename {src/main/java/tfm/exec => sdg-cli/src/main/java/tfm/cli}/CFGLog.java (90%) rename {src/main/java/tfm/exec => sdg-cli/src/main/java/tfm/cli}/GraphLog.java (99%) rename {src/main/java/tfm/exec => sdg-cli/src/main/java/tfm/cli}/Main.java (92%) rename {src/main/java/tfm/exec => sdg-cli/src/main/java/tfm/cli}/MethodResolver.java (97%) rename {src/main/java/tfm/exec => sdg-cli/src/main/java/tfm/cli}/PDGLog.java (98%) rename {src/main/java/tfm/exec => sdg-cli/src/main/java/tfm/cli}/SDGLog.java (90%) rename {cfg => sdg-core/cfg}/Eval_1_main.ff3 (100%) rename pom.xml => sdg-core/pom.xml (76%) rename {src => sdg-core/src}/main/java/tfm/arcs/Arc.java (100%) rename {src => sdg-core/src}/main/java/tfm/arcs/cfg/ControlFlowArc.java (100%) rename {src => sdg-core/src}/main/java/tfm/arcs/pdg/ControlDependencyArc.java (100%) rename {src => sdg-core/src}/main/java/tfm/arcs/pdg/DataDependencyArc.java (100%) rename {src => sdg-core/src}/main/java/tfm/arcs/sdg/CallArc.java (100%) rename {src => sdg-core/src}/main/java/tfm/arcs/sdg/ParameterInOutArc.java (100%) rename {src => sdg-core/src}/main/java/tfm/arcs/sdg/SummaryArc.java (100%) rename {src => sdg-core/src}/main/java/tfm/graphs/Buildable.java (100%) rename {src => sdg-core/src}/main/java/tfm/graphs/Graph.java (97%) rename {src => sdg-core/src}/main/java/tfm/graphs/GraphWithRootNode.java (100%) rename {src => sdg-core/src}/main/java/tfm/graphs/Sliceable.java (100%) rename {src => sdg-core/src}/main/java/tfm/graphs/augmented/ACFG.java (100%) rename {src => sdg-core/src}/main/java/tfm/graphs/augmented/ACFGBuilder.java (100%) rename {src => sdg-core/src}/main/java/tfm/graphs/augmented/APDG.java (100%) rename {src => sdg-core/src}/main/java/tfm/graphs/augmented/PPDG.java (100%) rename {src => sdg-core/src}/main/java/tfm/graphs/cfg/CFG.java (100%) rename {src => sdg-core/src}/main/java/tfm/graphs/cfg/CFGBuilder.java (100%) rename {src => sdg-core/src}/main/java/tfm/graphs/pdg/ControlDependencyBuilder.java (100%) rename {src => sdg-core/src}/main/java/tfm/graphs/pdg/DataDependencyBuilder.java (100%) rename {src => sdg-core/src}/main/java/tfm/graphs/pdg/PDG.java (100%) rename {src => sdg-core/src}/main/java/tfm/graphs/pdg/PDGBuilder.java (100%) rename {src => sdg-core/src}/main/java/tfm/graphs/sdg/MethodCallReplacer.java (79%) rename {src => sdg-core/src}/main/java/tfm/graphs/sdg/MethodCallReplacerVisitor.java (100%) rename {src => sdg-core/src}/main/java/tfm/graphs/sdg/OutNodeVariableVisitor.java (100%) rename {src => sdg-core/src}/main/java/tfm/graphs/sdg/SDG.java (100%) rename {src => sdg-core/src}/main/java/tfm/graphs/sdg/SDGBuilder.java (100%) rename {src => sdg-core/src}/main/java/tfm/graphs/sdg/sumarcs/NaiveSummaryArcsBuilder.java (100%) rename {src => sdg-core/src}/main/java/tfm/graphs/sdg/sumarcs/SummaryArcsBuilder.java (100%) rename {src => sdg-core/src}/main/java/tfm/nodes/GraphNode.java (100%) rename {src => sdg-core/src}/main/java/tfm/nodes/IdHelper.java (100%) rename {src => sdg-core/src}/main/java/tfm/nodes/NodeFactory.java (98%) rename {src => sdg-core/src}/main/java/tfm/nodes/TypeNodeFactory.java (100%) rename {src => sdg-core/src}/main/java/tfm/nodes/type/NodeType.java (100%) rename {src => sdg-core/src}/main/java/tfm/slicing/GraphNodeCriterion.java (100%) rename {src => sdg-core/src}/main/java/tfm/slicing/LineNumberCriterion.java (100%) rename {src => sdg-core/src}/main/java/tfm/slicing/Slice.java (100%) rename {src => sdg-core/src}/main/java/tfm/slicing/SliceAstVisitor.java (100%) rename {src => sdg-core/src}/main/java/tfm/slicing/Sliceable.java (100%) rename {src => sdg-core/src}/main/java/tfm/slicing/SlicingCriterion.java (100%) rename {src => sdg-core/src}/main/java/tfm/utils/ASTUtils.java (98%) rename {src => sdg-core/src}/main/java/tfm/utils/Context.java (100%) rename {src => sdg-core/src}/main/java/tfm/utils/FileUtil.java (100%) rename {src => sdg-core/src}/main/java/tfm/utils/Logger.java (100%) rename {src => sdg-core/src}/main/java/tfm/utils/MethodDeclarationSolver.java (100%) rename {src => sdg-core/src}/main/java/tfm/utils/NodeNotFoundException.java (100%) rename {src => sdg-core/src}/main/java/tfm/utils/Utils.java (100%) rename {src => sdg-core/src}/main/java/tfm/variables/VariableExtractor.java (97%) rename {src => sdg-core/src}/main/java/tfm/variables/VariableVisitor.java (99%) rename {src => sdg-core/src}/main/java/tfm/variables/actions/VariableAction.java (100%) rename {src => sdg-core/src}/main/java/tfm/variables/actions/VariableDeclaration.java (100%) rename {src => sdg-core/src}/main/java/tfm/variables/actions/VariableDefinition.java (100%) rename {src => sdg-core/src}/main/java/tfm/variables/actions/VariableUse.java (100%) rename {src => sdg-core/src}/test/java/tfm/graphs/pdg/HandCraftedGraphs.java (100%) rename {src => sdg-core/src}/test/java/tfm/graphs/pdg/PDGTests.java (99%) rename {src => sdg-core/src}/test/java/tfm/utils/FileFinder.java (100%) rename {src => sdg-core/src}/test/res/carlos/Problem1.java (100%) rename {src => sdg-core/src}/test/res/carlos/Problem2.java (100%) rename {src => sdg-core/src}/test/res/carlos/Problem3.java (100%) rename {src => sdg-core/src}/test/res/carlos/Test1.java (100%) rename {src => sdg-core/src}/test/res/java-slicing-benchmarks (100%) rename {src => sdg-core/src}/test/res/ltd-samples/BasicBreak.java (100%) rename {src => sdg-core/src}/test/res/ltd-samples/BasicContinue.java (100%) rename {src => sdg-core/src}/test/res/ltd-samples/BasicForeach.java (100%) rename {src => sdg-core/src}/test/res/ltd-samples/BasicIf.java (100%) rename {src => sdg-core/src}/test/res/ltd-samples/BasicIfElse.java (100%) rename {src => sdg-core/src}/test/res/ltd-samples/BasicSwitch.java (100%) rename {src => sdg-core/src}/test/res/ltd-samples/BasicSwitchDefault.java (100%) rename {src => sdg-core/src}/test/res/ltd-samples/BasicSwitchNoBreak.java (100%) rename {src => sdg-core/src}/test/res/ltd-samples/Bucles_1.java (100%) rename {src => sdg-core/src}/test/res/ltd-samples/Bucles_2.java (100%) rename {src => sdg-core/src}/test/res/ltd-samples/Bucles_3.java (100%) rename {src => sdg-core/src}/test/res/ltd-samples/Bucles_4.java (100%) rename {src => sdg-core/src}/test/res/ltd-samples/Bucles_5.java (100%) rename {src => sdg-core/src}/test/res/ltd-samples/Bucles_6.java (100%) rename {src => sdg-core/src}/test/res/ltd-samples/Bucles_Josep.java (100%) rename {src => sdg-core/src}/test/res/ltd-samples/ReturnTest.java (100%) rename {src => sdg-core/src}/test/res/ltd-samples/Test_1.java (100%) rename {src => sdg-core/src}/test/res/ltd-samples/Test_2.java (100%) rename {src => sdg-core/src}/test/res/ltd-samples/Test_3.java (100%) rename {src => sdg-core/src}/test/res/ltd-samples/Test_4.java (100%) rename {src => sdg-core/src}/test/res/ltd-samples/Test_5.java (100%) rename {src => sdg-core/src}/test/res/ltd-samples/Test_6.java (100%) rename {src => sdg-core/src}/test/res/ltd-samples/Test_7.java (100%) rename {src => sdg-core/src}/test/res/ltd-samples/Test_8.java (100%) rename {src => sdg-core/src}/test/res/ltd-samples/Test_9.java (100%) rename {src => sdg-core/src}/test/res/papers/Example1_Horwitz_PPDG.java (100%) rename {src => sdg-core/src}/test/res/programs/WhileLoop.java (100%) rename {src => sdg-core/src}/test/res/programs/cfg/CFG_Test1.java (100%) rename {src => sdg-core/src}/test/res/programs/cfg/CFG_Test2.java (100%) rename {src => sdg-core/src}/test/res/programs/cfg/CFG_Test3.java (100%) rename {src => sdg-core/src}/test/res/programs/cfg/CFG_Test4.java (100%) rename {src => sdg-core/src}/test/res/programs/cfg/CFG_Test5.java (100%) rename {src => sdg-core/src}/test/res/programs/cfg/Eval_1.java (100%) rename {src => sdg-core/src}/test/res/programs/cfg/Eval_2.java (100%) rename {src => sdg-core/src}/test/res/programs/cfg/Eval_3.java (100%) rename {src => sdg-core/src}/test/res/programs/cfg/Eval_4.java (100%) rename {src => sdg-core/src}/test/res/programs/pdg/Example1.java (100%) rename {src => sdg-core/src}/test/res/programs/pdg/Example2.java (100%) rename {src => sdg-core/src}/test/res/programs/pdg/Example3.java (100%) rename {src => sdg-core/src}/test/res/programs/pdg/Test.java (100%) rename {src => sdg-core/src}/test/res/programs/sdg/Example1.java (100%) rename {src => sdg-core/src}/test/res/programs/sdg/Example2.java (100%) diff --git a/.gitignore b/.gitignore index d627c53..1fab45f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,174 @@ +# Created by https://www.gitignore.io/api/java,linux,macos,maven,intellij +# Edit at https://www.gitignore.io/?templates=java,linux,macos,maven,intellij + +### Intellij ### +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/**/usage.statistics.xml +.idea/**/dictionaries +.idea/**/shelf + +# Generated files +.idea/**/contentModel.xml + +# Sensitive or high-churn files +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml +.idea/**/dbnavigator.xml + +# Gradle +.idea/**/gradle.xml +.idea/**/libraries + +# Gradle and Maven with auto-import +# When using Gradle or Maven with auto-import, you should exclude module files, +# since they will be recreated, and may cause churn. Uncomment if using +# auto-import. +.idea/modules.xml +.idea/*.iml +.idea/modules *.iml -.idea/ -target/ +*.ipr + +# CMake +cmake-build-*/ + +# Mongo Explorer plugin +.idea/**/mongoSettings.xml + +# File-based project format +*.iws + +# IntelliJ out/ -.settings + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +# Editor-based Rest Client +.idea/httpRequests + +# Android studio 3.1+ serialized cache file +.idea/caches/build_file_checksums.ser + +### Intellij Patch ### +# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721 + +# *.iml +# modules.xml +# .idea/misc.xml +# *.ipr + +# Sonarlint plugin +.idea/**/sonarlint/ + +# SonarQube Plugin +.idea/**/sonarIssues.xml + +# Markdown Navigator plugin +.idea/**/markdown-navigator.xml +.idea/**/markdown-navigator/ + +### Java ### +# Compiled class file +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.nar +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +### Linux ### +*~ + +# temporary files which can be created if a process still has a handle open of a deleted file +.fuse_hidden* + +# KDE directory preferences +.directory + +# Linux trash folder which might appear on any partition or disk +.Trash-* + +# .nfs files are created when an open file is removed but is still being accessed +.nfs* + +### macOS ### +# General +.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + +### Maven ### +target/ +pom.xml.tag +pom.xml.releaseBackup +pom.xml.versionsBackup +pom.xml.next +release.properties +dependency-reduced-pom.xml +buildNumber.properties +.mvn/timing.properties +.mvn/wrapper/maven-wrapper.jar +.flattened-pom.xml + +# End of https://www.gitignore.io/api/java,linux,macos,maven,intellij .attach* diff --git a/.gitmodules b/.gitmodules index 46394f3..ee5ecc4 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,3 @@ -[submodule "src/test/res/java-slicing-benchmarks"] - path = src/test/res/java-slicing-benchmarks - url = kaz:repos/java-slicing-benchmarks.git +[submodule "sdg-core/src/test/res/java-slicing-benchmarks"] + path = sdg-core/src/test/res/java-slicing-benchmarks + url = https://kaz.dsic.upv.es/git/program-slicing/java-benchmarks.git diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/sdg-cli/pom.xml b/sdg-cli/pom.xml new file mode 100644 index 0000000..c3aaed0 --- /dev/null +++ b/sdg-cli/pom.xml @@ -0,0 +1,28 @@ + + + 4.0.0 + + tfm + sdg-cli + 1.0-SNAPSHOT + + + 11 + 11 + + + + + commons-cli + commons-cli + 1.4 + + + tfm + sdg-core + 1.0-SNAPSHOT + + + diff --git a/src/main/java/tfm/exec/CFGLog.java b/sdg-cli/src/main/java/tfm/cli/CFGLog.java similarity index 90% rename from src/main/java/tfm/exec/CFGLog.java rename to sdg-cli/src/main/java/tfm/cli/CFGLog.java index 3969535..af8cf10 100644 --- a/src/main/java/tfm/exec/CFGLog.java +++ b/sdg-cli/src/main/java/tfm/cli/CFGLog.java @@ -1,4 +1,4 @@ -package tfm.exec; +package tfm.cli; import tfm.graphs.cfg.CFG; diff --git a/src/main/java/tfm/exec/GraphLog.java b/sdg-cli/src/main/java/tfm/cli/GraphLog.java similarity index 99% rename from src/main/java/tfm/exec/GraphLog.java rename to sdg-cli/src/main/java/tfm/cli/GraphLog.java index 0fc1fe2..baaa4c8 100644 --- a/src/main/java/tfm/exec/GraphLog.java +++ b/sdg-cli/src/main/java/tfm/cli/GraphLog.java @@ -1,4 +1,4 @@ -package tfm.exec; +package tfm.cli; import tfm.graphs.Graph; import tfm.utils.FileUtil; diff --git a/src/main/java/tfm/exec/Main.java b/sdg-cli/src/main/java/tfm/cli/Main.java similarity index 92% rename from src/main/java/tfm/exec/Main.java rename to sdg-cli/src/main/java/tfm/cli/Main.java index 6d7a54e..85a9e62 100644 --- a/src/main/java/tfm/exec/Main.java +++ b/sdg-cli/src/main/java/tfm/cli/Main.java @@ -1,15 +1,11 @@ -package tfm.exec; +package tfm.cli; import com.github.javaparser.JavaParser; import com.github.javaparser.ast.Node; import com.github.javaparser.ast.NodeList; import com.github.javaparser.ast.body.MethodDeclaration; -import com.github.javaparser.ast.type.Type; -import com.github.javaparser.ast.visitor.GenericVisitor; -import com.github.javaparser.ast.visitor.VoidVisitor; -import com.github.javaparser.resolution.types.ResolvedType; -import tfm.graphs.cfg.CFG; import tfm.graphs.Graph; +import tfm.graphs.cfg.CFG; import tfm.graphs.pdg.PDG; import tfm.graphs.sdg.SDG; import tfm.utils.Logger; diff --git a/src/main/java/tfm/exec/MethodResolver.java b/sdg-cli/src/main/java/tfm/cli/MethodResolver.java similarity index 97% rename from src/main/java/tfm/exec/MethodResolver.java rename to sdg-cli/src/main/java/tfm/cli/MethodResolver.java index e2d28d6..8d88d63 100644 --- a/src/main/java/tfm/exec/MethodResolver.java +++ b/sdg-cli/src/main/java/tfm/cli/MethodResolver.java @@ -1,9 +1,8 @@ -package tfm.exec; +package tfm.cli; import com.github.javaparser.JavaParser; import com.github.javaparser.ast.CompilationUnit; import com.github.javaparser.ast.NodeList; -import com.github.javaparser.ast.body.AnnotationDeclaration; import com.github.javaparser.ast.body.MethodDeclaration; import com.github.javaparser.ast.expr.MethodCallExpr; import com.github.javaparser.ast.visitor.VoidVisitorAdapter; @@ -17,7 +16,6 @@ import com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeS import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver; import tfm.graphs.sdg.SDG; import tfm.nodes.GraphNode; -import tfm.utils.Context; import tfm.utils.Logger; import java.io.File; diff --git a/src/main/java/tfm/exec/PDGLog.java b/sdg-cli/src/main/java/tfm/cli/PDGLog.java similarity index 98% rename from src/main/java/tfm/exec/PDGLog.java rename to sdg-cli/src/main/java/tfm/cli/PDGLog.java index af36803..a50a51c 100644 --- a/src/main/java/tfm/exec/PDGLog.java +++ b/sdg-cli/src/main/java/tfm/cli/PDGLog.java @@ -1,4 +1,4 @@ -package tfm.exec; +package tfm.cli; import tfm.graphs.pdg.PDG; import tfm.nodes.GraphNode; diff --git a/src/main/java/tfm/exec/SDGLog.java b/sdg-cli/src/main/java/tfm/cli/SDGLog.java similarity index 90% rename from src/main/java/tfm/exec/SDGLog.java rename to sdg-cli/src/main/java/tfm/cli/SDGLog.java index cdeae27..6c8e3e9 100644 --- a/src/main/java/tfm/exec/SDGLog.java +++ b/sdg-cli/src/main/java/tfm/cli/SDGLog.java @@ -1,4 +1,4 @@ -package tfm.exec; +package tfm.cli; import tfm.graphs.sdg.SDG; diff --git a/cfg/Eval_1_main.ff3 b/sdg-core/cfg/Eval_1_main.ff3 similarity index 100% rename from cfg/Eval_1_main.ff3 rename to sdg-core/cfg/Eval_1_main.ff3 diff --git a/pom.xml b/sdg-core/pom.xml similarity index 76% rename from pom.xml rename to sdg-core/pom.xml index 44ab23e..e0b4d31 100644 --- a/pom.xml +++ b/sdg-core/pom.xml @@ -5,20 +5,13 @@ 4.0.0 tfm - tfm + sdg-core 1.0-SNAPSHOT - - - - org.apache.maven.plugins - maven-compiler-plugin - - 8 - 8 - - - - + + + 11 + 11 + @@ -52,5 +45,12 @@ RELEASE test + + + tfm + sdg-cli + 1.0-SNAPSHOT + test + - \ No newline at end of file + diff --git a/src/main/java/tfm/arcs/Arc.java b/sdg-core/src/main/java/tfm/arcs/Arc.java similarity index 100% rename from src/main/java/tfm/arcs/Arc.java rename to sdg-core/src/main/java/tfm/arcs/Arc.java diff --git a/src/main/java/tfm/arcs/cfg/ControlFlowArc.java b/sdg-core/src/main/java/tfm/arcs/cfg/ControlFlowArc.java similarity index 100% rename from src/main/java/tfm/arcs/cfg/ControlFlowArc.java rename to sdg-core/src/main/java/tfm/arcs/cfg/ControlFlowArc.java diff --git a/src/main/java/tfm/arcs/pdg/ControlDependencyArc.java b/sdg-core/src/main/java/tfm/arcs/pdg/ControlDependencyArc.java similarity index 100% rename from src/main/java/tfm/arcs/pdg/ControlDependencyArc.java rename to sdg-core/src/main/java/tfm/arcs/pdg/ControlDependencyArc.java diff --git a/src/main/java/tfm/arcs/pdg/DataDependencyArc.java b/sdg-core/src/main/java/tfm/arcs/pdg/DataDependencyArc.java similarity index 100% rename from src/main/java/tfm/arcs/pdg/DataDependencyArc.java rename to sdg-core/src/main/java/tfm/arcs/pdg/DataDependencyArc.java diff --git a/src/main/java/tfm/arcs/sdg/CallArc.java b/sdg-core/src/main/java/tfm/arcs/sdg/CallArc.java similarity index 100% rename from src/main/java/tfm/arcs/sdg/CallArc.java rename to sdg-core/src/main/java/tfm/arcs/sdg/CallArc.java diff --git a/src/main/java/tfm/arcs/sdg/ParameterInOutArc.java b/sdg-core/src/main/java/tfm/arcs/sdg/ParameterInOutArc.java similarity index 100% rename from src/main/java/tfm/arcs/sdg/ParameterInOutArc.java rename to sdg-core/src/main/java/tfm/arcs/sdg/ParameterInOutArc.java diff --git a/src/main/java/tfm/arcs/sdg/SummaryArc.java b/sdg-core/src/main/java/tfm/arcs/sdg/SummaryArc.java similarity index 100% rename from src/main/java/tfm/arcs/sdg/SummaryArc.java rename to sdg-core/src/main/java/tfm/arcs/sdg/SummaryArc.java diff --git a/src/main/java/tfm/graphs/Buildable.java b/sdg-core/src/main/java/tfm/graphs/Buildable.java similarity index 100% rename from src/main/java/tfm/graphs/Buildable.java rename to sdg-core/src/main/java/tfm/graphs/Buildable.java diff --git a/src/main/java/tfm/graphs/Graph.java b/sdg-core/src/main/java/tfm/graphs/Graph.java similarity index 97% rename from src/main/java/tfm/graphs/Graph.java rename to sdg-core/src/main/java/tfm/graphs/Graph.java index 9326553..bb0fab6 100644 --- a/src/main/java/tfm/graphs/Graph.java +++ b/sdg-core/src/main/java/tfm/graphs/Graph.java @@ -9,7 +9,8 @@ import tfm.nodes.GraphNode; import tfm.nodes.NodeFactory; import tfm.utils.ASTUtils; -import java.util.*; +import java.util.Objects; +import java.util.Optional; import java.util.stream.Collectors; /** diff --git a/src/main/java/tfm/graphs/GraphWithRootNode.java b/sdg-core/src/main/java/tfm/graphs/GraphWithRootNode.java similarity index 100% rename from src/main/java/tfm/graphs/GraphWithRootNode.java rename to sdg-core/src/main/java/tfm/graphs/GraphWithRootNode.java diff --git a/src/main/java/tfm/graphs/Sliceable.java b/sdg-core/src/main/java/tfm/graphs/Sliceable.java similarity index 100% rename from src/main/java/tfm/graphs/Sliceable.java rename to sdg-core/src/main/java/tfm/graphs/Sliceable.java diff --git a/src/main/java/tfm/graphs/augmented/ACFG.java b/sdg-core/src/main/java/tfm/graphs/augmented/ACFG.java similarity index 100% rename from src/main/java/tfm/graphs/augmented/ACFG.java rename to sdg-core/src/main/java/tfm/graphs/augmented/ACFG.java diff --git a/src/main/java/tfm/graphs/augmented/ACFGBuilder.java b/sdg-core/src/main/java/tfm/graphs/augmented/ACFGBuilder.java similarity index 100% rename from src/main/java/tfm/graphs/augmented/ACFGBuilder.java rename to sdg-core/src/main/java/tfm/graphs/augmented/ACFGBuilder.java diff --git a/src/main/java/tfm/graphs/augmented/APDG.java b/sdg-core/src/main/java/tfm/graphs/augmented/APDG.java similarity index 100% rename from src/main/java/tfm/graphs/augmented/APDG.java rename to sdg-core/src/main/java/tfm/graphs/augmented/APDG.java diff --git a/src/main/java/tfm/graphs/augmented/PPDG.java b/sdg-core/src/main/java/tfm/graphs/augmented/PPDG.java similarity index 100% rename from src/main/java/tfm/graphs/augmented/PPDG.java rename to sdg-core/src/main/java/tfm/graphs/augmented/PPDG.java diff --git a/src/main/java/tfm/graphs/cfg/CFG.java b/sdg-core/src/main/java/tfm/graphs/cfg/CFG.java similarity index 100% rename from src/main/java/tfm/graphs/cfg/CFG.java rename to sdg-core/src/main/java/tfm/graphs/cfg/CFG.java diff --git a/src/main/java/tfm/graphs/cfg/CFGBuilder.java b/sdg-core/src/main/java/tfm/graphs/cfg/CFGBuilder.java similarity index 100% rename from src/main/java/tfm/graphs/cfg/CFGBuilder.java rename to sdg-core/src/main/java/tfm/graphs/cfg/CFGBuilder.java diff --git a/src/main/java/tfm/graphs/pdg/ControlDependencyBuilder.java b/sdg-core/src/main/java/tfm/graphs/pdg/ControlDependencyBuilder.java similarity index 100% rename from src/main/java/tfm/graphs/pdg/ControlDependencyBuilder.java rename to sdg-core/src/main/java/tfm/graphs/pdg/ControlDependencyBuilder.java diff --git a/src/main/java/tfm/graphs/pdg/DataDependencyBuilder.java b/sdg-core/src/main/java/tfm/graphs/pdg/DataDependencyBuilder.java similarity index 100% rename from src/main/java/tfm/graphs/pdg/DataDependencyBuilder.java rename to sdg-core/src/main/java/tfm/graphs/pdg/DataDependencyBuilder.java diff --git a/src/main/java/tfm/graphs/pdg/PDG.java b/sdg-core/src/main/java/tfm/graphs/pdg/PDG.java similarity index 100% rename from src/main/java/tfm/graphs/pdg/PDG.java rename to sdg-core/src/main/java/tfm/graphs/pdg/PDG.java diff --git a/src/main/java/tfm/graphs/pdg/PDGBuilder.java b/sdg-core/src/main/java/tfm/graphs/pdg/PDGBuilder.java similarity index 100% rename from src/main/java/tfm/graphs/pdg/PDGBuilder.java rename to sdg-core/src/main/java/tfm/graphs/pdg/PDGBuilder.java diff --git a/src/main/java/tfm/graphs/sdg/MethodCallReplacer.java b/sdg-core/src/main/java/tfm/graphs/sdg/MethodCallReplacer.java similarity index 79% rename from src/main/java/tfm/graphs/sdg/MethodCallReplacer.java rename to sdg-core/src/main/java/tfm/graphs/sdg/MethodCallReplacer.java index e8290fd..c3bc8af 100644 --- a/src/main/java/tfm/graphs/sdg/MethodCallReplacer.java +++ b/sdg-core/src/main/java/tfm/graphs/sdg/MethodCallReplacer.java @@ -1,12 +1,7 @@ package tfm.graphs.sdg; import com.github.javaparser.ast.body.MethodDeclaration; -import tfm.graphs.GraphWithRootNode; -import tfm.nodes.GraphNode; import tfm.utils.Context; -import tfm.utils.Logger; - -import java.util.Optional; class MethodCallReplacer { diff --git a/src/main/java/tfm/graphs/sdg/MethodCallReplacerVisitor.java b/sdg-core/src/main/java/tfm/graphs/sdg/MethodCallReplacerVisitor.java similarity index 100% rename from src/main/java/tfm/graphs/sdg/MethodCallReplacerVisitor.java rename to sdg-core/src/main/java/tfm/graphs/sdg/MethodCallReplacerVisitor.java diff --git a/src/main/java/tfm/graphs/sdg/OutNodeVariableVisitor.java b/sdg-core/src/main/java/tfm/graphs/sdg/OutNodeVariableVisitor.java similarity index 100% rename from src/main/java/tfm/graphs/sdg/OutNodeVariableVisitor.java rename to sdg-core/src/main/java/tfm/graphs/sdg/OutNodeVariableVisitor.java diff --git a/src/main/java/tfm/graphs/sdg/SDG.java b/sdg-core/src/main/java/tfm/graphs/sdg/SDG.java similarity index 100% rename from src/main/java/tfm/graphs/sdg/SDG.java rename to sdg-core/src/main/java/tfm/graphs/sdg/SDG.java diff --git a/src/main/java/tfm/graphs/sdg/SDGBuilder.java b/sdg-core/src/main/java/tfm/graphs/sdg/SDGBuilder.java similarity index 100% rename from src/main/java/tfm/graphs/sdg/SDGBuilder.java rename to sdg-core/src/main/java/tfm/graphs/sdg/SDGBuilder.java diff --git a/src/main/java/tfm/graphs/sdg/sumarcs/NaiveSummaryArcsBuilder.java b/sdg-core/src/main/java/tfm/graphs/sdg/sumarcs/NaiveSummaryArcsBuilder.java similarity index 100% rename from src/main/java/tfm/graphs/sdg/sumarcs/NaiveSummaryArcsBuilder.java rename to sdg-core/src/main/java/tfm/graphs/sdg/sumarcs/NaiveSummaryArcsBuilder.java diff --git a/src/main/java/tfm/graphs/sdg/sumarcs/SummaryArcsBuilder.java b/sdg-core/src/main/java/tfm/graphs/sdg/sumarcs/SummaryArcsBuilder.java similarity index 100% rename from src/main/java/tfm/graphs/sdg/sumarcs/SummaryArcsBuilder.java rename to sdg-core/src/main/java/tfm/graphs/sdg/sumarcs/SummaryArcsBuilder.java diff --git a/src/main/java/tfm/nodes/GraphNode.java b/sdg-core/src/main/java/tfm/nodes/GraphNode.java similarity index 100% rename from src/main/java/tfm/nodes/GraphNode.java rename to sdg-core/src/main/java/tfm/nodes/GraphNode.java diff --git a/src/main/java/tfm/nodes/IdHelper.java b/sdg-core/src/main/java/tfm/nodes/IdHelper.java similarity index 100% rename from src/main/java/tfm/nodes/IdHelper.java rename to sdg-core/src/main/java/tfm/nodes/IdHelper.java diff --git a/src/main/java/tfm/nodes/NodeFactory.java b/sdg-core/src/main/java/tfm/nodes/NodeFactory.java similarity index 98% rename from src/main/java/tfm/nodes/NodeFactory.java rename to sdg-core/src/main/java/tfm/nodes/NodeFactory.java index b54b2ef..b0c5889 100644 --- a/src/main/java/tfm/nodes/NodeFactory.java +++ b/sdg-core/src/main/java/tfm/nodes/NodeFactory.java @@ -2,7 +2,6 @@ package tfm.nodes; import com.github.javaparser.ast.Node; import org.jetbrains.annotations.NotNull; -import tfm.nodes.GraphNode; import java.util.Collection; diff --git a/src/main/java/tfm/nodes/TypeNodeFactory.java b/sdg-core/src/main/java/tfm/nodes/TypeNodeFactory.java similarity index 100% rename from src/main/java/tfm/nodes/TypeNodeFactory.java rename to sdg-core/src/main/java/tfm/nodes/TypeNodeFactory.java diff --git a/src/main/java/tfm/nodes/type/NodeType.java b/sdg-core/src/main/java/tfm/nodes/type/NodeType.java similarity index 100% rename from src/main/java/tfm/nodes/type/NodeType.java rename to sdg-core/src/main/java/tfm/nodes/type/NodeType.java diff --git a/src/main/java/tfm/slicing/GraphNodeCriterion.java b/sdg-core/src/main/java/tfm/slicing/GraphNodeCriterion.java similarity index 100% rename from src/main/java/tfm/slicing/GraphNodeCriterion.java rename to sdg-core/src/main/java/tfm/slicing/GraphNodeCriterion.java diff --git a/src/main/java/tfm/slicing/LineNumberCriterion.java b/sdg-core/src/main/java/tfm/slicing/LineNumberCriterion.java similarity index 100% rename from src/main/java/tfm/slicing/LineNumberCriterion.java rename to sdg-core/src/main/java/tfm/slicing/LineNumberCriterion.java diff --git a/src/main/java/tfm/slicing/Slice.java b/sdg-core/src/main/java/tfm/slicing/Slice.java similarity index 100% rename from src/main/java/tfm/slicing/Slice.java rename to sdg-core/src/main/java/tfm/slicing/Slice.java diff --git a/src/main/java/tfm/slicing/SliceAstVisitor.java b/sdg-core/src/main/java/tfm/slicing/SliceAstVisitor.java similarity index 100% rename from src/main/java/tfm/slicing/SliceAstVisitor.java rename to sdg-core/src/main/java/tfm/slicing/SliceAstVisitor.java diff --git a/src/main/java/tfm/slicing/Sliceable.java b/sdg-core/src/main/java/tfm/slicing/Sliceable.java similarity index 100% rename from src/main/java/tfm/slicing/Sliceable.java rename to sdg-core/src/main/java/tfm/slicing/Sliceable.java diff --git a/src/main/java/tfm/slicing/SlicingCriterion.java b/sdg-core/src/main/java/tfm/slicing/SlicingCriterion.java similarity index 100% rename from src/main/java/tfm/slicing/SlicingCriterion.java rename to sdg-core/src/main/java/tfm/slicing/SlicingCriterion.java diff --git a/src/main/java/tfm/utils/ASTUtils.java b/sdg-core/src/main/java/tfm/utils/ASTUtils.java similarity index 98% rename from src/main/java/tfm/utils/ASTUtils.java rename to sdg-core/src/main/java/tfm/utils/ASTUtils.java index 7744e8b..dc3de70 100644 --- a/src/main/java/tfm/utils/ASTUtils.java +++ b/sdg-core/src/main/java/tfm/utils/ASTUtils.java @@ -3,7 +3,6 @@ package tfm.utils; import com.github.javaparser.ast.CompilationUnit; import com.github.javaparser.ast.Node; import com.github.javaparser.ast.NodeList; -import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration; import com.github.javaparser.ast.stmt.*; import java.util.Objects; diff --git a/src/main/java/tfm/utils/Context.java b/sdg-core/src/main/java/tfm/utils/Context.java similarity index 100% rename from src/main/java/tfm/utils/Context.java rename to sdg-core/src/main/java/tfm/utils/Context.java diff --git a/src/main/java/tfm/utils/FileUtil.java b/sdg-core/src/main/java/tfm/utils/FileUtil.java similarity index 100% rename from src/main/java/tfm/utils/FileUtil.java rename to sdg-core/src/main/java/tfm/utils/FileUtil.java diff --git a/src/main/java/tfm/utils/Logger.java b/sdg-core/src/main/java/tfm/utils/Logger.java similarity index 100% rename from src/main/java/tfm/utils/Logger.java rename to sdg-core/src/main/java/tfm/utils/Logger.java diff --git a/src/main/java/tfm/utils/MethodDeclarationSolver.java b/sdg-core/src/main/java/tfm/utils/MethodDeclarationSolver.java similarity index 100% rename from src/main/java/tfm/utils/MethodDeclarationSolver.java rename to sdg-core/src/main/java/tfm/utils/MethodDeclarationSolver.java diff --git a/src/main/java/tfm/utils/NodeNotFoundException.java b/sdg-core/src/main/java/tfm/utils/NodeNotFoundException.java similarity index 100% rename from src/main/java/tfm/utils/NodeNotFoundException.java rename to sdg-core/src/main/java/tfm/utils/NodeNotFoundException.java diff --git a/src/main/java/tfm/utils/Utils.java b/sdg-core/src/main/java/tfm/utils/Utils.java similarity index 100% rename from src/main/java/tfm/utils/Utils.java rename to sdg-core/src/main/java/tfm/utils/Utils.java diff --git a/src/main/java/tfm/variables/VariableExtractor.java b/sdg-core/src/main/java/tfm/variables/VariableExtractor.java similarity index 97% rename from src/main/java/tfm/variables/VariableExtractor.java rename to sdg-core/src/main/java/tfm/variables/VariableExtractor.java index 962e538..d482849 100644 --- a/src/main/java/tfm/variables/VariableExtractor.java +++ b/sdg-core/src/main/java/tfm/variables/VariableExtractor.java @@ -1,7 +1,6 @@ package tfm.variables; import com.github.javaparser.ast.Node; -import com.github.javaparser.ast.expr.Expression; import org.checkerframework.checker.nullness.qual.NonNull; import tfm.variables.actions.VariableAction; diff --git a/src/main/java/tfm/variables/VariableVisitor.java b/sdg-core/src/main/java/tfm/variables/VariableVisitor.java similarity index 99% rename from src/main/java/tfm/variables/VariableVisitor.java rename to sdg-core/src/main/java/tfm/variables/VariableVisitor.java index fd2f639..fb62998 100644 --- a/src/main/java/tfm/variables/VariableVisitor.java +++ b/sdg-core/src/main/java/tfm/variables/VariableVisitor.java @@ -7,8 +7,6 @@ import com.github.javaparser.ast.visitor.VoidVisitorAdapter; import org.checkerframework.checker.nullness.qual.NonNull; import tfm.variables.actions.VariableAction; -import java.awt.*; - abstract class VariableVisitor extends VoidVisitorAdapter { @Override diff --git a/src/main/java/tfm/variables/actions/VariableAction.java b/sdg-core/src/main/java/tfm/variables/actions/VariableAction.java similarity index 100% rename from src/main/java/tfm/variables/actions/VariableAction.java rename to sdg-core/src/main/java/tfm/variables/actions/VariableAction.java diff --git a/src/main/java/tfm/variables/actions/VariableDeclaration.java b/sdg-core/src/main/java/tfm/variables/actions/VariableDeclaration.java similarity index 100% rename from src/main/java/tfm/variables/actions/VariableDeclaration.java rename to sdg-core/src/main/java/tfm/variables/actions/VariableDeclaration.java diff --git a/src/main/java/tfm/variables/actions/VariableDefinition.java b/sdg-core/src/main/java/tfm/variables/actions/VariableDefinition.java similarity index 100% rename from src/main/java/tfm/variables/actions/VariableDefinition.java rename to sdg-core/src/main/java/tfm/variables/actions/VariableDefinition.java diff --git a/src/main/java/tfm/variables/actions/VariableUse.java b/sdg-core/src/main/java/tfm/variables/actions/VariableUse.java similarity index 100% rename from src/main/java/tfm/variables/actions/VariableUse.java rename to sdg-core/src/main/java/tfm/variables/actions/VariableUse.java diff --git a/src/test/java/tfm/graphs/pdg/HandCraftedGraphs.java b/sdg-core/src/test/java/tfm/graphs/pdg/HandCraftedGraphs.java similarity index 100% rename from src/test/java/tfm/graphs/pdg/HandCraftedGraphs.java rename to sdg-core/src/test/java/tfm/graphs/pdg/HandCraftedGraphs.java diff --git a/src/test/java/tfm/graphs/pdg/PDGTests.java b/sdg-core/src/test/java/tfm/graphs/pdg/PDGTests.java similarity index 99% rename from src/test/java/tfm/graphs/pdg/PDGTests.java rename to sdg-core/src/test/java/tfm/graphs/pdg/PDGTests.java index 3063df6..bf80587 100644 --- a/src/test/java/tfm/graphs/pdg/PDGTests.java +++ b/sdg-core/src/test/java/tfm/graphs/pdg/PDGTests.java @@ -9,8 +9,8 @@ import com.github.javaparser.ast.stmt.ThrowStmt; import com.github.javaparser.ast.stmt.TryStmt; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; -import tfm.exec.GraphLog; -import tfm.exec.PDGLog; +import tfm.cli.GraphLog; +import tfm.cli.PDGLog; import tfm.graphs.augmented.ACFG; import tfm.graphs.augmented.APDG; import tfm.graphs.augmented.PPDG; diff --git a/src/test/java/tfm/utils/FileFinder.java b/sdg-core/src/test/java/tfm/utils/FileFinder.java similarity index 100% rename from src/test/java/tfm/utils/FileFinder.java rename to sdg-core/src/test/java/tfm/utils/FileFinder.java diff --git a/src/test/res/carlos/Problem1.java b/sdg-core/src/test/res/carlos/Problem1.java similarity index 100% rename from src/test/res/carlos/Problem1.java rename to sdg-core/src/test/res/carlos/Problem1.java diff --git a/src/test/res/carlos/Problem2.java b/sdg-core/src/test/res/carlos/Problem2.java similarity index 100% rename from src/test/res/carlos/Problem2.java rename to sdg-core/src/test/res/carlos/Problem2.java diff --git a/src/test/res/carlos/Problem3.java b/sdg-core/src/test/res/carlos/Problem3.java similarity index 100% rename from src/test/res/carlos/Problem3.java rename to sdg-core/src/test/res/carlos/Problem3.java diff --git a/src/test/res/carlos/Test1.java b/sdg-core/src/test/res/carlos/Test1.java similarity index 100% rename from src/test/res/carlos/Test1.java rename to sdg-core/src/test/res/carlos/Test1.java diff --git a/src/test/res/java-slicing-benchmarks b/sdg-core/src/test/res/java-slicing-benchmarks similarity index 100% rename from src/test/res/java-slicing-benchmarks rename to sdg-core/src/test/res/java-slicing-benchmarks diff --git a/src/test/res/ltd-samples/BasicBreak.java b/sdg-core/src/test/res/ltd-samples/BasicBreak.java similarity index 100% rename from src/test/res/ltd-samples/BasicBreak.java rename to sdg-core/src/test/res/ltd-samples/BasicBreak.java diff --git a/src/test/res/ltd-samples/BasicContinue.java b/sdg-core/src/test/res/ltd-samples/BasicContinue.java similarity index 100% rename from src/test/res/ltd-samples/BasicContinue.java rename to sdg-core/src/test/res/ltd-samples/BasicContinue.java diff --git a/src/test/res/ltd-samples/BasicForeach.java b/sdg-core/src/test/res/ltd-samples/BasicForeach.java similarity index 100% rename from src/test/res/ltd-samples/BasicForeach.java rename to sdg-core/src/test/res/ltd-samples/BasicForeach.java diff --git a/src/test/res/ltd-samples/BasicIf.java b/sdg-core/src/test/res/ltd-samples/BasicIf.java similarity index 100% rename from src/test/res/ltd-samples/BasicIf.java rename to sdg-core/src/test/res/ltd-samples/BasicIf.java diff --git a/src/test/res/ltd-samples/BasicIfElse.java b/sdg-core/src/test/res/ltd-samples/BasicIfElse.java similarity index 100% rename from src/test/res/ltd-samples/BasicIfElse.java rename to sdg-core/src/test/res/ltd-samples/BasicIfElse.java diff --git a/src/test/res/ltd-samples/BasicSwitch.java b/sdg-core/src/test/res/ltd-samples/BasicSwitch.java similarity index 100% rename from src/test/res/ltd-samples/BasicSwitch.java rename to sdg-core/src/test/res/ltd-samples/BasicSwitch.java diff --git a/src/test/res/ltd-samples/BasicSwitchDefault.java b/sdg-core/src/test/res/ltd-samples/BasicSwitchDefault.java similarity index 100% rename from src/test/res/ltd-samples/BasicSwitchDefault.java rename to sdg-core/src/test/res/ltd-samples/BasicSwitchDefault.java diff --git a/src/test/res/ltd-samples/BasicSwitchNoBreak.java b/sdg-core/src/test/res/ltd-samples/BasicSwitchNoBreak.java similarity index 100% rename from src/test/res/ltd-samples/BasicSwitchNoBreak.java rename to sdg-core/src/test/res/ltd-samples/BasicSwitchNoBreak.java diff --git a/src/test/res/ltd-samples/Bucles_1.java b/sdg-core/src/test/res/ltd-samples/Bucles_1.java similarity index 100% rename from src/test/res/ltd-samples/Bucles_1.java rename to sdg-core/src/test/res/ltd-samples/Bucles_1.java diff --git a/src/test/res/ltd-samples/Bucles_2.java b/sdg-core/src/test/res/ltd-samples/Bucles_2.java similarity index 100% rename from src/test/res/ltd-samples/Bucles_2.java rename to sdg-core/src/test/res/ltd-samples/Bucles_2.java diff --git a/src/test/res/ltd-samples/Bucles_3.java b/sdg-core/src/test/res/ltd-samples/Bucles_3.java similarity index 100% rename from src/test/res/ltd-samples/Bucles_3.java rename to sdg-core/src/test/res/ltd-samples/Bucles_3.java diff --git a/src/test/res/ltd-samples/Bucles_4.java b/sdg-core/src/test/res/ltd-samples/Bucles_4.java similarity index 100% rename from src/test/res/ltd-samples/Bucles_4.java rename to sdg-core/src/test/res/ltd-samples/Bucles_4.java diff --git a/src/test/res/ltd-samples/Bucles_5.java b/sdg-core/src/test/res/ltd-samples/Bucles_5.java similarity index 100% rename from src/test/res/ltd-samples/Bucles_5.java rename to sdg-core/src/test/res/ltd-samples/Bucles_5.java diff --git a/src/test/res/ltd-samples/Bucles_6.java b/sdg-core/src/test/res/ltd-samples/Bucles_6.java similarity index 100% rename from src/test/res/ltd-samples/Bucles_6.java rename to sdg-core/src/test/res/ltd-samples/Bucles_6.java diff --git a/src/test/res/ltd-samples/Bucles_Josep.java b/sdg-core/src/test/res/ltd-samples/Bucles_Josep.java similarity index 100% rename from src/test/res/ltd-samples/Bucles_Josep.java rename to sdg-core/src/test/res/ltd-samples/Bucles_Josep.java diff --git a/src/test/res/ltd-samples/ReturnTest.java b/sdg-core/src/test/res/ltd-samples/ReturnTest.java similarity index 100% rename from src/test/res/ltd-samples/ReturnTest.java rename to sdg-core/src/test/res/ltd-samples/ReturnTest.java diff --git a/src/test/res/ltd-samples/Test_1.java b/sdg-core/src/test/res/ltd-samples/Test_1.java similarity index 100% rename from src/test/res/ltd-samples/Test_1.java rename to sdg-core/src/test/res/ltd-samples/Test_1.java diff --git a/src/test/res/ltd-samples/Test_2.java b/sdg-core/src/test/res/ltd-samples/Test_2.java similarity index 100% rename from src/test/res/ltd-samples/Test_2.java rename to sdg-core/src/test/res/ltd-samples/Test_2.java diff --git a/src/test/res/ltd-samples/Test_3.java b/sdg-core/src/test/res/ltd-samples/Test_3.java similarity index 100% rename from src/test/res/ltd-samples/Test_3.java rename to sdg-core/src/test/res/ltd-samples/Test_3.java diff --git a/src/test/res/ltd-samples/Test_4.java b/sdg-core/src/test/res/ltd-samples/Test_4.java similarity index 100% rename from src/test/res/ltd-samples/Test_4.java rename to sdg-core/src/test/res/ltd-samples/Test_4.java diff --git a/src/test/res/ltd-samples/Test_5.java b/sdg-core/src/test/res/ltd-samples/Test_5.java similarity index 100% rename from src/test/res/ltd-samples/Test_5.java rename to sdg-core/src/test/res/ltd-samples/Test_5.java diff --git a/src/test/res/ltd-samples/Test_6.java b/sdg-core/src/test/res/ltd-samples/Test_6.java similarity index 100% rename from src/test/res/ltd-samples/Test_6.java rename to sdg-core/src/test/res/ltd-samples/Test_6.java diff --git a/src/test/res/ltd-samples/Test_7.java b/sdg-core/src/test/res/ltd-samples/Test_7.java similarity index 100% rename from src/test/res/ltd-samples/Test_7.java rename to sdg-core/src/test/res/ltd-samples/Test_7.java diff --git a/src/test/res/ltd-samples/Test_8.java b/sdg-core/src/test/res/ltd-samples/Test_8.java similarity index 100% rename from src/test/res/ltd-samples/Test_8.java rename to sdg-core/src/test/res/ltd-samples/Test_8.java diff --git a/src/test/res/ltd-samples/Test_9.java b/sdg-core/src/test/res/ltd-samples/Test_9.java similarity index 100% rename from src/test/res/ltd-samples/Test_9.java rename to sdg-core/src/test/res/ltd-samples/Test_9.java diff --git a/src/test/res/papers/Example1_Horwitz_PPDG.java b/sdg-core/src/test/res/papers/Example1_Horwitz_PPDG.java similarity index 100% rename from src/test/res/papers/Example1_Horwitz_PPDG.java rename to sdg-core/src/test/res/papers/Example1_Horwitz_PPDG.java diff --git a/src/test/res/programs/WhileLoop.java b/sdg-core/src/test/res/programs/WhileLoop.java similarity index 100% rename from src/test/res/programs/WhileLoop.java rename to sdg-core/src/test/res/programs/WhileLoop.java diff --git a/src/test/res/programs/cfg/CFG_Test1.java b/sdg-core/src/test/res/programs/cfg/CFG_Test1.java similarity index 100% rename from src/test/res/programs/cfg/CFG_Test1.java rename to sdg-core/src/test/res/programs/cfg/CFG_Test1.java diff --git a/src/test/res/programs/cfg/CFG_Test2.java b/sdg-core/src/test/res/programs/cfg/CFG_Test2.java similarity index 100% rename from src/test/res/programs/cfg/CFG_Test2.java rename to sdg-core/src/test/res/programs/cfg/CFG_Test2.java diff --git a/src/test/res/programs/cfg/CFG_Test3.java b/sdg-core/src/test/res/programs/cfg/CFG_Test3.java similarity index 100% rename from src/test/res/programs/cfg/CFG_Test3.java rename to sdg-core/src/test/res/programs/cfg/CFG_Test3.java diff --git a/src/test/res/programs/cfg/CFG_Test4.java b/sdg-core/src/test/res/programs/cfg/CFG_Test4.java similarity index 100% rename from src/test/res/programs/cfg/CFG_Test4.java rename to sdg-core/src/test/res/programs/cfg/CFG_Test4.java diff --git a/src/test/res/programs/cfg/CFG_Test5.java b/sdg-core/src/test/res/programs/cfg/CFG_Test5.java similarity index 100% rename from src/test/res/programs/cfg/CFG_Test5.java rename to sdg-core/src/test/res/programs/cfg/CFG_Test5.java diff --git a/src/test/res/programs/cfg/Eval_1.java b/sdg-core/src/test/res/programs/cfg/Eval_1.java similarity index 100% rename from src/test/res/programs/cfg/Eval_1.java rename to sdg-core/src/test/res/programs/cfg/Eval_1.java diff --git a/src/test/res/programs/cfg/Eval_2.java b/sdg-core/src/test/res/programs/cfg/Eval_2.java similarity index 100% rename from src/test/res/programs/cfg/Eval_2.java rename to sdg-core/src/test/res/programs/cfg/Eval_2.java diff --git a/src/test/res/programs/cfg/Eval_3.java b/sdg-core/src/test/res/programs/cfg/Eval_3.java similarity index 100% rename from src/test/res/programs/cfg/Eval_3.java rename to sdg-core/src/test/res/programs/cfg/Eval_3.java diff --git a/src/test/res/programs/cfg/Eval_4.java b/sdg-core/src/test/res/programs/cfg/Eval_4.java similarity index 100% rename from src/test/res/programs/cfg/Eval_4.java rename to sdg-core/src/test/res/programs/cfg/Eval_4.java diff --git a/src/test/res/programs/pdg/Example1.java b/sdg-core/src/test/res/programs/pdg/Example1.java similarity index 100% rename from src/test/res/programs/pdg/Example1.java rename to sdg-core/src/test/res/programs/pdg/Example1.java diff --git a/src/test/res/programs/pdg/Example2.java b/sdg-core/src/test/res/programs/pdg/Example2.java similarity index 100% rename from src/test/res/programs/pdg/Example2.java rename to sdg-core/src/test/res/programs/pdg/Example2.java diff --git a/src/test/res/programs/pdg/Example3.java b/sdg-core/src/test/res/programs/pdg/Example3.java similarity index 100% rename from src/test/res/programs/pdg/Example3.java rename to sdg-core/src/test/res/programs/pdg/Example3.java diff --git a/src/test/res/programs/pdg/Test.java b/sdg-core/src/test/res/programs/pdg/Test.java similarity index 100% rename from src/test/res/programs/pdg/Test.java rename to sdg-core/src/test/res/programs/pdg/Test.java diff --git a/src/test/res/programs/sdg/Example1.java b/sdg-core/src/test/res/programs/sdg/Example1.java similarity index 100% rename from src/test/res/programs/sdg/Example1.java rename to sdg-core/src/test/res/programs/sdg/Example1.java diff --git a/src/test/res/programs/sdg/Example2.java b/sdg-core/src/test/res/programs/sdg/Example2.java similarity index 100% rename from src/test/res/programs/sdg/Example2.java rename to sdg-core/src/test/res/programs/sdg/Example2.java -- GitLab From d7efed70f0d0b06276458cc742095571c0059459 Mon Sep 17 00:00:00 2001 From: Carlos Galindo Date: Mon, 1 Jun 2020 20:53:53 +0200 Subject: [PATCH 02/10] Created the SDG's ClassicSlicingAlgorithm --- sdg-core/src/main/java/tfm/arcs/Arc.java | 8 +++ .../src/main/java/tfm/arcs/sdg/CallArc.java | 13 +++- .../java/tfm/arcs/sdg/InterproceduralArc.java | 15 ++++ .../java/tfm/arcs/sdg/ParameterInOutArc.java | 17 ++++- .../main/java/tfm/arcs/sdg/SummaryArc.java | 15 +++- .../main/java/tfm/graphs/augmented/PPDG.java | 34 +++------- .../src/main/java/tfm/graphs/pdg/PDG.java | 19 +----- .../src/main/java/tfm/graphs/sdg/SDG.java | 10 ++- .../tfm/slicing/ClassicSlicingAlgorithm.java | 68 +++++++++++++++++++ .../PseudoPredicateSlicingAlgorithm.java | 35 ++++++++++ sdg-core/src/main/java/tfm/slicing/Slice.java | 4 ++ .../java/tfm/slicing/SlicingAlgorithm.java | 7 ++ 12 files changed, 199 insertions(+), 46 deletions(-) create mode 100644 sdg-core/src/main/java/tfm/arcs/sdg/InterproceduralArc.java create mode 100644 sdg-core/src/main/java/tfm/slicing/ClassicSlicingAlgorithm.java create mode 100644 sdg-core/src/main/java/tfm/slicing/PseudoPredicateSlicingAlgorithm.java create mode 100644 sdg-core/src/main/java/tfm/slicing/SlicingAlgorithm.java diff --git a/sdg-core/src/main/java/tfm/arcs/Arc.java b/sdg-core/src/main/java/tfm/arcs/Arc.java index 950ae8f..e425b25 100644 --- a/sdg-core/src/main/java/tfm/arcs/Arc.java +++ b/sdg-core/src/main/java/tfm/arcs/Arc.java @@ -65,6 +65,14 @@ public abstract class Arc extends DefaultEdge { throw new UnsupportedOperationException("Not a DataDependencyArc"); } + public boolean isInterproceduralInputArc() { + return false; + } + + public boolean isInterproceduralOutputArc() { + return false; + } + /** @see CallArc */ public final boolean isCallArc() { return this instanceof CallArc; diff --git a/sdg-core/src/main/java/tfm/arcs/sdg/CallArc.java b/sdg-core/src/main/java/tfm/arcs/sdg/CallArc.java index 43c4138..a6af4b5 100644 --- a/sdg-core/src/main/java/tfm/arcs/sdg/CallArc.java +++ b/sdg-core/src/main/java/tfm/arcs/sdg/CallArc.java @@ -2,15 +2,24 @@ package tfm.arcs.sdg; import org.jgrapht.io.Attribute; import org.jgrapht.io.DefaultAttribute; -import tfm.arcs.Arc; import java.util.Map; -public class CallArc extends Arc { +public class CallArc extends InterproceduralArc { @Override public Map getDotAttributes() { Map map = super.getDotAttributes(); map.put("style", DefaultAttribute.createAttribute("dashed")); return map; } + + @Override + public boolean isInterproceduralInputArc() { + return true; + } + + @Override + public boolean isInterproceduralOutputArc() { + return false; + } } diff --git a/sdg-core/src/main/java/tfm/arcs/sdg/InterproceduralArc.java b/sdg-core/src/main/java/tfm/arcs/sdg/InterproceduralArc.java new file mode 100644 index 0000000..13c7471 --- /dev/null +++ b/sdg-core/src/main/java/tfm/arcs/sdg/InterproceduralArc.java @@ -0,0 +1,15 @@ +package tfm.arcs.sdg; + +import tfm.arcs.Arc; + +public abstract class InterproceduralArc extends Arc { + protected InterproceduralArc() { + super(); + } + + @Override + public abstract boolean isInterproceduralInputArc(); + + @Override + public abstract boolean isInterproceduralOutputArc(); +} diff --git a/sdg-core/src/main/java/tfm/arcs/sdg/ParameterInOutArc.java b/sdg-core/src/main/java/tfm/arcs/sdg/ParameterInOutArc.java index 50673b7..4f377d7 100644 --- a/sdg-core/src/main/java/tfm/arcs/sdg/ParameterInOutArc.java +++ b/sdg-core/src/main/java/tfm/arcs/sdg/ParameterInOutArc.java @@ -2,15 +2,28 @@ package tfm.arcs.sdg; import org.jgrapht.io.Attribute; import org.jgrapht.io.DefaultAttribute; -import tfm.arcs.Arc; +import tfm.nodes.GraphNode; +import tfm.nodes.type.NodeType; import java.util.Map; -public class ParameterInOutArc extends Arc { +public class ParameterInOutArc extends InterproceduralArc { @Override public Map getDotAttributes() { Map map = super.getDotAttributes(); map.put("style", DefaultAttribute.createAttribute("dashed")); return map; } + + @Override + public boolean isInterproceduralInputArc() { + return ((GraphNode) getSource()).getNodeType() == NodeType.ACTUAL_IN && + ((GraphNode) getTarget()).getNodeType() == NodeType.FORMAL_IN; + } + + @Override + public boolean isInterproceduralOutputArc() { + return ((GraphNode) getSource()).getNodeType() == NodeType.FORMAL_OUT && + ((GraphNode) getTarget()).getNodeType() == NodeType.ACTUAL_OUT; + } } diff --git a/sdg-core/src/main/java/tfm/arcs/sdg/SummaryArc.java b/sdg-core/src/main/java/tfm/arcs/sdg/SummaryArc.java index bde9724..c16b1a4 100644 --- a/sdg-core/src/main/java/tfm/arcs/sdg/SummaryArc.java +++ b/sdg-core/src/main/java/tfm/arcs/sdg/SummaryArc.java @@ -2,15 +2,28 @@ package tfm.arcs.sdg; import org.jgrapht.io.Attribute; import org.jgrapht.io.DefaultAttribute; -import tfm.arcs.Arc; import java.util.Map; +<<<<<<< HEAD:sdg-core/src/main/java/tfm/arcs/sdg/SummaryArc.java public class SummaryArc extends Arc { +======= +public class ReturnArc extends InterproceduralArc { +>>>>>>> 303de98... Created the SDG's ClassicSlicingAlgorithm:sdg-core/src/main/java/tfm/arcs/sdg/ReturnArc.java @Override public Map getDotAttributes() { Map map = super.getDotAttributes(); map.put("style", DefaultAttribute.createAttribute("bold")); return map; } + + @Override + public boolean isInterproceduralInputArc() { + return false; + } + + @Override + public boolean isInterproceduralOutputArc() { + return true; + } } diff --git a/sdg-core/src/main/java/tfm/graphs/augmented/PPDG.java b/sdg-core/src/main/java/tfm/graphs/augmented/PPDG.java index c864533..6dd57cf 100644 --- a/sdg-core/src/main/java/tfm/graphs/augmented/PPDG.java +++ b/sdg-core/src/main/java/tfm/graphs/augmented/PPDG.java @@ -1,9 +1,12 @@ package tfm.graphs.augmented; -import tfm.arcs.Arc; import tfm.nodes.GraphNode; +import tfm.slicing.PseudoPredicateSlicingAlgorithm; import tfm.slicing.Slice; -import tfm.utils.ASTUtils; +import tfm.slicing.SlicingCriterion; +import tfm.utils.NodeNotFoundException; + +import java.util.Optional; public class PPDG extends APDG { public PPDG() { @@ -15,27 +18,10 @@ public class PPDG extends APDG { } @Override - protected void getSliceNodes(Slice slice, GraphNode node) { - slice.add(node); - - for (Arc arc : incomingEdgesOf(node)) { - GraphNode from = getEdgeSource(arc); - if (slice.contains(from)) - continue; - getSliceNodesPPDG(slice, from); - } - } - - protected void getSliceNodesPPDG(Slice slice, GraphNode node) { - slice.add(node); - if (ASTUtils.isPseudoPredicate(node.getAstNode())) - return; - - for (Arc arc : incomingEdgesOf(node)) { - GraphNode from = getEdgeSource(arc); - if (slice.contains(from)) - continue; - getSliceNodesPPDG(slice, from); - } + public Slice slice(SlicingCriterion slicingCriterion) { + Optional> node = slicingCriterion.findNode(this); + if (node.isEmpty()) + throw new NodeNotFoundException(slicingCriterion); + return new PseudoPredicateSlicingAlgorithm(this).traverse(node.get()); } } diff --git a/sdg-core/src/main/java/tfm/graphs/pdg/PDG.java b/sdg-core/src/main/java/tfm/graphs/pdg/PDG.java index 328281a..976ee64 100644 --- a/sdg-core/src/main/java/tfm/graphs/pdg/PDG.java +++ b/sdg-core/src/main/java/tfm/graphs/pdg/PDG.java @@ -1,13 +1,13 @@ package tfm.graphs.pdg; import com.github.javaparser.ast.body.MethodDeclaration; -import tfm.arcs.Arc; import tfm.arcs.pdg.ControlDependencyArc; import tfm.arcs.pdg.DataDependencyArc; import tfm.graphs.GraphWithRootNode; import tfm.graphs.Sliceable; import tfm.graphs.cfg.CFG; import tfm.nodes.GraphNode; +import tfm.slicing.ClassicSlicingAlgorithm; import tfm.slicing.Slice; import tfm.slicing.SlicingCriterion; import tfm.utils.NodeNotFoundException; @@ -47,22 +47,9 @@ public class PDG extends GraphWithRootNode implements Sliceab @Override public Slice slice(SlicingCriterion slicingCriterion) { Optional> node = slicingCriterion.findNode(this); - if (!node.isPresent()) + if (node.isEmpty()) throw new NodeNotFoundException(slicingCriterion); - Slice slice = new Slice(); - getSliceNodes(slice, node.get()); - return slice; - } - - protected void getSliceNodes(Slice slice, GraphNode node) { - slice.add(node); - - for (Arc arc : incomingEdgesOf(node)) { - GraphNode from = getEdgeSource(arc); - if (slice.contains(from)) - continue; - getSliceNodes(slice, from); - } + return new ClassicSlicingAlgorithm(this).traverse(node.get()); } public CFG getCfg() { diff --git a/sdg-core/src/main/java/tfm/graphs/sdg/SDG.java b/sdg-core/src/main/java/tfm/graphs/sdg/SDG.java index bcabf95..21224da 100644 --- a/sdg-core/src/main/java/tfm/graphs/sdg/SDG.java +++ b/sdg-core/src/main/java/tfm/graphs/sdg/SDG.java @@ -13,7 +13,12 @@ import tfm.arcs.sdg.SummaryArc; import tfm.graphs.Buildable; import tfm.graphs.Graph; import tfm.graphs.cfg.CFG; +<<<<<<< HEAD import tfm.nodes.*; +======= +import tfm.nodes.GraphNode; +import tfm.slicing.ClassicSlicingAlgorithm; +>>>>>>> 303de98... Created the SDG's ClassicSlicingAlgorithm import tfm.slicing.Slice; import tfm.slicing.Sliceable; import tfm.slicing.SlicingCriterion; @@ -33,7 +38,10 @@ public class SDG extends Graph implements Sliceable, Buildable> optSlicingNode = slicingCriterion.findNode(this); + if (optSlicingNode.isEmpty()) + throw new IllegalArgumentException("Could not locate the slicing criterion in the SDG"); + return new ClassicSlicingAlgorithm(this).traverse(optSlicingNode.get()); } @Override diff --git a/sdg-core/src/main/java/tfm/slicing/ClassicSlicingAlgorithm.java b/sdg-core/src/main/java/tfm/slicing/ClassicSlicingAlgorithm.java new file mode 100644 index 0000000..e608af4 --- /dev/null +++ b/sdg-core/src/main/java/tfm/slicing/ClassicSlicingAlgorithm.java @@ -0,0 +1,68 @@ +package tfm.slicing; + +import tfm.arcs.Arc; +import tfm.graphs.Graph; +import tfm.nodes.GraphNode; + +import java.util.HashSet; +import java.util.Iterator; +import java.util.LinkedHashSet; +import java.util.Set; +import java.util.function.Predicate; + +public class ClassicSlicingAlgorithm implements SlicingAlgorithm { + protected final Graph graph; + + public ClassicSlicingAlgorithm(Graph graph) { + this.graph = graph; + } + + @Override + public Slice traverse(GraphNode slicingCriterion) { + Slice slice = new Slice(); + slice.add(slicingCriterion); + pass(slice, this::ignorePass1); + pass(slice, this::ignorePass2); + return slice; + } + + protected boolean ignorePass1(Arc arc) { + return arc.isInterproceduralOutputArc(); + } + + protected boolean ignorePass2(Arc arc) { + return arc.isInterproceduralInputArc(); + } + + protected void pass(Slice slice, Predicate ignoreCondition) { + // `toVisit` behaves like a set and using iterable we can use it as a queue + // More info: https://stackoverflow.com/a/2319126 + LinkedHashSet> toVisit = new LinkedHashSet<>(slice.getGraphNodes()); + Set> visited = new HashSet<>(); + + while (!toVisit.isEmpty()) { + GraphNode node = removeFirst(toVisit); + // Avoid duplicate traversal + if (visited.contains(node)) + continue; + visited.add(node); + // Traverse all edges backwards + for (Arc arc : graph.incomingEdgesOf(node)) { + if (ignoreCondition.test(arc)) + continue; + GraphNode source = graph.getEdgeSource(arc); + if (!visited.contains(source)) + toVisit.add(source); + } + } + + visited.forEach(slice::add); + } + + protected static E removeFirst(Set set) { + Iterator i = set.iterator(); + E e = i.next(); + i.remove(); + return e; + } +} diff --git a/sdg-core/src/main/java/tfm/slicing/PseudoPredicateSlicingAlgorithm.java b/sdg-core/src/main/java/tfm/slicing/PseudoPredicateSlicingAlgorithm.java new file mode 100644 index 0000000..8f66c43 --- /dev/null +++ b/sdg-core/src/main/java/tfm/slicing/PseudoPredicateSlicingAlgorithm.java @@ -0,0 +1,35 @@ +package tfm.slicing; + +import tfm.arcs.Arc; +import tfm.graphs.Graph; +import tfm.nodes.GraphNode; +import tfm.utils.ASTUtils; + +public class PseudoPredicateSlicingAlgorithm extends ClassicSlicingAlgorithm { + protected GraphNode slicingCriterion; + + public PseudoPredicateSlicingAlgorithm(Graph graph) { + super(graph); + } + + @Override + public Slice traverse(GraphNode slicingCriterion) { + this.slicingCriterion = slicingCriterion; + return super.traverse(slicingCriterion); + } + + @Override + protected boolean ignorePass1(Arc arc) { + return super.ignorePass1(arc) || ignorePseudoPredicate(arc); + } + + @Override + protected boolean ignorePass2(Arc arc) { + return super.ignorePass2(arc) || ignorePseudoPredicate(arc); + } + + protected boolean ignorePseudoPredicate(Arc arc) { + GraphNode target = graph.getEdgeTarget(arc); + return ASTUtils.isPseudoPredicate(target.getAstNode()) && target != slicingCriterion; + } +} diff --git a/sdg-core/src/main/java/tfm/slicing/Slice.java b/sdg-core/src/main/java/tfm/slicing/Slice.java index 91563f3..89f9d79 100644 --- a/sdg-core/src/main/java/tfm/slicing/Slice.java +++ b/sdg-core/src/main/java/tfm/slicing/Slice.java @@ -39,6 +39,10 @@ public class Slice { return obj instanceof Slice && map.equals(((Slice) obj).map); } + public Set> getGraphNodes() { + return Set.copyOf(map.values()); + } + public Node getAst() { List> methods = map.values().stream().filter(e -> e.getAstNode() instanceof MethodDeclaration).collect(Collectors.toList()); if (methods.size() == 1) { diff --git a/sdg-core/src/main/java/tfm/slicing/SlicingAlgorithm.java b/sdg-core/src/main/java/tfm/slicing/SlicingAlgorithm.java new file mode 100644 index 0000000..8ad7c4a --- /dev/null +++ b/sdg-core/src/main/java/tfm/slicing/SlicingAlgorithm.java @@ -0,0 +1,7 @@ +package tfm.slicing; + +import tfm.nodes.GraphNode; + +public interface SlicingAlgorithm { + Slice traverse(GraphNode slicingCriterion); +} -- GitLab From 2ffbbbd6f3793c96f5cf5b33e91de83209081266 Mon Sep 17 00:00:00 2001 From: Carlos Galindo Date: Tue, 2 Jun 2020 11:16:29 +0200 Subject: [PATCH 03/10] Resolve methods using JavaParser TypeSolver --- .../src/main/java/tfm/cli/MethodResolver.java | 103 ------------------ .../graphs/sdg/MethodCallReplacerVisitor.java | 11 +- .../tfm/utils/MethodDeclarationSolver.java | 48 -------- 3 files changed, 4 insertions(+), 158 deletions(-) delete mode 100644 sdg-cli/src/main/java/tfm/cli/MethodResolver.java delete mode 100644 sdg-core/src/main/java/tfm/utils/MethodDeclarationSolver.java diff --git a/sdg-cli/src/main/java/tfm/cli/MethodResolver.java b/sdg-cli/src/main/java/tfm/cli/MethodResolver.java deleted file mode 100644 index 8d88d63..0000000 --- a/sdg-cli/src/main/java/tfm/cli/MethodResolver.java +++ /dev/null @@ -1,103 +0,0 @@ -package tfm.cli; - -import com.github.javaparser.JavaParser; -import com.github.javaparser.ast.CompilationUnit; -import com.github.javaparser.ast.NodeList; -import com.github.javaparser.ast.body.MethodDeclaration; -import com.github.javaparser.ast.expr.MethodCallExpr; -import com.github.javaparser.ast.visitor.VoidVisitorAdapter; -import com.github.javaparser.resolution.UnsolvedSymbolException; -import com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration; -import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade; -import com.github.javaparser.symbolsolver.model.resolution.SymbolReference; -import com.github.javaparser.symbolsolver.model.resolution.TypeSolver; -import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver; -import com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver; -import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver; -import tfm.graphs.sdg.SDG; -import tfm.nodes.GraphNode; -import tfm.utils.Logger; - -import java.io.File; -import java.io.FileNotFoundException; -import java.util.Arrays; -import java.util.Optional; - -public class MethodResolver { - - private static class Args { - String file; - String method; - } - - public static void main(String[] inputArgs) throws FileNotFoundException { - Args args = parseArgs(inputArgs); - - CompilationUnit cu = JavaParser.parse(new File(args.file)); - - SDG sdg = new SDG(); - sdg.build(new NodeList<>(cu)); - - VoidVisitorAdapter visitor = new VoidVisitorAdapter() { - @Override - public void visit(MethodCallExpr n, Void arg) { - TypeSolver solver = new JavaParserTypeSolver(args.file.substring(0, args.file.lastIndexOf('/'))); - - Logger.log("-- Trying to solve method " + n.getNameAsString() + " --"); - - Optional optionalResolvedMethod; - - try { - optionalResolvedMethod = getMethodCallWithJavaParserSymbolSolver(n, solver, new ReflectionTypeSolver()); - } catch (UnsolvedSymbolException e) { - optionalResolvedMethod = Optional.empty(); - } - - if (!optionalResolvedMethod.isPresent()) { - Logger.format("Not found: %s", n); - return; - } - - Logger.format("Found: %s", n.getNameAsString()); - Logger.log(optionalResolvedMethod.get().getSignature().asString()); - - Logger.log("-- Trying to match with a node from SDG --"); - Optional> methodDeclarationNode = optionalResolvedMethod.flatMap(sdg::findNodeByASTNode); - - if (!methodDeclarationNode.isPresent()) { - Logger.log("Failed to find node in SDG"); - return; - } - - Logger.format("SDG node: %s", methodDeclarationNode.get()); - - } - }; - - cu.accept(visitor, null); - } - - private static Args parseArgs(String[] args) { - Args res = new Args(); - - Logger.log(Arrays.asList(args)); - - try { - res.file = args[0]; - // res.method = args[2]; - } catch (Exception e) { - Logger.log("Incorrect syntax: java MethodResolver.class "); - System.exit(1); - } - - return res; - } - - private static Optional getMethodCallWithJavaParserSymbolSolver(MethodCallExpr methodCallExpr, TypeSolver... solvers) { - CombinedTypeSolver combinedTypeSolver = new CombinedTypeSolver(solvers); - - SymbolReference solver = JavaParserFacade.get(combinedTypeSolver).solve(methodCallExpr); - - return solver.isSolved() ? solver.getCorrespondingDeclaration().toAst() : Optional.empty(); - } -} diff --git a/sdg-core/src/main/java/tfm/graphs/sdg/MethodCallReplacerVisitor.java b/sdg-core/src/main/java/tfm/graphs/sdg/MethodCallReplacerVisitor.java index ab9d7cb..dbec4bc 100644 --- a/sdg-core/src/main/java/tfm/graphs/sdg/MethodCallReplacerVisitor.java +++ b/sdg-core/src/main/java/tfm/graphs/sdg/MethodCallReplacerVisitor.java @@ -17,7 +17,6 @@ import tfm.nodes.TypeNodeFactory; import tfm.nodes.type.NodeType; import tfm.utils.Context; import tfm.utils.Logger; -import tfm.utils.MethodDeclarationSolver; import java.util.*; import java.util.stream.Collectors; @@ -97,17 +96,15 @@ class MethodCallReplacerVisitor extends VoidVisitorAdapter { Logger.log("MethodCallReplacerVisitor", context); - Optional> optionalNethodDeclarationNode = - MethodDeclarationSolver.getInstance() - .findDeclarationFrom(methodCallExpr) - .flatMap(methodDeclaration -> sdg.findNodeByASTNode(methodDeclaration)); + Optional> optMethodDeclGraphNode = methodCallExpr.resolve().toAst() + .flatMap(sdg::findNodeByASTNode); - if (!optionalNethodDeclarationNode.isPresent()) { + if (optMethodDeclGraphNode.isEmpty()) { Logger.format("Not found: '%s'. Discarding", methodCallExpr); return; } - GraphNode methodDeclarationNode = optionalNethodDeclarationNode.get(); + GraphNode methodDeclarationNode = optMethodDeclGraphNode.get(); MethodDeclaration methodDeclaration = methodDeclarationNode.getAstNode(); Optional optionalCFG = sdg.getMethodCFG(methodDeclaration); diff --git a/sdg-core/src/main/java/tfm/utils/MethodDeclarationSolver.java b/sdg-core/src/main/java/tfm/utils/MethodDeclarationSolver.java deleted file mode 100644 index b6e7eba..0000000 --- a/sdg-core/src/main/java/tfm/utils/MethodDeclarationSolver.java +++ /dev/null @@ -1,48 +0,0 @@ -package tfm.utils; - -import com.github.javaparser.ast.body.MethodDeclaration; -import com.github.javaparser.ast.expr.MethodCallExpr; -import com.github.javaparser.resolution.UnsolvedSymbolException; -import com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration; -import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade; -import com.github.javaparser.symbolsolver.model.resolution.SymbolReference; -import com.github.javaparser.symbolsolver.model.resolution.TypeSolver; -import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver; - -import java.util.*; - -public class MethodDeclarationSolver { - - private static final MethodDeclarationSolver instance = new MethodDeclarationSolver(); - private static final List usedTypeSolvers = new ArrayList<>(); - - private MethodDeclarationSolver() { - - } - - public static void addTypeSolvers(TypeSolver... typeSolvers) { - usedTypeSolvers.addAll(Arrays.asList(typeSolvers)); - } - - public static MethodDeclarationSolver getInstance() { - return instance; - } - - public Optional findDeclarationFrom(MethodCallExpr methodCallExpr) { - return this.findDeclarationFrom(methodCallExpr, usedTypeSolvers); - } - - public Optional findDeclarationFrom(MethodCallExpr methodCallExpr, Collection customTypeSolvers) { - CombinedTypeSolver combinedTypeSolver = new CombinedTypeSolver(customTypeSolvers.toArray(new TypeSolver[0])); - - try { - SymbolReference solver = JavaParserFacade.get(combinedTypeSolver).solve(methodCallExpr); - - return solver.isSolved() - ? solver.getCorrespondingDeclaration().toAst() - : Optional.empty(); - } catch (UnsolvedSymbolException e) { - return Optional.empty(); - } - } -} -- GitLab From 5eebd9e14896d1b8d088823084b08de84c3ca6fe Mon Sep 17 00:00:00 2001 From: Carlos Galindo Date: Fri, 5 Jun 2020 16:39:37 +0200 Subject: [PATCH 04/10] Fix ACTUAL_IN and ACTUAL_OUT data dependency connections. --- .../graphs/sdg/MethodCallReplacerVisitor.java | 28 ++++++------------- 1 file changed, 8 insertions(+), 20 deletions(-) diff --git a/sdg-core/src/main/java/tfm/graphs/sdg/MethodCallReplacerVisitor.java b/sdg-core/src/main/java/tfm/graphs/sdg/MethodCallReplacerVisitor.java index dbec4bc..fd842c0 100644 --- a/sdg-core/src/main/java/tfm/graphs/sdg/MethodCallReplacerVisitor.java +++ b/sdg-core/src/main/java/tfm/graphs/sdg/MethodCallReplacerVisitor.java @@ -151,16 +151,10 @@ class MethodCallReplacerVisitor extends VoidVisitorAdapter { // Handle data dependency: Remove arc from method call node and add it to IN node - List inDataDependencies = sdg.incomingEdgesOf(originalMethodCallNode).stream() - .filter(arc -> arc.isDataDependencyArc() && Objects.equals(arc.getLabel(), argument.toString())) - .map(Arc::asDataDependencyArc) - .collect(Collectors.toList()); - - for (DataDependencyArc arc : inDataDependencies) { - GraphNode dataDependencySource = sdg.getEdgeSource(arc); - sdg.removeEdge(arc); - sdg.addDataDependencyArc(dataDependencySource, argumentInNode, argument.toString()); - } + sdg.incomingEdgesOf(originalMethodCallNode).stream() + .filter(Arc::isDataDependencyArc) + .filter(arc -> Objects.equals(arc.getLabel(), argument.toString())) + .forEach(arc -> sdg.addDataDependencyArc(sdg.getEdgeSource(arc), argumentInNode, argument.toString())); // Now, find the corresponding method declaration's in node and link argument node with it @@ -230,16 +224,10 @@ class MethodCallReplacerVisitor extends VoidVisitorAdapter { // Handle data dependency: remove arc from method call node and add it to OUT node - List outDataDependencies = sdg.outgoingEdgesOf(originalMethodCallNode).stream() - .filter(arc -> arc.isDataDependencyArc() && Objects.equals(arc.getLabel(), argument.toString())) - .map(Arc::asDataDependencyArc) - .collect(Collectors.toList()); - - for (DataDependencyArc arc : outDataDependencies) { - GraphNode dataDependencyTarget = sdg.getEdgeTarget(arc); - sdg.removeEdge(arc); - sdg.addDataDependencyArc(argumentOutNode, dataDependencyTarget, argument.toString()); - } + sdg.outgoingEdgesOf(originalMethodCallNode).stream() + .filter(Arc::isDataDependencyArc) + .filter(arc -> Objects.equals(arc.getLabel(), argument.toString())) + .forEach(arc -> sdg.addDataDependencyArc(argumentOutNode, sdg.getEdgeTarget(arc), argument.toString())); if (optionalParameterOutNode.isPresent()) { sdg.addParameterInOutArc(optionalParameterOutNode.get(), argumentOutNode); -- GitLab From ec490f8e572702e94274145bd18960b6817e21ab Mon Sep 17 00:00:00 2001 From: Carlos Galindo Date: Thu, 11 Jun 2020 11:57:55 +0200 Subject: [PATCH 05/10] Removed circular dependencies in Maven Side-effect: PDGTests cannot access GraphLog. --- sdg-core/pom.xml | 7 ------- .../src/test/java/tfm/graphs/pdg/PDGTests.java | 18 ++++++++---------- 2 files changed, 8 insertions(+), 17 deletions(-) diff --git a/sdg-core/pom.xml b/sdg-core/pom.xml index e0b4d31..88d56ba 100644 --- a/sdg-core/pom.xml +++ b/sdg-core/pom.xml @@ -45,12 +45,5 @@ RELEASE test - - - tfm - sdg-cli - 1.0-SNAPSHOT - test - diff --git a/sdg-core/src/test/java/tfm/graphs/pdg/PDGTests.java b/sdg-core/src/test/java/tfm/graphs/pdg/PDGTests.java index bf80587..106858d 100644 --- a/sdg-core/src/test/java/tfm/graphs/pdg/PDGTests.java +++ b/sdg-core/src/test/java/tfm/graphs/pdg/PDGTests.java @@ -9,8 +9,6 @@ import com.github.javaparser.ast.stmt.ThrowStmt; import com.github.javaparser.ast.stmt.TryStmt; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; -import tfm.cli.GraphLog; -import tfm.cli.PDGLog; import tfm.graphs.augmented.ACFG; import tfm.graphs.augmented.APDG; import tfm.graphs.augmented.PPDG; @@ -59,14 +57,14 @@ public class PDGTests { private void runPdg(File file, String methodName, MethodDeclaration root, PDG pdg) throws IOException { pdg.build(root); - GraphLog graphLog = new PDGLog(pdg); - graphLog.log(); - try { - graphLog.generateImages(file.getPath() + "-" + methodName); - } catch (Exception e) { - System.err.println("Could not generate PNG"); - System.err.println(e.getMessage()); - } +// GraphLog graphLog = new PDGLog(pdg); +// graphLog.log(); +// try { +// graphLog.generateImages(file.getPath() + "-" + methodName); +// } catch (Exception e) { +// System.err.println("Could not generate PNG"); +// System.err.println(e.getMessage()); +// } } @ParameterizedTest(name = "[{index}] {0} ({1})") -- GitLab From 9ae5825e5a6c707f4e7d5b2a8ec28637072f28f6 Mon Sep 17 00:00:00 2001 From: Carlos Galindo Date: Thu, 11 Jun 2020 11:59:24 +0200 Subject: [PATCH 06/10] Added FileLineSlicingCriterion, to select nodes in the SDG. --- .../src/main/java/tfm/graphs/sdg/SDG.java | 7 ++++ .../tfm/slicing/FileLineSlicingCriterion.java | 42 +++++++++++++++++++ .../java/tfm/slicing/LineNumberCriterion.java | 10 ++++- 3 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 sdg-core/src/main/java/tfm/slicing/FileLineSlicingCriterion.java diff --git a/sdg-core/src/main/java/tfm/graphs/sdg/SDG.java b/sdg-core/src/main/java/tfm/graphs/sdg/SDG.java index 21224da..5e092f2 100644 --- a/sdg-core/src/main/java/tfm/graphs/sdg/SDG.java +++ b/sdg-core/src/main/java/tfm/graphs/sdg/SDG.java @@ -31,11 +31,16 @@ public class SDG extends Graph implements Sliceable, Buildable methodCFGMap; + private NodeList compilationUnits; public SDG() { this.methodCFGMap = new HashMap<>(); } + public NodeList getCompilationUnits() { + return compilationUnits; + } + @Override public Slice slice(SlicingCriterion slicingCriterion) { Optional> optSlicingNode = slicingCriterion.findNode(this); @@ -47,6 +52,8 @@ public class SDG extends Graph implements Sliceable, Buildable nodeList) { nodeList.accept(new SDGBuilder(this), new Context()); + compilationUnits = nodeList; + built = true; } @Override diff --git a/sdg-core/src/main/java/tfm/slicing/FileLineSlicingCriterion.java b/sdg-core/src/main/java/tfm/slicing/FileLineSlicingCriterion.java new file mode 100644 index 0000000..21aaea6 --- /dev/null +++ b/sdg-core/src/main/java/tfm/slicing/FileLineSlicingCriterion.java @@ -0,0 +1,42 @@ +package tfm.slicing; + +import com.github.javaparser.ast.CompilationUnit; +import com.github.javaparser.ast.NodeList; +import com.github.javaparser.ast.stmt.Statement; +import tfm.graphs.sdg.SDG; +import tfm.nodes.GraphNode; + +import java.io.File; +import java.util.Optional; + +public class FileLineSlicingCriterion extends LineNumberCriterion { + protected final File file; + + public FileLineSlicingCriterion(File file, int lineNumber) { + super(lineNumber, null); + this.file = file; + } + + @Override + public Optional> findNode(SDG graph) { + Optional optCu = findCompilationUnit(graph.getCompilationUnits()); + if (optCu.isEmpty()) + return Optional.empty(); + return optCu.get().findFirst(Statement.class, this::matchesLine).flatMap(graph::findNodeByASTNode); + } + + protected Optional findCompilationUnit(NodeList cus) { + for (CompilationUnit cu : cus) { + Optional optStorage = cu.getStorage(); + if (optStorage.isPresent() && optStorage.get().getFileName().equals(file.getName()) + && optStorage.get().getDirectory().equals(file.getParentFile().toPath())) + return Optional.of(cu); + } + return Optional.empty(); + } + + @Override + public String toString() { + return file + "#" + lineNumber + ":" + variable; + } +} diff --git a/sdg-core/src/main/java/tfm/slicing/LineNumberCriterion.java b/sdg-core/src/main/java/tfm/slicing/LineNumberCriterion.java index 3eb8ead..f7a2385 100644 --- a/sdg-core/src/main/java/tfm/slicing/LineNumberCriterion.java +++ b/sdg-core/src/main/java/tfm/slicing/LineNumberCriterion.java @@ -1,5 +1,6 @@ package tfm.slicing; +import com.github.javaparser.Position; import com.github.javaparser.ast.Node; import tfm.graphs.cfg.CFG; import tfm.graphs.pdg.PDG; @@ -10,8 +11,9 @@ import tfm.utils.Logger; import java.util.Optional; public class LineNumberCriterion extends SlicingCriterion { + protected static final Position DEFAULT_POSITION = new Position(0, 0); - private int lineNumber; + protected int lineNumber; public LineNumberCriterion(int lineNumber, String variable) { super(variable); @@ -30,7 +32,7 @@ public class LineNumberCriterion extends SlicingCriterion { return graph.vertexSet().stream().filter(node -> { Node astNode = node.getAstNode(); - if (!astNode.getBegin().isPresent() || !astNode.getEnd().isPresent()) + if (astNode.getBegin().isEmpty() || astNode.getEnd().isEmpty()) return false; int begin = astNode.getBegin().get().line; @@ -47,6 +49,10 @@ public class LineNumberCriterion extends SlicingCriterion { return Optional.empty(); } + protected boolean matchesLine(Node node) { + return node.getBegin().orElse(DEFAULT_POSITION).line == lineNumber; + } + @Override public String toString() { return String.format("(%s, %s)", lineNumber, variable); -- GitLab From 31f15bccf4002172b6eae5d9e9b970aa55c7a99d Mon Sep 17 00:00:00 2001 From: Carlos Galindo Date: Thu, 11 Jun 2020 13:48:12 +0200 Subject: [PATCH 07/10] Extended Slice and SliceAstVisitor to handle multiple methods, classes and files --- sdg-core/src/main/java/tfm/slicing/Slice.java | 37 +++- .../java/tfm/slicing/SliceAstVisitor.java | 118 ------------ .../java/tfm/slicing/SlicePruneVisitor.java | 171 ++++++++++++++++++ 3 files changed, 207 insertions(+), 119 deletions(-) delete mode 100644 sdg-core/src/main/java/tfm/slicing/SliceAstVisitor.java create mode 100644 sdg-core/src/main/java/tfm/slicing/SlicePruneVisitor.java diff --git a/sdg-core/src/main/java/tfm/slicing/Slice.java b/sdg-core/src/main/java/tfm/slicing/Slice.java index 89f9d79..48ee7aa 100644 --- a/sdg-core/src/main/java/tfm/slicing/Slice.java +++ b/sdg-core/src/main/java/tfm/slicing/Slice.java @@ -1,6 +1,8 @@ package tfm.slicing; +import com.github.javaparser.ast.CompilationUnit; import com.github.javaparser.ast.Node; +import com.github.javaparser.ast.NodeList; import com.github.javaparser.ast.body.MethodDeclaration; import com.github.javaparser.ast.visitor.CloneVisitor; import com.github.javaparser.ast.visitor.Visitable; @@ -43,6 +45,39 @@ public class Slice { return Set.copyOf(map.values()); } + /** + * Organize all nodes pertaining to this slice in one or more CompilationUnits. + * CompilationUnits themselves need not be part of the slice to be included if any of their + * components are present. + */ + public NodeList toAst() { + Map> cuMap = new HashMap<>(); + // Build key set + nodes.stream().filter(n -> n instanceof CompilationUnit) + .forEach(cu -> cuMap.put((CompilationUnit) cu, new HashSet<>())); + // Add each node to the corresponding bucket of the map + // Nodes may not belong to a compilation unit (fictional nodes), and they are skipped for the slice. + for (Node node : nodes) + node.findCompilationUnit() + .flatMap(n -> Optional.ofNullable(cuMap.get(n))) + .ifPresent(set -> set.add(node)); + // Traverse the AST of each compilation unit, creating a copy and + // removing any element not present in the slice. + NodeList cus = new NodeList<>(); + SlicePruneVisitor sliceVisitor = new SlicePruneVisitor(); + CloneVisitor cloneVisitor = new CloneVisitor(); + for (Map.Entry> entry : cuMap.entrySet()) { + CompilationUnit clone = (CompilationUnit) entry.getKey().accept(cloneVisitor, null); + assert entry.getKey().getStorage().isPresent(); + clone.setStorage(entry.getKey().getStorage().get().getPath()); + Visitable sliced = clone.accept(sliceVisitor, entry.getValue()); + assert sliced instanceof CompilationUnit; + cus.add((CompilationUnit) sliced); + } + return cus; + } + + @Deprecated public Node getAst() { List> methods = map.values().stream().filter(e -> e.getAstNode() instanceof MethodDeclaration).collect(Collectors.toList()); if (methods.size() == 1) { @@ -63,7 +98,7 @@ public class Slice { private MethodDeclaration getMethodAst(Node node) { Visitable clone = node.accept(new CloneVisitor(), null); assert clone instanceof MethodDeclaration; - clone.accept(new SliceAstVisitor(), this); + clone.accept(new SlicePruneVisitor(), nodes); return ((MethodDeclaration) clone); } } diff --git a/sdg-core/src/main/java/tfm/slicing/SliceAstVisitor.java b/sdg-core/src/main/java/tfm/slicing/SliceAstVisitor.java deleted file mode 100644 index 63f187c..0000000 --- a/sdg-core/src/main/java/tfm/slicing/SliceAstVisitor.java +++ /dev/null @@ -1,118 +0,0 @@ -package tfm.slicing; - -import com.github.javaparser.ast.Node; -import com.github.javaparser.ast.NodeList; -import com.github.javaparser.ast.expr.BooleanLiteralExpr; -import com.github.javaparser.ast.nodeTypes.NodeWithBody; -import com.github.javaparser.ast.stmt.*; -import com.github.javaparser.ast.visitor.ModifierVisitor; -import com.github.javaparser.ast.visitor.Visitable; - -import java.util.stream.Collectors; - -public class SliceAstVisitor extends ModifierVisitor { - @Override - public Visitable visit(BreakStmt n, Slice arg) { - return arg.contains(n) ? n : null; - } - - @Override - public Visitable visit(ContinueStmt n, Slice arg) { - return arg.contains(n) ? n : null; - } - - @Override - public Visitable visit(DoStmt n, Slice arg) { - boolean keep = arg.contains(n); - super.visit(n, arg); - fillBody(n); - return keep ? n : null; - } - - @Override - public Visitable visit(ForEachStmt n, Slice arg) { - boolean keep = arg.contains(n); - super.visit(n, arg); - fillBody(n); - return keep ? n : null; - } - - @Override - public Visitable visit(ForStmt n, Slice arg) { - boolean keep = arg.contains(n); - super.visit(n, arg); - n.setInitialization(new NodeList<>(n.getInitialization().stream() - .filter(arg::contains).collect(Collectors.toList()))); - n.setUpdate(new NodeList<>(n.getUpdate().stream() - .filter(arg::contains).collect(Collectors.toList()))); - fillBody(n); - if (keep) - return n; - if (n.getInitialization().isEmpty() && n.getUpdate().isEmpty()) - return null; - return new ForStmt(n.getInitialization(), new BooleanLiteralExpr(false), - n.getUpdate(), n.getBody()); - } - - @Override - public Visitable visit(WhileStmt n, Slice arg) { - boolean keep = arg.contains(n); - super.visit(n, arg); - fillBody(n); - return keep ? n : null; - } - - @Override - public Visitable visit(IfStmt n, Slice arg) { - boolean keep = arg.contains(n); - super.visit(n, arg); - if (n.getThenStmt() == null) - n.setThenStmt(new EmptyStmt()); - return keep ? n : null; - } - - @Override - public Visitable visit(LabeledStmt n, Slice arg) { - super.visit(n, arg); - return n.getStatement() != null ? n : null; - } - - @Override - public Visitable visit(ReturnStmt n, Slice arg) { - return arg.contains(n) ? n : null; - } - - @Override - public Visitable visit(ThrowStmt n, Slice arg) { - return arg.contains(n) ? n : null; - } - - @Override - public Visitable visit(SwitchEntryStmt n, Slice arg) { - boolean keep = arg.contains(n); - super.visit(n, arg); - if (!n.getStatements().isEmpty()) - return n; - return keep ? n : null; - } - - @Override - public Visitable visit(SwitchStmt n, Slice arg) { - boolean keep = arg.contains(n); - super.visit(n, arg); - return keep ? n : null; - } - - @Override - public Visitable visit(ExpressionStmt n, Slice arg) { - return arg.contains(n) ? n : null; - } - - private void fillBody(Node n) { - if (!(n instanceof NodeWithBody)) - return; - NodeWithBody nb = ((NodeWithBody) n); - if (nb.getBody() == null) - nb.setBody(new EmptyStmt()); - } -} diff --git a/sdg-core/src/main/java/tfm/slicing/SlicePruneVisitor.java b/sdg-core/src/main/java/tfm/slicing/SlicePruneVisitor.java new file mode 100644 index 0000000..f66fadc --- /dev/null +++ b/sdg-core/src/main/java/tfm/slicing/SlicePruneVisitor.java @@ -0,0 +1,171 @@ +package tfm.slicing; + +import com.github.javaparser.ast.CompilationUnit; +import com.github.javaparser.ast.Node; +import com.github.javaparser.ast.NodeList; +import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration; +import com.github.javaparser.ast.body.ConstructorDeclaration; +import com.github.javaparser.ast.body.FieldDeclaration; +import com.github.javaparser.ast.body.MethodDeclaration; +import com.github.javaparser.ast.expr.BooleanLiteralExpr; +import com.github.javaparser.ast.nodeTypes.NodeWithBody; +import com.github.javaparser.ast.stmt.*; +import com.github.javaparser.ast.visitor.ModifierVisitor; +import com.github.javaparser.ast.visitor.Visitable; + +import java.util.Set; +import java.util.stream.Collectors; + +public class SlicePruneVisitor extends ModifierVisitor> { + // ========== Utility methods ========== + + protected void fillBody(Node n) { + if (!(n instanceof NodeWithBody)) + return; + NodeWithBody nb = ((NodeWithBody) n); + if (nb.getBody() == null) + nb.setBody(new EmptyStmt()); + } + + // ========== File visitors ========== + + @Override + public Visitable visit(CompilationUnit n, Set arg) { + boolean keep = arg.contains(n); + Visitable v = super.visit(n, arg); + return keep ? v : null; + } + + @Override + public Visitable visit(ClassOrInterfaceDeclaration n, Set arg) { + boolean keep = arg.contains(n); + Visitable v = super.visit(n, arg); + return keep ? v : null; + } + + // ========== Class body visitors ========== + + @Override + public Visitable visit(MethodDeclaration n, Set arg) { + boolean keep = arg.contains(n); + Visitable v = super.visit(n, arg); + return keep ? v : null; + } + + @Override + public Visitable visit(ConstructorDeclaration n, Set arg) { + boolean keep = arg.contains(n); + Visitable v = super.visit(n, arg); + return keep ? v : null; + } + + @Override + public Visitable visit(FieldDeclaration n, Set arg) { + boolean keep = arg.contains(n); + Visitable v = super.visit(n, arg); + return keep ? v : null; + } + +// ========== Method body visitors ========== + // 3 alternatives: + // a. Without relevant children and included if on the slice or not (e.g. ExpressionStmt) + // b. With relevant children and included if of the slice or not, children are discarded if not included (e.g. WhileStmt) + // c. With relevant children and included if any children is included OR if on the slice (e.g. SwitchEntryStmt, LabeledStmt) + + @Override + public Visitable visit(BreakStmt n, Set arg) { + return arg.contains(n) ? n : null; + } + + @Override + public Visitable visit(ContinueStmt n, Set arg) { + return arg.contains(n) ? n : null; + } + + @Override + public Visitable visit(DoStmt n, Set arg) { + boolean keep = arg.contains(n); + super.visit(n, arg); + fillBody(n); + return keep ? n : null; + } + + @Override + public Visitable visit(ForEachStmt n, Set arg) { + boolean keep = arg.contains(n); + super.visit(n, arg); + fillBody(n); + return keep ? n : null; + } + + @Override + public Visitable visit(ForStmt n, Set arg) { + boolean keep = arg.contains(n); + super.visit(n, arg); + n.setInitialization(new NodeList<>(n.getInitialization().stream() + .filter(arg::contains).collect(Collectors.toList()))); + n.setUpdate(new NodeList<>(n.getUpdate().stream() + .filter(arg::contains).collect(Collectors.toList()))); + fillBody(n); + if (keep) + return n; + if (n.getInitialization().isEmpty() && n.getUpdate().isEmpty()) + return null; + return new ForStmt(n.getInitialization(), new BooleanLiteralExpr(false), + n.getUpdate(), n.getBody()); + } + + @Override + public Visitable visit(WhileStmt n, Set arg) { + boolean keep = arg.contains(n); + super.visit(n, arg); + fillBody(n); + return keep ? n : null; + } + + @Override + public Visitable visit(IfStmt n, Set arg) { + boolean keep = arg.contains(n); + super.visit(n, arg); + if (n.getThenStmt() == null) + n.setThenStmt(new EmptyStmt()); + return keep ? n : null; + } + + @Override + public Visitable visit(LabeledStmt n, Set arg) { + super.visit(n, arg); + return n.getStatement() != null ? n : null; + } + + @Override + public Visitable visit(ReturnStmt n, Set arg) { + return arg.contains(n) ? n : null; + } + + @Override + public Visitable visit(ThrowStmt n, Set arg) { + return arg.contains(n) ? n : null; + } + + @Override + public Visitable visit(SwitchEntryStmt n, Set arg) { + boolean keep = arg.contains(n); + super.visit(n, arg); + if (!n.getStatements().isEmpty()) + return n; + return keep ? n : null; + } + + @Override + public Visitable visit(SwitchStmt n, Set arg) { + boolean keep = arg.contains(n); + super.visit(n, arg); + return keep ? n : null; + } + + @Override + public Visitable visit(ExpressionStmt n, Set arg) { + return arg.contains(n) ? n : null; + } +} -- GitLab From e68a2d42753058f2a2b86b0476e02923bd39e9b4 Mon Sep 17 00:00:00 2001 From: Carlos Galindo Date: Thu, 11 Jun 2020 15:20:22 +0200 Subject: [PATCH 08/10] Slicer CLI entrypoint * Includes minor fixes to slicing criterion and slice to AST conversion * Minor changes in logging, to avoid logging without error/warning --- sdg-cli/src/main/java/tfm/cli/Main.java | 90 ------ sdg-cli/src/main/java/tfm/cli/Slicer.java | 258 ++++++++++++++++++ .../graphs/sdg/MethodCallReplacerVisitor.java | 4 - .../tfm/slicing/FileLineSlicingCriterion.java | 2 +- sdg-core/src/main/java/tfm/slicing/Slice.java | 18 +- .../java/tfm/slicing/SlicePruneVisitor.java | 4 +- 6 files changed, 269 insertions(+), 107 deletions(-) delete mode 100644 sdg-cli/src/main/java/tfm/cli/Main.java create mode 100644 sdg-cli/src/main/java/tfm/cli/Slicer.java diff --git a/sdg-cli/src/main/java/tfm/cli/Main.java b/sdg-cli/src/main/java/tfm/cli/Main.java deleted file mode 100644 index 85a9e62..0000000 --- a/sdg-cli/src/main/java/tfm/cli/Main.java +++ /dev/null @@ -1,90 +0,0 @@ -package tfm.cli; - -import com.github.javaparser.JavaParser; -import com.github.javaparser.ast.Node; -import com.github.javaparser.ast.NodeList; -import com.github.javaparser.ast.body.MethodDeclaration; -import tfm.graphs.Graph; -import tfm.graphs.cfg.CFG; -import tfm.graphs.pdg.PDG; -import tfm.graphs.sdg.SDG; -import tfm.utils.Logger; -import tfm.utils.Utils; - -import java.io.File; -import java.io.IOException; -import java.util.Objects; -import java.util.Optional; - -public class Main { - - public static final String PROGRAM = Utils.PROGRAMS_FOLDER + "sdg/Example1.java"; - public static final String GRAPH = GraphLog.PDG; - public static final String METHOD = "main"; - - public static void main(String[] args) throws IOException { - JavaParser.getStaticConfiguration().setAttributeComments(false); - - // File - File file = new File(PROGRAM); - Node root = JavaParser.parse(file); - - if (!METHOD.isEmpty()) { - Optional methodDeclarationOptional = root.findFirst(MethodDeclaration.class, - methodDeclaration -> Objects.equals(methodDeclaration.getNameAsString(), METHOD)); - - if (!methodDeclarationOptional.isPresent()) { - Logger.format("Method '%s' not found in '%s'. Exiting...", METHOD, PROGRAM); - return; - } - - root = methodDeclarationOptional.get(); - } - - // GraphLog - long t0 = System.nanoTime(); - Graph graph = getBuiltGraph(args.length == 1 ? args[0] : GRAPH, (MethodDeclaration) root); - long tf = System.nanoTime(); - long tt = tf - t0; - - GraphLog graphLog = getGraphLog(graph); - graphLog.log(); - graphLog.openVisualRepresentation(); - - Logger.log(); - Logger.format("Graph generated in %.2f ms", tt / 10e6); - } - - private static Graph getBuiltGraph(String graph, MethodDeclaration method) { - switch (graph) { - case GraphLog.CFG: - CFG cfg = new CFG(); - cfg.build(method); - return cfg; - case GraphLog.PDG: - PDG pdg = new PDG(); - pdg.build(method); - return pdg; - case GraphLog.SDG: - SDG sdg = new SDG(); - sdg.build(new NodeList<>(method.findCompilationUnit().get())); - return sdg; - default: - Logger.log("Unkown graph type"); - System.exit(1); - return null; - } - } - - private static GraphLog getGraphLog(Graph graph) { - if (graph instanceof CFG) - return new CFGLog((CFG) graph); - else if (graph instanceof PDG) - return new PDGLog((PDG) graph); - else if (graph instanceof SDG) - return new SDGLog((SDG) graph); - Logger.log("Unknown graph type"); - System.exit(1); - return null; - } -} diff --git a/sdg-cli/src/main/java/tfm/cli/Slicer.java b/sdg-cli/src/main/java/tfm/cli/Slicer.java new file mode 100644 index 0000000..f6c46c9 --- /dev/null +++ b/sdg-cli/src/main/java/tfm/cli/Slicer.java @@ -0,0 +1,258 @@ +package tfm.cli; + +import com.github.javaparser.JavaParser; +import com.github.javaparser.ast.CompilationUnit; +import com.github.javaparser.ast.NodeList; +import com.github.javaparser.ast.comments.BlockComment; +import com.github.javaparser.ast.nodeTypes.NodeWithName; +import com.github.javaparser.symbolsolver.JavaSymbolSolver; +import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver; +import com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver; +import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver; +import org.apache.commons.cli.*; +import tfm.graphs.sdg.SDG; +import tfm.slicing.FileLineSlicingCriterion; +import tfm.slicing.Slice; +import tfm.slicing.SlicingCriterion; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.PrintWriter; +import java.util.*; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class Slicer { + protected static final Pattern SC_PATTERN; + protected static final File DEFAULT_OUTPUT_DIR = new File("./slice/"); + protected static final Options OPTIONS = new Options(); + + static { + String fileRe = "(?[^#]+\\.java)"; + String lineRe = "(?[1-9]\\d*)"; + String varsRe = "(?[a-zA-Z_]\\w*(?:,[a-zA-Z_]\\w*)*)"; + String numsRe = "(?[1-9]\\d*(?:,[1-9]\\d*)*)"; + SC_PATTERN = Pattern.compile(fileRe + "#" + lineRe + "(?::" + varsRe + "(?:!" + numsRe + ")?)?"); + } + + static { + OptionGroup criterionOptionGroup = new OptionGroup(); + criterionOptionGroup.addOption(Option + .builder("f").longOpt("file") + .hasArg().argName("CriterionFile.java").type(File.class) + .desc("The file that contains the slicing criterion.") + .build()); + criterionOptionGroup.addOption(Option + .builder("l").longOpt("line") + .hasArg().argName("lineNumber").type(Integer.class) + .desc("The line that contains the statement of the slicing criterion.") + .build()); + criterionOptionGroup.addOption(Option + .builder("v").longOpt("var") + .hasArgs().argName("variableName").valueSeparator(',') + .desc("The name of the variable of the slicing criterion. Not setting this option is" + + " equivalent to selecting an empty set; setting multiple variables is allowed," + + " separated by commas") + .build()); + criterionOptionGroup.addOption(Option + .builder("n").longOpt("number") + .hasArgs().argName("occurrenceNumber").valueSeparator(',') + .desc("The occurrence number of the variable(s) selected. If this argument is not set, it will" + + " default to the first appearance of each variable. If the occurrence number must be set" + + " for every variable in the same order.") + .build()); + OPTIONS.addOptionGroup(criterionOptionGroup); + OPTIONS.addOption(Option + .builder("c").longOpt("criterion") + .hasArg().argName("file#line[:var[!occurrence]]") + .desc("The slicing criterion, in the format \"file#line:var\". Optionally, the occurrence can be" + + " appended as \"!occurrence\". This option replaces \"-f\", \"-l\", \"-v\" and \"-n\", and" + + " functions in a similar way: the variable and occurrence may be skipped or declared multiple times.") + .build()); + OPTIONS.addOption(Option + .builder("i").longOpt("include") + .hasArgs().argName("directory[,directory,...]").valueSeparator(',') + .desc("Includes the directories listed in the search for methods called from the slicing criterion " + + "(directly or transitively). Methods that are not included here or part of the JRE, including" + + " third party libraries will not be analyzed, resulting in less precise slicing.") + .build()); + OPTIONS.addOption(Option + .builder("o").longOpt("output") + .hasArg().argName("outputDir") + .desc("The directory where the sliced source code should be placed. By default, it is placed at " + + DEFAULT_OUTPUT_DIR) + .build()); + OPTIONS.addOption(Option + .builder("h").longOpt("help") + .desc("Shows this text") + .build()); + } + + private final Set dirIncludeSet = new HashSet<>(); + private File outputDir = DEFAULT_OUTPUT_DIR; + private File scFile; + private int scLine; + private final List scVars = new ArrayList<>(); + private final List scVarOccurrences = new ArrayList<>(); + + public Slicer(String... cliArgs) throws ParseException { + CommandLine cl = new DefaultParser().parse(OPTIONS, cliArgs); + if (cl.hasOption('h')) + throw new ParseException(OPTIONS.toString()); + if (cl.hasOption('c')) { + Matcher matcher = SC_PATTERN.matcher(cl.getOptionValue("criterion")); + if (!matcher.matches()) + throw new ParseException("Invalid format for slicing criterion, see --help for more details"); + setScFile(matcher.group("file")); + setScLine(Integer.parseInt(matcher.group("line"))); + String vars = matcher.group("vars"); + String nums = matcher.group("nums"); + if (vars != null) { + if (nums != null) + setScVars(vars.split(","), nums.split(",")); + else + setScVars(vars.split(",")); + } + } else if (cl.hasOption('f') && cl.hasOption('l')) { + setScFile(cl.getOptionValue('f')); + setScLine((Integer) cl.getParsedOptionValue("l")); + if (cl.hasOption('v')) { + if (cl.hasOption('n')) + setScVars(cl.getOptionValues('v'), cl.getOptionValues('n')); + else + setScVars(cl.getOptionValues('v')); + } + } else { + throw new ParseException("Slicing criterion not specified: either use \"-c\" or \"-f\" and \"l\"."); + } + + if (cl.hasOption('o')) + outputDir = (File) cl.getParsedOptionValue("o"); + + if (cl.hasOption('i')) { + for (String str : cl.getOptionValues('i')) { + File dir = new File(str); + if (!dir.isDirectory()) + throw new ParseException("One of the include directories is not a directory or isn't accesible: " + str); + dirIncludeSet.add(dir); + } + } + } + + private void setScFile(String fileName) throws ParseException { + File file = new File(fileName); + if (!(file.exists() && file.isFile())) + throw new ParseException("Slicing criterion file is not an existing file."); + scFile = file; + } + + private void setScLine(int line) throws ParseException { + if (line <= 0) + throw new ParseException("The line of the slicing criterion must be strictly greater than zero."); + scLine = line; + } + + private void setScVars(String[] scVars) throws ParseException { + String[] array = new String[scVars.length]; + Arrays.fill(array, "1"); + setScVars(scVars, array); + } + + private void setScVars(String[] scVars, String[] scOccurrences) throws ParseException { + if (scVars.length != scOccurrences.length) + throw new ParseException("If the number of occurrence is specified, it must be specified once per variable."); + try { + for (int i = 0; i < scVars.length; i++) { + this.scVars.add(scVars[i]); + int n = Integer.parseUnsignedInt(scOccurrences[i]); + if (n <= 0) + throw new ParseException("The number of occurrence must be larger than 0."); + this.scVarOccurrences.add(n); + } + } catch (NumberFormatException e) { + throw new ParseException(e.getMessage()); + } + } + + public Set getDirIncludeSet() { + return Collections.unmodifiableSet(dirIncludeSet); + } + + public File getOutputDir() { + return outputDir; + } + + public File getScFile() { + return scFile; + } + + public int getScLine() { + return scLine; + } + + public List getScVars() { + return Collections.unmodifiableList(scVars); + } + + public List getScVarOccurrences() { + return Collections.unmodifiableList(scVarOccurrences); + } + + public void slice() throws ParseException { + // Configure JavaParser + CombinedTypeSolver combinedTypeSolver = new CombinedTypeSolver(); + combinedTypeSolver.add(new ReflectionTypeSolver(true)); + for (File directory : dirIncludeSet) + combinedTypeSolver.add(new JavaParserTypeSolver(directory)); + JavaParser.getStaticConfiguration().setSymbolResolver(new JavaSymbolSolver(combinedTypeSolver)); + JavaParser.getStaticConfiguration().setAttributeComments(false); + + // Build the SDG + NodeList units = new NodeList<>(); + try { + for (File directory : dirIncludeSet) + units.add(JavaParser.parse(directory)); + CompilationUnit scUnit = JavaParser.parse(scFile); + if (!units.contains(scUnit)) + units.add(scUnit); + } catch (FileNotFoundException e) { + throw new ParseException(e.getMessage()); + } + SDG sdg = new SDG(); + sdg.build(units); + + // Slice the SDG + SlicingCriterion sc = new FileLineSlicingCriterion(scFile, scLine); + Slice slice = sdg.slice(sc); + + // Convert the slice to code and output the result to `outputDir` + for (CompilationUnit cu : slice.toAst()) { + if (cu.getStorage().isEmpty()) + throw new IllegalStateException("A synthetic CompilationUnit was discovered, with no file associated to it."); + String packagePath = cu.getPackageDeclaration().map(NodeWithName::getNameAsString).orElse("").replace(".", "/"); + File packageDir = new File(outputDir, packagePath); + packageDir.mkdirs(); + File javaFile = new File(packageDir, cu.getStorage().get().getFileName()); + try (PrintWriter pw = new PrintWriter(javaFile)) { + pw.print(new BlockComment(getDisclaimer(cu.getStorage().get()))); + pw.print(cu); + } catch (FileNotFoundException e) { + System.err.println("Could not write file " + javaFile); + } + } + } + + protected String getDisclaimer(CompilationUnit.Storage s) { + return String.format("\n\tThis file was automatically generated as part of a slice with criterion" + + "\n\tfile: %s, line: %d, variable(s): %s\n\tOriginal file: %s\n", + scFile, scLine, String.join(", ", scVars), s.getPath()); + } + + public static void main(String... args) { + try { + new Slicer(args).slice(); + } catch (ParseException e) { + System.err.println("Error parsing the arguments!\n" + e.getMessage()); + } + } +} diff --git a/sdg-core/src/main/java/tfm/graphs/sdg/MethodCallReplacerVisitor.java b/sdg-core/src/main/java/tfm/graphs/sdg/MethodCallReplacerVisitor.java index fd842c0..fda8e47 100644 --- a/sdg-core/src/main/java/tfm/graphs/sdg/MethodCallReplacerVisitor.java +++ b/sdg-core/src/main/java/tfm/graphs/sdg/MethodCallReplacerVisitor.java @@ -10,7 +10,6 @@ import com.github.javaparser.ast.expr.*; import com.github.javaparser.ast.stmt.*; import com.github.javaparser.ast.visitor.VoidVisitorAdapter; import tfm.arcs.Arc; -import tfm.arcs.pdg.DataDependencyArc; import tfm.graphs.cfg.CFG; import tfm.nodes.GraphNode; import tfm.nodes.TypeNodeFactory; @@ -19,7 +18,6 @@ import tfm.utils.Context; import tfm.utils.Logger; import java.util.*; -import java.util.stream.Collectors; class MethodCallReplacerVisitor extends VoidVisitorAdapter { @@ -94,8 +92,6 @@ class MethodCallReplacerVisitor extends VoidVisitorAdapter { // .filter(Expression::isMethodCallExpr) // .forEach(expression -> expression.accept(this, context)); - Logger.log("MethodCallReplacerVisitor", context); - Optional> optMethodDeclGraphNode = methodCallExpr.resolve().toAst() .flatMap(sdg::findNodeByASTNode); diff --git a/sdg-core/src/main/java/tfm/slicing/FileLineSlicingCriterion.java b/sdg-core/src/main/java/tfm/slicing/FileLineSlicingCriterion.java index 21aaea6..df01201 100644 --- a/sdg-core/src/main/java/tfm/slicing/FileLineSlicingCriterion.java +++ b/sdg-core/src/main/java/tfm/slicing/FileLineSlicingCriterion.java @@ -29,7 +29,7 @@ public class FileLineSlicingCriterion extends LineNumberCriterion { for (CompilationUnit cu : cus) { Optional optStorage = cu.getStorage(); if (optStorage.isPresent() && optStorage.get().getFileName().equals(file.getName()) - && optStorage.get().getDirectory().equals(file.getParentFile().toPath())) + && optStorage.get().getDirectory().toAbsolutePath().equals(file.getParentFile().toPath().toAbsolutePath())) return Optional.of(cu); } return Optional.empty(); diff --git a/sdg-core/src/main/java/tfm/slicing/Slice.java b/sdg-core/src/main/java/tfm/slicing/Slice.java index 48ee7aa..485a5da 100644 --- a/sdg-core/src/main/java/tfm/slicing/Slice.java +++ b/sdg-core/src/main/java/tfm/slicing/Slice.java @@ -52,15 +52,14 @@ public class Slice { */ public NodeList toAst() { Map> cuMap = new HashMap<>(); - // Build key set - nodes.stream().filter(n -> n instanceof CompilationUnit) - .forEach(cu -> cuMap.put((CompilationUnit) cu, new HashSet<>())); // Add each node to the corresponding bucket of the map // Nodes may not belong to a compilation unit (fictional nodes), and they are skipped for the slice. - for (Node node : nodes) - node.findCompilationUnit() - .flatMap(n -> Optional.ofNullable(cuMap.get(n))) - .ifPresent(set -> set.add(node)); + for (Node node : nodes) { + Optional cu = node.findCompilationUnit(); + if (cu.isEmpty()) continue; + cuMap.putIfAbsent(cu.get(), new HashSet<>()); + cuMap.get(cu.get()).add(node); + } // Traverse the AST of each compilation unit, creating a copy and // removing any element not present in the slice. NodeList cus = new NodeList<>(); @@ -70,9 +69,8 @@ public class Slice { CompilationUnit clone = (CompilationUnit) entry.getKey().accept(cloneVisitor, null); assert entry.getKey().getStorage().isPresent(); clone.setStorage(entry.getKey().getStorage().get().getPath()); - Visitable sliced = clone.accept(sliceVisitor, entry.getValue()); - assert sliced instanceof CompilationUnit; - cus.add((CompilationUnit) sliced); + clone.accept(sliceVisitor, entry.getValue()); + cus.add(clone); } return cus; } diff --git a/sdg-core/src/main/java/tfm/slicing/SlicePruneVisitor.java b/sdg-core/src/main/java/tfm/slicing/SlicePruneVisitor.java index f66fadc..f78e870 100644 --- a/sdg-core/src/main/java/tfm/slicing/SlicePruneVisitor.java +++ b/sdg-core/src/main/java/tfm/slicing/SlicePruneVisitor.java @@ -33,14 +33,14 @@ public class SlicePruneVisitor extends ModifierVisitor> { public Visitable visit(CompilationUnit n, Set arg) { boolean keep = arg.contains(n); Visitable v = super.visit(n, arg); - return keep ? v : null; + return keep || !((Node) v).getChildNodes().isEmpty() ? v : null; } @Override public Visitable visit(ClassOrInterfaceDeclaration n, Set arg) { boolean keep = arg.contains(n); Visitable v = super.visit(n, arg); - return keep ? v : null; + return keep || !((Node) v).getChildNodes().isEmpty() ? v : null; } // ========== Class body visitors ========== -- GitLab From ce166a7dc4800eae903fdeb2535445204f7486c9 Mon Sep 17 00:00:00 2001 From: jacosro Date: Sun, 14 Jun 2020 19:47:30 +0200 Subject: [PATCH 09/10] Updated pom.xml and added MANIFEST --- .gitignore | 2 ++ META-INF/MANIFEST.MF | 1 + pom.xml | 20 +++++++++++++++++++ sdg-cli/pom.xml | 6 ++++++ sdg-core/src/main/java/tfm/arcs/Arc.java | 12 ----------- .../main/java/tfm/arcs/sdg/SummaryArc.java | 7 ++----- .../src/main/java/tfm/graphs/sdg/SDG.java | 4 ---- 7 files changed, 31 insertions(+), 21 deletions(-) create mode 100644 META-INF/MANIFEST.MF create mode 100644 pom.xml diff --git a/.gitignore b/.gitignore index 1fab45f..ac48bd7 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,8 @@ # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 +.idea + # User-specific stuff .idea/**/workspace.xml .idea/**/tasks.xml diff --git a/META-INF/MANIFEST.MF b/META-INF/MANIFEST.MF new file mode 100644 index 0000000..2f4b568 --- /dev/null +++ b/META-INF/MANIFEST.MF @@ -0,0 +1 @@ +Manifest-Version: 1.0 diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..ebed3bf --- /dev/null +++ b/pom.xml @@ -0,0 +1,20 @@ + + + 4.0.0 + + tfm + tfm + 1.0-SNAPSHOT + + + 11 + 11 + + + + sdg-core + sdg-cli + + diff --git a/sdg-cli/pom.xml b/sdg-cli/pom.xml index c3aaed0..0984278 100644 --- a/sdg-cli/pom.xml +++ b/sdg-cli/pom.xml @@ -19,10 +19,16 @@ commons-cli 1.4 + + com.github.javaparser + javaparser-core + 3.9.1 + tfm sdg-core 1.0-SNAPSHOT + compile diff --git a/sdg-core/src/main/java/tfm/arcs/Arc.java b/sdg-core/src/main/java/tfm/arcs/Arc.java index e425b25..e9ecd30 100644 --- a/sdg-core/src/main/java/tfm/arcs/Arc.java +++ b/sdg-core/src/main/java/tfm/arcs/Arc.java @@ -7,7 +7,6 @@ import tfm.arcs.pdg.ControlDependencyArc; import tfm.arcs.pdg.DataDependencyArc; import tfm.arcs.sdg.CallArc; import tfm.arcs.sdg.ParameterInOutArc; -import tfm.arcs.sdg.ReturnArc; import tfm.arcs.sdg.SummaryArc; import tfm.nodes.GraphNode; @@ -95,17 +94,6 @@ public abstract class Arc extends DefaultEdge { throw new UnsupportedOperationException("Not a ParameterInOutArc"); } - /** @see ReturnArc */ - public final boolean isReturnArc() { - return this instanceof ReturnArc; - } - - public final ReturnArc asReturnArc() { - if (isReturnArc()) - return (ReturnArc) this; - throw new UnsupportedOperationException("Not a ReturnArc"); - } - /** @see SummaryArc */ public final boolean isSummaryArc() { return this instanceof SummaryArc; diff --git a/sdg-core/src/main/java/tfm/arcs/sdg/SummaryArc.java b/sdg-core/src/main/java/tfm/arcs/sdg/SummaryArc.java index c16b1a4..1eb583d 100644 --- a/sdg-core/src/main/java/tfm/arcs/sdg/SummaryArc.java +++ b/sdg-core/src/main/java/tfm/arcs/sdg/SummaryArc.java @@ -2,14 +2,11 @@ package tfm.arcs.sdg; import org.jgrapht.io.Attribute; import org.jgrapht.io.DefaultAttribute; +import tfm.arcs.Arc; import java.util.Map; -<<<<<<< HEAD:sdg-core/src/main/java/tfm/arcs/sdg/SummaryArc.java public class SummaryArc extends Arc { -======= -public class ReturnArc extends InterproceduralArc { ->>>>>>> 303de98... Created the SDG's ClassicSlicingAlgorithm:sdg-core/src/main/java/tfm/arcs/sdg/ReturnArc.java @Override public Map getDotAttributes() { Map map = super.getDotAttributes(); @@ -24,6 +21,6 @@ public class ReturnArc extends InterproceduralArc { @Override public boolean isInterproceduralOutputArc() { - return true; + return false; } } diff --git a/sdg-core/src/main/java/tfm/graphs/sdg/SDG.java b/sdg-core/src/main/java/tfm/graphs/sdg/SDG.java index 5e092f2..11bc80f 100644 --- a/sdg-core/src/main/java/tfm/graphs/sdg/SDG.java +++ b/sdg-core/src/main/java/tfm/graphs/sdg/SDG.java @@ -13,12 +13,8 @@ import tfm.arcs.sdg.SummaryArc; import tfm.graphs.Buildable; import tfm.graphs.Graph; import tfm.graphs.cfg.CFG; -<<<<<<< HEAD -import tfm.nodes.*; -======= import tfm.nodes.GraphNode; import tfm.slicing.ClassicSlicingAlgorithm; ->>>>>>> 303de98... Created the SDG's ClassicSlicingAlgorithm import tfm.slicing.Slice; import tfm.slicing.Sliceable; import tfm.slicing.SlicingCriterion; -- GitLab From e3feee1921fb0514c7c8043aa120de8fce2d413d Mon Sep 17 00:00:00 2001 From: Carlos Galindo Date: Tue, 23 Jun 2020 18:53:18 +0200 Subject: [PATCH 10/10] Fix method resolution: unresolved methods could cause an exception --- .../tfm/graphs/sdg/MethodCallReplacerVisitor.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/sdg-core/src/main/java/tfm/graphs/sdg/MethodCallReplacerVisitor.java b/sdg-core/src/main/java/tfm/graphs/sdg/MethodCallReplacerVisitor.java index fda8e47..277f073 100644 --- a/sdg-core/src/main/java/tfm/graphs/sdg/MethodCallReplacerVisitor.java +++ b/sdg-core/src/main/java/tfm/graphs/sdg/MethodCallReplacerVisitor.java @@ -9,6 +9,7 @@ import com.github.javaparser.ast.body.VariableDeclarator; import com.github.javaparser.ast.expr.*; import com.github.javaparser.ast.stmt.*; import com.github.javaparser.ast.visitor.VoidVisitorAdapter; +import com.github.javaparser.resolution.UnsolvedSymbolException; import tfm.arcs.Arc; import tfm.graphs.cfg.CFG; import tfm.nodes.GraphNode; @@ -92,15 +93,16 @@ class MethodCallReplacerVisitor extends VoidVisitorAdapter { // .filter(Expression::isMethodCallExpr) // .forEach(expression -> expression.accept(this, context)); - Optional> optMethodDeclGraphNode = methodCallExpr.resolve().toAst() - .flatMap(sdg::findNodeByASTNode); - - if (optMethodDeclGraphNode.isEmpty()) { - Logger.format("Not found: '%s'. Discarding", methodCallExpr); + GraphNode methodDeclarationNode; + try { + methodDeclarationNode = methodCallExpr.resolve().toAst() + .flatMap(sdg::findNodeByASTNode) + .orElseThrow(() -> new UnsolvedSymbolException("")); + } catch (UnsolvedSymbolException e) { + Logger.format("Method declaration not found: '%s'. Discarding", methodCallExpr); return; } - GraphNode methodDeclarationNode = optMethodDeclGraphNode.get(); MethodDeclaration methodDeclaration = methodDeclarationNode.getAstNode(); Optional optionalCFG = sdg.getMethodCFG(methodDeclaration); -- GitLab