Skip to content

Commit

Permalink
Allow external clang-tidy configs
Browse files Browse the repository at this point in the history
In the clang-tidy wrapper script, create a symlink in the current
working directory to the provided clang-tidy config if not already
present.
  • Loading branch information
oliverlee authored and erenon committed Jan 6, 2023
1 parent 31d62bf commit 41df6ad
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
4 changes: 1 addition & 3 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ label_flag(
visibility = ["//visibility:public"],
)


filegroup(
name = "clang_tidy_executable_default",
srcs = [], # empty list: system clang-tidy
srcs = [], # empty list: system clang-tidy
)

label_flag(
Expand All @@ -24,7 +23,6 @@ label_flag(
visibility = ["//visibility:public"],
)


filegroup(
name = "clang_tidy_additional_deps_default",
srcs = [],
Expand Down
25 changes: 22 additions & 3 deletions clang_tidy/clang_tidy.bzl
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain")

def _run_tidy(ctx, wrapper, exe, additional_deps, config, flags, compilation_context, infile, discriminator):
inputs = depset(direct = [infile, config] + additional_deps.files.to_list() + ([exe.files_to_run.executable] if exe.files_to_run.executable else []), transitive = [compilation_context.headers])
def _run_tidy(
ctx,
wrapper,
exe,
additional_deps,
config,
flags,
compilation_context,
infile,
discriminator):
inputs = depset(
direct = (
[infile, config] +
additional_deps.files.to_list() +
([exe.files_to_run.executable] if exe.files_to_run.executable else [])
),
transitive = [compilation_context.headers],
)

args = ctx.actions.args()

Expand All @@ -12,11 +28,14 @@ def _run_tidy(ctx, wrapper, exe, additional_deps, config, flags, compilation_con

# this is consumed by the wrapper script
if len(exe.files.to_list()) == 0:
args.add("clang-tidy")
args.add("clang-tidy")
else:
args.add(exe.files_to_run.executable)

args.add(outfile.path) # this is consumed by the wrapper script

args.add(config.path)

args.add("--export-fixes", outfile.path)

# add source to check
Expand Down
9 changes: 8 additions & 1 deletion clang_tidy/run_clang_tidy.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#! /bin/bash
#!/usr/bin/env bash
# Usage: run_clang_tidy <OUTPUT> [ARGS...]
set -ue

Expand All @@ -8,10 +8,17 @@ shift
OUTPUT=$1
shift

CONFIG=$1
shift

# clang-tidy doesn't create a patchfile if there are no errors.
# make sure the output exists, and empty if there are no errors,
# so the build system will not be confused.
touch $OUTPUT
truncate -s 0 $OUTPUT

# if $CONFIG is provided by some external workspace, we need to
# place it in the current directory
test -e .clang-tidy || ln -s -f $CONFIG .clang-tidy

"${CLANG_TIDY_BIN}" "$@"

0 comments on commit 41df6ad

Please sign in to comment.