blob: 9cfe28f28075c75bf419c27b1e40ecfe185c1c47 [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"
Colin Crossc8bed312021-07-14 17:56:21 -070020
21 "android/soong/android"
Colin Cross3f40fa42015-01-30 17:27:36 -080022)
23
24// Cflags that should be filtered out when compiling with clang
Colin Crossb98c8b02016-07-29 13:44:28 -070025var ClangUnknownCflags = sorted([]string{
Colin Cross3f40fa42015-01-30 17:27:36 -080026 "-finline-functions",
27 "-finline-limit=64",
28 "-fno-canonical-system-headers",
Dan Willemsen3bf6b472015-09-11 17:41:10 -070029 "-Wno-clobbered",
30 "-fno-devirtualize",
Colin Cross3f40fa42015-01-30 17:27:36 -080031 "-fno-tree-sra",
Colin Crossa360e8b2015-03-16 16:22:28 -070032 "-fprefetch-loop-arrays",
Colin Cross3f40fa42015-01-30 17:27:36 -080033 "-funswitch-loops",
Dan Willemsene8c52372016-05-19 16:57:11 -070034 "-Werror=unused-but-set-parameter",
35 "-Werror=unused-but-set-variable",
Colin Cross3f40fa42015-01-30 17:27:36 -080036 "-Wmaybe-uninitialized",
Dan Willemsen3bf6b472015-09-11 17:41:10 -070037 "-Wno-error=clobbered",
Colin Cross3f40fa42015-01-30 17:27:36 -080038 "-Wno-error=maybe-uninitialized",
Colin Cross74d1ec02015-04-28 13:30:13 -070039 "-Wno-error=unused-but-set-parameter",
40 "-Wno-error=unused-but-set-variable",
Chih-Hung Hsieh3ede2942018-01-10 14:30:44 -080041 "-Wno-extended-offsetof",
Colin Cross3f40fa42015-01-30 17:27:36 -080042 "-Wno-free-nonheap-object",
43 "-Wno-literal-suffix",
44 "-Wno-maybe-uninitialized",
45 "-Wno-old-style-declaration",
Colin Cross3f40fa42015-01-30 17:27:36 -080046 "-Wno-unused-but-set-parameter",
Colin Cross74d1ec02015-04-28 13:30:13 -070047 "-Wno-unused-but-set-variable",
Colin Cross3f40fa42015-01-30 17:27:36 -080048 "-Wno-unused-local-typedefs",
Colin Cross62ec5f42015-03-18 17:20:28 -070049 "-Wunused-but-set-parameter",
Colin Cross74d1ec02015-04-28 13:30:13 -070050 "-Wunused-but-set-variable",
Dan Willemsene6540452015-10-20 15:21:33 -070051 "-fdiagnostics-color",
Yabin Cui8ec05ff2020-04-10 13:36:41 -070052 // http://b/153759688
53 "-fuse-init-array",
Colin Cross3f40fa42015-01-30 17:27:36 -080054
Elliott Hughesda3a0712020-03-06 16:55:28 -080055 // arm + arm64
Colin Cross3f40fa42015-01-30 17:27:36 -080056 "-fgcse-after-reload",
57 "-frerun-cse-after-loop",
58 "-frename-registers",
59 "-fno-strict-volatile-bitfields",
60
61 // arm + arm64
62 "-fno-align-jumps",
Colin Cross3f40fa42015-01-30 17:27:36 -080063
64 // arm
65 "-mthumb-interwork",
66 "-fno-builtin-sin",
67 "-fno-caller-saves",
68 "-fno-early-inlining",
69 "-fno-move-loop-invariants",
70 "-fno-partial-inlining",
71 "-fno-tree-copy-prop",
72 "-fno-tree-loop-optimize",
73
Colin Cross3f40fa42015-01-30 17:27:36 -080074 // x86 + x86_64
75 "-finline-limit=300",
76 "-fno-inline-functions-called-once",
77 "-mfpmath=sse",
78 "-mbionic",
Dan Willemsen01f388c2017-11-30 13:31:26 -080079
80 // windows
81 "--enable-stdcall-fixup",
Dan Willemsene6540452015-10-20 15:21:33 -070082})
Colin Cross3f40fa42015-01-30 17:27:36 -080083
Chih-Hung Hsieh1017b372018-12-06 12:12:41 -080084var ClangLibToolingUnknownCflags = sorted([]string{})
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070085
Dan Albertd12afec2020-08-14 16:53:21 -070086// List of tidy checks that should be disabled globally. When the compiler is
87// updated, some checks enabled by this module may be disabled if they have
88// become more strict, or if they are a new match for a wildcard group like
89// `modernize-*`.
Stephen Hines2210e722020-07-15 11:11:57 -070090var ClangTidyDisableChecks = []string{
91 "misc-no-recursion",
Yabin Cuidb7dda82020-11-30 15:47:45 -080092 "readability-function-cognitive-complexity", // http://b/175055536
Stephen Hines2210e722020-07-15 11:11:57 -070093}
Dan Albertd12afec2020-08-14 16:53:21 -070094
Colin Cross3f40fa42015-01-30 17:27:36 -080095func init() {
Jingwen Chen51a1e1c2021-05-20 13:40:14 +000096 exportStringListStaticVariable("ClangExtraCflags", []string{
Colin Cross3f40fa42015-01-30 17:27:36 -080097 "-D__compiler_offsetof=__builtin_offsetof",
98
Yi Kong1b0ba942019-03-19 20:05:05 -070099 // Emit address-significance table which allows linker to perform safe ICF. Clang does
100 // not emit the table by default on Android since NDK still uses GNU binutils.
101 "-faddrsig",
102
Stephen Hines2210e722020-07-15 11:11:57 -0700103 // Turn on -fcommon explicitly, since Clang now defaults to -fno-common. The cleanup bug
104 // tracking this is http://b/151457797.
105 "-fcommon",
106
Colin Cross3f40fa42015-01-30 17:27:36 -0800107 // Help catch common 32/64-bit errors.
108 "-Werror=int-conversion",
109
Yi Kong2fc32482019-04-22 22:33:23 -0700110 // Enable the new pass manager.
111 "-fexperimental-new-pass-manager",
112
Colin Cross74d1ec02015-04-28 13:30:13 -0700113 // Disable overly aggressive warning for macros defined with a leading underscore
114 // This happens in AndroidConfig.h, which is included nearly everywhere.
Dan Willemsen3bf6b472015-09-11 17:41:10 -0700115 // TODO: can we remove this now?
Colin Cross74d1ec02015-04-28 13:30:13 -0700116 "-Wno-reserved-id-macro",
117
Colin Cross3f40fa42015-01-30 17:27:36 -0800118 // Workaround for ccache with clang.
119 // See http://petereisentraut.blogspot.com/2011/05/ccache-and-clang.html.
120 "-Wno-unused-command-line-argument",
121
Dan Willemsene6540452015-10-20 15:21:33 -0700122 // Force clang to always output color diagnostics. Ninja will strip the ANSI
123 // color codes if it is not running in a terminal.
124 "-fcolor-diagnostics",
Pirama Arumuga Nainarb6572b12016-06-28 10:56:03 -0700125
Chih-Hung Hsieh3ede2942018-01-10 14:30:44 -0800126 // Warnings from clang-7.0
Chih-Hung Hsieh3ede2942018-01-10 14:30:44 -0800127 "-Wno-sign-compare",
Yi Kong53ed59e2018-10-30 15:31:38 -0700128
129 // Warnings from clang-8.0
130 "-Wno-defaulted-function-deleted",
Colin Cross3f40fa42015-01-30 17:27:36 -0800131
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800132 // Disable -Winconsistent-missing-override until we can clean up the existing
133 // codebase for it.
134 "-Wno-inconsistent-missing-override",
Nick Desaulnierseb207442019-12-12 10:15:42 -0800135
136 // Warnings from clang-10
137 // Nested and array designated initialization is nice to have.
138 "-Wno-c99-designator",
Jiyong Park423e3782020-08-11 09:35:08 +0900139
Chih-Hung Hsieh72e88762021-02-03 17:52:16 -0800140 // Warnings from clang-12
141 "-Wno-gnu-folding-constant",
142
Jiyong Park423e3782020-08-11 09:35:08 +0900143 // Calls to the APIs that are newer than the min sdk version of the caller should be
144 // guarded with __builtin_available.
145 "-Wunguarded-availability",
146 // This macro allows the bionic versioning.h to indirectly determine whether the
147 // option -Wunguarded-availability is on or not.
Elliott Hughes5add0c62021-02-11 13:20:42 -0800148 "-D__ANDROID_UNAVAILABLE_SYMBOLS_ARE_WEAK__",
Jingwen Chenbf61afb2021-05-06 13:31:18 +0000149 })
Pirama Arumuga Nainarb6572b12016-06-28 10:56:03 -0700150
Colin Cross3f40fa42015-01-30 17:27:36 -0800151}
152
Colin Crossb98c8b02016-07-29 13:44:28 -0700153func ClangFilterUnknownCflags(cflags []string) []string {
Dan Albertd12afec2020-08-14 16:53:21 -0700154 result, _ := android.FilterList(cflags, ClangUnknownCflags)
155 return result
156}
157
158func clangTidyNegateChecks(checks []string) []string {
159 ret := make([]string, 0, len(checks))
160 for _, c := range checks {
161 if strings.HasPrefix(c, "-") {
162 ret = append(ret, c)
163 } else {
164 ret = append(ret, "-"+c)
Colin Cross3f40fa42015-01-30 17:27:36 -0800165 }
166 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800167 return ret
168}
169
Dan Albertd12afec2020-08-14 16:53:21 -0700170func ClangRewriteTidyChecks(checks []string) []string {
171 checks = append(checks, clangTidyNegateChecks(ClangTidyDisableChecks)...)
172 // clang-tidy does not allow later arguments to override earlier arguments,
173 // so if we just disabled an argument that was explicitly enabled we must
174 // remove the enabling argument from the list.
175 result, _ := android.FilterList(checks, ClangTidyDisableChecks)
176 return result
177}
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -0700178
Yo Chiang8aa4e3f2020-11-19 16:30:49 +0800179func ClangLibToolingFilterUnknownCflags(libToolingFlags []string) []string {
180 return android.RemoveListFromList(libToolingFlags, ClangLibToolingUnknownCflags)
181}
182
Dan Willemsene6540452015-10-20 15:21:33 -0700183func sorted(list []string) []string {
184 sort.Strings(list)
185 return list
186}