blob: 3caa688982d913dfd3306322b03c4cdcfe2de3e2 [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",
34 "-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",
Chih-Hung Hsieh3ede2942018-01-10 14:30:44 -080037 "-Wno-extended-offsetof",
Colin Cross3f40fa42015-01-30 17:27:36 -080038 "-Wno-free-nonheap-object",
39 "-Wno-literal-suffix",
40 "-Wno-maybe-uninitialized",
41 "-Wno-old-style-declaration",
Colin Cross3f40fa42015-01-30 17:27:36 -080042 "-Wno-unused-local-typedefs",
Dan Willemsene6540452015-10-20 15:21:33 -070043 "-fdiagnostics-color",
Yabin Cui8ec05ff2020-04-10 13:36:41 -070044 // http://b/153759688
45 "-fuse-init-array",
Colin Cross3f40fa42015-01-30 17:27:36 -080046
Elliott Hughesda3a0712020-03-06 16:55:28 -080047 // arm + arm64
Colin Cross3f40fa42015-01-30 17:27:36 -080048 "-fgcse-after-reload",
49 "-frerun-cse-after-loop",
50 "-frename-registers",
51 "-fno-strict-volatile-bitfields",
52
53 // arm + arm64
54 "-fno-align-jumps",
Colin Cross3f40fa42015-01-30 17:27:36 -080055
56 // arm
57 "-mthumb-interwork",
58 "-fno-builtin-sin",
59 "-fno-caller-saves",
60 "-fno-early-inlining",
61 "-fno-move-loop-invariants",
62 "-fno-partial-inlining",
63 "-fno-tree-copy-prop",
64 "-fno-tree-loop-optimize",
65
Colin Cross3f40fa42015-01-30 17:27:36 -080066 // x86 + x86_64
67 "-finline-limit=300",
68 "-fno-inline-functions-called-once",
69 "-mfpmath=sse",
70 "-mbionic",
Dan Willemsen01f388c2017-11-30 13:31:26 -080071
72 // windows
73 "--enable-stdcall-fixup",
Dan Willemsene6540452015-10-20 15:21:33 -070074})
Colin Cross3f40fa42015-01-30 17:27:36 -080075
Chih-Hung Hsieh1017b372018-12-06 12:12:41 -080076var ClangLibToolingUnknownCflags = sorted([]string{})
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070077
Dan Albertd12afec2020-08-14 16:53:21 -070078// List of tidy checks that should be disabled globally. When the compiler is
79// updated, some checks enabled by this module may be disabled if they have
80// become more strict, or if they are a new match for a wildcard group like
81// `modernize-*`.
Stephen Hines2210e722020-07-15 11:11:57 -070082var ClangTidyDisableChecks = []string{
83 "misc-no-recursion",
Yabin Cuidb7dda82020-11-30 15:47:45 -080084 "readability-function-cognitive-complexity", // http://b/175055536
Stephen Hines2210e722020-07-15 11:11:57 -070085}
Dan Albertd12afec2020-08-14 16:53:21 -070086
Colin Crossb98c8b02016-07-29 13:44:28 -070087func ClangFilterUnknownCflags(cflags []string) []string {
Dan Albertd12afec2020-08-14 16:53:21 -070088 result, _ := android.FilterList(cflags, ClangUnknownCflags)
89 return result
90}
91
92func clangTidyNegateChecks(checks []string) []string {
93 ret := make([]string, 0, len(checks))
94 for _, c := range checks {
95 if strings.HasPrefix(c, "-") {
96 ret = append(ret, c)
97 } else {
98 ret = append(ret, "-"+c)
Colin Cross3f40fa42015-01-30 17:27:36 -080099 }
100 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800101 return ret
102}
103
Dan Albertd12afec2020-08-14 16:53:21 -0700104func ClangRewriteTidyChecks(checks []string) []string {
105 checks = append(checks, clangTidyNegateChecks(ClangTidyDisableChecks)...)
106 // clang-tidy does not allow later arguments to override earlier arguments,
107 // so if we just disabled an argument that was explicitly enabled we must
108 // remove the enabling argument from the list.
109 result, _ := android.FilterList(checks, ClangTidyDisableChecks)
110 return result
111}
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -0700112
Yo Chiang8aa4e3f2020-11-19 16:30:49 +0800113func ClangLibToolingFilterUnknownCflags(libToolingFlags []string) []string {
114 return android.RemoveListFromList(libToolingFlags, ClangLibToolingUnknownCflags)
115}
116
Dan Willemsene6540452015-10-20 15:21:33 -0700117func sorted(list []string) []string {
118 sort.Strings(list)
119 return list
120}