Skip to content

Commit bf7437f

Browse files
authored
Merge pull request #10224 from tamasvajk/kotlin-comment-fixes
Kotlin: Fix issues in comment extraction
2 parents b5f9fbe + 0cbb73a commit bf7437f

File tree

4 files changed

+49
-27
lines changed

4 files changed

+49
-27
lines changed

java/kotlin-extractor/src/main/kotlin/KotlinFileExtractor.kt

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -159,31 +159,6 @@ open class KotlinFileExtractor(
159159
}
160160
}
161161

162-
163-
164-
fun getLabel(element: IrElement) : String? {
165-
when (element) {
166-
is IrClass -> return getClassLabel(element, listOf()).classLabel
167-
is IrTypeParameter -> return getTypeParameterLabel(element)
168-
is IrFunction -> return getFunctionLabel(element, null)
169-
is IrValueParameter -> return getValueParameterLabel(element, null)
170-
is IrProperty -> return getPropertyLabel(element)
171-
is IrField -> return getFieldLabel(element)
172-
is IrEnumEntry -> return getEnumEntryLabel(element)
173-
is IrTypeAlias -> return getTypeAliasLabel(element)
174-
175-
// Fresh entities:
176-
is IrBody -> return null
177-
is IrExpression -> return null
178-
179-
// todo add others:
180-
else -> {
181-
logger.errorElement("Unhandled element type: ${element::class}", element)
182-
return null
183-
}
184-
}
185-
}
186-
187162
private fun extractTypeParameter(tp: IrTypeParameter, apparentIndex: Int, javaTypeParameter: JavaTypeParameter?): Label<out DbTypevariable>? {
188163
with("type parameter", tp) {
189164
val parentId = getTypeParameterParentLabel(tp) ?: return null

java/kotlin-extractor/src/main/kotlin/comments/CommentExtractor.kt

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import com.intellij.psi.PsiComment
77
import com.intellij.psi.PsiElement
88
import org.jetbrains.kotlin.ir.IrElement
99
import org.jetbrains.kotlin.ir.declarations.*
10+
import org.jetbrains.kotlin.ir.expressions.IrBody
11+
import org.jetbrains.kotlin.ir.expressions.IrExpression
12+
import org.jetbrains.kotlin.ir.util.parentClassOrNull
1013
import org.jetbrains.kotlin.kdoc.psi.api.KDoc
1114
import org.jetbrains.kotlin.lexer.KtTokens
1215
import org.jetbrains.kotlin.psi.KtVisitor
@@ -102,7 +105,7 @@ class CommentExtractor(private val fileExtractor: KotlinFileExtractor, private v
102105
label = "variable ${ownerIr.name.asString()}"
103106
tw.getExistingVariableLabelFor(ownerIr)
104107
} else {
105-
label = fileExtractor.getLabel(ownerIr) ?: continue
108+
label = getLabel(ownerIr) ?: continue
106109
tw.getExistingLabelFor<DbTop>(label)
107110
}
108111
if (existingLabel == null) {
@@ -118,9 +121,42 @@ class CommentExtractor(private val fileExtractor: KotlinFileExtractor, private v
118121
private fun getKDocOwner(comment: KDoc) : PsiElement? {
119122
val owner = comment.owner
120123
if (owner == null) {
121-
logger.warn("Couldn't get owner of KDoc.")
124+
logger.warn("Couldn't get owner of KDoc. The comment is extracted without an owner.")
122125
}
123126
return owner
124127
}
128+
129+
private fun getLabel(element: IrElement) : String? {
130+
when (element) {
131+
is IrClass -> return fileExtractor.getClassLabel(element, listOf()).classLabel
132+
is IrTypeParameter -> return fileExtractor.getTypeParameterLabel(element)
133+
is IrFunction -> return fileExtractor.getFunctionLabel(element, null)
134+
is IrValueParameter -> return fileExtractor.getValueParameterLabel(element, null)
135+
is IrProperty -> return fileExtractor.getPropertyLabel(element)
136+
is IrField -> return fileExtractor.getFieldLabel(element)
137+
is IrEnumEntry -> return fileExtractor.getEnumEntryLabel(element)
138+
is IrTypeAlias -> return fileExtractor.getTypeAliasLabel(element)
139+
140+
is IrAnonymousInitializer -> {
141+
val parentClass = element.parentClassOrNull
142+
if (parentClass == null) {
143+
logger.warnElement("Parent of anonymous initializer is not a class", element)
144+
return null
145+
}
146+
// Assign the comment to the class. The content of the `init` blocks might be extracted in multiple constructors.
147+
return getLabel(parentClass)
148+
}
149+
150+
// Fresh entities:
151+
is IrBody -> return null
152+
is IrExpression -> return null
153+
154+
// todo add others:
155+
else -> {
156+
logger.warnElement("Unhandled element type found during comment extraction: ${element::class}", element)
157+
return null
158+
}
159+
}
125160
}
161+
}
126162
}

java/ql/test/kotlin/library-tests/comments/comments.expected

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ comments
99
| comments.kt:37:5:37:23 | /** This is high */ | /** This is high */ |
1010
| comments.kt:42:5:44:6 | /**\n * A variable.\n */ | /**\n * A variable.\n */ |
1111
| comments.kt:48:1:50:3 | /**\n * A type alias comment\n */ | /**\n * A type alias comment\n */ |
12+
| comments.kt:54:5:56:7 | /**\n * An init block comment\n */ | /**\n * An init block comment\n */ |
1213
commentOwners
1314
| comments.kt:4:1:11:3 | /**\n * A group of *members*.\n *\n * This class has no useful logic; it's just a documentation example.\n *\n * @property name the name of this group.\n * @constructor Creates an empty group.\n */ | comments.kt:12:1:31:1 | Group |
1415
| comments.kt:4:1:11:3 | /**\n * A group of *members*.\n *\n * This class has no useful logic; it's just a documentation example.\n *\n * @property name the name of this group.\n * @constructor Creates an empty group.\n */ | comments.kt:12:1:31:1 | Group |
@@ -20,6 +21,7 @@ commentOwners
2021
| comments.kt:37:5:37:23 | /** This is high */ | comments.kt:38:5:38:11 | High |
2122
| comments.kt:42:5:44:6 | /**\n * A variable.\n */ | comments.kt:45:5:45:13 | int a |
2223
| comments.kt:48:1:50:3 | /**\n * A type alias comment\n */ | comments.kt:51:1:51:24 | MyType |
24+
| comments.kt:54:5:56:7 | /**\n * An init block comment\n */ | comments.kt:53:1:58:1 | InitBlock |
2325
commentSections
2426
| comments.kt:1:1:1:25 | /** Kdoc with no owner */ | Kdoc with no owner |
2527
| comments.kt:4:1:11:3 | /**\n * A group of *members*.\n *\n * This class has no useful logic; it's just a documentation example.\n *\n * @property name the name of this group.\n * @constructor Creates an empty group.\n */ | A group of *members*.\n\nThis class has no useful logic; it's just a documentation example.\n\n |
@@ -31,11 +33,13 @@ commentSections
3133
| comments.kt:37:5:37:23 | /** This is high */ | This is high |
3234
| comments.kt:42:5:44:6 | /**\n * A variable.\n */ | A variable. |
3335
| comments.kt:48:1:50:3 | /**\n * A type alias comment\n */ | A type alias comment |
36+
| comments.kt:54:5:56:7 | /**\n * An init block comment\n */ | An init block comment |
3437
commentSectionContents
3538
| A group of *members*.\n\nThis class has no useful logic; it's just a documentation example.\n\n | A group of *members*.\n\nThis class has no useful logic; it's just a documentation example.\n\n |
3639
| A type alias comment | A type alias comment |
3740
| A variable. | A variable. |
3841
| Adds a [member] to this group.\n | Adds a [member] to this group.\n |
42+
| An init block comment | An init block comment |
3943
| Creates an empty group. | Creates an empty group. |
4044
| Kdoc with no owner | Kdoc with no owner |
4145
| Medium is in the middle | Medium is in the middle |

java/ql/test/kotlin/library-tests/comments/comments.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,10 @@ fun fn1() {
4949
* A type alias comment
5050
*/
5151
typealias MyType = Group
52+
53+
class InitBlock {
54+
/**
55+
* An init block comment
56+
*/
57+
init { }
58+
}

0 commit comments

Comments
 (0)