2016-09-27 06:45:04 +08:00
|
|
|
// Copyright 2016 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package cc
|
|
|
|
|
|
|
|
import (
|
2021-01-15 07:45:25 +08:00
|
|
|
"regexp"
|
2016-09-27 06:45:04 +08:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/google/blueprint/proptools"
|
|
|
|
|
2021-02-11 13:56:03 +08:00
|
|
|
"android/soong/android"
|
2016-09-27 06:45:04 +08:00
|
|
|
"android/soong/cc/config"
|
|
|
|
)
|
|
|
|
|
|
|
|
type TidyProperties struct {
|
|
|
|
// whether to run clang-tidy over C-like sources.
|
|
|
|
Tidy *bool
|
|
|
|
|
|
|
|
// Extra flags to pass to clang-tidy
|
|
|
|
Tidy_flags []string
|
|
|
|
|
|
|
|
// Extra checks to enable or disable in clang-tidy
|
|
|
|
Tidy_checks []string
|
2019-03-27 04:33:49 +08:00
|
|
|
|
|
|
|
// Checks that should be treated as errors.
|
|
|
|
Tidy_checks_as_errors []string
|
2016-09-27 06:45:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type tidyFeature struct {
|
|
|
|
Properties TidyProperties
|
|
|
|
}
|
|
|
|
|
2021-02-23 09:03:15 +08:00
|
|
|
var quotedFlagRegexp, _ = regexp.Compile(`^-?-[^=]+=('|").*('|")$`)
|
|
|
|
|
|
|
|
// When passing flag -name=value, if user add quotes around 'value',
|
|
|
|
// the quotation marks will be preserved by NinjaAndShellEscapeList
|
|
|
|
// and the 'value' string with quotes won't work like the intended value.
|
|
|
|
// So here we report an error if -*='*' is found.
|
|
|
|
func checkNinjaAndShellEscapeList(ctx ModuleContext, prop string, slice []string) []string {
|
|
|
|
for _, s := range slice {
|
|
|
|
if quotedFlagRegexp.MatchString(s) {
|
|
|
|
ctx.PropertyErrorf(prop, "Extra quotes in: %s", s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return proptools.NinjaAndShellEscapeList(slice)
|
|
|
|
}
|
|
|
|
|
2016-09-27 06:45:04 +08:00
|
|
|
func (tidy *tidyFeature) props() []interface{} {
|
|
|
|
return []interface{}{&tidy.Properties}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tidy *tidyFeature) begin(ctx BaseModuleContext) {
|
|
|
|
}
|
|
|
|
|
2016-12-14 09:06:13 +08:00
|
|
|
func (tidy *tidyFeature) deps(ctx DepsContext, deps Deps) Deps {
|
2016-09-27 06:45:04 +08:00
|
|
|
return deps
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tidy *tidyFeature) flags(ctx ModuleContext, flags Flags) Flags {
|
2016-12-06 09:11:06 +08:00
|
|
|
CheckBadTidyFlags(ctx, "tidy_flags", tidy.Properties.Tidy_flags)
|
|
|
|
CheckBadTidyChecks(ctx, "tidy_checks", tidy.Properties.Tidy_checks)
|
|
|
|
|
2016-09-27 06:45:04 +08:00
|
|
|
// Check if tidy is explicitly disabled for this module
|
|
|
|
if tidy.Properties.Tidy != nil && !*tidy.Properties.Tidy {
|
|
|
|
return flags
|
|
|
|
}
|
|
|
|
|
|
|
|
// If not explicitly set, check the global tidy flag
|
2017-11-29 16:27:14 +08:00
|
|
|
if tidy.Properties.Tidy == nil && !ctx.Config().ClangTidy() {
|
2016-09-27 06:45:04 +08:00
|
|
|
return flags
|
|
|
|
}
|
|
|
|
|
|
|
|
flags.Tidy = true
|
|
|
|
|
2018-09-22 06:12:44 +08:00
|
|
|
// Add global WITH_TIDY_FLAGS and local tidy_flags.
|
|
|
|
withTidyFlags := ctx.Config().Getenv("WITH_TIDY_FLAGS")
|
|
|
|
if len(withTidyFlags) > 0 {
|
|
|
|
flags.TidyFlags = append(flags.TidyFlags, withTidyFlags)
|
|
|
|
}
|
2021-02-23 09:03:15 +08:00
|
|
|
esc := checkNinjaAndShellEscapeList
|
|
|
|
flags.TidyFlags = append(flags.TidyFlags, esc(ctx, "tidy_flags", tidy.Properties.Tidy_flags)...)
|
2021-02-11 13:56:03 +08:00
|
|
|
// If TidyFlags does not contain -header-filter, add default header filter.
|
|
|
|
// Find the substring because the flag could also appear as --header-filter=...
|
|
|
|
// and with or without single or double quotes.
|
|
|
|
if !android.SubstringInList(flags.TidyFlags, "-header-filter=") {
|
|
|
|
defaultDirs := ctx.Config().Getenv("DEFAULT_TIDY_HEADER_DIRS")
|
|
|
|
headerFilter := "-header-filter="
|
|
|
|
if defaultDirs == "" {
|
|
|
|
headerFilter += ctx.ModuleDir() + "/"
|
|
|
|
} else {
|
|
|
|
headerFilter += "\"(" + ctx.ModuleDir() + "/|" + defaultDirs + ")\""
|
|
|
|
}
|
2016-09-27 06:45:04 +08:00
|
|
|
flags.TidyFlags = append(flags.TidyFlags, headerFilter)
|
|
|
|
}
|
|
|
|
|
2017-12-16 12:57:48 +08:00
|
|
|
// If clang-tidy is not enabled globally, add the -quiet flag.
|
|
|
|
if !ctx.Config().ClangTidy() {
|
|
|
|
flags.TidyFlags = append(flags.TidyFlags, "-quiet")
|
2018-01-04 17:41:16 +08:00
|
|
|
flags.TidyFlags = append(flags.TidyFlags, "-extra-arg-before=-fno-caret-diagnostics")
|
2017-12-16 12:57:48 +08:00
|
|
|
}
|
|
|
|
|
2018-05-15 07:30:46 +08:00
|
|
|
extraArgFlags := []string{
|
|
|
|
// We might be using the static analyzer through clang tidy.
|
|
|
|
// https://bugs.llvm.org/show_bug.cgi?id=32914
|
|
|
|
"-D__clang_analyzer__",
|
|
|
|
|
|
|
|
// A recent change in clang-tidy (r328258) enabled destructor inlining, which
|
|
|
|
// appears to cause a number of false positives. Until that's resolved, this turns
|
|
|
|
// off the effects of r328258.
|
|
|
|
// https://bugs.llvm.org/show_bug.cgi?id=37459
|
|
|
|
"-Xclang", "-analyzer-config", "-Xclang", "c++-temp-dtor-inlining=false",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, f := range extraArgFlags {
|
|
|
|
flags.TidyFlags = append(flags.TidyFlags, "-extra-arg-before="+f)
|
|
|
|
}
|
2017-05-04 09:13:08 +08:00
|
|
|
|
2016-09-27 06:45:04 +08:00
|
|
|
tidyChecks := "-checks="
|
2017-11-29 16:27:14 +08:00
|
|
|
if checks := ctx.Config().TidyChecks(); len(checks) > 0 {
|
2016-09-27 06:45:04 +08:00
|
|
|
tidyChecks += checks
|
|
|
|
} else {
|
|
|
|
tidyChecks += config.TidyChecksForDir(ctx.ModuleDir())
|
|
|
|
}
|
|
|
|
if len(tidy.Properties.Tidy_checks) > 0 {
|
2021-02-23 09:03:15 +08:00
|
|
|
tidyChecks = tidyChecks + "," + strings.Join(esc(ctx, "tidy_checks",
|
2020-08-15 07:53:21 +08:00
|
|
|
config.ClangRewriteTidyChecks(tidy.Properties.Tidy_checks)), ",")
|
2016-09-27 06:45:04 +08:00
|
|
|
}
|
2018-12-11 08:28:56 +08:00
|
|
|
if ctx.Windows() {
|
|
|
|
// https://b.corp.google.com/issues/120614316
|
|
|
|
// mingw32 has cert-dcl16-c warning in NO_ERROR,
|
|
|
|
// which is used in many Android files.
|
|
|
|
tidyChecks = tidyChecks + ",-cert-dcl16-c"
|
|
|
|
}
|
2020-04-09 01:42:16 +08:00
|
|
|
// https://b.corp.google.com/issues/153464409
|
|
|
|
// many local projects enable cert-* checks, which
|
|
|
|
// trigger bugprone-reserved-identifier.
|
2020-04-10 07:28:24 +08:00
|
|
|
tidyChecks = tidyChecks + ",-bugprone-reserved-identifier*,-cert-dcl51-cpp,-cert-dcl37-c"
|
2020-04-11 04:36:41 +08:00
|
|
|
// http://b/153757728
|
|
|
|
tidyChecks = tidyChecks + ",-readability-qualified-auto"
|
|
|
|
// http://b/155034563
|
|
|
|
tidyChecks = tidyChecks + ",-bugprone-signed-char-misuse"
|
|
|
|
// http://b/155034972
|
|
|
|
tidyChecks = tidyChecks + ",-bugprone-branch-clone"
|
2016-09-27 06:45:04 +08:00
|
|
|
flags.TidyFlags = append(flags.TidyFlags, tidyChecks)
|
|
|
|
|
2021-01-15 07:45:25 +08:00
|
|
|
if ctx.Config().IsEnvTrue("WITH_TIDY") {
|
|
|
|
// WITH_TIDY=1 enables clang-tidy globally. There could be many unexpected
|
|
|
|
// warnings from new checks and many local tidy_checks_as_errors and
|
|
|
|
// -warnings-as-errors can break a global build.
|
|
|
|
// So allow all clang-tidy warnings.
|
|
|
|
inserted := false
|
|
|
|
for i, s := range flags.TidyFlags {
|
|
|
|
if strings.Contains(s, "-warnings-as-errors=") {
|
|
|
|
// clang-tidy accepts only one -warnings-as-errors
|
|
|
|
// replace the old one
|
|
|
|
re := regexp.MustCompile(`'?-?-warnings-as-errors=[^ ]* *`)
|
|
|
|
newFlag := re.ReplaceAllString(s, "")
|
|
|
|
if newFlag == "" {
|
|
|
|
flags.TidyFlags[i] = "-warnings-as-errors=-*"
|
|
|
|
} else {
|
|
|
|
flags.TidyFlags[i] = newFlag + " -warnings-as-errors=-*"
|
|
|
|
}
|
|
|
|
inserted = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !inserted {
|
|
|
|
flags.TidyFlags = append(flags.TidyFlags, "-warnings-as-errors=-*")
|
|
|
|
}
|
|
|
|
} else if len(tidy.Properties.Tidy_checks_as_errors) > 0 {
|
2021-02-23 09:03:15 +08:00
|
|
|
tidyChecksAsErrors := "-warnings-as-errors=" + strings.Join(esc(ctx, "tidy_checks_as_errors", tidy.Properties.Tidy_checks_as_errors), ",")
|
2019-03-27 04:33:49 +08:00
|
|
|
flags.TidyFlags = append(flags.TidyFlags, tidyChecksAsErrors)
|
|
|
|
}
|
2016-09-27 06:45:04 +08:00
|
|
|
return flags
|
|
|
|
}
|