blob: 53a73066e6e75e068f361aae1071d49de1d8d522 [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 Crossb98c8b02016-07-29 13:44:28 -070095func ClangFilterUnknownCflags(cflags []string) []string {
Dan Albertd12afec2020-08-14 16:53:21 -070096 result, _ := android.FilterList(cflags, ClangUnknownCflags)
97 return result
98}
99
100func clangTidyNegateChecks(checks []string) []string {
101 ret := make([]string, 0, len(checks))
102 for _, c := range checks {
103 if strings.HasPrefix(c, "-") {
104 ret = append(ret, c)
105 } else {
106 ret = append(ret, "-"+c)
Colin Cross3f40fa42015-01-30 17:27:36 -0800107 }
108 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800109 return ret
110}
111
Dan Albertd12afec2020-08-14 16:53:21 -0700112func ClangRewriteTidyChecks(checks []string) []string {
113 checks = append(checks, clangTidyNegateChecks(ClangTidyDisableChecks)...)
114 // clang-tidy does not allow later arguments to override earlier arguments,
115 // so if we just disabled an argument that was explicitly enabled we must
116 // remove the enabling argument from the list.
117 result, _ := android.FilterList(checks, ClangTidyDisableChecks)
118 return result
119}
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -0700120
Yo Chiang8aa4e3f2020-11-19 16:30:49 +0800121func ClangLibToolingFilterUnknownCflags(libToolingFlags []string) []string {
122 return android.RemoveListFromList(libToolingFlags, ClangLibToolingUnknownCflags)
123}
124
Dan Willemsene6540452015-10-20 15:21:33 -0700125func sorted(list []string) []string {
126 sort.Strings(list)
127 return list
128}