blob: 67f92a26557052a7865f3063958f5ac68c2ec4e0 [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 Hsieha7aa9582018-08-15 11:28:38 -070033 "clang-diagnostic-unused-command-line-argument",
Dan Willemsen318af8b2016-11-02 14:50:21 -070034 "google*",
35 "misc-macro-parentheses",
36 "performance*",
37 "-google-readability*",
38 "-google-runtime-references",
Dan Willemsen54daaf02018-03-12 13:24:09 -070039 }, ",")
Dan Willemsen318af8b2016-11-02 14:50:21 -070040 })
Dan Willemsena03cf6d2016-09-26 15:45:04 -070041
42 // There are too many clang-tidy warnings in external and vendor projects.
43 // Enable only some google checks for these projects.
Dan Willemsen54daaf02018-03-12 13:24:09 -070044 pctx.VariableFunc("TidyExternalVendorChecks", func(ctx android.PackageVarContext) string {
45 if override := ctx.Config().Getenv("DEFAULT_EXTERNAL_VENDOR_TIDY_CHECKS"); override != "" {
46 return override
Dan Willemsen318af8b2016-11-02 14:50:21 -070047 }
48 return strings.Join([]string{
49 "-*",
Chih-Hung Hsieha7aa9582018-08-15 11:28:38 -070050 "clang-diagnostic-unused-command-line-argument",
Dan Willemsen318af8b2016-11-02 14:50:21 -070051 "google*",
52 "-google-build-using-namespace",
53 "-google-default-arguments",
54 "-google-explicit-constructor",
55 "-google-readability*",
56 "-google-runtime-int",
57 "-google-runtime-references",
Dan Willemsen54daaf02018-03-12 13:24:09 -070058 }, ",")
Dan Willemsen318af8b2016-11-02 14:50:21 -070059 })
Dan Willemsena03cf6d2016-09-26 15:45:04 -070060
61 // Give warnings to header files only in selected directories.
62 // Do not give warnings to external or vendor header files, which contain too
63 // many warnings.
64 pctx.StaticVariable("TidyDefaultHeaderDirs", strings.Join([]string{
65 "art/",
66 "bionic/",
67 "bootable/",
68 "build/",
69 "cts/",
70 "dalvik/",
71 "developers/",
72 "development/",
73 "frameworks/",
74 "libcore/",
75 "libnativehelper/",
76 "system/",
77 }, "|"))
78}
79
80type PathBasedTidyCheck struct {
81 PathPrefix string
82 Checks string
83}
84
85const tidyDefault = "${config.TidyDefaultGlobalChecks}"
86const tidyExternalVendor = "${config.TidyExternalVendorChecks}"
87
88// This is a map of local path prefixes to the set of default clang-tidy checks
89// to be used.
90// The last matched local_path_prefix should be the most specific to be used.
91var DefaultLocalTidyChecks = []PathBasedTidyCheck{
92 {"external/", tidyExternalVendor},
93 {"external/google", tidyDefault},
94 {"external/webrtc", tidyDefault},
95 {"frameworks/compile/mclinker/", tidyExternalVendor},
96 {"hardware/qcom", tidyExternalVendor},
97 {"vendor/", tidyExternalVendor},
98 {"vendor/google", tidyDefault},
99 {"vendor/google_devices", tidyExternalVendor},
100}
101
102var reversedDefaultLocalTidyChecks = reverseTidyChecks(DefaultLocalTidyChecks)
103
104func reverseTidyChecks(in []PathBasedTidyCheck) []PathBasedTidyCheck {
105 ret := make([]PathBasedTidyCheck, len(in))
106 for i, check := range in {
107 ret[len(in)-i-1] = check
108 }
109 return ret
110}
111
112func TidyChecksForDir(dir string) string {
113 for _, pathCheck := range reversedDefaultLocalTidyChecks {
114 if strings.HasPrefix(dir, pathCheck.PathPrefix) {
115 return pathCheck.Checks
116 }
117 }
118 return tidyDefault
119}