1// 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 (
18	"android/soong/android"
19	"strings"
20)
21
22func init() {
23	// Many clang-tidy checks like altera-*, llvm-*, modernize-*
24	// are not designed for Android source code or creating too
25	// many (false-positive) warnings. The global default tidy checks
26	// should include only tested groups and exclude known noisy checks.
27	// See https://clang.llvm.org/extra/clang-tidy/checks/list.html
28	pctx.VariableFunc("TidyDefaultGlobalChecks", func(ctx android.PackageVarContext) string {
29		if override := ctx.Config().Getenv("DEFAULT_GLOBAL_TIDY_CHECKS"); override != "" {
30			return override
31		}
32		checks := strings.Join([]string{
33			"-*",
34			"android-*",
35			"bugprone-*",
36			"cert-*",
37			"clang-diagnostic-unused-command-line-argument",
38			"google-*",
39			"misc-*",
40			"performance-*",
41			"portability-*",
42			"-bugprone-narrowing-conversions",
43			"-google-readability*",
44			"-google-runtime-references",
45			"-misc-no-recursion",
46			"-misc-non-private-member-variables-in-classes",
47			"-misc-unused-parameters",
48			// the following groups are excluded by -*
49			// -altera-*
50			// -cppcoreguidelines-*
51			// -darwin-*
52			// -fuchsia-*
53			// -hicpp-*
54			// -llvm-*
55			// -llvmlibc-*
56			// -modernize-*
57			// -mpi-*
58			// -objc-*
59			// -readability-*
60			// -zircon-*
61		}, ",")
62		// clang-analyzer-* checks are too slow to be in the default for WITH_TIDY=1.
63		// nightly builds add CLANG_ANALYZER_CHECKS=1 to run those checks.
64		if ctx.Config().IsEnvTrue("CLANG_ANALYZER_CHECKS") {
65			checks += ",clang-analyzer-*"
66		}
67		return checks
68	})
69
70	// There are too many clang-tidy warnings in external and vendor projects.
71	// Enable only some google checks for these projects.
72	pctx.VariableFunc("TidyExternalVendorChecks", func(ctx android.PackageVarContext) string {
73		if override := ctx.Config().Getenv("DEFAULT_EXTERNAL_VENDOR_TIDY_CHECKS"); override != "" {
74			return override
75		}
76		return strings.Join([]string{
77			"-*",
78			"clang-diagnostic-unused-command-line-argument",
79			"google*",
80			"-google-build-using-namespace",
81			"-google-default-arguments",
82			"-google-explicit-constructor",
83			"-google-readability*",
84			"-google-runtime-int",
85			"-google-runtime-references",
86		}, ",")
87	})
88
89	// To reduce duplicate warnings from the same header files,
90	// header-filter will contain only the module directory and
91	// those specified by DEFAULT_TIDY_HEADER_DIRS.
92	pctx.VariableFunc("TidyDefaultHeaderDirs", func(ctx android.PackageVarContext) string {
93		return ctx.Config().Getenv("DEFAULT_TIDY_HEADER_DIRS")
94	})
95
96	// Use WTIH_TIDY_FLAGS to pass extra global default clang-tidy flags.
97	pctx.VariableFunc("TidyWithTidyFlags", func(ctx android.PackageVarContext) string {
98		return ctx.Config().Getenv("WITH_TIDY_FLAGS")
99	})
100}
101
102type PathBasedTidyCheck struct {
103	PathPrefix string
104	Checks     string
105}
106
107const tidyDefault = "${config.TidyDefaultGlobalChecks}"
108const tidyExternalVendor = "${config.TidyExternalVendorChecks}"
109
110// This is a map of local path prefixes to the set of default clang-tidy checks
111// to be used.
112// The last matched local_path_prefix should be the most specific to be used.
113var DefaultLocalTidyChecks = []PathBasedTidyCheck{
114	{"external/", tidyExternalVendor},
115	{"external/google", tidyDefault},
116	{"external/webrtc", tidyDefault},
117	{"frameworks/compile/mclinker/", tidyExternalVendor},
118	{"hardware/qcom", tidyExternalVendor},
119	{"vendor/", tidyExternalVendor},
120	{"vendor/google", tidyDefault},
121	{"vendor/google_devices", tidyExternalVendor},
122}
123
124var reversedDefaultLocalTidyChecks = reverseTidyChecks(DefaultLocalTidyChecks)
125
126func reverseTidyChecks(in []PathBasedTidyCheck) []PathBasedTidyCheck {
127	ret := make([]PathBasedTidyCheck, len(in))
128	for i, check := range in {
129		ret[len(in)-i-1] = check
130	}
131	return ret
132}
133
134func TidyChecksForDir(dir string) string {
135	for _, pathCheck := range reversedDefaultLocalTidyChecks {
136		if strings.HasPrefix(dir, pathCheck.PathPrefix) {
137			return pathCheck.Checks
138		}
139	}
140	return tidyDefault
141}
142