Skip to content

Commit

Permalink
fix(c): fix imports issue #24
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Jan 30, 2024
1 parent 7feb7ca commit bd0e7ec
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 19 deletions.
27 changes: 16 additions & 11 deletions chapi-ast-c/src/main/antlr/C.g4
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?: "")
Expand Down Expand Up @@ -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 ?: ""
)

Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -237,10 +238,11 @@ typedef struct {
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "redismodule.h"
""".trimIndent()

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

@Test
Expand Down

0 comments on commit bd0e7ec

Please # to comment.