summaryrefslogtreecommitdiffstats
path: root/WebKitTools/Scripts/clean-header-guards
blob: 2bad046edf27b3097fe416eadccf9712d4d031bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/ruby

require 'find'
require 'optparse'

options = {}
OptionParser.new do |opts|
  opts.banner = "Usage: clean-header-guards [options]"
  
  opts.on("--prefix [PREFIX]", "Append a header prefix to all guards") do |prefix|
    options[:prefix] = prefix
  end
end.parse!

IgnoredFilenamePatterns = [
  # ignore headers which are known not to have guard
  /WebCorePrefix/, 
  /ForwardingHeaders/,
  %r|bindings/objc|, 
  /vcproj/, # anything inside a vcproj is in the windows wasteland
  
  # we don't own any of these headers
  %r|icu/unicode|,
  %r|platform/graphics/cairo|,
  %r|platform/image-decoders|,
  
  /config.h/ # changing this one sounds scary
].freeze

IgnoreFileNamesPattern = Regexp.union(*IgnoredFilenamePatterns).freeze

Find::find(".") do |filename|
  next unless filename =~ /\.h$/
  next if filename.match(IgnoreFileNamesPattern)
  
  File.open(filename, "r+") do |file|
    contents = file.read
    match_results = contents.match(/#ifndef (\S+)\n#define \1/s)
    if match_results
      current_guard = match_results[1]
      new_guard = File.basename(filename).sub('.', '_')
      new_guard = options[:prefix] + '_' + new_guard  if options[:prefix]
      contents.gsub!(/#{current_guard}\b/, new_guard)
    else
      puts "Ignoring #{filename}, failed to find existing header guards."
    end
    tmp_filename = filename + ".tmp"
    File.open(tmp_filename, "w+") do |new_file|
      new_file.write(contents)
    end
    File.rename tmp_filename, filename
  end
end