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