[clang] [CMake] Enable -Werror=global-constructors for clang/lib (PR #246753)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Nov 27 13:12:17 PST 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Fangrui Song (MaskRay)
<details>
<summary>Changes</summary>
Like #<!-- -->239678, enforce
llvm.org/docs/CodingStandards.html#do-not-use-static-constructors in
Clang builds for clang/lib source files.
List the libraries that still have C++ dynamic initializations so that
the others cannot gain new ones.
LLM-aided
---
Full diff: https://github.com/llvm/llvm-project/pull/246753.diff
1 Files Affected:
- (modified) clang/lib/CMakeLists.txt (+37)
``````````diff
diff --git a/clang/lib/CMakeLists.txt b/clang/lib/CMakeLists.txt
index 22d260e55b271..ebb216595d15f 100644
--- a/clang/lib/CMakeLists.txt
+++ b/clang/lib/CMakeLists.txt
@@ -1,3 +1,10 @@
+# HAS_WERROR_GLOBAL_CTORS and LLVM_HAS_NOGLOBAL_CTOR_MUTEX are set by
+# llvm/lib/Support/CMakeLists.txt.
+if (HAS_WERROR_GLOBAL_CTORS AND LLVM_HAS_NOGLOBAL_CTOR_MUTEX)
+ set(CLANG_LIB_WERROR_GLOBAL_CTORS ON)
+ string(APPEND CMAKE_CXX_FLAGS " -Werror=global-constructors")
+endif()
+
add_subdirectory(Headers)
add_subdirectory(Basic)
add_subdirectory(APINotes)
@@ -37,3 +44,33 @@ add_subdirectory(UnifiedSymbolResolution)
if(CLANG_ENABLE_CIR)
add_subdirectory(CIR)
endif()
+
+# Libraries that still have static initializers. Remove a library once it has
+# none, so that it cannot regain them.
+if (CLANG_LIB_WERROR_GLOBAL_CTORS)
+ foreach(lib
+ SSAFExamplePlugin
+ clangBasic
+ clangDriver
+ clangFormat
+ clangFrontendTool
+ clangInstallAPI
+ clangInterpreter
+ clangScalableStaticAnalysisAnalyses
+ clangScalableStaticAnalysisCore
+ clangScalableStaticAnalysisSourceTransformation
+ clangSema
+ clangSerialization
+ clangStaticAnalyzerCheckers
+ clangStaticAnalyzerCore
+ clangStaticAnalyzerFrontend
+ clangTooling
+ clangToolingInclusions
+ )
+ if (TARGET obj.${lib})
+ target_compile_options(obj.${lib} PRIVATE -Wno-global-constructors)
+ elseif (TARGET ${lib})
+ target_compile_options(${lib} PRIVATE -Wno-global-constructors)
+ endif()
+ endforeach()
+endif()
``````````
</details>
https://github.com/llvm/llvm-project/pull/246753
More information about the cfe-commits
mailing list