[clang] [llvm] [clang] Clean up the Clang export annotations (PR #239750)

via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 10 03:22:12 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-support

Author: Fabrice de Gans (Steelskin)

<details>
<summary>Changes</summary>

This shuffles `clang/Support/Compiler.h` to more closely match `llvm/Support/Compiler.h`. In particular, this adds a `clang-config.h` header defining `CLANG_ENABLE_CLANG_EXPORT_ANNOTATIONS` to simplify the `CLANG_ABI` and friends definitions.

This also narrows down the compile definitions to the MSVC/clang-cl clang-cpp dylib build. Since this configuration is still a work in progress tracked in #<!-- -->238871, this makes `CLANG_ABI` and friends expand to nothing outside of an MSVC `CLANG_LINK_CLANG_DYLIB` build. As a side effect, this removes many PCH-related warnings concerning these compile definitions.

This is a preliminary change to support PCHs in the LLVM dylib build on Windows, tracked in #<!-- -->226993.

LLM-aided with Claude Code.

---
Full diff: https://github.com/llvm/llvm-project/pull/239750.diff


10 Files Affected:

- (modified) clang/CMakeLists.txt (+14) 
- (modified) clang/cmake/modules/AddClang.cmake (+6-8) 
- (added) clang/include/clang/Config/clang-config.h.cmake (+21) 
- (modified) clang/include/clang/Support/Compiler.h (+28-31) 
- (modified) clang/tools/libclang/CMakeLists.txt (+1-10) 
- (modified) llvm/include/llvm/Support/Registry.h (+1-1) 
- (modified) llvm/utils/gn/build/BUILD.gn (-1) 
- (modified) llvm/utils/gn/secondary/clang/include/clang/Config/BUILD.gn (+7) 
- (modified) utils/bazel/llvm-project-overlay/clang/BUILD.bazel (+2) 
- (added) utils/bazel/llvm-project-overlay/clang/include/clang/Config/clang-config.h (+19) 


``````````diff
diff --git a/clang/CMakeLists.txt b/clang/CMakeLists.txt
index fbb4cfaf090059..f54506e1bd02c1 100644
--- a/clang/CMakeLists.txt
+++ b/clang/CMakeLists.txt
@@ -388,6 +388,17 @@ if (NOT LLVM_LINK_LLVM_DYLIB AND CLANG_LINK_CLANG_DYLIB)
                       "LLVM_LINK_LLVM_DYLIB=OFF")
 endif()
 
+# Whether CLANG_ABI and related macros annotate clang's public interface.
+# Work to support this configuration is ongoing.
+# See https://github.com/llvm/llvm-project/issues/238871
+if((WIN32 AND NOT MINGW) AND CLANG_LINK_CLANG_DYLIB)
+  set(CLANG_ENABLE_CLANG_EXPORT_ANNOTATIONS ON)
+  set(CLANG_COMPONENT_LIB_DEFINITIONS CLANG_EXPORTS)
+else()
+  set(CLANG_ENABLE_CLANG_EXPORT_ANNOTATIONS OFF)
+  set(CLANG_COMPONENT_LIB_DEFINITIONS "")
+endif()
+
 # The libdir suffix must exactly match whatever LLVM's configuration used.
 set(CLANG_LIBDIR_SUFFIX "${LLVM_LIBDIR_SUFFIX}")
 
@@ -1045,3 +1056,6 @@ set(CLANG_INSTALL_LIBDIR_BASENAME "lib${CLANG_LIBDIR_SUFFIX}")
 configure_file(
   ${CLANG_SOURCE_DIR}/include/clang/Config/config.h.cmake
   ${CLANG_BINARY_DIR}/include/clang/Config/config.h)
+configure_file(
+  ${CLANG_SOURCE_DIR}/include/clang/Config/clang-config.h.cmake
+  ${CLANG_BINARY_DIR}/include/clang/Config/clang-config.h)
diff --git a/clang/cmake/modules/AddClang.cmake b/clang/cmake/modules/AddClang.cmake
index 42abfc32aea758..ed895ad2c33e01 100644
--- a/clang/cmake/modules/AddClang.cmake
+++ b/clang/cmake/modules/AddClang.cmake
@@ -108,16 +108,14 @@ macro(add_clang_library name)
   endif()
   llvm_add_library(${name} ${LIBTYPE} ${ARG_UNPARSED_ARGUMENTS} ${srcs})
 
