fixes to getting source positions from JVML
CAst rewriter abstraction
This commit is contained in:
parent
74cc8454e3
commit
9e35099326
@ -10,27 +10,31 @@
|
||||
*****************************************************************************/
|
||||
package com.ibm.wala.cast.js.translator;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import com.ibm.wala.cast.ir.translator.RewritingTranslatorToCAst;
|
||||
import com.ibm.wala.cast.ir.translator.TranslatorToCAst;
|
||||
import com.ibm.wala.cast.tree.CAstEntity;
|
||||
import com.ibm.wala.cast.tree.impl.CAstImpl;
|
||||
import com.ibm.wala.cast.tree.rewrite.CAstRewriterFactory;
|
||||
import com.ibm.wala.cast.tree.rewrite.CAstRewriter.CopyKey;
|
||||
import com.ibm.wala.cast.tree.rewrite.CAstRewriter.RewriteContext;
|
||||
import com.ibm.wala.cast.tree.rewrite.CAstRewriterFactory;
|
||||
import com.ibm.wala.classLoader.SourceFileModule;
|
||||
import com.ibm.wala.classLoader.SourceModule;
|
||||
|
||||
public class CAstRhinoTranslator implements TranslatorToCAst {
|
||||
public class CAstRhinoTranslator extends RewritingTranslatorToCAst implements TranslatorToCAst {
|
||||
private final List<CAstRewriterFactory> rewriters = new LinkedList<CAstRewriterFactory>();
|
||||
private final SourceModule M;
|
||||
private final boolean replicateForDoLoops;
|
||||
|
||||
private static String getName(SourceModule M) {
|
||||
if (M instanceof SourceFileModule) {
|
||||
return ((SourceFileModule) M).getClassName();
|
||||
} else {
|
||||
return M.getName();
|
||||
}
|
||||
}
|
||||
|
||||
public CAstRhinoTranslator(SourceModule M, boolean replicateForDoLoops) {
|
||||
this.M = M;
|
||||
this.replicateForDoLoops = replicateForDoLoops;
|
||||
super(M, new RhinoToAstTranslator(new CAstImpl(), M, getName(M), replicateForDoLoops));
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -41,20 +45,4 @@ public class CAstRhinoTranslator implements TranslatorToCAst {
|
||||
rewriters.add(factory);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CAstEntity translateToCAst() throws IOException, Error {
|
||||
String N;
|
||||
if (M instanceof SourceFileModule) {
|
||||
N = ((SourceFileModule) M).getClassName();
|
||||
} else {
|
||||
N = M.getName();
|
||||
}
|
||||
|
||||
CAstImpl Ast = new CAstImpl();
|
||||
CAstEntity entity = new RhinoToAstTranslator(Ast, M, N, replicateForDoLoops).translateToCAst();
|
||||
for(CAstRewriterFactory rwf : rewriters)
|
||||
entity = rwf.createCAstRewriter(Ast).rewrite(entity);
|
||||
return entity;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -108,6 +108,9 @@ import com.ibm.wala.cast.tree.CAstType;
|
||||
import com.ibm.wala.cast.tree.impl.CAstOperator;
|
||||
import com.ibm.wala.cast.tree.impl.CAstSymbolImpl;
|
||||
import com.ibm.wala.cast.tree.impl.RangePosition;
|
||||
import com.ibm.wala.cast.tree.rewrite.CAstRewriter.CopyKey;
|
||||
import com.ibm.wala.cast.tree.rewrite.CAstRewriter.RewriteContext;
|
||||
import com.ibm.wala.cast.tree.rewrite.CAstRewriterFactory;
|
||||
import com.ibm.wala.cast.tree.visit.CAstVisitor;
|
||||
import com.ibm.wala.cast.util.CAstPattern;
|
||||
import com.ibm.wala.classLoader.SourceModule;
|
||||
@ -117,7 +120,7 @@ import com.ibm.wala.util.collections.HashSetFactory;
|
||||
import com.ibm.wala.util.debug.Assertions;
|
||||
import com.ibm.wala.util.warnings.Warning;
|
||||
|
||||
public class RhinoToAstTranslator {
|
||||
public class RhinoToAstTranslator implements TranslatorToCAst {
|
||||
|
||||
/**
|
||||
* a dummy name to use for standard function calls, only used to distinguish
|
||||
@ -2439,4 +2442,9 @@ private CAstNode[] walkChildren(final Node n, WalkContext context) {
|
||||
this.doLoopTranslator = new DoLoopTranslator(replicateForDoLoops, Ast);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <C extends RewriteContext<K>, K extends CopyKey<K>> void addRewriter(CAstRewriterFactory<C, K> factory, boolean prepend) {
|
||||
assert false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -0,0 +1,41 @@
|
||||
package com.ibm.wala.cast.ir.translator;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import com.ibm.wala.cast.tree.CAstEntity;
|
||||
import com.ibm.wala.cast.tree.impl.CAstImpl;
|
||||
import com.ibm.wala.cast.tree.rewrite.CAstRewriter.CopyKey;
|
||||
import com.ibm.wala.cast.tree.rewrite.CAstRewriter.RewriteContext;
|
||||
import com.ibm.wala.cast.tree.rewrite.CAstRewriterFactory;
|
||||
import com.ibm.wala.classLoader.ModuleEntry;
|
||||
|
||||
public class RewritingTranslatorToCAst implements TranslatorToCAst {
|
||||
private final List<CAstRewriterFactory> rewriters = new LinkedList<CAstRewriterFactory>();
|
||||
protected final ModuleEntry M;
|
||||
private final TranslatorToCAst base;
|
||||
|
||||
public RewritingTranslatorToCAst(ModuleEntry m2, TranslatorToCAst base) {
|
||||
this.M = m2;
|
||||
this.base = base;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <C extends RewriteContext<K>, K extends CopyKey<K>> void addRewriter(CAstRewriterFactory<C, K> factory, boolean prepend) {
|
||||
if(prepend)
|
||||
rewriters.add(0, factory);
|
||||
else
|
||||
rewriters.add(factory);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CAstEntity translateToCAst() throws IOException, Error {
|
||||
CAstImpl Ast = new CAstImpl();
|
||||
CAstEntity entity = base.translateToCAst();
|
||||
for(CAstRewriterFactory rwf : rewriters)
|
||||
entity = rwf.createCAstRewriter(Ast).rewrite(entity);
|
||||
return entity;
|
||||
}
|
||||
|
||||
}
|
||||
@ -372,7 +372,7 @@ public abstract class CAstRewriter<C extends CAstRewriter.RewriteContext<K>, K e
|
||||
|
||||
@Override
|
||||
public CAstSourcePositionMap newPos() {
|
||||
if (theSource == null)
|
||||
if (theSource == null && pos != null)
|
||||
theSource = copySource(nodes, pos);
|
||||
return theSource;
|
||||
}
|
||||
|
||||
@ -10,6 +10,8 @@
|
||||
*******************************************************************************/
|
||||
package com.ibm.wala.classLoader;
|
||||
|
||||
import java.net.URL;
|
||||
|
||||
import com.ibm.wala.ipa.callgraph.ContextItem;
|
||||
import com.ibm.wala.shrikeCT.InvalidClassFileException;
|
||||
import com.ibm.wala.types.Descriptor;
|
||||
|
||||
@ -11,8 +11,11 @@
|
||||
package com.ibm.wala.classLoader;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.util.Collection;
|
||||
|
||||
import com.ibm.wala.classLoader.ShrikeClass.GetReader;
|
||||
import com.ibm.wala.ipa.cha.IClassHierarchy;
|
||||
import com.ibm.wala.shrikeBT.Decoder;
|
||||
import com.ibm.wala.shrikeBT.IndirectionData;
|
||||
@ -27,6 +30,7 @@ import com.ibm.wala.shrikeCT.InvalidClassFileException;
|
||||
import com.ibm.wala.shrikeCT.LineNumberTableReader;
|
||||
import com.ibm.wala.shrikeCT.LocalVariableTableReader;
|
||||
import com.ibm.wala.shrikeCT.SignatureReader;
|
||||
import com.ibm.wala.shrikeCT.SourceFileReader;
|
||||
import com.ibm.wala.shrikeCT.SourcePositionTableReader;
|
||||
import com.ibm.wala.shrikeCT.SourcePositionTableReader.Position;
|
||||
import com.ibm.wala.types.ClassLoaderReference;
|
||||
@ -149,17 +153,18 @@ public final class ShrikeCTMethod extends ShrikeBTMethod implements IBytecodeMet
|
||||
/** BEGIN Custom change: precise positions */
|
||||
|
||||
private static final class SPos implements SourcePosition {
|
||||
|
||||
String fileName;
|
||||
final int firstLine;
|
||||
final int lastLine;
|
||||
final int firstCol;
|
||||
final int lastCol;
|
||||
|
||||
private SPos(int firstLine, int lastLine, int firstCol, int lastCol) {
|
||||
private SPos(String fileName, int firstLine, int lastLine, int firstCol, int lastCol) {
|
||||
this.firstLine = firstLine;
|
||||
this.lastLine = lastLine;
|
||||
this.firstCol = firstCol;
|
||||
this.lastCol = lastCol;
|
||||
this.fileName = fileName;
|
||||
}
|
||||
|
||||
|
||||
@ -215,9 +220,8 @@ public final class ShrikeCTMethod extends ShrikeBTMethod implements IBytecodeMet
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "(" + firstLine + "," + firstCol + "-" + lastLine + "," + lastCol + ")";
|
||||
return fileName + "(" + firstLine + "," + firstCol + "-" + lastLine + "," + lastCol + ")";
|
||||
}
|
||||
|
||||
}
|
||||
/** END Custom change: precise positions */
|
||||
|
||||
@ -237,7 +241,8 @@ public final class ShrikeCTMethod extends ShrikeBTMethod implements IBytecodeMet
|
||||
|
||||
bcInfo.paramPositionMap = new SPos[getNumberOfParameters()];
|
||||
if (param != null) {
|
||||
SPos paramPos = new SPos(param.firstLine, param.lastLine, param.firstCol, param.lastCol);
|
||||
String fileName = ((ShrikeClass)getDeclaringClass()).getSourceFileReader().getSourceFile();
|
||||
SPos paramPos = new SPos(fileName, param.firstLine, param.lastLine, param.firstCol, param.lastCol);
|
||||
for (int i = 0; i < getNumberOfParameters(); i++) {
|
||||
bcInfo.paramPositionMap[i] = paramPos;
|
||||
}
|
||||
@ -255,10 +260,15 @@ public final class ShrikeCTMethod extends ShrikeBTMethod implements IBytecodeMet
|
||||
}
|
||||
|
||||
if (pos != null) {
|
||||
String sourceFile = null;
|
||||
SourceFileReader reader = ((ShrikeClass)getDeclaringClass()).getSourceFileReader();
|
||||
if (reader != null) {
|
||||
sourceFile = reader.getSourceFile();
|
||||
}
|
||||
bcInfo.positionMap = new SPos[pos.length];
|
||||
for (int i = 0; i < pos.length; i++) {
|
||||
Position p = pos[i];
|
||||
bcInfo.positionMap[i] = new SPos(p.firstLine, p.lastLine, p.firstCol, p.lastCol);
|
||||
bcInfo.positionMap[i] = new SPos(sourceFile, p.firstLine, p.lastLine, p.firstCol, p.lastCol);
|
||||
}
|
||||
}
|
||||
/** END Custom change: : precise bytecode positions */
|
||||
@ -329,61 +339,38 @@ public final class ShrikeCTMethod extends ShrikeBTMethod implements IBytecodeMet
|
||||
return ((ShrikeClass) getDeclaringClass()).getReader();
|
||||
}
|
||||
|
||||
private <T> T getReader(String attrName, GetReader<T> reader) {
|
||||
ClassReader.AttrIterator iter = new AttrIterator();
|
||||
getClassReader().initMethodAttributeIterator(shrikeMethodIndex, iter);
|
||||
|
||||
return ((ShrikeClass)getDeclaringClass()).getReader(iter, attrName, reader);
|
||||
}
|
||||
|
||||
private CodeReader getCodeReader() {
|
||||
ClassReader.AttrIterator iter = new AttrIterator();
|
||||
getClassReader().initMethodAttributeIterator(shrikeMethodIndex, iter);
|
||||
|
||||
// search for the code attribute
|
||||
CodeReader code = null;
|
||||
try {
|
||||
for (; iter.isValid(); iter.advance()) {
|
||||
if (iter.getName().equals("Code")) {
|
||||
code = new CodeReader(iter);
|
||||
break;
|
||||
}
|
||||
return getReader("Code", new GetReader<CodeReader>() {
|
||||
@Override
|
||||
public CodeReader getReader(AttrIterator iter) throws InvalidClassFileException {
|
||||
return new CodeReader(iter);
|
||||
}
|
||||
} catch (InvalidClassFileException e) {
|
||||
Assertions.UNREACHABLE();
|
||||
}
|
||||
return code;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
private ExceptionsReader getExceptionReader() {
|
||||
ClassReader.AttrIterator iter = new AttrIterator();
|
||||
getClassReader().initMethodAttributeIterator(shrikeMethodIndex, iter);
|
||||
|
||||
// search for the desired attribute
|
||||
ExceptionsReader result = null;
|
||||
try {
|
||||
for (; iter.isValid(); iter.advance()) {
|
||||
if (iter.getName().equals("Exceptions")) {
|
||||
result = new ExceptionsReader(iter);
|
||||
break;
|
||||
}
|
||||
return getReader("Exceptions", new GetReader<ExceptionsReader>() {
|
||||
@Override
|
||||
public ExceptionsReader getReader(AttrIterator iter) throws InvalidClassFileException {
|
||||
return new ExceptionsReader(iter);
|
||||
}
|
||||
} catch (InvalidClassFileException e) {
|
||||
Assertions.UNREACHABLE();
|
||||
}
|
||||
return result;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
private SignatureReader getSignatureReader() {
|
||||
ClassReader.AttrIterator iter = new AttrIterator();
|
||||
getClassReader().initMethodAttributeIterator(shrikeMethodIndex, iter);
|
||||
|
||||
// search for the desired attribute
|
||||
SignatureReader result = null;
|
||||
try {
|
||||
for (; iter.isValid(); iter.advance()) {
|
||||
if (iter.getName().equals("Signature")) {
|
||||
result = new SignatureReader(iter);
|
||||
break;
|
||||
}
|
||||
return getReader("Signature", new GetReader<SignatureReader>() {
|
||||
@Override
|
||||
public SignatureReader getReader(AttrIterator iter) throws InvalidClassFileException {
|
||||
return new SignatureReader(iter);
|
||||
}
|
||||
} catch (InvalidClassFileException e) {
|
||||
Assertions.UNREACHABLE();
|
||||
}
|
||||
return result;
|
||||
});
|
||||
}
|
||||
|
||||
private AnnotationsReader getAnnotationsReader(AnnotationType type) {
|
||||
|
||||
@ -25,6 +25,7 @@ import com.ibm.wala.shrikeCT.ClassReader.AttrIterator;
|
||||
import com.ibm.wala.shrikeCT.InnerClassesReader;
|
||||
import com.ibm.wala.shrikeCT.InvalidClassFileException;
|
||||
import com.ibm.wala.shrikeCT.SignatureReader;
|
||||
import com.ibm.wala.shrikeCT.SourceFileReader;
|
||||
import com.ibm.wala.types.TypeName;
|
||||
import com.ibm.wala.types.TypeReference;
|
||||
import com.ibm.wala.types.annotations.Annotation;
|
||||
@ -269,6 +270,24 @@ public final class ShrikeClass extends JVMClass<IClassLoader> {
|
||||
: AnnotationType.RuntimeVisibleAnnotations, attrs);
|
||||
}
|
||||
|
||||
interface GetReader<T> {
|
||||
T getReader(ClassReader.AttrIterator iter) throws InvalidClassFileException;
|
||||
}
|
||||
|
||||
<T> T getReader(ClassReader.AttrIterator iter, String attrName, GetReader<T> reader) {
|
||||
// search for the attribute
|
||||
try {
|
||||
for (; iter.isValid(); iter.advance()) {
|
||||
if (iter.getName().equals(attrName)) {
|
||||
return reader.getReader(iter);
|
||||
}
|
||||
}
|
||||
} catch (InvalidClassFileException e) {
|
||||
Assertions.UNREACHABLE();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private InnerClassesReader getInnerClassesReader() throws InvalidClassFileException {
|
||||
ClassReader r = reader.get();
|
||||
ClassReader.AttrIterator attrs = new ClassReader.AttrIterator();
|
||||
@ -289,6 +308,18 @@ public final class ShrikeClass extends JVMClass<IClassLoader> {
|
||||
return result;
|
||||
}
|
||||
|
||||
SourceFileReader getSourceFileReader() {
|
||||
ClassReader.AttrIterator attrs = new ClassReader.AttrIterator();
|
||||
getReader().initClassAttributeIterator(attrs);
|
||||
|
||||
return getReader(attrs, "SourceFile", new GetReader<SourceFileReader>() {
|
||||
@Override
|
||||
public SourceFileReader getReader(AttrIterator iter) throws InvalidClassFileException {
|
||||
return new SourceFileReader(iter);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private AnnotationsReader getFieldAnnotationsReader(boolean runtimeInvisible, int fieldIndex) throws InvalidClassFileException {
|
||||
ClassReader.AttrIterator iter = new AttrIterator();
|
||||
reader.get().initFieldAttributeIterator(fieldIndex, iter);
|
||||
|
||||
@ -29,4 +29,8 @@ public final class SourceFileReader extends AttributeReader {
|
||||
public int getSourceFileCPIndex() {
|
||||
return cr.getUShort(attr + 6);
|
||||
}
|
||||
|
||||
public String getSourceFile() throws IllegalArgumentException, InvalidClassFileException {
|
||||
return cr.getCP().getCPUtf8(getSourceFileCPIndex());
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user