Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SourceInfo: simplify the common case for makeMessage, print it differently #4249

Merged
merged 6 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/src/main/scala/chisel3/Data.scala
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ abstract class Data extends HasId with NamedComponent with SourceInfoDoc {
visibleFromWhen match {
case Some(sourceInfo) =>
throwException(
s"operand '$this' has escaped the scope of the when (${sourceInfo.makeMessage(x => x)}) in which it was constructed."
s"operand '$this' has escaped the scope of the when (${sourceInfo.makeMessage()}) in which it was constructed."
)
case None => ()
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala/chisel3/Mem.scala
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ sealed abstract class MemBase[T <: Data](val t: T, val length: BigInt, sourceInf

protected def clockWarning(sourceInfo: Option[SourceInfo], dir: MemPortDirection): Unit = {
// Turn into pretty String if possible, if not, Builder.deprecated will find one via stack trace
val infoStr = sourceInfo.collect { case s => s.makeMessage(x => x) }
val infoStr = sourceInfo.collect { case s => s.makeMessage() }
Builder.deprecated(
"The clock used to initialize the memory is different than the one used to initialize the port. " +
"If this is intentional, please pass the clock explicitly when creating the port. This behavior will be an error in 3.6.0",
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/scala/chisel3/Module.scala
Original file line number Diff line number Diff line change
Expand Up @@ -612,14 +612,14 @@ package experimental {
if (_namespace.contains(name)) {
Builder.error(
s"""Unable to name port $port to "$name" in $this,""" +
s" name is already taken by another port! ${source.makeMessage(x => x)}"
s" name is already taken by another port! ${source.makeMessage()}"
)(UnlocatableSourceInfo)
}
port.setRef(ModuleIO(this, _namespace.name(name)))
case None =>
Builder.error(
s"Unable to name port $port in $this, " +
s"try making it a public field of the Module ${source.makeMessage(x => x)}"
s"try making it a public field of the Module ${source.makeMessage()}"
)(UnlocatableSourceInfo)
port.setRef(ModuleIO(this, "<UNNAMED>"))
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala/chisel3/RawModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ abstract class RawModule extends BaseModule {
if (port._computeName(None).isEmpty) {
Builder.error(
s"Unable to name port $port in $this, " +
s"try making it a public field of the Module ${source.makeMessage(x => x)}"
s"try making it a public field of the Module ${source.makeMessage()}"
)(UnlocatableSourceInfo)
}
}
Expand Down
10 changes: 7 additions & 3 deletions core/src/main/scala/chisel3/experimental/SourceInfo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ sealed trait SourceInfo {
*
* Make a useful message if SourceInfo is available, nothing otherwise
*/
def makeMessage(f: String => String): String
def makeMessage(f: String => String = x => x): String

/** The filename for the originating source file, if known */
def filenameOption: Option[String]
}

sealed trait NoSourceInfo extends SourceInfo {
def makeMessage(f: String => String): String = ""
def makeMessage(f: String => String = x => x): String = ""
def filenameOption: Option[String] = None
}

Expand All @@ -51,9 +51,13 @@ case object DeprecatedSourceInfo extends NoSourceInfo
* @note A column == 0 indicates no column
*/
case class SourceLine(filename: String, line: Int, col: Int) extends SourceInfo {
def makeMessage(f: String => String): String = f(s"@[${this.serialize}]")
def makeMessage(f: String => String = x => x): String = f(s"@[${this.prettyPrint}]")
def filenameOption: Option[String] = Some(filename)

def prettyPrint: String = {
mwachs5 marked this conversation as resolved.
Show resolved Hide resolved
if (col == 0) s"$filename:$line" else s"$filename:$line:$col"
}

/** Convert to String for FIRRTL emission */
def serialize: String = {
if (col == 0) s"$filename $line" else s"$filename $line:$col"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ sealed class DataView[T: DataProduct, V <: Data] private[chisel3] (
def total: Boolean = _total

override def toString: String = {
val base = sourceInfo.makeMessage(x => x)
val base = sourceInfo.makeMessage()
val loc = if (base.nonEmpty) base else "@unknown"
val name = if (total) "DataView" else "PartialDataView"
s"$name(defined $loc)"
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/scala/chisel3/internal/MonoConnect.scala
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ private[chisel3] object MonoConnect {
)
def SourceEscapedWhenScopeException(source: Data, whenInfo: SourceInfo) =
MonoConnectException(
s"Source ${formatName(source)} has escaped the scope of the when (${whenInfo.makeMessage(x => x)}) in which it was constructed."
s"Source ${formatName(source)} has escaped the scope of the when (${whenInfo.makeMessage()}) in which it was constructed."
)
def SinkEscapedWhenScopeException(sink: Data, whenInfo: SourceInfo) =
MonoConnectException(
s"Sink ${formatName(sink)} has escaped the scope of the when (${whenInfo.makeMessage(x => x)}) in which it was constructed."
s"Sink ${formatName(sink)} has escaped the scope of the when (${whenInfo.makeMessage()}) in which it was constructed."
)
def UnknownRelationException =
MonoConnectException("Sink or source unavailable to current module.")
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/chisel3/simulator/PeekPokeAPI.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ trait PeekPokeAPI {
sourceInfo: SourceInfo,
extraContext: Seq[String]
): FailedExpectationException[T] = {
val fullMessage = s"$message ${sourceInfo.makeMessage(x => x)}" +
val fullMessage = s"$message ${sourceInfo.makeMessage()}" +
(if (extraContext.nonEmpty) s"\n${extraContext.mkString("\n")}" else "")
new FailedExpectationException(observed, expected, fullMessage)
}
Expand Down
11 changes: 10 additions & 1 deletion src/test/scala/chiselTests/SourceLocatorSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package chiselTests

import circt.stage.ChiselStage.emitCHIRRTL
import chisel3._
import chisel3.experimental.{BaseModule, ExtModule, SourceLine}
import chisel3.experimental.{BaseModule, ExtModule, SourceInfo, SourceLine}
import chisel3.experimental.hierarchy.Definition
import firrtl.ir.FileInfo

Expand Down Expand Up @@ -109,4 +109,13 @@ class SourceLocatorSpec extends ChiselFunSpec with Utils {
chirrtl should include(s"module RawModuleChild : @[$thisFile 14:9]")
}
}

describe("(3) SourceLocator.makeMessage()") {
it("(3.a) Should have click-to-source functionality") {
val locator = SourceInfo.materialize
// This click-to-source works in VSCode terminal, uncomment to manually test
// println(s"Try clicking to this source locator! ${locator.makeMessage()}")
locator.makeMessage() should include(s"$thisFile:115:32")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ class InstantiateSpec extends ChiselFunSpec with Utils {
val chirrtl = convert(new Top {
val inst = Instantiate(new OneArg(3))
}).serialize
chirrtl should include(s"inst inst of OneArg ${info.makeMessage(x => x)}")
chirrtl should include(s"inst inst of OneArg ${info.makeMessage()}")
}

it("should support BlackBoxes") {
Expand Down
Loading