Skip to content

Commit 68805af

Browse files
committed
Add documentation and a release note
1 parent 0f8f66e commit 68805af

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ Attribute Changes in Clang
103103
Improvements to Clang's diagnostics
104104
-----------------------------------
105105

106+
- The ``-Wunique-object-duplication`` warning has been added to warn about objects
107+
which are supposed to only exist once per program, but may get duplicated when
108+
built into a shared library.
109+
106110
Improvements to Clang's time-trace
107111
----------------------------------
108112

clang/include/clang/Basic/DiagnosticGroups.td

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,36 @@ def SuspiciousMemaccess : DiagGroup<"suspicious-memaccess",
696696
NonTrivialMemaccess, MemsetTransposedArgs, SuspiciousBzero]>;
697697
def StaticInInline : DiagGroup<"static-in-inline">;
698698
def StaticLocalInInline : DiagGroup<"static-local-in-inline">;
699-
def UniqueObjectDuplication : DiagGroup<"unique-object-duplication">;
699+
def UniqueObjectDuplication : DiagGroup<"unique-object-duplication"> {
700+
code Documentation = [{
701+
Warns when objects which are supposed to be globally unique might get duplicated
702+
when built into a shared library.
703+
704+
If an object with hidden visibility is built into a shared library, each instance
705+
of the library will get its own copy. This can cause very subtle bugs if there was
706+
only supposed to be one copy of the object in question: singletons aren't single,
707+
changes to one object won't affect the others, the object's initializer will run
708+
once per copy, etc.
709+
710+
Specifically, this warning fires when it detects an object which:
711+
1. Appears in a header file (so it might get compiled into multiple libaries), and
712+
2. Has external linkage (otherwise it's supposed to be duplicated), and
713+
3. Has hidden visibility.
714+
715+
As well as one of the following:
716+
1. The object is mutable, or
717+
2. The object's initializer definitely has side effects.
718+
719+
The warning is best resolved by making the object ``const`` (if possible), or by explicitly
720+
giving the object non-hidden visibility, e.g. using ``__attribute((visibility("default")))``.
721+
Note that all levels of a pointer variable must be constant; ``const int*`` will
722+
trigger the warning because the pointer itself is mutable.
723+
724+
This warning is currently disabled on Windows since it uses import/export rules
725+
instead of visibility.
726+
}];
727+
}
728+
700729
def GNUStaticFloatInit : DiagGroup<"gnu-static-float-init">;
701730
def StaticFloatInit : DiagGroup<"static-float-init", [GNUStaticFloatInit]>;
702731
// Allow differentiation between GNU statement expressions in a macro versus

0 commit comments

Comments
 (0)