Skip to content

Commit

Permalink
Allow disabling clang-tidy only for headers
Browse files Browse the repository at this point in the history
Since bazel doesn't automatically build headers in isolation unless you
enable parse_headers (which only recently works at all), headers may not
pass clang-tidy in isolation. This adds a new `no-clang-tidy-headers`
tag that can be added to libraries so that only their source files are
clang-tidy'd
  • Loading branch information
keith authored and erenon committed Oct 9, 2024
1 parent c4c4b9c commit a01e5e2
Showing 1 changed file with 25 additions and 18 deletions.
43 changes: 25 additions & 18 deletions clang_tidy/clang_tidy.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -83,26 +83,29 @@ def _run_tidy(
)
return outfile

def _rule_sources(ctx):
def _rule_sources(ctx, include_headers):
header_extensions = (
".h",
".hh",
".hpp",
".hxx",
".inc",
".inl",
".H",
)
permitted_file_types = [
".c",
".cc",
".cpp",
".cxx",
".c++",
".C",
] + list(header_extensions)

def check_valid_file_type(src):
"""
Returns True if the file type matches one of the permitted srcs file types for C and C++ header/source files.
"""
permitted_file_types = [
".c",
".cc",
".cpp",
".cxx",
".c++",
".C",
".h",
".hh",
".hpp",
".hxx",
".inc",
".inl",
".H",
]
for file_type in permitted_file_types:
if src.basename.endswith(file_type):
return True
Expand All @@ -115,7 +118,10 @@ def _rule_sources(ctx):
if hasattr(ctx.rule.attr, "hdrs"):
for hdr in ctx.rule.attr.hdrs:
srcs += [hdr for hdr in hdr.files.to_list() if hdr.is_source and check_valid_file_type(hdr)]
return srcs
if include_headers:
return srcs
else:
return [src for src in srcs if not src.basename.endswith(header_extensions)]

def _toolchain_flags(ctx, action_name = ACTION_NAMES.cpp_compile):
cc_toolchain = find_cpp_toolchain(ctx)
Expand Down Expand Up @@ -189,7 +195,8 @@ def _clang_tidy_aspect_impl(target, ctx):
c_flags = _safe_flags(_toolchain_flags(ctx, ACTION_NAMES.c_compile) + rule_flags) + ["-xc"]
cxx_flags = _safe_flags(_toolchain_flags(ctx, ACTION_NAMES.cpp_compile) + rule_flags) + ["-xc++"]

srcs = _rule_sources(ctx)
include_headers = "no-clang-tidy-headers" not in ctx.rule.attr.tags
srcs = _rule_sources(ctx, include_headers)

outputs = [
_run_tidy(
Expand Down

0 comments on commit a01e5e2

Please sign in to comment.