Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modernize clang-tidy setup #1951

Merged
merged 19 commits into from
Jan 8, 2025
Merged

Modernize clang-tidy setup #1951

merged 19 commits into from
Jan 8, 2025

Conversation

bbannier
Copy link
Member

@bbannier bbannier commented Dec 18, 2024

The idea of this PR is to bring us to date so that no clang-tidy diagnostics are raised in my editor via clangd, at least for my version of clang-tidy on macos-14.7.1. The way I went about this was to execute clang-tidy as part of the build, in build/

$ cmake -DCMAKE_CXX_CLANG_TIDY=/opt/homebrew/opt/llvm/bin/clang-tidy \
        -DCMAKE_C_CLANG_TIDY=/opt/homebrew/opt/llvm/bin/clang-tidy ..

Suppressions are now be consistently configured via .clang-tidy configurations so they should work with the above setup. I fixed or suppressed some warnings I saw (often: only raised for newer clang-tidy).

@bbannier bbannier self-assigned this Dec 18, 2024
@bbannier bbannier force-pushed the topic/bbannier/cmake-clang-tidy branch 4 times, most recently from acbe6a9 to 65fb497 Compare December 20, 2024 13:37
@bbannier bbannier marked this pull request as ready for review December 20, 2024 14:20
@bbannier bbannier requested a review from rsmmr January 6, 2025 15:29
rsmmr
rsmmr previously approved these changes Jan 7, 2025
hilti/runtime/src/fiber.cc Outdated Show resolved Hide resolved
This patch brings our clang-tidy setup up to date. Much of this was
built with `./ci/run-ci` `in mind with extra suppressions hardcoded
e.g., in `.clang-tidy.ignore`. With this patch we instead configure
`.clang-tidy` files so they can suppress diagnostics from certain
directories. We also set up 3rdparty code to work with CMake's
`clang-tidy` integration[1].

We also globally suppress some diagnostics which we have no intention of
addressing.

[^1]: https://cmake.org/cmake/help/latest/prop_tgt/LANG_CLANG_TIDY.html#prop_tgt:%3CLANG%3E_CLANG_TIDY
This either switches to using `const char*` to signify guaranteed null
termination, or locally suppress clang-tidy warnings about assuming
null-terminated string_views.
This suppresses a `clang-tidy` lint suggesting to mark our default
`main` function as not exported. This function is shipped as part of
libhilti and intended to be available as a weak symbol.
In general `std::string_view` values do not need to be null-terminated,
e.g., if they reference subranges in a bigger null-terminated string. We
were previously passing `std::string_view` in a few places where we
assumed them to be null-terminated, e.g., for passing them on to C APIs
expecting null-terminated `const char*`. These worked well enough for us
for now, but these APIs were generally unsafe.

This patch switches to instead passing either `const char*` signalling
proper, null-terminated strings, or by passing in a `const std::string&`
where it should already be present in the caller.
`std::string_view` is a type which is designed to be (very) cheap to
copy.
`clang-tidy` suggests adding extra parentheses when mixing e.g.,
addition and multiplication. Apply these suggestions (automatically).
It is not immediately clear to me when the previous implementation could
move their values over. `clang-tidy` ended up flagging many of these as
not moving and forcing copies.

This patch rewrites the loops in these helpers to enforce iteration with
move.
Many functions in the AST API were moving arguments only for them to
ultimately be copied; `clang-tidy` diagnosed most of these.
This patch cleans up the flagged useless passes by value and `std::move`
without effect.
Instead of implementing this ourself instea use CMake with
`CMAKE_CXX_CLANG_TIDY`/`CMAKE_C_CLANG_TIDY`.
This was already unused since we moved clang-format to a pre-commit
hook.
@bbannier bbannier force-pushed the topic/bbannier/cmake-clang-tidy branch from 65fb497 to 616a88f Compare January 7, 2025 12:06
3rdparty/filesystem Outdated Show resolved Hide resolved
@bbannier bbannier force-pushed the topic/bbannier/cmake-clang-tidy branch from 356afc4 to a050c87 Compare January 8, 2025 07:47
@bbannier bbannier merged commit dc31aee into main Jan 8, 2025
20 checks passed
@bbannier bbannier deleted the topic/bbannier/cmake-clang-tidy branch January 8, 2025 08:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants