Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / compilerplugins / clang / singlevalfields.py
blobc9df813cf3da50c866e2e073d2e032f515b7e43c
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 not fieldInfo 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 #if len(assignValues - set(["0", "1", "-1", "nullptr"])) > 0:
60 # continue
61 # ignore things which are locally declared but are actually redeclarations of things from 3rd party code
62 containingClass = fieldInfo[0]
63 if containingClass == "_mwmhints":
64 continue
65 # ignore things which are representations of on-disk structures
66 if containingClass in ["SEPr", "WW8Dop", "BmpInfoHeader", "BmpFileHeader", "Exif::ExifIFD",
67 "sw::WW8FFData", "FFDataHeader", "INetURLHistory_Impl::head_entry", "ImplPPTParaPropSet", "SvxSwAutoFormatFlags",
68 "T602ImportFilter::T602ImportFilter::format602struct", "DataNode"]:
69 continue
70 if v2.startswith("hwpfilter/source"):
71 continue
72 # ignore things which are representations of structures from external code
73 if v2.startswith("desktop/unx/source/splashx.c"):
74 continue
75 # Windows-only
76 if containingClass in ["SfxAppData_Impl", "sfx2::ImplDdeItem", "SvFileStream",
77 "DdeService", "DdeTopic", "DdeItem", "DdeConnection", "connectivity::sdbcx::OUser", "connectivity::sdbcx::OGroup", "connectivity::sdbcx::OCatalog",
78 "cairocanvas::SpriteHelper"]:
79 continue
80 if v2.startswith("include/svl/svdde.hxx") or v2.startswith("embeddedobj/source/inc/oleembobj.hxx"):
81 continue
82 # Some of our supported compilers don't do constexpr, which means o3tl::typed_flags can't be 'static const'
83 if containingClass in ["WaitWindow_Impl"]:
84 continue
85 if len(assignValues) == 2:
86 if "0" in assignValues and "1" in assignValues:
87 fieldType = definitionToTypeMap[fieldInfo]
88 if not "_Bool" in fieldType and not "enum " in fieldType and not "boolean" in fieldType:
89 tmp2list.append((v0,v1,v2,fieldType))
90 elif len(assignValues) == 1:
91 # ignore timers/idles
92 if not("Idle" in v1 or "Timer" in v1):
93 tmp1list.append((v0,v1,v2))
94 else:
95 tmp1list.append((v0,v1,v2))
97 # sort results by filename:lineno
98 def natural_sort_key(s, _nsre=re.compile('([0-9]+)')):
99 return [int(text) if text.isdigit() else text.lower()
100 for text in re.split(_nsre, s)]
101 tmp1list.sort(key=lambda v: natural_sort_key(v[2]))
102 tmp2list.sort(key=lambda v: natural_sort_key(v[2]))
104 # print out the results
105 with open("compilerplugins/clang/singlevalfields.results", "wt") as f:
106 for v in tmp1list:
107 f.write(v[2] + "\n")
108 f.write(" " + v[0] + "\n")
109 f.write(" " + v[1] + "\n")
110 with open("compilerplugins/clang/singlevalfields.could-be-bool.results", "wt") as f:
111 for v in tmp2list:
112 f.write(v[2] + "\n")
113 f.write(" " + v[0] + "\n")
114 f.write(" " + v[3] + "\n")