sw vba: move SwWordBasic to its own file
[LibreOffice.git] / bin / find-can-be-private-symbols.py
blobb45dd181caf7ebd24a9004749cdea7d9d26dbd7a
1 #!/usr/bin/python3
3 # Find exported symbols that can be made non-exported.
5 # Noting that (a) parsing these commands is a pain, the output is quite irregular and (b) I'm fumbling in the
6 # dark here, trying to guess what exactly constitutes an "import" vs an "export" of a symbol, linux linking
7 # is rather complex.
9 # Takes about 5min to run on a decent machine.
11 # The standalone function analysis is reasonable reliable, but the class/method analysis is less so
12 # (something to do with destructor thunks not showing up in my results?)
14 # Also, the class/method analysis will not catch problems like
15 # 'dynamic_cast from 'Foo' with hidden type visibility to 'Bar' with default type visibility'
16 # but loplugin:dyncastvisibility will do that for you
19 import subprocess
20 import sys
21 import re
23 exported_symbols1 = set()
24 imported_symbols1 = set()
25 exported_symbols2 = set() # decoded
26 imported_symbols2 = set() # decoded
27 # all names that exist in the source code
28 #all_source_names = set()
31 #subprocess_find_all_source_names = subprocess.Popen("git grep -oh -P '\\b\\w\\w\\w+\\b' -- '*.h*' | sort -u",
32 # stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
33 #with subprocess_find_all_source_names.stdout as txt:
34 # for line in txt:
35 # line = line.strip()
36 # all_source_names.add(line)
37 #subprocess_find_all_source_names.terminate()
39 # find all our shared libs
40 subprocess_find = subprocess.Popen("find ./instdir -name *.so && find ./workdir/LinkTarget/CppunitTest -name *.so",
41 stdout=subprocess.PIPE, shell=True)
42 with subprocess_find.stdout as txt:
43 for line in txt:
44 sharedlib = line.strip()
45 # look for exported symbols
46 subprocess_nm = subprocess.Popen(b"nm -D " + sharedlib, stdout=subprocess.PIPE, shell=True)
47 with subprocess_nm.stdout as txt2:
48 # We are looking for lines something like:
49 # 0000000000036ed0 T flash_component_getFactory
50 line_regex = re.compile(r'^[0-9a-fA-F]+ T ')
51 for line2_bytes in txt2:
52 line2 = line2_bytes.strip().decode("utf-8")
53 if line_regex.match(line2):
54 sym = line2.split(" ")[2].strip()
55 exported_symbols1.add(sym)
56 subprocess_nm.terminate()
57 # look for imported symbols
58 subprocess_objdump = subprocess.Popen(b"objdump -T " + sharedlib, stdout=subprocess.PIPE, shell=True)
59 with subprocess_objdump.stdout as txt2:
60 # ignore some header bumpf
61 txt2.readline()
62 txt2.readline()
63 txt2.readline()
64 txt2.readline()
65 # We are looking for lines something like:
66 # 0000000000000000 DF *UND* 0000000000000000 _ZN16FilterConfigItem10WriteInt32ERKN3rtl8OUStringEi
67 for line2_bytes in txt2:
68 line2 = line2_bytes.strip().decode("utf-8")
69 if not("*UND*"in line2): continue
70 tokens = line2.split(" ")
71 sym = tokens[len(tokens)-1].strip()
72 imported_symbols1.add(sym)
73 subprocess_objdump.terminate()
74 subprocess_find.terminate()
76 # look for imported symbols in executables
77 subprocess_find = subprocess.Popen("find ./instdir -name *.bin", stdout=subprocess.PIPE, shell=True)
78 with subprocess_find.stdout as txt:
79 for line in txt:
80 executable = line.strip()
81 # look for exported symbols
82 subprocess_nm = subprocess.Popen(b"nm -D " + executable + b" | grep -w U", stdout=subprocess.PIPE, shell=True)
83 with subprocess_nm.stdout as txt2:
84 # We are looking for lines something like:
85 # U sal_detail_deinitialize
86 for line2_bytes in txt2:
87 line2 = line2_bytes.strip().decode("utf-8")
88 sym = line2.split(" ")[1]
89 imported_symbols1.add(sym)
90 subprocess_find.terminate()
92 #progress = 0;
93 #for sym in sorted(imported_symbols - exported_symbols):
94 # progress += 1
95 # if (progress % 128 == 0): print( str(int(progress * 100 / len(diff))) + "%")
96 # filtered_sym = subprocess.check_output(["c++filt", sym]).strip().decode("utf-8")
97 # if filtered_sym.startswith("non-virtual thunk to "): filtered_sym = filtered_sym[21:]
98 # elif filtered_sym.startswith("virtual thunk to "): filtered_sym = filtered_sym[17:]
99 # print("Symbol imported but not exported? " + filtered_sym)
101 # Now we have to symbolize before comparing because sometimes (due to thunks) two
102 # different encoded names symbolize to the same method/func name
104 progress = 0;
105 progress_max_len = len(imported_symbols1) + len(exported_symbols1)
106 for sym in imported_symbols1:
107 progress += 1
108 if (progress % 128 == 0): print( str(int(progress * 100 / progress_max_len)) + "%")
109 filtered_sym = subprocess.check_output(["c++filt", sym]).strip().decode("utf-8")
110 if filtered_sym.startswith("non-virtual thunk to "): filtered_sym = filtered_sym[21:]
111 elif filtered_sym.startswith("virtual thunk to "): filtered_sym = filtered_sym[17:]
112 imported_symbols2.add(filtered_sym)
113 progress = 0;
114 for sym in exported_symbols1:
115 progress += 1
116 if (progress % 128 == 0): print( str(int(progress * 100 / progress_max_len)) + "%")
117 filtered_sym = subprocess.check_output(["c++filt", sym]).strip().decode("utf-8")
118 if filtered_sym.startswith("non-virtual thunk to "): filtered_sym = filtered_sym[21:]
119 elif filtered_sym.startswith("virtual thunk to "): filtered_sym = filtered_sym[17:]
120 exported_symbols2.add(filtered_sym)
122 unused_exports = exported_symbols2 - imported_symbols2
123 print("exported = " + str(len(exported_symbols2)))
124 print("imported = " + str(len(imported_symbols2)))
125 print("unused_exports = " + str(len(unused_exports)))
127 #def extractFunctionNameFromSignature(sym):
128 # i = sym.find("(")
129 # if i == -1: return sym
130 # return sym[:i]
132 with open("bin/find-can-be-private-symbols.functions.results", "wt") as f:
133 for sym in sorted(unused_exports):
134 # Filter out most of the noise.
135 # No idea where these are coming from, but not our code.
136 if sym.startswith("CERT_"): continue
137 elif sym.startswith("DER_"): continue
138 elif sym.startswith("FORM_"): continue
139 elif sym.startswith("FPDF"): continue
140 elif sym.startswith("HASH_"): continue
141 elif sym.startswith("Hunspell_"): continue
142 elif sym.startswith("LL_"): continue
143 elif sym.startswith("LP_"): continue
144 elif sym.startswith("LU"): continue
145 elif sym.startswith("MIP"): continue
146 elif sym.startswith("MPS"): continue
147 elif sym.startswith("NSS"): continue
148 elif sym.startswith("NSC_"): continue
149 elif sym.startswith("PK11"): continue
150 elif sym.startswith("PL_"): continue
151 elif sym.startswith("PQ"): continue
152 elif sym.startswith("PBE_"): continue
153 elif sym.startswith("PORT_"): continue
154 elif sym.startswith("PRP_"): continue
155 elif sym.startswith("PR_"): continue
156 elif sym.startswith("PT_"): continue
157 elif sym.startswith("QS_"): continue
158 elif sym.startswith("REPORT_"): continue
159 elif sym.startswith("RSA_"): continue
160 elif sym.startswith("SEC"): continue
161 elif sym.startswith("SGN"): continue
162 elif sym.startswith("SOS"): continue
163 elif sym.startswith("SSL_"): continue
164 elif sym.startswith("VFY_"): continue
165 elif sym.startswith("_PR_"): continue
166 elif sym.startswith("ber_"): continue
167 elif sym.startswith("bfp_"): continue
168 elif sym.startswith("ldap_"): continue
169 elif sym.startswith("ne_"): continue
170 elif sym.startswith("opj_"): continue
171 elif sym.startswith("pg_"): continue
172 elif sym.startswith("pq"): continue
173 elif sym.startswith("presolve_"): continue
174 elif sym.startswith("sqlite3_"): continue
175 elif sym.startswith("libepubgen::"): continue
176 elif sym.startswith("lucene::"): continue
177 elif sym.startswith("Hunspell::"): continue
178 elif sym.startswith("sk_"): continue
179 elif sym.startswith("_Z"): continue
180 # dynamically loaded
181 elif sym.endswith("get_implementation"): continue
182 elif sym.endswith("component_getFactory"): continue
183 elif sym == "CreateDialogFactory": continue
184 elif sym == "CreateUnoWrapper": continue
185 elif sym == "ExportDOC": continue
186 elif sym == "ExportRTF": continue
187 elif sym == "GetSaveWarningOfMSVBAStorage_ww8": continue
188 elif sym == "GetSpecialCharsForEdit": continue
189 elif sym.startswith("Import"): continue
190 elif sym.startswith("Java_com_sun_star_"): continue
191 elif sym.startswith("TestImport"): continue
192 elif sym.startswith("getAllCalendars_"): continue
193 elif sym.startswith("getAllCurrencies_"): continue
194 elif sym.startswith("getAllFormats"): continue
195 elif sym.startswith("getBreakIteratorRules_"): continue
196 elif sym.startswith("getCollationOptions_"): continue
197 elif sym.startswith("getCollatorImplementation_"): continue
198 elif sym.startswith("getContinuousNumberingLevels_"): continue
199 elif sym.startswith("getDateAcceptancePatterns_"): continue
200 elif sym.startswith("getForbiddenCharacters_"): continue
201 elif sym.startswith("getIndexAlgorithm_"): continue
202 elif sym.startswith("getLCInfo_"): continue
203 elif sym.startswith("getLocaleItem_"): continue
204 elif sym.startswith("getOutlineNumberingLevels_"): continue
205 elif sym.startswith("getReservedWords_"): continue
206 elif sym.startswith("getSTC_"): continue
207 elif sym.startswith("getSearchOptions_"): continue
208 elif sym.startswith("getTransliterations_"): continue
209 elif sym.startswith("getUnicodeScripts_"): continue
210 elif sym.startswith("lok_"): continue
211 # UDK API
212 elif sym.startswith("osl_"): continue
213 elif sym.startswith("rtl_"): continue
214 elif sym.startswith("typelib_"): continue
215 elif sym.startswith("typereg_"): continue
216 elif sym.startswith("uno_"): continue
217 # remove things we found that do not exist in our source code, they're not ours
218 #if not(extractFunctionNameFromSignature(sym) in all_source_names): continue
219 f.write(sym + "\n")