Don't reparse headers when collecting macro names

This commit is contained in:
Svyatoslav Scherbina
2019-01-26 15:24:15 +03:00
committed by SvyatoslavScherbina
parent 27a31167e6
commit f9346f5d5c
2 changed files with 21 additions and 36 deletions
@@ -913,7 +913,6 @@ internal class NativeIndexImpl(val library: NativeLibrary, val verbose: Boolean
fun buildNativeIndexImpl(library: NativeLibrary, verbose: Boolean): NativeIndex { fun buildNativeIndexImpl(library: NativeLibrary, verbose: Boolean): NativeIndex {
val result = NativeIndexImpl(library, verbose) val result = NativeIndexImpl(library, verbose)
indexDeclarations(result) indexDeclarations(result)
findMacros(result)
return result return result
} }
@@ -955,6 +954,8 @@ private fun indexDeclarations(nativeIndex: NativeIndexImpl) {
} }
CXChildVisitResult.CXChildVisit_Continue CXChildVisitResult.CXChildVisit_Continue
} }
findMacros(nativeIndex, translationUnit, headers)
} finally { } finally {
clang_disposeTranslationUnit(translationUnit) clang_disposeTranslationUnit(translationUnit)
} }
@@ -23,8 +23,8 @@ import java.io.File
/** /**
* Finds all "macro constants" and registers them as [NativeIndex.constants] in given index. * Finds all "macro constants" and registers them as [NativeIndex.constants] in given index.
*/ */
internal fun findMacros(nativeIndex: NativeIndexImpl) { internal fun findMacros(nativeIndex: NativeIndexImpl, translationUnit: CXTranslationUnit, headers: Set<CXFile?>) {
val names = collectMacroNames(nativeIndex) val names = collectMacroNames(nativeIndex, translationUnit, headers)
// TODO: apply user-defined filters. // TODO: apply user-defined filters.
val macros = expandMacros(nativeIndex.library, names, typeConverter = { nativeIndex.convertType(it) }) val macros = expandMacros(nativeIndex.library, names, typeConverter = { nativeIndex.convertType(it) })
@@ -226,42 +226,26 @@ enum class VisitorState {
EXPECT_END, INVALID EXPECT_END, INVALID
} }
private fun collectMacroNames(nativeIndex: NativeIndexImpl): List<String> { private fun collectMacroNames(nativeIndex: NativeIndexImpl, translationUnit: CXTranslationUnit, headers: Set<CXFile?>): List<String> {
val result = mutableSetOf<String>() val result = mutableSetOf<String>()
val index = clang_createIndex(excludeDeclarationsFromPCH = 0, displayDiagnostics = 0)!!
try {
// Include macros into AST:
val options = CXTranslationUnit_DetailedPreprocessingRecord
val translationUnit = nativeIndex.library.parse(index, options) visitChildren(translationUnit) { cursor, _ ->
try { val file = memScoped {
translationUnit.ensureNoCompileErrors() val fileVar = alloc<CXFileVar>()
val headers = getFilteredHeaders(nativeIndex, index, translationUnit) clang_getFileLocation(clang_getCursorLocation(cursor), fileVar.ptr, null, null, null)
fileVar.value
visitChildren(translationUnit) { cursor, _ ->
val file = memScoped {
val fileVar = alloc<CXFileVar>()
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)
} }
} 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() return result.toList()