]> git.lizzy.rs Git - rust.git/blob - src/etc/tidy.py
Remove alternate stack with sigaltstack before unmapping it.
[rust.git] / src / etc / tidy.py
1 # Copyright 2010-2014 The Rust Project Developers. See the COPYRIGHT
2 # file at the top-level directory of this distribution and at
3 # http://rust-lang.org/COPYRIGHT.
4 #
5 # Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 # http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 # <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 # option. This file may not be copied, modified, or distributed
9 # except according to those terms.
10
11 import sys
12 import fileinput
13 import subprocess
14 import re
15 import os
16 from licenseck import check_license
17 import snapshot
18
19 err = 0
20 cols = 100
21 cr_flag = "ignore-tidy-cr"
22 tab_flag = "ignore-tidy-tab"
23 linelength_flag = "ignore-tidy-linelength"
24
25 interesting_files = ['.rs', '.py', '.js', '.sh', '.c', '.h']
26 uninteresting_files = ['miniz.c', 'jquery', 'rust_android_dummy']
27
28
29 def report_error_name_no(name, no, s):
30     global err
31     print("%s:%d: %s" % (name, no, s))
32     err = 1
33
34
35 def report_err(s):
36     report_error_name_no(fileinput.filename(), fileinput.filelineno(), s)
37
38
39 def report_warn(s):
40     print("%s:%d: %s" % (fileinput.filename(),
41                          fileinput.filelineno(),
42                          s))
43
44
45 def do_license_check(name, contents):
46     if not check_license(name, contents):
47         report_error_name_no(name, 1, "incorrect license")
48
49
50 def update_counts(current_name):
51     global file_counts
52     global count_other_linted_files
53
54     _, ext = os.path.splitext(current_name)
55
56     if ext in interesting_files:
57         file_counts[ext] += 1
58     else:
59         count_other_linted_files += 1
60
61
62 def interesting_file(f):
63     if any(x in f for x in uninteresting_files):
64         return False
65
66     return any(os.path.splitext(f)[1] == ext for ext in interesting_files)
67
68
69 # Be careful to support Python 2.4, 2.6, and 3.x here!
70 config_proc = subprocess.Popen(["git", "config", "core.autocrlf"],
71                                stdout=subprocess.PIPE)
72 result = config_proc.communicate()[0]
73
74 true = "true".encode('utf8')
75 autocrlf = result.strip() == true if result is not None else False
76
77 current_name = ""
78 current_contents = ""
79 check_tab = True
80 check_cr = True
81 check_linelength = True
82
83 if len(sys.argv) < 2:
84     print("usage: tidy.py <src-dir>")
85     sys.exit(1)
86
87 src_dir = sys.argv[1]
88
89 count_lines = 0
90 count_non_blank_lines = 0
91 count_other_linted_files = 0
92
93 file_counts = {ext: 0 for ext in interesting_files}
94
95 all_paths = set()
96
97 try:
98     for (dirpath, dirnames, filenames) in os.walk(src_dir):
99         # Skip some third-party directories
100         skippable_dirs = {
101             'src/jemalloc',
102             'src/llvm',
103             'src/gyp',
104             'src/libbacktrace',
105             'src/libuv',
106             'src/compiler-rt',
107             'src/rt/hoedown',
108             'src/rustllvm',
109             'src/rt/valgrind',
110             'src/rt/msvc',
111             'src/rust-installer',
112             'src/liblibc',
113         }
114
115         if any(d in dirpath for d in skippable_dirs):
116             continue
117
118         file_names = [os.path.join(dirpath, f) for f in filenames
119                       if interesting_file(f)
120                       and not f.endswith("_gen.rs")
121                       and not ".#" is f]
122
123         if not file_names:
124             continue
125
126         for line in fileinput.input(file_names,
127                                 openhook=fileinput.hook_encoded("utf-8")):
128
129             filename = fileinput.filename()
130
131             if "tidy.py" not in filename:
132                 if "TODO" in line:
133                     report_err("TODO is deprecated; use FIXME")
134                 match = re.match(r'^.*/(\*|/!?)\s*XXX', line)
135                 if match:
136                     report_err("XXX is no longer necessary, use FIXME")
137                 match = re.match(r'^.*//\s*(NOTE.*)$', line)
138                 if match and "TRAVIS" not in os.environ:
139                     m = match.group(1)
140                     if "snap" in m.lower():
141                         report_warn(match.group(1))
142                 match = re.match(r'^.*//\s*SNAP\s+(\w+)', line)
143                 if match:
144                     hsh = match.group(1)
145                     date, rev = snapshot.curr_snapshot_rev()
146                     if not hsh.startswith(rev):
147                         report_err("snapshot out of date (" + date
148                             + "): " + line)
149                 else:
150                     if "SNAP " in line:
151                         report_warn("unmatched SNAP line: " + line)
152
153             if cr_flag in line:
154                 check_cr = False
155             if tab_flag in line:
156                 check_tab = False
157             if linelength_flag in line:
158                 check_linelength = False
159
160             if check_tab and ('\t' in line and
161                               "Makefile" not in filename):
162                 report_err("tab character")
163             if check_cr and not autocrlf and '\r' in line:
164                 report_err("CR character")
165             if line.endswith(" \n") or line.endswith("\t\n"):
166                 report_err("trailing whitespace")
167             line_len = len(line)-2 if autocrlf else len(line)-1
168
169             if check_linelength and line_len > cols:
170                 report_err("line longer than %d chars" % cols)
171
172             if fileinput.isfirstline():
173                 # This happens at the end of each file except the last.
174                 if current_name != "":
175                     update_counts(current_name)
176                     assert len(current_contents) > 0
177                     do_license_check(current_name, current_contents)
178
179                 current_name = filename
180                 current_contents = ""
181                 check_cr = True
182                 check_tab = True
183                 check_linelength = True
184
185             # Put a reasonable limit on the amount of header data we use for
186             # the licenseck
187             if len(current_contents) < 1000:
188                 current_contents += line
189
190             count_lines += 1
191             if line.strip():
192                 count_non_blank_lines += 1
193
194     if current_name != "":
195         update_counts(current_name)
196         assert len(current_contents) > 0
197         do_license_check(current_name, current_contents)
198
199 except UnicodeDecodeError as e:
200     report_err("UTF-8 decoding error " + str(e))
201
202 print
203 for ext in sorted(file_counts, key=file_counts.get, reverse=True):
204     print("* linted {} {} files".format(file_counts[ext], ext))
205 print("* linted {} other files".format(count_other_linted_files))
206 print("* total lines of code: {}".format(count_lines))
207 print("* total non-blank lines of code: {}".format(count_non_blank_lines))
208 print()
209
210 sys.exit(err)