Bump version to 24.04.3.4
[LibreOffice.git] / bin / find-unused-defines.py
blob53db9d1d532a39248f0c6eb3ce2f1b07d429381b
1 #!/usr/bin/python3
3 # Search for unused constants in header files.
5 # Note that sometimes these constants are calculated, so some careful checking of the output is necessary.
7 # Takes about 4 hours to run this on a fast machine with an SSD
10 import subprocess
11 import sys
12 import re
14 exclusionSet = set([
15 # List of RID constants where we compute a value using a base before calling one of the RESSTR methods
16 # Found with: git grep -P 'RID_\w+\s*\+' -- :/ ':!*.hrc' ':!*.src' ':!*.java' ':!*.py' ':!*.xba'
17 "RID_SVXSTR_KEY_",
18 "RID_UPDATE_BUBBLE_TEXT_",
19 "RID_UPDATE_BUBBLE_T_TEXT_",
20 "RID_SVXSTR_TBLAFMT_",
21 "RID_BMP_CONTENT_",
22 "RID_DROPMODE_",
23 "RID_BMP_LEVEL",
24 "RID_SVXSTR_BULLET_DESCRIPTION",
25 "RID_SVXSTR_SINGLENUM_DESCRIPTION",
26 "RID_SVXSTR_OUTLINENUM_DESCRIPTION",
27 "RID_SVXSTR_RULER_",
28 "RID_GALLERYSTR_THEME_",
29 "RID_SVXSTR_BULLET_DESCRIPTION",
30 "RID_SVXSTR_SINGLENUM_DESCRIPTION",
31 "RID_SVXSTR_OUTLINENUM_DESCRIPTION",
32 # doing some weird stuff in svx/source/unodraw/unoprov.cxx involving mapping of UNO api names to translated names and back again
33 "RID_SVXSTR_GRDT",
34 "RID_SVXSTR_HATCH",
35 "RID_SVXSTR_BMP",
36 "RID_SVXSTR_DASH",
37 "RID_SVXSTR_LEND",
38 "RID_SVXSTR_TRASNGR",
39 # other places doing calculations
40 "RID_SVXSTR_DEPTH",
41 "RID_SUBSETSTR_",
42 "ANALYSIS_",
43 "FLD_DOCINFO_CHANGE",
44 "FLD_EU_",
45 "FLD_INPUT_",
46 "FLD_PAGEREF_",
47 "FLD_STAT_",
48 "FMT_AUTHOR_",
49 "FMT_CHAPTER_",
50 "FMT_DBFLD_",
51 "FMT_FF_",
52 "FMT_GETVAR_",
53 "FMT_MARK_",
54 "FMT_REF_",
55 "FMT_SETVAR_",
56 "STR_AUTH_FIELD_ADDRESS_",
57 "STR_AUTH_TYPE_",
58 "STR_AUTOFMTREDL_",
59 "STR_CONTENT_TYPE_",
60 "STR_UPDATE_ALL",
61 "STR_UPDATE_INDEX",
62 "STR_UPDATE_LINK",
63 "BMP_PLACEHOLDER_",
64 "STR_RPT_HELP_",
65 "STR_TEMPLATE_NAME",
66 "UID_BRWEVT_",
67 "HID_EVT_",
68 "HID_PROP_",
69 "STR_VOBJ_MODE_",
70 "STR_COND_",
71 "SCSTR_CONTENT_",
72 "DATE_FUNCDESC_",
73 "DATE_FUNCNAME_",
74 "DATE_DEFFUNCNAME_",
75 "PRICING_DEFFUNCNAME_",
76 "PRICING_FUNCDESC_",
77 "PRICING_FUNCNAME_",
78 "STR_ItemValCAPTION",
79 "STR_ItemValCIRC",
80 "STR_ItemValEDGE",
81 "STR_ItemValFITTOSIZE",
82 "STR_ItemValMEASURE_",
83 "STR_ItemValMEASURETEXT_",
84 "STR_ItemValTEXTANI_",
85 "STR_ItemValTEXTHADJ",
86 "STR_ItemValTEXTVADJ",
87 "RID_SVXITEMS_VERJUST",
88 "RID_SVXITEMS_ORI",
89 "RID_SVXITEMS_JUSTMETHOD",
90 "RID_SVXITEMS_HORJUST",
91 "MM_PART",
95 def in_exclusion_set( a ):
96 for f in exclusionSet:
97 if a.startswith(f):
98 return True;
99 return False;
102 # Hack to turn off unicode decoding errors, which sometimes happens in the output and I really don't
103 # care
104 import codecs
105 def strict_handler(exception):
106 return u"", exception.end
107 codecs.register_error("strict", strict_handler)
109 # find defines, excluding the externals folder
110 a = subprocess.Popen("git grep -hP '^#define\\s+\\w\\w\\w\\w+\\s*' -- \"[!e][!x][!t]*\" | sort -u", stdout=subprocess.PIPE, shell=True, encoding='UTF-8')
112 name_re = re.compile(r"#define\s+(\w+)")
113 with a.stdout as txt:
114 for line in txt:
115 idName = name_re.match(str(line)).group(1)
116 if idName.startswith("INCLUDED_"): continue
117 # the various _START and _END constants are normally unused outside of the .hrc and .src files, and that's fine
118 if idName.endswith("_START"): continue
119 if idName.endswith("_BEGIN"): continue
120 if idName.endswith("_END"): continue
121 if idName == "RID_SVX_FIRSTFREE": continue
122 if idName == "": continue
123 if idName.startswith("__com"): continue # these are the include/header macros for the UNO stuff
124 if in_exclusion_set(idName): continue
125 # search for the constant
126 b = subprocess.Popen(["git", "grep", "-w", idName], stdout=subprocess.PIPE, encoding='UTF-8')
127 found_reason_to_exclude = False
128 with b.stdout as txt2:
129 cnt = 0
130 for line2 in txt2:
131 line2 = line2.strip() # otherwise the comparisons below will not work
132 # ignore if/undef magic, does not indicate an actual use (most of the time)
133 if "ifdef" in line2: continue
134 if "undef" in line2: continue
135 # ignore commented out code
136 if line2.startswith("//"): continue
137 if line2.startswith("/*"): continue
138 # check if we found one in actual code
139 if idName.startswith("SID_"):
140 if not ".hrc:" in line2 and not ".src:" in line2 and not ".sdi:" in line2: found_reason_to_exclude = True
141 else:
142 if not ".hrc:" in line2 and not ".src:" in line2: found_reason_to_exclude = True
143 if idName.startswith("RID_"):
144 # is the constant being used as an identifier by entries in .src files?
145 if ".src:" in line2 and "Identifier = " in line2: found_reason_to_exclude = True
146 # is the constant being used by the property controller extension or reportdesigner inspection,
147 # which use macros to declare constants, hiding them from a search
148 if "extensions/source/propctrlr" in line2: found_reason_to_exclude = True
149 if "reportdesign/source/ui/inspection/inspection.src" in line2: found_reason_to_exclude = True
150 if idName.startswith("HID_"):
151 # is the constant being used as an identifier by entries in .src files
152 if ".src:" in line2 and "HelpId = " in line2: found_reason_to_exclude = True
153 # is it being used as a constant in an ItemList in .src files?
154 if ".src:" in line2 and (";> ;" in line2 or "; >;" in line2): found_reason_to_exclude = True
155 # these are used in calculations in other .hrc files
156 if "sw/inc/rcid.hrc:" in line2: found_reason_to_exclude = True
157 # calculations
158 if "sw/source/uibase/inc/ribbar.hrc:" in line2 and "ST_" in idName: found_reason_to_exclude = True
159 if "sw/source/uibase/inc/ribbar.hrc:" in line2 and "STR_IMGBTN_" in idName: found_reason_to_exclude = True
160 if "sw/source/core/undo/undo.hrc:" in line2: found_reason_to_exclude = True
161 if "sw/inc/poolfmt.hrc:" in line2: found_reason_to_exclude = True
162 # used via a macro that hides them from search
163 if "dbaccess/" in line2 and idName.startswith("PROPERTY_ID_"): found_reason_to_exclude = True
164 if "reportdesign/" in line2 and idName.startswith("HID_RPT_PROP_"): found_reason_to_exclude = True
165 if "reportdesign/" in line2 and idName.startswith("RID_STR_"): found_reason_to_exclude = True
166 if "forms/" in line2 and idName.startswith("PROPERTY_"): found_reason_to_exclude = True
167 if "svx/source/tbxctrls/extrusioncontrols.hrc:" in line2 and idName.startswith("DIRECTION_"): found_reason_to_exclude = True
168 if "svx/source/tbxctrls/extrusioncontrols.hrc:" in line2 and idName.startswith("FROM_"): found_reason_to_exclude = True
169 # if we see more than a few lines then it's probably one of the BASE/START/BEGIN things
170 cnt = cnt + 1
171 if cnt > 2: found_reason_to_exclude = True
172 if not found_reason_to_exclude:
173 print(idName)
174 # otherwise the previous line of output will be incorrectly mixed into the below git output, because of buffering
175 sys.stdout.flush()
176 # search again, so we log the location and filename of stuff we want to remove
177 subprocess.call(["git", "grep", "-wn", idName])