i am trying to use cppcheck for an embedded C project and encountered a problem, where cppcheck behaves different than gcc. The Project uses drivers from Atmel, which use a lot of preprocessor logic to set defines for chiptype, chipfamily, etc.
Now cppcheck seems to handle #if conditionals different, than gcc.
The following simplified (non-) working example ilustrates the problem:
test.c
#include<stdio.h>#define FOODEF defined(FOO)intmain(void){#if FOODEFprintf("FOO defined");#elseprintf("FOO not defined");#endifreturn0;}
i have seen your commit yesterday - works great for the simple example. :)
unluckily the problem i initally stumbled with (code from atmel) is quite complex and uses string concatenation insde a macro (to build the macro name to check for).
here is a minimal example, whitch shoud behave different, whether __FOO__ is defined or not:
#include<stdio.h>#define macro(var) (defined(__ ## var ## __))intmain(void){#if macro(FOO)printf("__FOO__ defined\r\n");#elseprintf("__FOO__ not defined\r\n");#endifreturn0;}
For example, if we define __SAM4S16C__ (by passing it as commandline paramteter -D__SAM4S16C__). The defines SAM4S16, SAM4S and SAM should all evaluate to 1, so that #if SAM is true.
The code for other chip families is quite similar.
I allready tried to solve this myself by extending the code you committed, but got stuck. Seems linke i am missing something about how such function like macros are being handled in simplecpp. Maybe this needs to be done in the macro expansion or evaluation methods?
It would be great if you could take a look at this or at least point me to the place where this should be implemented.
Regards
Robert
Last edit: Robert Schwarzelt 2017-05-29
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi,
i am trying to use cppcheck for an embedded C project and encountered a problem, where cppcheck behaves different than gcc. The Project uses drivers from Atmel, which use a lot of preprocessor logic to set defines for chiptype, chipfamily, etc.
Now cppcheck seems to handle #if conditionals different, than gcc.
The following simplified (non-) working example ilustrates the problem:
test.c
preprocessing using gcc gives:
cppcheck does evaluate the "#if FOO" condition as false:
Is this a error in cppcheck's preprocessing routines or some kind of undefined behavior?
Regards
Robert
Last edit: Robert Schwarzelt 2017-05-23
Hello!
Interesting code. I don't know. But I think we should implement this.
Hi Daniel,
i have seen your commit yesterday - works great for the simple example. :)
unluckily the problem i initally stumbled with (code from atmel) is quite complex and uses string concatenation insde a macro (to build the macro name to check for).
here is a minimal example, whitch shoud behave different, whether
__FOO__
is defined or not:some real world (atmel) code using this kind of expression:
https://github.com/ARMmbed/mbed-hal-atmel-common/blob/master/source/utils/parts.h
below is a short excerpt of what they are doing there.
For example, if we define
__SAM4S16C__
(by passing it as commandline paramteter-D__SAM4S16C__
). The definesSAM4S16
,SAM4S
andSAM
should all evaluate to 1, so that#if SAM
is true.The code for other chip families is quite similar.
I allready tried to solve this myself by extending the code you committed, but got stuck. Seems linke i am missing something about how such function like macros are being handled in simplecpp. Maybe this needs to be done in the macro expansion or evaluation methods?
It would be great if you could take a look at this or at least point me to the place where this should be implemented.
Regards
Robert
Last edit: Robert Schwarzelt 2017-05-29
ok please report a new issue in the simplecpp issue tracker.
if you are interested to investigate this ... please do it. It's probably not very simple. But feel free to take a look at
https://github.com/danmar/simplecpp/commit/96ade8d9239962fa04702747ce6372b47ec24ef5
that code should be fixed.
you might be able to call some function that will preprocess the "defined argument". maybe you can debug and see how expandHashHash is used.