Skip to content

function macro definition not output #1122

Open
@GPHemsley

Description

@GPHemsley

I realize that it doesn't promise to compile, but when using --translate-fn-macros to maintain function macros, no line is included for the actual definition of the macro, leading to "cannot find macro `...` in this scope" errors instead of errors about the actual macro code.

Since the original impetus for the addition of this functionality was to possibly improve the rewrite effort (f1c1c38), it would be good to give the developer a pointer to where and how the macro should be defined.

Luckily, the tests already have a file that can demonstrate the issue:

./target/release/c2rust-transpile --translate-const-macros --translate-fn-macros tests/macros/src/define.c

In the corresponding output, TEST_FN_MACRO! and inc! are not defined anywhere.

--- tests/macros/src/define_without_fn.rs	2024-09-07 01:37:59.243037053 -0400
+++ tests/macros/src/define_with_fn.rs	2024-09-07 01:32:26.060152070 -0400
@@ -12,7 +12,7 @@
 }
 #[no_mangle]
 pub unsafe extern "C" fn test_fn_macro(mut x: libc::c_int) -> libc::c_int {
-    return x * x;
+    return TEST_FN_MACRO!(x);
 }
 pub const NULL: libc::c_int = 0 as libc::c_int;
 pub const TEST_CONST1: libc::c_int = 1 as libc::c_int;
@@ -64,16 +64,13 @@
     let mut a: libc::c_int = 0 as libc::c_int;
     let mut b: *mut libc::c_int = &mut a;
     ({
-        *b += 1;
-        *b;
-        *b;
-        *b
-    });
-    return ({
-        *b += 1;
+        let ref mut fresh0 = inc!(b);
+        *fresh0 += 1;
+        *fresh0;
         *b;
         *b
     });
+    return inc!(b);
 }
 #[no_mangle]
 pub unsafe extern "C" fn test_switch(mut x: libc::c_int) -> libc::c_int {

For reference, the original definitions and their relative placement:

#define TEST_FN_MACRO(x) ((x) * (x))
int test_fn_macro(int x) {
return TEST_FN_MACRO(x);
}

#define inc(ptr) ({\
(*ptr)++;\
*ptr;\
})
// Ensure the macro generated stmt expr block is codegen'd
int stmt_expr_inc(void) {
int a = 0;
int* b = &a;
// unused
inc(b);
// used
return inc(b);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions