Background
I have a project where I have two separate products with near-identical macro names, for which I would like to create a macro-like-function to retrieve the values of the macros quickly. I have written a getTranslation macro-function to take the literal text provided to the "function", which should be treated as a string and a string prefix (shown below).
Question
How can I accomplish this operation of taking the arguments supplied to the macro, concatenating them together (with an underscore in the middle), and treating that result as a preprocessor macro instead of a string?
Code Listing
/*******************************************************************************
* coconut.h
******************************************************************************/
#define COCONUT (PRODUCT_COCONUT)
#define COCONUT_FX_REGISTER (100)
#define COCONUT_BASE_REGISTER (101)
/*******************************************************************************
* pineapple.h
******************************************************************************/
#define PINEAPPLE (PRODUCT_PINEAPPLE)
#define PINEAPPLE_FX_REGISTER (200)
#define PINEAPPLE_BASE_REGISTER (201)
/*******************************************************************************
* test.c.
******************************************************************************/
#include <stdio.h>
#include "translation.h"
#include "coconut.h"
int main(void) {
int i = getTranslation(FX_REGISTER, COCONUT);
printf("Translation:%d.\n", i);
return 0;
}
/*******************************************************************************
* translation.h
******************************************************************************/
#define FX_REGISTER (0)
#define BASE_REGISTER (1)
#define getTranslationInternal(x, y) #y "_" #x
#define getTranslation(x, y) getTranslationInternal(x, y)
enum Products {
PRODUCT_COCONUT = 0,
PRODUCT_PINEAPPLE,
PRODUCT_MAX,
PRODUCT_INVALID = (-1)
};
Compiler Warnings
test.c: In function ‘main’:
test.c:10:45: warning: initialization makes integer from pointer without a cast [-Wint-conversion]
int i = getTranslation(FX_REGISTER, COCONUT);
^
translation.h:7:39: note: in definition of macro ‘getTranslationInternal’
#define getTranslationInternal(x, y) #y "_" #x
^
test.c:10:10: note: in expansion of macro ‘getTranslation’
int i = getTranslation(FX_REGISTER, COCONUT);
Sample Run
Translation:4195812.
#define getTranslationInternal(x, y) y ## _ ## x
worked for me on clang, if you drop parentheses around the macro definitions.
Related
How to fix the macro expansion issue below ?
#define GET_VAL 3,2
#define ADD_VAL(val0, val1) ((val0) + (val1))
void foo()
{
int res = ADD_VAL(GET_VAL);
}
The macro is getting expanded as below and resulting in an error. I am using MSVC 2019
res = 3,2 + ;
I even tried using a helper macro as below, but still getting the same error.
#define GET_VAL 3,2
#define ADD_VAL1(val0, val1) (val0 + val1)
#define ADD_VAL(val) ADD_VAL1(val)
Expecting expansion:
ADD_VAL(GET_VAL); --> ADD_VAL(3, 2); --> 3 + 2
By default msvc doesn't use a standard confirming preprocessor implementation, make sure to enable it with /Zc:preprocessor
Macros fully expand their arguments in isolation before pasting them into the replacement text, but the resulting tokens aren't separated into a new argument list. They way to fix your behavior is to create an intermediate macro that expands the arguments, and passes the expanded arguments to your macro:
#define GET_VAL 1,2
#define ADD_VAL(...) ADD_VAL_(__VA_ARGS__)
#define ADD_VAL_(a,b) ((a)+(b))
ADD_VAL(GET_VAL) // should work now
Another option is to write a fx macro that evaluates arguments and applies a function to them:
#define FX(f,...) f(__VA_ARGS__)
#define ADD_VAL(a,b) ((a)+(b))
FX(ADD_VAL,GET_VAL) // should work now
C preprocessor can be abused in horrible ways
#define GET_VAL 3,2
// #define ADD_VAL(val0, val1) ((val0) + (val1))
#define ADD_VAL(val) ((int [2]){val}[0] + (int [2]){val}[1])
int main() {
printf("%d\n",ADD_VAL(GET_VAL));
}
Output
5
I want to do create a macro whose name is a concatenation of two things i.e.
#define name ## _body(a) \
a
but gcc -E gives the error
macros.c:9:18: error: '##' cannot appear at either end of a macro expansion
#define typename ## _body(body) \
QUESTION
Is it possible to do this using only C preprocessor?
In a macro definition, the name part is always a single preprocessor token. However, you can use function-like macros, with parameters, that expand to a single token.
Consider the following macros:
#define JOIN(a,b) a ## b
#define FOO(suffix) JOIN(FOO_, suffix)
#define FOOBAZ JOIN(FOO_, BAZ)
#define FOOBAAZ JOIN(foo_, baaz)
#define FOO_BAR first
#define FOO_BAZ second
With the above definitions, declaring
int FOO(BAR) = 1;
int FOOBAZ = 2;
int FOOBAAZ = 3;
is equivalent to (i.e., gets preprocessed to)
int first = 1;
int second = 2;
int foo_baaz = 3;
There are cases (especially when exploring various algorithms) when templates or polymorphic code is useful. Consider for example the following ops.h:
#if defined(TYPE) && defined(PREFIX)
#undef JOIN2
#define JOIN2_(a,b) a ## b
#define JOIN2(a,b) JOIN2_(a, b)
#define NAME(end) JOIN2(PREFIX, end)
static inline TYPE NAME(_add)(const TYPE val1, const TYPE val2)
{
return val1 + val2;
}
static inline TYPE NAME(_sub)(const TYPE val1, const TYPE val2)
{
return val1 - val2;
}
static inline TYPE NAME(_neg)(const TYPE val)
{
return -val;
}
static inline TYPE NAME(_mul)(const TYPE val1, const TYPE val2)
{
return val1 * val2;
}
static inline TYPE NAME(_div)(const TYPE val1, const TYPE val2)
{
return val1 / val2;
}
#endif
#undef NAME
#undef JOIN2
#undef PREFIX
#undef TYPE
The macro NAME(suffix) expands to a single token consisting of the expansion of PREFIX immediately followed by the expansion of suffix. (If they are not preprocessor macros, they are used as-is.) This allows the same header file to be included multiple times, assuming PREFIX is defined to a new value each time.
Note that it is common to have a space between e.g. NAME(_add) and the following (const TYPE val1, const TYPE val2). I omitted it, in the hopes that it makes the function definitions more familiar-looking.
Let's look at an example program using such a header file:
#include <stdlib.h>
#include <inttypes.h>
#include <stdio.h>
#define TYPE uint32_t
#define PREFIX u32
#include "ops.h"
#define TYPE float
#define PREFIX float
#include "ops.h"
#define TYPE double
#define PREFIX dbl
#include "ops.h"
int main(void)
{
printf("dbl_div(217.0, 31.0) = %.1f\n", dbl_div(217.0, 31.0));
printf("u32_sub(4, 2) = %" PRIu32 "\n", u32_sub(4, 2));
printf("float_mul(1.25f, 72.00f) = %.2ff\n", float_mul(1.25f, 72.00f));
return EXIT_SUCCESS;
}
The first #include "ops.h" defines functions u32_add(), u32_sub(), u32_neg(), u32_mul(), and u32_div(). The second defines functions float_add() and so on, and the third dbl_add() and so on.
The above files are valid C99, and when compiled and run, it outputs
dbl_div(217.0, 31.0) = 7.0
u32_sub(4, 2) = 2
float_mul(1.25f, 72.00f) = 90.00f
If you combine the above with suitable macros using C11 _Generic, you can make "functions" that call different implementations based on the type of their argument.
Using Paul Fultz II's solution in the post C-preprocessor recursive macro, I'd like to expand an unlimited number of parenthesized macro arguments, e.g.
#define MY_CHAIN (alpha) (beta) (gamma)
into a comma-separated list which can be passed to a variadic macro, e.g.
CHAIN_COMMA(MY_CHAIN) // alpha, beta, gamma
I'm able to expand into braces [alpha] [beta] [gamma] and delimit the list with everything I've tried except a comma, alpha :: beta :: gamma in the example below.
Here is my full (compiling) code:
#include <iostream>
using namespace std;
// unrelated macro utilities
#define SEE(expression) cout << #expression ": " << STR(expression) << endl;
#define CMD(function, ...) function(__VA_ARGS__)
#define STR(s) CMD(STR_, s)
#define STR_(s) #s
// concatenation
#define CAT(x, y) CAT_(x, y)
#define CAT_(x,y) x ## y // error from CHAIN_COMMA: passed 4 arguments
// surround each chain element with square brackets []
#define CHAIN_BRACE(chain) CAT(CHAIN_BRACE_1 chain, _END)
#define CHAIN_BRACE_1(x) [x] CHAIN_BRACE_2
#define CHAIN_BRACE_2(x) [x] CHAIN_BRACE_1
#define CHAIN_BRACE_1_END
#define CHAIN_BRACE_2_END
// separate each chain element with the scope operator ::
#define CHAIN_SCOPE(chain) CAT(CHAIN_SCOPE_0 chain, _END)
#define CHAIN_SCOPE_0(x) x CHAIN_SCOPE_1
#define CHAIN_SCOPE_1(x) :: x CHAIN_SCOPE_2
#define CHAIN_SCOPE_2(x) :: x CHAIN_SCOPE_1
#define CHAIN_SCOPE_0_END
#define CHAIN_SCOPE_1_END
#define CHAIN_SCOPE_2_END
// trouble here: can't separate chain elements with commas
#define CHAIN_COMMA(chain) CAT(CHAIN_COMMA_0 chain, _END) // error
#define CHAIN_COMMA_0(x) x CHAIN_COMMA_1
#define CHAIN_COMMA_1(x) , x CHAIN_COMMA_2
#define CHAIN_COMMA_2(x) , x CHAIN_COMMA_1
#define CHAIN_COMMA_0_END
#define CHAIN_COMMA_1_END
#define CHAIN_COMMA_2_END
// define a custom chain and save various forms of it
#define MY_CHAIN (alpha) (beta) (gamma)
#define MY_BRACES CHAIN_BRACE(MY_CHAIN) // [alpha] [beta] [gamma]
#define MY_SCOPES CHAIN_SCOPE(MY_CHAIN) // alpha :: beta :: gamma
#define MY_COMMAS CHAIN_COMMA(MY_CHAIN) // alpha , beta , gamma
int main() {
SEE(MY_CHAIN);
SEE(MY_BRACES);
SEE(MY_SCOPES);
// SEE(MY_COMMAS); // error: macro "CAT_" passed 4 arguments, but takes just 2
return 0;
}
This outputs:
MY_CHAIN: (alpha) (beta) (gamma)
MY_BRACES: [alpha] [beta] [gamma]
MY_SCOPES: alpha :: beta :: gamma
I tried parenthesizing the comma-separated list but CAT won't append ) to _END. Any clever ideas to expand into alpha, beta, gamma?
As the comma is both important to your output and is a syntactic element, you need to make a substitute comma for outputting.
#define COMMA() ,
We will also need some deferring functions so that COMMA isn't evaluated immediately.
#define EMPTY()
#define DEFER(id) id EMPTY()
Now we can redefine your two macros into
#define CHAIN_COMMA_1(x) DEFER(COMMA)() x CHAIN_COMMA_2
#define CHAIN_COMMA_2(x) DEFER(COMMA)() x CHAIN_COMMA_1
However, your SEE macro also doesn't like the commas that are placed, and so will error for having too many parameters passed.
You can see that the macro is performing correctly by looking at the output of the preprocessor with the -E option.
Background
In a separate question of mine, I created a function-like-macro that allows me to concatenate a user-supplied text literal to create a macro name, i.e.:
/******************************************************************************
* coconut.h
******************************************************************************/
#define COCONUT_FX_REGISTER (100)
#define COCONUT_BASE_REGISTER (101)
/*******************************************************************************
* pineapple.h
******************************************************************************/
#define PINEAPPLE_FX_REGISTER (200)
#define PINEAPPLE_BASE_REGISTER (201)
/*******************************************************************************
* test.c.
******************************************************************************/
#include <stdio.h>
#include "translation.h"
#include "coconut.h"
#include "pineapple.h"
int main(void) {
int i = getTranslation(FX_REGISTER, COCONUT);
printf("Translation:%d.\n", i);
return 0;
}
/*******************************************************************************
* translation.h
******************************************************************************/
#define getTranslation(x, y) y ## _ ## x
Goal
I would like to extend this logic so that I can use a macro for a default value to pass to getTranslation, i.e.:
#define XFRM(x) #x
#define XFRM2(x) XFRM(x)
#define DEFAULT_PRODUCT XFRM2(COCONUT)
int main(void) {
int i = getTranslation(FX_REGISTER, DEFAULT_PRODUCT);
printf("Translation:%d.\n", i);
return 0;
}
Problem
However, I can't seem to get DEFAULT_PRODUCT to be converted to a non-string text literal.
Build Errors
main.c: In function ‘main’:
main.c:14:35: error: ‘DEFAULT_PRODUCT_FX_REGISTER’ undeclared (first use in this function)
printf("%d\n", getTranslation(FX_REGISTER, DEFAULT_PRODUCT));
^
translation.h:33:25: note: in definition of macro ‘getTranslation’
#define getTranslation(x, y) y ## _ ## x
^
main.c:14:35: note: each undeclared identifier is reported only once for each function it appears in
printf("%d\n", getTranslation(FX_REGISTER, DEFAULT_PRODUCT));
^
translation.h:33:25: note: in definition of macro ‘getTranslation’
#define getTranslation(x, y) y ## _ ## x
Question
How can I create a DEFAULT_PRODUCT macro that resolves to a non-string text literal so that I can create a "default" value to use with getTranslation? This is using GCC set to C99 pedantic.
Sounds like an XY problem.
It seems that macro concatenations are processed simultaneous to macro literal expansions, so I'm afraid there's no way to create a DEFAULT_PRODUCT macro that gets expanded before getTranslation.
My proposal: Create another macro function getDefaultTranslation(x) and you'll easily achieve what you want.
// You may want to add appropriate comments
// so code reviewers know what this is doing.
#define getDefaultTranslation(x) COCONUT ## x
Regarding this question, macro expansion is done layer-by-layer, and at the same layer concatenation has a higher precedence , so adding another layer should work. See ringø's answer below.
You need to add an indirection in order to let the preprocessor expand the macros before doing the concatenation
#define CONCAT(a, b) a ## _ ## b
#define getTranslation(x, y) CONCAT(x,y)
#define XFRM(x) x
#define XFRM2(x) XFRM(x)
#define DEFAULT_PRODUCT XFRM2(COCONUT)
Note that XFRM has its # removed (off x), otherwise the " gives an invalid preprocessing token.
This way you get
int i = FX_REGISTER_COCONUT;
I am having trouble getting this macro expanison right
#define foo Hello
#ifdef foo
#define wrapper(x) foo ## x
#else
#define wrapper(x) boo ## x
#endif
calling:
wrapper(_world)
I would like the result of
Hello_world
however, the macro is treating the "foo" define as a literal, and thus giving
foo_world
Can someone point out my mistake?
Thanks
I would recommend gnu-cpp-manual which clearly explains how macros are expanded.
Macro arguments are completely macro-expanded before they are substituted into a macro body, unless they(macro arguments) are stringified or pasted with other tokens (by the macro function that is directly applied to).
For example:
If an argument is stringified or concatenated, the prescan does not occur.
#define AFTERX(x) X_ ## x
#define XAFTERX(x) AFTERX(x)
#define TABLESIZE 1024
#define BUFSIZE TABLESIZE
AFTERX(BUFSIZE) => X_BUFSIZE: since AFTERX is to concatenate argument with prefix, its argument is not expanded, remaining BUFSIZE.
XAFTERX(BUFSIZE) => X_1024: XAFTERX does not do concatenation directly, so BUFSIZE will be expanded first.
Generally, arguments are scanned twice to expand macro called in them.
--- edit ---
So the better practice is: (code from QEMU source)
#ifndef glue
#define xglue(x, y) x ## y
#define glue(x, y) xglue(x, y)
#define stringify(s) tostring(s)
#define tostring(s) #s
#endif
glue(x,y) will concatenate x and y with both already expanded.