[Libreoffice-commits] core.git: 2 commits - bin/find-unused-defines.py bin/find-unused-typedefs.py bin/find-unused-typedefs.sh compilerplugins/clang

Noel Grandin noel.grandin at collabora.co.uk
Fri Dec 15 11:58:57 UTC 2017


 bin/find-unused-defines.py                 |    1 +
 bin/find-unused-typedefs.py                |   23 ++++++++++++++++++++---
 bin/find-unused-typedefs.sh                |   25 -------------------------
 compilerplugins/clang/test/unusedindex.cxx |   16 ++++++++++++++++
 compilerplugins/clang/unusedindex.cxx      |   23 ++++++++++++-----------
 5 files changed, 49 insertions(+), 39 deletions(-)

New commits:
commit 06c281c20654baaabfdb7080a77a350ca789f9b2
Author: Noel Grandin <noel.grandin at collabora.co.uk>
Date:   Fri Dec 15 12:27:10 2017 +0200

    loplugin:unusedindex fix false+ in nested loops
    
    Change-Id: I31acbf104e49a4d1f077817a68d0b116fd2e0a30

diff --git a/compilerplugins/clang/test/unusedindex.cxx b/compilerplugins/clang/test/unusedindex.cxx
index 69ad7000b38c..7b98f8645b91 100644
--- a/compilerplugins/clang/test/unusedindex.cxx
+++ b/compilerplugins/clang/test/unusedindex.cxx
@@ -18,6 +18,22 @@ void func1()
         n += 1;
     for (int i = 0; i < 10; ++i)
         n += i;
+
+    for (int i = 0; i < 10; ++i) // expected-error {{loop variable not used [loplugin:unusedindex]}}
+    {
+        for (int j = 0; j < 10; ++j)
+        {
+            n += j;
+        }
+    }
+    for (int i = 0; i < 10; ++i)
+    {
+        for (int j = 0; j < 10; ++j)
+        {
+            n += j;
+            n += i;
+        }
+    }
 }
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
diff --git a/compilerplugins/clang/unusedindex.cxx b/compilerplugins/clang/unusedindex.cxx
index 96c343820a6b..1235e32b3a28 100644
--- a/compilerplugins/clang/unusedindex.cxx
+++ b/compilerplugins/clang/unusedindex.cxx
@@ -38,7 +38,7 @@ public:
     bool VisitDeclRefExpr(DeclRefExpr const* stmt);
 
 private:
-    VarDecl const* mLoopVarDecl = nullptr;
+    std::vector<VarDecl const*> mLoopVarDecls;
     std::unordered_set<VarDecl const*> mFoundSet;
 };
 
@@ -46,27 +46,28 @@ bool UnusedIndex::TraverseForStmt(ForStmt* stmt)
 {
     if (ignoreLocation(stmt))
         return true;
-    auto savedCopy = mLoopVarDecl;
 
-    mLoopVarDecl = nullptr;
+    VarDecl const* loopVarDecl = nullptr;
     if (stmt->getInit())
     {
         auto declStmt = dyn_cast<DeclStmt>(stmt->getInit());
         if (declStmt && declStmt->isSingleDecl())
         {
-            auto varDecl = dyn_cast<VarDecl>(declStmt->getSingleDecl());
-            if (varDecl)
-                mLoopVarDecl = varDecl;
+            loopVarDecl = dyn_cast<VarDecl>(declStmt->getSingleDecl());
         }
     }
+    if (loopVarDecl)
+        mLoopVarDecls.push_back(loopVarDecl);
 
     // deliberately ignore the other parts of the for stmt, except for the body
     auto ret = RecursiveASTVisitor::TraverseStmt(stmt->getBody());
 
-    if (mLoopVarDecl && mFoundSet.erase(mLoopVarDecl) == 0)
-        report(DiagnosticsEngine::Warning, "loop variable not used", mLoopVarDecl->getLocStart())
-            << mLoopVarDecl->getSourceRange();
-    mLoopVarDecl = savedCopy;
+    if (loopVarDecl && mFoundSet.erase(loopVarDecl) == 0)
+        report(DiagnosticsEngine::Warning, "loop variable not used", loopVarDecl->getLocStart())
+            << loopVarDecl->getSourceRange();
+
+    if (loopVarDecl)
+        mLoopVarDecls.pop_back();
     return ret;
 }
 
