How to use token concatenation with scope operator? - c++

I would like to do something like the following:
#define CODE_BLOCK( id) \
do { \
someNameSpace::##id foo; \
foo.ParseFromString( ...); \
print_##id( foo);
} while(0)
[...]
CODE_BLOCK( BlahStream);
So basically, I want foo to be declared as type someNameSpace::id where id macro argument. print_id is defined elsewhere. Anyways, g++ complains with the message:
error: pasting "::" and "BlahStream" does not give a valid preprocessing token
Is there anyway around this?
If it helps, foo is from some userdefined googleprotocol buffer. My g++ version is 4.7.2 on debian linux.
Thanks.

Just remove the ## operator and it'll work just fine.
e.g.
#define stuff(x) \
std:: x
#include <iostream>
int main() {
stuff(cout) << "Hello World\n";
}

The preprocessor is already aware that x::y is three separate tokens: x, :: and y.
Therefore, you do not need any concatenation and, in fact, as you've found out, trying to use it will result in an error.
When you write this:
someNameSpace::##id
you are writing someNamespace, ::y … and ::y is not a single token.
You should simply write:
someNameSpace::id
Now you have someNamespace, ::, y … as desired.
Here's your finished code:
#define CODE_BLOCK(id) \
do { \
someNameSpace::id foo; \
foo.ParseFromString(...); \
print_##id(foo);
} while(0)
// [...]
CODE_BLOCK(BlahStream);
N.B. I've removed the spaces that you keep writing after the ( symbol, because ew. :)

Related

string concatenation, log4cplus, and MISRA compliance

I am using log4cplus, and my misra-checks for something as innocent-looking as
LOG4CPLUS_INFO(
logger,
std::string("ABC") + std::string("DEF"));
yield (among others) The underlying type of `0L` is implicitly reduced from `8-bit signed char` to {code{bool}}.. This happens also when I put the respective literals rather than wrapping them inside string. I wonder how to fix this. Or, more generally put, how would you concatenate several log messages and yet keep MISRA-checks happy?
I had a look at LOG4CPLUS_INFO and that is a macro defined as:
#if !defined(LOG4CPLUS_DISABLE_INFO)
#define LOG4CPLUS_INFO(logger, logEvent) \
LOG4CPLUS_MACRO_BODY (logger, logEvent, INFO_LOG_LEVEL)
and LOG4CPLUS_MACRO_BODY is defined as:
#define LOG4CPLUS_MACRO_BODY(logger, logEvent, logLevel) \
LOG4CPLUS_SUPPRESS_DOWHILE_WARNING() \
do { \
log4cplus::Logger const & _l \
= log4cplus::detail::macros_get_logger (logger); \
if (LOG4CPLUS_MACRO_LOGLEVEL_PRED ( \
_l.isEnabledFor (log4cplus::logLevel), logLevel)) { \
LOG4CPLUS_MACRO_INSTANTIATE_OSTRINGSTREAM (_log4cplus_buf); \
_log4cplus_buf << logEvent; \
log4cplus::detail::macro_forced_log (_l, \
log4cplus::logLevel, _log4cplus_buf.str(), \
__FILE__, __LINE__, LOG4CPLUS_MACRO_FUNCTION ()); \
} \
} while (0) \
LOG4CPLUS_RESTORE_DOWHILE_WARNING()
and so your MISRA checker will be checking the invocation of the macro. And MISRA likes if statements to be defined explicitly in terms of bool e.g. rather than if(node) it likes if(node!=nullptr) and I think it may have an issue with the last line:
} while (0) \
As 0 is being implicitly cast to bool. You should see if you get the same warning by adding a simple do{}while(0); loop to your code and run your checker again.
And if you want a MISRA safe way of logging, I would avoid the use of macros. You can make a simple logging class with a std::ofstream and a std::mutex and keep it in namespace scope defined as an extern in your header file.

How to implement a standard-compliant assert macro with an optional formatted message?

