7

I recently updated my project and its CI pipelines to use C++20 features. When I changed the compiler settings to use C++20, clang-tidy started to emit the following warning (I treat them as errors) for all my checked files:

error: invalid case style for template parameter 'expr-type' [readability-identifier-naming,-warnings-as-errors]

I could narrow it down to the following minimal example using CMake and a clang-tidy config file:

CMakeLists.txt

cmake_minimum_required(VERSION 3.5)

project(clang_tidy_warning LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_executable(clang_tidy_warning main.cpp)

.clang-tidy

Checks: >
  readability-*,

WarningsAsErrors: '*'

CheckOptions:
  - {key: readability-identifier-naming.TemplateParameterCase,           value: lower_case}
  - {key: readability-identifier-naming.TypeTemplateParameterCase,       value: CamelCase }
  - {key: readability-identifier-naming.TemplateParameterPrefix,         value: t_ }
  - {key: readability-identifier-naming.TypeTemplateParameterPrefix,     value: T_ }

main.cpp

#include <iostream>

int main() { return 0; }

You should be able to reproduce it in Ubuntu 20.04 with the following commands starting in the root directory:

mkdir build
cd build
cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_BUILD_TYPE=DEBUG ..
run-clang-tidy-12  -quiet

The error/warning disappears if you do one of the following things:

  • remove the specified check options from the config file
  • set the c++ standard to 17
  • remove the #include in the main file

Exchanging iostream with another standard library header didn't help, but I have not tested them all. It is also worth mentioning that all other possible clang-tidy configuration options do not cause this error. I had nearly all of them in the original config file and removed the ones that do not cause the error. It seems like only the TemplateParameter and TypeTemplateParameter suboptions are affected by this. I have tested clang-tidy versions 9, 10, and 12. All emit the same warning. I also tested passing different compilers (clang 12 and gcc10) to CMake. Did not change anything (do not know if clang-tidy is even affected by the compiler choice, I guess not). I am using Ubuntu 20.04 (up-to-date) and you can also reproduce the error in a GitHub action runner.

So my questions are. Is this a (known) bug or is there any incompatibility with C++ 20 in this clang-tidy setup? If it is already known, can you give me an explanation of why it occurs and how to fix it?

EDIT

As Lawrence Benson mentioned in the comments, this might be also OS-related. He couldn't reproduce on a Mac, but on Ubuntu.

Related bug report

Here is the link to a related bug report found by 0x5453: click me

Update: As "Buster" pointed out in the comments, the bug report has been moved here

wychmaster
  • 712
  • 2
  • 8
  • 23
  • FWIW I cannot reproduce on [Compiler Explorer](https://gcc.godbolt.org/z/zbac34qb8) with the latest clang-tidy, so could be a bug in version 12. – 0x5453 Jul 21 '21 at 21:40
  • 3
    Possibly related: https://bugs.llvm.org/show_bug.cgi?id=46752 – 0x5453 Jul 21 '21 at 21:43
  • 1
    @0x5453 seems to be the same problem. I think I might add a comment there to link this question (if I find the time). The example I posted might help to track down the bug if it isn't already solved (as you mentioned in your first comment). Good news for me is, that I just have to wait for the new version to be available in Ubuntus package manager. – wychmaster Jul 22 '21 at 20:43
  • I'm also facing this problem at the moment with clang-tidy-10. This might also be related to the OS. On my MacBook, I don't get any errors but in GitHub Actions I get `error: invalid case style for type template parameter 'expr-type'`. So maybe this is a problem of the Ubuntu version, as brew llvm seems to be fine. – Lawrence Benson Jul 27 '21 at 15:12
  • @LawrenceBenson Thank you for the input. I added the info from your comment to the question. I finally managed to request an account on bugzilla. As soon as it gets activated, I will link this question in the bug report shared by 0x5453. I hope it will help to track down and fix the bug soon – wychmaster Jul 28 '21 at 07:16
  • @wychmaster I just double checked and brew must have updated at some point. On my MacBook, I have clang-tidy from LLVM 11.1.0 running, whereas GitHub Actions is running on clang-tidy-10. I bumped GitHub Actions to clang-tidy-11 and then -12 but the problem still remains. So this is definitely not only a 12 bug, but also in 10 and 11 on Ubuntu. – Lawrence Benson Jul 29 '21 at 09:10
  • I reproduced the problem with LLVM 13 – Schrodinger ZHU Feb 16 '22 at 13:53
  • 1
    I wonder if something like `readability-identifier-naming.TypeTemplateParameterIgnoredRegexp` can be used to workaround this – Schrodinger ZHU Feb 16 '22 at 14:00
  • 1
    Recent comments on the related bug are at https://github.com/llvm/llvm-project/issues/46097 (LLVM Bugzilla is now read-only) – Buster Feb 28 '22 at 09:32
  • @Buster Thanks for the link. I added it to the question. – wychmaster Feb 28 '22 at 10:15

1 Answers1

2

This bug is still not fixed in version 14.

However, as "Schrodinger ZHU" already hinted in the question comments and as shown by the user "saitou1024" in the bug report on Github, one can use readability-identifier-naming.TypeTemplateParameterIgnoredRegexp to work around this problem.

Add the following line under CheckOptions to your clang-tidy configuration file:

CheckOptions:  
  - {key: readability-identifier-naming.TypeTemplateParameterIgnoredRegexp, value: expr-type}

For me, this fixed the issue and I can now treat readability-identifier-naming warnings as errors again to enforce naming conventions in a CI pipeline.

Additional note:

Since the line above only affects the TypeTemplateParameter options and the bug also occurred when TemplateParameter options were specified, note that there is also a TemplateParameterIgnoredRegexp option. So in case this workaround doesn't fix the problem for you, try adding the same line using TemplateParameterIgnoredRegexp.

wychmaster
  • 712
  • 2
  • 8
  • 23