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