|
| 1 | +--- |
| 2 | +name: clang-tidy-check |
| 3 | +description: Run clang-tidy on newly added/modified BE C++ code |
| 4 | +compatibility: opencode |
| 5 | +--- |
| 6 | + |
| 7 | +## What I do |
| 8 | + |
| 9 | +Run clang-tidy static analysis on newly added or modified C++ files in the BE and Cloud modules, using the project's `.clang-tidy` configuration. The script parses `git diff` to identify changed line ranges and filters clang-tidy output to diagnostics on those lines where possible, reducing noise from pre-existing code. Diagnostics from included headers that were not part of the diff are filtered out, though some edge cases may still appear. |
| 10 | + |
| 11 | +## When to use me |
| 12 | + |
| 13 | +- After building BE, before committing C++ code changes |
| 14 | +- When CI reports clang-tidy warnings on your PR |
| 15 | +- When you want to proactively check new code for common bugs and style issues |
| 16 | + |
| 17 | +## Prerequisites |
| 18 | + |
| 19 | +1. **The relevant module must be built first** — clang-tidy needs `compile_commands.json` generated during the CMake build. For BE files, build BE; for Cloud files, build Cloud. |
| 20 | +2. **clang-tidy must be installed** — the project uses clang-tidy from the LDB toolchain. |
| 21 | + |
| 22 | +## Procedure |
| 23 | + |
| 24 | +### Step 1: Build the relevant module (if not already done) |
| 25 | + |
| 26 | +For **BE** files: |
| 27 | +```bash |
| 28 | +./build.sh --be -j${DORIS_PARALLELISM} |
| 29 | +``` |
| 30 | +This generates `compile_commands.json` in `be/build_Release/` or `be/build_ASAN/`. |
| 31 | + |
| 32 | +For **Cloud** files: |
| 33 | +```bash |
| 34 | +./build.sh --cloud -j${DORIS_PARALLELISM} |
| 35 | +``` |
| 36 | +This generates `compile_commands.json` in `cloud/build_Release/` or `cloud/build_ASAN/`. |
| 37 | + |
| 38 | +**Note**: A single compilation database typically covers only one module (BE or Cloud). If your changes span both modules, you may need to run clang-tidy twice with different `--build-dir` values. |
| 39 | + |
| 40 | +### Step 2: Run clang-tidy on changed files |
| 41 | + |
| 42 | +```bash |
| 43 | +build-support/run-clang-tidy.sh |
| 44 | +``` |
| 45 | + |
| 46 | +By default, this script: |
| 47 | +- Detects changed C++ files via `git diff` (uncommitted changes + staged changes) |
| 48 | +- Automatically finds `compile_commands.json` from the latest BE build |
| 49 | +- Runs clang-tidy using the `.clang-tidy` config |
| 50 | +- Skips files in excluded directories (third-party, generated code) |
| 51 | +- Reports warnings grouped by file |
| 52 | + |
| 53 | +**Options:** |
| 54 | + |
| 55 | +```bash |
| 56 | +# Compare against a specific branch (e.g., for PR review) |
| 57 | +build-support/run-clang-tidy.sh --base origin/master |
| 58 | + |
| 59 | +# Check specific files (all lines, no line-level filtering) |
| 60 | +build-support/run-clang-tidy.sh --files be/src/vec/functions/my_new_func.cpp |
| 61 | + |
| 62 | +# Report ALL warnings in changed files (no line filtering) |
| 63 | +build-support/run-clang-tidy.sh --full |
| 64 | + |
| 65 | +# Specify build directory explicitly (required for Cloud if BE build dir was auto-detected) |
| 66 | +build-support/run-clang-tidy.sh --build-dir be/build_ASAN |
| 67 | + |
| 68 | +# Check Cloud files with Cloud compilation database |
| 69 | +build-support/run-clang-tidy.sh --build-dir cloud/build_ASAN |
| 70 | + |
| 71 | +# Apply auto-fixes where possible (note: fixes may apply beyond changed lines) |
| 72 | +build-support/run-clang-tidy.sh --fix |
| 73 | +``` |
| 74 | + |
| 75 | +**Important**: By default, only warnings on changed lines are reported (diagnostics from headers not in the diff are filtered out). This prevents the agent from "fixing" unrelated pre-existing warnings in large files. Use `--full` to see all warnings if needed. |
| 76 | + |
| 77 | +### Step 3: Interpret and fix warnings |
| 78 | + |
| 79 | +clang-tidy output looks like: |
| 80 | + |
| 81 | +``` |
| 82 | +be/src/vec/functions/foo.cpp:42:5: warning: use auto when initializing with a cast [modernize-use-auto] |
| 83 | +be/src/vec/functions/foo.cpp:55:1: warning: function 'process' exceeds recommended size [readability-function-size] |
| 84 | +``` |
| 85 | + |
| 86 | +**Fix approach:** |
| 87 | +1. Fix all warnings that represent real issues (bugs, performance, readability) |
| 88 | +2. For false positives or intentional patterns, add `// NOLINT(check-name)` with a brief justification |
| 89 | +3. Report any warnings you cannot reasonably fix |
| 90 | + |
| 91 | +### Step 4: Verify |
| 92 | + |
| 93 | +Re-run the script to confirm all warnings are resolved: |
| 94 | + |
| 95 | +```bash |
| 96 | +build-support/run-clang-tidy.sh |
| 97 | +``` |
| 98 | + |
| 99 | +## Enabled Checks (from `.clang-tidy`) |
| 100 | + |
| 101 | +| Category | Examples | |
| 102 | +|----------|----------| |
| 103 | +| `clang-diagnostic-*` | Compiler diagnostics | |
| 104 | +| `clang-analyzer-*` | Static analysis (null deref, use-after-free, etc.) | |
| 105 | +| `bugprone-*` | Use-after-move, redundant branch condition, unused RAII | |
| 106 | +| `modernize-*` | Auto, range-for, nullptr (excludes trailing return, nodiscard) | |
| 107 | +| `readability-*` | Function size (≤80 lines), cognitive complexity (≤50) | |
| 108 | +| `performance-*` | String find, inefficient algorithm, move-const-arg | |
| 109 | +| `misc-redundant-expression` | Redundant expressions | |
| 110 | + |
| 111 | +## Excluded Directories |
| 112 | + |
| 113 | +Files in these paths are automatically skipped: |
| 114 | +- `be/src/apache-orc/`, `be/src/clucene/`, `be/src/gutil/` |
| 115 | +- `be/src/glibc-compatibility/` |
| 116 | +- `contrib/` (all third-party code) |
| 117 | +- Generated code directories |
| 118 | + |
| 119 | +## NOLINT Usage |
| 120 | + |
| 121 | +When a clang-tidy warning cannot be fixed, suppress it with a comment: |
| 122 | + |
| 123 | +```cpp |
| 124 | +// Good: specific check name + reason |
| 125 | +int x = (int)y; // NOLINT(modernize-use-auto): explicit cast for clarity |
| 126 | + |
| 127 | +// Bad: blanket suppression without reason |
| 128 | +int x = (int)y; // NOLINT |
| 129 | +``` |
| 130 | + |
| 131 | +## Troubleshooting |
| 132 | + |
| 133 | +| Problem | Solution | |
| 134 | +|---------|----------| |
| 135 | +| `compile_commands.json not found` | Build BE first: `./build.sh --be -j${DORIS_PARALLELISM}` | |
| 136 | +| `clang-tidy not found` | Install via LDB toolchain or `apt install clang-tidy-16` | |
| 137 | +| Too many warnings on old code | Use `--base` to diff against a branch, ensuring only your changes are checked | |
| 138 | +| Warning in header included by your file | Only fix if the header is also in your changeset; otherwise note it in your report | |
0 commit comments