llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.97k stars 11.94k forks source link

scan-build ignores @something.rsp from cmdline of original compiler #85113

Open socketpair opened 7 months ago

socketpair commented 7 months ago

The following script demonstrates the problem:

#!/bin/bash

rm -rf /tmp/xxx
mkdir /tmp/xxx
cd /tmp/xxx

cat >test.c <<EOF
#include <some_test.h>
int main() { return 0; }
EOF

mkdir somedir
touch somedir/some_test.h

echo '-Isomedir' > test.rsp

gcc @test.rsp test.c # WORKS!

scan-build -v -v -v gcc @test.rsp test.c # FAILS to compile
scan-build: Using '/usr/bin/clang-17' for static analysis
scan-build: Emitting reports for this run to '/tmp/scan-build-2024-03-13-230008-497742-1'.
gcc @test.rsp test.c

[LOCATION]: /tmp/xxx
#SHELL (cd '/tmp/xxx' && '/usr/bin/clang-17' '-cc1' '-triple' 'x86_64-redhat-linux-gnu' '-analyze' '-disable-free' '-clear-ast-before-backend' '-disable-llvm-verifier' '-discard-value-names' '-main-file-name' 'test.c' '-analyzer-checker=core' '-analyzer-checker=apiModeling' '-analyzer-checker=unix' '-analyzer-checker=deadcode' '-analyzer-checker=security.insecureAPI.UncheckedReturn' '-analyzer-checker=security.insecureAPI.getpw' '-analyzer-checker=security.insecureAPI.gets' '-analyzer-checker=security.insecureAPI.mktemp' '-analyzer-checker=security.insecureAPI.mkstemp' '-analyzer-checker=security.insecureAPI.vfork' '-analyzer-checker=nullability.NullPassedToNonnull' '-analyzer-checker=nullability.NullReturnedFromNonnull' '-analyzer-output' 'plist' '-w' '-setup-static-analyzer' '-mrelocation-model' 'static' '-mframe-pointer=all' '-fmath-errno' '-ffp-contract=on' '-fno-rounding-math' '-mconstructor-aliases' '-funwind-tables=2' '-target-cpu' 'x86-64' '-tune-cpu' 'generic' '-debugger-tuning=gdb' '-fcoverage-compilation-dir=/tmp/xxx' '-resource-dir' '/usr/bin/../lib/clang/17' '-internal-isystem' '/usr/bin/../lib/clang/17/include' '-internal-isystem' '/usr/local/include' '-internal-isystem' '/usr/bin/../lib/gcc/x86_64-redhat-linux/13/../../../../x86_64-redhat-linux/include' '-internal-externc-isystem' '/include' '-internal-externc-isystem' '/usr/include' '-fdebug-compilation-dir=/tmp/xxx' '-ferror-limit' '19' '-fgnuc-version=4.2.1' '-analyzer-display-progress' '-analyzer-output=html' '-faddrsig' '-D__GCC_HAVE_DWARF2_CFI_ASM=1' '-o' '/tmp/scan-build-2024-03-13-230008-497742-1' '-x' 'c' 'test.c')
test.c:1:10: fatal error: 'some_test.h' file not found
    1 | #include <some_test.h>
      |          ^~~~~~~~~~~~~
1 error generated.
test.c:1:10: fatal error: 'some_test.h' file not found
    1 | #include <some_test.h>
      |          ^~~~~~~~~~~~~
1 error generated.
scan-build: Analysis run complete.
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-2024-03-13-230008-497742-1/failures'.
scan-build: Please consider submitting a bug report using these files:
scan-build:   http://clang-analyzer.llvm.org/filing_bugs.html
$ rpm -qa | grep anal
clang-analyzer-17.0.6-2.fc39.noarch

Fedora 39, x86-64.

socketpair commented 7 months ago

This is EXTREMELY important, since scan-builds does not work correctly on cmake-generated makefiles in my environment (cross-compiling with mingw)

EugeneZelenko commented 7 months ago

@socketpair: You could generate compile database with CMake and then use run-clang-tidy to run Static Analyzer checks from Clang-tidy.

socketpair commented 7 months ago

@EugeneZelenko

  1. Does clang-tidy do the same set of checks ?
  2. Can you please give an example of commandline how to do everything? :)
  3. Anyway, cmake adds .rsp files, so clang fails to compile internally.
EugeneZelenko commented 7 months ago
  1. -checks=-*,clang-analyzer-* should be enable all released Static Analyzer checks. See also https://clang.llvm.org/extra/clang-tidy/index.html.
  2. run-clang-tidy.py -quiet -header-filter "*" -p <directory where compile database is located> -checks=<checks> -j <number process to spawn>
  3. You could post-process compile database to remove unwanted files. It's plain JSON.

You also need to run CMake with CMAKE_EXPORT_COMPILE_COMMANDS=ON.

socketpair commented 7 months ago

@EugeneZelenko Seems, I understand what you meant, but anyway, compiler's cmdline INCLUDES @xxx.rsp, so even your way will fail, since clang's analyzer will not be able to include some header files.

So, this bug is still actual.

As a workaround I can flatten these files to long cmdline, but this is a crutch.