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