danmar / simplecpp

C++ preprocessor
BSD Zero Clause License
209 stars 83 forks source link

syntax error: failed to expand 'HASH_TOKEN_IN_MACRO' #328

Open chrchr-github opened 1 year ago

chrchr-github commented 1 year ago

From https://trac.cppcheck.net/ticket/12105

void g(const char*);
#define HASH_TOKEN_IN_MACRO( A ) g( #A )
void f() {
    HASH_TOKEN_IN_MACRO (
#ifdef SW
        "Message 1"
#else
        "Message 2"
#endif
    );
}

foo.cpp:5: syntax error: failed to expand 'HASH_TOKEN_IN_MACRO', it is invalid to use a preprocessor directive as macro parameter

danmar commented 1 year ago

This code is undefined behavior.

https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf

page 152

If there are sequences of preprocessing tokens within the list of arguments that would otherwise act as preprocessing directives,150) the behavior is undefined

parrotrueper commented 10 months ago

@danmar Agreed that the code is bad, the linter should flag it and carry on, rather than grind to a halt and stop checking the rest of the code. As a side note this code was created to evaluate linters. Out of five linters Cppcheck was the only one that stopped checking after encountering this error.

danmar commented 10 months ago

@parrotrueper I would hope you can consider to fix the evaluation code somehow then. And evaluate cppcheck using that. A user would fix the code and then recheck..

danmar commented 10 months ago

alright I guess we can reopen this. Fixing this doesn't mean we have to remove the error message...