Skip to content

Commit

Permalink
Replaced removeTrap with removeExceptionalFlowFromAllBlocks
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelyoukeim committed Oct 2, 2024
1 parent 0cc09d2 commit 9b2fde3
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package sootup.core.graph;

/*-
* #%L
* Soot - a J*va Optimization Framework
Expand Down Expand Up @@ -134,7 +135,8 @@ public List<Trap> buildTraps() {
}

@Override
public void removeTrap(@Nonnull Trap trap) {
backingGraph.removeTrap(trap);
public void removeExceptionalFlowFromAllBlocks(
@Nonnull ClassType exceptionType, @Nonnull Stmt exceptionHandlerStmt) {
backingGraph.removeExceptionalFlowFromAllBlocks(exceptionType, exceptionHandlerStmt);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public List<Trap> buildTraps() {
}

@Override
public void removeTrap(@Nonnull Trap trap) {
public void removeExceptionalFlowFromAllBlocks(ClassType classType, Stmt exceptionHandlerStmt) {
throw new UnsupportedOperationException("Not implemented yet!");
}

Expand Down
9 changes: 6 additions & 3 deletions sootup.core/src/main/java/sootup/core/graph/StmtGraph.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package sootup.core.graph;

/*-
* #%L
* Soot - a J*va Optimization Framework
Expand Down Expand Up @@ -155,12 +156,14 @@ public int degree(@Nonnull Stmt node) {
public abstract List<Trap> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down Expand Up @@ -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<Trap> 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());
}
}

0 comments on commit 9b2fde3

Please sign in to comment.