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
):
20 return (normalizeTypeParams(tokens
[1]), tokens
[2])
22 return (normalizeTypeParams(tokens
[1]), "")
24 with io
.open("workdir/loplugin.constfields.log", "rb", buffering
=1024*1024) as txt
:
26 tokens
= line
.strip().split("\t")
27 if tokens
[0] == "definition:":
29 fieldInfo
= (normalizeTypeParams(tokens
[2]), tokens
[3])
31 # ignore external source code
32 if (srcLoc
.startswith("external/")):
35 if (srcLoc
.startswith("workdir/")):
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
))
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
:
50 srcLoc
= definitionToSourceLocationMap
[d
];
51 fieldType
= definitionToTypeMap
[d
]
52 if fieldType
.startswith("const "):
54 if "std::unique_ptr" in fieldType
:
56 if "std::shared_ptr" in fieldType
:
58 if "Reference<" in fieldType
:
60 if "VclPtr<" in fieldType
:
62 if "osl::Mutex" in fieldType
:
64 if "::sfx2::sidebar::ControllerItem" in fieldType
:
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
:
80 f
.write( t
[1] + "\n" )
81 f
.write( " " + t
[0] + "\n" )