Clang-tidy: How to suppress warnings?

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

-header-filter=<string> or -line-filter=<string>

but so far unsuccessfully. Therefore, for people with limited time, I asked the question here at the beginning and later I will explain what I have already tried.

Question

What parameter do I need to provide the clang-tidy tool to suppress a warning from a specific line and file?

If this is not possible

Which option works to suppress warnings from external header files?


What have i done so far

My original clang-tidy call is as follows

 clang-tidy-3.8 -checks=-*,clang-analyzer-*,-clang-analyzer-alpha* -p Generated/LinuxMakeClangNoPCH Sources/CodeAssistant/ModuleListsFileManipulator_fixtures.cpp 

and the first line of the warning I want to suppress looks like this:

 .../gmock/gmock-spec-builders.h:1272:5: warning: Use of memory after it is freed [clang-analyzer-cplusplus.NewDelete] return function_mocker_->AddNewExpectation( 

The gmock people told me that this is a false positive, so I want to suppress it. At first I tried using the -line-filter=<string> option. The documentation states:

  -line-filter=<string> - List of files with line ranges to filter the warnings. Can be used together with -header-filter. The format of the list is a JSON array of objects: [ {"name":"file1.cpp","lines":[[1,3],[5,7]]}, {"name":"file2.h"} ] 

I assumed that the warnings in these lines are filtered out. But the dock says if they are filtered or in. After some fiddeling arround, I created a .json file with the contents

 [ {"name":"gmock-spec-builders.h","lines":[[1272,1272]]} ] 

and changed the command line to

 clang-tidy-3.8 -checks=-*,clang-analyzer-*,-clang-analyzer-alpha* -p Generated/LinuxMakeClangNoPCH -line-filter="$(< Sources/CodeAssistant/CodeAssistant_ClangTidySuppressions.json)" Sources/CodeAssistant/ModuleListsFileManipulator_fixtures.cpp 

which writes the contents of the file to an argument. This suppresses the warning, but not only this warning, but all warnings from the ModuleListsFileManipulator_fixtures.cpp file. I tried more things, but I could not get it to work.

So, I tried the -header-filter=<string> parameter. Here, the documentation states that you need to provide a regular expression that matches all header files from which the diagnostics should be displayed. Well, I thought, let's use a regualar expression that matches everything in the same folder as the parsed .cpp file. I can live with this, although it can remove warnings that are due to misuse of external headers.

Here I was not sure whether the regular expression should match the full (absolute) file name or only part of the file name. I tried

 -header-filter=.*\/CodeAssistant\/.*.h 

which matches all absolute header file names in the CodeAssistant folder, but it did not suppress the warnings from the gmock-spec-builders.h file.

Therefore, I would like to suppress each warning individually, so that I can determine for everyone if this is a real problem or not, but if this is not possible, I could also live with suppressing warnings from all external headers.

Thank you for your time.

+5
source share
3 answers

I solved the problem by adding // NOLINT to line 1790 from 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 ascend this patch (I see another NOLINT code in the code) or publish a bug report with people who own bliss.

+1
source

I also came across some other cases that the original patch did not cover. When trying to fix this, I also encountered a segmentation error in clang-tidy ( https://llvm.org/bugs/show_bug.cgi?id=30565 ). Currently, I have not been able to suppress all errors.

I will give an updated answer here if / when I find it :)

+1
source

I could not achieve what I needed with the parameters of the commmand line, so I will use // NOLINT comments in the cpp files that were suggested by the accepted answer.

I will also try to click on the fix on googletest.

I found out that the lines in the -line-filter options are filtered out. But providing specific strings in any case is not a real solution to my problem. I rather need a suppression mechanism, as implemented by Valgrind.

0
source

All Articles