Skip to content

Commit

Permalink
feat(cppast): add support for identifying namespaces
Browse files Browse the repository at this point in the history
This commit adds support for identifying namespaces in the CPPBasicIdentListenerTest. A new test case `shouldIdentifyNameSpace` is added to test the identification of namespaces. The `CPPBasicIdentListener` is modified to handle the `enterNamespaceDefinition` event and extract the namespace name from the context. The extracted namespace name is then assigned to the `PackageName` property of the `CodeContainer` class.
  • Loading branch information
phodal committed Jan 30, 2024
1 parent 2728231 commit d382bdd
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ class CPPBasicIdentListener(fileName: String) : CPP14ParserBaseListener() {
private var classes = mutableListOf<CodeDataStruct>()
private var currentNode: CodeDataStruct? = null

override fun enterNamespaceDefinition(ctx: CPP14Parser.NamespaceDefinitionContext?) {
ctx?.Identifier()?.let {
codeContainer.PackageName = it.text
}
}

override fun enterFunctionDefinition(ctx: CPP14Parser.FunctionDefinitionContext?) {
val method = CodeFunction()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ void display(char c, int n) {
}

@Test
internal fun shouldIdentifyFunction() {
internal fun shouldIdentifyClassFunction() {
val code = """
class EntityB: public Entity
{
Expand All @@ -112,4 +112,23 @@ void display(char c, int n) {
assertEquals(container.DataStructures[0].NodeName, "EntityB")
assertEquals(container.DataStructures[0].Functions[0].Name, "init")
}

@Test
internal fun shouldIdentifyNameSpace() {
val code = """
namespace NS
{
class M
{
friend class F; // Introduces F but doesn't define it
};
}
""".trimIndent()

val container = CPPAnalyser().analysis(code, "helloworld.cpp")
assertEquals(container.PackageName, "NS")
assertEquals(container.DataStructures.size, 1)
assertEquals(container.DataStructures[0].NodeName, "M")
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import kotlinx.serialization.Serializable
data class CodeContainer(
var FullName: String = "",
/**
* In Rust, the package name is the module name.
* In C++, C#, the namespace is the package name.
* In toml, the package name is the table name.
*/
var PackageName: String = "",
Expand Down

0 comments on commit d382bdd

Please sign in to comment.