Skip to content

Commit c049583

Browse files
authored
[llvm] add LLVM_ABI_FRIEND macro for friend function decls (#136595)
## Purpose Introduce a new `LLVM_ABI_FRIEND` macro to `llvm/Support/Compiler.h` for annotating `friend` function declarations for DLL export. ## Overview 1. Add a new `LLVM_ABI_FRIEND` macro, which behaves identically to the existing `LLVM_ABI` macro on Windows and compiles to nothing on other platforms. 2. Update existing documentation to describe proper usage of the `LLVM_ABI_FRIEND` annotation. ## Background * MSVC issues a warning when it encounters a `friend` function declaration that does not match the DLL import/export annotation of the original function. * When compiling ELF and Mach-O shared libraries, `friend` function declarations with visibility annotations produce compilation errors (GCC) and warnings (Clang). * Additional context on the effort to annotate LLVM's public interface is in [this discourse](https://discourse.llvm.org/t/psa-annotating-llvm-public-interface/85307).
1 parent ab4e181 commit c049583

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

llvm/docs/InterfaceExportAnnotations.rst

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,10 @@ method in a C++ class, it may be annotated for export.
222222

223223
Friend Functions
224224
~~~~~~~~~~~~~~~~
225-
Friend functions declared in a class, struct or union must be annotated with
226-
``LLVM_ABI`` if the corresponding function declaration is also annotated. This
227-
requirement applies even when the class itself is annotated with ``LLVM_ABI``.
225+
Friend functions declared in a class, struct or union should be annotated with
226+
``LLVM_ABI_FRIEND`` if the corresponding function declaration is annotated with
227+
``LLVM_ABI``. This requirement applies even when the class containing the friend
228+
declaration is annotated with ``LLVM_ABI``.
228229

229230
.. code:: cpp
230231
@@ -236,14 +237,14 @@ requirement applies even when the class itself is annotated with ``LLVM_ABI``.
236237
class ExampleClass {
237238
// Friend declaration of a function must be annotated the same as the actual
238239
// function declaration.
239-
LLVM_ABI friend int friend_function(ExampleClass &obj);
240+
LLVM_ABI_FRIEND friend int friend_function(ExampleClass &obj);
240241
};
241242
242243
.. note::
243244

244245
Annotating the friend declaration avoids an “inconsistent dll linkage”
245-
compiler error when building for Windows. This annotation is harmless but not
246-
required when building ELF or Mach-O shared libraries.
246+
compiler error when building a DLL for Windows. The ``LLVM_ABI_FRIEND``
247+
annotation is a no-op when building ELF or Mach-O shared libraries.
247248

248249
Virtual Table and Type Info
249250
~~~~~~~~~~~~~~~~~~~~~~~~~~~

llvm/include/llvm/Support/Compiler.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,11 @@
167167
/// for both functions and classes. On windows its turned in to dllimport for
168168
/// library consumers, for other platforms its a default visibility attribute.
169169
///
170+
/// LLVM_ABI_FRIEND is for annotating friend function declarations when the
171+
/// target function's original declaration is annotated with LLVM_ABI. This
172+
/// macro matches the LLVM_ABI macro on Windows, on other platforms it does
173+
/// nothing.
174+
///
170175
/// LLVM_C_ABI is used to annotated functions and data that need to be exported
171176
/// for the libllvm-c API. This used both for the llvm-c headers and for the
172177
/// functions declared in the different Target's c++ source files that don't
@@ -183,6 +188,7 @@
183188
// missing symbol linker errors on windows.
184189
#if defined(LLVM_BUILD_STATIC)
185190
#define LLVM_ABI
191+
#define LLVM_ABI_FRIEND
186192
#define LLVM_TEMPLATE_ABI
187193
#define LLVM_EXPORT_TEMPLATE
188194
#define LLVM_ABI_EXPORT
@@ -196,21 +202,25 @@
196202
#define LLVM_TEMPLATE_ABI __declspec(dllimport)
197203
#define LLVM_EXPORT_TEMPLATE
198204
#endif
205+
#define LLVM_ABI_FRIEND LLVM_ABI
199206
#define LLVM_ABI_EXPORT __declspec(dllexport)
200207
#elif defined(__ELF__) || defined(__MINGW32__) || defined(_AIX) || \
201208
defined(__MVS__)
202209
#define LLVM_ABI LLVM_ATTRIBUTE_VISIBILITY_DEFAULT
210+
#define LLVM_ABI_FRIEND
203211
#define LLVM_TEMPLATE_ABI LLVM_ATTRIBUTE_VISIBILITY_DEFAULT
204212
#define LLVM_EXPORT_TEMPLATE
205213
#define LLVM_ABI_EXPORT LLVM_ATTRIBUTE_VISIBILITY_DEFAULT
206214
#elif defined(__MACH__) || defined(__WASM__) || defined(__EMSCRIPTEN__)
207215
#define LLVM_ABI LLVM_ATTRIBUTE_VISIBILITY_DEFAULT
216+
#define LLVM_ABI_FRIEND
208217
#define LLVM_TEMPLATE_ABI
209218
#define LLVM_EXPORT_TEMPLATE
210219
#define LLVM_ABI_EXPORT LLVM_ATTRIBUTE_VISIBILITY_DEFAULT
211220
#endif
212221
#else
213222
#define LLVM_ABI
223+
#define LLVM_ABI_FRIEND
214224
#define LLVM_TEMPLATE_ABI
215225
#define LLVM_EXPORT_TEMPLATE
216226
#define LLVM_ABI_EXPORT

0 commit comments

Comments
 (0)