Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can one make clang-tidy fail on invalid configuration?

Tags:

c++

clang-tidy

Whenever clang-tidy finds an error in my code, it also warns me that there's an invalid configuration value in my .clang-tidy file:

warning: invalid configuration value '' for option 'readability-identifier-naming.MacroDefinitionCase'

That is indeed an error, which I intend to fix.

I would however like if I could make clang-tidy fail when there's something wrong in my configuration, instead of just warning about it and continuing. It's extra bad since these warnings are only printed whenever clang-tidy finds an error in my code. So I won't notice until that happens.

Is there a way to ask clang-tidy to fail hard for configuration errors?

like image 369
knatten Avatar asked Sep 07 '25 06:09

knatten


2 Answers

We ran into this and had no good clang-tidy errors for a month. :vomit.

Our fix is to add clang-tidy --verify-config in our build pipeline. Since on failure it still says No config errors detected. and return 0, we look at stderr.

If you use cmake, it could look like this:

  find_program(BASH bash HINTS /bin)
  message("stderr=\`${CMAKE_CXX_CLANG_TIDY} --verify-config\`")
  add_custom_command(
    TARGET <whatever target you care about>
    PRE_BUILD
    COMMAND ${CMAKE_COMMAND} -E env ${BASH} -c "stderr=\`${CMAKE_CXX_CLANG_TIDY} --verify-config 2\>&1 >/dev/null \`; echo \"Validating clang-tidy; testing if $stderr is empty\"; test -z \"$stderr\""
    WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/<where you want to verify config>
# Provides correct escaping for the command.
    VERBATIM
  )
like image 137
Peteris Ledins Avatar answered Sep 10 '25 00:09

Peteris Ledins


I needed a simple one-line command that could be easily embedded into pipelines. I ended up with the following bash-only solution:

OUT=$(clang-tidy --dump-config 2>&1); echo "$OUT"; if echo "$OUT" | grep -q "error:"; then exit -1; fi

GitHub Actions version:

- name: clang-tidy verify config
  run: bash -c 'OUT=$(clang-tidy --dump-config 2>&1); echo "$OUT"; if echo "$OUT" | grep -q "error:"; then exit -1; fi'

It's not as fancy as it could be, but at least clang-tidy wouldn't silently ignore the configuration in case of errors.

like image 34
Alex2772 Avatar answered Sep 10 '25 00:09

Alex2772