Skip to content

Commit

Permalink
feat(c): add support for single line macro declarations #24
Browse files Browse the repository at this point in the history
This commit adds support for single line macro declarations in the C grammar. It also includes a test case for handling macros with broken conditions.
  • Loading branch information
phodal committed Jan 31, 2024
1 parent dbbaa52 commit ff26513
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
9 changes: 5 additions & 4 deletions chapi-ast-c/src/main/antlr/C.g4
Original file line number Diff line number Diff line change
Expand Up @@ -342,9 +342,7 @@ directDeclarator
| '(' typeSpecifier? pointer directDeclarator ')' #functionPointerDirectDeclarator // function pointer like: (__cdecl *f)
//singleLineMacroDeclaration
// #define KUMAX(x) ((uintmax_t)x##ULL)
| '#' '#'? macroKeywords? expression #defineDirectDeclarator
// #define KUMAX(x) ((uintmax_t)x##ULL)
// | '#' Identifier #macroCastDeclarator
| singleLineMacroDeclaration #singleLineMacroDirectDeclarator
;

vcSpecificModifer
Expand Down Expand Up @@ -474,7 +472,6 @@ macroStatement
: singleLineMacroDeclaration
;


singleLineMacroDeclaration
: '#' include (StringLiteral | ('<' includeIdentifier '>' )) #includeDeclaration
| '#' macroKeywords expression* (',' (expression | singleLineMacroDeclaration))* #defineDeclaration
Expand Down Expand Up @@ -973,6 +970,10 @@ fragment IdentifierNondigit
//| // other implementation-defined characters...
;

fragment UpperCase
: [A-Z_]
;

fragment Nondigit
: [a-zA-Z_]
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -491,4 +491,47 @@ typedef struct {
val codeFile = CAnalyser().analysis(code, "helloworld.c")
assertEquals(codeFile.DataStructures.size, 1)
}

@Test
fun shouldHandleForMacroForBrokenCondition() {
val code = """
static const ctl_named_node_t stats_mutexes_node[] = {
#define OP(mtx) {NAME(#mtx), CHILD(named, stats_mutexes_##mtx)},
MUTEX_PROF_GLOBAL_MUTEXES
#undef OP
{NAME("reset"), CTL(stats_mutexes_reset)}
};
#undef MUTEX_PROF_DATA_NODE
static 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 ff26513

Please sign in to comment.