|
| 1 | +package com.github.codeql |
| 2 | + |
| 3 | +import com.github.codeql.utils.versions.getPsi2Ir |
| 4 | +import com.intellij.psi.PsiComment |
| 5 | +import com.intellij.psi.PsiElement |
| 6 | +import com.intellij.psi.PsiWhiteSpace |
| 7 | +import org.jetbrains.kotlin.config.KotlinCompilerVersion |
| 8 | +import org.jetbrains.kotlin.ir.IrElement |
| 9 | +import org.jetbrains.kotlin.ir.declarations.* |
| 10 | +import org.jetbrains.kotlin.kdoc.psi.api.KDocElement |
| 11 | +import org.jetbrains.kotlin.psi.KtCodeFragment |
| 12 | +import org.jetbrains.kotlin.psi.KtVisitor |
| 13 | + |
| 14 | +class LinesOfCodePSI( |
| 15 | + val logger: FileLogger, |
| 16 | + val tw: FileTrapWriter, |
| 17 | + val file: IrFile |
| 18 | +) { |
| 19 | + val psi2Ir = getPsi2Ir().also { |
| 20 | + if (it == null) { |
| 21 | + logger.warn("Lines of code will not be populated as Kotlin version is too old (${KotlinCompilerVersion.getVersion()})") |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + fun linesOfCodeInFile(id: Label<DbFile>): Boolean { |
| 26 | + if (psi2Ir == null) { |
| 27 | + return false |
| 28 | + } |
| 29 | + val ktFile = psi2Ir.getKtFile(file) |
| 30 | + if (ktFile == null) { |
| 31 | + return false |
| 32 | + } |
| 33 | + linesOfCodeInPsi(id, ktFile, file) |
| 34 | + // Even if linesOfCodeInPsi didn't manage to extract any |
| 35 | + // information, if we got as far as calling it then we have |
| 36 | + // PSI info for the file |
| 37 | + return true |
| 38 | + } |
| 39 | + |
| 40 | + fun linesOfCodeInDeclaration(d: IrDeclaration, id: Label<out DbSourceline>): Boolean { |
| 41 | + if (psi2Ir == null) { |
| 42 | + return false |
| 43 | + } |
| 44 | + val p = psi2Ir.findPsiElement(d, file) |
| 45 | + if (p == null) { |
| 46 | + return false |
| 47 | + } |
| 48 | + linesOfCodeInPsi(id, p, d) |
| 49 | + // Even if linesOfCodeInPsi didn't manage to extract any |
| 50 | + // information, if we got as far as calling it then we have |
| 51 | + // PSI info for the declaration |
| 52 | + return true |
| 53 | + } |
| 54 | + |
| 55 | + private fun linesOfCodeInPsi(id: Label<out DbSourceline>, root: PsiElement, e: IrElement) { |
| 56 | + val document = root.getContainingFile().getViewProvider().getDocument() |
| 57 | + if (document == null) { |
| 58 | + logger.errorElement("Cannot find document for PSI", e) |
| 59 | + tw.writeNumlines(id, 0, 0, 0) |
| 60 | + return |
| 61 | + } |
| 62 | + |
| 63 | + val rootRange = root.getTextRange() |
| 64 | + val rootStartOffset = rootRange.getStartOffset() |
| 65 | + val rootEndOffset = rootRange.getEndOffset() |
| 66 | + if (rootStartOffset < 0 || rootEndOffset < 0) { |
| 67 | + // This is synthetic, or has an invalid location |
| 68 | + tw.writeNumlines(id, 0, 0, 0) |
| 69 | + return |
| 70 | + } |
| 71 | + val rootFirstLine = document.getLineNumber(rootStartOffset) |
| 72 | + val rootLastLine = document.getLineNumber(rootEndOffset) |
| 73 | + if (rootLastLine < rootFirstLine) { |
| 74 | + logger.errorElement("PSI ends before it starts", e) |
| 75 | + tw.writeNumlines(id, 0, 0, 0) |
| 76 | + return |
| 77 | + } |
| 78 | + val numLines = 1 + rootLastLine - rootFirstLine |
| 79 | + val lineContents = Array(numLines) { LineContent() } |
| 80 | + |
| 81 | + val visitor = |
| 82 | + object : KtVisitor<Unit, Unit>() { |
| 83 | + override fun visitElement(element: PsiElement) { |
| 84 | + val isComment = element is PsiComment |
| 85 | + // Comments may include nodes that aren't PsiComments, |
| 86 | + // so we don't want to visit them or we'll think they |
| 87 | + // are code. |
| 88 | + if (!isComment) { |
| 89 | + element.acceptChildren(this) |
| 90 | + } |
| 91 | + |
| 92 | + if (element is PsiWhiteSpace) { |
| 93 | + return |
| 94 | + } |
| 95 | + // Leaf nodes are assumed to be tokens, and |
| 96 | + // therefore we count any lines that they are on. |
| 97 | + // For comments, we actually need to look at the |
| 98 | + // outermost node, as the leaves of KDocs don't |
| 99 | + // necessarily cover all lines. |
| 100 | + if (isComment || element.getChildren().size == 0) { |
| 101 | + val range = element.getTextRange() |
| 102 | + val startOffset = range.getStartOffset() |
| 103 | + val endOffset = range.getEndOffset() |
| 104 | + // The PSI doesn't seem to have anything like |
| 105 | + // the IR's UNDEFINED_OFFSET and SYNTHETIC_OFFSET, |
| 106 | + // but < 0 still seem to represent bad/unknown |
| 107 | + // locations. |
| 108 | + if (startOffset < 0 || endOffset < 0) { |
| 109 | + logger.errorElement("PSI element has negative offset", e) |
| 110 | + return |
| 111 | + } |
| 112 | + if (startOffset > endOffset) { |
| 113 | + logger.errorElement("PSI element has negative size", e) |
| 114 | + return |
| 115 | + } |
| 116 | + // We might get e.g. an import list for a file |
| 117 | + // with no imports, which claims to have start |
| 118 | + // and end offsets of 0. Anything of 0 width |
| 119 | + // we therefore just skip. |
| 120 | + if (startOffset == endOffset) { |
| 121 | + return |
| 122 | + } |
| 123 | + val firstLine = document.getLineNumber(startOffset) |
| 124 | + val lastLine = document.getLineNumber(endOffset) |
| 125 | + if (firstLine < rootFirstLine) { |
| 126 | + logger.errorElement("PSI element starts before root", e) |
| 127 | + return |
| 128 | + } else if (lastLine > rootLastLine) { |
| 129 | + logger.errorElement("PSI element ends after root", e) |
| 130 | + return |
| 131 | + } |
| 132 | + for (line in firstLine..lastLine) { |
| 133 | + val lineContent = lineContents[line - rootFirstLine] |
| 134 | + if (isComment) { |
| 135 | + lineContent.containsComment = true |
| 136 | + } else { |
| 137 | + lineContent.containsCode = true |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + root.accept(visitor) |
| 144 | + val code = lineContents.count { it.containsCode } |
| 145 | + val comment = lineContents.count { it.containsComment } |
| 146 | + tw.writeNumlines(id, numLines, code, comment) |
| 147 | + } |
| 148 | + |
| 149 | + private class LineContent { |
| 150 | + var containsComment = false |
| 151 | + var containsCode = false |
| 152 | + } |
| 153 | +} |
0 commit comments