-
Notifications
You must be signed in to change notification settings - Fork 64
Add clang_tidy_test #85
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5e07a7b
Add clang_tidy_test
keith bc3b49b
remove escaping
keith e3cdb6d
add fix exporting
keith 18b83b8
add skylib
keith 6c83842
Revert "add skylib"
keith 48b1b83
remove skylib dep
keith 4ea801b
requires bash
keith File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
build:clang-tidy --aspects @bazel_clang_tidy//clang_tidy:clang_tidy.bzl%clang_tidy_aspect | ||
build:clang-tidy --output_groups=report | ||
build:clang-tidy --output_groups=report | ||
|
||
build --test_output=errors |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
module(name = "bazel_clang_tidy") | ||
|
||
bazel_dep(name = "bazel_skylib", version = "1.7.1") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,12 @@ | ||
workspace(name = "bazel_clang_tidy") | ||
|
||
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") | ||
|
||
http_archive( | ||
name = "bazel_skylib", | ||
sha256 = "bc283cdfcd526a52c3201279cda4bc298652efa898b10b4db0837dc51652756f", | ||
urls = [ | ||
"https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.7.1/bazel-skylib-1.7.1.tar.gz", | ||
"https://github.com/bazelbuild/bazel-skylib/releases/download/1.7.1/bazel-skylib-1.7.1.tar.gz", | ||
], | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
"""A test rule to run clang-tidy""" | ||
|
||
load("@bazel_skylib//lib:shell.bzl", "shell") | ||
load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES") | ||
load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain") | ||
load(":clang_tidy.bzl", "deps_flags", "is_c_translation_unit", "rule_sources", "safe_flags", "toolchain_flags") | ||
|
||
# Tests run with a different directory structure than normal compiles. This | ||
# fixes up include paths or any other arguments that are sensitive to this | ||
def _fix_argument_path(ctx, arg): | ||
return arg.replace(ctx.bin_dir.path, ".") | ||
|
||
def _get_copts_attr(ctx, copts_attr): | ||
copts = [] | ||
for copt in getattr(ctx.attr, copts_attr): | ||
copts.append(ctx.expand_make_variables( | ||
copts_attr, | ||
copt, | ||
{}, | ||
)) | ||
|
||
return copts | ||
|
||
def _clang_tidy_rule_impl(ctx): | ||
clang_tidy = ctx.attr.clang_tidy_executable | ||
clang_tidy_executable = clang_tidy[DefaultInfo].files_to_run.executable | ||
|
||
ccinfo_copts, additional_files = deps_flags(ctx, ctx.attr.deps) | ||
|
||
include_headers = "no-clang-tidy-headers" not in ctx.attr.tags | ||
srcs = rule_sources(ctx.attr, include_headers) | ||
|
||
rule_copts = _get_copts_attr(ctx, "copts") | ||
rule_conlyopts = _get_copts_attr(ctx, "conlyopts") | ||
rule_cxxopts = _get_copts_attr(ctx, "cxxopts") | ||
|
||
c_flags = safe_flags(toolchain_flags(ctx, ACTION_NAMES.c_compile) + rule_copts + rule_conlyopts) + ["-xc"] | ||
cxx_flags = safe_flags(toolchain_flags(ctx, ACTION_NAMES.cpp_compile) + rule_copts + rule_cxxopts) + ["-xc++"] | ||
|
||
ctx.actions.write( | ||
output = ctx.outputs.executable, | ||
is_executable = True, | ||
content = """\ | ||
#!/usr/bin/env bash | ||
|
||
set -euo pipefail | ||
|
||
readonly bin="{clang_tidy_bin}" | ||
readonly config="{clang_tidy_config}" | ||
|
||
test -e .clang-tidy || ln -s -f \\$config .clang-tidy | ||
if [[ ! -f .clang-tidy ]]; then | ||
echo "error: failed to setup config" >&2 | ||
exit 1 | ||
fi | ||
|
||
ln -s .. external | ||
|
||
has_srcs=false | ||
if [[ -n "{c_sources}" ]]; then | ||
"$bin" --quiet --export-fixes $TEST_UNDECLARED_OUTPUTS_DIR/cfixes.yaml {c_sources} -- {c_flags} | ||
has_srcs=true | ||
fi | ||
|
||
if [[ -n "{cxx_sources}" ]]; then | ||
"$bin" --quiet --export-fixes $TEST_UNDECLARED_OUTPUTS_DIR/cxxfixes.yaml {cxx_sources} -- {cxx_flags} | ||
has_srcs=true | ||
fi | ||
|
||
if [[ "$has_srcs" == "false" ]]; then | ||
echo "error: no sources to run clang-tidy on" >&2 | ||
exit 1 | ||
fi | ||
""".format( | ||
clang_tidy_bin = clang_tidy_executable.short_path if clang_tidy_executable else "clang-tidy", | ||
clang_tidy_config = ctx.file.clang_tidy_config.short_path, | ||
output = ctx.outputs.executable.path, | ||
c_sources = " ".join([x.short_path for x in srcs if is_c_translation_unit(x, ctx.attr.tags)]), | ||
cxx_sources = " ".join([x.short_path for x in srcs if not is_c_translation_unit(x, ctx.attr.tags)]), | ||
c_flags = " ".join([shell.quote(_fix_argument_path(ctx, x)) for x in ccinfo_copts + c_flags]), | ||
cxx_flags = " ".join([shell.quote(_fix_argument_path(ctx, x)) for x in ccinfo_copts + cxx_flags]), | ||
erenon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
), | ||
) | ||
|
||
return [ | ||
DefaultInfo( | ||
executable = ctx.outputs.executable, | ||
runfiles = ctx.runfiles( | ||
ctx.files.srcs + ctx.files.hdrs + ctx.files.data, | ||
transitive_files = depset( | ||
[ctx.file.clang_tidy_config], | ||
transitive = [additional_files, find_cpp_toolchain(ctx).all_files, ctx.attr.clang_tidy_additional_deps.files], | ||
), | ||
) | ||
.merge(clang_tidy[DefaultInfo].default_runfiles), | ||
), | ||
] | ||
|
||
clang_tidy_test = rule( | ||
implementation = _clang_tidy_rule_impl, | ||
test = True, | ||
fragments = ["cpp"], | ||
attrs = { | ||
"deps": attr.label_list(providers = [CcInfo]), | ||
"clang_tidy_executable": attr.label(default = Label("//:clang_tidy_executable")), | ||
"clang_tidy_additional_deps": attr.label(default = Label("//:clang_tidy_additional_deps")), | ||
"clang_tidy_config": attr.label( | ||
default = Label("//:clang_tidy_config"), | ||
allow_single_file = True, | ||
), | ||
"srcs": attr.label_list(allow_files = True), | ||
"hdrs": attr.label_list(allow_files = True), | ||
"data": attr.label_list(allow_files = True), | ||
"copts": attr.string_list(), | ||
"conlyopts": attr.string_list(), | ||
"cxxopts": attr.string_list(), | ||
}, | ||
toolchains = ["@bazel_tools//tools/cpp:toolchain_type"], | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
load("//clang_tidy:clang_tidy_test.bzl", "clang_tidy_test") | ||
|
||
clang_tidy_test( | ||
name = "check_files_test", | ||
srcs = [ | ||
"lib.cpp", | ||
], | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
int func() { | ||
int result = 5; | ||
return result; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.