aboutsummaryrefslogtreecommitdiff
path: root/src/test/scala/firrtlTests/annotationTests/UnrecognizedAnnotationSpec.scala
blob: d2df77037f9b5e5cee1340e1b5cf8a5edadfc0fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// SPDX-License-Identifier: Apache-2.0

package firrtlTests.annotationTests

import firrtl.FileUtils
import firrtl.annotations._
import firrtl.passes.memlib.ReplSeqMemAnnotation
import firrtl.stage.FirrtlMain
import firrtl.testutils.FirrtlFlatSpec
import firrtl.transforms.BlackBoxInlineAnno
import logger.Logger
import logger.Logger.OutputCaptor

import java.io.{File, PrintWriter}

class UnrecognizedAnnotationSpec extends FirrtlFlatSpec {
  behavior.of("unrecognized annotations can be carried through serialization and deserialization")

  it should "preserve unknown annotations when allowed" in {
    val annotations =
      JsonProtocol.deserialize(UnrecognizedAnnotationTextGenerator.jsonText(includeAllowUnrecognizedAnnotations = true))

    annotations.exists(_.isInstanceOf[BlackBoxInlineAnno]) should be(true)
    annotations.count(_.isInstanceOf[UnrecognizedAnnotation]) should be(2)
    annotations.exists(_.isInstanceOf[ReplSeqMemAnnotation]) should be(false)

    val jsonOutputText = JsonProtocol.serialize(annotations)

    jsonOutputText should include(""""class":"firrtl.transforms.BlackBoxInlineAnno"""")
    jsonOutputText should include(""""class":"freechips.rocketchip.util.RegFieldDescMappingAnnotation"""")
    jsonOutputText should include(""""class":"freechips.rocketchip.util.SRAMAnnotation"""")
  }

  it should "throw an error when unknown annotations are present but AllowUnrecognizedAnnotation is not" in {

    // Default log level is error, which the JSON parsing uses here
    Logger.makeScope(Seq()) {
      val captor = new OutputCaptor
      Logger.setOutput(captor.printStream)

      val parsingError = intercept[UnrecogizedAnnotationsException] {
        JsonProtocol.deserialize(
          UnrecognizedAnnotationTextGenerator.jsonText(includeAllowUnrecognizedAnnotations = false)
        )
      }
      parsingError.getMessage should include("RegFieldDescMappingAnnotation")

      val output = captor.getOutputAsString
      output should include("Annotation parsing found unrecognized annotations")
      output should include(
        "This error can be ignored with an AllowUnrecognizedAnnotationsAnnotation or command line flag --allow-unrecognized-annotations"
      )
      output should include(
        "freechips.rocketchip.util.RegFieldDescMappingAnnotation"
      )
      output should include(
        "freechips.rocketchip.util.SRAMAnnotation"
      )
    }
  }

  // Following test will operate on an annotation JSON file with two unrecognized annotations in it
  //

  it should "fail by default" in {
    val fileNames = setupFiles(addAllowUnrecognizedFlag = false, addAllowUnrecognizedAnno = false)
    val args = makeCommandLineArgs(fileNames)
    val e = intercept[InvalidAnnotationFileException] {
      FirrtlMain.main(args)
    }

    e.getMessage should include(fileNames.inputAnnotations)
    e.getCause.getMessage should include("freechips.rocketchip.util.RegFieldDescMappingAnnotation")
    e.getCause.getMessage should include("freechips.rocketchip.util.SRAMAnnotation")
  }

  it should "succeed when the AllowUnrecognized flag is passed on command line" in {
    val fileNames = setupFiles(addAllowUnrecognizedFlag = false, addAllowUnrecognizedAnno = true)
    shouldSucceed(fileNames)
  }

  it should "succeed when the AllowUnrecognizedAnnotation is in the annotation file" in {
    val fileNames = setupFiles(addAllowUnrecognizedFlag = true, addAllowUnrecognizedAnno = false)
    shouldSucceed(fileNames)
  }

  it should "succeed when both forms of the override are specified" in {
    val fileNames = setupFiles(addAllowUnrecognizedFlag = true, addAllowUnrecognizedAnno = true)
    shouldSucceed(fileNames)
  }

  def shouldSucceed(fileNames: TestFileNames): Unit = {
    val args = makeCommandLineArgs(fileNames)
    FirrtlMain.main(args)

    val outputAnnotationText = FileUtils.getText(fileNames.outputAnnotationsFull)
    outputAnnotationText should include("freechips.rocketchip.util.RegFieldDescMappingAnnotation")
    outputAnnotationText should include("freechips.rocketchip.util.SRAMAnnotation")
  }

  case class TestFileNames(
    allowUnrecognized:     Boolean,
    inputAnnotations:      String,
    outputAnnotations:     String,
    outputAnnotationsFull: String,
    firrtlSource:          String,
    firrtlOutput:          String)

  def setupFiles(addAllowUnrecognizedFlag: Boolean, addAllowUnrecognizedAnno: Boolean): TestFileNames = {
    val dirName = (addAllowUnrecognizedFlag, addAllowUnrecognizedAnno) match {
      case (false, false) => s"test_run_dir/unrecognized_annotation_fail"
      case (true, false)  => s"test_run_dir/unrecognized_annotation_flag"
      case (false, true)  => s"test_run_dir/unrecognized_annotation_anno"
      case (true, true)   => s"test_run_dir/unrecognized_annotation_flag_and_anno"
    }
    val dir = new File(dirName)
    dir.mkdirs()

    val fileNames = TestFileNames(
      allowUnrecognized = addAllowUnrecognizedFlag,
      inputAnnotations = s"$dirName/input_annotations.json",
      outputAnnotations = s"$dirName/output_annotations",
      outputAnnotationsFull = s"$dirName/output_annotations.anno.json",
      firrtlSource = s"$dirName/trivial.fir",
      firrtlOutput = s"$dirName/trivial_out"
    )

    def writeText(fileName: String, text: String): Unit = {
      val writer = new PrintWriter(fileName)
      writer.write(text)
      writer.close()
    }

    writeText(
      fileNames.inputAnnotations,
      UnrecognizedAnnotationTextGenerator.jsonText(includeAllowUnrecognizedAnnotations = addAllowUnrecognizedAnno)
    )
    writeText(
      fileNames.firrtlSource,
      s"""
         |circuit Trivial :
         |  module Trivial :
         |    input clock : Clock
         |    input reset : UInt<1>
         |""".stripMargin
    )
    fileNames
  }

  /* construct an array of command line strings, based on file names and  */
  def makeCommandLineArgs(fileNames: TestFileNames): Array[String] = {

    (if (fileNames.allowUnrecognized) {
       Array("--allow-unrecognized-annotations")
     } else {
       Array.empty[String]
     }) ++
      Array(
        "--annotation-file",
        fileNames.inputAnnotations,
        "-i",
        fileNames.firrtlSource,
        "-X",
        "high",
        "-o",
        fileNames.firrtlOutput,
        "--output-annotation-file",
        fileNames.outputAnnotations
      )
  }
}

object UnrecognizedAnnotationTextGenerator {

  def jsonText(includeAllowUnrecognizedAnnotations: Boolean): String = {
    val serializedAllowUnrecognized = if (includeAllowUnrecognizedAnnotations) {
      """
        |  {
        |    "class": "firrtl.stage.AllowUnrecognizedAnnotations$"
        |  },""".stripMargin
    } else {
      ""
    }

    s"""|[$serializedAllowUnrecognized
        |  {
        |    "class": "firrtl.transforms.BlackBoxInlineAnno",
        |    "target": "TestHarness.plusarg_reader_27",
        |    "name": "plusarg_reader.v",
        |    "text": "License text"
        |  },
        |  {
        |    "class": "freechips.rocketchip.util.RegFieldDescMappingAnnotation",
        |  },
        |  {
        |    "class": "freechips.rocketchip.util.SRAMAnnotation",
        |  }
        |]
        |""".stripMargin
  }
}