diff --git a/.github/PULL_REQUEST_TEMPLATE/improvement.md b/.github/PULL_REQUEST_TEMPLATE/improvement.md index 090ad43..7c992be 100644 --- a/.github/PULL_REQUEST_TEMPLATE/improvement.md +++ b/.github/PULL_REQUEST_TEMPLATE/improvement.md @@ -13,7 +13,7 @@ ## Related ticket > [!IMPORTANT] -> Please replace `[ISSUE-NUMBER]` with the issue-number that tracks this bug fix. If there is no such +> Please replace `[ISSUE-NUMBER]` with the issue-number that tracks this improvement. If there is no such > ticket yet, create one via [this issue template](../ISSUE_TEMPLATE/new?template=improvement.md). closes [ISSUE-NUMBER] (improvement ticket) diff --git a/MODULE.bazel b/MODULE.bazel index ac5b607..d858cc0 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -17,3 +17,5 @@ bazel_dep(name = "bazel_skylib", version = "1.8.2") bazel_dep(name = "platforms", version = "0.0.10") bazel_dep(name = "rules_cc", version = "0.1.5") +bazel_dep(name = "aspect_rules_lint", version = "2.5.0") + diff --git a/README.md b/README.md index 707ea02..bed49c0 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,10 @@ # score_cpp_policies -Centralized C++ quality tool policies for Eclipse S-CORE, providing sanitizer configurations reusable across all S-CORE modules (logging, communication, baselibs, etc.). +Centralized C++ quality tool policies for Eclipse S-CORE, providing sanitizer +configurations and clang-tidy integration reusable across all S-CORE modules +(logging, communication, baselibs, etc.). -Planned: clang-tidy, clang-format, code coverage policies. +Planned: clang-format, code coverage policies. ## What This Provides @@ -11,6 +13,8 @@ Planned: clang-tidy, clang-format, code coverage policies. - **`sanitizers/sanitizers.bazelrc`** — canonical config that consumers import or copy - **Suppression files** — per-sanitizer suppression lists for known false positives (GoogleTest, etc.) - **Constraint system** — `target_compatible_with` settings for sanitizer-incompatible targets +- **`clang_tidy/.clang-tidy`** — centralized default check set (conservative baseline, tailorable per module) +- **`clang_tidy/clang_tidy.bazelrc`** — `--config=clang-tidy` bazelrc config consumers can import ## Available Sanitizer Configurations @@ -27,7 +31,7 @@ Planned: clang-tidy, clang-format, code coverage policies. ### Add Dependency ```python -bazel_dep(name = "score_cpp_policies", version = "0.0.0") +bazel_dep(name = "score_cpp_policies") ``` ### Configure Sanitizers @@ -103,6 +107,7 @@ cd tests bazel test --config=asan_ubsan_lsan //... bazel test --config=tsan //... +bazel test --config=clang-tidy //... ``` ## Contributing diff --git a/clang_tidy/.clang-tidy b/clang_tidy/.clang-tidy new file mode 100644 index 0000000..19502ce --- /dev/null +++ b/clang_tidy/.clang-tidy @@ -0,0 +1,39 @@ +# ******************************************************************************* +# Copyright (c) 2026 Contributors to the Eclipse Foundation +# +# See the NOTICE file(s) distributed with this work for additional +# information regarding copyright ownership. +# +# This program and the accompanying materials are made available under the +# terms of the Apache License Version 2.0 which is available at +# https://www.apache.org/licenses/LICENSE-2.0 +# +# SPDX-License-Identifier: Apache-2.0 +# ******************************************************************************* + +--- +# Default clang-tidy configuration for S-CORE C++ modules. +# NOTE: the set of enabled checks is yet subject to be tailored per module. +Checks: >- + -*, + clang-analyzer-*, + cert-*, + cppcoreguidelines-*, + bugprone-*, + misc-*, + performance-*, + readability-*, + modernize-* + +# NOTE: WarningsAsErrors is yet subject to be expanded per module as compliance increases. +WarningsAsErrors: >- + clang-analyzer-* + +# Exclude third-party and generated headers from analysis. +HeaderFilterRegex: '^(?!.*/(third_party|bazel-out|external)/).*$' + +FormatStyle: file + +# NOTE: CheckOptions are yet subject to be provided for each enabled check per module. +#CheckOptions: +# none yet diff --git a/clang_tidy/BUILD.bazel b/clang_tidy/BUILD.bazel new file mode 100644 index 0000000..b8dbb4f --- /dev/null +++ b/clang_tidy/BUILD.bazel @@ -0,0 +1,20 @@ +# ******************************************************************************* +# Copyright (c) 2026 Contributors to the Eclipse Foundation +# +# See the NOTICE file(s) distributed with this work for additional +# information regarding copyright ownership. +# +# This program and the accompanying materials are made available under the +# terms of the Apache License Version 2.0 which is available at +# https://www.apache.org/licenses/LICENSE-2.0 +# +# SPDX-License-Identifier: Apache-2.0 +# ******************************************************************************* + +exports_files( + [ + ".clang-tidy", + "clang_tidy.bazelrc", + ], + visibility = ["//visibility:public"], +) diff --git a/clang_tidy/README.md b/clang_tidy/README.md new file mode 100644 index 0000000..f6254a6 --- /dev/null +++ b/clang_tidy/README.md @@ -0,0 +1,108 @@ +# Clang-Tidy + +Centralized clang-tidy configuration for Eclipse S-CORE C++ modules. + +## What This Provides + +| File | Purpose | +|------|---------| +| `.clang-tidy` | Canonical S-CORE check set (conservative baseline, tailorable per module) | +| `clang_tidy.bazelrc` | Importable Bazel flags that activate the `clang-tidy` test config | + +This module ships **policy assets**, not API wrappers. Consumers wire up +`lint_clang_tidy_aspect` / `lint_test` from `@aspect_rules_lint` directly — +exactly the same pattern used by the `sanitizers` module. + +## Setup (8 steps) + +### 1 — Add Bazel dependencies + +In your `MODULE.bazel`: + +```python +bazel_dep(name = "score_cpp_policies", version = "") +bazel_dep(name = "aspect_rules_lint", version = "2.5.0") +bazel_dep(name = "toolchains_llvm", version = "1.7.0") +``` + +Register an LLVM toolchain via the `llvm` extension (see `tests/MODULE.bazel` for a +minimal example). + +### 2 — Import `clang_tidy.bazelrc` + +In your workspace `.bazelrc`: + +```bazelrc +import %workspace%/path/to/clang_tidy.bazelrc # if vendored locally +# — or, once Bazel supports external-repo imports — +# import %workspace%/../clang_tidy/clang_tidy.bazelrc +``` + +> **Tip**: If your repo layout places `score_cpp_policies` as a sibling (as in the +> `tests/` self-test workspace here), a relative `import` works. Otherwise vendor +> the three lines from `clang_tidy.bazelrc` into your own `.bazelrc`. + +### 3 — Create `tools/lint/BUILD.bazel` + +```python +# tools/lint/BUILD.bazel (empty package marker is sufficient) +``` + +### 4 — Create `tools/lint/linters.bzl` + +```python +load("@aspect_rules_lint//lint:clang_tidy.bzl", "lint_clang_tidy_aspect") +load("@aspect_rules_lint//lint:lint_test.bzl", "lint_test") + +clang_tidy_aspect = lint_clang_tidy_aspect( + binary = Label("@llvm_toolchain//:clang-tidy"), + configs = [ + Label("@score_cpp_policies//clang_tidy:.clang-tidy"), # central baseline + Label("//:.clang-tidy"), # local overrides (optional) + ], + lint_target_headers = True, + angle_includes_are_system = True, +) + +clang_tidy_test = lint_test(aspect = clang_tidy_aspect) +``` + +If you do not need per-module overrides, omit the `Label("//:.clang-tidy")` entry and +skip step 5. + +### 5 — (Optional) Add a local `.clang-tidy` override + +Place a `.clang-tidy` at your repo root to extend or tighten the central check set. +Use [`@score_cpp_policies//clang_tidy:.clang-tidy`](.clang-tidy) as the starting point. + +> **Advisory checks**: Checks in the `cppcoreguidelines-*` and `modernize-*` families are +> advisory (warnings only). Only `clang-analyzer-*` is `WarningsAsErrors` by default. +> Module owners should tighten `WarningsAsErrors` incrementally as compliance improves. + +### 6 — Expose `clang_tidy_test` in a `BUILD` file + +```python +load("//tools/lint:linters.bzl", "clang_tidy_test") + +clang_tidy_test( + name = "clang_tidy", + srcs = ["//..."], # or a more targeted glob +) +``` + +### 7 — Run + +```bash +bazel test --config=clang-tidy //... +``` + +Reports are written to `bazel-out/.../rules_lint_report/` as text files next to each +linted target. + +### 8 — CI integration + +Add a workflow job that runs: + +```bash +bazel test --config=clang-tidy //... +``` diff --git a/clang_tidy/clang_tidy.bazelrc b/clang_tidy/clang_tidy.bazelrc new file mode 100644 index 0000000..12fde45 --- /dev/null +++ b/clang_tidy/clang_tidy.bazelrc @@ -0,0 +1,16 @@ +# ******************************************************************************* +# Copyright (c) 2026 Contributors to the Eclipse Foundation +# +# See the NOTICE file(s) distributed with this work for additional +# information regarding copyright ownership. +# +# This program and the accompanying materials are made available under the +# terms of the Apache License Version 2.0 which is available at +# https://www.apache.org/licenses/LICENSE-2.0 +# +# SPDX-License-Identifier: Apache-2.0 +# ******************************************************************************* + +test:clang-tidy --aspects=//tools/lint:linters.bzl%clang_tidy_aspect +test:clang-tidy --output_groups=+rules_lint_report +test:clang-tidy --extra_toolchains=@llvm_toolchain//:cc-toolchain-x86_64-linux diff --git a/tests/.bazelrc b/tests/.bazelrc index d2b7f84..b2ae9e4 100644 --- a/tests/.bazelrc +++ b/tests/.bazelrc @@ -11,12 +11,12 @@ # SPDX-License-Identifier: Apache-2.0 # ******************************************************************************* -# Import centralized sanitizer configurations import %workspace%/../sanitizers/sanitizers.bazelrc +import %workspace%/../clang_tidy/clang_tidy.bazelrc + common --registry=https://raw.githubusercontent.com/eclipse-score/bazel_registry/main/ common --registry=https://bcr.bazel.build -# Use LLVM toolchain for sanitizer configs (same as consuming modules) build:asan_ubsan_lsan --extra_toolchains=@llvm_toolchain//:cc-toolchain-x86_64-linux build:tsan --extra_toolchains=@llvm_toolchain//:cc-toolchain-x86_64-linux diff --git a/tests/MODULE.bazel b/tests/MODULE.bazel index 62d0e1b..93ffc75 100644 --- a/tests/MODULE.bazel +++ b/tests/MODULE.bazel @@ -11,13 +11,10 @@ # SPDX-License-Identifier: Apache-2.0 # ******************************************************************************* -module( - name = "score_cpp_policies_tests", -) +module(name = "score_cpp_policies_tests") bazel_dep(name = "googletest", version = "1.17.0.bcr.2") -bazel_dep(name = "rules_cc", version = "0.1.5") -bazel_dep(name = "toolchains_llvm", version = "1.7.0") +bazel_dep(name = "rules_cc", version = "0.2.17") bazel_dep(name = "score_cpp_policies") local_path_override( @@ -25,7 +22,13 @@ local_path_override( path = "..", ) -llvm = use_extension("@toolchains_llvm//toolchain/extensions:llvm.bzl", "llvm") +# LLVM toolchain for clang-tidy (provides @llvm_toolchain//:clang-tidy). +bazel_dep(name = "toolchains_llvm", version = "1.7.0") + +llvm = use_extension( + "@toolchains_llvm//toolchain/extensions:llvm.bzl", + "llvm", +) llvm.toolchain( llvm_version = "19.1.7", extra_known_features = [ diff --git a/tests/tools/lint/BUILD.bazel b/tests/tools/lint/BUILD.bazel new file mode 100644 index 0000000..206c4e4 --- /dev/null +++ b/tests/tools/lint/BUILD.bazel @@ -0,0 +1,17 @@ +# ******************************************************************************* +# Copyright (c) 2026 Contributors to the Eclipse Foundation +# +# See the NOTICE file(s) distributed with this work for additional +# information regarding copyright ownership. +# +# This program and the accompanying materials are made available under the +# terms of the Apache License Version 2.0 which is available at +# https://www.apache.org/licenses/LICENSE-2.0 +# +# SPDX-License-Identifier: Apache-2.0 +# ******************************************************************************* + +exports_files( + ["linters.bzl"], + visibility = ["//visibility:public"], +) diff --git a/tests/tools/lint/linters.bzl b/tests/tools/lint/linters.bzl new file mode 100644 index 0000000..d7d4cd1 --- /dev/null +++ b/tests/tools/lint/linters.bzl @@ -0,0 +1,27 @@ +# ******************************************************************************* +# Copyright (c) 2026 Contributors to the Eclipse Foundation +# +# See the NOTICE file(s) distributed with this work for additional +# information regarding copyright ownership. +# +# This program and the accompanying materials are made available under the +# terms of the Apache License Version 2.0 which is available at +# https://www.apache.org/licenses/LICENSE-2.0 +# +# SPDX-License-Identifier: Apache-2.0 +# ******************************************************************************* + +"""Clang-tidy aspect and test rule.""" + +load("@aspect_rules_lint//lint:clang_tidy.bzl", "lint_clang_tidy_aspect") +load("@aspect_rules_lint//lint:lint_test.bzl", "lint_test") + +clang_tidy_aspect = lint_clang_tidy_aspect( + binary = Label("@llvm_toolchain//:clang-tidy"), + configs = [Label("@score_cpp_policies//clang_tidy:.clang-tidy")], + lint_target_headers = True, + angle_includes_are_system = True, + +) + +clang_tidy_test = lint_test(aspect = clang_tidy_aspect)