r317076 - Fix -Wunused-private-field to fire regardless of which implicit special members have been implicitly declared.

Richard Smith via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 31 21:52:13 PDT 2017


Author: rsmith
Date: Tue Oct 31 21:52:12 2017
New Revision: 317076

URL: http://llvm.org/viewvc/llvm-project?rev=317076&view=rev
Log:
Fix -Wunused-private-field to fire regardless of which implicit special members have been implicitly declared.

Modified:
    cfe/trunk/lib/Sema/Sema.cpp
    cfe/trunk/lib/Sema/SemaExprMember.cpp
    cfe/trunk/test/SemaCXX/unused.cpp
    cfe/trunk/test/SemaCXX/warn-unused-private-field.cpp

Modified: cfe/trunk/lib/Sema/Sema.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/Sema.cpp?rev=317076&r1=317075&r2=317076&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/Sema.cpp (original)
+++ cfe/trunk/lib/Sema/Sema.cpp Tue Oct 31 21:52:12 2017
@@ -737,7 +737,8 @@ static bool MethodsAndNestedClassesCompl
                                   E = RD->decls_end();
        I != E && Complete; ++I) {
     if (const CXXMethodDecl *M = dyn_cast<CXXMethodDecl>(*I))
-      Complete = M->isDefined() || (M->isPure() && !isa<CXXDestructorDecl>(M));
+      Complete = M->isDefined() || M->isDefaulted() ||
+                 (M->isPure() && !isa<CXXDestructorDecl>(M));
     else if (const FunctionTemplateDecl *F = dyn_cast<FunctionTemplateDecl>(*I))
       // If the template function is marked as late template parsed at this
       // point, it has not been instantiated and therefore we have not

Modified: cfe/trunk/lib/Sema/SemaExprMember.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExprMember.cpp?rev=317076&r1=317075&r2=317076&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExprMember.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExprMember.cpp Tue Oct 31 21:52:12 2017
@@ -1791,7 +1791,9 @@ Sema::BuildFieldReferenceExpr(Expr *Base
       MemberType = Context.getQualifiedType(MemberType, Combined);
   }
 
-  UnusedPrivateFields.remove(Field);
+  auto *CurMethod = dyn_cast<CXXMethodDecl>(CurContext);
+  if (!(CurMethod && CurMethod->isDefaulted()))
+    UnusedPrivateFields.remove(Field);
 
   ExprResult Base = PerformObjectMemberConversion(BaseExpr, SS.getScopeRep(),
                                                   FoundDecl, Field);

Modified: cfe/trunk/test/SemaCXX/unused.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/unused.cpp?rev=317076&r1=317075&r2=317076&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/unused.cpp (original)
+++ cfe/trunk/test/SemaCXX/unused.cpp Tue Oct 31 21:52:12 2017
@@ -6,7 +6,7 @@
 // PR4103 : Make sure we don't get a bogus unused expression warning
 namespace PR4103 {
   class APInt {
-    char foo;
+    char foo; // expected-warning {{private field 'foo' is not used}}
   };
   class APSInt : public APInt {
     char bar; // expected-warning {{private field 'bar' is not used}}

Modified: cfe/trunk/test/SemaCXX/warn-unused-private-field.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/warn-unused-private-field.cpp?rev=317076&r1=317075&r2=317076&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/warn-unused-private-field.cpp (original)
+++ cfe/trunk/test/SemaCXX/warn-unused-private-field.cpp Tue Oct 31 21:52:12 2017
@@ -1,4 +1,5 @@
 // RUN: %clang_cc1 -fsyntax-only -Wunused-private-field -Wused-but-marked-unused -Wno-uninitialized -verify -std=c++11 %s
+// RUN: %clang_cc1 -fsyntax-only -Wunused-private-field -Wused-but-marked-unused -Wno-uninitialized -verify -std=c++17 %s
 
 class NotFullyDefined {
  public:
@@ -246,3 +247,19 @@ namespace pr13543 {
     X x[4]; // no-warning
   };
 }
+
+class implicit_special_member {
+public:
+  static implicit_special_member make() { return implicit_special_member(); }
+
+private:
+  int n; // expected-warning{{private field 'n' is not used}}
+};
+
+class defaulted_special_member {
+public:
+  defaulted_special_member(const defaulted_special_member&) = default;
+
+private:
+  int n; // expected-warning{{private field 'n' is not used}}
+};




More information about the cfe-commits mailing list