Package: LexLocation

LexLocation

nameinstructionbranchcomplexitylinemethod
LexLocation()
M: 0 C: 14
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
LexLocation(File, String, int, int, int, int, int, int)
M: 0 C: 49
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 16
100%
M: 0 C: 1
100%
LexLocation(String, String, int, int, int, int, int, int)
M: 52 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 16 C: 0
0%
M: 1 C: 0
0%
addSpan(LexNameToken, LexToken)
M: 0 C: 34
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
clearAfter(File, int, int)
M: 0 C: 45
100%
M: 2 C: 8
80%
M: 2 C: 4
67%
M: 0 C: 11
100%
M: 0 C: 1
100%
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%
clone()
M: 0 C: 30
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
equals(Object)
M: 2 C: 32
94%
M: 3 C: 7
70%
M: 3 C: 3
50%
M: 1 C: 6
86%
M: 0 C: 1
100%
executable(boolean)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
getAllLocations()
M: 17 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
getEndLine()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getEndOffset()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getEndPos()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getExecutable()
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%
getFile()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getHits()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getModule()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getNameSpans()
M: 32 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 7 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: 10 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%
getStartLine()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getStartOffset()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getStartPos()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
hashCode()
M: 0 C: 14
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
hit()
M: 0 C: 10
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
mergeHits(File, File)
M: 100 C: 0
0%
M: 12 C: 0
0%
M: 7 C: 0
0%
M: 20 C: 0
0%
M: 1 C: 0
0%
readResolve()
M: 2 C: 9
82%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 1 C: 3
75%
M: 0 C: 1
100%
resetLocations()
M: 0 C: 23
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
setHits(long)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
static {...}
M: 0 C: 15
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
toShortString()
M: 2 C: 21
91%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 1 C: 2
67%
M: 0 C: 1
100%
toString()
M: 31 C: 42
58%
M: 6 C: 4
40%
M: 5 C: 1
17%
M: 2 C: 4
67%
M: 0 C: 1
100%
within(ILexLocation)
M: 40 C: 0
0%
M: 14 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: *
3: *        Copyright (c) 2008 Fujitsu Services Ltd.
4: *
5: *        Author: Nick Battle
6: *
7: *        This file is part of VDMJ.
8: *
9: *        VDMJ is free software: you can redistribute it and/or modify
10: *        it under the terms of the GNU General Public License as published by
11: *        the Free Software Foundation, either version 3 of the License, or
12: *        (at your option) any later version.
13: *
14: *        VDMJ is distributed in the hope that it will be useful,
15: *        but WITHOUT ANY WARRANTY; without even the implied warranty of
16: *        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17: *        GNU General Public License for more details.
18: *
19: *        You should have received a copy of the GNU General Public License
20: *        along with VDMJ. If not, see <http://www.gnu.org/licenses/>.
21: *
22: ******************************************************************************/
23:
24: package org.overture.ast.lex;
25:
26: import java.io.BufferedReader;
27: import java.io.File;
28: import java.io.FileReader;
29: import java.io.IOException;
30: import java.io.ObjectStreamException;
31: import java.io.Serializable;
32: import java.util.HashMap;
33: import java.util.List;
34: import java.util.ListIterator;
35: import java.util.Map;
36: import java.util.Map.Entry;
37: import java.util.Vector;
38:
39: import org.overture.ast.intf.lex.ILexLocation;
40: import org.overture.ast.node.ExternalNode;
41:
42: /**
43: * A class to hold the location of a token.
44: */
45:
46: public class LexLocation implements Serializable, ExternalNode, ILexLocation
47: {
48:         @Override
49:         public LexLocation clone()
50:         {
51:                 LexLocation location = new LexLocation(file, module, startLine, startPos, endLine, endPos, startOffset, endOffset);
52:                 location.hits = hits;
53:                 location.executable = executable;
54:                 return location;
55:         }
56:
57:         public static boolean absoluteToStringLocation = true;
58:
59:         private static final long serialVersionUID = 1L;
60:
61:         /** A collection of all LexLocation objects. */
62:         private static List<LexLocation> allLocations = new Vector<LexLocation>();
63:
64:         /** A unique map of LexLocation objects, for rapid searching. */
65:         private static Map<LexLocation, LexLocation> uniqueLocations = new HashMap<LexLocation, LexLocation>();
66:
67:         /** A map of class names to their lexical span, for coverage. */
68:         private static Map<LexNameToken, LexLocation> nameSpans = new HashMap<LexNameToken, LexLocation>();// TODO
69:
70:
71:         /** True if the location is executable. */
72:         private boolean executable = false;
73:
74:         /** The filename of the token. */
75:         public final File file;
76:         /** The module/class name of the token. */
77:         public final String module;
78:         /** The line number of the start of the token. */
79:         public final int startLine;
80:         /** The character position of the start of the token. */
81:         public final int startPos;
82:         /** The last line of the token. */
83:         public final int endLine;
84:         /** The character position of the end of the token. */
85:         public final int endPos;
86:
87:         public final int startOffset;
88:
89:         public final int endOffset;
90:
91:         /** The number of times the location has been executed. */
92:         public long hits = 0;
93:
94:         /**
95:          * Create a location with the given fields.
96:          *
97:          * @param file
98:          * @param module
99:          * @param startLine
100:          * @param startPos
101:          * @param endLine
102:          * @param endPos
103:          * @param startOffset
104:          * @param endOffset
105:          */
106:
107:         public LexLocation(File file, String module, int startLine, int startPos,
108:                         int endLine, int endPos, int startOffset, int endOffset)
109:         {
110:                 this.file = file;
111:                 this.module = module;
112:                 this.startLine = startLine;
113:                 this.startPos = startPos;
114:                 this.endLine = endLine;
115:                 this.endPos = endPos;
116:                 this.startOffset = startOffset;
117:                 this.endOffset = endOffset;
118:
119:                 synchronized (allLocations)
120:                 {
121:                         allLocations.add(this);
122:                         uniqueLocations.put(this, this);
123:                 }
124:         }
125:
126:         public LexLocation(String filePath, String module, int startLine,
127:                         int startPos, int endLine, int endPos, int startOffset,
128:                         int endOffset)
129:         {
130:                 this.file = new File(filePath);
131:                 this.module = module;
132:                 this.startLine = startLine;
133:                 this.startPos = startPos;
134:                 this.endLine = endLine;
135:                 this.endPos = endPos;
136:                 this.startOffset = startOffset;
137:                 this.endOffset = endOffset;
138:
139:                 synchronized (allLocations)
140:                 {
141:                         allLocations.add(this);
142:                         uniqueLocations.put(this, this);
143:                 }
144:         }
145:
146:         /**
147:          * Create a default location.
148:          */
149:
150:         public LexLocation()
151:         {
152:                 this(new File("?"), "?", 0, 0, 0, 0, 0, 0);
153:         }
154:
155:         @Override
156:         public boolean getExecutable()
157:         {
158:                 return executable;
159:         }
160:
161:         @Override
162:         public long getHits()
163:         {
164:                 return hits;
165:         }
166:
167:         @Override
168:         public void setHits(long hits)
169:         {
170:                 this.hits = hits;
171:         }
172:
173:         @Override
174:         public File getFile()
175:         {
176:                 return file;
177:         }
178:
179:         @Override
180:         public String getModule()
181:         {
182:                 return module;
183:         }
184:
185:         @Override
186:         public int getStartLine()
187:         {
188:                 return startLine;
189:         }
190:
191:         @Override
192:         public int getStartPos()
193:         {
194:                 return startPos;
195:         }
196:
197:         @Override
198:         public int getEndLine()
199:         {
200:                 return endLine;
201:         }
202:
203:         @Override
204:         public int getEndPos()
205:         {
206:                 return endPos;
207:         }
208:
209:         @Override
210:         public int getStartOffset()
211:         {
212:                 return startOffset;
213:         }
214:
215:         @Override
216:         public int getEndOffset()
217:         {
218:                 return endOffset;
219:         }
220:
221:         @Override
222:         public String toString()
223:         {
224:•                if (file.getPath().equals("?"))
225:                 {
226:                         return ""; // Default LexLocation has no location string
227:•                } else if (module == null || module.equals(""))
228:                 {
229:•                        return "in '" + (absoluteToStringLocation ? file : file.getName())
230:                                         + "' at line " + startLine + ":" + startPos;
231:                 } else
232:                 {
233:•                        return "in '" + module + "' ("
234:                                         + (absoluteToStringLocation ? file : file.getName())
235:                                         + ") at line " + startLine + ":" + startPos;
236:                 }
237:         }
238:
239:         public String toShortString()
240:         {
241:•                if (file.getPath().equals("?"))
242:                 {
243:                         return ""; // Default LexLocation has no location string
244:                 } else
245:                 {
246:                         return "at " + startLine + ":" + startPos;
247:                 }
248:         }
249:
250:         /**
251:          * Method to resolve existing locations during de-serialise - as used during deep copy. This is to avoid problems
252:          * with coverage.
253:          */
254:         private Object readResolve() throws ObjectStreamException
255:         {
256:                 LexLocation existing = uniqueLocations.get(this);
257:
258:•                if (existing == null)
259:                 {
260:                         return this;
261:                 } else
262:                 {
263:                         return existing;
264:                 }
265:         }
266:
267:         @Override
268:         public boolean equals(Object other)
269:         {
270:•                if (other instanceof ILexLocation)
271:                 {
272:                         ILexLocation lother = (ILexLocation) other;
273:
274:•                        return startPos == lother.getStartPos()
275:•                                        && startLine == lother.getStartLine()
276:•                                        && module.equals(lother.getModule())
277:•                                        && file.equals(lother.getFile());
278:                 }
279:
280:                 return false;
281:         }
282:
283:         @Override
284:         public int hashCode()
285:         {
286:                 return file.hashCode() + module.hashCode() + startLine + startPos;
287:         }
288:
289:         public boolean within(ILexLocation span)
290:         {
291:•                return (startLine > span.getStartLine() || startLine == span.getStartLine()
292:•                                && startPos >= span.getStartPos())
293:•                                && (startLine <= span.getEndLine() || startLine == span.getEndLine()
294:•                                                && startPos < span.getEndPos())
295:•                                && file.equals(span.getFile());
296:         }
297:
298:         public void executable(boolean exe)
299:         {
300:                 executable = exe;
301:         }
302:
303:         public void hit()
304:         {
305:•                if (executable)
306:                 {
307:                         hits++;
308:                 }
309:         }
310:
311:         public static void clearLocations()
312:         {
313:                 synchronized (allLocations)
314:                 {
315:•                        for (LexLocation loc : allLocations)
316:                         {
317:                                 loc.hits = 0;
318:                         }
319:                 }
320:         }
321:
322:         public static void resetLocations()
323:         {
324:                 synchronized (allLocations)
325:                 {
326:                         allLocations = new Vector<LexLocation>();
327:                 }
328:
329:                 synchronized (uniqueLocations)
330:                 {
331:                         uniqueLocations = new HashMap<LexLocation, LexLocation>();
332:                 }
333:
334:                 // synchronized (locationToAstNode)
335:                 // {
336:                 // locationToAstNode = new Hashtable<LexLocation, INode>();
337:                 // }
338:                 //
339:                 // synchronized (nameSpans)
340:                 // {
341:                 // nameSpans = new HashMap<LexNameToken, LexLocation>();
342:                 // }
343:         }
344:
345:         public static void clearAfter(File file, int linecount, int charpos)
346:         {
347:                 // Called from the LexTokenReader's pop method, to remove any
348:                 // locations "popped". We assume any pushes are on the end of
349:                 // the vector.
350:                 synchronized (allLocations)
351:                 {
352:                         ListIterator<LexLocation> it = allLocations.listIterator(allLocations.size());
353:
354:•                        while (it.hasPrevious())
355:                         {
356:                                 LexLocation l = it.previous();
357:
358:•                                if (!l.file.equals(file) || l.startLine < linecount
359:                                                 || l.startLine == linecount && l.startPos < charpos)
360:                                 {
361:                                         break;
362:                                 } else
363:                                 {
364:                                         it.remove();
365:                                         uniqueLocations.remove(l);
366:                                 }
367:                         }
368:                 }
369:         }
370:
371:         public static void addSpan(LexNameToken name, LexToken upto)
372:         {
373:                 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());
374:
375:                 nameSpans.put(name, span);
376:         }
377:
378:         public static List<Integer> getSourceList(File file)
379:         {
380:                 List<Integer> lines = new Vector<Integer>();
381:                 int last = 0;
382:
383:                 synchronized (allLocations)
384:                 {
385:•                        for (LexLocation l : allLocations)
386:                         {
387:•                                if (l.executable && l.startLine != last && l.file.equals(file))
388:                                 {
389:                                         lines.add(l.startLine);
390:                                         last = l.startLine;
391:                                 }
392:                         }
393:                 }
394:
395:                 return lines;
396:         }
397:
398:         public static List<LexLocation> getSourceLocations(File file)
399:         {
400:                 List<LexLocation> locations = new Vector<LexLocation>();
401:
402:                 synchronized (allLocations)
403:                 {
404:•                        for (LexLocation l : allLocations)
405:                         {
406:•                                if (l.executable && l.file.equals(file))
407:                                 {
408:                                         locations.add(l);
409:                                 }
410:                         }
411:                 }
412:
413:                 return locations;
414:         }
415:
416:         public static void mergeHits(File source, File coverage) throws IOException
417:         {
418:                 List<LexLocation> locations = getSourceLocations(source);
419:                 BufferedReader br = new BufferedReader(new FileReader(coverage));
420:                 String line = br.readLine();
421:
422:•                while (line != null)
423:                 {
424:•                        if (line.charAt(0) == '+')
425:                         {
426:                                 // Hit lines are "+line from-to=hits"
427:
428:                                 int s1 = line.indexOf(' ');
429:                                 int s2 = line.indexOf('-');
430:                                 int s3 = line.indexOf('=');
431:
432:                                 int lnum = Integer.parseInt(line.substring(1, s1));
433:                                 int from = Integer.parseInt(line.substring(s1 + 1, s2));
434:                                 int to = Integer.parseInt(line.substring(s2 + 1, s3));
435:                                 int hits = Integer.parseInt(line.substring(s3 + 1));
436:
437:•                                for (LexLocation l : locations) // Only executable locations
438:                                 {
439:•                                        if (l.startLine == lnum && l.startPos == from
440:                                                         && l.endPos == to)
441:                                         {
442:                                                 l.hits += hits;
443:                                                 break;
444:                                         }
445:                                 }
446:                         }
447:
448:                         line = br.readLine();
449:                 }
450:
451:                 br.close();
452:         }
453:
454:         public static List<ILexLocation> getAllLocations()
455:         {
456:                 Vector<ILexLocation> tmp = new Vector<ILexLocation>();
457:                 synchronized (allLocations) {
458:                         tmp.addAll(allLocations);
459:                 }
460:                 return tmp;
461:         }
462:         
463:         public static Map<LexNameToken, ILexLocation> getNameSpans()
464:         {
465:                 Map<LexNameToken, ILexLocation> tmp = new HashMap<>();
466:                 synchronized (nameSpans) {
467:•                for (Entry<LexNameToken, LexLocation> entry : nameSpans.entrySet()) {
468:                         tmp.put(entry.getKey(), entry.getValue());
469:                 }        
470:                 }
471:                 return tmp;
472:                 
473:         }
474: }