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