clang.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. // 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. package config
  15. import (
  16. "sort"
  17. "strings"
  18. )
  19. // Cflags that should be filtered out when compiling with clang
  20. var ClangUnknownCflags = sorted([]string{
  21. "-finline-functions",
  22. "-finline-limit=64",
  23. "-fno-canonical-system-headers",
  24. "-Wno-clobbered",
  25. "-fno-devirtualize",
  26. "-fno-tree-sra",
  27. "-fprefetch-loop-arrays",
  28. "-funswitch-loops",
  29. "-Werror=unused-but-set-parameter",
  30. "-Werror=unused-but-set-variable",
  31. "-Wmaybe-uninitialized",
  32. "-Wno-error=clobbered",
  33. "-Wno-error=maybe-uninitialized",
  34. "-Wno-error=unused-but-set-parameter",
  35. "-Wno-error=unused-but-set-variable",
  36. "-Wno-extended-offsetof",
  37. "-Wno-free-nonheap-object",
  38. "-Wno-literal-suffix",
  39. "-Wno-maybe-uninitialized",
  40. "-Wno-old-style-declaration",
  41. "-Wno-psabi",
  42. "-Wno-unused-but-set-parameter",
  43. "-Wno-unused-but-set-variable",
  44. "-Wno-unused-local-typedefs",
  45. "-Wunused-but-set-parameter",
  46. "-Wunused-but-set-variable",
  47. "-fdiagnostics-color",
  48. // arm + arm64
  49. "-fgcse-after-reload",
  50. "-frerun-cse-after-loop",
  51. "-frename-registers",
  52. "-fno-strict-volatile-bitfields",
  53. // arm + arm64
  54. "-fno-align-jumps",
  55. // arm
  56. "-mthumb-interwork",
  57. "-fno-builtin-sin",
  58. "-fno-caller-saves",
  59. "-fno-early-inlining",
  60. "-fno-move-loop-invariants",
  61. "-fno-partial-inlining",
  62. "-fno-tree-copy-prop",
  63. "-fno-tree-loop-optimize",
  64. // x86 + x86_64
  65. "-finline-limit=300",
  66. "-fno-inline-functions-called-once",
  67. "-mfpmath=sse",
  68. "-mbionic",
  69. // windows
  70. "--enable-stdcall-fixup",
  71. })
  72. // Ldflags that should be filtered out when linking with clang lld
  73. var ClangUnknownLldflags = sorted([]string{
  74. "-fuse-ld=gold",
  75. "-Wl,--fix-cortex-a8",
  76. "-Wl,--no-fix-cortex-a8",
  77. "-Wl,-m,aarch64_elf64_le_vec",
  78. })
  79. var ClangLibToolingUnknownCflags = sorted([]string{})
  80. func init() {
  81. pctx.StaticVariable("ClangExtraCflags", strings.Join([]string{
  82. "-D__compiler_offsetof=__builtin_offsetof",
  83. // Emit address-significance table which allows linker to perform safe ICF. Clang does
  84. // not emit the table by default on Android since NDK still uses GNU binutils.
  85. "-faddrsig",
  86. // Help catch common 32/64-bit errors.
  87. "-Werror=int-conversion",
  88. // Enable the new pass manager.
  89. "-fexperimental-new-pass-manager",
  90. // Disable overly aggressive warning for macros defined with a leading underscore
  91. // This happens in AndroidConfig.h, which is included nearly everywhere.
  92. // TODO: can we remove this now?
  93. "-Wno-reserved-id-macro",
  94. // Workaround for ccache with clang.
  95. // See http://petereisentraut.blogspot.com/2011/05/ccache-and-clang.html.
  96. "-Wno-unused-command-line-argument",
  97. // Force clang to always output color diagnostics. Ninja will strip the ANSI
  98. // color codes if it is not running in a terminal.
  99. "-fcolor-diagnostics",
  100. // http://b/68236239 Allow 0/NULL instead of using nullptr everywhere.
  101. "-Wno-zero-as-null-pointer-constant",
  102. // Warnings from clang-7.0
  103. "-Wno-sign-compare",
  104. // Warnings from clang-8.0
  105. "-Wno-defaulted-function-deleted",
  106. // Disable -Winconsistent-missing-override until we can clean up the existing
  107. // codebase for it.
  108. "-Wno-inconsistent-missing-override",
  109. // Warnings from clang-10
  110. // Nested and array designated initialization is nice to have.
  111. "-Wno-c99-designator",
  112. }, " "))
  113. pctx.StaticVariable("ClangExtraCppflags", strings.Join([]string{
  114. // -Wimplicit-fallthrough is not enabled by -Wall.
  115. "-Wimplicit-fallthrough",
  116. // Enable clang's thread-safety annotations in libcxx.
  117. "-D_LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS",
  118. // libc++'s math.h has an #include_next outside of system_headers.
  119. "-Wno-gnu-include-next",
  120. }, " "))
  121. pctx.StaticVariable("ClangExtraTargetCflags", strings.Join([]string{
  122. "-nostdlibinc",
  123. }, " "))
  124. pctx.StaticVariable("ClangExtraNoOverrideCflags", strings.Join([]string{
  125. "-Werror=address-of-temporary",
  126. // Bug: http://b/29823425 Disable -Wnull-dereference until the
  127. // new cases detected by this warning in Clang r271374 are
  128. // fixed.
  129. //"-Werror=null-dereference",
  130. "-Werror=return-type",
  131. // http://b/72331526 Disable -Wtautological-* until the instances detected by these
  132. // new warnings are fixed.
  133. "-Wno-tautological-constant-compare",
  134. "-Wno-tautological-type-limit-compare",
  135. // http://b/145210666
  136. "-Wno-reorder-init-list",
  137. // http://b/145211066
  138. "-Wno-implicit-int-float-conversion",
  139. // New warnings to be fixed after clang-r377782.
  140. "-Wno-int-in-bool-context", // http://b/148287349
  141. "-Wno-sizeof-array-div", // http://b/148815709
  142. "-Wno-tautological-overlap-compare", // http://b/148815696
  143. }, " "))
  144. // Extra cflags for external third-party projects to disable warnings that
  145. // are infeasible to fix in all the external projects and their upstream repos.
  146. pctx.StaticVariable("ClangExtraExternalCflags", strings.Join([]string{
  147. "-Wno-enum-compare",
  148. "-Wno-enum-compare-switch",
  149. // http://b/72331524 Allow null pointer arithmetic until the instances detected by
  150. // this new warning are fixed.
  151. "-Wno-null-pointer-arithmetic",
  152. // Bug: http://b/29823425 Disable -Wnull-dereference until the
  153. // new instances detected by this warning are fixed.
  154. "-Wno-null-dereference",
  155. // http://b/145211477
  156. "-Wno-pointer-compare",
  157. // http://b/145211022
  158. "-Wno-xor-used-as-pow",
  159. // http://b/145211022
  160. "-Wno-final-dtor-non-final-class",
  161. }, " "))
  162. }
  163. func ClangFilterUnknownCflags(cflags []string) []string {
  164. ret := make([]string, 0, len(cflags))
  165. for _, f := range cflags {
  166. if !inListSorted(f, ClangUnknownCflags) {
  167. ret = append(ret, f)
  168. }
  169. }
  170. return ret
  171. }
  172. func ClangFilterUnknownLldflags(lldflags []string) []string {
  173. ret := make([]string, 0, len(lldflags))
  174. for _, f := range lldflags {
  175. if !inListSorted(f, ClangUnknownLldflags) {
  176. ret = append(ret, f)
  177. }
  178. }
  179. return ret
  180. }
  181. func 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. }
  191. func sorted(list []string) []string {
  192. sort.Strings(list)
  193. return list
  194. }