bump product version to 6.3.0.0.beta1
[LibreOffice.git] / compilerplugins / clang / constfields.py
blob980363f78eb9e5adcba848c52fe3cf9973fe4db6
1 #!/usr/bin/python
3 import sys
4 import re
5 import io
7 definitionSet = set()
8 definitionToSourceLocationMap = dict()
9 definitionToTypeMap = dict()
10 writeFromOutsideConstructorSet = set()
12 # clang does not always use exactly the same numbers in the type-parameter vars it generates
13 # so I need to substitute them to ensure we can match correctly.
14 normalizeTypeParamsRegex = re.compile(r"type-parameter-\d+-\d+")
15 def normalizeTypeParams( line ):
16 return normalizeTypeParamsRegex.sub("type-parameter-?-?", line)
18 def parseFieldInfo( tokens ):
19 if len(tokens) == 3:
20 return (normalizeTypeParams(tokens[1]), tokens[2])
21 else:
22 return (normalizeTypeParams(tokens[1]), "")
24 with io.open("workdir/loplugin.constfields.log", "rb", buffering=1024*1024) as txt:
25 for line in txt:
26 tokens = line.strip().split("\t")
27 if tokens[0] == "definition:":
28 access = tokens[1]
29 fieldInfo = (normalizeTypeParams(tokens[2]), tokens[3])
30 srcLoc = tokens[5]
31 # ignore external source code
32 if (srcLoc.startswith("external/")):
33 continue
34 # ignore build folder
35 if (srcLoc.startswith("workdir/")):
36 continue
37 definitionSet.add(fieldInfo)
38 definitionToTypeMap[fieldInfo] = tokens[4]
39 definitionToSourceLocationMap[fieldInfo] = tokens[5]
40 elif tokens[0] == "write-outside-constructor:":
41 writeFromOutsideConstructorSet.add(parseFieldInfo(tokens))
42 else:
43 print( "unknown line: " + line)
45 # Calculate can-be-const-field set
46 canBeConstFieldSet = set()
47 for d in definitionSet:
48 if d in writeFromOutsideConstructorSet:
49 continue
50 srcLoc = definitionToSourceLocationMap[d];
51 fieldType = definitionToTypeMap[d]
52 if fieldType.startswith("const "):
53 continue
54 if "std::unique_ptr" in fieldType:
55 continue
56 if "std::shared_ptr" in fieldType:
57 continue
58 if "Reference<" in fieldType:
59 continue
60 if "VclPtr<" in fieldType:
61 continue
62 if "osl::Mutex" in fieldType:
63 continue
64 if "::sfx2::sidebar::ControllerItem" in fieldType:
65 continue
66 canBeConstFieldSet.add((d[0] + " " + d[1] + " " + fieldType, srcLoc))
69 # sort the results using a "natural order" so sequences like [item1,item2,item10] sort nicely
70 def natural_sort_key(s, _nsre=re.compile('([0-9]+)')):
71 return [int(text) if text.isdigit() else text.lower()
72 for text in re.split(_nsre, s)]
74 # sort results by name and line number
75 tmp6list = sorted(canBeConstFieldSet, key=lambda v: natural_sort_key(v[1]))
77 # print out the results
78 with open("compilerplugins/clang/constfields.results", "wt") as f:
79 for t in tmp6list:
80 f.write( t[1] + "\n" )
81 f.write( " " + t[0] + "\n" )