C compilers and cppcheck

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
WIckedWitch
Posts: 276
Joined: Thu 29 Mar 2018, 22:13
Location: West Wales bandit country

C compilers and cppcheck

#1 Post by WIckedWitch »

Just for a laugh, on a system safety email list, I posted a few programs that show how the FOSS C/C++ checker program cppcheck in some cases does no better than well-known compilers. Have a look at this compact and bijou piece of mischief:

/* cimplex-d-0007.c : tests order of evaluation of function designator and argument list in a function call*/
#include <stdio.h>

static void f0(int n) { printf("\ncall was f0(%i) - func. designator evaluated first\n", n); return; }
static void f1(int n) { printf("\ncall was f1(%i) - argument list is evaluated first\n", n); return; }

/* static void (*fpa[2])(int n) = { f0, f1 };*/

int main(void)
{
int i = 1;

void (*fpa[2])(int n) = { f0, f1 }; /* declare & initialise array of pointers pointing to f0 and f1 */

/* now make a function call to check the order of evaluation of the func. designator and the arg. list */
/* f0(1) occurs if func. designator is evaluated first, f1(0) occurs if argument list is evaluated first */

(*fpa)(i=(1-i)); /* note the symmetric double side effect on i here */

return 0;
}



This program tests the order of evaluation of the operands of the function-call operator. Clang and gcc flag the double side-effect but cppcheck and tcc do not.

Worse, cppcheck gives the diagnostic:

cimplex-d-0007.c:18: error: Uninitialized variable: fpa

which is just plain wrong!

In this and other tests. I've found that clang-4.0 does no worse than cppcheck in diagnosis and in some cases does better.
Sometimes I post mindfully, sometimes not mindfully, and sometimes both mindfully and not mindfully. It all depends on whether and when my mind goes walkies while I'm posting :?

Post Reply