-
Notifications
You must be signed in to change notification settings - Fork 578
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
Do not report DLS_DEAD_LOCAL_STORE
for java 21 type switch variables
#2828
Changes from all commits
ffa5a12
32c16a6
30ee77a
d123d32
c0403e8
4de4e10
b5893ac
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 |
---|---|---|
|
@@ -47,17 +47,20 @@ | |
import org.apache.bcel.generic.Instruction; | ||
import org.apache.bcel.generic.InstructionHandle; | ||
import org.apache.bcel.generic.InvokeInstruction; | ||
import org.apache.bcel.generic.INVOKEDYNAMIC; | ||
import org.apache.bcel.generic.LDC; | ||
import org.apache.bcel.generic.LRETURN; | ||
import org.apache.bcel.generic.LSTORE; | ||
import org.apache.bcel.generic.LoadInstruction; | ||
import org.apache.bcel.generic.LOOKUPSWITCH; | ||
import org.apache.bcel.generic.MULTIANEWARRAY; | ||
import org.apache.bcel.generic.MethodGen; | ||
import org.apache.bcel.generic.NEWARRAY; | ||
import org.apache.bcel.generic.ObjectType; | ||
import org.apache.bcel.generic.StoreInstruction; | ||
import org.apache.bcel.generic.Type; | ||
|
||
|
||
import edu.umd.cs.findbugs.BugAccumulator; | ||
import edu.umd.cs.findbugs.BugInstance; | ||
import edu.umd.cs.findbugs.BugReporter; | ||
|
@@ -67,6 +70,7 @@ | |
import edu.umd.cs.findbugs.LocalVariableAnnotation; | ||
import edu.umd.cs.findbugs.Priorities; | ||
import edu.umd.cs.findbugs.SourceLineAnnotation; | ||
import edu.umd.cs.findbugs.SwitchHandler; | ||
import edu.umd.cs.findbugs.SystemProperties; | ||
import edu.umd.cs.findbugs.ba.AnalysisContext; | ||
import edu.umd.cs.findbugs.ba.CFG; | ||
|
@@ -219,6 +223,7 @@ private void analyzeMethod(ClassContext classContext, Method method) throws Data | |
int[] localLoadCount = new int[numLocals]; | ||
int[] localIncrementCount = new int[numLocals]; | ||
MethodGen methodGen = classContext.getMethodGen(method); | ||
ConstantPoolGen cpg = classContext.getConstantPoolGen(); | ||
CFG cfg = classContext.getCFG(method); | ||
if (cfg.isFlagSet(CFG.FOUND_INEXACT_UNCONDITIONAL_THROWERS)) { | ||
return; | ||
|
@@ -245,6 +250,9 @@ private void analyzeMethod(ClassContext classContext, Method method) throws Data | |
// due to inlining of finally blocks. | ||
BitSet liveStoreSourceLineSet = new BitSet(); | ||
|
||
// Keep track of the switches so we can ignore the dead stores corresponding to type switches | ||
SwitchHandler switchHandler = new SwitchHandler(); | ||
|
||
// Scan method for | ||
// - dead stores | ||
// - stores to parameters that are dead upon entry to the method | ||
|
@@ -254,18 +262,41 @@ private void analyzeMethod(ClassContext classContext, Method method) throws Data | |
BugInstance pendingBugReportAboutOverwrittenParameter = null; | ||
try { | ||
WarningPropertySet<WarningProperty> propertySet = new WarningPropertySet<>(); | ||
|
||
InstructionHandle handle = location.getHandle(); | ||
int pc = handle.getPosition(); | ||
Comment on lines
+266
to
+267
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. It's great that you moved these into separate variables. Can you please use these inside this method instead of retrieving it by calling the functions several times? 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. I actually moved these up because the variables were declared further down and I needed them for the new code. |
||
|
||
if (handle.getInstruction() instanceof INVOKEDYNAMIC) { | ||
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. Maybe |
||
INVOKEDYNAMIC invokeDynamicInstruction = (INVOKEDYNAMIC) handle.getInstruction(); | ||
String invokeMethodName = invokeDynamicInstruction.getMethodName(cpg); | ||
switchHandler.sawInvokeDynamic(pc, invokeMethodName); | ||
|
||
continue; | ||
} else if (handle.getInstruction() instanceof LOOKUPSWITCH) { | ||
LOOKUPSWITCH switchInstruction = (LOOKUPSWITCH) handle.getInstruction(); | ||
int[] indices = switchInstruction.getIndices(); | ||
|
||
switchHandler.enterSwitch(switchInstruction.getOpcode(), | ||
pc, | ||
indices, | ||
0, // Not sure how to get the default offset from BCEL but it doesn't matter here | ||
false); // It shouldn't matter here if the switch was exhaustive or not | ||
Comment on lines
+279
to
+283
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 please take a look at the definition of this method? The last parameter, which is a primitive boolean, has the annotation 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. I have removed the |
||
} | ||
|
||
// Skip any instruction which is not a store | ||
if (!isStore(location)) { | ||
continue; | ||
} | ||
|
||
if (switchHandler.isTypeSwitchCaseLoad(location)) { | ||
continue; | ||
} | ||
|
||
// Heuristic: exception handler blocks often contain | ||
// dead stores generated by the compiler. | ||
if (location.getBasicBlock().isExceptionHandler()) { | ||
propertySet.addProperty(DeadLocalStoreProperty.EXCEPTION_HANDLER); | ||
} | ||
InstructionHandle handle = location.getHandle(); | ||
int pc = handle.getPosition(); | ||
IndexedInstruction ins = (IndexedInstruction) location.getHandle().getInstruction(); | ||
|
||
int local = ins.getIndex(); | ||
|
@@ -369,7 +400,6 @@ private void analyzeMethod(ClassContext classContext, Method method) throws Data | |
continue; // not an interesting dead store | ||
} else if (prevIns instanceof GETSTATIC) { | ||
GETSTATIC getStatic = (GETSTATIC) prevIns; | ||
ConstantPoolGen cpg = methodGen.getConstantPool(); | ||
foundDeadClassInitialization = getStatic.getFieldName(cpg).startsWith("class$") | ||
&& "Ljava/lang/Class;".equals(getStatic.getSignature(cpg)); | ||
for (Iterator<Location> j = cfg.locationIterator(); j.hasNext();) { | ||
|
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.
This method is quite long as it is, with this change, ranging from line 207 to 645. IMO it's worth considering moving some parts of it into separate functions. (Not necessarily in this PR.)