blob: af49e88d11b387441ac9cfd5fc4916f544bfcbc9 [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",
Chih-Hung Hsieh794b81d2022-06-11 18:10:58 -070043 }
44
45 // Some clang-tidy checks are included in some tidy_checks_as_errors lists,
46 // but not all warnings are fixed/suppressed yet. These checks are not
47 // disabled in the TidyGlobalNoChecks list, so we can see them and fix/suppress them.
48 globalNoErrorCheckList = []string{
Chih-Hung Hsieh3b93ac62022-08-08 22:40:50 -070049 // http://b/241997913
50 "-bugprone-assignment-in-if-condition",
Chih-Hung Hsieh794b81d2022-06-11 18:10:58 -070051 // http://b/155034972
52 "-bugprone-branch-clone",
Chih-Hung Hsieh3b93ac62022-08-08 22:40:50 -070053 // http://b/155034563
54 "-bugprone-signed-char-misuse",
55 // http://b/241819232
56 "-misc-const-correctness",
Chih-Hung Hsieh794b81d2022-06-11 18:10:58 -070057 }
58)
59
Dan Willemsena03cf6d2016-09-26 15:45:04 -070060func init() {
Chih-Hung Hsieh34850d32021-01-14 16:51:49 -080061 // Many clang-tidy checks like altera-*, llvm-*, modernize-*
62 // are not designed for Android source code or creating too
63 // many (false-positive) warnings. The global default tidy checks
64 // should include only tested groups and exclude known noisy checks.
65 // See https://clang.llvm.org/extra/clang-tidy/checks/list.html
Dan Willemsen54daaf02018-03-12 13:24:09 -070066 pctx.VariableFunc("TidyDefaultGlobalChecks", func(ctx android.PackageVarContext) string {
67 if override := ctx.Config().Getenv("DEFAULT_GLOBAL_TIDY_CHECKS"); override != "" {
68 return override
Dan Willemsen318af8b2016-11-02 14:50:21 -070069 }
Chih-Hung Hsieh04f8d372021-01-19 18:28:18 -080070 checks := strings.Join([]string{
Chris Li46cad062021-01-14 19:48:49 +000071 "-*",
Chih-Hung Hsieh34850d32021-01-14 16:51:49 -080072 "android-*",
73 "bugprone-*",
74 "cert-*",
Chris Li46cad062021-01-14 19:48:49 +000075 "clang-diagnostic-unused-command-line-argument",
Chih-Hung Hsieh82126212022-05-17 15:26:34 -070076 // Select only google-* checks that do not have thousands of warnings.
77 // Add more such checks when we clean up source code.
78 // "google-build-using-namespace",
79 // "google-default-arguments",
80 // "google-explicit-constructor",
81 // "google-global-names-in-headers",
82 // "google-runtime-int",
83 "google-build-explicit-make-pair",
84 "google-build-namespaces",
85 "google-runtime-operator",
86 "google-upgrade-*",
Chih-Hung Hsieh34850d32021-01-14 16:51:49 -080087 "misc-*",
88 "performance-*",
89 "portability-*",
Chih-Hung Hsieh3b93ac62022-08-08 22:40:50 -070090 "-bugprone-assignment-in-if-condition",
Stephen Hines4b721452021-09-11 01:14:36 -070091 "-bugprone-easily-swappable-parameters",
Chih-Hung Hsieh70b93162020-03-03 12:05:22 -080092 "-bugprone-narrowing-conversions",
Chih-Hung Hsieh3b93ac62022-08-08 22:40:50 -070093 "-misc-const-correctness",
Chih-Hung Hsieh34850d32021-01-14 16:51:49 -080094 "-misc-no-recursion",
95 "-misc-non-private-member-variables-in-classes",
96 "-misc-unused-parameters",
Chih-Hung Hsieh5d46cd32022-05-09 16:01:10 -070097 "-performance-no-int-to-ptr",
Chih-Hung Hsieh34850d32021-01-14 16:51:49 -080098 // the following groups are excluded by -*
99 // -altera-*
100 // -cppcoreguidelines-*
101 // -darwin-*
102 // -fuchsia-*
103 // -hicpp-*
104 // -llvm-*
105 // -llvmlibc-*
106 // -modernize-*
107 // -mpi-*
108 // -objc-*
109 // -readability-*
110 // -zircon-*
Dan Willemsen54daaf02018-03-12 13:24:09 -0700111 }, ",")
Chih-Hung Hsieh04f8d372021-01-19 18:28:18 -0800112 // clang-analyzer-* checks are too slow to be in the default for WITH_TIDY=1.
113 // nightly builds add CLANG_ANALYZER_CHECKS=1 to run those checks.
Chih-Hung Hsieh9bcce2e2022-02-07 16:44:13 -0800114 // The insecureAPI.DeprecatedOrUnsafeBufferHandling warning does not apply to Android.
Chih-Hung Hsieh04f8d372021-01-19 18:28:18 -0800115 if ctx.Config().IsEnvTrue("CLANG_ANALYZER_CHECKS") {
Chih-Hung Hsieh9bcce2e2022-02-07 16:44:13 -0800116 checks += ",clang-analyzer-*,-clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling"
Chih-Hung Hsieh04f8d372021-01-19 18:28:18 -0800117 }
118 return checks
Dan Willemsen318af8b2016-11-02 14:50:21 -0700119 })
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700120
121 // There are too many clang-tidy warnings in external and vendor projects.
122 // Enable only some google checks for these projects.
Dan Willemsen54daaf02018-03-12 13:24:09 -0700123 pctx.VariableFunc("TidyExternalVendorChecks", func(ctx android.PackageVarContext) string {
124 if override := ctx.Config().Getenv("DEFAULT_EXTERNAL_VENDOR_TIDY_CHECKS"); override != "" {
125 return override
Dan Willemsen318af8b2016-11-02 14:50:21 -0700126 }
127 return strings.Join([]string{
128 "-*",
Chih-Hung Hsieha7aa9582018-08-15 11:28:38 -0700129 "clang-diagnostic-unused-command-line-argument",
Chih-Hung Hsieh82126212022-05-17 15:26:34 -0700130 "google-build-explicit-make-pair",
131 "google-build-namespaces",
132 "google-runtime-operator",
133 "google-upgrade-*",
Dan Willemsen54daaf02018-03-12 13:24:09 -0700134 }, ",")
Dan Willemsen318af8b2016-11-02 14:50:21 -0700135 })
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700136
Chih-Hung Hsieh43b920e2022-06-09 17:58:41 -0700137 pctx.VariableFunc("TidyGlobalNoChecks", func(ctx android.PackageVarContext) string {
Chih-Hung Hsieh794b81d2022-06-11 18:10:58 -0700138 return strings.Join(globalNoCheckList, ",")
139 })
140
141 pctx.VariableFunc("TidyGlobalNoErrorChecks", func(ctx android.PackageVarContext) string {
142 return strings.Join(globalNoErrorCheckList, ",")
Chih-Hung Hsieh43b920e2022-06-09 17:58:41 -0700143 })
144
Chih-Hung Hsieh9f94c362021-02-10 21:56:03 -0800145 // To reduce duplicate warnings from the same header files,
146 // header-filter will contain only the module directory and
147 // those specified by DEFAULT_TIDY_HEADER_DIRS.
Chih-Hung Hsieh60bd4bf2018-09-21 09:38:17 -0700148 pctx.VariableFunc("TidyDefaultHeaderDirs", func(ctx android.PackageVarContext) string {
Chih-Hung Hsieh9f94c362021-02-10 21:56:03 -0800149 return ctx.Config().Getenv("DEFAULT_TIDY_HEADER_DIRS")
Chih-Hung Hsieh60bd4bf2018-09-21 09:38:17 -0700150 })
Chih-Hung Hsieh9e5d8a62018-09-21 15:12:44 -0700151
152 // Use WTIH_TIDY_FLAGS to pass extra global default clang-tidy flags.
153 pctx.VariableFunc("TidyWithTidyFlags", func(ctx android.PackageVarContext) string {
154 return ctx.Config().Getenv("WITH_TIDY_FLAGS")
155 })
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700156}
157
158type PathBasedTidyCheck struct {
159 PathPrefix string
160 Checks string
161}
162
163const tidyDefault = "${config.TidyDefaultGlobalChecks}"
164const tidyExternalVendor = "${config.TidyExternalVendorChecks}"
Chih-Hung Hsieh062e9342021-08-30 13:32:29 -0700165const tidyDefaultNoAnalyzer = "${config.TidyDefaultGlobalChecks},-clang-analyzer-*"
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700166
167// This is a map of local path prefixes to the set of default clang-tidy checks
Chih-Hung Hsieh1a467532022-09-01 17:14:22 -0700168// to be used. This is like android.IsThirdPartyPath, but with more patterns.
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700169// The last matched local_path_prefix should be the most specific to be used.
170var DefaultLocalTidyChecks = []PathBasedTidyCheck{
171 {"external/", tidyExternalVendor},
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700172 {"frameworks/compile/mclinker/", tidyExternalVendor},
Chih-Hung Hsieh1a467532022-09-01 17:14:22 -0700173 {"hardware/", tidyExternalVendor},
174 {"hardware/google/", tidyDefault},
175 {"hardware/interfaces/", tidyDefault},
176 {"hardware/ril/", tidyDefault},
177 {"hardware/libhardware", tidyDefault}, // all 'hardware/libhardware*'
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700178 {"vendor/", tidyExternalVendor},
Chih-Hung Hsieh1a467532022-09-01 17:14:22 -0700179 {"vendor/google", tidyDefault}, // all 'vendor/google*'
180 {"vendor/google/external/", tidyExternalVendor},
Chih-Hung Hsieh47e35bb2022-05-05 14:09:12 -0700181 {"vendor/google_arc/libs/org.chromium.arc.mojom", tidyExternalVendor},
Chih-Hung Hsieh1a467532022-09-01 17:14:22 -0700182 {"vendor/google_devices/", tidyExternalVendor}, // many have vendor code
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700183}
184
185var reversedDefaultLocalTidyChecks = reverseTidyChecks(DefaultLocalTidyChecks)
186
187func reverseTidyChecks(in []PathBasedTidyCheck) []PathBasedTidyCheck {
188 ret := make([]PathBasedTidyCheck, len(in))
189 for i, check := range in {
190 ret[len(in)-i-1] = check
191 }
192 return ret
193}
194
195func TidyChecksForDir(dir string) string {
Chih-Hung Hsiehf92c7152021-09-05 19:53:15 -0700196 dir = dir + "/"
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700197 for _, pathCheck := range reversedDefaultLocalTidyChecks {
198 if strings.HasPrefix(dir, pathCheck.PathPrefix) {
199 return pathCheck.Checks
200 }
201 }
202 return tidyDefault
203}
Chih-Hung Hsieh062e9342021-08-30 13:32:29 -0700204
Chih-Hung Hsieh1a467532022-09-01 17:14:22 -0700205func NoClangTidyForDir(dir string) bool {
206 // This function depends on TidyChecksForDir, which selects tidyExternalVendor
207 // checks for external/vendor projects. For those projects we disable clang-tidy
208 // by default, unless some modules enable clang-tidy with tidy:true.
209 return TidyChecksForDir(dir) == tidyExternalVendor
210}
211
Chih-Hung Hsieh43b920e2022-06-09 17:58:41 -0700212// Returns a globally disabled tidy checks, overriding locally selected checks.
213func TidyGlobalNoChecks() string {
Chih-Hung Hsieh794b81d2022-06-11 18:10:58 -0700214 if len(globalNoCheckList) > 0 {
215 return ",${config.TidyGlobalNoChecks}"
216 }
217 return ""
218}
219
220// Returns a globally allowed/no-error tidy checks, appended to -warnings-as-errors.
221func TidyGlobalNoErrorChecks() string {
222 if len(globalNoErrorCheckList) > 0 {
223 return ",${config.TidyGlobalNoErrorChecks}"
224 }
225 return ""
Chih-Hung Hsieh43b920e2022-06-09 17:58:41 -0700226}
227
Chih-Hung Hsieh062e9342021-08-30 13:32:29 -0700228func TidyFlagsForSrcFile(srcFile android.Path, flags string) string {
229 // Disable clang-analyzer-* checks globally for generated source files
230 // because some of them are too huge. Local .bp files can add wanted
231 // clang-analyzer checks through the tidy_checks property.
232 // Need to do this patch per source file, because some modules
233 // have both generated and organic source files.
234 if _, ok := srcFile.(android.WritablePath); ok {
235 if strings.Contains(flags, tidyDefault) {
236 return strings.ReplaceAll(flags, tidyDefault, tidyDefaultNoAnalyzer)
237 }
238 }
239 return flags
240}
Chih-Hung Hsieha4724a02022-09-23 16:48:13 -0700241
242var (
243 removedCFlags = regexp.MustCompile(" -fsanitize=[^ ]*memtag-[^ ]* ")
244)
245
246func TidyReduceCFlags(flags string) string {
247 return removedCFlags.ReplaceAllString(flags, " ")
248}