Skip to content

Commit

Permalink
[FE 1.0] Add compiler flag for rendering internal diagnostic names in…
Browse files Browse the repository at this point in the history
… error messages
  • Loading branch information
demiurg906 authored and TeamCityServer committed Jan 23, 2022
1 parent b9c22a5 commit dd95390
Show file tree
Hide file tree
Showing 28 changed files with 112 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,9 @@ abstract class CommonCompilerArguments : CommonToolArguments() {
@Argument(value = "-Xenable-incremental-compilation", description = "Enable incremental compilation")
var incrementalCompilation: Boolean? by FreezableVar(null)

@Argument(value = "-Xrender-internal-diagnostic-names", description = "Render internal names of warnings and errors")
var renderInternalDiagnosticNames: Boolean by FreezableVar(false)

open fun configureAnalysisFlags(collector: MessageCollector, languageVersion: LanguageVersion): MutableMap<AnalysisFlag<*>, Any> {
return HashMap<AnalysisFlag<*>, Any>().apply {
put(AnalysisFlags.skipMetadataVersionCheck, skipMetadataVersionCheck)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ protected ExitCode doExecute(
List<KtFile> sourcesFiles = environmentForJS.getSourceFiles();

environmentForJS.getConfiguration().put(CLIConfigurationKeys.ALLOW_KOTLIN_PACKAGE, arguments.getAllowKotlinPackage());
environmentForJS.getConfiguration().put(CLIConfigurationKeys.RENDER_DIAGNOSTIC_INTERNAL_NAME, arguments.getRenderInternalDiagnosticNames());


if (!UtilsKt.checkKotlinPackageUsage(environmentForJS.getConfiguration(), sourcesFiles)) return ExitCode.COMPILATION_ERROR;

Expand Down Expand Up @@ -252,7 +254,9 @@ public void warning(@NotNull String message) {
AnalysisResult analysisResult;
do {
AnalyzerWithCompilerReport analyzerWithCompilerReport = new AnalyzerWithCompilerReport(
messageCollector, CommonConfigurationKeysKt.getLanguageVersionSettings(configuration)
messageCollector,
CommonConfigurationKeysKt.getLanguageVersionSettings(configuration),
configuration.getBoolean(CLIConfigurationKeys.RENDER_DIAGNOSTIC_INTERNAL_NAME)
);
List<KtFile> sources = environmentForJS.getSourceFiles();
analyzerWithCompilerReport.analyzeAndReport(sourcesFiles, () -> TopDownAnalyzerFacadeForJS.analyzeFiles(sources, config));
Expand Down Expand Up @@ -321,7 +325,7 @@ public void warning(@NotNull String message) {

ProgressIndicatorAndCompilationCanceledStatus.checkCanceled();

AnalyzerWithCompilerReport.Companion.reportDiagnostics(translationResult.getDiagnostics(), messageCollector);
AnalyzerWithCompilerReport.Companion.reportDiagnostics(translationResult.getDiagnostics(), messageCollector, configuration.getBoolean(CLIConfigurationKeys.RENDER_DIAGNOSTIC_INTERNAL_NAME));

if (translationResult instanceof TranslationResult.Fail) return ExitCode.COMPILATION_ERROR;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ class K2JsIrCompiler : CLICompiler<K2JSCompilerArguments>() {
val sourcesFiles = environmentForJS.getSourceFiles()

configurationJs.put(CLIConfigurationKeys.ALLOW_KOTLIN_PACKAGE, arguments.allowKotlinPackage)
configurationJs.put(CLIConfigurationKeys.RENDER_DIAGNOSTIC_INTERNAL_NAME, arguments.renderInternalDiagnosticNames)
configurationJs.put(JSConfigurationKeys.PROPERTY_LAZY_INITIALIZATION, arguments.irPropertyLazyInitialization)

if (!checkKotlinPackageUsage(environmentForJS.configuration, sourcesFiles)) return ExitCode.COMPILATION_ERROR
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ public class CLIConfigurationKeys {
public static final CompilerConfigurationKey<MessageCollector> ORIGINAL_MESSAGE_COLLECTOR_KEY =
CompilerConfigurationKey.create("original message collector");

public static final CompilerConfigurationKey<Boolean> RENDER_DIAGNOSTIC_INTERNAL_NAME =
CompilerConfigurationKey.create("render diagnostic internal name");

public static final CompilerConfigurationKey<Boolean> ALLOW_KOTLIN_PACKAGE =
CompilerConfigurationKey.create("allow kotlin package");
public static final CompilerConfigurationKey<CommonCompilerPerformanceManager> PERF_MANAGER =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ import org.jetbrains.kotlin.resolve.jvm.JvmBindingContextSlices

class AnalyzerWithCompilerReport(
private val messageCollector: MessageCollector,
private val languageVersionSettings: LanguageVersionSettings
private val languageVersionSettings: LanguageVersionSettings,
private val renderDiagnosticName: Boolean
) : AbstractAnalyzerWithCompilerReport {
override val targetEnvironment: TargetEnvironment
get() = CompilerEnvironment
Expand All @@ -47,7 +48,8 @@ class AnalyzerWithCompilerReport(

constructor(configuration: CompilerConfiguration) : this(
configuration.get(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY) ?: MessageCollector.NONE,
configuration.languageVersionSettings
configuration.languageVersionSettings,
configuration.getBoolean(CLIConfigurationKeys.RENDER_DIAGNOSTIC_INTERNAL_NAME)
)

private fun reportIncompleteHierarchies() {
Expand Down Expand Up @@ -117,7 +119,7 @@ class AnalyzerWithCompilerReport(
reportWarning = { message -> messageCollector.report(WARNING, message) }
)
reportSyntaxErrors(files)
reportDiagnostics(analysisResult.bindingContext.diagnostics, messageCollector)
reportDiagnostics(analysisResult.bindingContext.diagnostics, messageCollector, renderDiagnosticName)
reportIncompleteHierarchies()
reportAlternativeSignatureErrors()
}
Expand All @@ -140,29 +142,43 @@ class AnalyzerWithCompilerReport(

private val SYNTAX_ERROR_FACTORY = DiagnosticFactory0.create<PsiErrorElement>(Severity.ERROR)

private fun reportDiagnostic(diagnostic: Diagnostic, reporter: DiagnosticMessageReporter): Boolean {
private fun reportDiagnostic(diagnostic: Diagnostic, reporter: DiagnosticMessageReporter, renderDiagnosticName: Boolean): Boolean {
if (!diagnostic.isValid) return false

val message = (diagnostic as? MyDiagnostic<*>)?.message ?: DefaultErrorMessages.render(diagnostic)
val textToRender = when (renderDiagnosticName) {
true -> "[${diagnostic.factoryName}] $message"
false -> message
}

reporter.report(
diagnostic,
diagnostic.psiFile,
(diagnostic as? MyDiagnostic<*>)?.message ?: DefaultErrorMessages.render(diagnostic)
textToRender
)

return diagnostic.severity == Severity.ERROR
}

fun reportDiagnostics(unsortedDiagnostics: GenericDiagnostics<*>, reporter: DiagnosticMessageReporter): Boolean {
fun reportDiagnostics(
unsortedDiagnostics: GenericDiagnostics<*>,
reporter: DiagnosticMessageReporter,
renderDiagnosticName: Boolean
): Boolean {
var hasErrors = false
val diagnostics = sortedDiagnostics(unsortedDiagnostics.all().filterIsInstance<Diagnostic>())
for (diagnostic in diagnostics) {
hasErrors = hasErrors or reportDiagnostic(diagnostic, reporter)
hasErrors = hasErrors or reportDiagnostic(diagnostic, reporter, renderDiagnosticName)
}
return hasErrors
}

fun reportDiagnostics(diagnostics: GenericDiagnostics<*>, messageCollector: MessageCollector): Boolean {
val hasErrors = reportDiagnostics(diagnostics, DefaultDiagnosticReporter(messageCollector))
fun reportDiagnostics(
diagnostics: GenericDiagnostics<*>,
messageCollector: MessageCollector,
renderInternalDiagnosticName: Boolean
): Boolean {
val hasErrors = reportDiagnostics(diagnostics, DefaultDiagnosticReporter(messageCollector), renderInternalDiagnosticName)

if (diagnostics.any { it.factory == Errors.INCOMPATIBLE_CLASS }) {
messageCollector.report(
Expand Down Expand Up @@ -207,7 +223,7 @@ class AnalyzerWithCompilerReport(

private fun <E : PsiElement> reportDiagnostic(element: E, factory: DiagnosticFactory0<E>, message: String) {
val diagnostic = MyDiagnostic(element, factory, message)
AnalyzerWithCompilerReport.reportDiagnostic(diagnostic, reporter)
AnalyzerWithCompilerReport.reportDiagnostic(diagnostic, reporter, renderDiagnosticName = false)
if (allErrorsAtEof && !element.isAtEof()) {
allErrorsAtEof = false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ object FirKotlinToJvmBytecodeCompiler {
),
projectEnvironment,
messageCollector,
moduleConfiguration.getBoolean(CLIConfigurationKeys.RENDER_DIAGNOSTIC_INTERNAL_NAME),
moduleConfiguration,
performanceManager,
targetIds,
Expand Down Expand Up @@ -337,7 +338,8 @@ object FirKotlinToJvmBytecodeCompiler {
generationState.collectedExtraJvmDiagnostics,
dummyBindingContext.diagnostics
),
messageCollector
messageCollector,
renderDiagnosticName
)
ProgressIndicatorAndCompilationCanceledStatus.checkCanceled()

Expand All @@ -349,6 +351,7 @@ object FirKotlinToJvmBytecodeCompiler {
val allSources: List<KtFile>,
val projectEnvironment: AbstractProjectEnvironment,
val messageCollector: MessageCollector,
val renderDiagnosticName: Boolean,
val moduleConfiguration: CompilerConfiguration,
val performanceManager: CommonCompilerPerformanceManager?,
val targetIds: List<TargetId>?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,11 @@ object KotlinToJVMBytecodeCompiler {
jvmResolveLibraries(klibPaths, collector.toLogger())
}?.getFullList() ?: emptyList()

val analyzerWithCompilerReport = AnalyzerWithCompilerReport(collector, environment.configuration.languageVersionSettings)
val analyzerWithCompilerReport = AnalyzerWithCompilerReport(
collector,
environment.configuration.languageVersionSettings,
environment.configuration.getBoolean(CLIConfigurationKeys.RENDER_DIAGNOSTIC_INTERNAL_NAME)
)
analyzerWithCompilerReport.analyzeAndReport(sourceFiles) {
val project = environment.project
val moduleOutputs = environment.configuration.get(JVMConfigurationKeys.MODULES)?.mapNotNullTo(hashSetOf()) { module ->
Expand Down Expand Up @@ -396,7 +400,8 @@ object KotlinToJVMBytecodeCompiler {
state.collectedExtraJvmDiagnostics,
bindingContext.diagnostics
),
messageCollector
messageCollector,
configuration.getBoolean(CLIConfigurationKeys.RENDER_DIAGNOSTIC_INTERNAL_NAME)
)
FirDiagnosticsCompilerResultsReporter.reportToMessageCollector(diagnosticsReporter, messageCollector)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ fun CompilerConfiguration.configureAdvancedJvmOptions(arguments: K2JVMCompilerAr
}

put(CLIConfigurationKeys.ALLOW_KOTLIN_PACKAGE, arguments.allowKotlinPackage)
put(CLIConfigurationKeys.RENDER_DIAGNOSTIC_INTERNAL_NAME, arguments.renderInternalDiagnosticNames)
put(JVMConfigurationKeys.USE_SINGLE_MODULE, arguments.singleModule)
put(JVMConfigurationKeys.USE_OLD_INLINE_CLASSES_MANGLING_SCHEME, arguments.useOldInlineClassesManglingScheme)
put(JVMConfigurationKeys.ENABLE_JVM_PREVIEW, arguments.enableJvmPreview)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ private fun runCommonAnalysis(
val files = environment.getSourceFiles()
val moduleName = Name.special("<${configuration.getNotNull(CommonConfigurationKeys.MODULE_NAME)}>")

val analyzer = AnalyzerWithCompilerReport(messageCollector, configuration.languageVersionSettings)
val analyzer = AnalyzerWithCompilerReport(
messageCollector,
configuration.languageVersionSettings,
configuration.getBoolean(CLIConfigurationKeys.RENDER_DIAGNOSTIC_INTERNAL_NAME)
)

analyzer.analyzeAndReport(files) {
CommonResolverForModuleFactory.analyzeFiles(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class K2MetadataCompiler : CLICompiler<K2MetadataCompilerArguments>() {
configuration.put(CommonConfigurationKeys.MODULE_NAME, moduleName)

configuration.put(CLIConfigurationKeys.ALLOW_KOTLIN_PACKAGE, arguments.allowKotlinPackage)
configuration.put(CLIConfigurationKeys.RENDER_DIAGNOSTIC_INTERNAL_NAME, arguments.renderInternalDiagnosticNames)

configuration.putIfNotNull(K2MetadataConfigurationKeys.FRIEND_PATHS, arguments.friendPaths?.toList())
configuration.putIfNotNull(K2MetadataConfigurationKeys.REFINES_PATHS, arguments.refinesPaths?.toList())
Expand Down
2 changes: 2 additions & 0 deletions compiler/testData/cli/js/jsExtraHelp.out
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ where advanced options include:
-Xproper-ieee754-comparisons Generate proper IEEE 754 comparisons in all cases if values are statically known to be of primitive numeric types
-Xread-deserialized-contracts Enable reading of contracts from metadata
-Xklib-relative-path-base Provide a base paths to compute source's relative paths in klib (default is empty)
-Xrender-internal-diagnostic-names
Render internal names of warnings and errors
-Xreport-output-files Report source to output files mapping
-Xreport-perf Report detailed performance statistics
-Xself-upper-bound-inference Support inferring type arguments based on only self upper bounds of the corresponding type parameters
Expand Down
2 changes: 2 additions & 0 deletions compiler/testData/cli/jvm/extraHelp.out
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ where advanced options include:
-Xproper-ieee754-comparisons Generate proper IEEE 754 comparisons in all cases if values are statically known to be of primitive numeric types
-Xread-deserialized-contracts Enable reading of contracts from metadata
-Xklib-relative-path-base Provide a base paths to compute source's relative paths in klib (default is empty)
-Xrender-internal-diagnostic-names
Render internal names of warnings and errors
-Xreport-output-files Report source to output files mapping
-Xreport-perf Report detailed performance statistics
-Xself-upper-bound-inference Support inferring type arguments based on only self upper bounds of the corresponding type parameters
Expand Down
4 changes: 4 additions & 0 deletions compiler/testData/cli/jvm/reportInternalDiagnosticNames.args
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
$TESTDATA_DIR$/reportInternalDiagnosticNames.kt
-Xrender-internal-diagnostic-names
-d
$TEMP_DIR$
3 changes: 3 additions & 0 deletions compiler/testData/cli/jvm/reportInternalDiagnosticNames.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fun test_2(): String {
val x: String = 1
}
7 changes: 7 additions & 0 deletions compiler/testData/cli/jvm/reportInternalDiagnosticNames.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
compiler/testData/cli/jvm/reportInternalDiagnosticNames.kt:2:21: error: [CONSTANT_EXPECTED_TYPE_MISMATCH] The integer literal does not conform to the expected type String
val x: String = 1
^
compiler/testData/cli/jvm/reportInternalDiagnosticNames.kt:3:1: error: [NO_RETURN_IN_FUNCTION_WITH_BLOCK_BODY] A 'return' expression required in a function with a block body ('{...}')
}
^
COMPILATION_ERROR
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ class DiagnosticMessagesTextHandler(
false
)

AnalyzerWithCompilerReport.reportDiagnostics(info.analysisResult.bindingContext.diagnostics, diagnosticsFullTextCollector)
AnalyzerWithCompilerReport.reportDiagnostics(
info.analysisResult.bindingContext.diagnostics,
diagnosticsFullTextCollector,
renderInternalDiagnosticName = false
)

diagnosticsFullTextCollector.flush()
diagnosticsFullTextPrintStream.flush()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,11 @@ abstract class AbstractDiagnosticsTest : BaseDiagnosticsTest() {

if (testFile.renderDiagnosticsFullText) {
shouldCheckDiagnosticsFullText = true
AnalyzerWithCompilerReport.reportDiagnostics(moduleBindingContext.diagnostics, diagnosticsFullTextCollector)
AnalyzerWithCompilerReport.reportDiagnostics(
moduleBindingContext.diagnostics,
diagnosticsFullTextCollector,
renderInternalDiagnosticName = false
)
}
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@ class CompileKotlinAgainstCustomBinariesTest : AbstractKotlinCompilerIntegration

AnalyzerWithCompilerReport.reportDiagnostics(
result.bindingContext.diagnostics,
PrintingMessageCollector(System.err, MessageRenderer.PLAIN_FULL_PATHS, false)
PrintingMessageCollector(System.err, MessageRenderer.PLAIN_FULL_PATHS, false),
renderInternalDiagnosticName = false
)

assertEquals("There should be no diagnostics", 0, result.bindingContext.diagnostics.count())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class JsTranslationResultHandler(testServices: TestServices) : JsBinaryArtifactH
if (result !is TranslationResult.Success) {
val outputStream = ByteArrayOutputStream()
val collector = PrintingMessageCollector(PrintStream(outputStream), MessageRenderer.PLAIN_FULL_PATHS, true)
AnalyzerWithCompilerReport.reportDiagnostics(result.diagnostics, collector)
AnalyzerWithCompilerReport.reportDiagnostics(result.diagnostics, collector, renderInternalDiagnosticName = false)
val messages = outputStream.toByteArray().toString(Charset.forName("UTF-8"))
throw AssertionError("The following errors occurred compiling test:\n$messages")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ abstract class AbstractJsLineNumberTest : KotlinTestWithEnvironment() {
if (translationResult !is TranslationResult.Success) {
val outputStream = ByteArrayOutputStream()
val collector = PrintingMessageCollector(PrintStream(outputStream), MessageRenderer.PLAIN_FULL_PATHS, true)
AnalyzerWithCompilerReport.reportDiagnostics(translationResult.diagnostics, collector)
AnalyzerWithCompilerReport.reportDiagnostics(translationResult.diagnostics, collector, renderInternalDiagnosticName = false)
val messages = outputStream.toByteArray().toString(Charset.forName("UTF-8"))
throw AssertionError("The following errors occurred compiling test:\n" + messages)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class K2Native : CLICompiler<K2NativeCompilerArguments>() {
}

configuration.put(CommonConfigurationKeys.KLIB_NORMALIZE_ABSOLUTE_PATH, arguments.normalizeAbsolutePath)
configuration.put(CLIConfigurationKeys.RENDER_DIAGNOSTIC_INTERNAL_NAME, arguments.renderInternalDiagnosticNames)

try {
val konanConfig = KonanConfig(project, configuration)
Expand Down

0 comments on commit dd95390

Please sign in to comment.