Skip to content

Commit d9a3d6b

Browse files
authored
Miscellaneous changes (#170)
- Capitalize constant. - Add test for #168. - Add a TensorFlow type. - Suppress warnings about unused logger (we'll use it). - Increase logging level for debugging. - Enhance test cases.
1 parent 3ded12f commit d9a3d6b

File tree

10 files changed

+43
-19
lines changed

10 files changed

+43
-19
lines changed

com.ibm.wala.cast.python.jython3/source/com/ibm/wala/cast/python/loader/PytestLoader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public PytestLoader(IClassHierarchy cha) {
3939

4040
@Override
4141
protected TranslatorToIR initTranslator(Set<Pair<CAstEntity, ModuleEntry>> topLevelEntities) {
42-
return new PythonCAstToIRTranslator(this, topLevelEntities) {
42+
return new PythonCAstToIRTranslator(this) {
4343

4444
private boolean isPytestEntry(CAstEntity F) {
4545
if (F.getType() instanceof CAstType.Function) {

com.ibm.wala.cast.python.ml.test/source/com/ibm/wala/cast/python/ml/test/TestTensorflow2Model.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
/** Test TF2 APIs. */
3737
public class TestTensorflow2Model extends TestPythonMLCallGraphShape {
3838

39-
private static final Logger logger = Logger.getLogger(TestTensorflow2Model.class.getName());
39+
private static final Logger LOGGER = Logger.getLogger(TestTensorflow2Model.class.getName());
4040

4141
@Test
4242
public void test()
@@ -1504,18 +1504,18 @@ private void test(
15041504
CallGraph CG = builder.makeCallGraph(builder.getOptions());
15051505
assertNotNull(CG);
15061506

1507-
if (logger.isLoggable(Level.FINE)) {
1507+
if (LOGGER.isLoggable(Level.FINE)) {
15081508
CAstCallGraphUtil.AVOID_DUMP = false;
15091509
CAstCallGraphUtil.dumpCG(
15101510
((SSAPropagationCallGraphBuilder) builder).getCFAContextInterpreter(),
15111511
builder.getPointerAnalysis(),
15121512
CG);
1513-
logger.fine("Call graph:\n" + CG);
1513+
LOGGER.fine("Call graph:\n" + CG);
15141514
}
15151515

15161516
TensorTypeAnalysis analysis = E.performAnalysis(builder);
15171517

1518-
logger.info("Tensor analysis: " + analysis);
1518+
LOGGER.info("Tensor analysis: " + analysis);
15191519

15201520
// Create a mapping from function signatures to pointer keys.
15211521
Map<String, Set<LocalPointerKey>> functionSignatureToPointerKeys = new HashMap<>();
@@ -1557,7 +1557,7 @@ private void test(
15571557
v.add(tensorVariable);
15581558
return v;
15591559
});
1560-
} else logger.warning(() -> "Encountered: " + pointerKey.getClass());
1560+
} else LOGGER.warning(() -> "Encountered: " + pointerKey.getClass());
15611561
});
15621562

15631563
final String functionSignature = "script " + filename + "." + functionName + ".do()LRoot;";

com.ibm.wala.cast.python.ml/source/com/ibm/wala/cast/python/ml/types/TensorFlowTypes.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
*/
1212
public class TensorFlowTypes extends PythonTypes {
1313

14+
public static final TypeReference TENSORFLOW =
15+
TypeReference.findOrCreate(pythonLoader, TypeName.findOrCreate("Ltensorflow"));
16+
1417
public static final TypeReference DATASET =
1518
TypeReference.findOrCreate(pythonLoader, TypeName.findOrCreate("Ltensorflow/data/Dataset"));
1619

com.ibm.wala.cast.python.test/data/tf2n3.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44

55
def func2(t):
6-
pass
6+
assert isinstance(t, tf.Tensor)
77

88

99
@tf.function

com.ibm.wala.cast.python.test/source/com/ibm/wala/cast/python/test/TestMulti.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,32 @@ public void testMulti1()
3737
PropagationCallGraphBuilder builder =
3838
(PropagationCallGraphBuilder) engine.defaultCallGraphBuilder();
3939
CallGraph CG = builder.makeCallGraph(engine.getOptions(), new NullProgressMonitor());
40+
CAstCallGraphUtil.AVOID_DUMP = false;
41+
CAstCallGraphUtil.dumpCG(
42+
(SSAContextInterpreter) builder.getContextInterpreter(), builder.getPointerAnalysis(), CG);
4043
verifyGraphAssertions(CG, assertionsMulti1);
44+
}
45+
46+
protected static final Object[][] assertionsMulti2 =
47+
new Object[][] {
48+
new Object[] {ROOT, new String[] {"script multi1.py", "script multi2.py"}},
49+
// TODO: Add the following code once https://github.com/wala/ML/issues/168 is fixed:
50+
// new Object[] {"script multi1.py", new String[] {"script multi2.py/silly"}},
51+
// TODO: Add the following code once https://github.com/wala/ML/issues/168 is fixed:
52+
// new Object[] {"script multi2.py/silly", new String[] {"script multi2.py/silly/inner"}},
53+
};
54+
55+
@Test
56+
public void testMulti2()
57+
throws ClassHierarchyException, IllegalArgumentException, CancelException, IOException {
58+
PythonAnalysisEngine<?> engine = makeEngine("multi1.py", "multi2.py");
59+
PropagationCallGraphBuilder builder =
60+
(PropagationCallGraphBuilder) engine.defaultCallGraphBuilder();
61+
CallGraph CG = builder.makeCallGraph(engine.getOptions(), new NullProgressMonitor());
4162
CAstCallGraphUtil.AVOID_DUMP = false;
4263
CAstCallGraphUtil.dumpCG(
4364
(SSAContextInterpreter) builder.getContextInterpreter(), builder.getPointerAnalysis(), CG);
65+
verifyGraphAssertions(CG, assertionsMulti2);
4466
}
4567

4668
protected static final Object[][] assertionsMulti3 =

com.ibm.wala.cast.python/source/com/ibm/wala/cast/python/ipa/callgraph/PythonSSAPropagationCallGraphBuilder.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767

6868
public class PythonSSAPropagationCallGraphBuilder extends AstSSAPropagationCallGraphBuilder {
6969

70+
@SuppressWarnings("unused")
7071
private static final Logger logger =
7172
Logger.getLogger(PythonSSAPropagationCallGraphBuilder.class.getName());
7273

com.ibm.wala.cast.python/source/com/ibm/wala/cast/python/ipa/summaries/BuiltinFunctions.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,8 @@ private static TypeReference builtinFunction(String name) {
292292
builtinFunctions.put("iter", Either.forRight(2));
293293
// https://docs.python.org/3/library/functions.html#next
294294
builtinFunctions.put("next", Either.forLeft(PythonTypes.object));
295+
// https://docs.python.org/3/library/functions.html#isinstance
296+
builtinFunctions.put("isinstance", Either.forLeft(TypeReference.Boolean));
295297
}
296298

297299
public static Set<String> builtins() {

com.ibm.wala.cast.python/source/com/ibm/wala/cast/python/ir/PythonCAstToIRTranslator.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import com.ibm.wala.classLoader.CallSiteReference;
4040
import com.ibm.wala.classLoader.IClass;
4141
import com.ibm.wala.classLoader.IClassLoader;
42-
import com.ibm.wala.classLoader.ModuleEntry;
4342
import com.ibm.wala.classLoader.NewSiteReference;
4443
import com.ibm.wala.core.util.strings.Atom;
4544
import com.ibm.wala.shrike.shrikeBT.IBinaryOpInstruction;
@@ -71,21 +70,18 @@ public class PythonCAstToIRTranslator extends AstTranslator {
7170

7271
private final Map<CAstType, TypeName> walaTypeNames = HashMapFactory.make();
7372
private final Set<Pair<Scope, String>> globalDeclSet = new HashSet<>();
74-
private static boolean signleFileAnalysis = true;
75-
private final Set<Pair<CAstEntity, ModuleEntry>> topLevelEntities;
73+
private static boolean singleFileAnalysis = true;
7674

77-
public PythonCAstToIRTranslator(
78-
IClassLoader loader, Set<Pair<CAstEntity, ModuleEntry>> topLevelEntities) {
75+
public PythonCAstToIRTranslator(IClassLoader loader) {
7976
super(loader);
80-
this.topLevelEntities = topLevelEntities;
8177
}
8278

8379
public static boolean isSingleFileAnalysis() {
84-
return signleFileAnalysis;
80+
return singleFileAnalysis;
8581
}
8682

8783
public static void setSingleFileAnalysis(boolean singleFile) {
88-
PythonCAstToIRTranslator.signleFileAnalysis = singleFile;
84+
PythonCAstToIRTranslator.singleFileAnalysis = singleFile;
8985
}
9086

9187
@Override
@@ -899,7 +895,7 @@ private void handleObjectLiteralAssign(
899895
}
900896

901897
boolean isGlobal(WalkContext context, String varName) {
902-
if (signleFileAnalysis) return false;
898+
if (singleFileAnalysis) return false;
903899
else {
904900
if (context.currentScope().getEntity().getKind() == CAstEntity.SCRIPT_ENTITY) return true;
905901
else {

com.ibm.wala.cast.python/source/com/ibm/wala/cast/python/loader/PythonLoader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ protected boolean shouldTranslate(CAstEntity entity) {
176176

177177
@Override
178178
protected TranslatorToIR initTranslator(Set<Pair<CAstEntity, ModuleEntry>> topLevelEntities) {
179-
return new PythonCAstToIRTranslator(this, topLevelEntities);
179+
return new PythonCAstToIRTranslator(this);
180180
}
181181

182182
final CoreClass CodeBody =

logging.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ handlers= java.util.logging.ConsoleHandler
2626
# can be overriden by a facility specific level
2727
# Note that the ConsoleHandler also has a separate level
2828
# setting to limit messages printed to the console.
29-
.level= FINE
29+
.level= FINEST
3030

3131
############################################################
3232
# Handler specific properties.
@@ -40,7 +40,7 @@ handlers= java.util.logging.ConsoleHandler
4040
#java.util.logging.FileHandler.formatter = java.util.logging.XMLFormatter
4141

4242
# Limit the message that are printed on the console to INFO and above.
43-
java.util.logging.ConsoleHandler.level = FINE
43+
java.util.logging.ConsoleHandler.level = FINEST
4444
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
4545

4646
# Example to customize the SimpleFormatter output format

0 commit comments

Comments
 (0)