#define SET_NONEMPTY(x) { const NString& var = r->hdrs->get_##x(); \
if (!var.empty()) { \
set_##x_for_serving(r->request, var.data(), var.length()); \
} \
}
The above macro tries to set a request member if it is not empty, but I get this following
error: 'set_x_for_serving' was not declared in this scope while I use this macro.
What is wrong in the above macro?
You need the token-pasting operator on both sides of x in order to get it to substitute correctly.
#define SET_NONEMPTY(x) { const NString& var = r->hdrs->get_##x(); \
if (!var.empty()) { \
set_##x##_for_serving(r->request, var.data(), var.length()); \
} \
}
It looks like inside a macro call of SET_NONEMPTY(foobar), you expect that set_##x_for_serving will expand to set_foobar_for_serving.
Is that correct?
If so, the phrase x_for_serving is a single token and the x will not be seen by the preprocessor as an item to replace.
I think you want set_##x##_for_serving instead:
#define SET_NONEMPTY(x) { const NString& var = r->hdrs->get_##x(); \
if (!var.empty()) { \
set_##x##_for_serving(r->request, var.data(), var.length()); \
} \
}
Related
I am writing a program in c language. So, suppose I have defined a macro in c as follows :
#define TEST_MACRO(type, name) \
{ \
int testVar = 0; \
}
#define TEST_MACRO_TWO(type, name) \
{ \
}
How can I use the testVar variable from the first macro in the second macro?
For a debug purpose I defined the following macro
#define SECTION_TIME(out, s) GPIO_SetOut(out); \
s \
GPIO_ClrOut(out);
usage:
SECTION_TIME(GPIO_fooOut,
foo();
bar();
foo=bar^foo;....;....;
)
Goal: needed to mesure time of some code.
Sometimes this macro do not compile. Did I miss understand somthing?
PS: I also tried surrounding my code with {}
error: macro "SECTION_TIME" passed 6 arguments, but takes just 2
When code walks like a duck and talks like a duck, it better fully behave exactly like a duck. What I mean by that is that SECTION_TIME(GPIO_fooOut, ...) (sort of) looks like one statement while in reality it maps to 3 or more statements. This is bad, and you should strive to truely make it one statement.
This is actually not difficult, and the common idiom used for this is to wrap the macro content in do { ... } while (0) without a trailing semicolon (so that the trailing semicolon is supplied to the end of the macro invocation).
So you should at least change your macro to something like
#define SECTION_TIME(out, s) \
do { \
GPIO_SetOut(out); \
s; \
GPIO_ClrOut(out); \
} while (0)
Also notice here that you should put the terminating semicolon for s in the macro and not the argument. So the macro should be invoked like
SECTION_TIME(GPIO_fooOut,
foo();
bar();
foo=bar^foo;....;....
);
Depending on use cases, the suggestion to use SETION_TIME_BEGIN and SECTION_TIME_END might be a better solution.
Solved using variadic macro
#define BOARD_SECTION_TIME(out, ...) do { \
GPIO_SetOut(out); \
__VA_ARGS__ \
GPIO_ClrOut(out); \
} while(0)
I also use the way of __VA_ARGS__ but I also make some curry-like syntax by defining a second macro, which name is in the first:
#define SECTION_TIME(out) \
do { \
/* remember to save the value, so that the same output is always cleared and can be used in the second one */ \
decltype(out) _o = out; \
GPIO_SetOut(_o); \
SECTION_TIME_BLOCK1
#define SECTION_TIME_BLOCK1(...) \
{__VA_ARGS__}; \
GPIO_ClrOut(_o); \
} while(0);
And it can be used like this:
SECTION_TIME(GPIO_fooOut) (
foo();
bar();
foo=bar^foo;
//....;....;
);
You see that the out input-parameter is a separate tuple and that the syntax is similar to the syntax of if for example, only the brackets are different. And if you want to define only one macro, you say that the code-parameter(s) should be in a tuple:
// this macro is only a help to remove the brackets and can be used in multiple definitions
#define PP_REMOVE_BRACKETS(...) __VA_ARGS__
/**
* \param code a tuple containing the code you want to run
**/
#define SECTION_TIME(out, code) \
do { \
/* remember to save the value, so that the same output is always cleared */ \
decltype(out) _o = out; \
GPIO_SetOut(_o); \
{PP_REMOVE_BRACKETS code}; \
GPIO_ClrOut(_o); \
} while(0);
This can be used like this:
SECTION_TIME(GPIO_fooOut, (
foo();
bar();
foo=bar^foo;
//....;....;
));
There is not much information about this. Is this the one and only way to do comment in C macro definition? Or can I make add comment using another way?
#define TEST(a, b) \
{ \
bool aGb = false;\
bool bGc = false;\
/* comment is here */ \
if (a > b) \
{\
... \
}\
}
You can do this:
#define DOC(ignored)
And combine like so:
#define TEST(a, b) \
{ \
bool aGb = false; \
bool bGc = false; \
DOC((This is a comment, hello world!)) \
if (a > b) \
{ \
... \
} \
}
Naturally you can't use the C99 comment style with //, since it would ignore the rest of the line and prevent you from creating a multi-line macro.
I would personally suggest just getting used to /* comment */ style. For a start, people using syntax-highlighting IDEs with your code won't see the highlighting if you use this DOC macro above.
The only limitation which adds to those already present when commenting "real" C sources is, that you may not add anything on a macro's "source" line after the final backslash.
This is an excerpt from linux kernel source. What is the stem## usage? seeing for first time in c
#define __pcpu_size_call_return(stem, variable) \
({ typeof(variable) pscr_ret__; \
__verify_pcpu_ptr(&(variable)); \
switch(sizeof(variable)) { \
case 1: pscr_ret__ = stem##1(variable);break; \
case 2: pscr_ret__ = stem##2(variable);break; \
case 4: pscr_ret__ = stem##4(variable);break; \
case 8: pscr_ret__ = stem##8(variable);break; \
default: \
__bad_size_call_parameter();break; \
} \
pscr_ret__; \
})
The preprocessor operator ## provides a way to concatenate actual arguments during macro expansion. If a parameter in the replacement text is adjacent to a ##, the parameter is replaced by the actual argument, the ## and surrounding white space are removed, and the result is re-scanned. For example, the macro paste concatenates its two arguments:
#define paste(front, back) front ## back
so paste(name, 1) creates the token name1.
The ## operator is a preprocessor operation which glues together tokens to form a single token.
So say you want to call two functions based on a common prefix, passing a single argument each time and allowing it to be changed.
You cannot use:
#define CallBoth(pref,arg) \
{ \
arg = pref A (arg); \
arg = pref B (arg); \
}
because the substituted pref and A (or B) will be distinct tokens. Similarly, you cannot use:
#define CallBoth(pref,arg) \
{ \
arg = prefA (arg); \
arg = prefB (arg); \
}
because no substitution of prefA or prefB will take place.
To do this, you use:
#define CallBoth(pref,arg) \
{ \
arg = pref##A(arg); \
arg = pref##B(arg); \
}
and the substituted pref and A (or B) are concatenated into a single token. That way, if you enter:
CallBoth(xyzzy,intVar);
it will be translated to:
{
intVar = xyzzyA(intVar);
intVar = xyzzyB(intVar);
}
Without this feature, there's no way to end up with a single token representing the function name.
As stated in a comment in the file you're referencing:
/* Branching function to split up a function into a set of functions that are called for different scalar sizes of the objects handled. */
So, depending on the size of the variable being given to the macro, it will call one of:
stem1(variable)
stem2(variable)
stem4(variable)
stem8(variable)
where stem and variable are supplied as a parameter to the macro. Or it will call __bad_size_call_parameter() if none of those sizes are relevant.
So, a call:
char char_var;
__pcpu_size_call_return(xyzzy,char_var)
will result in a call:
xyzzy1(char_var):
int int_var;
__pcpu_size_call_return(xyzzy,int_var)
will result in a call:
xyzzy4(int_var)
where sizeof(int) == 4.
I'm trying to instrument some code to catch and print error messages. Currently I'm using a macro somethng like this:
#define my_function(x) \
switch(function(x)) { \
case ERROR: \
fprintf(stderr, "Error!\n"); \
break; \
}
Normally, I never capture the function output and this works fine. But I've found a couple cases where I also need the return value of function(). I tried something like the following, but this produces a syntax error.
#define my_function(x) \
do { \
int __err = function(x); \
switch(__err) { \
case ERROR: \
fprintf(stderr, "Error!\n"); \
break; \
} \
__err; \
} while(0)
I could declare a global variable to hold the return value of the function, but that looks ugly and my program is multithreaded, so that's likely to cause problems. I'm hoping there's a better solution out there.
GCC has a feature called statement expressions
So if define macro like
#define FOO(A) ({int retval; retval = do_something(A); retval;})
then you will be able to use it like
foo = FOO(bar);
This is relatively complicated code, there is not much reason to have it in a macro. Make it inline (C99) or static (C89) or both if you really want to place it in a header file. With any reasonable compiler this then should result in the same efficiency as a macro.
A very late reply. But none the less. I agree inline functions are better but MACROs do offer some pretty printing fun you can't get with inline functions. I agree with #qrdl that you can indeed use statement expressions had you restructured your statements a bit. Here is how it would work with a macro -
#define my_function(x, y) ({ \
int __err = 0; \
do { \
__err = function(x, y); \
switch(__err) { \
case ERROR: \
fprintf(stderr, "Error!\n"); \
break; \
} \
} while(0); \
__err; \
})
Sorry, this is an edit...
I think you just need the curly braces. No need for the do..while keywords
Make sure that the backslashes are the last characters on each line (no space after).
If you need to get the err value out of the macro, you can just add a parameter
Like so:
#define my_function(x, out) \
{ \
int __err = function(x); \
switch(__err) { \
case ERROR: \
fprintf(stderr, "Error!\n"); \
break; \
} \
__err; \
(*(out)) = _err; \
}
To preserve the pass-by-reference C paradigm, you should call my_function this way:
int output_err;
my_function(num, &output_err);
This way, later, if you decide to make my_function a real function, you don't need to change the call references.
Btw, qrdl's "Statement Expressions" is also a good way to do it.
there is no need to declare variable if your function is returning something then you can directly get that value. For example:
#define FOO(A) do_something(A)
Here do_something returns some integer. Then you can easily use it like:
int a = FOO(a);