In a C/C++ code project, I would like to find all if/else sentences which are not using curly braces. Is there any compiler check or utility to find them?
The main problem is that I want to redefine some debug macros to avoid traces evaluation in some conditions (does any know any other solution?). And I need to find them to avoid the “dangling-else problem”, as pointed out in "if" block without curly braces makes subsequent "else if" nested
Code pieces:
Before modification:
DEBUG GET_DEBUG_DST(DEBUG_LEVEL_DEBUG).nospace() << DEBUG_PREFIX << __PRETTY_FUNCTION__
Now we would use an if
without braces inside:
DEBUG if (getDebugmode(DEBUG_LEVEL_DEBUG)) GET_DEBUG_DST(DEBUG_LEVEL_DEBUG).nospace() << DEBUG_PREFIX << __PRETTY_FUNCTION__
Example of problematic code:
if (my condition)
DEBUG << "hi there";
else
{some more code;}