From f9346f5d5ca23f161d1830e012ef9b0d08318e01 Mon Sep 17 00:00:00 2001 From: Svyatoslav Scherbina Date: Sat, 26 Jan 2019 15:24:15 +0300 Subject: [PATCH] Don't reparse headers when collecting macro names --- .../kotlin/native/interop/indexer/Indexer.kt | 3 +- .../native/interop/indexer/MacroConstants.kt | 54 +++++++------------ 2 files changed, 21 insertions(+), 36 deletions(-) diff --git a/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/Indexer.kt b/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/Indexer.kt index 6e257129e64..04aa4ed4f9b 100644 --- a/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/Indexer.kt +++ b/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/Indexer.kt @@ -913,7 +913,6 @@ internal class NativeIndexImpl(val library: NativeLibrary, val verbose: Boolean fun buildNativeIndexImpl(library: NativeLibrary, verbose: Boolean): NativeIndex { val result = NativeIndexImpl(library, verbose) indexDeclarations(result) - findMacros(result) return result } @@ -955,6 +954,8 @@ private fun indexDeclarations(nativeIndex: NativeIndexImpl) { } CXChildVisitResult.CXChildVisit_Continue } + + findMacros(nativeIndex, translationUnit, headers) } finally { clang_disposeTranslationUnit(translationUnit) } diff --git a/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/MacroConstants.kt b/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/MacroConstants.kt index d55102f3a41..86da1fee452 100644 --- a/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/MacroConstants.kt +++ b/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/MacroConstants.kt @@ -23,8 +23,8 @@ import java.io.File /** * Finds all "macro constants" and registers them as [NativeIndex.constants] in given index. */ -internal fun findMacros(nativeIndex: NativeIndexImpl) { - val names = collectMacroNames(nativeIndex) +internal fun findMacros(nativeIndex: NativeIndexImpl, translationUnit: CXTranslationUnit, headers: Set) { + val names = collectMacroNames(nativeIndex, translationUnit, headers) // TODO: apply user-defined filters. val macros = expandMacros(nativeIndex.library, names, typeConverter = { nativeIndex.convertType(it) }) @@ -226,42 +226,26 @@ enum class VisitorState { EXPECT_END, INVALID } -private fun collectMacroNames(nativeIndex: NativeIndexImpl): List { +private fun collectMacroNames(nativeIndex: NativeIndexImpl, translationUnit: CXTranslationUnit, headers: Set): List { val result = mutableSetOf() - val index = clang_createIndex(excludeDeclarationsFromPCH = 0, displayDiagnostics = 0)!! - try { - // Include macros into AST: - val options = CXTranslationUnit_DetailedPreprocessingRecord - val translationUnit = nativeIndex.library.parse(index, options) - try { - translationUnit.ensureNoCompileErrors() - val headers = getFilteredHeaders(nativeIndex, index, translationUnit) - - visitChildren(translationUnit) { cursor, _ -> - val file = memScoped { - val fileVar = alloc() - clang_getFileLocation(clang_getCursorLocation(cursor), fileVar.ptr, null, null, null) - fileVar.value - } - - if (cursor.kind == CXCursorKind.CXCursor_MacroDefinition && - nativeIndex.library.includesDeclaration(cursor) && - file != null && // Builtin macros mostly seem to be useless. - file in headers && - canMacroBeConstant(cursor)) - { - val spelling = getCursorSpelling(cursor) - result.add(spelling) - } - CXChildVisitResult.CXChildVisit_Continue - } - - } finally { - clang_disposeTranslationUnit(translationUnit) + visitChildren(translationUnit) { cursor, _ -> + val file = memScoped { + val fileVar = alloc() + clang_getFileLocation(clang_getCursorLocation(cursor), fileVar.ptr, null, null, null) + fileVar.value } - } finally { - clang_disposeIndex(index) + + if (cursor.kind == CXCursorKind.CXCursor_MacroDefinition && + nativeIndex.library.includesDeclaration(cursor) && + file != null && // Builtin macros mostly seem to be useless. + file in headers && + canMacroBeConstant(cursor)) + { + val spelling = getCursorSpelling(cursor) + result.add(spelling) + } + CXChildVisitResult.CXChildVisit_Continue } return result.toList()