Skip to content

Commit cdeeb39

Browse files
committed
Revert "Warn when unique objects might be duplicated in shared libraries (llvm#117622)"
There are some buildbot breakages, see the PR. Reverting for now. This reverts commit d5684b8.
1 parent d03b5de commit cdeeb39

File tree

7 files changed

+0
-323
lines changed

7 files changed

+0
-323
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,6 @@ Attribute Changes in Clang
113113
Improvements to Clang's diagnostics
114114
-----------------------------------
115115

116-
- The ``-Wunique-object-duplication`` warning has been added to warn about objects
117-
which are supposed to only exist once per program, but may get duplicated when
118-
built into a shared library.
119-
120116
Improvements to Clang's time-trace
121117
----------------------------------
122118

clang/include/clang/Basic/DiagnosticGroups.td

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

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6167,15 +6167,6 @@ def warn_static_local_in_extern_inline : Warning<
61676167
def note_convert_inline_to_static : Note<
61686168
"use 'static' to give inline function %0 internal linkage">;
61696169

6170-
def warn_possible_object_duplication_mutable : Warning<
6171-
"%0 may be duplicated when built into a shared library: "
6172-
"it is mutable, has hidden visibility, and external linkage">,
6173-
InGroup<UniqueObjectDuplication>, DefaultIgnore;
6174-
def warn_possible_object_duplication_init : Warning<
6175-
"initializeation of %0 may run twice when built into a shared library: "
6176-
"it has hidden visibility and external linkage">,
6177-
InGroup<UniqueObjectDuplication>, DefaultIgnore;
6178-
61796170
def ext_redefinition_of_typedef : ExtWarn<
61806171
"redefinition of typedef %0 is a C11 feature">,
61816172
InGroup<DiagGroup<"typedef-redefinition"> >;

clang/include/clang/Sema/Sema.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3664,12 +3664,6 @@ class Sema final : public SemaBase {
36643664
NonTrivialCUnionContext UseContext,
36653665
unsigned NonTrivialKind);
36663666

3667-
/// Certain globally-unique variables might be accidentally duplicated if
3668-
/// built into multiple shared libraries with hidden visibility. This can
3669-
/// cause problems if the variable is mutable, its initialization is
3670-
/// effectful, or its address is taken.
3671-
bool GloballyUniqueObjectMightBeAccidentallyDuplicated(const VarDecl *Dcl);
3672-
36733667
/// AddInitializerToDecl - Adds the initializer Init to the
36743668
/// declaration dcl. If DirectInit is true, this is C++ direct
36753669
/// initialization rather than copy initialization.

clang/lib/Sema/SemaDecl.cpp

Lines changed: 0 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -13380,62 +13380,6 @@ void Sema::checkNonTrivialCUnion(QualType QT, SourceLocation Loc,
1338013380
.visit(QT, nullptr, false);
1338113381
}
1338213382

