Files
kotlin-fork/native/analysis-api-klib-reader
Sebastian Sellmair 426d71b088 [aa-klib-reader] Only resolve symbols from the associated library/sourceFile
Previously the addresses were resolved by only respecting
CallableId or ClassId. This however, could lead to
resolving symbols that were not defined in the klib which
initially provided the declaration address.

E.g. this commit adds a test in `GetSymbolsTest`, where
```
// A.kt
private fun foo() = 42

// B.kt
private fun foo() = 42
```

In this case the two source files (A.kt and B.kt) defined
two distinct addresses for `foo`. However: Resolving
any of those two addresses would resolve both functions (A&B), which
is not expected.

^KT-66271 Fixed
2024-03-18 10:13:57 +00:00
..

Native Analysis Api based Klib reader

Tasks

Run Tests

./gradlew :native:analysis-api-klib-reader:check

Usage: Reading symbols from a given klib module

A set of top level KlibDeclarationAddress can be read from KtLibraryModule by

fun example(module: KtLibraryModule) {
    val addresses = module.readKlibDeclarationAddresses()
}

Such addresses can be resolved to KtSymbols by

addresses.flatMap { address -> address.getSymbols() }

Note: getSymbols returns a sequence as multiple symbols can live under the same address.

Usage: Getting all library modules in a Standalone Analysis API session.

Example of creating a session

 val session = buildStandaloneAnalysisAPISession {
     buildKtModuleProvider {
         // ... 
         addModule(buildKtLibraryModule {
             addBinaryRoot(pathToKlib)
             // ..
         })
     }
  }

Example analyzing libraries within the session

session.getAllLibraryModules().forEach { module -> 
    analyze(module) { 
        module.readKlibDeclarationAddresses()
            .flatMap { address -> address.getSymbols() }
    }
}

Test Strategy

Black Box Test:

We are building a klib from the testProject. This klib will be used to read the addresses. Those addresses will be rendered and compared to !testProject.addresses