Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: parameterise clang-tidy aspect #307

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
# https://bazelbuild.slack.com/archives/C014RARENH0/p1691158021917459?thread_ts=1691156601.420349&cid=C014RARENH0
common --check_direct_dependencies=off

# Provide more output on error in CI
common --verbose_failures
common --test_output=errors

# Load any settings specific to the current user.
# .bazelrc.user should appear in .gitignore so that settings are not shared with team members
# This needs to be last statement in this
Expand Down
6 changes: 4 additions & 2 deletions docs/clang-tidy.md

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

28 changes: 20 additions & 8 deletions lint/clang_tidy.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ 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", "dummy_successful_lint_action", "patch_and_report_files", "report_files")

_MNEMONIC = "AspectRulesLintClangTidy"
# default mnemonic of AspectRulesLintClangTidy can be customized with mnemonic_suffix attribute
_MNEMONIC_PREFIX = "AspectRulesLint"

def _gather_inputs(ctx, compilation_context, srcs):
inputs = srcs + ctx.files._configs + compilation_context.headers.to_list()
Expand Down Expand Up @@ -219,6 +220,9 @@ def _get_args(ctx, compilation_context, srcs):

return args

def _mnemonic(ctx):
return _MNEMONIC_PREFIX + ctx.attr._mnemonic_suffix

def clang_tidy_action(ctx, compilation_context, executable, srcs, stdout, exit_code):
"""Create a Bazel Action that spawns a clang-tidy process.

Expand Down Expand Up @@ -252,8 +256,8 @@ def clang_tidy_action(ctx, compilation_context, executable, srcs, stdout, exit_c
arguments = [executable._clang_tidy.path] + _get_args(ctx, compilation_context, srcs),
use_default_shell_env = True,
env = env,
mnemonic = _MNEMONIC,
progress_message = "Linting %{label} with clang-tidy",
mnemonic = _mnemonic(ctx),
progress_message = "Linting %{label} with " + ctx.attr.name,
)

def clang_tidy_fix(ctx, compilation_context, executable, srcs, patch, stdout, exit_code):
Expand Down Expand Up @@ -297,8 +301,8 @@ def clang_tidy_fix(ctx, compilation_context, executable, srcs, patch, stdout, ex
"JS_BINARY__SILENT_ON_SUCCESS": "1",
},
tools = [executable._clang_tidy_wrapper, executable._clang_tidy],
mnemonic = _MNEMONIC,
progress_message = "Linting %{label} with clang-tidy",
mnemonic = _mnemonic(ctx),
progress_message = "Linting %{label} with " + ctx.attr.name,
)

# buildifier: disable=function-docstring
Expand All @@ -310,20 +314,20 @@ def _clang_tidy_aspect_impl(target, ctx):
compilation_context = target[CcInfo].compilation_context

if ctx.attr._options[LintOptionsInfo].fix:
patch, report, exit_code, info = patch_and_report_files(_MNEMONIC, target, ctx)
patch, report, exit_code, info = patch_and_report_files(_mnemonic(ctx), target, ctx)
if len(files_to_lint) == 0:
dummy_successful_lint_action(ctx, report, exit_code, patch)
else:
clang_tidy_fix(ctx, compilation_context, ctx.executable, files_to_lint, patch, report, exit_code)
else:
report, exit_code, info = report_files(_MNEMONIC, target, ctx)
report, exit_code, info = report_files(_mnemonic(ctx), target, ctx)
if len(files_to_lint) == 0:
dummy_successful_lint_action(ctx, report, exit_code)
else:
clang_tidy_action(ctx, compilation_context, ctx.executable, files_to_lint, report, exit_code)
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, name = "clang-tidy", configs = [], global_config = [], header_filter = "", lint_target_headers = False, angle_includes_are_system = True, verbose = False, mnemonic_suffix = "ClangTidy"):
"""A factory function to create a linter aspect.

Args:
Expand All @@ -336,6 +340,7 @@ def lint_clang_tidy_aspect(binary, configs = [], global_config = [], header_filt
out = "clang_tidy",
)
```
name: name of the aspect.
configs: labels of the .clang-tidy files to make available to clang-tidy's config search. These may be
in subdirectories and clang-tidy will apply them if appropriate. This may also include .clang-format
files which may be used for formatting fixes.
Expand All @@ -349,6 +354,7 @@ def lint_clang_tidy_aspect(binary, configs = [], global_config = [], header_filt
passes these as -isystem. Change this to False to pass these as -I, which allows clang-tidy to regard
them as regular header files.
verbose: print debug messages including clang-tidy command lines being invoked.
mnemonic_suffix: suffix of mnemonic to be used. A prefix of AspectRulesLint is always used.
"""

if type(global_config) == "string":
Expand All @@ -357,6 +363,9 @@ def lint_clang_tidy_aspect(binary, configs = [], global_config = [], header_filt
return aspect(
implementation = _clang_tidy_aspect_impl,
attrs = {
"name": attr.string(
default = name,
),
"_options": attr.label(
default = "//lint:options",
providers = [LintOptionsInfo],
Expand All @@ -381,6 +390,9 @@ def lint_clang_tidy_aspect(binary, configs = [], global_config = [], header_filt
"_verbose": attr.bool(
default = verbose,
),
"_mnemonic_suffix": attr.string(
default = mnemonic_suffix,
),
"_clang_tidy": attr.label(
default = binary,
executable = True,
Expand Down
Loading