summaryrefslogtreecommitdiff
path: root/compilerplugins/clang/finalclasses.py
diff options
context:
space:
mode:
authorNoel Grandin <noel.grandin@collabora.co.uk>2017-10-03 09:17:35 +0200
committerNoel Grandin <noel.grandin@collabora.co.uk>2017-10-03 11:15:47 +0200
commitccb1894c02d77ed89741ca1f82bad87a17fd76fa (patch)
tree1c138d10e381522bd736b1499aabbf4766a87678 /compilerplugins/clang/finalclasses.py
parent419ae5765ac562982f843adbfff32154e2559162 (diff)
new loplugin:finalclasses
look for classes which are effectively final, but contain protected members. These members can be made private. Change-Id: If53d535b068b668d1aff548ebfd0fe6c51a48a0e
Diffstat (limited to 'compilerplugins/clang/finalclasses.py')
-rwxr-xr-xcompilerplugins/clang/finalclasses.py48
1 files changed, 48 insertions, 0 deletions
diff --git a/compilerplugins/clang/finalclasses.py b/compilerplugins/clang/finalclasses.py
new file mode 100755
index 000000000000..64d192905f7c
--- /dev/null
+++ b/compilerplugins/clang/finalclasses.py
@@ -0,0 +1,48 @@
+#!/usr/bin/python
+
+import re
+import sys
+
+definitionSet = set()
+inheritFromSet = set()
+definitionToFileDict = {}
+
+with open("loplugin.finalclasses.log") as txt:
+ for line in txt:
+ tokens = line.strip().split("\t")
+
+ if len(tokens) == 1:
+ pass
+
+ elif tokens[0] == "definition:":
+ clazzName = tokens[1]
+ # the 1.. is so we skip the leading /
+ fileName = tokens[2][1:]
+ definitionSet.add(clazzName)
+ definitionToFileDict[clazzName] = fileName
+
+ elif tokens[0] == "inherited-from:":
+ parent = tokens[1]
+ if (parent.startswith("class ")):
+ parent = parent[6:]
+ elif (parent.startswith("struct ")):
+ parent = parent[7:]
+ inheritFromSet.add(parent);
+
+tmpset = set()
+for clazz in sorted(definitionSet - inheritFromSet):
+ tmpset.add((clazz, definitionToFileDict[clazz]))
+
+# sort the results using a "natural order" so sequences like [item1,item2,item10] sort nicely
+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)]
+def sort_set_by_natural_key(s):
+ return sorted(s, key=lambda v: natural_sort_key(v[1]))
+
+# print output, sorted by name and line number
+with open("compilerplugins/clang/finalclasses.results", "wt") as f:
+ for t in sort_set_by_natural_key(tmpset):
+ f.write(t[1] + "\n")
+ f.write(" " + t[0] + "\n")
+