blob: 5d53a8cd6422cba017c35aafc5f7fee185b3a695 [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.
25var ClangTidyUnknownCflags = sorted([]string{
26 "-Wa,%",
27})
28
Dan Willemsena03cf6d2016-09-26 15:45:04 -070029func init() {
30 // Most Android source files are not clang-tidy clean yet.
31 // Global tidy checks include only google*, performance*,
32 // and misc-macro-parentheses, but not google-readability*
33 // or google-runtime-references.
Dan Willemsen54daaf02018-03-12 13:24:09 -070034 pctx.VariableFunc("TidyDefaultGlobalChecks", func(ctx android.PackageVarContext) string {
35 if override := ctx.Config().Getenv("DEFAULT_GLOBAL_TIDY_CHECKS"); override != "" {
36 return override
Dan Willemsen318af8b2016-11-02 14:50:21 -070037 }
38 return strings.Join([]string{
39 "-*",
40 "google*",
41 "misc-macro-parentheses",
42 "performance*",
43 "-google-readability*",
44 "-google-runtime-references",
Dan Willemsen54daaf02018-03-12 13:24:09 -070045 }, ",")
Dan Willemsen318af8b2016-11-02 14:50:21 -070046 })
Dan Willemsena03cf6d2016-09-26 15:45:04 -070047
48 // There are too many clang-tidy warnings in external and vendor projects.
49 // Enable only some google checks for these projects.
Dan Willemsen54daaf02018-03-12 13:24:09 -070050 pctx.VariableFunc("TidyExternalVendorChecks", func(ctx android.PackageVarContext) string {
51 if override := ctx.Config().Getenv("DEFAULT_EXTERNAL_VENDOR_TIDY_CHECKS"); override != "" {
52 return override
Dan Willemsen318af8b2016-11-02 14:50:21 -070053 }
54 return strings.Join([]string{
55 "-*",
56 "google*",
57 "-google-build-using-namespace",
58 "-google-default-arguments",
59 "-google-explicit-constructor",
60 "-google-readability*",
61 "-google-runtime-int",
62 "-google-runtime-references",
Dan Willemsen54daaf02018-03-12 13:24:09 -070063 }, ",")
Dan Willemsen318af8b2016-11-02 14:50:21 -070064 })
Dan Willemsena03cf6d2016-09-26 15:45:04 -070065
66 // Give warnings to header files only in selected directories.
67 // Do not give warnings to external or vendor header files, which contain too
68 // many warnings.
69 pctx.StaticVariable("TidyDefaultHeaderDirs", strings.Join([]string{
70 "art/",
71 "bionic/",
72 "bootable/",
73 "build/",
74 "cts/",
75 "dalvik/",
76 "developers/",
77 "development/",
78 "frameworks/",
79 "libcore/",
80 "libnativehelper/",
81 "system/",
82 }, "|"))
83}
84
85type PathBasedTidyCheck struct {
86 PathPrefix string
87 Checks string
88}
89
90const tidyDefault = "${config.TidyDefaultGlobalChecks}"
91const tidyExternalVendor = "${config.TidyExternalVendorChecks}"
92
93// This is a map of local path prefixes to the set of default clang-tidy checks
94// to be used.
95// The last matched local_path_prefix should be the most specific to be used.
96var DefaultLocalTidyChecks = []PathBasedTidyCheck{
97 {"external/", tidyExternalVendor},
98 {"external/google", tidyDefault},
99 {"external/webrtc", tidyDefault},
100 {"frameworks/compile/mclinker/", tidyExternalVendor},
101 {"hardware/qcom", tidyExternalVendor},
102 {"vendor/", tidyExternalVendor},
103 {"vendor/google", tidyDefault},
104 {"vendor/google_devices", tidyExternalVendor},
105}
106
107var reversedDefaultLocalTidyChecks = reverseTidyChecks(DefaultLocalTidyChecks)
108
109func reverseTidyChecks(in []PathBasedTidyCheck) []PathBasedTidyCheck {
110 ret := make([]PathBasedTidyCheck, len(in))
111 for i, check := range in {
112 ret[len(in)-i-1] = check
113 }
114 return ret
115}
116
117func TidyChecksForDir(dir string) string {
118 for _, pathCheck := range reversedDefaultLocalTidyChecks {
119 if strings.HasPrefix(dir, pathCheck.PathPrefix) {
120 return pathCheck.Checks
121 }
122 }
123 return tidyDefault
124}