simplecpp icon indicating copy to clipboard operation
simplecpp copied to clipboard

Fail to expand macros

Open danmar opened this issue 1 year ago • 3 comments

Example code:

#define bitmask(name) setbits(name##_START, name##_BITS)
#define field(name, value) (((value) << name##_START) & name##_MASK)
#define FOO_START 54
#define FOO_BITS 1
#define FOO_MASK bitmask(FOO)
#define FOO field(FOO, 0b1)
FOO_MASK; 

simplecpp output:

a.c:1: syntax error: failed to expand 'FOO_MASK', Invalid ## usage when expanding 'bitmask': Unexpected token ')'

danmar avatar Mar 16 '23 18:03 danmar

I also encounter the problem, cppcheck outputs below

boost/boost/preprocessor/logical/bitor.hpp:27:0: error: failed to expand 'BOOST_MPL_AUX_NA_SPEC2', Invalid ## usage when expanding 'BOOST_PP_BITOR_I': Unexpected token ')' [preprocessorErrorDirective]
#    define BOOST_PP_BITOR_I(x, y) BOOST_PP_BITOR_ ## x ## y
^

any workaround for this?

liuluheng avatar Mar 21 '23 23:03 liuluheng

@liuluheng I typically do not include the boost headers when I run cppcheck. maybe that is a workaround that will work for you.

Some "hack" in the source code could then be needed, one project I work on has this little piece of code:

#ifndef __cppcheck__
/// @note Cppcheck does not like the syntax `% %` used here and reports a syntax error if that is seen in analysis.
#define FOO(...) \
    BOOST_PP_SEQ_FOR_EACH_I(VAR_TO_STRING_COMMA_VAR, % %, BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__))
#endif

When FOO is used throughout this project the macro will not be expanded by cppcheck but that is by intention..

danmar avatar Mar 26 '23 15:03 danmar

#300 seems to be related

hobala avatar Aug 07 '23 17:08 hobala