-
Notifications
You must be signed in to change notification settings - Fork 11
/
find-ids.rb
65 lines (60 loc) · 2.04 KB
/
find-ids.rb
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
54
55
56
57
58
59
60
61
62
63
64
65
# This program should be called with a bunch of C (and possibly C++)
# source file paths on the command line and it emits the identifiers
# with their filenames and line numbers (useful for vim's quickfix command
# or equivalent - see :help :cbuf) in the files, sorted by:
#
# 1. Frequency - less frequent identifiers appear first.
# 2. Lexicographical order.
#
# This proved useful for refactoring / code cleanups
ignore_list = IO.read("scripts/ids-whitelist.txt").split(/\s+/)
ignore = Hash.new
for s in ignore_list do
ignore[s] = 0
end
col = Hash.new
ARGV.each do |fn|
if fn == "test_inline.c"
next
end
File.open(fn, "r") do |file|
begin
line_num = 1
while (l = file.gets)
l.gsub!(/\/\*.*?\*\//m, "");
l.gsub!(/(?:"([^\\]|\\.)*?")|(?:'[^'']+?')/m, " ");
l.gsub!(/\/\/.*/m, "");
if (m = /\A(.*?)((?:\/\*)|\z)/m.match(l))
before = m[1]
open_comment = m[2]
before.scan(/\b([a-zA-Z_]\w+)/).each{
|s| id = s[0]; col[id] ||= Array.new; col[id] << [fn, line_num]
}
if (open_comment == "/*")
found_line = false
while (l = file.gets)
line_num += 1
if (l.gsub!(/\A.*?\*\//, ""))
found_line = true
break
end
end
if (found_line)
redo
end
else
line_num += 1
end
end
end
rescue ArgumentError
puts "#{fn} is malformed UTF8-wise"
exit
end
end
end
puts col.keys.select { |id| ret = ignore.has_key?(id); if (ret) then ignore[id] += 1 ; end; !ret }. \
sort_by { |x| [col[x].length,x] }. \
map{ |x| col[x].map { |arr| arr[0] + ":" + arr[1].to_s + ":" + x }}. \
flatten
STDERR.puts(ignore.keys.select { |id| ignore[id] == 0 })