Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Free static checker for C99 code

I am looking for a free static checker for C99 code (including GCC extensions) with the ability to explicitly say "these preprocessor macros are always defined."

I need that last part because I am compiling embedded code for a single target processor. The compiler (Microchip's C32, GCC based) sets a macro based on the selected processor, which is then used in the PIC32 header files to select a processor-specific header file to include. cppcheck therefore fails because it detects the 30 different #ifdefs used to select one of the many possible PIC32 processors, tries to analyse all possible combinations of these plus all other #defines, and fails.

For example, if splint could process C99 code, I would use

splint -D__PIC32_FEATURE_SET__=460 -D__32MX460F512L__ \
-D__LANGUAGE_C__ -I/path/to/my/includes source.c

An additional problem is that the PIC32 toolchain compiler is called pic32-gcc and not just gcc, although I haven't yet gotten to the point of needing to account for this.

Update #1 - One thing I'm interested in, but is orthogonal to this question, is Eclipse integration (it'd be nice not to have to write a makefile for 30+ compilation units). I asked about this on the Eclipse forums (although the discussion there is more about integration into Eclipse). Nothing groundbreaking.

Update #2 - just tried scan-build from clang, using:

scan-build --use-cc=/usr/local/bin/pic32-gcc make -B -k all

...(also without the --use-cc flag) but all I got was the typical build output, an example of which is:

Building file: ../src/MoreMath.c
Invoking: PIC C32 C Compiler
pic32-gcc -D__DEBUG -I/usr/local/pic32-libs/include -O0 -Wall -c -fmessage-length=0 -std=gnu99 -Werror-implicit-function-declaration -MMD -MP -MF"src/MoreMath.d" -MT"src/MoreMath.d" -mprocessor=32MX460F512L -D__DEBUG -g -o"src/MoreMath.o" "../src/MoreMath.c"
Finished building: ../src/MoreMath.c

...and at the end:

Building target: MyBinary.elf
Invoking: PIC C32 C Linker
pic32-gcc -Wl,-Map,MyBinary.map -mprocessor=32MX460F512L --defsym=__MPLAB_DEBUG=1 -o"MyBinary.elf" <<ALL OF MY *.o FILES HERE>>
Finished building target: MyBinary.elf

scan-build: Removing directory '/tmp/scan-build-2010-06-21-1' because it contains no reports.

So either my code is perfect according to scan-build, or it's not doing anything. I'm not sure what a good test might be to see if it is working.

like image 204
detly Avatar asked Apr 27 '10 03:04

detly


People also ask

Is Jtest a static analysis tool?

By analyzing the execution paths through the code, Jtest's static analysis can detect potential issues early in the development stage, such as null pointer exceptions, division by zero, array out of bound problems, and more. To manage complexity, Jtest helps you understand code metrics.

What is Cppcheck tool?

Cppcheck is a static analysis tool for C/C++ code. Unlike C/C++ compilers and many other analysis tools it does not detect syntax errors in the code. Cppcheck primarily detects the types of bugs that the compilers normally do not detect.

How do I use Cppcheck tool?

Running Cppcheck on Selected FilesSelect Analyze > Cppcheck. In the Binary field, enter the path to the Cppcheck executable file. In the Checks group, select the checks to perform. Note: By default, Cppcheck uses multiple threads to perform checks.


1 Answers

Clang's static analyzer should work.

Another option with the source code #defines is that you could run cpp over the source code with some of the preprocessor statements, and then run that resultant code through a static analyzer.

like image 71
Bill Lynch Avatar answered Sep 21 '22 00:09

Bill Lynch