blob: 71c7626d90de8a03a4f8943f99fddc9829443884 [file] [log] [blame]
Colin Crossd00350c2017-11-17 10:55:38 -08001// 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 Crossb98c8b02016-07-29 13:44:28 -070015package config
Colin Cross3f40fa42015-01-30 17:27:36 -080016
17import (
Dan Albertd12afec2020-08-14 16:53:21 -070018 "android/soong/android"
Colin Cross3f40fa42015-01-30 17:27:36 -080019 "sort"
20 "strings"
21)
22
23// Cflags that should be filtered out when compiling with clang
Colin Crossb98c8b02016-07-29 13:44:28 -070024var ClangUnknownCflags = sorted([]string{
Colin Cross3f40fa42015-01-30 17:27:36 -080025 "-finline-functions",
26 "-finline-limit=64",
27 "-fno-canonical-system-headers",
Dan Willemsen3bf6b472015-09-11 17:41:10 -070028 "-Wno-clobbered",
29 "-fno-devirtualize",
Colin Cross3f40fa42015-01-30 17:27:36 -080030 "-fno-tree-sra",
Colin Crossa360e8b2015-03-16 16:22:28 -070031 "-fprefetch-loop-arrays",
Colin Cross3f40fa42015-01-30 17:27:36 -080032 "-funswitch-loops",
Dan Willemsene8c52372016-05-19 16:57:11 -070033 "-Werror=unused-but-set-parameter",
34 "-Werror=unused-but-set-variable",
Colin Cross3f40fa42015-01-30 17:27:36 -080035 "-Wmaybe-uninitialized",
Dan Willemsen3bf6b472015-09-11 17:41:10 -070036 "-Wno-error=clobbered",
Colin Cross3f40fa42015-01-30 17:27:36 -080037 "-Wno-error=maybe-uninitialized",
Colin Cross74d1ec02015-04-28 13:30:13 -070038 "-Wno-error=unused-but-set-parameter",
39 "-Wno-error=unused-but-set-variable",
Chih-Hung Hsieh3ede2942018-01-10 14:30:44 -080040 "-Wno-extended-offsetof",
Colin Cross3f40fa42015-01-30 17:27:36 -080041 "-Wno-free-nonheap-object",
42 "-Wno-literal-suffix",
43 "-Wno-maybe-uninitialized",
44 "-Wno-old-style-declaration",
Colin Cross3f40fa42015-01-30 17:27:36 -080045 "-Wno-unused-but-set-parameter",
Colin Cross74d1ec02015-04-28 13:30:13 -070046 "-Wno-unused-but-set-variable",
Colin Cross3f40fa42015-01-30 17:27:36 -080047 "-Wno-unused-local-typedefs",
Colin Cross62ec5f42015-03-18 17:20:28 -070048 "-Wunused-but-set-parameter",
Colin Cross74d1ec02015-04-28 13:30:13 -070049 "-Wunused-but-set-variable",
Dan Willemsene6540452015-10-20 15:21:33 -070050 "-fdiagnostics-color",
Yabin Cui8ec05ff2020-04-10 13:36:41 -070051 // http://b/153759688
52 "-fuse-init-array",
Colin Cross3f40fa42015-01-30 17:27:36 -080053
Elliott Hughesda3a0712020-03-06 16:55:28 -080054 // arm + arm64
Colin Cross3f40fa42015-01-30 17:27:36 -080055 "-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 Cross3f40fa42015-01-30 17:27:36 -080062
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 Cross3f40fa42015-01-30 17:27:36 -080073 // x86 + x86_64
74 "-finline-limit=300",
75 "-fno-inline-functions-called-once",
76 "-mfpmath=sse",
77 "-mbionic",
Dan Willemsen01f388c2017-11-30 13:31:26 -080078
79 // windows
80 "--enable-stdcall-fixup",
Dan Willemsene6540452015-10-20 15:21:33 -070081})
Colin Cross3f40fa42015-01-30 17:27:36 -080082
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -070083// Ldflags that should be filtered out when linking with clang lld
84var ClangUnknownLldflags = sorted([]string{
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -070085 "-Wl,--fix-cortex-a8",
86 "-Wl,--no-fix-cortex-a8",
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -070087})
88
Chih-Hung Hsieh1017b372018-12-06 12:12:41 -080089var ClangLibToolingUnknownCflags = sorted([]string{})
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070090
Dan Albertd12afec2020-08-14 16:53:21 -070091// 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 Hines2210e722020-07-15 11:11:57 -070095var ClangTidyDisableChecks = []string{
96 "misc-no-recursion",
Yabin Cuidb7dda82020-11-30 15:47:45 -080097 "readability-function-cognitive-complexity", // http://b/175055536
Stephen Hines2210e722020-07-15 11:11:57 -070098}
Dan Albertd12afec2020-08-14 16:53:21 -070099
Colin Cross3f40fa42015-01-30 17:27:36 -0800100func init() {
Colin Crossb98c8b02016-07-29 13:44:28 -0700101 pctx.StaticVariable("ClangExtraCflags", strings.Join([]string{
Colin Cross3f40fa42015-01-30 17:27:36 -0800102 "-D__compiler_offsetof=__builtin_offsetof",
103
Yi Kong1b0ba942019-03-19 20:05:05 -0700104 // 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 Hines2210e722020-07-15 11:11:57 -0700108 // 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 Cross3f40fa42015-01-30 17:27:36 -0800112 // Help catch common 32/64-bit errors.
113 "-Werror=int-conversion",
114
Yi Kong2fc32482019-04-22 22:33:23 -0700115 // Enable the new pass manager.
116 "-fexperimental-new-pass-manager",
117
Colin Cross74d1ec02015-04-28 13:30:13 -0700118 // Disable overly aggressive warning for macros defined with a leading underscore
119 // This happens in AndroidConfig.h, which is included nearly everywhere.
Dan Willemsen3bf6b472015-09-11 17:41:10 -0700120 // TODO: can we remove this now?
Colin Cross74d1ec02015-04-28 13:30:13 -0700121 "-Wno-reserved-id-macro",
122
Colin Cross3f40fa42015-01-30 17:27:36 -0800123 // 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 Willemsene6540452015-10-20 15:21:33 -0700127 // 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 Nainarb6572b12016-06-28 10:56:03 -0700130
Chih-Hung Hsieh3ede2942018-01-10 14:30:44 -0800131 // Warnings from clang-7.0
Chih-Hung Hsieh3ede2942018-01-10 14:30:44 -0800132 "-Wno-sign-compare",
Yi Kong53ed59e2018-10-30 15:31:38 -0700133
134 // Warnings from clang-8.0
135 "-Wno-defaulted-function-deleted",
Colin Cross3f40fa42015-01-30 17:27:36 -0800136
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800137 // Disable -Winconsistent-missing-override until we can clean up the existing
138 // codebase for it.
139 "-Wno-inconsistent-missing-override",
Nick Desaulnierseb207442019-12-12 10:15:42 -0800140
141 // Warnings from clang-10
142 // Nested and array designated initialization is nice to have.
143 "-Wno-c99-designator",
Jiyong Park423e3782020-08-11 09:35:08 +0900144
Chih-Hung Hsieh72e88762021-02-03 17:52:16 -0800145 // Warnings from clang-12
146 "-Wno-gnu-folding-constant",
147
Jiyong Park423e3782020-08-11 09:35:08 +0900148 // Calls to the APIs that are newer than the min sdk version of the caller should be
149 // guarded with __builtin_available.
150 "-Wunguarded-availability",
151 // This macro allows the bionic versioning.h to indirectly determine whether the
152 // option -Wunguarded-availability is on or not.
153 "-D__ANDROID_UNGUARDED_AVAILABILITY__",
Yi Kong4d048d52019-03-27 18:26:22 -0700154 }, " "))
Pirama Arumuga Nainarb6572b12016-06-28 10:56:03 -0700155
Yi Kong4d048d52019-03-27 18:26:22 -0700156 pctx.StaticVariable("ClangExtraCppflags", strings.Join([]string{
Nick Desaulniers4e31fb82019-10-30 13:55:26 -0700157 // -Wimplicit-fallthrough is not enabled by -Wall.
158 "-Wimplicit-fallthrough",
159
Josh Gaoe0b933b2017-04-26 20:26:14 -0700160 // Enable clang's thread-safety annotations in libcxx.
Josh Gaoe0b933b2017-04-26 20:26:14 -0700161 "-D_LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS",
Dan Albertf2ceea72018-02-07 17:24:42 -0800162
163 // libc++'s math.h has an #include_next outside of system_headers.
164 "-Wno-gnu-include-next",
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800165 }, " "))
166
Colin Crossb98c8b02016-07-29 13:44:28 -0700167 pctx.StaticVariable("ClangExtraTargetCflags", strings.Join([]string{
Colin Cross3f40fa42015-01-30 17:27:36 -0800168 "-nostdlibinc",
169 }, " "))
Dan Willemsenbe03f342016-03-03 17:21:04 -0800170
Colin Crossb98c8b02016-07-29 13:44:28 -0700171 pctx.StaticVariable("ClangExtraNoOverrideCflags", strings.Join([]string{
Dan Willemsenbe03f342016-03-03 17:21:04 -0800172 "-Werror=address-of-temporary",
Pirama Arumuga Nainarb6572b12016-06-28 10:56:03 -0700173 // Bug: http://b/29823425 Disable -Wnull-dereference until the
174 // new cases detected by this warning in Clang r271374 are
175 // fixed.
176 //"-Werror=null-dereference",
Dan Willemsenbe03f342016-03-03 17:21:04 -0800177 "-Werror=return-type",
Yi Kong599a6032017-12-06 19:56:34 -0800178
179 // http://b/72331526 Disable -Wtautological-* until the instances detected by these
180 // new warnings are fixed.
Stephen Hinesa42e0a02018-02-06 14:49:42 -0800181 "-Wno-tautological-constant-compare",
Chih-Hung Hsieh3ede2942018-01-10 14:30:44 -0800182 "-Wno-tautological-type-limit-compare",
Nick Desaulnierseb207442019-12-12 10:15:42 -0800183 // http://b/145210666
184 "-Wno-reorder-init-list",
185 // http://b/145211066
186 "-Wno-implicit-int-float-conversion",
Chih-Hung Hsieh9d9555e2020-02-10 19:01:14 -0800187 // New warnings to be fixed after clang-r377782.
Chih-Hung Hsieh5f78d552020-02-14 10:10:22 -0800188 "-Wno-int-in-bool-context", // http://b/148287349
189 "-Wno-sizeof-array-div", // http://b/148815709
190 "-Wno-tautological-overlap-compare", // http://b/148815696
Yabin Cui8ec05ff2020-04-10 13:36:41 -0700191 // New warnings to be fixed after clang-r383902.
192 "-Wno-deprecated-copy", // http://b/153746672
193 "-Wno-range-loop-construct", // http://b/153747076
194 "-Wno-misleading-indentation", // http://b/153746954
195 "-Wno-zero-as-null-pointer-constant", // http://b/68236239
196 "-Wno-deprecated-anon-enum-enum-conversion", // http://b/153746485
197 "-Wno-deprecated-enum-enum-conversion", // http://b/153746563
198 "-Wno-string-compare", // http://b/153764102
199 "-Wno-enum-enum-conversion", // http://b/154138986
200 "-Wno-enum-float-conversion", // http://b/154255917
201 "-Wno-pessimizing-move", // http://b/154270751
Stephen Hines2210e722020-07-15 11:11:57 -0700202 // New warnings to be fixed after clang-r399163
203 "-Wno-non-c-typedef-for-linkage", // http://b/161304145
Yabin Cuidb7dda82020-11-30 15:47:45 -0800204 // New warnings to be fixed after clang-r407598
205 "-Wno-string-concatenation", // http://b/175068488
Dan Willemsenbe03f342016-03-03 17:21:04 -0800206 }, " "))
Yi Kongcc80f8d2018-06-06 14:42:44 -0700207
Yi Kong950e0ba2019-10-08 02:27:17 -0700208 // Extra cflags for external third-party projects to disable warnings that
209 // are infeasible to fix in all the external projects and their upstream repos.
Yi Kongcc80f8d2018-06-06 14:42:44 -0700210 pctx.StaticVariable("ClangExtraExternalCflags", strings.Join([]string{
Yi Kongcf4cbdd2018-06-29 20:20:38 +0000211 "-Wno-enum-compare",
212 "-Wno-enum-compare-switch",
Yi Kong3e88cb02018-12-13 03:55:29 -0800213
214 // http://b/72331524 Allow null pointer arithmetic until the instances detected by
215 // this new warning are fixed.
216 "-Wno-null-pointer-arithmetic",
Yi Kongfae5dac2018-12-17 17:18:37 -0800217
218 // Bug: http://b/29823425 Disable -Wnull-dereference until the
219 // new instances detected by this warning are fixed.
220 "-Wno-null-dereference",
Nick Desaulnierseb207442019-12-12 10:15:42 -0800221
222 // http://b/145211477
223 "-Wno-pointer-compare",
224 // http://b/145211022
225 "-Wno-xor-used-as-pow",
226 // http://b/145211022
227 "-Wno-final-dtor-non-final-class",
Stephen Hines2210e722020-07-15 11:11:57 -0700228
229 // http://b/165945989
230 "-Wno-psabi",
Yi Kongcc80f8d2018-06-06 14:42:44 -0700231 }, " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800232}
233
Colin Crossb98c8b02016-07-29 13:44:28 -0700234func ClangFilterUnknownCflags(cflags []string) []string {
Dan Albertd12afec2020-08-14 16:53:21 -0700235 result, _ := android.FilterList(cflags, ClangUnknownCflags)
236 return result
237}
238
239func clangTidyNegateChecks(checks []string) []string {
240 ret := make([]string, 0, len(checks))
241 for _, c := range checks {
242 if strings.HasPrefix(c, "-") {
243 ret = append(ret, c)
244 } else {
245 ret = append(ret, "-"+c)
Colin Cross3f40fa42015-01-30 17:27:36 -0800246 }
247 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800248 return ret
249}
250
Dan Albertd12afec2020-08-14 16:53:21 -0700251func ClangRewriteTidyChecks(checks []string) []string {
252 checks = append(checks, clangTidyNegateChecks(ClangTidyDisableChecks)...)
253 // clang-tidy does not allow later arguments to override earlier arguments,
254 // so if we just disabled an argument that was explicitly enabled we must
255 // remove the enabling argument from the list.
256 result, _ := android.FilterList(checks, ClangTidyDisableChecks)
257 return result
258}
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -0700259
Dan Albertd12afec2020-08-14 16:53:21 -0700260func ClangFilterUnknownLldflags(lldflags []string) []string {
261 result, _ := android.FilterList(lldflags, ClangUnknownLldflags)
262 return result
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -0700263}
264
Yo Chiang8aa4e3f2020-11-19 16:30:49 +0800265func ClangLibToolingFilterUnknownCflags(libToolingFlags []string) []string {
266 return android.RemoveListFromList(libToolingFlags, ClangLibToolingUnknownCflags)
267}
268
Colin Cross3f40fa42015-01-30 17:27:36 -0800269func inListSorted(s string, list []string) bool {
270 for _, l := range list {
271 if s == l {
272 return true
273 } else if s < l {
274 return false
275 }
276 }
277 return false
278}
Dan Willemsene6540452015-10-20 15:21:33 -0700279
280func sorted(list []string) []string {
281 sort.Strings(list)
282 return list
283}