Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 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 | |
| 15 | package cc |
| 16 | |
| 17 | import ( |
Chih-Hung Hsieh | 1b4934a | 2021-01-14 15:45:25 -0800 | [diff] [blame] | 18 | "regexp" |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 19 | "strings" |
| 20 | |
| 21 | "github.com/google/blueprint/proptools" |
| 22 | |
Chih-Hung Hsieh | 9f94c36 | 2021-02-10 21:56:03 -0800 | [diff] [blame] | 23 | "android/soong/android" |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 24 | "android/soong/cc/config" |
| 25 | ) |
| 26 | |
| 27 | type TidyProperties struct { |
| 28 | // whether to run clang-tidy over C-like sources. |
| 29 | Tidy *bool |
| 30 | |
| 31 | // Extra flags to pass to clang-tidy |
| 32 | Tidy_flags []string |
| 33 | |
| 34 | // Extra checks to enable or disable in clang-tidy |
| 35 | Tidy_checks []string |
Nikita Ioffe | 32c4986 | 2019-03-26 20:33:49 +0000 | [diff] [blame] | 36 | |
| 37 | // Checks that should be treated as errors. |
| 38 | Tidy_checks_as_errors []string |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | type tidyFeature struct { |
| 42 | Properties TidyProperties |
| 43 | } |
| 44 | |
Chih-Hung Hsieh | 217e09a | 2021-02-22 17:03:15 -0800 | [diff] [blame] | 45 | var quotedFlagRegexp, _ = regexp.Compile(`^-?-[^=]+=('|").*('|")$`) |
| 46 | |
| 47 | // When passing flag -name=value, if user add quotes around 'value', |
| 48 | // the quotation marks will be preserved by NinjaAndShellEscapeList |
| 49 | // and the 'value' string with quotes won't work like the intended value. |
| 50 | // So here we report an error if -*='*' is found. |
| 51 | func checkNinjaAndShellEscapeList(ctx ModuleContext, prop string, slice []string) []string { |
| 52 | for _, s := range slice { |
| 53 | if quotedFlagRegexp.MatchString(s) { |
| 54 | ctx.PropertyErrorf(prop, "Extra quotes in: %s", s) |
| 55 | } |
| 56 | } |
| 57 | return proptools.NinjaAndShellEscapeList(slice) |
| 58 | } |
| 59 | |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 60 | func (tidy *tidyFeature) props() []interface{} { |
| 61 | return []interface{}{&tidy.Properties} |
| 62 | } |
| 63 | |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 64 | func (tidy *tidyFeature) flags(ctx ModuleContext, flags Flags) Flags { |
Colin Cross | 379d2cb | 2016-12-05 17:11:06 -0800 | [diff] [blame] | 65 | CheckBadTidyFlags(ctx, "tidy_flags", tidy.Properties.Tidy_flags) |
| 66 | CheckBadTidyChecks(ctx, "tidy_checks", tidy.Properties.Tidy_checks) |
| 67 | |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 68 | // Check if tidy is explicitly disabled for this module |
| 69 | if tidy.Properties.Tidy != nil && !*tidy.Properties.Tidy { |
| 70 | return flags |
| 71 | } |
| 72 | |
| 73 | // If not explicitly set, check the global tidy flag |
Colin Cross | 6510f91 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 74 | if tidy.Properties.Tidy == nil && !ctx.Config().ClangTidy() { |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 75 | return flags |
| 76 | } |
| 77 | |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 78 | flags.Tidy = true |
| 79 | |
Chih-Hung Hsieh | 9e5d8a6 | 2018-09-21 15:12:44 -0700 | [diff] [blame] | 80 | // Add global WITH_TIDY_FLAGS and local tidy_flags. |
| 81 | withTidyFlags := ctx.Config().Getenv("WITH_TIDY_FLAGS") |
| 82 | if len(withTidyFlags) > 0 { |
| 83 | flags.TidyFlags = append(flags.TidyFlags, withTidyFlags) |
| 84 | } |
Chih-Hung Hsieh | 217e09a | 2021-02-22 17:03:15 -0800 | [diff] [blame] | 85 | esc := checkNinjaAndShellEscapeList |
| 86 | flags.TidyFlags = append(flags.TidyFlags, esc(ctx, "tidy_flags", tidy.Properties.Tidy_flags)...) |
Chih-Hung Hsieh | 9f94c36 | 2021-02-10 21:56:03 -0800 | [diff] [blame] | 87 | // If TidyFlags does not contain -header-filter, add default header filter. |
| 88 | // Find the substring because the flag could also appear as --header-filter=... |
| 89 | // and with or without single or double quotes. |
| 90 | if !android.SubstringInList(flags.TidyFlags, "-header-filter=") { |
| 91 | defaultDirs := ctx.Config().Getenv("DEFAULT_TIDY_HEADER_DIRS") |
| 92 | headerFilter := "-header-filter=" |
| 93 | if defaultDirs == "" { |
| 94 | headerFilter += ctx.ModuleDir() + "/" |
| 95 | } else { |
| 96 | headerFilter += "\"(" + ctx.ModuleDir() + "/|" + defaultDirs + ")\"" |
| 97 | } |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 98 | flags.TidyFlags = append(flags.TidyFlags, headerFilter) |
| 99 | } |
| 100 | |
Chih-Hung Hsieh | dc0c030 | 2017-12-15 20:57:48 -0800 | [diff] [blame] | 101 | // If clang-tidy is not enabled globally, add the -quiet flag. |
| 102 | if !ctx.Config().ClangTidy() { |
| 103 | flags.TidyFlags = append(flags.TidyFlags, "-quiet") |
Chih-Hung Hsieh | 669cb91 | 2018-01-04 01:41:16 -0800 | [diff] [blame] | 104 | flags.TidyFlags = append(flags.TidyFlags, "-extra-arg-before=-fno-caret-diagnostics") |
Chih-Hung Hsieh | dc0c030 | 2017-12-15 20:57:48 -0800 | [diff] [blame] | 105 | } |
| 106 | |
George Burgess IV | 030ccee | 2018-05-14 16:30:46 -0700 | [diff] [blame] | 107 | extraArgFlags := []string{ |
| 108 | // We might be using the static analyzer through clang tidy. |
| 109 | // https://bugs.llvm.org/show_bug.cgi?id=32914 |
| 110 | "-D__clang_analyzer__", |
| 111 | |
| 112 | // A recent change in clang-tidy (r328258) enabled destructor inlining, which |
| 113 | // appears to cause a number of false positives. Until that's resolved, this turns |
| 114 | // off the effects of r328258. |
| 115 | // https://bugs.llvm.org/show_bug.cgi?id=37459 |
| 116 | "-Xclang", "-analyzer-config", "-Xclang", "c++-temp-dtor-inlining=false", |
| 117 | } |
| 118 | |
| 119 | for _, f := range extraArgFlags { |
| 120 | flags.TidyFlags = append(flags.TidyFlags, "-extra-arg-before="+f) |
| 121 | } |
George Burgess IV | 561a3fe | 2017-05-03 18:13:08 -0700 | [diff] [blame] | 122 | |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 123 | tidyChecks := "-checks=" |
Colin Cross | 6510f91 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 124 | if checks := ctx.Config().TidyChecks(); len(checks) > 0 { |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 125 | tidyChecks += checks |
| 126 | } else { |
| 127 | tidyChecks += config.TidyChecksForDir(ctx.ModuleDir()) |
| 128 | } |
| 129 | if len(tidy.Properties.Tidy_checks) > 0 { |
Chih-Hung Hsieh | 217e09a | 2021-02-22 17:03:15 -0800 | [diff] [blame] | 130 | tidyChecks = tidyChecks + "," + strings.Join(esc(ctx, "tidy_checks", |
Dan Albert | d12afec | 2020-08-14 16:53:21 -0700 | [diff] [blame] | 131 | config.ClangRewriteTidyChecks(tidy.Properties.Tidy_checks)), ",") |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 132 | } |
Chih-Hung Hsieh | 327b6f0 | 2018-12-10 16:28:56 -0800 | [diff] [blame] | 133 | if ctx.Windows() { |
| 134 | // https://b.corp.google.com/issues/120614316 |
| 135 | // mingw32 has cert-dcl16-c warning in NO_ERROR, |
| 136 | // which is used in many Android files. |
| 137 | tidyChecks = tidyChecks + ",-cert-dcl16-c" |
| 138 | } |
Chih-Hung Hsieh | 3d3df82 | 2020-04-08 10:42:16 -0700 | [diff] [blame] | 139 | // https://b.corp.google.com/issues/153464409 |
| 140 | // many local projects enable cert-* checks, which |
| 141 | // trigger bugprone-reserved-identifier. |
Yabin Cui | 70ba0e2 | 2020-04-09 16:28:24 -0700 | [diff] [blame] | 142 | tidyChecks = tidyChecks + ",-bugprone-reserved-identifier*,-cert-dcl51-cpp,-cert-dcl37-c" |
Yabin Cui | 8ec05ff | 2020-04-10 13:36:41 -0700 | [diff] [blame] | 143 | // http://b/153757728 |
| 144 | tidyChecks = tidyChecks + ",-readability-qualified-auto" |
| 145 | // http://b/155034563 |
| 146 | tidyChecks = tidyChecks + ",-bugprone-signed-char-misuse" |
| 147 | // http://b/155034972 |
| 148 | tidyChecks = tidyChecks + ",-bugprone-branch-clone" |
Yabin Cui | 10bf3b8 | 2021-08-10 15:42:10 +0000 | [diff] [blame^] | 149 | // http://b/193716442 |
| 150 | tidyChecks = tidyChecks + ",-bugprone-implicit-widening-of-multiplication-result" |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 151 | flags.TidyFlags = append(flags.TidyFlags, tidyChecks) |
| 152 | |
Chih-Hung Hsieh | 1b4934a | 2021-01-14 15:45:25 -0800 | [diff] [blame] | 153 | if ctx.Config().IsEnvTrue("WITH_TIDY") { |
| 154 | // WITH_TIDY=1 enables clang-tidy globally. There could be many unexpected |
| 155 | // warnings from new checks and many local tidy_checks_as_errors and |
| 156 | // -warnings-as-errors can break a global build. |
| 157 | // So allow all clang-tidy warnings. |
| 158 | inserted := false |
| 159 | for i, s := range flags.TidyFlags { |
| 160 | if strings.Contains(s, "-warnings-as-errors=") { |
| 161 | // clang-tidy accepts only one -warnings-as-errors |
| 162 | // replace the old one |
| 163 | re := regexp.MustCompile(`'?-?-warnings-as-errors=[^ ]* *`) |
| 164 | newFlag := re.ReplaceAllString(s, "") |
| 165 | if newFlag == "" { |
| 166 | flags.TidyFlags[i] = "-warnings-as-errors=-*" |
| 167 | } else { |
| 168 | flags.TidyFlags[i] = newFlag + " -warnings-as-errors=-*" |
| 169 | } |
| 170 | inserted = true |
| 171 | break |
| 172 | } |
| 173 | } |
| 174 | if !inserted { |
| 175 | flags.TidyFlags = append(flags.TidyFlags, "-warnings-as-errors=-*") |
| 176 | } |
| 177 | } else if len(tidy.Properties.Tidy_checks_as_errors) > 0 { |
Chih-Hung Hsieh | 217e09a | 2021-02-22 17:03:15 -0800 | [diff] [blame] | 178 | tidyChecksAsErrors := "-warnings-as-errors=" + strings.Join(esc(ctx, "tidy_checks_as_errors", tidy.Properties.Tidy_checks_as_errors), ",") |
Nikita Ioffe | 32c4986 | 2019-03-26 20:33:49 +0000 | [diff] [blame] | 179 | flags.TidyFlags = append(flags.TidyFlags, tidyChecksAsErrors) |
| 180 | } |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 181 | return flags |
| 182 | } |