clang-tidy: How to suppress warnings?

后端 未结 4 1800
抹茶落季
抹茶落季 2020-12-30 04:02

I recently started experimenting with the clang-tidy tool of llvm. Now I am trying to suppress false warnings from third party library code. For this I want to

相关标签:
4条回答
  • 2020-12-30 04:29

    I couldn't achive what I wanted with the commmand line options, so I will use the // NOLINT comments in the cpp files which was proposed by the accepted answer.

    I will also try to push the fix to googletest.

    I found out that lines in the -line-filter options are filtered in. But giving concrete lines is no real solution for my problem anyways. I rather need a suppression mechanism like it is implemented in Valgrind.

    0 讨论(0)
  • 2020-12-30 04:30

    I solved the problem by adding // NOLINT to line 1790 of gmock-spec-builders.h

    Here is the diff:

    --- gmock-spec-builders.orig.h  2016-09-17 09:46:48.527313088 +0200
    +++ gmock-spec-builders.h       2016-09-17 09:46:58.958353697 +0200
    @@ -1787,7 +1787,7 @@
     #define ON_CALL(obj, call) GMOCK_ON_CALL_IMPL_(obj, call)
    
     #define GMOCK_EXPECT_CALL_IMPL_(obj, call) \
    -    ((obj).gmock_##call).InternalExpectedAt(__FILE__, __LINE__, #obj, #call)
    +    ((obj).gmock_##call).InternalExpectedAt(__FILE__, __LINE__, #obj, #call) // NOLINT
     #define EXPECT_CALL(obj, call) GMOCK_EXPECT_CALL_IMPL_(obj, call)
    
     #endif  // GMOCK_INCLUDE_GMOCK_GMOCK_SPEC_BUILDERS_H_
    

    It would be nice to either upstream this patch (I see other NOLINT in the code) or post a bug report with the clang-tidy folks.

    0 讨论(0)
  • 2020-12-30 04:47

    I have found another non-invasive (without adding // NOLINT to a third-party library) way to suppress warnings. For example, the current version of Google Test fails some cppcoreguidelines-* checks. The following code allows you to validate the current diff excluding lines that contain gtest's macros:

    git diff -U3 | sed '
        s/^+\( *TEST(\)/ \1/;
        s/^+\( *EXPECT_[A-Z]*(\)/ \1/;
        s/^+\( *ASSERT_[A-Z]*(\)/ \1/;
    ' | recountdiff | interdiff -U0 /dev/null /dev/stdin | clang-tidy-diff.py -p1 -path build
    

    It assumes that file build/compile_commands.json is generated before and clang-tidy-diff.py is available from your environment. recountdiff and interdiff from patchutils are the standard tools for manipulating patches.

    The script works as follows:

    1. git diff -U3 generates a patch with 3 context lines.
    2. sed ... removes prefix + from the undesired lines, i.e. transform them to the context.
    3. recountdiff correct offsets (in first ranges) in the chunk headers.
    4. interdiff -U0 /dev/null /dev/stdin just removes all context lines from a patch. As a result, it splits the initial hunks.
    5. clang-tidy-diff.py reads only second ranges from chunk headers and passes them to clang-tidy via -line-filter option.

    UPD: It's important to provide interdiff with a sufficient number of context lines, otherwise it may produce some artifacts in the result. See the citation from man interdiff:

    For best results, the diffs must have at least three lines of context.

    Particularly, I have found that git diff -U0 | ... | interdiff generates some spurious literals $!otj after splitting chunks.

    0 讨论(0)
  • 2020-12-30 04:47

    Use -isystem instead of -I to set your system and 3rd party include paths. -I should only be used to include code that is part of the project being built.

    This is the only thing required to make clang-tidy ignore all errors in external code. All the other answers (at the point of writing) are just poor workarounds for something that is perfectly solved with -isystem.

    If you use a build system like CMake or Meson it will automatically set -I and -isystem correctly for you.

    -isystem is also the mechanism that is used for telling compilers, at least GCC and Clang, what's not your code. If you start to use -isystem you can also enable more compiler warnings without getting "false positives" from external code.

    0 讨论(0)
提交回复
热议问题