Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clang-Tidy `NOLINT` for multiple lines?

I'm working on a C++ project that has some large sections of code that are autogenerated, and I don't want to be linted. Is there something akin to the //NOLINT comment that can be applied to multiple lines? Like the following:

// BEGINNOLINT
bad-code;
// ENDNOLINT

All I could find online was a suggestion that this should be implemented. Is there any way to avoid having to write // NOLINT on the end of every single line?

like image 380
user3002473 Avatar asked Mar 14 '19 15:03

user3002473


2 Answers

clang-tidy 14 introduced this feature:

// NOLINTBEGIN
...
// NOLINTEND

Note, if you want to disable a specific warning, the end-comment must match the begin-comment:

// NOLINTBEGIN(check-name)
...
// NOLINTEND(check-name)
like image 160
GPMueller Avatar answered Oct 08 '22 22:10

GPMueller


Unfortunately there is no direct way to do this, clang-tidy only supports //NOLINT and //NOLINTNEXTLINE.

I don't know how much control you have about that code generation...you could generate it in one line, that could help you a lot as c++ doesn't care about whitespace.

A crude but effective solution is to use a text-manipulation tool like sed:

$ sed -i -re '/^\/\/BEGIN_NOLINT/,/^\/\/END_NOLINT/{s/$/\/\/NOLINT/}' *.cpp

This would add //NOLINT at the end of every line between //BEGIN_NOLINT and //END_NOLINT comments (which can be probably generated).

You can also run clang-tidy with parameter

-line-filter='[{"name":"test.cpp","lines":[[1,10],[12,100]]}]'

Line 11 will be skipped in this example. This is however difficult to maintain as you need to update the filter every time you add/remove lines in the file. Maybe it would be a good idea to generate code into separate files if possible.

like image 5
pablo285 Avatar answered Oct 08 '22 22:10

pablo285