blob: 1f908430843eff1c93ae5f6d4d880bdcce6ac0a1 [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
22func init() {
Chih-Hung Hsieh34850d32021-01-14 16:51:49 -080023 // 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 Willemsen54daaf02018-03-12 13:24:09 -070028 pctx.VariableFunc("TidyDefaultGlobalChecks", func(ctx android.PackageVarContext) string {
29 if override := ctx.Config().Getenv("DEFAULT_GLOBAL_TIDY_CHECKS"); override != "" {
30 return override
Dan Willemsen318af8b2016-11-02 14:50:21 -070031 }
Chih-Hung Hsieh04f8d372021-01-19 18:28:18 -080032 checks := strings.Join([]string{
Chris Li46cad062021-01-14 19:48:49 +000033 "-*",
Chih-Hung Hsieh34850d32021-01-14 16:51:49 -080034 "android-*",
35 "bugprone-*",
36 "cert-*",
Chris Li46cad062021-01-14 19:48:49 +000037 "clang-diagnostic-unused-command-line-argument",
Chih-Hung Hsieh34850d32021-01-14 16:51:49 -080038 "google-*",
39 "misc-*",
40 "performance-*",
41 "portability-*",
Stephen Hines4b721452021-09-11 01:14:36 -070042 "-bugprone-easily-swappable-parameters",
Chih-Hung Hsieh70b93162020-03-03 12:05:22 -080043 "-bugprone-narrowing-conversions",
Chris Li46cad062021-01-14 19:48:49 +000044 "-google-readability*",
Dan Willemsen318af8b2016-11-02 14:50:21 -070045 "-google-runtime-references",
Chih-Hung Hsieh34850d32021-01-14 16:51:49 -080046 "-misc-no-recursion",
47 "-misc-non-private-member-variables-in-classes",
48 "-misc-unused-parameters",
Chih-Hung Hsieh5d46cd32022-05-09 16:01:10 -070049 "-performance-no-int-to-ptr",
Chih-Hung Hsieh34850d32021-01-14 16:51:49 -080050 // the following groups are excluded by -*
51 // -altera-*
52 // -cppcoreguidelines-*
53 // -darwin-*
54 // -fuchsia-*
55 // -hicpp-*
56 // -llvm-*
57 // -llvmlibc-*
58 // -modernize-*
59 // -mpi-*
60 // -objc-*
61 // -readability-*
62 // -zircon-*
Dan Willemsen54daaf02018-03-12 13:24:09 -070063 }, ",")
Chih-Hung Hsieh04f8d372021-01-19 18:28:18 -080064 // clang-analyzer-* checks are too slow to be in the default for WITH_TIDY=1.
65 // nightly builds add CLANG_ANALYZER_CHECKS=1 to run those checks.
Chih-Hung Hsieh9bcce2e2022-02-07 16:44:13 -080066 // The insecureAPI.DeprecatedOrUnsafeBufferHandling warning does not apply to Android.
Chih-Hung Hsieh04f8d372021-01-19 18:28:18 -080067 if ctx.Config().IsEnvTrue("CLANG_ANALYZER_CHECKS") {
Chih-Hung Hsieh9bcce2e2022-02-07 16:44:13 -080068 checks += ",clang-analyzer-*,-clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling"
Chih-Hung Hsieh04f8d372021-01-19 18:28:18 -080069 }
70 return checks
Dan Willemsen318af8b2016-11-02 14:50:21 -070071 })
Dan Willemsena03cf6d2016-09-26 15:45:04 -070072
73 // There are too many clang-tidy warnings in external and vendor projects.
74 // Enable only some google checks for these projects.
Dan Willemsen54daaf02018-03-12 13:24:09 -070075 pctx.VariableFunc("TidyExternalVendorChecks", func(ctx android.PackageVarContext) string {
76 if override := ctx.Config().Getenv("DEFAULT_EXTERNAL_VENDOR_TIDY_CHECKS"); override != "" {
77 return override
Dan Willemsen318af8b2016-11-02 14:50:21 -070078 }
79 return strings.Join([]string{
80 "-*",
Chih-Hung Hsieha7aa9582018-08-15 11:28:38 -070081 "clang-diagnostic-unused-command-line-argument",
Dan Willemsen318af8b2016-11-02 14:50:21 -070082 "google*",
83 "-google-build-using-namespace",
84 "-google-default-arguments",
85 "-google-explicit-constructor",
86 "-google-readability*",
87 "-google-runtime-int",
88 "-google-runtime-references",
Dan Willemsen54daaf02018-03-12 13:24:09 -070089 }, ",")
Dan Willemsen318af8b2016-11-02 14:50:21 -070090 })
Dan Willemsena03cf6d2016-09-26 15:45:04 -070091
Chih-Hung Hsieh9f94c362021-02-10 21:56:03 -080092 // To reduce duplicate warnings from the same header files,
93 // header-filter will contain only the module directory and
94 // those specified by DEFAULT_TIDY_HEADER_DIRS.
Chih-Hung Hsieh60bd4bf2018-09-21 09:38:17 -070095 pctx.VariableFunc("TidyDefaultHeaderDirs", func(ctx android.PackageVarContext) string {
Chih-Hung Hsieh9f94c362021-02-10 21:56:03 -080096 return ctx.Config().Getenv("DEFAULT_TIDY_HEADER_DIRS")
Chih-Hung Hsieh60bd4bf2018-09-21 09:38:17 -070097 })
Chih-Hung Hsieh9e5d8a62018-09-21 15:12:44 -070098
99 // Use WTIH_TIDY_FLAGS to pass extra global default clang-tidy flags.
100 pctx.VariableFunc("TidyWithTidyFlags", func(ctx android.PackageVarContext) string {
101 return ctx.Config().Getenv("WITH_TIDY_FLAGS")
102 })
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700103}
104
105type PathBasedTidyCheck struct {
106 PathPrefix string
107 Checks string
108}
109
110const tidyDefault = "${config.TidyDefaultGlobalChecks}"
111const tidyExternalVendor = "${config.TidyExternalVendorChecks}"
Chih-Hung Hsieh062e9342021-08-30 13:32:29 -0700112const tidyDefaultNoAnalyzer = "${config.TidyDefaultGlobalChecks},-clang-analyzer-*"
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700113
114// This is a map of local path prefixes to the set of default clang-tidy checks
115// to be used.
116// The last matched local_path_prefix should be the most specific to be used.
117var DefaultLocalTidyChecks = []PathBasedTidyCheck{
118 {"external/", tidyExternalVendor},
119 {"external/google", tidyDefault},
120 {"external/webrtc", tidyDefault},
Chih-Hung Hsiehf92c7152021-09-05 19:53:15 -0700121 {"external/googletest/", tidyExternalVendor},
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700122 {"frameworks/compile/mclinker/", tidyExternalVendor},
123 {"hardware/qcom", tidyExternalVendor},
124 {"vendor/", tidyExternalVendor},
125 {"vendor/google", tidyDefault},
Chih-Hung Hsieh47e35bb2022-05-05 14:09:12 -0700126 {"vendor/google_arc/libs/org.chromium.arc.mojom", tidyExternalVendor},
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700127 {"vendor/google_devices", tidyExternalVendor},
128}
129
130var reversedDefaultLocalTidyChecks = reverseTidyChecks(DefaultLocalTidyChecks)
131
132func reverseTidyChecks(in []PathBasedTidyCheck) []PathBasedTidyCheck {
133 ret := make([]PathBasedTidyCheck, len(in))
134 for i, check := range in {
135 ret[len(in)-i-1] = check
136 }
137 return ret
138}
139
140func TidyChecksForDir(dir string) string {
Chih-Hung Hsiehf92c7152021-09-05 19:53:15 -0700141 dir = dir + "/"
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700142 for _, pathCheck := range reversedDefaultLocalTidyChecks {
143 if strings.HasPrefix(dir, pathCheck.PathPrefix) {
144 return pathCheck.Checks
145 }
146 }
147 return tidyDefault
148}
Chih-Hung Hsieh062e9342021-08-30 13:32:29 -0700149
150func TidyFlagsForSrcFile(srcFile android.Path, flags string) string {
151 // Disable clang-analyzer-* checks globally for generated source files
152 // because some of them are too huge. Local .bp files can add wanted
153 // clang-analyzer checks through the tidy_checks property.
154 // Need to do this patch per source file, because some modules
155 // have both generated and organic source files.
156 if _, ok := srcFile.(android.WritablePath); ok {
157 if strings.Contains(flags, tidyDefault) {
158 return strings.ReplaceAll(flags, tidyDefault, tidyDefaultNoAnalyzer)
159 }
160 }
161 return flags
162}