Repository navigation
Revert "[clang][Sema] Check -Wunused-private-field at template instantiation instead of definition" - #243997
Merged
Conversation
…tiation …" This reverts commit a3e5321.
aeubanks
enabled auto-merge (squash)
November 18, 2026 20:45
|
@llvm/pr-subscribers-clang Author: Arthur Eubanks (aeubanks) ChangesReverts llvm/llvm-project#237374 Causes false positives, see #237374 (comment) 6 Files Affected:
diff --git a/clang/include/clang/AST/Decl.h b/clang/include/clang/AST/Decl.h
index c73300e7816bb..3048e14a2da1f 100644
--- a/clang/include/clang/AST/Decl.h
+++ b/clang/include/clang/AST/Decl.h
@@ -3540,8 +3540,6 @@ class FieldDecl : public DeclaratorDecl, public Mergeable<FieldDecl> {
/// \p nullptr if either the attribute or the field doesn't exist.
const FieldDecl *findCountedByField() const;
- bool isUnusedPrivateField() const;
-
private:
void setLazyInClassInitializer(LazyDeclStmtPtr NewInit);
diff --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp
index 3fc5eed34dc61..39fa98e023c5d 100644
--- a/clang/lib/AST/Decl.cpp
+++ b/clang/lib/AST/Decl.cpp
@@ -4974,34 +4974,6 @@ const FieldDecl *FieldDecl::findCountedByField() const {
return dyn_cast<FieldDecl>(CountDecl);
}
-static bool initializationHasSideEffects(const FieldDecl &FD) {
- const Type *T = FD.getType()->getBaseElementTypeUnsafe();
- // FIXME: Destruction of ObjC lifetime types has side-effects.
- if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl())
- return !RD->isCompleteDefinition() || !RD->hasTrivialDefaultConstructor() ||
- !RD->hasTrivialDestructor();
- return false;
-}
-
-template <typename AttrType> static bool hasAttribute(QualType T) {
- if (const TagDecl *TD = T->getAsTagDecl())
- return TD->hasAttr<AttrType>();
- if (const TypedefType *TDT = T->getAs<TypedefType>())
- return TDT->getDecl()->hasAttr<AttrType>();
- return false;
-}
-
-bool FieldDecl::isUnusedPrivateField() const {
- if (isImplicit() || !getDeclName() || getAccess() != AS_private ||
- hasAttr<UnusedAttr>() || getParent()->isDependentContext())
- return false;
-
- QualType FieldType = getType();
- return !hasAttribute<UnusedAttr>(FieldType) &&
- (hasAttribute<WarnUnusedAttr>(FieldType) ||
- !initializationHasSideEffects(*this));
-}
-
//===----------------------------------------------------------------------===//
// TagDecl Implementation
//===----------------------------------------------------------------------===//
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index efe8d3fd7f3d4..64a6fe190d3cf 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -3415,6 +3415,16 @@ bool Sema::CheckIfOverriddenFunctionIsMarkedFinal(const CXXMethodDecl *New,
return true;
}
+static bool InitializationHasSideEffects(const FieldDecl &FD) {
+ const Type *T = FD.getType()->getBaseElementTypeUnsafe();
+ // FIXME: Destruction of ObjC lifetime types has side-effects.
+ if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl())
+ return !RD->isCompleteDefinition() ||
+ !RD->hasTrivialDefaultConstructor() ||
+ !RD->hasTrivialDestructor();
+ return false;
+}
+
void Sema::CheckShadowInheritedFields(const SourceLocation &Loc,
DeclarationName FieldName,
const CXXRecordDecl *RD,
@@ -3465,6 +3475,29 @@ void Sema::CheckShadowInheritedFields(const SourceLocation &Loc,
}
}
+template <typename AttrType>
+inline static bool HasAttribute(const QualType &T) {
+ if (const TagDecl *TD = T->getAsTagDecl())
+ return TD->hasAttr<AttrType>();
+ if (const TypedefType *TDT = T->getAs<TypedefType>())
+ return TDT->getDecl()->hasAttr<AttrType>();
+ return false;
+}
+
+static bool IsUnusedPrivateField(const FieldDecl *FD) {
+ if (FD->getAccess() == AS_private && FD->getDeclName()) {
+ QualType FieldType = FD->getType();
+ if (HasAttribute<WarnUnusedAttr>(FieldType))
+ return true;
+
+ return !FD->isImplicit() && !FD->hasAttr<UnusedAttr>() &&
+ !FD->getParent()->isDependentContext() &&
+ !HasAttribute<UnusedAttr>(FieldType) &&
+ !InitializationHasSideEffects(*FD);
+ }
+ return false;
+}
+
NamedDecl *
Sema::ActOnCXXMemberDeclarator(Scope *S, AccessSpecifier AS, Declarator &D,
MultiTemplateParamsArg TemplateParameterLists,
@@ -3758,7 +3791,7 @@ Sema::ActOnCXXMemberDeclarator(Scope *S, AccessSpecifier AS, Declarator &D,
FieldCollector->Add(FD);
if (!Diags.isIgnored(diag::warn_unused_private_field, FD->getLocation()) &&
- FD->isUnusedPrivateField()) {
+ IsUnusedPrivateField(FD)) {
// Remember all explicit private FieldDecls that have a name, no side
// effects and are not part of a dependent type declaration.
UnusedPrivateFields.insert(FD);
diff --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
index ead896eaa303c..b72864aec8d51 100644
--- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
+++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
@@ -1967,11 +1967,6 @@ Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
Field->setAccess(D->getAccess());
Owner->addDecl(Field);
- if (!SemaRef.Diags.isIgnored(diag::warn_unused_private_field,
- Field->getLocation()) &&
- Field->isUnusedPrivateField())
- SemaRef.UnusedPrivateFields.insert(Field);
-
return Field;
}
diff --git a/clang/test/SemaCXX/uninitialized.cpp b/clang/test/SemaCXX/uninitialized.cpp
index 0e90bb67840ad..9ecea1ac4cc3b 100644
--- a/clang/test/SemaCXX/uninitialized.cpp
+++ b/clang/test/SemaCXX/uninitialized.cpp
@@ -593,7 +593,7 @@ namespace rdar10398199 {
};
void stuff();
template <typename T> class FooImpl : public Foo {
- T val; // expected-warning 2{{private field 'val' is not used}}
+ T val;
public:
FooImpl(const T &x) : val(x) {}
~FooImpl() { stuff(); }
diff --git a/clang/test/SemaCXX/warn-unused-private-field.cpp b/clang/test/SemaCXX/warn-unused-private-field.cpp
index 650a2cffc557e..0bcca6b82227f 100644
--- a/clang/test/SemaCXX/warn-unused-private-field.cpp
+++ b/clang/test/SemaCXX/warn-unused-private-field.cpp
@@ -354,37 +354,3 @@ class C {
T t2; // ok
};
}
-
-namespace warn_unused_template {
-struct [[gnu::warn_unused]] WarnUnusedType {};
-
-template <typename T>
-struct Outer {
- class Inner {
- friend struct Outer<T>;
- WarnUnusedType field_;
- };
-
- WarnUnusedType use(Inner &i) {
- return i.field_;
- }
-};
-WarnUnusedType test() {
- Outer<int> o;
- Outer<int>::Inner i;
- return o.use(i);
-}
-
-template <typename T>
-struct Outer2 {
- class Inner {
- WarnUnusedType field_; // expected-warning {{private field 'field_' is not used}}
- };
-};
-void test2() {
- Outer2<int> o;
- Outer2<int>::Inner i;
- (void)o;
- (void)i;
-}
-} // namespace warn_unused_template
|
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
nickdesaulniers
approved these changes
Nov 18, 2026
sync-agent-neo Bot
pushed a commit
to aosp-mirror-neo/toolchain_llvm_android
that referenced
this pull request
Nov 24, 2026
Drop cherry/a3e53216ebd1915ea99637c5bc1bbc33e6ad2153.patch
("[clang][Sema] Check -Wunused-private-field at template instantiation
instead of definition (#237374)"), which was added in ag/43779663.
Upstream reverted #237374 in:
llvm/llvm-project#243997
It caused 170k+ -Werror,-Wunused-private-field regressions across 42
Android build targets (notably art/libartbase/base/bit_struct_detail.h)
on clang-r639965. Keep the three accompanying runtime [[maybe_unused]]
patches (#241054, #241052, #241575) from ag/43779663, which remain
upstream.
Bug: 577228044
Bug: 573304683
Test: presubmit
Flag: EXEMPT BUGFIX
Cherrypick-From: https://googleplex-android-review.googlesource.com/q/commit:5d32917801f24e30576148ba48f7e62f4f650759
Merged-In: I59cddfaa741d8b95c85caeb16d11540186ccdc76
Change-Id: I59cddfaa741d8b95c85caeb16d11540186ccdc76
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Reverts #237374
Causes false positives, see #237374 (comment)