Colin Cross | d00350c | 2017-11-17 10:55:38 -0800 | [diff] [blame] | 1 | // Copyright 2017 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 | |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 15 | package config |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 16 | |
| 17 | import ( |
Dan Albert | d12afec | 2020-08-14 16:53:21 -0700 | [diff] [blame] | 18 | "android/soong/android" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 19 | "sort" |
| 20 | "strings" |
| 21 | ) |
| 22 | |
| 23 | // Cflags that should be filtered out when compiling with clang |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 24 | var ClangUnknownCflags = sorted([]string{ |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 25 | "-finline-functions", |
| 26 | "-finline-limit=64", |
| 27 | "-fno-canonical-system-headers", |
Dan Willemsen | 3bf6b47 | 2015-09-11 17:41:10 -0700 | [diff] [blame] | 28 | "-Wno-clobbered", |
| 29 | "-fno-devirtualize", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 30 | "-fno-tree-sra", |
Colin Cross | a360e8b | 2015-03-16 16:22:28 -0700 | [diff] [blame] | 31 | "-fprefetch-loop-arrays", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 32 | "-funswitch-loops", |
Dan Willemsen | e8c5237 | 2016-05-19 16:57:11 -0700 | [diff] [blame] | 33 | "-Werror=unused-but-set-parameter", |
| 34 | "-Werror=unused-but-set-variable", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 35 | "-Wmaybe-uninitialized", |
Dan Willemsen | 3bf6b47 | 2015-09-11 17:41:10 -0700 | [diff] [blame] | 36 | "-Wno-error=clobbered", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 37 | "-Wno-error=maybe-uninitialized", |
Colin Cross | 74d1ec0 | 2015-04-28 13:30:13 -0700 | [diff] [blame] | 38 | "-Wno-error=unused-but-set-parameter", |
| 39 | "-Wno-error=unused-but-set-variable", |
Chih-Hung Hsieh | 3ede294 | 2018-01-10 14:30:44 -0800 | [diff] [blame] | 40 | "-Wno-extended-offsetof", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 41 | "-Wno-free-nonheap-object", |
| 42 | "-Wno-literal-suffix", |
| 43 | "-Wno-maybe-uninitialized", |
| 44 | "-Wno-old-style-declaration", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 45 | "-Wno-unused-but-set-parameter", |
Colin Cross | 74d1ec0 | 2015-04-28 13:30:13 -0700 | [diff] [blame] | 46 | "-Wno-unused-but-set-variable", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 47 | "-Wno-unused-local-typedefs", |
Colin Cross | 62ec5f4 | 2015-03-18 17:20:28 -0700 | [diff] [blame] | 48 | "-Wunused-but-set-parameter", |
Colin Cross | 74d1ec0 | 2015-04-28 13:30:13 -0700 | [diff] [blame] | 49 | "-Wunused-but-set-variable", |
Dan Willemsen | e654045 | 2015-10-20 15:21:33 -0700 | [diff] [blame] | 50 | "-fdiagnostics-color", |
Yabin Cui | 8ec05ff | 2020-04-10 13:36:41 -0700 | [diff] [blame] | 51 | // http://b/153759688 |
| 52 | "-fuse-init-array", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 53 | |
Elliott Hughes | da3a071 | 2020-03-06 16:55:28 -0800 | [diff] [blame] | 54 | // arm + arm64 |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 55 | "-fgcse-after-reload", |
| 56 | "-frerun-cse-after-loop", |
| 57 | "-frename-registers", |
| 58 | "-fno-strict-volatile-bitfields", |
| 59 | |
| 60 | // arm + arm64 |
| 61 | "-fno-align-jumps", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 62 | |
| 63 | // arm |
| 64 | "-mthumb-interwork", |
| 65 | "-fno-builtin-sin", |
| 66 | "-fno-caller-saves", |
| 67 | "-fno-early-inlining", |
| 68 | "-fno-move-loop-invariants", |
| 69 | "-fno-partial-inlining", |
| 70 | "-fno-tree-copy-prop", |
| 71 | "-fno-tree-loop-optimize", |
| 72 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 73 | // x86 + x86_64 |
| 74 | "-finline-limit=300", |
| 75 | "-fno-inline-functions-called-once", |
| 76 | "-mfpmath=sse", |
| 77 | "-mbionic", |
Dan Willemsen | 01f388c | 2017-11-30 13:31:26 -0800 | [diff] [blame] | 78 | |
| 79 | // windows |
| 80 | "--enable-stdcall-fixup", |
Dan Willemsen | e654045 | 2015-10-20 15:21:33 -0700 | [diff] [blame] | 81 | }) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 82 | |
Chih-Hung Hsieh | 02b4da5 | 2018-04-03 11:33:34 -0700 | [diff] [blame] | 83 | // Ldflags that should be filtered out when linking with clang lld |
| 84 | var ClangUnknownLldflags = sorted([]string{ |
Chih-Hung Hsieh | 02b4da5 | 2018-04-03 11:33:34 -0700 | [diff] [blame] | 85 | "-Wl,--fix-cortex-a8", |
| 86 | "-Wl,--no-fix-cortex-a8", |
Chih-Hung Hsieh | 02b4da5 | 2018-04-03 11:33:34 -0700 | [diff] [blame] | 87 | }) |
| 88 | |
Chih-Hung Hsieh | 1017b37 | 2018-12-06 12:12:41 -0800 | [diff] [blame] | 89 | var ClangLibToolingUnknownCflags = sorted([]string{}) |
Jayant Chowdhary | 9677e8c | 2017-06-15 14:45:18 -0700 | [diff] [blame] | 90 | |
Dan Albert | d12afec | 2020-08-14 16:53:21 -0700 | [diff] [blame] | 91 | // List of tidy checks that should be disabled globally. When the compiler is |
| 92 | // updated, some checks enabled by this module may be disabled if they have |
| 93 | // become more strict, or if they are a new match for a wildcard group like |
| 94 | // `modernize-*`. |
Stephen Hines | 2210e72 | 2020-07-15 11:11:57 -0700 | [diff] [blame] | 95 | var ClangTidyDisableChecks = []string{ |
| 96 | "misc-no-recursion", |
Yabin Cui | db7dda8 | 2020-11-30 15:47:45 -0800 | [diff] [blame] | 97 | "readability-function-cognitive-complexity", // http://b/175055536 |
Stephen Hines | 2210e72 | 2020-07-15 11:11:57 -0700 | [diff] [blame] | 98 | } |
Dan Albert | d12afec | 2020-08-14 16:53:21 -0700 | [diff] [blame] | 99 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 100 | func init() { |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 101 | pctx.StaticVariable("ClangExtraCflags", strings.Join([]string{ |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 102 | "-D__compiler_offsetof=__builtin_offsetof", |
| 103 | |
Yi Kong | 1b0ba94 | 2019-03-19 20:05:05 -0700 | [diff] [blame] | 104 | // Emit address-significance table which allows linker to perform safe ICF. Clang does |
| 105 | // not emit the table by default on Android since NDK still uses GNU binutils. |
| 106 | "-faddrsig", |
| 107 | |
Stephen Hines | 2210e72 | 2020-07-15 11:11:57 -0700 | [diff] [blame] | 108 | // Turn on -fcommon explicitly, since Clang now defaults to -fno-common. The cleanup bug |
| 109 | // tracking this is http://b/151457797. |
| 110 | "-fcommon", |
| 111 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 112 | // Help catch common 32/64-bit errors. |
| 113 | "-Werror=int-conversion", |
| 114 | |
Yi Kong | 2fc3248 | 2019-04-22 22:33:23 -0700 | [diff] [blame] | 115 | // Enable the new pass manager. |
| 116 | "-fexperimental-new-pass-manager", |
| 117 | |
Colin Cross | 74d1ec0 | 2015-04-28 13:30:13 -0700 | [diff] [blame] | 118 | // Disable overly aggressive warning for macros defined with a leading underscore |
| 119 | // This happens in AndroidConfig.h, which is included nearly everywhere. |
Dan Willemsen | 3bf6b47 | 2015-09-11 17:41:10 -0700 | [diff] [blame] | 120 | // TODO: can we remove this now? |
Colin Cross | 74d1ec0 | 2015-04-28 13:30:13 -0700 | [diff] [blame] | 121 | "-Wno-reserved-id-macro", |
| 122 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 123 | // Workaround for ccache with clang. |
| 124 | // See http://petereisentraut.blogspot.com/2011/05/ccache-and-clang.html. |
| 125 | "-Wno-unused-command-line-argument", |
| 126 | |
Dan Willemsen | e654045 | 2015-10-20 15:21:33 -0700 | [diff] [blame] | 127 | // Force clang to always output color diagnostics. Ninja will strip the ANSI |
| 128 | // color codes if it is not running in a terminal. |
| 129 | "-fcolor-diagnostics", |
Pirama Arumuga Nainar | b6572b1 | 2016-06-28 10:56:03 -0700 | [diff] [blame] | 130 | |
Chih-Hung Hsieh | 3ede294 | 2018-01-10 14:30:44 -0800 | [diff] [blame] | 131 | // Warnings from clang-7.0 |
Chih-Hung Hsieh | 3ede294 | 2018-01-10 14:30:44 -0800 | [diff] [blame] | 132 | "-Wno-sign-compare", |
Yi Kong | 53ed59e | 2018-10-30 15:31:38 -0700 | [diff] [blame] | 133 | |
| 134 | // Warnings from clang-8.0 |
| 135 | "-Wno-defaulted-function-deleted", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 136 | |
Dan Willemsen | ac5e1cb | 2016-01-12 16:22:40 -0800 | [diff] [blame] | 137 | // Disable -Winconsistent-missing-override until we can clean up the existing |
| 138 | // codebase for it. |
| 139 | "-Wno-inconsistent-missing-override", |
Nick Desaulniers | eb20744 | 2019-12-12 10:15:42 -0800 | [diff] [blame] | 140 | |
| 141 | // Warnings from clang-10 |
| 142 | // Nested and array designated initialization is nice to have. |
| 143 | "-Wno-c99-designator", |
Yi Kong | 4d048d5 | 2019-03-27 18:26:22 -0700 | [diff] [blame] | 144 | }, " ")) |
Pirama Arumuga Nainar | b6572b1 | 2016-06-28 10:56:03 -0700 | [diff] [blame] | 145 | |
Yi Kong | 4d048d5 | 2019-03-27 18:26:22 -0700 | [diff] [blame] | 146 | pctx.StaticVariable("ClangExtraCppflags", strings.Join([]string{ |
Nick Desaulniers | 4e31fb8 | 2019-10-30 13:55:26 -0700 | [diff] [blame] | 147 | // -Wimplicit-fallthrough is not enabled by -Wall. |
| 148 | "-Wimplicit-fallthrough", |
| 149 | |
Josh Gao | e0b933b | 2017-04-26 20:26:14 -0700 | [diff] [blame] | 150 | // Enable clang's thread-safety annotations in libcxx. |
Josh Gao | e0b933b | 2017-04-26 20:26:14 -0700 | [diff] [blame] | 151 | "-D_LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS", |
Dan Albert | f2ceea7 | 2018-02-07 17:24:42 -0800 | [diff] [blame] | 152 | |
| 153 | // libc++'s math.h has an #include_next outside of system_headers. |
| 154 | "-Wno-gnu-include-next", |
Dan Willemsen | ac5e1cb | 2016-01-12 16:22:40 -0800 | [diff] [blame] | 155 | }, " ")) |
| 156 | |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 157 | pctx.StaticVariable("ClangExtraTargetCflags", strings.Join([]string{ |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 158 | "-nostdlibinc", |
| 159 | }, " ")) |
Dan Willemsen | be03f34 | 2016-03-03 17:21:04 -0800 | [diff] [blame] | 160 | |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 161 | pctx.StaticVariable("ClangExtraNoOverrideCflags", strings.Join([]string{ |
Dan Willemsen | be03f34 | 2016-03-03 17:21:04 -0800 | [diff] [blame] | 162 | "-Werror=address-of-temporary", |
Pirama Arumuga Nainar | b6572b1 | 2016-06-28 10:56:03 -0700 | [diff] [blame] | 163 | // Bug: http://b/29823425 Disable -Wnull-dereference until the |
| 164 | // new cases detected by this warning in Clang r271374 are |
| 165 | // fixed. |
| 166 | //"-Werror=null-dereference", |
Dan Willemsen | be03f34 | 2016-03-03 17:21:04 -0800 | [diff] [blame] | 167 | "-Werror=return-type", |
Yi Kong | 599a603 | 2017-12-06 19:56:34 -0800 | [diff] [blame] | 168 | |
| 169 | // http://b/72331526 Disable -Wtautological-* until the instances detected by these |
| 170 | // new warnings are fixed. |
Stephen Hines | a42e0a0 | 2018-02-06 14:49:42 -0800 | [diff] [blame] | 171 | "-Wno-tautological-constant-compare", |
Chih-Hung Hsieh | 3ede294 | 2018-01-10 14:30:44 -0800 | [diff] [blame] | 172 | "-Wno-tautological-type-limit-compare", |
Nick Desaulniers | eb20744 | 2019-12-12 10:15:42 -0800 | [diff] [blame] | 173 | // http://b/145210666 |
| 174 | "-Wno-reorder-init-list", |
| 175 | // http://b/145211066 |
| 176 | "-Wno-implicit-int-float-conversion", |
Chih-Hung Hsieh | 9d9555e | 2020-02-10 19:01:14 -0800 | [diff] [blame] | 177 | // New warnings to be fixed after clang-r377782. |
Chih-Hung Hsieh | 5f78d55 | 2020-02-14 10:10:22 -0800 | [diff] [blame] | 178 | "-Wno-int-in-bool-context", // http://b/148287349 |
| 179 | "-Wno-sizeof-array-div", // http://b/148815709 |
| 180 | "-Wno-tautological-overlap-compare", // http://b/148815696 |
Yabin Cui | 8ec05ff | 2020-04-10 13:36:41 -0700 | [diff] [blame] | 181 | // New warnings to be fixed after clang-r383902. |
| 182 | "-Wno-deprecated-copy", // http://b/153746672 |
| 183 | "-Wno-range-loop-construct", // http://b/153747076 |
| 184 | "-Wno-misleading-indentation", // http://b/153746954 |
| 185 | "-Wno-zero-as-null-pointer-constant", // http://b/68236239 |
| 186 | "-Wno-deprecated-anon-enum-enum-conversion", // http://b/153746485 |
| 187 | "-Wno-deprecated-enum-enum-conversion", // http://b/153746563 |
| 188 | "-Wno-string-compare", // http://b/153764102 |
| 189 | "-Wno-enum-enum-conversion", // http://b/154138986 |
| 190 | "-Wno-enum-float-conversion", // http://b/154255917 |
| 191 | "-Wno-pessimizing-move", // http://b/154270751 |
Stephen Hines | 2210e72 | 2020-07-15 11:11:57 -0700 | [diff] [blame] | 192 | // New warnings to be fixed after clang-r399163 |
| 193 | "-Wno-non-c-typedef-for-linkage", // http://b/161304145 |
Yabin Cui | db7dda8 | 2020-11-30 15:47:45 -0800 | [diff] [blame] | 194 | // New warnings to be fixed after clang-r407598 |
| 195 | "-Wno-string-concatenation", // http://b/175068488 |
Dan Willemsen | be03f34 | 2016-03-03 17:21:04 -0800 | [diff] [blame] | 196 | }, " ")) |
Yi Kong | cc80f8d | 2018-06-06 14:42:44 -0700 | [diff] [blame] | 197 | |
Yi Kong | 950e0ba | 2019-10-08 02:27:17 -0700 | [diff] [blame] | 198 | // Extra cflags for external third-party projects to disable warnings that |
| 199 | // are infeasible to fix in all the external projects and their upstream repos. |
Yi Kong | cc80f8d | 2018-06-06 14:42:44 -0700 | [diff] [blame] | 200 | pctx.StaticVariable("ClangExtraExternalCflags", strings.Join([]string{ |
Yi Kong | cf4cbdd | 2018-06-29 20:20:38 +0000 | [diff] [blame] | 201 | "-Wno-enum-compare", |
| 202 | "-Wno-enum-compare-switch", |
Yi Kong | 3e88cb0 | 2018-12-13 03:55:29 -0800 | [diff] [blame] | 203 | |
| 204 | // http://b/72331524 Allow null pointer arithmetic until the instances detected by |
| 205 | // this new warning are fixed. |
| 206 | "-Wno-null-pointer-arithmetic", |
Yi Kong | fae5dac | 2018-12-17 17:18:37 -0800 | [diff] [blame] | 207 | |
| 208 | // Bug: http://b/29823425 Disable -Wnull-dereference until the |
| 209 | // new instances detected by this warning are fixed. |
| 210 | "-Wno-null-dereference", |
Nick Desaulniers | eb20744 | 2019-12-12 10:15:42 -0800 | [diff] [blame] | 211 | |
| 212 | // http://b/145211477 |
| 213 | "-Wno-pointer-compare", |
| 214 | // http://b/145211022 |
| 215 | "-Wno-xor-used-as-pow", |
| 216 | // http://b/145211022 |
| 217 | "-Wno-final-dtor-non-final-class", |
Stephen Hines | 2210e72 | 2020-07-15 11:11:57 -0700 | [diff] [blame] | 218 | |
| 219 | // http://b/165945989 |
| 220 | "-Wno-psabi", |
Yi Kong | cc80f8d | 2018-06-06 14:42:44 -0700 | [diff] [blame] | 221 | }, " ")) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 222 | } |
| 223 | |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 224 | func ClangFilterUnknownCflags(cflags []string) []string { |
Dan Albert | d12afec | 2020-08-14 16:53:21 -0700 | [diff] [blame] | 225 | result, _ := android.FilterList(cflags, ClangUnknownCflags) |
| 226 | return result |
| 227 | } |
| 228 | |
| 229 | func clangTidyNegateChecks(checks []string) []string { |
| 230 | ret := make([]string, 0, len(checks)) |
| 231 | for _, c := range checks { |
| 232 | if strings.HasPrefix(c, "-") { |
| 233 | ret = append(ret, c) |
| 234 | } else { |
| 235 | ret = append(ret, "-"+c) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 236 | } |
| 237 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 238 | return ret |
| 239 | } |
| 240 | |
Dan Albert | d12afec | 2020-08-14 16:53:21 -0700 | [diff] [blame] | 241 | func ClangRewriteTidyChecks(checks []string) []string { |
| 242 | checks = append(checks, clangTidyNegateChecks(ClangTidyDisableChecks)...) |
| 243 | // clang-tidy does not allow later arguments to override earlier arguments, |
| 244 | // so if we just disabled an argument that was explicitly enabled we must |
| 245 | // remove the enabling argument from the list. |
| 246 | result, _ := android.FilterList(checks, ClangTidyDisableChecks) |
| 247 | return result |
| 248 | } |
Chih-Hung Hsieh | 02b4da5 | 2018-04-03 11:33:34 -0700 | [diff] [blame] | 249 | |
Dan Albert | d12afec | 2020-08-14 16:53:21 -0700 | [diff] [blame] | 250 | func ClangFilterUnknownLldflags(lldflags []string) []string { |
| 251 | result, _ := android.FilterList(lldflags, ClangUnknownLldflags) |
| 252 | return result |
Chih-Hung Hsieh | 02b4da5 | 2018-04-03 11:33:34 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Yo Chiang | 8aa4e3f | 2020-11-19 16:30:49 +0800 | [diff] [blame] | 255 | func ClangLibToolingFilterUnknownCflags(libToolingFlags []string) []string { |
| 256 | return android.RemoveListFromList(libToolingFlags, ClangLibToolingUnknownCflags) |
| 257 | } |
| 258 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 259 | func inListSorted(s string, list []string) bool { |
| 260 | for _, l := range list { |
| 261 | if s == l { |
| 262 | return true |
| 263 | } else if s < l { |
| 264 | return false |
| 265 | } |
| 266 | } |
| 267 | return false |
| 268 | } |
Dan Willemsen | e654045 | 2015-10-20 15:21:33 -0700 | [diff] [blame] | 269 | |
| 270 | func sorted(list []string) []string { |
| 271 | sort.Strings(list) |
| 272 | return list |
| 273 | } |