-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
GROOVY-11263: Analyze dead code #2023
Open
daniellansun
wants to merge
6
commits into
master
Choose a base branch
from
GROOVY-11263
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3f2344c
GROOVY-11263: Analyze dead code
daniellansun c37ac8a
Set source unit via setter
daniellansun 23af892
Add more test cases
daniellansun 0f313a8
Support analyzing dead code after `throw` statement
daniellansun 103cfa0
Keep `Verifier` as it was
daniellansun 66bc06f
Add an option to switch off dead code analysis
daniellansun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
src/main/java/org/codehaus/groovy/classgen/DeadCodeAnalyzer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.codehaus.groovy.classgen; | ||
|
||
import org.codehaus.groovy.ast.ClassCodeVisitorSupport; | ||
import org.codehaus.groovy.ast.stmt.BlockStatement; | ||
import org.codehaus.groovy.ast.stmt.BreakStatement; | ||
import org.codehaus.groovy.ast.stmt.ContinueStatement; | ||
import org.codehaus.groovy.ast.stmt.ReturnStatement; | ||
import org.codehaus.groovy.ast.stmt.Statement; | ||
import org.codehaus.groovy.control.SourceUnit; | ||
|
||
/** | ||
* Analyze AST for dead code | ||
* | ||
* @since 5.0.0 | ||
*/ | ||
public class DeadCodeAnalyzer extends ClassCodeVisitorSupport { | ||
|
||
private final SourceUnit sourceUnit; | ||
|
||
public DeadCodeAnalyzer(final SourceUnit sourceUnit) { | ||
this.sourceUnit = sourceUnit; | ||
} | ||
|
||
@Override | ||
protected SourceUnit getSourceUnit() { | ||
return sourceUnit; | ||
} | ||
@Override | ||
public void visitBlockStatement(BlockStatement statement) { | ||
analyzeDeadCode(statement); | ||
super.visitBlockStatement(statement); | ||
} | ||
|
||
private void analyzeDeadCode(BlockStatement block) { | ||
int foundCnt = 0; | ||
for (Statement statement : block.getStatements()) { | ||
if (statement instanceof ReturnStatement | ||
|| statement instanceof BreakStatement | ||
|| statement instanceof ContinueStatement | ||
) { | ||
foundCnt++; | ||
if (1 == foundCnt) continue; | ||
} | ||
|
||
if (foundCnt > 0) { | ||
addError("Unreachable statement found", statement); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
why do you need the source unit? I think there is also a conceptional mistake. I may be wrong though, but do we not reuse the Verifier for multiple source units? If yes, then having the source unit in the constructor there is wrong. If you really need the source unit you should use a setter in case of the Verifier I think
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.
The method
org.codehaus.groovy.ast.ClassCodeVisitorSupport#addError
needs source unit, I useaddError
to collect and report errors.