Skip to content

Commit cde5d15

Browse files
committed
Address feedback again
1 parent 6356a4f commit cde5d15

File tree

3 files changed

+26
-56
lines changed

3 files changed

+26
-56
lines changed

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6170,12 +6170,12 @@ def note_convert_inline_to_static : Note<
61706170
"use 'static' to give inline function %0 internal linkage">;
61716171

61726172
def warn_possible_object_duplication_mutable : Warning<
6173-
"%0 is mutable, has hidden visibility, and external linkage; it may be "
6174-
"duplicated when built into a shared library">,
6173+
"%0 may be duplicated when built into a shared library: "
6174+
"it is mutable, has hidden visibility, and external linkage">,
61756175
InGroup<UniqueObjectDuplication>, DefaultIgnore;
61766176
def warn_possible_object_duplication_init : Warning<
6177-
"%0 has hidden visibility, and external linkage; its initialization may run "
6178-
"more than once when built into a shared library">,
6177+
"initializeation of %0 may run twice when built into a shared library: "
6178+
"it has hidden visibility and external linkage">,
61796179
InGroup<UniqueObjectDuplication>, DefaultIgnore;
61806180

61816181
def ext_redefinition_of_typedef : ExtWarn<

clang/include/clang/Sema/Sema.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3668,7 +3668,7 @@ class Sema final : public SemaBase {
36683668
/// built into multiple shared libraries with hidden visibility. This can
36693669
/// cause problems if the variable is mutable, its initialization is
36703670
/// effectful, or its address is taken.
3671-
bool GloballyUniqueObjectMightBeAccidentallyDuplicated(const VarDecl *dcl);
3671+
bool GloballyUniqueObjectMightBeAccidentallyDuplicated(const VarDecl *Dcl);
36723672

36733673
/// AddInitializerToDecl - Adds the initializer Init to the
36743674
/// declaration dcl. If DirectInit is true, this is C++ direct

clang/test/SemaCXX/unique_object_duplication.h

Lines changed: 21 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,6 @@
11
/**
2-
* When building shared libraries, hidden objects which are defined in header
3-
* files will be duplicated, with one copy in each shared library. If the object
4-
* was meant to be globally unique (one copy per program), this can cause very
5-
* subtle bugs. This file contains tests for the -Wunique-object-duplication
6-
* warning, which is meant to detect this.
7-
*
8-
* Roughly, an object might be incorrectly duplicated if:
9-
* - Is defined in a header (so it might appear in multiple TUs), and
10-
* - Has external linkage (otherwise it's supposed to be duplicated), and
11-
* - Has hidden visibility (or else the dynamic linker will handle it)
12-
*
13-
* Duplication becomes an issue only if one of the following is true:
14-
* - The object is mutable (the copies won't be in sync), or
15-
* - Its initialization may has side effects (it may now run more than once), or
16-
* - The value of its address is used.
17-
*
18-
* Currently, we only detect the first two, and only warn on effectful
19-
* initialization if we're certain there are side effects. Warning if the
20-
* address is taken is prone to false positives, so we don't warn for now.
21-
*
22-
* The check is also disabled on Windows for now, since it uses
23-
* dllimport/dllexport instead of visibility.
2+
* This file contains tests for the -Wunique_object_duplication warning.
3+
* See the warning's documentation for more information.
244
*/
255

266
#define HIDDEN __attribute__((visibility("hidden")))
@@ -37,9 +17,9 @@ namespace StaticLocalTest {
3717

3818
inline void has_static_locals_external() {
3919
// Mutable
40-
static int disallowedStatic1 = 0; // hidden-warning {{'disallowedStatic1' is mutable, has hidden visibility, and external linkage; it may be duplicated when built into a shared library}}
20+
static int disallowedStatic1 = 0; // hidden-warning {{'disallowedStatic1' may be duplicated when built into a shared library: it is mutable, has hidden visibility, and external linkage}}
4121
// Initialization might run more than once
42-
static const double disallowedStatic2 = disallowedStatic1++; // hidden-warning {{'disallowedStatic2' has hidden visibility, and external linkage; its initialization may run more than once when built into a shared library}}
22+
static const double disallowedStatic2 = disallowedStatic1++; // hidden-warning {{initializeation of 'disallowedStatic2' may run twice when built into a shared library: it has hidden visibility and external linkage}}
4323

4424
// OK, because immutable and compile-time-initialized
4525
static constexpr int allowedStatic1 = 0;
@@ -62,11 +42,7 @@ static void has_static_locals_internal() {
6242
static int allowedStatic1 = 0;
6343
static double allowedStatic2 = init_dynamic(2);
6444
static char allowedStatic3 = []() { return allowedStatic1++; }();
65-
6645
static constexpr int allowedStatic4 = 0;
67-
static const float allowedStatic5 = 1;
68-
static constexpr int allowedStatic6 = init_constexpr(2);
69-
static const int allowedStatic7 = init_constexpr(3);
7046
}
7147

7248
namespace {
@@ -76,26 +52,22 @@ void has_static_locals_anon() {
7652
static int allowedStatic1 = 0;
7753
static double allowedStatic2 = init_dynamic(2);
7854
static char allowedStatic3 = []() { return allowedStatic1++; }();
79-
80-
static constexpr int allowedStatic4 = 0;
81-
static const float allowedStatic5 = 1;
82-
static constexpr int allowedStatic6 = init_constexpr(2);
83-
static const int allowedStatic7 = init_constexpr(3);
55+
static constexpr int allowedStatic4 = init_constexpr(3);
8456
}
8557

8658
} // Anonymous namespace
8759

8860
HIDDEN inline void static_local_always_hidden() {
89-
static int disallowedStatic1 = 3; // hidden-warning {{'disallowedStatic1' is mutable, has hidden visibility, and external linkage; it may be duplicated when built into a shared library}}
90-
// expected-warning@-1 {{'disallowedStatic1' is mutable, has hidden visibility, and external linkage; it may be duplicated when built into a shared library}}
61+
static int disallowedStatic1 = 3; // hidden-warning {{'disallowedStatic1' may be duplicated when built into a shared library: it is mutable, has hidden visibility, and external linkage}}
62+
// expected-warning@-1 {{'disallowedStatic1' may be duplicated when built into a shared library: it is mutable, has hidden visibility, and external linkage}}
9163
{
92-
static int disallowedStatic2 = 3; // hidden-warning {{'disallowedStatic2' is mutable, has hidden visibility, and external linkage; it may be duplicated when built into a shared library}}
93-
// expected-warning@-1 {{'disallowedStatic2' is mutable, has hidden visibility, and external linkage; it may be duplicated when built into a shared library}}
64+
static int disallowedStatic2 = 3; // hidden-warning {{'disallowedStatic2' may be duplicated when built into a shared library: it is mutable, has hidden visibility, and external linkage}}
65+
// expected-warning@-1 {{'disallowedStatic2' may be duplicated when built into a shared library: it is mutable, has hidden visibility, and external linkage}}
9466
}
9567

9668
auto lmb = []() {
97-
static int disallowedStatic3 = 3; // hidden-warning {{'disallowedStatic3' is mutable, has hidden visibility, and external linkage; it may be duplicated when built into a shared library}}
98-
// expected-warning@-1 {{'disallowedStatic3' is mutable, has hidden visibility, and external linkage; it may be duplicated when built into a shared library}}
69+
static int disallowedStatic3 = 3; // hidden-warning {{'disallowedStatic3' may be duplicated when built into a shared library: it is mutable, has hidden visibility, and external linkage}}
70+
// expected-warning@-1 {{'disallowedStatic3' may be duplicated when built into a shared library: it is mutable, has hidden visibility, and external linkage}}
9971
};
10072
}
10173

@@ -124,7 +96,7 @@ inline void has_regular_local() {
12496

12597
inline void has_thread_local() {
12698
// thread_local variables are static by default
127-
thread_local int disallowedThreadLocal = 0; // hidden-warning {{'disallowedThreadLocal' is mutable, has hidden visibility, and external linkage; it may be duplicated when built into a shared library}}
99+
thread_local int disallowedThreadLocal = 0; // hidden-warning {{'disallowedThreadLocal' may be duplicated when built into a shared library: it is mutable, has hidden visibility, and external linkage}}
128100
}
129101

130102
} // namespace StaticLocalTest
@@ -134,12 +106,10 @@ inline void has_thread_local() {
134106
******************************************************************************/
135107
namespace GlobalTest {
136108
// Mutable
137-
inline float disallowedGlobal1 = 3.14; // hidden-warning {{'disallowedGlobal1' is mutable, has hidden visibility, and external linkage; it may be duplicated when built into a shared library}}
138-
// Same as above, but explicitly marked inline
139-
inline float disallowedGlobal4 = 3.14; // hidden-warning {{'disallowedGlobal4' is mutable, has hidden visibility, and external linkage; it may be duplicated when built into a shared library}}
109+
inline float disallowedGlobal1 = 3.14; // hidden-warning {{'disallowedGlobal1' may be duplicated when built into a shared library: it is mutable, has hidden visibility, and external linkage}}
140110

141111
// Initialization might run more than once
142-
inline const double disallowedGlobal5 = disallowedGlobal1++; // hidden-warning {{'disallowedGlobal5' has hidden visibility, and external linkage; its initialization may run more than once when built into a shared library}}
112+
inline const double disallowedGlobal5 = disallowedGlobal1++; // hidden-warning {{initializeation of 'disallowedGlobal5' may run twice when built into a shared library: it has hidden visibility and external linkage}}
143113

144114
// OK because internal linkage, so duplication is intended
145115
static float allowedGlobal1 = 3.14;
@@ -158,21 +128,21 @@ namespace GlobalTest {
158128
float allowedGlobal9 = 3.14;
159129

160130
// Pointers need to be double-const-qualified
161-
inline float& nonConstReference = disallowedGlobal1; // hidden-warning {{'nonConstReference' is mutable, has hidden visibility, and external linkage; it may be duplicated when built into a shared library}}
131+
inline float& nonConstReference = disallowedGlobal1; // hidden-warning {{'nonConstReference' may be duplicated when built into a shared library: it is mutable, has hidden visibility, and external linkage}}
162132
const inline int& constReference = allowedGlobal5;
163133

164-
inline int* nonConstPointerToNonConst = nullptr; // hidden-warning {{'nonConstPointerToNonConst' is mutable, has hidden visibility, and external linkage; it may be duplicated when built into a shared library}}
165-
inline int const* nonConstPointerToConst = nullptr; // hidden-warning {{'nonConstPointerToConst' is mutable, has hidden visibility, and external linkage; it may be duplicated when built into a shared library}}
166-
inline int* const constPointerToNonConst = nullptr; // hidden-warning {{'constPointerToNonConst' is mutable, has hidden visibility, and external linkage; it may be duplicated when built into a shared library}}
134+
inline int* nonConstPointerToNonConst = nullptr; // hidden-warning {{'nonConstPointerToNonConst' may be duplicated when built into a shared library: it is mutable, has hidden visibility, and external linkage}}
135+
inline int const* nonConstPointerToConst = nullptr; // hidden-warning {{'nonConstPointerToConst' may be duplicated when built into a shared library: it is mutable, has hidden visibility, and external linkage}}
136+
inline int* const constPointerToNonConst = nullptr; // hidden-warning {{'constPointerToNonConst' may be duplicated when built into a shared library: it is mutable, has hidden visibility, and external linkage}}
167137
inline int const* const constPointerToConst = nullptr;
168138
// Don't warn on new because it tends to generate false positives
169139
inline int const* const constPointerToConstNew = new int(7);
170140

171141
inline int const * const * const * const nestedConstPointer = nullptr;
172-
inline int const * const ** const * const nestedNonConstPointer = nullptr; // hidden-warning {{'nestedNonConstPointer' is mutable, has hidden visibility, and external linkage; it may be duplicated when built into a shared library}}
142+
inline int const * const ** const * const nestedNonConstPointer = nullptr; // hidden-warning {{'nestedNonConstPointer' may be duplicated when built into a shared library: it is mutable, has hidden visibility, and external linkage}}
173143

174144
struct Test {
175-
static inline float disallowedStaticMember1; // hidden-warning {{'disallowedStaticMember1' is mutable, has hidden visibility, and external linkage; it may be duplicated when built into a shared library}}
145+
static inline float disallowedStaticMember1; // hidden-warning {{'disallowedStaticMember1' may be duplicated when built into a shared library: it is mutable, has hidden visibility, and external linkage}}
176146
// Defined below, in the header file
177147
static float disallowedStaticMember2;
178148
// Defined in the cpp file, so won't get duplicated
@@ -183,5 +153,5 @@ namespace GlobalTest {
183153
// may still cause problems if their address is taken.
184154
};
185155

186-
inline float Test::disallowedStaticMember2 = 2.3; // hidden-warning {{'disallowedStaticMember2' is mutable, has hidden visibility, and external linkage; it may be duplicated when built into a shared library}}
156+
inline float Test::disallowedStaticMember2 = 2.3; // hidden-warning {{'disallowedStaticMember2' may be duplicated when built into a shared library: it is mutable, has hidden visibility, and external linkage}}
187157
} // namespace GlobalTest

0 commit comments

Comments
 (0)