1

I want to exclude certain regex patterns from linting for readability-identifier-naming.

Part of .clang-tidy I'm using:

Checks: 'readability-*'
CheckOptions:
  - key:             readability-identifier-naming.TypeAliasCase
    value:           CamelCase
  - key:             readability-identifier-naming.TypeAliasIgnoredRegexp
    value:           '(*_type|*reference|*iterator)'
  - key:             readability-identifier-naming.TypedefCase
    value:           CamelCase
  - key:             readability-identifier-naming.TypedefIgnoredRegexp
    value:           '(*_type|*reference|*iterator)'

However, warnings on those regex patterns are not suppressed.

For example,

using value_type = T;
using reference = value_type&;
using const_iterator = const T*;

Is this the right way to use regex for clang-tidy?

PHD
  • 595
  • 1
  • 8
  • 18
  • Seems you use globbing syntax and not regex one... (don't know which one is expected though). – Jarod42 Apr 19 '21 at 16:10
  • @Jarod42 I tried to use `.*` and it didn't work. I was wondering if `clang-tidy` uses POSIX or Perl's for its regex engine. I'm also not sure if I have to escape parentheses or pipes. – PHD Apr 19 '21 at 17:11

1 Answers1

0

Looking at the changeset that introduced the IgnoredRegexp it seems you have to use a proper regex - not a glob-match in the value-string

%check_clang_tidy %s readability-identifier-naming %t -- 
  -config='{CheckOptions: [ 
    {key: readability-identifier-naming.ParameterCase, value: CamelCase}, 
    {key: readability-identifier-naming.ParameterIgnoredRegexp, value: "^[a-z]{1,2}$"}, 
    {key: readability-identifier-naming.ClassCase, value: CamelCase}, 
    {key: readability-identifier-naming.ClassIgnoredRegexp, value: "^fo$|^fooo$"}, 
    {key: readability-identifier-naming.StructCase, value: CamelCase}, 
    {key: readability-identifier-naming.StructIgnoredRegexp, value: "sooo|so|soo|$invalidregex["} 
 ]}'

See: clang-tools-extra/test/clang-tidy/checkers/readability-identifier-naming-ignored-regexp.cpp

Guessing from your question and the samples, a pattern might look like this:

value:           '^.*_type$|^.*reference$|^.*iterator$'

If you need a non-greedy version you might be out of luck since the leveraged llvm::Regex class is using POSIX ERE and I guess lazy matching is not supported:

value:           '^.*?_type$|^.*?reference$|^.*?iterator$'
wp78de
  • 18,207
  • 7
  • 43
  • 71
  • I think there's a problem with `clang-tidy`. If I run `clang-tidy --dump-config` inside a directory that contains `.clang-tidy`, `readability-identifier-naming.*IgnoredRegexp` contains an empty sting (`''`). Even if I do not use `.clang-tidy` and directly enter config, say `clang-tidy --config='{Checks: "readability-*", CheckOptions: [{key: readability-identifier-naming.TypeAliasCase, value: CamelCase}, {key: readability-identifier-naming.TypeAliasCaseIgnoredRegexp, value: "^.+_type$"}]}'`, this still does not suppress warnings. – PHD Apr 20 '21 at 02:50
  • 1
    It seems that `clang-tidy` I'm using (`11.0.0`) does [not support](https://releases.llvm.org/11.0.0/tools/clang/tools/extra/docs/clang-tidy/checks/readability-identifier-naming.html) `readability-identifier-naming.*IgnoredRegexp`. – PHD Apr 20 '21 at 03:26
  • @PHD Good catch. It looks like at least version 12+ is needed to use this feature. https://releases.llvm.org/12.0.0/tools/clang/tools/extra/docs/clang-tidy/checks/readability-identifier-naming.html The lack of version tags on their GitLab makes it difficult for me to tell the exact version number but I don't see any point releases; so, I think this is accurate. – wp78de Apr 20 '21 at 03:48