Test several line numbers are generated for one instruction

(cherry picked from commit 1ea9280)
This commit is contained in:
Nikolay Krasko
2016-10-03 20:23:10 +03:00
committed by Nikolay Krasko
parent 25051746d1
commit 3d9d6e666d
13 changed files with 38 additions and 38 deletions
@@ -10,4 +10,4 @@ fun testSome(): Boolean {
return false return false
} }
// 2 3 4 2 7 10 // 2 +3 4 2 7 10
+1 -1
View File
@@ -12,4 +12,4 @@ fun foo() {
.bar() .bar()
} }
// 2 3 1 7 8 9 8 9 11 12 11 12 17 13 // 2 3 1 7 8 +9 +8 9 11 +12 +11 12 16 13
+1 -1
View File
@@ -12,4 +12,4 @@ fun foo() {
.bar() .bar()
} }
// 2 3 1 7 8 9 8 9 11 12 11 17 12 18 13 // 2 3 1 7 8 +9 +8 9 11 +12 +11 16 12 17 13
+1 -1
View File
@@ -3,4 +3,4 @@ fun foo() {
42 42
} }
// 2 3 2 4 // 2 +3 2 4
@@ -9,4 +9,4 @@ fun foo(i: Int = 1) {
inline fun bar(i: Int = 1) { inline fun bar(i: Int = 1) {
} }
// 2 3 14 15 4 7 6 10 9 16 // 2 3 13 14 +4 7 6 10 9 15
@@ -13,4 +13,4 @@ inline fun foo(f: () -> Unit) {
f() f()
} }
// 2 18 19 3 4 20 6 21 22 7 8 23 9 12 13 14 // 2 17 18 +3 4 19 +6 20 21 +7 8 22 +9 12 13 14
+1 -1
View File
@@ -12,4 +12,4 @@ fun foo() {
} }
} }
// 2 3 5 6 8 9 11 8 13 // 2 3 5 6 +8 9 11 8 13
+1 -1
View File
@@ -12,4 +12,4 @@ fun foo() {
} }
} }
// 2 3 4 5 6 8 9 10 11 8 13 // 2 3 4 5 6 +8 9 10 11 8 13
+1 -1
View File
@@ -16,4 +16,4 @@ fun foo() {
} }
} }
// 2 3 7 4 5 7 8 10 11 15 12 13 15 10 17 // 2 3 7 4 5 7 8 +10 11 15 12 13 15 10 17
+1 -1
View File
@@ -12,4 +12,4 @@ fun foo() {
} }
} }
// 2 3 5 6 8 9 11 8 13 // 2 3 5 6 +8 9 11 8 13
+1 -1
View File
@@ -12,4 +12,4 @@ fun foo(x: Int) {
} }
} }
// 2 3 4 5 6 8 9 10 11 8 13 // 2 +3 4 5 6 +8 +9 10 11 8 13
+1 -1
View File
@@ -12,4 +12,4 @@ fun foo(x: Int) {
} }
} }
// 2 3 4 5 6 8 9 10 11 8 13 // 2 3 4 5 6 +8 9 10 11 8 13
@@ -37,14 +37,10 @@ import org.jetbrains.org.objectweb.asm.*;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.*;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import static org.jetbrains.kotlin.codegen.CodegenTestUtil.generateFiles;
public abstract class AbstractLineNumberTest extends TestCaseWithTmpdir { public abstract class AbstractLineNumberTest extends TestCaseWithTmpdir {
private static final String LINE_NUMBER_FUN = "lineNumber"; private static final String LINE_NUMBER_FUN = "lineNumber";
private static final Pattern TEST_LINE_NUMBER_PATTERN = Pattern.compile("^.*test." + LINE_NUMBER_FUN + "\\(\\).*$"); private static final Pattern TEST_LINE_NUMBER_PATTERN = Pattern.compile("^.*test." + LINE_NUMBER_FUN + "\\(\\).*$");
@@ -101,14 +97,14 @@ public abstract class AbstractLineNumberTest extends TestCaseWithTmpdir {
try { try {
if (custom) { if (custom) {
List<Integer> actualLineNumbers = extractActualLineNumbersFromBytecode(classFileFactory, false); List<String> actualLineNumbers = extractActualLineNumbersFromBytecode(classFileFactory, false);
String text = psiFile.getText(); String text = psiFile.getText();
String newFileText = text.substring(0, text.indexOf("// ")) + getActualLineNumbersAsString(actualLineNumbers); String newFileText = text.substring(0, text.indexOf("// ")) + getActualLineNumbersAsString(actualLineNumbers);
KotlinTestUtils.assertEqualsToFile(new File(filename), newFileText); KotlinTestUtils.assertEqualsToFile(new File(filename), newFileText);
} }
else { else {
List<Integer> expectedLineNumbers = extractSelectedLineNumbersFromSource(psiFile); List<String> expectedLineNumbers = extractSelectedLineNumbersFromSource(psiFile);
List<Integer> actualLineNumbers = extractActualLineNumbersFromBytecode(classFileFactory, true); List<String> actualLineNumbers = extractActualLineNumbersFromBytecode(classFileFactory, true);
assertSameElements(actualLineNumbers, expectedLineNumbers); assertSameElements(actualLineNumbers, expectedLineNumbers);
} }
} catch (Throwable e) { } catch (Throwable e) {
@@ -118,22 +114,22 @@ public abstract class AbstractLineNumberTest extends TestCaseWithTmpdir {
} }
private static String getActualLineNumbersAsString(List<Integer> list) { private static String getActualLineNumbersAsString(List<String> lines) {
return CollectionsKt.joinToString(list, " ", "// ", "", -1, "...", new Function1<Integer, CharSequence>() { return CollectionsKt.joinToString(lines, " ", "// ", "", -1, "...", new Function1<String, CharSequence>() {
@Override @Override
public CharSequence invoke(Integer integer) { public CharSequence invoke(String lineNumber) {
return integer.toString(); return lineNumber;
} }
}); });
} }
@NotNull @NotNull
private static List<Integer> extractActualLineNumbersFromBytecode(@NotNull ClassFileFactory factory, boolean testFunInvoke) { private static List<String> extractActualLineNumbersFromBytecode(@NotNull ClassFileFactory factory, boolean testFunInvoke) {
List<Integer> actualLineNumbers = Lists.newArrayList(); List<String> actualLineNumbers = Lists.newArrayList();
for (OutputFile outputFile : ClassFileUtilsKt.getClassFiles(factory)) { for (OutputFile outputFile : ClassFileUtilsKt.getClassFiles(factory)) {
ClassReader cr = new ClassReader(outputFile.asByteArray()); ClassReader cr = new ClassReader(outputFile.asByteArray());
try { try {
List<Integer> lineNumbers = testFunInvoke ? readTestFunLineNumbers(cr) : readAllLineNumbers(cr); List<String> lineNumbers = testFunInvoke ? readTestFunLineNumbers(cr) : readAllLineNumbers(cr);
actualLineNumbers.addAll(lineNumbers); actualLineNumbers.addAll(lineNumbers);
} }
catch (Throwable e) { catch (Throwable e) {
@@ -154,15 +150,15 @@ public abstract class AbstractLineNumberTest extends TestCaseWithTmpdir {
} }
@NotNull @NotNull
private static List<Integer> extractSelectedLineNumbersFromSource(@NotNull KtFile file) { private static List<String> extractSelectedLineNumbersFromSource(@NotNull KtFile file) {
String fileContent = file.getText(); String fileContent = file.getText();
List<Integer> lineNumbers = Lists.newArrayList(); List<String> lineNumbers = Lists.newArrayList();
String[] lines = StringUtil.convertLineSeparators(fileContent).split("\n"); String[] lines = StringUtil.convertLineSeparators(fileContent).split("\n");
for (int i = 0; i < lines.length; i++) { for (int i = 0; i < lines.length; i++) {
Matcher matcher = TEST_LINE_NUMBER_PATTERN.matcher(lines[i]); Matcher matcher = TEST_LINE_NUMBER_PATTERN.matcher(lines[i]);
if (matcher.matches()) { if (matcher.matches()) {
lineNumbers.add(i + 1); lineNumbers.add(Integer.toString(i + 1));
} }
} }
@@ -170,9 +166,9 @@ public abstract class AbstractLineNumberTest extends TestCaseWithTmpdir {
} }
@NotNull @NotNull
private static List<Integer> readTestFunLineNumbers(@NotNull ClassReader cr) { private static List<String> readTestFunLineNumbers(@NotNull ClassReader cr) {
final List<Label> labels = Lists.newArrayList(); final List<Label> labels = Lists.newArrayList();
final Map<Label, Integer> labels2LineNumbers = Maps.newHashMap(); final Map<Label, String> labels2LineNumbers = Maps.newHashMap();
ClassVisitor visitor = new ClassVisitor(Opcodes.ASM5) { ClassVisitor visitor = new ClassVisitor(Opcodes.ASM5) {
@Override @Override
@@ -196,7 +192,7 @@ public abstract class AbstractLineNumberTest extends TestCaseWithTmpdir {
@Override @Override
public void visitLineNumber(int line, @NotNull Label start) { public void visitLineNumber(int line, @NotNull Label start) {
labels2LineNumbers.put(start, line); labels2LineNumbers.put(start, Integer.toString(line));
} }
}; };
} }
@@ -204,9 +200,9 @@ public abstract class AbstractLineNumberTest extends TestCaseWithTmpdir {
cr.accept(visitor, ClassReader.SKIP_FRAMES); cr.accept(visitor, ClassReader.SKIP_FRAMES);
List<Integer> lineNumbers = Lists.newArrayList(); List<String> lineNumbers = Lists.newArrayList();
for (Label label : labels) { for (Label label : labels) {
Integer lineNumber = labels2LineNumbers.get(label); String lineNumber = labels2LineNumbers.get(label);
assert lineNumber != null : "No line number found for a label"; assert lineNumber != null : "No line number found for a label";
lineNumbers.add(lineNumber); lineNumbers.add(lineNumber);
} }
@@ -215,15 +211,19 @@ public abstract class AbstractLineNumberTest extends TestCaseWithTmpdir {
} }
@NotNull @NotNull
private static List<Integer> readAllLineNumbers(@NotNull ClassReader reader) { private static List<String> readAllLineNumbers(@NotNull ClassReader reader) {
final List<Integer> result = new ArrayList<Integer>(); final List<String> result = new ArrayList<String>();
final Set<String> visitedLabels = new HashSet<String>();
reader.accept(new ClassVisitor(Opcodes.ASM5) { reader.accept(new ClassVisitor(Opcodes.ASM5) {
@Override @Override
public MethodVisitor visitMethod(int access, @NotNull String name, @NotNull String desc, String signature, String[] exceptions) { public MethodVisitor visitMethod(int access, @NotNull String name, @NotNull String desc, String signature, String[] exceptions) {
return new MethodVisitor(Opcodes.ASM5) { return new MethodVisitor(Opcodes.ASM5) {
@Override @Override
public void visitLineNumber(int line, @NotNull Label label) { public void visitLineNumber(int line, @NotNull Label label) {
result.add(line); boolean overrides = !visitedLabels.add(label.toString());
result.add((overrides ? "+" : "") + line);
} }
}; };
} }