forked from enso-org/enso
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Locations Filter for Instrumented Locations (enso-org#1564)
PR adds the location filter that excludes lambdas and local functions from the instrumentation.
- Loading branch information
Showing
10 changed files
with
1,088 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
.../main/java/org/enso/interpreter/service/error/ModuleNotFoundForExpressionIdException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package org.enso.interpreter.service.error; | ||
|
||
import java.util.UUID; | ||
|
||
/** Thrown when a module containing the given expression id can not be found. */ | ||
public class ModuleNotFoundForExpressionIdException extends ModuleNotFoundException { | ||
|
||
/** | ||
* Create new instance of this error. | ||
* | ||
* @param expressionId the expression identifier. | ||
*/ | ||
public ModuleNotFoundForExpressionIdException(UUID expressionId) { | ||
super("containing expression " + expressionId); | ||
} | ||
} |
115 changes: 115 additions & 0 deletions
115
engine/runtime/src/main/scala/org/enso/interpreter/instrument/execution/LocationFilter.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package org.enso.interpreter.instrument.execution | ||
|
||
import com.oracle.truffle.api.source.{Source, SourceSection} | ||
import org.enso.compiler.core.IR | ||
import org.enso.syntax.text.Location | ||
|
||
import scala.collection.mutable | ||
|
||
/** Contains instrumentable source locations. | ||
* | ||
* @param sections the list of source sections to instrument | ||
*/ | ||
case class LocationFilter(sections: Set[SourceSection]) { | ||
|
||
/** Get the list of source sections to instrument. */ | ||
def getSections: Array[SourceSection] = | ||
sections.toArray | ||
} | ||
|
||
object LocationFilter { | ||
|
||
private type Builder = mutable.Set[Location] | ||
|
||
/** Create the location filter. | ||
* | ||
* @param ir the module ir | ||
* @param span the source section to instrument | ||
* @return new location filter | ||
*/ | ||
def create(ir: IR.Module, span: SourceSection): LocationFilter = { | ||
val location = Location(span.getCharIndex, span.getCharEndIndex) | ||
val targetNode = LocationResolver.findByLocation(ir, location).getOrElse(ir) | ||
val locations = createLocations(targetNode) | ||
|
||
LocationFilter(locations.map(toSection(span.getSource, _))) | ||
} | ||
|
||
private def createLocations(ir: IR): Set[Location] = { | ||
val builder = mutable.Set.empty[Location] | ||
analyzeEnterable(ir, builder) | ||
builder.result().toSet | ||
} | ||
|
||
private def analyzeEnterable(ir: IR, builder: Builder): Unit = | ||
ir match { | ||
case module: IR.Module => | ||
module.bindings.foreach(analyzeModuleDefinition(_, builder)) | ||
case definition: IR.Module.Scope.Definition => | ||
analyzeModuleDefinition(definition, builder) | ||
case function: IR.Function => | ||
analyzeBody(function.body, builder) | ||
case expression: IR.Expression => | ||
analyzeExpression(expression, builder) | ||
case _ => | ||
} | ||
|
||
private def analyzeModuleDefinition( | ||
binding: IR.Module.Scope.Definition, | ||
builder: Builder | ||
): Unit = | ||
binding match { | ||
case method: IR.Module.Scope.Definition.Method.Explicit => | ||
analyzeBody(method.body, builder) | ||
case _ => | ||
} | ||
|
||
@scala.annotation.tailrec | ||
private def analyzeBody(ir: IR.Expression, builder: Builder): Unit = | ||
ir match { | ||
case function: IR.Function => | ||
analyzeBody(function.body, builder) | ||
case expression => | ||
analyzeExpression(expression, builder) | ||
} | ||
|
||
private def analyzeExpression(expression: IR, builder: Builder): Unit = { | ||
@scala.annotation.tailrec | ||
def go(queue: mutable.Queue[IR]): Unit = { | ||
if (queue.nonEmpty) { | ||
val element = queue.dequeue() | ||
val location = getLocation(element) | ||
|
||
element match { | ||
case IR.Expression.Binding(_, function: IR.Function, _, _, _) | ||
if isSynthetic(function) => | ||
builder ++= getLocation(function) | ||
case IR.Expression.Binding(_, block: IR.Expression.Block, _, _, _) => | ||
builder ++= getLocation(block) | ||
case function: IR.Function => | ||
if (!isSynthetic(function)) { | ||
builder ++= location | ||
} | ||
case _ => | ||
builder ++= location | ||
queue ++= element.children | ||
} | ||
|
||
go(queue) | ||
} | ||
} | ||
|
||
go(mutable.Queue(expression)) | ||
} | ||
|
||
private def isSynthetic(function: IR.Function): Boolean = { | ||
val irLocation = getLocation(function) | ||
irLocation.isDefined && irLocation == getLocation(function.body) | ||
} | ||
|
||
private def toSection(source: Source, location: Location): SourceSection = | ||
source.createSection(location.start, location.length) | ||
|
||
private def getLocation(ir: IR): Option[Location] = | ||
ir.location.map(_.location) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.