Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to ignore a header with clang-tidy

Tags:

clang-tidy

I want to include a header from an external project, but clang-tidy is quite unhappy about it and produces a huge list of warnings. To workaround this, I am trying to disable all diagnostics coming from this header.

I tried:

// NOLINTBEGIN
// NOLINTNEXTLINE
#include <bad.hpp> // NOLINT
// NOLINTEND

But this does not work unfortunately.

This email thread suggests to use -header-filter (HeaderFilterRegex) option.

HeaderFilterRegex: '^((?!bad.hpp).)*$'

But this results into all headers being ignored, since clang tidy uses POSIX regex syntax. Which does not support negative look ahead.

I also considered using line-filter for this as this answer suggests, but there is no such option for the config file.

Is it possible at all?

like image 436
ivaigult Avatar asked Dec 08 '25 01:12

ivaigult


1 Answers

May 2024

Yet unreleased clang-tidy from llvm-19 supports headers filtering. According to the documentation, one can use --exclude-header-filter flag or ExcludeHeaderFilterRegex option to filter out unwanted headers:

  --exclude-header-filter=<string> - Regular expression matching the names of the
                                     headers to exclude diagnostics from. Diagnostics
                                     from the main file of each translation unit are
                                     always displayed.
                                     Must be used together with --header-filter.
                                     Can be used together with -line-filter.
                                     This option overrides the 'ExcludeHeaderFilterRegex'
                                     option in .clang-tidy file, if any.

April 2022

As of today (Apr 19 2022), this thread on disclosure llvm blog suggests that the feature is not supported.

Relevant notes are:

  • HeaderFilterRegex is parsed using llvm::Regex, which does not support negative lookahead.
  • Using std::regex instead of llvm::Regex is not possible yet, as some compilers do not have std::regex support. Future versions of clang-tidy may implement glob based file names filtering.

I can see only two possible workarounds for this now:

  • List all the allowed paths in HeaderFilterRegex.
  • Patch clang-tidy to use std::regex and use your own version.
like image 176
ivaigult Avatar answered Dec 11 '25 20:12

ivaigult



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!