From a01e5e262e6d604c21ed11c420a2a245397b995a Mon Sep 17 00:00:00 2001 From: Keith Smiley Date: Mon, 7 Oct 2024 18:06:46 +0000 Subject: [PATCH] Allow disabling clang-tidy only for headers 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 --- clang_tidy/clang_tidy.bzl | 43 +++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/clang_tidy/clang_tidy.bzl b/clang_tidy/clang_tidy.bzl index db6fc95..89e9950 100644 --- a/clang_tidy/clang_tidy.bzl +++ b/clang_tidy/clang_tidy.bzl @@ -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 @@ -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) @@ -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(