Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable clang-tidy warning for a specific macro

Tags:

c++

clang-tidy

I have a macro which instantiates a global variable. And thus clang-tidy will correctly issue an "Initialization of ... with static storage duration may throw an exception which cannot be caught".

Can I disable this warning on a per-macro basis? Preferable inline where the macro is defined.

That is, let's say I have:

// header.h
#define UGLY_MACRO(X)  SomeClass X(#X)

// source.cpp
UGLY_MACRO(SomeName); // clang-tidy complains here

And I want clang-tidy to stop complaining about this.

I want to be as specific as possible. I only want to turn of this warning for this macro. I do not want to turn of the warning globally, then someone might add more of this kind of macros into the code unnoticed. Also, I don’t want to add something (like a //NOLNT(...)) at every place where the macro is used, that would be too much of a hassle.

Or am I approaching this from the wrong direction? I have worked with pc-lint before, where this is possible.

like image 474
Johan W Avatar asked Apr 04 '20 16:04

Johan W


People also ask

How do I disable clang-tidy warning?

To suppress a Clang-Tidy check for a particular line, use the Suppress "check_name" for line option. CLion will add a // NOLINT comment at the end of the selected line.

How does clang-tidy work?

clang-tidy is a clang-based C++ “linter” tool. Its purpose is to provide an extensible framework for diagnosing and fixing typical programming errors, like style violations, interface misuse, or bugs that can be deduced via static analysis.

How do I use clang-tidy in Visual Studio?

Clang-Tidy configurationIn the project Property Pages dialog, open the Configuration Properties > Code Analysis > Clang-Tidy page. Enter checks to run in the Clang-Tidy Checks property. A good default set is clang-analyzer-* . This property value is provided to the --checks argument of the tool.


2 Answers

I have the same problem. I have found two ways to do this, none of which are perfect, sadly.

Option 1 by using the --line-filter command-line argument for clang-tidy:

clang-tidy --line-filter='["name":"header.h"]' source.cpp

Notes:

  • Sadly this doesn't selectively work on one warning only, it disables all of them.
  • Unlike --filter-regex, this works even for macro expansion.
  • You can be more specific and specify the range of line numbers in header.h you want to disable the warnings for (see the help text for --line-filter)

Option 2 by wrapping the macro:

#define UGLY_MACRO_NOLINT(...) UGLY_MACRO(__VA_ARGS__) //NOLINT(...)

Notes:

  • Requires modifying your source code to use the nolint version
  • Does not work if UGLY_MACRO is multi-line
like image 143
Dan Nestor Avatar answered Oct 26 '22 00:10

Dan Nestor


As far as I know this is not possible. The check in question (cert-err58-cpp) doesn't have configurable options which means it can't be disabled on a per-macro basis, only globally.

Save for modifying the check's source the only option you have here is // NOLINT(cert-err58-cpp) and // NOLINTNEXTLINE(cert-err58-cpp). The latter is a little easier to use for text search and replace as you only need to catch a newline followed by the macro name.

like image 30
pablo285 Avatar answered Oct 26 '22 00:10

pablo285