update credits
[LibreOffice.git] / compilerplugins / clang / mergeclasses.py
blobccf2b66f7f9ca2696fb1fc26d032f0c299fda713
1 #!/usr/bin/python3
3 instantiatedSet = set()
4 definitionSet = set()
5 parentChildDict = {}
6 definitionToFileDict = {}
8 with open("workdir/loplugin.mergeclasses.log") as txt:
9 for line in txt:
10 tokens = line.strip().split("\t")
12 if len(tokens) == 1:
13 pass
15 elif tokens[0] == "instantiated:":
16 clazzName = tokens[1]
17 if (clazzName.startswith("const ")):
18 clazzName = clazzName[6:]
19 if (clazzName.startswith("class ")):
20 clazzName = clazzName[6:]
21 if (clazzName.startswith("::")):
22 clazzName = clazzName[2:]
23 instantiatedSet.add(clazzName)
25 elif tokens[0] == "definition:":
26 clazzName = tokens[1]
27 # the 1.. is so we skip the leading /
28 fileName = tokens[2][1:]
29 definitionSet.add(clazzName)
30 definitionToFileDict[clazzName] = fileName
32 elif tokens[0] == "has-subclass:":
33 child = tokens[1]
34 parent = tokens[2]
35 if (parent.startswith("class ")):
36 parent = parent[6:]
37 elif (parent.startswith("struct ")):
38 parent = parent[7:]
39 if (child.startswith("class ")):
40 child = child[6:]
41 elif (child.startswith("struct ")):
42 child = child[7:]
43 if (parent not in parentChildDict):
44 parentChildDict[parent] = set()
45 parentChildDict[parent].add(child)
47 def extractModuleName(clazz):
48 filename = definitionToFileDict[clazz]
49 if filename.startswith("include/"):
50 filename = filename[8:]
51 idx = filename.find("/")
52 return filename[:idx]
54 with open("compilerplugins/clang/mergeclasses.results", "wt") as f:
55 # loop over defined, but not instantiated classes
56 for clazz in sorted(definitionSet - instantiatedSet):
57 # ignore classes without any children, and classes with more than one child
58 if (clazz not in parentChildDict) or (len(parentChildDict[clazz]) != 1):
59 continue
60 # exclude some common false positives
61 a = ['Dialog', 'Dlg', 'com::sun']
62 if any(x in clazz for x in a):
63 continue
64 # ignore base class that contain the word "mutex", they are normally there to
65 # help with the WeakComponentImpl template magic
66 if ("mutex" in clazz) or ("Mutex" in clazz):
67 continue
68 subclazz = next(iter(parentChildDict[clazz]))
69 # if the other class has more than child, it is not a candidate for merging
70 #if (otherclazz in parentChildDict) and (len(parentChildDict[otherclazz]) != 1):
71 # continue
72 # Combinations that span modules we often use those to make cross-module dependencies more manageable,
73 # so mark them with maybe.
74 module1 = extractModuleName(clazz)
75 module2 = extractModuleName(subclazz)
76 if module1 != module2:
77 f.write( "maybe merge " + clazz + " with " + subclazz + ", in modules " + module1 + " and " + module2 + "\n" )
78 else:
79 f.write( "merge " + clazz + " with " + subclazz + "\n" )