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