-
Notifications
You must be signed in to change notification settings - Fork 3
Add centralized clang-tidy infrastructure #2
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
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
|
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's better to add separat readme file in each tool, i.e clang_tidy/readme.md, sanitizers/readme.md etc.
Contributor
Author
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. Fixed. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| # ******************************************************************************* | ||
| # 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", | ||
| ], | ||
| visibility = ["//visibility:public"], | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| # Clang-Tidy Integration | ||
|
|
||
| Centralized clang-tidy configuration and Bazel macros for Eclipse S-CORE C++ modules. | ||
|
|
||
| ## What This Provides | ||
|
|
||
| - **`//clang_tidy:defs.bzl`** — `make_clang_tidy_aspect` / `make_clang_tidy_test` macros wrapping `aspect_rules_lint` | ||
| - **`clang_tidy/.clang-tidy`** — default S-CORE check set (conservative baseline, tailorable per module) | ||
|
|
||
| ## Usage | ||
|
|
||
| ### Add Dependency | ||
|
|
||
| ```python | ||
| bazel_dep(name = "score_cpp_policies", version = "0.0.0") | ||
| bazel_dep(name = "toolchains_llvm", version = "1.5.0") | ||
| ``` | ||
|
|
||
| Set up `@llvm_toolchain` via the `llvm` extension (see `tests/MODULE.bazel` for an example). | ||
|
|
||
| ### Add the Bazelrc Snippet | ||
|
|
||
| There is no importable `.bazelrc` from an external module — `%workspace%` resolves to the | ||
| consuming workspace, not to `@score_cpp_policies`. Copy these lines into your own `.bazelrc` | ||
| instead: | ||
|
|
||
| ```bazelrc | ||
| test:clang-tidy --output_groups=+rules_lint_report | ||
| # Pin the LLVM toolchain for clang-tidy — adjust the target for your host arch: | ||
| test:clang-tidy --extra_toolchains=@llvm_toolchain//:cc-toolchain-x86_64-linux | ||
| test:clang-tidy --aspects=//tools/lint:linters.bzl%clang_tidy_aspect | ||
| ``` | ||
|
|
||
| > **Note**: The `--extra_toolchains` line hard-codes `x86_64-linux`. On AArch64 hosts (Apple | ||
| > Silicon CI runners, etc.) replace it with `cc-toolchain-aarch64-linux`. | ||
|
|
||
| ### Create `tools/lint/linters.bzl` | ||
|
|
||
| ```python | ||
| load("@score_cpp_policies//clang_tidy:defs.bzl", "make_clang_tidy_aspect", "make_clang_tidy_test") | ||
|
|
||
| clang_tidy_aspect = make_clang_tidy_aspect( | ||
| # binary MUST be a Label literal here — string labels silently resolve to the wrong repo. | ||
| binary = Label("@llvm_toolchain//:clang-tidy"), | ||
| configs = [Label("//:.clang-tidy")], | ||
| ) | ||
|
|
||
| clang_tidy_test = make_clang_tidy_test(aspect = clang_tidy_aspect) | ||
| ``` | ||
|
|
||
| ### Add a `.clang-tidy` Config | ||
|
|
||
| Place a `.clang-tidy` at your repo root. Use | ||
| [`@score_cpp_policies//clang_tidy:.clang-tidy`](.clang-tidy) as the starting point. | ||
|
|
||
| > **Advisory checks**: The enabled checks (`cppcoreguidelines-*`, `modernize-*`, etc.) are | ||
| > advisory — only `clang-analyzer-*` is `WarningsAsErrors`. Module owners should tighten | ||
| > `WarningsAsErrors` incrementally as compliance improves. | ||
|
|
||
| ### Run | ||
|
|
||
| ```bash | ||
| # Lint all C++ targets | ||
| bazel test --config=clang-tidy //... | ||
| ``` | ||
|
|
||
| Reports are written to `bazel-out/.../rules_lint_report/` as text files. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| # ******************************************************************************* | ||
| # 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 support macros for S-CORE C++ modules. | ||
|
|
||
| Provides macros to create a clang-tidy aspect and test rule with S-CORE defaults. | ||
| Consuming projects call make_clang_tidy_aspect() in their own linters.bzl to | ||
| create a project-specific aspect bound to their toolchain and .clang-tidy config. | ||
|
|
||
| Example usage in your project's tools/lint/linters.bzl: | ||
|
|
||
| load("@score_cpp_policies//clang_tidy:defs.bzl", "make_clang_tidy_aspect", "make_clang_tidy_test") | ||
|
|
||
| clang_tidy_aspect = make_clang_tidy_aspect( | ||
| binary = Label("@llvm_toolchain//:clang-tidy"), | ||
| configs = [Label("//:.clang-tidy")], | ||
| ) | ||
|
|
||
| clang_tidy_test = make_clang_tidy_test(aspect = clang_tidy_aspect) | ||
| """ | ||
|
|
||
| load("@aspect_rules_lint//lint:clang_tidy.bzl", "lint_clang_tidy_aspect") | ||
| load("@aspect_rules_lint//lint:lint_test.bzl", "lint_test") | ||
|
|
||
| def make_clang_tidy_aspect( | ||
| binary, | ||
| configs, | ||
| lint_target_headers = True, | ||
| angle_includes_are_system = True, | ||
| verbose = False): | ||
| """Creates a clang-tidy lint aspect with S-CORE defaults. | ||
|
|
||
| Args: | ||
| binary: Label of the clang-tidy binary. | ||
| Must be resolved in the calling .bzl file's repository context, | ||
| e.g. Label("@llvm_toolchain//:clang-tidy"). | ||
| configs: List of Labels to .clang-tidy config files that clang-tidy needs | ||
| as inputs, e.g. [Label("//:.clang-tidy")]. | ||
| lint_target_headers: Whether to lint headers owned by analyzed targets (default: True). | ||
| angle_includes_are_system: Treat angle bracket includes as system headers (default: True). | ||
| verbose: Enable verbose clang-tidy output (default: False). | ||
|
|
||
| Returns: | ||
| A clang-tidy aspect. Assign to a top-level variable in your .bzl file | ||
| so it can be referenced via --aspects= in .bazelrc. | ||
| """ | ||
| return lint_clang_tidy_aspect( | ||
| binary = binary, | ||
| configs = configs, | ||
| lint_target_headers = lint_target_headers, | ||
| angle_includes_are_system = angle_includes_are_system, | ||
| verbose = verbose, | ||
| ) | ||
|
|
||
| def make_clang_tidy_test(aspect): | ||
| """Creates a clang-tidy lint test rule for per-target testing. | ||
|
|
||
| Args: | ||
| aspect: The clang-tidy aspect returned by make_clang_tidy_aspect(). | ||
|
|
||
| Returns: | ||
| A rule that can be instantiated in BUILD files to run clang-tidy | ||
| on individual cc_library / cc_binary / cc_test targets. | ||
|
|
||
| Example usage in a BUILD file: | ||
| load("//tools/lint:linters.bzl", "clang_tidy_test") | ||
| clang_tidy_test(name = "my_lib_tidy", srcs = [":my_lib"]) | ||
| """ | ||
| return lint_test(aspect = aspect) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 configuration for the score_cpp_policies test workspace. | ||
| # NOTE: conservative check set so that existing sample_test.cpp passes cleanly. | ||
| Checks: > | ||
| -*, | ||
| clang-analyzer-*, | ||
| bugprone-* | ||
|
|
||
| WarningsAsErrors: "" | ||
|
|
||
| # Exclude third-party and googletest headers from analysis. | ||
| HeaderFilterRegex: '^(?!.*/third_party/)(?!.*/googletest/).*' | ||
|
|
||
| FormatStyle: file |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,19 +13,27 @@ | |
|
|
||
| module( | ||
| name = "score_cpp_policies_tests", | ||
| version = "0.0.0", | ||
| ) | ||
|
|
||
| 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 = "rules_go", version = "0.60.0", repo_name = "io_bazel_rules_go") | ||
|
Member
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. Why are rules_go needed? |
||
|
|
||
| bazel_dep(name = "score_cpp_policies") | ||
| bazel_dep(name = "score_cpp_policies", version = "0.0.0") | ||
|
Member
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. The |
||
| local_path_override( | ||
| module_name = "score_cpp_policies", | ||
| 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 = [ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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"], | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| # ******************************************************************************* | ||
| # 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 for the score_cpp_policies test workspace.""" | ||
|
|
||
| load("@score_cpp_policies//clang_tidy:defs.bzl", "make_clang_tidy_aspect", "make_clang_tidy_test") | ||
|
|
||
| clang_tidy_aspect = make_clang_tidy_aspect( | ||
| binary = Label("@llvm_toolchain//:clang-tidy"), | ||
| configs = [Label("//:.clang-tidy")], | ||
| ) | ||
|
|
||
| clang_tidy_test = make_clang_tidy_test(aspect = clang_tidy_aspect) |
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.
The version = "0.0.0", is not really needed