FIR CFA: properly visit subgraphs in checkers

Interpretation: a graph A is a subgraph of B if information available at
nodes of A depends on the paths taken in B. For example, local classes
are subgraphs of a graph in which they are declared, and members of
those classes are subgraphs of the local class itself - because these
members can reference captured values.

Consequences:

 * if graph G is a subgraph of node N, then G is a subgraph of N's
   owner;
 * `ControlFlowAnalysisDiagnosticComponent` will only visit root graphs;
 * `graph.traverse` will ignore subgraph boundaries, as if all subgraphs
   are inlined into one huge root graph;
 * if a control flow checker needs information from a declaration to
   which a graph is attached, it must look at subgraphs explicitly.

For example, consider the `callsInPlace` checker. When a function
has a `callsInPlace` contract and a local declaration, the checker must
visit that local declaration to ensure it does not capture the allegedly
called-in-place argument - hence `graph.traverse` will look at the
nodes. However, the local declaration can also be a function with its
own `callsInPlace` contracts, so the checker should also run for it in
isolation. If that sounds quadratic, that's because unfortunately it is.
This commit is contained in:
pyos
2022-12-09 12:32:46 +01:00
committed by Dmitriy Novozhilov
parent 5180e1f4a4
commit aadea0e26f
35 changed files with 1191 additions and 1325 deletions
@@ -344,25 +344,24 @@ digraph boundSmartcasts_kt {
126 -> {127};
subgraph cluster_28 {
color=red
128 [label="Enter property" style="filled" fillcolor=red];
129 [label="Access variable R|<local>/any|"];
130 [label="Exit property" style="filled" fillcolor=red];
}
128 -> {129};
129 -> {130};
130 -> {133} [color=green];
subgraph cluster_29 {
color=red
131 [label="Enter class D" style="filled" fillcolor=red];
132 [label="Part of class initialization"];
subgraph cluster_29 {
color=blue
128 [label="Enter property" style="filled" fillcolor=red];
129 [label="Access variable R|<local>/any|"];
130 [label="Exit property" style="filled" fillcolor=red];
}
133 [label="Exit class D" style="filled" fillcolor=red];
}
131 -> {132} [color=green];
132 -> {128} [color=green];
132 -> {133} [style=dotted];
132 -> {128} [style=dashed];
128 -> {129};
129 -> {130};
130 -> {133} [color=green];
subgraph cluster_30 {
color=red
@@ -13,25 +13,24 @@ digraph boundSmartcastsInBranches_kt {
1 -> {2};
subgraph cluster_1 {
color=red
3 [label="Enter property" style="filled" fillcolor=red];
4 [label="Const: String()"];
5 [label="Exit property" style="filled" fillcolor=red];
}
3 -> {4};
4 -> {5};
5 -> {8} [color=green];
subgraph cluster_2 {
color=red
6 [label="Enter class A" style="filled" fillcolor=red];
7 [label="Part of class initialization"];
subgraph cluster_2 {
color=blue
3 [label="Enter property" style="filled" fillcolor=red];
4 [label="Const: String()"];
5 [label="Exit property" style="filled" fillcolor=red];
}
8 [label="Exit class A" style="filled" fillcolor=red];
}
6 -> {7} [color=green];
7 -> {3} [color=green];
7 -> {8} [style=dotted];
7 -> {3} [style=dashed];
3 -> {4};
4 -> {5};
5 -> {8} [color=green];
subgraph cluster_3 {
color=red
@@ -29,25 +29,24 @@ digraph functionCallBound_kt {
6 -> {7};
subgraph cluster_3 {
color=red
8 [label="Enter property" style="filled" fillcolor=red];
9 [label="Access variable R|<local>/data|"];
10 [label="Exit property" style="filled" fillcolor=red];
}
8 -> {9};
9 -> {10};
10 -> {13} [color=green];
subgraph cluster_4 {
color=red
11 [label="Enter class Sub" style="filled" fillcolor=red];
12 [label="Part of class initialization"];
subgraph cluster_4 {
color=blue
8 [label="Enter property" style="filled" fillcolor=red];
9 [label="Access variable R|<local>/data|"];
10 [label="Exit property" style="filled" fillcolor=red];
}
13 [label="Exit class Sub" style="filled" fillcolor=red];
}
11 -> {12} [color=green];
12 -> {8} [color=green];
12 -> {13} [style=dotted];
12 -> {8} [style=dashed];
8 -> {9};
9 -> {10};
10 -> {13} [color=green];
subgraph cluster_5 {
color=red
@@ -29,26 +29,16 @@ digraph lambdaInWhenBranch_kt {
6 -> {7};
subgraph cluster_3 {
color=red
8 [label="Enter property" style="filled" fillcolor=red];
9 [label="Access variable R|<local>/t|"];
10 [label="Exit property" style="filled" fillcolor=red];
}
8 -> {9};
9 -> {10};
10 -> {20} [color=green];
subgraph cluster_4 {
color=red
11 [label="Enter function component1" style="filled" fillcolor=red];
12 [label="Exit function component1" style="filled" fillcolor=red];
}
11 -> {12};
subgraph cluster_5 {
subgraph cluster_4 {
color=red
13 [label="Enter function copy" style="filled" fillcolor=red];
subgraph cluster_6 {
subgraph cluster_5 {
color=blue
15 [label="Enter default value of t" style="filled" fillcolor=red];
16 [label="Access variable R|/SubClass1.t|"];
@@ -61,16 +51,25 @@ digraph lambdaInWhenBranch_kt {
15 -> {15} [style=dashed];
16 -> {17};
subgraph cluster_7 {
subgraph cluster_6 {
color=red
18 [label="Enter class SubClass1" style="filled" fillcolor=red];
19 [label="Part of class initialization"];
subgraph cluster_7 {
color=blue
8 [label="Enter property" style="filled" fillcolor=red];
9 [label="Access variable R|<local>/t|"];
10 [label="Exit property" style="filled" fillcolor=red];
}
20 [label="Exit class SubClass1" style="filled" fillcolor=red];
}
18 -> {19} [color=green];
19 -> {8} [color=green];
19 -> {20} [style=dotted];
19 -> {8} [style=dashed];
8 -> {9};
9 -> {10};
10 -> {20} [color=green];
subgraph cluster_8 {
color=red
@@ -62,19 +62,9 @@ digraph nullability_kt {
15 -> {16};
subgraph cluster_8 {
color=red
17 [label="Enter property" style="filled" fillcolor=red];
18 [label="Access variable R|<local>/data|"];
19 [label="Exit property" style="filled" fillcolor=red];
}
17 -> {18};
18 -> {19};
19 -> {29} [color=green];
subgraph cluster_9 {
color=red
20 [label="Enter function fdata" style="filled" fillcolor=red];
subgraph cluster_10 {
subgraph cluster_9 {
color=blue
21 [label="Enter block"];
22 [label="Const: Null(null)"];
@@ -92,16 +82,25 @@ digraph nullability_kt {
24 -> {25} [style=dotted];
25 -> {26} [style=dotted];
subgraph cluster_11 {
subgraph cluster_10 {
color=red
27 [label="Enter class QImpl" style="filled" fillcolor=red];
28 [label="Part of class initialization"];
subgraph cluster_11 {
color=blue
17 [label="Enter property" style="filled" fillcolor=red];
18 [label="Access variable R|<local>/data|"];
19 [label="Exit property" style="filled" fillcolor=red];
}
29 [label="Exit class QImpl" style="filled" fillcolor=red];
}
27 -> {28} [color=green];
28 -> {17} [color=green];
28 -> {29} [style=dotted];
28 -> {17} [style=dashed];
17 -> {18};
18 -> {19};
19 -> {29} [color=green];
subgraph cluster_12 {
color=red
@@ -113,19 +112,9 @@ digraph nullability_kt {
31 -> {32};
subgraph cluster_13 {
color=red
33 [label="Enter property" style="filled" fillcolor=red];
34 [label="Access variable R|<local>/data|"];
35 [label="Exit property" style="filled" fillcolor=red];
}
33 -> {34};
34 -> {35};
35 -> {45} [color=green];
subgraph cluster_14 {
color=red
36 [label="Enter function fdata" style="filled" fillcolor=red];
subgraph cluster_15 {
subgraph cluster_14 {
color=blue
37 [label="Enter block"];
38 [label="Const: Null(null)"];
@@ -143,16 +132,25 @@ digraph nullability_kt {
40 -> {41} [style=dotted];
41 -> {42} [style=dotted];
subgraph cluster_16 {
subgraph cluster_15 {
color=red
43 [label="Enter class QImplMutable" style="filled" fillcolor=red];
44 [label="Part of class initialization"];
subgraph cluster_16 {
color=blue
33 [label="Enter property" style="filled" fillcolor=red];
34 [label="Access variable R|<local>/data|"];
35 [label="Exit property" style="filled" fillcolor=red];
}
45 [label="Exit class QImplMutable" style="filled" fillcolor=red];
}
43 -> {44} [color=green];
44 -> {33} [color=green];
44 -> {45} [style=dotted];
44 -> {33} [style=dashed];
33 -> {34};
34 -> {35};
35 -> {45} [color=green];
subgraph cluster_17 {
color=red
@@ -34,19 +34,9 @@ digraph assignSafeCall_kt {
8 -> {9} [style=dotted];
subgraph cluster_3 {
color=red
10 [label="Enter property" style="filled" fillcolor=red];
11 [label="Const: Int(1)"];
12 [label="Exit property" style="filled" fillcolor=red];
}
10 -> {11};
11 -> {12};
12 -> {19} [color=green];
subgraph cluster_4 {
color=red
13 [label="Enter function bar" style="filled" fillcolor=red];
subgraph cluster_5 {
subgraph cluster_4 {
color=blue
14 [label="Enter block"];
15 [label="Exit block"];
@@ -57,16 +47,25 @@ digraph assignSafeCall_kt {
14 -> {15};
15 -> {16};
subgraph cluster_6 {
subgraph cluster_5 {
color=red
17 [label="Enter class A" style="filled" fillcolor=red];
18 [label="Part of class initialization"];
subgraph cluster_6 {
color=blue
10 [label="Enter property" style="filled" fillcolor=red];
11 [label="Const: Int(1)"];
12 [label="Exit property" style="filled" fillcolor=red];
}
19 [label="Exit class A" style="filled" fillcolor=red];
}
17 -> {18} [color=green];
18 -> {10} [color=green];
18 -> {19} [style=dotted];
18 -> {10} [style=dashed];
10 -> {11};
11 -> {12};
12 -> {19} [color=green];
subgraph cluster_7 {
color=red
@@ -57,19 +57,29 @@ digraph smartCastInInit_kt {
subgraph cluster_6 {
color=red
17 [label="Enter init block" style="filled" fillcolor=red];
26 [label="Enter class Main" style="filled" fillcolor=red];
27 [label="Part of class initialization"];
subgraph cluster_7 {
color=blue
18 [label="Enter block"];
19 [label="Function call: R|/s|()" style="filled" fillcolor=yellow];
20 [label="Assignment: R|/Main.x|"];
21 [label="Access variable R|/Main.x|"];
22 [label="Smart cast: this@R|/Main|.R|/Main.x|"];
23 [label="Function call: this@R|/Main|.R|/Main.x|.R|/S.foo|()" style="filled" fillcolor=yellow];
24 [label="Exit block"];
17 [label="Enter init block" style="filled" fillcolor=red];
subgraph cluster_8 {
color=blue
18 [label="Enter block"];
19 [label="Function call: R|/s|()" style="filled" fillcolor=yellow];
20 [label="Assignment: R|/Main.x|"];
21 [label="Access variable R|/Main.x|"];
22 [label="Smart cast: this@R|/Main|.R|/Main.x|"];
23 [label="Function call: this@R|/Main|.R|/Main.x|.R|/S.foo|()" style="filled" fillcolor=yellow];
24 [label="Exit block"];
}
25 [label="Exit init block" style="filled" fillcolor=red];
}
25 [label="Exit init block" style="filled" fillcolor=red];
28 [label="Exit class Main" style="filled" fillcolor=red];
}
26 -> {27} [color=green];
27 -> {17} [color=green];
27 -> {28} [style=dotted];
27 -> {17} [style=dashed];
17 -> {18};
18 -> {19};
19 -> {20};
@@ -80,15 +90,4 @@ digraph smartCastInInit_kt {
24 -> {25};
25 -> {28} [color=green];
subgraph cluster_8 {
color=red
26 [label="Enter class Main" style="filled" fillcolor=red];
27 [label="Part of class initialization"];
28 [label="Exit class Main" style="filled" fillcolor=red];
}
26 -> {27} [color=green];
27 -> {17} [color=green];
27 -> {28} [style=dotted];
27 -> {17} [style=dashed];
}
@@ -13,30 +13,22 @@ digraph smartcastInByClause_kt {
1 -> {2};
subgraph cluster_1 {
color=red
3 [label="Enter property" style="filled" fillcolor=red];
4 [label="Access variable R|<local>/path|"];
5 [label="Exit property" style="filled" fillcolor=red];
}
3 -> {4};
4 -> {5};
5 -> {11} [color=green];
subgraph cluster_2 {
color=red
6 [label="Enter property" style="filled" fillcolor=red];
7 [label="Access variable R|<local>/index|"];
8 [label="Exit property" style="filled" fillcolor=red];
}
6 -> {7};
7 -> {8};
8 -> {12} [color=green];
subgraph cluster_3 {
color=red
9 [label="Enter class A" style="filled" fillcolor=red];
10 [label="Part of class initialization"];
subgraph cluster_2 {
color=blue
3 [label="Enter property" style="filled" fillcolor=red];
4 [label="Access variable R|<local>/path|"];
5 [label="Exit property" style="filled" fillcolor=red];
}
11 [label="Part of class initialization"];
subgraph cluster_3 {
color=blue
6 [label="Enter property" style="filled" fillcolor=red];
7 [label="Access variable R|<local>/index|"];
8 [label="Exit property" style="filled" fillcolor=red];
}
12 [label="Exit class A" style="filled" fillcolor=red];
}
9 -> {10} [color=green];
@@ -46,6 +38,12 @@ digraph smartcastInByClause_kt {
11 -> {6} [color=green];
11 -> {12} [style=dotted];
11 -> {6} [style=dashed];
3 -> {4};
4 -> {5};
5 -> {11} [color=green];
6 -> {7};
7 -> {8};
8 -> {12} [color=green];
subgraph cluster_4 {
color=red
@@ -64,25 +62,24 @@ digraph smartcastInByClause_kt {
16 -> {17};
subgraph cluster_6 {
color=red
18 [label="Enter property" style="filled" fillcolor=red];
19 [label="Access variable R|<local>/index|"];
20 [label="Exit property" style="filled" fillcolor=red];
}
18 -> {19};
19 -> {20};
20 -> {23} [color=green];
subgraph cluster_7 {
color=red
21 [label="Enter class Derived" style="filled" fillcolor=red];
22 [label="Part of class initialization"];
subgraph cluster_7 {
color=blue
18 [label="Enter property" style="filled" fillcolor=red];
19 [label="Access variable R|<local>/index|"];
20 [label="Exit property" style="filled" fillcolor=red];
}
23 [label="Exit class Derived" style="filled" fillcolor=red];
}
21 -> {22} [color=green];
22 -> {18} [color=green];
22 -> {23} [style=dotted];
22 -> {18} [style=dashed];
18 -> {19};
19 -> {20};
20 -> {23} [color=green];
subgraph cluster_8 {
color=red
@@ -111,7 +108,24 @@ digraph smartcastInByClause_kt {
color=blue
49 [label="Enter class <anonymous object>" style="filled" fillcolor=red];
50 [label="Part of class initialization"];
subgraph cluster_11 {
color=blue
53 [label="Enter field" style="filled" fillcolor=red];
54 [label="Access variable R|<local>/a|"];
55 [label="Smart cast: R|<local>/a|"];
56 [label="Access variable R|/A.index|"];
57 [label="Function call: R|/Derived.Derived|(...)" style="filled" fillcolor=yellow];
58 [label="Exit field" style="filled" fillcolor=red];
}
51 [label="Part of class initialization"];
subgraph cluster_12 {
color=blue
59 [label="Enter property" style="filled" fillcolor=red];
60 [label="Access variable R|<local>/a|"];
61 [label="Smart cast: R|<local>/a|"];
62 [label="Access variable R|/A.index|"];
63 [label="Exit property" style="filled" fillcolor=red];
}
52 [label="Exit class <anonymous object>" style="filled" fillcolor=red];
}
43 [label="Exit anonymous object"];
@@ -137,6 +151,26 @@ digraph smartcastInByClause_kt {
}
48 [label="Exit function test" style="filled" fillcolor=red];
}
subgraph cluster_13 {
color=blue
67 [label="Enter function foo" style="filled" fillcolor=red];
subgraph cluster_14 {
color=blue
68 [label="Enter block"];
69 [label="Access variable R|<local>/a|"];
70 [label="Smart cast: R|<local>/a|"];
71 [label="Access variable R|/A.index|"];
72 [label="Function call: R|/takeInt|(...)" style="filled" fillcolor=yellow];
73 [label="Exit block"];
}
74 [label="Exit function foo" style="filled" fillcolor=red];
}
subgraph cluster_15 {
color=blue
64 [label="Enter function <init>" style="filled" fillcolor=red];
65 [label="Delegated constructor call: super<R|kotlin/Any|>()" style="filled" fillcolor=yellow];
66 [label="Exit function <init>" style="filled" fillcolor=red];
}
24 -> {25};
25 -> {26};
26 -> {27 31};
@@ -156,80 +190,39 @@ digraph smartcastInByClause_kt {
39 -> {40};
40 -> {41};
41 -> {42};
41 -> {53 56 62 67} [color=red];
41 -> {53 59 64 67} [color=red];
42 -> {49} [color=green];
42 -> {43} [color=red];
42 -> {49} [style=dashed];
43 -> {44};
43 -> {53 67} [color=green];
43 -> {53 67} [style=dashed];
43 -> {64 67} [color=green];
43 -> {64 67} [style=dashed];
44 -> {45};
45 -> {48};
45 -> {46} [style=dotted];
46 -> {47} [style=dotted];
47 -> {48} [style=dotted];
49 -> {50} [color=green];
50 -> {56} [color=green];
50 -> {53} [color=green];
50 -> {51} [style=dotted];
50 -> {56} [style=dashed];
51 -> {62} [color=green];
50 -> {53} [style=dashed];
51 -> {59} [color=green];
51 -> {52} [style=dotted];
51 -> {62} [style=dashed];
51 -> {59} [style=dashed];
52 -> {43} [color=green];
subgraph cluster_11 {
color=red
53 [label="Enter function <init>" style="filled" fillcolor=red];
54 [label="Delegated constructor call: super<R|kotlin/Any|>()" style="filled" fillcolor=yellow];
55 [label="Exit function <init>" style="filled" fillcolor=red];
}
53 -> {54};
54 -> {55};
subgraph cluster_12 {
color=red
56 [label="Enter field" style="filled" fillcolor=red];
57 [label="Access variable R|<local>/a|"];
58 [label="Smart cast: R|<local>/a|"];
59 [label="Access variable R|/A.index|"];
60 [label="Function call: R|/Derived.Derived|(...)" style="filled" fillcolor=yellow];
61 [label="Exit field" style="filled" fillcolor=red];
}
55 -> {56};
56 -> {57};
57 -> {58};
58 -> {59};
58 -> {51} [color=green];
59 -> {60};
60 -> {61};
61 -> {51} [color=green];
subgraph cluster_13 {
color=red
62 [label="Enter property" style="filled" fillcolor=red];
63 [label="Access variable R|<local>/a|"];
64 [label="Smart cast: R|<local>/a|"];
65 [label="Access variable R|/A.index|"];
66 [label="Exit property" style="filled" fillcolor=red];
}
61 -> {62};
62 -> {63};
63 -> {64};
63 -> {52} [color=green];
64 -> {65};
65 -> {66};
66 -> {52} [color=green];
subgraph cluster_14 {
color=red
67 [label="Enter function foo" style="filled" fillcolor=red];
subgraph cluster_15 {
color=blue
68 [label="Enter block"];
69 [label="Access variable R|<local>/a|"];
70 [label="Smart cast: R|<local>/a|"];
71 [label="Access variable R|/A.index|"];
72 [label="Function call: R|/takeInt|(...)" style="filled" fillcolor=yellow];
73 [label="Exit block"];
}
74 [label="Exit function foo" style="filled" fillcolor=red];
}
67 -> {68};
68 -> {69};
69 -> {70};
@@ -124,30 +124,22 @@ digraph smartcastToNothing_kt {
40 -> {41};
subgraph cluster_11 {
color=red
42 [label="Enter property" style="filled" fillcolor=red];
43 [label="Const: Int(1)"];
44 [label="Exit property" style="filled" fillcolor=red];
}
42 -> {43};
43 -> {44};
44 -> {50} [color=green];
subgraph cluster_12 {
color=red
45 [label="Enter property" style="filled" fillcolor=red];
46 [label="Const: Boolean(true)"];
47 [label="Exit property" style="filled" fillcolor=red];
}
45 -> {46};
46 -> {47};
47 -> {51} [color=green];
subgraph cluster_13 {
color=red
48 [label="Enter class A" style="filled" fillcolor=red];
49 [label="Part of class initialization"];
subgraph cluster_12 {
color=blue
42 [label="Enter property" style="filled" fillcolor=red];
43 [label="Const: Int(1)"];
44 [label="Exit property" style="filled" fillcolor=red];
}
50 [label="Part of class initialization"];
subgraph cluster_13 {
color=blue
45 [label="Enter property" style="filled" fillcolor=red];
46 [label="Const: Boolean(true)"];
47 [label="Exit property" style="filled" fillcolor=red];
}
51 [label="Exit class A" style="filled" fillcolor=red];
}
48 -> {49} [color=green];
@@ -157,6 +149,12 @@ digraph smartcastToNothing_kt {
50 -> {45} [color=green];
50 -> {51} [style=dotted];
50 -> {45} [style=dashed];
42 -> {43};
43 -> {44};
44 -> {50} [color=green];
45 -> {46};
46 -> {47};
47 -> {51} [color=green];
subgraph cluster_14 {
color=red
@@ -13,25 +13,24 @@ digraph overridenOpenVal_kt {
1 -> {2};
subgraph cluster_1 {
color=red
3 [label="Enter property" style="filled" fillcolor=red];
4 [label="Access variable R|<local>/x|"];
5 [label="Exit property" style="filled" fillcolor=red];
}
3 -> {4};
4 -> {5};
5 -> {8} [color=green];
subgraph cluster_2 {
color=red
6 [label="Enter class A" style="filled" fillcolor=red];
7 [label="Part of class initialization"];
subgraph cluster_2 {
color=blue
3 [label="Enter property" style="filled" fillcolor=red];
4 [label="Access variable R|<local>/x|"];
5 [label="Exit property" style="filled" fillcolor=red];
}
8 [label="Exit class A" style="filled" fillcolor=red];
}
6 -> {7} [color=green];
7 -> {3} [color=green];
7 -> {8} [style=dotted];
7 -> {3} [style=dashed];
3 -> {4};
4 -> {5};
5 -> {8} [color=green];
subgraph cluster_3 {
color=red