simplecpp icon indicating copy to clipboard operation
simplecpp copied to clipboard

syntax error: failed to expand 'HASH_TOKEN_IN_MACRO'

Open chrchr-github opened this issue 2 years ago • 4 comments

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

chrchr-github avatar Oct 23 '23 08:10 chrchr-github

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

danmar avatar Nov 04 '23 11:11 danmar

@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.

parrotrueper avatar Jan 03 '24 19:01 parrotrueper

@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 avatar Jan 04 '24 09:01 danmar

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

danmar avatar Jan 04 '24 18:01 danmar