blob: 274ccd5d5f7030265bb8905346d57f72cf479092 [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",
Colin Cross3f40fa42015-01-30 17:27:36 -080051
Elliott Hughesda3a0712020-03-06 16:55:28 -080052 // arm + arm64
Colin Cross3f40fa42015-01-30 17:27:36 -080053 "-fgcse-after-reload",
54 "-frerun-cse-after-loop",
55 "-frename-registers",
56 "-fno-strict-volatile-bitfields",
57
58 // arm + arm64
59 "-fno-align-jumps",
Colin Cross3f40fa42015-01-30 17:27:36 -080060
61 // arm
62 "-mthumb-interwork",
63 "-fno-builtin-sin",
64 "-fno-caller-saves",
65 "-fno-early-inlining",
66 "-fno-move-loop-invariants",
67 "-fno-partial-inlining",
68 "-fno-tree-copy-prop",
69 "-fno-tree-loop-optimize",
70
Colin Cross3f40fa42015-01-30 17:27:36 -080071 // x86 + x86_64
72 "-finline-limit=300",
73 "-fno-inline-functions-called-once",
74 "-mfpmath=sse",
75 "-mbionic",
Dan Willemsen01f388c2017-11-30 13:31:26 -080076
77 // windows
78 "--enable-stdcall-fixup",
Dan Willemsene6540452015-10-20 15:21:33 -070079})
Colin Cross3f40fa42015-01-30 17:27:36 -080080
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -070081// Ldflags that should be filtered out when linking with clang lld
82var ClangUnknownLldflags = sorted([]string{
83 "-fuse-ld=gold",
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -070084 "-Wl,--fix-cortex-a8",
85 "-Wl,--no-fix-cortex-a8",
86 "-Wl,-m,aarch64_elf64_le_vec",
87})
88
Chih-Hung Hsieh1017b372018-12-06 12:12:41 -080089var ClangLibToolingUnknownCflags = sorted([]string{})
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070090
Colin Cross3f40fa42015-01-30 17:27:36 -080091func init() {
Colin Crossb98c8b02016-07-29 13:44:28 -070092 pctx.StaticVariable("ClangExtraCflags", strings.Join([]string{
Colin Cross3f40fa42015-01-30 17:27:36 -080093 "-D__compiler_offsetof=__builtin_offsetof",
94
Yi Kong1b0ba942019-03-19 20:05:05 -070095 // Emit address-significance table which allows linker to perform safe ICF. Clang does
96 // not emit the table by default on Android since NDK still uses GNU binutils.
97 "-faddrsig",
98
Colin Cross3f40fa42015-01-30 17:27:36 -080099 // Help catch common 32/64-bit errors.
100 "-Werror=int-conversion",
101
Yi Kong2fc32482019-04-22 22:33:23 -0700102 // Enable the new pass manager.
103 "-fexperimental-new-pass-manager",
104
Colin Cross74d1ec02015-04-28 13:30:13 -0700105 // Disable overly aggressive warning for macros defined with a leading underscore
106 // This happens in AndroidConfig.h, which is included nearly everywhere.
Dan Willemsen3bf6b472015-09-11 17:41:10 -0700107 // TODO: can we remove this now?
Colin Cross74d1ec02015-04-28 13:30:13 -0700108 "-Wno-reserved-id-macro",
109
Colin Cross3f40fa42015-01-30 17:27:36 -0800110 // Workaround for ccache with clang.
111 // See http://petereisentraut.blogspot.com/2011/05/ccache-and-clang.html.
112 "-Wno-unused-command-line-argument",
113
Dan Willemsene6540452015-10-20 15:21:33 -0700114 // Force clang to always output color diagnostics. Ninja will strip the ANSI
115 // color codes if it is not running in a terminal.
116 "-fcolor-diagnostics",
Pirama Arumuga Nainarb6572b12016-06-28 10:56:03 -0700117
Stephen Hines0ed7d242017-10-04 16:12:37 -0700118 // http://b/68236239 Allow 0/NULL instead of using nullptr everywhere.
119 "-Wno-zero-as-null-pointer-constant",
Chih-Hung Hsieh3ede2942018-01-10 14:30:44 -0800120
121 // Warnings from clang-7.0
Chih-Hung Hsieh3ede2942018-01-10 14:30:44 -0800122 "-Wno-sign-compare",
Yi Kong53ed59e2018-10-30 15:31:38 -0700123
124 // Warnings from clang-8.0
125 "-Wno-defaulted-function-deleted",
Colin Cross3f40fa42015-01-30 17:27:36 -0800126
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800127 // Disable -Winconsistent-missing-override until we can clean up the existing
128 // codebase for it.
129 "-Wno-inconsistent-missing-override",
Nick Desaulnierseb207442019-12-12 10:15:42 -0800130
131 // Warnings from clang-10
132 // Nested and array designated initialization is nice to have.
133 "-Wno-c99-designator",
Yi Kong4d048d52019-03-27 18:26:22 -0700134 }, " "))
Pirama Arumuga Nainarb6572b12016-06-28 10:56:03 -0700135
Yi Kong4d048d52019-03-27 18:26:22 -0700136 pctx.StaticVariable("ClangExtraCppflags", strings.Join([]string{
Nick Desaulniers4e31fb82019-10-30 13:55:26 -0700137 // -Wimplicit-fallthrough is not enabled by -Wall.
138 "-Wimplicit-fallthrough",
139
Josh Gaoe0b933b2017-04-26 20:26:14 -0700140 // Enable clang's thread-safety annotations in libcxx.
Josh Gaoe0b933b2017-04-26 20:26:14 -0700141 "-D_LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS",
Dan Albertf2ceea72018-02-07 17:24:42 -0800142
143 // libc++'s math.h has an #include_next outside of system_headers.
144 "-Wno-gnu-include-next",
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800145 }, " "))
146
Colin Crossb98c8b02016-07-29 13:44:28 -0700147 pctx.StaticVariable("ClangExtraTargetCflags", strings.Join([]string{
Colin Cross3f40fa42015-01-30 17:27:36 -0800148 "-nostdlibinc",
149 }, " "))
Dan Willemsenbe03f342016-03-03 17:21:04 -0800150
Colin Crossb98c8b02016-07-29 13:44:28 -0700151 pctx.StaticVariable("ClangExtraNoOverrideCflags", strings.Join([]string{
Dan Willemsenbe03f342016-03-03 17:21:04 -0800152 "-Werror=address-of-temporary",
Pirama Arumuga Nainarb6572b12016-06-28 10:56:03 -0700153 // Bug: http://b/29823425 Disable -Wnull-dereference until the
154 // new cases detected by this warning in Clang r271374 are
155 // fixed.
156 //"-Werror=null-dereference",
Dan Willemsenbe03f342016-03-03 17:21:04 -0800157 "-Werror=return-type",
Yi Kong599a6032017-12-06 19:56:34 -0800158
159 // http://b/72331526 Disable -Wtautological-* until the instances detected by these
160 // new warnings are fixed.
Stephen Hinesa42e0a02018-02-06 14:49:42 -0800161 "-Wno-tautological-constant-compare",
Chih-Hung Hsieh3ede2942018-01-10 14:30:44 -0800162 "-Wno-tautological-type-limit-compare",
Nick Desaulnierseb207442019-12-12 10:15:42 -0800163 // http://b/145210666
164 "-Wno-reorder-init-list",
165 // http://b/145211066
166 "-Wno-implicit-int-float-conversion",
Chih-Hung Hsieh9d9555e2020-02-10 19:01:14 -0800167 // New warnings to be fixed after clang-r377782.
Chih-Hung Hsieh5f78d552020-02-14 10:10:22 -0800168 "-Wno-int-in-bool-context", // http://b/148287349
169 "-Wno-sizeof-array-div", // http://b/148815709
170 "-Wno-tautological-overlap-compare", // http://b/148815696
Dan Willemsenbe03f342016-03-03 17:21:04 -0800171 }, " "))
Yi Kongcc80f8d2018-06-06 14:42:44 -0700172
Yi Kong950e0ba2019-10-08 02:27:17 -0700173 // Extra cflags for external third-party projects to disable warnings that
174 // are infeasible to fix in all the external projects and their upstream repos.
Yi Kongcc80f8d2018-06-06 14:42:44 -0700175 pctx.StaticVariable("ClangExtraExternalCflags", strings.Join([]string{
Yi Kongcf4cbdd2018-06-29 20:20:38 +0000176 "-Wno-enum-compare",
177 "-Wno-enum-compare-switch",
Yi Kong3e88cb02018-12-13 03:55:29 -0800178
179 // http://b/72331524 Allow null pointer arithmetic until the instances detected by
180 // this new warning are fixed.
181 "-Wno-null-pointer-arithmetic",
Yi Kongfae5dac2018-12-17 17:18:37 -0800182
183 // Bug: http://b/29823425 Disable -Wnull-dereference until the
184 // new instances detected by this warning are fixed.
185 "-Wno-null-dereference",
Nick Desaulnierseb207442019-12-12 10:15:42 -0800186
187 // http://b/145211477
188 "-Wno-pointer-compare",
189 // http://b/145211022
190 "-Wno-xor-used-as-pow",
191 // http://b/145211022
192 "-Wno-final-dtor-non-final-class",
Yi Kongcc80f8d2018-06-06 14:42:44 -0700193 }, " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800194}
195
Colin Crossb98c8b02016-07-29 13:44:28 -0700196func ClangFilterUnknownCflags(cflags []string) []string {
Colin Cross3f40fa42015-01-30 17:27:36 -0800197 ret := make([]string, 0, len(cflags))
198 for _, f := range cflags {
Colin Crossb98c8b02016-07-29 13:44:28 -0700199 if !inListSorted(f, ClangUnknownCflags) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800200 ret = append(ret, f)
201 }
202 }
203
204 return ret
205}
206
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -0700207func ClangFilterUnknownLldflags(lldflags []string) []string {
208 ret := make([]string, 0, len(lldflags))
209 for _, f := range lldflags {
210 if !inListSorted(f, ClangUnknownLldflags) {
211 ret = append(ret, f)
212 }
213 }
214
215 return ret
216}
217
Colin Cross3f40fa42015-01-30 17:27:36 -0800218func inListSorted(s string, list []string) bool {
219 for _, l := range list {
220 if s == l {
221 return true
222 } else if s < l {
223 return false
224 }
225 }
226 return false
227}
Dan Willemsene6540452015-10-20 15:21:33 -0700228
229func sorted(list []string) []string {
230 sort.Strings(list)
231 return list
232}