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 config |
| 16 | |
| 17 | import ( |
Dan Willemsen | 318af8b | 2016-11-02 14:50:21 -0700 | [diff] [blame] | 18 | "android/soong/android" |
Dan Willemsen | 54daaf0 | 2018-03-12 13:24:09 -0700 | [diff] [blame] | 19 | "strings" |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 20 | ) |
| 21 | |
| 22 | func init() { |
| 23 | // Most Android source files are not clang-tidy clean yet. |
Chih-Hung Hsieh | b063dc4 | 2021-01-11 17:31:02 -0800 | [diff] [blame] | 24 | // Default global tidy checks must exclude all checks that |
Chih-Hung Hsieh | b767f91 | 2021-01-13 13:24:15 -0800 | [diff] [blame] | 25 | // have found too many false positives. |
Dan Willemsen | 54daaf0 | 2018-03-12 13:24:09 -0700 | [diff] [blame] | 26 | pctx.VariableFunc("TidyDefaultGlobalChecks", func(ctx android.PackageVarContext) string { |
| 27 | if override := ctx.Config().Getenv("DEFAULT_GLOBAL_TIDY_CHECKS"); override != "" { |
| 28 | return override |
Dan Willemsen | 318af8b | 2016-11-02 14:50:21 -0700 | [diff] [blame] | 29 | } |
| 30 | return strings.Join([]string{ |
Chih-Hung Hsieh | b063dc4 | 2021-01-11 17:31:02 -0800 | [diff] [blame] | 31 | "*", |
| 32 | "-altera-*", |
Chih-Hung Hsieh | 70b9316 | 2020-03-03 12:05:22 -0800 | [diff] [blame] | 33 | "-bugprone-narrowing-conversions", |
Chih-Hung Hsieh | b063dc4 | 2021-01-11 17:31:02 -0800 | [diff] [blame] | 34 | "-cppcoreguidelines-*", |
| 35 | "-fuchsia-*", |
| 36 | "-google-readability-*", |
Dan Willemsen | 318af8b | 2016-11-02 14:50:21 -0700 | [diff] [blame] | 37 | "-google-runtime-references", |
Chih-Hung Hsieh | b063dc4 | 2021-01-11 17:31:02 -0800 | [diff] [blame] | 38 | "-hicpp-*", |
| 39 | "-llvm-*", |
| 40 | "-llvmlibc-*", |
| 41 | "-misc-no-recursion", |
| 42 | "-misc-non-private-member-variables-in-classes", |
| 43 | "-misc-unused-parameters", |
| 44 | "-modernize-*", |
| 45 | "-readability-*", |
Dan Willemsen | 54daaf0 | 2018-03-12 13:24:09 -0700 | [diff] [blame] | 46 | }, ",") |
Dan Willemsen | 318af8b | 2016-11-02 14:50:21 -0700 | [diff] [blame] | 47 | }) |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 48 | |
| 49 | // There are too many clang-tidy warnings in external and vendor projects. |
| 50 | // Enable only some google checks for these projects. |
Dan Willemsen | 54daaf0 | 2018-03-12 13:24:09 -0700 | [diff] [blame] | 51 | pctx.VariableFunc("TidyExternalVendorChecks", func(ctx android.PackageVarContext) string { |
| 52 | if override := ctx.Config().Getenv("DEFAULT_EXTERNAL_VENDOR_TIDY_CHECKS"); override != "" { |
| 53 | return override |
Dan Willemsen | 318af8b | 2016-11-02 14:50:21 -0700 | [diff] [blame] | 54 | } |
| 55 | return strings.Join([]string{ |
| 56 | "-*", |
Chih-Hung Hsieh | a7aa958 | 2018-08-15 11:28:38 -0700 | [diff] [blame] | 57 | "clang-diagnostic-unused-command-line-argument", |
Dan Willemsen | 318af8b | 2016-11-02 14:50:21 -0700 | [diff] [blame] | 58 | "google*", |
| 59 | "-google-build-using-namespace", |
| 60 | "-google-default-arguments", |
| 61 | "-google-explicit-constructor", |
| 62 | "-google-readability*", |
| 63 | "-google-runtime-int", |
| 64 | "-google-runtime-references", |
Dan Willemsen | 54daaf0 | 2018-03-12 13:24:09 -0700 | [diff] [blame] | 65 | }, ",") |
Dan Willemsen | 318af8b | 2016-11-02 14:50:21 -0700 | [diff] [blame] | 66 | }) |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 67 | |
| 68 | // Give warnings to header files only in selected directories. |
| 69 | // Do not give warnings to external or vendor header files, which contain too |
| 70 | // many warnings. |
Chih-Hung Hsieh | 60bd4bf | 2018-09-21 09:38:17 -0700 | [diff] [blame] | 71 | pctx.VariableFunc("TidyDefaultHeaderDirs", func(ctx android.PackageVarContext) string { |
| 72 | if override := ctx.Config().Getenv("DEFAULT_TIDY_HEADER_DIRS"); override != "" { |
| 73 | return override |
| 74 | } |
| 75 | return strings.Join([]string{ |
| 76 | "art/", |
| 77 | "bionic/", |
| 78 | "bootable/", |
| 79 | "build/", |
| 80 | "cts/", |
| 81 | "dalvik/", |
| 82 | "developers/", |
| 83 | "development/", |
| 84 | "frameworks/", |
| 85 | "libcore/", |
| 86 | "libnativehelper/", |
| 87 | "system/", |
| 88 | }, "|") |
| 89 | }) |
Chih-Hung Hsieh | 9e5d8a6 | 2018-09-21 15:12:44 -0700 | [diff] [blame] | 90 | |
| 91 | // Use WTIH_TIDY_FLAGS to pass extra global default clang-tidy flags. |
| 92 | pctx.VariableFunc("TidyWithTidyFlags", func(ctx android.PackageVarContext) string { |
| 93 | return ctx.Config().Getenv("WITH_TIDY_FLAGS") |
| 94 | }) |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | type PathBasedTidyCheck struct { |
| 98 | PathPrefix string |
| 99 | Checks string |
| 100 | } |
| 101 | |
| 102 | const tidyDefault = "${config.TidyDefaultGlobalChecks}" |
| 103 | const tidyExternalVendor = "${config.TidyExternalVendorChecks}" |
| 104 | |
| 105 | // This is a map of local path prefixes to the set of default clang-tidy checks |
| 106 | // to be used. |
| 107 | // The last matched local_path_prefix should be the most specific to be used. |
| 108 | var DefaultLocalTidyChecks = []PathBasedTidyCheck{ |
| 109 | {"external/", tidyExternalVendor}, |
| 110 | {"external/google", tidyDefault}, |
| 111 | {"external/webrtc", tidyDefault}, |
| 112 | {"frameworks/compile/mclinker/", tidyExternalVendor}, |
| 113 | {"hardware/qcom", tidyExternalVendor}, |
| 114 | {"vendor/", tidyExternalVendor}, |
| 115 | {"vendor/google", tidyDefault}, |
| 116 | {"vendor/google_devices", tidyExternalVendor}, |
| 117 | } |
| 118 | |
| 119 | var reversedDefaultLocalTidyChecks = reverseTidyChecks(DefaultLocalTidyChecks) |
| 120 | |
| 121 | func reverseTidyChecks(in []PathBasedTidyCheck) []PathBasedTidyCheck { |
| 122 | ret := make([]PathBasedTidyCheck, len(in)) |
| 123 | for i, check := range in { |
| 124 | ret[len(in)-i-1] = check |
| 125 | } |
| 126 | return ret |
| 127 | } |
| 128 | |
| 129 | func TidyChecksForDir(dir string) string { |
| 130 | for _, pathCheck := range reversedDefaultLocalTidyChecks { |
| 131 | if strings.HasPrefix(dir, pathCheck.PathPrefix) { |
| 132 | return pathCheck.Checks |
| 133 | } |
| 134 | } |
| 135 | return tidyDefault |
| 136 | } |