Eliminate some warnings from Kotlin code

This commit is contained in:
Svyatoslav Scherbina
2017-03-30 11:24:23 +03:00
committed by SvyatoslavScherbina
parent 760de3cdad
commit 4a1b2721d8
12 changed files with 46 additions and 20 deletions
@@ -14,7 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
@file:Suppress("UNUSED_EXPRESSION") @file:Suppress("UNUSED_EXPRESSION", "UNUSED_VARIABLE")
package clang package clang
import kotlinx.cinterop.* import kotlinx.cinterop.*
@@ -203,18 +203,17 @@ internal class NativeIndexImpl(val library: NativeLibrary) : NativeIndex() {
CXCursorKind.CXCursor_UnionDecl -> return false CXCursorKind.CXCursor_UnionDecl -> return false
CXCursorKind.CXCursor_StructDecl -> memScoped { CXCursorKind.CXCursor_StructDecl -> {
val hasAttributes = memScope.alloc<CInt32Var>() var hasAttributes = false
hasAttributes.value = 0
clang_visitChildren(structDefCursor, staticCFunction { cursor, parent, clientData -> visitChildren(structDefCursor) { cursor, _ ->
if (clang_isAttribute(cursor.kind.value) != 0) { if (clang_isAttribute(cursor.kind) != 0) {
val hasAttributes = clientData!!.reinterpret<CInt32Var>().pointed hasAttributes = true
hasAttributes.value = 1
} }
CXChildVisitResult.CXChildVisit_Continue CXChildVisitResult.CXChildVisit_Continue
}, hasAttributes.ptr) }
return hasAttributes.value == 0 return !hasAttributes
} }
else -> throw IllegalArgumentException(defKind.toString()) else -> throw IllegalArgumentException(defKind.toString())
@@ -332,6 +331,10 @@ internal class NativeIndexImpl(val library: NativeLibrary) : NativeIndex() {
CXIdxEntity_Enum -> { CXIdxEntity_Enum -> {
getEnumDefAt(cursor) getEnumDefAt(cursor)
} }
else -> {
// Ignore declaration.
}
} }
} }
@@ -365,6 +368,7 @@ private fun indexDeclarations(library: NativeLibrary, nativeIndex: NativeIndexIm
importedASTFile.value = null importedASTFile.value = null
startedTranslationUnit.value = null startedTranslationUnit.value = null
indexDeclaration.value = staticCFunction { clientData, info -> indexDeclaration.value = staticCFunction { clientData, info ->
@Suppress("NAME_SHADOWING")
val nativeIndex = StableObjPtr.fromValue(clientData!!).get() as NativeIndexImpl val nativeIndex = StableObjPtr.fromValue(clientData!!).get() as NativeIndexImpl
nativeIndex.indexDeclaration(info!!.pointed) nativeIndex.indexDeclaration(info!!.pointed)
} }
@@ -139,7 +139,7 @@ private fun processCodeSnippet(
var longValue: Long? = null var longValue: Long? = null
var doubleValue: Double? = null var doubleValue: Double? = null
val visitor: CursorVisitor = { cursor, parent -> val visitor: CursorVisitor = { cursor, _ ->
val kind = cursor.kind val kind = cursor.kind
when { when {
state == VisitorState.EXPECT_VARIABLE && kind == CXCursorKind.CXCursor_VarDecl -> { state == VisitorState.EXPECT_VARIABLE && kind == CXCursorKind.CXCursor_VarDecl -> {
@@ -216,7 +216,7 @@ private fun collectMacroConstantsNames(library: NativeLibrary): List<String> {
val translationUnit = library.parse(index, options).ensureNoCompileErrors() val translationUnit = library.parse(index, options).ensureNoCompileErrors()
try { try {
visitChildren(translationUnit) { cursor, parent -> visitChildren(translationUnit) { cursor, _ ->
if (cursor.kind == CXCursorKind.CXCursor_MacroDefinition && if (cursor.kind == CXCursorKind.CXCursor_MacroDefinition &&
library.includesDeclaration(cursor) && library.includesDeclaration(cursor) &&
canMacroBeConstant(cursor)) canMacroBeConstant(cursor))
@@ -117,6 +117,7 @@ internal fun visitChildren(parent: CValue<CXCursor>, visitor: CursorVisitor) {
val visitorPtr = StableObjPtr.create(visitor) val visitorPtr = StableObjPtr.create(visitor)
val clientData = visitorPtr.value val clientData = visitorPtr.value
clang_visitChildren(parent, staticCFunction { cursor, parent, clientData -> clang_visitChildren(parent, staticCFunction { cursor, parent, clientData ->
@Suppress("NAME_SHADOWING", "UNCHECKED_CAST")
val visitor = StableObjPtr.fromValue(clientData!!).get() as CursorVisitor val visitor = StableObjPtr.fromValue(clientData!!).get() as CursorVisitor
visitor(cursor.readValue(), parent.readValue()) visitor(cursor.readValue(), parent.readValue())
}, clientData) }, clientData)
@@ -233,9 +233,10 @@ private fun ffiCreateCif(returnType: ffi_type, paramTypes: List<ffi_type>): ffi_
return interpretPointed(res) return interpretPointed(res)
} }
@Suppress("UNUSED_PARAMETER")
private fun ffiFunImpl0(ffiCif: Long, ret: Long, args: Long, userData: Any) { private fun ffiFunImpl0(ffiCif: Long, ret: Long, args: Long, userData: Any) {
ffiFunImpl(interpretPointed(ffiCif), @Suppress("UNCHECKED_CAST")
interpretCPointer(ret)!!, ffiFunImpl(interpretCPointer(ret)!!,
interpretCPointer(args)!!, interpretCPointer(args)!!,
userData as UserData) userData as UserData)
} }
@@ -246,8 +247,7 @@ private fun ffiFunImpl0(ffiCif: Long, ret: Long, args: Long, userData: Any) {
* @param ret pointer to memory to be filled with return value of the invoked native function * @param ret pointer to memory to be filled with return value of the invoked native function
* @param args pointer to array of pointers to arguments passed to the invoked native function * @param args pointer to array of pointers to arguments passed to the invoked native function
*/ */
private fun ffiFunImpl(ffiCif: ffi_cif, ret: COpaquePointer, args: CArrayPointer<COpaquePointerVar>, private fun ffiFunImpl(ret: COpaquePointer, args: CArrayPointer<COpaquePointerVar>, userData: UserData) {
userData: UserData) {
userData.invoke(ret, args) userData.invoke(ret, args)
} }
@@ -16,8 +16,8 @@
package kotlinx.cinterop package kotlinx.cinterop
import kotlin.reflect.companionObjectInstance import kotlin.reflect.full.companionObjectInstance
import kotlin.reflect.primaryConstructor import kotlin.reflect.full.primaryConstructor
typealias NativePtr = Long typealias NativePtr = Long
val nativeNullPtr: NativePtr = 0L val nativeNullPtr: NativePtr = 0L
@@ -40,6 +40,7 @@ inline fun <reified T : NativePointed> interpretNullablePointed(ptr: NativePtr):
if (primaryConstructor == null) { if (primaryConstructor == null) {
throw IllegalArgumentException("${kClass.simpleName} doesn't have a constructor") throw IllegalArgumentException("${kClass.simpleName} doesn't have a constructor")
} }
@Suppress("UNCHECKED_CAST")
return (primaryConstructor as (NativePtr) -> T)(ptr) return (primaryConstructor as (NativePtr) -> T)(ptr)
} }
} }
@@ -237,26 +237,32 @@ abstract class CEnumVar : CPrimitiveVar()
// generics below are used for typedef support // generics below are used for typedef support
// these classes are not supposed to be used directly, instead the typealiases are provided. // these classes are not supposed to be used directly, instead the typealiases are provided.
@Suppress("FINAL_UPPER_BOUND")
class CInt8VarWithValueMappedTo<T : Byte>(override val rawPtr: NativePtr) : CPrimitiveVar() { class CInt8VarWithValueMappedTo<T : Byte>(override val rawPtr: NativePtr) : CPrimitiveVar() {
companion object : Type(1) companion object : Type(1)
} }
@Suppress("FINAL_UPPER_BOUND")
class CInt16VarWithValueMappedTo<T : Short>(override val rawPtr: NativePtr) : CPrimitiveVar() { class CInt16VarWithValueMappedTo<T : Short>(override val rawPtr: NativePtr) : CPrimitiveVar() {
companion object : Type(2) companion object : Type(2)
} }
@Suppress("FINAL_UPPER_BOUND")
class CInt32VarWithValueMappedTo<T : Int>(override val rawPtr: NativePtr) : CPrimitiveVar() { class CInt32VarWithValueMappedTo<T : Int>(override val rawPtr: NativePtr) : CPrimitiveVar() {
companion object : Type(4) companion object : Type(4)
} }
@Suppress("FINAL_UPPER_BOUND")
class CInt64VarWithValueMappedTo<T : Long>(override val rawPtr: NativePtr) : CPrimitiveVar() { class CInt64VarWithValueMappedTo<T : Long>(override val rawPtr: NativePtr) : CPrimitiveVar() {
companion object : Type(8) companion object : Type(8)
} }
@Suppress("FINAL_UPPER_BOUND")
class CFloat32VarWithValueMappedTo<T : Float>(override val rawPtr: NativePtr) : CPrimitiveVar() { class CFloat32VarWithValueMappedTo<T : Float>(override val rawPtr: NativePtr) : CPrimitiveVar() {
companion object : Type(4) companion object : Type(4)
} }
@Suppress("FINAL_UPPER_BOUND")
class CFloat64VarWithValueMappedTo<T : Double>(override val rawPtr: NativePtr) : CPrimitiveVar() { class CFloat64VarWithValueMappedTo<T : Double>(override val rawPtr: NativePtr) : CPrimitiveVar() {
companion object : Type(8) companion object : Type(8)
} }
@@ -268,28 +274,34 @@ typealias CInt64Var = CInt64VarWithValueMappedTo<Long>
typealias CFloat32Var = CFloat32VarWithValueMappedTo<Float> typealias CFloat32Var = CFloat32VarWithValueMappedTo<Float>
typealias CFloat64Var = CFloat64VarWithValueMappedTo<Double> typealias CFloat64Var = CFloat64VarWithValueMappedTo<Double>
@Suppress("FINAL_UPPER_BOUND", "UNCHECKED_CAST")
var <T : Byte> CInt8VarWithValueMappedTo<T>.value: T var <T : Byte> CInt8VarWithValueMappedTo<T>.value: T
get() = nativeMemUtils.getByte(this) as T get() = nativeMemUtils.getByte(this) as T
set(value) = nativeMemUtils.putByte(this, value) set(value) = nativeMemUtils.putByte(this, value)
@Suppress("FINAL_UPPER_BOUND", "UNCHECKED_CAST")
var <T : Short> CInt16VarWithValueMappedTo<T>.value: T var <T : Short> CInt16VarWithValueMappedTo<T>.value: T
get() = nativeMemUtils.getShort(this) as T get() = nativeMemUtils.getShort(this) as T
set(value) = nativeMemUtils.putShort(this, value) set(value) = nativeMemUtils.putShort(this, value)
@Suppress("FINAL_UPPER_BOUND", "UNCHECKED_CAST")
var <T : Int> CInt32VarWithValueMappedTo<T>.value: T var <T : Int> CInt32VarWithValueMappedTo<T>.value: T
get() = nativeMemUtils.getInt(this) as T get() = nativeMemUtils.getInt(this) as T
set(value) = nativeMemUtils.putInt(this, value) set(value) = nativeMemUtils.putInt(this, value)
@Suppress("FINAL_UPPER_BOUND", "UNCHECKED_CAST")
var <T : Long> CInt64VarWithValueMappedTo<T>.value: T var <T : Long> CInt64VarWithValueMappedTo<T>.value: T
get() = nativeMemUtils.getLong(this) as T get() = nativeMemUtils.getLong(this) as T
set(value) = nativeMemUtils.putLong(this, value) set(value) = nativeMemUtils.putLong(this, value)
// TODO: ensure native floats have the appropriate binary representation // TODO: ensure native floats have the appropriate binary representation
@Suppress("FINAL_UPPER_BOUND", "UNCHECKED_CAST")
var <T : Float> CFloat32VarWithValueMappedTo<T>.value: T var <T : Float> CFloat32VarWithValueMappedTo<T>.value: T
get() = nativeMemUtils.getFloat(this) as T get() = nativeMemUtils.getFloat(this) as T
set(value) = nativeMemUtils.putFloat(this, value) set(value) = nativeMemUtils.putFloat(this, value)
@Suppress("FINAL_UPPER_BOUND", "UNCHECKED_CAST")
var <T : Double> CFloat64VarWithValueMappedTo<T>.value: T var <T : Double> CFloat64VarWithValueMappedTo<T>.value: T
get() = nativeMemUtils.getDouble(this) as T get() = nativeMemUtils.getDouble(this) as T
set(value) = nativeMemUtils.putDouble(this, value) set(value) = nativeMemUtils.putDouble(this, value)
@@ -307,6 +319,7 @@ typealias CPointerVar<T> = CPointerVarWithValueMappedTo<CPointer<T>>
/** /**
* The value of this variable. * The value of this variable.
*/ */
@Suppress("UNCHECKED_CAST")
inline var <P : CPointer<*>> CPointerVarWithValueMappedTo<P>.value: P? inline var <P : CPointer<*>> CPointerVarWithValueMappedTo<P>.value: P?
get() = interpretCPointer<CPointed>(nativeMemUtils.getNativePtr(this)) as P? get() = interpretCPointer<CPointed>(nativeMemUtils.getNativePtr(this)) as P?
set(value) = nativeMemUtils.putNativePtr(this, value.rawValue) set(value) = nativeMemUtils.putNativePtr(this, value.rawValue)
@@ -1192,7 +1192,7 @@ class StubGenerator(
* Produces to [out] the contents of file with Kotlin bindings. * Produces to [out] the contents of file with Kotlin bindings.
*/ */
fun generateKotlinFile() { fun generateKotlinFile() {
out("@file:Suppress(\"UNUSED_EXPRESSION\")") out("@file:Suppress(\"UNUSED_EXPRESSION\", \"UNUSED_VARIABLE\")")
if (pkgName != "") { if (pkgName != "") {
out("package $pkgName") out("package $pkgName")
out("") out("")
@@ -136,6 +136,7 @@ private fun runCmd(command: Array<String>, workDir: File, verbose: Boolean = fal
private fun maybeExecuteHelper(dependenciesRoot: String, properties: Properties, dependencies: List<String>) { private fun maybeExecuteHelper(dependenciesRoot: String, properties: Properties, dependencies: List<String>) {
try { try {
val kClass = Class.forName("org.jetbrains.kotlin.konan.Helper0").kotlin val kClass = Class.forName("org.jetbrains.kotlin.konan.Helper0").kotlin
@Suppress("UNCHECKED_CAST")
val ctor = kClass.constructors.single() as KFunction<Runnable> val ctor = kClass.constructors.single() as KFunction<Runnable>
val result = ctor.call(dependenciesRoot, properties, dependencies) val result = ctor.call(dependenciesRoot, properties, dependencies)
result.run() result.run()
@@ -42,7 +42,10 @@ fun validateIrModule(context: BackendContext, irModule: IrModuleFragment) {
irModule.acceptVoid(visitor) irModule.acceptVoid(visitor)
// TODO: investigate and re-enable // TODO: investigate and re-enable
return val ENABLE_EXTENDED_VALIDATION = false
if (!ENABLE_EXTENDED_VALIDATION) {
return
}
val moduleDeclarations = visitor.foundDeclarations val moduleDeclarations = visitor.foundDeclarations
@@ -188,6 +188,7 @@ private class AutoboxingTransformer(val context: Context) : AbstractValueUsageTr
/** /**
* Casts this expression to `type` without changing its representation in generated code. * Casts this expression to `type` without changing its representation in generated code.
*/ */
@Suppress("UNUSED_PARAMETER")
private fun IrExpression.uncheckedCast(type: KotlinType): IrExpression { private fun IrExpression.uncheckedCast(type: KotlinType): IrExpression {
// TODO: apply some cast if types are incompatible; not required currently. // TODO: apply some cast if types are incompatible; not required currently.
return this return this
@@ -106,6 +106,7 @@ private class InteropTransformer(val context: Context, val irFile: IrFile) : IrB
return alignOf(typeObject) return alignOf(typeObject)
} }
@Suppress("UNUSED_PARAMETER")
private fun IrBuilderWithScope.interpretPointed(expression: IrCall, type: KotlinType): IrExpression = private fun IrBuilderWithScope.interpretPointed(expression: IrCall, type: KotlinType): IrExpression =
irCall(interop.interpretNullablePointed).apply { irCall(interop.interpretNullablePointed).apply {
putValueArgument(0, expression) putValueArgument(0, expression)
@@ -149,6 +150,7 @@ private class InteropTransformer(val context: Context, val irFile: IrFile) : IrB
} }
} }
@Suppress("UNUSED_PARAMETER")
private fun IrBuilderWithScope.nativePointedToCPointer( private fun IrBuilderWithScope.nativePointedToCPointer(
expression: IrExpression, pointeeType: KotlinType expression: IrExpression, pointeeType: KotlinType
): IrExpression = irCall(interop.interpretCPointer).apply { ): IrExpression = irCall(interop.interpretCPointer).apply {