-  if((WIN32 AND NOT MINGW) AND NOT CLANG_LINK_CLANG_DYLIB)
-    # Make sure all consumers also turn off visibility macros so they're not
-    # trying to dllimport symbols.
-    target_compile_definitions(${name} PUBLIC CLANG_BUILD_STATIC)
+  if(NOT ARG_SHARED AND NOT ARG_STATIC)
+    # Add the CLANG_COMPONENT_LIB_DEFINITIONS to the compile definitions for
+    # this target. These set whether the Clang API is annotated for export.
+    target_compile_definitions(${name} PRIVATE ${CLANG_COMPONENT_LIB_DEFINITIONS})
     if(TARGET "obj.${name}")
-      target_compile_definitions("obj.${name}" PUBLIC CLANG_BUILD_STATIC)
+      target_compile_definitions("obj.${name}" PRIVATE
+                                 ${CLANG_COMPONENT_LIB_DEFINITIONS})
     endif()
-  elseif(TARGET "obj.${name}" AND NOT ARG_SHARED AND NOT ARG_STATIC)
-    # Clang component libraries linked to clang-cpp are declared without SHARED or STATIC
-    target_compile_definitions("obj.${name}" PUBLIC CLANG_EXPORTS)
   endif()
 
   set(libs ${name})
diff --git a/clang/include/clang/Config/clang-config.h.cmake b/clang/include/clang/Config/clang-config.h.cmake
new file mode 100644
index 00000000000000..a54d188714bac7
--- /dev/null
+++ b/clang/include/clang/Config/clang-config.h.cmake
@@ -0,0 +1,21 @@
+/*===------- clang/Config/clang-config.h - clang configuration ----*- C -*-===*/
+/*                                                                            */
+/* Part of the LLVM Project, under the Apache License v2.0 with LLVM          */
+/* Exceptions.                                                                */
+/* See https://llvm.org/LICENSE.txt for license information.                  */
+/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception                    */
+/*                                                                            */
+/*===----------------------------------------------------------------------===*/
+
+/* This file enumerates variables from the clang configuration so that they
+   can be used in exported headers. Unlike clang/Config/config.h, it is public
+   and may be included from any header. */
+
+#ifndef CLANG_CONFIG_CLANG_CONFIG_H
+#define CLANG_CONFIG_CLANG_CONFIG_H
+
+/* Define if the export annotations (CLANG_ABI, ...) of the clang public
+   interface are enabled. */
+#cmakedefine CLANG_ENABLE_CLANG_EXPORT_ANNOTATIONS
+
+#endif
diff --git a/clang/include/clang/Support/Compiler.h b/clang/include/clang/Support/Compiler.h
index 66d6c5b30e7b72..8266455278a136 100644
--- a/clang/include/clang/Support/Compiler.h
+++ b/clang/include/clang/Support/Compiler.h
@@ -14,7 +14,7 @@
 #ifndef CLANG_SUPPORT_COMPILER_H
 #define CLANG_SUPPORT_COMPILER_H
 
-#include "llvm/Support/Compiler.h"
+#include "clang/Config/clang-config.h"
 
 /// CLANG_ABI is the main export/visibility macro to mark something as
 /// explicitly exported when clang is built as a shared library with everything
@@ -33,41 +33,38 @@
 /// is used where a symbol must be exported by the defining library but must not
 /// be imported by consumers.
 #ifndef CLANG_ABI_GENERATING_ANNOTATIONS
+
 // Marker to add to classes or functions in public headers that should not have
 // export macros added to them by the clang tool
 #define CLANG_ABI_NOT_EXPORTED
