summaryrefslogtreecommitdiff
path: root/compilerplugins/clang/constantparam.py
diff options
context:
space:
mode:
authorNoel Grandin <noel@peralex.com>2016-03-08 09:04:18 +0200
committerNoel Grandin <noel@peralex.com>2016-03-08 09:04:47 +0200
commitb4fda7b3f9cf928f45baf6846dd70e97cdb9904a (patch)
tree9158f4d04ed9c144fbe32d4af73939df56b76179 /compilerplugins/clang/constantparam.py
parentc0bd26d73d438b6efc14b56231a6e7f05c8e4b92 (diff)
new loplugin:constantparam
finds parameters that are only ever being called with a single value Change-Id: Ibd0c9b6e6dbc1d1b5d5a005eaa19959560a6e50f
Diffstat (limited to 'compilerplugins/clang/constantparam.py')
-rwxr-xr-xcompilerplugins/clang/constantparam.py61
1 files changed, 61 insertions, 0 deletions
diff --git a/compilerplugins/clang/constantparam.py b/compilerplugins/clang/constantparam.py
new file mode 100755
index 000000000000..a23be74be50f
--- /dev/null
+++ b/compilerplugins/clang/constantparam.py
@@ -0,0 +1,61 @@
+#!/usr/bin/python
+
+import sys
+import re
+import io
+
+definitionSet = set()
+definitionToSourceLocationMap = dict()
+callParamSet = dict()
+
+# things we need to exclude for reasons like :
+# - it's a weird template thingy that confuses the plugin
+exclusionSet = set([
+])
+
+# clang does not always use exactly the same numbers in the type-parameter vars it generates
+# so I need to substitute them to ensure we can match correctly.
+normalizeTypeParamsRegex = re.compile(r"type-parameter-\d+-\d+")
+def normalizeTypeParams( line ):
+ return normalizeTypeParamsRegex.sub("type-parameter-?-?", line)
+
+# The parsing here is designed to avoid grabbing stuff which is mixed in from gbuild.
+# I have not yet found a way of suppressing the gbuild output.
+with io.open(sys.argv[1], "rb", buffering=1024*1024) as txt:
+ for line in txt:
+ idx1 = line.find("\t")
+ idx2 = line.find("\t",idx1+1)
+ idx3 = line.find("\t",idx2+1)
+ idx4 = line.find("\t",idx3+1)
+ returnType = line[:idx1]
+ nameAndParams = line[idx1+1:idx2]
+ sourceLocation = line[idx2+1:idx3]
+ paramName = line[idx3+1:idx4]
+ callValue = line[idx4+1:].strip()
+ callInfo = (normalizeTypeParams(returnType), normalizeTypeParams(nameAndParams), paramName)
+ if callInfo in callParamSet:
+ callParamSet[callInfo].add(callValue)
+ else:
+ callParamSet[callInfo] = set([callValue])
+ definitionToSourceLocationMap[callInfo] = sourceLocation
+
+tmp1set = set()
+for callInfo, callValues in callParamSet.iteritems():
+ if len(callValues) == 1 and "unknown" not in callValues and ("0" in callValues or "1" in callValues or "nullptr" in callValues):
+ v1 = (" ".join(callInfo)) + " " + (",".join(callValues))
+ v2 = definitionToSourceLocationMap[callInfo]
+ tmp1set.add((v1,v2))
+
+# sort results by name and line number
+def natural_sort_key(s, _nsre=re.compile('([0-9]+)')):
+ return [int(text) if text.isdigit() else text.lower()
+ for text in re.split(_nsre, s)]
+tmp1list = sorted(tmp1set, key=lambda v: natural_sort_key(v[1]))
+
+# print out the results
+with open("unused.constantparams", "wt") as f:
+ for t in tmp1list:
+ f.write(t[1] + "\n")
+ f.write(" " + t[0] + "\n")
+
+