From 766d10fd4009bf404780d615b08fd197e6504769 Mon Sep 17 00:00:00 2001 From: Phodal Huang Date: Tue, 30 Jan 2024 10:27:56 +0800 Subject: [PATCH] feat(cpp): add support for identifying inheritance #24 Add a new test case to identify inheritance in CPPBasicIdentListenerTest. The test case checks if the code correctly identifies the base class and implements it in the derived class. --- .../chapi/ast/cppast/CPPBasicIdentListener.kt | 7 +++++++ .../ast/cppast/CPPBasicIdentListenerTest.kt | 21 +++++++++++++++++++ 2 files changed, 28 insertions(+) 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") + } }