9

I'm attempting to move the hardcoded config option of clang-tidy from our Makefile to a .clang-tidy YAML file. As of now, we call run-clang-tidy-6.0.py as such:

# Run clang-tidy. The header filter includes files in the foo and bar/baz directories
# but it excludes files ending in .g.h (auto-generated headers).
run-clang-tidy-6.0.py -header-filter='.*(foo|bar\/baz).*(?<!\.g\.h)$$' > clang-tidy-output.txt

This works fine. But if I have HeaderFilterRegex: '.*(foo|bar\/baz).*(?<!\.g\.h)$$' in .clang-tidy, the filtering doesn't work as expected.

I attempted various things surrounding what characters were escaped / not escaped as I know Make and YAML have different expectations but I can't get it just right. i.e:

  • Replace $$ -> $
  • Double escaping \ ( \ -> \\)
  • Removing ''

Could someone explain to me the appropriate format of this regex in a YAML file?

Xiddoc
  • 3,369
  • 3
  • 11
  • 37
Brandon Schaefer
  • 451
  • 1
  • 4
  • 12
  • @pablo285 Per the comment above `run-cland-tidy`, I am trying to match paths that start with either `foo` OR `bar/baz` (exclude all other directories). Also don't match any file ending with `.g.h` as these are autogenerated and should not be ran through the linter. – Brandon Schaefer Oct 18 '19 at 22:47
  • 5
    While I don't have a fully working regex for you (yet), I found this out for myself while looking at a similar problem: clang-tidy uses Posix ERE flavor in its regexes. The ERE documentation is here: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap09.html#tag_09_04 – psyill Dec 16 '19 at 12:57
  • 1
    So have you come up with a solution on your own ? What would be the answer to your question ? – lumpidu Sep 10 '21 at 13:54

1 Answers1

2

As far as I know regular expressions should be written as strings in a YAML file, and backslashes should be escaped with another backslash. In your scenario, the YAML file should include:

HeaderFilterRegex: '.*(foo|bar\\/baz).*(?<!\\.g\\.h)$'

Backslashes before the forward slash and before the dot are escaped with another backslash in this case. Backslashes are used to escape the backslashes before the dollar sign and the dot inside the negative lookbehind assertion. To surround the regular expression in YAML, use either single or double quotes. If you use double quotes, you must escape the backslashes and several other YAML-specific symbols, such as the dollar sign. As an example:

HeaderFilterRegex: ".*(foo|bar\\/baz).*(?<!\\.g\\.h)$"
starball
  • 20,030
  • 7
  • 43
  • 238
utkarshx27
  • 21
  • 3