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 modifies the `singleLineMacroDeclaration` rule to allow for multiple expressions separated by commas. It also adds the `macroIdDeclaration` rule to handle macro identifiers. Additionally, the commit includes changes to the `CFullIdentListenerTest.kt` file to test the new functionality.
  • Loading branch information
phodal committed Jan 30, 2024
1 parent b5b7c5f commit 9510ba7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 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 @@ -39,15 +39,15 @@ compilationUnit

singleLineMacroDeclaration
: '#' include (StringLiteral | ('<' includeIdentifier '>' )) #includeDeclaration
| '#' 'define' expression* #defineDeclaration
| '#' macroKeywords expression* #conditionalDeclaration
| '#' macroKeywords expression* (',' (expression | singleLineMacroDeclaration))* #defineDeclaration
| Identifier postixCall? ('{' blockItem* '}')? #macroCallBlockDeclaration
| Identifier postixCall ';'? #macroFuncCallDeclaration
| Identifier #macroDeclaration
| '#' Identifier #macroIdDeclaration
;

macroKeywords
: 'if' | 'undef' | 'else' | 'pragma' | 'endif' | 'ifdef' | 'ifndef' | 'elif'
: 'if' | 'undef' | 'else' | 'pragma' | 'endif' | 'ifdef' | 'ifndef' | 'elif' | 'define'
;

MultiLineMacro
Expand Down Expand Up @@ -270,6 +270,7 @@ structDeclarationList
structDeclaration // The first two rules have priority order and cannot be simplified to one expression.
: specifierQualifierList structDeclaratorList? ';'
| staticAssertDeclaration
| singleLineMacroDeclaration
;

specifierQualifierList
Expand Down Expand Up @@ -548,6 +549,8 @@ typeKeywords
| 'double'
| 'signed'
| 'unsigned'
| 'void'
| 'static'
;

keywords
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ internal class CFullIdentListenerTest {

@Test
fun allGrammarUnderResources() {
val content = this::class.java.getResource("/grammar")!!.toURI()
// val content = "/Users/phodal/Downloads/redis-unstable"
// val content = this::class.java.getResource("/grammar")!!.toURI()
val content = "/Users/phodal/Downloads/redis-unstable"
File(content).walkTopDown().forEach {
if (it.isFile && (it.extension == "c" || it.extension == "h")) {
println("Analyse ${it.path}")
Expand Down Expand Up @@ -377,6 +377,28 @@ typedef struct {
void hello() {
va_arg(ap, char *);
}
#include "test/jemalloc_test.h"
#include "jemalloc/internal/mpsc_queue.h"
mpsc_queue_proto(static);
#define ERR(e) e, #e
""".trimIndent()

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

@Test
fun shouldHandleMacroInStructure() {
val code = """
struct node_s {
#define NODE_MAGIC 0x9823af7e
uint32_t magic;
heap_link_t link;
uint64_t key;
};
""".trimIndent()

val codeFile = CAnalyser().analysis(code, "helloworld.c")
Expand Down

0 comments on commit 9510ba7

Please # to comment.