Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / compilerplugins / clang / unusedenumconstants.py
blobc498f38c76d765b4e058a909adbd6681f023e10b
1 #!/usr/bin/python
3 import sys
4 import re
5 import io
7 definitionSet = set()
8 definitionToSourceLocationMap = dict()
9 readSet = set()
10 writeSet = set()
11 sourceLocationSet = set()
13 # clang does not always use exactly the same numbers in the type-parameter vars it generates
14 # so I need to substitute them to ensure we can match correctly.
15 normalizeTypeParamsRegex = re.compile(r"type-parameter-\d+-\d+")
16 def normalizeTypeParams( line ):
17 return normalizeTypeParamsRegex.sub("type-parameter-?-?", line)
19 def parseFieldInfo( tokens ):
20 if len(tokens) == 3:
21 return (normalizeTypeParams(tokens[1]), tokens[2])
22 else:
23 return (normalizeTypeParams(tokens[1]), "")
25 with io.open("workdir/loplugin.unusedenumconstants.log", "rb", buffering=1024*1024) as txt:
26 for line in txt:
27 tokens = line.strip().split("\t")
28 if tokens[0] == "definition:":
29 fieldInfo = (normalizeTypeParams(tokens[1]), tokens[2])
30 srcLoc = tokens[3]
31 # ignore external source code
32 if (srcLoc.startswith("external/")):
33 continue
34 # ignore build folder
35 if (srcLoc.startswith("workdir/")):
36 continue
37 definitionSet.add(fieldInfo)
38 definitionToSourceLocationMap[fieldInfo] = srcLoc
39 elif tokens[0] == "read:":
40 readSet.add(parseFieldInfo(tokens))
41 elif tokens[0] == "write:":
42 writeSet.add(parseFieldInfo(tokens))
43 else:
44 print( "unknown line: " + line)
46 # Invert the definitionToSourceLocationMap
47 # If we see more than one method at the same sourceLocation, it's being autogenerated as part of a template
48 # and we should just ignore
49 sourceLocationToDefinitionMap = {}
50 for k, v in definitionToSourceLocationMap.iteritems():
51 sourceLocationToDefinitionMap[v] = sourceLocationToDefinitionMap.get(v, [])
52 sourceLocationToDefinitionMap[v].append(k)
53 for k, definitions in sourceLocationToDefinitionMap.iteritems():
54 if len(definitions) > 1:
55 for d in definitions:
56 definitionSet.remove(d)
58 def startswith_one_of( srcLoc, fileSet ):
59 for f in fileSet:
60 if srcLoc.startswith(f):
61 return True;
62 return False;
64 def is_ignore(srcLoc):
65 if startswith_one_of(srcLoc,
67 # this is all representations of on-disk or external data structures
68 "basic/source/inc/filefmt.hxx",
69 "basic/source/sbx/sbxscan.cxx",
70 "cppcanvas/source/mtfrenderer/emfpbrush.hxx",
71 "filter/source/graphicfilter/ipcd/ipcd.cxx",
72 "filter/source/t602/t602filter.hxx",
73 "include/filter/msfilter/escherex.hxx",
74 "include/filter/msfilter/svdfppt.hxx",
75 "hwpfilter/",
76 "include/registry/types.hxx",
77 "lotuswordpro/",
78 "include/sot/formats.hxx",
79 "include/svx/msdffdef.hxx",
80 "sc/source/filter/inc/xlconst.hxx",
81 "include/unotools/saveopt.hxx",
82 "sw/inc/fldbas.hxx",
83 "sw/source/filter/inc/wwstyles.hxx",
84 "sw/source/filter/ww8/fields.hxx",
85 "vcl/source/fontsubset/cff.cxx",
86 "include/vcl/settings.hxx", # stored in a setting, can't remove it without potentially triggering UBSAN
87 "basic/source/inc/opcodes.hxx", # can't touch this without breaking unit tests, not sure why
88 "include/unotools/securityoptions.hxx", # comes from the UI
89 "sot/source/sdstor/stgelem.hxx",
90 "sd/source/filter/eppt/epptbase.hxx",
91 "include/registry/refltype.hxx",
92 "include/registry/version.h",
93 "include/svtools/rtftoken.h",
94 "sc/source/filter/inc/xltracer.hxx",
95 "writerfilter/source/dmapper/FieldTypes.hxx",
96 "vcl/source/fontsubset/cff.cxx",
97 "vcl/source/filter/wmf/winmtf.hxx",
98 "vcl/source/filter/sgvmain.hxx",
99 "vcl/source/filter/jpeg/transupp.h",
100 "include/vcl/bitmapex.hxx", # TransparentType
101 "vcl/inc/sft.hxx", # CompositeFlags, WidthClass, WeightClass
102 "vcl/inc/CommonSalLayout.hxx", # VerticalOrientation
103 "include/tools/fontenum.hxx", # part of GDI file format
104 "svx/inc/galobj.hxx", # GalSoundType
105 "include/svx/msdffdef.hxx",
106 "include/svtools/rtftoken.h", # RTF_TOKEN_IDS
107 "starmath/source/mathtype.hxx", # MathType::MTOKENS
108 "sd/source/filter/eppt/epptbase.hxx", # PPTExTextAttr
109 "sc/source/filter/inc/tokstack.hxx", # E_TYPE
110 "filter/source/graphicfilter/icgm/cgmtypes.hxx",
111 "basic/source/inc/filefmt.hxx", # FileOffset
112 "include/basic/sbxdef.hxx", # SbxDataType
113 "connectivity/source/inc/dbase/DTable.hxx", # ODbaseTable::DBFType
114 "codemaker/source/javamaker/classfile.hxx", # AccessFlags
115 "basic/source/inc/filefmt.hxx", # FileOffset
116 "basic/source/inc/opcodes.hxx", # SbiOpcode
117 "sc/source/filter/inc/flttypes.hxx", # BiffTyp
118 "sc/inc/optutil.hxx", # ScOptionsUtil::KeyBindingType
119 "include/sfx2/chalign.hxx", # SfxChildAlignment
120 # unit test code
121 "cppu/source/uno/check.cxx",
122 # general weird nonsense going on
123 "framework/inc/helper/mischelper.hxx"
124 "include/sfx2/shell.hxx",
125 "framework/inc/helper/mischelper.hxx",
126 "include/svtools/htmltokn.h",
127 "include/sfx2/shell.hxx",
128 "sw/inc/iodetect.hxx",
129 "sw/inc/fmtfordr.hxx",
130 "sw/inc/flddat.hxx",
131 "sw/source/uibase/config/modcfg.cxx", # InsertConfigProp
132 "sw/inc/calc.hxx", # SwCalcOper
133 "svtools/source/config/helpopt.cxx", # HelpProperty
134 "include/svtools/htmltokn.h",
135 "include/sfx2/sidebar/Theme.hxx", # ThemeItem
136 "sc/source/ui/docshell/impex.cxx", # SylkVersion
137 "include/i18nutil/paper.hxx", # Paper
138 "cppcanvas/source/mtfrenderer/emfplus.cxx", # EmfPlusCombineMode
139 "cppcanvas/source/mtfrenderer/emfpbrush.hxx", # EmfPlusHatchStyle
140 "include/filter/msfilter/svdfppt.hxx", # PptPlaceholder, PptSlideLayout
141 "include/filter/msfilter/escherex.hxx", # various
142 "basic/source/inc/opcodes.hxx", # SbiOpcode
143 "basic/source/inc/token.hxx", # SbiToken
144 "binaryurp/source/specialfunctionids.hxx", # binaryurp::SpecialFunctionIds
145 "connectivity/source/inc/odbc/OTools.hxx", # ODBC3SQLFunctionId
146 "include/formula/grammar.hxx", # FormulaGrammar::Grammar
147 "basic/source/sbx/sbxres.hxx", # StringId
148 "sc/source/core/tool/chartpos.cxx", # CellState
149 "sc/source/core/data/global2.cxx", # State
150 "sc/inc/global.hxx", # ScAggregateFunc
151 "registry/source/reflcnst.hxx", #CPInfoTag
152 # Windows or OSX only
153 "include/canvas/rendering/icolorbuffer.hxx",
154 "include/vcl/commandevent.hxx",
155 "vcl/inc/unx/gendata.hxx",
156 "vcl/inc/salwtype.hxx",
157 "include/vcl/svapp.hxx",
158 "include/vcl/salbtype.hxx",
159 "include/vcl/commandevent.hxx", # CommandEvent, MediaCommand, ShowDialogId
160 "include/canvas/rendering/irendermodule.hxx", # canvas::IRenderModule::PrimitiveType
161 "sal/osl/unx/file.cxx", # FileHandle_Impl::Kind
162 # must match some other enum
163 "include/editeng/bulletitem.hxx",
164 "include/editeng/svxenum.hxx",
165 "include/formula/opcode.hxx", # OpCode
166 "include/i18nutil/paper.hxx",
167 "include/oox/drawingml/shapepropertymap.hxx",
168 "include/svl/nfkeytab.hx",
169 "include/svl/zforlist.hxx",
170 "include/svtools/svtabbx.hxx",
171 "include/vcl/print.hxx", # NupOrderType, from UI combobox
172 "sw/source/uibase/inc/swcont.hxx", # RegionMode, from UI; ContentTypeId, from UI(registry)
173 "sw/inc/toxe.hxx", # ToxAuthorityType (from UI)
174 "include/svx/sxekitm.hxx", # SdrEdgeKind (from UI)
175 "include/svx/paraprev.hxx", # SvxPrevLineSpace (from UI)
176 "include/svx/ctredlin.hxx", # SvxRedlinDateMode (from UI)
177 "sd/source/ui/inc/animobjs.hxx", # BitmapAdjustment (from UI)
178 "sd/source/ui/dlg/PhotoAlbumDialog.hxx", # SlideImageLayout (from UI)
179 "sd/inc/pres.hxx", # AutoLayout (from UI)
180 "sc/source/ui/inc/scuitphfedit.hxx", # ScHFEntryId (from UI)
181 "include/i18nlangtag/languagetag.hxx", # LanguageTag::ScriptType
182 "extensions/source/scanner/grid.hxx", # ResetType (from UI)
183 "dbaccess/source/inc/dsntypes.hxx", # dbaccess::DATASOURCE_TYPE (from UI)
184 "cui/source/tabpages/tparea.cxx", # FillType (from UI)
185 "include/editeng/svxenum.hxx", # css::style::NumberingType
186 "include/editeng/bulletitem.hxx", # css::style::NumberingType
187 "basic/source/sbx/sbxdec.hxx", # SbxDecimal::CmpResult, must match some Windows API
188 "codemaker/source/javamaker/javatype.cxx", # TypeInfo::Flags, from UNO
189 "chart2/source/view/inc/AbstractShapeFactory.hxx", # chart::SymbolEnum, called via int UNO param
190 # represents constants from an external API
191 "opencl/inc/opencl_device_selection.h",
192 "vcl/inc/sft.hxx",
193 "vcl/inc/unx/XIM.h",
194 "vcl/unx/gtk/xid_fullscreen_on_all_monitors.c",
195 "vcl/unx/gtk/salnativewidgets-gtk.cxx",
196 "sc/inc/callform.hxx", # ParamType
197 "include/i18nlangtag/applelangid.hxx", # AppleLanguageId
198 "connectivity/source/drivers/firebird/Util.hxx", # firebird::BlobSubtype
200 return True
201 if d[1] == "UNKNOWN" or d[1] == "LAST" or d[1].endswith("NONE") or d[1].endswith("None") or d[1].endswith("EQUAL_SIZE"):
202 return True
203 return False
206 untouchedSet = set()
207 for d in definitionSet:
208 if d in readSet or d in writeSet:
209 continue
210 srcLoc = definitionToSourceLocationMap[d];
211 if (is_ignore(srcLoc)):
212 continue
214 untouchedSet.add((d[0] + " " + d[1], srcLoc))
216 writeonlySet = set()
217 for d in writeSet:
218 if d in readSet:
219 continue
220 srcLoc = definitionToSourceLocationMap[d];
221 if (is_ignore(srcLoc)):
222 continue
223 writeonlySet.add((d[0] + " " + d[1], srcLoc))
225 readonlySet = set()
226 for d in readSet:
227 if d in writeSet:
228 continue
229 srcLoc = definitionToSourceLocationMap[d];
230 if (is_ignore(srcLoc)):
231 continue
232 readonlySet.add((d[0] + " " + d[1], srcLoc))
234 # sort the results using a "natural order" so sequences like [item1,item2,item10] sort nicely
235 def natural_sort_key(s, _nsre=re.compile('([0-9]+)')):
236 return [int(text) if text.isdigit() else text.lower()
237 for text in re.split(_nsre, s)]
239 # sort results by name and line number
240 tmp1list = sorted(untouchedSet, key=lambda v: natural_sort_key(v[1]))
241 tmp2list = sorted(writeonlySet, key=lambda v: natural_sort_key(v[1]))
242 tmp3list = sorted(readonlySet, key=lambda v: natural_sort_key(v[1]))
244 # print out the results
245 with open("compilerplugins/clang/unusedenumconstants.untouched.results", "wt") as f:
246 for t in tmp1list:
247 f.write( t[1] + "\n" )
248 f.write( " " + t[0] + "\n" )
249 with open("compilerplugins/clang/unusedenumconstants.writeonly.results", "wt") as f:
250 for t in tmp2list:
251 f.write( t[1] + "\n" )
252 f.write( " " + t[0] + "\n" )
253 with open("compilerplugins/clang/unusedenumconstants.readonly.results", "wt") as f:
254 for t in tmp3list:
255 f.write( t[1] + "\n" )
256 f.write( " " + t[0] + "\n" )