Package: LexLocationUtils

LexLocationUtils

nameinstructionbranchcomplexitylinemethod
LexLocationUtils()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
addAstNode(LexLocation, INode)
M: 13 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
addSpan(LexNameToken, LexToken)
M: 34 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
clearAfter(File, int, int)
M: 41 C: 0
0%
M: 10 C: 0
0%
M: 6 C: 0
0%
M: 12 C: 0
0%
M: 1 C: 0
0%
clearLocations()
M: 22 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
getAllLocations()
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getHitList(File)
M: 40 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
getHitLocations(File)
M: 61 C: 0
0%
M: 10 C: 0
0%
M: 6 C: 0
0%
M: 13 C: 0
0%
M: 1 C: 0
0%
getHitPercent(File)
M: 55 C: 0
0%
M: 10 C: 0
0%
M: 6 C: 0
0%
M: 12 C: 0
0%
M: 1 C: 0
0%
getLocationToAstNodeMap()
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getMissList(File)
M: 40 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
getMissLocations(File)
M: 61 C: 0
0%
M: 10 C: 0
0%
M: 6 C: 0
0%
M: 13 C: 0
0%
M: 1 C: 0
0%
getSourceList(File)
M: 47 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 11 C: 0
0%
M: 1 C: 0
0%
getSourceLocations(File)
M: 36 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
getSpanCalls(ILexNameToken)
M: 46 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 11 C: 0
0%
M: 1 C: 0
0%
getSpanNames(File)
M: 32 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
getSpanPercent(ILexNameToken)
M: 68 C: 0
0%
M: 10 C: 0
0%
M: 6 C: 0
0%
M: 16 C: 0
0%
M: 1 C: 0
0%
mergeHits(File, File)
M: 100 C: 0
0%
M: 12 C: 0
0%
M: 7 C: 0
0%
M: 21 C: 0
0%
M: 1 C: 0
0%
resetLocations()
M: 23 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 15 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * #%~
3: * The Overture Abstract Syntax Tree
4: * %%
5: * Copyright (C) 2008 - 2014 Overture
6: * %%
7: * This program is free software: you can redistribute it and/or modify
8: * it under the terms of the GNU General Public License as
9: * published by the Free Software Foundation, either version 3 of the
10: * License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public
18: * License along with this program. If not, see
19: * <http://www.gnu.org/licenses/gpl-3.0.html>.
20: * #~%
21: */
22: package org.overture.ast.lex;
23:
24: import java.io.BufferedReader;
25: import java.io.File;
26: import java.io.FileReader;
27: import java.io.IOException;
28: import java.io.Serializable;
29: import java.util.HashMap;
30: import java.util.Hashtable;
31: import java.util.List;
32: import java.util.ListIterator;
33: import java.util.Map;
34: import java.util.Vector;
35:
36: import org.overture.ast.intf.lex.ILexLocation;
37: import org.overture.ast.intf.lex.ILexNameToken;
38: import org.overture.ast.node.INode;
39:
40: public class LexLocationUtils implements Serializable
41: {
42:
43:         public static boolean absoluteToStringLocation = true;
44:
45:         private static final long serialVersionUID = 1L;
46:
47:         /** A collection of all LexLocation objects. */
48:         private static List<ILexLocation> allLocations = new Vector<ILexLocation>();
49:
50:         /** A collection of all LexLocation objects to the AstNodes. */
51:         private static Map<ILexLocation, INode> locationToAstNode = new Hashtable<ILexLocation, INode>();
52:
53:         /** A map of f/op/class names to their lexical span, for coverage. */
54:         private static Map<LexNameToken, ILexLocation> nameSpans = new HashMap<LexNameToken, ILexLocation>();// TODO
55:
56:         public static void clearLocations()
57:         {
58:                 synchronized (allLocations)
59:                 {
60:•                        for (ILexLocation loc : allLocations)
61:                         {
62:                                 loc.setHits(0);
63:                         }
64:                 }
65:         }
66:
67:         public static void resetLocations()
68:         {
69:                 synchronized (allLocations)
70:                 {
71:                         allLocations = new Vector<ILexLocation>();
72:                 }
73:
74:                 synchronized (locationToAstNode)
75:                 {
76:                         locationToAstNode = new Hashtable<ILexLocation, INode>();
77:                 }
78:                 //
79:                 // synchronized (nameSpans)
80:                 // {
81:                 // nameSpans = new HashMap<LexNameToken, LexLocation>();
82:                 // }
83:         }
84:
85:         public static void clearAfter(File file, int linecount, int charpos)
86:         {
87:                 // Called from the LexTokenReader's pop method, to remove any
88:                 // locations "popped". We assume any pushes are on the end of
89:                 // the vector.
90:                 synchronized (allLocations)
91:                 {
92:
93:                         ListIterator<ILexLocation> it = allLocations.listIterator(allLocations.size());
94:
95:•                        while (it.hasPrevious())
96:                         {
97:                                 ILexLocation l = it.previous();
98:
99:•                                if (!l.getFile().equals(file) || l.getStartLine() < linecount
100:•                                                || l.getStartLine() == linecount
101:•                                                && l.getStartPos() < charpos)
102:                                 {
103:                                         break;
104:                                 } else
105:                                 {
106:                                         it.remove();
107:                                 }
108:                         }
109:                 }
110:         }
111:
112:         public static void addSpan(LexNameToken name, LexToken upto)
113:         {
114:                 LexLocation span = new LexLocation(name.location.getFile(), name.location.getModule(), name.location.getStartLine(), name.location.getStartPos(), upto.location.getEndLine(), upto.location.getEndPos(), upto.location.getStartOffset(), upto.location.getEndOffset());
115:
116:                 nameSpans.put(name, span);
117:         }
118:
119:         public static LexNameList getSpanNames(File filename)
120:         {
121:                 LexNameList list = new LexNameList();
122:
123:•                for (LexNameToken name : nameSpans.keySet())
124:                 {
125:                         ILexLocation span = nameSpans.get(name);
126:
127:•                        if (span.getFile().equals(filename))
128:                         {
129:                                 list.add(name);
130:                         }
131:                 }
132:
133:                 return list;
134:         }
135:
136:         public static float getSpanPercent(ILexNameToken name)
137:         {
138:                 int hits = 0;
139:                 int misses = 0;
140:                 ILexLocation span = null;
141:
142:                 synchronized (nameSpans)
143:                 {
144:                         span = nameSpans.get(name);
145:                 }
146:
147:                 synchronized (allLocations)
148:                 {
149:•                        for (ILexLocation l : allLocations)
150:                         {
151:•                                if (l.getExecutable() && l.within(span))
152:                                 {
153:•                                        if (l.getHits() > 0)
154:                                         {
155:                                                 hits++;
156:                                         } else
157:                                         {
158:                                                 misses++;
159:                                         }
160:                                 }
161:                         }
162:                 }
163:
164:                 int sum = hits + misses;
165:•                return sum == 0 ? 0 : (float) (1000 * hits / sum) / 10; // NN.N%
166:         }
167:
168:         public static long getSpanCalls(ILexNameToken name)
169:         {
170:                 // The assumption is that the first executable location in
171:                 // the span for the name is hit as many time as the span is called.
172:
173:                 ILexLocation span = null;
174:
175:                 synchronized (nameSpans)
176:                 {
177:                         span = nameSpans.get(name);
178:                 }
179:
180:                 synchronized (allLocations)
181:                 {
182:•                        for (ILexLocation l : allLocations)
183:                         {
184:•                                if (l.getExecutable() && l.within(span))
185:                                 {
186:                                         return l.getHits();
187:                                 }
188:                         }
189:                 }
190:
191:                 return 0;
192:         }
193:
194:         public static List<Integer> getHitList(File file)
195:         {
196:                 List<Integer> hits = new Vector<Integer>();
197:
198:                 synchronized (allLocations)
199:                 {
200:•                        for (ILexLocation l : allLocations)
201:                         {
202:•                                if (l.getHits() > 0 && l.getFile().equals(file))
203:                                 {
204:                                         hits.add(l.getStartLine());
205:                                 }
206:                         }
207:                 }
208:
209:                 return hits;
210:         }
211:
212:         public static List<Integer> getMissList(File file)
213:         {
214:                 List<Integer> misses = new Vector<Integer>();
215:
216:                 synchronized (allLocations)
217:                 {
218:•                        for (ILexLocation l : allLocations)
219:                         {
220:•                                if (l.getHits() == 0 && l.getFile().equals(file))
221:                                 {
222:                                         misses.add(l.getStartLine());
223:                                 }
224:                         }
225:                 }
226:
227:                 return misses;
228:         }
229:
230:         public static List<Integer> getSourceList(File file)
231:         {
232:                 List<Integer> lines = new Vector<Integer>();
233:                 int last = 0;
234:
235:                 synchronized (allLocations)
236:                 {
237:•                        for (ILexLocation l : allLocations)
238:                         {
239:•                                if (l.getExecutable() && l.getStartLine() != last
240:•                                                && l.getFile().equals(file))
241:                                 {
242:                                         lines.add(l.getStartLine());
243:                                         last = l.getStartLine();
244:                                 }
245:                         }
246:                 }
247:
248:                 return lines;
249:         }
250:
251:         public static Map<Integer, List<ILexLocation>> getHitLocations(File file)
252:         {
253:                 Map<Integer, List<ILexLocation>> map = new HashMap<Integer, List<ILexLocation>>();
254:
255:                 synchronized (allLocations)
256:                 {
257:•                        for (ILexLocation l : allLocations)
258:                         {
259:•                                if (l.getExecutable() && l.getHits() > 0
260:•                                                && l.getFile().equals(file))
261:                                 {
262:                                         List<ILexLocation> list = map.get(l.getStartLine());
263:
264:•                                        if (list == null)
265:                                         {
266:                                                 list = new Vector<ILexLocation>();
267:                                                 map.put(l.getStartLine(), list);
268:                                         }
269:
270:                                         list.add(l);
271:                                 }
272:                         }
273:                 }
274:
275:                 return map;
276:         }
277:
278:         public static float getHitPercent(File file)
279:         {
280:                 int hits = 0;
281:                 int misses = 0;
282:
283:                 synchronized (allLocations)
284:                 {
285:•                        for (ILexLocation l : allLocations)
286:                         {
287:•                                if (l.getFile().equals(file) && l.getExecutable())
288:                                 {
289:•                                        if (l.getHits() > 0)
290:                                         {
291:                                                 hits++;
292:                                         } else
293:                                         {
294:                                                 misses++;
295:                                         }
296:                                 }
297:                         }
298:                 }
299:
300:                 int sum = hits + misses;
301:•                return sum == 0 ? 0 : (float) (1000 * hits / sum) / 10; // NN.N%
302:         }
303:
304:         public static Map<Integer, List<ILexLocation>> getMissLocations(File file)
305:         {
306:                 Map<Integer, List<ILexLocation>> map = new HashMap<Integer, List<ILexLocation>>();
307:
308:                 synchronized (allLocations)
309:                 {
310:•                        for (ILexLocation l : allLocations)
311:                         {
312:•                                if (l.getExecutable() && l.getHits() == 0
313:•                                                && l.getFile().equals(file))
314:                                 {
315:                                         List<ILexLocation> list = map.get(l.getStartLine());
316:
317:•                                        if (list == null)
318:                                         {
319:                                                 list = new Vector<ILexLocation>();
320:                                                 map.put(l.getStartLine(), list);
321:                                         }
322:
323:                                         list.add(l);
324:                                 }
325:                         }
326:                 }
327:
328:                 return map;
329:         }
330:
331:         public static List<ILexLocation> getSourceLocations(File file)
332:         {
333:                 List<ILexLocation> locations = new Vector<ILexLocation>();
334:
335:                 synchronized (allLocations)
336:                 {
337:•                        for (ILexLocation l : allLocations)
338:                         {
339:•                                if (l.getExecutable() && l.getFile().equals(file))
340:                                 {
341:                                         locations.add(l);
342:                                 }
343:                         }
344:                 }
345:
346:                 return locations;
347:         }
348:
349:         public static void mergeHits(File source, File coverage) throws IOException
350:         {
351:                 List<ILexLocation> locations = getSourceLocations(source);
352:                 BufferedReader br = new BufferedReader(new FileReader(coverage));
353:                 String line = br.readLine();
354:
355:•                while (line != null)
356:                 {
357:•                        if (line.charAt(0) == '+')
358:                         {
359:                                 // Hit lines are "+line from-to=hits"
360:
361:                                 int s1 = line.indexOf(' ');
362:                                 int s2 = line.indexOf('-');
363:                                 int s3 = line.indexOf('=');
364:
365:                                 int lnum = Integer.parseInt(line.substring(1, s1));
366:                                 int from = Integer.parseInt(line.substring(s1 + 1, s2));
367:                                 int to = Integer.parseInt(line.substring(s2 + 1, s3));
368:                                 int hits = Integer.parseInt(line.substring(s3 + 1));
369:
370:•                                for (ILexLocation l : locations) // Only executable locations
371:                                 {
372:•                                        if (l.getStartLine() == lnum && l.getStartPos() == from
373:•                                                        && l.getEndPos() == to)
374:                                         {
375:                                                 l.setHits(l.getHits() + hits);
376:                                                 // l.hits += hits;
377:                                                 break;
378:                                         }
379:                                 }
380:                         }
381:
382:                         line = br.readLine();
383:                 }
384:
385:                 br.close();
386:         }
387:
388:         // FIXME we know this is never called a new solutions is needed
389:         public static void addAstNode(LexLocation location, INode node)
390:         {
391:                 synchronized (locationToAstNode)
392:                 {
393:                         locationToAstNode.put(location, node);
394:                 }
395:         }
396:
397:         public static Map<ILexLocation, INode> getLocationToAstNodeMap()
398:         {
399:                 return locationToAstNode;
400:         }
401:
402:         public static List<ILexLocation> getAllLocations()
403:         {
404:                 return allLocations;
405:         }
406:
407: }