如何将Clang静态分析仪与Cortex-M项目一起使用?

时间:2017-05-26 14:59:25

标签: c++ arm clang cortex-m clang-static-analyzer

我正在为基于ARM Cortex-M的MCU开发“裸机”C / C ++应用程序。 我想将Clang静态分析器整合到我的构建系统中。

所以而不是

arm-none-eabi-g++ ....

我现在用

scan-build arm-none-eabi-g++ ...

这似乎工作正常,但我得到了一堆关于CMSIS头文件和我从芯片制造商那里得到的东西的消息:

  • 错误:从指针转换为较小的类型'uint32_t'(又名'unsigned int')丢失信息

我同意他们应该使用uintptr_t而不是int32_t,但不能重写整个代码库和CMSIS标头。我可以告诉scan-build这个平台上的指针实际上是32位,所以不会丢失信息吗?

我试图告诉它考虑我使用的是哪个平台。但如果我使用scan-build --analyzer-target=arm-none-eabi,我会得到:

  • 致命错误:找不到'cstring'文件
  • 错误:asm中未知的注册名称'vfpcc'

再次,在CMSIS标题中。

我的问题是:

  • 当我使用cstring时,为什么分析器找不到--analyzer-target=arm-none-eabi标题?
  • 如何告诉分析仪我的指针是真正的32位?
  • 为什么它会给我关于那个未知寄存器的错误?

或者:

  • 如何消除CMSIS标题的警告?

修改

我也尝试了以下建议,但它没有用,导致了相同的错误:

scan-build
    --use-cc=/usr/bin/arm-none-eabi-gcc
    --use-c++=/usr/bin/arm-none-eabi-g++
    --analyzer-target=arm-none-eabi
    arm-none-eabi-g++ ...

注意:我使用 qbs 构建系统,并使用scan-build文件中的cpp.compilerWrapper属性生成.qbs次调用。< / p>

典型的命令如下所示:

/usr/bin/scan-build --use-cc=/usr/bin/arm-none-eabi-gcc --use-c++=/usr/bin/arm-none-eabi-g++ --analyzer-target=arm-none-eabi /usr/bin/arm-none-eabi-g++ -g -O0 -Wall -Wextra -mcpu=cortex-m4 -mfloat-abi=hard -mthumb -mabi=aapcs -mno-sched-prolog -mabort-on-noreturn -fdata-sections -ffunction-sections -fno-strict-aliasing -fno-builtin -specs=nosys.specs -specs=nano.specs -static -nodefaultlibs -Wdouble-promotion -ggdb -g3 -mfpu=fpv4-sp-d16 -pipe -frandom-seed=0x633bf14c -Wdate-time -fno-exceptions -fno-rtti -fvisibility=default -Wall -Wextra -Wpedantic -Wno-unused-function -DS1_USE_SEGGER_RTT -DEFM32WG940F256 -D__HEAP_SIZE=0 -I/home/Timur/Projects/MyProject/my-software/dependencies/platform/emdrv/common/inc -I/home/Timur/Projects/MyProject/my-software/dependencies/platform/emdrv/config -I/home/Timur/Projects/MyProject/my-software/dependencies/platform/emdrv/dmadrv/inc -I/home/Timur/Projects/MyProject/my-software/dependencies/platform/emdrv/dmadrv/config -I/home/Timur/Projects/MyProject/my-software/dependencies/hardware/kit/common/drivers -I/home/Timur/Projects/MyProject/my-software/dependencies/platform/emdrv/gpiointerrupt/inc -I/home/Timur/Projects/MyProject/my-software/dependencies/platform/emdrv/nvm/inc -I/home/Timur/Projects/MyProject/my-software/dependencies/platform/emdrv/nvm/config -I/home/Timur/Projects/MyProject/my-software/dependencies/platform/emdrv/rtcdrv/inc -I/home/Timur/Projects/MyProject/my-software/dependencies/platform/emdrv/rtcdrv/config -I/home/Timur/Projects/MyProject/my-software/dependencies/platform/emdrv/sleep/inc -I/home/Timur/Projects/MyProject/my-software/dependencies/platform/emdrv/spidrv/inc -I/home/Timur/Projects/MyProject/my-software/dependencies/platform/emdrv/spidrv/config -I/home/Timur/Projects/MyProject/my-software/dependencies/platform/emdrv/uartdrv/inc -I/home/Timur/Projects/MyProject/my-software/dependencies/platform/emdrv/uartdrv/config -I/home/Timur/Projects/MyProject/my-software/dependencies/platform/emdrv/ustimer/inc -I/home/Timur/Projects/MyProject/my-software/dependencies/platform/emdrv/ustimer/config -I/home/Timur/Projects/MyProject/my-software/dependencies/platform/emlib/inc -I/home/Timur/Projects/MyProject/my-software/dependencies/platform/CMSIS/Include -I/home/Timur/Projects/MyProject/my-software/dependencies/platform/Device/SiliconLabs/EFM32WG/Include -I/home/Timur/Projects/MyProject/my-software/dependencies/RTT -std=c++0x -o /home/Timur/Projects/MyProject/build-myproject-my-software-arm_none_eabi-Debug/qtc_arm_none_a793425c-debug/myproject-my-software.qtc-arm-none-a793425c.7e216384/.obj/e6c416981c959a66/efm32-serial-port.cpp.o -c /home/Timur/Projects/MyProject/my-software/source/utilities/hal/efm32-serial-port.cpp
In file included from /home/Timur/Projects/MyProject/my-software/source/utilities/hal/efm32-serial-port.cpp:24:
In file included from /home/Timur/Projects/MyProject/my-software/source/utilities/hal/efm32-serial-port.h:27:
In file included from /home/Timur/Projects/MyProject/my-software/source/utilities/hal/abstract-serial-port.h:32:
/home/Timur/Projects/MyProject/my-software/source/utilities/hal/../core/callback.h:32:10: fatal error: 'cstddef' file not found
#include <cstddef>
         ^~~~~~~~~
