Skip to content

Commit 9525e65

Browse files
olesya.subbotinanadeeshtv
authored andcommitted
Fix regex in KMP for visiting inner classes
Summary: The old regex alone was accepting all inner classes per each inner class instead of just the next inner class in the hierarchy, therefore the analysis was blowing up and visiting classes an unnesessarily huge amount of times. Now the visits happen only to the classes that have not been previously visited, therefore there is no additional branching happening.
1 parent 63b9f2e commit 9525e65

1 file changed

Lines changed: 17 additions & 6 deletions

File tree

kmp-library/src/main/java/com/guardsquare/proguard/kotlin/printer/KotlinMetadataPrinter.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@
5858
import proguard.classfile.kotlin.visitor.filter.KotlinTypeFilter;
5959
import proguard.classfile.util.ClassUtil;
6060
import proguard.classfile.visitor.ClassCounter;
61+
import proguard.classfile.visitor.ClassPoolFiller;
62+
import proguard.classfile.visitor.ClassPresenceFilter;
6163
import proguard.classfile.visitor.MultiClassVisitor;
6264
import proguard.classfile.visitor.MultiMemberVisitor;
6365
import com.guardsquare.proguard.kotlin.printer.internal.visitor.KotlinTypeVisitorWrapper;
@@ -95,7 +97,7 @@ public class KotlinMetadataPrinter
9597
private int indentation;
9698
private Context context;
9799
private final boolean excludeEmbedded;
98-
100+
private final ClassPool visitedNestedClassPool = new ClassPool();
99101

100102
public KotlinMetadataPrinter(ClassPool programClassPool)
101103
{
@@ -326,6 +328,7 @@ else if (classCounter.getCount() > 0)
326328
});
327329

328330
kotlinClassKindMetadata.nestedClassesAccept(false,
331+
new MultiClassVisitor(new ClassPoolFiller(visitedNestedClassPool),
329332
new ReferencedKotlinMetadataVisitor(
330333
new KotlinMetadataVisitorWrapper(
331334
(i, metadata) -> {
@@ -336,7 +339,11 @@ else if (classCounter.getCount() > 0)
336339
println();
337340
}
338341
},
339-
MyKotlinSourceMetadataPrinter.this)));
342+
MyKotlinSourceMetadataPrinter.this))));
343+
344+
if (kotlinClassKindMetadata.referencedCompanionClass != null) {
345+
visitedNestedClassPool.removeClass(kotlinClassKindMetadata.referencedCompanionClass);
346+
}
340347

341348
visitChildClasses(clazz);
342349

@@ -1079,11 +1086,15 @@ private void visitChildClasses(Clazz clazz)
10791086
{
10801087
// Cache the synthetic class string, as we might have already visited them, so the string will be empty.
10811088
pushStringBuilder();
1089+
// Only visit the classes that have not yet been visited in the previous nested classes visits.
10821090
programClassPool.classesAccept(
1083-
clazz.getName() + TypeConstants.INNER_CLASS_SEPARATOR + "[^$]+",
1084-
new ReferencedKotlinMetadataVisitor(MyKotlinSourceMetadataPrinter.this));
1091+
clazz.getName() + TypeConstants.INNER_CLASS_SEPARATOR + "*",
1092+
new ClassPresenceFilter(
1093+
visitedNestedClassPool,
1094+
null,
1095+
new ReferencedKotlinMetadataVisitor(MyKotlinSourceMetadataPrinter.this)));
10851096
String innerClassesString = popStringBuilder();
1086-
if (innerClassesString.length() > 0)
1097+
if (!innerClassesString.isEmpty())
10871098
{
10881099
println();
10891100
println("// Synthetic inner classes - these were generated by the Kotlin compiler from e.g. lambdas", true);
@@ -1326,7 +1337,7 @@ private String valueParameterFlags(KotlinValueParameterFlags flags)
13261337
(flags.isNoInline ? "noinline " : "") +
13271338
(flags.hasDefaultValue ? "" : "");
13281339
}
1329-
1340+
13301341
private enum ValueParameterType
13311342
{
13321343
NORMAL, VAR, VAL

0 commit comments

Comments
 (0)