From 6f160bbd08de6298f4c06c67a21f39d431337419 Mon Sep 17 00:00:00 2001 From: Phodal Huang Date: Thu, 1 Feb 2024 09:28:52 +0800 Subject: [PATCH] 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`. --- chapi-ast-c/src/main/antlr/C.g4 | 14 +++++++++++--- .../chapi/ast/cast/CFullIdentListenerTest.kt | 17 +++++++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/chapi-ast-c/src/main/antlr/C.g4 b/chapi-ast-c/src/main/antlr/C.g4 index 016894f9..7b2a3011 100644 --- a/chapi-ast-c/src/main/antlr/C.g4 +++ b/chapi-ast-c/src/main/antlr/C.g4 @@ -35,7 +35,7 @@ grammar C; compilationUnit // statement for macro support - : (externalDeclaration | statement)* EOF + : (externalDeclaration | statement | macroPostixCall)* EOF ; MultiLineMacro @@ -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)* ; @@ -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 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 a554e1db..e5603414 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 @@ -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) + } }