From f55e394212d32c024b6de76645cb3055e4a51169 Mon Sep 17 00:00:00 2001 From: Ryan Northey Date: Wed, 27 Nov 2024 15:22:54 +0000 Subject: [PATCH 1/2] bazel/clang-tidy: Cleanups and optimizations Signed-off-by: Ryan Northey --- bazel/format/clang_tidy/clang_tidy.bzl | 85 +++++++++-------------- bazel/format/clang_tidy/run_clang_tidy.sh | 2 + 2 files changed, 34 insertions(+), 53 deletions(-) diff --git a/bazel/format/clang_tidy/clang_tidy.bzl b/bazel/format/clang_tidy/clang_tidy.bzl index 64c1172e2..3944a632c 100644 --- a/bazel/format/clang_tidy/clang_tidy.bzl +++ b/bazel/format/clang_tidy/clang_tidy.bzl @@ -11,12 +11,13 @@ def _run_tidy( compilation_context, infile, discriminator): + direct_deps = [infile, config] + if exe.files_to_run.executable: + direct_deps.append(exe.files_to_run.executable) + if additional_deps.files: + direct_deps.append(additional_deps.files) inputs = depset( - direct = ( - [infile, config] + - additional_deps.files.to_list() + - ([exe.files_to_run.executable] if exe.files_to_run.executable else []) - ), + direct = direct_deps, transitive = [compilation_context.headers], ) @@ -24,49 +25,29 @@ def _run_tidy( # specify the output file - twice outfile = ctx.actions.declare_file( - "bazel_clang_tidy_" + infile.path + "." + discriminator + ".clang-tidy.yaml", + "bazel_clang_tidy_%s.%s.clang-tidy.yaml" % (infile.path, discriminator) ) - # this is consumed by the wrapper script - if len(exe.files.to_list()) == 0: - 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(exe.files_to_run.executable if exe.files else "clang-tidy") + args.add(outfile.path) args.add(config.path) - args.add("--export-fixes", outfile.path) - args.add("--quiet") - # add source to check args.add(infile.path) # start args passed to the compiler args.add("--") - # add args specified by the toolchain, on the command line and rule copts args.add_all(flags) - # add defines - for define in compilation_context.defines.to_list(): - args.add("-D" + define) - - for define in compilation_context.local_defines.to_list(): - args.add("-D" + define) - + args.add_all(compilation_context.defines, before_each = "-D") + args.add_all(compilation_context.local_defines, before_each = "-D") # add includes - for i in compilation_context.framework_includes.to_list(): - args.add("-F" + i) - - for i in compilation_context.includes.to_list(): - args.add("-I" + i) - - args.add_all(compilation_context.quote_includes.to_list(), before_each = "-iquote") - - args.add_all(compilation_context.system_includes.to_list(), before_each = "-isystem") + args.add_all(compilation_context.framework_includes, before_each = "-F") + args.add_all(compilation_context.includes, before_each = "-I") + args.add_all(compilation_context.quote_includes, before_each = "-iquote") + args.add_all(compilation_context.system_includes, before_each = "-isystem") ctx.actions.run( inputs = inputs, @@ -79,23 +60,25 @@ def _run_tidy( ) return outfile -def _rule_sources(ctx): - 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 - return False +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 + return False + + +def _rule_sources(ctx): srcs = [] if hasattr(ctx.rule.attr, "srcs"): for src in ctx.rule.attr.srcs: - srcs += [src for src in src.files.to_list() if src.is_source and check_valid_file_type(src)] + srcs += [_src for _src in src.files.to_list() if _src.is_source and _check_valid_file_type(_src)] return srcs def _toolchain_flags(ctx, action_name = ACTION_NAMES.cpp_compile): @@ -124,7 +107,6 @@ def _safe_flags(flags): "-fno-canonical-system-headers", "-fstack-usage", ] - return [flag for flag in flags if flag not in unsupported_flags and not flag.startswith("--sysroot")] def _clang_tidy_aspect_impl(target, ctx): @@ -143,9 +125,6 @@ def _clang_tidy_aspect_impl(target, ctx): ACTION_NAMES.cpp_compile: _safe_flags(_toolchain_flags(ctx, ACTION_NAMES.cpp_compile) + rule_flags), ACTION_NAMES.c_compile: _safe_flags(_toolchain_flags(ctx, ACTION_NAMES.c_compile) + rule_flags), } - - srcs = _rule_sources(ctx) - outputs = [ _run_tidy( ctx, @@ -158,11 +137,11 @@ def _clang_tidy_aspect_impl(target, ctx): src, target.label.name, ) - for src in srcs + for src in _rule_sources(ctx) ] return [ - OutputGroupInfo(report = depset(direct = outputs)), + OutputGroupInfo(report = depset(direct = outputs)) ] clang_tidy_aspect = aspect( diff --git a/bazel/format/clang_tidy/run_clang_tidy.sh b/bazel/format/clang_tidy/run_clang_tidy.sh index 9cadcf56f..f11f28a77 100755 --- a/bazel/format/clang_tidy/run_clang_tidy.sh +++ b/bazel/format/clang_tidy/run_clang_tidy.sh @@ -21,4 +21,6 @@ truncate -s 0 "$OUTPUT" # place it in the current directory test -e .clang-tidy || ln -s -f "$CONFIG" .clang-tidy +# echo "RUN: ${CLANG_TIDY_BIN} ${*}" + "${CLANG_TIDY_BIN}" "$@" |& grep -v "warnings generated" || : From 821544aa1f4d8045331f77f1a645187faf026433 Mon Sep 17 00:00:00 2001 From: Ryan Northey Date: Sun, 1 Dec 2024 22:17:04 +0000 Subject: [PATCH 2/2] use file extension for sources Signed-off-by: Ryan Northey --- bazel/format/clang_tidy/clang_tidy.bzl | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/bazel/format/clang_tidy/clang_tidy.bzl b/bazel/format/clang_tidy/clang_tidy.bzl index 3944a632c..6b120475f 100644 --- a/bazel/format/clang_tidy/clang_tidy.bzl +++ b/bazel/format/clang_tidy/clang_tidy.bzl @@ -1,6 +1,10 @@ load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES") load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain") +TARGET_EXTENSIONS = [ + "c", "cc", "cpp", "cxx", "c++", "C", "h", "hh", "hpp", "hxx", "inc", "inl", "H", +] + def _run_tidy( ctx, wrapper, @@ -60,25 +64,11 @@ def _run_tidy( ) return outfile - -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 - return False - - def _rule_sources(ctx): srcs = [] if hasattr(ctx.rule.attr, "srcs"): for src in ctx.rule.attr.srcs: - srcs += [_src for _src in src.files.to_list() if _src.is_source and _check_valid_file_type(_src)] + srcs += [_src for _src in src.files.to_list() if _src.is_source and _src.extension in TARGET_EXTENSIONS] return srcs def _toolchain_flags(ctx, action_name = ACTION_NAMES.cpp_compile):