Skip to content

Commit a01e5e2

Browse files
keitherenon
authored andcommitted
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
1 parent c4c4b9c commit a01e5e2

File tree

1 file changed

+25
-18
lines changed

1 file changed

+25
-18
lines changed

clang_tidy/clang_tidy.bzl

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -83,26 +83,29 @@ def _run_tidy(
8383
)
8484
return outfile
8585

86-
def _rule_sources(ctx):
86+
def _rule_sources(ctx, include_headers):
87+
header_extensions = (
88+
".h",
89+
".hh",
90+
".hpp",
91+
".hxx",
92+
".inc",
93+
".inl",
94+
".H",
95+
)
96+
permitted_file_types = [
97+
".c",
98+
".cc",
99+
".cpp",
100+
".cxx",
101+
".c++",
102+
".C",
103+
] + list(header_extensions)
104+
87105
def check_valid_file_type(src):
88106
"""
89107
Returns True if the file type matches one of the permitted srcs file types for C and C++ header/source files.
90108
"""
91-
permitted_file_types = [
92-
".c",
93-
".cc",
94-
".cpp",
95-
".cxx",
96-
".c++",
97-
".C",
98-
".h",
99-
".hh",
100-
".hpp",
101-
".hxx",
102-
".inc",
103-
".inl",
104-
".H",
105-
]
106109
for file_type in permitted_file_types:
107110
if src.basename.endswith(file_type):
108111
return True
@@ -115,7 +118,10 @@ def _rule_sources(ctx):
115118
if hasattr(ctx.rule.attr, "hdrs"):
116119
for hdr in ctx.rule.attr.hdrs:
117120
srcs += [hdr for hdr in hdr.files.to_list() if hdr.is_source and check_valid_file_type(hdr)]
118-
return srcs
121+
if include_headers:
122+
return srcs
123+
else:
124+
return [src for src in srcs if not src.basename.endswith(header_extensions)]
119125

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

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

194201
outputs = [
195202
_run_tidy(

0 commit comments

Comments
 (0)