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	// 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.
27	pctx.VariableFunc("TidyDefaultGlobalChecks", func(ctx android.PackageVarContext) string {
28		if override := ctx.Config().Getenv("DEFAULT_GLOBAL_TIDY_CHECKS"); override != "" {
29			return override
30		}
31		return strings.Join([]string{
32			"-*",
33			"bugprone*",
34			"clang-diagnostic-unused-command-line-argument",
35			"google*",
36			"misc-macro-parentheses",
37			"performance*",
38			"-bugprone-narrowing-conversions",
39			"-google-readability*",
40			"-google-runtime-references",
41		}, ",")
42	})
43
44	// There are too many clang-tidy warnings in external and vendor projects.
45	// Enable only some google checks for these projects.
46	pctx.VariableFunc("TidyExternalVendorChecks", func(ctx android.PackageVarContext) string {
47		if override := ctx.Config().Getenv("DEFAULT_EXTERNAL_VENDOR_TIDY_CHECKS"); override != "" {
48			return override
49		}
50		return strings.Join([]string{
51			"-*",
52			"clang-diagnostic-unused-command-line-argument",
53			"google*",
54			"-google-build-using-namespace",
55			"-google-default-arguments",
56			"-google-explicit-constructor",
57			"-google-readability*",
58			"-google-runtime-int",
59			"-google-runtime-references",
60		}, ",")
61	})
62
63	// Give warnings to header files only in selected directories.
64	// Do not give warnings to external or vendor header files, which contain too
65	// many warnings.
66	pctx.VariableFunc("TidyDefaultHeaderDirs", func(ctx android.PackageVarContext) string {
67		if override := ctx.Config().Getenv("DEFAULT_TIDY_HEADER_DIRS"); override != "" {
68			return override
69		}
70		return strings.Join([]string{
71			"art/",
72			"bionic/",
73			"bootable/",
74			"build/",
75			"cts/",
76			"dalvik/",
77			"developers/",
78			"development/",
79			"frameworks/",
80			"libcore/",
81			"libnativehelper/",
82			"system/",
83		}, "|")
84	})
85
86	// Use WTIH_TIDY_FLAGS to pass extra global default clang-tidy flags.
87	pctx.VariableFunc("TidyWithTidyFlags", func(ctx android.PackageVarContext) string {
88		return ctx.Config().Getenv("WITH_TIDY_FLAGS")
89	})
90}
91
92type PathBasedTidyCheck struct {
93	PathPrefix string
94	Checks     string
95}
96
97const tidyDefault = "${config.TidyDefaultGlobalChecks}"
98const tidyExternalVendor = "${config.TidyExternalVendorChecks}"
99
100// This is a map of local path prefixes to the set of default clang-tidy checks
101// to be used.
102// The last matched local_path_prefix should be the most specific to be used.
103var DefaultLocalTidyChecks = []PathBasedTidyCheck{
104	{"external/", tidyExternalVendor},
105	{"external/google", tidyDefault},
106	{"external/webrtc", tidyDefault},
107	{"frameworks/compile/mclinker/", tidyExternalVendor},
108	{"hardware/qcom", tidyExternalVendor},
109	{"vendor/", tidyExternalVendor},
110	{"vendor/google", tidyDefault},
111	{"vendor/google_devices", tidyExternalVendor},
112}
113
114var reversedDefaultLocalTidyChecks = reverseTidyChecks(DefaultLocalTidyChecks)
115
116func reverseTidyChecks(in []PathBasedTidyCheck) []PathBasedTidyCheck {
117	ret := make([]PathBasedTidyCheck, len(in))
118	for i, check := range in {
119		ret[len(in)-i-1] = check
120	}
121	return ret
122}
123
124func TidyChecksForDir(dir string) string {
125	for _, pathCheck := range reversedDefaultLocalTidyChecks {
126		if strings.HasPrefix(dir, pathCheck.PathPrefix) {
127			return pathCheck.Checks
128		}
129	}
130	return tidyDefault
131}
132