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