|
| 1 | +# ******************************************************************************* |
| 2 | +# Copyright (c) 2026 Contributors to the Eclipse Foundation |
| 3 | +# |
| 4 | +# See the NOTICE file(s) distributed with this work for additional |
| 5 | +# information regarding copyright ownership. |
| 6 | +# |
| 7 | +# This program and the accompanying materials are made available under the |
| 8 | +# terms of the Apache License Version 2.0 which is available at |
| 9 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# SPDX-License-Identifier: Apache-2.0 |
| 12 | +# ******************************************************************************* |
| 13 | + |
| 14 | +"""Clang-tidy support macros for S-CORE C++ modules. |
| 15 | +
|
| 16 | +Provides macros to create a clang-tidy aspect and test rule with S-CORE defaults. |
| 17 | +Consuming projects call make_clang_tidy_aspect() in their own linters.bzl to |
| 18 | +create a project-specific aspect bound to their toolchain and .clang-tidy config. |
| 19 | +
|
| 20 | +Example usage in your project's tools/lint/linters.bzl: |
| 21 | +
|
| 22 | + load("@score_cpp_policies//clang_tidy:defs.bzl", "make_clang_tidy_aspect", "make_clang_tidy_test") |
| 23 | +
|
| 24 | + clang_tidy_aspect = make_clang_tidy_aspect( |
| 25 | + binary = Label("@llvm_toolchain//:clang-tidy"), |
| 26 | + configs = [Label("//:.clang-tidy")], |
| 27 | + ) |
| 28 | +
|
| 29 | + clang_tidy_test = make_clang_tidy_test(aspect = clang_tidy_aspect) |
| 30 | +""" |
| 31 | + |
| 32 | +load("@aspect_rules_lint//lint:clang_tidy.bzl", "lint_clang_tidy_aspect") |
| 33 | +load("@aspect_rules_lint//lint:lint_test.bzl", "lint_test") |
| 34 | + |
| 35 | +def make_clang_tidy_aspect( |
| 36 | + binary, |
| 37 | + configs, |
| 38 | + lint_target_headers = True, |
| 39 | + angle_includes_are_system = True, |
| 40 | + verbose = False): |
| 41 | + """Creates a clang-tidy lint aspect with S-CORE defaults. |
| 42 | +
|
| 43 | + Args: |
| 44 | + binary: Label of the clang-tidy binary. |
| 45 | + Must be resolved in the calling .bzl file's repository context, |
| 46 | + e.g. Label("@llvm_toolchain//:clang-tidy"). |
| 47 | + configs: List of Labels to .clang-tidy config files that clang-tidy needs |
| 48 | + as inputs, e.g. [Label("//:.clang-tidy")]. |
| 49 | + lint_target_headers: Whether to lint headers owned by analyzed targets (default: True). |
| 50 | + angle_includes_are_system: Treat angle bracket includes as system headers (default: True). |
| 51 | + verbose: Enable verbose clang-tidy output (default: False). |
| 52 | +
|
| 53 | + Returns: |
| 54 | + A clang-tidy aspect. Assign to a top-level variable in your .bzl file |
| 55 | + so it can be referenced via --aspects= in .bazelrc. |
| 56 | + """ |
| 57 | + return lint_clang_tidy_aspect( |
| 58 | + binary = binary, |
| 59 | + configs = configs, |
| 60 | + lint_target_headers = lint_target_headers, |
| 61 | + angle_includes_are_system = angle_includes_are_system, |
| 62 | + verbose = verbose, |
| 63 | + ) |
| 64 | + |
| 65 | +def make_clang_tidy_test(aspect): |
| 66 | + """Creates a clang-tidy lint test rule for per-target testing. |
| 67 | +
|
| 68 | + Args: |
| 69 | + aspect: The clang-tidy aspect returned by make_clang_tidy_aspect(). |
| 70 | +
|
| 71 | + Returns: |
| 72 | + A rule that can be instantiated in BUILD files to run clang-tidy |
| 73 | + on individual cc_library / cc_binary / cc_test targets. |
| 74 | +
|
| 75 | + Example usage in a BUILD file: |
| 76 | + load("//tools/lint:linters.bzl", "clang_tidy_test") |
| 77 | + clang_tidy_test(name = "my_lib_tidy", srcs = [":my_lib"]) |
| 78 | + """ |
| 79 | + return lint_test(aspect = aspect) |
0 commit comments