Skip to content

Commit

Permalink
Remove redundant initializers
Browse files Browse the repository at this point in the history
In most of these cases, the initializer is redundant because some other
assignment to the same location appears after the initialization but
before any use.

In a few cases involving fields, the explicit initialization value is
identical to what would be used implicitly.
  • Loading branch information
liblit committed Sep 5, 2023
1 parent df52cbb commit 8c80217
Show file tree
Hide file tree
Showing 57 changed files with 86 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
// possible)

public abstract class JDTJava2CAstTranslator<T extends Position> {
protected boolean dump = false;
protected boolean dump;

protected final CAst fFactory = new CAstImpl();

Expand Down Expand Up @@ -1083,7 +1083,7 @@ private ProcedureEntity(
}

if (fDecl != null) {
int i = 0; // index to start filling up with real params
int i; // index to start filling up with real params
if ((fModifiers & Modifier.STATIC) != 0) {
fParameterNames = new String[fDecl.parameters().size()];
i = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class Java2IRTranslator {

protected final SetOfClasses exclusions;

CAstRewriterFactory<?, ?> castRewriterFactory = null;
CAstRewriterFactory<?, ?> castRewriterFactory;

public Java2IRTranslator(JavaSourceLoaderImpl srcLoader, SetOfClasses exclusions) {
this(srcLoader, null, exclusions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public InputStream getInputStream() {
Assertions.UNREACHABLE(e.getMessage());
}

String wrapperSource = null;
final String wrapperSource;
String ext = FilenameUtils.getExtension(getFile().toString()).toLowerCase();
switch (ext) {
case "js":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,7 @@ public PointerAnalysis<ObjectVertex> getPointerAnalysis(
private final Map<Pair<PrototypeField, ObjectVertex>, PrototypeFieldVertex> proto =
HashMapFactory.make();

private GraphReachability<Vertex, ObjectVertex> pointerAnalysis =
computeClosure(graph, monitor, ObjectVertex.class);
final private GraphReachability<Vertex, ObjectVertex> pointerAnalysis;

private final ExtensionGraph<Vertex> dataflow = new ExtensionGraph<>(graph);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ private boolean addCorrelation(

if (!entity.getPosition().getURL().toString().equals(startPos.getURL().toString())) return true;
Set<ChildPos> startNodes = findNodesAtPos(CAstNode.OBJECT_REF, startPos, entity);
Set<ChildPos> endNodes = null;
final Set<ChildPos> endNodes;
if (corr instanceof ReadWriteCorrelation) {
endNodes = findNodesAtPos(CAstNode.ASSIGN, endPos, entity);
} else if (corr instanceof EscapeCorrelation) {
Expand Down Expand Up @@ -207,9 +207,9 @@ private static void filterNames(Set<ChildPos> nodes, String indexName) {
String parmName,
List<String> locals) {
ChildPos pos = startNode;
CAstNode block = null;
int start = -1, end = 0;
int start_inner = -1, end_inner = -1;
CAstNode block;
int start, end;
int start_inner, end_inner = -1;

do {
if (pos != startNode && pos.getParentPos() instanceof ChildPos)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import com.ibm.wala.util.collections.HashMapFactory;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Map;
import java.util.Map.Entry;
Expand Down Expand Up @@ -63,7 +62,7 @@ public String dump(CAstEntity entity) {
}

private void dump(CAstEntity entity, int indent, StringBuilder buf) {
Collection<CAstEntity> scopedEntities = Collections.emptySet();
final Collection<CAstEntity> scopedEntities;
if (entity.getKind() == CAstEntity.SCRIPT_ENTITY) {
buf.append(indent(indent)).append(entity.getName()).append(":\n");
scopedEntities = dumpScopedEntities(entity, indent + 2, buf);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ public static String eraseGeneratedNames(String str) {
}

public void testRewriter(String testName, String in, String out) {
String expected = null;
String actual = null;
String expected;
String actual;
try {
final var tmp = File.createTempFile("test", ".js", tmpDir);
FileUtil.writeFile(tmp, in);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,8 @@ private void translateModuleEntryToCAst(
} else {
TranslatorToCAst xlatorToCAst = getTranslatorToCAst(ast, moduleEntry, modules);

CAstEntity fileEntity = null;
try {
fileEntity = xlatorToCAst.translateToCAst();
final CAstEntity fileEntity = xlatorToCAst.translateToCAst();

if (DEBUG) {
CAstPrinter.printTo(fileEntity, new PrintWriter(System.err));
Expand Down
4 changes: 2 additions & 2 deletions cast/src/main/java/com/ibm/wala/cast/util/SourceBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public SourceBuffer(Position p) throws IOException {
try (Reader pr = p.getReader()) {
try (BufferedReader reader = new BufferedReader(pr)) {

String currentLine = null;
String currentLine;
List<String> lines = new ArrayList<>();
int offset = 0, line = 0;
do {
Expand All @@ -123,7 +123,7 @@ public SourceBuffer(Position p) throws IOException {
int endColumn = -1;
int startOffset = -1;
int startLine = line;
int startColumn = -1;
final int startColumn;
if (p.getLastOffset() >= 0) {
if (p.getFirstOffset() == offset) {
startOffset = p.getFirstOffset();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class NormalOrder implements Comparator<Weight> {

@Override
public int compare(Weight o1, Weight o2) {
int result = 0;
final int result;

if (o1.getType() == Type.NOT_SET || o2.getType() == Type.NOT_SET) {
throw new IllegalArgumentException("Tried to compare weights, which are not set yet.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public Weight(Type type, int number) {
* @return this + other
*/
public Weight add(Weight other) {
Weight result = null;
final Weight result;
if (this.getType() == Type.NUMBER) {
if (other.getType() == Type.NUMBER) {
result = new Weight(Type.NUMBER, this.getNumber() + other.getNumber());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ public UnaryOperator<BitVectorVariable> getEdgeTransferFunction(CGNode dst, CGNo
*/

Iterator<CallSiteReference> callsites = cg.getPossibleSites(src, dst);
BitVector filtered = new BitVector(transformer.getValues().getSize());

if (callsites.hasNext()) {

Expand All @@ -84,7 +83,7 @@ public UnaryOperator<BitVectorVariable> getEdgeTransferFunction(CGNode dst, CGNo
caught.retainAll(intraResult.getAnalysis(src).getCaughtExceptions(callsite));
}

filtered = transformer.computeBitVector(caught);
BitVector filtered = transformer.computeBitVector(caught);
return new BitVectorMinusVector(filtered);
} else {
// This case should not happen, as we should only get src, dst pairs,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ private SSAInstruction[] makeStatements(IClass klass) {
int retValue = nextLocal++;
// value number of the result of the clone()
NewSiteReference ref = NewSiteReference.make(NEW_PC, klass.getReference());
SSANewInstruction N = null;
final SSANewInstruction N;
if (klass.isArrayClass()) {
int length = nextLocal++;
statements.add(insts.ArrayLengthInstruction(statements.size(), length, 1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ private void addStatementsForSetOfTypes(Iterator<IClass> it) {
typesAllocated.add(T);
int i = getLocalForType(T);
NewSiteReference ref = NewSiteReference.make(getNewSiteForType(T), T);
SSANewInstruction a = null;
final SSANewInstruction a;
if (T.isArrayType()) {
int[] sizes = new int[((ArrayClass) klass).getDimensionality()];
initValueNumberForConstantOne();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ private IR makeIR(IMethod method, IMethod target, Context recv) {
// pointer
int args[] = new int[nargs];
int pc = 0;
int parametersVn = -1; // parametersVn will hold the value number of parameters array
final int parametersVn; // parametersVn will hold the value number of parameters array

if (method.getReference().equals(CTOR_NEW_INSTANCE)) {
// allocate the new object constructed
Expand Down Expand Up @@ -239,7 +239,7 @@ private IR makeIR(IMethod method, IMethod target, Context recv) {
}

int exceptions = nextLocal++;
int result = -1;
final int result;

// emit the dispatch and return instructions
if (method.getReference().equals(CTOR_NEW_INSTANCE)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ private TypeAbstraction meetDeclaredExceptionTypes(SSAGetCaughtExceptionInstruct
Iterator<TypeReference> it = bb.getCaughtExceptionTypes();
TypeReference t = it.next();
IClass klass = cha.lookupClass(t);
TypeAbstraction result = null;
TypeAbstraction result;
if (klass == null) {
// a type that cannot be loaded.
// be pessimistic
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public IField getField(Atom name) {

@Override
public IField getField(Atom name, TypeName type) {
boolean unresolved = false;
boolean unresolved;
try {
// typically, there will be at most one field with the name
IField field = getField(name);
Expand Down Expand Up @@ -452,7 +452,7 @@ protected Collection<IClass> computeAllInterfacesAsCollection() {

// at this point result holds all interfaces the class directly extends.
// now expand to a fixed point.
Set<IClass> last = null;
Set<IClass> last;
do {
last = HashSetFactory.make(result);
for (IClass i : last) {
Expand Down Expand Up @@ -519,7 +519,7 @@ protected void addFieldToList(
Collection<Annotation> annotations,
Collection<TypeAnnotation> typeAnnotations,
TypeSignature sig) {
TypeName T = null;
final TypeName T;
if (fieldType.get(fieldType.length() - 1) == ';') {
T = TypeName.findOrCreate(fieldType, 0, fieldType.length() - 1);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ private Map<String, Object> getAllClassAndSourceFileContents(
Map<String, Object> result = HashMapFactory.make();
try (final JarInputStream s =
new JarInputStream(new ByteArrayInputStream(jarFileContents), false)) {
JarEntry entry = null;
JarEntry entry;
while ((entry = s.getNextJarEntry()) != null) {
byte[] entryBytes = getEntryBytes(entrySizesForFile.get(entry.getName()), s);
if (entryBytes == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ protected void processDebugInfo(BytecodeInfo bcInfo) throws InvalidClassFileExce

@Override
public String getLocalVariableName(int bcIndex, int localNumber) {
int[][] map = null;
final int[][] map;
try {
map = getBCInfo().localVariableMap;
} catch (InvalidClassFileException e1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public IR makeIR(final IBytecodeMethod<IInstruction> method, Context C, final SS
if (method == null) {
throw new IllegalArgumentException("null method");
}
com.ibm.wala.shrike.shrikeBT.IInstruction[] shrikeInstructions = null;
final com.ibm.wala.shrike.shrikeBT.IInstruction[] shrikeInstructions;
try {
shrikeInstructions = method.getInstructions();
} catch (InvalidClassFileException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static TypeReference makeTypeReference(ClassLoaderReference loader, Strin
return p;
}
ImmutableByteArray b = ImmutableByteArray.make(type);
TypeName T = null;
final TypeName T;
/*if (b.get(0) != '[') {
T = TypeName.findOrCreate(b, 0, b.length() - 1);
} else {*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ public static final TypeName[] parseForParameterNames(Language l, ImmutableByteA
while (StringStuff.isTypeCodeChar(b, i)) {
++i;
}
TypeName T = null;
final TypeName T;
byte c = b.get(i++);
if (c == TypeReference.ClassTypeCode || c == TypeReference.OtherPrimitiveTypeCode) {
while (b.get(i++) != ';')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ protected void processCall(final PathEdge<T> edge) {
if (DEBUG_LEVEL > 0) {
System.err.println(" process return site: " + returnSite);
}
IUnaryFlowFunction f = null;
final IUnaryFlowFunction f;
if (hasCallee) {
f = flowFunctionMap.getCallToReturnFlowFunction(edge.target, returnSite);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ private void handleMethodEntry(CallerSiteContext callSite) {
System.err.println("FOUND RECURSION");
System.err.println("stack " + prevStack + " contains " + callSite);
}
CallerSiteContext topCallSite = null;
CallerSiteContext topCallSite;
CallStack tmpStack = prevStack;
// mark the appropriate call sites as recursive
// and pop them
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,12 +330,11 @@ private Pair<PointsToResult, Collection<InstanceKeyAndState>> outerRefinementLoo
setNumNodesTraversed(0);
setTraversalBudget(refinementPolicy.getBudgetForPass(passNum));
Collection<InstanceKeyAndState> curP2Set = null;
PointsToComputer computer = null;
boolean completedPassInBudget = false;
try {
while (true) {
try {
computer = new PointsToComputer(queried);
final PointsToComputer computer = new PointsToComputer(queried);
computer.compute();
curP2Set = computer.getComputedP2Set(queried);
// System.err.println("completed pass");
Expand Down Expand Up @@ -382,7 +381,7 @@ private Pair<PointsToResult, Collection<InstanceKeyAndState>> outerRefinementLoo
break;
}
}
PointsToResult result = null;
final PointsToResult result;
if (succeeded) {
result = PointsToResult.SUCCESS;
} else if (passNum == numPasses) {
Expand Down Expand Up @@ -491,7 +490,7 @@ public PointsToResult pointsToPassesPred(
break;
}
}
PointsToResult result = null;
final PointsToResult result;
if (succeeded) {
result = PointsToResult.SUCCESS;
} else if (passNum == numPasses) {
Expand Down Expand Up @@ -572,7 +571,7 @@ private Pair<PointsToResult, Collection<PointerKey>> getFlowsToInternal(
setNumNodesTraversed(0);
setTraversalBudget(refinementPolicy.getBudgetForPass(passNum));
Collection<PointerKeyAndState> curFlowsToSet = null;
FlowsToComputer computer = null;
FlowsToComputer computer;
try {
while (true) {
try {
Expand Down Expand Up @@ -619,7 +618,7 @@ private Pair<PointsToResult, Collection<PointerKey>> getFlowsToInternal(
break;
}
}
PointsToResult result = null;
final PointsToResult result;
if (succeeded) {
result = PointsToResult.SUCCESS;
} else if (passNum == numPasses) {
Expand Down Expand Up @@ -2433,7 +2432,7 @@ private Collection<Pair<PointerKey, PointerKey>> getBaseAndStored(
return null;
}
IR ir = node.getIR();
PointerKey base = null, stored = null;
final PointerKey base, stored;
if (field == ArrayContents.v()) {
final SSAInstruction instruction =
ir.getInstructions()[fieldWrite.getInstructionIndex()];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ private static void debugPrintIR(IR ir) {

@Override
public Set<CallerSiteContext> getPotentialCallers(PointerKey formalPk) {
CGNode callee = null;
final CGNode callee;
if (formalPk instanceof LocalPointerKey) {
callee = ((LocalPointerKey) formalPk).getNode();
} else if (formalPk instanceof ReturnValueKey) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public static Process run(
System.err.println("Statement: " + s);

// compute the slice as a collection of statements
Collection<Statement> slice = null;
final Collection<Statement> slice;
if (goBackward) {
final PointerAnalysis<InstanceKey> pointerAnalysis = builder.getPointerAnalysis();
slice = Slicer.computeBackwardSlice(s, cg, pointerAnalysis, dOptions, cOptions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public SSAAbstractInvokeInstruction addInvocation(int[] params, CallSiteReferenc
CallSiteReference newSite =
CallSiteReference.make(
statements.size(), site.getDeclaredTarget(), site.getInvocationCode());
SSAAbstractInvokeInstruction s = null;
final SSAAbstractInvokeInstruction s;
if (newSite.getDeclaredTarget().getReturnType().equals(TypeReference.Void)) {
s = insts.InvokeInstruction(statements.size(), params, nextLocal++, newSite, null);
} else {
Expand Down Expand Up @@ -238,7 +238,7 @@ private SSANewInstruction addAllocation(TypeReference T, boolean invokeCtor) {
// allocate an instance for the array contents
NewSiteReference n = NewSiteReference.make(statements.size(), e);
int alloc = nextLocal++;
SSANewInstruction ni = null;
final SSANewInstruction ni;
if (e.isArrayType()) {
int[] sizes = new int[((ArrayClass) cha.lookupClass(T)).getDimensionality()];
Arrays.fill(sizes, getValueNumberForIntConstant(1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1331,7 +1331,7 @@ protected IntSet getInstanceKeysForClass(IClass klass) {
*/
@SuppressWarnings("unused")
protected IntSet filterForClass(IntSet S, IClass klass) {
MutableIntSet filter = null;
final MutableIntSet filter;
if (klass.getReference().equals(TypeReference.JavaLangObject)) {
return S;
} else {
Expand Down
Loading

0 comments on commit 8c80217

Please sign in to comment.