@@ -75,7 +76,7 @@ bool UnusedIndex::VisitDeclRefExpr(DeclRefExpr const* stmt)
     auto varDecl = dyn_cast<VarDecl>(stmt->getDecl());
     if (!varDecl)
         return true;
-    if (mLoopVarDecl && mLoopVarDecl == varDecl)
+    if (std::find(mLoopVarDecls.begin(), mLoopVarDecls.end(), varDecl) != mLoopVarDecls.end())
         mFoundSet.insert(varDecl);
     return true;
 }
commit ca6d205e88f052d25325d360a6fd0d744cb43000
Author: Noel Grandin <noel.grandin at collabora.co.uk>
Date:   Fri Dec 15 12:26:07 2017 +0200

    improve the find-unused-typedefs script
    
    Change-Id: If4ab3bf8759c194e6005091d6df6a3a11cd633b7

diff --git a/bin/find-unused-defines.py b/bin/find-unused-defines.py
index 7f5f27cb6bc7..2c08cc6cd342 100755
--- a/bin/find-unused-defines.py
+++ b/bin/find-unused-defines.py
@@ -97,6 +97,7 @@ def in_exclusion_set( a ):
             return True;
     return False;
 
+# find defines, excluding the externals folder
 a = subprocess.Popen("git grep -hP '^#define\s+\w+\s+' -- \"[!e][!x][!t]*\" | sort -u", stdout=subprocess.PIPE, shell=True)
 
 with a.stdout as txt:
diff --git a/bin/find-unused-typedefs.py b/bin/find-unused-typedefs.py
index e292f097526a..1f3395835b89 100755
--- a/bin/find-unused-typedefs.py
+++ b/bin/find-unused-typedefs.py
@@ -2,8 +2,11 @@
 
 import subprocess
 
-a = subprocess.Popen("git grep -P '^typedef\s+.+\s+\w+;' -- \"[!e][!x][!t]*\"", stdout=subprocess.PIPE, shell=True)
+# find typedefs, excluding the externals folder
+a = subprocess.Popen("git grep -P 'typedef\s+.+\s+\w+;' -- \"[!e][!x][!t]*\"", stdout=subprocess.PIPE, shell=True)
 
+# parse out the typedef names
+typedefSet = set()
 with a.stdout as txt:
     for line in txt:
         idx2 = line.rfind(";")
@@ -12,6 +15,20 @@ with a.stdout as txt:
         if typedefName.startswith("*"):
            typedefName = typedefName[1:]
         # ignore anything less than 5 characters, it's probably a parsing error
-        if len(typedefName) > 4:
-            print typedefName
+        if len(typedefName) < 5: continue
+        typedefSet.add(typedefName)
+
+for typedefName in sorted(typedefSet):
+    print("checking: " + typedefName)
+    a = subprocess.Popen(["git", "grep", "-wn", typedefName], stdout=subprocess.PIPE)
+    foundLine2 = ""
+    cnt = 0
+    with a.stdout as txt2:
+        for line2 in txt2:
+            cnt = cnt + 1
+            foundLine2 += line2
+    if cnt == 1:
+        print("remove: " + foundLine2)
+    elif cnt == 2:
+        print("inline: " + foundLine2)
 
diff --git a/bin/find-unused-typedefs.sh b/bin/find-unused-typedefs.sh
deleted file mode 100755
index bc4378533efa..000000000000
--- a/bin/find-unused-typedefs.sh
+++ /dev/null
@@ -1,25 +0,0 @@
-#
-# This is a pretty brute-force approach. It takes several hours to run on a top-spec MacbookAir.
-# It also produces some false positives, so it requires careful examination and testing of the results.
-#
-# Algorithm Summary:
-#   First we find all #defines,
-#   then we search for each of them in turn,
-#   and if we find only one instance of a #define, we print it out.
-#
-# Algorithm Detail:
-# (1) find #defines, excluding the externals folder
-# (2) extract just the constant name from the search results
-# (3) trim blank lines
-# (4) sort the results, mostly so I have an idea how far along the process is
-# (5) for each result:
-#   (6) grep for the constant
-#   (7) use awk to check if only one match for a given constant was found
-#   (8) if so, generate a sed command to remove the #define
-#
-bin/find-unused-typedefs.py \
-  | sort -u \
-  | xargs -Ixxx -n 1 -P 8 sh -c \
-    '( git grep -w xxx | awk -f bin/find-unused-defines.awk -v p1=xxx ) && echo xxx 1>&2'
-
-


More information about the Libreoffice-commits mailing list