2008-11-04 Cameron Zwarich <zwarich@apple.com>
[webkit/qt.git] / WebKitTools / Scripts / clean-header-guards
blob2bad046edf27b3097fe416eadccf9712d4d031bb
1 #!/usr/bin/ruby
3 require 'find'
4 require 'optparse'
6 options = {}
7 OptionParser.new do |opts|
8 opts.banner = "Usage: clean-header-guards [options]"
10 opts.on("--prefix [PREFIX]", "Append a header prefix to all guards") do |prefix|
11 options[:prefix] = prefix
12 end
13 end.parse!
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
22 # we don't own any of these headers
23 %r|icu/unicode|,
24 %r|platform/graphics/cairo|,
25 %r|platform/image-decoders|,
27 /config.h/ # changing this one sounds scary
28 ].freeze
30 IgnoreFileNamesPattern = Regexp.union(*IgnoredFilenamePatterns).freeze
32 Find::find(".") do |filename|
33 next unless filename =~ /\.h$/
34 next if filename.match(IgnoreFileNamesPattern)
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