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
15package config
16
17import (
18	"sort"
19	"strings"
20)
21
22// Cflags that should be filtered out when compiling with clang
23var ClangUnknownCflags = sorted([]string{
24	"-finline-functions",
25	"-finline-limit=64",
26	"-fno-canonical-system-headers",
27	"-Wno-clobbered",
28	"-fno-devirtualize",
29	"-fno-tree-sra",
30	"-fprefetch-loop-arrays",
31	"-funswitch-loops",
32	"-Werror=unused-but-set-parameter",
33	"-Werror=unused-but-set-variable",
34	"-Wmaybe-uninitialized",
35	"-Wno-error=clobbered",
36	"-Wno-error=maybe-uninitialized",
37	"-Wno-error=unused-but-set-parameter",
38	"-Wno-error=unused-but-set-variable",
39	"-Wno-extended-offsetof",
40	"-Wno-free-nonheap-object",
41	"-Wno-literal-suffix",
42	"-Wno-maybe-uninitialized",
43	"-Wno-old-style-declaration",
44	"-Wno-psabi",
45	"-Wno-unused-but-set-parameter",
46	"-Wno-unused-but-set-variable",
47	"-Wno-unused-local-typedefs",
48	"-Wunused-but-set-parameter",
49	"-Wunused-but-set-variable",
50	"-fdiagnostics-color",
51	// http://b/153759688
52	"-fuse-init-array",
53
54	// arm + arm64
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",
62
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	// x86 + x86_64
74	"-finline-limit=300",
75	"-fno-inline-functions-called-once",
76	"-mfpmath=sse",
77	"-mbionic",
78
79	// windows
80	"--enable-stdcall-fixup",
81})
82
83// Ldflags that should be filtered out when linking with clang lld
84var ClangUnknownLldflags = sorted([]string{
85	"-Wl,--fix-cortex-a8",
86	"-Wl,--no-fix-cortex-a8",
87})
88
89var ClangLibToolingUnknownCflags = sorted([]string{})
90
91func init() {
92	pctx.StaticVariable("ClangExtraCflags", strings.Join([]string{
93		"-D__compiler_offsetof=__builtin_offsetof",
94
95		// 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
99		// Help catch common 32/64-bit errors.
100		"-Werror=int-conversion",
101
102		// Enable the new pass manager.
103		"-fexperimental-new-pass-manager",
104
105		// Disable overly aggressive warning for macros defined with a leading underscore
106		// This happens in AndroidConfig.h, which is included nearly everywhere.
107		// TODO: can we remove this now?
108		"-Wno-reserved-id-macro",
109
110		// Workaround for ccache with clang.
111		// See http://petereisentraut.blogspot.com/2011/05/ccache-and-clang.html.
112		"-Wno-unused-command-line-argument",
113
114		// 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",
117
118		// Warnings from clang-7.0
119		"-Wno-sign-compare",
120
121		// Warnings from clang-8.0
122		"-Wno-defaulted-function-deleted",
123
124		// Disable -Winconsistent-missing-override until we can clean up the existing
125		// codebase for it.
126		"-Wno-inconsistent-missing-override",
127
128		// Warnings from clang-10
129		// Nested and array designated initialization is nice to have.
130		"-Wno-c99-designator",
131	}, " "))
132
133	pctx.StaticVariable("ClangExtraCppflags", strings.Join([]string{
134		// -Wimplicit-fallthrough is not enabled by -Wall.
135		"-Wimplicit-fallthrough",
136
137		// Enable clang's thread-safety annotations in libcxx.
138		"-D_LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS",
139
140		// libc++'s math.h has an #include_next outside of system_headers.
141		"-Wno-gnu-include-next",
142	}, " "))
143
144	pctx.StaticVariable("ClangExtraTargetCflags", strings.Join([]string{
145		"-nostdlibinc",
146	}, " "))
147
148	pctx.StaticVariable("ClangExtraNoOverrideCflags", strings.Join([]string{
149		"-Werror=address-of-temporary",
150		// Bug: http://b/29823425 Disable -Wnull-dereference until the
151		// new cases detected by this warning in Clang r271374 are
152		// fixed.
153		//"-Werror=null-dereference",
154		"-Werror=return-type",
155
156		// http://b/72331526 Disable -Wtautological-* until the instances detected by these
157		// new warnings are fixed.
158		"-Wno-tautological-constant-compare",
159		"-Wno-tautological-type-limit-compare",
160		// http://b/145210666
161		"-Wno-reorder-init-list",
162		// http://b/145211066
163		"-Wno-implicit-int-float-conversion",
164		// New warnings to be fixed after clang-r377782.
165		"-Wno-int-in-bool-context",          // http://b/148287349
166		"-Wno-sizeof-array-div",             // http://b/148815709
167		"-Wno-tautological-overlap-compare", // http://b/148815696
168		// New warnings to be fixed after clang-r383902.
169		"-Wno-deprecated-copy",                      // http://b/153746672
170		"-Wno-range-loop-construct",                 // http://b/153747076
171		"-Wno-misleading-indentation",               // http://b/153746954
172		"-Wno-zero-as-null-pointer-constant",        // http://b/68236239
173		"-Wno-deprecated-anon-enum-enum-conversion", // http://b/153746485
174		"-Wno-deprecated-enum-enum-conversion",      // http://b/153746563
175		"-Wno-string-compare",                       // http://b/153764102
176		"-Wno-enum-enum-conversion",                 // http://b/154138986
177		"-Wno-enum-float-conversion",                // http://b/154255917
178		"-Wno-pessimizing-move",                     // http://b/154270751
179	}, " "))
180
181	// Extra cflags for external third-party projects to disable warnings that
182	// are infeasible to fix in all the external projects and their upstream repos.
183	pctx.StaticVariable("ClangExtraExternalCflags", strings.Join([]string{
184		"-Wno-enum-compare",
185		"-Wno-enum-compare-switch",
186
187		// http://b/72331524 Allow null pointer arithmetic until the instances detected by
188		// this new warning are fixed.
189		"-Wno-null-pointer-arithmetic",
190
191		// Bug: http://b/29823425 Disable -Wnull-dereference until the
192		// new instances detected by this warning are fixed.
193		"-Wno-null-dereference",
194
195		// http://b/145211477
196		"-Wno-pointer-compare",
197		// http://b/145211022
198		"-Wno-xor-used-as-pow",
199		// http://b/145211022
200		"-Wno-final-dtor-non-final-class",
201	}, " "))
202}
203
204func ClangFilterUnknownCflags(cflags []string) []string {
205	ret := make([]string, 0, len(cflags))
206	for _, f := range cflags {
207		if !inListSorted(f, ClangUnknownCflags) {
208			ret = append(ret, f)
209		}
210	}
211
212	return ret
213}
214
215func ClangFilterUnknownLldflags(lldflags []string) []string {
216	ret := make([]string, 0, len(lldflags))
217	for _, f := range lldflags {
218		if !inListSorted(f, ClangUnknownLldflags) {
219			ret = append(ret, f)
220		}
221	}
222
223	return ret
224}
225
226func inListSorted(s string, list []string) bool {
227	for _, l := range list {
228		if s == l {
229			return true
230		} else if s < l {
231			return false
232		}
233	}
234	return false
235}
236
237func sorted(list []string) []string {
238	sort.Strings(list)
239	return list
240}
241