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 15package cc 16 17import ( 18 "regexp" 19 "strings" 20 21 "github.com/google/blueprint/proptools" 22 23 "android/soong/android" 24 "android/soong/cc/config" 25) 26 27type 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 36 37 // Checks that should be treated as errors. 38 Tidy_checks_as_errors []string 39} 40 41type tidyFeature struct { 42 Properties TidyProperties 43} 44 45var 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. 51func 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 60func (tidy *tidyFeature) props() []interface{} { 61 return []interface{}{&tidy.Properties} 62} 63 64func (tidy *tidyFeature) begin(ctx BaseModuleContext) { 65} 66 67func (tidy *tidyFeature) deps(ctx DepsContext, deps Deps) Deps { 68 return deps 69} 70 71func (tidy *tidyFeature) flags(ctx ModuleContext, flags Flags) Flags { 72 CheckBadTidyFlags(ctx, "tidy_flags", tidy.Properties.Tidy_flags) 73 CheckBadTidyChecks(ctx, "tidy_checks", tidy.Properties.Tidy_checks) 74 75 // Check if tidy is explicitly disabled for this module 76 if tidy.Properties.Tidy != nil && !*tidy.Properties.Tidy { 77 return flags 78 } 79 80 // If not explicitly set, check the global tidy flag 81 if tidy.Properties.Tidy == nil && !ctx.Config().ClangTidy() { 82 return flags 83 } 84 85 flags.Tidy = true 86 87 // Add global WITH_TIDY_FLAGS and local tidy_flags. 88 withTidyFlags := ctx.Config().Getenv("WITH_TIDY_FLAGS") 89 if len(withTidyFlags) > 0 { 90 flags.TidyFlags = append(flags.TidyFlags, withTidyFlags) 91 } 92 esc := checkNinjaAndShellEscapeList 93 flags.TidyFlags = append(flags.TidyFlags, esc(ctx, "tidy_flags", tidy.Properties.Tidy_flags)...) 94 // If TidyFlags does not contain -header-filter, add default header filter. 95 // Find the substring because the flag could also appear as --header-filter=... 96 // and with or without single or double quotes. 97 if !android.SubstringInList(flags.TidyFlags, "-header-filter=") { 98 defaultDirs := ctx.Config().Getenv("DEFAULT_TIDY_HEADER_DIRS") 99 headerFilter := "-header-filter=" 100 if defaultDirs == "" { 101 headerFilter += ctx.ModuleDir() + "/" 102 } else { 103 headerFilter += "\"(" + ctx.ModuleDir() + "/|" + defaultDirs + ")\"" 104 } 105 flags.TidyFlags = append(flags.TidyFlags, headerFilter) 106 } 107 108 // If clang-tidy is not enabled globally, add the -quiet flag. 109 if !ctx.Config().ClangTidy() { 110 flags.TidyFlags = append(flags.TidyFlags, "-quiet") 111 flags.TidyFlags = append(flags.TidyFlags, "-extra-arg-before=-fno-caret-diagnostics") 112 } 113 114 extraArgFlags := []string{ 115 // We might be using the static analyzer through clang tidy. 116 // https://bugs.llvm.org/show_bug.cgi?id=32914 117 "-D__clang_analyzer__", 118 119 // A recent change in clang-tidy (r328258) enabled destructor inlining, which 120 // appears to cause a number of false positives. Until that's resolved, this turns 121 // off the effects of r328258. 122 // https://bugs.llvm.org/show_bug.cgi?id=37459 123 "-Xclang", "-analyzer-config", "-Xclang", "c++-temp-dtor-inlining=false", 124 } 125 126 for _, f := range extraArgFlags { 127 flags.TidyFlags = append(flags.TidyFlags, "-extra-arg-before="+f) 128 } 129 130 tidyChecks := "-checks=" 131 if checks := ctx.Config().TidyChecks(); len(checks) > 0 { 132 tidyChecks += checks 133 } else { 134 tidyChecks += config.TidyChecksForDir(ctx.ModuleDir()) 135 } 136 if len(tidy.Properties.Tidy_checks) > 0 { 137 tidyChecks = tidyChecks + "," + strings.Join(esc(ctx, "tidy_checks", 138 config.ClangRewriteTidyChecks(tidy.Properties.Tidy_checks)), ",") 139 } 140 if ctx.Windows() { 141 // https://b.corp.google.com/issues/120614316 142 // mingw32 has cert-dcl16-c warning in NO_ERROR, 143 // which is used in many Android files. 144 tidyChecks = tidyChecks + ",-cert-dcl16-c" 145 } 146 // https://b.corp.google.com/issues/153464409 147 // many local projects enable cert-* checks, which 148 // trigger bugprone-reserved-identifier. 149 tidyChecks = tidyChecks + ",-bugprone-reserved-identifier*,-cert-dcl51-cpp,-cert-dcl37-c" 150 // http://b/153757728 151 tidyChecks = tidyChecks + ",-readability-qualified-auto" 152 // http://b/155034563 153 tidyChecks = tidyChecks + ",-bugprone-signed-char-misuse" 154 // http://b/155034972 155 tidyChecks = tidyChecks + ",-bugprone-branch-clone" 156 flags.TidyFlags = append(flags.TidyFlags, tidyChecks) 157 158 if ctx.Config().IsEnvTrue("WITH_TIDY") { 159 // WITH_TIDY=1 enables clang-tidy globally. There could be many unexpected 160 // warnings from new checks and many local tidy_checks_as_errors and 161 // -warnings-as-errors can break a global build. 162 // So allow all clang-tidy warnings. 163 inserted := false 164 for i, s := range flags.TidyFlags { 165 if strings.Contains(s, "-warnings-as-errors=") { 166 // clang-tidy accepts only one -warnings-as-errors 167 // replace the old one 168 re := regexp.MustCompile(`'?-?-warnings-as-errors=[^ ]* *`) 169 newFlag := re.ReplaceAllString(s, "") 170 if newFlag == "" { 171 flags.TidyFlags[i] = "-warnings-as-errors=-*" 172 } else { 173 flags.TidyFlags[i] = newFlag + " -warnings-as-errors=-*" 174 } 175 inserted = true 176 break 177 } 178 } 179 if !inserted { 180 flags.TidyFlags = append(flags.TidyFlags, "-warnings-as-errors=-*") 181 } 182 } else if len(tidy.Properties.Tidy_checks_as_errors) > 0 { 183 tidyChecksAsErrors := "-warnings-as-errors=" + strings.Join(esc(ctx, "tidy_checks_as_errors", tidy.Properties.Tidy_checks_as_errors), ",") 184 flags.TidyFlags = append(flags.TidyFlags, tidyChecksAsErrors) 185 } 186 return flags 187} 188