001: /* Soot - a J*va Optimization Framework
002: * Copyright (C) 2003 Ondrej Lhotak
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the
016: * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
017: * Boston, MA 02111-1307, USA.
018: */
019:
020: package soot;
021:
022: import java.util.*;
023: import soot.tagkit.*;
024:
025: /**
026: * Adds PositionTags to ValueBoxes to identify their position in the output.
027: */
028: public class AttributesUnitPrinter {
029:
030: private Stack<Integer> startOffsets;
031: private int endOffset;
032: private int startStmtOffset;
033: private int startLn;
034: private int currentLn;
035: private int lastNewline;
036: private UnitPrinter printer;
037:
038: public AttributesUnitPrinter(int currentLnNum) {
039: this .currentLn = currentLnNum;
040: }
041:
042: public void startUnit(Unit u) {
043: startLn = currentLn;
044: startStmtOffset = output().length() - lastNewline;
045: }
046:
047: public void endUnit(Unit u) {
048: int endStmtOffset = output().length() - lastNewline;
049: //G.v().out.println("u: "+u.toString());
050: if (hasTag(u)) {
051: //G.v().out.println("u: "+u.toString()+" has tag");
052: u.addTag(new JimpleLineNumberTag(startLn, currentLn));
053: }
054: if (hasColorTag(u)) {
055: u.addTag(new PositionTag(startStmtOffset, endStmtOffset));
056: }
057: }
058:
059: public void startValueBox(ValueBox u) {
060: if (startOffsets == null) {
061: startOffsets = new Stack<Integer>();
062: }
063: startOffsets.push(new Integer(output().length() - lastNewline));
064: }
065:
066: public void endValueBox(ValueBox u) {
067: endOffset = output().length() - lastNewline;
068: if (hasColorTag(u)) {
069: u.addTag(new PositionTag(startOffsets.pop().intValue(),
070: endOffset));
071: }
072: }
073:
074: private boolean hasTag(Host h) {
075: if (h instanceof Unit) {
076: Iterator usesAndDefsIt = ((Unit) h).getUseAndDefBoxes()
077: .iterator();
078: while (usesAndDefsIt.hasNext()) {
079: if (hasTag((ValueBox) usesAndDefsIt.next()))
080: return true;
081: }
082: }
083: if (h.getTags().isEmpty())
084: return false;
085: return true;
086: }
087:
088: private boolean hasColorTag(Host h) {
089: Iterator it = h.getTags().iterator();
090: while (it.hasNext()) {
091: if (it.next() instanceof ColorTag)
092: return true;
093: }
094: return false;
095: }
096:
097: public void setEndLn(int ln) {
098: currentLn = ln;
099: }
100:
101: public int getEndLn() {
102: return currentLn;
103: }
104:
105: public void newline() {
106: currentLn++;
107: lastNewline = output().length();
108: }
109:
110: private StringBuffer output() {
111: return printer.output();
112: }
113:
114: public void setUnitPrinter(UnitPrinter up) {
115: printer = up;
116: }
117: }
|