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
):
19 return (normalizeTypeParams(tokens
[1]), tokens
[2])
21 return (normalizeTypeParams(tokens
[1]), "")
23 with io
.open("workdir/loplugin.constfields.log", "rb", buffering
=1024*1024) as txt
:
25 tokens
= line
.strip().split("\t")
26 if tokens
[0] == "definition:":
28 fieldInfo
= (normalizeTypeParams(tokens
[2]), tokens
[3])
30 # ignore external source code
31 if (srcLoc
.startswith("external/")):
34 if (srcLoc
.startswith("workdir/")):
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
))
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
:
49 srcLoc
= definitionToSourceLocationMap
[d
]
50 fieldType
= definitionToTypeMap
[d
]
51 if fieldType
.startswith("const "):
53 if "std::unique_ptr" in fieldType
:
55 if "std::shared_ptr" in fieldType
:
57 if "Reference<" in fieldType
:
59 if "VclPtr<" in fieldType
:
61 if "osl::Mutex" in fieldType
:
63 if "::sfx2::sidebar::ControllerItem" in fieldType
:
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
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
:
83 f
.write( t
[1] + "\n" )
84 f
.write( " " + t
[0] + "\n" )