blob: 23bda664cefdeaedfdfc7c53d3fcb204ebbcf0ec [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"
Dan Willemsen54daaf02018-03-12 13:24:09 -070019 "strings"
Dan Willemsena03cf6d2016-09-26 15:45:04 -070020)
21
Chih-Hung Hsieh794b81d2022-06-11 18:10:58 -070022var (
Chih-Hung Hsieh9f876e92022-06-12 20:28:00 -070023 // Some clang-tidy checks have bugs or don't work for Android.
Chih-Hung Hsieh794b81d2022-06-11 18:10:58 -070024 // 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",
Chih-Hung Hsieh3b93ac62022-08-08 22:40:50 -070040 // http://b/241125373
41 "-bugprone-unchecked-optional-access",
Chih-Hung Hsieh794b81d2022-06-11 18:10:58 -070042 }
43
44 // Some clang-tidy checks are included in some tidy_checks_as_errors lists,
45 // but not all warnings are fixed/suppressed yet. These checks are not
46 // disabled in the TidyGlobalNoChecks list, so we can see them and fix/suppress them.
47 globalNoErrorCheckList = []string{
Chih-Hung Hsieh3b93ac62022-08-08 22:40:50 -070048 // http://b/241997913
49 "-bugprone-assignment-in-if-condition",
Chih-Hung Hsieh794b81d2022-06-11 18:10:58 -070050 // http://b/155034972
51 "-bugprone-branch-clone",
Chih-Hung Hsieh3b93ac62022-08-08 22:40:50 -070052 // http://b/155034563
53 "-bugprone-signed-char-misuse",
54 // http://b/241819232
55 "-misc-const-correctness",
Chih-Hung Hsieh794b81d2022-06-11 18:10:58 -070056 }
57)
58
Dan Willemsena03cf6d2016-09-26 15:45:04 -070059func init() {
Chih-Hung Hsieh34850d32021-01-14 16:51:49 -080060 // Many clang-tidy checks like altera-*, llvm-*, modernize-*
61 // are not designed for Android source code or creating too
62 // many (false-positive) warnings. The global default tidy checks
63 // should include only tested groups and exclude known noisy checks.
64 // See https://clang.llvm.org/extra/clang-tidy/checks/list.html
Dan Willemsen54daaf02018-03-12 13:24:09 -070065 pctx.VariableFunc("TidyDefaultGlobalChecks", func(ctx android.PackageVarContext) string {
66 if override := ctx.Config().Getenv("DEFAULT_GLOBAL_TIDY_CHECKS"); override != "" {
67 return override
Dan Willemsen318af8b2016-11-02 14:50:21 -070068 }
Chih-Hung Hsieh04f8d372021-01-19 18:28:18 -080069 checks := strings.Join([]string{
Chris Li46cad062021-01-14 19:48:49 +000070 "-*",
Chih-Hung Hsieh34850d32021-01-14 16:51:49 -080071 "android-*",
72 "bugprone-*",
73 "cert-*",
Chris Li46cad062021-01-14 19:48:49 +000074 "clang-diagnostic-unused-command-line-argument",
Chih-Hung Hsieh82126212022-05-17 15:26:34 -070075 // Select only google-* checks that do not have thousands of warnings.
76 // Add more such checks when we clean up source code.
77 // "google-build-using-namespace",
78 // "google-default-arguments",
79 // "google-explicit-constructor",
80 // "google-global-names-in-headers",
81 // "google-runtime-int",
82 "google-build-explicit-make-pair",
83 "google-build-namespaces",
84 "google-runtime-operator",
85 "google-upgrade-*",
Chih-Hung Hsieh34850d32021-01-14 16:51:49 -080086 "misc-*",
87 "performance-*",
88 "portability-*",
Chih-Hung Hsieh3b93ac62022-08-08 22:40:50 -070089 "-bugprone-assignment-in-if-condition",
Stephen Hines4b721452021-09-11 01:14:36 -070090 "-bugprone-easily-swappable-parameters",
Chih-Hung Hsieh70b93162020-03-03 12:05:22 -080091 "-bugprone-narrowing-conversions",
Chih-Hung Hsieh3b93ac62022-08-08 22:40:50 -070092 "-misc-const-correctness",
Chih-Hung Hsieh34850d32021-01-14 16:51:49 -080093 "-misc-no-recursion",
94 "-misc-non-private-member-variables-in-classes",
95 "-misc-unused-parameters",
Chih-Hung Hsieh5d46cd32022-05-09 16:01:10 -070096 "-performance-no-int-to-ptr",
Chih-Hung Hsieh34850d32021-01-14 16:51:49 -080097 // the following groups are excluded by -*
98 // -altera-*
99 // -cppcoreguidelines-*
100 // -darwin-*
101 // -fuchsia-*
102 // -hicpp-*
103 // -llvm-*
104 // -llvmlibc-*
105 // -modernize-*
106 // -mpi-*
107 // -objc-*
108 // -readability-*
109 // -zircon-*
Dan Willemsen54daaf02018-03-12 13:24:09 -0700110 }, ",")
Chih-Hung Hsieh04f8d372021-01-19 18:28:18 -0800111 // clang-analyzer-* checks are too slow to be in the default for WITH_TIDY=1.
112 // nightly builds add CLANG_ANALYZER_CHECKS=1 to run those checks.
Chih-Hung Hsieh9bcce2e2022-02-07 16:44:13 -0800113 // The insecureAPI.DeprecatedOrUnsafeBufferHandling warning does not apply to Android.
Chih-Hung Hsieh04f8d372021-01-19 18:28:18 -0800114 if ctx.Config().IsEnvTrue("CLANG_ANALYZER_CHECKS") {
Chih-Hung Hsieh9bcce2e2022-02-07 16:44:13 -0800115 checks += ",clang-analyzer-*,-clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling"
Chih-Hung Hsieh04f8d372021-01-19 18:28:18 -0800116 }
117 return checks
Dan Willemsen318af8b2016-11-02 14:50:21 -0700118 })
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700119
120 // There are too many clang-tidy warnings in external and vendor projects.
121 // Enable only some google checks for these projects.
Dan Willemsen54daaf02018-03-12 13:24:09 -0700122 pctx.VariableFunc("TidyExternalVendorChecks", func(ctx android.PackageVarContext) string {
123 if override := ctx.Config().Getenv("DEFAULT_EXTERNAL_VENDOR_TIDY_CHECKS"); override != "" {
124 return override
Dan Willemsen318af8b2016-11-02 14:50:21 -0700125 }
126 return strings.Join([]string{
127 "-*",
Chih-Hung Hsieha7aa9582018-08-15 11:28:38 -0700128 "clang-diagnostic-unused-command-line-argument",
Chih-Hung Hsieh82126212022-05-17 15:26:34 -0700129 "google-build-explicit-make-pair",
130 "google-build-namespaces",
131 "google-runtime-operator",
132 "google-upgrade-*",
Dan Willemsen54daaf02018-03-12 13:24:09 -0700133 }, ",")
Dan Willemsen318af8b2016-11-02 14:50:21 -0700134 })
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700135
Chih-Hung Hsieh43b920e2022-06-09 17:58:41 -0700136 pctx.VariableFunc("TidyGlobalNoChecks", func(ctx android.PackageVarContext) string {
Chih-Hung Hsieh794b81d2022-06-11 18:10:58 -0700137 return strings.Join(globalNoCheckList, ",")
138 })
139
140 pctx.VariableFunc("TidyGlobalNoErrorChecks", func(ctx android.PackageVarContext) string {
141 return strings.Join(globalNoErrorCheckList, ",")
Chih-Hung Hsieh43b920e2022-06-09 17:58:41 -0700142 })
143
Chih-Hung Hsieh9f94c362021-02-10 21:56:03 -0800144 // To reduce duplicate warnings from the same header files,
145 // header-filter will contain only the module directory and
146 // those specified by DEFAULT_TIDY_HEADER_DIRS.
Chih-Hung Hsieh60bd4bf2018-09-21 09:38:17 -0700147 pctx.VariableFunc("TidyDefaultHeaderDirs", func(ctx android.PackageVarContext) string {
Chih-Hung Hsieh9f94c362021-02-10 21:56:03 -0800148 return ctx.Config().Getenv("DEFAULT_TIDY_HEADER_DIRS")
Chih-Hung Hsieh60bd4bf2018-09-21 09:38:17 -0700149 })
Chih-Hung Hsieh9e5d8a62018-09-21 15:12:44 -0700150
151 // Use WTIH_TIDY_FLAGS to pass extra global default clang-tidy flags.
152 pctx.VariableFunc("TidyWithTidyFlags", func(ctx android.PackageVarContext) string {
153 return ctx.Config().Getenv("WITH_TIDY_FLAGS")
154 })
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700155}
156
157type PathBasedTidyCheck struct {
158 PathPrefix string
159 Checks string
160}
161
162const tidyDefault = "${config.TidyDefaultGlobalChecks}"
163const tidyExternalVendor = "${config.TidyExternalVendorChecks}"
Chih-Hung Hsieh062e9342021-08-30 13:32:29 -0700164const tidyDefaultNoAnalyzer = "${config.TidyDefaultGlobalChecks},-clang-analyzer-*"
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700165
166// This is a map of local path prefixes to the set of default clang-tidy checks
Chih-Hung Hsieh1a467532022-09-01 17:14:22 -0700167// to be used. This is like android.IsThirdPartyPath, but with more patterns.
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700168// The last matched local_path_prefix should be the most specific to be used.
169var DefaultLocalTidyChecks = []PathBasedTidyCheck{
170 {"external/", tidyExternalVendor},
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700171 {"frameworks/compile/mclinker/", tidyExternalVendor},
Chih-Hung Hsieh1a467532022-09-01 17:14:22 -0700172 {"hardware/", tidyExternalVendor},
173 {"hardware/google/", tidyDefault},
174 {"hardware/interfaces/", tidyDefault},
175 {"hardware/ril/", tidyDefault},
176 {"hardware/libhardware", tidyDefault}, // all 'hardware/libhardware*'
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700177 {"vendor/", tidyExternalVendor},
Chih-Hung Hsieh1a467532022-09-01 17:14:22 -0700178 {"vendor/google", tidyDefault}, // all 'vendor/google*'
179 {"vendor/google/external/", tidyExternalVendor},
Chih-Hung Hsieh47e35bb2022-05-05 14:09:12 -0700180 {"vendor/google_arc/libs/org.chromium.arc.mojom", tidyExternalVendor},
Chih-Hung Hsieh1a467532022-09-01 17:14:22 -0700181 {"vendor/google_devices/", tidyExternalVendor}, // many have vendor code
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700182}
183
184var reversedDefaultLocalTidyChecks = reverseTidyChecks(DefaultLocalTidyChecks)
185
186func reverseTidyChecks(in []PathBasedTidyCheck) []PathBasedTidyCheck {
187 ret := make([]PathBasedTidyCheck, len(in))
188 for i, check := range in {
189 ret[len(in)-i-1] = check
190 }
191 return ret
192}
193
194func TidyChecksForDir(dir string) string {
Chih-Hung Hsiehf92c7152021-09-05 19:53:15 -0700195 dir = dir + "/"
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700196 for _, pathCheck := range reversedDefaultLocalTidyChecks {
197 if strings.HasPrefix(dir, pathCheck.PathPrefix) {
198 return pathCheck.Checks
199 }
200 }
201 return tidyDefault
202}
Chih-Hung Hsieh062e9342021-08-30 13:32:29 -0700203
Chih-Hung Hsieh1a467532022-09-01 17:14:22 -0700204func NoClangTidyForDir(dir string) bool {
205 // This function depends on TidyChecksForDir, which selects tidyExternalVendor
206 // checks for external/vendor projects. For those projects we disable clang-tidy
207 // by default, unless some modules enable clang-tidy with tidy:true.
208 return TidyChecksForDir(dir) == tidyExternalVendor
209}
210
Chih-Hung Hsieh43b920e2022-06-09 17:58:41 -0700211// Returns a globally disabled tidy checks, overriding locally selected checks.
212func TidyGlobalNoChecks() string {
Chih-Hung Hsieh794b81d2022-06-11 18:10:58 -0700213 if len(globalNoCheckList) > 0 {
214 return ",${config.TidyGlobalNoChecks}"
215 }
216 return ""
217}
218
219// Returns a globally allowed/no-error tidy checks, appended to -warnings-as-errors.
220func TidyGlobalNoErrorChecks() string {
221 if len(globalNoErrorCheckList) > 0 {
222 return ",${config.TidyGlobalNoErrorChecks}"
223 }
224 return ""
Chih-Hung Hsieh43b920e2022-06-09 17:58:41 -0700225}
226
Chih-Hung Hsieh062e9342021-08-30 13:32:29 -0700227func TidyFlagsForSrcFile(srcFile android.Path, flags string) string {
228 // Disable clang-analyzer-* checks globally for generated source files
229 // because some of them are too huge. Local .bp files can add wanted
230 // clang-analyzer checks through the tidy_checks property.
231 // Need to do this patch per source file, because some modules
232 // have both generated and organic source files.
233 if _, ok := srcFile.(android.WritablePath); ok {
234 if strings.Contains(flags, tidyDefault) {
235 return strings.ReplaceAll(flags, tidyDefault, tidyDefaultNoAnalyzer)
236 }
237 }
238 return flags
239}