I have the following code:
bad_code.c
/* Bad Macro */
#define HASH_TOKEN_IN_MACRO( A ) printf ( #A )
/* array should not be partially initialised */
static int32_t s32ArrayBadInit[3]={0,1};
/* value written beyond the size of the buffer */
void buffer_overflow(void)
{
static uint32_t u32Buffer[2];
u32Buffer[3] = 0u;
/* objects should not be initialised more than once */
s32ArrayBadInit[0]=2;
}
/* macro abuse breaks Cppcheck */
void tokens_in_macros(void)
{
HASH_TOKEN_IN_MACRO (
#ifdef SW /* Non-compliant */ // <---- Cppcheck error reported here but no other error
"Message 1"
#else /* Non-compliant */
"Message 2"
#endif /* Non-compliant */
);
}
running Cppcheck on this code as follows:
cppcheck ./bad_code.c
Reports a single error on line #ifdef SW but no other error.
How can I get Cppcheck to report all the other issues with this code sample?
Cppcheck parses the code from a file into some internal representation, then does some analysis on that internal representation to statically analyze the code.
If it runs into an error that prevents it from correctly parsing the code in the first place, then it can't build that internal representation and run the analysis on that source file.
preprocessorErrorDirectiveis one of those errors that prevents it from parsing, so cppcheck won't be able to parse the rest of the file when it hits one of these.If you run
cppchecklike this and generate a report, then its a little clearer that this is happening:cppcheck bad_code.c --checkers-report=report.txtreport.txt:
That being said, this seems to compile with gcc, but I don't know if this "macro abuse" construct is compiler-specific behavior or ISO-standard C. If it is standard, you could report a bug with cppcheck https://trac.cppcheck.net/