blob: ff49c64bf6b2c982d532e69b29c2fb87af2256a0 [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 cc
16
17import (
Chih-Hung Hsieh80783772021-10-11 16:46:56 -070018 "path/filepath"
Chih-Hung Hsieh1b4934a2021-01-14 15:45:25 -080019 "regexp"
Dan Willemsena03cf6d2016-09-26 15:45:04 -070020 "strings"
21
22 "github.com/google/blueprint/proptools"
23
Chih-Hung Hsieh9f94c362021-02-10 21:56:03 -080024 "android/soong/android"
Dan Willemsena03cf6d2016-09-26 15:45:04 -070025 "android/soong/cc/config"
26)
27
28type TidyProperties struct {
29 // whether to run clang-tidy over C-like sources.
30 Tidy *bool
31
32 // Extra flags to pass to clang-tidy
33 Tidy_flags []string
34
35 // Extra checks to enable or disable in clang-tidy
36 Tidy_checks []string
Nikita Ioffe32c49862019-03-26 20:33:49 +000037
38 // Checks that should be treated as errors.
39 Tidy_checks_as_errors []string
Dan Willemsena03cf6d2016-09-26 15:45:04 -070040}
41
42type tidyFeature struct {
43 Properties TidyProperties
44}
45
Chih-Hung Hsieh217e09a2021-02-22 17:03:15 -080046var quotedFlagRegexp, _ = regexp.Compile(`^-?-[^=]+=('|").*('|")$`)
47
48// When passing flag -name=value, if user add quotes around 'value',
49// the quotation marks will be preserved by NinjaAndShellEscapeList
50// and the 'value' string with quotes won't work like the intended value.
51// So here we report an error if -*='*' is found.
52func checkNinjaAndShellEscapeList(ctx ModuleContext, prop string, slice []string) []string {
53 for _, s := range slice {
54 if quotedFlagRegexp.MatchString(s) {
55 ctx.PropertyErrorf(prop, "Extra quotes in: %s", s)
56 }
57 }
58 return proptools.NinjaAndShellEscapeList(slice)
59}
60
Dan Willemsena03cf6d2016-09-26 15:45:04 -070061func (tidy *tidyFeature) props() []interface{} {
62 return []interface{}{&tidy.Properties}
63}
64
Dan Willemsena03cf6d2016-09-26 15:45:04 -070065func (tidy *tidyFeature) flags(ctx ModuleContext, flags Flags) Flags {
Colin Cross379d2cb2016-12-05 17:11:06 -080066 CheckBadTidyFlags(ctx, "tidy_flags", tidy.Properties.Tidy_flags)
67 CheckBadTidyChecks(ctx, "tidy_checks", tidy.Properties.Tidy_checks)
68
Dan Willemsena03cf6d2016-09-26 15:45:04 -070069 // Check if tidy is explicitly disabled for this module
70 if tidy.Properties.Tidy != nil && !*tidy.Properties.Tidy {
71 return flags
72 }
73
Chih-Hung Hsieh7540a782022-01-08 19:56:09 -080074 // If not explicitly disabled, set flags.Tidy to generate .tidy rules.
75 // Note that libraries and binaries will depend on .tidy files ONLY if
76 // the global WITH_TIDY or module 'tidy' property is true.
Dan Willemsena03cf6d2016-09-26 15:45:04 -070077 flags.Tidy = true
78
Chih-Hung Hsieh104f51f2022-04-20 15:48:41 -070079 // If explicitly enabled, by global WITH_TIDY or local tidy:true property,
Chih-Hung Hsieh7540a782022-01-08 19:56:09 -080080 // set flags.NeedTidyFiles to make this module depend on .tidy files.
Chih-Hung Hsieh104f51f2022-04-20 15:48:41 -070081 // Note that locally set tidy:true is ignored if ALLOW_LOCAL_TIDY_TRUE is not set to true.
82 if ctx.Config().IsEnvTrue("WITH_TIDY") || (ctx.Config().IsEnvTrue("ALLOW_LOCAL_TIDY_TRUE") && Bool(tidy.Properties.Tidy)) {
Chih-Hung Hsieh7540a782022-01-08 19:56:09 -080083 flags.NeedTidyFiles = true
84 }
85
Chih-Hung Hsieh9e5d8a62018-09-21 15:12:44 -070086 // Add global WITH_TIDY_FLAGS and local tidy_flags.
87 withTidyFlags := ctx.Config().Getenv("WITH_TIDY_FLAGS")
88 if len(withTidyFlags) > 0 {
89 flags.TidyFlags = append(flags.TidyFlags, withTidyFlags)
90 }
Chih-Hung Hsieh217e09a2021-02-22 17:03:15 -080091 esc := checkNinjaAndShellEscapeList
92 flags.TidyFlags = append(flags.TidyFlags, esc(ctx, "tidy_flags", tidy.Properties.Tidy_flags)...)
Chih-Hung Hsieh9f94c362021-02-10 21:56:03 -080093 // If TidyFlags does not contain -header-filter, add default header filter.
94 // Find the substring because the flag could also appear as --header-filter=...
95 // and with or without single or double quotes.
96 if !android.SubstringInList(flags.TidyFlags, "-header-filter=") {
97 defaultDirs := ctx.Config().Getenv("DEFAULT_TIDY_HEADER_DIRS")
98 headerFilter := "-header-filter="
Chih-Hung Hsieh5fe637a2022-05-09 11:02:25 -070099 // Default header filter should include only the module directory,
100 // not the out/soong/.../ModuleDir/...
101 // Otherwise, there will be too many warnings from generated files in out/...
102 // If a module wants to see warnings in the generated source files,
103 // it should specify its own -header-filter flag.
Chih-Hung Hsieh9f94c362021-02-10 21:56:03 -0800104 if defaultDirs == "" {
Chih-Hung Hsieh5fe637a2022-05-09 11:02:25 -0700105 headerFilter += "^" + ctx.ModuleDir() + "/"
Chih-Hung Hsieh9f94c362021-02-10 21:56:03 -0800106 } else {
Chih-Hung Hsieh5fe637a2022-05-09 11:02:25 -0700107 headerFilter += "\"(^" + ctx.ModuleDir() + "/|" + defaultDirs + ")\""
Chih-Hung Hsieh9f94c362021-02-10 21:56:03 -0800108 }
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700109 flags.TidyFlags = append(flags.TidyFlags, headerFilter)
110 }
Chih-Hung Hsieh63d59eb2022-02-04 17:42:02 -0800111 // Work around RBE bug in parsing clang-tidy flags, replace "--flag" with "-flag".
112 // Some C/C++ modules added local tidy flags like --header-filter= and --extra-arg-before=.
113 doubleDash := regexp.MustCompile("^('?)--(.*)$")
114 for i, s := range flags.TidyFlags {
115 flags.TidyFlags[i] = doubleDash.ReplaceAllString(s, "$1-$2")
116 }
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700117
Chih-Hung Hsiehdc0c0302017-12-15 20:57:48 -0800118 // If clang-tidy is not enabled globally, add the -quiet flag.
119 if !ctx.Config().ClangTidy() {
120 flags.TidyFlags = append(flags.TidyFlags, "-quiet")
Chih-Hung Hsieh669cb912018-01-04 01:41:16 -0800121 flags.TidyFlags = append(flags.TidyFlags, "-extra-arg-before=-fno-caret-diagnostics")
Chih-Hung Hsiehdc0c0302017-12-15 20:57:48 -0800122 }
123
George Burgess IV030ccee2018-05-14 16:30:46 -0700124 extraArgFlags := []string{
125 // We might be using the static analyzer through clang tidy.
126 // https://bugs.llvm.org/show_bug.cgi?id=32914
127 "-D__clang_analyzer__",
128
129 // A recent change in clang-tidy (r328258) enabled destructor inlining, which
130 // appears to cause a number of false positives. Until that's resolved, this turns
131 // off the effects of r328258.
132 // https://bugs.llvm.org/show_bug.cgi?id=37459
133 "-Xclang", "-analyzer-config", "-Xclang", "c++-temp-dtor-inlining=false",
134 }
135
136 for _, f := range extraArgFlags {
137 flags.TidyFlags = append(flags.TidyFlags, "-extra-arg-before="+f)
138 }
George Burgess IV561a3fe2017-05-03 18:13:08 -0700139
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700140 tidyChecks := "-checks="
Colin Cross6510f912017-11-29 00:27:14 -0800141 if checks := ctx.Config().TidyChecks(); len(checks) > 0 {
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700142 tidyChecks += checks
143 } else {
144 tidyChecks += config.TidyChecksForDir(ctx.ModuleDir())
145 }
146 if len(tidy.Properties.Tidy_checks) > 0 {
Chih-Hung Hsieh80e3e032022-06-02 19:55:15 -0700147 // If Tidy_checks contains "-*", ignore all checks before "-*".
148 localChecks := tidy.Properties.Tidy_checks
149 ignoreGlobalChecks := false
150 for n, check := range tidy.Properties.Tidy_checks {
151 if check == "-*" {
152 ignoreGlobalChecks = true
153 localChecks = tidy.Properties.Tidy_checks[n:]
154 }
155 }
156 if ignoreGlobalChecks {
157 tidyChecks = "-checks=" + strings.Join(esc(ctx, "tidy_checks",
158 config.ClangRewriteTidyChecks(localChecks)), ",")
159 } else {
160 tidyChecks = tidyChecks + "," + strings.Join(esc(ctx, "tidy_checks",
161 config.ClangRewriteTidyChecks(localChecks)), ",")
162 }
163
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700164 }
Chih-Hung Hsieh327b6f02018-12-10 16:28:56 -0800165 if ctx.Windows() {
166 // https://b.corp.google.com/issues/120614316
167 // mingw32 has cert-dcl16-c warning in NO_ERROR,
168 // which is used in many Android files.
169 tidyChecks = tidyChecks + ",-cert-dcl16-c"
170 }
Chih-Hung Hsieh3d3df822020-04-08 10:42:16 -0700171 // https://b.corp.google.com/issues/153464409
172 // many local projects enable cert-* checks, which
173 // trigger bugprone-reserved-identifier.
Yabin Cui70ba0e22020-04-09 16:28:24 -0700174 tidyChecks = tidyChecks + ",-bugprone-reserved-identifier*,-cert-dcl51-cpp,-cert-dcl37-c"
Yabin Cui8ec05ff2020-04-10 13:36:41 -0700175 // http://b/153757728
176 tidyChecks = tidyChecks + ",-readability-qualified-auto"
177 // http://b/155034563
178 tidyChecks = tidyChecks + ",-bugprone-signed-char-misuse"
179 // http://b/155034972
180 tidyChecks = tidyChecks + ",-bugprone-branch-clone"
Yabin Cui10bf3b82021-08-10 15:42:10 +0000181 // http://b/193716442
182 tidyChecks = tidyChecks + ",-bugprone-implicit-widening-of-multiplication-result"
Yi Konge8273292021-08-31 14:04:18 +0800183 // Too many existing functions trigger this rule, and fixing it requires large code
184 // refactoring. The cost of maintaining this tidy rule outweighs the benefit it brings.
185 tidyChecks = tidyChecks + ",-bugprone-easily-swappable-parameters"
Pirama Arumuga Nainar5fc137b2022-01-25 15:42:39 -0800186 // http://b/216364337 - TODO: Follow-up after compiler update to
187 // disable or fix individual instances.
188 tidyChecks = tidyChecks + ",-cert-err33-c"
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700189 flags.TidyFlags = append(flags.TidyFlags, tidyChecks)
190
Chih-Hung Hsieh1b4934a2021-01-14 15:45:25 -0800191 if ctx.Config().IsEnvTrue("WITH_TIDY") {
192 // WITH_TIDY=1 enables clang-tidy globally. There could be many unexpected
193 // warnings from new checks and many local tidy_checks_as_errors and
194 // -warnings-as-errors can break a global build.
195 // So allow all clang-tidy warnings.
196 inserted := false
197 for i, s := range flags.TidyFlags {
198 if strings.Contains(s, "-warnings-as-errors=") {
199 // clang-tidy accepts only one -warnings-as-errors
200 // replace the old one
201 re := regexp.MustCompile(`'?-?-warnings-as-errors=[^ ]* *`)
202 newFlag := re.ReplaceAllString(s, "")
203 if newFlag == "" {
204 flags.TidyFlags[i] = "-warnings-as-errors=-*"
205 } else {
206 flags.TidyFlags[i] = newFlag + " -warnings-as-errors=-*"
207 }
208 inserted = true
209 break
210 }
211 }
212 if !inserted {
213 flags.TidyFlags = append(flags.TidyFlags, "-warnings-as-errors=-*")
214 }
215 } else if len(tidy.Properties.Tidy_checks_as_errors) > 0 {
Chih-Hung Hsieh217e09a2021-02-22 17:03:15 -0800216 tidyChecksAsErrors := "-warnings-as-errors=" + strings.Join(esc(ctx, "tidy_checks_as_errors", tidy.Properties.Tidy_checks_as_errors), ",")
Nikita Ioffe32c49862019-03-26 20:33:49 +0000217 flags.TidyFlags = append(flags.TidyFlags, tidyChecksAsErrors)
218 }
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700219 return flags
220}
Chih-Hung Hsieh80783772021-10-11 16:46:56 -0700221
222func init() {
223 android.RegisterSingletonType("tidy_phony_targets", TidyPhonySingleton)
224}
225
226// This TidyPhonySingleton generates both tidy-* and obj-* phony targets for C/C++ files.
227func TidyPhonySingleton() android.Singleton {
228 return &tidyPhonySingleton{}
229}
230
231type tidyPhonySingleton struct{}
232
233// Given a final module, add its tidy/obj phony targets to tidy/objModulesInDirGroup.
234func collectTidyObjModuleTargets(ctx android.SingletonContext, module android.Module,
235 tidyModulesInDirGroup, objModulesInDirGroup map[string]map[string]android.Paths) {
236 allObjFileGroups := make(map[string]android.Paths) // variant group name => obj file Paths
237 allTidyFileGroups := make(map[string]android.Paths) // variant group name => tidy file Paths
238 subsetObjFileGroups := make(map[string]android.Paths) // subset group name => obj file Paths
239 subsetTidyFileGroups := make(map[string]android.Paths) // subset group name => tidy file Paths
240
241 // (1) Collect all obj/tidy files into OS-specific groups.
242 ctx.VisitAllModuleVariants(module, func(variant android.Module) {
243 if ctx.Config().KatiEnabled() && android.ShouldSkipAndroidMkProcessing(variant) {
244 return
245 }
246 if m, ok := variant.(*Module); ok {
247 osName := variant.Target().Os.Name
248 addToOSGroup(osName, m.objFiles, allObjFileGroups, subsetObjFileGroups)
249 addToOSGroup(osName, m.tidyFiles, allTidyFileGroups, subsetTidyFileGroups)
250 }
251 })
252
253 // (2) Add an all-OS group, with "" or "subset" name, to include all os-specific phony targets.
254 addAllOSGroup(ctx, module, allObjFileGroups, "", "obj")
255 addAllOSGroup(ctx, module, allTidyFileGroups, "", "tidy")
256 addAllOSGroup(ctx, module, subsetObjFileGroups, "subset", "obj")
257 addAllOSGroup(ctx, module, subsetTidyFileGroups, "subset", "tidy")
258
259 tidyTargetGroups := make(map[string]android.Path)
260 objTargetGroups := make(map[string]android.Path)
261 genObjTidyPhonyTargets(ctx, module, "obj", allObjFileGroups, objTargetGroups)
262 genObjTidyPhonyTargets(ctx, module, "obj", subsetObjFileGroups, objTargetGroups)
263 genObjTidyPhonyTargets(ctx, module, "tidy", allTidyFileGroups, tidyTargetGroups)
264 genObjTidyPhonyTargets(ctx, module, "tidy", subsetTidyFileGroups, tidyTargetGroups)
265
266 moduleDir := ctx.ModuleDir(module)
267 appendToModulesInDirGroup(tidyTargetGroups, moduleDir, tidyModulesInDirGroup)
268 appendToModulesInDirGroup(objTargetGroups, moduleDir, objModulesInDirGroup)
269}
270
271func (m *tidyPhonySingleton) GenerateBuildActions(ctx android.SingletonContext) {
272 // For tidy-* directory phony targets, there are different variant groups.
273 // tidyModulesInDirGroup[G][D] is for group G, directory D, with Paths
274 // of all phony targets to be included into direct dependents of tidy-D_G.
275 tidyModulesInDirGroup := make(map[string]map[string]android.Paths)
276 // Also for obj-* directory phony targets.
277 objModulesInDirGroup := make(map[string]map[string]android.Paths)
278
279 // Collect tidy/obj targets from the 'final' modules.
280 ctx.VisitAllModules(func(module android.Module) {
281 if module == ctx.FinalModule(module) {
282 collectTidyObjModuleTargets(ctx, module, tidyModulesInDirGroup, objModulesInDirGroup)
283 }
284 })
285
286 suffix := ""
287 if ctx.Config().KatiEnabled() {
288 suffix = "-soong"
289 }
290 generateObjTidyPhonyTargets(ctx, suffix, "obj", objModulesInDirGroup)
291 generateObjTidyPhonyTargets(ctx, suffix, "tidy", tidyModulesInDirGroup)
292}
293
294// The name for an obj/tidy module variant group phony target is Name_group-obj/tidy,
295func objTidyModuleGroupName(module android.Module, group string, suffix string) string {
296 if group == "" {
297 return module.Name() + "-" + suffix
298 }
299 return module.Name() + "_" + group + "-" + suffix
300}
301
302// Generate obj-* or tidy-* phony targets.
303func generateObjTidyPhonyTargets(ctx android.SingletonContext, suffix string, prefix string, objTidyModulesInDirGroup map[string]map[string]android.Paths) {
304 // For each variant group, create a <prefix>-<directory>_group target that
305 // depends on all subdirectories and modules in the directory.
306 for group, modulesInDir := range objTidyModulesInDirGroup {
307 groupSuffix := ""
308 if group != "" {
309 groupSuffix = "_" + group
310 }
311 mmTarget := func(dir string) string {
312 return prefix + "-" + strings.Replace(filepath.Clean(dir), "/", "-", -1) + groupSuffix
313 }
314 dirs, topDirs := android.AddAncestors(ctx, modulesInDir, mmTarget)
315 // Create a <prefix>-soong_group target that depends on all <prefix>-dir_group of top level dirs.
316 var topDirPaths android.Paths
317 for _, dir := range topDirs {
318 topDirPaths = append(topDirPaths, android.PathForPhony(ctx, mmTarget(dir)))
319 }
320 ctx.Phony(prefix+suffix+groupSuffix, topDirPaths...)
321 // Create a <prefix>-dir_group target that depends on all targets in modulesInDir[dir]
322 for _, dir := range dirs {
323 if dir != "." && dir != "" {
324 ctx.Phony(mmTarget(dir), modulesInDir[dir]...)
325 }
326 }
327 }
328}
329
330// Append (obj|tidy)TargetGroups[group] into (obj|tidy)ModulesInDirGroups[group][moduleDir].
331func appendToModulesInDirGroup(targetGroups map[string]android.Path, moduleDir string, modulesInDirGroup map[string]map[string]android.Paths) {
332 for group, phonyPath := range targetGroups {
333 if _, found := modulesInDirGroup[group]; !found {
334 modulesInDirGroup[group] = make(map[string]android.Paths)
335 }
336 modulesInDirGroup[group][moduleDir] = append(modulesInDirGroup[group][moduleDir], phonyPath)
337 }
338}
339
340// Add given files to the OS group and subset group.
341func addToOSGroup(osName string, files android.Paths, allGroups, subsetGroups map[string]android.Paths) {
342 if len(files) > 0 {
343 subsetName := osName + "_subset"
344 allGroups[osName] = append(allGroups[osName], files...)
345 // Now include only the first variant in the subsetGroups.
346 // If clang and clang-tidy get faster, we might include more variants.
347 if _, found := subsetGroups[subsetName]; !found {
348 subsetGroups[subsetName] = files
349 }
350 }
351}
352
353// Add an all-OS group, with groupName, to include all os-specific phony targets.
354func addAllOSGroup(ctx android.SingletonContext, module android.Module, phonyTargetGroups map[string]android.Paths, groupName string, objTidyName string) {
355 if len(phonyTargetGroups) > 0 {
356 var targets android.Paths
357 for group, _ := range phonyTargetGroups {
358 targets = append(targets, android.PathForPhony(ctx, objTidyModuleGroupName(module, group, objTidyName)))
359 }
360 phonyTargetGroups[groupName] = targets
361 }
362}
363
364// Create one phony targets for each group and add them to the targetGroups.
365func genObjTidyPhonyTargets(ctx android.SingletonContext, module android.Module, objTidyName string, fileGroups map[string]android.Paths, targetGroups map[string]android.Path) {
366 for group, files := range fileGroups {
367 groupName := objTidyModuleGroupName(module, group, objTidyName)
368 ctx.Phony(groupName, files...)
369 targetGroups[group] = android.PathForPhony(ctx, groupName)
370 }
371}