From d12afec49c18e99ad2755e8a123feb1efcbfb409 Mon Sep 17 00:00:00 2001 From: Dan Albert Date: Fri, 14 Aug 2020 16:53:21 -0700 Subject: [PATCH] Allow globally disabling some clang-tidy checks. Test: used for the upcoming compiler update Bug: None Change-Id: Id17db2c48fa3e165da81a1d084827bde142406dd --- cc/config/clang.go | 42 +++++++++++++++++++++++++++++------------- cc/tidy.go | 3 ++- 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/cc/config/clang.go b/cc/config/clang.go index 24dc6b932..7db405c2e 100644 --- a/cc/config/clang.go +++ b/cc/config/clang.go @@ -15,6 +15,7 @@ package config import ( + "android/soong/android" "sort" "strings" ) @@ -88,6 +89,12 @@ var ClangUnknownLldflags = sorted([]string{ var ClangLibToolingUnknownCflags = sorted([]string{}) +// List of tidy checks that should be disabled globally. When the compiler is +// updated, some checks enabled by this module may be disabled if they have +// become more strict, or if they are a new match for a wildcard group like +// `modernize-*`. +var ClangTidyDisableChecks = []string{} + func init() { pctx.StaticVariable("ClangExtraCflags", strings.Join([]string{ "-D__compiler_offsetof=__builtin_offsetof", @@ -202,25 +209,34 @@ func init() { } func ClangFilterUnknownCflags(cflags []string) []string { - ret := make([]string, 0, len(cflags)) - for _, f := range cflags { - if !inListSorted(f, ClangUnknownCflags) { - ret = append(ret, f) + result, _ := android.FilterList(cflags, ClangUnknownCflags) + return result +} + +func clangTidyNegateChecks(checks []string) []string { + ret := make([]string, 0, len(checks)) + for _, c := range checks { + if strings.HasPrefix(c, "-") { + ret = append(ret, c) + } else { + ret = append(ret, "-"+c) } } - return ret } -func ClangFilterUnknownLldflags(lldflags []string) []string { - ret := make([]string, 0, len(lldflags)) - for _, f := range lldflags { - if !inListSorted(f, ClangUnknownLldflags) { - ret = append(ret, f) - } - } +func ClangRewriteTidyChecks(checks []string) []string { + checks = append(checks, clangTidyNegateChecks(ClangTidyDisableChecks)...) + // clang-tidy does not allow later arguments to override earlier arguments, + // so if we just disabled an argument that was explicitly enabled we must + // remove the enabling argument from the list. + result, _ := android.FilterList(checks, ClangTidyDisableChecks) + return result +} - return ret +func ClangFilterUnknownLldflags(lldflags []string) []string { + result, _ := android.FilterList(lldflags, ClangUnknownLldflags) + return result } func inListSorted(s string, list []string) bool { diff --git a/cc/tidy.go b/cc/tidy.go index 364e56c5d..17471e674 100644 --- a/cc/tidy.go +++ b/cc/tidy.go @@ -109,7 +109,8 @@ func (tidy *tidyFeature) flags(ctx ModuleContext, flags Flags) Flags { tidyChecks += config.TidyChecksForDir(ctx.ModuleDir()) } if len(tidy.Properties.Tidy_checks) > 0 { - tidyChecks = tidyChecks + "," + strings.Join(esc(tidy.Properties.Tidy_checks), ",") + tidyChecks = tidyChecks + "," + strings.Join(esc( + config.ClangRewriteTidyChecks(tidy.Properties.Tidy_checks)), ",") } if ctx.Windows() { // https://b.corp.google.com/issues/120614316