How to disable C4127 for do {} while(false) [duplicate] - c++

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C/C++: How to use the do-while(0); construct without compiler warnings like C4127?
//file error.h
#define FAIL(message) \
do { \
std::ostringstream ossMsg; \
ossMsg << message; \
THROW_EXCEPTION(ossMsg.str());\
} while (false)
//main.cpp
...
FAIL("invalid parameters"); // <<< warning C4127: conditional expression is constant
...
As you can see the warning is related to the do {} while(false).
I can only figure out the following way to disable the warning:
#pragma warning( push )
#pragma warning( disable : 4127 )
FAIL("invalid parameters");
#pragma warning( pop )
but I don't like this solution.
I also tried to put those macro in error.h without effect.
Any comments on how to suppress this warning in a decent way?
Thank you

The warning is due to the while(false). This site gives an example of how to workaround this problem. Example from site (you'll have to re-work it for your code):
#define MULTI_LINE_MACRO_BEGIN do {
#define MULTI_LINE_MACRO_END \
__pragma(warning(push)) \
__pragma(warning(disable:4127)) \
} while(0) \
__pragma(warning(pop))
#define MULTI_LINE_MACRO \
MULTI_LINE_MACRO_BEGIN \
std::printf("Hello "); \
std::printf("world!\n"); \
MULTI_LINE_MACRO_END
Just insert your code between the BEGIN and END:
#define FAIL(message) \
MULTI_LINE_MACRO_BEGIN \
std::ostringstream ossMsg; \
ossMsg << message; \
THROW_EXCEPTION(ossMsg.str());\
MULTI_LINE_MACRO_END

1) Why not just THROW_EXCEPTION("invalid parameters")?
2) while(true) and break at the end?

Related

Is it possible to use #if 0 inside a multiline macro

I want to implement something like below
#define MACRO_X \
MACRO_1 \
MACRO_2 \
#if condition
MACRO_3 \
#endif
MACRO_4 \
You can't embed preprocessor directives into macros. Instead, do this:
#if condition
#define IF_COND(...) __VA_ARGS__
#else
#define IF_COND(...)
#endif
Then:
#define MACRO_X \
MACRO_1 \
MACRO_2 \
IF_COND( \
MACRO_3 \
) \
MACRO_4 \

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.

pragma in `#define` macro to disable warnings

I'm trying to do something like this:
#ifdef _MSC_VER
#define DISABLE_WARNINGS() \
#pragma warning( push, 0 )
#elif __GNUC__
#define DISABLE_WARNINGS() \
#define DISABLE_WARNINGS \
#pragma GCC diagnostic push \
#pragma GCC diagnostic ignored "-Wall"
#endif
I would like to define a single macro such as "DISABLE_WARNINGS" in my code before including 3rd party headers that produce enormous amount of warnings on W4, and also ensure the code compiles on any platform.
For example:
DISABLE_WARNINGS
#include <gtkmm/buttonbox.h>
#include <gtkmm/box.h>
#include <gtkmm/window.h>
#include <gtkmm/button.h>
ENABLE_WARNINGS
What would be the best way to achieve this with single macro?
In C99 mode, you can use _Pragma instead of #pragma:
#define DISABLE_WARNINGS \
_Pragma("GCC diagnostic push") \
_Pragma("GCC diagnostic ignored \"-Wall\"")

Generic RETURN_IF_FALSE() macro?

