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