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

Make code gen support EmptyGram #1121

Merged
merged 1 commit into from
Nov 28, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,9 @@ object DaffodilCCodeGenerator
*/
def generateCode(gram: Gram, cgState: CodeGeneratorState): Unit = {
gram match {
// Skip empty grams
case g: Gram if g.isEmpty => noop(g)
// Handle non-empty grams
case g: AlignmentFill => alignmentFillGenerateCode(g, cgState)
case g: AssertBooleanPrim => noop(g)
case g: BinaryBoolean => binaryBooleanGenerateCode(g.e, cgState)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,19 @@

package org.apache.daffodil.codegen.c

import org.apache.daffodil.core.compiler.Compiler
import org.apache.daffodil.lib.api.TDMLImplementation
import org.apache.daffodil.lib.util.SchemaUtils

import org.junit.Test

/**
* Runs DaffodilCExamplesGenerator in a test since "sbt coverage compile" doesn't
* capture call of genCExamples
*/
class TestDaffodilCExamplesGenerator {
// Test added for code coverage and debugging

// Calls DaffodilCExamplesGenerator for code coverage and debugging
@Test def test_DaffodilCExamplesGenerator_main(): Unit = {
// Generate the C examples in a safe place (target/examples)
val rootDir = if (os.exists(os.pwd / "src")) os.pwd / os.up else os.pwd
Expand All @@ -36,4 +41,39 @@ class TestDaffodilCExamplesGenerator {
val generatedCode = examplesDir / "variablelen" / "generated_code.c"
assert(os.exists(generatedCode))
}

// Checks C code can be generated from a schema with an empty grammar object
@Test def test_generateCode(): Unit = {
// Define a schema containing an empty grammar object
val testSchema = SchemaUtils.dfdlTestSchema(
<xs:include schemaLocation="/org/apache/daffodil/xsd/DFDLGeneralFormat.dfdl.xsd"/>,
<dfdl:format representation="binary" ref="GeneralFormat"/>,
<xs:element name="foo">
<xs:complexType>
<xs:choice>
<xs:element name="bar" type="xs:int"/>
<xs:sequence/>
</xs:choice>
</xs:complexType>
</xs:element>,
)

// Compile the schema into a ProcessorFactory
val pf = Compiler().compileNode(testSchema)
assert(!pf.isError, pf.getDiagnostics.map(_.getMessage()).mkString("\n"))

// Get a CodeGenerator from the ProcessorFactory
val cg = pf.forLanguage("c")
assert(!cg.isError, cg.getDiagnostics.map(_.getMessage()).mkString("\n"))

// Generate C code into a temporary directory
val tempDir: os.Path =
os.temp.dir(dir = null, prefix = TDMLImplementation.DaffodilC.toString)
cg.generateCode(tempDir.toString)
os.remove.all(tempDir)

// Check the C code was generated successfully
assert(!cg.isError, cg.getDiagnostics.map(_.getMessage()).mkString("\n"))
}

}