[llvm] [llvm] add LLVM_FRIEND_ABI macro for friend function decls (PR #136595)

Andrew Rogers via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 21 17:09:35 PDT 2025


https://github.com/andrurogerz updated https://github.com/llvm/llvm-project/pull/136595

>From 3d0fd6c7c08af77d4cdf86c6161fda608db2593f Mon Sep 17 00:00:00 2001
From: Andrew Rogers <andrurogerz at gmail.com>
Date: Mon, 21 Apr 2025 10:26:23 -0700
Subject: [PATCH 1/2] [llvm] add LLVM_FRIEND_ABI macro to annotate friend
 function decls

---
 llvm/docs/InterfaceExportAnnotations.rst | 13 +++++++------
 llvm/include/llvm/Support/Compiler.h     | 10 ++++++++++
 2 files changed, 17 insertions(+), 6 deletions(-)

diff --git a/llvm/docs/InterfaceExportAnnotations.rst b/llvm/docs/InterfaceExportAnnotations.rst
index eecf6ffe6eaca..715472f644763 100644
--- a/llvm/docs/InterfaceExportAnnotations.rst
+++ b/llvm/docs/InterfaceExportAnnotations.rst
@@ -222,9 +222,10 @@ method in a C++ class, it may be annotated for export.
 
 Friend Functions
 ~~~~~~~~~~~~~~~~
-Friend functions declared in a class, struct or union must be annotated with
-``LLVM_ABI`` if the corresponding function declaration is also annotated. This
-requirement applies even when the class itself is annotated with ``LLVM_ABI``.
+Friend functions declared in a class, struct or union should be annotated with
+``LLVM_FRIEND_ABI`` if the corresponding function declaration is annotated with
+``LLVM_ABI``. This requirement applies even when the class containing the friend
+declaration is annotated with ``LLVM_ABI``.
 
 .. code:: cpp
 
@@ -236,14 +237,14 @@ requirement applies even when the class itself is annotated with ``LLVM_ABI``.
    class ExampleClass {
      // Friend declaration of a function must be annotated the same as the actual
      // function declaration.
-     LLVM_ABI friend int friend_function(ExampleClass &obj);
+     LLVM_FRIEND_ABI friend int friend_function(ExampleClass &obj);
    };
 
 .. note::
 
    Annotating the friend declaration avoids an “inconsistent dll linkage”
-   compiler error when building for Windows. This annotation is harmless but not
-   required when building ELF or Mach-O shared libraries.
+   compiler error when building a DLL for Windows. The ``LLVM_FRIEND_ABI``
+   annotation is a no-op when building ELF or Mach-O shared libraries.
 
 Virtual Table and Type Info
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
diff --git a/llvm/include/llvm/Support/Compiler.h b/llvm/include/llvm/Support/Compiler.h
index d265d864228ca..0f29299a1f0b8 100644
--- a/llvm/include/llvm/Support/Compiler.h
+++ b/llvm/include/llvm/Support/Compiler.h
@@ -167,6 +167,11 @@
 /// for both functions and classes. On windows its turned in to dllimport for
 /// library consumers, for other platforms its a default visibility attribute.
 ///
+/// LLVM_FRIEND_ABI is for annotating friend function declarations when the
+/// target function's original declaration is annotated with LLVM_ABI. This
+/// macro matches the LLVM_ABI macro on Windows, on other platforms it does
+/// nothing.
+///
 /// LLVM_C_ABI is used to annotated functions and data that need to be exported
 /// for the libllvm-c API. This used both for the llvm-c headers and for the
 /// functions declared in the different Target's c++ source files that don't
@@ -183,6 +188,7 @@
 // missing symbol linker errors on windows.
 #if defined(LLVM_BUILD_STATIC)
 #define LLVM_ABI
+#define LLVM_FRIEND_ABI
 #define LLVM_TEMPLATE_ABI
 #define LLVM_EXPORT_TEMPLATE
 #define LLVM_ABI_EXPORT
@@ -196,21 +202,25 @@
 #define LLVM_TEMPLATE_ABI __declspec(dllimport)
 #define LLVM_EXPORT_TEMPLATE
 #endif
+#define LLVM_FRIEND_ABI LLVM_ABI
 #define LLVM_ABI_EXPORT __declspec(dllexport)
 #elif defined(__ELF__) || defined(__MINGW32__) || defined(_AIX) ||             \
     defined(__MVS__)
 #define LLVM_ABI LLVM_ATTRIBUTE_VISIBILITY_DEFAULT
+#define LLVM_FRIEND_ABI
 #define LLVM_TEMPLATE_ABI LLVM_ATTRIBUTE_VISIBILITY_DEFAULT
 #define LLVM_EXPORT_TEMPLATE
 #define LLVM_ABI_EXPORT LLVM_ATTRIBUTE_VISIBILITY_DEFAULT
 #elif defined(__MACH__) || defined(__WASM__) || defined(__EMSCRIPTEN__)
 #define LLVM_ABI LLVM_ATTRIBUTE_VISIBILITY_DEFAULT
+#define LLVM_FRIEND_ABI
 #define LLVM_TEMPLATE_ABI
 #define LLVM_EXPORT_TEMPLATE
 #define LLVM_ABI_EXPORT LLVM_ATTRIBUTE_VISIBILITY_DEFAULT
 #endif
 #else
 #define LLVM_ABI
+#define LLVM_FRIEND_ABI
 #define LLVM_TEMPLATE_ABI
 #define LLVM_EXPORT_TEMPLATE
 #define LLVM_ABI_EXPORT

>From c046bca05e2ab3ef2c6960bde8a9ac9231aeb3a2 Mon Sep 17 00:00:00 2001
From: Andrew Rogers <andrurogerz at gmail.com>
Date: Mon, 21 Apr 2025 17:09:23 -0700
Subject: [PATCH 2/2] PR feedback: rename LLVM_FRIEND_ABI -> LLVM_ABI_FRIEND

---
 llvm/docs/InterfaceExportAnnotations.rst |  6 +++---
 llvm/include/llvm/Support/Compiler.h     | 12 ++++++------
 2 files changed, 9 insertions(+), 9 deletions(-)

diff --git a/llvm/docs/InterfaceExportAnnotations.rst b/llvm/docs/InterfaceExportAnnotations.rst
index 715472f644763..581fcd8513185 100644
--- a/llvm/docs/InterfaceExportAnnotations.rst
+++ b/llvm/docs/InterfaceExportAnnotations.rst
@@ -223,7 +223,7 @@ method in a C++ class, it may be annotated for export.
 Friend Functions
 ~~~~~~~~~~~~~~~~
 Friend functions declared in a class, struct or union should be annotated with
-``LLVM_FRIEND_ABI`` if the corresponding function declaration is annotated with
+``LLVM_ABI_FRIEND`` if the corresponding function declaration is annotated with
 ``LLVM_ABI``. This requirement applies even when the class containing the friend
 declaration is annotated with ``LLVM_ABI``.
 
@@ -237,13 +237,13 @@ declaration is annotated with ``LLVM_ABI``.
    class ExampleClass {
      // Friend declaration of a function must be annotated the same as the actual
      // function declaration.
-     LLVM_FRIEND_ABI friend int friend_function(ExampleClass &obj);
+     LLVM_ABI_FRIEND friend int friend_function(ExampleClass &obj);
    };
 
 .. note::
 
    Annotating the friend declaration avoids an “inconsistent dll linkage”
-   compiler error when building a DLL for Windows. The ``LLVM_FRIEND_ABI``
+   compiler error when building a DLL for Windows. The ``LLVM_ABI_FRIEND``
    annotation is a no-op when building ELF or Mach-O shared libraries.
 
 Virtual Table and Type Info
diff --git a/llvm/include/llvm/Support/Compiler.h b/llvm/include/llvm/Support/Compiler.h
index 0f29299a1f0b8..4864071ed87a8 100644
--- a/llvm/include/llvm/Support/Compiler.h
+++ b/llvm/include/llvm/Support/Compiler.h
@@ -167,7 +167,7 @@
 /// for both functions and classes. On windows its turned in to dllimport for
 /// library consumers, for other platforms its a default visibility attribute.
 ///
-/// LLVM_FRIEND_ABI is for annotating friend function declarations when the
+/// LLVM_ABI_FRIEND is for annotating friend function declarations when the
 /// target function's original declaration is annotated with LLVM_ABI. This
 /// macro matches the LLVM_ABI macro on Windows, on other platforms it does
 /// nothing.
@@ -188,7 +188,7 @@
 // missing symbol linker errors on windows.
 #if defined(LLVM_BUILD_STATIC)
 #define LLVM_ABI
-#define LLVM_FRIEND_ABI
+#define LLVM_ABI_FRIEND
 #define LLVM_TEMPLATE_ABI
 #define LLVM_EXPORT_TEMPLATE
 #define LLVM_ABI_EXPORT
@@ -202,25 +202,25 @@
 #define LLVM_TEMPLATE_ABI __declspec(dllimport)
 #define LLVM_EXPORT_TEMPLATE
 #endif
-#define LLVM_FRIEND_ABI LLVM_ABI
+#define LLVM_ABI_FRIEND LLVM_ABI
 #define LLVM_ABI_EXPORT __declspec(dllexport)
 #elif defined(__ELF__) || defined(__MINGW32__) || defined(_AIX) ||             \
     defined(__MVS__)
 #define LLVM_ABI LLVM_ATTRIBUTE_VISIBILITY_DEFAULT
-#define LLVM_FRIEND_ABI
+#define LLVM_ABI_FRIEND
 #define LLVM_TEMPLATE_ABI LLVM_ATTRIBUTE_VISIBILITY_DEFAULT
 #define LLVM_EXPORT_TEMPLATE
 #define LLVM_ABI_EXPORT LLVM_ATTRIBUTE_VISIBILITY_DEFAULT
 #elif defined(__MACH__) || defined(__WASM__) || defined(__EMSCRIPTEN__)
 #define LLVM_ABI LLVM_ATTRIBUTE_VISIBILITY_DEFAULT
-#define LLVM_FRIEND_ABI
+#define LLVM_ABI_FRIEND
 #define LLVM_TEMPLATE_ABI
 #define LLVM_EXPORT_TEMPLATE
 #define LLVM_ABI_EXPORT LLVM_ATTRIBUTE_VISIBILITY_DEFAULT
 #endif
 #else
 #define LLVM_ABI
-#define LLVM_FRIEND_ABI
+#define LLVM_ABI_FRIEND
 #define LLVM_TEMPLATE_ABI
 #define LLVM_EXPORT_TEMPLATE
 #define LLVM_ABI_EXPORT



More information about the llvm-commits mailing list