Skip to content

Commit 6f160bb

Browse files
committed
feat(c): add support for macro postfix call #24
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`.
1 parent 7f29459 commit 6f160bb

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

chapi-ast-c/src/main/antlr/C.g4

+11-3
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ grammar C;
3535

3636
compilationUnit
3737
// statement for macro support
38-
: (externalDeclaration | statement)* EOF
38+
: (externalDeclaration | statement | macroPostixCall)* EOF
3939
;
4040

4141
MultiLineMacro
@@ -84,12 +84,17 @@ postfixExpression
8484
extensionExpression : '__extension__'? '(' typeName ')' '{' initializerList ','? '}' ;
8585

8686
postixCall
87-
:'[' (macroStatement expression)? expression ']' #arrayAccessPostfixExpression
87+
:'[' (macroStatement expression)? expression ']' #arrayAccessPostfixExpression
8888
// for macro support: ph_gen(, hpdata_age_heap, hpdata_t, age_link, hpdata_age_comp)
8989
| '(' ','? argumentExpressionList? ')' #functionCallPostfixExpression
9090
| ('.' | '->') Identifier #memberAccessPostfixExpression
9191
;
9292

93+
macroPostixCall
94+
: postixCall
95+
| Identifier '(' statement* ')'
96+
;
97+
9398
argumentExpressionList
9499
: assignmentExpression (',' assignmentExpression)*
95100
;
@@ -167,8 +172,11 @@ conditionalExpression
167172
assignmentExpression
168173
: conditionalExpression
169174
| unaryExpression assignmentOperator assignmentExpression
170-
| DigitSequence // for
175+
| DigitSequence
176+
// for support macro like: ph_gen(, hpdata_age_heap, &=)
171177
| macroStatement
178+
| assignmentOperator
179+
| macroPostixCall
172180
;
173181

174182
assignmentOperator

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

+17
Original file line numberDiff line numberDiff line change
@@ -611,4 +611,21 @@ typedef struct {
611611
val codeFile = CAnalyser().analysis(code, "helloworld.c")
612612
assertEquals(codeFile.DataStructures.size, 0)
613613
}
614+
615+
@Test
616+
fun shouldHandleMacroCall() {
617+
val code = """
618+
#define Protect(x) { L->savedpc = pc; {x;}; base = L->base; }
619+
620+
Protect(luaV_gettable(L, &g, rb, ra));
621+
622+
Protect(
623+
if (!call_binTM(L, rb, luaO_nilobject, ra, TM_LEN))
624+
luaG_typeerror(L, rb, "get length of");
625+
)
626+
""".trimIndent()
627+
628+
val codeFile = CAnalyser().analysis(code, "helloworld.c")
629+
assertEquals(codeFile.DataStructures.size, 0)
630+
}
614631
}

0 commit comments

Comments
 (0)