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 |
| 34 | } |
| 35 | |
| 36 | type tidyFeature struct { |
| 37 | Properties TidyProperties |
| 38 | } |
| 39 | |
| 40 | func (tidy *tidyFeature) props() []interface{} { |
| 41 | return []interface{}{&tidy.Properties} |
| 42 | } |
| 43 | |
| 44 | func (tidy *tidyFeature) begin(ctx BaseModuleContext) { |
| 45 | } |
| 46 | |
Colin Cross | 37047f1 | 2016-12-13 17:06:13 -0800 | [diff] [blame] | 47 | func (tidy *tidyFeature) deps(ctx DepsContext, deps Deps) Deps { |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 48 | return deps |
| 49 | } |
| 50 | |
| 51 | func (tidy *tidyFeature) flags(ctx ModuleContext, flags Flags) Flags { |
Colin Cross | 379d2cb | 2016-12-05 17:11:06 -0800 | [diff] [blame] | 52 | CheckBadTidyFlags(ctx, "tidy_flags", tidy.Properties.Tidy_flags) |
| 53 | CheckBadTidyChecks(ctx, "tidy_checks", tidy.Properties.Tidy_checks) |
| 54 | |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 55 | // Check if tidy is explicitly disabled for this module |
| 56 | if tidy.Properties.Tidy != nil && !*tidy.Properties.Tidy { |
| 57 | return flags |
| 58 | } |
| 59 | |
| 60 | // If not explicitly set, check the global tidy flag |
Colin Cross | 6510f91 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 61 | if tidy.Properties.Tidy == nil && !ctx.Config().ClangTidy() { |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 62 | return flags |
| 63 | } |
| 64 | |
| 65 | // Clang-tidy requires clang |
| 66 | if !flags.Clang { |
| 67 | return flags |
| 68 | } |
| 69 | |
| 70 | flags.Tidy = true |
| 71 | |
Chih-Hung Hsieh | 9e5d8a6 | 2018-09-21 15:12:44 -0700 | [diff] [blame^] | 72 | // Add global WITH_TIDY_FLAGS and local tidy_flags. |
| 73 | withTidyFlags := ctx.Config().Getenv("WITH_TIDY_FLAGS") |
| 74 | if len(withTidyFlags) > 0 { |
| 75 | flags.TidyFlags = append(flags.TidyFlags, withTidyFlags) |
| 76 | } |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 77 | esc := proptools.NinjaAndShellEscape |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 78 | flags.TidyFlags = append(flags.TidyFlags, esc(tidy.Properties.Tidy_flags)...) |
Chih-Hung Hsieh | 9e5d8a6 | 2018-09-21 15:12:44 -0700 | [diff] [blame^] | 79 | // If TidyFlags is empty, add default header filter. |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 80 | if len(flags.TidyFlags) == 0 { |
| 81 | headerFilter := "-header-filter=\"(" + ctx.ModuleDir() + "|${config.TidyDefaultHeaderDirs})\"" |
| 82 | flags.TidyFlags = append(flags.TidyFlags, headerFilter) |
| 83 | } |
| 84 | |
Chih-Hung Hsieh | dc0c030 | 2017-12-15 20:57:48 -0800 | [diff] [blame] | 85 | // If clang-tidy is not enabled globally, add the -quiet flag. |
| 86 | if !ctx.Config().ClangTidy() { |
| 87 | flags.TidyFlags = append(flags.TidyFlags, "-quiet") |
Chih-Hung Hsieh | 669cb91 | 2018-01-04 01:41:16 -0800 | [diff] [blame] | 88 | flags.TidyFlags = append(flags.TidyFlags, "-extra-arg-before=-fno-caret-diagnostics") |
Chih-Hung Hsieh | dc0c030 | 2017-12-15 20:57:48 -0800 | [diff] [blame] | 89 | } |
| 90 | |
George Burgess IV | 030ccee | 2018-05-14 16:30:46 -0700 | [diff] [blame] | 91 | extraArgFlags := []string{ |
| 92 | // We might be using the static analyzer through clang tidy. |
| 93 | // https://bugs.llvm.org/show_bug.cgi?id=32914 |
| 94 | "-D__clang_analyzer__", |
| 95 | |
| 96 | // A recent change in clang-tidy (r328258) enabled destructor inlining, which |
| 97 | // appears to cause a number of false positives. Until that's resolved, this turns |
| 98 | // off the effects of r328258. |
| 99 | // https://bugs.llvm.org/show_bug.cgi?id=37459 |
| 100 | "-Xclang", "-analyzer-config", "-Xclang", "c++-temp-dtor-inlining=false", |
| 101 | } |
| 102 | |
| 103 | for _, f := range extraArgFlags { |
| 104 | flags.TidyFlags = append(flags.TidyFlags, "-extra-arg-before="+f) |
| 105 | } |
George Burgess IV | 561a3fe | 2017-05-03 18:13:08 -0700 | [diff] [blame] | 106 | |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 107 | tidyChecks := "-checks=" |
Colin Cross | 6510f91 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 108 | if checks := ctx.Config().TidyChecks(); len(checks) > 0 { |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 109 | tidyChecks += checks |
| 110 | } else { |
| 111 | tidyChecks += config.TidyChecksForDir(ctx.ModuleDir()) |
| 112 | } |
| 113 | if len(tidy.Properties.Tidy_checks) > 0 { |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 114 | tidyChecks = tidyChecks + "," + strings.Join(esc(tidy.Properties.Tidy_checks), ",") |
| 115 | } |
| 116 | flags.TidyFlags = append(flags.TidyFlags, tidyChecks) |
| 117 | |
| 118 | return flags |
| 119 | } |