Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try Kotlin 2.1.0-RC2 #484

Draft
wants to merge 3 commits into
base: margo/rewrite-predict-runtime-value
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ group=org.sonarsource.kotlin
version=2.21-SNAPSHOT
description=Code Analyzer for Kotlin
projectTitle=Kotlin
kotlinVersion=2.0.20
kotlinVersion=2.1.0-RC2
org.gradle.jvmargs=-Xmx4096M
1 change: 1 addition & 0 deletions sonar-kotlin-api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ dependencies {
implementation(libs.staxmate)
implementation(libs.gson)
implementation(libs.sonar.analyzer.commons.recognizers)
implementation("com.github.ben-manes.caffeine:caffeine:2.9.3")

testImplementation(testLibs.junit.jupiter)
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/*
* SonarSource Kotlin
* Copyright (C) 2018-2024 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonarsource.kotlin.api.frontend;


import com.intellij.core.CoreApplicationEnvironment;
import com.intellij.mock.MockApplication;
import com.intellij.mock.MockProject;
import com.intellij.openapi.Disposable;
import com.intellij.psi.ClassTypePointerFactory;
import com.intellij.psi.impl.smartPointers.PsiClassReferenceTypePointerFactory;
import org.jetbrains.kotlin.analysis.api.descriptors.CliFe10AnalysisFacade;
import org.jetbrains.kotlin.analysis.api.descriptors.Fe10AnalysisFacade;
import org.jetbrains.kotlin.analysis.api.descriptors.KaFe10AnalysisHandlerExtension;
import org.jetbrains.kotlin.analysis.api.platform.lifetime.KotlinAlwaysAccessibleLifetimeTokenFactory;
import org.jetbrains.kotlin.analysis.api.platform.lifetime.KotlinLifetimeTokenFactory;
import org.jetbrains.kotlin.analysis.api.platform.modification.KotlinGlobalModificationService;
import org.jetbrains.kotlin.analysis.api.platform.modification.KotlinModificationTrackerFactory;
import org.jetbrains.kotlin.analysis.api.platform.projectStructure.KotlinByModulesResolutionScopeProvider;
import org.jetbrains.kotlin.analysis.api.platform.projectStructure.KotlinProjectStructureProvider;
import org.jetbrains.kotlin.analysis.api.platform.projectStructure.KotlinResolutionScopeProvider;
import org.jetbrains.kotlin.analysis.api.standalone.base.modification.KotlinStandaloneGlobalModificationService;
import org.jetbrains.kotlin.analysis.api.standalone.base.modification.KotlinStandaloneModificationTrackerFactory;
import org.jetbrains.kotlin.analysis.api.standalone.base.projectStructure.AnalysisApiSimpleServiceRegistrar;
import org.jetbrains.kotlin.analysis.api.standalone.base.projectStructure.PluginStructureProvider;
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment;
import org.jetbrains.kotlin.references.fe10.base.DummyKtFe10ReferenceResolutionHelper;
import org.jetbrains.kotlin.references.fe10.base.KtFe10ReferenceResolutionHelper;
import org.jetbrains.kotlin.resolve.extensions.AnalysisHandlerExtension;

import java.util.Collections;

@SuppressWarnings("ALL")
public class K1 {

private K1() {
// Utility class
}

public static void configureK1AnalysisApiServices(KotlinCoreEnvironment env) {
MockApplication application = env.getProjectEnvironment().getEnvironment().getApplication();

if (application.getServiceIfCreated(KtFe10ReferenceResolutionHelper.class) == null) {
AnalysisApiFe10ServiceRegistrar.INSTANCE.registerApplicationServices(application);
}
final var project = env.getProjectEnvironment().getProject();
AnalysisApiFe10ServiceRegistrar.INSTANCE.registerProjectServices(project);
AnalysisApiFe10ServiceRegistrar.INSTANCE.registerProjectModelServices(
project,
env.getProjectEnvironment().getParentDisposable()
);

project.registerService(
KotlinModificationTrackerFactory.class,
KotlinStandaloneModificationTrackerFactory.class
);
project.registerService(
KotlinGlobalModificationService.class,
KotlinStandaloneGlobalModificationService.class
);
project.registerService(
KotlinLifetimeTokenFactory.class,
KotlinAlwaysAccessibleLifetimeTokenFactory.class
);
project.registerService(
KotlinResolutionScopeProvider.class,
KotlinByModulesResolutionScopeProvider.class
);
project.registerService(
KotlinProjectStructureProvider.class,
KtModuleProviderByCompilerConfiguration.build(
env.getProjectEnvironment(),
env.getConfiguration(),
Collections.emptyList()
)
);
}

private static class AnalysisApiFe10ServiceRegistrar extends AnalysisApiSimpleServiceRegistrar {
public static final AnalysisApiSimpleServiceRegistrar INSTANCE = new AnalysisApiFe10ServiceRegistrar();
private static final String PLUGIN_RELATIVE_PATH = "/META-INF/analysis-api/analysis-api-fe10.xml";

private AnalysisApiFe10ServiceRegistrar() {
}

@Override
public void registerApplicationServices(MockApplication application) {
PluginStructureProvider.INSTANCE.registerApplicationServices(application, PLUGIN_RELATIVE_PATH);
application.registerService(
KtFe10ReferenceResolutionHelper.class,
DummyKtFe10ReferenceResolutionHelper.INSTANCE
);

final var applicationArea = application.getExtensionArea();
if (!applicationArea.hasExtensionPoint(ClassTypePointerFactory.EP_NAME)) {
CoreApplicationEnvironment.registerApplicationExtensionPoint(
ClassTypePointerFactory.EP_NAME,
ClassTypePointerFactory.class
);
applicationArea
.getExtensionPoint(ClassTypePointerFactory.EP_NAME)
.registerExtension(new PsiClassReferenceTypePointerFactory(), application);
}
}

@Override
public void registerProjectExtensionPoints(MockProject project) {
AnalysisHandlerExtension.Companion.registerExtensionPoint(project);
PluginStructureProvider.INSTANCE.registerProjectExtensionPoints(project, PLUGIN_RELATIVE_PATH);
}

@Override
public void registerProjectServices(MockProject project) {
PluginStructureProvider.INSTANCE.registerProjectServices(project, PLUGIN_RELATIVE_PATH);
PluginStructureProvider.INSTANCE.registerProjectListeners(project, PLUGIN_RELATIVE_PATH);
}

@Override
public void registerProjectModelServices(MockProject project, Disposable disposable) {
project.registerService(Fe10AnalysisFacade.class, new CliFe10AnalysisFacade());
AnalysisHandlerExtension.Companion.registerExtension(project, new KaFe10AnalysisHandlerExtension());
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* SonarSource Kotlin
* Copyright (C) 2018-2024 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonarsource.kotlin.api.frontend

@Deprecated("")
annotation class K1only(val comments: String = "")
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import org.jetbrains.kotlin.analysis.project.structure.builder.buildKtSdkModule
import org.jetbrains.kotlin.analysis.project.structure.builder.buildKtSourceModule
import org.jetbrains.kotlin.cli.common.CliModuleVisibilityManagerImpl
import org.jetbrains.kotlin.cli.jvm.config.jvmClasspathRoots
import org.jetbrains.kotlin.config.CommonConfigurationKeys
import org.jetbrains.kotlin.config.CompilerConfiguration
import org.jetbrains.kotlin.config.JVMConfigurationKeys
import org.jetbrains.kotlin.load.kotlin.ModuleVisibilityManager
Expand All @@ -54,13 +55,15 @@ fun createK2AnalysisSession(
// https://github.com/JetBrains/kotlin/blob/a9ff22693479cabd201909a06e6764c00eddbf7b/analysis/analysis-api-fe10/tests/org/jetbrains/kotlin/analysis/api/fe10/test/configurator/AnalysisApiFe10TestServiceRegistrar.kt#L49
registerProjectService(ModuleVisibilityManager::class.java, CliModuleVisibilityManagerImpl(enabled = true))

// TODO language version, jvm target, etc
val platform = JvmPlatforms.defaultJvmPlatform
buildKtModuleProvider {
this.platform = platform
addModule(buildKtSourceModule {
this.platform = platform
moduleName = "module"
compilerConfiguration[CommonConfigurationKeys.LANGUAGE_VERSION_SETTINGS]?.let {
languageVersionSettings = it
}
addSourceVirtualFiles(virtualFiles)
addRegularDependency(buildKtLibraryModule {
this.platform = platform
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import org.jetbrains.kotlin.psi.KtPsiFactory
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.lazy.declarations.FileBasedDeclarationProviderFactory
import org.jetbrains.kotlin.util.slicedMap.WritableSlice
import org.sonarsource.kotlin.api.frontend.K1.configureK1AnalysisApiServices
import java.io.File

/**
Expand Down
Loading