update credits
[LibreOffice.git] / compilerplugins / clang / singlevalfields.py
blob43416b7e40165edefe76be27aadbc6fc2b18e6a7
1 #!/usr/bin/python3
3 import re
4 import io
6 definitionToSourceLocationMap = dict() # dict of tuple(parentClass, fieldName) to sourceLocation
7 definitionToTypeMap = dict() # dict of tuple(parentClass, fieldName) to field type
8 fieldAssignDict = dict() # dict of tuple(parentClass, fieldName) to (set of values)
10 # clang does not always use exactly the same numbers in the type-parameter vars it generates
11 # so I need to substitute them to ensure we can match correctly.
12 normalizeTypeParamsRegex = re.compile(r"type-parameter-\d+-\d+")
13 def normalizeTypeParams( line ):
14 return normalizeTypeParamsRegex.sub("type-parameter-?-?", line)
16 # reading as binary (since we known it is pure ascii) is much faster than reading as unicode
17 with io.open("workdir/loplugin.singlevalfields.log", "r", buffering=1024*1024) as txt:
18 for line in txt:
19 tokens = line.strip().split("\t")
20 if tokens[0] == "defn:":
21 parentClass = normalizeTypeParams(tokens[1])
22 fieldName = normalizeTypeParams(tokens[2])
23 fieldType = normalizeTypeParams(tokens[3])
24 srcLoc = tokens[4]
25 # ignore some external stuff that is somehow sneaking through
26 if not(srcLoc.startswith("workdir/") or srcLoc.startswith("-3.0/") or srcLoc.startswith("_64-linux-gnu/")):
27 fieldInfo = (parentClass, fieldName)
28 definitionToSourceLocationMap[fieldInfo] = srcLoc
29 definitionToTypeMap[fieldInfo] = fieldType
30 elif tokens[0] == "asgn:":
31 parentClass = normalizeTypeParams(tokens[1])
32 fieldName = normalizeTypeParams(tokens[2])
33 if len(tokens) > 3:
34 assignValue = tokens[3]
35 else:
36 assignValue = ""
37 fieldInfo = (parentClass, fieldName)
38 if fieldInfo not in fieldAssignDict:
39 fieldAssignDict[fieldInfo] = set()
40 fieldAssignDict[fieldInfo].add(assignValue)
41 else:
42 print( "unknown line: " + line)
44 # look for stuff also has a single value
45 tmp1list = list()
46 # look for things which have two values - zero and one
47 tmp2list = list()
48 for fieldInfo, assignValues in fieldAssignDict.items():
49 v0 = fieldInfo[0] + " " + fieldInfo[1]
50 v1 = (",".join(assignValues))
51 v2 = ""
52 if fieldInfo not in definitionToSourceLocationMap:
53 continue
54 v2 = definitionToSourceLocationMap[fieldInfo]
55 if len(assignValues) > 2:
56 continue
57 if "?" in assignValues:
58 continue
59 # ignore some random noise, no idea why this does not get filtered out by the normal checks in the C++
60 if v2.startswith("ux-gnu/") or v2.startswith(":") or v2.startswith(".h:") or v2.startswith("bxml/"):
61 continue
62 if v2.startswith("ib.h:") or v2.startswith("freetype/") or v2.startswith("k/") or v2.startswith("n.h"):
63 continue
64 if v2.startswith("pango/") or v2.startswith("t.h") or v2.startswith("h:"):
65 continue
66 #if len(assignValues - set(["0", "1", "-1", "nullptr"])) > 0:
67 # continue
68 # ignore things which are locally declared but are actually redeclarations of things from 3rd party code
69 containingClass = fieldInfo[0]
70 if containingClass == "_mwmhints":
71 continue
72 # ignore things which are representations of on-disk structures
73 if containingClass in ["SEPr", "WW8Dop", "BmpInfoHeader", "BmpFileHeader", "Exif::ExifIFD",
74 "sw::WW8FFData", "FFDataHeader", "INetURLHistory_Impl::head_entry", "ImplPPTParaPropSet", "SvxSwAutoFormatFlags",
75 "T602ImportFilter::T602ImportFilter::format602struct", "DataNode"]:
76 continue
77 if v2.startswith("hwpfilter/source"):
78 continue
79 # ignore things which are representations of structures from external code
80 if v2.startswith("desktop/unx/source/splashx.c"):
81 continue
82 # Windows-only
83 if containingClass in ["SfxAppData_Impl", "sfx2::ImplDdeItem", "SvFileStream",
84 "DdeService", "DdeTopic", "DdeItem", "DdeConnection", "connectivity::sdbcx::OUser", "connectivity::sdbcx::OGroup", "connectivity::sdbcx::OCatalog",
85 "cairocanvas::SpriteHelper"]:
86 continue
87 if v2.startswith("include/svl/svdde.hxx") or v2.startswith("embeddedobj/source/inc/oleembobj.hxx"):
88 continue
89 # Some of our supported compilers don't do constexpr, which means o3tl::typed_flags can't be 'static const'
90 if containingClass in ["WaitWindow_Impl"]:
91 continue
92 if len(assignValues) == 2:
93 if "0" in assignValues and "1" in assignValues:
94 fieldType = definitionToTypeMap[fieldInfo]
95 if "_Bool" not in fieldType and "enum " not in fieldType and "boolean" not in fieldType:
96 tmp2list.append((v0,v1,v2,fieldType))
97 elif len(assignValues) == 1:
98 # ignore timers/idles
99 if not("Idle" in v1 or "Timer" in v1):
100 tmp1list.append((v0,v1,v2))
101 else:
102 tmp1list.append((v0,v1,v2))
104 # sort results by filename:lineno
105 def natural_sort_key(s, _nsre=re.compile('([0-9]+)')):
106 return [int(text) if text.isdigit() else text.lower()
107 for text in re.split(_nsre, s)]
108 tmp1list.sort(key=lambda v: natural_sort_key(v[2]))
109 tmp2list.sort(key=lambda v: natural_sort_key(v[2]))
111 # print out the results
112 with open("compilerplugins/clang/singlevalfields.results", "wt") as f:
113 for v in tmp1list:
114 f.write(v[2] + "\n")
115 f.write(" " + v[0] + "\n")
116 f.write(" " + v[1] + "\n")
117 with open("compilerplugins/clang/singlevalfields.could-be-bool.results", "wt") as f:
118 for v in tmp2list:
119 f.write(v[2] + "\n")
120 f.write(" " + v[0] + "\n")
121 f.write(" " + v[3] + "\n")