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

CFG Visualizer (implemented as a test detector) #2014

Merged
merged 8 commits into from May 4, 2022

Conversation

baloghadamsoftware
Copy link
Contributor

Sometimes, during development it is an advantage if the CFG can be seen visually. This patch adds a detector
that generates the CFG in DOT format so it can be viewed (or converted to a graphical format) using GraphViz.


Make sure these boxes are checked before submitting your PR -- thank you!

  • Added an entry into CHANGELOG.md if you have changed SpotBugs code

Sometimes, during development it is an advantage if the CFG can be seen visually. This patch adds a detector
that generates the CFG in DOT format so it can be viewed (or converted to a graphical format) using GraphViz.
@baloghadamsoftware
Copy link
Contributor Author

I did not add tests (yet) because I do not know how to test this properly. An example for the visualized CFG (generated from test RedundantNullCheck.foo()):

test

@baloghadamsoftware
Copy link
Contributor Author

I do not see exactly what failed here...

@KengoTODA
Copy link
Member

it just means SpotBugs found problems in the codebase. We can reproduce it by ./gradlew spotbugs:spotbugsMain even in local, I hope.

@baloghadamsoftware
Copy link
Contributor Author

it just means SpotBugs found problems in the codebase. We can reproduce it by ./gradlew spotbugs:spotbugsMain even in local, I hope.

Yes, that was clear but I could not understand how a disabled detector can make the tests fail. However, it seems that even disabled detectors are enabled during all tests. Is this intentional?

Anyway, I fixed the error and now all tests pass.

private void analyzeMethod(ClassContext classContext, Method method, Path classDir) throws CFGBuilderException {
Path methodFile = getMethodFile(classDir, method.getName());

try (PrintStream out = new PrintStream(Files.createFile(methodFile).toFile(), Charset.defaultCharset().name())) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Methods of the PrintStream class does not throw IOException, so better to check problems by the checkError() method after you finish using the stream.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Creation of PrintStream throws IOException. Is it OK now that I moved this to a conventional try block?


try (PrintStream out = new PrintStream(Files.createFile(methodFile).toFile(), Charset.defaultCharset().name())) {
CFG cfg = classContext.getCFG(method);
out.println("digraph " + method.getName() + " {");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably we need to wrap the method name by ", to support special names like <new>.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is worse. Characters like < and > are not supported on Windows so I changed method names like <init> to ____init.

Copy link
Member

@KengoTODA KengoTODA left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code is LGTM, please add a changelog entry into CHANGELOG.md under the ### Added part.

ThrawnCA
ThrawnCA previously approved these changes Apr 20, 2022
ThrawnCA
ThrawnCA previously approved these changes Apr 21, 2022
KengoTODA
KengoTODA previously approved these changes May 4, 2022
@KengoTODA KengoTODA dismissed stale reviews from ThrawnCA and themself via b5d0bab May 4, 2022 01:00
@KengoTODA KengoTODA merged commit 9725e34 into spotbugs:master May 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants