diff --git a/com.ibm.wala.cast.python.jython3/source/com/ibm/wala/cast/python/loader/PytestLoader.java b/com.ibm.wala.cast.python.jython3/source/com/ibm/wala/cast/python/loader/PytestLoader.java index 99ae45e03..2d6fe4e0f 100644 --- a/com.ibm.wala.cast.python.jython3/source/com/ibm/wala/cast/python/loader/PytestLoader.java +++ b/com.ibm.wala.cast.python.jython3/source/com/ibm/wala/cast/python/loader/PytestLoader.java @@ -39,7 +39,7 @@ public PytestLoader(IClassHierarchy cha) { @Override protected TranslatorToIR initTranslator(Set> topLevelEntities) { - return new PythonCAstToIRTranslator(this, topLevelEntities) { + return new PythonCAstToIRTranslator(this) { private boolean isPytestEntry(CAstEntity F) { if (F.getType() instanceof CAstType.Function) { diff --git a/com.ibm.wala.cast.python.ml.test/source/com/ibm/wala/cast/python/ml/test/TestTensorflow2Model.java b/com.ibm.wala.cast.python.ml.test/source/com/ibm/wala/cast/python/ml/test/TestTensorflow2Model.java index 3b8dd06c5..4b088ac67 100644 --- a/com.ibm.wala.cast.python.ml.test/source/com/ibm/wala/cast/python/ml/test/TestTensorflow2Model.java +++ b/com.ibm.wala.cast.python.ml.test/source/com/ibm/wala/cast/python/ml/test/TestTensorflow2Model.java @@ -36,7 +36,7 @@ /** Test TF2 APIs. */ public class TestTensorflow2Model extends TestPythonMLCallGraphShape { - private static final Logger logger = Logger.getLogger(TestTensorflow2Model.class.getName()); + private static final Logger LOGGER = Logger.getLogger(TestTensorflow2Model.class.getName()); @Test public void test() @@ -1504,18 +1504,18 @@ private void test( CallGraph CG = builder.makeCallGraph(builder.getOptions()); assertNotNull(CG); - if (logger.isLoggable(Level.FINE)) { + if (LOGGER.isLoggable(Level.FINE)) { CAstCallGraphUtil.AVOID_DUMP = false; CAstCallGraphUtil.dumpCG( ((SSAPropagationCallGraphBuilder) builder).getCFAContextInterpreter(), builder.getPointerAnalysis(), CG); - logger.fine("Call graph:\n" + CG); + LOGGER.fine("Call graph:\n" + CG); } TensorTypeAnalysis analysis = E.performAnalysis(builder); - logger.info("Tensor analysis: " + analysis); + LOGGER.info("Tensor analysis: " + analysis); // Create a mapping from function signatures to pointer keys. Map> functionSignatureToPointerKeys = new HashMap<>(); @@ -1557,7 +1557,7 @@ private void test( v.add(tensorVariable); return v; }); - } else logger.warning(() -> "Encountered: " + pointerKey.getClass()); + } else LOGGER.warning(() -> "Encountered: " + pointerKey.getClass()); }); final String functionSignature = "script " + filename + "." + functionName + ".do()LRoot;"; diff --git a/com.ibm.wala.cast.python.ml/source/com/ibm/wala/cast/python/ml/types/TensorFlowTypes.java b/com.ibm.wala.cast.python.ml/source/com/ibm/wala/cast/python/ml/types/TensorFlowTypes.java index 8e0cb5acf..dbe791c2b 100644 --- a/com.ibm.wala.cast.python.ml/source/com/ibm/wala/cast/python/ml/types/TensorFlowTypes.java +++ b/com.ibm.wala.cast.python.ml/source/com/ibm/wala/cast/python/ml/types/TensorFlowTypes.java @@ -11,6 +11,9 @@ */ public class TensorFlowTypes extends PythonTypes { + public static final TypeReference TENSORFLOW = + TypeReference.findOrCreate(pythonLoader, TypeName.findOrCreate("Ltensorflow")); + public static final TypeReference DATASET = TypeReference.findOrCreate(pythonLoader, TypeName.findOrCreate("Ltensorflow/data/Dataset")); diff --git a/com.ibm.wala.cast.python.test/data/tf2n3.py b/com.ibm.wala.cast.python.test/data/tf2n3.py index 211f64b91..2921e2c09 100644 --- a/com.ibm.wala.cast.python.test/data/tf2n3.py +++ b/com.ibm.wala.cast.python.test/data/tf2n3.py @@ -3,7 +3,7 @@ def func2(t): - pass + assert isinstance(t, tf.Tensor) @tf.function diff --git a/com.ibm.wala.cast.python.test/source/com/ibm/wala/cast/python/test/TestMulti.java b/com.ibm.wala.cast.python.test/source/com/ibm/wala/cast/python/test/TestMulti.java index 2c4a33613..7a27637ad 100644 --- a/com.ibm.wala.cast.python.test/source/com/ibm/wala/cast/python/test/TestMulti.java +++ b/com.ibm.wala.cast.python.test/source/com/ibm/wala/cast/python/test/TestMulti.java @@ -37,10 +37,32 @@ public void testMulti1() PropagationCallGraphBuilder builder = (PropagationCallGraphBuilder) engine.defaultCallGraphBuilder(); CallGraph CG = builder.makeCallGraph(engine.getOptions(), new NullProgressMonitor()); + CAstCallGraphUtil.AVOID_DUMP = false; + CAstCallGraphUtil.dumpCG( + (SSAContextInterpreter) builder.getContextInterpreter(), builder.getPointerAnalysis(), CG); verifyGraphAssertions(CG, assertionsMulti1); + } + + protected static final Object[][] assertionsMulti2 = + new Object[][] { + new Object[] {ROOT, new String[] {"script multi1.py", "script multi2.py"}}, + // TODO: Add the following code once https://github.com/wala/ML/issues/168 is fixed: + // new Object[] {"script multi1.py", new String[] {"script multi2.py/silly"}}, + // TODO: Add the following code once https://github.com/wala/ML/issues/168 is fixed: + // new Object[] {"script multi2.py/silly", new String[] {"script multi2.py/silly/inner"}}, + }; + + @Test + public void testMulti2() + throws ClassHierarchyException, IllegalArgumentException, CancelException, IOException { + PythonAnalysisEngine engine = makeEngine("multi1.py", "multi2.py"); + PropagationCallGraphBuilder builder = + (PropagationCallGraphBuilder) engine.defaultCallGraphBuilder(); + CallGraph CG = builder.makeCallGraph(engine.getOptions(), new NullProgressMonitor()); CAstCallGraphUtil.AVOID_DUMP = false; CAstCallGraphUtil.dumpCG( (SSAContextInterpreter) builder.getContextInterpreter(), builder.getPointerAnalysis(), CG); + verifyGraphAssertions(CG, assertionsMulti2); } protected static final Object[][] assertionsMulti3 = diff --git a/com.ibm.wala.cast.python/source/com/ibm/wala/cast/python/ipa/callgraph/PythonSSAPropagationCallGraphBuilder.java b/com.ibm.wala.cast.python/source/com/ibm/wala/cast/python/ipa/callgraph/PythonSSAPropagationCallGraphBuilder.java index 403c3017b..5650320d1 100644 --- a/com.ibm.wala.cast.python/source/com/ibm/wala/cast/python/ipa/callgraph/PythonSSAPropagationCallGraphBuilder.java +++ b/com.ibm.wala.cast.python/source/com/ibm/wala/cast/python/ipa/callgraph/PythonSSAPropagationCallGraphBuilder.java @@ -67,6 +67,7 @@ public class PythonSSAPropagationCallGraphBuilder extends AstSSAPropagationCallGraphBuilder { + @SuppressWarnings("unused") private static final Logger logger = Logger.getLogger(PythonSSAPropagationCallGraphBuilder.class.getName()); diff --git a/com.ibm.wala.cast.python/source/com/ibm/wala/cast/python/ipa/summaries/BuiltinFunctions.java b/com.ibm.wala.cast.python/source/com/ibm/wala/cast/python/ipa/summaries/BuiltinFunctions.java index 73ef701b8..c95db053e 100644 --- a/com.ibm.wala.cast.python/source/com/ibm/wala/cast/python/ipa/summaries/BuiltinFunctions.java +++ b/com.ibm.wala.cast.python/source/com/ibm/wala/cast/python/ipa/summaries/BuiltinFunctions.java @@ -292,6 +292,8 @@ private static TypeReference builtinFunction(String name) { builtinFunctions.put("iter", Either.forRight(2)); // https://docs.python.org/3/library/functions.html#next builtinFunctions.put("next", Either.forLeft(PythonTypes.object)); + // https://docs.python.org/3/library/functions.html#isinstance + builtinFunctions.put("isinstance", Either.forLeft(TypeReference.Boolean)); } public static Set builtins() { diff --git a/com.ibm.wala.cast.python/source/com/ibm/wala/cast/python/ir/PythonCAstToIRTranslator.java b/com.ibm.wala.cast.python/source/com/ibm/wala/cast/python/ir/PythonCAstToIRTranslator.java index 9eb559993..106624e01 100644 --- a/com.ibm.wala.cast.python/source/com/ibm/wala/cast/python/ir/PythonCAstToIRTranslator.java +++ b/com.ibm.wala.cast.python/source/com/ibm/wala/cast/python/ir/PythonCAstToIRTranslator.java @@ -39,7 +39,6 @@ import com.ibm.wala.classLoader.CallSiteReference; import com.ibm.wala.classLoader.IClass; import com.ibm.wala.classLoader.IClassLoader; -import com.ibm.wala.classLoader.ModuleEntry; import com.ibm.wala.classLoader.NewSiteReference; import com.ibm.wala.core.util.strings.Atom; import com.ibm.wala.shrike.shrikeBT.IBinaryOpInstruction; @@ -71,21 +70,18 @@ public class PythonCAstToIRTranslator extends AstTranslator { private final Map walaTypeNames = HashMapFactory.make(); private final Set> globalDeclSet = new HashSet<>(); - private static boolean signleFileAnalysis = true; - private final Set> topLevelEntities; + private static boolean singleFileAnalysis = true; - public PythonCAstToIRTranslator( - IClassLoader loader, Set> topLevelEntities) { + public PythonCAstToIRTranslator(IClassLoader loader) { super(loader); - this.topLevelEntities = topLevelEntities; } public static boolean isSingleFileAnalysis() { - return signleFileAnalysis; + return singleFileAnalysis; } public static void setSingleFileAnalysis(boolean singleFile) { - PythonCAstToIRTranslator.signleFileAnalysis = singleFile; + PythonCAstToIRTranslator.singleFileAnalysis = singleFile; } @Override @@ -899,7 +895,7 @@ private void handleObjectLiteralAssign( } boolean isGlobal(WalkContext context, String varName) { - if (signleFileAnalysis) return false; + if (singleFileAnalysis) return false; else { if (context.currentScope().getEntity().getKind() == CAstEntity.SCRIPT_ENTITY) return true; else { diff --git a/com.ibm.wala.cast.python/source/com/ibm/wala/cast/python/loader/PythonLoader.java b/com.ibm.wala.cast.python/source/com/ibm/wala/cast/python/loader/PythonLoader.java index c82645ee1..18b6c4355 100644 --- a/com.ibm.wala.cast.python/source/com/ibm/wala/cast/python/loader/PythonLoader.java +++ b/com.ibm.wala.cast.python/source/com/ibm/wala/cast/python/loader/PythonLoader.java @@ -176,7 +176,7 @@ protected boolean shouldTranslate(CAstEntity entity) { @Override protected TranslatorToIR initTranslator(Set> topLevelEntities) { - return new PythonCAstToIRTranslator(this, topLevelEntities); + return new PythonCAstToIRTranslator(this); } final CoreClass CodeBody = diff --git a/logging.properties b/logging.properties index 996996f74..34dd4fef5 100644 --- a/logging.properties +++ b/logging.properties @@ -26,7 +26,7 @@ handlers= java.util.logging.ConsoleHandler # can be overriden by a facility specific level # Note that the ConsoleHandler also has a separate level # setting to limit messages printed to the console. -.level= FINE +.level= FINEST ############################################################ # Handler specific properties. @@ -40,7 +40,7 @@ handlers= java.util.logging.ConsoleHandler #java.util.logging.FileHandler.formatter = java.util.logging.XMLFormatter # Limit the message that are printed on the console to INFO and above. -java.util.logging.ConsoleHandler.level = FINE +java.util.logging.ConsoleHandler.level = FINEST java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter # Example to customize the SimpleFormatter output format