Skip to content

Commit 3956222

Browse files
committed
feat(chapi-ast-cpp): add support for importing C++ headers
This commit adds support for importing C++ headers in the CPPBasicIdentListener class. The includes are extracted from the code using a regular expression and added to the CodeContainer as CodeImport objects. This allows for better analysis of C++ code that relies on external headers.
1 parent 6f772ae commit 3956222

4 files changed

Lines changed: 28 additions & 3 deletions

File tree

chapi-ast-c/src/test/kotlin/chapi/ast/cast/CFullIdentListenerTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ typedef struct {
304304
}
305305

306306
@Test
307-
fun readlWorld() {
307+
fun shouldSuccessHandleDs() {
308308
val code = """
309309
// https://stackoverflow.com/questions/12642830/can-i-define-a-function-inside-a-c-structure
310310
#include <stdio.h>

chapi-ast-cpp/src/main/kotlin/chapi/ast/cppast/CPPAnalyser.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,16 @@ open class CPPAnalyser: Analyser {
4545
return output
4646
}
4747

48+
private val importRegex = Regex("""#include\s+(<[^>]+>|\"[^\"]+\")""")
4849

4950
override fun analysis(code: String, filePath: String): CodeContainer {
5051
val processedCode = preProcessing(code)
52+
val includesDirective = importRegex.findAll(code).map {
53+
it.groupValues[1]
54+
}.toMutableList()
55+
5156
val context = this.parse(processedCode).translationUnit()
52-
val listener = CPPBasicIdentListener(fileName = filePath)
57+
val listener = CPPBasicIdentListener(fileName = filePath, includesDirective)
5358

5459
ParseTreeWalker().walk(listener, context)
5560

chapi-ast-cpp/src/main/kotlin/chapi/ast/cppast/CPPBasicIdentListener.kt

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,30 @@ import chapi.ast.antlr.CPP14Parser
44
import chapi.ast.antlr.CPP14ParserBaseListener
55
import chapi.domain.core.*
66

7-
class CPPBasicIdentListener(fileName: String) : CPP14ParserBaseListener() {
7+
class CPPBasicIdentListener(fileName: String, includes: MutableList<String>) : CPP14ParserBaseListener() {
88
private var codeContainer: CodeContainer = CodeContainer(FullName = fileName)
99
/// for example, Friend function, global function (`main`), etc.
1010
private var defaultNode = CodeDataStruct()
1111
private var currentFunction: CodeFunction? = null
1212
private var classes = mutableListOf<CodeDataStruct>()
1313
private var currentNode: CodeDataStruct? = null
1414

15+
init {
16+
includes.forEach {
17+
val value = it
18+
.removeSurrounding("\"", "\"")
19+
.removeSurrounding("<", ">")
20+
21+
val imp = CodeImport(
22+
Source = value,
23+
AsName = value
24+
)
25+
26+
codeContainer.Imports += imp
27+
}
28+
29+
}
30+
1531
override fun enterNamespaceDefinition(ctx: CPP14Parser.NamespaceDefinitionContext?) {
1632
ctx?.Identifier()?.let {
1733
codeContainer.PackageName = it.text

chapi-ast-cpp/src/test/kotlin/chapi/ast/cppast/CPPBasicIdentListenerTest.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ int main(){}
3131
"""
3232
val container = CPPAnalyser().analysis(code, "helloworld.cpp")
3333
assertEquals(container.DataStructures[0].Functions[0].ReturnType, "int")
34+
35+
// imports
36+
assertEquals(container.Imports.size, 1)
37+
assertEquals(container.Imports[0].Source, "iostream")
3438
}
3539

3640
@Test

0 commit comments

Comments
 (0)