https://github.com/rniwa updated https://github.com/llvm/llvm-project/pull/241102
>From f6ee44be6a6e44f70391353fc30358668c014432 Mon Sep 17 00:00:00 2001 From: Ryosuke Niwa <[email protected]> Date: Tue, 10 Nov 2026 19:11:56 -0800 Subject: [PATCH 1/2] [alpha.webkit.NoDeleteChecker] Don't warn members with an unused default constructor A nodelete constructor was reported for constructing a member whose type has a `= default` default constructor that is non-trivial only because of default member initializers, when nothing in the translation unit used that constructor. Such a constructor has no body in the AST until something uses it, so IsFunctionTrivialImpl hit the null-body check and called it non-trivial. Whether the warning fired therefore depended on unrelated code in the same file or unified source bundle, even though the member was explicitly initialized by the constructor being checked. For a default constructor with no body, decide from what it would run instead: in-class initializer of each field (or, for fields without one, whether the type is trivially constructible), and the bases and virtual bases. Constructors that have a body take the existing path. Co-authored with Claude. rdar://192380708 --- .../Checkers/WebKit/PtrTypesSemantics.cpp | 22 +++++++++++++++++++ .../Checkers/WebKit/nodelete-annotation.cpp | 19 ++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp b/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp index 9cc782985c834..419a55efc9bab 100644 --- a/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp +++ b/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp @@ -1016,6 +1016,28 @@ class TrivialFunctionAnalysisVisitor (CtorDecl->isMoveConstructor() && RD->hasTrivialMoveConstructor())) return true; + // A defaulted default constructor that nothing has used yet has no + // definition, so decide from the initializers it would run. + if (CtorDecl->isDefaultConstructor() && !CtorDecl->isDeleted() && + !CtorDecl->doesThisDeclarationHaveABody() && + RD->hasDefinition()) { + for (const FieldDecl *F : RD->fields()) { + if (const Expr *Init = F->getInClassInitializer()) { + if (!Visit(Init)) + return false; + } else if (!CanTriviallyConstruct(F->getType())) + return false; + } + for (const CXXBaseSpecifier &B : RD->bases()) { + if (!CanTriviallyConstruct(B.getType())) + return false; + } + for (const CXXBaseSpecifier &B : RD->vbases()) { + if (!CanTriviallyConstruct(B.getType())) + return false; + } + return true; + } } if (DtorDecl) return CanTriviallyRunImplicitDtor(DtorDecl); diff --git a/clang/test/Analysis/Checkers/WebKit/nodelete-annotation.cpp b/clang/test/Analysis/Checkers/WebKit/nodelete-annotation.cpp index bc5ecf2803cea..3e4aa577930b6 100644 --- a/clang/test/Analysis/Checkers/WebKit/nodelete-annotation.cpp +++ b/clang/test/Analysis/Checkers/WebKit/nodelete-annotation.cpp @@ -980,6 +980,25 @@ constexpr T min(initializer_list<T> list) { } // namespace std +namespace unused_defaulted_default_ctor { + +struct Ext { double w; }; + +struct Size { + constexpr Size() = default; + [[clang::annotate("webkit.nodelete")]] Size(const Ext&); + double m_w { 0 }; +}; + +struct Rect { + [[clang::annotate("webkit.nodelete")]] Rect(const Ext&); + Size m_size; +}; + +Rect::Rect(const Ext& e) : m_size(e) { } + +} // namespace unused_defaulted_default_ctor + namespace std_initializer_list { // A braced list passed as std::initializer_list materialises a backing array >From b975469515b7c1b34ff186a83d2b24d5a5594ea1 Mon Sep 17 00:00:00 2001 From: Ryosuke Niwa <[email protected]> Date: Fri, 13 Nov 2026 01:08:08 -0800 Subject: [PATCH 2/2] Address review comments and add a test case. --- .../Checkers/WebKit/PtrTypesSemantics.cpp | 15 ++++---- .../Checkers/WebKit/nodelete-annotation.cpp | 38 +++++++++++++++++++ 2 files changed, 45 insertions(+), 8 deletions(-) diff --git a/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp b/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp index 419a55efc9bab..57b1a1c5845b2 100644 --- a/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp +++ b/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp @@ -1016,11 +1016,14 @@ class TrivialFunctionAnalysisVisitor (CtorDecl->isMoveConstructor() && RD->hasTrivialMoveConstructor())) return true; - // A defaulted default constructor that nothing has used yet has no - // definition, so decide from the initializers it would run. + // Sema synthesizes the body and inits() of a defaulted default + // constructor only once it is odr-used, so until then there is + // nothing to read; decide from what it would run instead. A field + // with an in-class initializer is constructed by that initializer + // rather than by its type's default constructor. Virtual bases are + // reached through the direct bases' own constructors. if (CtorDecl->isDefaultConstructor() && !CtorDecl->isDeleted() && - !CtorDecl->doesThisDeclarationHaveABody() && - RD->hasDefinition()) { + !CtorDecl->doesThisDeclarationHaveABody()) { for (const FieldDecl *F : RD->fields()) { if (const Expr *Init = F->getInClassInitializer()) { if (!Visit(Init)) @@ -1032,10 +1035,6 @@ class TrivialFunctionAnalysisVisitor if (!CanTriviallyConstruct(B.getType())) return false; } - for (const CXXBaseSpecifier &B : RD->vbases()) { - if (!CanTriviallyConstruct(B.getType())) - return false; - } return true; } } diff --git a/clang/test/Analysis/Checkers/WebKit/nodelete-annotation.cpp b/clang/test/Analysis/Checkers/WebKit/nodelete-annotation.cpp index 3e4aa577930b6..4a144f22e0588 100644 --- a/clang/test/Analysis/Checkers/WebKit/nodelete-annotation.cpp +++ b/clang/test/Analysis/Checkers/WebKit/nodelete-annotation.cpp @@ -997,6 +997,44 @@ struct Rect { Rect::Rect(const Ext& e) : m_size(e) { } +// The in-class initializer, not the field type's default constructor, is what +// the defaulted default constructor runs for that field. +struct OpaqueDefaultSafeConverting { + OpaqueDefaultSafeConverting(); + [[clang::annotate("webkit.nodelete")]] OpaqueDefaultSafeConverting(int); +}; + +struct InitializedWithSafeConverting { + InitializedWithSafeConverting() = default; + [[clang::annotate("webkit.nodelete")]] InitializedWithSafeConverting(const Ext&); + OpaqueDefaultSafeConverting m_obj = 3; +}; + +struct HoldsInitializedWithSafeConverting { + [[clang::annotate("webkit.nodelete")]] HoldsInitializedWithSafeConverting(const Ext&); + InitializedWithSafeConverting m_member; +}; + +HoldsInitializedWithSafeConverting::HoldsInitializedWithSafeConverting(const Ext& e) : m_member(e) { } + +struct OpaqueConverting { + OpaqueConverting(int); +}; + +struct InitializedWithOpaqueConverting { + InitializedWithOpaqueConverting() = default; + [[clang::annotate("webkit.nodelete")]] InitializedWithOpaqueConverting(const Ext&); + OpaqueConverting m_obj = 3; +}; + +struct HoldsInitializedWithOpaqueConverting { + [[clang::annotate("webkit.nodelete")]] HoldsInitializedWithOpaqueConverting(const Ext&); + InitializedWithOpaqueConverting m_member; +}; + +HoldsInitializedWithOpaqueConverting::HoldsInitializedWithOpaqueConverting(const Ext& e) : m_member(e) { } +// expected-warning@-1{{A constructor 'HoldsInitializedWithOpaqueConverting' has [[clang::annotate_type("webkit.nodelete")]] but it constructs a member variable 'm_member' that could destruct an object}} + } // namespace unused_defaulted_default_ctor namespace std_initializer_list { _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
