blob: 4ac9e5863cda698d46ddf0dcb7737e548a58d924 [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.
24 // Global tidy checks include only google*, performance*,
25 // and misc-macro-parentheses, but not google-readability*
26 // or google-runtime-references.
Dan Willemsen54daaf02018-03-12 13:24:09 -070027 pctx.VariableFunc("TidyDefaultGlobalChecks", func(ctx android.PackageVarContext) string {
28 if override := ctx.Config().Getenv("DEFAULT_GLOBAL_TIDY_CHECKS"); override != "" {
29 return override
Dan Willemsen318af8b2016-11-02 14:50:21 -070030 }
31 return strings.Join([]string{
32 "-*",
Chih-Hung Hsieh70b93162020-03-03 12:05:22 -080033 "bugprone*",
Chih-Hung Hsieha7aa9582018-08-15 11:28:38 -070034 "clang-diagnostic-unused-command-line-argument",
Dan Willemsen318af8b2016-11-02 14:50:21 -070035 "google*",
36 "misc-macro-parentheses",
37 "performance*",
Chih-Hung Hsieh70b93162020-03-03 12:05:22 -080038 "-bugprone-narrowing-conversions",
Dan Willemsen318af8b2016-11-02 14:50:21 -070039 "-google-readability*",
40 "-google-runtime-references",
Dan Willemsen54daaf02018-03-12 13:24:09 -070041 }, ",")
Dan Willemsen318af8b2016-11-02 14:50:21 -070042 })
Dan Willemsena03cf6d2016-09-26 15:45:04 -070043
44 // There are too many clang-tidy warnings in external and vendor projects.
45 // Enable only some google checks for these projects.
Dan Willemsen54daaf02018-03-12 13:24:09 -070046 pctx.VariableFunc("TidyExternalVendorChecks", func(ctx android.PackageVarContext) string {
47 if override := ctx.Config().Getenv("DEFAULT_EXTERNAL_VENDOR_TIDY_CHECKS"); override != "" {
48 return override
Dan Willemsen318af8b2016-11-02 14:50:21 -070049 }
50 return strings.Join([]string{
51 "-*",
Chih-Hung Hsieha7aa9582018-08-15 11:28:38 -070052 "clang-diagnostic-unused-command-line-argument",
Dan Willemsen318af8b2016-11-02 14:50:21 -070053 "google*",
54 "-google-build-using-namespace",
55 "-google-default-arguments",
56 "-google-explicit-constructor",
57 "-google-readability*",
58 "-google-runtime-int",
59 "-google-runtime-references",
Dan Willemsen54daaf02018-03-12 13:24:09 -070060 }, ",")
Dan Willemsen318af8b2016-11-02 14:50:21 -070061 })
Dan Willemsena03cf6d2016-09-26 15:45:04 -070062
63 // Give warnings to header files only in selected directories.
64 // Do not give warnings to external or vendor header files, which contain too
65 // many warnings.
Chih-Hung Hsieh60bd4bf2018-09-21 09:38:17 -070066 pctx.VariableFunc("TidyDefaultHeaderDirs", func(ctx android.PackageVarContext) string {
67 if override := ctx.Config().Getenv("DEFAULT_TIDY_HEADER_DIRS"); override != "" {
68 return override
69 }
70 return strings.Join([]string{
71 "art/",
72 "bionic/",
73 "bootable/",
74 "build/",
75 "cts/",
76 "dalvik/",
77 "developers/",
78 "development/",
79 "frameworks/",
80 "libcore/",
81 "libnativehelper/",
82 "system/",
83 }, "|")
84 })
Chih-Hung Hsieh9e5d8a62018-09-21 15:12:44 -070085
86 // Use WTIH_TIDY_FLAGS to pass extra global default clang-tidy flags.
87 pctx.VariableFunc("TidyWithTidyFlags", func(ctx android.PackageVarContext) string {
88 return ctx.Config().Getenv("WITH_TIDY_FLAGS")
89 })
Dan Willemsena03cf6d2016-09-26 15:45:04 -070090}
91
92type PathBasedTidyCheck struct {
93 PathPrefix string
94 Checks string
95}
96
97const tidyDefault = "${config.TidyDefaultGlobalChecks}"
98const tidyExternalVendor = "${config.TidyExternalVendorChecks}"
99
100// This is a map of local path prefixes to the set of default clang-tidy checks
101// to be used.
102// The last matched local_path_prefix should be the most specific to be used.
103var DefaultLocalTidyChecks = []PathBasedTidyCheck{
104 {"external/", tidyExternalVendor},
105 {"external/google", tidyDefault},
106 {"external/webrtc", tidyDefault},
107 {"frameworks/compile/mclinker/", tidyExternalVendor},
108 {"hardware/qcom", tidyExternalVendor},
109 {"vendor/", tidyExternalVendor},
110 {"vendor/google", tidyDefault},
111 {"vendor/google_devices", tidyExternalVendor},
112}
113
114var reversedDefaultLocalTidyChecks = reverseTidyChecks(DefaultLocalTidyChecks)
115
116func reverseTidyChecks(in []PathBasedTidyCheck) []PathBasedTidyCheck {
117 ret := make([]PathBasedTidyCheck, len(in))
118 for i, check := range in {
119 ret[len(in)-i-1] = check
120 }
121 return ret
122}
123
124func TidyChecksForDir(dir string) string {
125 for _, pathCheck := range reversedDefaultLocalTidyChecks {
126 if strings.HasPrefix(dir, pathCheck.PathPrefix) {
127 return pathCheck.Checks
128 }
129 }
130 return tidyDefault
131}