blob: 0df17bce71a7868d8b12cd020bb144b66b39f05b [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() {
23 // Most Android source files are not clang-tidy clean yet.
Chih-Hung Hsiehb063dc42021-01-11 17:31:02 -080024 // Default global tidy checks must exclude all checks that
25 // have found too many warnings.
Dan Willemsen54daaf02018-03-12 13:24:09 -070026 pctx.VariableFunc("TidyDefaultGlobalChecks", func(ctx android.PackageVarContext) string {
27 if override := ctx.Config().Getenv("DEFAULT_GLOBAL_TIDY_CHECKS"); override != "" {
28 return override
Dan Willemsen318af8b2016-11-02 14:50:21 -070029 }
30 return strings.Join([]string{
Chih-Hung Hsiehb063dc42021-01-11 17:31:02 -080031 "*",
32 "-altera-*",
Chih-Hung Hsieh70b93162020-03-03 12:05:22 -080033 "-bugprone-narrowing-conversions",
Chih-Hung Hsiehb063dc42021-01-11 17:31:02 -080034 "-cppcoreguidelines-*",
35 "-fuchsia-*",
36 "-google-readability-*",
Dan Willemsen318af8b2016-11-02 14:50:21 -070037 "-google-runtime-references",
Chih-Hung Hsiehb063dc42021-01-11 17:31:02 -080038 "-hicpp-*",
39 "-llvm-*",
40 "-llvmlibc-*",
41 "-misc-no-recursion",
42 "-misc-non-private-member-variables-in-classes",
43 "-misc-unused-parameters",
44 "-modernize-*",
45 "-readability-*",
Dan Willemsen54daaf02018-03-12 13:24:09 -070046 }, ",")
Dan Willemsen318af8b2016-11-02 14:50:21 -070047 })
Dan Willemsena03cf6d2016-09-26 15:45:04 -070048
49 // There are too many clang-tidy warnings in external and vendor projects.
50 // Enable only some google checks for these projects.
Dan Willemsen54daaf02018-03-12 13:24:09 -070051 pctx.VariableFunc("TidyExternalVendorChecks", func(ctx android.PackageVarContext) string {
52 if override := ctx.Config().Getenv("DEFAULT_EXTERNAL_VENDOR_TIDY_CHECKS"); override != "" {
53 return override
Dan Willemsen318af8b2016-11-02 14:50:21 -070054 }
55 return strings.Join([]string{
56 "-*",
Chih-Hung Hsieha7aa9582018-08-15 11:28:38 -070057 "clang-diagnostic-unused-command-line-argument",
Dan Willemsen318af8b2016-11-02 14:50:21 -070058 "google*",
59 "-google-build-using-namespace",
60 "-google-default-arguments",
61 "-google-explicit-constructor",
62 "-google-readability*",
63 "-google-runtime-int",
64 "-google-runtime-references",
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 // Give warnings to header files only in selected directories.
69 // Do not give warnings to external or vendor header files, which contain too
70 // many warnings.
Chih-Hung Hsieh60bd4bf2018-09-21 09:38:17 -070071 pctx.VariableFunc("TidyDefaultHeaderDirs", func(ctx android.PackageVarContext) string {
72 if override := ctx.Config().Getenv("DEFAULT_TIDY_HEADER_DIRS"); override != "" {
73 return override
74 }
75 return strings.Join([]string{
76 "art/",
77 "bionic/",
78 "bootable/",
79 "build/",
80 "cts/",
81 "dalvik/",
82 "developers/",
83 "development/",
84 "frameworks/",
85 "libcore/",
86 "libnativehelper/",
87 "system/",
88 }, "|")
89 })
Chih-Hung Hsieh9e5d8a62018-09-21 15:12:44 -070090
91 // Use WTIH_TIDY_FLAGS to pass extra global default clang-tidy flags.
92 pctx.VariableFunc("TidyWithTidyFlags", func(ctx android.PackageVarContext) string {
93 return ctx.Config().Getenv("WITH_TIDY_FLAGS")
94 })
Dan Willemsena03cf6d2016-09-26 15:45:04 -070095}
96
97type PathBasedTidyCheck struct {
98 PathPrefix string
99 Checks string
100}
101
102const tidyDefault = "${config.TidyDefaultGlobalChecks}"
103const tidyExternalVendor = "${config.TidyExternalVendorChecks}"
104
105// This is a map of local path prefixes to the set of default clang-tidy checks
106// to be used.
107// The last matched local_path_prefix should be the most specific to be used.
108var DefaultLocalTidyChecks = []PathBasedTidyCheck{
109 {"external/", tidyExternalVendor},
110 {"external/google", tidyDefault},
111 {"external/webrtc", tidyDefault},
112 {"frameworks/compile/mclinker/", tidyExternalVendor},
113 {"hardware/qcom", tidyExternalVendor},
114 {"vendor/", tidyExternalVendor},
115 {"vendor/google", tidyDefault},
116 {"vendor/google_devices", tidyExternalVendor},
117}
118
119var reversedDefaultLocalTidyChecks = reverseTidyChecks(DefaultLocalTidyChecks)
120
121func reverseTidyChecks(in []PathBasedTidyCheck) []PathBasedTidyCheck {
122 ret := make([]PathBasedTidyCheck, len(in))
123 for i, check := range in {
124 ret[len(in)-i-1] = check
125 }
126 return ret
127}
128
129func TidyChecksForDir(dir string) string {
130 for _, pathCheck := range reversedDefaultLocalTidyChecks {
131 if strings.HasPrefix(dir, pathCheck.PathPrefix) {
132 return pathCheck.Checks
133 }
134 }
135 return tidyDefault
136}