Skip to content

Commit dc39308

Browse files
authored
Escape special characters in Property String literals (#4564)
1 parent 9015179 commit dc39308

File tree

5 files changed

+29
-4
lines changed

5 files changed

+29
-4
lines changed

core/src/main/scala/chisel3/properties/Property.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ private[chisel3] object PropertyType extends LowPriorityPropertyTypeInstances {
147147
}
148148

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

152152
implicit val boolPropertyTypeInstance: SimplePropertyType[Boolean] =
153153
makeSimple[Boolean](fir.BooleanPropertyType, fir.BooleanPropertyLiteral(_))

firrtl/src/main/scala/firrtl/ir/IR.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ case class DoublePropertyLiteral(value: Double) extends Expression with UseSeria
241241
val width = UnknownWidth
242242
}
243243

244-
case class StringPropertyLiteral(value: String) extends Expression with UseSerializer {
244+
case class StringPropertyLiteral(value: StringLit) extends Expression with UseSerializer {
245245
def tpe = StringPropertyType
246246
val width = UnknownWidth
247247
}

firrtl/src/main/scala/firrtl/ir/Serializer.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ object Serializer {
113113
case DoublePropertyLiteral(value) =>
114114
b ++= "Double("; b ++= value.toString(); b ++= ")"
115115
case StringPropertyLiteral(value) =>
116-
b ++= "String(\""; b ++= value; b ++= "\")"
116+
b ++= "String("; b ++= value.escape; b ++= ")"
117117
case BooleanPropertyLiteral(value) =>
118118
b ++= s"Bool(${value})"
119119
case PathPropertyLiteral(value) =>

panamaconverter/src/PanamaCIRCTConverter.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ class PanamaCIRCTConverter(val circt: PanamaCIRCT, fos: Option[FirtoolOptions],
576576
case fir.DoublePropertyLiteral(value) =>
577577
val attrs = Seq(("value", circt.mlirFloatAttrDoubleGet(circt.mlirF64TypeGet(), value)))
578578
("double", attrs, Seq.empty)
579-
case fir.StringPropertyLiteral(value) =>
579+
case fir.StringPropertyLiteral(fir.StringLit(value)) =>
580580
val attrs = Seq(("value", circt.mlirStringAttrGet(value)))
581581
("string", attrs, Seq.empty)
582582
case fir.BooleanPropertyLiteral(value) =>

src/test/scala/chiselTests/properties/PropertySpec.scala

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,31 @@ class PropertySpec extends ChiselFlatSpec with MatchesAndOmits {
132132
)()
133133
}
134134

135+
it should "escape special characters in Property String literals" in {
136+
val input = "foo\"\n\t\\bar"
137+
val expected = """foo\"\n\t\\bar"""
138+
val chirrtl = ChiselStage.emitCHIRRTL(new RawModule {
139+
val propOut = IO(Output(Property[String]()))
140+
propOut := Property(input)
141+
})
142+
143+
matchesAndOmits(chirrtl)(
144+
s"""propassign propOut, String("$expected")"""
145+
)()
146+
}
147+
148+
it should "not escape characters that do not need escaping in Property String literals" in {
149+
val input = "foo!@#$%^&*()_+bar"
150+
val chirrtl = ChiselStage.emitCHIRRTL(new RawModule {
151+
val propOut = IO(Output(Property[String]()))
152+
propOut := Property(input)
153+
})
154+
155+
matchesAndOmits(chirrtl)(
156+
s"""propassign propOut, String("$input")"""
157+
)()
158+
}
159+
135160
it should "support Boolean as a Property type" in {
136161
val chirrtl = ChiselStage.emitCHIRRTL(new RawModule {
137162
val boolProp = IO(Input(Property[Boolean]()))

0 commit comments

Comments
 (0)