@@ -37,12 +37,12 @@ ensure the proper set of public symbols is exported and visible to clients.
37
37
Annotation Macros
38
38
-----------------
39
39
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.
44
44
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
46
46
<https://github.com/llvm/llvm-project/blob/main/llvm/include/llvm/Support/Compiler.h#L152> `__
47
47
for this purpose:
48
48
@@ -83,8 +83,8 @@ building source that is part of the LLVM shared library (e.g. source under
83
83
symbol from a different LLVM project (such as Clang) it would always resolve to
84
84
``__declspec(dllimport) `` and the symbol would not be properly exported.
85
85
86
- Annotating Symbols
87
- ------------------
86
+ How to Annotate Symbols
87
+ -----------------------
88
88
Functions
89
89
~~~~~~~~~
90
90
Exported function declarations in header files must be annotated with
@@ -157,10 +157,9 @@ declaration must be annotated with ``LLVM_ABI``.
157
157
static constexpr int initializedConstexprStaticField = 0;
158
158
};
159
159
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.
164
163
165
164
.. code :: cpp
166
165
@@ -222,7 +221,7 @@ method in a C++ class, it may be annotated for export.
222
221
223
222
Friend Functions
224
223
~~~~~~~~~~~~~~~~
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
226
225
``LLVM_ABI_FRIEND `` if the corresponding function declaration is annotated with
227
226
``LLVM_ABI ``. This requirement applies even when the class containing the friend
228
227
declaration is annotated with ``LLVM_ABI ``.
@@ -248,24 +247,27 @@ declaration is annotated with ``LLVM_ABI``.
248
247
249
248
Virtual Table and Type Info
250
249
~~~~~~~~~~~~~~~~~~~~~~~~~~~
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 ``.
255
257
256
258
.. code :: cpp
257
259
258
260
#include "llvm/Support/Compiler.h"
259
261
260
- class ParentClass {
262
+ // Annotating the class exports vtable and type information as well as all
263
+ // class members.
264
+ class LLVM_ABI ParentClass {
261
265
public:
262
266
virtual int virtualMethod(int a, int b);
263
267
virtual int anotherVirtualMethod(int a, int b);
264
268
virtual ~ParentClass();
265
269
};
266
270
267
- // Annotating the class exports vtable and type information as well as all
268
- // class members.
269
271
class LLVM_ABI ChildClass : public ParentClass {
270
272
public:
271
273
// Inline method override does not require the class be annotated.
@@ -274,30 +276,52 @@ class members.
274
276
}
275
277
276
278
// Overriding a virtual method from the parent requires the class be
277
- // annotated. The parent class may require annotation as well.
279
+ // annotated.
278
280
int pureVirtualMethod(int a, int b) override;
281
+
279
282
~ChildClass();
280
283
};
281
284
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:
287
306
288
307
.. code :: cpp
289
308
290
309
#include "llvm/Support/Compiler.h"
291
310
292
- #include <vector>
293
-
294
311
class LLVM_ABI ExportedClass {
295
312
public:
313
+ ExportedClass() = default;
314
+
296
315
// Explicitly delete the copy constructor and assignment operator.
297
316
ExportedClass(ExportedClass const&) = delete;
298
317
ExportedClass& operator=(ExportedClass const&) = delete;
299
318
};
300
319
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
+
301
325
Templates
302
326
~~~~~~~~~
303
327
Most template classes are entirely header-defined and do not need to be exported
0 commit comments