Skip to content

Commit

Permalink
feat(cpp): add support for identifying inheritance #24
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
phodal committed Jan 30, 2024
1 parent 5a8d120 commit 766d10f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<CodeDataStruct>()
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

0 comments on commit 766d10f

Please # to comment.