blob: 2ef153be160148cfba872f8d37fc5a1518f18c20 [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 Willemsene6540452015-10-20 15:21:33 -070080})
Colin Cross3f40fa42015-01-30 17:27:36 -080081
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070082var ClangLibToolingUnknownCflags = []string{
Jayant Chowdhary20f5d132017-10-27 11:29:37 -070083 "-flto*",
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070084 "-fsanitize*",
85}
86
Colin Cross3f40fa42015-01-30 17:27:36 -080087func init() {
Colin Crossb98c8b02016-07-29 13:44:28 -070088 pctx.StaticVariable("ClangExtraCflags", strings.Join([]string{
Colin Cross3f40fa42015-01-30 17:27:36 -080089 "-D__compiler_offsetof=__builtin_offsetof",
90
91 // Help catch common 32/64-bit errors.
92 "-Werror=int-conversion",
93
Colin Cross74d1ec02015-04-28 13:30:13 -070094 // Disable overly aggressive warning for macros defined with a leading underscore
95 // This happens in AndroidConfig.h, which is included nearly everywhere.
Dan Willemsen3bf6b472015-09-11 17:41:10 -070096 // TODO: can we remove this now?
Colin Cross74d1ec02015-04-28 13:30:13 -070097 "-Wno-reserved-id-macro",
98
99 // Disable overly aggressive warning for format strings.
100 // Bug: 20148343
101 "-Wno-format-pedantic",
102
Colin Cross3f40fa42015-01-30 17:27:36 -0800103 // Workaround for ccache with clang.
104 // See http://petereisentraut.blogspot.com/2011/05/ccache-and-clang.html.
105 "-Wno-unused-command-line-argument",
106
Dan Willemsene6540452015-10-20 15:21:33 -0700107 // Force clang to always output color diagnostics. Ninja will strip the ANSI
108 // color codes if it is not running in a terminal.
109 "-fcolor-diagnostics",
Pirama Arumuga Nainarb6572b12016-06-28 10:56:03 -0700110
111 // http://b/29823425 Disable -Wexpansion-to-defined for Clang update to r271374
112 "-Wno-expansion-to-defined",
Dan Willemsen253cab82017-03-27 16:53:38 -0700113
Stephen Hines0ed7d242017-10-04 16:12:37 -0700114 // http://b/68236239 Allow 0/NULL instead of using nullptr everywhere.
115 "-Wno-zero-as-null-pointer-constant",
116
Dan Willemsen253cab82017-03-27 16:53:38 -0700117 // http://b/36463318 Clang executes with an absolute path, so clang-provided
118 // headers are now absolute.
119 "-fdebug-prefix-map=$$PWD/=",
Colin Cross3f40fa42015-01-30 17:27:36 -0800120 }, " "))
121
Colin Crossb98c8b02016-07-29 13:44:28 -0700122 pctx.StaticVariable("ClangExtraCppflags", strings.Join([]string{
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800123 // Disable -Winconsistent-missing-override until we can clean up the existing
124 // codebase for it.
125 "-Wno-inconsistent-missing-override",
Pirama Arumuga Nainarb6572b12016-06-28 10:56:03 -0700126
127 // Bug: http://b/29823425 Disable -Wnull-dereference until the
128 // new instances detected by this warning are fixed.
129 "-Wno-null-dereference",
Josh Gaoe0b933b2017-04-26 20:26:14 -0700130
131 // Enable clang's thread-safety annotations in libcxx.
132 // Turn off -Wthread-safety-negative, to avoid breaking projects that use -Weverything.
133 "-D_LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS",
134 "-Wno-thread-safety-negative",
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800135 }, " "))
136
Colin Crossb98c8b02016-07-29 13:44:28 -0700137 pctx.StaticVariable("ClangExtraTargetCflags", strings.Join([]string{
Colin Cross3f40fa42015-01-30 17:27:36 -0800138 "-nostdlibinc",
139 }, " "))
Dan Willemsenbe03f342016-03-03 17:21:04 -0800140
Colin Crossb98c8b02016-07-29 13:44:28 -0700141 pctx.StaticVariable("ClangExtraNoOverrideCflags", strings.Join([]string{
Dan Willemsenbe03f342016-03-03 17:21:04 -0800142 "-Werror=address-of-temporary",
Pirama Arumuga Nainarb6572b12016-06-28 10:56:03 -0700143 // Bug: http://b/29823425 Disable -Wnull-dereference until the
144 // new cases detected by this warning in Clang r271374 are
145 // fixed.
146 //"-Werror=null-dereference",
Dan Willemsenbe03f342016-03-03 17:21:04 -0800147 "-Werror=return-type",
148 }, " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800149}
150
Colin Crossb98c8b02016-07-29 13:44:28 -0700151func ClangFilterUnknownCflags(cflags []string) []string {
Colin Cross3f40fa42015-01-30 17:27:36 -0800152 ret := make([]string, 0, len(cflags))
153 for _, f := range cflags {
Colin Crossb98c8b02016-07-29 13:44:28 -0700154 if !inListSorted(f, ClangUnknownCflags) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800155 ret = append(ret, f)
156 }
157 }
158
159 return ret
160}
161
162func inListSorted(s string, list []string) bool {
163 for _, l := range list {
164 if s == l {
165 return true
166 } else if s < l {
167 return false
168 }
169 }
170 return false
171}
Dan Willemsene6540452015-10-20 15:21:33 -0700172
173func sorted(list []string) []string {
174 sort.Strings(list)
175 return list
176}