You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Disable unique-object-duplication warning in templates (#129120)
I've been trying to resolve instances of the unique-object-duplication
warning in chromium code. Unfortunately, I've found that practically
speaking, it's near-impossible to actually fix the problem when
templates are involved.
My understanding is that the warning is correct -- the variables it's
flagging are indeed duplicated and potentially causing bugs as a result.
The problem is that hiddenness is contagious: if a templated class or
variable depends on something hidden, then it itself must also be
hidden, even if the user explicitly marked it visible. In order to make
it actually visible, the user must manually figure out everything that
it depends on, mark them as visible, and do so recursively until all of
its ancestors are visible.
This process is extremely difficult and unergonomic, negating much of
the benefits of templates since now each new use requires additional
work. Furthermore, the process doesn't work if the user can't edit some
of the files, e.g. if they're in a third-party library.
Since a warning that can't practically be fixed isn't useful, this PR
disables the warning for _all_ templated code by inverting the check.
The warning remains active (and, in my experience, easily fixable) in
non-templated code.
Copy file name to clipboardExpand all lines: clang/test/SemaCXX/unique_object_duplication.h
+6-70Lines changed: 6 additions & 70 deletions
Original file line number
Diff line number
Diff line change
@@ -165,81 +165,17 @@ namespace GlobalTest {
165
165
166
166
namespaceTemplateTest {
167
167
168
-
template <typename T>
169
-
int disallowedTemplate1 = 0; // hidden-warning {{'disallowedTemplate1<int>' may be duplicated when built into a shared library: it is mutable, has hidden visibility, and external linkage}}
170
-
171
-
template int disallowedTemplate1<int>; // hidden-note {{in instantiation of}}
172
-
173
-
174
-
// Should work for implicit instantiation as well
175
-
template <typename T>
176
-
int disallowedTemplate2 = 0; // hidden-warning {{'disallowedTemplate2<int>' may be duplicated when built into a shared library: it is mutable, has hidden visibility, and external linkage}}
// We never warn inside templates because it's frequently infeasible to actually
169
+
// fix the warning.
182
170
183
-
// Ensure we only get warnings for templates that are actually instantiated
184
171
template <typename T>
185
-
int maybeAllowedTemplate = 0; // Not instantiated, so no warning here
186
-
187
-
template <typename T>
188
-
int maybeAllowedTemplate<T*> = 1; // hidden-warning {{'maybeAllowedTemplate<int *>' may be duplicated when built into a shared library: it is mutable, has hidden visibility, and external linkage}}
189
-
190
-
template <>
191
-
int maybeAllowedTemplate<bool> = 2; // hidden-warning {{'maybeAllowedTemplate<bool>' may be duplicated when built into a shared library: it is mutable, has hidden visibility, and external linkage}}
192
-
193
-
template int maybeAllowedTemplate<int*>; // hidden-note {{in instantiation of}}
172
+
int allowedTemplate1 = 0;
194
173
195
-
196
-
197
-
// Should work the same for static class members
198
-
template <typename T>
199
-
structS {
200
-
staticint staticMember;
201
-
};
174
+
template int allowedTemplate1<int>;
202
175
203
176
template <typename T>
204
-
intS<T>::staticMember = 0;// Never instantiated
177
+
inlineintallowedTemplate2 = 0;
205
178
206
-
// T* specialization
207
-
template <typename T>
208
-
structS<T*> {
209
-
staticint staticMember;
210
-
};
211
-
212
-
template <typename T>
213
-
int S<T*>::staticMember = 1; // hidden-warning {{'staticMember' may be duplicated when built into a shared library: it is mutable, has hidden visibility, and external linkage}}
int S<T&>::staticMember = 2; // hidden-warning {{'staticMember' may be duplicated when built into a shared library: it is mutable, has hidden visibility, and external linkage}}
staticint staticLocal; // hidden-warning {{'staticLocal' may be duplicated when built into a shared library: it is mutable, has hidden visibility, and external linkage}}
235
-
return &staticLocal;
236
-
}
237
-
238
-
template <>
239
-
int* wrapper<int*>() {
240
-
staticint staticLocal; // hidden-warning {{'staticLocal' may be duplicated when built into a shared library: it is mutable, has hidden visibility, and external linkage}}
241
-
return &staticLocal;
242
-
}
179
+
template int allowedTemplate2<int>;
243
180
244
-
auto dummy = wrapper<bool>(); // hidden-note {{in instantiation of}}
0 commit comments