blob: b3a86f3979c7a396302f08163af8c64800652208 [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 }
32 return 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-*",
Chih-Hung Hsieh4d31a042021-01-15 21:08:33 -080038 // clang-analyzer-* check is slow and enables clang-diagnostic-padded,
39 // which cannot be suppressed yet.
40 // "clang-analyzer-*",
Chris Li46cad062021-01-14 19:48:49 +000041 "clang-diagnostic-unused-command-line-argument",
Chih-Hung Hsieh34850d32021-01-14 16:51:49 -080042 "google-*",
43 "misc-*",
44 "performance-*",
45 "portability-*",
Chih-Hung Hsieh70b93162020-03-03 12:05:22 -080046 "-bugprone-narrowing-conversions",
Chris Li46cad062021-01-14 19:48:49 +000047 "-google-readability*",
Dan Willemsen318af8b2016-11-02 14:50:21 -070048 "-google-runtime-references",
Chih-Hung Hsieh34850d32021-01-14 16:51:49 -080049 "-misc-no-recursion",
50 "-misc-non-private-member-variables-in-classes",
51 "-misc-unused-parameters",
52 // the following groups are excluded by -*
53 // -altera-*
54 // -cppcoreguidelines-*
55 // -darwin-*
56 // -fuchsia-*
57 // -hicpp-*
58 // -llvm-*
59 // -llvmlibc-*
60 // -modernize-*
61 // -mpi-*
62 // -objc-*
63 // -readability-*
64 // -zircon-*
Dan Willemsen54daaf02018-03-12 13:24:09 -070065 }, ",")
Dan Willemsen318af8b2016-11-02 14:50:21 -070066 })
Dan Willemsena03cf6d2016-09-26 15:45:04 -070067
68 // There are too many clang-tidy warnings in external and vendor projects.
69 // Enable only some google checks for these projects.
Dan Willemsen54daaf02018-03-12 13:24:09 -070070 pctx.VariableFunc("TidyExternalVendorChecks", func(ctx android.PackageVarContext) string {
71 if override := ctx.Config().Getenv("DEFAULT_EXTERNAL_VENDOR_TIDY_CHECKS"); override != "" {
72 return override
Dan Willemsen318af8b2016-11-02 14:50:21 -070073 }
74 return strings.Join([]string{
75 "-*",
Chih-Hung Hsieha7aa9582018-08-15 11:28:38 -070076 "clang-diagnostic-unused-command-line-argument",
Dan Willemsen318af8b2016-11-02 14:50:21 -070077 "google*",
78 "-google-build-using-namespace",
79 "-google-default-arguments",
80 "-google-explicit-constructor",
81 "-google-readability*",
82 "-google-runtime-int",
83 "-google-runtime-references",
Dan Willemsen54daaf02018-03-12 13:24:09 -070084 }, ",")
Dan Willemsen318af8b2016-11-02 14:50:21 -070085 })
Dan Willemsena03cf6d2016-09-26 15:45:04 -070086
87 // Give warnings to header files only in selected directories.
88 // Do not give warnings to external or vendor header files, which contain too
89 // many warnings.
Chih-Hung Hsieh60bd4bf2018-09-21 09:38:17 -070090 pctx.VariableFunc("TidyDefaultHeaderDirs", func(ctx android.PackageVarContext) string {
91 if override := ctx.Config().Getenv("DEFAULT_TIDY_HEADER_DIRS"); override != "" {
92 return override
93 }
94 return strings.Join([]string{
95 "art/",
96 "bionic/",
97 "bootable/",
98 "build/",
99 "cts/",
100 "dalvik/",
101 "developers/",
102 "development/",
103 "frameworks/",
104 "libcore/",
105 "libnativehelper/",
106 "system/",
107 }, "|")
108 })
Chih-Hung Hsieh9e5d8a62018-09-21 15:12:44 -0700109
110 // Use WTIH_TIDY_FLAGS to pass extra global default clang-tidy flags.
111 pctx.VariableFunc("TidyWithTidyFlags", func(ctx android.PackageVarContext) string {
112 return ctx.Config().Getenv("WITH_TIDY_FLAGS")
113 })
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700114}
115
116type PathBasedTidyCheck struct {
117 PathPrefix string
118 Checks string
119}
120
121const tidyDefault = "${config.TidyDefaultGlobalChecks}"
122const tidyExternalVendor = "${config.TidyExternalVendorChecks}"
123
124// This is a map of local path prefixes to the set of default clang-tidy checks
125// to be used.
126// The last matched local_path_prefix should be the most specific to be used.
127var DefaultLocalTidyChecks = []PathBasedTidyCheck{
128 {"external/", tidyExternalVendor},
129 {"external/google", tidyDefault},
130 {"external/webrtc", tidyDefault},
131 {"frameworks/compile/mclinker/", tidyExternalVendor},
132 {"hardware/qcom", tidyExternalVendor},
133 {"vendor/", tidyExternalVendor},
134 {"vendor/google", tidyDefault},
135 {"vendor/google_devices", tidyExternalVendor},
136}
137
138var reversedDefaultLocalTidyChecks = reverseTidyChecks(DefaultLocalTidyChecks)
139
140func reverseTidyChecks(in []PathBasedTidyCheck) []PathBasedTidyCheck {
141 ret := make([]PathBasedTidyCheck, len(in))
142 for i, check := range in {
143 ret[len(in)-i-1] = check
144 }
145 return ret
146}
147
148func TidyChecksForDir(dir string) string {
149 for _, pathCheck := range reversedDefaultLocalTidyChecks {
150 if strings.HasPrefix(dir, pathCheck.PathPrefix) {
151 return pathCheck.Checks
152 }
153 }
154 return tidyDefault
155}