Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 1 | // Copyright 2016 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package config |
| 16 | |
| 17 | import ( |
Dan Willemsen | 318af8b | 2016-11-02 14:50:21 -0700 | [diff] [blame] | 18 | "android/soong/android" |
Dan Willemsen | 54daaf0 | 2018-03-12 13:24:09 -0700 | [diff] [blame] | 19 | "strings" |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 20 | ) |
| 21 | |
| 22 | func init() { |
Chih-Hung Hsieh | 34850d3 | 2021-01-14 16:51:49 -0800 | [diff] [blame^] | 23 | // Many clang-tidy checks like altera-*, llvm-*, modernize-* |
| 24 | // are not designed for Android source code or creating too |
| 25 | // many (false-positive) warnings. The global default tidy checks |
| 26 | // should include only tested groups and exclude known noisy checks. |
| 27 | // See https://clang.llvm.org/extra/clang-tidy/checks/list.html |
Dan Willemsen | 54daaf0 | 2018-03-12 13:24:09 -0700 | [diff] [blame] | 28 | pctx.VariableFunc("TidyDefaultGlobalChecks", func(ctx android.PackageVarContext) string { |
| 29 | if override := ctx.Config().Getenv("DEFAULT_GLOBAL_TIDY_CHECKS"); override != "" { |
| 30 | return override |
Dan Willemsen | 318af8b | 2016-11-02 14:50:21 -0700 | [diff] [blame] | 31 | } |
| 32 | return strings.Join([]string{ |
Chris Li | 46cad06 | 2021-01-14 19:48:49 +0000 | [diff] [blame] | 33 | "-*", |
Chih-Hung Hsieh | 34850d3 | 2021-01-14 16:51:49 -0800 | [diff] [blame^] | 34 | "abseil-*", |
| 35 | "android-*", |
| 36 | "bugprone-*", |
| 37 | "cert-*", |
| 38 | "clang-analyzer-*", |
Chris Li | 46cad06 | 2021-01-14 19:48:49 +0000 | [diff] [blame] | 39 | "clang-diagnostic-unused-command-line-argument", |
Chih-Hung Hsieh | 34850d3 | 2021-01-14 16:51:49 -0800 | [diff] [blame^] | 40 | "google-*", |
| 41 | "misc-*", |
| 42 | "performance-*", |
| 43 | "portability-*", |
Chih-Hung Hsieh | 70b9316 | 2020-03-03 12:05:22 -0800 | [diff] [blame] | 44 | "-bugprone-narrowing-conversions", |
Chris Li | 46cad06 | 2021-01-14 19:48:49 +0000 | [diff] [blame] | 45 | "-google-readability*", |
Dan Willemsen | 318af8b | 2016-11-02 14:50:21 -0700 | [diff] [blame] | 46 | "-google-runtime-references", |
Chih-Hung Hsieh | 34850d3 | 2021-01-14 16:51:49 -0800 | [diff] [blame^] | 47 | "-misc-no-recursion", |
| 48 | "-misc-non-private-member-variables-in-classes", |
| 49 | "-misc-unused-parameters", |
| 50 | // the following groups are excluded by -* |
| 51 | // -altera-* |
| 52 | // -cppcoreguidelines-* |
| 53 | // -darwin-* |
| 54 | // -fuchsia-* |
| 55 | // -hicpp-* |
| 56 | // -llvm-* |
| 57 | // -llvmlibc-* |
| 58 | // -modernize-* |
| 59 | // -mpi-* |
| 60 | // -objc-* |
| 61 | // -readability-* |
| 62 | // -zircon-* |
Dan Willemsen | 54daaf0 | 2018-03-12 13:24:09 -0700 | [diff] [blame] | 63 | }, ",") |
Dan Willemsen | 318af8b | 2016-11-02 14:50:21 -0700 | [diff] [blame] | 64 | }) |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 65 | |
| 66 | // There are too many clang-tidy warnings in external and vendor projects. |
| 67 | // Enable only some google checks for these projects. |
Dan Willemsen | 54daaf0 | 2018-03-12 13:24:09 -0700 | [diff] [blame] | 68 | pctx.VariableFunc("TidyExternalVendorChecks", func(ctx android.PackageVarContext) string { |
| 69 | if override := ctx.Config().Getenv("DEFAULT_EXTERNAL_VENDOR_TIDY_CHECKS"); override != "" { |
| 70 | return override |
Dan Willemsen | 318af8b | 2016-11-02 14:50:21 -0700 | [diff] [blame] | 71 | } |
| 72 | return strings.Join([]string{ |
| 73 | "-*", |
Chih-Hung Hsieh | a7aa958 | 2018-08-15 11:28:38 -0700 | [diff] [blame] | 74 | "clang-diagnostic-unused-command-line-argument", |
Dan Willemsen | 318af8b | 2016-11-02 14:50:21 -0700 | [diff] [blame] | 75 | "google*", |
| 76 | "-google-build-using-namespace", |
| 77 | "-google-default-arguments", |
| 78 | "-google-explicit-constructor", |
| 79 | "-google-readability*", |
| 80 | "-google-runtime-int", |
| 81 | "-google-runtime-references", |
Dan Willemsen | 54daaf0 | 2018-03-12 13:24:09 -0700 | [diff] [blame] | 82 | }, ",") |
Dan Willemsen | 318af8b | 2016-11-02 14:50:21 -0700 | [diff] [blame] | 83 | }) |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 84 | |
| 85 | // Give warnings to header files only in selected directories. |
| 86 | // Do not give warnings to external or vendor header files, which contain too |
| 87 | // many warnings. |
Chih-Hung Hsieh | 60bd4bf | 2018-09-21 09:38:17 -0700 | [diff] [blame] | 88 | pctx.VariableFunc("TidyDefaultHeaderDirs", func(ctx android.PackageVarContext) string { |
| 89 | if override := ctx.Config().Getenv("DEFAULT_TIDY_HEADER_DIRS"); override != "" { |
| 90 | return override |
| 91 | } |
| 92 | return strings.Join([]string{ |
| 93 | "art/", |
| 94 | "bionic/", |
| 95 | "bootable/", |
| 96 | "build/", |
| 97 | "cts/", |
| 98 | "dalvik/", |
| 99 | "developers/", |
| 100 | "development/", |
| 101 | "frameworks/", |
| 102 | "libcore/", |
| 103 | "libnativehelper/", |
| 104 | "system/", |
| 105 | }, "|") |
| 106 | }) |
Chih-Hung Hsieh | 9e5d8a6 | 2018-09-21 15:12:44 -0700 | [diff] [blame] | 107 | |
| 108 | // Use WTIH_TIDY_FLAGS to pass extra global default clang-tidy flags. |
| 109 | pctx.VariableFunc("TidyWithTidyFlags", func(ctx android.PackageVarContext) string { |
| 110 | return ctx.Config().Getenv("WITH_TIDY_FLAGS") |
| 111 | }) |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | type PathBasedTidyCheck struct { |
| 115 | PathPrefix string |
| 116 | Checks string |
| 117 | } |
| 118 | |
| 119 | const tidyDefault = "${config.TidyDefaultGlobalChecks}" |
| 120 | const tidyExternalVendor = "${config.TidyExternalVendorChecks}" |
| 121 | |
| 122 | // This is a map of local path prefixes to the set of default clang-tidy checks |
| 123 | // to be used. |
| 124 | // The last matched local_path_prefix should be the most specific to be used. |
| 125 | var DefaultLocalTidyChecks = []PathBasedTidyCheck{ |
| 126 | {"external/", tidyExternalVendor}, |
| 127 | {"external/google", tidyDefault}, |
| 128 | {"external/webrtc", tidyDefault}, |
| 129 | {"frameworks/compile/mclinker/", tidyExternalVendor}, |
| 130 | {"hardware/qcom", tidyExternalVendor}, |
| 131 | {"vendor/", tidyExternalVendor}, |
| 132 | {"vendor/google", tidyDefault}, |
| 133 | {"vendor/google_devices", tidyExternalVendor}, |
| 134 | } |
| 135 | |
| 136 | var reversedDefaultLocalTidyChecks = reverseTidyChecks(DefaultLocalTidyChecks) |
| 137 | |
| 138 | func reverseTidyChecks(in []PathBasedTidyCheck) []PathBasedTidyCheck { |
| 139 | ret := make([]PathBasedTidyCheck, len(in)) |
| 140 | for i, check := range in { |
| 141 | ret[len(in)-i-1] = check |
| 142 | } |
| 143 | return ret |
| 144 | } |
| 145 | |
| 146 | func TidyChecksForDir(dir string) string { |
| 147 | for _, pathCheck := range reversedDefaultLocalTidyChecks { |
| 148 | if strings.HasPrefix(dir, pathCheck.PathPrefix) { |
| 149 | return pathCheck.Checks |
| 150 | } |
| 151 | } |
| 152 | return tidyDefault |
| 153 | } |