postgresql: upgrade to release 13.8
[LibreOffice.git] / bin / find-duplicated-sids.py
blobebffce230b09ff4a706da4846659f3bfc15bb789
1 #!/usr/bin/python
4 # Scan .hrc files for conflicting SID constants
6 # This is not as easy as it sounds because some of the constants depend on other constants whose names do not start with SID_
9 import subprocess
10 import sys
12 sidNameToValue = dict()
13 sidNameToOriginalLine = dict()
16 def extractSidValue(sidValue):
17 if isinstance(sidValue, int):
18 return sidValue
19 if sidValue.isdigit():
20 return int(sidValue)
21 if sidValue[0:2] == "0x":
22 return int(sidValue, 16)
23 if sidValue.find("+") != -1:
24 tokens = sidValue.split("+")
25 tokens1 = tokens[0].strip()
26 tokens2 = tokens[1].strip()
27 return extractSidValue(tokens1) + extractSidValue(tokens2)
28 rv = extractSidValue(sidNameToValue[sidValue])
29 sidNameToValue[sidValue] = rv
30 return rv
33 #a = subprocess.Popen(r"git grep -P '#define\s+(SID_|SC_|DETECTIVE_|DRAWTEXTBAR_|DRAW_BAR_|RID_|OBJBAR_FORMAT_|TAB_POPUP_|DATA_MENU_|EXTRA_MENU_|FORMAT_MENU_|INSERT_MENU_|VIEW_MENU_|EDIT_MENU_|FILE_MENU_|SC_FUNCTION_|RC_)'", stdout=subprocess.PIPE, shell=True)
34 a = subprocess.Popen(r"git grep -Pn '#define\s+(\S+)' -- *.hrc", stdout=subprocess.PIPE, shell=True)
36 with a.stdout as txt:
37 for line in txt:
38 originalLine = line.strip()
39 # strip the '#define' off the front
40 idx1 = line.find(" ")
41 line = line[idx1 : len(line)].strip()
42 # extract the name
43 idx1 = line.find(" ")
44 if (idx1 == -1): continue
45 sidName = line[0 : idx1].strip()
46 line = line[idx1 : len(line)].strip()
47 # strip any trailing comments
48 idx1 = line.find("//")
49 if (idx1 != -1):
50 line = line[0 : idx1].strip()
51 idx1 = line.find("/*")
52 if (idx1 != -1):
53 line = line[0 : idx1].strip()
54 if len(line) == 0: continue
55 # strip brackets
56 if line[0] == "(": line = line[1:]
57 if line[len(line)-1] == ")": line = line[0:len(line)-1]
58 sidTextValue = line.strip()
59 # ignore the #define strings
60 if (sidTextValue.find("\"") != -1): continue
61 # ignore the multiline macros
62 if (sidTextValue.find("\\") != -1): continue
63 # check for redefinitions
64 if sidName[0:4] == "SID_" and sidNameToValue.has_key(sidName):
65 print "Redefinition:\n\t", sidNameToOriginalLine[sidName], "\n\t" , originalLine
66 else:
67 sidNameToValue[sidName] = sidTextValue
68 sidNameToOriginalLine[sidName] = originalLine
70 # decode the constants into their numeric values recursively
71 sidNamesToIgnore = set()
72 for sidName in sidNameToValue:
73 sidTextValue = sidNameToValue[sidName]
74 try:
75 sidValueNum = extractSidValue(sidTextValue)
76 sidNameToValue[sidName] = sidValueNum
77 except KeyError:
78 sidNamesToIgnore.add(sidName)
80 # check for conflicts
81 sidValueToName = dict()
82 for sidName in sidNameToValue:
83 if sidName in sidNamesToIgnore: continue
84 if sidName[0:4] != "SID_": continue
85 sidValue = sidNameToValue[sidName]
86 if sidValueToName.has_key(sidValue):
87 print "conflict:\n\t", sidNameToOriginalLine[sidName], "\n\t", sidNameToOriginalLine[sidValueToName[sidValue]]
88 else:
89 sidValueToName[sidValue] = sidName