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

Saleem Abdulrasool via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 9 12:52:50 PST 2026


================
@@ -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)
----------------
compnerd wrote:

But, that is the point - `CLANG_ENABLE_CLANG_EXPORT_ANNOTATIONS` and `CLANG_EXPORTS` need not be two separate axis. `CLANG_EXPORTS` -> `__declspec(dllexport)` on Windows, `__attribute__((__visibility__("default")))` on other platforms. The only bit that we need beyond that is `CLANG_STATIC`, which primarily we use on Windows only.


```c++
#if defined(CLANG_STATIC)
# define CLANG_ABI /**/
#else
# if defined(_WIN32)
#   if defined(CLANG_EXPORTS)
#     define CLANG_ABI __declspec(dllexport)
#   else
#     define CLANG_ABI __declspec(dllimport)
#   endif
# elif defined(__linux__)
#   if defined(CLANG_EXPORTS)
#     define CLANG_ABI __attribute__((__visibility__("protected"))
#   else
#     define CLANG_ABI __attribute__((__visibility__("default")))
#   endif
# else
#   define CLANG_ABI __attribute__((__visibility__("default")))
# endif
#endif
```

The idea is that we assume that there are two ways to build a library, and the default is shared. If you build statically, we assume that you are not going to re-export the interface, because that is already incorrect! If you want to re-expose the ABI, then that needs to be gated on the behaviour of your consumer. That is, if library B exposes library A's interface, it _should_ expose that through `B_ABI`. You _could_ do that as `-DA_ABI=B_ABI`.

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


More information about the cfe-commits mailing list