Skip to content

Commit

Permalink
feat(c): add macro support to primaryExpression and genericSelection #24
Browse files Browse the repository at this point in the history


- Added support for macros in primaryExpression and genericSelection in the C grammar.
- Updated primaryExpression to include typeKeywords, Identifier, '==', '!=', and pointer.
- Updated genericSelection to include singleLineMacroDeclaration.
- Updated initializerList to include singleLineMacroDeclaration.
  • Loading branch information
phodal committed Jan 30, 2024
1 parent dc538e2 commit dae101f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
9 changes: 6 additions & 3 deletions chapi-ast-c/src/main/antlr/C.g4
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,17 @@ includeIdentifier

primaryExpression
: Identifier
| typeKeywords (Identifier | typeKeywords)* pointer?
| Constant
| StringLiteral+
| '(' expression ')'
| genericSelection
| '__extension__'? '(' compoundStatement ')' // Blocks (GCC extension)
| '__builtin_va_arg' '(' unaryExpression ',' typeName ')'
| '__builtin_offsetof' '(' typeName ',' unaryExpression ')'
| StringLiteral singleLineMacroDeclaration StringLiteral (singleLineMacroDeclaration | StringLiteral)*
// for macro support
| (typeKeywords | Identifier | '==' | '!=') (Identifier | typeKeywords )* pointer?
| StringLiteral singleLineMacroDeclaration (singleLineMacroDeclaration | StringLiteral)*
| Ellipsis
;

genericSelection
Expand Down Expand Up @@ -347,6 +349,7 @@ directDeclarator
| Identifier ':' DigitSequence #bitFieldDirectDeclarator // bit field
| vcSpecificModifer Identifier #vcSpecificModiferDirectDeclarator
| '(' typeSpecifier? pointer directDeclarator ')' #functionPointerDirectDeclarator // function pointer like: (__cdecl *f)
| singleLineMacroDeclaration #macroDirectDeclarator
;

vcSpecificModifer
Expand Down Expand Up @@ -434,7 +437,7 @@ typedefName

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

initializerList
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ internal class CFullIdentListenerTest {
@Test
fun allGrammarUnderResources() {
val content = this::class.java.getResource("/grammar")!!.toURI()
// val content = "/Users/phodal/Downloads/redis-unstable"
// val content = "/Users/phodal/Downloads/redis-unstable/deps/jemalloc/test"
File(content).walkTopDown().forEach {
if (it.isFile && (it.extension == "c" || it.extension == "h")) {
println("Analyse ${it.path}")
Expand Down Expand Up @@ -409,4 +409,36 @@ typedef struct {
val codeFile = CAnalyser().analysis(code, "helloworld.c")
assertEquals(codeFile.DataStructures.size, 1)
}

@Test
fun shouldHandleMacroInDecl() {
val code = """
TEST_BEGIN(test_junk_alloc_free) {
size_t sizevals[] = {
1, 8, 100, 1000, 100*1000
#if LG_SIZEOF_PTR == 3
, 10 * 1000 * 1000
#endif
};
size_t lg_alignvals[] = {
0, 4, 10, 15, 16, LG_PAGE
#if LG_SIZEOF_PTR == 3
, 20, 24
#endif
};
}
END_TEST
#if 0
#define TRACE_HOOK(fmt, ...) malloc_printf(fmt, __VA_ARGS__)
#else
#define TRACE_HOOK(fmt, ...)
#endif
size_t n = malloc_snprintf(&buf[i], buflen-i, "%"FMTu64, t0 / t1);
""".trimIndent()

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

0 comments on commit dae101f

Please # to comment.