compilerplugins/clang/test/unusedmember.cxx |   30 +++++++++++++++++++++++-----
 compilerplugins/clang/unusedmember.cxx      |   15 ++++++++++++++
 2 files changed, 40 insertions(+), 5 deletions(-)

New commits:
commit d1ff9c1d650ada0049c867d76ba38890633a444c
Author:     Stephan Bergmann <sberg...@redhat.com>
AuthorDate: Mon Nov 1 17:41:06 2021 +0100
Commit:     Stephan Bergmann <sberg...@redhat.com>
CommitDate: Mon Nov 1 19:13:23 2021 +0100

    loplugin:unusedmember: Work around more cases not marking an enum as 
referenced
    
    ...similar to the "For some reason..." workaround already present in
    VisitElaboratedTypeLoc.  (Thanks to mst for finding this.)
    
    Change-Id: Ic682e8290efa64093d3c4a831dfb4d23091b6056
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/124559
    Tested-by: Jenkins
    Reviewed-by: Stephan Bergmann <sberg...@redhat.com>

diff --git a/compilerplugins/clang/test/unusedmember.cxx 
b/compilerplugins/clang/test/unusedmember.cxx
index 00b136249aca..a495b786919e 100644
--- a/compilerplugins/clang/test/unusedmember.cxx
+++ b/compilerplugins/clang/test/unusedmember.cxx
@@ -13,15 +13,35 @@ namespace
 {
 struct S
 {
-    enum E
+    enum E1
     {
-        E1,
-        E2
+        E11,
+        E12
+    };
+    E1 e1;
+    enum E2
+    {
+        E21,
+        E22
     };
-    E e;
+    E2 e2; // expected-error {{unused class member [loplugin:unusedmember]}}
+    enum E3
+    {
+        E31,
+        E32
+    } e3;
+    enum E4
+    {
+        E41,
+        E42
+    } e4; // expected-error {{unused class member [loplugin:unusedmember]}}
 };
 }
-void f(S s) { (void)s.e; }
+void f(S s)
+{
+    (void)s.e1;
+    (void)s.e3;
+}
 }
 
 namespace ElaboratedEnum
diff --git a/compilerplugins/clang/unusedmember.cxx 
b/compilerplugins/clang/unusedmember.cxx
index bfd4f591616d..8239b5ae1d8d 100644
--- a/compilerplugins/clang/unusedmember.cxx
+++ b/compilerplugins/clang/unusedmember.cxx
@@ -94,6 +94,21 @@ public:
 
 #endif
 
+    bool VisitDeclaratorDecl(DeclaratorDecl const* decl)
+    {
+        // For declarations like
+        //
+        //   enum E { ... } e;
+        //
+        // it may be that the declaration of E is not marked as referenced 
even though the
+        // declaration of e clearly references it:
+        if (auto const t = decl->getType()->getAs<EnumType>())
+        {
+            deferred_.erase(t->getDecl());
+        }
+        return true;
+    }
+
     bool VisitCXXRecordDecl(CXXRecordDecl const* decl) //TODO: non-CXX 
RecordDecl?
     {
         if (ignoreLocation(decl))

Reply via email to