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 ( |
| 18 | "sort" |
| 19 | "strings" |
Colin Cross | c8bed31 | 2021-07-14 17:56:21 -0700 | [diff] [blame^] | 20 | |
| 21 | "android/soong/android" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 22 | ) |
| 23 | |
| 24 | // Cflags that should be filtered out when compiling with clang |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 25 | var ClangUnknownCflags = sorted([]string{ |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 26 | "-finline-functions", |
| 27 | "-finline-limit=64", |
| 28 | "-fno-canonical-system-headers", |
Dan Willemsen | 3bf6b47 | 2015-09-11 17:41:10 -0700 | [diff] [blame] | 29 | "-Wno-clobbered", |
| 30 | "-fno-devirtualize", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 31 | "-fno-tree-sra", |
Colin Cross | a360e8b | 2015-03-16 16:22:28 -0700 | [diff] [blame] | 32 | "-fprefetch-loop-arrays", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 33 | "-funswitch-loops", |
Dan Willemsen | e8c5237 | 2016-05-19 16:57:11 -0700 | [diff] [blame] | 34 | "-Werror=unused-but-set-parameter", |
| 35 | "-Werror=unused-but-set-variable", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 36 | "-Wmaybe-uninitialized", |
Dan Willemsen | 3bf6b47 | 2015-09-11 17:41:10 -0700 | [diff] [blame] | 37 | "-Wno-error=clobbered", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 38 | "-Wno-error=maybe-uninitialized", |
Colin Cross | 74d1ec0 | 2015-04-28 13:30:13 -0700 | [diff] [blame] | 39 | "-Wno-error=unused-but-set-parameter", |
| 40 | "-Wno-error=unused-but-set-variable", |
Chih-Hung Hsieh | 3ede294 | 2018-01-10 14:30:44 -0800 | [diff] [blame] | 41 | "-Wno-extended-offsetof", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 42 | "-Wno-free-nonheap-object", |
| 43 | "-Wno-literal-suffix", |
| 44 | "-Wno-maybe-uninitialized", |
| 45 | "-Wno-old-style-declaration", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 46 | "-Wno-unused-but-set-parameter", |
Colin Cross | 74d1ec0 | 2015-04-28 13:30:13 -0700 | [diff] [blame] | 47 | "-Wno-unused-but-set-variable", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 48 | "-Wno-unused-local-typedefs", |
Colin Cross | 62ec5f4 | 2015-03-18 17:20:28 -0700 | [diff] [blame] | 49 | "-Wunused-but-set-parameter", |
Colin Cross | 74d1ec0 | 2015-04-28 13:30:13 -0700 | [diff] [blame] | 50 | "-Wunused-but-set-variable", |
Dan Willemsen | e654045 | 2015-10-20 15:21:33 -0700 | [diff] [blame] | 51 | "-fdiagnostics-color", |
Yabin Cui | 8ec05ff | 2020-04-10 13:36:41 -0700 | [diff] [blame] | 52 | // http://b/153759688 |
| 53 | "-fuse-init-array", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 54 | |
Elliott Hughes | da3a071 | 2020-03-06 16:55:28 -0800 | [diff] [blame] | 55 | // arm + arm64 |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 56 | "-fgcse-after-reload", |
| 57 | "-frerun-cse-after-loop", |
| 58 | "-frename-registers", |
| 59 | "-fno-strict-volatile-bitfields", |
| 60 | |
| 61 | // arm + arm64 |
| 62 | "-fno-align-jumps", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 63 | |
| 64 | // arm |
| 65 | "-mthumb-interwork", |
| 66 | "-fno-builtin-sin", |
| 67 | "-fno-caller-saves", |
| 68 | "-fno-early-inlining", |
| 69 | "-fno-move-loop-invariants", |
| 70 | "-fno-partial-inlining", |
| 71 | "-fno-tree-copy-prop", |
| 72 | "-fno-tree-loop-optimize", |
| 73 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 74 | // x86 + x86_64 |
| 75 | "-finline-limit=300", |
| 76 | "-fno-inline-functions-called-once", |
| 77 | "-mfpmath=sse", |
| 78 | "-mbionic", |
Dan Willemsen | 01f388c | 2017-11-30 13:31:26 -0800 | [diff] [blame] | 79 | |
| 80 | // windows |
| 81 | "--enable-stdcall-fixup", |
Dan Willemsen | e654045 | 2015-10-20 15:21:33 -0700 | [diff] [blame] | 82 | }) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 83 | |
Chih-Hung Hsieh | 1017b37 | 2018-12-06 12:12:41 -0800 | [diff] [blame] | 84 | var ClangLibToolingUnknownCflags = sorted([]string{}) |
Jayant Chowdhary | 9677e8c | 2017-06-15 14:45:18 -0700 | [diff] [blame] | 85 | |
Dan Albert | d12afec | 2020-08-14 16:53:21 -0700 | [diff] [blame] | 86 | // List of tidy checks that should be disabled globally. When the compiler is |
| 87 | // updated, some checks enabled by this module may be disabled if they have |
| 88 | // become more strict, or if they are a new match for a wildcard group like |
| 89 | // `modernize-*`. |
Stephen Hines | 2210e72 | 2020-07-15 11:11:57 -0700 | [diff] [blame] | 90 | var ClangTidyDisableChecks = []string{ |
| 91 | "misc-no-recursion", |
Yabin Cui | db7dda8 | 2020-11-30 15:47:45 -0800 | [diff] [blame] | 92 | "readability-function-cognitive-complexity", // http://b/175055536 |
Stephen Hines | 2210e72 | 2020-07-15 11:11:57 -0700 | [diff] [blame] | 93 | } |
Dan Albert | d12afec | 2020-08-14 16:53:21 -0700 | [diff] [blame] | 94 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 95 | func init() { |
Jingwen Chen | 51a1e1c | 2021-05-20 13:40:14 +0000 | [diff] [blame] | 96 | exportStringListStaticVariable("ClangExtraCflags", []string{ |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 97 | "-D__compiler_offsetof=__builtin_offsetof", |
| 98 | |
Yi Kong | 1b0ba94 | 2019-03-19 20:05:05 -0700 | [diff] [blame] | 99 | // Emit address-significance table which allows linker to perform safe ICF. Clang does |
| 100 | // not emit the table by default on Android since NDK still uses GNU binutils. |
| 101 | "-faddrsig", |
| 102 | |
Stephen Hines | 2210e72 | 2020-07-15 11:11:57 -0700 | [diff] [blame] | 103 | // Turn on -fcommon explicitly, since Clang now defaults to -fno-common. The cleanup bug |
| 104 | // tracking this is http://b/151457797. |
| 105 | "-fcommon", |
| 106 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 107 | // Help catch common 32/64-bit errors. |
| 108 | "-Werror=int-conversion", |
| 109 | |
Yi Kong | 2fc3248 | 2019-04-22 22:33:23 -0700 | [diff] [blame] | 110 | // Enable the new pass manager. |
| 111 | "-fexperimental-new-pass-manager", |
| 112 | |
Colin Cross | 74d1ec0 | 2015-04-28 13:30:13 -0700 | [diff] [blame] | 113 | // Disable overly aggressive warning for macros defined with a leading underscore |
| 114 | // This happens in AndroidConfig.h, which is included nearly everywhere. |
Dan Willemsen | 3bf6b47 | 2015-09-11 17:41:10 -0700 | [diff] [blame] | 115 | // TODO: can we remove this now? |
Colin Cross | 74d1ec0 | 2015-04-28 13:30:13 -0700 | [diff] [blame] | 116 | "-Wno-reserved-id-macro", |
| 117 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 118 | // Workaround for ccache with clang. |
| 119 | // See http://petereisentraut.blogspot.com/2011/05/ccache-and-clang.html. |
| 120 | "-Wno-unused-command-line-argument", |
| 121 | |
Dan Willemsen | e654045 | 2015-10-20 15:21:33 -0700 | [diff] [blame] | 122 | // Force clang to always output color diagnostics. Ninja will strip the ANSI |
| 123 | // color codes if it is not running in a terminal. |
| 124 | "-fcolor-diagnostics", |
Pirama Arumuga Nainar | b6572b1 | 2016-06-28 10:56:03 -0700 | [diff] [blame] | 125 | |
Chih-Hung Hsieh | 3ede294 | 2018-01-10 14:30:44 -0800 | [diff] [blame] | 126 | // Warnings from clang-7.0 |
Chih-Hung Hsieh | 3ede294 | 2018-01-10 14:30:44 -0800 | [diff] [blame] | 127 | "-Wno-sign-compare", |
Yi Kong | 53ed59e | 2018-10-30 15:31:38 -0700 | [diff] [blame] | 128 | |
| 129 | // Warnings from clang-8.0 |
| 130 | "-Wno-defaulted-function-deleted", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 131 | |
Dan Willemsen | ac5e1cb | 2016-01-12 16:22:40 -0800 | [diff] [blame] | 132 | // Disable -Winconsistent-missing-override until we can clean up the existing |
| 133 | // codebase for it. |
| 134 | "-Wno-inconsistent-missing-override", |
Nick Desaulniers | eb20744 | 2019-12-12 10:15:42 -0800 | [diff] [blame] | 135 | |
| 136 | // Warnings from clang-10 |
| 137 | // Nested and array designated initialization is nice to have. |
| 138 | "-Wno-c99-designator", |
Jiyong Park | 423e378 | 2020-08-11 09:35:08 +0900 | [diff] [blame] | 139 | |
Chih-Hung Hsieh | 72e8876 | 2021-02-03 17:52:16 -0800 | [diff] [blame] | 140 | // Warnings from clang-12 |
| 141 | "-Wno-gnu-folding-constant", |
| 142 | |
Jiyong Park | 423e378 | 2020-08-11 09:35:08 +0900 | [diff] [blame] | 143 | // Calls to the APIs that are newer than the min sdk version of the caller should be |
| 144 | // guarded with __builtin_available. |
| 145 | "-Wunguarded-availability", |
| 146 | // This macro allows the bionic versioning.h to indirectly determine whether the |
| 147 | // option -Wunguarded-availability is on or not. |
Elliott Hughes | 5add0c6 | 2021-02-11 13:20:42 -0800 | [diff] [blame] | 148 | "-D__ANDROID_UNAVAILABLE_SYMBOLS_ARE_WEAK__", |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 149 | }) |
Pirama Arumuga Nainar | b6572b1 | 2016-06-28 10:56:03 -0700 | [diff] [blame] | 150 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 151 | } |
| 152 | |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 153 | func ClangFilterUnknownCflags(cflags []string) []string { |
Dan Albert | d12afec | 2020-08-14 16:53:21 -0700 | [diff] [blame] | 154 | result, _ := android.FilterList(cflags, ClangUnknownCflags) |
| 155 | return result |
| 156 | } |
| 157 | |
| 158 | func clangTidyNegateChecks(checks []string) []string { |
| 159 | ret := make([]string, 0, len(checks)) |
| 160 | for _, c := range checks { |
| 161 | if strings.HasPrefix(c, "-") { |
| 162 | ret = append(ret, c) |
| 163 | } else { |
| 164 | ret = append(ret, "-"+c) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 165 | } |
| 166 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 167 | return ret |
| 168 | } |
| 169 | |
Dan Albert | d12afec | 2020-08-14 16:53:21 -0700 | [diff] [blame] | 170 | func ClangRewriteTidyChecks(checks []string) []string { |
| 171 | checks = append(checks, clangTidyNegateChecks(ClangTidyDisableChecks)...) |
| 172 | // clang-tidy does not allow later arguments to override earlier arguments, |
| 173 | // so if we just disabled an argument that was explicitly enabled we must |
| 174 | // remove the enabling argument from the list. |
| 175 | result, _ := android.FilterList(checks, ClangTidyDisableChecks) |
| 176 | return result |
| 177 | } |
Chih-Hung Hsieh | 02b4da5 | 2018-04-03 11:33:34 -0700 | [diff] [blame] | 178 | |
Yo Chiang | 8aa4e3f | 2020-11-19 16:30:49 +0800 | [diff] [blame] | 179 | func ClangLibToolingFilterUnknownCflags(libToolingFlags []string) []string { |
| 180 | return android.RemoveListFromList(libToolingFlags, ClangLibToolingUnknownCflags) |
| 181 | } |
| 182 | |
Dan Willemsen | e654045 | 2015-10-20 15:21:33 -0700 | [diff] [blame] | 183 | func sorted(list []string) []string { |
| 184 | sort.Strings(list) |
| 185 | return list |
| 186 | } |