Skip to content

Commit dbe320e

Browse files
authored
[llvm] minor revisions to export annotation macro docs (#138761)
## Purpose Make some minor tweaks and clarifications to the `LLVM_ABI` export annotation docs. ## Overview 1. Minor cleanup on wording throughout 2. Clarification around class-level annotation requirements to export vtable ## Validation Manually inspected docs on [my fork here](https://github.com/andrurogerz/llvm-project/blob/llvm-export-annotation-doc-update/llvm/docs/InterfaceExportAnnotations.rst).
1 parent 45d493b commit dbe320e

File tree

1 file changed

+51
-27
lines changed

1 file changed

+51
-27
lines changed

llvm/docs/InterfaceExportAnnotations.rst

Lines changed: 51 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ ensure the proper set of public symbols is exported and visible to clients.
3737
Annotation Macros
3838
-----------------
3939
The distinct DLL import and export annotations required for Windows DLLs
40-
typically lead developers to define a preprocessor macro for annotating exported
41-
symbols in header public files. The custom macro resolves to the _export_
42-
annotation when building the library and the _import_ annotation when building
43-
the client.
40+
typically lead developers to define a preprocessor macro for annotating
41+
exported symbols in header public files. The custom macro resolves to the
42+
**export** annotation when building the library and the **import** annotation
43+
when building the client.
4444

45-
We have defined the `LLVM_ABI` macro in `llvm/Support/Compiler.h
45+
We have defined the ``LLVM_ABI`` macro in `llvm/Support/Compiler.h
4646
<https://github.com/llvm/llvm-project/blob/main/llvm/include/llvm/Support/Compiler.h#L152>`__
4747
for this purpose:
4848

@@ -83,8 +83,8 @@ building source that is part of the LLVM shared library (e.g. source under
8383
symbol from a different LLVM project (such as Clang) it would always resolve to
8484
``__declspec(dllimport)`` and the symbol would not be properly exported.
8585

86-
Annotating Symbols
87-
------------------
86+
How to Annotate Symbols
87+
-----------------------
8888
Functions
8989
~~~~~~~~~
9090
Exported function declarations in header files must be annotated with
@@ -157,10 +157,9 @@ declaration must be annotated with ``LLVM_ABI``.
157157
static constexpr int initializedConstexprStaticField = 0;
158158
};
159159
160-
Private methods may also require ``LLVM_ABI`` annotation in certain cases. This
161-
situation occurs when a method defined in a header calls the private method. The
162-
private method call may be from within the class, a parent class, or a friend
163-
class.
160+
Private methods may also require ``LLVM_ABI`` annotation. This situation occurs
161+
when a method defined in a header calls the private method. The private method
162+
call may be from within the class or a friend class or method.
164163

165164
.. code:: cpp
166165
@@ -222,7 +221,7 @@ method in a C++ class, it may be annotated for export.
222221

223222
Friend Functions
224223
~~~~~~~~~~~~~~~~
225-
Friend functions declared in a class, struct or union should be annotated with
224+
Friend functions declared in a class, struct or union must be annotated with
226225
``LLVM_ABI_FRIEND`` if the corresponding function declaration is annotated with
227226
``LLVM_ABI``. This requirement applies even when the class containing the friend
228227
declaration is annotated with ``LLVM_ABI``.
@@ -248,24 +247,27 @@ declaration is annotated with ``LLVM_ABI``.
248247

249248
Virtual Table and Type Info
250249
~~~~~~~~~~~~~~~~~~~~~~~~~~~
251-
Classes and structs with exported virtual methods, or child classes that export
252-
overridden virtual methods, must also export their vtable for ELF and Mach-O
253-
builds. This can be achieved by annotating the class rather than individual
254-
class members.
250+
Classes and structs with exported virtual methods, including child classes that
251+
export overridden virtual methods, must also export their vtable for ELF and
252+
Mach-O builds. This can be achieved by annotating the class rather than
253+
individual class members.
254+
255+
The general rule here is to annotate at the class level if any out-of-line
256+
method is declared ``virtual`` or ``override``.
255257

256258
.. code:: cpp
257259
258260
#include "llvm/Support/Compiler.h"
259261
260-
class ParentClass {
262+
// Annotating the class exports vtable and type information as well as all
263+
// class members.
264+
class LLVM_ABI ParentClass {
261265
public:
262266
virtual int virtualMethod(int a, int b);
263267
virtual int anotherVirtualMethod(int a, int b);
264268
virtual ~ParentClass();
265269
};
266270
267-
// Annotating the class exports vtable and type information as well as all
268-
// class members.
269271
class LLVM_ABI ChildClass : public ParentClass {
270272
public:
271273
// Inline method override does not require the class be annotated.
@@ -274,30 +276,52 @@ class members.
274276
}
275277
276278
// Overriding a virtual method from the parent requires the class be
277-
// annotated. The parent class may require annotation as well.
279+
// annotated.
278280
int pureVirtualMethod(int a, int b) override;
281+
279282
~ChildClass();
280283
};
281284
282-
If annotating a type with ``LLVM_ABI`` causes compilation issues such as those
283-
described
284-
`here <https://devblogs.microsoft.com/oldnewthing/20190927-00/?p=102932>`__,
285-
the class may require modification. Often, explicitly deleting the copy
286-
constructor and copy assignment operator will resolve the issue.
285+
.. note::
286+
287+
If a class is annotated, none of its members may be annotated. If class- and
288+
member-level annotations are combined on a class, it will fail compilation on
289+
Windows.
290+
291+
Compilation Errors
292+
++++++++++++++++++
293+
Annotating a class with ``LLVM_ABI`` causes the compiler to fully instantiate
294+
the class at compile time. This requires exporting every method that could be
295+
potentially used by a client even though no existing clients may actually use
296+
them. This can cause compilation errors that were not previously present.
297+
298+
The most common type of error occurs when the compiler attempts to instantiate
299+
and export a class' implicit copy constructor and copy assignment operator. If
300+
the class contains move-only members that cannot be copied (``std::unique_ptr``
301+
for example), the compiler will fail to instantiate these implicit
302+
methods.
303+
304+
This problem is easily addressed by explicitly deleting the class' copy
305+
constructor and copy assignment operator:
287306

288307
.. code:: cpp
289308
290309
#include "llvm/Support/Compiler.h"
291310
292-
#include <vector>
293-
294311
class LLVM_ABI ExportedClass {
295312
public:
313+
ExportedClass() = default;
314+
296315
// Explicitly delete the copy constructor and assignment operator.
297316
ExportedClass(ExportedClass const&) = delete;
298317
ExportedClass& operator=(ExportedClass const&) = delete;
299318
};
300319
320+
We know this modification is harmless because any clients attempting to use
321+
these methods already would fail to compile. For a more detailed explanation,
322+
see `this Microsoft dev blog
323+
<https://devblogs.microsoft.com/oldnewthing/20190927-00/?p=102932>`__.
324+
301325
Templates
302326
~~~~~~~~~
303327
Most template classes are entirely header-defined and do not need to be exported

0 commit comments

Comments
 (0)