简体   繁体   English

免费的C99代码静态检查器

[英]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." 我正在寻找一个C99代码(包括GCC扩展)的免费静态检查器,能够明确地说“这些预处理器宏总是被定义”。

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. 编译器(Microchip的C32,基于GCC)根据所选处理器设置宏,然后在PIC32头文件中使用该宏来选择要包含的特定于处理器的头文件。 cppcheck therefore fails because it detects the 30 different #ifdef s used to select one of the many possible PIC32 processors, tries to analyse all possible combinations of these plus all other #define s, and fails. 因此, cppcheck失败了,因为它检测到用于选择许多可能的PIC32处理器之一的30种不同的#ifdef ,试图分析这些加上所有其他#define的所有可能组合,并失败。

For example, if splint could process C99 code, I would use 例如,如果splint可以处理C99代码,我会使用

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. 另一个问题是PIC32工具链编译器被称为pic32-gcc而不仅仅是gcc ,尽管我还没有达到需要考虑的程度。

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). 更新#1 - 我感兴趣的一件事,但与这个问题正交,是Eclipse集成(不必为30多个编译单元编写一个makefile很好)。 I asked about this on the Eclipse forums (although the discussion there is more about integration into Eclipse). 我在Eclipse论坛上询问过这个问题(虽然讨论的内容更多是关于Eclipse的集成)。 Nothing groundbreaking. 没有什么是开创性的。

Update #2 - just tried scan-build from clang , using: 更新#2 - 刚从clang尝试scan-build ,使用:

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: ...(也没有--use-cc标志)但我得到的只是典型的构建输出,其中一个例子是:

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. 因此,根据scan-build ,我的代码是完美的,或者它没有做任何事情。 I'm not sure what a good test might be to see if it is working. 我不确定一个好的测试可能是看它是否有效。

Clang's static analyzer should work. Clang的静态分析仪应该可以工作。

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. 源代码#defines另一个选项是你可以使用一些预处理器语句在源代码上运行cpp ,然后通过静态分析器运行结果代码。

You could just add some code like this to the top of your header that guarantees that it's defined: 你可以在标题的顶部添加一些这样的代码,以保证它的定义:

#ifndef MACRO_I_NEED
#error "MACRO_I_NEED should be defined"
#define MACRO_I_NEED  // to appease cppcheck
#endif

Instead of using scan-build with clang, consider swapping out gcc altogether! 不要使用带有clang的scan-build,而是考虑完全交换gcc! Clang's C support is stable (and does its best to emulate gcc), and should handle your code just fine. Clang的C支持是稳定的(并且最好模仿gcc),并且应该很好地处理你的代码。

Try something like make -j3 CC=clang and see what happens! 试试make -j3 CC=clang类的东西,看看会发生什么!

PS. PS。 That syntax might be totally wrong. 这种语法可能完全错误。 Haven't used makefiles in ages (CMake is amazing btw). 多年没有使用过makefile(CMake是惊人的btw)。

Depending on what actual analyses you want to run on your code, you might take a look at Frama-C . 根据您希望在代码上运行的实际分析,您可以查看Frama-C It uses whatever C preprocessor you tell it to, so you can use PIC32's CPP if you want. 它使用您告诉它的任何C预处理器,因此您可以根据需要使用PIC32的CPP。

This might not directly give you the solution, but you might consider having a look to Coverity, which is a proprietary static syntax analyser, but that is free of charge for OS projects. 这可能不会直接为您提供解决方案,但您可能会考虑查看Coverity,这是一个专有的静态语法分析器,但对于OS项目是免费的。 It should do the job concerning your needs! 它应该做你需要的工作!

Cheers! 干杯!

You can use a tool like sunifdef to partially preprocess the source code according to the assumed defined macros. 您可以使用sunifdef之类的工具根据假定的定义宏对源代码进行部分预处理。 You'd have to make copies of system and library headers that are affected by those defines and process them as well. 您必须制作受这些定义影响的系统和库头的副本,并对其进行处理。 Then when doing the static analysis you would specify a different include path pointing to your already processed headers. 然后,在进行静态分析时,您将指定指向已处理标头的不同包含路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM