blob: 3d1a0a0a879b215526351093f2ef579c84b37fc2 [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 Hsiehff7cff72018-07-26 17:43:12 -070025// b/111885396: -flto affected header include directory;
26// -fsanitize and -fwhole-program-vtables need -flto.
Chih-Hung Hsiehad47a272018-06-25 13:48:42 -070027var ClangTidyUnknownCflags = sorted([]string{
28 "-Wa,%",
Chih-Hung Hsieh5942fe42018-07-26 15:00:28 -070029 "-flto",
30 "-fsanitize=%",
Chih-Hung Hsiehff7cff72018-07-26 17:43:12 -070031 "-fwhole-program-vtables",
Chih-Hung Hsiehad47a272018-06-25 13:48:42 -070032})
33
Dan Willemsena03cf6d2016-09-26 15:45:04 -070034func init() {
35 // Most Android source files are not clang-tidy clean yet.
36 // Global tidy checks include only google*, performance*,
37 // and misc-macro-parentheses, but not google-readability*
38 // or google-runtime-references.
Dan Willemsen54daaf02018-03-12 13:24:09 -070039 pctx.VariableFunc("TidyDefaultGlobalChecks", func(ctx android.PackageVarContext) string {
40 if override := ctx.Config().Getenv("DEFAULT_GLOBAL_TIDY_CHECKS"); override != "" {
41 return override
Dan Willemsen318af8b2016-11-02 14:50:21 -070042 }
43 return strings.Join([]string{
44 "-*",
45 "google*",
46 "misc-macro-parentheses",
47 "performance*",
48 "-google-readability*",
49 "-google-runtime-references",
Dan Willemsen54daaf02018-03-12 13:24:09 -070050 }, ",")
Dan Willemsen318af8b2016-11-02 14:50:21 -070051 })
Dan Willemsena03cf6d2016-09-26 15:45:04 -070052
53 // There are too many clang-tidy warnings in external and vendor projects.
54 // Enable only some google checks for these projects.
Dan Willemsen54daaf02018-03-12 13:24:09 -070055 pctx.VariableFunc("TidyExternalVendorChecks", func(ctx android.PackageVarContext) string {
56 if override := ctx.Config().Getenv("DEFAULT_EXTERNAL_VENDOR_TIDY_CHECKS"); override != "" {
57 return override
Dan Willemsen318af8b2016-11-02 14:50:21 -070058 }
59 return strings.Join([]string{
60 "-*",
61 "google*",
62 "-google-build-using-namespace",
63 "-google-default-arguments",
64 "-google-explicit-constructor",
65 "-google-readability*",
66 "-google-runtime-int",
67 "-google-runtime-references",
Dan Willemsen54daaf02018-03-12 13:24:09 -070068 }, ",")
Dan Willemsen318af8b2016-11-02 14:50:21 -070069 })
Dan Willemsena03cf6d2016-09-26 15:45:04 -070070
71 // Give warnings to header files only in selected directories.
72 // Do not give warnings to external or vendor header files, which contain too
73 // many warnings.
74 pctx.StaticVariable("TidyDefaultHeaderDirs", strings.Join([]string{
75 "art/",
76 "bionic/",
77 "bootable/",
78 "build/",
79 "cts/",
80 "dalvik/",
81 "developers/",
82 "development/",
83 "frameworks/",
84 "libcore/",
85 "libnativehelper/",
86 "system/",
87 }, "|"))
88}
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}