Skip to content

Commit 69ff2b7

Browse files
committed
wip
1 parent 51b3e0d commit 69ff2b7

File tree

2 files changed

+73
-11
lines changed
  • semanticdb-kotlinc/src

2 files changed

+73
-11
lines changed

semanticdb-kotlinc/src/main/kotlin/com/sourcegraph/semanticdb_kotlinc/Analyzer.kt

Lines changed: 52 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
package com.sourcegraph.semanticdb_kotlinc
22

3+
import java.io.PrintWriter
4+
import java.io.Writer
35
import java.nio.file.Files
46
import java.nio.file.Path
57
import java.nio.file.Paths
68
import kotlin.contracts.ExperimentalContracts
79
import org.jetbrains.kotlin.analyzer.AnalysisResult
10+
import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys
11+
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity
12+
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
813
import org.jetbrains.kotlin.com.intellij.openapi.project.Project
14+
import org.jetbrains.kotlin.config.CompilerConfiguration
915
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
1016
import org.jetbrains.kotlin.psi.*
1117
import org.jetbrains.kotlin.resolve.BindingTrace
1218
import org.jetbrains.kotlin.resolve.jvm.extensions.AnalysisHandlerExtension
19+
import org.jetbrains.kotlin.utils.rethrow
20+
import java.io.StringWriter
1321

1422
@ExperimentalContracts
1523
class Analyzer(
@@ -19,24 +27,37 @@ class Analyzer(
1927
) : AnalysisHandlerExtension {
2028
private val globals = GlobalSymbolsCache()
2129

30+
private val messageCollector =
31+
CompilerConfiguration()
32+
.get(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, MessageCollector.NONE)
33+
2234
override fun analysisCompleted(
2335
project: Project,
2436
module: ModuleDescriptor,
2537
bindingTrace: BindingTrace,
2638
files: Collection<KtFile>
27-
): AnalysisResult? {
28-
val resolver = DescriptorResolver(bindingTrace).also { globals.resolver = it }
29-
for (file in files) {
30-
val lineMap = LineMap(project, file)
31-
val document = SemanticdbVisitor(sourceroot, resolver, file, lineMap, globals).build()
32-
semanticdbOutPathForFile(file)?.apply {
33-
Files.write(this, TextDocuments { addDocuments(document) }.toByteArray())
39+
): AnalysisResult? =
40+
try {
41+
val resolver = DescriptorResolver(bindingTrace).also { globals.resolver = it }
42+
for (file in files) {
43+
try {
44+
val lineMap = LineMap(project, file)
45+
val document =
46+
SemanticdbVisitor(sourceroot, resolver, file, lineMap, globals).build()
47+
semanticdbOutPathForFile(file)?.apply {
48+
Files.write(this, TextDocuments { addDocuments(document) }.toByteArray())
49+
}
50+
callback(document)
51+
} catch (e: Exception) {
52+
handleException(e)
53+
}
3454
}
35-
callback(document)
36-
}
3755

38-
return super.analysisCompleted(project, module, bindingTrace, files)
39-
}
56+
super.analysisCompleted(project, module, bindingTrace, files)
57+
} catch (e: Exception) {
58+
handleException(e)
59+
super.analysisCompleted(project, module, bindingTrace, files)
60+
}
4061

4162
private fun semanticdbOutPathForFile(file: KtFile): Path? {
4263
val normalizedPath = Paths.get(file.virtualFilePath).normalize()
@@ -57,4 +78,24 @@ class Analyzer(
5778
"given file is not under the sourceroot.\n\tSourceroot: $sourceroot\n\tFile path: ${file.virtualFilePath}\n\tNormalized file path: $normalizedPath")
5879
return null
5980
}
81+
82+
private fun handleException(e: Exception) {
83+
val writer =
84+
PrintWriter(
85+
object : Writer() {
86+
val buf = StringBuffer()
87+
override fun close() =
88+
messageCollector.report(
89+
CompilerMessageSeverity.EXCEPTION, buf.toString())
90+
override fun flush() = Unit
91+
override fun write(data: CharArray, offset: Int, len: Int) {
92+
buf.append(data, offset, len)
93+
}
94+
},
95+
false)
96+
e.printStackTrace(writer)
97+
writer.println(
98+
"Please report a bug to https://github.com/sourcegraph/lsif-kotlin with the stack trace above.")
99+
writer.close()
100+
}
60101
}

semanticdb-kotlinc/src/test/kotlin/com/sourcegraph/semanticdb_kotlinc/test/AnalyzerTest.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,27 @@ class AnalyzerTest {
121121
assertSoftly(document.symbolsList) { withClue(this) { symbols.forEach(::shouldContain) } }
122122
}
123123

124+
@Test
125+
fun `exception test`(@TempDir path: Path) {
126+
val buildPath = File(path.resolve("build").toString()).apply { mkdir() }
127+
val result =
128+
KotlinCompilation()
129+
.apply {
130+
sources = listOf(SourceFile.testKt(""))
131+
compilerPlugins = listOf(AnalyzerRegistrar { throw Exception("sample text") })
132+
verbose = false
133+
pluginOptions =
134+
listOf(
135+
PluginOption("semanticdb-kotlinc", "sourceroot", path.toString()),
136+
PluginOption("semanticdb-kotlinc", "targetroot", buildPath.toString()))
137+
commandLineProcessors = listOf(AnalyzerCommandLineProcessor())
138+
workingDir = path.toFile()
139+
}
140+
.compile()
141+
142+
result.exitCode shouldBe KotlinCompilation.ExitCode.OK
143+
}
144+
124145
@Test
125146
// shamelessly stolen code snippet from https://learnxinyminutes.com/docs/kotlin/
126147
fun `learn x in y test`(@TempDir path: Path) {

0 commit comments

Comments
 (0)