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.
*/
@file:Suppress("UNUSED_EXPRESSION")
@file:Suppress("UNUSED_EXPRESSION", "UNUSED_VARIABLE")
package clang
import kotlinx.cinterop.*
@@ -203,18 +203,17 @@ internal class NativeIndexImpl(val library: NativeLibrary) : NativeIndex() {
CXCursorKind.CXCursor_UnionDecl -> return false
CXCursorKind.CXCursor_StructDecl -> memScoped {
val hasAttributes = memScope.alloc<CInt32Var>()
hasAttributes.value = 0
clang_visitChildren(structDefCursor, staticCFunction { cursor, parent, clientData ->
if (clang_isAttribute(cursor.kind.value) != 0) {
val hasAttributes = clientData!!.reinterpret<CInt32Var>().pointed
hasAttributes.value = 1
CXCursorKind.CXCursor_StructDecl -> {
var hasAttributes = false
visitChildren(structDefCursor) { cursor, _ ->
if (clang_isAttribute(cursor.kind) != 0) {
hasAttributes = true
}
CXChildVisitResult.CXChildVisit_Continue
}, hasAttributes.ptr)
}
return hasAttributes.value == 0
return !hasAttributes
}
else -> throw IllegalArgumentException(defKind.toString())
@@ -332,6 +331,10 @@ internal class NativeIndexImpl(val library: NativeLibrary) : NativeIndex() {
CXIdxEntity_Enum -> {
getEnumDefAt(cursor)
}
else -> {
// Ignore declaration.
}
}
}
@@ -365,6 +368,7 @@ private fun indexDeclarations(library: NativeLibrary, nativeIndex: NativeIndexIm
importedASTFile.value = null
startedTranslationUnit.value = null
indexDeclaration.value = staticCFunction { clientData, info ->
@Suppress("NAME_SHADOWING")
val nativeIndex = StableObjPtr.fromValue(clientData!!).get() as NativeIndexImpl
nativeIndex.indexDeclaration(info!!.pointed)
}
@@ -139,7 +139,7 @@ private fun processCodeSnippet(
var longValue: Long? = null
var doubleValue: Double? = null
val visitor: CursorVisitor = { cursor, parent ->
val visitor: CursorVisitor = { cursor, _ ->
val kind = cursor.kind
when {
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()
try {
visitChildren(translationUnit) { cursor, parent ->
visitChildren(translationUnit) { cursor, _ ->
if (cursor.kind == CXCursorKind.CXCursor_MacroDefinition &&
library.includesDeclaration(cursor) &&
canMacroBeConstant(cursor))
@@ -117,6 +117,7 @@ internal fun visitChildren(parent: CValue<CXCursor>, visitor: CursorVisitor) {
val visitorPtr = StableObjPtr.create(visitor)
val clientData = visitorPtr.value
clang_visitChildren(parent, staticCFunction { cursor, parent, clientData ->
@Suppress("NAME_SHADOWING", "UNCHECKED_CAST")
val visitor = StableObjPtr.fromValue(clientData!!).get() as CursorVisitor
visitor(cursor.readValue(), parent.readValue())
}, clientData)
@@ -233,9 +233,10 @@ private fun ffiCreateCif(returnType: ffi_type, paramTypes: List<ffi_type>): ffi_
return interpretPointed(res)
}
@Suppress("UNUSED_PARAMETER")
private fun ffiFunImpl0(ffiCif: Long, ret: Long, args: Long, userData: Any) {
ffiFunImpl(interpretPointed(ffiCif),
interpretCPointer(ret)!!,
@Suppress("UNCHECKED_CAST")
ffiFunImpl(interpretCPointer(ret)!!,
interpretCPointer(args)!!,
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 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>,
userData: UserData) {
private fun ffiFunImpl(ret: COpaquePointer, args: CArrayPointer<COpaquePointerVar>, userData: UserData) {
userData.invoke(ret, args)
}
@@ -16,8 +16,8 @@
package kotlinx.cinterop
import kotlin.reflect.companionObjectInstance
import kotlin.reflect.primaryConstructor
import kotlin.reflect.full.companionObjectInstance
import kotlin.reflect.full.primaryConstructor
typealias NativePtr = Long
val nativeNullPtr: NativePtr = 0L
@@ -40,6 +40,7 @@ inline fun <reified T : NativePointed> interpretNullablePointed(ptr: NativePtr):
if (primaryConstructor == null) {
throw IllegalArgumentException("${kClass.simpleName} doesn't have a constructor")
}
@Suppress("UNCHECKED_CAST")
return (primaryConstructor as (NativePtr) -> T)(ptr)
}
}
@@ -237,26 +237,32 @@ abstract class CEnumVar : CPrimitiveVar()
// generics below are used for typedef support
// 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() {
companion object : Type(1)
}
@Suppress("FINAL_UPPER_BOUND")
class CInt16VarWithValueMappedTo<T : Short>(override val rawPtr: NativePtr) : CPrimitiveVar() {
companion object : Type(2)
}
@Suppress("FINAL_UPPER_BOUND")
class CInt32VarWithValueMappedTo<T : Int>(override val rawPtr: NativePtr) : CPrimitiveVar() {
companion object : Type(4)
}
@Suppress("FINAL_UPPER_BOUND")
class CInt64VarWithValueMappedTo<T : Long>(override val rawPtr: NativePtr) : CPrimitiveVar() {
companion object : Type(8)
}
@Suppress("FINAL_UPPER_BOUND")
class CFloat32VarWithValueMappedTo<T : Float>(override val rawPtr: NativePtr) : CPrimitiveVar() {
companion object : Type(4)
}
@Suppress("FINAL_UPPER_BOUND")
class CFloat64VarWithValueMappedTo<T : Double>(override val rawPtr: NativePtr) : CPrimitiveVar() {
companion object : Type(8)
}
@@ -268,28 +274,34 @@ typealias CInt64Var = CInt64VarWithValueMappedTo<Long>
typealias CFloat32Var = CFloat32VarWithValueMappedTo<Float>
typealias CFloat64Var = CFloat64VarWithValueMappedTo<Double>
@Suppress("FINAL_UPPER_BOUND", "UNCHECKED_CAST")
var <T : Byte> CInt8VarWithValueMappedTo<T>.value: T
get() = nativeMemUtils.getByte(this) as T
set(value) = nativeMemUtils.putByte(this, value)
@Suppress("FINAL_UPPER_BOUND", "UNCHECKED_CAST")
var <T : Short> CInt16VarWithValueMappedTo<T>.value: T
get() = nativeMemUtils.getShort(this) as T
set(value) = nativeMemUtils.putShort(this, value)
@Suppress("FINAL_UPPER_BOUND", "UNCHECKED_CAST")
var <T : Int> CInt32VarWithValueMappedTo<T>.value: T
get() = nativeMemUtils.getInt(this) as T
set(value) = nativeMemUtils.putInt(this, value)
@Suppress("FINAL_UPPER_BOUND", "UNCHECKED_CAST")
var <T : Long> CInt64VarWithValueMappedTo<T>.value: T
get() = nativeMemUtils.getLong(this) as T
set(value) = nativeMemUtils.putLong(this, value)
// TODO: ensure native floats have the appropriate binary representation
@Suppress("FINAL_UPPER_BOUND", "UNCHECKED_CAST")
var <T : Float> CFloat32VarWithValueMappedTo<T>.value: T
get() = nativeMemUtils.getFloat(this) as T
set(value) = nativeMemUtils.putFloat(this, value)
@Suppress("FINAL_UPPER_BOUND", "UNCHECKED_CAST")
var <T : Double> CFloat64VarWithValueMappedTo<T>.value: T
get() = nativeMemUtils.getDouble(this) as T
set(value) = nativeMemUtils.putDouble(this, value)
@@ -307,6 +319,7 @@ typealias CPointerVar<T> = CPointerVarWithValueMappedTo<CPointer<T>>
/**
* The value of this variable.
*/
@Suppress("UNCHECKED_CAST")
inline var <P : CPointer<*>> CPointerVarWithValueMappedTo<P>.value: P?
get() = interpretCPointer<CPointed>(nativeMemUtils.getNativePtr(this)) as P?
set(value) = nativeMemUtils.putNativePtr(this, value.rawValue)
@@ -1192,7 +1192,7 @@ class StubGenerator(
* Produces to [out] the contents of file with Kotlin bindings.
*/
fun generateKotlinFile() {
out("@file:Suppress(\"UNUSED_EXPRESSION\")")
out("@file:Suppress(\"UNUSED_EXPRESSION\", \"UNUSED_VARIABLE\")")
if (pkgName != "") {
out("package $pkgName")
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>) {
try {
val kClass = Class.forName("org.jetbrains.kotlin.konan.Helper0").kotlin
@Suppress("UNCHECKED_CAST")
val ctor = kClass.constructors.single() as KFunction<Runnable>
val result = ctor.call(dependenciesRoot, properties, dependencies)
result.run()
@@ -42,7 +42,10 @@ fun validateIrModule(context: BackendContext, irModule: IrModuleFragment) {
irModule.acceptVoid(visitor)
// TODO: investigate and re-enable
return
val ENABLE_EXTENDED_VALIDATION = false
if (!ENABLE_EXTENDED_VALIDATION) {
return
}
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.
*/
@Suppress("UNUSED_PARAMETER")
private fun IrExpression.uncheckedCast(type: KotlinType): IrExpression {
// TODO: apply some cast if types are incompatible; not required currently.
return this
@@ -106,6 +106,7 @@ private class InteropTransformer(val context: Context, val irFile: IrFile) : IrB
return alignOf(typeObject)
}
@Suppress("UNUSED_PARAMETER")
private fun IrBuilderWithScope.interpretPointed(expression: IrCall, type: KotlinType): IrExpression =
irCall(interop.interpretNullablePointed).apply {
putValueArgument(0, expression)
@@ -149,6 +150,7 @@ private class InteropTransformer(val context: Context, val irFile: IrFile) : IrB
}
}
@Suppress("UNUSED_PARAMETER")
private fun IrBuilderWithScope.nativePointedToCPointer(
expression: IrExpression, pointeeType: KotlinType
): IrExpression = irCall(interop.interpretCPointer).apply {