• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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			"clang-diagnostic-unused-command-line-argument",
34			"google*",
35			"misc-macro-parentheses",
36			"performance*",
37			"-google-readability*",
38			"-google-runtime-references",
39		}, ",")
40	})
41
42	// There are too many clang-tidy warnings in external and vendor projects.
43	// Enable only some google checks for these projects.
44	pctx.VariableFunc("TidyExternalVendorChecks", func(ctx android.PackageVarContext) string {
45		if override := ctx.Config().Getenv("DEFAULT_EXTERNAL_VENDOR_TIDY_CHECKS"); override != "" {
46			return override
47		}
48		return strings.Join([]string{
49			"-*",
50			"clang-diagnostic-unused-command-line-argument",
51			"google*",
52			"-google-build-using-namespace",
53			"-google-default-arguments",
54			"-google-explicit-constructor",
55			"-google-readability*",
56			"-google-runtime-int",
57			"-google-runtime-references",
58		}, ",")
59	})
60
61	// Give warnings to header files only in selected directories.
62	// Do not give warnings to external or vendor header files, which contain too
63	// many warnings.
64	pctx.VariableFunc("TidyDefaultHeaderDirs", func(ctx android.PackageVarContext) string {
65		if override := ctx.Config().Getenv("DEFAULT_TIDY_HEADER_DIRS"); override != "" {
66			return override
67		}
68		return strings.Join([]string{
69			"art/",
70			"bionic/",
71			"bootable/",
72			"build/",
73			"cts/",
74			"dalvik/",
75			"developers/",
76			"development/",
77			"frameworks/",
78			"libcore/",
79			"libnativehelper/",
80			"system/",
81		}, "|")
82	})
83
84	// Use WTIH_TIDY_FLAGS to pass extra global default clang-tidy flags.
85	pctx.VariableFunc("TidyWithTidyFlags", func(ctx android.PackageVarContext) string {
86		return ctx.Config().Getenv("WITH_TIDY_FLAGS")
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}
130