Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix and implement ConditionalBranchFolder, Unreachable Code Eliminator #1041

Draft
wants to merge 14 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
55 changes: 55 additions & 0 deletions shared-test-resources/bugfixes/ConditionalBranchFolderTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
public class ConditionalBranchFolderTest {
void tc1() {
boolean bool = true;
if (!bool) {
System.out.println("False 1");
} else if (bool) {
if (bool){
System.out.println("lets see");
}
System.out.println("mid");
}
if (!bool) {
System.out.println("False 2");
}
}

void tc2() {
boolean bool = true;
try {
if (bool) {
throw new Exception("True");
}
} catch (Exception e) {
e.printStackTrace();
}
}

void tc3() {
boolean bool = false;
try {
if (bool) {
throw new Exception("True");
}
} catch (Exception e) {
e.printStackTrace();
}
}

void tc4() {
int x = 10;
boolean bool = true;
if(x > 5) {
try {
System.out.println("Try Block");
if (bool) {
System.out.println("True inside Try");
}
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println("");
}

}
Binary file added shared-test-resources/bugfixes/LineIterator.class
Binary file not shown.
85 changes: 85 additions & 0 deletions shared-test-resources/bugfixes/LineIterator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import java.io.BufferedReader;
import java.io.Closeable;
import java.io.IOException;
import java.io.Reader;
import java.util.Iterator;
import java.util.NoSuchElementException;

public class LineIterator implements Iterator<String>, Closeable {
private final BufferedReader bufferedReader;
private String cachedLine;
private boolean finished;

public LineIterator(Reader reader) throws IllegalArgumentException {
if (reader == null) {
throw new IllegalArgumentException("Reader must not be null");
} else {
if (reader instanceof BufferedReader) {
this.bufferedReader = (BufferedReader)reader;
} else {
this.bufferedReader = new BufferedReader(reader);
}

}
}

public boolean hasNext() {
if (this.cachedLine != null) {
return true;
} else if (this.finished) {
return false;
} else {
try {
String line;
do {
line = this.bufferedReader.readLine();
if (line == null) {
this.finished = true;
return false;
}
} while(!this.isValidLine(line));

this.cachedLine = line;
return true;
} catch (IOException var2) {
IOException ioe = var2;
// IOUtils.closeQuietly(this, ioe::addSuppressed);
throw new IllegalStateException(ioe);
}
}
}

protected boolean isValidLine(String line) {
return true;
}

public String next() {
return this.nextLine();
}

public String nextLine() {
if (!this.hasNext()) {
throw new NoSuchElementException("No more lines");
} else {
String currentLine = this.cachedLine;
this.cachedLine = null;
return currentLine;
}
}

public void close() throws IOException {
this.finished = true;
this.cachedLine = null;
// IOUtils.close(this.bufferedReader);
}

public void remove() {
throw new UnsupportedOperationException("remove not supported");
}

/** @deprecated */
@Deprecated
public static void closeQuietly(LineIterator iterator) {
// IOUtils.closeQuietly(iterator);
}
}
25 changes: 25 additions & 0 deletions shared-test-resources/bugfixes/NestedTryCatchFinally.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import java.io.FileInputStream;
import java.io.File;
import java.io.ObjectInputStream;

public class NestedTryCatchFinally {

private static String test0(File storedResults) throws Exception {
try {
FileInputStream file = new FileInputStream(storedResults);
try {
ObjectInputStream stream = new ObjectInputStream(file);
try {
return (String) stream.readObject();
} finally {
stream.close();
}
} finally {
file.close();
}
} catch (Exception e) {
throw new Exception(e);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -577,10 +577,22 @@ public void removeBlock(BasicBlock<?> block) {
blockOf.clearPredecessorBlocks();
blockOf.clearSuccessorBlocks();
blockOf.clearExceptionalSuccessorBlocks();

blocks.remove(blockOf);
}

@Override
public void replaceStmt(@Nonnull Stmt oldStmt, @Nonnull Stmt newStmt) {
Pair<Integer, MutableBasicBlock> blockPair = stmtToBlock.get(oldStmt);
if (blockPair == null) {
// Stmt does not exist in the graph
throw new IllegalArgumentException("splitStmt does not exist in this block!");
}
MutableBasicBlock block = blockPair.getRight();
block.replaceStmt(oldStmt, newStmt);
stmtToBlock.remove(oldStmt);
stmtToBlock.put(newStmt, blockPair);
}

@Override
public void addNode(@Nonnull Stmt stmt, @Nonnull Map<ClassType, Stmt> exceptions) {
Pair<Integer, MutableBasicBlock> blockPair = stmtToBlock.get(stmt);
Expand Down Expand Up @@ -1276,6 +1288,24 @@ protected void putEdge_internal(@Nonnull Stmt stmtA, int succesorIdx, @Nonnull S
}
}

@Override
public void unLinkNodes(@Nonnull Stmt from, @Nonnull Stmt to) {
Pair<Integer, MutableBasicBlock> blockOfFromPair = stmtToBlock.get(from);
if (blockOfFromPair == null) {
throw new IllegalArgumentException("stmt '" + from + "' does not exist in this StmtGraph!");
}
MutableBasicBlock blockOfFrom = blockOfFromPair.getRight();
Pair<Integer, MutableBasicBlock> blockOfToPair = stmtToBlock.get(to);
if (blockOfToPair == null) {
throw new IllegalArgumentException("stmt '" + to + "' does not exist in this StmtGraph!");
}
MutableBasicBlock blockOfTo = blockOfToPair.getRight();

// Unlink 2 blocks
blockOfFrom.removeFromSuccessorBlocks(blockOfTo);
blockOfTo.removePredecessorBlock(blockOfFrom);
}

@Override
public List<Integer> removeEdge(@Nonnull Stmt from, @Nonnull Stmt to) {
Pair<Integer, MutableBasicBlock> blockOfFromPair = stmtToBlock.get(from);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ public void addNode(@Nonnull Stmt stmt) {
addNode(stmt, Collections.emptyMap());
}

/** replace a "oldStmt" with "newStmt" in the StmtGraph */
public abstract void replaceStmt(@Nonnull Stmt oldStmt, @Nonnull Stmt newStmt);

/** inserts a "stmt" with exceptional flows "traps" into the StmtGraph */
public abstract void addNode(@Nonnull Stmt stmt, @Nonnull Map<ClassType, Stmt> traps);

Expand Down Expand Up @@ -107,6 +110,8 @@ public void setEdges(@Nonnull BranchingStmt from, @Nonnull Stmt... targets) {
setEdges(from, Arrays.asList(targets));
}

public abstract void unLinkNodes(@Nonnull Stmt from, @Nonnull Stmt to);

/**
* removes the current outgoing flows of "from" to "to"
*
Expand Down
Loading