diff --git a/chapi-ast-c/src/main/antlr/C.g4 b/chapi-ast-c/src/main/antlr/C.g4 index 8c8e5e24..ac518b3b 100644 --- a/chapi-ast-c/src/main/antlr/C.g4 +++ b/chapi-ast-c/src/main/antlr/C.g4 @@ -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 @@ -474,7 +472,6 @@ macroStatement : singleLineMacroDeclaration ; - singleLineMacroDeclaration : '#' include (StringLiteral | ('<' includeIdentifier '>' )) #includeDeclaration | '#' macroKeywords expression* (',' (expression | singleLineMacroDeclaration))* #defineDeclaration @@ -973,6 +970,10 @@ fragment IdentifierNondigit //| // other implementation-defined characters... ; +fragment UpperCase + : [A-Z_] + ; + fragment Nondigit : [a-zA-Z_] ; 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 4f3143d5..1042bdde 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 @@ -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(": 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) + } }