-
Notifications
You must be signed in to change notification settings - Fork 20
Enable clang-tidy tooling #372
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
base: main
Are you sure you want to change the base?
Changes from all commits
2cfeebf
4b02832
17cb3f5
bb72b52
7f2eedd
70b56c4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| # Shared clang-tidy baseline for dd-trace-cpp, httpd-datadog, and nginx-datadog. | ||
| # Keep this file in sync across those repositories. | ||
| Checks: > | ||
| -*, | ||
| bugprone-assert-side-effect, | ||
| bugprone-bool-pointer-implicit-conversion, | ||
| bugprone-copy-constructor-init, | ||
| bugprone-dangling-handle, | ||
| bugprone-forwarding-reference-overload, | ||
| bugprone-inaccurate-erase, | ||
| bugprone-infinite-loop, | ||
| bugprone-macro-repeated-side-effects, | ||
| bugprone-move-forwarding-reference, | ||
| bugprone-parent-virtual-call, | ||
| bugprone-posix-return, | ||
| bugprone-string-constructor, | ||
| bugprone-undelegated-constructor, | ||
| bugprone-unused-raii, | ||
| bugprone-virtual-near-miss, | ||
| misc-unused-using-decls, | ||
| readability-delete-null-pointer, | ||
| readability-redundant-control-flow, | ||
| readability-redundant-string-cstr | ||
| # Deferred until existing findings are cleaned up: | ||
| # bugprone-argument-comment | ||
| # bugprone-use-after-move | ||
| # misc-redundant-expression | ||
| # misc-static-assert | ||
| # modernize-redundant-void-arg | ||
| # performance-move-const-arg | ||
| # performance-noexcept-move-constructor | ||
| # readability-redundant-member-init | ||
| # readability-simplify-boolean-expr | ||
| WarningsAsErrors: '*' | ||
| HeaderFilterRegex: '^src/' | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems this never matches because CMake passes absolute paths, so Clang Tidy sees |
||
| FormatStyle: file | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| .build/ | ||
| .clang-tidy-build/ | ||
| .cache/ | ||
| .coverage/ | ||
| .cursor/ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,7 +18,7 @@ run apt-get update && apt-get install --yes software-properties-common && \ | |
| add-apt-repository ppa:git-core/ppa --yes && \ | ||
| apt-get update && apt-get upgrade --yes && \ | ||
| apt-get install --yes \ | ||
| wget build-essential clang sed gdb clang-format git ssh shellcheck \ | ||
| wget build-essential clang sed gdb clang-format clang-tidy git ssh shellcheck \ | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit, opt, pedantic: By the way, please sort packages alphabetically. |
||
| libc++-dev libc++abi-dev python3 pip coreutils curl gnupg nodejs | ||
|
|
||
| # bazelisk, a launcher for bazel. `bazelisk --help` will cause the latest | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,9 @@ This directory contains scripts that are useful during development. | |
| before pushing changes. | ||
| - [check-format](check-format) verifies that the source code is formatted as | ||
| [format](format) prefers. | ||
| - [check-tidy](check-tidy) runs **clang-tidy-14** (pinned, same major as the | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: I would prefer a much simpler and terser comment, without bold, without version, without internal technical details: |
||
| CI Clang) through CMake's `CXX_CLANG_TIDY` on `dd-trace-cpp-objects`. Do | ||
| not point it at a host `compile_commands.json`. | ||
| - [check-version](check-version) accepts a version string as a command line | ||
| argument (e.g. "v1.2.3") and checks whether the version within the source code | ||
| matches. This is a good check to perform before publishing a source release. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| #!/bin/sh | ||
| # Run the pinned clang-tidy through CMake in the CI container. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: I find this comment a bit complicated. I suggest: |
||
| # | ||
| # clang-tidy must use the same compiler, flags, and stdlib as the real | ||
| # build (ci-clang + libc++). CMake's CXX_CLANG_TIDY on | ||
| # dd-trace-cpp-objects does that: it invokes tidy with the exact compile | ||
| # line after `--`. Do not run tidy against a host compilation database. | ||
| # | ||
| # Usage: bin/check-tidy | ||
|
|
||
| set -e | ||
|
|
||
| SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd) | ||
| REPO_ROOT=$(cd "$SCRIPT_DIR/.." && pwd) | ||
| cd "$REPO_ROOT" | ||
|
|
||
| # Keep this in sync with .github/workflows/dev.yml and cmake/compiler/clang.cmake. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit:
|
||
| CLANG_TIDY_VERSION=14 | ||
| CI_IMAGE_BASE=datadog/docker-library:dd-trace-cpp-ci-23768e9 | ||
|
|
||
| in_container() { | ||
| [ -f /.dockerenv ] || [ -n "${KUBERNETES_SERVICE_HOST:-}" ] || \ | ||
| [ "${DD_TRACE_CPP_TIDY_IN_CONTAINER:-}" = "1" ] | ||
| } | ||
|
|
||
| docker_arch() { | ||
| case "$(uname -m)" in | ||
| arm64|aarch64) echo arm64 ;; | ||
| *) echo amd64 ;; | ||
| esac | ||
| } | ||
|
|
||
| if ! in_container; then | ||
| if ! command -v docker >/dev/null 2>&1; then | ||
| >&2 echo "docker is required to run clang-tidy-$CLANG_TIDY_VERSION." | ||
| exit 1 | ||
| fi | ||
| if ! docker info >/dev/null 2>&1; then | ||
| >&2 echo "Docker is not running. Please start Docker." | ||
| exit 1 | ||
| fi | ||
| arch=$(docker_arch) | ||
| image="${CI_IMAGE_BASE}-${arch}" | ||
| exec docker run --rm -t \ | ||
| --platform "linux/${arch}" \ | ||
| -e DD_TRACE_CPP_TIDY_IN_CONTAINER=1 \ | ||
| -e BUILD_DIR=.clang-tidy-build \ | ||
| -v "$REPO_ROOT:$REPO_ROOT" \ | ||
| -w "$REPO_ROOT" \ | ||
| "$image" \ | ||
| bin/check-tidy | ||
| fi | ||
|
|
||
| build_dir=${BUILD_DIR:-.clang-tidy-build} | ||
| tidy=clang-tidy-$CLANG_TIDY_VERSION | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit, opt, personal preference: I would prefer to name this variable |
||
|
|
||
| if ! command -v "$tidy" >/dev/null 2>&1; then | ||
| if [ -f /etc/debian_version ]; then | ||
| apt-get update | ||
| DEBIAN_FRONTEND=noninteractive apt-get install --yes "$tidy" | ||
| else | ||
| >&2 echo "$tidy is required (pinned). Install it in this container." | ||
| exit 1 | ||
| fi | ||
| fi | ||
|
|
||
| if ! command -v "$tidy" >/dev/null 2>&1; then | ||
| >&2 echo "$tidy is not installed." | ||
| exit 1 | ||
| fi | ||
|
|
||
| compiler=${CXX:-clang++} | ||
| if ! command -v "$compiler" >/dev/null 2>&1; then | ||
| >&2 echo "$compiler is required (same toolchain as CMake)." | ||
| exit 1 | ||
| fi | ||
|
|
||
| compiler_major=$("$compiler" -dumpversion | cut -d. -f1) | ||
| tidy_major=$("$tidy" --version | sed -n 's/.*version \([0-9][0-9]*\).*/\1/p' | head -n 1) | ||
| if [ "$compiler_major" != "$CLANG_TIDY_VERSION" ] || [ "$tidy_major" != "$CLANG_TIDY_VERSION" ]; then | ||
| >&2 echo "clang-tidy and the CMake compiler must both be LLVM $CLANG_TIDY_VERSION." | ||
| >&2 echo " $compiler: $compiler_major" | ||
| >&2 echo " $tidy: $tidy_major" | ||
| exit 1 | ||
| fi | ||
|
|
||
| # Reconfigure so CXX_CLANG_TIDY is attached to dd-trace-cpp-objects, then | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of compiling with clang-tidy step, I think we could rather only run clang-tidy directly. By the way, it seems that files in Moreover, I think this would ease adding, possibly incrementally, the What do you think? |
||
| # compile that target. CMake passes the exact ci-clang compile line | ||
| # (including -stdlib=libc++) to tidy. First-party src/ only; no extra-args. | ||
| bin/with-toolchain llvm cmake . -B "$build_dir" --preset ci-clang \ | ||
| -DCMAKE_EXPORT_COMPILE_COMMANDS=ON \ | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems this is redundant with the added line (line 42) in |
||
| -DDD_TRACE_ENABLE_CLANG_TIDY=ON \ | ||
| -DDD_TRACE_CLANG_TIDY="$tidy" | ||
|
|
||
| cmake --build "$build_dir" --target dd-trace-cpp-objects -j | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,3 +22,27 @@ command: | |
| ```shell | ||
| bin/format | ||
| ``` | ||
|
|
||
| To check formatting without writing files: | ||
|
|
||
| ```shell | ||
| bin/check-format | ||
| ``` | ||
|
|
||
| ## Static Analysis | ||
|
|
||
| C++ is analyzed with **clang-tidy-14** (pinned; same major as `clang-format-14`) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need a much simpler and direct documentation, without internal technical details. I suggest: |
||
| using the shared `.clang-tidy` baseline. Warnings are errors. | ||
|
|
||
| Do not run clang-tidy on the host. Tidy must use the same compiler, flags, and | ||
| stdlib as the real build (`ci-clang` + libc++). `bin/check-tidy` re-execs in | ||
| `datadog/docker-library:dd-trace-cpp-ci-23768e9-*`, configures CMake there with | ||
| `DD_TRACE_ENABLE_CLANG_TIDY`, and builds `dd-trace-cpp-objects` so CMake | ||
| invokes `clang-tidy-14` with the exact compile line (first-party `src/` only): | ||
|
|
||
| ```shell | ||
| bin/check-tidy | ||
| ``` | ||
|
|
||
| CI runs the same script in that image. A finding fails the pull request. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This setting turns existing diagnostics in configured translation units into CI failures. Running the configured checks against
src/datadog/tracer.cppreportsreadability-redundant-member-initandperformance-move-const-arg, whilesrc/datadog/remote_config/product.cppreports twomisc-static-assertdiagnostics; therefore the new check cannot pass even after its input list is limited to compilation-database entries. Fix or suppress the baseline findings before making all warnings fatal.Useful? React with 👍 / 👎.