-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add target to apply clang-tidy fixes
- Loading branch information
Showing
6 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
workspace(name = "bazel_clang_tidy") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#!/usr/bin/env bash | ||
set -euo pipefail | ||
|
||
bazel=$(readlink -f /proc/${PPID}/exe) | ||
|
||
args=$(printf " union %s" "${@}" | sed 's/^ union \(.*\)/\1/') | ||
targets="${args:-//...}" | ||
|
||
bazel_tidy_config=(\ | ||
"--aspects=@@WORKSPACE@//clang_tidy:clang_tidy.bzl%clang_tidy_aspect" \ | ||
"--@@WORKSPACE@//:clang_tidy_executable=@TIDY_BINARY@" \ | ||
"--@@WORKSPACE@//:clang_tidy_config=@TIDY_CONFIG@" \ | ||
"--output_groups=report") | ||
|
||
cd $BUILD_WORKSPACE_DIRECTORY | ||
|
||
exported_fixes=$("$bazel" aquery \ | ||
"mnemonic(\"ClangTidy\", kind(\"cc_.* rule\", $targets))" \ | ||
--noshow_progress \ | ||
--ui_event_filters=-info \ | ||
"${bazel_tidy_config[@]}" \ | ||
| grep 'Outputs:' \ | ||
| sed 's:^\s\+Outputs\: \[\(.*\)\]$:\1:') | ||
|
||
"$bazel" build \ | ||
--noshow_progress \ | ||
--ui_event_filters=-info,-error,-stdout,-stderr \ | ||
--keep_going \ | ||
"${bazel_tidy_config[@]}" \ | ||
"${@:-//...}" || true | ||
|
||
for file in $exported_fixes; do | ||
# get the build directory which is probably some sandbox | ||
build_dir=$(grep --max-count=1 'BuildDirectory:' "$file" \ | ||
| sed "s:\s\+BuildDirectory\:\s\+'\(.*\)':\1:" || true) | ||
|
||
# if we didn't find BuildDirectory, it's probably an empty file | ||
if [ -z "$build_dir" ]; then | ||
continue | ||
fi | ||
|
||
# relative path of a fix file copied to BUILD_WORKSPACE_DIRECTORY | ||
suggested_fixes=$(basename "$file") | ||
|
||
# strip the build_dir prefix | ||
# and set BuildDirectory to empty | ||
# so clang-apply-replacements won't look for it | ||
sed "s:$build_dir/::" "$file" \ | ||
| sed "s:$build_dir::" \ | ||
> "$suggested_fixes" | ||
|
||
# resolve symlinks and relative paths | ||
while path=$(grep --max-count=1 '_virtual_includes\|\./' "$suggested_fixes" \ | ||
| sed "s:\s\+FilePath\:\s\+'\(.*\)':\1:" || true); do | ||
if [ -z "$path" ]; then | ||
break | ||
fi | ||
|
||
sed -i "s:$path:$(readlink -f $path):" "$suggested_fixes" | ||
done | ||
|
||
# remove the original exported fixes, otherwise they are found by | ||
# clang-apply-replacements | ||
rm -f "$file" | ||
done | ||
|
||
@APPLY_REPLACEMENTS_BINARY@ -remove-change-desc-files . |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
""" | ||
clang-tidy fix rule | ||
""" | ||
|
||
def _clang_tidy_apply_fixes_impl(ctx): | ||
apply_fixes = ctx.actions.declare_file( | ||
"clang_tidy.{}.sh".format(ctx.attr.name), | ||
) | ||
|
||
config = ctx.attr._tidy_config.files.to_list() | ||
if len(config) != 1: | ||
fail(":config ({}) must contain a single file".format(config)) | ||
|
||
apply_bin = ctx.attr._apply_replacements_binary.files_to_run.executable | ||
apply_path = apply_bin.path if apply_bin else "clang-apply-replacements" | ||
|
||
ctx.actions.expand_template( | ||
template = ctx.attr._template.files.to_list()[0], | ||
output = apply_fixes, | ||
substitutions = { | ||
"@APPLY_REPLACEMENTS_BINARY@": apply_path, | ||
"@TIDY_BINARY@": str(ctx.attr._tidy_binary.label), | ||
"@TIDY_CONFIG@": str(ctx.attr._tidy_config.label), | ||
"@WORKSPACE@": ctx.label.workspace_name, | ||
}, | ||
) | ||
|
||
tidy_bin = ctx.attr._tidy_binary.files_to_run.executable | ||
runfiles = ctx.runfiles( | ||
( | ||
[apply_bin] if apply_bin else [] + | ||
[tidy_bin] if tidy_bin else [] + | ||
config | ||
), | ||
) | ||
|
||
return [ | ||
DefaultInfo( | ||
executable = apply_fixes, | ||
runfiles = runfiles, | ||
), | ||
] | ||
|
||
clang_tidy_apply_fixes = rule( | ||
implementation = _clang_tidy_apply_fixes_impl, | ||
fragments = ["cpp"], | ||
attrs = { | ||
"_template": attr.label(default = Label("//clang_tidy:apply_fixes_template")), | ||
"_tidy_config": attr.label(default = Label("//:clang_tidy_config")), | ||
"_tidy_binary": attr.label(default = Label("//:clang_tidy_executable")), | ||
"_apply_replacements_binary": attr.label( | ||
default = Label("//:clang_apply_replacements_executable"), | ||
), | ||
}, | ||
toolchains = ["@bazel_tools//tools/cpp:toolchain_type"], | ||
executable = True, | ||
) |