[clang] [llvm] [clang] Clean up the Clang export annotations (PR #239750)
Fabrice de Gans via cfe-commits
cfe-commits at lists.llvm.org
Sun Nov 8 23:15:46 PST 2026
https://github.com/Steelskin updated https://github.com/llvm/llvm-project/pull/239750
>From f6cd030baed1c927dd05288436ce8eeddd1805bf Mon Sep 17 00:00:00 2001
From: Fabrice de Gans <steelskin at gmail.com>
Date: Fri, 6 Nov 2026 19:55:55 +0100
Subject: [PATCH] [clang] Clean up the Clang export annotations
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.
---
clang/CMakeLists.txt | 14 +++++++++
clang/cmake/modules/AddClang.cmake | 14 ++++-----
.../include/clang/Config/clang-config.h.cmake | 21 +++++++++++++
clang/include/clang/Support/Compiler.h | 31 ++++++++++---------
.../clang/include/clang/Config/BUILD.gn | 7 +++++
.../llvm-project-overlay/clang/BUILD.bazel | 2 ++
.../clang/include/clang/Config/clang-config.h | 19 ++++++++++++
7 files changed, 86 insertions(+), 22 deletions(-)
create mode 100644 clang/include/clang/Config/clang-config.h.cmake
create mode 100644 utils/bazel/llvm-project-overlay/clang/include/clang/Config/clang-config.h
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..05896eba45b316 100644
--- a/clang/include/clang/Support/Compiler.h
+++ b/clang/include/clang/Support/Compiler.h
@@ -14,6 +14,7 @@
#ifndef CLANG_SUPPORT_COMPILER_H
#define CLANG_SUPPORT_COMPILER_H
+#include "clang/Config/clang-config.h"
#include "llvm/Support/Compiler.h"
/// CLANG_ABI is the main export/visibility macro to mark something as
@@ -40,12 +41,9 @@
// 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_ENABLE_CLANG_EXPORT_ANNOTATIONS) && \
+ !defined(CLANG_BUILD_STATIC)
+#if defined(_WIN32) && !defined(__MINGW32__)
#if defined(CLANG_EXPORTS)
#define CLANG_ABI __declspec(dllexport)
#define CLANG_TEMPLATE_ABI
@@ -56,17 +54,22 @@
#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
+#elif __has_attribute(visibility)
+#if defined(__ELF__) || defined(__MINGW32__) || defined(_AIX) || \
+ defined(__MVS__) || defined(__CYGWIN__) || defined(__MACH__) || \
+ defined(__wasm__) || defined(__EMSCRIPTEN__)
+#define CLANG_ABI __attribute__((visibility("default")))
+#define CLANG_TEMPLATE_ABI CLANG_ABI
#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_ABI_EXPORT CLANG_ABI
+#endif
+#endif
+#endif
+#if !defined(CLANG_ABI)
+#define CLANG_ABI
#define CLANG_TEMPLATE_ABI
#define CLANG_EXPORT_TEMPLATE
+#define CLANG_ABI_EXPORT
#endif
#endif
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 6ec8a4625c28a6..9ab4ecf658f4ff 100644
--- a/utils/bazel/llvm-project-overlay/clang/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/clang/BUILD.bazel
@@ -868,6 +868,7 @@ cc_library(
name = "config",
hdrs = [
"include/clang/Basic/Version.inc",
+ "include/clang/Config/clang-config.h",
"include/clang/Config/config.h",
],
defines = select({
@@ -2316,6 +2317,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
More information about the cfe-commits
mailing list