Skip to content

Commit

Permalink
refactor(chapi-ast-c): simplify macro handling in C.g4 #24
Browse files Browse the repository at this point in the history
Simplify the `compilationUnit` rule in C.g4 by removing the `singleLineMacroDeclaration` rule and introducing a new `macroStatement` rule. This improves readability and maintainability of the grammar file.
  • Loading branch information
phodal committed Jan 31, 2024
1 parent 8536913 commit dbbaa52
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 9 deletions.
25 changes: 16 additions & 9 deletions chapi-ast-c/src/main/antlr/C.g4
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,7 @@ grammar C;

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

singleLineMacroDeclaration
: '#' include (StringLiteral | ('<' includeIdentifier '>' )) #includeDeclaration
| '#' macroKeywords expression* (',' (expression | singleLineMacroDeclaration))* #defineDeclaration
| '#' '#'? Identifier #macroCastDeclaration
: (externalDeclaration | statement)* EOF
;

macroKeywords
Expand Down Expand Up @@ -438,15 +432,16 @@ typedefName

initializer
: assignmentExpression
| '{' initializerList ','? directDeclarator? '}'
| '{' initializerList ','? '}'
;

initializerList
: designation? initializer (',' designation? initializer)*
: designation? initializer (',' designation? initializer macroStatement?)*
;

designation
: designatorList '='
| directDeclarator
;

designatorList
Expand All @@ -472,6 +467,18 @@ statement
| ('__asm' | '__asm__') ('volatile' | '__volatile__') '(' (
logicalOrExpression (',' logicalOrExpression)*
)? (':' (logicalOrExpression (',' logicalOrExpression)*)?)* ')' ';'
| macroStatement
;

macroStatement
: singleLineMacroDeclaration
;


singleLineMacroDeclaration
: '#' include (StringLiteral | ('<' includeIdentifier '>' )) #includeDeclaration
| '#' macroKeywords expression* (',' (expression | singleLineMacroDeclaration))* #defineDeclaration
| '#' '#'? Identifier #macroCastDeclaration
;

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

@Test
fun shouldHandleMacroInFunc() {
val code = """
static const ctl_named_node_t stats_arenas_i_mutexes_node[] = {
#define OP(mtx) {NAME(#mtx), CHILD(named, stats_arenas_i_mutexes_##mtx)},
MUTEX_PROF_ARENA_MUTEXES
#undef OP
};
void os_pages_unmap(void *addr, size_t size) {
assert(ALIGNMENT_ADDR2BASE(addr, os_page) == addr);
assert(ALIGNMENT_CEILING(size, os_page) == size);
#ifdef _WIN32
if (VirtualFree(addr, 0, MEM_RELEASE) == 0)
#else
if (munmap(addr, size) == -1)
#endif
{
char buf[BUFERROR_BUF];
buferror(get_errno(), buf, sizeof(buf));
malloc_printf("<jemalloc>: Error in "
#ifdef _WIN32
"VirtualFree"
#else
"munmap"
#endif
"(): %s\n", buf);
if (opt_abort) {
abort();
}
}
}
""".trimIndent()

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

0 comments on commit dbbaa52

Please sign in to comment.