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
15 IgnoredFilenamePatterns = [
16 # ignore headers which are known not to have guard
20 /vcproj/, # anything inside a vcproj is in the windows wasteland
22 # we don't own any of these headers
24 %r|platform/graphics/cairo|,
25 %r|platform/image-decoders|,
27 /config.h/ # changing this one sounds scary
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|
38 match_results = contents.match(/#ifndef (\S+)\n#define \1/s)
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)
45 puts "Ignoring #{filename}, failed to find existing header guards."
47 tmp_filename = filename + ".tmp"
48 File.open(tmp_filename, "w+") do |new_file|
49 new_file.write(contents)
51 File.rename tmp_filename, filename