1 error generated.
scan-build: Using '/usr/bin/clang-4.0' for static analysis
scan-build: 0 bugs found.
scan-build: The analyzer encountered problems on some source files.
scan-build: Preprocessed versions of these sources were deposited in '/tmp/scan-build-2017-07-27-194505-8969-1/failures'.
scan-build: Please consider submitting a bug report using these files:
scan-build:   http://clang-analyzer.llvm.org/filing_bugs.html

2 个答案:

答案 0 :(得分:1)

您似乎也需要指定use-cc=/usr/bin/arm-none-eabi-gcc标记:http://blog.canyonbliss.net/static-code-analysis-with-scan-build-while-cross-compiling/

答案 1 :(得分:0)

缺少“cstring”是因为clang不包含STL标头。我遇到了同样的问题并添加-I<path_to_st_includes>修复了它。

所以在我的QBS中我有类似的东西:

property string repoDir: "C:/Program Files (x86)/Atmel/Studio/7.0/packs"
cpp.includePaths: [
        repoDir + "/arm/CMSIS/4.2.0/CMSIS/Include",
        "C:/Program Files (x86)/Atmel/Studio/7.0/toolchain/arm/arm-gnu-toolchain/arm-none-eabi/include/c++/6.3.1",
        "C:/Program Files (x86)/Atmel/Studio/7.0/toolchain/arm/arm-gnu-toolchain/arm-none-eabi/include"
        ]

对于vfpcc来说,这似乎是clang的一个问题。 Chromium项目甚至已修复删除vfpcchttps://codereview.chromium.org/411803002/

的使用情况

到目前为止,我发现的唯一解决方案是禁用静态分析的硬件FPU。

一种方法是在clang上使用-mfpu=none。但是gcc会抱怨none无效......而且由于Qt Creator在分析之前构建项目,构建将失败并且分析将无法启动。

另一种方法是定义__SOFTFP__。这会强制__FPU_USED0并禁用所有使用vfpcc的函数。