blob: 7cc9f438cea7da27c0556e4dba2ba498a85f0f48 [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.
Chih-Hung Hsieh04f8d372021-01-19 18:28:18 -080065 if ctx.Config().IsEnvTrue("CLANG_ANALYZER_CHECKS") {
Chih-Hung Hsiehe9dca0a2021-01-25 14:00:51 -080066 checks += ",clang-analyzer-*"
Chih-Hung Hsieh04f8d372021-01-19 18:28:18 -080067 }
68 return checks
Dan Willemsen318af8b2016-11-02 14:50:21 -070069 })
Dan Willemsena03cf6d2016-09-26 15:45:04 -070070
71 // There are too many clang-tidy warnings in external and vendor projects.
72 // Enable only some google checks for these projects.
Dan Willemsen54daaf02018-03-12 13:24:09 -070073 pctx.VariableFunc("TidyExternalVendorChecks", func(ctx android.PackageVarContext) string {
74 if override := ctx.Config().Getenv("DEFAULT_EXTERNAL_VENDOR_TIDY_CHECKS"); override != "" {
75 return override
Dan Willemsen318af8b2016-11-02 14:50:21 -070076 }
77 return strings.Join([]string{
78 "-*",
Chih-Hung Hsieha7aa9582018-08-15 11:28:38 -070079 "clang-diagnostic-unused-command-line-argument",
Dan Willemsen318af8b2016-11-02 14:50:21 -070080 "google*",
81 "-google-build-using-namespace",
82 "-google-default-arguments",
83 "-google-explicit-constructor",
84 "-google-readability*",
85 "-google-runtime-int",
86 "-google-runtime-references",
Dan Willemsen54daaf02018-03-12 13:24:09 -070087 }, ",")
Dan Willemsen318af8b2016-11-02 14:50:21 -070088 })
Dan Willemsena03cf6d2016-09-26 15:45:04 -070089
90 // Give warnings to header files only in selected directories.
91 // Do not give warnings to external or vendor header files, which contain too
92 // many warnings.
Chih-Hung Hsieh60bd4bf2018-09-21 09:38:17 -070093 pctx.VariableFunc("TidyDefaultHeaderDirs", func(ctx android.PackageVarContext) string {
94 if override := ctx.Config().Getenv("DEFAULT_TIDY_HEADER_DIRS"); override != "" {
95 return override
96 }
97 return strings.Join([]string{
98 "art/",
99 "bionic/",
100 "bootable/",
101 "build/",
102 "cts/",
103 "dalvik/",
104 "developers/",
105 "development/",
106 "frameworks/",
107 "libcore/",
108 "libnativehelper/",
109 "system/",
110 }, "|")
111 })
Chih-Hung Hsieh9e5d8a62018-09-21 15:12:44 -0700112
113 // Use WTIH_TIDY_FLAGS to pass extra global default clang-tidy flags.
114 pctx.VariableFunc("TidyWithTidyFlags", func(ctx android.PackageVarContext) string {
115 return ctx.Config().Getenv("WITH_TIDY_FLAGS")
116 })
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700117}
118
119type PathBasedTidyCheck struct {
120 PathPrefix string
121 Checks string
122}
123
124const tidyDefault = "${config.TidyDefaultGlobalChecks}"
125const tidyExternalVendor = "${config.TidyExternalVendorChecks}"
126
127// This is a map of local path prefixes to the set of default clang-tidy checks
128// to be used.
129// The last matched local_path_prefix should be the most specific to be used.
130var DefaultLocalTidyChecks = []PathBasedTidyCheck{
131 {"external/", tidyExternalVendor},
132 {"external/google", tidyDefault},
133 {"external/webrtc", tidyDefault},
134 {"frameworks/compile/mclinker/", tidyExternalVendor},
135 {"hardware/qcom", tidyExternalVendor},
136 {"vendor/", tidyExternalVendor},
137 {"vendor/google", tidyDefault},
138 {"vendor/google_devices", tidyExternalVendor},
139}
140
141var reversedDefaultLocalTidyChecks = reverseTidyChecks(DefaultLocalTidyChecks)
142
143func reverseTidyChecks(in []PathBasedTidyCheck) []PathBasedTidyCheck {
144 ret := make([]PathBasedTidyCheck, len(in))
145 for i, check := range in {
146 ret[len(in)-i-1] = check
147 }
148 return ret
149}
150
151func TidyChecksForDir(dir string) string {
152 for _, pathCheck := range reversedDefaultLocalTidyChecks {
153 if strings.HasPrefix(dir, pathCheck.PathPrefix) {
154 return pathCheck.Checks
155 }
156 }
157 return tidyDefault
158}