Skip to content

Commit

Permalink
Escape special characters in Property String literals
Browse files Browse the repository at this point in the history
  • Loading branch information
jackkoenig committed Dec 16, 2024
1 parent 9015179 commit ab66a6a
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 4 deletions.
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

0 comments on commit ab66a6a

Please sign in to comment.