-// Some libraries like those for tablegen are linked in to tools that used
-// in the build so can't depend on the llvm shared library. If export macros
-// were left enabled when building these we would get duplicate or
-// missing symbol linker errors on windows.
-#if defined(CLANG_BUILD_STATIC)
-#define CLANG_ABI
-#define CLANG_ABI_EXPORT
-#define CLANG_TEMPLATE_ABI
-#define CLANG_EXPORT_TEMPLATE
-#elif defined(_WIN32) && !defined(__MINGW32__)
-#if defined(CLANG_EXPORTS)
-#define CLANG_ABI __declspec(dllexport)
-#define CLANG_TEMPLATE_ABI
-#define CLANG_EXPORT_TEMPLATE __declspec(dllexport)
+
+// clang-format off
+#if defined(CLANG_ENABLE_CLANG_EXPORT_ANNOTATIONS)
+# if defined(_WIN32) && !defined(__MINGW32__)
+#  if defined(CLANG_EXPORTS)
+#   define CLANG_ABI __declspec(dllexport)
+#   define CLANG_TEMPLATE_ABI
+#   define CLANG_EXPORT_TEMPLATE __declspec(dllexport)
+#  else
+#   define CLANG_ABI __declspec(dllimport)
+#   define CLANG_TEMPLATE_ABI __declspec(dllimport)
+#   define CLANG_EXPORT_TEMPLATE
+#  endif
+#  define CLANG_ABI_EXPORT __declspec(dllexport)
+# else
+#  define CLANG_ABI __attribute__((visibility("default")))
+#  define CLANG_TEMPLATE_ABI CLANG_ABI
+#  define CLANG_EXPORT_TEMPLATE
+#  define CLANG_ABI_EXPORT CLANG_ABI
+# endif
 #else
-#define CLANG_ABI __declspec(dllimport)
-#define CLANG_TEMPLATE_ABI __declspec(dllimport)
-#define CLANG_EXPORT_TEMPLATE
-#endif
-#define CLANG_ABI_EXPORT __declspec(dllexport)
-#elif defined(__ELF__) || defined(__MINGW32__) || defined(_AIX) ||             \
-    defined(__MVS__) || defined(__CYGWIN__)
-#define CLANG_ABI LLVM_ATTRIBUTE_VISIBILITY_DEFAULT
-#define CLANG_ABI_EXPORT LLVM_ATTRIBUTE_VISIBILITY_DEFAULT
-#define CLANG_TEMPLATE_ABI LLVM_ATTRIBUTE_VISIBILITY_DEFAULT
-#define CLANG_EXPORT_TEMPLATE
-#elif defined(__MACH__) || defined(__wasm__) || defined(__EMSCRIPTEN__)
-#define CLANG_ABI LLVM_ATTRIBUTE_VISIBILITY_DEFAULT
-#define CLANG_ABI_EXPORT LLVM_ATTRIBUTE_VISIBILITY_DEFAULT
-#define CLANG_TEMPLATE_ABI
-#define CLANG_EXPORT_TEMPLATE
+# define CLANG_ABI
+# define CLANG_TEMPLATE_ABI
+# define CLANG_EXPORT_TEMPLATE
+# define CLANG_ABI_EXPORT
 #endif
+// clang-format on
+
 #endif
 
 #endif
diff --git a/clang/tools/libclang/CMakeLists.txt b/clang/tools/libclang/CMakeLists.txt
index 0f78e2913b4ef9..68487a0f9316d6 100644
--- a/clang/tools/libclang/CMakeLists.txt
+++ b/clang/tools/libclang/CMakeLists.txt
@@ -160,16 +160,7 @@ if(ENABLE_STATIC)
 endif()
 
 if(ENABLE_SHARED)
