blob: 46d8528800bd8411f1bf98509210a4273903788c [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
52 // arm + arm64 + mips + mips64
53 "-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
71 // mips + mips64
72 "-msynci",
Dan Willemsen3bf6b472015-09-11 17:41:10 -070073 "-mno-synci",
Colin Cross3f40fa42015-01-30 17:27:36 -080074 "-mno-fused-madd",
75
76 // x86 + x86_64
77 "-finline-limit=300",
78 "-fno-inline-functions-called-once",
79 "-mfpmath=sse",
80 "-mbionic",
Dan Willemsen01f388c2017-11-30 13:31:26 -080081
82 // windows
83 "--enable-stdcall-fixup",
Dan Willemsene6540452015-10-20 15:21:33 -070084})
Colin Cross3f40fa42015-01-30 17:27:36 -080085
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -070086// Ldflags that should be filtered out when linking with clang lld
87var ClangUnknownLldflags = sorted([]string{
88 "-fuse-ld=gold",
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -070089 "-Wl,--fix-cortex-a8",
90 "-Wl,--no-fix-cortex-a8",
91 "-Wl,-m,aarch64_elf64_le_vec",
92})
93
Chih-Hung Hsieh3567e622018-11-15 14:01:36 -080094var ClangLibToolingUnknownCflags = sorted([]string{
95 "-fno-sanitize=implicit-integer-sign-change",
96})
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
Chih-Hung Hsieh53390f22018-10-16 09:57:08 -0700102 // -Wimplicit fallthrough is not enabled by -Wall.
Chih-Hung Hsiehbd783f12018-08-14 13:20:38 -0700103 "-Wimplicit-fallthrough",
Chih-Hung Hsiehbd783f12018-08-14 13:20:38 -0700104
Colin Cross3f40fa42015-01-30 17:27:36 -0800105 // Help catch common 32/64-bit errors.
106 "-Werror=int-conversion",
107
Colin Cross74d1ec02015-04-28 13:30:13 -0700108 // Disable overly aggressive warning for macros defined with a leading underscore
109 // This happens in AndroidConfig.h, which is included nearly everywhere.
Dan Willemsen3bf6b472015-09-11 17:41:10 -0700110 // TODO: can we remove this now?
Colin Cross74d1ec02015-04-28 13:30:13 -0700111 "-Wno-reserved-id-macro",
112
113 // Disable overly aggressive warning for format strings.
114 // Bug: 20148343
115 "-Wno-format-pedantic",
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
125 // http://b/29823425 Disable -Wexpansion-to-defined for Clang update to r271374
126 "-Wno-expansion-to-defined",
Dan Willemsen253cab82017-03-27 16:53:38 -0700127
Stephen Hines0ed7d242017-10-04 16:12:37 -0700128 // http://b/68236239 Allow 0/NULL instead of using nullptr everywhere.
129 "-Wno-zero-as-null-pointer-constant",
Chih-Hung Hsieh3ede2942018-01-10 14:30:44 -0800130
131 // Warnings from clang-7.0
132 "-Wno-deprecated-register",
133 "-Wno-sign-compare",
Yi Kong53ed59e2018-10-30 15:31:38 -0700134
135 // Warnings from clang-8.0
136 "-Wno-defaulted-function-deleted",
Colin Cross3f40fa42015-01-30 17:27:36 -0800137 }, " "))
138
Colin Crossb98c8b02016-07-29 13:44:28 -0700139 pctx.StaticVariable("ClangExtraCppflags", strings.Join([]string{
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800140 // Disable -Winconsistent-missing-override until we can clean up the existing
141 // codebase for it.
142 "-Wno-inconsistent-missing-override",
Pirama Arumuga Nainarb6572b12016-06-28 10:56:03 -0700143
144 // Bug: http://b/29823425 Disable -Wnull-dereference until the
145 // new instances detected by this warning are fixed.
146 "-Wno-null-dereference",
Josh Gaoe0b933b2017-04-26 20:26:14 -0700147
148 // Enable clang's thread-safety annotations in libcxx.
149 // Turn off -Wthread-safety-negative, to avoid breaking projects that use -Weverything.
150 "-D_LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS",
151 "-Wno-thread-safety-negative",
Dan Albertf2ceea72018-02-07 17:24:42 -0800152
153 // libc++'s math.h has an #include_next outside of system_headers.
154 "-Wno-gnu-include-next",
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800155 }, " "))
156
Colin Crossb98c8b02016-07-29 13:44:28 -0700157 pctx.StaticVariable("ClangExtraTargetCflags", strings.Join([]string{
Colin Cross3f40fa42015-01-30 17:27:36 -0800158 "-nostdlibinc",
159 }, " "))
Dan Willemsenbe03f342016-03-03 17:21:04 -0800160
Colin Crossb98c8b02016-07-29 13:44:28 -0700161 pctx.StaticVariable("ClangExtraNoOverrideCflags", strings.Join([]string{
Dan Willemsenbe03f342016-03-03 17:21:04 -0800162 "-Werror=address-of-temporary",
Pirama Arumuga Nainarb6572b12016-06-28 10:56:03 -0700163 // 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 Willemsenbe03f342016-03-03 17:21:04 -0800167 "-Werror=return-type",
Yi Kong599a6032017-12-06 19:56:34 -0800168
169 // http://b/72331526 Disable -Wtautological-* until the instances detected by these
170 // new warnings are fixed.
Stephen Hinesa42e0a02018-02-06 14:49:42 -0800171 "-Wno-tautological-constant-compare",
Chih-Hung Hsieh3ede2942018-01-10 14:30:44 -0800172 "-Wno-tautological-type-limit-compare",
173 "-Wno-tautological-unsigned-enum-zero-compare",
174 "-Wno-tautological-unsigned-zero-compare",
Yi Kong599a6032017-12-06 19:56:34 -0800175
176 // http://b/72331524 Allow null pointer arithmetic until the instances detected by
177 // this new warning are fixed.
Stephen Hinesa42e0a02018-02-06 14:49:42 -0800178 "-Wno-null-pointer-arithmetic",
Yi Kong599a6032017-12-06 19:56:34 -0800179
Yi Kongc729b502018-06-29 08:19:46 +0000180 // http://b/72330874 Disable -Wenum-compare until the instances detected by this new
181 // warning are fixed.
182 "-Wno-enum-compare",
183 "-Wno-enum-compare-switch",
184
Kevin Rocarda9ccbb72018-06-05 21:43:46 +0000185 // Disable c++98-specific warning since Android is not concerned with C++98
186 // compatibility.
187 "-Wno-c++98-compat-extra-semi",
Stephen Hinesc91ab9e2018-09-20 18:01:39 -0700188
Martin Stjernholmf993e772018-12-06 12:11:21 +0000189 // Disable this warning until we can fix all instances where it fails.
190 "-Wno-constant-logical-operand",
191
Stephen Hinesc91ab9e2018-09-20 18:01:39 -0700192 // Disable this warning because we don't care about behavior with older compilers.
193 "-Wno-return-std-move-in-c++11",
194
195 // Disable this warning until we can fix all instances where it fails.
196 "-Wno-dangling-field",
Dan Willemsenbe03f342016-03-03 17:21:04 -0800197 }, " "))
Yi Kongcc80f8d2018-06-06 14:42:44 -0700198
Yi Kongc729b502018-06-29 08:19:46 +0000199 // Extra cflags for projects under external/ directory
Yi Kongcc80f8d2018-06-06 14:42:44 -0700200 pctx.StaticVariable("ClangExtraExternalCflags", strings.Join([]string{
Yi Kongc729b502018-06-29 08:19:46 +0000201 // TODO(yikong): Move -Wno flags here
Yi Kongcc80f8d2018-06-06 14:42:44 -0700202 }, " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800203}
204
Colin Crossb98c8b02016-07-29 13:44:28 -0700205func ClangFilterUnknownCflags(cflags []string) []string {
Colin Cross3f40fa42015-01-30 17:27:36 -0800206 ret := make([]string, 0, len(cflags))
207 for _, f := range cflags {
Colin Crossb98c8b02016-07-29 13:44:28 -0700208 if !inListSorted(f, ClangUnknownCflags) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800209 ret = append(ret, f)
210 }
211 }
212
213 return ret
214}
215
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -0700216func ClangFilterUnknownLldflags(lldflags []string) []string {
217 ret := make([]string, 0, len(lldflags))
218 for _, f := range lldflags {
219 if !inListSorted(f, ClangUnknownLldflags) {
220 ret = append(ret, f)
221 }
222 }
223
224 return ret
225}
226
Colin Cross3f40fa42015-01-30 17:27:36 -0800227func inListSorted(s string, list []string) bool {
228 for _, l := range list {
229 if s == l {
230 return true
231 } else if s < l {
232 return false
233 }
234 }
235 return false
236}
Dan Willemsene6540452015-10-20 15:21:33 -0700237
238func sorted(list []string) []string {
239 sort.Strings(list)
240 return list
241}