Skip to content

Commit

Permalink
Initial support for resolving wildcard imports (#169)
Browse files Browse the repository at this point in the history
Fixes #65 for both libraries and
(internal) "scripts."
  • Loading branch information
khatchad authored Mar 19, 2024
1 parent 404a34f commit 3ded12f
Show file tree
Hide file tree
Showing 15 changed files with 459 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -293,11 +293,9 @@ public void testR()
@Test
public void testS()
throws ClassHierarchyException, IllegalArgumentException, CancelException, IOException {
test(
"tf2s.py", "add", 0,
0); // NOTE: Set the expected number of tensor parameters, variables, and tensor parameter
// value numbers to 2, 3, and 2 and 3, respectively, when
// https://github.com/wala/ML/issues/65 is fixed.
// NOTE: Set the expected number of tensor variables to 3 once
// https://github.com/wala/ML/issues/135 is fixed.
test("tf2s.py", "add", 2, 2, 2, 3);
}

@Test
Expand Down Expand Up @@ -1422,6 +1420,75 @@ public void testTFRange3()
test("test_tf_range.py", "f", 1, 1, 2);
}

@Test
public void testImport()
throws ClassHierarchyException, IllegalArgumentException, CancelException, IOException {
test("tf2_test_import.py", "f", 1, 1, 2);
}

@Test
public void testImport2()
throws ClassHierarchyException, IllegalArgumentException, CancelException, IOException {
test("tf2_test_import2.py", "f", 1, 1, 2);
}

@Test
public void testImport3()
throws ClassHierarchyException, IllegalArgumentException, CancelException, IOException {
test("tf2_test_import3.py", "f", 1, 2, 2);
test("tf2_test_import3.py", "g", 1, 1, 2);
}

@Test
public void testImport4()
throws ClassHierarchyException, IllegalArgumentException, CancelException, IOException {
test("tf2_test_import4.py", "f", 1, 2, 2);
test("tf2_test_import4.py", "g", 1, 1, 2);
}

@Test
public void testImport5()
throws ClassHierarchyException, IllegalArgumentException, CancelException, IOException {
test("tf2_test_import5.py", "f", 0, 1);
test("tf2_test_import5.py", "g", 1, 1, 2);
}

@Test
public void testImport6()
throws ClassHierarchyException, IllegalArgumentException, CancelException, IOException {
test("tf2_test_import6.py", "f", 0, 1);
test("tf2_test_import6.py", "g", 1, 1, 2);
}

/**
* This is an invalid case. If there are no wildcard imports, we should resolve them like they
* are.
*/
@Test
public void testImport7()
throws ClassHierarchyException, IllegalArgumentException, CancelException, IOException {
test("tf2_test_import7.py", "f", 0, 0);
test("tf2_test_import7.py", "g", 0, 0);
}

/**
* This is an invalid case. If there are no wildcard imports, we should resolve them like they
* are.
*/
@Test
public void testImport8()
throws ClassHierarchyException, IllegalArgumentException, CancelException, IOException {
test("tf2_test_import8.py", "f", 0, 0);
test("tf2_test_import8.py", "g", 0, 0);
}

@Test
public void testImport9()
throws ClassHierarchyException, IllegalArgumentException, CancelException, IOException {
test("tf2_test_import9.py", "f", 1, 1, 2);
test("tf2_test_import9.py", "g", 1, 1, 2);
}

