From bd0e7eca66e2b1b5f52fbbd7caa40db9b79ce7bd Mon Sep 17 00:00:00 2001 From: Phodal Huang Date: Tue, 30 Jan 2024 17:17:54 +0800 Subject: [PATCH] fix(c): fix imports issue #24 --- chapi-ast-c/src/main/antlr/C.g4 | 27 +++++++++++-------- .../chapi/ast/cast/CFullIdentListener.kt | 6 ++--- .../chapi/ast/cast/CFullIdentListenerTest.kt | 12 +++++---- 3 files changed, 26 insertions(+), 19 deletions(-) diff --git a/chapi-ast-c/src/main/antlr/C.g4 b/chapi-ast-c/src/main/antlr/C.g4 index 6c47b10c..168e5a6a 100644 --- a/chapi-ast-c/src/main/antlr/C.g4 +++ b/chapi-ast-c/src/main/antlr/C.g4 @@ -34,15 +34,28 @@ grammar C; compilationUnit - : includeDeclaration* (externalDeclaration+)? EOF + : (includeDeclaration+)? (externalDeclaration+)? EOF ; includeDeclaration - : '#' Whitespace? 'include' Whitespace? (('"' includeIdentifier '"') | ('<' includeIdentifier '>' )) + : '#' include (StringLiteral | ('<' includeIdentifier '>' )) + | '#' Identifier expression* + ; + +MultiLineMacro + : '#' (~[\n]*? '\\' '\r'? '\n')+ ~ [\n]+ -> channel (HIDDEN) + ; + +//Directive +// : '#' ~ [\n]* -> channel (HIDDEN) +// ; + +include + : 'include' ; includeIdentifier - : Identifier (Dot Identifier+)? + : Identifier (Dot Identifier)? ; primaryExpression @@ -1088,14 +1101,6 @@ fragment SChar | '\\\n' // Added line | '\\\r\n' // Added line ; -// -//MultiLineMacro -// : '#' (~[\n]*? '\\' '\r'? '\n')+ ~ [\n]+ -> channel (HIDDEN) -// ; -// -//Directive -// : '#' ~ [\n]* -> channel (HIDDEN) -// ; // ignore the following asm blocks: /* diff --git a/chapi-ast-c/src/main/kotlin/chapi/ast/cast/CFullIdentListener.kt b/chapi-ast-c/src/main/kotlin/chapi/ast/cast/CFullIdentListener.kt index 09d14c97..3fe8acc1 100644 --- a/chapi-ast-c/src/main/kotlin/chapi/ast/cast/CFullIdentListener.kt +++ b/chapi-ast-c/src/main/kotlin/chapi/ast/cast/CFullIdentListener.kt @@ -41,7 +41,7 @@ open class CFullIdentListener(fileName: String) : CAstBaseListener() { structOrUnionSpecifier?.let { var nodeName = maybeNodeName ?: structOrUnionSpecifier.Identifier()?.text if (nodeName.isNullOrEmpty()) { - nodeName = structOrUnionSpecifier.structOrUnion().text + nodeName = structOrUnionSpecifier?.structOrUnion()?.text ?: "" } handleStructOrUnion(structOrUnionSpecifier, nodeName ?: "") @@ -75,7 +75,7 @@ open class CFullIdentListener(fileName: String) : CAstBaseListener() { structDeclCtx.specifierQualifierList()?.let { qualifierList -> val field = CodeField( - TypeType = qualifierList.typeSpecifier().text, + TypeType = qualifierList.typeSpecifier()?.text ?: "", TypeValue = qualifierList.specifierQualifierList()?.text ?: "" ) @@ -156,7 +156,7 @@ open class CFullIdentListener(fileName: String) : CAstBaseListener() { override fun enterFunctionDefinition(ctx: CParser.FunctionDefinitionContext?) { currentFunction = CodeFunction(Position = buildPosition(ctx)) ctx?.declarationSpecifier()?.map { - if (it.typeSpecifier().text != null) { + if (it.typeSpecifier()?.text != null) { if (it.typeSpecifier()?.typedefName() != null) { currentFunction.Name = it.typeSpecifier().typedefName().text } else { diff --git a/chapi-ast-c/src/test/kotlin/chapi/ast/cast/CFullIdentListenerTest.kt b/chapi-ast-c/src/test/kotlin/chapi/ast/cast/CFullIdentListenerTest.kt index 72d9483a..8b252d56 100644 --- a/chapi-ast-c/src/test/kotlin/chapi/ast/cast/CFullIdentListenerTest.kt +++ b/chapi-ast-c/src/test/kotlin/chapi/ast/cast/CFullIdentListenerTest.kt @@ -8,10 +8,11 @@ internal class CFullIdentListenerTest { @Test fun allGrammarUnderResources() { - val content = this::class.java.getResource("/grammar")!! - File(content.toURI()).walkTopDown().forEach { - if (it.isFile && it.extension == "c") { -// println("Analyse ${it.name}") + val content = this::class.java.getResource("/grammar")!!.toURI() +// val content = "/Users/phodal/Downloads/redis-unstable" + File(content).walkTopDown().forEach { + if (it.isFile && (it.extension == "c" || it.extension == "h")) { + println("Analyse ${it.path}") CAnalyser().analysis(it.readText(), it.name) } } @@ -237,10 +238,11 @@ typedef struct { #include #include #include + #include "redismodule.h" """.trimIndent() val codeFile = CAnalyser().analysis(code, "helloworld.c") - assertEquals(codeFile.Imports.size, 3) + assertEquals(codeFile.Imports.size, 4) } @Test