Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ bazel_dep(name = "aspect_bazel_lib", version = "2.7.7")
bazel_dep(name = "aspect_rules_js", version = "1.40.0")
bazel_dep(name = "aspect_tools_telemetry", version = "0.2.8")
bazel_dep(name = "bazel_features", version = "1.0.0")
bazel_dep(name = "bazel_skylib", version = "1.4.2")
bazel_dep(name = "bazel_skylib", version = "1.8.1")
bazel_dep(name = "platforms", version = "0.0.7")
bazel_dep(name = "rules_multirun", version = "0.9.0")
bazel_dep(name = "rules_multitool", version = "0.11.0")
Expand Down
11 changes: 11 additions & 0 deletions docs/clang-tidy.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion example/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ bazel_dep(name = "aspect_rules_swc", version = "2.0.0")
bazel_dep(name = "aspect_rules_ts", version = "3.0.0")
bazel_dep(name = "rules_buf", version = "0.5.2")
bazel_dep(name = "bazel_features", version = "1.29.0")
bazel_dep(name = "bazel_skylib", version = "1.4.2")
bazel_dep(name = "bazel_skylib", version = "1.8.1")
bazel_dep(name = "toolchains_llvm", version = "1.1.2")
bazel_dep(name = "toolchains_protoc", version = "0.3.7")
bazel_dep(name = "rules_java", version = "8.5.0")
Expand Down
9 changes: 9 additions & 0 deletions example/WORKSPACE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,15 @@ load("@buildifier_prebuilt//:deps.bzl", "buildifier_prebuilt_deps")

buildifier_prebuilt_deps()

http_archive(
name = "bazel_skylib",
sha256 = "51b5105a760b353773f904d2bbc5e664d0987fbaf22265164de65d43e910d8ac",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.8.1/bazel-skylib-1.8.1.tar.gz",
"https://github.com/bazelbuild/bazel-skylib/releases/download/1.8.1/bazel-skylib-1.8.1.tar.gz",
],
)

load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")

bazel_skylib_workspace()
Expand Down
32 changes: 31 additions & 1 deletion lint/clang_tidy.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ clang_tidy = lint_clang_tidy_aspect(
```
"""

load("@bazel_skylib//rules/directory:providers.bzl", "DirectoryInfo")
load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES")
load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain")
load("//lint/private:lint_aspect.bzl", "LintOptionsInfo", "OPTIONAL_SARIF_PARSER_TOOLCHAIN", "OUTFILE_FORMAT", "noop_lint_action", "output_files", "parse_to_sarif_action", "patch_and_output_files")
Expand All @@ -50,6 +51,8 @@ def _gather_inputs(ctx, compilation_context, srcs):
inputs = srcs + ctx.files._configs
if (any(ctx.files._global_config)):
inputs.append(ctx.files._global_config[0])
for dep in ctx.files._deps:
inputs.append(dep)
return depset(inputs, transitive = [compilation_context.headers])

def _toolchain_env(ctx, user_flags, action_name = ACTION_NAMES.cpp_compile):
Expand Down Expand Up @@ -262,6 +265,14 @@ def _get_args(ctx, compilation_context, srcs):
def _get_compiler_args(ctx, compilation_context, srcs):
# add args specified by the toolchain, on the command line and rule copts
args = []

if ctx.attr._gcc_install_dir:
gcc_install_dir = ctx.attr._gcc_install_dir[0].files.to_list()
if len(gcc_install_dir) > 1:
fail("gcc_install_dir must contain at most one directory")
for dir in gcc_install_dir:
args.append("--gcc-install-dir=" + dir.path)

rule_flags = ctx.rule.attr.copts if hasattr(ctx.rule.attr, "copts") else []
sources_are_cxx = _is_cxx(srcs[0])
if (sources_are_cxx):
Expand Down Expand Up @@ -409,7 +420,16 @@ def _clang_tidy_aspect_impl(target, ctx):
parse_to_sarif_action(ctx, _MNEMONIC, raw_machine_report, outputs.machine.out)
return [info]

def lint_clang_tidy_aspect(binary, configs = [], global_config = [], header_filter = "", lint_target_headers = False, angle_includes_are_system = True, verbose = False):
def lint_clang_tidy_aspect(
binary,
configs = [],
global_config = [],
gcc_install_dir = [],
deps = [],
header_filter = "",
lint_target_headers = False,
angle_includes_are_system = True,
verbose = False):
"""A factory function to create a linter aspect.

Args:
Expand All @@ -427,6 +447,9 @@ def lint_clang_tidy_aspect(binary, configs = [], global_config = [], header_filt
files which may be used for formatting fixes.
global_config: label of a single global .clang-tidy file to pass to clang-tidy on the command line. This
will cause clang-tidy to ignore any other config files in the source directories.
deps: labels of additional dependencies used during the clang-tidy run.
gcc_install_dir: optional, label of a `Directory` from the skylib library pointing to the gcc install
directory. The argument is passed to the underlying clang as `--gcc-install-dir`.
header_filter: optional, set to a posix regex to supply to clang-tidy with the -header-filter option
lint_target_headers: optional, set to True to pass a pattern that includes all headers with the target's
directory prefix. This crude control may include headers from the linted target in the results. If
Expand Down Expand Up @@ -455,6 +478,13 @@ def lint_clang_tidy_aspect(binary, configs = [], global_config = [], header_filt
default = global_config,
allow_files = True,
),
"_deps": attr.label_list(
default = deps,
),
"_gcc_install_dir": attr.label_list(
default = gcc_install_dir,
providers = [DirectoryInfo],
),
"_lint_target_headers": attr.bool(
default = lint_target_headers,
),
Expand Down
Loading