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 ( |
| 18 | "strings" |
| 19 | |
| 20 | "github.com/google/blueprint/proptools" |
| 21 | |
| 22 | "android/soong/cc/config" |
| 23 | ) |
| 24 | |
| 25 | type TidyProperties struct { |
| 26 | // whether to run clang-tidy over C-like sources. |
| 27 | Tidy *bool |
| 28 | |
| 29 | // Extra flags to pass to clang-tidy |
| 30 | Tidy_flags []string |
| 31 | |
| 32 | // Extra checks to enable or disable in clang-tidy |
| 33 | Tidy_checks []string |
Nikita Ioffe | 32c4986 | 2019-03-26 20:33:49 +0000 | [diff] [blame] | 34 | |
| 35 | // Checks that should be treated as errors. |
| 36 | Tidy_checks_as_errors []string |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | type tidyFeature struct { |
| 40 | Properties TidyProperties |
| 41 | } |
| 42 | |
| 43 | func (tidy *tidyFeature) props() []interface{} { |
| 44 | return []interface{}{&tidy.Properties} |
| 45 | } |
| 46 | |
| 47 | func (tidy *tidyFeature) begin(ctx BaseModuleContext) { |
| 48 | } |
| 49 | |
Colin Cross | 37047f1 | 2016-12-13 17:06:13 -0800 | [diff] [blame] | 50 | func (tidy *tidyFeature) deps(ctx DepsContext, deps Deps) Deps { |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 51 | return deps |
| 52 | } |
| 53 | |
| 54 | func (tidy *tidyFeature) flags(ctx ModuleContext, flags Flags) Flags { |
Colin Cross | 379d2cb | 2016-12-05 17:11:06 -0800 | [diff] [blame] | 55 | CheckBadTidyFlags(ctx, "tidy_flags", tidy.Properties.Tidy_flags) |
| 56 | CheckBadTidyChecks(ctx, "tidy_checks", tidy.Properties.Tidy_checks) |
| 57 | |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 58 | // Check if tidy is explicitly disabled for this module |
| 59 | if tidy.Properties.Tidy != nil && !*tidy.Properties.Tidy { |
| 60 | return flags |
| 61 | } |
| 62 | |
| 63 | // If not explicitly set, check the global tidy flag |
Colin Cross | 6510f91 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 64 | if tidy.Properties.Tidy == nil && !ctx.Config().ClangTidy() { |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 65 | return flags |
| 66 | } |
| 67 | |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 68 | flags.Tidy = true |
| 69 | |
Chih-Hung Hsieh | 9e5d8a6 | 2018-09-21 15:12:44 -0700 | [diff] [blame] | 70 | // Add global WITH_TIDY_FLAGS and local tidy_flags. |
| 71 | withTidyFlags := ctx.Config().Getenv("WITH_TIDY_FLAGS") |
| 72 | if len(withTidyFlags) > 0 { |
| 73 | flags.TidyFlags = append(flags.TidyFlags, withTidyFlags) |
| 74 | } |
Colin Cross | 0b9f31f | 2019-02-28 11:00:01 -0800 | [diff] [blame] | 75 | esc := proptools.NinjaAndShellEscapeList |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 76 | flags.TidyFlags = append(flags.TidyFlags, esc(tidy.Properties.Tidy_flags)...) |
Chih-Hung Hsieh | 9e5d8a6 | 2018-09-21 15:12:44 -0700 | [diff] [blame] | 77 | // If TidyFlags is empty, add default header filter. |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 78 | if len(flags.TidyFlags) == 0 { |
| 79 | headerFilter := "-header-filter=\"(" + ctx.ModuleDir() + "|${config.TidyDefaultHeaderDirs})\"" |
| 80 | flags.TidyFlags = append(flags.TidyFlags, headerFilter) |
| 81 | } |
| 82 | |
Chih-Hung Hsieh | dc0c030 | 2017-12-15 20:57:48 -0800 | [diff] [blame] | 83 | // If clang-tidy is not enabled globally, add the -quiet flag. |
| 84 | if !ctx.Config().ClangTidy() { |
| 85 | flags.TidyFlags = append(flags.TidyFlags, "-quiet") |
Chih-Hung Hsieh | 669cb91 | 2018-01-04 01:41:16 -0800 | [diff] [blame] | 86 | flags.TidyFlags = append(flags.TidyFlags, "-extra-arg-before=-fno-caret-diagnostics") |
Chih-Hung Hsieh | dc0c030 | 2017-12-15 20:57:48 -0800 | [diff] [blame] | 87 | } |
| 88 | |
George Burgess IV | 030ccee | 2018-05-14 16:30:46 -0700 | [diff] [blame] | 89 | extraArgFlags := []string{ |
| 90 | // We might be using the static analyzer through clang tidy. |
| 91 | // https://bugs.llvm.org/show_bug.cgi?id=32914 |
| 92 | "-D__clang_analyzer__", |
| 93 | |
| 94 | // A recent change in clang-tidy (r328258) enabled destructor inlining, which |
| 95 | // appears to cause a number of false positives. Until that's resolved, this turns |
| 96 | // off the effects of r328258. |
| 97 | // https://bugs.llvm.org/show_bug.cgi?id=37459 |
| 98 | "-Xclang", "-analyzer-config", "-Xclang", "c++-temp-dtor-inlining=false", |
| 99 | } |
| 100 | |
| 101 | for _, f := range extraArgFlags { |
| 102 | flags.TidyFlags = append(flags.TidyFlags, "-extra-arg-before="+f) |
| 103 | } |
George Burgess IV | 561a3fe | 2017-05-03 18:13:08 -0700 | [diff] [blame] | 104 | |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 105 | tidyChecks := "-checks=" |
Colin Cross | 6510f91 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 106 | if checks := ctx.Config().TidyChecks(); len(checks) > 0 { |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 107 | tidyChecks += checks |
| 108 | } else { |
| 109 | tidyChecks += config.TidyChecksForDir(ctx.ModuleDir()) |
| 110 | } |
| 111 | if len(tidy.Properties.Tidy_checks) > 0 { |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 112 | tidyChecks = tidyChecks + "," + strings.Join(esc(tidy.Properties.Tidy_checks), ",") |
| 113 | } |
Chih-Hung Hsieh | 327b6f0 | 2018-12-10 16:28:56 -0800 | [diff] [blame] | 114 | if ctx.Windows() { |
| 115 | // https://b.corp.google.com/issues/120614316 |
| 116 | // mingw32 has cert-dcl16-c warning in NO_ERROR, |
| 117 | // which is used in many Android files. |
| 118 | tidyChecks = tidyChecks + ",-cert-dcl16-c" |
| 119 | } |
Chih-Hung Hsieh | 3d3df82 | 2020-04-08 10:42:16 -0700 | [diff] [blame] | 120 | // https://b.corp.google.com/issues/153464409 |
| 121 | // many local projects enable cert-* checks, which |
| 122 | // trigger bugprone-reserved-identifier. |
Yabin Cui | 70ba0e2 | 2020-04-09 16:28:24 -0700 | [diff] [blame] | 123 | tidyChecks = tidyChecks + ",-bugprone-reserved-identifier*,-cert-dcl51-cpp,-cert-dcl37-c" |
Yabin Cui | 8ec05ff | 2020-04-10 13:36:41 -0700 | [diff] [blame^] | 124 | // http://b/153757728 |
| 125 | tidyChecks = tidyChecks + ",-readability-qualified-auto" |
| 126 | // http://b/155034563 |
| 127 | tidyChecks = tidyChecks + ",-bugprone-signed-char-misuse" |
| 128 | // http://b/155034972 |
| 129 | tidyChecks = tidyChecks + ",-bugprone-branch-clone" |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 130 | flags.TidyFlags = append(flags.TidyFlags, tidyChecks) |
| 131 | |
Nikita Ioffe | 32c4986 | 2019-03-26 20:33:49 +0000 | [diff] [blame] | 132 | if len(tidy.Properties.Tidy_checks_as_errors) > 0 { |
| 133 | tidyChecksAsErrors := "-warnings-as-errors=" + strings.Join(esc(tidy.Properties.Tidy_checks_as_errors), ",") |
| 134 | flags.TidyFlags = append(flags.TidyFlags, tidyChecksAsErrors) |
| 135 | } |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 136 | return flags |
| 137 | } |