Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 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 | |
| 15 | package config |
| 16 | |
| 17 | import ( |
Dan Willemsen | 318af8b | 2016-11-02 14:50:21 -0700 | [diff] [blame] | 18 | "android/soong/android" |
Dan Willemsen | 54daaf0 | 2018-03-12 13:24:09 -0700 | [diff] [blame] | 19 | "strings" |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 20 | ) |
| 21 | |
| 22 | func init() { |
Chih-Hung Hsieh | 34850d3 | 2021-01-14 16:51:49 -0800 | [diff] [blame] | 23 | // 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 Willemsen | 54daaf0 | 2018-03-12 13:24:09 -0700 | [diff] [blame] | 28 | pctx.VariableFunc("TidyDefaultGlobalChecks", func(ctx android.PackageVarContext) string { |
| 29 | if override := ctx.Config().Getenv("DEFAULT_GLOBAL_TIDY_CHECKS"); override != "" { |
| 30 | return override |
Dan Willemsen | 318af8b | 2016-11-02 14:50:21 -0700 | [diff] [blame] | 31 | } |
Chih-Hung Hsieh | 04f8d37 | 2021-01-19 18:28:18 -0800 | [diff] [blame] | 32 | checks := strings.Join([]string{ |
Chris Li | 46cad06 | 2021-01-14 19:48:49 +0000 | [diff] [blame] | 33 | "-*", |
Chih-Hung Hsieh | 34850d3 | 2021-01-14 16:51:49 -0800 | [diff] [blame] | 34 | "android-*", |
| 35 | "bugprone-*", |
| 36 | "cert-*", |
Chris Li | 46cad06 | 2021-01-14 19:48:49 +0000 | [diff] [blame] | 37 | "clang-diagnostic-unused-command-line-argument", |
Chih-Hung Hsieh | 34850d3 | 2021-01-14 16:51:49 -0800 | [diff] [blame] | 38 | "google-*", |
| 39 | "misc-*", |
| 40 | "performance-*", |
| 41 | "portability-*", |
Stephen Hines | 4b72145 | 2021-09-11 01:14:36 -0700 | [diff] [blame] | 42 | "-bugprone-easily-swappable-parameters", |
Chih-Hung Hsieh | 70b9316 | 2020-03-03 12:05:22 -0800 | [diff] [blame] | 43 | "-bugprone-narrowing-conversions", |
Chris Li | 46cad06 | 2021-01-14 19:48:49 +0000 | [diff] [blame] | 44 | "-google-readability*", |
Dan Willemsen | 318af8b | 2016-11-02 14:50:21 -0700 | [diff] [blame] | 45 | "-google-runtime-references", |
Chih-Hung Hsieh | 34850d3 | 2021-01-14 16:51:49 -0800 | [diff] [blame] | 46 | "-misc-no-recursion", |
| 47 | "-misc-non-private-member-variables-in-classes", |
| 48 | "-misc-unused-parameters", |
| 49 | // the following groups are excluded by -* |
| 50 | // -altera-* |
| 51 | // -cppcoreguidelines-* |
| 52 | // -darwin-* |
| 53 | // -fuchsia-* |
| 54 | // -hicpp-* |
| 55 | // -llvm-* |
| 56 | // -llvmlibc-* |
| 57 | // -modernize-* |
| 58 | // -mpi-* |
| 59 | // -objc-* |
| 60 | // -readability-* |
| 61 | // -zircon-* |
Dan Willemsen | 54daaf0 | 2018-03-12 13:24:09 -0700 | [diff] [blame] | 62 | }, ",") |
Chih-Hung Hsieh | 04f8d37 | 2021-01-19 18:28:18 -0800 | [diff] [blame] | 63 | // clang-analyzer-* checks are too slow to be in the default for WITH_TIDY=1. |
| 64 | // nightly builds add CLANG_ANALYZER_CHECKS=1 to run those checks. |
Chih-Hung Hsieh | 9bcce2e | 2022-02-07 16:44:13 -0800 | [diff] [blame] | 65 | // The insecureAPI.DeprecatedOrUnsafeBufferHandling warning does not apply to Android. |
Chih-Hung Hsieh | 04f8d37 | 2021-01-19 18:28:18 -0800 | [diff] [blame] | 66 | if ctx.Config().IsEnvTrue("CLANG_ANALYZER_CHECKS") { |
Chih-Hung Hsieh | 9bcce2e | 2022-02-07 16:44:13 -0800 | [diff] [blame] | 67 | checks += ",clang-analyzer-*,-clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling" |
Chih-Hung Hsieh | 04f8d37 | 2021-01-19 18:28:18 -0800 | [diff] [blame] | 68 | } |
| 69 | return checks |
Dan Willemsen | 318af8b | 2016-11-02 14:50:21 -0700 | [diff] [blame] | 70 | }) |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 71 | |
| 72 | // There are too many clang-tidy warnings in external and vendor projects. |
| 73 | // Enable only some google checks for these projects. |
Dan Willemsen | 54daaf0 | 2018-03-12 13:24:09 -0700 | [diff] [blame] | 74 | pctx.VariableFunc("TidyExternalVendorChecks", func(ctx android.PackageVarContext) string { |
| 75 | if override := ctx.Config().Getenv("DEFAULT_EXTERNAL_VENDOR_TIDY_CHECKS"); override != "" { |
| 76 | return override |
Dan Willemsen | 318af8b | 2016-11-02 14:50:21 -0700 | [diff] [blame] | 77 | } |
| 78 | return strings.Join([]string{ |
| 79 | "-*", |
Chih-Hung Hsieh | a7aa958 | 2018-08-15 11:28:38 -0700 | [diff] [blame] | 80 | "clang-diagnostic-unused-command-line-argument", |
Dan Willemsen | 318af8b | 2016-11-02 14:50:21 -0700 | [diff] [blame] | 81 | "google*", |
| 82 | "-google-build-using-namespace", |
| 83 | "-google-default-arguments", |
| 84 | "-google-explicit-constructor", |
| 85 | "-google-readability*", |
| 86 | "-google-runtime-int", |
| 87 | "-google-runtime-references", |
Dan Willemsen | 54daaf0 | 2018-03-12 13:24:09 -0700 | [diff] [blame] | 88 | }, ",") |
Dan Willemsen | 318af8b | 2016-11-02 14:50:21 -0700 | [diff] [blame] | 89 | }) |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 90 | |
Chih-Hung Hsieh | 9f94c36 | 2021-02-10 21:56:03 -0800 | [diff] [blame] | 91 | // To reduce duplicate warnings from the same header files, |
| 92 | // header-filter will contain only the module directory and |
| 93 | // those specified by DEFAULT_TIDY_HEADER_DIRS. |
Chih-Hung Hsieh | 60bd4bf | 2018-09-21 09:38:17 -0700 | [diff] [blame] | 94 | pctx.VariableFunc("TidyDefaultHeaderDirs", func(ctx android.PackageVarContext) string { |
Chih-Hung Hsieh | 9f94c36 | 2021-02-10 21:56:03 -0800 | [diff] [blame] | 95 | return ctx.Config().Getenv("DEFAULT_TIDY_HEADER_DIRS") |
Chih-Hung Hsieh | 60bd4bf | 2018-09-21 09:38:17 -0700 | [diff] [blame] | 96 | }) |
Chih-Hung Hsieh | 9e5d8a6 | 2018-09-21 15:12:44 -0700 | [diff] [blame] | 97 | |
| 98 | // Use WTIH_TIDY_FLAGS to pass extra global default clang-tidy flags. |
| 99 | pctx.VariableFunc("TidyWithTidyFlags", func(ctx android.PackageVarContext) string { |
| 100 | return ctx.Config().Getenv("WITH_TIDY_FLAGS") |
| 101 | }) |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | type PathBasedTidyCheck struct { |
| 105 | PathPrefix string |
| 106 | Checks string |
| 107 | } |
| 108 | |
| 109 | const tidyDefault = "${config.TidyDefaultGlobalChecks}" |
| 110 | const tidyExternalVendor = "${config.TidyExternalVendorChecks}" |
Chih-Hung Hsieh | 062e934 | 2021-08-30 13:32:29 -0700 | [diff] [blame] | 111 | const tidyDefaultNoAnalyzer = "${config.TidyDefaultGlobalChecks},-clang-analyzer-*" |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 112 | |
| 113 | // This is a map of local path prefixes to the set of default clang-tidy checks |
| 114 | // to be used. |
| 115 | // The last matched local_path_prefix should be the most specific to be used. |
| 116 | var DefaultLocalTidyChecks = []PathBasedTidyCheck{ |
| 117 | {"external/", tidyExternalVendor}, |
| 118 | {"external/google", tidyDefault}, |
| 119 | {"external/webrtc", tidyDefault}, |
Chih-Hung Hsieh | f92c715 | 2021-09-05 19:53:15 -0700 | [diff] [blame] | 120 | {"external/googletest/", tidyExternalVendor}, |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 121 | {"frameworks/compile/mclinker/", tidyExternalVendor}, |
| 122 | {"hardware/qcom", tidyExternalVendor}, |
| 123 | {"vendor/", tidyExternalVendor}, |
| 124 | {"vendor/google", tidyDefault}, |
| 125 | {"vendor/google_devices", tidyExternalVendor}, |
| 126 | } |
| 127 | |
| 128 | var reversedDefaultLocalTidyChecks = reverseTidyChecks(DefaultLocalTidyChecks) |
| 129 | |
| 130 | func reverseTidyChecks(in []PathBasedTidyCheck) []PathBasedTidyCheck { |
| 131 | ret := make([]PathBasedTidyCheck, len(in)) |
| 132 | for i, check := range in { |
| 133 | ret[len(in)-i-1] = check |
| 134 | } |
| 135 | return ret |
| 136 | } |
| 137 | |
| 138 | func TidyChecksForDir(dir string) string { |
Chih-Hung Hsieh | f92c715 | 2021-09-05 19:53:15 -0700 | [diff] [blame] | 139 | dir = dir + "/" |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 140 | for _, pathCheck := range reversedDefaultLocalTidyChecks { |
| 141 | if strings.HasPrefix(dir, pathCheck.PathPrefix) { |
| 142 | return pathCheck.Checks |
| 143 | } |
| 144 | } |
| 145 | return tidyDefault |
| 146 | } |
Chih-Hung Hsieh | 062e934 | 2021-08-30 13:32:29 -0700 | [diff] [blame] | 147 | |
| 148 | func TidyFlagsForSrcFile(srcFile android.Path, flags string) string { |
| 149 | // Disable clang-analyzer-* checks globally for generated source files |
| 150 | // because some of them are too huge. Local .bp files can add wanted |
| 151 | // clang-analyzer checks through the tidy_checks property. |
| 152 | // Need to do this patch per source file, because some modules |
| 153 | // have both generated and organic source files. |
| 154 | if _, ok := srcFile.(android.WritablePath); ok { |
| 155 | if strings.Contains(flags, tidyDefault) { |
| 156 | return strings.ReplaceAll(flags, tidyDefault, tidyDefaultNoAnalyzer) |
| 157 | } |
| 158 | } |
| 159 | return flags |
| 160 | } |