-
Notifications
You must be signed in to change notification settings - Fork 357
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
Don't issue warnings on dead code #6246
Changes from 13 commits
9032419
049892c
8f157c7
19637ff
1aec779
847c866
a424f40
4d0aac7
6b90638
5fd148f
961c491
2720c3d
a29450a
4fc7b49
cb59d97
22eb7a6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,8 @@ | |
import java.util.Set; | ||
import java.util.StringJoiner; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
import java.util.function.Function; | ||
import javax.lang.model.type.TypeMirror; | ||
import org.checkerframework.checker.initialization.qual.UnknownInitialization; | ||
import org.checkerframework.checker.nullness.qual.Nullable; | ||
import org.checkerframework.dataflow.analysis.AnalysisResult; | ||
|
@@ -28,6 +30,7 @@ | |
import org.checkerframework.dataflow.cfg.block.ExceptionBlock; | ||
import org.checkerframework.dataflow.cfg.block.RegularBlock; | ||
import org.checkerframework.dataflow.cfg.block.SingleSuccessorBlock; | ||
import org.checkerframework.dataflow.cfg.block.SingleSuccessorBlockImpl; | ||
import org.checkerframework.dataflow.cfg.block.SpecialBlock; | ||
import org.checkerframework.dataflow.cfg.block.SpecialBlockImpl; | ||
import org.checkerframework.dataflow.cfg.node.Node; | ||
|
@@ -276,6 +279,77 @@ public List<Node> getAllNodes( | |
return result; | ||
} | ||
|
||
/** | ||
* Returns the set of all basic blocks in this control flow graph, <b>except</b> those that are | ||
* only reachable via an exception whose type is ignored by parameter {@code | ||
* shouldIgnoreException}. | ||
* | ||
* @param shouldIgnoreException returns true if it is passed a {@code TypeMirror} that should be | ||
* ignored | ||
* @return the set of all basic blocks in this control flow graph, <b>except</b> those that are | ||
* only reachable via an exception whose type is ignored by {@code shouldIgnoreException} | ||
*/ | ||
public Set<Block> getAllBlocks( | ||
@UnknownInitialization(ControlFlowGraph.class) ControlFlowGraph this, | ||
Function<TypeMirror, Boolean> shouldIgnoreException) { | ||
Set<Block> visited = new LinkedHashSet<>(); | ||
// worklist is always a subset of visited; any block in worklist is also in visited. | ||
Queue<Block> worklist = new ArrayDeque<>(); | ||
Block cur = entryBlock; | ||
visited.add(entryBlock); | ||
|
||
// traverse the whole control flow graph | ||
while (cur != null) { | ||
if (cur instanceof ExceptionBlock) { | ||
((ExceptionBlock) cur) | ||
.getExceptionalSuccessors() | ||
.forEach( | ||
(key, value) -> { | ||
if (!shouldIgnoreException.apply(key)) { | ||
for (Block b : value) { | ||
if (visited.add(b)) { | ||
worklist.add(b); | ||
} | ||
} | ||
} | ||
}); | ||
Block b = ((SingleSuccessorBlockImpl) cur).getSuccessor(); | ||
if (b != null && visited.add(b)) { | ||
worklist.add(b); | ||
} | ||
|
||
} else { | ||
for (Block b : cur.getSuccessors()) { | ||
if (visited.add(b)) { | ||
worklist.add(b); | ||
} | ||
} | ||
} | ||
cur = worklist.poll(); | ||
} | ||
|
||
return visited; | ||
} | ||
|
||
/** | ||
* Returns the list of all nodes in this control flow graph, <b>except</b> those that are only | ||
* reachable via an exception whose type is ignored by parameter {@code shouldIgnoreException}. | ||
* | ||
* @param shouldIgnoreException returns true if it is passed a {@code TypeMirror} that should be | ||
* ignored | ||
* @return the list of all nodes in this control flow graph, <b>except</b> those that are only | ||
* reachable via an exception whose type is ignored by {@code shouldIgnoreException} | ||
*/ | ||
public List<Node> getAllNodes( | ||
@UnknownInitialization(ControlFlowGraph.class) ControlFlowGraph this, | ||
Function<TypeMirror, Boolean> shouldIgnoreException) { | ||
List<Node> result = new ArrayList<>(); | ||
for (Block b : getAllBlocks(shouldIgnoreException)) { | ||
result.addAll(b.getNodes()); | ||
} | ||
return result; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could be done with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
But still it creates a Stream object. I think
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any of those, or the current code, is fine with me. |
||
} | ||
|
||
/** | ||
* Returns all basic blocks in this control flow graph, in reversed depth-first postorder. Blocks | ||
* may appear more than once in the sequence. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Writing this as
is two lines shorter and I find it easier to read (for example, types are given and there are no lambdas).
I do wish there were a way to bind variables to the parts of the Entry, right in the
for
statement header.