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

Add helper method to switch statements, which shows the target #2052

Merged
merged 2 commits into from
Feb 15, 2024
Merged
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
8 changes: 8 additions & 0 deletions src/main/java/soot/jimple/SwitchStmt.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ public interface SwitchStmt extends Stmt {
public ValueBox getKeyBox();

public List<Unit> getTargets();

/**
* Returns the target unit if a value with value
* "value" is the key
* @param value the value
* @return the target
*/
public Unit getTargetForValue(int value);

public Unit getTarget(int index);

Expand Down
10 changes: 10 additions & 0 deletions src/main/java/soot/jimple/internal/JLookupSwitchStmt.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,14 @@ public void convertToBaf(JimpleToBafContext context, List<Unit> out) {
u.addAllTagsOf(this);
out.add(u);
}

@Override
public Unit getTargetForValue(int value) {
for (int i = 0; i < lookupValues.size(); i++) {
if (lookupValues.get(i).value == value) {
return getTarget(i);
}
}
return getDefaultTarget();
}
}
19 changes: 19 additions & 0 deletions src/main/java/soot/jimple/internal/JTableSwitchStmt.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,23 @@ public void convertToBaf(JimpleToBafContext context, List<Unit> out) {
u.addAllTagsOf(this);
out.add(u);
}

@Override
public Unit getTargetForValue(int value) {

final int high = highIndex;
int tgtIdx = 0;
// In this for-loop, we cannot use "<=" since 'i' would wrap around.
// The case for "i == highIndex" is handled separately after the loop.
for (int i = lowIndex; i < high; i++) {
if (value == i) {
return getTarget(tgtIdx);
}
tgtIdx++;
}
if (high == value) {
return getTarget(tgtIdx);
}
return getDefaultTarget();
}
}
Loading