mirror of
https://github.com/github/codeql.git
synced 2025-12-22 19:56:32 +01:00
Kotlin: minor refactoring in comment extraction
This commit is contained in:
@@ -7,6 +7,9 @@ import com.intellij.psi.PsiComment
|
||||
import com.intellij.psi.PsiElement
|
||||
import org.jetbrains.kotlin.ir.IrElement
|
||||
import org.jetbrains.kotlin.ir.declarations.*
|
||||
import org.jetbrains.kotlin.ir.expressions.IrBody
|
||||
import org.jetbrains.kotlin.ir.expressions.IrExpression
|
||||
import org.jetbrains.kotlin.ir.util.parentClassOrNull
|
||||
import org.jetbrains.kotlin.kdoc.psi.api.KDoc
|
||||
import org.jetbrains.kotlin.lexer.KtTokens
|
||||
import org.jetbrains.kotlin.psi.KtVisitor
|
||||
@@ -102,7 +105,7 @@ class CommentExtractor(private val fileExtractor: KotlinFileExtractor, private v
|
||||
label = "variable ${ownerIr.name.asString()}"
|
||||
tw.getExistingVariableLabelFor(ownerIr)
|
||||
} else {
|
||||
label = fileExtractor.getLabel(ownerIr) ?: continue
|
||||
label = getLabel(ownerIr) ?: continue
|
||||
tw.getExistingLabelFor<DbTop>(label)
|
||||
}
|
||||
if (existingLabel == null) {
|
||||
@@ -118,9 +121,42 @@ class CommentExtractor(private val fileExtractor: KotlinFileExtractor, private v
|
||||
private fun getKDocOwner(comment: KDoc) : PsiElement? {
|
||||
val owner = comment.owner
|
||||
if (owner == null) {
|
||||
logger.warn("Couldn't get owner of KDoc.")
|
||||
logger.warn("Couldn't get owner of KDoc. The comment is extracted without an owner.")
|
||||
}
|
||||
return owner
|
||||
}
|
||||
|
||||
private fun getLabel(element: IrElement) : String? {
|
||||
when (element) {
|
||||
is IrClass -> return fileExtractor.getClassLabel(element, listOf()).classLabel
|
||||
is IrTypeParameter -> return fileExtractor.getTypeParameterLabel(element)
|
||||
is IrFunction -> return fileExtractor.getFunctionLabel(element, null)
|
||||
is IrValueParameter -> return fileExtractor.getValueParameterLabel(element, null)
|
||||
is IrProperty -> return fileExtractor.getPropertyLabel(element)
|
||||
is IrField -> return fileExtractor.getFieldLabel(element)
|
||||
is IrEnumEntry -> return fileExtractor.getEnumEntryLabel(element)
|
||||
is IrTypeAlias -> return fileExtractor.getTypeAliasLabel(element)
|
||||
|
||||
is IrAnonymousInitializer -> {
|
||||
val parentClass = element.parentClassOrNull
|
||||
if (parentClass == null) {
|
||||
logger.errorElement("Parent of anonymous initializer is not a class", element)
|
||||
return null
|
||||
}
|
||||
// Assign the comment to the class. The content of the `init` blocks might be extracted in multiple constructors.
|
||||
return fileExtractor.getClassLabel(parentClass, listOf()).classLabel
|
||||
}
|
||||
|
||||
// Fresh entities:
|
||||
is IrBody -> return null
|
||||
is IrExpression -> return null
|
||||
|
||||
// todo add others:
|
||||
else -> {
|
||||
logger.errorElement("Unhandled element type: ${element::class}", element)
|
||||
return null
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user