-
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
base: master
Are you sure you want to change the base?
Changes from all commits
3f2344c
c37ac8a
23af892
0f313a8
103cfa0
66bc06f
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 |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* 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.ast.stmt.ThrowStatement; | ||
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 | ||
|| statement instanceof ThrowStatement) { | ||
foundCnt++; | ||
if (1 == foundCnt) continue; | ||
} | ||
|
||
if (foundCnt > 0) { | ||
addError("Unreachable statement found", statement); | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,6 +66,9 @@ public class CompilerConfiguration { | |
/** Joint Compilation Option for enabling generating stubs in memory. */ | ||
public static final String MEM_STUB = "memStub"; | ||
|
||
/** Optimization Option for enabling dead code analysis */ | ||
public static final String ANALYZE_DEAD_CODE = "analyzeDeadCode"; | ||
|
||
Comment on lines
+69
to
+71
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. Can you move this adjacent to the other optimization options and add an I'm partial to naming it |
||
/** This (<code>"1.4"</code>) is the value for targetBytecode to compile for a JDK 1.4. */ | ||
@Deprecated public static final String JDK4 = "1.4"; | ||
/** This (<code>"1.5"</code>) is the value for targetBytecode to compile for a JDK 1.5. */ | ||
|
@@ -469,6 +472,8 @@ public CompilerConfiguration() { | |
handleOptimizationOption(GROOVYDOC, getSystemPropertySafe("groovy.attach.groovydoc")); | ||
handleOptimizationOption(RUNTIME_GROOVYDOC, getSystemPropertySafe("groovy.attach.runtime.groovydoc")); | ||
handleOptimizationOption(PARALLEL_PARSE, getSystemPropertySafe("groovy.parallel.parse", "true")); | ||
handleOptimizationOption(ANALYZE_DEAD_CODE, getSystemPropertySafe("groovy.analyze.deadcode", "true")); | ||
|
||
Comment on lines
+475
to
+476
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.
Spurious extra line added. |
||
|
||
if (getBooleanSafe("groovy.mem.stub")) { | ||
jointCompilationOptions = new HashMap<>(2); | ||
|
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.