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