generated from bazel-contrib/rules-template
-
-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: clang-tidy for linting of C/C++ code (#287)
* install clang-tidy and test data * fix: some windows issues * feat: clang-tidy aspect * feat: integrate clang-tidy aspect into example * feat: avoid missing linters on windows * fix: fix compile error * fix: code tidy * fix: extra debugging for --fix mode * feat: support clang-tidy --fix on linux * fix: bug * fix: chmod=+x * fix: tests * fix: typo * fix: remove workaround_windows.bat * fix: show all reports on windows * chore: update docs * fix: address comments * fix: address comments * fix: address comment * fix: send multiple files to clang-tidy * fix: typo * fix: make wrapper executable * chore: green up buildifier * chore: update docs * chore: fix integration test --------- Co-authored-by: Alex Eagle <[email protected]>
- Loading branch information
1 parent
78cf65d
commit 7a14cfd
Showing
30 changed files
with
970 additions
and
12 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,6 +1,22 @@ | ||
# Automatically apply --config=linux, --config=windows etc | ||
common --enable_platform_specific_config | ||
|
||
# Don't depend on a JAVA_HOME pointing at a system JDK | ||
# see https://github.com/bazelbuild/rules_jvm_external/issues/445 | ||
build --repo_env=JAVA_HOME=../bazel_tools/jdk | ||
|
||
common --incompatible_enable_proto_toolchain_resolution | ||
common --@aspect_rules_ts//ts:skipLibCheck=always | ||
|
||
# enable symlinks on windows | ||
startup --windows_enable_symlinks | ||
|
||
# c++ options | ||
common:linux --action_env=BAZEL_CXXOPTS="-std=c++20" | ||
common:windows --action_env=BAZEL_CXXOPTS="/std:c++20" | ||
|
||
# clang-tidy needs the INCLUDE variable set to the system value. | ||
# Would prefer to do this inside the aspect, not sure if it is possible. | ||
common --action_env=INCLUDE | ||
# ensure that minimal other envvars are passed by clang-tidy run_shell | ||
common --incompatible_strict_action_env |
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,19 @@ | ||
# https://clang.llvm.org/extra/clang-tidy/ | ||
# config-file to provoke some checks in example | ||
--- | ||
Checks: "*, | ||
-abseil-*, | ||
-altera-*, | ||
-android-*, | ||
-fuchsia-*, | ||
-google-*, | ||
-llvm*, | ||
-modernize-use-trailing-return-type, | ||
-zircon-*, | ||
-readability-else-after-return, | ||
-readability-static-accessed-through-instance, | ||
-readability-avoid-const-params-in-decls, | ||
-cppcoreguidelines-non-private-member-variables-in-classes, | ||
-misc-non-private-member-variables-in-classes," | ||
# promote a random warning to error to test integration. Do not copy this line. | ||
WarningsAsErrors: "readability-inconsistent-declaration-parameter-name" |
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
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,18 @@ | ||
load("@rules_cc//cc:defs.bzl", "cc_library") | ||
|
||
exports_files(["get/.clang-tidy"]) | ||
|
||
cc_library( | ||
name = "hello-time", | ||
srcs = [ | ||
"get/get-time.cc", | ||
"get/get-time.h", | ||
"hello-time.cc", | ||
], | ||
hdrs = [ | ||
"hello-time.h", | ||
"xhello-time.h", | ||
], | ||
includes = ["."], | ||
visibility = ["//src/cpp/main:__pkg__"], | ||
) |
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,7 @@ | ||
# https://clang.llvm.org/extra/clang-tidy/ | ||
# this is an example of a .clang-tidy file in a subdirectory that overrides | ||
# the configuration for files in this directory and below. It must be made | ||
# visibile through an export_files declaration in this package and added | ||
# to the 'configs' attribute in clang_tidy aspect. | ||
--- | ||
Checks: "-*, misc-const-correctness" |
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,13 @@ | ||
#include "get-time.h" | ||
#include <ctime> | ||
#include <string> | ||
|
||
// Some deliberately bad code | ||
const char* get_localtime_impl() { | ||
// warning: variable 'result' of type 'std::time_t' (aka 'long long') can be declared 'const' [misc-const-correctness] | ||
std::time_t result = std::time(nullptr); | ||
// warning: function 'asctime' is not bounds-checking and non-reentrant; 'strftime' should be used instead [bugprone-unsafe-functions,cert-msc24-c,cert-msc33-c] | ||
std::string result_str = std::asctime(std::localtime(&result)); | ||
// rules_lint author: note the memory leak is not detected | ||
return strdup(result_str.c_str()); | ||
} |
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,17 @@ | ||
#ifndef LIB_GET_TIME_H_ | ||
#define LIB_GET_TIME_H_ | ||
|
||
#include <string.h> | ||
|
||
const char* get_localtime_impl(); | ||
|
||
// Deliberatly bad code | ||
inline char* get_localtime() { | ||
const char* time = get_localtime_impl(); | ||
char timebuf[20]; | ||
strcpy(timebuf, time); | ||
// warning: Address of stack memory associated with local variable 'timebuf' returned to caller [clang-analyzer-core.StackAddressEscape] | ||
return timebuf; | ||
} | ||
|
||
#endif |
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,12 @@ | ||
#include "hello-time.h" | ||
// warning: included header ctime is not used directly [misc-include-cleaner] | ||
#include <ctime> | ||
#include <iostream> | ||
#include <get/get-time.h> | ||
|
||
// Deliberately bad code | ||
void print_localtime() { | ||
char* localtime = get_localtime(); | ||
// warning: do not call c-style vararg functions [cppcoreguidelines-pro-type-vararg,hicpp-vararg] | ||
printf("%s\n", localtime); | ||
} |
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,16 @@ | ||
#ifndef LIB_HELLO_TIME_H_ | ||
#define LIB_HELLO_TIME_H_ | ||
|
||
#include <string> | ||
#include <stdio.h> | ||
#include <xhello-time.h> | ||
|
||
void print_localtime(); | ||
|
||
inline void print_localtime2() { | ||
std::string a = "time"; | ||
for (int i=0; i<a.size(); ++i) { | ||
printf("%s", a.c_str()[i]); | ||
} | ||
} | ||
#endif |
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,13 @@ | ||
#ifndef LIB_HELLO_TIME2_H_ | ||
#define LIB_HELLO_TIME2_H_ | ||
|
||
#include <string> | ||
#include <stdio.h> | ||
|
||
inline void print_localtime3() { | ||
std::string a = "time"; | ||
for (int i=0; i<a.size(); ++i) { | ||
printf("%s", a.c_str()[i]); | ||
} | ||
} | ||
#endif |
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,26 @@ | ||
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library") | ||
|
||
cc_library( | ||
name = "hello-greet", | ||
srcs = ["hello-greet.cc"], | ||
hdrs = ["hello-greet.h"], | ||
includes = ["."], | ||
) | ||
|
||
cc_binary( | ||
name = "hello-world", | ||
srcs = ["hello-world.cc"], | ||
includes = ["."], | ||
deps = [ | ||
":hello-greet", | ||
"//src/cpp/lib:hello-time", | ||
], | ||
) | ||
|
||
cc_library( | ||
name = "hello-greet-with-error", | ||
srcs = ["hello-greet-with-error.cc"], | ||
hdrs = ["hello-greet-with-error.h"], | ||
includes = ["."], | ||
tags = ["manual"], | ||
) |
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,5 @@ | ||
#include "hello-greet-with-error.h" | ||
|
||
std::string get_greet(const std::string& who) { | ||
return "Hello " + who; | ||
} |
Oops, something went wrong.
oops, this broke the
--fix
option to this script, which was untested...