generate pseudocode for functions in local classes

#KT-4405 In progress
 #KT-3501 Fixed
This commit is contained in:
Svetlana Isakova
2014-01-17 18:12:18 +04:00
parent f74fbf9f8b
commit 5ef320173c
9 changed files with 183 additions and 12 deletions
@@ -916,16 +916,6 @@ public class JetControlFlowProcessor {
JetObjectDeclaration declaration = expression.getObjectDeclaration();
generateInstructions(declaration, context);
List<JetDeclaration> declarations = declaration.getDeclarations();
List<JetDeclaration> functions = Lists.newArrayList();
for (JetDeclaration localDeclaration : declarations) {
if (!(localDeclaration instanceof JetProperty) && !(localDeclaration instanceof JetClassInitializer)) {
functions.add(localDeclaration);
}
}
for (JetDeclaration function : functions) {
generateInstructions(function, context);
}
builder.createAnonymousObject(expression);
}
@@ -961,6 +951,13 @@ public class JetControlFlowProcessor {
generateInstructions(specifier, context);
}
List<JetDeclaration> declarations = classOrObject.getDeclarations();
if (JetPsiUtil.isLocal(classOrObject)) {
for (JetDeclaration declaration : declarations) {
generateInstructions(declaration, context);
}
return;
}
//For top-level and inner classes and objects functions are collected and checked separately.
for (JetDeclaration declaration : declarations) {
if (declaration instanceof JetProperty || declaration instanceof JetClassInitializer) {
generateInstructions(declaration, context);
@@ -0,0 +1,113 @@
== f ==
fun f() {
class LocalClass() {
fun f() {
val x = ""
fun loc() {
val x3 = ""
}
}
}
}
---------------------
L0:
<START>
mark({ class LocalClass() { fun f() { val x = "" fun loc() { val x3 = "" } } } })
jmp?(L2) NEXT:[<END>, d(fun f() { val x = "" fun loc() { val x3 = "" } })]
d(fun f() { val x = "" fun loc() { val x3 = "" } }) NEXT:[<SINK>]
L1:
L2:
<END> NEXT:[<SINK>] PREV:[jmp?(L2)]
error:
<ERROR> PREV:[]
sink:
<SINK> PREV:[<ERROR>, <END>, d(fun f() { val x = "" fun loc() { val x3 = "" } })]
L3:
<START>
mark({ val x = "" fun loc() { val x3 = "" } })
v(val x = "")
mark("")
r("")
w(x)
jmp?(L5) NEXT:[<END>, d(fun loc() { val x3 = "" })]
d(fun loc() { val x3 = "" }) NEXT:[<SINK>]
L4:
L5:
<END> NEXT:[<SINK>] PREV:[jmp?(L5)]
error:
<ERROR> PREV:[]
sink:
<SINK> PREV:[<ERROR>, <END>, d(fun loc() { val x3 = "" })]
L6:
<START>
mark({ val x3 = "" })
v(val x3 = "")
mark("")
r("")
w(x3)
L7:
<END> NEXT:[<SINK>]
error:
<ERROR> PREV:[]
sink:
<SINK> PREV:[<ERROR>, <END>]
=====================
== f ==
fun f() {
val x = ""
fun loc() {
val x3 = ""
}
}
---------------------
L3:
<START>
mark({ val x = "" fun loc() { val x3 = "" } })
v(val x = "")
mark("")
r("")
w(x)
jmp?(L5) NEXT:[<END>, d(fun loc() { val x3 = "" })]
d(fun loc() { val x3 = "" }) NEXT:[<SINK>]
L4:
L5:
<END> NEXT:[<SINK>] PREV:[jmp?(L5)]
error:
<ERROR> PREV:[]
sink:
<SINK> PREV:[<ERROR>, <END>, d(fun loc() { val x3 = "" })]
L6:
<START>
mark({ val x3 = "" })
v(val x3 = "")
mark("")
r("")
w(x3)
L7:
<END> NEXT:[<SINK>]
error:
<ERROR> PREV:[]
sink:
<SINK> PREV:[<ERROR>, <END>]
=====================
== loc ==
fun loc() {
val x3 = ""
}
---------------------
L6:
<START>
mark({ val x3 = "" })
v(val x3 = "")
mark("")
r("")
w(x3)
L7:
<END> NEXT:[<SINK>]
error:
<ERROR> PREV:[]
sink:
<SINK> PREV:[<ERROR>, <END>]
=====================
+11
View File
@@ -0,0 +1,11 @@
fun f() {
class LocalClass() {
fun f() {
val x = ""
fun loc() {
val x3 = ""
}
}
}
}
@@ -0,0 +1,9 @@
//KT-3501 Variable/parameter is highlighted as unused if it is used in member of local class
fun f(p: String) { // "p" is marked as unused
class LocalClass {
fun f() {
<!UNUSED_EXPRESSION!>p<!>
}
}
}
@@ -0,0 +1,26 @@
package f
fun f() {
class LocalClass() {
{
val <!UNUSED_VARIABLE!>x1<!> = "" // ok: unused
fun loc1(): Int {
val <!UNUSED_VARIABLE!>x1_<!> = "" // ok: unused
<!NO_RETURN_IN_FUNCTION_WITH_BLOCK_BODY!>}<!>
}
fun f() {
val <!UNUSED_VARIABLE!>x2<!> = "" // error: should be UNUSED_VARIABLE
fun loc2(): Int {
val <!UNUSED_VARIABLE!>x2_<!> = "" // error: should be UNUSED_VARIABLE
<!NO_RETURN_IN_FUNCTION_WITH_BLOCK_BODY, NO_RETURN_IN_FUNCTION_WITH_BLOCK_BODY!>}<!>
}
val v: String
get() {
val <!UNUSED_VARIABLE!>x3<!> = "" // ok: unused
<!NO_RETURN_IN_FUNCTION_WITH_BLOCK_BODY!>}<!>
}
}
@@ -7,7 +7,7 @@ fun f() {
}
fun member() {
val x: MyClass = MyClass()
val <!UNUSED_VARIABLE!>x<!>: MyClass = MyClass()
}
}
@@ -157,6 +157,11 @@ public class ControlFlowTestGenerated extends AbstractControlFlowTest {
doTest("compiler/testData/cfg/LazyBooleans.kt");
}
@TestMetadata("localClass.kt")
public void testLocalClass() throws Exception {
doTest("compiler/testData/cfg/localClass.kt");
}
@TestMetadata("LocalDeclarations.kt")
public void testLocalDeclarations() throws Exception {
doTest("compiler/testData/cfg/LocalDeclarations.kt");
@@ -1608,6 +1608,11 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest {
doTest("compiler/testData/diagnostics/tests/controlFlowAnalysis/kt3444.kt");
}
@TestMetadata("kt3501.kt")
public void testKt3501() throws Exception {
doTest("compiler/testData/diagnostics/tests/controlFlowAnalysis/kt3501.kt");
}
@TestMetadata("kt510.kt")
public void testKt510() throws Exception {
doTest("compiler/testData/diagnostics/tests/controlFlowAnalysis/kt510.kt");
@@ -1643,6 +1648,11 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest {
doTest("compiler/testData/diagnostics/tests/controlFlowAnalysis/kt897.kt");
}
@TestMetadata("localClasses.kt")
public void testLocalClasses() throws Exception {
doTest("compiler/testData/diagnostics/tests/controlFlowAnalysis/localClasses.kt");
}
@TestMetadata("propertiesOrderInPackage.kt")
public void testPropertiesOrderInPackage() throws Exception {
doTest("compiler/testData/diagnostics/tests/controlFlowAnalysis/propertiesOrderInPackage.kt");
@@ -58,7 +58,7 @@ fun localFunctionAndClass() {
}
}
val <info descr="Value captured in a closure"><warning>v</warning></info> = 1 // erroneous "unused warning" is caused by KT-3501
val <info descr="Value captured in a closure">v</info> = 1
class LocalClass {
fun f() {
run {