blob: 2e0b2418320261c58fd2281f732ed5e48137b75c [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 (
18 "sort"
19 "strings"
20)
21
22// Cflags that should be filtered out when compiling with clang
Colin Crossb98c8b02016-07-29 13:44:28 -070023var ClangUnknownCflags = sorted([]string{
Colin Cross3f40fa42015-01-30 17:27:36 -080024 "-finline-functions",
25 "-finline-limit=64",
26 "-fno-canonical-system-headers",
Dan Willemsen3bf6b472015-09-11 17:41:10 -070027 "-Wno-clobbered",
28 "-fno-devirtualize",
Colin Cross3f40fa42015-01-30 17:27:36 -080029 "-fno-tree-sra",
Colin Crossa360e8b2015-03-16 16:22:28 -070030 "-fprefetch-loop-arrays",
Colin Cross3f40fa42015-01-30 17:27:36 -080031 "-funswitch-loops",
Dan Willemsene8c52372016-05-19 16:57:11 -070032 "-Werror=unused-but-set-parameter",
33 "-Werror=unused-but-set-variable",
Colin Cross3f40fa42015-01-30 17:27:36 -080034 "-Wmaybe-uninitialized",
Dan Willemsen3bf6b472015-09-11 17:41:10 -070035 "-Wno-error=clobbered",
Colin Cross3f40fa42015-01-30 17:27:36 -080036 "-Wno-error=maybe-uninitialized",
Colin Cross74d1ec02015-04-28 13:30:13 -070037 "-Wno-error=unused-but-set-parameter",
38 "-Wno-error=unused-but-set-variable",
Chih-Hung Hsieh3ede2942018-01-10 14:30:44 -080039 "-Wno-extended-offsetof",
Colin Cross3f40fa42015-01-30 17:27:36 -080040 "-Wno-free-nonheap-object",
41 "-Wno-literal-suffix",
42 "-Wno-maybe-uninitialized",
43 "-Wno-old-style-declaration",
44 "-Wno-psabi",
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 Cui2547d402020-04-10 13:36:41 -070051 // http://b/153759688
52 "-fuse-init-array",
Colin Cross3f40fa42015-01-30 17:27:36 -080053
54 // arm + arm64 + mips + mips64
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 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
73 // mips + mips64
74 "-msynci",
Dan Willemsen3bf6b472015-09-11 17:41:10 -070075 "-mno-synci",
Colin Cross3f40fa42015-01-30 17:27:36 -080076 "-mno-fused-madd",
77
78 // x86 + x86_64
79 "-finline-limit=300",
80 "-fno-inline-functions-called-once",
81 "-mfpmath=sse",
82 "-mbionic",
Dan Willemsen01f388c2017-11-30 13:31:26 -080083
84 // windows
85 "--enable-stdcall-fixup",
Dan Willemsene6540452015-10-20 15:21:33 -070086})
Colin Cross3f40fa42015-01-30 17:27:36 -080087
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -070088// Ldflags that should be filtered out when linking with clang lld
89var ClangUnknownLldflags = sorted([]string{
90 "-fuse-ld=gold",
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -070091 "-Wl,--fix-cortex-a8",
92 "-Wl,--no-fix-cortex-a8",
93 "-Wl,-m,aarch64_elf64_le_vec",
94})
95
Chih-Hung Hsieh1017b372018-12-06 12:12:41 -080096var ClangLibToolingUnknownCflags = sorted([]string{})
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070097
Colin Cross3f40fa42015-01-30 17:27:36 -080098func init() {
Colin Crossb98c8b02016-07-29 13:44:28 -070099 pctx.StaticVariable("ClangExtraCflags", strings.Join([]string{
Colin Cross3f40fa42015-01-30 17:27:36 -0800100 "-D__compiler_offsetof=__builtin_offsetof",
101
Yi Kong1b0ba942019-03-19 20:05:05 -0700102 // Emit address-significance table which allows linker to perform safe ICF. Clang does
103 // not emit the table by default on Android since NDK still uses GNU binutils.
104 "-faddrsig",
105
Colin Cross3f40fa42015-01-30 17:27:36 -0800106 // Help catch common 32/64-bit errors.
107 "-Werror=int-conversion",
108
Yi Kong2fc32482019-04-22 22:33:23 -0700109 // Enable the new pass manager.
110 "-fexperimental-new-pass-manager",
111
Colin Cross74d1ec02015-04-28 13:30:13 -0700112 // Disable overly aggressive warning for macros defined with a leading underscore
113 // This happens in AndroidConfig.h, which is included nearly everywhere.
Dan Willemsen3bf6b472015-09-11 17:41:10 -0700114 // TODO: can we remove this now?
Colin Cross74d1ec02015-04-28 13:30:13 -0700115 "-Wno-reserved-id-macro",
116
Colin Cross3f40fa42015-01-30 17:27:36 -0800117 // Workaround for ccache with clang.
118 // See http://petereisentraut.blogspot.com/2011/05/ccache-and-clang.html.
119 "-Wno-unused-command-line-argument",
120
Dan Willemsene6540452015-10-20 15:21:33 -0700121 // Force clang to always output color diagnostics. Ninja will strip the ANSI
122 // color codes if it is not running in a terminal.
123 "-fcolor-diagnostics",
Pirama Arumuga Nainarb6572b12016-06-28 10:56:03 -0700124
Chih-Hung Hsieh3ede2942018-01-10 14:30:44 -0800125 // Warnings from clang-7.0
Chih-Hung Hsieh3ede2942018-01-10 14:30:44 -0800126 "-Wno-sign-compare",
Yi Kong53ed59e2018-10-30 15:31:38 -0700127
128 // Warnings from clang-8.0
129 "-Wno-defaulted-function-deleted",
Colin Cross3f40fa42015-01-30 17:27:36 -0800130
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800131 // Disable -Winconsistent-missing-override until we can clean up the existing
132 // codebase for it.
133 "-Wno-inconsistent-missing-override",
Nick Desaulnierseb207442019-12-12 10:15:42 -0800134
135 // Warnings from clang-10
136 // Nested and array designated initialization is nice to have.
137 "-Wno-c99-designator",
Yi Kong4d048d52019-03-27 18:26:22 -0700138 }, " "))
Pirama Arumuga Nainarb6572b12016-06-28 10:56:03 -0700139
Yi Kong4d048d52019-03-27 18:26:22 -0700140 pctx.StaticVariable("ClangExtraCppflags", strings.Join([]string{
Nick Desaulniers4e31fb82019-10-30 13:55:26 -0700141 // -Wimplicit-fallthrough is not enabled by -Wall.
142 "-Wimplicit-fallthrough",
143
Josh Gaoe0b933b2017-04-26 20:26:14 -0700144 // Enable clang's thread-safety annotations in libcxx.
Josh Gaoe0b933b2017-04-26 20:26:14 -0700145 "-D_LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS",
Dan Albertf2ceea72018-02-07 17:24:42 -0800146
147 // libc++'s math.h has an #include_next outside of system_headers.
148 "-Wno-gnu-include-next",
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800149 }, " "))
150
Colin Crossb98c8b02016-07-29 13:44:28 -0700151 pctx.StaticVariable("ClangExtraTargetCflags", strings.Join([]string{
Colin Cross3f40fa42015-01-30 17:27:36 -0800152 "-nostdlibinc",
153 }, " "))
Dan Willemsenbe03f342016-03-03 17:21:04 -0800154
Colin Crossb98c8b02016-07-29 13:44:28 -0700155 pctx.StaticVariable("ClangExtraNoOverrideCflags", strings.Join([]string{
Dan Willemsenbe03f342016-03-03 17:21:04 -0800156 "-Werror=address-of-temporary",
Pirama Arumuga Nainarb6572b12016-06-28 10:56:03 -0700157 // Bug: http://b/29823425 Disable -Wnull-dereference until the
158 // new cases detected by this warning in Clang r271374 are
159 // fixed.
160 //"-Werror=null-dereference",
Dan Willemsenbe03f342016-03-03 17:21:04 -0800161 "-Werror=return-type",
Yi Kong599a6032017-12-06 19:56:34 -0800162
163 // http://b/72331526 Disable -Wtautological-* until the instances detected by these
164 // new warnings are fixed.
Stephen Hinesa42e0a02018-02-06 14:49:42 -0800165 "-Wno-tautological-constant-compare",
Chih-Hung Hsieh3ede2942018-01-10 14:30:44 -0800166 "-Wno-tautological-type-limit-compare",
Nick Desaulnierseb207442019-12-12 10:15:42 -0800167 // http://b/145210666
168 "-Wno-reorder-init-list",
169 // http://b/145211066
170 "-Wno-implicit-int-float-conversion",
Chih-Hung Hsieh9d9555e2020-02-10 19:01:14 -0800171 // New warnings to be fixed after clang-r377782.
Chih-Hung Hsiehde21a352020-02-14 10:10:22 -0800172 "-Wno-int-in-bool-context", // http://b/148287349
173 "-Wno-sizeof-array-div", // http://b/148815709
174 "-Wno-tautological-overlap-compare", // http://b/148815696
Yabin Cui2547d402020-04-10 13:36:41 -0700175 // New warnings to be fixed after clang-r383902.
176 "-Wno-deprecated-copy", // http://b/153746672
177 "-Wno-range-loop-construct", // http://b/153747076
178 "-Wno-misleading-indentation", // http://b/153746954
179 "-Wno-zero-as-null-pointer-constant", // http://b/68236239
180 "-Wno-deprecated-anon-enum-enum-conversion", // http://b/153746485
181 "-Wno-deprecated-enum-enum-conversion", // http://b/153746563
182 "-Wno-string-compare", // http://b/153764102
183 "-Wno-enum-enum-conversion", // http://b/154138986
184 "-Wno-enum-float-conversion", // http://b/154255917
185 "-Wno-pessimizing-move", // http://b/154270751
Dan Willemsenbe03f342016-03-03 17:21:04 -0800186 }, " "))
Yi Kongcc80f8d2018-06-06 14:42:44 -0700187
Yi Kong950e0ba2019-10-08 02:27:17 -0700188 // Extra cflags for external third-party projects to disable warnings that
189 // are infeasible to fix in all the external projects and their upstream repos.
Yi Kongcc80f8d2018-06-06 14:42:44 -0700190 pctx.StaticVariable("ClangExtraExternalCflags", strings.Join([]string{
Yi Kongcf4cbdd2018-06-29 20:20:38 +0000191 "-Wno-enum-compare",
192 "-Wno-enum-compare-switch",
Yi Kong3e88cb02018-12-13 03:55:29 -0800193
194 // http://b/72331524 Allow null pointer arithmetic until the instances detected by
195 // this new warning are fixed.
196 "-Wno-null-pointer-arithmetic",
Yi Kongfae5dac2018-12-17 17:18:37 -0800197
198 // Bug: http://b/29823425 Disable -Wnull-dereference until the
199 // new instances detected by this warning are fixed.
200 "-Wno-null-dereference",
Nick Desaulnierseb207442019-12-12 10:15:42 -0800201
202 // http://b/145211477
203 "-Wno-pointer-compare",
204 // http://b/145211022
205 "-Wno-xor-used-as-pow",
206 // http://b/145211022
207 "-Wno-final-dtor-non-final-class",
Yi Kongcc80f8d2018-06-06 14:42:44 -0700208 }, " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800209}
210
Colin Crossb98c8b02016-07-29 13:44:28 -0700211func ClangFilterUnknownCflags(cflags []string) []string {
Colin Cross3f40fa42015-01-30 17:27:36 -0800212 ret := make([]string, 0, len(cflags))
213 for _, f := range cflags {
Colin Crossb98c8b02016-07-29 13:44:28 -0700214 if !inListSorted(f, ClangUnknownCflags) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800215 ret = append(ret, f)
216 }
217 }
218
219 return ret
220}
221
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -0700222func ClangFilterUnknownLldflags(lldflags []string) []string {
223 ret := make([]string, 0, len(lldflags))
224 for _, f := range lldflags {
225 if !inListSorted(f, ClangUnknownLldflags) {
226 ret = append(ret, f)
227 }
228 }
229
230 return ret
231}
232
Colin Cross3f40fa42015-01-30 17:27:36 -0800233func inListSorted(s string, list []string) bool {
234 for _, l := range list {
235 if s == l {
236 return true
237 } else if s < l {
238 return false
239 }
240 }
241 return false
242}
Dan Willemsene6540452015-10-20 15:21:33 -0700243
244func sorted(list []string) []string {
245 sort.Strings(list)
246 return list
247}