ยปCore Development>Code coverage>Tools/scripts/fixcid.py

Python code coverage for Tools/scripts/fixcid.py

#countcontent
1n/a#! /usr/bin/env python3
2n/a
3n/a# Perform massive identifier substitution on C source files.
4n/a# This actually tokenizes the files (to some extent) so it can
5n/a# avoid making substitutions inside strings or comments.
6n/a# Inside strings, substitutions are never made; inside comments,
7n/a# it is a user option (off by default).
8n/a#
9n/a# The substitutions are read from one or more files whose lines,
10n/a# when not empty, after stripping comments starting with #,
11n/a# must contain exactly two words separated by whitespace: the
12n/a# old identifier and its replacement.
13n/a#
14n/a# The option -r reverses the sense of the substitutions (this may be
15n/a# useful to undo a particular substitution).
16n/a#
17n/a# If the old identifier is prefixed with a '*' (with no intervening
18n/a# whitespace), then it will not be substituted inside comments.
19n/a#
20n/a# Command line arguments are files or directories to be processed.
21n/a# Directories are searched recursively for files whose name looks
22n/a# like a C file (ends in .h or .c). The special filename '-' means
23n/a# operate in filter mode: read stdin, write stdout.
24n/a#
25n/a# Symbolic links are always ignored (except as explicit directory
26n/a# arguments).
27n/a#
28n/a# The original files are kept as back-up with a "~" suffix.
29n/a#
30n/a# Changes made are reported to stdout in a diff-like format.
31n/a#
32n/a# NB: by changing only the function fixline() you can turn this
33n/a# into a program for different changes to C source files; by
34n/a# changing the function wanted() you can make a different selection of
35n/a# files.
36n/a
37n/aimport sys
38n/aimport re
39n/aimport os
40n/afrom stat import *
41n/aimport getopt
42n/a
43n/aerr = sys.stderr.write
44n/adbg = err
45n/arep = sys.stdout.write
46n/a
47n/adef usage():
48n/a progname = sys.argv[0]
49n/a err('Usage: ' + progname +
50n/a ' [-c] [-r] [-s file] ... file-or-directory ...\n')
51n/a err('\n')
52n/a err('-c : substitute inside comments\n')
53n/a err('-r : reverse direction for following -s options\n')
54n/a err('-s substfile : add a file of substitutions\n')
55n/a err('\n')
56n/a err('Each non-empty non-comment line in a substitution file must\n')
57n/a err('contain exactly two words: an identifier and its replacement.\n')
58n/a err('Comments start with a # character and end at end of line.\n')
59n/a err('If an identifier is preceded with a *, it is not substituted\n')
60n/a err('inside a comment even when -c is specified.\n')
61n/a
62n/adef main():
63n/a try:
64n/a opts, args = getopt.getopt(sys.argv[1:], 'crs:')
65n/a except getopt.error as msg:
66n/a err('Options error: ' + str(msg) + '\n')
67n/a usage()
68n/a sys.exit(2)
69n/a bad = 0
70n/a if not args: # No arguments
71n/a usage()
72n/a sys.exit(2)
73n/a for opt, arg in opts:
74n/a if opt == '-c':
75n/a setdocomments()
76n/a if opt == '-r':
77n/a setreverse()
78n/a if opt == '-s':
79n/a addsubst(arg)
80n/a for arg in args:
81n/a if os.path.isdir(arg):
82n/a if recursedown(arg): bad = 1
83n/a elif os.path.islink(arg):
84n/a err(arg + ': will not process symbolic links\n')
85n/a bad = 1
86n/a else:
87n/a if fix(arg): bad = 1
88n/a sys.exit(bad)
89n/a
90n/a# Change this regular expression to select a different set of files
91n/aWanted = r'^[a-zA-Z0-9_]+\.[ch]$'
92n/adef wanted(name):
93n/a return re.match(Wanted, name)
94n/a
95n/adef recursedown(dirname):
96n/a dbg('recursedown(%r)\n' % (dirname,))
97n/a bad = 0
98n/a try:
99n/a names = os.listdir(dirname)
100n/a except OSError as msg:
101n/a err(dirname + ': cannot list directory: ' + str(msg) + '\n')
102n/a return 1
103n/a names.sort()
104n/a subdirs = []
105n/a for name in names:
106n/a if name in (os.curdir, os.pardir): continue
107n/a fullname = os.path.join(dirname, name)
108n/a if os.path.islink(fullname): pass
109n/a elif os.path.isdir(fullname):
110n/a subdirs.append(fullname)
111n/a elif wanted(name):
112n/a if fix(fullname): bad = 1
113n/a for fullname in subdirs:
114n/a if recursedown(fullname): bad = 1
115n/a return bad
116n/a
117n/adef fix(filename):
118n/a## dbg('fix(%r)\n' % (filename,))
119n/a if filename == '-':
120n/a # Filter mode
121n/a f = sys.stdin
122n/a g = sys.stdout
123n/a else:
124n/a # File replacement mode
125n/a try:
126n/a f = open(filename, 'r')
127n/a except IOError as msg:
128n/a err(filename + ': cannot open: ' + str(msg) + '\n')
129n/a return 1
130n/a head, tail = os.path.split(filename)
131n/a tempname = os.path.join(head, '@' + tail)
132n/a g = None
133n/a # If we find a match, we rewind the file and start over but
134n/a # now copy everything to a temp file.
135n/a lineno = 0
136n/a initfixline()
137n/a while 1:
138n/a line = f.readline()
139n/a if not line: break
140n/a lineno = lineno + 1
141n/a while line[-2:] == '\\\n':
142n/a nextline = f.readline()
143n/a if not nextline: break
144n/a line = line + nextline
145n/a lineno = lineno + 1
146n/a newline = fixline(line)
147n/a if newline != line:
148n/a if g is None:
149n/a try:
150n/a g = open(tempname, 'w')
151n/a except IOError as msg:
152n/a f.close()
153n/a err(tempname+': cannot create: '+
154n/a str(msg)+'\n')
155n/a return 1
156n/a f.seek(0)
157n/a lineno = 0
158n/a initfixline()
159n/a rep(filename + ':\n')
160n/a continue # restart from the beginning
161n/a rep(repr(lineno) + '\n')
162n/a rep('< ' + line)
163n/a rep('> ' + newline)
164n/a if g is not None:
165n/a g.write(newline)
166n/a
167n/a # End of file
168n/a if filename == '-': return 0 # Done in filter mode
169n/a f.close()
170n/a if not g: return 0 # No changes
171n/a g.close()
172n/a
173n/a # Finishing touch -- move files
174n/a
175n/a # First copy the file's mode to the temp file
176n/a try:
177n/a statbuf = os.stat(filename)
178n/a os.chmod(tempname, statbuf[ST_MODE] & 0o7777)
179n/a except OSError as msg:
180n/a err(tempname + ': warning: chmod failed (' + str(msg) + ')\n')
181n/a # Then make a backup of the original file as filename~
182n/a try:
183n/a os.rename(filename, filename + '~')
184n/a except OSError as msg:
185n/a err(filename + ': warning: backup failed (' + str(msg) + ')\n')
186n/a # Now move the temp file to the original file
187n/a try:
188n/a os.rename(tempname, filename)
189n/a except OSError as msg:
190n/a err(filename + ': rename failed (' + str(msg) + ')\n')
191n/a return 1
192n/a # Return success
193n/a return 0
194n/a
195n/a# Tokenizing ANSI C (partly)
196n/a
197n/aIdentifier = '(struct )?[a-zA-Z_][a-zA-Z0-9_]+'
198n/aString = r'"([^\n\\"]|\\.)*"'
199n/aChar = r"'([^\n\\']|\\.)*'"
200n/aCommentStart = r'/\*'
201n/aCommentEnd = r'\*/'
202n/a
203n/aHexnumber = '0[xX][0-9a-fA-F]*[uUlL]*'
204n/aOctnumber = '0[0-7]*[uUlL]*'
205n/aDecnumber = '[1-9][0-9]*[uUlL]*'
206n/aIntnumber = Hexnumber + '|' + Octnumber + '|' + Decnumber
207n/aExponent = '[eE][-+]?[0-9]+'
208n/aPointfloat = r'([0-9]+\.[0-9]*|\.[0-9]+)(' + Exponent + r')?'
209n/aExpfloat = '[0-9]+' + Exponent
210n/aFloatnumber = Pointfloat + '|' + Expfloat
211n/aNumber = Floatnumber + '|' + Intnumber
212n/a
213n/a# Anything else is an operator -- don't list this explicitly because of '/*'
214n/a
215n/aOutsideComment = (Identifier, Number, String, Char, CommentStart)
216n/aOutsideCommentPattern = '(' + '|'.join(OutsideComment) + ')'
217n/aOutsideCommentProgram = re.compile(OutsideCommentPattern)
218n/a
219n/aInsideComment = (Identifier, Number, CommentEnd)
220n/aInsideCommentPattern = '(' + '|'.join(InsideComment) + ')'
221n/aInsideCommentProgram = re.compile(InsideCommentPattern)
222n/a
223n/adef initfixline():
224n/a global Program
225n/a Program = OutsideCommentProgram
226n/a
227n/adef fixline(line):
228n/a global Program
229n/a## print('-->', repr(line))
230n/a i = 0
231n/a while i < len(line):
232n/a match = Program.search(line, i)
233n/a if match is None: break
234n/a i = match.start()
235n/a found = match.group(0)
236n/a## if Program is InsideCommentProgram: print(end='... ')
237n/a## else: print(end=' ')
238n/a## print(found)
239n/a if len(found) == 2:
240n/a if found == '/*':
241n/a Program = InsideCommentProgram
242n/a elif found == '*/':
243n/a Program = OutsideCommentProgram
244n/a n = len(found)
245n/a if found in Dict:
246n/a subst = Dict[found]
247n/a if Program is InsideCommentProgram:
248n/a if not Docomments:
249n/a print('Found in comment:', found)
250n/a i = i + n
251n/a continue
252n/a if found in NotInComment:
253n/a## print(end='Ignored in comment: ')
254n/a## print(found, '-->', subst)
255n/a## print('Line:', line, end='')
256n/a subst = found
257n/a## else:
258n/a## print(end='Substituting in comment: ')
259n/a## print(found, '-->', subst)
260n/a## print('Line:', line, end='')
261n/a line = line[:i] + subst + line[i+n:]
262n/a n = len(subst)
263n/a i = i + n
264n/a return line
265n/a
266n/aDocomments = 0
267n/adef setdocomments():
268n/a global Docomments
269n/a Docomments = 1
270n/a
271n/aReverse = 0
272n/adef setreverse():
273n/a global Reverse
274n/a Reverse = (not Reverse)
275n/a
276n/aDict = {}
277n/aNotInComment = {}
278n/adef addsubst(substfile):
279n/a try:
280n/a fp = open(substfile, 'r')
281n/a except IOError as msg:
282n/a err(substfile + ': cannot read substfile: ' + str(msg) + '\n')
283n/a sys.exit(1)
284n/a lineno = 0
285n/a while 1:
286n/a line = fp.readline()
287n/a if not line: break
288n/a lineno = lineno + 1
289n/a try:
290n/a i = line.index('#')
291n/a except ValueError:
292n/a i = -1 # Happens to delete trailing \n
293n/a words = line[:i].split()
294n/a if not words: continue
295n/a if len(words) == 3 and words[0] == 'struct':
296n/a words[:2] = [words[0] + ' ' + words[1]]
297n/a elif len(words) != 2:
298n/a err(substfile + '%s:%r: warning: bad line: %r' % (substfile, lineno, line))
299n/a continue
300n/a if Reverse:
301n/a [value, key] = words
302n/a else:
303n/a [key, value] = words
304n/a if value[0] == '*':
305n/a value = value[1:]
306n/a if key[0] == '*':
307n/a key = key[1:]
308n/a NotInComment[key] = value
309n/a if key in Dict:
310n/a err('%s:%r: warning: overriding: %r %r\n' % (substfile, lineno, key, value))
311n/a err('%s:%r: warning: previous: %r\n' % (substfile, lineno, Dict[key]))
312n/a Dict[key] = value
313n/a fp.close()
314n/a
315n/aif __name__ == '__main__':
316n/a main()