Skip to content

Commit

Permalink
feat(c): add support for parsing #include directives
Browse files Browse the repository at this point in the history
This commit adds support for parsing #include directives in C code. The `CAnalyser` class now uses a regular expression to extract the included file paths and stores them in the `includesDirective` property. This allows for better analysis of code dependencies. Additionally, a test case in `CFullIdentListenerTest` has been updated to reflect the new behavior.
  • Loading branch information
phodal committed Feb 3, 2024
1 parent 037695d commit e1cda08
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
6 changes: 6 additions & 0 deletions chapi-ast-c/src/main/kotlin/chapi/ast/cast/CAnalyser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@ open class CAnalyser : Analyser {
pp.addInput(LexerSource(InputStreamReader(code.byteInputStream()), true))
}

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

override fun analysis(code: String, filePath: String): CodeContainer {
includesDirective = importRegex.findAll(code).map {
it.groupValues[1]
}.toMutableList()

val output = preProcessing(code)

val context = this.parse(output).compilationUnit()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ static RedisModuleType *MemAllocType;
"""
val codeFile = CAnalyser().analysis(code, "helloworld.c")

assertEquals(codeFile.Imports.size, 0)
// assertEquals(codeFile.Imports[0].Source, "stdio.h")
assertEquals(codeFile.Imports.size, 1)
assertEquals(codeFile.Imports[0].Source, "stdio.h")
}

@Test
Expand Down Expand Up @@ -300,7 +300,7 @@ typedef struct {
""".trimIndent()

val codeFile = CAnalyser().analysis(code, "helloworld.c")
assertEquals(codeFile.Imports.size, 0)
assertEquals(codeFile.Imports.size, 6)
}

@Test
Expand Down

0 comments on commit e1cda08

Please sign in to comment.