private void test(
String filename,
String functionName,
Expand Down
4 changes: 4 additions & 0 deletions com.ibm.wala.cast.python.test/data/multi3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from multi2 import silly, x

print(silly(1))
print(x)
4 changes: 4 additions & 0 deletions com.ibm.wala.cast.python.test/data/multi4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from multi2 import x, silly

print(silly(1))
print(x)
4 changes: 4 additions & 0 deletions com.ibm.wala.cast.python.test/data/multi5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from multi2 import *

print(silly(1))
print(x)
10 changes: 10 additions & 0 deletions com.ibm.wala.cast.python.test/data/tf2_test_import.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Test https://github.com/wala/ML/issues/65.

from tensorflow import ones, Tensor


def f(a):
assert isinstance(a, Tensor)


f(ones([1, 2]))
10 changes: 10 additions & 0 deletions com.ibm.wala.cast.python.test/data/tf2_test_import2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Test https://github.com/wala/ML/issues/65.

from tensorflow import *


def f(a):
assert isinstance(a, Tensor)


f(ones([1, 2]))
15 changes: 15 additions & 0 deletions com.ibm.wala.cast.python.test/data/tf2_test_import3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Test https://github.com/wala/ML/issues/65.

from tensorflow import ones, Tensor


def g(a):
assert isinstance(a, Tensor)


def f(a):
assert isinstance(a, Tensor)
g(ones([1, 2]))


f(ones([1, 2]))
15 changes: 15 additions & 0 deletions com.ibm.wala.cast.python.test/data/tf2_test_import4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Test https://github.com/wala/ML/issues/65.

from tensorflow import *


def g(a):
assert isinstance(a, Tensor)


def f(a):
assert isinstance(a, Tensor)
g(ones([1, 2]))


f(ones([1, 2]))
14 changes: 14 additions & 0 deletions com.ibm.wala.cast.python.test/data/tf2_test_import5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Test https://github.com/wala/ML/issues/65.

from tensorflow import ones, Tensor


def g(a):
assert isinstance(a, Tensor)


def f(a):
g(ones([1, 2]))


f(5)
14 changes: 14 additions & 0 deletions com.ibm.wala.cast.python.test/data/tf2_test_import6.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Test https://github.com/wala/ML/issues/65.

from tensorflow import *


def g(a):
assert isinstance(a, Tensor)


def f(a):
g(ones([1, 2]))


f(5)
13 changes: 13 additions & 0 deletions com.ibm.wala.cast.python.test/data/tf2_test_import7.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Test https://github.com/wala/ML/issues/65.
# This is an invalid case. No wildcard import; we shouldn't present that there is one.


def g(a):
assert isinstance(a, Tensor)


def f(a):
g(ones([1, 2]))


f(5)
15 changes: 15 additions & 0 deletions com.ibm.wala.cast.python.test/data/tf2_test_import8.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Test https://github.com/wala/ML/issues/65.
# This is an invalid case. No wildcard import; we shouldn't present that there is one.

from tensorflow import Tensor


def g(a):
assert isinstance(a, Tensor)


def f(a):
g(ones([1, 2]))


f(5)
15 changes: 15 additions & 0 deletions com.ibm.wala.cast.python.test/data/tf2_test_import9.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Test https://github.com/wala/ML/issues/65.

from tensorflow import *


def g(a):
assert isinstance(a, Tensor)


def f(a):
assert isinstance(a, Tensor)
g(a)


f(ones([1, 2]))
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,64 @@ public void testMulti1()
CAstCallGraphUtil.dumpCG(
(SSAContextInterpreter) builder.getContextInterpreter(), builder.getPointerAnalysis(), CG);
}

protected static final Object[][] assertionsMulti3 =
new Object[][] {
new Object[] {ROOT, new String[] {"script multi3.py", "script multi2.py"}},
new Object[] {"script multi3.py", new String[] {"script multi2.py/silly"}},
new Object[] {"script multi2.py/silly", new String[] {"script multi2.py/silly/inner"}},
};

@Test
public void testMulti3()
throws ClassHierarchyException, IllegalArgumentException, CancelException, IOException {
PythonAnalysisEngine<?> engine = makeEngine("multi2.py", "multi3.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, assertionsMulti3);
}

protected static final Object[][] assertionsMulti4 =
new Object[][] {
new Object[] {ROOT, new String[] {"script multi4.py", "script multi2.py"}},
new Object[] {"script multi4.py", new String[] {"script multi2.py/silly"}},
new Object[] {"script multi2.py/silly", new String[] {"script multi2.py/silly/inner"}},
};

@Test
public void testMulti4()
throws ClassHierarchyException, IllegalArgumentException, CancelException, IOException {
PythonAnalysisEngine<?> engine = makeEngine("multi2.py", "multi4.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, assertionsMulti4);
}

protected static final Object[][] assertionsMulti5 =
new Object[][] {
new Object[] {ROOT, new String[] {"script multi5.py", "script multi2.py"}},
new Object[] {"script multi5.py", new String[] {"script multi2.py/silly"}},
new Object[] {"script multi2.py/silly", new String[] {"script multi2.py/silly/inner"}},
};

@Test
public void testMulti5()
throws ClassHierarchyException, IllegalArgumentException, CancelException, IOException {
PythonAnalysisEngine<?> engine = makeEngine("multi2.py", "multi5.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, assertionsMulti5);
}
}
Loading

0 comments on commit 3ded12f

Please sign in to comment.