From 9b2fde337ef1b7e28d8767e4cb64b4472c7a1ebb Mon Sep 17 00:00:00 2001 From: michaelyoukeim <110854872+michaelyoukeim@users.noreply.github.com> Date: Wed, 2 Oct 2024 16:55:09 +0200 Subject: [PATCH] Replaced removeTrap with removeExceptionalFlowFromAllBlocks --- .../core/graph/ForwardingStmtGraph.java | 6 ++- .../core/graph/ImmutableBlockStmtGraph.java | 2 +- .../java/sootup/core/graph/StmtGraph.java | 9 ++-- .../core/graph/MutableBlockStmtGraphTest.java | 50 ++++--------------- 4 files changed, 20 insertions(+), 47 deletions(-) diff --git a/sootup.core/src/main/java/sootup/core/graph/ForwardingStmtGraph.java b/sootup.core/src/main/java/sootup/core/graph/ForwardingStmtGraph.java index 4525946288e..80c4393323e 100644 --- a/sootup.core/src/main/java/sootup/core/graph/ForwardingStmtGraph.java +++ b/sootup.core/src/main/java/sootup/core/graph/ForwardingStmtGraph.java @@ -1,4 +1,5 @@ package sootup.core.graph; + /*- * #%L * Soot - a J*va Optimization Framework @@ -134,7 +135,8 @@ public List buildTraps() { } @Override - public void removeTrap(@Nonnull Trap trap) { - backingGraph.removeTrap(trap); + public void removeExceptionalFlowFromAllBlocks( + @Nonnull ClassType exceptionType, @Nonnull Stmt exceptionHandlerStmt) { + backingGraph.removeExceptionalFlowFromAllBlocks(exceptionType, exceptionHandlerStmt); } } diff --git a/sootup.core/src/main/java/sootup/core/graph/ImmutableBlockStmtGraph.java b/sootup.core/src/main/java/sootup/core/graph/ImmutableBlockStmtGraph.java index 5e4c7ee5537..84e0b4fe91a 100644 --- a/sootup.core/src/main/java/sootup/core/graph/ImmutableBlockStmtGraph.java +++ b/sootup.core/src/main/java/sootup/core/graph/ImmutableBlockStmtGraph.java @@ -144,7 +144,7 @@ public List buildTraps() { } @Override - public void removeTrap(@Nonnull Trap trap) { + public void removeExceptionalFlowFromAllBlocks(ClassType classType, Stmt exceptionHandlerStmt) { throw new UnsupportedOperationException("Not implemented yet!"); } diff --git a/sootup.core/src/main/java/sootup/core/graph/StmtGraph.java b/sootup.core/src/main/java/sootup/core/graph/StmtGraph.java index acdeb619f59..06eb6f9791f 100644 --- a/sootup.core/src/main/java/sootup/core/graph/StmtGraph.java +++ b/sootup.core/src/main/java/sootup/core/graph/StmtGraph.java @@ -1,4 +1,5 @@ package sootup.core.graph; + /*- * #%L * Soot - a J*va Optimization Framework @@ -155,12 +156,14 @@ public int degree(@Nonnull Stmt node) { public abstract List buildTraps(); /** - * Removes a trap from the graph and updates the control flow accordingly. + * Removes the specified exceptional flow from all blocks. * - * @param trap The trap to be removed. + * @param exceptionType The class type of the exceptional flow. + * @param exceptionHandlerStmt The handler statement of the exceptional flow. * @throws IllegalArgumentException if the trap is not found in the graph. */ - public abstract void removeTrap(@Nonnull Trap trap); + public abstract void removeExceptionalFlowFromAllBlocks( + ClassType exceptionType, Stmt exceptionHandlerStmt); /** * returns a Collection of Stmts that leave the body (i.e. JReturnVoidStmt, JReturnStmt and diff --git a/sootup.core/src/test/java/sootup/core/graph/MutableBlockStmtGraphTest.java b/sootup.core/src/test/java/sootup/core/graph/MutableBlockStmtGraphTest.java index a89d0da0592..37f6d9a4e12 100644 --- a/sootup.core/src/test/java/sootup/core/graph/MutableBlockStmtGraphTest.java +++ b/sootup.core/src/test/java/sootup/core/graph/MutableBlockStmtGraphTest.java @@ -1058,7 +1058,9 @@ public void testRemoveSingleTrap() { assertEquals(handlerStmt, traps.get(0).getHandlerStmt()); // Remove the trap and verify it's removed - graph.removeTrap(traps.get(0)); + Trap trapToRemove = traps.get(0); + graph.removeExceptionalFlowFromAllBlocks( + trapToRemove.getExceptionType(), trapToRemove.getHandlerStmt()); traps = graph.buildTraps(); assertEquals(0, traps.size()); } @@ -1097,48 +1099,14 @@ public void testRemoveMultipleTrapsWithDifferentExceptionTypes() { assertEquals(2, traps.size()); // Remove one trap and verify the remaining - graph.removeTrap(traps.get(0)); - traps = graph.buildTraps(); - assertEquals(1, traps.size()); - assertEquals(stmt2, traps.get(0).getBeginStmt()); - assertEquals(handlerStmt2, traps.get(0).getHandlerStmt()); - } - - @Test - public void testRemoveTrapThatSharesHandlerWithAnotherTrap() { - MutableBlockStmtGraph graph = new MutableBlockStmtGraph(); - - FallsThroughStmt stmt1 = new JNopStmt(StmtPositionInfo.getNoStmtPositionInfo()); - JGotoStmt gotoStmt = new JGotoStmt(StmtPositionInfo.getNoStmtPositionInfo()); - FallsThroughStmt stmt2 = new JNopStmt(StmtPositionInfo.getNoStmtPositionInfo()); - JReturnVoidStmt returnStmt = new JReturnVoidStmt(StmtPositionInfo.getNoStmtPositionInfo()); - - Stmt sharedHandlerStmt = - new JIdentityStmt( - new Local("sharedEx", throwableSig), - new JCaughtExceptionRef(throwableSig), - StmtPositionInfo.getNoStmtPositionInfo()); - - // Add blocks to the graph - graph.addBlock(Collections.singletonList(stmt1)); - graph.addBlock(Collections.singletonList(gotoStmt)); - graph.addBlock(Collections.singletonList(stmt2)); - graph.addBlock(Collections.singletonList(returnStmt)); - graph.setStartingStmt(stmt1); - - // Add two traps with different exception types but sharing the same handler - graph.addExceptionalEdge(stmt1, throwableSig, sharedHandlerStmt); - graph.addExceptionalEdge(stmt2, ioExceptionSig, sharedHandlerStmt); - - List traps = graph.buildTraps(); - assertEquals(2, traps.size()); - - // Remove the first trap and verify the other remains with the same handler - graph.removeTrap(traps.get(0)); + Trap trapToRemove = traps.get(0); + Trap trapToKeep = traps.get(1); + graph.removeExceptionalFlowFromAllBlocks( + trapToRemove.getExceptionType(), trapToRemove.getHandlerStmt()); traps = graph.buildTraps(); assertEquals(1, traps.size()); - assertEquals(stmt2, traps.get(0).getBeginStmt()); - assertEquals(sharedHandlerStmt, traps.get(0).getHandlerStmt()); + assertEquals(stmt2, trapToKeep.getBeginStmt()); + assertEquals(handlerStmt2, trapToKeep.getHandlerStmt()); } }