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 15package config 16 17import ( 18 "android/soong/android" 19 "regexp" 20 "strings" 21) 22 23var ( 24 // Some clang-tidy checks have bugs or don't work for Android. 25 // 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", 41 // http://b/241125373 42 "-bugprone-unchecked-optional-access", 43 // http://b/265438407 44 "-misc-use-anonymous-namespace", 45 } 46 47 // Some clang-tidy checks are included in some tidy_checks_as_errors lists, 48 // but not all warnings are fixed/suppressed yet. These checks are not 49 // disabled in the TidyGlobalNoChecks list, so we can see them and fix/suppress them. 50 globalNoErrorCheckList = []string{ 51 // http://b/241997913 52 "-bugprone-assignment-in-if-condition", 53 // http://b/155034972 54 "-bugprone-branch-clone", 55 // http://b/155034563 56 "-bugprone-signed-char-misuse", 57 // http://b/241819232 58 "-misc-const-correctness", 59 } 60 61 extraArgFlags = []string{ 62 // We might be using the static analyzer through clang tidy. 63 // https://bugs.llvm.org/show_bug.cgi?id=32914 64 "-D__clang_analyzer__", 65 66 // A recent change in clang-tidy (r328258) enabled destructor inlining, which 67 // appears to cause a number of false positives. Until that's resolved, this turns 68 // off the effects of r328258. 69 // https://bugs.llvm.org/show_bug.cgi?id=37459 70 "-Xclang", 71 "-analyzer-config", 72 "-Xclang", 73 "c++-temp-dtor-inlining=false", 74 } 75) 76 77func init() { 78 // The global default tidy checks should include clang-tidy 79 // default checks and tested groups, but exclude known noisy checks. 80 // See https://clang.llvm.org/extra/clang-tidy/checks/list.html 81 exportedVars.ExportVariableConfigMethod("TidyDefaultGlobalChecks", func(config android.Config) string { 82 if override := config.Getenv("DEFAULT_GLOBAL_TIDY_CHECKS"); override != "" { 83 return override 84 } 85 checks := strings.Join([]string{ 86 "android-*", 87 "bugprone-*", 88 "cert-*", 89 "clang-diagnostic-unused-command-line-argument", 90 // Select only google-* checks that do not have thousands of warnings. 91 // Add more such checks when we clean up source code. 92 // "google-build-using-namespace", 93 // "google-default-arguments", 94 // "google-explicit-constructor", 95 // "google-global-names-in-headers", 96 // "google-runtime-int", 97 "google-build-explicit-make-pair", 98 "google-build-namespaces", 99 "google-runtime-operator", 100 "google-upgrade-*", 101 "misc-*", 102 "performance-*", 103 "portability-*", 104 "-bugprone-assignment-in-if-condition", 105 "-bugprone-easily-swappable-parameters", 106 "-bugprone-narrowing-conversions", 107 "-misc-const-correctness", 108 "-misc-no-recursion", 109 "-misc-non-private-member-variables-in-classes", 110 "-misc-unused-parameters", 111 "-performance-no-int-to-ptr", 112 // the following groups are not in clang-tidy default checks. 113 // -altera-* 114 // -cppcoreguidelines-* 115 // -darwin-* 116 // -fuchsia-* 117 // -hicpp-* 118 // -llvm-* 119 // -llvmlibc-* 120 // -modernize-* 121 // -mpi-* 122 // -objc-* 123 // -readability-* 124 // -zircon-* 125 }, ",") 126 // clang-analyzer-* checks are slow for large files, but we have TIDY_TIMEOUT to 127 // limit clang-tidy runtime. We allow clang-tidy default clang-analyzer-* checks, 128 // and add it explicitly when CLANG_ANALYZER_CHECKS is set. 129 // The insecureAPI.DeprecatedOrUnsafeBufferHandling warning does not apply to Android. 130 if config.IsEnvTrue("CLANG_ANALYZER_CHECKS") { 131 checks += ",clang-analyzer-*,-clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling" 132 } else { 133 checks += ",-clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling" 134 } 135 return checks 136 }) 137 138 // The external and vendor projects do not run clang-tidy unless TIDY_EXTERNAL_VENDOR is set. 139 // We do not add "-*" to the check list to avoid suppressing the check list in .clang-tidy config files. 140 // There are too many clang-tidy warnings in external and vendor projects, so we only 141 // enable some google checks for these projects. Users can add more checks locally with the 142 // "tidy_checks" list in .bp files, or the "Checks" list in .clang-tidy config files. 143 exportedVars.ExportVariableConfigMethod("TidyExternalVendorChecks", func(config android.Config) string { 144 if override := config.Getenv("DEFAULT_EXTERNAL_VENDOR_TIDY_CHECKS"); override != "" { 145 return override 146 } 147 return strings.Join([]string{ 148 "clang-diagnostic-unused-command-line-argument", 149 "google-build-explicit-make-pair", 150 "google-build-namespaces", 151 "google-runtime-operator", 152 "google-upgrade-*", 153 "-clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling", 154 }, ",") 155 }) 156 157 exportedVars.ExportVariableFuncVariable("TidyGlobalNoChecks", func() string { 158 return strings.Join(globalNoCheckList, ",") 159 }) 160 161 exportedVars.ExportVariableFuncVariable("TidyGlobalNoErrorChecks", func() string { 162 return strings.Join(globalNoErrorCheckList, ",") 163 }) 164 165 exportedVars.ExportStringListStaticVariable("TidyExtraArgFlags", extraArgFlags) 166 167 // To reduce duplicate warnings from the same header files, 168 // header-filter will contain only the module directory and 169 // those specified by DEFAULT_TIDY_HEADER_DIRS. 170 exportedVars.ExportVariableConfigMethod("TidyDefaultHeaderDirs", func(config android.Config) string { 171 return config.Getenv("DEFAULT_TIDY_HEADER_DIRS") 172 }) 173 174 // Use WTIH_TIDY_FLAGS to pass extra global default clang-tidy flags. 175 exportedVars.ExportVariableConfigMethod("TidyWithTidyFlags", func(config android.Config) string { 176 return config.Getenv("WITH_TIDY_FLAGS") 177 }) 178} 179 180type PathBasedTidyCheck struct { 181 PathPrefix string 182 Checks string 183} 184 185const tidyDefault = "${config.TidyDefaultGlobalChecks}" 186const tidyExternalVendor = "${config.TidyExternalVendorChecks}" 187const tidyDefaultNoAnalyzer = "${config.TidyDefaultGlobalChecks},-clang-analyzer-*" 188 189// This is a map of local path prefixes to the set of default clang-tidy checks 190// to be used. This is like android.IsThirdPartyPath, but with more patterns. 191// The last matched local_path_prefix should be the most specific to be used. 192var DefaultLocalTidyChecks = []PathBasedTidyCheck{ 193 {"external/", tidyExternalVendor}, 194 {"frameworks/compile/mclinker/", tidyExternalVendor}, 195 {"hardware/", tidyExternalVendor}, 196 {"hardware/google/", tidyDefault}, 197 {"hardware/interfaces/", tidyDefault}, 198 {"hardware/ril/", tidyDefault}, 199 {"hardware/libhardware", tidyDefault}, // all 'hardware/libhardware*' 200 {"vendor/", tidyExternalVendor}, 201 {"vendor/google", tidyDefault}, // all 'vendor/google*' 202 {"vendor/google/external/", tidyExternalVendor}, 203 {"vendor/google_arc/libs/org.chromium.arc.mojom", tidyExternalVendor}, 204 {"vendor/google_devices/", tidyExternalVendor}, // many have vendor code 205} 206 207var reversedDefaultLocalTidyChecks = reverseTidyChecks(DefaultLocalTidyChecks) 208 209func reverseTidyChecks(in []PathBasedTidyCheck) []PathBasedTidyCheck { 210 ret := make([]PathBasedTidyCheck, len(in)) 211 for i, check := range in { 212 ret[len(in)-i-1] = check 213 } 214 return ret 215} 216 217func TidyChecksForDir(dir string) string { 218 dir = dir + "/" 219 for _, pathCheck := range reversedDefaultLocalTidyChecks { 220 if strings.HasPrefix(dir, pathCheck.PathPrefix) { 221 return pathCheck.Checks 222 } 223 } 224 return tidyDefault 225} 226 227func neverTidyForDir(dir string) bool { 228 // This function can be extended if tidy needs to be disabled for more directories. 229 return strings.HasPrefix(dir, "external/grpc-grpc") 230} 231 232func NoClangTidyForDir(allowExternalVendor bool, dir string) bool { 233 // Tidy can be disable for a module in dir, if the dir is "neverTidyForDir", 234 // or if it belongs to external|vendor and !allowExternalVendor. 235 // This function depends on TidyChecksForDir, which selects tidyExternalVendor 236 // checks for external/vendor projects. 237 return neverTidyForDir(dir) || 238 (!allowExternalVendor && TidyChecksForDir(dir) == tidyExternalVendor) 239} 240 241// Returns a globally disabled tidy checks, overriding locally selected checks. 242func TidyGlobalNoChecks() string { 243 if len(globalNoCheckList) > 0 { 244 return ",${config.TidyGlobalNoChecks}" 245 } 246 return "" 247} 248 249// Returns a globally allowed/no-error tidy checks, appended to -warnings-as-errors. 250func TidyGlobalNoErrorChecks() string { 251 if len(globalNoErrorCheckList) > 0 { 252 return ",${config.TidyGlobalNoErrorChecks}" 253 } 254 return "" 255} 256 257func TidyExtraArgFlags() []string { 258 return extraArgFlags 259} 260 261func TidyFlagsForSrcFile(srcFile android.Path, flags string) string { 262 // Disable clang-analyzer-* checks globally for generated source files 263 // because some of them are too huge. Local .bp files can add wanted 264 // clang-analyzer checks through the tidy_checks property. 265 // Need to do this patch per source file, because some modules 266 // have both generated and organic source files. 267 if _, ok := srcFile.(android.WritablePath); ok { 268 if strings.Contains(flags, tidyDefault) { 269 return strings.ReplaceAll(flags, tidyDefault, tidyDefaultNoAnalyzer) 270 } 271 } 272 return flags 273} 274 275var ( 276 removedCFlags = regexp.MustCompile(" -fsanitize=[^ ]*memtag-[^ ]* ") 277) 278 279func TidyReduceCFlags(flags string) string { 280 return removedCFlags.ReplaceAllString(flags, " ") 281} 282