I'm trying to clean up some legacy code that contains hundreds of functions with bodies that look like this:
void functionWithSideEffect(Foo* foo)
{
if (foo)
{
// Use `foo' to warm the planet
...
}
}
Obviously, silently failing if a precondition check fails isn't the best idea, so I'd like to refactor this to:
int functionWithSideEffect(Foo* foo)
{
RETURN_IF_FALSE(foo != NULL, "foo is NULL in functionWithSideEffect!");
// Use `foo' to warm the planet
...
}
The following macro seems to work fine for functions that don't return a value:
#define RETURN_IF_FALSE(cond, msg) \
do { \
if (!(cond)) { \
LOG("%s\n", (msg)); \
assert((cond)); \
} \
return; \
} while(0)
And it has the following desirable properties:
It is clear and succinct in usage
It does not silently fail
It will crash in debug builds, but attempt to soldier on in release builds
(Admittedly, for functions returning void, soldiering on in release builds may not always be 'desirable'.)
For functions that do return a value, this macro does the trick:
#define RETURN_VALUE_IF_FALSE(cond, msg, retval ) \
do { \
if (!(cond)) { \
LOG("%s\n", (msg)); \
assert((cond)); \
} \
return(retval); \
} while(0)
My question is: is it possible to write a single RETURN_IF_FALSE macro to handle both void functions and functions returning a value? I sat down to attempt something using a varargs macro and quickly discovered I'm not very good at writing complex macros. I started out with this test program:
#include <stdio.h>
#include <assert.h>
#define RETURN_IF_FALSE(cond, msg, ... ) \
do { \
if (!(cond)) { \
fprintf(stderr, "%s\n", (msg)); \
assert((cond)); \
} \
return (##__VA_ARGS__); \
} while(0)
int main()
{
RETURN_IF_FALSE(1 < 0, "1 is not less than 0!", -1);
return 0;
}
Perhaps not surprisingly, it generated the following compile error:
g++ macro_test.cpp -o macro_test
macro_test.cpp:10:14: error: pasting "(" and "-" does not give a valid preprocessing token
return (##__VA_ARGS__); \
^
macro_test.cpp:16:5: note: in expansion of macro ‘RETURN_IF_FALSE’
RETURN_IF_FALSE(1 < 0, "1 is not less than 0!", -1);
^
Is it even possible to cover both cases with a single macro? I'm using gcc 4.8.1 on Linux. (I can compile with -std=c++11, if it helps...)
UPDATE: To bring this full-circle, here's the implementation I ultimately wound up with based on #Turix's answer and a suggestion from #Deduplicator to move the assert() call up to avoid a double evaluation of the conditional in the 'sunny day' case:
#define RETURN_IF_FALSE(cond, ... ) \
do { \
if (!(cond)) { \
const char* msg = \
"Pre-condition '" #cond "' not met, returning " #__VA_ARGS__ "..."; \
LOG("%s\n", msg); \
assert((cond)); \
return __VA_ARGS__; \
} \
} while(0)
(I decided it wasn't really all that necessary/useful to allow setting of a 'free form' message string, so I just generated a canned one from the condition...)
Just replace this part of your macro return (##__VA_ARGS__); with return __VA_ARGS__ ; and I think it should do what you want (assuming that what you would pass for the return value isn't a complex expression -- if it is, you would need to pre-wrap the parameter with parentheses).
I got this to work.
#include <stdio.h>
#define RET_IF_FALSE(x, y, z) if (!x) { printf(y); return z; }
int a(int *p)
{
RET_IF_FALSE(p, __FUNCTION__, 0);
return *p;
}
void b(int *p)
{
RET_IF_FALSE(p, __FUNCTION__, );
}
int main()
{
int x;
x = a(&x);
b(&x);
x = a(NULL);
b(NULL);
return 0;
}
It may not be the prettiest solution with a trailing comma, and it isn't compliant with standards according to gcc's -pedantic option.
Using:
#define RET_IF_FALSE(x, y, ...) if (!x) { printf(y); return __VA_ARGS__; }
with the rest of the code the same works for gcc with pedantic and -std=c99, and with -std=c++11 in clang++ and g++. Not sure what MS compilers do, as their support for standards is a little less stellar at times (and I haven't got a Windows setup to test on at present).

C++ Preprocessor macro loop __VA_ARGS__ 1 vs 2+ arguments

I'm using the macros from this post looping through my arguments. Everything works great! However, is there a way to combine these two CCB_CREATE and CCB_CREATE_MORE?
I need to extract the first argument object_type to write additional code. The additional object_types will be using the FOR_EACH loop to insert into the map.
The compiler complaints when I only have one argument when using CCB_CREATE_MORE(Type1). To fix that I made another macro to handle that CCB_CREATE(Type1). Hoping to find a clever solution to combine these two into one elegant macro. Any ideas?
#define INSERT_LOADER_MAP(object_type) loader_map.insert(make_pair(#object_type, object_type##Loader::loader()))
#define CCB_CREATE_MORE(object_type,...) \
static CCNode * create##object_type##Node() { \
std::map<std::string, CCNodeLoader*> loader_map; \
std::string classname = #object_type; \
FOR_EACH(INSERT_LOADER_MAP,object_type,__VA_ARGS__); \
return loadCCBFile((classname + ".ccbi").c_str(), loader_map); \
}
#define CCB_CREATE(object_type) \
static CCNode * create##object_type##Node() { \
std::map<std::string, CCNodeLoader*> loader_map; \
std::string classname = #object_type; \
INSERT_LOADER_MAP(object_type); \
return loadCCBFile((classname + ".ccbi").c_str(), loader_map); \
}
The compiler is likely complaining about the trailing comma when the variadic arguments list is empty. GCC and Visual Studio compilers support the non-standard extension ##__VA_ARGS__ to suppress the trailing comma:
#define FOO(fmt, ...) printf(fmt, ##__VA_ARGS__)
The Visual Studio compilers will also suppress the trailing comma even without the ## extension.
See GCC documentation here, and Visual Studio documentation here.
If you need a standards-compliant solution, there is one detailed in an answer to this question.
So if you are using either gcc or Visual Studio, you should be able to use your original macro with this simple change:
#define CCB_CREATE(object_type,...) \
static CCNode * create##object_type##Node() { \
std::map<std::string, CCNodeLoader*> loader_map; \
std::string classname = #object_type; \
FOR_EACH(INSERT_LOADER_MAP,object_type,##__VA_ARGS__); \
return loadCCBFile((classname + ".ccbi").c_str(), loader_map); \
}
Edit:
You would need to use the ##__VA_ARGS__ extension in the FOR_EACH() macro as well, or the more elegant modification suggested by ugoren.
#define FOR_EACH(what, x, ...) FOR_EACH_(FOR_EACH_NARG(x, ##__VA_ARGS__), what, x, __VA_ARGS__)
In addition to Chris Olsen's suggestion, a slight change to the FOR_EACH macro is needed:
#define FOR_EACH(what, ...) FOR_EACH_(FOR_EACH_NARG(__VA_ARGS__), what, __VA_ARGS__)
As a result, FOR_EACH(X, a) will become X(a) (instead of X(a); X();). This eliminates an empty INSERT_LOADER_MAP invocation.