Skip to content

Commit

Permalink
feat(c): add support for macro postfix call #24
Browse files Browse the repository at this point in the history
Add support for macro postfix call in the C grammar by modifying the `compilationUnit` rule. This allows for macro calls to be recognized and parsed correctly. Also, include tests for macro calls in `CFullIdentListenerTest`.
  • Loading branch information
phodal committed Feb 1, 2024
1 parent 7f29459 commit 6f160bb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
14 changes: 11 additions & 3 deletions chapi-ast-c/src/main/antlr/C.g4
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ grammar C;

compilationUnit
// statement for macro support
: (externalDeclaration | statement)* EOF
: (externalDeclaration | statement | macroPostixCall)* EOF
;

MultiLineMacro
Expand Down Expand Up @@ -84,12 +84,17 @@ postfixExpression
extensionExpression : '__extension__'? '(' typeName ')' '{' initializerList ','? '}' ;

postixCall
:'[' (macroStatement expression)? expression ']' #arrayAccessPostfixExpression
:'[' (macroStatement expression)? expression ']' #arrayAccessPostfixExpression
// for macro support: ph_gen(, hpdata_age_heap, hpdata_t, age_link, hpdata_age_comp)
| '(' ','? argumentExpressionList? ')' #functionCallPostfixExpression
| ('.' | '->') Identifier #memberAccessPostfixExpression
;

macroPostixCall
: postixCall
| Identifier '(' statement* ')'
;

argumentExpressionList
: assignmentExpression (',' assignmentExpression)*
;
Expand Down Expand Up @@ -167,8 +172,11 @@ conditionalExpression
assignmentExpression
: conditionalExpression
| unaryExpression assignmentOperator assignmentExpression
| DigitSequence // for
| DigitSequence
// for support macro like: ph_gen(, hpdata_age_heap, &=)
| macroStatement
| assignmentOperator
| macroPostixCall
;

assignmentOperator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -611,4 +611,21 @@ typedef struct {
val codeFile = CAnalyser().analysis(code, "helloworld.c")
assertEquals(codeFile.DataStructures.size, 0)
}

@Test
fun shouldHandleMacroCall() {
val code = """
#define Protect(x) { L->savedpc = pc; {x;}; base = L->base; }
Protect(luaV_gettable(L, &g, rb, ra));
Protect(
if (!call_binTM(L, rb, luaO_nilobject, ra, TM_LEN))
luaG_typeerror(L, rb, "get length of");
)
""".trimIndent()

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

0 comments on commit 6f160bb

Please # to comment.