Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

PrimAndStringOp bug in AstJavaTypeInference #12

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@
*****************************************************************************/
package com.ibm.wala.cast.java.test;

import java.io.File;
import java.util.Arrays;

import org.junit.Test;

public class JDTJava15IRTests extends JDTJavaTest {

public JDTJava15IRTests() {
super(JDTJavaIRTests.PROJECT);
}

@Test
public void testAnonGeneNullarySimple() {
runTest(singlePkgTestSrc("javaonepointfive"), rtJar, simplePkgTestEntryPoint("javaonepointfive"), emptyList, true);
Expand Down Expand Up @@ -119,4 +122,10 @@ public void testAnnotations() {
runTest(singlePkgTestSrc("javaonepointfive"), rtJar, simplePkgTestEntryPoint("javaonepointfive"), emptyList, true);
}

@Test
public void testTypeInferencePrimAndStringOp() {
String pkgName = "javaonepointfive";
runTest(singlePkgTestSrc(pkgName), rtJar, simplePkgTestEntryPoint(pkgName),
Arrays.asList(new TypeInferenceAssertion(pkgName + File.separator + singleInputForTest())), false);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.ibm.wala.cast.java.test;

import com.ibm.wala.cast.java.analysis.typeInference.AstJavaTypeInference;
import com.ibm.wala.cast.java.test.IRTests.IRAssertion;
import com.ibm.wala.classLoader.IMethod;
import com.ibm.wala.ipa.callgraph.CGNode;
import com.ibm.wala.ipa.callgraph.CallGraph;
import com.ibm.wala.ipa.callgraph.impl.Everywhere;
import com.ibm.wala.ipa.cha.IClassHierarchy;
import com.ibm.wala.ssa.IR;
import com.ibm.wala.ssa.SSAInstruction;
import com.ibm.wala.types.MethodReference;
import com.ibm.wala.util.collections.Iterator2Iterable;

final class TypeInferenceAssertion implements IRAssertion {
private String typeName;

public TypeInferenceAssertion(String packageName) {
this.typeName = packageName;
}

// For now just check things in the main method
public void check(CallGraph cg) {
IR ir = getIR(cg, typeName, "main", "[Ljava/lang/String;", "V");
AstJavaTypeInference inference = new AstJavaTypeInference(ir, cg.getClassHierarchy(), true);

for (SSAInstruction instr : Iterator2Iterable.make(ir.iterateAllInstructions())) {
// Check defs
for (int def = 0; def < instr.getNumberOfDefs(); def++) {
int ssaVariable = instr.getDef(def);
inference.getType(ssaVariable);
}

// Check uses
for (int def = 0; def < instr.getNumberOfUses(); def++) {
int ssaVariable = instr.getUse(def);
inference.getType(ssaVariable);
}
}

}

private IR getIR(CallGraph cg, String fullyQualifiedTypeName, String methodName, String methodParameter, String methodReturnType) {
IClassHierarchy classHierarchy = cg.getClassHierarchy();
MethodReference methodRef = JDTJava15IRTests
.descriptorToMethodRef(
String.format("Source#%s#%s#(%s)%s", fullyQualifiedTypeName, methodName, methodParameter, methodReturnType),
classHierarchy);
IMethod method = classHierarchy.resolveMethod(methodRef);
CGNode node = cg.getNode(method, Everywhere.EVERYWHERE);
return node.getIR();
}

}
Binary file modified com.ibm.wala.cast.java.jdt.test/testdata/test_project.zip
100644 → 100755
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

public class AstJavaTypeInference extends AstTypeInference {

protected final IClass stringClass;
protected IClass stringClass;

protected class AstJavaTypeOperatorFactory extends AstTypeOperatorFactory implements AstJavaInstructionVisitor {
public void visitBinaryOp(SSABinaryOpInstruction instruction) {
Expand Down Expand Up @@ -96,7 +96,13 @@ public IVariable makeVariable(int valueNumber) {

public AstJavaTypeInference(IR ir, IClassHierarchy cha, boolean doPrimitives) {
super(ir, cha, JavaPrimitiveType.BOOLEAN, doPrimitives);
this.stringClass = cha.lookupClass(TypeReference.JavaLangString);
}

IClass getStringClass() {
if (stringClass == null) {
this.stringClass = cha.lookupClass(TypeReference.JavaLangString);
}
return stringClass;
}

protected void initialize() {
Expand Down Expand Up @@ -125,12 +131,12 @@ public byte evaluate(TypeVariable lhs, TypeVariable[] rhs) {
TypeVariable r = (TypeVariable) rhs[i];
TypeAbstraction ta = r.getType();
if (ta instanceof PointType) {
if (ta.getType().equals(stringClass)) {
if (ta.getType().equals(getStringClass())) {
meet = new PointType(ta.getType());
break;
}
} else if (ta instanceof ConeType) {
if (ta.getType().equals(stringClass)) {
if (ta.getType().equals(getStringClass())) {
meet = new PointType(ta.getType());
break;
}
Expand Down