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