-  if(WIN32)
-    set_target_properties(libclang
-      PROPERTIES
-      VERSION ${LIBCLANG_LIBRARY_VERSION}
-      DEFINE_SYMBOL _CINDEX_LIB_)
-      # Avoid declaring clang c++ symbols that are statically linked into libclang as dllimport'ed.
-      # If llvm/libclang-cpp dll is also being built for windows clang c++ symbols will still be
-      # implicitly be exported from libclang.
-      target_compile_definitions(libclang PRIVATE CLANG_BUILD_STATIC)
-  elseif(APPLE)
+  if(APPLE)
     if(LLVM_VERSIONED_DYLIB_NAME_ON_DARWIN)
       set_target_properties(libclang PROPERTIES
         VERSION ${LIBCLANG_LIBRARY_VERSION}
diff --git a/llvm/include/llvm/Support/Registry.h b/llvm/include/llvm/Support/Registry.h
index 53f84396edf33c..99a45e68ba6de1 100644
--- a/llvm/include/llvm/Support/Registry.h
+++ b/llvm/include/llvm/Support/Registry.h
@@ -283,7 +283,7 @@ template <typename T, typename... CtorParamTypes> class Registry {
 /// EXPORT_ABI must export-or-be-empty, NEVER dllimport, for the same reason
 /// `LLVM_ABI_EXPORT` is used here rather than `LLVM_ABI`: the definition may be
 /// in the same object, a static library, or an import library.
-/// `CLANG_ABI_EXPORT` satisfies this (it is empty under `CLANG_BUILD_STATIC`).
+/// `CLANG_ABI_EXPORT` satisfies this.
 #define LLVM_DECLARE_REGISTRY_EX(EXPORT_ABI, REGISTRY_CLASS)                   \
   namespace llvm::detail {                                                     \
   template <>                                                                  \
diff --git a/llvm/utils/gn/build/BUILD.gn b/llvm/utils/gn/build/BUILD.gn
index be59fc1e83d4b2..d0899f0dcab0b6 100644
--- a/llvm/utils/gn/build/BUILD.gn
+++ b/llvm/utils/gn/build/BUILD.gn
@@ -179,7 +179,6 @@ config("compiler_defaults") {
       "_HAS_EXCEPTIONS=0",
       "_UNICODE",
       "UNICODE",
-      "CLANG_BUILD_STATIC",
     ]
     cflags += [ "/EHs-c-" ]
     cflags_cc += [ "/std:c++17" ]
diff --git a/llvm/utils/gn/secondary/clang/include/clang/Config/BUILD.gn b/llvm/utils/gn/secondary/clang/include/clang/Config/BUILD.gn
index 7b222d2d0e957e..dd1f51fcf72f73 100644
--- a/llvm/utils/gn/secondary/clang/include/clang/Config/BUILD.gn
+++ b/llvm/utils/gn/secondary/clang/include/clang/Config/BUILD.gn
@@ -3,7 +3,14 @@ import("//llvm/utils/gn/build/libs/xml/enable.gni")
 import("//llvm/utils/gn/build/write_cmake_config.gni")
 import("//llvm/version.gni")
 
+write_cmake_config("clang-config") {
+  input = "clang-config.h.cmake"
+  output = "$target_gen_dir/clang-config.h"
+  values = [ "CLANG_ENABLE_CLANG_EXPORT_ANNOTATIONS=" ]
+}
+
 write_cmake_config("Config") {
+  public_deps = [ ":clang-config" ]
   input = "config.h.cmake"
   output = "$target_gen_dir/config.h"
   values = [
diff --git a/utils/bazel/llvm-project-overlay/clang/BUILD.bazel b/utils/bazel/llvm-project-overlay/clang/BUILD.bazel
index bf84c5c8d9cfb0..02ad606b802281 100644
--- a/utils/bazel/llvm-project-overlay/clang/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/clang/BUILD.bazel
@@ -878,6 +878,7 @@ cc_library(
     name = "config",
     hdrs = [
         "include/clang/Basic/Version.inc",
+        "include/clang/Config/clang-config.h",
         "include/clang/Config/config.h",
     ],
     defines = select({
@@ -2326,6 +2327,7 @@ cc_library(
     ]),
     includes = ["include"],
     deps = [
+        ":config",
         "//llvm:Support",
     ],
 )
diff --git a/utils/bazel/llvm-project-overlay/clang/include/clang/Config/clang-config.h b/utils/bazel/llvm-project-overlay/clang/include/clang/Config/clang-config.h
new file mode 100644
index 00000000000000..aebf86683f12a2
--- /dev/null
+++ b/utils/bazel/llvm-project-overlay/clang/include/clang/Config/clang-config.h
@@ -0,0 +1,19 @@
+/*===------- clang/Config/clang-config.h - clang configuration ----*- C -*-===*/
+/*                                                                            */
+/* Part of the LLVM Project, under the Apache License v2.0 with LLVM          */
+/* Exceptions.                                                                */
+/* See https://llvm.org/LICENSE.txt for license information.                  */
+/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception                    */
+/*                                                                            */
+/*===----------------------------------------------------------------------===*/
+
+/* This is a manual port of clang-config.h.cmake. */
+
+#ifndef CLANG_CONFIG_CLANG_CONFIG_H
+#define CLANG_CONFIG_CLANG_CONFIG_H
+
+/* Define if the export annotations (CLANG_ABI, ...) of the clang public
+   interface are enabled. */
+/* #undef CLANG_ENABLE_CLANG_EXPORT_ANNOTATIONS */
+
+#endif

``````````

</details>


https://github.com/llvm/llvm-project/pull/239750


More information about the cfe-commits mailing list