Skip to content

Commit

Permalink
Add global option to disable running clang-tidy on headers
Browse files Browse the repository at this point in the history
* Commit f43f9d6 added support to run
  clang-tidy on headers. After that commit, all headers will be checked
  by clang-tidy.
* Commit f43f9d6 added a tag to make it
  possible to not run clang-tidy on specific libraries.
* For projects that only want to run clang-tidy on source files, the
  only way to disable running clang-tidy on headers is to add the above
  mentioned tag to all libraries. For large projects this requires
  changing all BUILD files. This commit adds a global option instead to
  always disable running clang-tidy on headers.
  • Loading branch information
johnor committed Nov 27, 2024
1 parent f23d924 commit 83a1214
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
8 changes: 8 additions & 0 deletions BUILD
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")

bool_flag(
name = "run_on_headers",
build_setting_default = True,
visibility = ["//visibility:public"],
)

filegroup(
name = "clang_tidy_config_default",
srcs = [
Expand Down
8 changes: 8 additions & 0 deletions clang_tidy/clang_tidy.bzl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES")
load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain")
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")

def _run_tidy(
ctx,
Expand Down Expand Up @@ -198,6 +199,12 @@ def _clang_tidy_aspect_impl(target, ctx):
cxx_flags = _safe_flags(_toolchain_flags(ctx, ACTION_NAMES.cpp_compile) + rule_flags) + ["-xc++"]

include_headers = "no-clang-tidy-headers" not in ctx.rule.attr.tags

run_on_headers = ctx.attr._run_on_headers[BuildSettingInfo].value
if type(run_on_headers) == type(True):
if not run_on_headers:
include_headers = False

srcs = _rule_sources(ctx, include_headers)

outputs = [
Expand Down Expand Up @@ -229,6 +236,7 @@ clang_tidy_aspect = aspect(
"_clang_tidy_executable": attr.label(default = Label("//:clang_tidy_executable")),
"_clang_tidy_additional_deps": attr.label(default = Label("//:clang_tidy_additional_deps")),
"_clang_tidy_config": attr.label(default = Label("//:clang_tidy_config")),
"_run_on_headers": attr.label(default = Label("//:run_on_headers")),
},
toolchains = ["@bazel_tools//tools/cpp:toolchain_type"],
)

0 comments on commit 83a1214

Please sign in to comment.