aboutsummaryrefslogtreecommitdiff
path: root/compiler_wrapper/clang_tidy_flag.go
diff options
context:
space:
mode:
authorTobias Bosch <tbosch@google.com>2019-07-08 11:03:26 -0700
committerTobias Bosch <tbosch@google.com>2019-07-09 08:28:47 +0000
commit38f3c42a535f0ec2d845c6e299738a4a1996f646 (patch)
tree7de26451ab815925e2dd8a0da127ab8be5b223a5 /compiler_wrapper/clang_tidy_flag.go
parentd868417979bda2479cf8ce0e01768585db2e1f5c (diff)
downloadtoolchain-utils-38f3c42a535f0ec2d845c6e299738a4a1996f646.tar.gz
Support calling clang-tidy in the compiler wrapper.
BUG=chromium:773875 TEST=unit test Change-Id: Iba2a8c38c5542b6289373e8c8e97e2e671008aef Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/toolchain-utils/+/1691114 Reviewed-by: Tobias Bosch <tbosch@google.com> Tested-by: Tobias Bosch <tbosch@google.com>
Diffstat (limited to 'compiler_wrapper/clang_tidy_flag.go')
-rw-r--r--compiler_wrapper/clang_tidy_flag.go97
1 files changed, 97 insertions, 0 deletions
diff --git a/compiler_wrapper/clang_tidy_flag.go b/compiler_wrapper/clang_tidy_flag.go
new file mode 100644
index 00000000..9c1761ee
--- /dev/null
+++ b/compiler_wrapper/clang_tidy_flag.go
@@ -0,0 +1,97 @@
+package main
+
+import (
+ "fmt"
+ "path/filepath"
+ "strings"
+)
+
+func processClangTidyFlags(builder *commandBuilder) (cSrcFile string, useClangTidy bool) {
+ if builder.env.getenv("WITH_TIDY") == "" {
+ return "", false
+ }
+ srcFileSuffixes := []string{
+ ".c",
+ ".cc",
+ ".cpp",
+ ".C",
+ ".cxx",
+ ".c++",
+ }
+ cSrcFile = ""
+ lastArg := ""
+ for _, arg := range builder.args {
+ if hasAtLeastOneSuffix(arg.value, srcFileSuffixes) && lastArg != "-o" {
+ cSrcFile = arg.value
+ }
+ lastArg = arg.value
+ }
+ useClangTidy = cSrcFile != ""
+ return cSrcFile, useClangTidy
+}
+
+func runClangTidy(env env, clangCmd *command, cSrcFile string) error {
+ defaultTidyChecks := strings.Join([]string{
+ "*",
+ "google*",
+ "-bugprone-narrowing-conversions",
+ "-cppcoreguidelines-*",
+ "-fuchsia-*",
+ "-google-build-using-namespace",
+ "-google-default-arguments",
+ "-google-explicit-constructor",
+ "-google-readability*",
+ "-google-runtime-int",
+ "-google-runtime-references",
+ "-hicpp-avoid-c-arrays",
+ "-hicpp-braces-around-statements",
+ "-hicpp-no-array-decay",
+ "-hicpp-signed-bitwise",
+ "-hicpp-uppercase-literal-suffix",
+ "-hicpp-use-auto",
+ "-llvm-namespace-comment",
+ "-misc-non-private-member-variables-in-classes",
+ "-misc-unused-parameters",
+ "-modernize-*",
+ "-readability-*",
+ }, ",")
+
+ resourceDir, err := getClangResourceDir(env, clangCmd.path)
+ if err != nil {
+ return err
+ }
+
+ clangTidyPath := filepath.Join(filepath.Dir(clangCmd.path), "clang-tidy")
+ clangTidyCmd := &command{
+ path: clangTidyPath,
+ args: append([]string{
+ "-checks=" + defaultTidyChecks,
+ cSrcFile,
+ "--",
+ "-resource-dir=" + resourceDir,
+ }, clangCmd.args...),
+ envUpdates: clangCmd.envUpdates,
+ }
+
+ if err := env.run(clangTidyCmd, env.stdout(), env.stderr()); err != nil {
+ if _, ok := getExitCode(err); ok {
+ // Note: We continue on purpose when clang-tidy fails
+ // to maintain compatibility with the previous wrapper.
+ fmt.Fprintf(env.stderr(), "clang-tidy failed")
+ } else {
+ return wrapErrorwithSourceLocf(err, "failed to call clang tidy. Command: %#v",
+ clangTidyCmd)
+ }
+ }
+
+ return nil
+}
+
+func hasAtLeastOneSuffix(s string, suffixes []string) bool {
+ for _, suffix := range suffixes {
+ if strings.HasSuffix(s, suffix) {
+ return true
+ }
+ }
+ return false
+}