title: How to use Clang static analyzer with a Cortex-M project?
body: <p>I'm developing a "bare-metal" C/C++ application for an ARM Cortex-M based MCU.
  I'd like to incorporate the Clang static analyzer to my build system.</p>
  
  <p>So instead of</p>
  
  <pre><code>arm-none-eabi-g++ ....
  </code></pre>
  
  <p>I now use</p>
  
  <pre><code>scan-build arm-none-eabi-g++ ...
  </code></pre>
  
  <p>This seems to work okay, but I get a bunch of messages about the CMSIS header files and the stuff that I got from the chip manufacturer:</p>
  
  <ul>
  <li>error: cast from pointer to smaller type 'uint32_t' (aka 'unsigned int') loses information</li>
  </ul>
  
  <p>I agree that they <strong>should have</strong> used <code>uintptr_t</code> instead of <code>int32_t</code> but can't rewrite their entire code base and the CMSIS headers as well. Can I tell <code>scan-build</code> that pointers on this platform are actually 32-bit so there is no loss of information?</p>
  
  <p>I tried to tell it to consider which platform I use. But if I use <code>scan-build --analyzer-target=arm-none-eabi</code>, I get:</p>
  
  <ul>
  <li>fatal error: 'cstring' file not found</li>
  <li>error: unknown register name 'vfpcc' in asm</li>
  </ul>
  
  <p>Again, in the CMSIS headers.</p>
  
  <p>My questions are:</p>
  
  <ul>
  <li>Why can't the analyzer find the <code>cstring</code> header when I use <code>--analyzer-target=arm-none-eabi</code>?</li>
  <li>How can I tell the analyzer that my pointers are truly 32-bit?</li>
  <li>Why does it give me that error about that unknown register?</li>
  </ul>
  
  <p>Or alternatively:</p>
  
  <ul>
  <li>How can I silence those warnings for the CMSIS headers?</li>
  </ul>
  
  <p><strong>EDIT</strong></p>
  
  <p>I also tried the following suggestion but it didn't work, it resulted in the same errros:</p>
  
  <pre><code>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++ ...
  </code></pre>
  
  <p>Note: I use the <em><a href="http://doc.qt.io/qbs/" rel="nofollow noreferrer">qbs</a></em> build system, and generated the <code>scan-build</code> calls using the <code>cpp.compilerWrapper</code> property in my <code>.qbs</code> file.</p>
  
  <p>A typical command looks like this:</p>
  
  <pre><code>/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 &lt;cstddef&gt;
           ^~~~~~~~~
  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
  </code></pre>