/
Analyzer.kt
205 lines (179 loc) · 8.64 KB
/
Analyzer.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package io.gitlab.arturbosch.detekt.core
import io.github.detekt.psi.absolutePath
import io.github.detekt.tooling.api.spec.ProcessingSpec
import io.gitlab.arturbosch.detekt.api.BaseRule
import io.gitlab.arturbosch.detekt.api.Config
import io.gitlab.arturbosch.detekt.api.FileProcessListener
import io.gitlab.arturbosch.detekt.api.Finding
import io.gitlab.arturbosch.detekt.api.Rule
import io.gitlab.arturbosch.detekt.api.RuleSetId
import io.gitlab.arturbosch.detekt.api.RuleSetProvider
import io.gitlab.arturbosch.detekt.api.internal.CompilerResources
import io.gitlab.arturbosch.detekt.api.internal.RequiresTypeResolution
import io.gitlab.arturbosch.detekt.api.internal.whichDetekt
import io.gitlab.arturbosch.detekt.api.internal.whichJava
import io.gitlab.arturbosch.detekt.api.internal.whichOS
import io.gitlab.arturbosch.detekt.core.config.AllRulesConfig
import io.gitlab.arturbosch.detekt.core.config.DisabledAutoCorrectConfig
import io.gitlab.arturbosch.detekt.core.rules.associateRuleIdsToRuleSetIds
import io.gitlab.arturbosch.detekt.core.rules.isActive
import io.gitlab.arturbosch.detekt.core.rules.shouldAnalyzeFile
import io.gitlab.arturbosch.detekt.core.suppressors.getSuppressors
import io.gitlab.arturbosch.detekt.core.tooling.getDefaultConfiguration
import org.jetbrains.kotlin.config.languageVersionSettings
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowValueFactoryImpl
import kotlin.reflect.full.hasAnnotation
private typealias FindingsResult = List<Map<RuleSetId, List<Finding>>>
internal class Analyzer(
private val settings: ProcessingSettings,
private val providers: List<RuleSetProvider>,
private val processors: List<FileProcessListener>
) {
private val config: Config = settings.spec.workaroundConfiguration(settings.config)
fun run(
ktFiles: Collection<KtFile>,
bindingContext: BindingContext = BindingContext.EMPTY
): Map<RuleSetId, List<Finding>> {
val languageVersionSettings = settings.environment.configuration.languageVersionSettings
val dataFlowValueFactory = DataFlowValueFactoryImpl(languageVersionSettings)
val compilerResources = CompilerResources(languageVersionSettings, dataFlowValueFactory)
val findingsPerFile: FindingsResult =
if (settings.spec.executionSpec.parallelAnalysis) {
runAsync(ktFiles, bindingContext, compilerResources)
} else {
runSync(ktFiles, bindingContext, compilerResources)
}
if (bindingContext == BindingContext.EMPTY) {
warnAboutEnabledRequiresTypeResolutionRules()
}
val findingsPerRuleSet = HashMap<RuleSetId, List<Finding>>()
for (findings in findingsPerFile) {
findingsPerRuleSet.mergeSmells(findings)
}
return findingsPerRuleSet
}
private fun runSync(
ktFiles: Collection<KtFile>,
bindingContext: BindingContext,
compilerResources: CompilerResources
): FindingsResult =
ktFiles.map { file ->
processors.forEach { it.onProcess(file, bindingContext) }
val findings = runCatching { analyze(file, bindingContext, compilerResources) }
.onFailure { throwIllegalStateException(file, it) }
.getOrDefault(emptyMap())
processors.forEach { it.onProcessComplete(file, findings, bindingContext) }
findings
}
private fun runAsync(
ktFiles: Collection<KtFile>,
bindingContext: BindingContext,
compilerResources: CompilerResources
): FindingsResult {
val service = settings.taskPool
val tasks: TaskList<Map<RuleSetId, List<Finding>>?> = ktFiles.map { file ->
service.task {
processors.forEach { it.onProcess(file, bindingContext) }
val findings = analyze(file, bindingContext, compilerResources)
processors.forEach { it.onProcessComplete(file, findings, bindingContext) }
findings
}.recover { throwIllegalStateException(file, it) }
}
return awaitAll(tasks).filterNotNull()
}
private fun analyze(
file: KtFile,
bindingContext: BindingContext,
compilerResources: CompilerResources
): Map<RuleSetId, List<Finding>> {
@Suppress("DEPRECATION")
fun isCorrectable(rule: BaseRule): Boolean = when (rule) {
is Rule -> rule.autoCorrect
is io.gitlab.arturbosch.detekt.api.MultiRule -> rule.rules.any { it.autoCorrect }
else -> error("No other rule type expected.")
}
val activeRuleSetsToRuleSetConfigs = providers.asSequence()
.map { it to config.subConfig(it.ruleSetId) }
.filter { (_, ruleSetConfig) -> ruleSetConfig.isActive() }
.map { (provider, ruleSetConfig) -> provider.instance(ruleSetConfig) to ruleSetConfig }
.filter { (_, ruleSetConfig) -> ruleSetConfig.shouldAnalyzeFile(file) }
val ruleIdsToRuleSetIds = associateRuleIdsToRuleSetIds(
activeRuleSetsToRuleSetConfigs.map { (ruleSet, _) -> ruleSet }
)
val (correctableRules, otherRules) = activeRuleSetsToRuleSetConfigs
.flatMap { (ruleSet, _) -> ruleSet.rules.asSequence() }
.filter { rule ->
bindingContext != BindingContext.EMPTY || !rule::class.hasAnnotation<RequiresTypeResolution>()
}
.partition { isCorrectable(it) }
val result = HashMap<RuleSetId, MutableList<Finding>>()
fun executeRules(rules: List<BaseRule>) {
for (rule in rules) {
rule.visitFile(file, bindingContext, compilerResources)
for (finding in filterSuppressedFindings(rule, bindingContext)) {
val mappedRuleSet = checkNotNull(ruleIdsToRuleSetIds[finding.id]) {
"Mapping for '${finding.id}' expected."
}
result.computeIfAbsent(mappedRuleSet) { mutableListOf() }
.add(finding)
}
}
}
executeRules(correctableRules)
executeRules(otherRules)
return result
}
private fun warnAboutEnabledRequiresTypeResolutionRules() {
providers.asSequence()
.map { it to config.subConfig(it.ruleSetId) }
.filter { (_, ruleSetConfig) -> ruleSetConfig.isActive() }
.map { (provider, ruleSetConfig) -> provider.instance(ruleSetConfig) to ruleSetConfig }
.flatMap { (ruleSet, _) -> ruleSet.rules.asSequence() }
.filter { rule -> (rule as? Rule)?.active == true }
.filter { rule -> rule::class.hasAnnotation<RequiresTypeResolution>() }
.forEach { rule ->
settings.debug { "The rule '${rule.ruleId}' requires type resolution but it was run without it." }
}
}
}
private fun filterSuppressedFindings(rule: BaseRule, bindingContext: BindingContext): List<Finding> {
val suppressors = getSuppressors(rule, bindingContext)
return if (suppressors.isNotEmpty()) {
rule.findings.filter { finding -> !suppressors.any { suppressor -> suppressor.shouldSuppress(finding) } }
} else {
rule.findings
}
}
private fun MutableMap<String, List<Finding>>.mergeSmells(other: Map<String, List<Finding>>) {
for ((key, findings) in other.entries) {
merge(key, findings) { f1, f2 -> f1.plus(f2) }
}
}
private fun throwIllegalStateException(file: KtFile, error: Throwable): Nothing {
val message = """
Analyzing ${file.absolutePath()} led to an exception.
Location: ${error.stackTrace.firstOrNull()?.toString()}
The original exception message was: ${error.localizedMessage}
Running detekt '${whichDetekt() ?: "unknown"}' on Java '${whichJava()}' on OS '${whichOS()}'
If the exception message does not help, please feel free to create an issue on our GitHub page.
""".trimIndent()
throw IllegalStateException(message, error)
}
internal fun ProcessingSpec.workaroundConfiguration(config: Config): Config = with(configSpec) {
var declaredConfig: Config? = when {
configPaths.isNotEmpty() -> config
resources.isNotEmpty() -> config
useDefaultConfig -> config
else -> null
}
if (rulesSpec.activateAllRules) {
val defaultConfig = getDefaultConfiguration()
declaredConfig = AllRulesConfig(declaredConfig ?: defaultConfig, defaultConfig)
}
if (!rulesSpec.autoCorrect) {
declaredConfig = DisabledAutoCorrectConfig(declaredConfig ?: getDefaultConfiguration())
}
return declaredConfig ?: getDefaultConfiguration()
}