Skip to content
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

always generate fixes in ClangTidy aspect #22

Merged
merged 1 commit into from
Aug 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 19 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ rules_clang_tidy_dependencies()
# //:.bazelrc
build:clang-tidy --aspects=@rules_clang_tidy//:aspects.bzl%check
build:clang-tidy --output_groups=report
build:clang-tidy --remote_download_outputs=toplevel
build:clang-tidy --keep_going
```

Expand All @@ -52,13 +53,14 @@ To specify a specific binary (e.g. `clang-tidy` is specified by a hermetic
toolchain like [this](https://github.com/bazel-contrib/toolchains_llvm)), update
the build setting in `.bazelrc`.

```Starlark
```starlark
# //:.bazelrc

build --@rules_clang_tidy//:clang-tidy=@llvm18//:clang-tidy

build:clang-tidy --aspects=@rules_clang_tidy//:aspects.bzl%check
build:clang-tidy --output_groups=report
build:clang-tidy --remote_download_outputs=toplevel
build:clang-tidy --keep_going
```

Expand All @@ -71,7 +73,7 @@ build:clang-tidy --keep_going
To override the default `.clang-tidy`, define a `filegroup` containing the
replacement config and update build setting in `.bazelrc`.

```Starlark
```starlark
# //:BUILD.bazel

