diff options
author | Noel Grandin <noel@peralex.com> | 2016-02-01 14:52:38 +0200 |
---|---|---|
committer | Noel Grandin <noel@peralex.com> | 2016-02-01 14:53:19 +0200 |
commit | d34d792230251f8d27008522c4e63fb61db32773 (patch) | |
tree | eb0e264251f676f5f9406d35e763bae3fdcd0760 /compilerplugins/clang/unusedmethods.py | |
parent | 27b623d9743667d123532f0e5aac6f6f14fbc0a2 (diff) |
new loplugin to find public methods that can be private
based on the unusedmethods plugin, which I should probably rename at
some point
Change-Id: If197423c59d4350ea1fdc69e99d24b631d9751b9
Diffstat (limited to 'compilerplugins/clang/unusedmethods.py')
-rwxr-xr-x | compilerplugins/clang/unusedmethods.py | 65 |
1 files changed, 53 insertions, 12 deletions
diff --git a/compilerplugins/clang/unusedmethods.py b/compilerplugins/clang/unusedmethods.py index 2f94f16bd9ca..504a97fc4e32 100755 --- a/compilerplugins/clang/unusedmethods.py +++ b/compilerplugins/clang/unusedmethods.py @@ -5,10 +5,13 @@ import re import io definitionSet = set() +publicDefinitionSet = set() definitionToSourceLocationMap = dict() callSet = set() -returnSet = set() +usedReturnSet = set() sourceLocationSet = set() +calledFromOutsideSet = set() + # things we need to exclude for reasons like : # - it's a weird template thingy that confuses the plugin exclusionSet = set([ @@ -120,19 +123,35 @@ with io.open(sys.argv[1], "rb", buffering=1024*1024) as txt: if line.startswith("definition:\t"): idx1 = line.find("\t",12) idx2 = line.find("\t",idx1+1) - funcInfo = (normalizeTypeParams(line[12:idx1]), normalizeTypeParams(line[idx1+1:idx2])) + idx3 = line.find("\t",idx2+1) + access = line[12:idx1] + returnType = line[idx1+1:idx2] + nameAndParams = line[idx2+1:idx3] + sourceLocation = line[idx3+1:].strip() + funcInfo = (normalizeTypeParams(returnType), normalizeTypeParams(nameAndParams)) definitionSet.add(funcInfo) - definitionToSourceLocationMap[funcInfo] = line[idx2+1:].strip() + if access == "public": + publicDefinitionSet.add(funcInfo) + definitionToSourceLocationMap[funcInfo] = sourceLocation elif line.startswith("call:\t"): idx1 = line.find("\t",6) - callSet.add((normalizeTypeParams(line[6:idx1]), normalizeTypeParams(line[idx1+1:].strip()))) + returnType = line[6:idx1] + nameAndParams = line[idx1+1:].strip() + callSet.add((normalizeTypeParams(returnType), normalizeTypeParams(nameAndParams))) elif line.startswith("usedReturn:\t"): idx1 = line.find("\t",12) - returnSet.add((normalizeTypeParams(line[12:idx1]), normalizeTypeParams(line[idx1+1:].strip()))) + returnType = line[12:idx1] + nameAndParams = line[idx1+1:].strip() + usedReturnSet.add((normalizeTypeParams(returnType), normalizeTypeParams(nameAndParams))) + elif line.startswith("calledFromOutsideSet:\t"): + idx1 = line.find("\t",22) + returnType = line[22:idx1] + nameAndParams = line[idx1+1:].strip() + calledFromOutsideSet.add((normalizeTypeParams(returnType), normalizeTypeParams(nameAndParams))) -# Invert the definitionToSourceLocationMap +# Invert the definitionToSourceLocationMap. # If we see more than one method at the same sourceLocation, it's being autogenerated as part of a template -# and we should just ignore +# and we should just ignore it. sourceLocationToDefinitionMap = {} for k, v in definitionToSourceLocationMap.iteritems(): sourceLocationToDefinitionMap[v] = sourceLocationToDefinitionMap.get(v, []) @@ -253,11 +272,9 @@ tmp1list = sorted(tmp1set, key=lambda v: natural_sort_key(v[1])) tmp2set = set() for d in definitionSet: clazz = d[0] + " " + d[1] - if clazz in exclusionSet: - continue - if d in returnSet: + if d in usedReturnSet: continue - if isOtherConstness(d, returnSet): + if isOtherConstness(d, usedReturnSet): continue if d[0] == "void": continue @@ -287,7 +304,31 @@ for d in definitionSet: # sort results by name and line number tmp2list = sorted(tmp2set, key=lambda v: natural_sort_key(v[1])) -for t in tmp2list: +#for t in tmp2list: +# print t[1] +# print " ", t[0] + + +# ------------------------------------------- +# Do the "unnecessary public" part +# ------------------------------------------- + +tmp3set = set() +for d in publicDefinitionSet: + clazz = d[0] + " " + d[1] + if d in calledFromOutsideSet: + continue + if isOtherConstness(d, calledFromOutsideSet): + continue + # ignore external code + if definitionToSourceLocationMap[d].startswith("external/"): + continue + tmp3set.add((clazz, definitionToSourceLocationMap[d])) + +# sort results by name and line number +tmp3list = sorted(tmp3set, key=lambda v: natural_sort_key(v[1])) + +for t in tmp3list: print t[1] print " ", t[0] |