Skip to content

Commit c0a1a4b

Browse files
author
git apple-llvm automerger
committed
Merge commit '6dca33ce2069' from llvm.org/main into next
2 parents 6ff0ac9 + 6dca33c commit c0a1a4b

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

clang/lib/Sema/SemaDecl.cpp

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14415,6 +14415,23 @@ bool Sema::GloballyUniqueObjectMightBeAccidentallyDuplicated(
1441514415
return true;
1441614416
}
1441714417

14418+
// Determine whether the object seems mutable for the purpose of diagnosing
14419+
// possible unique object duplication, i.e. non-const-qualified, and
14420+
// not an always-constant type like a function.
14421+
// Not perfect: doesn't account for mutable members, for example, or
14422+
// elements of container types.
14423+
// For nested pointers, any individual level being non-const is sufficient.
14424+
static bool looksMutable(QualType T, const ASTContext &Ctx) {
14425+
T = T.getNonReferenceType();
14426+
if (T->isFunctionType())
14427+
return false;
14428+
if (!T.isConstant(Ctx))
14429+
return true;
14430+
if (T->isPointerType())
14431+
return looksMutable(T->getPointeeType(), Ctx);
14432+
return false;
14433+
}
14434+
1441814435
void Sema::DiagnoseUniqueObjectDuplication(const VarDecl *VD) {
1441914436
// If this object has external linkage and hidden visibility, it might be
1442014437
// duplicated when built into a shared library, which causes problems if it's
@@ -14429,24 +14446,10 @@ void Sema::DiagnoseUniqueObjectDuplication(const VarDecl *VD) {
1442914446
!VD->isTemplated() &&
1443014447
GloballyUniqueObjectMightBeAccidentallyDuplicated(VD)) {
1443114448

14432-
// Check mutability. For pointers, ensure that both the pointer and the
14433-
// pointee are (recursively) const.
14434-
QualType Type = VD->getType().getNonReferenceType();
14435-
if (!Type.isConstant(VD->getASTContext())) {
14449+
QualType Type = VD->getType();
14450+
if (looksMutable(Type, VD->getASTContext())) {
1443614451
Diag(VD->getLocation(), diag::warn_possible_object_duplication_mutable)
1443714452
<< VD;
14438-
} else {
14439-
while (Type->isPointerType()) {
14440-
Type = Type->getPointeeType();
14441-
if (Type->isFunctionType())
14442-
break;
14443-
if (!Type.isConstant(VD->getASTContext())) {
14444-
Diag(VD->getLocation(),
14445-
diag::warn_possible_object_duplication_mutable)
14446-
<< VD;
14447-
break;
14448-
}
14449-
}
1445014453
}
1445114454

1445214455
// To keep false positives low, only warn if we're certain that the

clang/test/SemaCXX/unique_object_duplication.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ inline void has_thread_local() {
9999
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}}
100100
}
101101

102+
// Functions themselves are always immutable, so referencing them is okay
103+
inline auto& allowedFunctionReference = has_static_locals_external;
104+
102105
} // namespace StaticLocalTest
103106

104107
/******************************************************************************

0 commit comments

Comments
 (0)