Skip to content

[llvm] add LLVM_ABI_FRIEND macro for friend function decls #136595

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions llvm/docs/InterfaceExportAnnotations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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_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``.

.. code:: cpp

Expand All @@ -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_ABI_FRIEND 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_ABI_FRIEND``
annotation is a no-op when building ELF or Mach-O shared libraries.

Virtual Table and Type Info
~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
10 changes: 10 additions & 0 deletions llvm/include/llvm/Support/Compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_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.
///
/// 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
Expand All @@ -183,6 +188,7 @@
// missing symbol linker errors on windows.
#if defined(LLVM_BUILD_STATIC)
#define LLVM_ABI
#define LLVM_ABI_FRIEND
#define LLVM_TEMPLATE_ABI
#define LLVM_EXPORT_TEMPLATE
#define LLVM_ABI_EXPORT
Expand All @@ -196,21 +202,25 @@
#define LLVM_TEMPLATE_ABI __declspec(dllimport)
#define LLVM_EXPORT_TEMPLATE
#endif
#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_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_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_ABI_FRIEND
#define LLVM_TEMPLATE_ABI
#define LLVM_EXPORT_TEMPLATE
#define LLVM_ABI_EXPORT
Expand Down
Loading