blob: 079cf256e434c6ccb0528bc75ba0c204a666c823 [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
Chih-Hung Hsiehad47a272018-06-25 13:48:42 -070022// clang-tidy doesn't recognize every flag that clang does. This is unlikely to
23// be a complete list, but we can populate this with the ones we know to avoid
24// issues with clang-diagnostic-unused-command-line-argument.
Chih-Hung Hsieh5942fe42018-07-26 15:00:28 -070025// b/111885396: -flto affected header include directory; -fsanitize needs -flto.
Chih-Hung Hsiehad47a272018-06-25 13:48:42 -070026var ClangTidyUnknownCflags = sorted([]string{
27 "-Wa,%",
Chih-Hung Hsieh5942fe42018-07-26 15:00:28 -070028 "-flto",
29 "-fsanitize=%",
Chih-Hung Hsiehad47a272018-06-25 13:48:42 -070030})
31
Dan Willemsena03cf6d2016-09-26 15:45:04 -070032func init() {
33 // Most Android source files are not clang-tidy clean yet.
34 // Global tidy checks include only google*, performance*,
35 // and misc-macro-parentheses, but not google-readability*
36 // or google-runtime-references.
Dan Willemsen54daaf02018-03-12 13:24:09 -070037 pctx.VariableFunc("TidyDefaultGlobalChecks", func(ctx android.PackageVarContext) string {
38 if override := ctx.Config().Getenv("DEFAULT_GLOBAL_TIDY_CHECKS"); override != "" {
39 return override
Dan Willemsen318af8b2016-11-02 14:50:21 -070040 }
41 return strings.Join([]string{
42 "-*",
43 "google*",
44 "misc-macro-parentheses",
45 "performance*",
46 "-google-readability*",
47 "-google-runtime-references",
Dan Willemsen54daaf02018-03-12 13:24:09 -070048 }, ",")
Dan Willemsen318af8b2016-11-02 14:50:21 -070049 })
Dan Willemsena03cf6d2016-09-26 15:45:04 -070050
51 // There are too many clang-tidy warnings in external and vendor projects.
52 // Enable only some google checks for these projects.
Dan Willemsen54daaf02018-03-12 13:24:09 -070053 pctx.VariableFunc("TidyExternalVendorChecks", func(ctx android.PackageVarContext) string {
54 if override := ctx.Config().Getenv("DEFAULT_EXTERNAL_VENDOR_TIDY_CHECKS"); override != "" {
55 return override
Dan Willemsen318af8b2016-11-02 14:50:21 -070056 }
57 return strings.Join([]string{
58 "-*",
59 "google*",
60 "-google-build-using-namespace",
61 "-google-default-arguments",
62 "-google-explicit-constructor",
63 "-google-readability*",
64 "-google-runtime-int",
65 "-google-runtime-references",
Dan Willemsen54daaf02018-03-12 13:24:09 -070066 }, ",")
Dan Willemsen318af8b2016-11-02 14:50:21 -070067 })
Dan Willemsena03cf6d2016-09-26 15:45:04 -070068
69 // Give warnings to header files only in selected directories.
70 // Do not give warnings to external or vendor header files, which contain too
71 // many warnings.
72 pctx.StaticVariable("TidyDefaultHeaderDirs", strings.Join([]string{
73 "art/",
74 "bionic/",
75 "bootable/",
76 "build/",
77 "cts/",
78 "dalvik/",
79 "developers/",
80 "development/",
81 "frameworks/",
82 "libcore/",
83 "libnativehelper/",
84 "system/",
85 }, "|"))
86}
87
88type PathBasedTidyCheck struct {
89 PathPrefix string
90 Checks string
91}
92
93const tidyDefault = "${config.TidyDefaultGlobalChecks}"
94const tidyExternalVendor = "${config.TidyExternalVendorChecks}"
95
96// This is a map of local path prefixes to the set of default clang-tidy checks
97// to be used.
98// The last matched local_path_prefix should be the most specific to be used.
99var DefaultLocalTidyChecks = []PathBasedTidyCheck{
100 {"external/", tidyExternalVendor},
101 {"external/google", tidyDefault},
102 {"external/webrtc", tidyDefault},
103 {"frameworks/compile/mclinker/", tidyExternalVendor},
104 {"hardware/qcom", tidyExternalVendor},
105 {"vendor/", tidyExternalVendor},
106 {"vendor/google", tidyDefault},
107 {"vendor/google_devices", tidyExternalVendor},
108}
109
110var reversedDefaultLocalTidyChecks = reverseTidyChecks(DefaultLocalTidyChecks)
111
112func reverseTidyChecks(in []PathBasedTidyCheck) []PathBasedTidyCheck {
113 ret := make([]PathBasedTidyCheck, len(in))
114 for i, check := range in {
115 ret[len(in)-i-1] = check
116 }
117 return ret
118}
119
120func TidyChecksForDir(dir string) string {
121 for _, pathCheck := range reversedDefaultLocalTidyChecks {
122 if strings.HasPrefix(dir, pathCheck.PathPrefix) {
123 return pathCheck.Checks
124 }
125 }
126 return tidyDefault
127}