filegroup(
Expand All @@ -81,13 +83,14 @@ filegroup(
)
```

```Starlark
```starlark
# //:.bazelrc

build --@rules_clang_tidy//:config=//:clang-tidy-config

build:clang-tidy --aspects=@rules_clang_tidy//:aspects.bzl%check
build:clang-tidy --output_groups=report
build:clang-tidy --remote_download_outputs=toplevel
build:clang-tidy --keep_going
```

Expand All @@ -97,38 +100,27 @@ build:clang-tidy --keep_going

<details><summary></summary>

To apply fixes, generate the exported fixes with the `export_fixes` aspect.

```Starlark
# //:.bazelrc

build:clang-tidy-export-fixes --aspects=@rules_clang_tidy//:aspects.bzl%export_fixes
build:clang-tidy-export-fixes --output_groups=report
build:clang-tidy-export-fixes --remote_download_outputs=toplevel
```
To apply fixes, run `@rules_clang_tidy//:apply-fixes`. This uses the exported
fixes generated by the `check` aspect in the `output_path`.

```sh
bazel build //... --config=clang-tidy-export-fixes
bazel build //... --config=clang-tidy
bazel run @rules_clang_tidy//:apply-fixes -- $(bazel info output_path)
```

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 \
bazel build //... --config=clang-tidy \
--@rules_clang_tidy//:extra-options="--checks=-*,misc-unused-alias-decls"
```

Then apply the exported fixes with

```sh
bazel run @rules_clang_tidy//:apply-fixes -- $(bazel info output_path)
```

Alternatively, use rule `apply_fixes` and specify the dependencies for the
target.

```Starlark
```starlark
load("@rules_clang_tidy//:defs.bzl", "apply_fixes")

apply_fixes(
Expand All @@ -146,6 +138,7 @@ and run the `apply_fixes` target
```sh
bazel run //:apply-fixes

# or generate and apply fixes for only a single check
bazel run //:apply-fixes \
--@rules_clang_tidy//:extra-options="--checks=-*,misc-unused-alias-decls*"
```
Expand All @@ -156,6 +149,12 @@ set, `clang-apply-replacements` must be in `PATH`. Similarly to
`--@rules_clang_tidy//:clang-tidy`, it's convenient to define the value in
`.bazelrc`.

```starlark
# //:.bazelrc

build --@rules_clang_tidy//:clang-apply-replacements=@llvm18//:clang-apply-replacements
```

</details>

## Requirements
Expand Down
6 changes: 3 additions & 3 deletions apply_fixes.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ load("@bazel_skylib//lib:shell.bzl", "shell")
load("@bazel_skylib//lib:versions.bzl", "versions")
load("@local_bazel_version//:bazel_version.bzl", "BAZEL_VERSION")
load("//private:functional.bzl", fn = "functional")
load(":aspects.bzl", "export_fixes")
load(":aspects.bzl", "check")

def _do_verify_deps(ctx, out):
label = str(ctx.label).strip("@").removesuffix(".verify")
Expand Down Expand Up @@ -204,7 +204,7 @@ _verify_deps = rule(

def _apply_fixes_impl(ctx):
apply_bin = ctx.attr._clang_apply_replacements.files_to_run.executable
depsets = [dep[OutputGroupInfo].report for dep in ctx.attr.deps]
depsets = [dep[OutputGroupInfo].fixes for dep in ctx.attr.deps]
fixes = [f for dep in depsets for f in dep.to_list()]

runfiles = ctx.runfiles(
Expand Down Expand Up @@ -252,7 +252,7 @@ _apply_fixes = rule(
attrs = {
"verify": attr.label(),
"deps": attr.label_list(
aspects = [export_fixes],
aspects = [check],
providers = [CcInfo],
mandatory = True,
),
Expand Down
8 changes: 0 additions & 8 deletions aspects.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,3 @@ check = make_clang_tidy_aspect(
"--warnings-as-errors='*'",
],
)

export_fixes = make_clang_tidy_aspect(
options = [
"--use-color",
"--warnings-as-errors='-*'",
"--export-fixes=$@",
],
)
4 changes: 0 additions & 4 deletions example/dep-cc_test/.bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,3 @@ build --symlink_prefix=/

build:clang-tidy --aspects=@rules_clang_tidy//:aspects.bzl%check
build:clang-tidy --output_groups=report

build:clang-tidy-export-fixes --aspects=@rules_clang_tidy//:aspects.bzl%export_fixes
build:clang-tidy-export-fixes --output_groups=report

3 changes: 0 additions & 3 deletions example/hermetic-toolchain/.bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,5 @@ build --symlink_prefix=/
build:clang-tidy --aspects=@rules_clang_tidy//:aspects.bzl%check
build:clang-tidy --output_groups=report

build:clang-tidy-export-fixes --aspects=@rules_clang_tidy//:aspects.bzl%export_fixes
build:clang-tidy-export-fixes --output_groups=report

build --@rules_clang_tidy//:clang-tidy=@llvm_toolchain//:clang-tidy
build --@rules_clang_tidy//:clang-apply-replacements=@llvm_toolchain//:clang-apply-replacements
3 changes: 0 additions & 3 deletions example/misc-unused/.bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,3 @@ build --symlink_prefix=/
build:clang-tidy --aspects=@rules_clang_tidy//:aspects.bzl%check
build:clang-tidy --output_groups=report

build:clang-tidy-export-fixes --aspects=@rules_clang_tidy//:aspects.bzl%export_fixes
build:clang-tidy-export-fixes --output_groups=report

3 changes: 0 additions & 3 deletions example/shared-source/.bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,3 @@ build --symlink_prefix=/
build:clang-tidy --aspects=@rules_clang_tidy//:aspects.bzl%check
build:clang-tidy --output_groups=report

build:clang-tidy-export-fixes --aspects=@rules_clang_tidy//:aspects.bzl%export_fixes
build:clang-tidy-export-fixes --output_groups=report

83 changes: 52 additions & 31 deletions make_clang_tidy_aspect.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,18 @@ def _do_tidy(ctx, compilation_ctx, source_file, **kwargs):
s.elems(),
)).replace(".bzl", "", 1)

outbase = ".".join([
source_file.short_path,
"_".join(fn.map(sanitize, ctx.aspect_ids)),
])

# This output file may be used by clang-apply-replacements which requires a
# yaml extension.
out = ctx.actions.declare_file(
".".join([
source_file.short_path,
"_".join(fn.map(sanitize, ctx.aspect_ids)),
"yaml",
]),
)

sub_outfile = lambda s: s.replace("$@", out.path)
fixes = ctx.actions.declare_file(outbase + ".yaml")
result = ctx.actions.declare_file(outbase + ".result")
phony = ctx.actions.declare_file(outbase + ".phony")

# This action should succeed so that the fixes file is generated and cached.
ctx.actions.run_shell(
inputs = depset(
direct = [
Expand All @@ -82,38 +82,36 @@ def _do_tidy(ctx, compilation_ctx, source_file, **kwargs):
],
transitive = [compilation_ctx.headers],
),
outputs = [out],
outputs = [fixes, result],
arguments = [str(not kwargs["display_stderr"]).lower()],
command = """\
#!/usr/bin/env bash
set -euo pipefail

{clang_tidy} \
({clang_tidy} \
--config-file={config} \
{tidy_options} \
{extra_options} \
--export-fixes="{fixes}" \
{infile} \
-- {compiler_command} 2> log.stderr \
|| (cat log.stderr >&2 && false)
-- {compiler_command} 2> log.stderr && echo "true" > {result})\
|| (cat log.stderr >&2 && echo "false" > {result})

$1 || cat log.stderr

touch {outfile}
touch {fixes}

# replace sandbox path prefix from file paths and
# hope `+` isn't used anywhere
sed --in-place --expression "s+$(pwd)+%workspace%+g" {outfile}
sed --in-place --expression "s+$(pwd)+%workspace%+g" {fixes}
""".format(
clang_tidy = clang_tidy.path,
config = ctx.file._config.path,
tidy_options = sub_outfile(
" ".join(kwargs["tidy_options"]),
),
extra_options = sub_outfile(
" ".join(ctx.attr._extra_options[BuildSettingInfo].value),
),
tidy_options = " ".join(kwargs["tidy_options"]),
extra_options = " ".join(ctx.attr._extra_options[BuildSettingInfo].value),
infile = source_file.path,
outfile = out.path,
fixes = fixes.path,
result = result.path,
compiler_command = " ".join(
_toolchain_args(ctx) +
_compilation_ctx_args(compilation_ctx) +
Expand All @@ -126,7 +124,23 @@ sed --in-place --expression "s+$(pwd)+%workspace%+g" {outfile}
execution_requirements = kwargs["execution_requirements"],
)

return out
# use result to conditionally fail the action
ctx.actions.run_shell(
inputs = depset(direct = [result]),
outputs = [phony],
command = """\
#!/usr/bin/env bash
set -euo pipefail

bash {result}
touch {phony}
""".format(
result = result.path,
phony = phony.path,
),
)

return struct(fixes = fixes, phony = phony)

def _check_attr(ctx):
config_files = ctx.attr._config.files.to_list()
Expand All @@ -148,20 +162,27 @@ def _clang_tidy_aspect_impl(**kwargs):

_check_attr(ctx)

outputs = [
_do_tidy(
fixes = []
phony = []
for source_file in (
_source_files_in(ctx, "hdrs") +
_source_files_in(ctx, "srcs")
):
out = _do_tidy(
ctx,
target[CcInfo].compilation_context,
source_file,
**kwargs
)
for source_file in (
_source_files_in(ctx, "hdrs") +
_source_files_in(ctx, "srcs")
)
]
fixes.append(out.fixes)
phony.append(out.phony)

return [OutputGroupInfo(report = depset(outputs))]
return [
OutputGroupInfo(
report = depset(fixes + phony),
fixes = depset(fixes),
),
]

return impl

Expand Down
5 changes: 5 additions & 0 deletions test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,8 @@ tidy_test(
],
example_external = "hermetic-toolchain",
)

tidy_test(
name = "fix_uses-check-action-cache_test",
example_external = "misc-unused",
)
2 changes: 1 addition & 1 deletion test/fix_hermetic-toolchain_test.bash
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ bazel \
run \
//:apply-fixes | tee "$log"

grep "warning: .*misc-unused-alias-decls" "$log"
grep "error: .*misc-unused-alias-decls" "$log"

diff \
--color=always \
Expand Down
2 changes: 1 addition & 1 deletion test/fix_rule_test.bash
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ bazel \
run \
//:apply-fixes | tee "$log"

grep "warning: .*misc-unused-alias-decls" "$log"
grep "error: .*misc-unused-alias-decls" "$log"

diff \
--color=always \
Expand Down
4 changes: 2 additions & 2 deletions test/fix_script_test.bash
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ unset TEST_TMPDIR
bazel \
--bazelrc="$test_bazelrc" \
build \
--config=clang-tidy-export-fixes \
//...
--config=clang-tidy \
//... || true

# apply fixes
bazel \
Expand Down
23 changes: 23 additions & 0 deletions test/fix_uses-check-action-cache_test.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env bash
set -euxo pipefail

source test/prelude.bash

orig="$(mktemp)"

fix_setup example/misc-unused

# This sets a 15 second idle timeout
# https://github.com/bazelbuild/bazel/issues/11062
unset TEST_TMPDIR

cat "$test_bazelrc" >> .bazelrc

# run check, generating fixes in cache
bazel build --config=clang-tidy //... | tee "$log" || true

grep "error: .*misc-unused-alias-decls" "$log"

bazel run //:apply-fixes 2>&1 | tee "$log"

[ 0 -eq $(grep -c "error: .*misc-unused-alias-decls" "$log") ]
Loading