-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathinvestigate.rb
111 lines (86 loc) · 1.89 KB
/
investigate.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
require 'json'
require 'set'
## scratch code to perform initial investigation of the generated compile commands
## was curious about
## total number of files
## common includes
## common defines
## possibility of grouping files based on the compiler args
def grouping_key(args)
s = [].to_set
args.each do |a|
if a=~/^-I/ or a=~/^-D/
s.add a
end
end
s
end
def count_all(commands)
h = {}
h.default = 0
commands.each do |c|
grouping_key(c["arguments"]).each do |k|
h[k] = h[k]+1
end
end
h
end
def intersect_all(commands)
h = [].to_set
commands.each do |c|
h = h & grouping_key(c["arguments"])
end
h
end
data = JSON.parse(File.read('compile_commands.json'))
all = count_all data
important = all.select{|k,v| v > 2000}.sort_by {|k,v| v}.reverse
important.each {|k,v| puts "#{k}->#{v}"}
imp = [].to_set
important.each {|k,v| imp.add(k)}
class Set
def to_s
to_a.join(', ')
end
end
puts imp
def important_grouping_key(args, imp)
s = [].to_set
args.each do |a|
if a=~/^-I/ or a=~/^-D/ and imp.member? a
s.add a
end
end
s
end
puts (important_grouping_key(data[0]["arguments"], imp))
def group_all(commands, imp)
h = {}
h.default=0
commands.each do |c|
k=important_grouping_key(c["arguments"], imp)
h[k]=h[k]+1
end
h
end
puts "results"
g = group_all data, imp
g.each do |k,v|
puts "#{v} #{k}"
end
##############################
js = data
dirs = [].to_set
js.each {|j| dirs.add j["directory"]}
puts js.size
puts dirs
js.each do |j|
args = j["arguments"]
abort "not cc: #{args}" unless args[0].eql? "cc"
abort "not -c: #{args}" unless args[1].eql? "-c" or args[1].eql? "-S"
abort "not -o: #{args}" unless args[-3].eql? "-o"
abort "not file: #{args}" unless args[-1].eql? j["file"]
end
escaped_quote='\\' * 3 + '"'
puts escaped_quote
puts (js[0]["arguments"].join ' ').gsub(/"/, escaped_quote)