I’m running into an issue using a macro which raises a compiler error which doesn’t show up if I don’t use the macro and instead substitute it with the actual code.
I have the following macro to define section attributes for different platforms
#if defined(__GNUC__) || defined(__clang__)
#if defined(__APPLE__)
#define SECTION_NO_INIT __attribute__((section("__DATA,.bss.noinit")))
#elif defined(__linux__)
#define SECTION_NO_INIT __attribute__((section(".bss.noinit")))
#else
#error "Unsupported OS"
#endif
#else
#error "Unsupported Compiler"
#endif
I use it in a series of macros that I use to create some static storage types
#define ACCUMULATOR_STORE(name) accumulator_store_##name
#define ACCUMULATOR_STORE_T(name) accumulator_store_##name##_t
#define ACCUMULATOR_STORE_DECL(name, size_in_bytes)
typedef struct {
accumulator_t acc;
uint8_t buffer[size_in_bytes];
}ACCUMULATOR_STORE_T(name)
#define ACCUMULATOR_STORE_DEF(name)
ACCUMULATOR_STORE_T(name) ACCUMULATOR_STORE(name) SECTION_NO_INIT
As you can see I use the SECTION_NO_INIT
macro in the definition helper macro.
I create an instance of this accumulator store in a test as follows
ACCUMULATOR_STORE_DECL(test, 6);
ACCUMULATOR_STORE_DEF(test);
I’m compiling this on a mac currently and I’m seeing the following compiler error
tests/accumulator_tests.c|29 col 1| error: expected ';' after top level declarator
|| ACCUMULATOR_STORE_DEF(test);
|| ^
inc/cutils/accumulator.h|56 col 51| note: expanded from macro 'ACCUMULATOR_STORE_DEF'
|| ACCUMULATOR_STORE_T(name) ACCUMULATOR_STORE(name) SECTION_NO_INIT
|| ^
|| 1 error generated.
If I replace the SECTION_NO_INIT call directly with the attribute declaration I don’t get the compiler error
#define ACCUMULATOR_STORE_DEF(name)
ACCUMULATOR_STORE_T(name) ACCUMULATOR_STORE(name) __attribute__((section("__DATA,.bss.noinit")))
I’m not sure how to fix the issue. I’ve tried things like this
#define ACCUMULATOR_STORE_DEF(name)
ACCUMULATOR_STORE_T(name) ACCUMULATOR_STORE(name) SECTION_NO_INIT;
But I still get the same error.