blob: 92019897f84e067c7cdc2f6653cb161ef01899d5 [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",
Colin Cross3f40fa42015-01-30 17:27:36 -080039 "-Wno-free-nonheap-object",
40 "-Wno-literal-suffix",
41 "-Wno-maybe-uninitialized",
42 "-Wno-old-style-declaration",
43 "-Wno-psabi",
Colin Cross3f40fa42015-01-30 17:27:36 -080044 "-Wno-unused-but-set-parameter",
Colin Cross74d1ec02015-04-28 13:30:13 -070045 "-Wno-unused-but-set-variable",
Colin Cross3f40fa42015-01-30 17:27:36 -080046 "-Wno-unused-local-typedefs",
Colin Cross62ec5f42015-03-18 17:20:28 -070047 "-Wunused-but-set-parameter",
Colin Cross74d1ec02015-04-28 13:30:13 -070048 "-Wunused-but-set-variable",
Dan Willemsene6540452015-10-20 15:21:33 -070049 "-fdiagnostics-color",
Colin Cross3f40fa42015-01-30 17:27:36 -080050
51 // arm + arm64 + mips + mips64
52 "-fgcse-after-reload",
53 "-frerun-cse-after-loop",
54 "-frename-registers",
55 "-fno-strict-volatile-bitfields",
56
57 // arm + arm64
58 "-fno-align-jumps",
Colin Cross3f40fa42015-01-30 17:27:36 -080059
60 // arm
61 "-mthumb-interwork",
62 "-fno-builtin-sin",
63 "-fno-caller-saves",
64 "-fno-early-inlining",
65 "-fno-move-loop-invariants",
66 "-fno-partial-inlining",
67 "-fno-tree-copy-prop",
68 "-fno-tree-loop-optimize",
69
70 // mips + mips64
71 "-msynci",
Dan Willemsen3bf6b472015-09-11 17:41:10 -070072 "-mno-synci",
Colin Cross3f40fa42015-01-30 17:27:36 -080073 "-mno-fused-madd",
74
75 // x86 + x86_64
76 "-finline-limit=300",
77 "-fno-inline-functions-called-once",
78 "-mfpmath=sse",
79 "-mbionic",
Dan Willemsen01f388c2017-11-30 13:31:26 -080080
81 // windows
82 "--enable-stdcall-fixup",
Dan Willemsene6540452015-10-20 15:21:33 -070083})
Colin Cross3f40fa42015-01-30 17:27:36 -080084
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070085var ClangLibToolingUnknownCflags = []string{
Jayant Chowdhary20f5d132017-10-27 11:29:37 -070086 "-flto*",
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070087 "-fsanitize*",
88}
89
Colin Cross3f40fa42015-01-30 17:27:36 -080090func init() {
Colin Crossb98c8b02016-07-29 13:44:28 -070091 pctx.StaticVariable("ClangExtraCflags", strings.Join([]string{
Colin Cross3f40fa42015-01-30 17:27:36 -080092 "-D__compiler_offsetof=__builtin_offsetof",
93
94 // Help catch common 32/64-bit errors.
95 "-Werror=int-conversion",
96
Colin Cross74d1ec02015-04-28 13:30:13 -070097 // Disable overly aggressive warning for macros defined with a leading underscore
98 // This happens in AndroidConfig.h, which is included nearly everywhere.
Dan Willemsen3bf6b472015-09-11 17:41:10 -070099 // TODO: can we remove this now?
Colin Cross74d1ec02015-04-28 13:30:13 -0700100 "-Wno-reserved-id-macro",
101
102 // Disable overly aggressive warning for format strings.
103 // Bug: 20148343
104 "-Wno-format-pedantic",
105
Colin Cross3f40fa42015-01-30 17:27:36 -0800106 // Workaround for ccache with clang.
107 // See http://petereisentraut.blogspot.com/2011/05/ccache-and-clang.html.
108 "-Wno-unused-command-line-argument",
109
Dan Willemsene6540452015-10-20 15:21:33 -0700110 // Force clang to always output color diagnostics. Ninja will strip the ANSI
111 // color codes if it is not running in a terminal.
112 "-fcolor-diagnostics",
Pirama Arumuga Nainarb6572b12016-06-28 10:56:03 -0700113
114 // http://b/29823425 Disable -Wexpansion-to-defined for Clang update to r271374
115 "-Wno-expansion-to-defined",
Dan Willemsen253cab82017-03-27 16:53:38 -0700116
Stephen Hines0ed7d242017-10-04 16:12:37 -0700117 // http://b/68236239 Allow 0/NULL instead of using nullptr everywhere.
118 "-Wno-zero-as-null-pointer-constant",
119
Dan Willemsen253cab82017-03-27 16:53:38 -0700120 // http://b/36463318 Clang executes with an absolute path, so clang-provided
121 // headers are now absolute.
122 "-fdebug-prefix-map=$$PWD/=",
Colin Cross3f40fa42015-01-30 17:27:36 -0800123 }, " "))
124
Colin Crossb98c8b02016-07-29 13:44:28 -0700125 pctx.StaticVariable("ClangExtraCppflags", strings.Join([]string{
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800126 // Disable -Winconsistent-missing-override until we can clean up the existing
127 // codebase for it.
128 "-Wno-inconsistent-missing-override",
Pirama Arumuga Nainarb6572b12016-06-28 10:56:03 -0700129
130 // Bug: http://b/29823425 Disable -Wnull-dereference until the
131 // new instances detected by this warning are fixed.
132 "-Wno-null-dereference",
Josh Gaoe0b933b2017-04-26 20:26:14 -0700133
134 // Enable clang's thread-safety annotations in libcxx.
135 // Turn off -Wthread-safety-negative, to avoid breaking projects that use -Weverything.
136 "-D_LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS",
137 "-Wno-thread-safety-negative",
Dan Albertf2ceea72018-02-07 17:24:42 -0800138
139 // libc++'s math.h has an #include_next outside of system_headers.
140 "-Wno-gnu-include-next",
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800141 }, " "))
142
Colin Crossb98c8b02016-07-29 13:44:28 -0700143 pctx.StaticVariable("ClangExtraTargetCflags", strings.Join([]string{
Colin Cross3f40fa42015-01-30 17:27:36 -0800144 "-nostdlibinc",
145 }, " "))
Dan Willemsenbe03f342016-03-03 17:21:04 -0800146
Colin Crossb98c8b02016-07-29 13:44:28 -0700147 pctx.StaticVariable("ClangExtraNoOverrideCflags", strings.Join([]string{
Dan Willemsenbe03f342016-03-03 17:21:04 -0800148 "-Werror=address-of-temporary",
Pirama Arumuga Nainarb6572b12016-06-28 10:56:03 -0700149 // Bug: http://b/29823425 Disable -Wnull-dereference until the
150 // new cases detected by this warning in Clang r271374 are
151 // fixed.
152 //"-Werror=null-dereference",
Dan Willemsenbe03f342016-03-03 17:21:04 -0800153 "-Werror=return-type",
Yi Kong599a6032017-12-06 19:56:34 -0800154
155 // http://b/72331526 Disable -Wtautological-* until the instances detected by these
156 // new warnings are fixed.
Stephen Hinesa42e0a02018-02-06 14:49:42 -0800157 "-Wno-tautological-constant-compare",
Yi Kong599a6032017-12-06 19:56:34 -0800158
159 // http://b/72331524 Allow null pointer arithmetic until the instances detected by
160 // this new warning are fixed.
Stephen Hinesa42e0a02018-02-06 14:49:42 -0800161 "-Wno-null-pointer-arithmetic",
Yi Kong599a6032017-12-06 19:56:34 -0800162
163 // http://b/72330874 Disable -Wenum-compare until the instances detected by this new
164 // warning are fixed.
Stephen Hinesa42e0a02018-02-06 14:49:42 -0800165 "-Wno-enum-compare",
166 "-Wno-enum-compare-switch",
Dan Willemsenbe03f342016-03-03 17:21:04 -0800167 }, " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800168}
169
Colin Crossb98c8b02016-07-29 13:44:28 -0700170func ClangFilterUnknownCflags(cflags []string) []string {
Colin Cross3f40fa42015-01-30 17:27:36 -0800171 ret := make([]string, 0, len(cflags))
172 for _, f := range cflags {
Colin Crossb98c8b02016-07-29 13:44:28 -0700173 if !inListSorted(f, ClangUnknownCflags) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800174 ret = append(ret, f)
175 }
176 }
177
178 return ret
179}
180
181func inListSorted(s string, list []string) bool {
182 for _, l := range list {
183 if s == l {
184 return true
185 } else if s < l {
186 return false
187 }
188 }
189 return false
190}
Dan Willemsene6540452015-10-20 15:21:33 -0700191
192func sorted(list []string) []string {
193 sort.Strings(list)
194 return list
195}