forked from soot-oss/soot
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTagCollector.java
More file actions
186 lines (165 loc) · 4.86 KB
/
TagCollector.java
File metadata and controls
186 lines (165 loc) · 4.86 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package soot.xml;
/*-
* #%L
* Soot - a J*va Optimization Framework
* %%
* Copyright (C) 2004 Jennifer Lhotak
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 2.1 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
* #L%
*/
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
import soot.Body;
import soot.SootClass;
import soot.SootField;
import soot.SootMethod;
import soot.Unit;
import soot.ValueBox;
import soot.tagkit.Host;
import soot.tagkit.JimpleLineNumberTag;
import soot.tagkit.KeyTag;
import soot.tagkit.SourceFileTag;
import soot.tagkit.Tag;
public class TagCollector {
private final ArrayList<Attribute> attributes;
private final ArrayList<Key> keys;
public TagCollector() {
this.attributes = new ArrayList<Attribute>();
this.keys = new ArrayList<Key>();
}
public boolean isEmpty() {
return attributes.isEmpty() && keys.isEmpty();
}
/**
* Convenience function for <code>collectTags(sc, true)</code>.
*
* @param sc
*/
public void collectTags(SootClass sc) {
collectTags(sc, true);
}
/**
* Collect tags from all fields and methods of <code>sc</code>. If <code>includeBodies</code> is true, then tags are also
* collected from method bodies.
*
* @param sc
* The class from which to collect the tags.
* @param includeBodies
*/
public void collectTags(SootClass sc, boolean includeBodies) {
// tag the class
collectClassTags(sc);
// tag fields
for (SootField sf : sc.getFields()) {
collectFieldTags(sf);
}
// tag methods
for (SootMethod sm : sc.getMethods()) {
collectMethodTags(sm);
if (includeBodies && sm.hasActiveBody()) {
collectBodyTags(sm.getActiveBody());
}
}
}
public void collectKeyTags(SootClass sc) {
for (Tag next : sc.getTags()) {
if (next instanceof KeyTag) {
KeyTag kt = (KeyTag) next;
Key k = new Key(kt.red(), kt.green(), kt.blue(), kt.key());
k.aType(kt.analysisType());
keys.add(k);
}
}
}
public void printKeys(PrintWriter writerOut) {
for (Key k : keys) {
k.print(writerOut);
}
}
private void addAttribute(Attribute a) {
if (!a.isEmpty()) {
attributes.add(a);
}
}
private void collectHostTags(Host h) {
collectHostTags(h, t -> true);
}
private void collectHostTags(Host h, Predicate<Tag> include) {
List<Tag> tags = h.getTags();
if (!tags.isEmpty()) {
Attribute a = new Attribute();
for (Tag t : tags) {
if (include.test(t)) {
a.addTag(t);
}
}
addAttribute(a);
}
}
public void collectClassTags(SootClass sc) {
// All classes are tagged with their source files which
// is not worth outputing because it can be inferred from
// other information (like the name of the XML file).
collectHostTags(sc, t -> !(t instanceof SourceFileTag));
}
public void collectFieldTags(SootField sf) {
collectHostTags(sf);
}
public void collectMethodTags(SootMethod sm) {
if (sm.hasActiveBody()) {
collectHostTags(sm);
}
}
public synchronized void collectBodyTags(Body b) {
for (Unit u : b.getUnits()) {
Attribute ua = new Attribute();
JimpleLineNumberTag jlnt = null;
for (Tag t : u.getTags()) {
ua.addTag(t);
if (t instanceof JimpleLineNumberTag) {
jlnt = (JimpleLineNumberTag) t;
}
// System.out.println("adding unit tag: "+t);
}
addAttribute(ua);
for (ValueBox vb : u.getUseAndDefBoxes()) {
// PosColorAttribute attr = new PosColorAttribute();
if (!vb.getTags().isEmpty()) {
Attribute va = new Attribute();
for (Tag t : vb.getTags()) {
// System.out.println("adding vb tag: "+t);
va.addTag(t);
// System.out.println("vb: "+vb.getValue()+" tag: "+t);
if (jlnt != null) {
va.addTag(jlnt);
}
}
// also here add line tags of the unit
addAttribute(va);
// System.out.println("added att: "+va);
}
}
}
}
public void printTags(PrintWriter writerOut) {
for (Attribute a : attributes) {
// System.out.println("will print attr: "+a);
a.print(writerOut);
}
}
}