SERVER-80396 - Establish the logic necessary to invoke clang-tidy via Bazel (#21221)

GitOrigin-RevId: f7cbec42a852f91f299a64ad787cc232cd4dd4bf
This commit is contained in:
stevegrossmongodb 2024-04-18 19:00:12 -04:00 committed by MongoDB Bot
parent b2aaea962d
commit edf26c4552
5 changed files with 57 additions and 0 deletions

View File

@ -49,6 +49,14 @@ build --bes_keywords=repo:mongo
build --jobs=200
common --remote_upload_local_results=False
# Settings specific for clang-tidy
--config=clang-tidy
build:clang-tidy --aspects @bazel_clang_tidy//clang_tidy:clang_tidy.bzl%clang_tidy_aspect
build:clang-tidy --output_groups=report
build:clang-tidy --@bazel_clang_tidy//:clang_tidy_config=//:clang_tidy_config
build:clang-tidy --@bazel_clang_tidy//:clang_tidy_executable=@mongo_toolchain//:clang_tidy
build:clang-tidy --@bazel_clang_tidy//:clang_tidy_additional_deps=@mongo_toolchain//:all
# if you don't have access to the remote execution cluster above, use the local config
# described below.
# pass local config to SCons like:

View File

@ -18,3 +18,11 @@ alias(
name = "codeowners",
actual = "//buildscripts:codeowners",
)
# This file group makes it possible to set the clang-tidy configuration setting:
# --@bazel_clang_tidy//:clang_tidy_config=//:clang_tidy_config
filegroup(
name = "clang_tidy_config",
srcs = [".clang-tidy"],
visibility = ["//visibility:public"],
)

View File

@ -2,6 +2,14 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("//bazel/platforms:local_config_platform.bzl", "setup_local_config_platform")
load("//bazel/toolchains:mongo_toolchain.bzl", "toolchain_download")
http_archive(
name = "bazel_clang_tidy",
# Note: strip_prefix is necessary to refer to the `@bazel_clang_tidy` package
strip_prefix = "bazel_clang_tidy-1.1",
# TODO(80396): Figure out how to get the sha256 of the release tarball
url = "https://github.com/mongodb-forks/bazel_clang_tidy/archive/refs/tags/v1.1.tar.gz",
)
setup_local_config_platform(name = "local_config_platform")
toolchain_download(name = "mongo_toolchain")

View File

@ -99,3 +99,27 @@ This allows SCons build targets to depend on Bazel build targets directly. The B
'new_library', # depend on the bazel "new_library" target defined above
],
)
## Running clang-tidy via Bazel
Note: This feature is still in development; see https://jira.mongodb.org/browse/SERVER-80396 for details)
To run clang-tidy via Bazel, do the following:
1. (Only necessary once, unless you change the `.clang-tidy.in`) Generate `.clang-tidy` by running `python3 buildscripts/clang_tidy.py`.
2. To analyze all code, run `bazel build --config=clang-tidy src/...`
3. To analyze a single target (e.g.: `fsync_locked`), run the following command (note that `_with_debug` suffix on the target): `bazel build --config=clang-tidy src/mongo/db/commands:fsync_locked_with_debug`
Remaining work to do:
- TODO(SERVER-80396): Make it SCons-invokable
- TODO(SERVER-80396): Link in Mongo's custom clang-tidy rules
- TODO(SERVER-80396): Make it run in CI
- TODO(SERVER-80396): Address the concern about [aspect / feature interaction](https://github.com/10gen/mongo/pull/21221#pullrequestreview-2009832686).
Testing notes:
- If you want to test whether clang-tidy is in fact finding bugs, you can inject the following code into a `cpp` file to generate a `bugprone-incorrect-roundings` warning:
```
const double f = 1.0;
const int foo = (int)(f + 0.5);
```

View File

@ -172,3 +172,12 @@ cc_toolchain_suite(
"{bazel_toolchain_cpu}": ":cc_mongo_toolchain",
},
)
# This file group makes it possible to set the clang-tidy configuration setting:
# --@bazel_clang_tidy//:clang_tidy_executable=@mongo_toolchain//:clang_tidy
filegroup(
name = "clang_tidy",
srcs = [
"v4/bin/clang-tidy",
],
)