|
1 #!/usr/bin/ruby |
|
2 |
|
3 require 'find' |
|
4 require 'optparse' |
|
5 |
|
6 options = {} |
|
7 OptionParser.new do |opts| |
|
8 opts.banner = "Usage: clean-header-guards [options]" |
|
9 |
|
10 opts.on("--prefix [PREFIX]", "Append a header prefix to all guards") do |prefix| |
|
11 options[:prefix] = prefix |
|
12 end |
|
13 end.parse! |
|
14 |
|
15 IgnoredFilenamePatterns = [ |
|
16 # ignore headers which are known not to have guard |
|
17 /WebCorePrefix/, |
|
18 /ForwardingHeaders/, |
|
19 %r|bindings/objc|, |
|
20 /vcproj/, # anything inside a vcproj is in the windows wasteland |
|
21 |
|
22 # we don't own any of these headers |
|
23 %r|icu/unicode|, |
|
24 %r|platform/graphics/cairo|, |
|
25 %r|platform/image-decoders|, |
|
26 |
|
27 /config.h/ # changing this one sounds scary |
|
28 ].freeze |
|
29 |
|
30 IgnoreFileNamesPattern = Regexp.union(*IgnoredFilenamePatterns).freeze |
|
31 |
|
32 Find::find(".") do |filename| |
|
33 next unless filename =~ /\.h$/ |
|
34 next if filename.match(IgnoreFileNamesPattern) |
|
35 |
|
36 File.open(filename, "r+") do |file| |
|
37 contents = file.read |
|
38 match_results = contents.match(/#ifndef (\S+)\n#define \1/s) |
|
39 if match_results |
|
40 current_guard = match_results[1] |
|
41 new_guard = File.basename(filename).sub('.', '_') |
|
42 new_guard = options[:prefix] + '_' + new_guard if options[:prefix] |
|
43 contents.gsub!(/#{current_guard}\b/, new_guard) |
|
44 else |
|
45 puts "Ignoring #{filename}, failed to find existing header guards." |
|
46 end |
|
47 tmp_filename = filename + ".tmp" |
|
48 File.open(tmp_filename, "w+") do |new_file| |
|
49 new_file.write(contents) |
|
50 end |
|
51 File.rename tmp_filename, filename |
|
52 end |
|
53 end |