Chromium Code Reviews| Index: tools/pragmaonce/pragmaonce.py |
| diff --git a/tools/pragmaonce/pragmaonce.py b/tools/pragmaonce/pragmaonce.py |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..072cba6cc8409f29378bedfd07faad4a8481a11f |
| --- /dev/null |
| +++ b/tools/pragmaonce/pragmaonce.py |
| @@ -0,0 +1,45 @@ |
| +import re |
|
Evan Martin
2010/07/22 00:44:54
Copyright header plz thx.
Nico
2010/07/26 18:54:58
Presubmit caught this before committing.
|
| +import sys |
| + |
| +# A tool to add "#pragma once" lines to files that don't have it yet. |
| +# Intended usage: |
| +# find chrome -name '*.h' -exec python tools/pragmaonce/pragmaonce.py {} \; |
|
Evan Martin
2010/07/22 00:44:54
nit: extra double space
Nico
2010/07/26 18:54:58
Done.
|
| + |
| +# Some files have absurdly long comments at the top |
| +NUM_LINES_TO_SCAN_FOR_GUARD = 250 |
| + |
| +def main(filename): |
| + f = open(filename) |
| + lines = f.readlines() |
| + f.close() |
| + |
| + index = -1 |
| + for i in xrange(min(NUM_LINES_TO_SCAN_FOR_GUARD, len(lines) - 1)): |
| + m1 = re.match(r'^#ifndef ([A-Z_]+)', lines[i]) |
|
Mark Mentovai
2010/07/22 02:48:38
You should likely permit digits too.
Nico
2010/07/26 18:54:58
Since I don't have a "$" at the end, this happened
|
| + m2 = re.match(r'^#define ([A-Z_]+)', lines[i + 1]) |
| + if m1 and m2: |
| + if m1.group(1) != m2.group(1): |
| + print 'Skipping', filename, \ |
| + ': Broken include guard (%s, %s)' % (m1.group(1), m2.group(1)) |
| + index = i + 2 |
| + break |
| + |
| + if index == -1: |
| + print 'Skipping', filename, ': no include guard found' |
| + return |
| + |
| + if index < len(lines) and re.match(r'#pragma once', lines[index]): |
| + # The pragma is already there. |
| + return |
| + |
| + lines.insert(index, "#pragma once\n") |
| + |
| + f = open(filename, 'w') |
| + f.write(''.join(lines)) |
| + f.close() |
| + |
| +if __name__ == '__main__': |
| + if len(sys.argv) != 2: |
| + print >>sys.stderr, "Usage: %s inputfile" % sys.argv[0] |
| + sys.exit(1) |
| + main(sys.argv[1]) |