##// END OF EJS Templates
check-code: two more rules...
Matt Mackall -
r10451:63a9bfad default
parent child Browse files
Show More
@@ -1,164 +1,167
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2 #
2 #
3 # check-code - a style and portability checker for Mercurial
3 # check-code - a style and portability checker for Mercurial
4 #
4 #
5 # Copyright 2010 Matt Mackall <mpm@selenic.com>
5 # Copyright 2010 Matt Mackall <mpm@selenic.com>
6 #
6 #
7 # This software may be used and distributed according to the terms of the
7 # This software may be used and distributed according to the terms of the
8 # GNU General Public License version 2 or any later version.
8 # GNU General Public License version 2 or any later version.
9
9
10 import sys, re, glob
10 import sys, re, glob
11
11
12 def repquote(m):
12 def repquote(m):
13 t = re.sub(r"\S", "x", m.group(2))
13 t = re.sub(r"\w", "x", m.group(2))
14 t = re.sub(r"[^\sx]", "o", t)
14 return m.group(1) + t + m.group(1)
15 return m.group(1) + t + m.group(1)
15
16
16 def repcomment(m):
17 def repcomment(m):
17 return m.group(1) + "#" * len(m.group(2))
18 return m.group(1) + "#" * len(m.group(2))
18
19
19 def repccomment(m):
20 def repccomment(m):
20 t = re.sub(r"((?<=\n) )|\S", "x", m.group(2))
21 t = re.sub(r"((?<=\n) )|\S", "x", m.group(2))
21 return m.group(1) + t + "*/"
22 return m.group(1) + t + "*/"
22
23
23 def repcallspaces(m):
24 def repcallspaces(m):
24 t = re.sub(r"\n\s+", "\n", m.group(2))
25 t = re.sub(r"\n\s+", "\n", m.group(2))
25 return m.group(1) + t
26 return m.group(1) + t
26
27
27 def repinclude(m):
28 def repinclude(m):
28 return m.group(1) + "<foo>"
29 return m.group(1) + "<foo>"
29
30
30 def rephere(m):
31 def rephere(m):
31 t = re.sub(r"\S", "x", m.group(2))
32 t = re.sub(r"\S", "x", m.group(2))
32 return m.group(1) + t
33 return m.group(1) + t
33
34
34
35
35 testpats = [
36 testpats = [
36 (r'(pushd|popd)', "don't use 'pushd' or 'popd', use 'cd'"),
37 (r'(pushd|popd)', "don't use 'pushd' or 'popd', use 'cd'"),
37 (r'\W\$?\(\([^\)]*\)\)', "don't use (()) or $(()), use 'expr'"),
38 (r'\W\$?\(\([^\)]*\)\)', "don't use (()) or $(()), use 'expr'"),
38 (r'^function', "don't use 'function', use old style"),
39 (r'^function', "don't use 'function', use old style"),
39 (r'grep.*-q', "don't use 'grep -q', redirect to /dev/null"),
40 (r'grep.*-q', "don't use 'grep -q', redirect to /dev/null"),
40 (r'echo.*\\n', "don't use 'echo \\n', use printf"),
41 (r'echo.*\\n', "don't use 'echo \\n', use printf"),
41 (r'^diff.*-\w*N', "don't use 'diff -N'"),
42 (r'^diff.*-\w*N', "don't use 'diff -N'"),
42 (r'(^| )wc[^|]*$', "filter wc output"),
43 (r'(^| )wc[^|]*$', "filter wc output"),
43 (r'head -c', "don't use 'head -c', use 'dd'"),
44 (r'head -c', "don't use 'head -c', use 'dd'"),
44 (r'ls.*-\w*R', "don't use 'ls -R', use 'find'"),
45 (r'ls.*-\w*R', "don't use 'ls -R', use 'find'"),
45 (r'printf.*\\\d\d\d', "don't use 'printf \NNN', use Python"),
46 (r'printf.*\\\d\d\d', "don't use 'printf \NNN', use Python"),
46 (r'printf.*\\x', "don't use printf \\x, use Python"),
47 (r'printf.*\\x', "don't use printf \\x, use Python"),
47 (r'\$\(.*\)', "don't use $(expr), use `expr`"),
48 (r'\$\(.*\)', "don't use $(expr), use `expr`"),
48 (r'rm -rf \*', "don't use naked rm -rf, target a directory"),
49 (r'rm -rf \*', "don't use naked rm -rf, target a directory"),
49 (r'(^|\|\s*)grep (-\w\s+)*[^|]*[(|]\w',
50 (r'(^|\|\s*)grep (-\w\s+)*[^|]*[(|]\w',
50 "use egrep for extended grep syntax"),
51 "use egrep for extended grep syntax"),
51 (r'/bin/', "don't use explicit paths for tools"),
52 (r'/bin/', "don't use explicit paths for tools"),
52 (r'\$PWD', "don't use $PWD, use `pwd`"),
53 (r'\$PWD', "don't use $PWD, use `pwd`"),
53 (r'[^\n]\Z', "no trailing newline"),
54 (r'[^\n]\Z', "no trailing newline"),
54 ]
55 ]
55
56
56 testfilters = [
57 testfilters = [
57 (r"( *)(#([^\n]*\S)?)", repcomment),
58 (r"( *)(#([^\n]*\S)?)", repcomment),
58 (r"<<(\S+)((.|\n)*?\n\1)", rephere),
59 (r"<<(\S+)((.|\n)*?\n\1)", rephere),
59 ]
60 ]
60
61
61 pypats = [
62 pypats = [
62 (r'^\s*\t', "don't use tabs"),
63 (r'^\s*\t', "don't use tabs"),
63 (r'\S;\s*\n', "semicolon"),
64 (r'\S;\s*\n', "semicolon"),
64 (r'\w,\w', "missing whitespace after ,"),
65 (r'\w,\w', "missing whitespace after ,"),
65 (r'\w[+/*\-<>]\w', "missing whitespace in expression"),
66 (r'\w[+/*\-<>]\w', "missing whitespace in expression"),
66 (r'^\s+\w+=\w+[^,)]$', "missing whitespace in assignment"),
67 (r'^\s+\w+=\w+[^,)]$', "missing whitespace in assignment"),
67 (r'.{85}', "line too long"),
68 (r'.{85}', "line too long"),
68 (r'[^\n]\Z', "no trailing newline"),
69 (r'[^\n]\Z', "no trailing newline"),
69 # (r'^\s+[^_ ][^_. ]+_[^_]+\s*=', "don't use underbars in identifiers"),
70 # (r'^\s+[^_ ][^_. ]+_[^_]+\s*=', "don't use underbars in identifiers"),
70 # (r'\w*[a-z][A-Z]\w*\s*=', "don't use camelcase in identifiers"),
71 # (r'\w*[a-z][A-Z]\w*\s*=', "don't use camelcase in identifiers"),
71 (r'^\s*(if|while|def|class|except|try)\s[^[]*:\s*[^\]#\s]+',
72 (r'^\s*(if|while|def|class|except|try)\s[^[]*:\s*[^\]#\s]+',
72 "linebreak after :"),
73 "linebreak after :"),
73 (r'class\s[^(]:', "old-style class, use class foo(object)"),
74 (r'class\s[^(]:', "old-style class, use class foo(object)"),
74 (r'^\s+del\(', "del isn't a function"),
75 (r'^\s+del\(', "del isn't a function"),
75 (r'^\s+except\(', "except isn't a function"),
76 (r'^\s+except\(', "except isn't a function"),
76 (r',]', "unneeded trailing ',' in list"),
77 (r',]', "unneeded trailing ',' in list"),
77 # (r'class\s[A-Z][^\(]*\((?!Exception)',
78 # (r'class\s[A-Z][^\(]*\((?!Exception)',
78 # "don't capitalize non-exception classes"),
79 # "don't capitalize non-exception classes"),
79 # (r'in range\(', "use xrange"),
80 # (r'in range\(', "use xrange"),
80 # (r'^\s*print\s+', "avoid using print in core and extensions"),
81 # (r'^\s*print\s+', "avoid using print in core and extensions"),
81 (r'[\x80-\xff]', "non-ASCII character literal"),
82 (r'[\x80-\xff]', "non-ASCII character literal"),
82 (r'("\')\.format\(', "str.format() not available in Python 2.4"),
83 (r'("\')\.format\(', "str.format() not available in Python 2.4"),
83 (r'^\s*with\s+', "with not available in Python 2.4"),
84 (r'^\s*with\s+', "with not available in Python 2.4"),
84 (r'if\s.*\selse', "if ... else form not available in Python 2.4"),
85 (r'if\s.*\selse', "if ... else form not available in Python 2.4"),
85 (r'([\(\[]\s\S)|(\S\s[\)\]])', "gratuitous whitespace in () or []"),
86 (r'([\(\[]\s\S)|(\S\s[\)\]])', "gratuitous whitespace in () or []"),
86 # (r'\s\s=', "gratuitous whitespace before ="),
87 # (r'\s\s=', "gratuitous whitespace before ="),
87 (r'[^>< ](\+=|-=|!=|<>|<=|>=|<<=|>>=)\S', "missing whitespace around operator"),
88 (r'[^>< ](\+=|-=|!=|<>|<=|>=|<<=|>>=)\S', "missing whitespace around operator"),
88 (r'[^>< ](\+=|-=|!=|<>|<=|>=|<<=|>>=)\s', "missing whitespace around operator"),
89 (r'[^>< ](\+=|-=|!=|<>|<=|>=|<<=|>>=)\s', "missing whitespace around operator"),
89 (r'\s(\+=|-=|!=|<>|<=|>=|<<=|>>=)\S', "missing whitespace around operator"),
90 (r'\s(\+=|-=|!=|<>|<=|>=|<<=|>>=)\S', "missing whitespace around operator"),
90 (r'[^+=*!<>&| -](\s=|=\s)[^= ]', "wrong whitespace around ="),
91 (r'[^+=*!<>&| -](\s=|=\s)[^= ]', "wrong whitespace around ="),
92 (r'raise Exception', "don't raise generic exceptions"),
93 (r'ui\.(status|progress|write|note)\([\'\"]x', "unwrapped ui message"),
91 ]
94 ]
92
95
93 pyfilters = [
96 pyfilters = [
94 (r"""(''')(([^']|\\'|'{1,2}(?!'))*)'''""", repquote),
97 (r"""(''')(([^']|\\'|'{1,2}(?!'))*)'''""", repquote),
95 (r'''(""")(([^"]|\\"|"{1,2}(?!"))*)"""''', repquote),
98 (r'''(""")(([^"]|\\"|"{1,2}(?!"))*)"""''', repquote),
96 (r'''(?<!")(")(([^"\n]|\\")+)"(?!")''', repquote),
99 (r'''(?<!")(")(([^"\n]|\\")+)"(?!")''', repquote),
97 (r"""(?<!')(')(([^'\n]|\\')+)'(?!')""", repquote),
100 (r"""(?<!')(')(([^'\n]|\\')+)'(?!')""", repquote),
98 (r"( *)(#([^\n]*\S)?)", repcomment),
101 (r"( *)(#([^\n]*\S)?)", repcomment),
99 ]
102 ]
100
103
101 cpats = [
104 cpats = [
102 (r'//', "don't use //-style comments"),
105 (r'//', "don't use //-style comments"),
103 (r'^ ', "don't use spaces to indent"),
106 (r'^ ', "don't use spaces to indent"),
104 (r'\S\t', "don't use tabs except for indent"),
107 (r'\S\t', "don't use tabs except for indent"),
105 (r'(\S\s+|^\s+)\n', "trailing whitespace"),
108 (r'(\S\s+|^\s+)\n', "trailing whitespace"),
106 (r'.{85}', "line too long"),
109 (r'.{85}', "line too long"),
107 (r'(while|if|do|for)\(', "use space after while/if/do/for"),
110 (r'(while|if|do|for)\(', "use space after while/if/do/for"),
108 (r'return\(', "return is not a function"),
111 (r'return\(', "return is not a function"),
109 (r' ;', "no space before ;"),
112 (r' ;', "no space before ;"),
110 (r'\w+\* \w+', "use int *foo, not int* foo"),
113 (r'\w+\* \w+', "use int *foo, not int* foo"),
111 (r'\([^\)]+\) \w+', "use (int)foo, not (int) foo"),
114 (r'\([^\)]+\) \w+', "use (int)foo, not (int) foo"),
112 (r'\S+ (\+\+|--)', "use foo++, not foo ++"),
115 (r'\S+ (\+\+|--)', "use foo++, not foo ++"),
113 (r'\w,\w', "missing whitespace after ,"),
116 (r'\w,\w', "missing whitespace after ,"),
114 (r'\w[+/*]\w', "missing whitespace in expression"),
117 (r'\w[+/*]\w', "missing whitespace in expression"),
115 (r'^#\s+\w', "use #foo, not # foo"),
118 (r'^#\s+\w', "use #foo, not # foo"),
116 (r'[^\n]\Z', "no trailing newline"),
119 (r'[^\n]\Z', "no trailing newline"),
117 ]
120 ]
118
121
119 cfilters = [
122 cfilters = [
120 (r'(/\*)(((\*(?!/))|[^*])*)\*/', repccomment),
123 (r'(/\*)(((\*(?!/))|[^*])*)\*/', repccomment),
121 (r'''(?<!")(")(([^"]|\\")+"(?!"))''', repquote),
124 (r'''(?<!")(")(([^"]|\\")+"(?!"))''', repquote),
122 (r'''(#\s*include\s+<)([^>]+)>''', repinclude),
125 (r'''(#\s*include\s+<)([^>]+)>''', repinclude),
123 (r'(\()([^)]+\))', repcallspaces),
126 (r'(\()([^)]+\))', repcallspaces),
124 ]
127 ]
125
128
126 checks = [
129 checks = [
127 ('python', r'.*\.(py|cgi)$', pyfilters, pypats),
130 ('python', r'.*\.(py|cgi)$', pyfilters, pypats),
128 ('test script', r'(.*/)?test-[^.~]*$', testfilters, testpats),
131 ('test script', r'(.*/)?test-[^.~]*$', testfilters, testpats),
129 ('c', r'.*\.c$', cfilters, cpats),
132 ('c', r'.*\.c$', cfilters, cpats),
130 ]
133 ]
131
134
132 if len(sys.argv) == 1:
135 if len(sys.argv) == 1:
133 check = glob.glob("*")
136 check = glob.glob("*")
134 else:
137 else:
135 check = sys.argv[1:]
138 check = sys.argv[1:]
136
139
137 for f in check:
140 for f in check:
138 for name, match, filters, pats in checks:
141 for name, match, filters, pats in checks:
139 fc = 0
142 fc = 0
140 if not re.match(match, f):
143 if not re.match(match, f):
141 continue
144 continue
142 pre = post = open(f).read()
145 pre = post = open(f).read()
143 if "no-" + "check-code" in pre:
146 if "no-" + "check-code" in pre:
144 break
147 break
145 for p, r in filters:
148 for p, r in filters:
146 post = re.sub(p, r, post)
149 post = re.sub(p, r, post)
147 # print post # uncomment to show filtered version
150 # print post # uncomment to show filtered version
148 z = enumerate(zip(pre.splitlines(), post.splitlines(True)))
151 z = enumerate(zip(pre.splitlines(), post.splitlines(True)))
149 for n, l in z:
152 for n, l in z:
150 if "check-code" + "-ignore" in l[0]:
153 if "check-code" + "-ignore" in l[0]:
151 continue
154 continue
152 lc = 0
155 lc = 0
153 for p, msg in pats:
156 for p, msg in pats:
154 if re.search(p, l[1]):
157 if re.search(p, l[1]):
155 if not lc:
158 if not lc:
156 print "%s:%d:" % (f, n + 1)
159 print "%s:%d:" % (f, n + 1)
157 print " > %s" % l[0]
160 print " > %s" % l[0]
158 print " %s" % msg
161 print " %s" % msg
159 lc += 1
162 lc += 1
160 fc += 1
163 fc += 1
161 if fc == 15:
164 if fc == 15:
162 print " (too many errors, giving up)"
165 print " (too many errors, giving up)"
163 break
166 break
164 break
167 break
General Comments 0
You need to be logged in to leave comments. Login now