Fix performance bug, review feedback on indexer changes (#818)

This commit is contained in:
Nikolay Igotti
2017-08-29 16:12:44 +03:00
committed by GitHub
parent 2aa077e542
commit f74265126e
4 changed files with 37 additions and 25 deletions
@@ -128,7 +128,7 @@ internal class NativeIndexImpl(val library: NativeLibrary) : NativeIndex() {
if (structDecl.def == null) {
val definitionCursor = clang_getCursorDefinition(cursor)
if (clang_Cursor_isNull(definitionCursor) == 0) {
assert (clang_isCursorDefinition(definitionCursor) != 0)
assert(clang_isCursorDefinition(definitionCursor) != 0)
createStructDef(structDecl, cursor)
}
}
@@ -285,7 +285,8 @@ internal class NativeIndexImpl(val library: NativeLibrary) : NativeIndex() {
result.methods.add(method)
}
}
else -> {}
else -> {
}
}
CXChildVisitResult.CXChildVisit_Continue
}
@@ -365,7 +366,7 @@ internal class NativeIndexImpl(val library: NativeLibrary) : NativeIndex() {
}
private fun convertCursorType(cursor: CValue<CXCursor>) =
convertType(clang_getCursorType(cursor), clang_getDeclTypeAttributes(cursor))
convertType(clang_getCursorType(cursor), clang_getDeclTypeAttributes(cursor))
private inline fun objCType(supplier: () -> ObjCPointer) = when (library.language) {
Language.C -> UnsupportedType
@@ -483,7 +484,7 @@ internal class NativeIndexImpl(val library: NativeLibrary) : NativeIndex() {
private fun convertFunctionType(type: CValue<CXType>): Type {
val kind = type.kind
assert (kind == CXType_Unexposed || kind == CXType_FunctionProto)
assert(kind == CXType_Unexposed || kind == CXType_FunctionProto)
return if (clang_isFunctionTypeVariadic(type) != 0) {
VoidType // make this function pointer opaque.
@@ -500,27 +501,27 @@ internal class NativeIndexImpl(val library: NativeLibrary) : NativeIndex() {
private val TARGET_ATTRIBUTE = "__target__"
internal fun tokenizeExtent(cursor: CValue<CXCursor>): List<String> {
val translationUnit = clang_Cursor_getTranslationUnit(cursor)!!
val cursorExtent = clang_getCursorExtent(cursor)
memScoped {
val tokensVar = alloc<CPointerVar<CXToken>>()
val numTokensVar = alloc<IntVar>()
clang_tokenize(translationUnit, cursorExtent, tokensVar.ptr, numTokensVar.ptr)
val numTokens = numTokensVar.value
val tokens = tokensVar.value
if (tokens == null) return emptyList<String>()
try {
return (0 until numTokens).map {
clang_getTokenSpelling(translationUnit, tokens[it].readValue()).convertAndDispose()
val translationUnit = clang_Cursor_getTranslationUnit(cursor)!!
val cursorExtent = clang_getCursorExtent(cursor)
memScoped {
val tokensVar = alloc<CPointerVar<CXToken>>()
val numTokensVar = alloc<IntVar>()
clang_tokenize(translationUnit, cursorExtent, tokensVar.ptr, numTokensVar.ptr)
val numTokens = numTokensVar.value
val tokens = tokensVar.value
if (tokens == null) return emptyList<String>()
try {
return (0 until numTokens).map {
clang_getTokenSpelling(translationUnit, tokens[it].readValue()).convertAndDispose()
}
} finally {
clang_disposeTokens(translationUnit, tokens, numTokens)
}
} finally {
clang_disposeTokens(translationUnit, tokens, numTokens)
}
}
TODO()
TODO()
}
private fun isSuitable(cursor: CValue<CXCursor>): Boolean {
private fun isSuitableFunction(cursor: CValue<CXCursor>): Boolean {
if (!isAvailable(cursor)) return false
// If function is specific for certain target, ignore that, as we may be
@@ -566,7 +567,7 @@ internal class NativeIndexImpl(val library: NativeLibrary) : NativeIndex() {
}
CXIdxEntity_Function -> {
if (isSuitable(cursor)) {
if (isSuitableFunction(cursor)) {
functionById[getDeclarationId(cursor)] = getFunction(cursor)
}
}
@@ -216,6 +216,9 @@ internal class Context(config: KonanConfig) : KonanBackendContext(config) {
var phase: KonanPhase? = null
var depth: Int = 0
// Cache used for source offset->(line,column) mapping.
val fileEntryCache = mutableMapOf<String, SourceManager.FileEntry>()
protected fun separator(title: String) {
println("\n\n--- ${title} ----------------------\n")
}
@@ -93,9 +93,9 @@ class NaiveSourceBasedFileEntryImpl(override val name: String) : SourceManager.F
//-----------------------------------------------------------------------------//
class IrFileImpl(fileName: String) : IrFile {
class IrFileImpl(entry: SourceManager.FileEntry) : IrFile {
override val fileEntry = NaiveSourceBasedFileEntryImpl(fileName)
override val fileEntry = entry
//-------------------------------------------------------------------------//
@@ -30,6 +30,7 @@ import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.incremental.components.NoLookupLocation
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.IrStatement
import org.jetbrains.kotlin.ir.SourceManager
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.descriptors.IrBuiltinOperatorDescriptorBase
import org.jetbrains.kotlin.ir.expressions.*
@@ -1433,8 +1434,15 @@ internal class CodeGeneratorVisitor(val context: Context, val lifetimes: Map<IrE
}
//-------------------------------------------------------------------------//
fun getFileEntry(sourceFileName: String): SourceManager.FileEntry {
// We must cache file entries, otherwise we reparse same file many times.
return context.fileEntryCache.getOrPut(sourceFileName) {
NaiveSourceBasedFileEntryImpl(sourceFileName)
}
}
private inner class ReturnableBlockScope(val returnableBlock: IrReturnableBlockImpl) : FileScope(IrFileImpl(returnableBlock.sourceFileName)) {
private inner class ReturnableBlockScope(val returnableBlock: IrReturnableBlockImpl) :
FileScope(IrFileImpl(getFileEntry(returnableBlock.sourceFileName))) {
var bbExit : LLVMBasicBlockRef? = null
var resultPhi : LLVMValueRef? = null