Ignore system headers in clang-tidy

2020-07-01 04:41发布

tldr;> How do I hide warnings from system headers in clang-tidy?

I have the following minimal example source file, which triggers a clang-tidy warning in the system headers:

#include <future>

int main() {
  std::promise<int> p;
  p.set_value(3);
}

Calling it with libstdc++ 7.0.1 using clang-tidy 4.0.0 on Ubuntu 17.04:

$ clang-tidy main.cpp -extra-arg=-std=c++14

yields

Running without flags.
1 warning generated.
/usr/lib/gcc/x86_64-linux-gnu/7.0.1/../../../../include/c++/7.0.1/mutex:693:5: warning: Address of stack memory associated with local variable '__callable' is still referred to by the global variable '__once_callable' upon returning to the caller.  This will be a dangling reference [clang-analyzer-core.StackAddressEscape]
    }
    ^
/home/user/main.cpp:5:3: note: Calling 'promise::set_value'
  p.set_value(3);
  ^
/usr/lib/gcc/x86_64-linux-gnu/7.0.1/../../../../include/c++/7.0.1/future:1094:9: note: Calling '_State_baseV2::_M_set_result'
      { _M_future->_M_set_result(_State::__setter(this, std::move(__r))); }
        ^
/usr/lib/gcc/x86_64-linux-gnu/7.0.1/../../../../include/c++/7.0.1/future:401:2: note: Calling 'call_once'
        call_once(_M_once, &_State_baseV2::_M_do_set, this,
        ^
/usr/lib/gcc/x86_64-linux-gnu/7.0.1/../../../../include/c++/7.0.1/mutex:691:11: note: Assuming '__e' is 0
      if (__e)
          ^
/usr/lib/gcc/x86_64-linux-gnu/7.0.1/../../../../include/c++/7.0.1/mutex:691:7: note: Taking false branch
      if (__e)
      ^
/usr/lib/gcc/x86_64-linux-gnu/7.0.1/../../../../include/c++/7.0.1/mutex:693:5: note: Address of stack memory associated with local variable '__callable' is still referred to by the global variable '__once_callable' upon returning to the caller.  This will be a dangling reference
    }

I want to hide warnings in system headers. I tried the following:

$ clang-tidy -extra-arg=-std=c++14 main.cpp -header-filter=$(realpath .) -system-headers=0

but the warning still shows.

2条回答
三岁会撩人
2楼-- · 2020-07-01 04:48

I ran into this issue as well, and spent some time trying to figure it out, but I could not see a way to disable this type of warning in clang-tidy.

From reading this discussion on the LLVM issue tracker regarding a similar issue, I get the impression that the problem is that from clang-tidy's perspective, the warning is actually located in main.cpp, because the call to set_value is from there.

My workaround has been to disable the static analysis checks in clang-tidy, and use the scan-build utility to run clang's static analysis, which seems to avoid these problems. For example, using your main.cpp:

$ scan-build-3.9 clang++ -std=c++14 main.cpp 
scan-build: Using '/usr/lib/llvm-3.9/bin/clang' for static analysis
In file included from main.cpp:1:
In file included from /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/future:39:
/usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/mutex:621:11: warning: Address of stack memory associated with local variable '__callable' is still referred to by the global variable '__once_callable' upon returning to the caller.  This will be a dangling reference
      if (__e)
          ^~~
1 warning generated.
scan-build: Removing directory '/tmp/scan-build-2017-12-02-112018-13035-1' because it contains no reports.
scan-build: No bugs found.

The analyzer finds the same error in a system header, but it's smart enough not to include it in the final report. ("No bugs found")

You'll still need to run clang-tidy separately if you are interested in the style guide type warnings, like modernize-* or readability-*.

查看更多
Summer. ? 凉城
3楼-- · 2020-07-01 04:56

I initially came to the conclusion that this was not possible, but I've got a hack that gets pretty close.

When I compile I use CMAKE_EXPORT_COMPILE_COMMANDS=ON which generates a compile_commands.json file showing what commands were passed to the compiler for each cpp file compiled. When I run clang-tidy I give it the -p option to point to the directory containing this file.

A typical entry in this file looks something like this:

{
  "directory": "/project/build/package1",
  "command": "/usr/bin/clang++-9 -I/opt/thirdparty/include -isystem /usr/include . . . /project/src/package1/src/foo.cpp",
  "file": "/project/src/package1/src/foo.cpp"
},

If I re-write this file so that -I/opt/thirdparty/include becomes -isystem /opt/thirdparty/include the previously problematic headers in /opt/third-party/include are ignored because clang-tidy will see them as system headers.

I use sed to rewrite the file

# Trick clang-tidy into thinking anything in /usr/include is a system header
sed -i 's|-I/usr/include|-isystem /usr/include|g' build/compile_commands.json
# Run clang-tidy using the run-clang-tidy python wrapper
run-clang-tidy.py -p build -header-filter .* $(find src -iname "*.cpp")
查看更多
登录 后发表回答