blob: efa45495bd1ab1f888db26d595a95e1e0d92f0df [file] [log] [blame]
Dan Willemsena03cf6d2016-09-26 15:45:04 -07001// 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
15package config
16
17import (
Dan Willemsen318af8b2016-11-02 14:50:21 -070018 "android/soong/android"
Chih-Hung Hsieha4724a02022-09-23 16:48:13 -070019 "regexp"
Dan Willemsen54daaf02018-03-12 13:24:09 -070020 "strings"
Dan Willemsena03cf6d2016-09-26 15:45:04 -070021)
22
Chih-Hung Hsieh794b81d2022-06-11 18:10:58 -070023var (
Chih-Hung Hsieh9f876e92022-06-12 20:28:00 -070024 // Some clang-tidy checks have bugs or don't work for Android.
Chih-Hung Hsieh794b81d2022-06-11 18:10:58 -070025 // They are disabled here, overriding any locally selected checks.
26 globalNoCheckList = []string{
27 // https://b.corp.google.com/issues/153464409
28 // many local projects enable cert-* checks, which
29 // trigger bugprone-reserved-identifier.
30 "-bugprone-reserved-identifier*,-cert-dcl51-cpp,-cert-dcl37-c",
31 // http://b/153757728
32 "-readability-qualified-auto",
33 // http://b/193716442
34 "-bugprone-implicit-widening-of-multiplication-result",
35 // Too many existing functions trigger this rule, and fixing it requires large code
36 // refactoring. The cost of maintaining this tidy rule outweighs the benefit it brings.
37 "-bugprone-easily-swappable-parameters",
38 // http://b/216364337 - TODO: Follow-up after compiler update to
39 // disable or fix individual instances.
40 "-cert-err33-c",
Chih-Hung Hsieh3b93ac62022-08-08 22:40:50 -070041 // http://b/241125373
42 "-bugprone-unchecked-optional-access",
Yi Kongdf456b52023-01-14 02:37:21 +090043 // http://b/265438407
44 "-misc-use-anonymous-namespace",
Yabin Cui2c336fe2023-05-31 19:25:49 +000045 // http://b/285005947
46 "-performance-avoid-endl",
Chih-Hung Hsieh794b81d2022-06-11 18:10:58 -070047 }
48
49 // Some clang-tidy checks are included in some tidy_checks_as_errors lists,
50 // but not all warnings are fixed/suppressed yet. These checks are not
51 // disabled in the TidyGlobalNoChecks list, so we can see them and fix/suppress them.
52 globalNoErrorCheckList = []string{
Chih-Hung Hsieh3b93ac62022-08-08 22:40:50 -070053 // http://b/241997913
54 "-bugprone-assignment-in-if-condition",
Chih-Hung Hsieh794b81d2022-06-11 18:10:58 -070055 // http://b/155034972
56 "-bugprone-branch-clone",
Chih-Hung Hsieh3b93ac62022-08-08 22:40:50 -070057 // http://b/155034563
58 "-bugprone-signed-char-misuse",
59 // http://b/241819232
60 "-misc-const-correctness",
Yabin Cuic31c2fb2023-06-01 18:19:39 +000061 // http://b/285356805
62 "-bugprone-unsafe-functions",
63 "-cert-msc24-c",
64 "-cert-msc33-c",
65 // http://b/285356799
66 "-modernize-type-traits",
67 // http://b/285361108
68 "-readability-avoid-unconditional-preprocessor-if",
Chih-Hung Hsieh794b81d2022-06-11 18:10:58 -070069 }
Sam Delmericoee030d22022-11-10 14:33:40 -050070
71 extraArgFlags = []string{
72 // We might be using the static analyzer through clang tidy.
73 // https://bugs.llvm.org/show_bug.cgi?id=32914
74 "-D__clang_analyzer__",
75
76 // A recent change in clang-tidy (r328258) enabled destructor inlining, which
77 // appears to cause a number of false positives. Until that's resolved, this turns
78 // off the effects of r328258.
79 // https://bugs.llvm.org/show_bug.cgi?id=37459
80 "-Xclang",
81 "-analyzer-config",
82 "-Xclang",
83 "c++-temp-dtor-inlining=false",
84 }
Chih-Hung Hsieh794b81d2022-06-11 18:10:58 -070085)
86
Dan Willemsena03cf6d2016-09-26 15:45:04 -070087func init() {
Chih-Hung Hsiehd21c46a2022-10-11 12:27:17 -070088 // The global default tidy checks should include clang-tidy
89 // default checks and tested groups, but exclude known noisy checks.
Chih-Hung Hsieh34850d32021-01-14 16:51:49 -080090 // See https://clang.llvm.org/extra/clang-tidy/checks/list.html
Sam Delmericob9b6c672022-10-21 11:52:18 -040091 exportedVars.ExportVariableConfigMethod("TidyDefaultGlobalChecks", func(config android.Config) string {
92 if override := config.Getenv("DEFAULT_GLOBAL_TIDY_CHECKS"); override != "" {
Dan Willemsen54daaf02018-03-12 13:24:09 -070093 return override
Dan Willemsen318af8b2016-11-02 14:50:21 -070094 }
Chih-Hung Hsieh04f8d372021-01-19 18:28:18 -080095 checks := strings.Join([]string{
Chih-Hung Hsieh34850d32021-01-14 16:51:49 -080096 "android-*",
97 "bugprone-*",
98 "cert-*",
Chris Li46cad062021-01-14 19:48:49 +000099 "clang-diagnostic-unused-command-line-argument",
Chih-Hung Hsieh82126212022-05-17 15:26:34 -0700100 // Select only google-* checks that do not have thousands of warnings.
101 // Add more such checks when we clean up source code.
102 // "google-build-using-namespace",
103 // "google-default-arguments",
104 // "google-explicit-constructor",
105 // "google-global-names-in-headers",
106 // "google-runtime-int",
107 "google-build-explicit-make-pair",
108 "google-build-namespaces",
109 "google-runtime-operator",
110 "google-upgrade-*",
Chih-Hung Hsieh34850d32021-01-14 16:51:49 -0800111 "misc-*",
112 "performance-*",
113 "portability-*",
Chih-Hung Hsieh3b93ac62022-08-08 22:40:50 -0700114 "-bugprone-assignment-in-if-condition",
Stephen Hines4b721452021-09-11 01:14:36 -0700115 "-bugprone-easily-swappable-parameters",
Chih-Hung Hsieh70b93162020-03-03 12:05:22 -0800116 "-bugprone-narrowing-conversions",
Chih-Hung Hsieh3b93ac62022-08-08 22:40:50 -0700117 "-misc-const-correctness",
Chih-Hung Hsieh34850d32021-01-14 16:51:49 -0800118 "-misc-no-recursion",
119 "-misc-non-private-member-variables-in-classes",
120 "-misc-unused-parameters",
Chih-Hung Hsieh5d46cd32022-05-09 16:01:10 -0700121 "-performance-no-int-to-ptr",
Chih-Hung Hsiehd21c46a2022-10-11 12:27:17 -0700122 // the following groups are not in clang-tidy default checks.
Chih-Hung Hsieh34850d32021-01-14 16:51:49 -0800123 // -altera-*
124 // -cppcoreguidelines-*
125 // -darwin-*
126 // -fuchsia-*
127 // -hicpp-*
128 // -llvm-*
129 // -llvmlibc-*
130 // -modernize-*
131 // -mpi-*
132 // -objc-*
133 // -readability-*
134 // -zircon-*
Dan Willemsen54daaf02018-03-12 13:24:09 -0700135 }, ",")
Chih-Hung Hsiehd21c46a2022-10-11 12:27:17 -0700136 // clang-analyzer-* checks are slow for large files, but we have TIDY_TIMEOUT to
137 // limit clang-tidy runtime. We allow clang-tidy default clang-analyzer-* checks,
138 // and add it explicitly when CLANG_ANALYZER_CHECKS is set.
Chih-Hung Hsieh9bcce2e2022-02-07 16:44:13 -0800139 // The insecureAPI.DeprecatedOrUnsafeBufferHandling warning does not apply to Android.
Sam Delmericob9b6c672022-10-21 11:52:18 -0400140 if config.IsEnvTrue("CLANG_ANALYZER_CHECKS") {
Chih-Hung Hsieh9bcce2e2022-02-07 16:44:13 -0800141 checks += ",clang-analyzer-*,-clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling"
Chih-Hung Hsiehd21c46a2022-10-11 12:27:17 -0700142 } else {
143 checks += ",-clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling"
Chih-Hung Hsieh04f8d372021-01-19 18:28:18 -0800144 }
145 return checks
Dan Willemsen318af8b2016-11-02 14:50:21 -0700146 })
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700147
Chih-Hung Hsiehd21c46a2022-10-11 12:27:17 -0700148 // The external and vendor projects do not run clang-tidy unless TIDY_EXTERNAL_VENDOR is set.
149 // We do not add "-*" to the check list to avoid suppressing the check list in .clang-tidy config files.
150 // There are too many clang-tidy warnings in external and vendor projects, so we only
151 // enable some google checks for these projects. Users can add more checks locally with the
152 // "tidy_checks" list in .bp files, or the "Checks" list in .clang-tidy config files.
Sam Delmericob9b6c672022-10-21 11:52:18 -0400153 exportedVars.ExportVariableConfigMethod("TidyExternalVendorChecks", func(config android.Config) string {
154 if override := config.Getenv("DEFAULT_EXTERNAL_VENDOR_TIDY_CHECKS"); override != "" {
Dan Willemsen54daaf02018-03-12 13:24:09 -0700155 return override
Dan Willemsen318af8b2016-11-02 14:50:21 -0700156 }
157 return strings.Join([]string{
Chih-Hung Hsieha7aa9582018-08-15 11:28:38 -0700158 "clang-diagnostic-unused-command-line-argument",
Chih-Hung Hsieh82126212022-05-17 15:26:34 -0700159 "google-build-explicit-make-pair",
160 "google-build-namespaces",
161 "google-runtime-operator",
162 "google-upgrade-*",
Chih-Hung Hsiehd21c46a2022-10-11 12:27:17 -0700163 "-clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling",
Dan Willemsen54daaf02018-03-12 13:24:09 -0700164 }, ",")
Dan Willemsen318af8b2016-11-02 14:50:21 -0700165 })
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700166
Sam Delmericob9b6c672022-10-21 11:52:18 -0400167 exportedVars.ExportVariableFuncVariable("TidyGlobalNoChecks", func() string {
Chih-Hung Hsieh794b81d2022-06-11 18:10:58 -0700168 return strings.Join(globalNoCheckList, ",")
169 })
170
Sam Delmericob9b6c672022-10-21 11:52:18 -0400171 exportedVars.ExportVariableFuncVariable("TidyGlobalNoErrorChecks", func() string {
Chih-Hung Hsieh794b81d2022-06-11 18:10:58 -0700172 return strings.Join(globalNoErrorCheckList, ",")
Chih-Hung Hsieh43b920e2022-06-09 17:58:41 -0700173 })
174
Sam Delmericoee030d22022-11-10 14:33:40 -0500175 exportedVars.ExportStringListStaticVariable("TidyExtraArgFlags", extraArgFlags)
176
Chih-Hung Hsieh9f94c362021-02-10 21:56:03 -0800177 // To reduce duplicate warnings from the same header files,
178 // header-filter will contain only the module directory and
179 // those specified by DEFAULT_TIDY_HEADER_DIRS.
Sam Delmericob9b6c672022-10-21 11:52:18 -0400180 exportedVars.ExportVariableConfigMethod("TidyDefaultHeaderDirs", func(config android.Config) string {
181 return config.Getenv("DEFAULT_TIDY_HEADER_DIRS")
Chih-Hung Hsieh60bd4bf2018-09-21 09:38:17 -0700182 })
Chih-Hung Hsieh9e5d8a62018-09-21 15:12:44 -0700183
184 // Use WTIH_TIDY_FLAGS to pass extra global default clang-tidy flags.
Sam Delmericob9b6c672022-10-21 11:52:18 -0400185 exportedVars.ExportVariableConfigMethod("TidyWithTidyFlags", func(config android.Config) string {
186 return config.Getenv("WITH_TIDY_FLAGS")
Chih-Hung Hsieh9e5d8a62018-09-21 15:12:44 -0700187 })
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700188}
189
190type PathBasedTidyCheck struct {
191 PathPrefix string
192 Checks string
193}
194
195const tidyDefault = "${config.TidyDefaultGlobalChecks}"
196const tidyExternalVendor = "${config.TidyExternalVendorChecks}"
Chih-Hung Hsieh062e9342021-08-30 13:32:29 -0700197const tidyDefaultNoAnalyzer = "${config.TidyDefaultGlobalChecks},-clang-analyzer-*"
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700198
199// This is a map of local path prefixes to the set of default clang-tidy checks
Chih-Hung Hsieh1a467532022-09-01 17:14:22 -0700200// to be used. This is like android.IsThirdPartyPath, but with more patterns.
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700201// The last matched local_path_prefix should be the most specific to be used.
202var DefaultLocalTidyChecks = []PathBasedTidyCheck{
203 {"external/", tidyExternalVendor},
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700204 {"frameworks/compile/mclinker/", tidyExternalVendor},
Chih-Hung Hsieh1a467532022-09-01 17:14:22 -0700205 {"hardware/", tidyExternalVendor},
206 {"hardware/google/", tidyDefault},
207 {"hardware/interfaces/", tidyDefault},
208 {"hardware/ril/", tidyDefault},
209 {"hardware/libhardware", tidyDefault}, // all 'hardware/libhardware*'
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700210 {"vendor/", tidyExternalVendor},
Chih-Hung Hsieh1a467532022-09-01 17:14:22 -0700211 {"vendor/google", tidyDefault}, // all 'vendor/google*'
212 {"vendor/google/external/", tidyExternalVendor},
Chih-Hung Hsieh47e35bb2022-05-05 14:09:12 -0700213 {"vendor/google_arc/libs/org.chromium.arc.mojom", tidyExternalVendor},
Chih-Hung Hsieh1a467532022-09-01 17:14:22 -0700214 {"vendor/google_devices/", tidyExternalVendor}, // many have vendor code
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700215}
216
217var reversedDefaultLocalTidyChecks = reverseTidyChecks(DefaultLocalTidyChecks)
218
219func reverseTidyChecks(in []PathBasedTidyCheck) []PathBasedTidyCheck {
220 ret := make([]PathBasedTidyCheck, len(in))
221 for i, check := range in {
222 ret[len(in)-i-1] = check
223 }
224 return ret
225}
226
227func TidyChecksForDir(dir string) string {
Chih-Hung Hsiehf92c7152021-09-05 19:53:15 -0700228 dir = dir + "/"
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700229 for _, pathCheck := range reversedDefaultLocalTidyChecks {
230 if strings.HasPrefix(dir, pathCheck.PathPrefix) {
231 return pathCheck.Checks
232 }
233 }
234 return tidyDefault
235}
Chih-Hung Hsieh062e9342021-08-30 13:32:29 -0700236
Chih-Hung Hsiehff2efae2022-10-18 14:43:27 -0700237func neverTidyForDir(dir string) bool {
238 // This function can be extended if tidy needs to be disabled for more directories.
239 return strings.HasPrefix(dir, "external/grpc-grpc")
240}
241
242func NoClangTidyForDir(allowExternalVendor bool, dir string) bool {
243 // Tidy can be disable for a module in dir, if the dir is "neverTidyForDir",
244 // or if it belongs to external|vendor and !allowExternalVendor.
Chih-Hung Hsieh1a467532022-09-01 17:14:22 -0700245 // This function depends on TidyChecksForDir, which selects tidyExternalVendor
Chih-Hung Hsiehff2efae2022-10-18 14:43:27 -0700246 // checks for external/vendor projects.
247 return neverTidyForDir(dir) ||
248 (!allowExternalVendor && TidyChecksForDir(dir) == tidyExternalVendor)
Chih-Hung Hsieh1a467532022-09-01 17:14:22 -0700249}
250
Chih-Hung Hsieh43b920e2022-06-09 17:58:41 -0700251// Returns a globally disabled tidy checks, overriding locally selected checks.
252func TidyGlobalNoChecks() string {
Chih-Hung Hsieh794b81d2022-06-11 18:10:58 -0700253 if len(globalNoCheckList) > 0 {
254 return ",${config.TidyGlobalNoChecks}"
255 }
256 return ""
257}
258
259// Returns a globally allowed/no-error tidy checks, appended to -warnings-as-errors.
260func TidyGlobalNoErrorChecks() string {
261 if len(globalNoErrorCheckList) > 0 {
262 return ",${config.TidyGlobalNoErrorChecks}"
263 }
264 return ""
Chih-Hung Hsieh43b920e2022-06-09 17:58:41 -0700265}
266
Sam Delmericoee030d22022-11-10 14:33:40 -0500267func TidyExtraArgFlags() []string {
268 return extraArgFlags
269}
270
Chih-Hung Hsieh062e9342021-08-30 13:32:29 -0700271func TidyFlagsForSrcFile(srcFile android.Path, flags string) string {
272 // Disable clang-analyzer-* checks globally for generated source files
273 // because some of them are too huge. Local .bp files can add wanted
274 // clang-analyzer checks through the tidy_checks property.
275 // Need to do this patch per source file, because some modules
276 // have both generated and organic source files.
277 if _, ok := srcFile.(android.WritablePath); ok {
278 if strings.Contains(flags, tidyDefault) {
279 return strings.ReplaceAll(flags, tidyDefault, tidyDefaultNoAnalyzer)
280 }
281 }
282 return flags
283}
Chih-Hung Hsieha4724a02022-09-23 16:48:13 -0700284
285var (
286 removedCFlags = regexp.MustCompile(" -fsanitize=[^ ]*memtag-[^ ]* ")
287)
288
289func TidyReduceCFlags(flags string) string {
290 return removedCFlags.ReplaceAllString(flags, " ")
291}