tidy.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. package cc
  15. import (
  16. "path/filepath"
  17. "regexp"
  18. "strings"
  19. "github.com/google/blueprint/proptools"
  20. "android/soong/android"
  21. "android/soong/cc/config"
  22. )
  23. type TidyProperties struct {
  24. // whether to run clang-tidy over C-like sources.
  25. Tidy *bool
  26. // Extra flags to pass to clang-tidy
  27. Tidy_flags []string
  28. // Extra checks to enable or disable in clang-tidy
  29. Tidy_checks []string
  30. // Checks that should be treated as errors.
  31. Tidy_checks_as_errors []string
  32. }
  33. type tidyFeature struct {
  34. Properties TidyProperties
  35. }
  36. var quotedFlagRegexp, _ = regexp.Compile(`^-?-[^=]+=('|").*('|")$`)
  37. // When passing flag -name=value, if user add quotes around 'value',
  38. // the quotation marks will be preserved by NinjaAndShellEscapeList
  39. // and the 'value' string with quotes won't work like the intended value.
  40. // So here we report an error if -*='*' is found.
  41. func checkNinjaAndShellEscapeList(ctx ModuleContext, prop string, slice []string) []string {
  42. for _, s := range slice {
  43. if quotedFlagRegexp.MatchString(s) {
  44. ctx.PropertyErrorf(prop, "Extra quotes in: %s", s)
  45. }
  46. }
  47. return proptools.NinjaAndShellEscapeList(slice)
  48. }
  49. func (tidy *tidyFeature) props() []interface{} {
  50. return []interface{}{&tidy.Properties}
  51. }
  52. func (tidy *tidyFeature) flags(ctx ModuleContext, flags Flags) Flags {
  53. CheckBadTidyFlags(ctx, "tidy_flags", tidy.Properties.Tidy_flags)
  54. CheckBadTidyChecks(ctx, "tidy_checks", tidy.Properties.Tidy_checks)
  55. // Check if tidy is explicitly disabled for this module
  56. if tidy.Properties.Tidy != nil && !*tidy.Properties.Tidy {
  57. return flags
  58. }
  59. // If not explicitly disabled, set flags.Tidy to generate .tidy rules.
  60. // Note that libraries and binaries will depend on .tidy files ONLY if
  61. // the global WITH_TIDY or module 'tidy' property is true.
  62. flags.Tidy = true
  63. // If explicitly enabled, by global default or local tidy property,
  64. // set flags.NeedTidyFiles to make this module depend on .tidy files.
  65. if ctx.Config().ClangTidy() || Bool(tidy.Properties.Tidy) {
  66. flags.NeedTidyFiles = true
  67. }
  68. // Add global WITH_TIDY_FLAGS and local tidy_flags.
  69. withTidyFlags := ctx.Config().Getenv("WITH_TIDY_FLAGS")
  70. if len(withTidyFlags) > 0 {
  71. flags.TidyFlags = append(flags.TidyFlags, withTidyFlags)
  72. }
  73. esc := checkNinjaAndShellEscapeList
  74. flags.TidyFlags = append(flags.TidyFlags, esc(ctx, "tidy_flags", tidy.Properties.Tidy_flags)...)
  75. // If TidyFlags does not contain -header-filter, add default header filter.
  76. // Find the substring because the flag could also appear as --header-filter=...
  77. // and with or without single or double quotes.
  78. if !android.SubstringInList(flags.TidyFlags, "-header-filter=") {
  79. defaultDirs := ctx.Config().Getenv("DEFAULT_TIDY_HEADER_DIRS")
  80. headerFilter := "-header-filter="
  81. if defaultDirs == "" {
  82. headerFilter += ctx.ModuleDir() + "/"
  83. } else {
  84. headerFilter += "\"(" + ctx.ModuleDir() + "/|" + defaultDirs + ")\""
  85. }
  86. flags.TidyFlags = append(flags.TidyFlags, headerFilter)
  87. }
  88. // Work around RBE bug in parsing clang-tidy flags, replace "--flag" with "-flag".
  89. // Some C/C++ modules added local tidy flags like --header-filter= and --extra-arg-before=.
  90. doubleDash := regexp.MustCompile("^('?)--(.*)$")
  91. for i, s := range flags.TidyFlags {
  92. flags.TidyFlags[i] = doubleDash.ReplaceAllString(s, "$1-$2")
  93. }
  94. // If clang-tidy is not enabled globally, add the -quiet flag.
  95. if !ctx.Config().ClangTidy() {
  96. flags.TidyFlags = append(flags.TidyFlags, "-quiet")
  97. flags.TidyFlags = append(flags.TidyFlags, "-extra-arg-before=-fno-caret-diagnostics")
  98. }
  99. extraArgFlags := []string{
  100. // We might be using the static analyzer through clang tidy.
  101. // https://bugs.llvm.org/show_bug.cgi?id=32914
  102. "-D__clang_analyzer__",
  103. // A recent change in clang-tidy (r328258) enabled destructor inlining, which
  104. // appears to cause a number of false positives. Until that's resolved, this turns
  105. // off the effects of r328258.
  106. // https://bugs.llvm.org/show_bug.cgi?id=37459
  107. "-Xclang", "-analyzer-config", "-Xclang", "c++-temp-dtor-inlining=false",
  108. }
  109. for _, f := range extraArgFlags {
  110. flags.TidyFlags = append(flags.TidyFlags, "-extra-arg-before="+f)
  111. }
  112. tidyChecks := "-checks="
  113. if checks := ctx.Config().TidyChecks(); len(checks) > 0 {
  114. tidyChecks += checks
  115. } else {
  116. tidyChecks += config.TidyChecksForDir(ctx.ModuleDir())
  117. }
  118. if len(tidy.Properties.Tidy_checks) > 0 {
  119. tidyChecks = tidyChecks + "," + strings.Join(esc(ctx, "tidy_checks",
  120. config.ClangRewriteTidyChecks(tidy.Properties.Tidy_checks)), ",")
  121. }
  122. if ctx.Windows() {
  123. // https://b.corp.google.com/issues/120614316
  124. // mingw32 has cert-dcl16-c warning in NO_ERROR,
  125. // which is used in many Android files.
  126. tidyChecks = tidyChecks + ",-cert-dcl16-c"
  127. }
  128. // https://b.corp.google.com/issues/153464409
  129. // many local projects enable cert-* checks, which
  130. // trigger bugprone-reserved-identifier.
  131. tidyChecks = tidyChecks + ",-bugprone-reserved-identifier*,-cert-dcl51-cpp,-cert-dcl37-c"
  132. // http://b/153757728
  133. tidyChecks = tidyChecks + ",-readability-qualified-auto"
  134. // http://b/155034563
  135. tidyChecks = tidyChecks + ",-bugprone-signed-char-misuse"
  136. // http://b/155034972
  137. tidyChecks = tidyChecks + ",-bugprone-branch-clone"
  138. // http://b/193716442
  139. tidyChecks = tidyChecks + ",-bugprone-implicit-widening-of-multiplication-result"
  140. // Too many existing functions trigger this rule, and fixing it requires large code
  141. // refactoring. The cost of maintaining this tidy rule outweighs the benefit it brings.
  142. tidyChecks = tidyChecks + ",-bugprone-easily-swappable-parameters"
  143. // http://b/216364337 - TODO: Follow-up after compiler update to
  144. // disable or fix individual instances.
  145. tidyChecks = tidyChecks + ",-cert-err33-c"
  146. flags.TidyFlags = append(flags.TidyFlags, tidyChecks)
  147. if ctx.Config().IsEnvTrue("WITH_TIDY") {
  148. // WITH_TIDY=1 enables clang-tidy globally. There could be many unexpected
  149. // warnings from new checks and many local tidy_checks_as_errors and
  150. // -warnings-as-errors can break a global build.
  151. // So allow all clang-tidy warnings.
  152. inserted := false
  153. for i, s := range flags.TidyFlags {
  154. if strings.Contains(s, "-warnings-as-errors=") {
  155. // clang-tidy accepts only one -warnings-as-errors
  156. // replace the old one
  157. re := regexp.MustCompile(`'?-?-warnings-as-errors=[^ ]* *`)
  158. newFlag := re.ReplaceAllString(s, "")
  159. if newFlag == "" {
  160. flags.TidyFlags[i] = "-warnings-as-errors=-*"
  161. } else {
  162. flags.TidyFlags[i] = newFlag + " -warnings-as-errors=-*"
  163. }
  164. inserted = true
  165. break
  166. }
  167. }
  168. if !inserted {
  169. flags.TidyFlags = append(flags.TidyFlags, "-warnings-as-errors=-*")
  170. }
  171. } else if len(tidy.Properties.Tidy_checks_as_errors) > 0 {
  172. tidyChecksAsErrors := "-warnings-as-errors=" + strings.Join(esc(ctx, "tidy_checks_as_errors", tidy.Properties.Tidy_checks_as_errors), ",")
  173. flags.TidyFlags = append(flags.TidyFlags, tidyChecksAsErrors)
  174. }
  175. return flags
  176. }
  177. func init() {
  178. android.RegisterSingletonType("tidy_phony_targets", TidyPhonySingleton)
  179. }
  180. // This TidyPhonySingleton generates both tidy-* and obj-* phony targets for C/C++ files.
  181. func TidyPhonySingleton() android.Singleton {
  182. return &tidyPhonySingleton{}
  183. }
  184. type tidyPhonySingleton struct{}
  185. // Given a final module, add its tidy/obj phony targets to tidy/objModulesInDirGroup.
  186. func collectTidyObjModuleTargets(ctx android.SingletonContext, module android.Module,
  187. tidyModulesInDirGroup, objModulesInDirGroup map[string]map[string]android.Paths) {
  188. allObjFileGroups := make(map[string]android.Paths) // variant group name => obj file Paths
  189. allTidyFileGroups := make(map[string]android.Paths) // variant group name => tidy file Paths
  190. subsetObjFileGroups := make(map[string]android.Paths) // subset group name => obj file Paths
  191. subsetTidyFileGroups := make(map[string]android.Paths) // subset group name => tidy file Paths
  192. // (1) Collect all obj/tidy files into OS-specific groups.
  193. ctx.VisitAllModuleVariants(module, func(variant android.Module) {
  194. if ctx.Config().KatiEnabled() && android.ShouldSkipAndroidMkProcessing(variant) {
  195. return
  196. }
  197. if m, ok := variant.(*Module); ok {
  198. osName := variant.Target().Os.Name
  199. addToOSGroup(osName, m.objFiles, allObjFileGroups, subsetObjFileGroups)
  200. addToOSGroup(osName, m.tidyFiles, allTidyFileGroups, subsetTidyFileGroups)
  201. }
  202. })
  203. // (2) Add an all-OS group, with "" or "subset" name, to include all os-specific phony targets.
  204. addAllOSGroup(ctx, module, allObjFileGroups, "", "obj")
  205. addAllOSGroup(ctx, module, allTidyFileGroups, "", "tidy")
  206. addAllOSGroup(ctx, module, subsetObjFileGroups, "subset", "obj")
  207. addAllOSGroup(ctx, module, subsetTidyFileGroups, "subset", "tidy")
  208. tidyTargetGroups := make(map[string]android.Path)
  209. objTargetGroups := make(map[string]android.Path)
  210. genObjTidyPhonyTargets(ctx, module, "obj", allObjFileGroups, objTargetGroups)
  211. genObjTidyPhonyTargets(ctx, module, "obj", subsetObjFileGroups, objTargetGroups)
  212. genObjTidyPhonyTargets(ctx, module, "tidy", allTidyFileGroups, tidyTargetGroups)
  213. genObjTidyPhonyTargets(ctx, module, "tidy", subsetTidyFileGroups, tidyTargetGroups)
  214. moduleDir := ctx.ModuleDir(module)
  215. appendToModulesInDirGroup(tidyTargetGroups, moduleDir, tidyModulesInDirGroup)
  216. appendToModulesInDirGroup(objTargetGroups, moduleDir, objModulesInDirGroup)
  217. }
  218. func (m *tidyPhonySingleton) GenerateBuildActions(ctx android.SingletonContext) {
  219. // For tidy-* directory phony targets, there are different variant groups.
  220. // tidyModulesInDirGroup[G][D] is for group G, directory D, with Paths
  221. // of all phony targets to be included into direct dependents of tidy-D_G.
  222. tidyModulesInDirGroup := make(map[string]map[string]android.Paths)
  223. // Also for obj-* directory phony targets.
  224. objModulesInDirGroup := make(map[string]map[string]android.Paths)
  225. // Collect tidy/obj targets from the 'final' modules.
  226. ctx.VisitAllModules(func(module android.Module) {
  227. if module == ctx.FinalModule(module) {
  228. collectTidyObjModuleTargets(ctx, module, tidyModulesInDirGroup, objModulesInDirGroup)
  229. }
  230. })
  231. suffix := ""
  232. if ctx.Config().KatiEnabled() {
  233. suffix = "-soong"
  234. }
  235. generateObjTidyPhonyTargets(ctx, suffix, "obj", objModulesInDirGroup)
  236. generateObjTidyPhonyTargets(ctx, suffix, "tidy", tidyModulesInDirGroup)
  237. }
  238. // The name for an obj/tidy module variant group phony target is Name_group-obj/tidy,
  239. func objTidyModuleGroupName(module android.Module, group string, suffix string) string {
  240. if group == "" {
  241. return module.Name() + "-" + suffix
  242. }
  243. return module.Name() + "_" + group + "-" + suffix
  244. }
  245. // Generate obj-* or tidy-* phony targets.
  246. func generateObjTidyPhonyTargets(ctx android.SingletonContext, suffix string, prefix string, objTidyModulesInDirGroup map[string]map[string]android.Paths) {
  247. // For each variant group, create a <prefix>-<directory>_group target that
  248. // depends on all subdirectories and modules in the directory.
  249. for group, modulesInDir := range objTidyModulesInDirGroup {
  250. groupSuffix := ""
  251. if group != "" {
  252. groupSuffix = "_" + group
  253. }
  254. mmTarget := func(dir string) string {
  255. return prefix + "-" + strings.Replace(filepath.Clean(dir), "/", "-", -1) + groupSuffix
  256. }
  257. dirs, topDirs := android.AddAncestors(ctx, modulesInDir, mmTarget)
  258. // Create a <prefix>-soong_group target that depends on all <prefix>-dir_group of top level dirs.
  259. var topDirPaths android.Paths
  260. for _, dir := range topDirs {
  261. topDirPaths = append(topDirPaths, android.PathForPhony(ctx, mmTarget(dir)))
  262. }
  263. ctx.Phony(prefix+suffix+groupSuffix, topDirPaths...)
  264. // Create a <prefix>-dir_group target that depends on all targets in modulesInDir[dir]
  265. for _, dir := range dirs {
  266. if dir != "." && dir != "" {
  267. ctx.Phony(mmTarget(dir), modulesInDir[dir]...)
  268. }
  269. }
  270. }
  271. }
  272. // Append (obj|tidy)TargetGroups[group] into (obj|tidy)ModulesInDirGroups[group][moduleDir].
  273. func appendToModulesInDirGroup(targetGroups map[string]android.Path, moduleDir string, modulesInDirGroup map[string]map[string]android.Paths) {
  274. for group, phonyPath := range targetGroups {
  275. if _, found := modulesInDirGroup[group]; !found {
  276. modulesInDirGroup[group] = make(map[string]android.Paths)
  277. }
  278. modulesInDirGroup[group][moduleDir] = append(modulesInDirGroup[group][moduleDir], phonyPath)
  279. }
  280. }
  281. // Add given files to the OS group and subset group.
  282. func addToOSGroup(osName string, files android.Paths, allGroups, subsetGroups map[string]android.Paths) {
  283. if len(files) > 0 {
  284. subsetName := osName + "_subset"
  285. allGroups[osName] = append(allGroups[osName], files...)
  286. // Now include only the first variant in the subsetGroups.
  287. // If clang and clang-tidy get faster, we might include more variants.
  288. if _, found := subsetGroups[subsetName]; !found {
  289. subsetGroups[subsetName] = files
  290. }
  291. }
  292. }
  293. // Add an all-OS group, with groupName, to include all os-specific phony targets.
  294. func addAllOSGroup(ctx android.SingletonContext, module android.Module, phonyTargetGroups map[string]android.Paths, groupName string, objTidyName string) {
  295. if len(phonyTargetGroups) > 0 {
  296. var targets android.Paths
  297. for group, _ := range phonyTargetGroups {
  298. targets = append(targets, android.PathForPhony(ctx, objTidyModuleGroupName(module, group, objTidyName)))
  299. }
  300. phonyTargetGroups[groupName] = targets
  301. }
  302. }
  303. // Create one phony targets for each group and add them to the targetGroups.
  304. func genObjTidyPhonyTargets(ctx android.SingletonContext, module android.Module, objTidyName string, fileGroups map[string]android.Paths, targetGroups map[string]android.Path) {
  305. for group, files := range fileGroups {
  306. groupName := objTidyModuleGroupName(module, group, objTidyName)
  307. ctx.Phony(groupName, files...)
  308. targetGroups[group] = android.PathForPhony(ctx, groupName)
  309. }
  310. }