blob: d5d01b44461255824f03f9d74ac56e84d77d431e [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 "abseil-*",
35 "android-*",
36 "bugprone-*",
37 "cert-*",
Chris Li46cad062021-01-14 19:48:49 +000038 "clang-diagnostic-unused-command-line-argument",
Chih-Hung Hsieh34850d32021-01-14 16:51:49 -080039 "google-*",
40 "misc-*",
41 "performance-*",
42 "portability-*",
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",
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 Willemsen54daaf02018-03-12 13:24:09 -070062 }, ",")
Chih-Hung Hsieh04f8d372021-01-19 18:28:18 -080063 // 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.
65 // Some test code have clang-diagnostic-padded warnings that cannot be
66 // suppressed, but only by disabling clang-analyzer-optin.performance.*.
67 if ctx.Config().IsEnvTrue("CLANG_ANALYZER_CHECKS") {
68 checks += ",clang-analyzer-*,-clang-analyzer-optin.performance.*"
69 }
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
92 // Give warnings to header files only in selected directories.
93 // Do not give warnings to external or vendor header files, which contain too
94 // many warnings.
Chih-Hung Hsieh60bd4bf2018-09-21 09:38:17 -070095 pctx.VariableFunc("TidyDefaultHeaderDirs", func(ctx android.PackageVarContext) string {
96 if override := ctx.Config().Getenv("DEFAULT_TIDY_HEADER_DIRS"); override != "" {
97 return override
98 }
99 return strings.Join([]string{
100 "art/",
101 "bionic/",
102 "bootable/",
103 "build/",
104 "cts/",
105 "dalvik/",
106 "developers/",
107 "development/",
108 "frameworks/",
109 "libcore/",
110 "libnativehelper/",
111 "system/",
112 }, "|")
113 })
Chih-Hung Hsieh9e5d8a62018-09-21 15:12:44 -0700114
115 // Use WTIH_TIDY_FLAGS to pass extra global default clang-tidy flags.
116 pctx.VariableFunc("TidyWithTidyFlags", func(ctx android.PackageVarContext) string {
117 return ctx.Config().Getenv("WITH_TIDY_FLAGS")
118 })
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700119}
120
121type PathBasedTidyCheck struct {
122 PathPrefix string
123 Checks string
124}
125
126const tidyDefault = "${config.TidyDefaultGlobalChecks}"
127const tidyExternalVendor = "${config.TidyExternalVendorChecks}"
128
129// This is a map of local path prefixes to the set of default clang-tidy checks
130// to be used.
131// The last matched local_path_prefix should be the most specific to be used.
132var DefaultLocalTidyChecks = []PathBasedTidyCheck{
133 {"external/", tidyExternalVendor},
134 {"external/google", tidyDefault},
135 {"external/webrtc", tidyDefault},
136 {"frameworks/compile/mclinker/", tidyExternalVendor},
137 {"hardware/qcom", tidyExternalVendor},
138 {"vendor/", tidyExternalVendor},
139 {"vendor/google", tidyDefault},
140 {"vendor/google_devices", tidyExternalVendor},
141}
142
143var reversedDefaultLocalTidyChecks = reverseTidyChecks(DefaultLocalTidyChecks)
144
145func reverseTidyChecks(in []PathBasedTidyCheck) []PathBasedTidyCheck {
146 ret := make([]PathBasedTidyCheck, len(in))
147 for i, check := range in {
148 ret[len(in)-i-1] = check
149 }
150 return ret
151}
152
153func TidyChecksForDir(dir string) string {
154 for _, pathCheck := range reversedDefaultLocalTidyChecks {
155 if strings.HasPrefix(dir, pathCheck.PathPrefix) {
156 return pathCheck.Checks
157 }
158 }
159 return tidyDefault
160}