blob: dd52a0ec56d4c150270da4487394b17656e40be2 [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.
Chih-Hung Hsieh60bd4bf2018-09-21 09:38:17 -070064 pctx.VariableFunc("TidyDefaultHeaderDirs", func(ctx android.PackageVarContext) string {
65 if override := ctx.Config().Getenv("DEFAULT_TIDY_HEADER_DIRS"); override != "" {
66 return override
67 }
68 return strings.Join([]string{
69 "art/",
70 "bionic/",
71 "bootable/",
72 "build/",
73 "cts/",
74 "dalvik/",
75 "developers/",
76 "development/",
77 "frameworks/",
78 "libcore/",
79 "libnativehelper/",
80 "system/",
81 }, "|")
82 })
Chih-Hung Hsieh9e5d8a62018-09-21 15:12:44 -070083
84 // Use WTIH_TIDY_FLAGS to pass extra global default clang-tidy flags.
85 pctx.VariableFunc("TidyWithTidyFlags", func(ctx android.PackageVarContext) string {
86 return ctx.Config().Getenv("WITH_TIDY_FLAGS")
87 })
Dan Willemsena03cf6d2016-09-26 15:45:04 -070088}
89
90type PathBasedTidyCheck struct {
91 PathPrefix string
92 Checks string
93}
94
95const tidyDefault = "${config.TidyDefaultGlobalChecks}"
96const tidyExternalVendor = "${config.TidyExternalVendorChecks}"
97
98// This is a map of local path prefixes to the set of default clang-tidy checks
99// to be used.
100// The last matched local_path_prefix should be the most specific to be used.
101var DefaultLocalTidyChecks = []PathBasedTidyCheck{
102 {"external/", tidyExternalVendor},
103 {"external/google", tidyDefault},
104 {"external/webrtc", tidyDefault},
105 {"frameworks/compile/mclinker/", tidyExternalVendor},
106 {"hardware/qcom", tidyExternalVendor},
107 {"vendor/", tidyExternalVendor},
108 {"vendor/google", tidyDefault},
109 {"vendor/google_devices", tidyExternalVendor},
110}
111
112var reversedDefaultLocalTidyChecks = reverseTidyChecks(DefaultLocalTidyChecks)
113
114func reverseTidyChecks(in []PathBasedTidyCheck) []PathBasedTidyCheck {
115 ret := make([]PathBasedTidyCheck, len(in))
116 for i, check := range in {
117 ret[len(in)-i-1] = check
118 }
119 return ret
120}
121
122func TidyChecksForDir(dir string) string {
123 for _, pathCheck := range reversedDefaultLocalTidyChecks {
124 if strings.HasPrefix(dir, pathCheck.PathPrefix) {
125 return pathCheck.Checks
126 }
127 }
128 return tidyDefault
129}