From 5e34ae5e0d745587bb1fc86e31d18bb35d51516b Mon Sep 17 00:00:00 2001 From: Sergej Jaskiewicz Date: Tue, 2 Aug 2022 14:50:00 +0200 Subject: [PATCH] [IR] Print annotations in source range compiler tests --- .../testData/ir/sourceRanges/annotations.kt | 5 +++ .../testData/ir/sourceRanges/annotations.txt | 36 +++++++++++++++++++ .../sourceRanges/declarations/classFuns.txt | 4 +++ .../declarations/classProperties.txt | 12 +++++++ .../ir/sourceRanges/declarations/classes.txt | 6 ++++ .../declarations/primaryConstructors.txt | 4 +++ .../declarations/secondaryConstructors.txt | 4 +++ .../declarations/topLevelFuns.txt | 4 +++ .../declarations/topLevelProperties.txt | 12 +++++++ .../ir/AbstractIrSourceRangesTestCase.kt | 18 +++++++++- .../ir/IrSourceRangesTestCaseGenerated.java | 5 +++ 11 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 compiler/testData/ir/sourceRanges/annotations.kt create mode 100644 compiler/testData/ir/sourceRanges/annotations.txt diff --git a/compiler/testData/ir/sourceRanges/annotations.kt b/compiler/testData/ir/sourceRanges/annotations.kt new file mode 100644 index 00000000000..87f18d3b509 --- /dev/null +++ b/compiler/testData/ir/sourceRanges/annotations.kt @@ -0,0 +1,5 @@ +@Target(AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.PROPERTY_SETTER) +internal annotation class MyAnnotation(val description: String) + +@MyAnnotation("fooAnotation") +fun foo() {} diff --git a/compiler/testData/ir/sourceRanges/annotations.txt b/compiler/testData/ir/sourceRanges/annotations.txt new file mode 100644 index 00000000000..87ced389ce8 --- /dev/null +++ b/compiler/testData/ir/sourceRanges/annotations.txt @@ -0,0 +1,36 @@ +@0:0..5:0 FILE fqName: fileName:/annotations.kt + @0:0..1:63 CLASS ANNOTATION_CLASS name:MyAnnotation modality:OPEN visibility:internal superTypes:[kotlin.Annotation] + annotations: + @0:0..102 CONSTRUCTOR_CALL 'public constructor (vararg allowedTargets: kotlin.annotation.AnnotationTarget) [primary] declared in kotlin.annotation.Target' type=kotlin.annotation.Target origin=null + @0:8..33 VARARG type=kotlin.Array varargElementType=kotlin.annotation.AnnotationTarget + @0:8..33 GET_ENUM 'ENUM_ENTRY IR_EXTERNAL_DECLARATION_STUB name:FUNCTION' type=kotlin.annotation.AnnotationTarget + @0:8..33 GET_ENUM 'ENUM_ENTRY IR_EXTERNAL_DECLARATION_STUB name:PROPERTY_GETTER' type=kotlin.annotation.AnnotationTarget + @0:8..33 GET_ENUM 'ENUM_ENTRY IR_EXTERNAL_DECLARATION_STUB name:PROPERTY_SETTER' type=kotlin.annotation.AnnotationTarget + @1:20..63 CONSTRUCTOR visibility:public <> (description:kotlin.String) returnType:.MyAnnotation [primary] + @1:39..62 VALUE_PARAMETER name:description index:0 type:kotlin.String + @0:0..1:63 BLOCK_BODY + @0:0..1:63 DELEGATING_CONSTRUCTOR_CALL 'public constructor () [primary] declared in kotlin.Any' + @0:0..1:63 INSTANCE_INITIALIZER_CALL classDescriptor='CLASS ANNOTATION_CLASS name:MyAnnotation modality:OPEN visibility:internal superTypes:[kotlin.Annotation]' + @1:39..62 PROPERTY name:description visibility:public modality:FINAL [val] + @1:39..62 FIELD PROPERTY_BACKING_FIELD name:description type:kotlin.String visibility:private [final] + @1:39..62 EXPRESSION_BODY + @1:39..62 GET_VAR 'description: kotlin.String declared in .MyAnnotation.' type=kotlin.String origin=INITIALIZE_PROPERTY_FROM_PARAMETER + @1:39..62 FUN DEFAULT_PROPERTY_ACCESSOR name: visibility:public modality:FINAL <> ($this:.MyAnnotation) returnType:kotlin.String + @1:39..62 VALUE_PARAMETER name: type:.MyAnnotation + @1:39..62 BLOCK_BODY + @1:39..62 RETURN type=kotlin.Nothing from='public final fun (): kotlin.String declared in .MyAnnotation' + @1:39..62 GET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:description type:kotlin.String visibility:private [final]' type=kotlin.String origin=null + @1:39..62 GET_VAR ': .MyAnnotation declared in .MyAnnotation.' type=.MyAnnotation origin=null + @1:20..63 FUN FAKE_OVERRIDE name:equals visibility:public modality:OPEN <> ($this:kotlin.Any, other:kotlin.Any?) returnType:kotlin.Boolean [fake_override,operator] + @0:0..1:63 VALUE_PARAMETER name: type:kotlin.Any + @1:20..63 VALUE_PARAMETER name:other index:0 type:kotlin.Any? + @1:20..63 FUN FAKE_OVERRIDE name:hashCode visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.Int [fake_override] + @0:0..1:63 VALUE_PARAMETER name: type:kotlin.Any + @1:20..63 FUN FAKE_OVERRIDE name:toString visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.String [fake_override] + @0:0..1:63 VALUE_PARAMETER name: type:kotlin.Any + @0:0..1:63 VALUE_PARAMETER INSTANCE_RECEIVER name: type:.MyAnnotation + @4:0..12 FUN name:foo visibility:public modality:FINAL <> () returnType:kotlin.Unit + annotations: + @3:0..29 CONSTRUCTOR_CALL 'public constructor (description: kotlin.String) [primary] declared in .MyAnnotation' type=.MyAnnotation origin=null + @3:14..28 CONST String type=kotlin.String value="fooAnotation" + @4:10..12 BLOCK_BODY diff --git a/compiler/testData/ir/sourceRanges/declarations/classFuns.txt b/compiler/testData/ir/sourceRanges/declarations/classFuns.txt index f082df05a5e..ba0bee21335 100644 --- a/compiler/testData/ir/sourceRanges/declarations/classFuns.txt +++ b/compiler/testData/ir/sourceRanges/declarations/classFuns.txt @@ -11,6 +11,10 @@ @11:4..18 VALUE_PARAMETER name: type:test.Test @11:16..18 BLOCK_BODY @17:4..18 FUN name:test2 visibility:public modality:FINAL <> ($this:test.Test) returnType:kotlin.Unit + annotations: + @14:4..16:5 CONSTRUCTOR_CALL 'public constructor (vararg names: kotlin.String) [primary] declared in kotlin.Suppress' type=kotlin.Suppress origin=null + @15:8..25 VARARG type=kotlin.Array varargElementType=kotlin.String + @15:8..25 CONST String type=kotlin.String value="UNUSED_VARIABLE" @14:4..17:18 VALUE_PARAMETER name: type:test.Test @17:16..18 BLOCK_BODY @21:4..18 FUN name:test3 visibility:private modality:FINAL <> ($this:test.Test) returnType:kotlin.Unit diff --git a/compiler/testData/ir/sourceRanges/declarations/classProperties.txt b/compiler/testData/ir/sourceRanges/declarations/classProperties.txt index 85f4c4f1917..9627df66c5a 100644 --- a/compiler/testData/ir/sourceRanges/declarations/classProperties.txt +++ b/compiler/testData/ir/sourceRanges/declarations/classProperties.txt @@ -25,6 +25,10 @@ @10:4..18 GET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:test1 type:kotlin.Int visibility:private [final]' type=kotlin.Int origin=null @10:4..18 GET_VAR ': test.Test declared in test.Test.' type=test.Test origin=null @13:4..16:18 PROPERTY name:test2 visibility:public modality:FINAL [val] + annotations: + @13:4..15:5 CONSTRUCTOR_CALL 'public constructor (vararg names: kotlin.String) [primary] declared in kotlin.Suppress' type=kotlin.Suppress origin=null + @14:8..25 VARARG type=kotlin.Array varargElementType=kotlin.String + @14:8..25 CONST String type=kotlin.String value="UNUSED_VARIABLE" @13:4..16:18 FIELD PROPERTY_BACKING_FIELD name:test2 type:kotlin.Int visibility:private [final] @16:16..18 EXPRESSION_BODY @16:16..18 CONST Int type=kotlin.Int value=42 @@ -64,6 +68,10 @@ @34:16..18 CONST Int type=kotlin.Int value=42 @37:4..41:18 PROPERTY name:test7 visibility:public modality:FINAL [val] @41:8..18 FUN name: visibility:public modality:FINAL <> ($this:test.Test) returnType:kotlin.Int + annotations: + @38:8..40:9 CONSTRUCTOR_CALL 'public constructor (vararg names: kotlin.String) [primary] declared in kotlin.Suppress' type=kotlin.Suppress origin=null + @39:12..29 VARARG type=kotlin.Array varargElementType=kotlin.String + @39:12..29 CONST String type=kotlin.String value="UNUSED_VARIABLE" @38:8..41:18 VALUE_PARAMETER name: type:test.Test @41:16..18 BLOCK_BODY @41:18..18 RETURN type=kotlin.Nothing from='public final fun (): kotlin.Int declared in test.Test' @@ -164,6 +172,10 @@ @69:4..75:9 GET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:test13 type:kotlin.Int visibility:private' type=kotlin.Int origin=null @69:4..75:9 GET_VAR ': test.Test declared in test.Test.' type=test.Test origin=null @73:8..75:9 FUN name: visibility:public modality:FINAL <> ($this:test.Test, value:kotlin.Int) returnType:kotlin.Unit + annotations: + @70:8..72:9 CONSTRUCTOR_CALL 'public constructor (vararg names: kotlin.String) [primary] declared in kotlin.Suppress' type=kotlin.Suppress origin=null + @71:12..29 VARARG type=kotlin.Array varargElementType=kotlin.String + @71:12..29 CONST String type=kotlin.String value="UNUSED_VARIABLE" @70:8..75:9 VALUE_PARAMETER name: type:test.Test @73:12..17 VALUE_PARAMETER name:value index:0 type:kotlin.Int @73:19..75:9 BLOCK_BODY diff --git a/compiler/testData/ir/sourceRanges/declarations/classes.txt b/compiler/testData/ir/sourceRanges/declarations/classes.txt index d171f858b7d..3657d4d39a8 100644 --- a/compiler/testData/ir/sourceRanges/declarations/classes.txt +++ b/compiler/testData/ir/sourceRanges/declarations/classes.txt @@ -39,6 +39,10 @@ @10:0..11 VALUE_PARAMETER name: type:kotlin.Any @10:0..11 VALUE_PARAMETER INSTANCE_RECEIVER name: type:.Test3 @13:0..14:11 CLASS CLASS name:Test4 modality:FINAL visibility:public superTypes:[kotlin.Any] + annotations: + @13:0..23 CONSTRUCTOR_CALL 'public constructor (vararg names: kotlin.String) [primary] declared in kotlin.Suppress' type=kotlin.Suppress origin=null + @13:10..22 VARARG type=kotlin.Array varargElementType=kotlin.String + @13:10..22 CONST String type=kotlin.String value="UNUSED_VAR" @14:0..11 CONSTRUCTOR visibility:public <> () returnType:.Test4 [primary] @13:0..14:11 BLOCK_BODY @13:0..14:11 DELEGATING_CONSTRUCTOR_CALL 'public constructor () [primary] declared in kotlin.Any' @@ -57,6 +61,8 @@ @17:0..18:11 ENUM_CONSTRUCTOR_CALL 'public constructor (name: kotlin.String, ordinal: kotlin.Int) [primary] declared in kotlin.Enum' @17:0..18:11 INSTANCE_INITIALIZER_CALL classDescriptor='CLASS ENUM_CLASS name:Test5 modality:FINAL visibility:public superTypes:[kotlin.Enum<.Test5>]' @17:0..18:11 PROPERTY FAKE_OVERRIDE name:name visibility:public modality:FINAL [fake_override,val] + annotations: + @-1:-1..-1 CONSTRUCTOR_CALL 'public constructor () [primary] declared in kotlin.internal.IntrinsicConstEvaluation' type=kotlin.internal.IntrinsicConstEvaluation origin=null @18:0..11 FUN FAKE_OVERRIDE name: visibility:public modality:FINAL <> ($this:kotlin.Enum<.Test5>) returnType:kotlin.String [fake_override] @17:0..18:11 VALUE_PARAMETER name: type:kotlin.Enum<.Test5> @17:0..18:11 PROPERTY FAKE_OVERRIDE name:ordinal visibility:public modality:FINAL [fake_override,val] diff --git a/compiler/testData/ir/sourceRanges/declarations/primaryConstructors.txt b/compiler/testData/ir/sourceRanges/declarations/primaryConstructors.txt index c4b4358d24b..d8caf9c3c1b 100644 --- a/compiler/testData/ir/sourceRanges/declarations/primaryConstructors.txt +++ b/compiler/testData/ir/sourceRanges/declarations/primaryConstructors.txt @@ -73,6 +73,10 @@ @5:0..9:23 VALUE_PARAMETER INSTANCE_RECEIVER name: type:.Test3 @11:0..13:23 CLASS CLASS name:Test4 modality:FINAL visibility:public superTypes:[kotlin.Any] @13:0..23 CONSTRUCTOR visibility:public <> (x:kotlin.Int) returnType:.Test4 [primary] + annotations: + @12:0..28 CONSTRUCTOR_CALL 'public constructor (vararg names: kotlin.String) [primary] declared in kotlin.Suppress' type=kotlin.Suppress origin=null + @12:10..27 VARARG type=kotlin.Array varargElementType=kotlin.String + @12:10..27 CONST String type=kotlin.String value="UNUSED_VARIABLE" @13:12..22 VALUE_PARAMETER name:x index:0 type:kotlin.Int @11:0..13:23 BLOCK_BODY @11:0..13:23 DELEGATING_CONSTRUCTOR_CALL 'public constructor () [primary] declared in kotlin.Any' diff --git a/compiler/testData/ir/sourceRanges/declarations/secondaryConstructors.txt b/compiler/testData/ir/sourceRanges/declarations/secondaryConstructors.txt index b96466e780f..2c4daad71e3 100644 --- a/compiler/testData/ir/sourceRanges/declarations/secondaryConstructors.txt +++ b/compiler/testData/ir/sourceRanges/declarations/secondaryConstructors.txt @@ -15,6 +15,10 @@ @9:29..36 DELEGATING_CONSTRUCTOR_CALL 'public constructor () [primary] declared in kotlin.Any' @9:4..36 INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:C modality:FINAL visibility:public superTypes:[kotlin.Any]' @12:4..32 CONSTRUCTOR visibility:public <> (x:kotlin.Any) returnType:.C + annotations: + @11:4..32 CONSTRUCTOR_CALL 'public constructor (vararg names: kotlin.String) [primary] declared in kotlin.Suppress' type=kotlin.Suppress origin=null + @11:14..31 VARARG type=kotlin.Array varargElementType=kotlin.String + @11:14..31 CONST String type=kotlin.String value="UNUSED_VARIABLE" @12:16..22 VALUE_PARAMETER name:x index:0 type:kotlin.Any @11:4..12:32 BLOCK_BODY @12:25..32 DELEGATING_CONSTRUCTOR_CALL 'public constructor () [primary] declared in kotlin.Any' diff --git a/compiler/testData/ir/sourceRanges/declarations/topLevelFuns.txt b/compiler/testData/ir/sourceRanges/declarations/topLevelFuns.txt index 2820ab3fb7f..86242203c1a 100644 --- a/compiler/testData/ir/sourceRanges/declarations/topLevelFuns.txt +++ b/compiler/testData/ir/sourceRanges/declarations/topLevelFuns.txt @@ -4,6 +4,10 @@ @9:0..14 FUN name:test1 visibility:public modality:FINAL <> () returnType:kotlin.Unit @9:12..14 BLOCK_BODY @15:0..14 FUN name:test2 visibility:public modality:FINAL <> () returnType:kotlin.Unit + annotations: + @12:0..14:1 CONSTRUCTOR_CALL 'public constructor (vararg names: kotlin.String) [primary] declared in kotlin.Suppress' type=kotlin.Suppress origin=null + @13:4..21 VARARG type=kotlin.Array varargElementType=kotlin.String + @13:4..21 CONST String type=kotlin.String value="UNUSED_VARIABLE" @15:12..14 BLOCK_BODY @19:0..14 FUN name:test3 visibility:private modality:FINAL <> () returnType:kotlin.Unit @19:12..14 BLOCK_BODY diff --git a/compiler/testData/ir/sourceRanges/declarations/topLevelProperties.txt b/compiler/testData/ir/sourceRanges/declarations/topLevelProperties.txt index 010b0d779cc..3babcdb4c78 100644 --- a/compiler/testData/ir/sourceRanges/declarations/topLevelProperties.txt +++ b/compiler/testData/ir/sourceRanges/declarations/topLevelProperties.txt @@ -16,6 +16,10 @@ @9:0..14 RETURN type=kotlin.Nothing from='public final fun (): kotlin.Int declared in test' @9:0..14 GET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:test1 type:kotlin.Int visibility:private [final,static]' type=kotlin.Int origin=null @12:0..15:14 PROPERTY name:test2 visibility:public modality:FINAL [val] + annotations: + @12:0..14:1 CONSTRUCTOR_CALL 'public constructor (vararg names: kotlin.String) [primary] declared in kotlin.Suppress' type=kotlin.Suppress origin=null + @13:4..21 VARARG type=kotlin.Array varargElementType=kotlin.String + @13:4..21 CONST String type=kotlin.String value="UNUSED_VARIABLE" @12:0..15:14 FIELD PROPERTY_BACKING_FIELD name:test2 type:kotlin.Int visibility:private [final,static] @15:12..14 EXPRESSION_BODY @15:12..14 CONST Int type=kotlin.Int value=42 @@ -48,6 +52,10 @@ @33:12..14 CONST Int type=kotlin.Int value=42 @36:0..40:14 PROPERTY name:test7 visibility:public modality:FINAL [val] @40:4..14 FUN name: visibility:public modality:FINAL <> () returnType:kotlin.Int + annotations: + @37:4..39:5 CONSTRUCTOR_CALL 'public constructor (vararg names: kotlin.String) [primary] declared in kotlin.Suppress' type=kotlin.Suppress origin=null + @38:8..25 VARARG type=kotlin.Array varargElementType=kotlin.String + @38:8..25 CONST String type=kotlin.String value="UNUSED_VARIABLE" @40:12..14 BLOCK_BODY @40:14..14 RETURN type=kotlin.Nothing from='public final fun (): kotlin.Int declared in test' @40:12..14 CONST Int type=kotlin.Int value=42 @@ -125,6 +133,10 @@ @64:0..68:32 RETURN type=kotlin.Nothing from='public final fun (): kotlin.Int declared in test' @64:0..68:32 GET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:test13 type:kotlin.Int visibility:private [static]' type=kotlin.Int origin=null @68:4..32 FUN name: visibility:public modality:FINAL <> (value:kotlin.Int) returnType:kotlin.Unit + annotations: + @65:4..67:5 CONSTRUCTOR_CALL 'public constructor (vararg names: kotlin.String) [primary] declared in kotlin.Suppress' type=kotlin.Suppress origin=null + @66:8..25 VARARG type=kotlin.Array varargElementType=kotlin.String + @66:8..25 CONST String type=kotlin.String value="UNUSED_VARIABLE" @68:8..13 VALUE_PARAMETER name:value index:0 type:kotlin.Int @68:15..32 BLOCK_BODY @68:17..22 SET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:test13 type:kotlin.Int visibility:private [static]' type=kotlin.Unit origin=EQ diff --git a/compiler/tests-common/tests/org/jetbrains/kotlin/ir/AbstractIrSourceRangesTestCase.kt b/compiler/tests-common/tests/org/jetbrains/kotlin/ir/AbstractIrSourceRangesTestCase.kt index ea8a3247ec1..f24f032275e 100644 --- a/compiler/tests-common/tests/org/jetbrains/kotlin/ir/AbstractIrSourceRangesTestCase.kt +++ b/compiler/tests-common/tests/org/jetbrains/kotlin/ir/AbstractIrSourceRangesTestCase.kt @@ -16,6 +16,7 @@ package org.jetbrains.kotlin.ir +import org.jetbrains.kotlin.ir.declarations.IrAnnotationContainer import org.jetbrains.kotlin.ir.util.RenderIrElementVisitor import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid import org.jetbrains.kotlin.ir.visitors.acceptChildrenVoid @@ -47,10 +48,25 @@ abstract class AbstractIrSourceRangesTestCase : AbstractIrGeneratorTestCase() { val printer = Printer(out, " ") val elementRenderer = RenderIrElementVisitor() - override fun visitElement(element: IrElement) { + private fun printElement(element: IrElement) { val sourceRangeInfo = fileEntry.getSourceRangeInfo(element.startOffset, element.endOffset) printer.println("@${sourceRangeInfo.render()} ${element.accept(elementRenderer, null)}") + } + + override fun visitElement(element: IrElement) { + printElement(element) printer.pushIndent() + if (element is IrAnnotationContainer && element.annotations.isNotEmpty()) { + printer.println("annotations:") + printer.pushIndent() + for (annotation in element.annotations) { + printElement(annotation) + printer.pushIndent() + annotation.acceptChildrenVoid(this) + printer.popIndent() + } + printer.popIndent() + } element.acceptChildrenVoid(this) printer.popIndent() } diff --git a/compiler/tests-gen/org/jetbrains/kotlin/ir/IrSourceRangesTestCaseGenerated.java b/compiler/tests-gen/org/jetbrains/kotlin/ir/IrSourceRangesTestCaseGenerated.java index 4ddbfae78d2..f71bcf39229 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/ir/IrSourceRangesTestCaseGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/ir/IrSourceRangesTestCaseGenerated.java @@ -29,6 +29,11 @@ public class IrSourceRangesTestCaseGenerated extends AbstractIrSourceRangesTestC KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/ir/sourceRanges"), Pattern.compile("^(.+)\\.kt$"), null, true); } + @TestMetadata("annotations.kt") + public void testAnnotations() throws Exception { + runTest("compiler/testData/ir/sourceRanges/annotations.kt"); + } + @TestMetadata("augmentedAssignmentWithExpression.kt") public void testAugmentedAssignmentWithExpression() throws Exception { runTest("compiler/testData/ir/sourceRanges/augmentedAssignmentWithExpression.kt");