'C MACRO expansion dynamically

I am trying something similar to following in a legacy code

//Defining MACRO for PLATFORM 1

#define PLATFORM_1_ENTRY_L2PT    10
#define PLATFORM_1_ENTRY_L3PT    11
#define PLATFORM_1_ENTRY_L4PT    12
#define PLATFORM_1_ENTRY_L5PT    13

//Defining MACRO for PLATFORM 2

#define PLATFORM_2_ENTRY_L2PT    20
#define PLATFORM_2_ENTRY_L3PT    21
#define PLATFORM_2_ENTRY_L4PT    22
#define PLATFORM_2_ENTRY_L5PT    23

//DEFINING MACRO for default platform

#define DEFAULT_PLATFORM_ENTRY_L2PT    30
#define DEFAULT_PLATFORM_ENTRY_L3PT    31
#define DEFAULT_PLATFORM_ENTRY_L4PT    32
#define DEFAULT_PLATFORM_ENTRY_L5PT    33

//GETTING offset based on 'dynamic' platform TYPE

#define GET_OFFSET(entry_type)   ({                                          \
     (global.platform_1) ?                                                   \
         PLATFORM_1_ENTRY_##entry_type :                                     \
         (global.platform_2) ?                                               \
              PLATFORM_2_ENTRY_##entry_type :                                \
              DEFAULT_PLATFORM_ENTRY_##entry_type)                           \
     })  

//Using the following macro expansion to expand based on platform type

#define ENTRY_L2PT                                       GET_OFFSET(L2PT)
#define ENTRY_L3PT                                       GET_OFFSET(L3PT)
#define ENTRY_L4PT                                       GET_OFFSET(L4PT)
#define ENTRY_L5PT                                       GET_OFFSET(L5PT)

//Defining the global structure

struct global_ {
    bool platform_1;
    bool platform_2;
} glbl;

glbl global = {0};

//calling from sample main


void main() {
    //set offset 
    int array[40] = {0};
    ...
    if(sizeof (void *) * CHARBIT == 64) {
        global.platform_1 = 1;
    } else {
        global.platform_2 = 1;
    }
    array[ENTRY_L2PT]++;
    array[ENTRY_L3PT]++;
    array[ENTRY_L4PT]++;
    array[ENTRY_L5PT]++;
    ...
}

** I am getting Getting following error during compilation:**

At top level:
error: braced-group within expression allowed only inside a function
#define GET_OFFSET(entry_type)   ({                                        \
                                  ^
note: in expansion of macro ‘GET_OFFSET’
#define ENTRY_L2PT                GET_OFFSET(L2PT)
                                             ^
note: in expansion of macro ‘ENTRY_L2PT’
        array[ENTRY_L2PT]++;

how do I make the above work?

Please note that there are multiple places where ENTRY_L2PT is used in code, and replacing all of them has a lot of effort in development/testing/regression etc.



Solution 1:[1]

how do I make the above work?

braced-group within expression allowed only inside a function

Don't use a braced-group.

#define GET_OFFSET(entry_type)   ( \
     .... \
     .... \
     )

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 KamilCuk