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 | |
Chih-Hung Hsieh | ad47a27 | 2018-06-25 13:48:42 -0700 | [diff] [blame] | 22 | // clang-tidy doesn't recognize every flag that clang does. This is unlikely to |
| 23 | // be a complete list, but we can populate this with the ones we know to avoid |
| 24 | // issues with clang-diagnostic-unused-command-line-argument. |
Chih-Hung Hsieh | 5942fe4 | 2018-07-26 15:00:28 -0700 | [diff] [blame^] | 25 | // b/111885396: -flto affected header include directory; -fsanitize needs -flto. |
Chih-Hung Hsieh | ad47a27 | 2018-06-25 13:48:42 -0700 | [diff] [blame] | 26 | var ClangTidyUnknownCflags = sorted([]string{ |
| 27 | "-Wa,%", |
Chih-Hung Hsieh | 5942fe4 | 2018-07-26 15:00:28 -0700 | [diff] [blame^] | 28 | "-flto", |
| 29 | "-fsanitize=%", |
Chih-Hung Hsieh | ad47a27 | 2018-06-25 13:48:42 -0700 | [diff] [blame] | 30 | }) |
| 31 | |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 32 | func init() { |
| 33 | // Most Android source files are not clang-tidy clean yet. |
| 34 | // Global tidy checks include only google*, performance*, |
| 35 | // and misc-macro-parentheses, but not google-readability* |
| 36 | // or google-runtime-references. |
Dan Willemsen | 54daaf0 | 2018-03-12 13:24:09 -0700 | [diff] [blame] | 37 | pctx.VariableFunc("TidyDefaultGlobalChecks", func(ctx android.PackageVarContext) string { |
| 38 | if override := ctx.Config().Getenv("DEFAULT_GLOBAL_TIDY_CHECKS"); override != "" { |
| 39 | return override |
Dan Willemsen | 318af8b | 2016-11-02 14:50:21 -0700 | [diff] [blame] | 40 | } |
| 41 | return strings.Join([]string{ |
| 42 | "-*", |
| 43 | "google*", |
| 44 | "misc-macro-parentheses", |
| 45 | "performance*", |
| 46 | "-google-readability*", |
| 47 | "-google-runtime-references", |
Dan Willemsen | 54daaf0 | 2018-03-12 13:24:09 -0700 | [diff] [blame] | 48 | }, ",") |
Dan Willemsen | 318af8b | 2016-11-02 14:50:21 -0700 | [diff] [blame] | 49 | }) |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 50 | |
| 51 | // There are too many clang-tidy warnings in external and vendor projects. |
| 52 | // Enable only some google checks for these projects. |
Dan Willemsen | 54daaf0 | 2018-03-12 13:24:09 -0700 | [diff] [blame] | 53 | pctx.VariableFunc("TidyExternalVendorChecks", func(ctx android.PackageVarContext) string { |
| 54 | if override := ctx.Config().Getenv("DEFAULT_EXTERNAL_VENDOR_TIDY_CHECKS"); override != "" { |
| 55 | return override |
Dan Willemsen | 318af8b | 2016-11-02 14:50:21 -0700 | [diff] [blame] | 56 | } |
| 57 | return strings.Join([]string{ |
| 58 | "-*", |
| 59 | "google*", |
| 60 | "-google-build-using-namespace", |
| 61 | "-google-default-arguments", |
| 62 | "-google-explicit-constructor", |
| 63 | "-google-readability*", |
| 64 | "-google-runtime-int", |
| 65 | "-google-runtime-references", |
Dan Willemsen | 54daaf0 | 2018-03-12 13:24:09 -0700 | [diff] [blame] | 66 | }, ",") |
Dan Willemsen | 318af8b | 2016-11-02 14:50:21 -0700 | [diff] [blame] | 67 | }) |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 68 | |
| 69 | // Give warnings to header files only in selected directories. |
| 70 | // Do not give warnings to external or vendor header files, which contain too |
| 71 | // many warnings. |
| 72 | pctx.StaticVariable("TidyDefaultHeaderDirs", strings.Join([]string{ |
| 73 | "art/", |
| 74 | "bionic/", |
| 75 | "bootable/", |
| 76 | "build/", |
| 77 | "cts/", |
| 78 | "dalvik/", |
| 79 | "developers/", |
| 80 | "development/", |
| 81 | "frameworks/", |
| 82 | "libcore/", |
| 83 | "libnativehelper/", |
| 84 | "system/", |
| 85 | }, "|")) |
| 86 | } |
| 87 | |
| 88 | type PathBasedTidyCheck struct { |
| 89 | PathPrefix string |
| 90 | Checks string |
| 91 | } |
| 92 | |
| 93 | const tidyDefault = "${config.TidyDefaultGlobalChecks}" |
| 94 | const tidyExternalVendor = "${config.TidyExternalVendorChecks}" |
| 95 | |
| 96 | // This is a map of local path prefixes to the set of default clang-tidy checks |
| 97 | // to be used. |
| 98 | // The last matched local_path_prefix should be the most specific to be used. |
| 99 | var DefaultLocalTidyChecks = []PathBasedTidyCheck{ |
| 100 | {"external/", tidyExternalVendor}, |
| 101 | {"external/google", tidyDefault}, |
| 102 | {"external/webrtc", tidyDefault}, |
| 103 | {"frameworks/compile/mclinker/", tidyExternalVendor}, |
| 104 | {"hardware/qcom", tidyExternalVendor}, |
| 105 | {"vendor/", tidyExternalVendor}, |
| 106 | {"vendor/google", tidyDefault}, |
| 107 | {"vendor/google_devices", tidyExternalVendor}, |
| 108 | } |
| 109 | |
| 110 | var reversedDefaultLocalTidyChecks = reverseTidyChecks(DefaultLocalTidyChecks) |
| 111 | |
| 112 | func reverseTidyChecks(in []PathBasedTidyCheck) []PathBasedTidyCheck { |
| 113 | ret := make([]PathBasedTidyCheck, len(in)) |
| 114 | for i, check := range in { |
| 115 | ret[len(in)-i-1] = check |
| 116 | } |
| 117 | return ret |
| 118 | } |
| 119 | |
| 120 | func TidyChecksForDir(dir string) string { |
| 121 | for _, pathCheck := range reversedDefaultLocalTidyChecks { |
| 122 | if strings.HasPrefix(dir, pathCheck.PathPrefix) { |
| 123 | return pathCheck.Checks |
| 124 | } |
| 125 | } |
| 126 | return tidyDefault |
| 127 | } |