What's the way to implement a standard-compliant assert macro with an optional formatted message?
What I have works in clang, but (correctly) triggers the -Wgnu-zero-variadic-macro-arguments warning if it is turned on (e.g. via -Wpedantic) when the macro is used without the optional message. Wandbox
#define MyAssert(expression, ...) \
do { \
if(!(expression)) \
{ \
printf("Assertion error: " #expression " | " __VA_ARGS__); \
abort(); \
} \
} while(0)
One needs to really use the preprocessor to the max in order to differentiate no additional arguments from the case where they are present. But with Boost.PP one can do this:
#include <boost/preprocessor/variadic/size.hpp>
#include <boost/preprocessor/arithmetic/sub.hpp>
#include <boost/preprocessor/logical/bool.hpp>
#include <boost/preprocessor/cat.hpp>
#define MyAssert(...) BOOST_PP_CAT(MY_ASSERT,BOOST_PP_BOOL(BOOST_PP_SUB(BOOST_PP_VARIADIC_SIZE(__VA_ARGS__), 1)))(__VA_ARGS__)
#define MY_ASSERT0(expr) MY_ASSERT1(expr,)
#define MY_ASSERT1(expression, ...) \
do { \
if(!(expression)) \
{ \
std::printf("Assertion error: " #expression " | " __VA_ARGS__); \
std::abort(); \
} \
} while(0)
MyAssert must accept at least one argument (standard). Then we count the arguments, subtract one, and turn to a boolean (0 or 1). This 0 or 1 is concatenated to the token MY_ASSERT to form a macro name, to which we proceed to forward the arguments.
MY_ASSERT1 (with args), is your original macro. MY_ASSERT0 substitutes itself with MY_ASSERT1(expr,), the trailing comma means we pass another argument (thus fulfilling the requirement for the one extra argument), but it is an empty token sequence, so it does nothing.
You can see it live.
Since we already went down this rabbit hole, if one doesn't want to pull in Boost.PP the above can be accomplished with the usual argument counting trick, slightly adapted. First, we must decide on a maximum limit for the arguments we allow. I chose 20, you can choose more. We'll need the typical CONCAT macro, and this macro here:
#define HAS_ARGS(...) HAS_ARGS_(__VA_ARGS__,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,)
#define HAS_ARGS_(a1,a2,a3,a4,a5,b1,b2,b3,b4,b5,c1,c2,c3,c4,c5,d1,d2,d3,d4,d5,e, N, ...) N
It's argument counting, but with a twist. When __VA_ARGS__ is a single argument (no extra ones), the N resolved as 0. Otherwise, it is resolved as 1. There can be up to 20 extra arguments after the expression, any number of which will resolve to the same 1. Now we just plug it into the same place we used boost before:
#define MyAssert(...) CONCAT(MY_ASSERT, HAS_ARGS(__VA_ARGS__))(__VA_ARGS__)
You can tinker with it here
I have a solution which I'm not particularly proud of..
We can obtain the first argument in plain form and as a string using:
#define VA_ARGS_HEAD(N, ...) N
#define VA_ARGS_HEAD_STR(N, ...) #N
Note that in usage, in order to not get warnings, you should do VA_ARGS_HEAD(__VA_ARGS__, ) (with the extra ,) so that VA_ARGS_HEAD is never used with a single parameter (trick taken from StoryTeller's answer).
We define the following helper function:
#include <stdarg.h>
#include <stdio.h>
inline int assertionMessage(bool, const char *fmt, ...)
{
int r;
va_list ap;
va_start(ap, fmt);
r = vprintf(fmt, ap);
va_end(ap);
return r;
}
When the assertion has a format string, the function would work with __VA_ARGS__ as is, however when the bool is the only argument, we're missing a format string. That's why we'll add another empty string after __VA_ARGS__ when invoking it:
#define MyAssert(...) \
do { \
if(!(VA_ARGS_HEAD(__VA_ARGS__, ))) \
{ \
printf("Assertion error: %s | ", VA_ARGS_HEAD_STR(__VA_ARGS__, )); \
assertionMessage(__VA_ARGS__, ""); \
abort(); \
} \
} while(0)
Note that assertionMessage doesn't have printf in its name. This is deliberate and intended to avoid the compiler giving format-string related warnings for its invocations with the extra "" argument. The down-side for this is that we don't get the format-string related warnings when they are helpful.
The basic solution is to use << on cerr:
#define MyAssert(expression, msg) \
do { \
if(!(expression)) \
{ \
std::cerr << msg; \
abort(); \
} \
} while(0)
This solution uses C++ streams, so you can format the output as you see fit. Actually this is a simplification of a C++17 solution that I'm using to avoid temporaries (people tend to use + instead of << with this solution, triggering some efficiency warnings).
Use it then like this:
MyAssert(true, "message " << variable << " units");
I think the optionality is bogus here, as you are outputting "Assertion error:" meaning that you expect a message.

Meaning of # symbol as argument in strcmp()

I came across this line of code in legacy code:
#define func(x,y) if(strcmp(x,#y)==0)
Anyone have an idea of the purpose for the # symbol preceding y?
as mentioned in the comments, this seems like stringification in a c macro.
here is a little example that uses your sample code:
#define doif(x, y) \
if(strcmp(x,#y)==0) { \
printf("doing! %s\n",x); \
}\
else { \
printf("not doing!\n"); \
}
int main()
{
char x[] = "test";
doif (x, test);
doif (x, something);
return 0;
}
the stringification operator actually pastes y variable as a string before the compilation stage
First of all you posted wrong or in complete code. #y should be used with macro definition, not while using macro.
#define MAC(STR) #STR
int main(int argc, char* argv[])
{
printf(MAC(ME));//prints ME
printf(MAC("ME"));//prints "ME"
return 0;
}
Here I have defined MAC macro which takes one argument. I did it's stringification.
Also see second printf, it exactly prints string. So you need not to give pair of "".

Macro that swallows semicolon outside of function

Is there any idiom that forces a semicolon after a cpp macro outside of a function?
The known solution for macros used inside functions is:
#define MACRO(x) \
do {
x * 2;
} while(0)
However, say I have a macro that looks like the following:
#define DETAIL(warning) _Pragma(#warning)
#define WARNING_DISABLE(warning) DETAIL(GCC diagnostic ignore warning)
What can I put in the macro that would force a semi-colon after that statement. The statement could be used in or outside of a function:
WARNING_DISABLE("-Wunused-local-typedefs")
#include "boost/filesystem.hpp"
void foo(const int x) {
WARNING_DISABLE("-Wsome-warning")
...
}
Is there any C/C++ syntax that will force a semi-colon in the parser at any point in a file that doesn't have side effects?
Edit: A possible use case:
#define MY_CPPUNIT_TEST_SUITE(test_suite_class) \
WARNING_PUSH \
/* the declaration of the copy assignment operator has been suppressed */ \
INTEL_WARNING_DISABLE(2268) \
/* the declaration of the copy assignment operator has been suppressed */ \
INTEL_WARNING_DISABLE(2270) \
/* the declaration of the copy constructor operator has been suppressed */ \
INTEL_WARNING_DISABLE(2273) \
CPPUNIT_TEST_SUITE(test_suite_class); \
WARNING_POP \
/* force a semi-colon */ \
UDP_KEYSTONE_DLL_LOCAL struct __udp_keystone_cppunit_test_suite ## __LINE__ {}
You don't need LINE trick - it is enough to forward-declare some structure, which is allowed multiple times and there is no need for actual definition. Also collision with actual struct should not be a problem.
#define DETAIL(warning) _Pragma(#warning) struct dummy
#define DETAIL(warning) _Pragma(#warning) struct X ## __LINE__ {}
The easiest is an extern function declaration
#define MACRO_END_(LINE) extern void some_inprobable_long_function_name ## LINE(void)
#define MACRO_END MACRO_END_(__LINE__)
the line trick is only there because some compilers with excessive warnings give you warnings on duplicate declarations.
This macro works in any context where a declaration is allowed, so with C99 almost everywhere:
#define DETAIL(warning) _Pragma(#warning) MACRO_END

Will this static assert work correctlly?

I saw recently following code:
#define MY_ASSERT_CONCAT_(a, b) a##b
#define MY_ASSERT_CONCAT(a, b) MY_ASSERT_CONCAT_(a, b)
#define MY_STATIC_ASSERT(e,msg) enum { MY_ASSERT_CONCAT(assert_line_,__LINE__) = 1/int(!!(e)) }
Will it work as expected (BOOST_STATIC_ASSERT-like) ?
Would it work for you?
#define MY_STATIC_ASSERT(e,msg) \
{ \
int MY_ASSERT_CONCAT(assert_line_,__LINE__)[!!e]; \
MY_ASSERT_CONCAT(assert_line_,__LINE__); \
}
It is trying to declare an array of size 1 or 0, depending on expression. It would work only on VC, since GCC allows zero sized arrays(by default). Second usage is just using the variable, so that compiler wont emit "unused variable" warning.
Note that there are no spaces after backslash (\), and it works on VC. Either change it to single line macro, or use appropriate alternative in you compiler.
I recommend using static_assert instead, which will produce elegant error message (and just one error message!).