-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add custom logger features * Remove @ComposeInvestigatorCompilerApi * Fix package * Clarify the name of the LogType class in ComposeInvestigate
- Loading branch information
Showing
13 changed files
with
298 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
...ain/kotlin/land/sungbin/composeinvestigator/compiler/internal/logger/InvestigateLogger.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
* Designed and developed by Ji Sungbin 2023. | ||
* | ||
* Licensed under the MIT. | ||
* Please see full license: https://github.com/jisungbin/ComposeInvestigator/blob/main/LICENSE | ||
*/ | ||
|
||
package land.sungbin.composeinvestigator.compiler.internal.logger | ||
|
||
import land.sungbin.composeinvestigator.compiler.internal.COMPOSE_INVESTIGATE_LOG_TYPE_FQN | ||
import land.sungbin.composeinvestigator.compiler.internal.COMPOSE_INVESTIGATE_LOG_TYPE_INVALIDATION_PROCESSED_FQN | ||
import land.sungbin.composeinvestigator.compiler.internal.COMPOSE_INVESTIGATE_LOG_TYPE_INVALIDATION_SKIPPED_FQN | ||
import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext | ||
import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET | ||
import org.jetbrains.kotlin.ir.expressions.IrCall | ||
import org.jetbrains.kotlin.ir.expressions.IrConst | ||
import org.jetbrains.kotlin.ir.expressions.IrDeclarationReference | ||
import org.jetbrains.kotlin.ir.expressions.impl.IrCallImpl | ||
import org.jetbrains.kotlin.ir.symbols.IrClassSymbol | ||
import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol | ||
import org.jetbrains.kotlin.ir.types.isNullableAny | ||
import org.jetbrains.kotlin.name.CallableId | ||
import org.jetbrains.kotlin.name.ClassId | ||
import org.jetbrains.kotlin.name.FqName | ||
import org.jetbrains.kotlin.name.Name | ||
|
||
internal object InvestigateLogger { | ||
private var loggerSymbol: IrSimpleFunctionSymbol? = null | ||
private var loggerType: LoggerType? = null | ||
|
||
private var logTypeSymbol: IrClassSymbol? = null | ||
private var logTypeInvalidationProcessedSymbol: IrClassSymbol? = null | ||
private var logTypeInvalidationSkippedSymbol: IrClassSymbol? = null | ||
|
||
internal fun checkLoggerIsInstalled(): Boolean = loggerSymbol != null && loggerType != null | ||
|
||
internal fun useDefaultLogger(context: IrPluginContext) { | ||
val printlnSymbol: IrSimpleFunctionSymbol = | ||
context | ||
.referenceFunctions( | ||
CallableId( | ||
packageName = FqName("kotlin.io"), | ||
callableName = Name.identifier("println"), | ||
), | ||
) | ||
.single { symbol -> | ||
symbol.owner.valueParameters.size == 1 && | ||
symbol.owner.valueParameters.single().type.isNullableAny() | ||
} | ||
|
||
loggerSymbol = printlnSymbol | ||
loggerType = LoggerType.Println | ||
} | ||
|
||
internal fun useCustomLogger(symbol: IrSimpleFunctionSymbol) { | ||
loggerSymbol = symbol | ||
loggerType = LoggerType.Custom | ||
} | ||
|
||
internal fun makeIrCall( | ||
composableName: IrConst<String>, | ||
logType: IrDeclarationReference, | ||
originalMessage: IrConst<String>, | ||
): IrCall = | ||
IrCallImpl.fromSymbolOwner( | ||
startOffset = UNDEFINED_OFFSET, | ||
endOffset = UNDEFINED_OFFSET, | ||
symbol = loggerSymbol!!, | ||
).apply { | ||
when (loggerType!!) { | ||
LoggerType.Println -> { | ||
putValueArgument(0, originalMessage) | ||
} | ||
LoggerType.Custom -> { | ||
putValueArgument(0, composableName) | ||
putValueArgument(1, logType) | ||
} | ||
} | ||
} | ||
|
||
internal fun obtainLogTypeSymbol(context: IrPluginContext): IrClassSymbol { | ||
if (logTypeSymbol == null) { | ||
logTypeSymbol = context.referenceClass(ClassId.topLevel(COMPOSE_INVESTIGATE_LOG_TYPE_FQN)) | ||
} | ||
return logTypeSymbol!! | ||
} | ||
|
||
internal fun obtainLogTypeInvalidationProcessedSymbol(context: IrPluginContext): IrClassSymbol { | ||
if (logTypeInvalidationProcessedSymbol == null) { | ||
logTypeInvalidationProcessedSymbol = | ||
context.referenceClass(ClassId.topLevel(COMPOSE_INVESTIGATE_LOG_TYPE_INVALIDATION_PROCESSED_FQN)) | ||
} | ||
return logTypeInvalidationProcessedSymbol!! | ||
} | ||
|
||
internal fun obtainLogTypeInvalidationSkippedSymbol(context: IrPluginContext): IrClassSymbol { | ||
if (logTypeInvalidationSkippedSymbol == null) { | ||
logTypeInvalidationSkippedSymbol = | ||
context.referenceClass(ClassId.topLevel(COMPOSE_INVESTIGATE_LOG_TYPE_INVALIDATION_SKIPPED_FQN)) | ||
} | ||
return logTypeInvalidationSkippedSymbol!! | ||
} | ||
|
||
private enum class LoggerType { | ||
Println, | ||
Custom, | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
...lin/land/sungbin/composeinvestigator/compiler/internal/logger/InvestigateLoggerVisitor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Designed and developed by Ji Sungbin 2023. | ||
* | ||
* Licensed under the MIT. | ||
* Please see full license: https://github.com/jisungbin/ComposeInvestigator/blob/main/LICENSE | ||
*/ | ||
|
||
package land.sungbin.composeinvestigator.compiler.internal.logger | ||
|
||
import land.sungbin.composeinvestigator.compiler.internal.COMPOSABLE_FQN | ||
import land.sungbin.composeinvestigator.compiler.internal.COMPOSE_INVESTIGATE_LOGGER_FQN | ||
import land.sungbin.composeinvestigator.compiler.internal.COMPOSE_INVESTIGATE_LOG_TYPE_FQN | ||
import land.sungbin.composeinvestigator.compiler.internal.origin.InvestigateLoggerUsedFuntionOrigin | ||
import land.sungbin.composeinvestigator.compiler.util.VerboseLogger | ||
import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext | ||
import org.jetbrains.kotlin.ir.IrStatement | ||
import org.jetbrains.kotlin.ir.declarations.IrFunction | ||
import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction | ||
import org.jetbrains.kotlin.ir.types.classFqName | ||
import org.jetbrains.kotlin.ir.types.isString | ||
import org.jetbrains.kotlin.ir.types.isUnit | ||
import org.jetbrains.kotlin.ir.util.dump | ||
import org.jetbrains.kotlin.ir.util.hasAnnotation | ||
import org.jetbrains.kotlin.ir.util.isSuspend | ||
import org.jetbrains.kotlin.ir.util.isTopLevel | ||
import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid | ||
|
||
internal class InvestigateLoggerVisitor( | ||
private val context: IrPluginContext, | ||
private val logger: VerboseLogger, | ||
) : IrElementTransformerVoid(), IrPluginContext by context { | ||
override fun visitSimpleFunction(declaration: IrSimpleFunction): IrStatement { | ||
if (declaration.isValidComposeInvestigateLoggerFunction()) { | ||
if (declaration.origin == InvestigateLoggerUsedFuntionOrigin) return super.visitFunction(declaration) | ||
if (InvestigateLogger.checkLoggerIsInstalled()) { | ||
error( | ||
"More than one ComposeInvestigateLogger function was found. " + | ||
"Only one ComposeInvestigateLogger function is supported.", | ||
) | ||
} | ||
logger("Found ComposeInvestigateLogger function: ${declaration.dump()}") | ||
declaration.origin = InvestigateLoggerUsedFuntionOrigin | ||
InvestigateLogger.useCustomLogger(declaration.symbol) | ||
} | ||
return super.visitFunction(declaration) | ||
} | ||
|
||
// 1. has @ComposeInvestigateLogger and no @Composable | ||
// 2. top-level declaration | ||
// 3. public | ||
// 4. no extension receiver and no context receiver | ||
// 5. not suspend | ||
// 6. unit type | ||
// 7. has only two parameters: <String, LogType> | ||
private fun IrFunction.isValidComposeInvestigateLoggerFunction(): Boolean = | ||
hasAnnotation(COMPOSE_INVESTIGATE_LOGGER_FQN) && | ||
!hasAnnotation(COMPOSABLE_FQN) && | ||
isTopLevel && | ||
visibility.isPublicAPI && | ||
extensionReceiverParameter == null && | ||
contextReceiverParametersCount == 0 && | ||
!isSuspend && | ||
returnType.isUnit() && | ||
valueParameters.size == 2 && | ||
valueParameters[0].type.isString() && | ||
valueParameters[1].type.classFqName == COMPOSE_INVESTIGATE_LOG_TYPE_FQN | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
...ungbin/composeinvestigator/compiler/internal/origin/InvestigateLoggerUsedFuntionOrigin.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* | ||
* Designed and developed by Ji Sungbin 2023. | ||
* | ||
* Licensed under the MIT. | ||
* Please see full license: https://github.com/jisungbin/ComposeInvestigator/blob/main/LICENSE | ||
*/ | ||
|
||
package land.sungbin.composeinvestigator.compiler.internal.origin | ||
|
||
import org.jetbrains.kotlin.ir.declarations.IrDeclarationOriginImpl | ||
|
||
public data object InvestigateLoggerUsedFuntionOrigin : IrDeclarationOriginImpl("USED_BY_INVESTIGATE_LOGGER") |
Oops, something went wrong.