-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add third_party dependency bazel_clang_tidy
GitOrigin-RevId: d562a4d340d456180acb7f1d3a78c0ded2a83302
- Loading branch information
1 parent
eb8507b
commit dc86456
Showing
22 changed files
with
602 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,2 @@ | ||
build:clang-tidy --aspects @bazel_clang_tidy//clang_tidy:clang_tidy.bzl%clang_tidy_aspect | ||
build:clang-tidy --output_groups=report |
This file contains 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,9 @@ | ||
UseColor: true | ||
|
||
Checks: > | ||
bugprone-*, | ||
cppcoreguidelines-*, | ||
google-*, | ||
performance-*, | ||
WarningsAsErrors: "*" |
This file contains 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,36 @@ | ||
# This is a basic workflow to help you get started with Actions | ||
|
||
name: CI | ||
|
||
# Controls when the action will run. | ||
on: | ||
# Triggers the workflow on push or pull request events but only for the master branch | ||
push: | ||
branches: [master] | ||
pull_request: | ||
branches: [master] | ||
|
||
# Allows you to run this workflow manually from the Actions tab | ||
workflow_dispatch: | ||
|
||
# A workflow run is made up of one or more jobs that can run sequentially or in parallel | ||
jobs: | ||
# This workflow contains a single job called "build" | ||
build: | ||
# The type of runner that the job will run on | ||
runs-on: ubuntu-latest | ||
|
||
# Steps represent a sequence of tasks that will be executed as part of the job | ||
steps: | ||
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it | ||
- uses: actions/checkout@v2 | ||
|
||
- name: Mount bazel cache | ||
uses: actions/cache@v1 | ||
with: | ||
path: "/home/runner/.cache/bazel" | ||
key: ${{ runner.os }}-bazel | ||
|
||
# Runs a single command using the runners shell | ||
- name: Build | ||
run: bazelisk build //... |
This file contains 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 @@ | ||
bazel-* |
This file contains 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,35 @@ | ||
filegroup( | ||
name = "clang_tidy_config_default", | ||
srcs = [ | ||
".clang-tidy", | ||
# '//example:clang_tidy_config', # add package specific configs if needed | ||
], | ||
) | ||
|
||
label_flag( | ||
name = "clang_tidy_config", | ||
build_setting_default = ":clang_tidy_config_default", | ||
visibility = ["//visibility:public"], | ||
) | ||
|
||
filegroup( | ||
name = "clang_tidy_executable_default", | ||
srcs = [], # empty list: system clang-tidy | ||
) | ||
|
||
label_flag( | ||
name = "clang_tidy_executable", | ||
build_setting_default = ":clang_tidy_executable_default", | ||
visibility = ["//visibility:public"], | ||
) | ||
|
||
filegroup( | ||
name = "clang_tidy_additional_deps_default", | ||
srcs = [], | ||
) | ||
|
||
label_flag( | ||
name = "clang_tidy_additional_deps", | ||
build_setting_default = ":clang_tidy_additional_deps_default", | ||
visibility = ["//visibility:public"], | ||
) |
This file contains 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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2020 Benedek Thaler | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains 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 @@ | ||
module(name = "bazel_clang_tidy") |
This file contains 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,123 @@ | ||
# bazel_clang_tidy | ||
|
||
Run clang-tidy on Bazel C/C++ targets directly, | ||
without generating a compile commands database, | ||
and take advantage of Bazels powerful cache mechanism. | ||
|
||
Usage: | ||
|
||
```py | ||
# //:WORKSPACE | ||
load( | ||
"@bazel_tools//tools/build_defs/repo:git.bzl", | ||
"git_repository", | ||
) | ||
|
||
git_repository( | ||
name = "bazel_clang_tidy", | ||
commit = "bff5c59c843221b05ef0e37cef089ecc9d24e7da", | ||
remote = "https://github.com/erenon/bazel_clang_tidy.git", | ||
) | ||
``` | ||
|
||
You can now compile using the default clang tidy configuration provided using | ||
the following command; | ||
|
||
```text | ||
bazel build //... \ | ||
--aspects @bazel_clang_tidy//clang_tidy:clang_tidy.bzl%clang_tidy_aspect \ | ||
--output_groups=report | ||
``` | ||
|
||
If you would like to override the default clang tidy configuration then you can | ||
reconfigure the default target from the command line. To do this you must first | ||
make a filegroup target that has the .clang-tidy config file as a data | ||
dependency. | ||
|
||
```py | ||
# //:BUILD | ||
filegroup( | ||
name = "clang_tidy_config", | ||
srcs = [".clang-tidy"], | ||
visibility = ["//visibility:public"], | ||
) | ||
``` | ||
|
||
Now you can override the default config file in this repository using | ||
a command line flag; | ||
|
||
```sh | ||
bazel build //... \ | ||
--aspects @bazel_clang_tidy//clang_tidy:clang_tidy.bzl%clang_tidy_aspect \ | ||
--output_groups=report \ | ||
--@bazel_clang_tidy//:clang_tidy_config=//:clang_tidy_config | ||
``` | ||
|
||
:exclamation: the config-file will not be forced by adding it to the clang-tidy command line. Therefore it must be in one of the parents of all source files. It is recommended to put it in the root directly besides the WORKSPACE file. | ||
|
||
Now if you don't want to type this out every time, it is recommended that you | ||
add a config in your .bazelrc that matches this command line; | ||
|
||
```text | ||
# Required for bazel_clang_tidy to operate as expected | ||
build:clang-tidy --aspects @bazel_clang_tidy//clang_tidy:clang_tidy.bzl%clang_tidy_aspect | ||
build:clang-tidy --output_groups=report | ||
# Optionally override the .clang-tidy config file target | ||
build:clang-tidy --@bazel_clang_tidy//:clang_tidy_config=//:clang_tidy_config | ||
``` | ||
|
||
Now from the command line this is a lot nicer to use; | ||
|
||
```sh | ||
bazel build //... --config clang-tidy | ||
``` | ||
|
||
### use a non-system clang-tidy | ||
|
||
by default, bazel_clang_tidy uses the system provided clang-tidy. | ||
If you have a hermetic build, you can use your own clang-tidy target like this: | ||
|
||
```text | ||
build:clang-tidy --@bazel_clang_tidy//:clang_tidy_executable=@local_config_cc//:clangtidy_bin | ||
``` | ||
|
||
This aspect is not executed on external targets. To exclude other targets, | ||
users may tag a target with `no-clang-tidy` or `noclangtidy`. | ||
|
||
## Features | ||
|
||
- Run clang-tidy on any C/C++ target | ||
- A file is treated as C if it has the `.c` extension or its target includes the `clang-tidy-is-c-tu` tag; otherwise, it is treated as C++. | ||
- Run clang-tidy without also building the target | ||
- Use Bazel to cache clang-tidy reports: recompute stale reports only | ||
|
||
## Install | ||
|
||
Copy `.clang-tidy`, `BUILD` and `clang_tidy` dir to your workspace. | ||
Edit `.clang-tidy` as needed. | ||
|
||
## Example | ||
|
||
To see the tool in action: | ||
|
||
1. Clone the repository | ||
1. Run clang-tidy: | ||
|
||
```sh | ||
bazel build //example --aspects clang_tidy/clang_tidy.bzl%clang_tidy_aspect --output_groups=report | ||
``` | ||
|
||
1. Check the error: | ||
|
||
```text | ||
lib.cpp:4:43: error: the parameter 'name' is copied for each invocation but only used as a const reference; consider making it a const reference [performance-unnecessary-value-param,-warnings-as-errors] std::string lib_get_greet_for(std::string name) | ||
Aspect //clang_tidy:clang_tidy.bzl%clang_tidy_aspect of //example:app failed to build | ||
``` | ||
|
||
1. Fix the error by changing `lib.cpp` only. | ||
1. Re-run clang-tidy with the same command. Observe that it does not run clang-tidy for `app.cpp`: the cached report is re-used. | ||
|
||
## Requirements | ||
|
||
- Bazel 4.0 or newer (might work with older versions) |
This file contains 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 @@ | ||
workspace(name = "bazel_clang_tidy") |
This file contains 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,6 @@ | ||
sh_binary( | ||
name = "clang_tidy", | ||
srcs = ["run_clang_tidy.sh"], | ||
data = ["//:clang_tidy_config"], | ||
visibility = ["//visibility:public"], | ||
) |
Oops, something went wrong.