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

Escape special characters in Property String literals #4564

Merged
merged 1 commit into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all 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/properties/Property.scala
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ private[chisel3] object PropertyType extends LowPriorityPropertyTypeInstances {
}

implicit val stringPropertyTypeInstance: SimplePropertyType[String] =
makeSimple[String](fir.StringPropertyType, fir.StringPropertyLiteral(_))
makeSimple[String](fir.StringPropertyType, s => fir.StringPropertyLiteral(fir.StringLit(s)))

implicit val boolPropertyTypeInstance: SimplePropertyType[Boolean] =
makeSimple[Boolean](fir.BooleanPropertyType, fir.BooleanPropertyLiteral(_))
Expand Down
2 changes: 1 addition & 1 deletion firrtl/src/main/scala/firrtl/ir/IR.scala
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ case class DoublePropertyLiteral(value: Double) extends Expression with UseSeria
val width = UnknownWidth
}

case class StringPropertyLiteral(value: String) extends Expression with UseSerializer {
case class StringPropertyLiteral(value: StringLit) extends Expression with UseSerializer {
def tpe = StringPropertyType
val width = UnknownWidth
}
Expand Down
2 changes: 1 addition & 1 deletion firrtl/src/main/scala/firrtl/ir/Serializer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ object Serializer {
case DoublePropertyLiteral(value) =>
b ++= "Double("; b ++= value.toString(); b ++= ")"
case StringPropertyLiteral(value) =>
b ++= "String(\""; b ++= value; b ++= "\")"
b ++= "String("; b ++= value.escape; b ++= ")"
case BooleanPropertyLiteral(value) =>
b ++= s"Bool(${value})"
case PathPropertyLiteral(value) =>
Expand Down
2 changes: 1 addition & 1 deletion panamaconverter/src/PanamaCIRCTConverter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ class PanamaCIRCTConverter(val circt: PanamaCIRCT, fos: Option[FirtoolOptions],
case fir.DoublePropertyLiteral(value) =>
val attrs = Seq(("value", circt.mlirFloatAttrDoubleGet(circt.mlirF64TypeGet(), value)))
("double", attrs, Seq.empty)
case fir.StringPropertyLiteral(value) =>
case fir.StringPropertyLiteral(fir.StringLit(value)) =>
val attrs = Seq(("value", circt.mlirStringAttrGet(value)))
("string", attrs, Seq.empty)
case fir.BooleanPropertyLiteral(value) =>
Expand Down
25 changes: 25 additions & 0 deletions src/test/scala/chiselTests/properties/PropertySpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,31 @@ class PropertySpec extends ChiselFlatSpec with MatchesAndOmits {
)()
}

it should "escape special characters in Property String literals" in {
val input = "foo\"\n\t\\bar"
val expected = """foo\"\n\t\\bar"""
val chirrtl = ChiselStage.emitCHIRRTL(new RawModule {
val propOut = IO(Output(Property[String]()))
propOut := Property(input)
})

matchesAndOmits(chirrtl)(
s"""propassign propOut, String("$expected")"""
)()
}

it should "not escape characters that do not need escaping in Property String literals" in {
val input = "foo!@#$%^&*()_+bar"
val chirrtl = ChiselStage.emitCHIRRTL(new RawModule {
val propOut = IO(Output(Property[String]()))
propOut := Property(input)
})

matchesAndOmits(chirrtl)(
s"""propassign propOut, String("$input")"""
)()
}

it should "support Boolean as a Property type" in {
val chirrtl = ChiselStage.emitCHIRRTL(new RawModule {
val boolProp = IO(Input(Property[Boolean]()))
Expand Down