13383-
bool Sema::GloballyUniqueObjectMightBeAccidentallyDuplicated(
13384-
const VarDecl *Dcl) {
13385-
if (!getLangOpts().CPlusPlus)
13386-
return false;
13387-
13388-
// We only need to warn if the definition is in a header file, so wait to
13389-
// diagnose until we've seen the definition.
13390-
if (!Dcl->isThisDeclarationADefinition())
13391-
return false;
13392-
13393-
// If an object is defined in a source file, its definition can't get
13394-
// duplicated since it will never appear in more than one TU.
13395-
if (Dcl->getASTContext().getSourceManager().isInMainFile(Dcl->getLocation()))
13396-
return false;
13397-
13398-
// If the variable we're looking at is a static local, then we actually care
13399-
// about the properties of the function containing it.
13400-
const ValueDecl *Target = Dcl;
13401-
// VarDecls and FunctionDecls have different functions for checking
13402-
// inline-ness, so we have to do it manually.
13403-
bool TargetIsInline = Dcl->isInline();
13404-
13405-
// Update the Target and TargetIsInline property if necessary
13406-
if (Dcl->isStaticLocal()) {
13407-
const DeclContext *Ctx = Dcl->getDeclContext();
13408-
if (!Ctx)
13409-
return false;
13410-
13411-
const FunctionDecl *FunDcl =
13412-
dyn_cast_if_present<FunctionDecl>(Ctx->getNonClosureAncestor());
13413-
if (!FunDcl)
13414-
return false;
13415-
13416-
Target = FunDcl;
13417-
// IsInlined() checks for the C++ inline property
13418-
TargetIsInline = FunDcl->isInlined();
13419-
}
13420-
13421-
// Non-inline variables can only legally appear in one TU
13422-
// FIXME: This also applies to templated variables, but that can rarely lead
13423-
// to false positives so templates are disabled for now.
13424-
if (!TargetIsInline)
13425-
return false;
13426-
13427-
// If the object isn't hidden, the dynamic linker will prevent duplication.
13428-
clang::LinkageInfo Lnk = Target->getLinkageAndVisibility();
13429-
if (Lnk.getVisibility() != HiddenVisibility)
13430-
return false;
13431-
13432-
// If the obj doesn't have external linkage, it's supposed to be duplicated.
13433-
if (!isExternalFormalLinkage(Lnk.getLinkage()))
13434-
return false;
13435-
13436-
return true;
13437-
}
13438-
1343913383
void Sema::AddInitializerToDecl(Decl *RealDecl, Expr *Init, bool DirectInit) {
1344013384
// If there is no declaration, there was an error parsing it. Just ignore
1344113385
// the initializer.
@@ -14842,51 +14786,6 @@ void Sema::FinalizeDeclaration(Decl *ThisDecl) {
1484214786
if (DC->getRedeclContext()->isFileContext() && VD->isExternallyVisible())
1484314787
AddPushedVisibilityAttribute(VD);
1484414788

14845-
// If this object has external linkage and hidden visibility, it might be
14846-
// duplicated when built into a shared library, which causes problems if it's
14847-
// mutable (since the copies won't be in sync) or its initialization has side
14848-
// effects (since it will run once per copy instead of once globally)
14849-
// FIXME: Windows uses dllexport/dllimport instead of visibility, and we don't
14850-
// handle that yet. Disable the warning on Windows for now.
14851-
// FIXME: Checking templates can cause false positives if the template in
14852-
// question is never instantiated (e.g. only specialized templates are used).
14853-
if (!Context.getTargetInfo().shouldDLLImportComdatSymbols() &&
14854-
!VD->isTemplated() &&
14855-
GloballyUniqueObjectMightBeAccidentallyDuplicated(VD)) {
14856-
// Check mutability. For pointers, ensure that both the pointer and the
14857-
// pointee are (recursively) const.
14858-
QualType Type = VD->getType().getNonReferenceType();
14859-
if (!Type.isConstant(VD->getASTContext())) {
14860-
Diag(VD->getLocation(), diag::warn_possible_object_duplication_mutable)
14861-
<< VD;
14862-
} else {
14863-
while (Type->isPointerType()) {
14864-
Type = Type->getPointeeType();
14865-
if (Type->isFunctionType())
14866-
break;
14867-
if (!Type.isConstant(VD->getASTContext())) {
14868-
Diag(VD->getLocation(),
14869-
diag::warn_possible_object_duplication_mutable)
14870-
<< VD;
14871-
break;
14872-
}
14873-
}
14874-
}
14875-
14876-
// To keep false positives low, only warn if we're certain that the
14877-
// initializer has side effects. Don't warn on operator new, since a mutable
14878-
// pointer will trigger the previous warning, and an immutable pointer
14879-
// getting duplicated just results in a little extra memory usage.
14880-
const Expr *Init = VD->getAnyInitializer();
14881-
if (Init &&
14882-
Init->HasSideEffects(VD->getASTContext(),
14883-
/*IncludePossibleEffects=*/false) &&
14884-
!isa<CXXNewExpr>(Init->IgnoreParenImpCasts())) {
14885-
Diag(Init->getExprLoc(), diag::warn_possible_object_duplication_init)
14886-
<< VD;
14887-
}
14888-
}
14889-
1489014789
// FIXME: Warn on unused var template partial specializations.
1489114790
if (VD->isFileVarDecl() && !isa<VarTemplatePartialSpecializationDecl>(VD))
1489214791
MarkUnusedFileScopedDecl(VD);

clang/test/SemaCXX/unique_object_duplication.cpp

Lines changed: 0 additions & 16 deletions
This file was deleted.

clang/test/SemaCXX/unique_object_duplication.h

Lines changed: 0 additions & 157 deletions
This file was deleted.

0 commit comments

Comments
 (0)