4

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?

2 Answers 2

3

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
  )
Sign up to request clarification or add additional context in comments.

1 Comment

That's... not nice, haha! It's a clever workaround though, so thanks! I wish it was possible to solve this in a better way.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.