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 | |
Chih-Hung Hsieh | 794b81d | 2022-06-11 18:10:58 -0700 | [diff] [blame^] | 22 | var ( |
| 23 | // Some clang-tidy checks have bugs or not work for Android. |
| 24 | // They are disabled here, overriding any locally selected checks. |
| 25 | globalNoCheckList = []string{ |
| 26 | // https://b.corp.google.com/issues/153464409 |
| 27 | // many local projects enable cert-* checks, which |
| 28 | // trigger bugprone-reserved-identifier. |
| 29 | "-bugprone-reserved-identifier*,-cert-dcl51-cpp,-cert-dcl37-c", |
| 30 | // http://b/153757728 |
| 31 | "-readability-qualified-auto", |
| 32 | // http://b/193716442 |
| 33 | "-bugprone-implicit-widening-of-multiplication-result", |
| 34 | // Too many existing functions trigger this rule, and fixing it requires large code |
| 35 | // refactoring. The cost of maintaining this tidy rule outweighs the benefit it brings. |
| 36 | "-bugprone-easily-swappable-parameters", |
| 37 | // http://b/216364337 - TODO: Follow-up after compiler update to |
| 38 | // disable or fix individual instances. |
| 39 | "-cert-err33-c", |
| 40 | } |
| 41 | |
| 42 | // Some clang-tidy checks are included in some tidy_checks_as_errors lists, |
| 43 | // but not all warnings are fixed/suppressed yet. These checks are not |
| 44 | // disabled in the TidyGlobalNoChecks list, so we can see them and fix/suppress them. |
| 45 | globalNoErrorCheckList = []string{ |
| 46 | // http://b/155034563 |
| 47 | "-bugprone-signed-char-misuse", |
| 48 | // http://b/155034972 |
| 49 | "-bugprone-branch-clone", |
| 50 | } |
| 51 | ) |
| 52 | |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 53 | func init() { |
Chih-Hung Hsieh | 34850d3 | 2021-01-14 16:51:49 -0800 | [diff] [blame] | 54 | // Many clang-tidy checks like altera-*, llvm-*, modernize-* |
| 55 | // are not designed for Android source code or creating too |
| 56 | // many (false-positive) warnings. The global default tidy checks |
| 57 | // should include only tested groups and exclude known noisy checks. |
| 58 | // See https://clang.llvm.org/extra/clang-tidy/checks/list.html |
Dan Willemsen | 54daaf0 | 2018-03-12 13:24:09 -0700 | [diff] [blame] | 59 | pctx.VariableFunc("TidyDefaultGlobalChecks", func(ctx android.PackageVarContext) string { |
| 60 | if override := ctx.Config().Getenv("DEFAULT_GLOBAL_TIDY_CHECKS"); override != "" { |
| 61 | return override |
Dan Willemsen | 318af8b | 2016-11-02 14:50:21 -0700 | [diff] [blame] | 62 | } |
Chih-Hung Hsieh | 04f8d37 | 2021-01-19 18:28:18 -0800 | [diff] [blame] | 63 | checks := strings.Join([]string{ |
Chris Li | 46cad06 | 2021-01-14 19:48:49 +0000 | [diff] [blame] | 64 | "-*", |
Chih-Hung Hsieh | 34850d3 | 2021-01-14 16:51:49 -0800 | [diff] [blame] | 65 | "android-*", |
| 66 | "bugprone-*", |
| 67 | "cert-*", |
Chris Li | 46cad06 | 2021-01-14 19:48:49 +0000 | [diff] [blame] | 68 | "clang-diagnostic-unused-command-line-argument", |
Chih-Hung Hsieh | 8212621 | 2022-05-17 15:26:34 -0700 | [diff] [blame] | 69 | // Select only google-* checks that do not have thousands of warnings. |
| 70 | // Add more such checks when we clean up source code. |
| 71 | // "google-build-using-namespace", |
| 72 | // "google-default-arguments", |
| 73 | // "google-explicit-constructor", |
| 74 | // "google-global-names-in-headers", |
| 75 | // "google-runtime-int", |
| 76 | "google-build-explicit-make-pair", |
| 77 | "google-build-namespaces", |
| 78 | "google-runtime-operator", |
| 79 | "google-upgrade-*", |
Chih-Hung Hsieh | 34850d3 | 2021-01-14 16:51:49 -0800 | [diff] [blame] | 80 | "misc-*", |
| 81 | "performance-*", |
| 82 | "portability-*", |
Stephen Hines | 4b72145 | 2021-09-11 01:14:36 -0700 | [diff] [blame] | 83 | "-bugprone-easily-swappable-parameters", |
Chih-Hung Hsieh | 70b9316 | 2020-03-03 12:05:22 -0800 | [diff] [blame] | 84 | "-bugprone-narrowing-conversions", |
Chih-Hung Hsieh | 34850d3 | 2021-01-14 16:51:49 -0800 | [diff] [blame] | 85 | "-misc-no-recursion", |
| 86 | "-misc-non-private-member-variables-in-classes", |
| 87 | "-misc-unused-parameters", |
Chih-Hung Hsieh | 5d46cd3 | 2022-05-09 16:01:10 -0700 | [diff] [blame] | 88 | "-performance-no-int-to-ptr", |
Chih-Hung Hsieh | 34850d3 | 2021-01-14 16:51:49 -0800 | [diff] [blame] | 89 | // the following groups are excluded by -* |
| 90 | // -altera-* |
| 91 | // -cppcoreguidelines-* |
| 92 | // -darwin-* |
| 93 | // -fuchsia-* |
| 94 | // -hicpp-* |
| 95 | // -llvm-* |
| 96 | // -llvmlibc-* |
| 97 | // -modernize-* |
| 98 | // -mpi-* |
| 99 | // -objc-* |
| 100 | // -readability-* |
| 101 | // -zircon-* |
Dan Willemsen | 54daaf0 | 2018-03-12 13:24:09 -0700 | [diff] [blame] | 102 | }, ",") |
Chih-Hung Hsieh | 04f8d37 | 2021-01-19 18:28:18 -0800 | [diff] [blame] | 103 | // clang-analyzer-* checks are too slow to be in the default for WITH_TIDY=1. |
| 104 | // nightly builds add CLANG_ANALYZER_CHECKS=1 to run those checks. |
Chih-Hung Hsieh | 9bcce2e | 2022-02-07 16:44:13 -0800 | [diff] [blame] | 105 | // The insecureAPI.DeprecatedOrUnsafeBufferHandling warning does not apply to Android. |
Chih-Hung Hsieh | 04f8d37 | 2021-01-19 18:28:18 -0800 | [diff] [blame] | 106 | if ctx.Config().IsEnvTrue("CLANG_ANALYZER_CHECKS") { |
Chih-Hung Hsieh | 9bcce2e | 2022-02-07 16:44:13 -0800 | [diff] [blame] | 107 | checks += ",clang-analyzer-*,-clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling" |
Chih-Hung Hsieh | 04f8d37 | 2021-01-19 18:28:18 -0800 | [diff] [blame] | 108 | } |
| 109 | return checks |
Dan Willemsen | 318af8b | 2016-11-02 14:50:21 -0700 | [diff] [blame] | 110 | }) |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 111 | |
| 112 | // There are too many clang-tidy warnings in external and vendor projects. |
| 113 | // Enable only some google checks for these projects. |
Dan Willemsen | 54daaf0 | 2018-03-12 13:24:09 -0700 | [diff] [blame] | 114 | pctx.VariableFunc("TidyExternalVendorChecks", func(ctx android.PackageVarContext) string { |
| 115 | if override := ctx.Config().Getenv("DEFAULT_EXTERNAL_VENDOR_TIDY_CHECKS"); override != "" { |
| 116 | return override |
Dan Willemsen | 318af8b | 2016-11-02 14:50:21 -0700 | [diff] [blame] | 117 | } |
| 118 | return strings.Join([]string{ |
| 119 | "-*", |
Chih-Hung Hsieh | a7aa958 | 2018-08-15 11:28:38 -0700 | [diff] [blame] | 120 | "clang-diagnostic-unused-command-line-argument", |
Chih-Hung Hsieh | 8212621 | 2022-05-17 15:26:34 -0700 | [diff] [blame] | 121 | "google-build-explicit-make-pair", |
| 122 | "google-build-namespaces", |
| 123 | "google-runtime-operator", |
| 124 | "google-upgrade-*", |
Dan Willemsen | 54daaf0 | 2018-03-12 13:24:09 -0700 | [diff] [blame] | 125 | }, ",") |
Dan Willemsen | 318af8b | 2016-11-02 14:50:21 -0700 | [diff] [blame] | 126 | }) |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 127 | |
Chih-Hung Hsieh | 43b920e | 2022-06-09 17:58:41 -0700 | [diff] [blame] | 128 | pctx.VariableFunc("TidyGlobalNoChecks", func(ctx android.PackageVarContext) string { |
Chih-Hung Hsieh | 794b81d | 2022-06-11 18:10:58 -0700 | [diff] [blame^] | 129 | return strings.Join(globalNoCheckList, ",") |
| 130 | }) |
| 131 | |
| 132 | pctx.VariableFunc("TidyGlobalNoErrorChecks", func(ctx android.PackageVarContext) string { |
| 133 | return strings.Join(globalNoErrorCheckList, ",") |
Chih-Hung Hsieh | 43b920e | 2022-06-09 17:58:41 -0700 | [diff] [blame] | 134 | }) |
| 135 | |
Chih-Hung Hsieh | 9f94c36 | 2021-02-10 21:56:03 -0800 | [diff] [blame] | 136 | // To reduce duplicate warnings from the same header files, |
| 137 | // header-filter will contain only the module directory and |
| 138 | // those specified by DEFAULT_TIDY_HEADER_DIRS. |
Chih-Hung Hsieh | 60bd4bf | 2018-09-21 09:38:17 -0700 | [diff] [blame] | 139 | pctx.VariableFunc("TidyDefaultHeaderDirs", func(ctx android.PackageVarContext) string { |
Chih-Hung Hsieh | 9f94c36 | 2021-02-10 21:56:03 -0800 | [diff] [blame] | 140 | return ctx.Config().Getenv("DEFAULT_TIDY_HEADER_DIRS") |
Chih-Hung Hsieh | 60bd4bf | 2018-09-21 09:38:17 -0700 | [diff] [blame] | 141 | }) |
Chih-Hung Hsieh | 9e5d8a6 | 2018-09-21 15:12:44 -0700 | [diff] [blame] | 142 | |
| 143 | // Use WTIH_TIDY_FLAGS to pass extra global default clang-tidy flags. |
| 144 | pctx.VariableFunc("TidyWithTidyFlags", func(ctx android.PackageVarContext) string { |
| 145 | return ctx.Config().Getenv("WITH_TIDY_FLAGS") |
| 146 | }) |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | type PathBasedTidyCheck struct { |
| 150 | PathPrefix string |
| 151 | Checks string |
| 152 | } |
| 153 | |
| 154 | const tidyDefault = "${config.TidyDefaultGlobalChecks}" |
| 155 | const tidyExternalVendor = "${config.TidyExternalVendorChecks}" |
Chih-Hung Hsieh | 062e934 | 2021-08-30 13:32:29 -0700 | [diff] [blame] | 156 | const tidyDefaultNoAnalyzer = "${config.TidyDefaultGlobalChecks},-clang-analyzer-*" |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 157 | |
| 158 | // This is a map of local path prefixes to the set of default clang-tidy checks |
| 159 | // to be used. |
| 160 | // The last matched local_path_prefix should be the most specific to be used. |
| 161 | var DefaultLocalTidyChecks = []PathBasedTidyCheck{ |
| 162 | {"external/", tidyExternalVendor}, |
| 163 | {"external/google", tidyDefault}, |
| 164 | {"external/webrtc", tidyDefault}, |
Chih-Hung Hsieh | f92c715 | 2021-09-05 19:53:15 -0700 | [diff] [blame] | 165 | {"external/googletest/", tidyExternalVendor}, |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 166 | {"frameworks/compile/mclinker/", tidyExternalVendor}, |
| 167 | {"hardware/qcom", tidyExternalVendor}, |
| 168 | {"vendor/", tidyExternalVendor}, |
| 169 | {"vendor/google", tidyDefault}, |
Chih-Hung Hsieh | 47e35bb | 2022-05-05 14:09:12 -0700 | [diff] [blame] | 170 | {"vendor/google_arc/libs/org.chromium.arc.mojom", tidyExternalVendor}, |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 171 | {"vendor/google_devices", tidyExternalVendor}, |
| 172 | } |
| 173 | |
| 174 | var reversedDefaultLocalTidyChecks = reverseTidyChecks(DefaultLocalTidyChecks) |
| 175 | |
| 176 | func reverseTidyChecks(in []PathBasedTidyCheck) []PathBasedTidyCheck { |
| 177 | ret := make([]PathBasedTidyCheck, len(in)) |
| 178 | for i, check := range in { |
| 179 | ret[len(in)-i-1] = check |
| 180 | } |
| 181 | return ret |
| 182 | } |
| 183 | |
| 184 | func TidyChecksForDir(dir string) string { |
Chih-Hung Hsieh | f92c715 | 2021-09-05 19:53:15 -0700 | [diff] [blame] | 185 | dir = dir + "/" |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 186 | for _, pathCheck := range reversedDefaultLocalTidyChecks { |
| 187 | if strings.HasPrefix(dir, pathCheck.PathPrefix) { |
| 188 | return pathCheck.Checks |
| 189 | } |
| 190 | } |
| 191 | return tidyDefault |
| 192 | } |
Chih-Hung Hsieh | 062e934 | 2021-08-30 13:32:29 -0700 | [diff] [blame] | 193 | |
Chih-Hung Hsieh | 43b920e | 2022-06-09 17:58:41 -0700 | [diff] [blame] | 194 | // Returns a globally disabled tidy checks, overriding locally selected checks. |
| 195 | func TidyGlobalNoChecks() string { |
Chih-Hung Hsieh | 794b81d | 2022-06-11 18:10:58 -0700 | [diff] [blame^] | 196 | if len(globalNoCheckList) > 0 { |
| 197 | return ",${config.TidyGlobalNoChecks}" |
| 198 | } |
| 199 | return "" |
| 200 | } |
| 201 | |
| 202 | // Returns a globally allowed/no-error tidy checks, appended to -warnings-as-errors. |
| 203 | func TidyGlobalNoErrorChecks() string { |
| 204 | if len(globalNoErrorCheckList) > 0 { |
| 205 | return ",${config.TidyGlobalNoErrorChecks}" |
| 206 | } |
| 207 | return "" |
Chih-Hung Hsieh | 43b920e | 2022-06-09 17:58:41 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Chih-Hung Hsieh | 062e934 | 2021-08-30 13:32:29 -0700 | [diff] [blame] | 210 | func TidyFlagsForSrcFile(srcFile android.Path, flags string) string { |
| 211 | // Disable clang-analyzer-* checks globally for generated source files |
| 212 | // because some of them are too huge. Local .bp files can add wanted |
| 213 | // clang-analyzer checks through the tidy_checks property. |
| 214 | // Need to do this patch per source file, because some modules |
| 215 | // have both generated and organic source files. |
| 216 | if _, ok := srcFile.(android.WritablePath); ok { |
| 217 | if strings.Contains(flags, tidyDefault) { |
| 218 | return strings.ReplaceAll(flags, tidyDefault, tidyDefaultNoAnalyzer) |
| 219 | } |
| 220 | } |
| 221 | return flags |
| 222 | } |