blob: 972ad7bc79d697ea6308a0f48a6e0e08752f331e [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 Hsieh1b4934a2021-01-14 15:45:25 -080018 "regexp"
Dan Willemsena03cf6d2016-09-26 15:45:04 -070019 "strings"
20
21 "github.com/google/blueprint/proptools"
22
23 "android/soong/cc/config"
24)
25
26type TidyProperties struct {
27 // whether to run clang-tidy over C-like sources.
28 Tidy *bool
29
30 // Extra flags to pass to clang-tidy
31 Tidy_flags []string
32
33 // Extra checks to enable or disable in clang-tidy
34 Tidy_checks []string
Nikita Ioffe32c49862019-03-26 20:33:49 +000035
36 // Checks that should be treated as errors.
37 Tidy_checks_as_errors []string
Dan Willemsena03cf6d2016-09-26 15:45:04 -070038}
39
40type tidyFeature struct {
41 Properties TidyProperties
42}
43
44func (tidy *tidyFeature) props() []interface{} {
45 return []interface{}{&tidy.Properties}
46}
47
48func (tidy *tidyFeature) begin(ctx BaseModuleContext) {
49}
50
Colin Cross37047f12016-12-13 17:06:13 -080051func (tidy *tidyFeature) deps(ctx DepsContext, deps Deps) Deps {
Dan Willemsena03cf6d2016-09-26 15:45:04 -070052 return deps
53}
54
55func (tidy *tidyFeature) flags(ctx ModuleContext, flags Flags) Flags {
Colin Cross379d2cb2016-12-05 17:11:06 -080056 CheckBadTidyFlags(ctx, "tidy_flags", tidy.Properties.Tidy_flags)
57 CheckBadTidyChecks(ctx, "tidy_checks", tidy.Properties.Tidy_checks)
58
Dan Willemsena03cf6d2016-09-26 15:45:04 -070059 // Check if tidy is explicitly disabled for this module
60 if tidy.Properties.Tidy != nil && !*tidy.Properties.Tidy {
61 return flags
62 }
63
64 // If not explicitly set, check the global tidy flag
Colin Cross6510f912017-11-29 00:27:14 -080065 if tidy.Properties.Tidy == nil && !ctx.Config().ClangTidy() {
Dan Willemsena03cf6d2016-09-26 15:45:04 -070066 return flags
67 }
68
Dan Willemsena03cf6d2016-09-26 15:45:04 -070069 flags.Tidy = true
70
Chih-Hung Hsieh9e5d8a62018-09-21 15:12:44 -070071 // Add global WITH_TIDY_FLAGS and local tidy_flags.
72 withTidyFlags := ctx.Config().Getenv("WITH_TIDY_FLAGS")
73 if len(withTidyFlags) > 0 {
74 flags.TidyFlags = append(flags.TidyFlags, withTidyFlags)
75 }
Colin Cross0b9f31f2019-02-28 11:00:01 -080076 esc := proptools.NinjaAndShellEscapeList
Dan Willemsena03cf6d2016-09-26 15:45:04 -070077 flags.TidyFlags = append(flags.TidyFlags, esc(tidy.Properties.Tidy_flags)...)
Chih-Hung Hsieh9e5d8a62018-09-21 15:12:44 -070078 // If TidyFlags is empty, add default header filter.
Dan Willemsena03cf6d2016-09-26 15:45:04 -070079 if len(flags.TidyFlags) == 0 {
80 headerFilter := "-header-filter=\"(" + ctx.ModuleDir() + "|${config.TidyDefaultHeaderDirs})\""
81 flags.TidyFlags = append(flags.TidyFlags, headerFilter)
82 }
83
Chih-Hung Hsiehdc0c0302017-12-15 20:57:48 -080084 // If clang-tidy is not enabled globally, add the -quiet flag.
85 if !ctx.Config().ClangTidy() {
86 flags.TidyFlags = append(flags.TidyFlags, "-quiet")
Chih-Hung Hsieh669cb912018-01-04 01:41:16 -080087 flags.TidyFlags = append(flags.TidyFlags, "-extra-arg-before=-fno-caret-diagnostics")
Chih-Hung Hsiehdc0c0302017-12-15 20:57:48 -080088 }
89
George Burgess IV030ccee2018-05-14 16:30:46 -070090 extraArgFlags := []string{
91 // We might be using the static analyzer through clang tidy.
92 // https://bugs.llvm.org/show_bug.cgi?id=32914
93 "-D__clang_analyzer__",
94
95 // A recent change in clang-tidy (r328258) enabled destructor inlining, which
96 // appears to cause a number of false positives. Until that's resolved, this turns
97 // off the effects of r328258.
98 // https://bugs.llvm.org/show_bug.cgi?id=37459
99 "-Xclang", "-analyzer-config", "-Xclang", "c++-temp-dtor-inlining=false",
100 }
101
102 for _, f := range extraArgFlags {
103 flags.TidyFlags = append(flags.TidyFlags, "-extra-arg-before="+f)
104 }
George Burgess IV561a3fe2017-05-03 18:13:08 -0700105
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700106 tidyChecks := "-checks="
Colin Cross6510f912017-11-29 00:27:14 -0800107 if checks := ctx.Config().TidyChecks(); len(checks) > 0 {
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700108 tidyChecks += checks
109 } else {
110 tidyChecks += config.TidyChecksForDir(ctx.ModuleDir())
111 }
112 if len(tidy.Properties.Tidy_checks) > 0 {
Dan Albertd12afec2020-08-14 16:53:21 -0700113 tidyChecks = tidyChecks + "," + strings.Join(esc(
114 config.ClangRewriteTidyChecks(tidy.Properties.Tidy_checks)), ",")
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700115 }
Chih-Hung Hsieh327b6f02018-12-10 16:28:56 -0800116 if ctx.Windows() {
117 // https://b.corp.google.com/issues/120614316
118 // mingw32 has cert-dcl16-c warning in NO_ERROR,
119 // which is used in many Android files.
120 tidyChecks = tidyChecks + ",-cert-dcl16-c"
121 }
Chih-Hung Hsieh3d3df822020-04-08 10:42:16 -0700122 // https://b.corp.google.com/issues/153464409
123 // many local projects enable cert-* checks, which
124 // trigger bugprone-reserved-identifier.
Yabin Cui70ba0e22020-04-09 16:28:24 -0700125 tidyChecks = tidyChecks + ",-bugprone-reserved-identifier*,-cert-dcl51-cpp,-cert-dcl37-c"
Yabin Cui8ec05ff2020-04-10 13:36:41 -0700126 // http://b/153757728
127 tidyChecks = tidyChecks + ",-readability-qualified-auto"
128 // http://b/155034563
129 tidyChecks = tidyChecks + ",-bugprone-signed-char-misuse"
130 // http://b/155034972
131 tidyChecks = tidyChecks + ",-bugprone-branch-clone"
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700132 flags.TidyFlags = append(flags.TidyFlags, tidyChecks)
133
Chih-Hung Hsieh1b4934a2021-01-14 15:45:25 -0800134 if ctx.Config().IsEnvTrue("WITH_TIDY") {
135 // WITH_TIDY=1 enables clang-tidy globally. There could be many unexpected
136 // warnings from new checks and many local tidy_checks_as_errors and
137 // -warnings-as-errors can break a global build.
138 // So allow all clang-tidy warnings.
139 inserted := false
140 for i, s := range flags.TidyFlags {
141 if strings.Contains(s, "-warnings-as-errors=") {
142 // clang-tidy accepts only one -warnings-as-errors
143 // replace the old one
144 re := regexp.MustCompile(`'?-?-warnings-as-errors=[^ ]* *`)
145 newFlag := re.ReplaceAllString(s, "")
146 if newFlag == "" {
147 flags.TidyFlags[i] = "-warnings-as-errors=-*"
148 } else {
149 flags.TidyFlags[i] = newFlag + " -warnings-as-errors=-*"
150 }
151 inserted = true
152 break
153 }
154 }
155 if !inserted {
156 flags.TidyFlags = append(flags.TidyFlags, "-warnings-as-errors=-*")
157 }
158 } else if len(tidy.Properties.Tidy_checks_as_errors) > 0 {
Nikita Ioffe32c49862019-03-26 20:33:49 +0000159 tidyChecksAsErrors := "-warnings-as-errors=" + strings.Join(esc(tidy.Properties.Tidy_checks_as_errors), ",")
160 flags.TidyFlags = append(flags.TidyFlags, tidyChecksAsErrors)
161 }
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700162 return flags
163}