diff options
author | thakis@chromium.org <thakis@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-07-21 23:49:14 +0000 |
---|---|---|
committer | thakis@chromium.org <thakis@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-07-21 23:49:14 +0000 |
commit | 9b9e4bc59f6e0b7629912668a686f2d343c55eda (patch) | |
tree | bba1429ddd751c28b1aef8c6b210ec1dfe3784ac /tools/pragmaonce/pragmaonce.py | |
parent | 47856cd29f38898c056a9ae1fa96149b92020fda (diff) | |
download | chromium_src-9b9e4bc59f6e0b7629912668a686f2d343c55eda.zip chromium_src-9b9e4bc59f6e0b7629912668a686f2d343c55eda.tar.gz chromium_src-9b9e4bc59f6e0b7629912668a686f2d343c55eda.tar.bz2 |
Add python script that can add "#pragma once" to all our files.
Review URL: http://codereview.chromium.org/2842064
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@53271 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'tools/pragmaonce/pragmaonce.py')
-rw-r--r-- | tools/pragmaonce/pragmaonce.py | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/tools/pragmaonce/pragmaonce.py b/tools/pragmaonce/pragmaonce.py new file mode 100644 index 0000000..d3a24c5 --- /dev/null +++ b/tools/pragmaonce/pragmaonce.py @@ -0,0 +1,49 @@ +# Copyright (c) 2010 The Chromium Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +import re +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 {} \; + +# 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]) + 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]) |