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	"android/soong/android"
19	"sort"
20	"strings"
21)
22
23// Cflags that should be filtered out when compiling with clang
24var ClangUnknownCflags = sorted([]string{
25	"-finline-functions",
26	"-finline-limit=64",
27	"-fno-canonical-system-headers",
28	"-Wno-clobbered",
29	"-fno-devirtualize",
30	"-fno-tree-sra",
31	"-fprefetch-loop-arrays",
32	"-funswitch-loops",
33	"-Werror=unused-but-set-parameter",
34	"-Werror=unused-but-set-variable",
35	"-Wmaybe-uninitialized",
36	"-Wno-error=clobbered",
37	"-Wno-error=maybe-uninitialized",
38	"-Wno-error=unused-but-set-parameter",
39	"-Wno-error=unused-but-set-variable",
40	"-Wno-extended-offsetof",
41	"-Wno-free-nonheap-object",
42	"-Wno-literal-suffix",
43	"-Wno-maybe-uninitialized",
44	"-Wno-old-style-declaration",
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
91// 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-*`.
95var ClangTidyDisableChecks = []string{
96	"misc-no-recursion",
97	"readability-function-cognitive-complexity", // http://b/175055536
98}
99
100func init() {
101	staticVariableExportedToBazel("ClangExtraCflags", []string{
102		"-D__compiler_offsetof=__builtin_offsetof",
103
104		// 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
108		// Turn on -fcommon explicitly, since Clang now defaults to -fno-common. The cleanup bug
109		// tracking this is http://b/151457797.
110		"-fcommon",
111
112		// Help catch common 32/64-bit errors.
113		"-Werror=int-conversion",
114
115		// Enable the new pass manager.
116		"-fexperimental-new-pass-manager",
117
118		// Disable overly aggressive warning for macros defined with a leading underscore
119		// This happens in AndroidConfig.h, which is included nearly everywhere.
120		// TODO: can we remove this now?
121		"-Wno-reserved-id-macro",
122
123		// Workaround for ccache with clang.
124		// See http://petereisentraut.blogspot.com/2011/05/ccache-and-clang.html.
125		"-Wno-unused-command-line-argument",
126
127		// 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",
130
131		// Warnings from clang-7.0
132		"-Wno-sign-compare",
133
134		// Warnings from clang-8.0
135		"-Wno-defaulted-function-deleted",
136
137		// Disable -Winconsistent-missing-override until we can clean up the existing
138		// codebase for it.
139		"-Wno-inconsistent-missing-override",
140
141		// Warnings from clang-10
142		// Nested and array designated initialization is nice to have.
143		"-Wno-c99-designator",
144
145		// Warnings from clang-12
146		"-Wno-gnu-folding-constant",
147
148		// 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_UNAVAILABLE_SYMBOLS_ARE_WEAK__",
154	})
155
156	staticVariableExportedToBazel("ClangExtraCppflags", []string{
157		// -Wimplicit-fallthrough is not enabled by -Wall.
158		"-Wimplicit-fallthrough",
159
160		// Enable clang's thread-safety annotations in libcxx.
161		"-D_LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS",
162
163		// libc++'s math.h has an #include_next outside of system_headers.
164		"-Wno-gnu-include-next",
165	})
166
167	staticVariableExportedToBazel("ClangExtraTargetCflags", []string{"-nostdlibinc"})
168
169	staticVariableExportedToBazel("ClangExtraNoOverrideCflags", []string{
170		"-Werror=address-of-temporary",
171		// Bug: http://b/29823425 Disable -Wnull-dereference until the
172		// new cases detected by this warning in Clang r271374 are
173		// fixed.
174		//"-Werror=null-dereference",
175		"-Werror=return-type",
176
177		// http://b/72331526 Disable -Wtautological-* until the instances detected by these
178		// new warnings are fixed.
179		"-Wno-tautological-constant-compare",
180		"-Wno-tautological-type-limit-compare",
181		// http://b/145210666
182		"-Wno-reorder-init-list",
183		// http://b/145211066
184		"-Wno-implicit-int-float-conversion",
185		// New warnings to be fixed after clang-r377782.
186		"-Wno-int-in-bool-context",          // http://b/148287349
187		"-Wno-sizeof-array-div",             // http://b/148815709
188		"-Wno-tautological-overlap-compare", // http://b/148815696
189		// New warnings to be fixed after clang-r383902.
190		"-Wno-deprecated-copy",                      // http://b/153746672
191		"-Wno-range-loop-construct",                 // http://b/153747076
192		"-Wno-misleading-indentation",               // http://b/153746954
193		"-Wno-zero-as-null-pointer-constant",        // http://b/68236239
194		"-Wno-deprecated-anon-enum-enum-conversion", // http://b/153746485
195		"-Wno-deprecated-enum-enum-conversion",      // http://b/153746563
196		"-Wno-string-compare",                       // http://b/153764102
197		"-Wno-enum-enum-conversion",                 // http://b/154138986
198		"-Wno-enum-float-conversion",                // http://b/154255917
199		"-Wno-pessimizing-move",                     // http://b/154270751
200		// New warnings to be fixed after clang-r399163
201		"-Wno-non-c-typedef-for-linkage", // http://b/161304145
202		// New warnings to be fixed after clang-r407598
203		"-Wno-string-concatenation", // http://b/175068488
204	})
205
206	// Extra cflags for external third-party projects to disable warnings that
207	// are infeasible to fix in all the external projects and their upstream repos.
208	staticVariableExportedToBazel("ClangExtraExternalCflags", []string{
209		"-Wno-enum-compare",
210		"-Wno-enum-compare-switch",
211
212		// http://b/72331524 Allow null pointer arithmetic until the instances detected by
213		// this new warning are fixed.
214		"-Wno-null-pointer-arithmetic",
215
216		// Bug: http://b/29823425 Disable -Wnull-dereference until the
217		// new instances detected by this warning are fixed.
218		"-Wno-null-dereference",
219
220		// http://b/145211477
221		"-Wno-pointer-compare",
222		// http://b/145211022
223		"-Wno-xor-used-as-pow",
224		// http://b/145211022
225		"-Wno-final-dtor-non-final-class",
226
227		// http://b/165945989
228		"-Wno-psabi",
229	})
230}
231
232func ClangFilterUnknownCflags(cflags []string) []string {
233	result, _ := android.FilterList(cflags, ClangUnknownCflags)
234	return result
235}
236
237func clangTidyNegateChecks(checks []string) []string {
238	ret := make([]string, 0, len(checks))
239	for _, c := range checks {
240		if strings.HasPrefix(c, "-") {
241			ret = append(ret, c)
242		} else {
243			ret = append(ret, "-"+c)
244		}
245	}
246	return ret
247}
248
249func ClangRewriteTidyChecks(checks []string) []string {
250	checks = append(checks, clangTidyNegateChecks(ClangTidyDisableChecks)...)
251	// clang-tidy does not allow later arguments to override earlier arguments,
252	// so if we just disabled an argument that was explicitly enabled we must
253	// remove the enabling argument from the list.
254	result, _ := android.FilterList(checks, ClangTidyDisableChecks)
255	return result
256}
257
258func ClangFilterUnknownLldflags(lldflags []string) []string {
259	result, _ := android.FilterList(lldflags, ClangUnknownLldflags)
260	return result
261}
262
263func ClangLibToolingFilterUnknownCflags(libToolingFlags []string) []string {
264	return android.RemoveListFromList(libToolingFlags, ClangLibToolingUnknownCflags)
265}
266
267func inListSorted(s string, list []string) bool {
268	for _, l := range list {
269		if s == l {
270			return true
271		} else if s < l {
272			return false
273		}
274	}
275	return false
276}
277
278func sorted(list []string) []string {
279	sort.Strings(list)
280	return list
281}
282