Skip to content

Commit

Permalink
handle repeated use of @rules_clang_tidy//:extra-options
Browse files Browse the repository at this point in the history
Change-Id: Idb97dc2465ef25e11df19fe77e4a5c5e3d4993bd
  • Loading branch information
oliverlee committed Aug 20, 2024
1 parent a1957b1 commit e8394ab
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 24 deletions.
60 changes: 47 additions & 13 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,28 @@ jobs:
run: |
bazel run //tools:buildifier.check
check-tidy:
check-tidy-matrix:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
include:
- bazel: latest
compiler: clang-18
example: misc-unused-alias-decls
- bazel: latest
compiler: clang-17
example: misc-unused-alias-decls
- bazel: latest
compiler: clang-16
example: misc-unused-alias-decls
- bazel: latest
compiler: clang-15
example: misc-unused-alias-decls
- bazel: latest
compiler: clang-14
example: misc-unused-alias-decls
- bazel: 7.x
compiler: clang-18
example: misc-unused-alias-decls
- bazel: 6.x
compiler: clang-18
example: misc-unused-alias-decls
- bazel: 5.x
compiler: clang-18
example: misc-unused-alias-decls

steps:
- name: install ${{ matrix.compiler }}
Expand Down Expand Up @@ -74,7 +66,7 @@ jobs:
run: |
set -x
cd example/${{ matrix.example }}
cd example
if [[ "${{ matrix.bazel }}" < 6 ]]; then
sed -i '/enable_bzlmod/d' .bazelrc
Expand All @@ -85,14 +77,56 @@ jobs:
--config=clang-tidy \
--color=yes \
//... | tee log || true
cat log
grep "error: .*${{ matrix.example }}" log
grep "error: .*misc-unused-alias-decls" log
check-tidy-extra-options:
runs-on: ubuntu-latest
steps:
- name: install clang-tidy
shell: bash
run: |
set -x
version=18
wget https://apt.llvm.org/llvm.sh
chmod +x llvm.sh
sudo ./llvm.sh $version
sudo apt-get install -y clang-tidy-$version libc++-$version-dev
sudo ln -sf $(which clang-tidy-$version) /usr/bin/clang-tidy
clang-tidy-$version --version
- uses: actions/checkout@v4

- name: run clang-tidy
shell: bash
env:
CC: clang-18
run: |
set -x
cd example
bazel build \
--announce_rc \
--config=clang-tidy \
--color=yes \
--@rules_clang_tidy//:extra-options=--enable-check-profile \
--@rules_clang_tidy//:extra-options=--checks='fuchsia-*' \
//... | tee log || true
grep "clang-tidy checks profiling" log
grep "error: .*misc-unused-alias-decls" log
grep "error: .*fuchsia-trailing-return" log
all:
runs-on: ubuntu-latest
if: ${{ github.base_ref == 'main' }}
needs:
- buildifier
- check-tidy
- check-tidy-matrix
- check-tidy-extra-options
steps:
- run: true
6 changes: 3 additions & 3 deletions BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("@bazel_skylib//rules:common_settings.bzl", "string_flag")
load("//private:extra_options_flag.bzl", "extra_options_flag")
load("//private:path_binary_wrapper.bzl", "path_binary_wrapper")

package(default_visibility = ["//visibility:public"])
Expand Down Expand Up @@ -36,9 +36,9 @@ label_flag(
# Extra options appended after `tidy_options`. This allows extra options to be
# specified on the command line (e.g. changing the checks to use with
# `clang-tidy`).
string_flag(
extra_options_flag(
name = "extra-options",
build_setting_default = "",
build_setting_default = [""],
)

# Manually applies fixes from .yaml files contained in a specified directory.
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ build:clang-tidy-export-fixes --remote_download_outputs=toplevel
bazel build //... --config=clang-tidy-export-fixes
```

If only a subset of checks needs to be run, those can be specified with `extra-options`.
If only a subset of checks needs to be run, those can be specified with
`extra-options`. This flag can be specified multiples times.

```sh
bazel build //... --config=clang-tidy-export-fixes \
Expand Down Expand Up @@ -160,4 +161,4 @@ set, `clang-apply-replacements` must be in `PATH`. Similarly to
## Requirements

- Bazel 5.x
- ClangTidy ??
- ClangTidy ?? (at least 14?)
File renamed without changes.
4 changes: 4 additions & 0 deletions example/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
cc_binary(
name = "misc-unused-alias-decls",
srcs = ["misc-unused-alias-decls.cpp"],
)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
local_repository(
name = "rules_clang_tidy",
path = "../..",
path = "..",
)
File renamed without changes.
4 changes: 0 additions & 4 deletions example/misc-unused-alias-decls/BUILD.bazel

This file was deleted.

2 changes: 1 addition & 1 deletion make_clang_tidy_aspect.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ sed --in-place --expression "s+$(pwd)+%workspace%+g" {outfile}
" ".join(kwargs["tidy_options"]),
),
extra_options = sub_outfile(
ctx.attr._extra_options[BuildSettingInfo].value,
" ".join(ctx.attr._extra_options[BuildSettingInfo].value),
),
infile = source_file.path,
outfile = out.path,
Expand Down
18 changes: 18 additions & 0 deletions private/extra_options_flag.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"""
Defines a repeatable options flag.
"""

load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")

def _impl(ctx):
return [
BuildSettingInfo(value = ctx.build_setting_value),
]

extra_options_flag = rule(
implementation = _impl,
build_setting = config.string_list(
flag = True,
repeatable = True,
),
)

0 comments on commit e8394ab

Please sign in to comment.