OLD | NEW |
---|---|
1 # Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import re | 5 import re |
6 import sys | 6 import sys |
7 | 7 |
8 # A tool to add "#pragma once" lines to files that don't have it yet. | 8 # A tool to add "#pragma once" lines to files that don't have it yet. |
9 # Intended usage: | 9 # Intended usage: |
10 # find chrome -name '*.h' -exec python tools/pragmaonce/pragmaonce.py {} \; | 10 # find chrome -name '*.h' -exec python tools/pragmaonce/pragmaonce.py {} \; |
11 | 11 |
12 # Some files have absurdly long comments at the top | 12 # Some files have absurdly long comments at the top |
13 NUM_LINES_TO_SCAN_FOR_GUARD = 250 | 13 NUM_LINES_TO_SCAN_FOR_GUARD = 250 |
14 | 14 |
15 def main(filename): | 15 def main(filename): |
16 f = open(filename) | 16 f = open(filename) |
17 lines = f.readlines() | 17 lines = f.readlines() |
18 f.close() | 18 f.close() |
19 | 19 |
20 index = -1 | 20 index = -1 |
21 for i in xrange(min(NUM_LINES_TO_SCAN_FOR_GUARD, len(lines) - 1)): | 21 for i in xrange(min(NUM_LINES_TO_SCAN_FOR_GUARD, len(lines) - 1)): |
22 m1 = re.match(r'^#ifndef ([A-Z_]+)', lines[i]) | 22 m1 = re.match(r'^#ifndef ([A-Z_0-9]+$)', lines[i]) |
Mark Mentovai
2010/07/26 18:58:42
I would take the $ out of the () in each of these.
Nico
2010/07/26 20:13:02
d'ohne
| |
23 m2 = re.match(r'^#define ([A-Z_]+)', lines[i + 1]) | 23 m2 = re.match(r'^#define ([A-Z_0-9]+$)', lines[i + 1]) |
Mark Mentovai
2010/07/26 18:58:42
This would probably perform slightly better if you
| |
24 if m1 and m2: | 24 if m1 and m2: |
25 if m1.group(1) != m2.group(1): | 25 if m1.group(1) != m2.group(1): |
26 print 'Skipping', filename, \ | 26 print 'Skipping', filename, \ |
27 ': Broken include guard (%s, %s)' % (m1.group(1), m2.group(1)) | 27 ': Broken include guard (%s, %s)' % (m1.group(1), m2.group(1)) |
28 index = i + 2 | 28 index = i + 2 |
29 break | 29 break |
30 | 30 |
31 if index == -1: | 31 if index == -1: |
32 print 'Skipping', filename, ': no include guard found' | 32 print 'Skipping', filename, ': no include guard found' |
33 return | 33 return |
34 | 34 |
35 if index < len(lines) and re.match(r'#pragma once', lines[index]): | 35 if index < len(lines) and re.match(r'#pragma once', lines[index]): |
36 # The pragma is already there. | 36 # The pragma is already there. |
37 return | 37 return |
38 | 38 |
39 lines.insert(index, "#pragma once\n") | 39 lines.insert(index, "#pragma once\n") |
40 | 40 |
41 f = open(filename, 'w') | 41 f = open(filename, 'w') |
42 f.write(''.join(lines)) | 42 f.write(''.join(lines)) |
43 f.close() | 43 f.close() |
44 | 44 |
45 if __name__ == '__main__': | 45 if __name__ == '__main__': |
46 if len(sys.argv) != 2: | 46 if len(sys.argv) != 2: |
47 print >>sys.stderr, "Usage: %s inputfile" % sys.argv[0] | 47 print >>sys.stderr, "Usage: %s inputfile" % sys.argv[0] |
48 sys.exit(1) | 48 sys.exit(1) |
49 main(sys.argv[1]) | 49 main(sys.argv[1]) |
OLD | NEW |