diff --git a/chapi-ast-cpp/src/main/kotlin/chapi/ast/cppast/CPPBasicIdentListener.kt b/chapi-ast-cpp/src/main/kotlin/chapi/ast/cppast/CPPBasicIdentListener.kt index e3d021e8..a3c02332 100644 --- a/chapi-ast-cpp/src/main/kotlin/chapi/ast/cppast/CPPBasicIdentListener.kt +++ b/chapi-ast-cpp/src/main/kotlin/chapi/ast/cppast/CPPBasicIdentListener.kt @@ -9,6 +9,7 @@ import chapi.domain.core.CodeProperty class CPPBasicIdentListener(fileName: String) : CPP14ParserBaseListener() { private var codeContainer: CodeContainer = CodeContainer(FullName = fileName) + /// for example, Friend function, global function (`main`), etc. private var defaultNode = CodeDataStruct() private var currentFunction: CodeFunction? = null private var classes = mutableListOf() @@ -53,6 +54,12 @@ class CPPBasicIdentListener(fileName: String) : CPP14ParserBaseListener() { ctx?.classHead()?.let { currentNode = CodeDataStruct() currentNode?.NodeName = it.classHeadName()?.className()?.text ?: "" + + val extends = it.baseClause()?.baseSpecifierList()?.baseSpecifier()?.map { baseSpecifier -> + baseSpecifier.baseTypeSpecifier()?.classOrDeclType()?.className()?.text ?: "" + } ?: listOf() + + currentNode?.Implements = extends } ctx?.memberSpecification()?.memberdeclaration()?.let { diff --git a/chapi-ast-cpp/src/test/kotlin/chapi/ast/cppast/CPPBasicIdentListenerTest.kt b/chapi-ast-cpp/src/test/kotlin/chapi/ast/cppast/CPPBasicIdentListenerTest.kt index 8704cee0..d632d171 100644 --- a/chapi-ast-cpp/src/test/kotlin/chapi/ast/cppast/CPPBasicIdentListenerTest.kt +++ b/chapi-ast-cpp/src/test/kotlin/chapi/ast/cppast/CPPBasicIdentListenerTest.kt @@ -69,4 +69,25 @@ void display(char c, int n) { assertEquals(container.DataStructures.size, 1) assertEquals(container.DataStructures[0].NodeName, "Entity") } + + @Test + internal fun shouldIdentifyInheritance() { + val code = """ + class Entity + { + public: + int Id; + }; + + class AggregateRoot: public Entity + { + }; + """.trimIndent() + + val container = CPPAnalyser().analysis(code, "helloworld.cpp") + assertEquals(container.DataStructures.size, 2) + assertEquals(container.DataStructures[0].NodeName, "Entity") + assertEquals(container.DataStructures[1].NodeName, "AggregateRoot") + assertEquals(container.DataStructures[1].Implements[0], "Entity") + } }