From 604d509f01ee40835be123b86b4b998a3265b184 Mon Sep 17 00:00:00 2001 From: John Interrante Date: Thu, 19 Oct 2023 18:10:01 -0400 Subject: [PATCH] Add schema validation code to C generator Modify C generator to compare floating point and integer numbers with enumerations and ranges specified in DFDL schemas. Test validation code with additional simple type root elements in simple.dfdl.xsd schema and additional TDML tests in simple_errors.tdml. Also allow C generator to compare hexBinary elements to values in enumerations since Daffodil does the same thing and C generator already compares hexBinary elements to values in fixed attributes. Allow C generator to ignore dfdl:assert expressions in DFDL schemas and generate code anyway instead of throwing an exception. Allow C generator to get schema version from DFDL schemas and put it into the generated C code to version the generated code as well. Found out Daffodil's default alignment properties (alignment="1" and alignmentUnits="bytes") makes Daffodil append extra fill bits to elements with odd sizes not a multiple of 8 bits. Because compatibility between Daffodil and DaffodilC depends on defining both dfdl:alignment="1" (default) and dfdl:alignmentUnits="bits" (non-default), define a known good binary data format in one place (network/format.dfdl.xsd) and include it in rest of daffodil-codegen-c's test schemas. Finally, make changes requested by PR review. DAFFODIL-2853 BUILD.md: Document how to install iwyu and libcriterion-dev (used only for Daffodil C code generator development and maintenance). README.md: Document how to check formatting of or reformat Daffodil. Also document how to put the Daffodil jars in the maven and ivy caches. c/files/.clang-format: Update to clang-format 14. Format declarations more concisely by removing AlignConsecutiveDeclarations (this is why some reformatted C files lose some extra whitespace). c/files/Makefile: Shorten iwyu's maximum line length from 999 to 111. c/files/**/**.[ch]: Add "auto-maintained by iwyu" comment to headers. c/files/libcli/cli_errors.[ch]: Rename CLI_ZZZ, ERR_ZZZ, and FIELD_ZZZ to CLI__NUM_CODES, ERR__NUM_CODES, and FIELD__NO_ARGS. c/files/libcli/daffodil_getopt.[ch]: Merge daffodil_parse_cli and daffodil_unparse_cli structs and objects into daffodil_pu_cli and daffodil_pu. Skip and ignore -r and -s options so one can run "c/daffodil parse" with the same options as "daffodil parse -r root -s schema -o outfile infile" without having to remove these options. c/files/libcli/daffodil_main.c: Merge daffodil_parse_cli and daffodil_unparse_cli structs and objects into daffodil_pu_cli and daffodil_pu. Initialize ParserOrUnparserState fields as well as PState/UState fields. Make sure any diagnostics will fail unparse as well as parse if validate mode is on. c/files/libruntime/errors.[ch]: Add ERR_ENUM_MATCH and ERR_OUTSIDE_RANGE error messages to diagnose elements not matching any of their enumerations or having values outside their allowed ranges. Rename ERR_ZZZ and FIELD_ZZZ to ERR__NUM_CODES and FIELD__NO_ARGS. Rename ERR_ENUM_MATCH, ERR_FIXED_VALUE, and ERR_OUTSIDE_RANGE to ERR_RESTR_ENUM, ERR_RESTR_FIXED, and ERR_RESTR_RANGE. Remove `daffodil_program_version` since we use `daffodil_version` in daffodil_version.h instead. c/files/libruntime/infoset.h: Move common PState/UState fields to ParseOrUnparseState to allow parse and unparse functions to call common validate functions. c/files/libruntime/parsers.[ch]: Use ParserOrUnparserState fields as well as PState/UState fields. Replace parse_check_bounds and parse_validate_fixed functions with validate_array_bounds and validate_fixed_attribute functions in validators.[ch]. Rename parse_align and parse_fill_bits to parse_align_to and parse_alignment_bits. c/files/libruntime/unparsers.[ch]: Use ParserOrUnparserState fields as well as UState fields. Replace unparse_check_bounds and unparse_validate_fixed functions with validate_array_bounds and validate_fixed_attribute functions in validators.[ch]. Rename unparse_align and unparse_fill_bits to unparse_align_to and unparse_alignment_bits. c/files/libruntime/validators.[ch]: Move common validation functions here from parsers.[ch] and unparsers.[ch]. Add new float, hexBinary, int, and universal validation functions to check that elements match their allowed enumerations (requires passing array of floating point numbers, hexBinary structs, or integer numbers) and fit within their allowed ranges. Validation functions create diagnostics instead of errors; the CLI or a caller is responsible for printing the diagnostics and exiting with the appropriate status code. c/files/tests/bits.c: Use ParserOrUnparserState fields as well as PState/UState fields. c/files/tests/extras.c: Regenerate iwyu comments. c/DaffodilCCodeGenerator.scala: Accept but ignore dfdl:assert statements (for now). c/DaffodilCExamplesGenerator.scala: Generate code from simple's all-in-one root element in order to show generated code for all simple types, enums, and ranges. c/generators/AlignmentFillCodeGenerator.scala: Rename parse_align and unparse_align to parse_align_to and unparse_align_to. Use ParserOrUnparserState fields as well as PState/UState fields. c/generators/BinaryBooleanCodeGenerator.scala: Use ParserOrUnparserState fields as well as PState/UState fields. c/generators/BinaryValueCodeGenerator.scala: Generate C code to validate enumerations and ranges of primitive elements. Get raw enumeration values into a Seq[String]. Avoid unsigned >= 0 comparisons to prevent gcc warnings. Use ParserOrUnparserState fields as well as PState/UState fields. Call correct function to validate enums depending on element's primType. Generate extra C initialization code for hexBinary enumerations to define arrays of hexBinary structs and pass them to validate_hexbinary_enumeration. c/generators/CodeGeneratorState.scala: Remove unnecessary immutable (built-in Set is immutable). Use ParserOrUnparserState fields as well as PState/UState fields. Rename parse_fill_bits and unparse_fill_bits to parse_alignment_bits and unparse_alignment_bits. Get actual schema version from Daffodil and assign its value to `schema_version` in generated_code.c. Declare `schema_version` in generated_code.h. Call validate_array_bounds instead of parse/unparse_check_bounds. Make cStructFieldAccess work correctly for DFDL expressions like "/rr:ReqstReply/..." used by NFS schemas. c/generators/HexBinaryCodeGenerator.scala: Use ParserOrUnparserState fields as well as PState/UState fields. Call validate_fixed_attribute instead of parse/unparse_validate_fixed. examples/**/generated_code.[ch]: Regenerate to show changes in C generator such as adding new "auto-maintained by iwyu" comment, calls to renamed functions such as parse_align_to and unparse_align_to, defining schema_version, using pu fields, and calling validation functions. c/data/simple*.dat: Remove (contents now inside simple.tdml). c/ex_nums.dfdl.xsd: Define schema version to be "1.0.2". Include network format instead of defining own binary format. Adjust elements' own format properties as needed to match original data file (explicitness is better than using defaults). c/infosets/simple*.xml: Remove (contents now inside simple.tdml). c/nested.dfdl.xsd: Include network format instead of defining own binary format. c/network/format.dfdl.xsd: Define network format in only one place, making sure to use vitally needed alignment properties but keep the format as minimal as possible to make it easier to include in a wide variety of schemas. Change "Network order binary format" comment to "Network order big endian format" as requested. Define bitOrder and byteOrder explicitly in format schema instead of inheriting them from DFDLGeneralFormat schema. c/padtest.dfdl.xsd: Include network format instead of defining own binary format. c/simple.dfdl.xsd: Define schema version to be "1.0.0". Include network format instead of defining own binary format. Add new elements to test enumerations and ranges of primitive types. Use custom simple types in order to define simple type root elements, enumerations of simple type root elements, ranges of simple type elements, and all-in-one root element as compactly as possible. c/simple.tdml: Make comments clearer how to run tests and include all data and infosets in test cases instead of using files. c/simple_errors.tdml: Make comments clearer how to run tests and add new test cases to validate enumerations and ranges of primitive types. c/variablelen.dfdl.xsd: Include network format instead of defining own binary format. core/dsom/SchemaDocument.scala: Modify SchemaDocument to capture schema versions from XML schema definitions and provide to codegen-c. tdml/TestDaffodilC.scala: Add future-proofing test to check test schema compiles without any warnings (would catch "relative location deprecated" warning if DFDLGeneralFormat url was still relative). Fix inconsistent use of "dp"/"tdp" and "isError"/"isProcessingError". c/TestSimpleErrors.scala: Call new test cases in simple_errors.tdml. --- BUILD.md | 19 +- README.md | 39 +- .../daffodil/codegen/c/files/.clang-format | 7 +- .../apache/daffodil/codegen/c/files/Makefile | 2 +- .../codegen/c/files/libcli/cli_errors.c | 25 +- .../codegen/c/files/libcli/cli_errors.h | 7 +- .../codegen/c/files/libcli/daffodil_getopt.c | 50 +- .../codegen/c/files/libcli/daffodil_getopt.h | 18 +- .../codegen/c/files/libcli/daffodil_main.c | 46 +- .../daffodil/codegen/c/files/libcli/stack.c | 1 + .../daffodil/codegen/c/files/libcli/stack.h | 7 +- .../codegen/c/files/libcli/xml_reader.c | 7 +- .../codegen/c/files/libcli/xml_reader.h | 7 +- .../codegen/c/files/libcli/xml_writer.c | 11 +- .../codegen/c/files/libcli/xml_writer.h | 5 +- .../codegen/c/files/libruntime/errors.c | 15 +- .../codegen/c/files/libruntime/errors.h | 25 +- .../codegen/c/files/libruntime/infoset.c | 23 +- .../codegen/c/files/libruntime/infoset.h | 59 +- .../codegen/c/files/libruntime/parsers.c | 127 +-- .../codegen/c/files/libruntime/parsers.h | 16 +- .../codegen/c/files/libruntime/unparsers.c | 115 +-- .../codegen/c/files/libruntime/unparsers.h | 16 +- .../codegen/c/files/libruntime/validators.c | 149 +++ .../codegen/c/files/libruntime/validators.h | 57 ++ .../daffodil/codegen/c/files/tests/bits.c | 301 +++--- .../daffodil/codegen/c/files/tests/extras.c | 3 +- .../codegen/c/DaffodilCCodeGenerator.scala | 2 + .../c/DaffodilCExamplesGenerator.scala | 5 +- .../AlignmentFillCodeGenerator.scala | 15 +- .../BinaryBooleanCodeGenerator.scala | 4 +- .../generators/BinaryValueCodeGenerator.scala | 140 ++- .../c/generators/CodeGeneratorState.scala | 74 +- .../c/generators/HexBinaryCodeGenerator.scala | 26 +- .../examples/NestedUnion/generated_code.c | 62 +- .../examples/NestedUnion/generated_code.h | 5 + .../test/examples/ex_nums/generated_code.c | 346 +++---- .../test/examples/ex_nums/generated_code.h | 5 + .../test/examples/padtest/generated_code.c | 40 +- .../test/examples/padtest/generated_code.h | 5 + .../src/test/examples/simple/generated_code.c | 864 +++++++++++++++++- .../src/test/examples/simple/generated_code.h | 51 +- .../examples/variablelen/generated_code.c | 50 +- .../examples/variablelen/generated_code.h | 5 + .../codegen/c/data/simple-boolean.dat | Bin 4 -> 0 bytes .../daffodil/codegen/c/data/simple-byte.dat | Bin 1 -> 0 bytes .../daffodil/codegen/c/data/simple-double.dat | Bin 8 -> 0 bytes .../daffodil/codegen/c/data/simple-float.dat | Bin 4 -> 0 bytes .../codegen/c/data/simple-hexBinary.dat | Bin 4 -> 0 bytes .../c/data/simple-hexBinaryPrefixed.dat | Bin 6 -> 0 bytes .../daffodil/codegen/c/data/simple-int.dat | Bin 4 -> 0 bytes .../codegen/c/data/simple-integer.dat | Bin 4 -> 0 bytes .../daffodil/codegen/c/data/simple-long.dat | Bin 8 -> 0 bytes .../c/data/simple-nonNegativeInteger.dat | Bin 4 -> 0 bytes .../daffodil/codegen/c/data/simple-short.dat | Bin 2 -> 0 bytes .../codegen/c/data/simple-unsignedByte.dat | Bin 1 -> 0 bytes .../codegen/c/data/simple-unsignedInt.dat | Bin 4 -> 0 bytes .../codegen/c/data/simple-unsignedLong.dat | Bin 8 -> 0 bytes .../codegen/c/data/simple-unsignedShort.dat | Bin 2 -> 0 bytes .../daffodil/codegen/c/ex_nums.dfdl.xsd | 355 +++---- .../codegen/c/infosets/simple-boolean.dat.xml | 19 - .../codegen/c/infosets/simple-byte.dat.xml | 19 - .../codegen/c/infosets/simple-double.dat.xml | 19 - .../codegen/c/infosets/simple-float.dat.xml | 19 - .../c/infosets/simple-hexBinary.dat.xml | 19 - .../infosets/simple-hexBinaryPrefixed.dat.xml | 19 - .../codegen/c/infosets/simple-int.dat.xml | 19 - .../codegen/c/infosets/simple-integer.dat.xml | 19 - .../codegen/c/infosets/simple-long.dat.xml | 19 - .../simple-nonNegativeInteger.dat.xml | 19 - .../codegen/c/infosets/simple-short.dat.xml | 19 - .../c/infosets/simple-unsignedByte.dat.xml | 19 - .../c/infosets/simple-unsignedInt.dat.xml | 19 - .../c/infosets/simple-unsignedLong.dat.xml | 19 - .../c/infosets/simple-unsignedShort.dat.xml | 19 - .../apache/daffodil/codegen/c/nested.dfdl.xsd | 12 +- .../codegen/c/network/format.dfdl.xsd | 52 ++ .../daffodil/codegen/c/padtest.dfdl.xsd | 16 +- .../apache/daffodil/codegen/c/simple.dfdl.xsd | 362 +++++++- .../org/apache/daffodil/codegen/c/simple.tdml | 198 ++-- .../daffodil/codegen/c/simple_errors.tdml | 605 +++++++++++- .../daffodil/codegen/c/variablelen.dfdl.xsd | 13 +- .../daffodil/core/dsom/SchemaDocument.scala | 2 + .../processor/tdml/TestDaffodilC.scala | 15 +- .../daffodil/codegen/c/TestSimpleErrors.scala | 34 + 85 files changed, 3331 insertions(+), 1474 deletions(-) create mode 100644 daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libruntime/validators.c create mode 100644 daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libruntime/validators.h delete mode 100644 daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/data/simple-boolean.dat delete mode 100644 daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/data/simple-byte.dat delete mode 100644 daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/data/simple-double.dat delete mode 100644 daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/data/simple-float.dat delete mode 100644 daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/data/simple-hexBinary.dat delete mode 100644 daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/data/simple-hexBinaryPrefixed.dat delete mode 100644 daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/data/simple-int.dat delete mode 100644 daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/data/simple-integer.dat delete mode 100644 daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/data/simple-long.dat delete mode 100644 daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/data/simple-nonNegativeInteger.dat delete mode 100644 daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/data/simple-short.dat delete mode 100644 daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/data/simple-unsignedByte.dat delete mode 100644 daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/data/simple-unsignedInt.dat delete mode 100644 daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/data/simple-unsignedLong.dat delete mode 100644 daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/data/simple-unsignedShort.dat delete mode 100644 daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/infosets/simple-boolean.dat.xml delete mode 100644 daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/infosets/simple-byte.dat.xml delete mode 100644 daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/infosets/simple-double.dat.xml delete mode 100644 daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/infosets/simple-float.dat.xml delete mode 100644 daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/infosets/simple-hexBinary.dat.xml delete mode 100644 daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/infosets/simple-hexBinaryPrefixed.dat.xml delete mode 100644 daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/infosets/simple-int.dat.xml delete mode 100644 daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/infosets/simple-integer.dat.xml delete mode 100644 daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/infosets/simple-long.dat.xml delete mode 100644 daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/infosets/simple-nonNegativeInteger.dat.xml delete mode 100644 daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/infosets/simple-short.dat.xml delete mode 100644 daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/infosets/simple-unsignedByte.dat.xml delete mode 100644 daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/infosets/simple-unsignedInt.dat.xml delete mode 100644 daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/infosets/simple-unsignedLong.dat.xml delete mode 100644 daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/infosets/simple-unsignedShort.dat.xml create mode 100644 daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/network/format.dfdl.xsd diff --git a/BUILD.md b/BUILD.md index 963a517ea2..ced5c6d595 100644 --- a/BUILD.md +++ b/BUILD.md @@ -50,9 +50,9 @@ following its website's instructions. This is necessary to install the [libmxml-devel][Mini-XML] package. You can use the `dnf` package manager to install most of the tools -needed to build Daffodil: +used to develop Daffodil: - sudo dnf install clang gcc git java-11-openjdk-devel llvm make mxml-devel pkgconf + sudo dnf install clang gcc git iwyu java-11-openjdk-devel llvm make mxml-devel pkgconf If you want to use clang instead of gcc, you'll have to set your environment variables `CC` and `AR` to the clang binaries' names: @@ -68,15 +68,17 @@ commands you type will be able to call the C compiler. ## Ubuntu -You can use the `apt` package manager to install most of the tools -needed to build Daffodil: +You can use the `apt` package manager to install all of the tools +used to develop Daffodil: - sudo apt install build-essential clang clang-format default-jdk git libmxml-dev + sudo apt install build-essential clang clang-format default-jdk git iwyu libcriterion-dev libmxml-dev + # If "iwyu -print-resource-dir" prints /usr/lib/clang/13.0.1 and it doesn't exist: + sudo apt install libclang-common-13-dev If you want to use clang instead of gcc, you'll have to set your environment variables `CC` and `AR` to the clang binaries' names: - export CC=clang AR=llvm-ar-14 # or llvm-ar-10 or whatever you have + export CC=clang AR=llvm-ar-14 # or whatever llvm-ar-* version you have However, Ubuntu has no [sbt] package in its own repositories. You'll have to install the latest [sbt] version following its @@ -95,7 +97,7 @@ Install [MSYS2] following its website's instructions and open a new libraries. You can use the `pacman` package manager to install most of the tools -needed to build Daffodil: +used to develop Daffodil: pacman -S clang diffutils gcc git make pkgconf @@ -130,9 +132,10 @@ Install the Xcode Command Line Tools: xcode-select --install You can use the [Homebrew] package manager to install most of the tools -needed to build Daffodil: +used to develop Daffodil: brew install clang-format + brew install criterion brew install git brew install llvm # needed by iwyu brew install include-what-you-use diff --git a/README.md b/README.md index 7ec007e781..6594d89ebf 100644 --- a/README.md +++ b/README.md @@ -59,32 +59,53 @@ Compile source code: sbt compile -### Tests +### Test -Run unit tests: +Check all unit tests pass: sbt test -Run slower integration tests: +Check all integration tests pass: sbt daffodil-test-integration/test -### Command Line Interface +### Format -Build the command line interface (Linux and Windows shell scripts in -`daffodil-cli/target/universal/stage/bin/`; see the [Command Line -Interface] documentation for details on their usage): +Check format of source and sbt files: + + sbt scalafmtCheckAll scalafmtSbtCheck + +Reformat source and sbt files if necessary: + + sbt scalafmtAll scalafmtSbt + +### Build + +Build the Daffodil command line interface (Linux and Windows shell +scripts in `daffodil-cli/target/universal/stage/bin/`; see the +[Command Line Interface] documentation for details on their usage): sbt daffodil-cli/stage -### License Check +Publish the Daffodil jars to a Maven repository (for Java projects) or +Ivy repository (for Scala or schema projects). + +Maven (for Java or mvn): + + sbt publishM2 + +Ivy (for Scala or sbt): + + sbt publishLocal + +### Check Licenses Run [Apache RAT] (license audit report in `target/rat.txt` and error if any unapproved licenses are found): sbt ratCheck -### Test Coverage Report +### Check Coverage Run [sbt-scoverage] (report in `target/scala-ver/scoverage-report/`): diff --git a/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/.clang-format b/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/.clang-format index 761a215357..c20c242252 100644 --- a/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/.clang-format +++ b/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/.clang-format @@ -13,13 +13,12 @@ # See the License for the specific language governing permissions and # limitations under the License. -AlignConsecutiveDeclarations: true AllowShortFunctionsOnASingleLine: None -AllowShortIfStatementsOnASingleLine: true +AllowShortIfStatementsOnASingleLine: WithoutElse AlwaysBreakAfterReturnType: TopLevelDefinitions -BasedOnStyle: llvm +BasedOnStyle: LLVM BreakBeforeBraces: Allman ColumnLimit: 110 IndentWidth: 4 KeepEmptyLinesAtTheStartOfBlocks: false -SortIncludes: false +SortIncludes: Never diff --git a/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/Makefile b/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/Makefile index d2191d8adf..fc9d0c3003 100644 --- a/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/Makefile +++ b/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/Makefile @@ -105,7 +105,7 @@ clean: # $ make iwyu FMT = clang-format -i -IWYU = iwyu -Xiwyu --max_line_length=999 -Xiwyu --update_comments +IWYU = iwyu -Xiwyu --max_line_length=111 -Xiwyu --update_comments format: $(FMT) $(HEADERS) $(SOURCES) $(TSOURCES) diff --git a/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libcli/cli_errors.c b/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libcli/cli_errors.c index 1e23c14556..ac79caf6d8 100644 --- a/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libcli/cli_errors.c +++ b/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libcli/cli_errors.c @@ -15,6 +15,7 @@ * limitations under the License. */ +// auto-maintained by iwyu // clang-format off #include "cli_errors.h" #include // for assert @@ -31,9 +32,9 @@ static const ErrorLookup * error_lookup(uint8_t code) { - static const ErrorLookup table[CLI_ZZZ - ERR_ZZZ] = { + static const ErrorLookup table[CLI__NUM_CODES - ERR__NUM_CODES] = { {CLI_DIAGNOSTICS, "parse failed with %" PRId64 " diagnostics\n", FIELD_D64}, - {CLI_FILE_CLOSE, "error closing file\n", FIELD_ZZZ}, + {CLI_FILE_CLOSE, "error closing file\n", FIELD__NO_ARGS}, {CLI_FILE_OPEN, "error opening file '%s'\n", FIELD_S}, {CLI_HELP_USAGE, "Usage: %s [OPTION...] [infile]\n" @@ -61,16 +62,16 @@ error_lookup(uint8_t code) {CLI_INVALID_INFOSET, "invalid infoset type -- '%s'\n" USAGE, FIELD_S}, {CLI_INVALID_OPTION, "invalid option -- '%c'\n" USAGE, FIELD_C}, {CLI_INVALID_VALIDATE, "invalid validate mode -- '%s'\n" USAGE, FIELD_S}, - {CLI_MISSING_COMMAND, "missing command\n" USAGE, FIELD_ZZZ}, + {CLI_MISSING_COMMAND, "missing command\n" USAGE, FIELD__NO_ARGS}, {CLI_MISSING_VALUE, "option requires an argument -- '%c'\n" USAGE, FIELD_C}, {CLI_PROGRAM_ERROR, "unexpected getopt code %" PRId64 "\n" "Check for program error\n", FIELD_D64}, {CLI_PROGRAM_VERSION, "%s\n", FIELD_S_ON_STDOUT}, - {CLI_STACK_EMPTY, "stack empty, stopping program\n", FIELD_ZZZ}, - {CLI_STACK_OVERFLOW, "stack overflow, stopping program\n", FIELD_ZZZ}, - {CLI_STACK_UNDERFLOW, "stack underflow, stopping program\n", FIELD_ZZZ}, + {CLI_STACK_EMPTY, "stack empty, stopping program\n", FIELD__NO_ARGS}, + {CLI_STACK_OVERFLOW, "stack overflow, stopping program\n", FIELD__NO_ARGS}, + {CLI_STACK_UNDERFLOW, "stack underflow, stopping program\n", FIELD__NO_ARGS}, {CLI_STRTOBOOL, "error converting XML data '%s' to boolean\n", FIELD_S}, {CLI_STRTOD_ERRNO, "error converting XML data '%s' to number\n", FIELD_S}, {CLI_STRTOI_ERRNO, "error converting XML data '%s' to integer\n", FIELD_S}, @@ -78,19 +79,19 @@ error_lookup(uint8_t code) {CLI_STRTONUM_NOT, "found non-number characters in XML data '%s'\n", FIELD_S}, {CLI_STRTONUM_RANGE, "number in XML data '%s' out of range\n", FIELD_S}, {CLI_UNEXPECTED_ARGUMENT, "unexpected extra argument -- '%s'\n" USAGE, FIELD_S}, - {CLI_XML_DECL, "error making new XML declaration\n", FIELD_ZZZ}, + {CLI_XML_DECL, "error making new XML declaration\n", FIELD__NO_ARGS}, {CLI_XML_ELEMENT, "error making new XML element '%s'\n", FIELD_S}, {CLI_XML_ERD, "unexpected ERD typeCode %" PRId64 " while reading XML data\n", FIELD_D64}, - {CLI_XML_GONE, "ran out of XML data\n", FIELD_ZZZ}, - {CLI_XML_INPUT, "unable to read XML data from input file\n", FIELD_ZZZ}, + {CLI_XML_GONE, "ran out of XML data\n", FIELD__NO_ARGS}, + {CLI_XML_INPUT, "unable to read XML data from input file\n", FIELD__NO_ARGS}, {CLI_XML_LEFT, "did not consume all of the XML data, '%s' left\n", FIELD_S}, {CLI_XML_MISMATCH, "found mismatch between XML data and infoset '%s'\n", FIELD_S}, - {CLI_XML_WRITE, "error writing XML document\n", FIELD_ZZZ}, + {CLI_XML_WRITE, "error writing XML document\n", FIELD__NO_ARGS}, }; - if (code >= ERR_ZZZ && code < CLI_ZZZ) + if (code >= ERR__NUM_CODES && code < CLI__NUM_CODES) { - const ErrorLookup *lookup = &table[code - ERR_ZZZ]; + const ErrorLookup *lookup = &table[code - ERR__NUM_CODES]; // Double check that we looked up correct row assert(code == lookup->code); diff --git a/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libcli/cli_errors.h b/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libcli/cli_errors.h index 47f146208e..7adefef348 100644 --- a/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libcli/cli_errors.h +++ b/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libcli/cli_errors.h @@ -18,15 +18,16 @@ #ifndef CLI_ERRORS_H #define CLI_ERRORS_H +// auto-maintained by iwyu // clang-format off -#include "errors.h" // for ERR_ZZZ +#include "errors.h" // for ERR__NUM_CODES // clang-format on // CliCode - identifiers of libcli errors enum CliCode { - CLI_DIAGNOSTICS = ERR_ZZZ, + CLI_DIAGNOSTICS = ERR__NUM_CODES, CLI_FILE_CLOSE, CLI_FILE_OPEN, CLI_HELP_USAGE, @@ -59,7 +60,7 @@ enum CliCode CLI_XML_LEFT, CLI_XML_MISMATCH, CLI_XML_WRITE, - CLI_ZZZ, + CLI__NUM_CODES, }; // CliLimits - limits on how many elements static arrays can hold diff --git a/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libcli/daffodil_getopt.c b/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libcli/daffodil_getopt.c index 315ad4a42f..26e6b1f97c 100644 --- a/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libcli/daffodil_getopt.c +++ b/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libcli/daffodil_getopt.c @@ -15,6 +15,7 @@ * limitations under the License. */ +// auto-maintained by iwyu // clang-format off #include "daffodil_getopt.h" #include // for strcmp, strrchr @@ -29,23 +30,15 @@ struct daffodil_cli daffodil_cli = { DAFFODIL_MISSING_COMMAND, // default subcommand }; -// Initialize our "daffodil parse" CLI options +// Initialize our "daffodil parse/unparse" CLI options -struct daffodil_parse_cli daffodil_parse = { +struct daffodil_pu_cli daffodil_pu = { "xml", // default infoset type "-", // default infile "-", // default outfile false, // default validate }; -// Initialize our "daffodil unparse" CLI options - -struct daffodil_unparse_cli daffodil_unparse = { - "xml", // default infoset type - "-", // default infile - "-", // default outfile -}; - // Parse our command line interface. Note there is NO portable way to // parse "daffodil [options] command [more options] arguments" with // getopt. We will have to put all options before all arguments, @@ -63,7 +56,7 @@ parse_daffodil_cli(int argc, char *argv[]) // We expect callers to put all non-option arguments at the end int opt = 0; - while ((opt = getopt(argc, argv, ":hI:o:V:v")) != -1) + while ((opt = getopt(argc, argv, ":hI:o:r:s:V:v")) != -1) { switch (opt) { @@ -78,17 +71,21 @@ parse_daffodil_cli(int argc, char *argv[]) error.arg.s = optarg; return &error; } - daffodil_parse.infoset_converter = optarg; - daffodil_unparse.infoset_converter = optarg; + daffodil_pu.infoset_converter = optarg; break; case 'o': - daffodil_parse.outfile = optarg; - daffodil_unparse.outfile = optarg; + daffodil_pu.outfile = optarg; + break; + case 'r': + // Ignore "-r root" option/optarg + break; + case 's': + // Ignore "-s schema" option/optarg break; case 'V': if (strcmp("limited", optarg) == 0 || strcmp("on", optarg) == 0) { - daffodil_parse.validate = true; + daffodil_pu.validate = true; } else if (strcmp("off", optarg) != 0) { @@ -122,25 +119,14 @@ parse_daffodil_cli(int argc, char *argv[]) { const char *arg = argv[i]; - if (DAFFODIL_PARSE == daffodil_cli.subcommand) - { - if (strcmp("-", daffodil_parse.infile) == 0) - { - daffodil_parse.infile = arg; - } - else - { - error.code = CLI_UNEXPECTED_ARGUMENT; - error.arg.s = arg; - return &error; - } - } - else if (DAFFODIL_UNPARSE == daffodil_cli.subcommand) + if (DAFFODIL_MISSING_COMMAND != daffodil_cli.subcommand) { - if (strcmp("-", daffodil_unparse.infile) == 0) + // Set infile only once + if (strcmp("-", daffodil_pu.infile) == 0) { - daffodil_unparse.infile = arg; + daffodil_pu.infile = arg; } + // Error if infile is followed by another arg else { error.code = CLI_UNEXPECTED_ARGUMENT; diff --git a/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libcli/daffodil_getopt.h b/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libcli/daffodil_getopt.h index 5479d79c69..4aa9cabf2a 100644 --- a/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libcli/daffodil_getopt.h +++ b/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libcli/daffodil_getopt.h @@ -18,6 +18,7 @@ #ifndef DAFFODIL_GETOPT_H #define DAFFODIL_GETOPT_H +// auto-maintained by iwyu // clang-format off #include // for bool #include "errors.h" // for Error @@ -35,24 +36,15 @@ extern struct daffodil_cli } subcommand; } daffodil_cli; -// Declare our "daffodil parse" CLI options +// Declare our "daffodil parse/unparse" CLI options -extern struct daffodil_parse_cli +extern struct daffodil_pu_cli { const char *infoset_converter; const char *infile; const char *outfile; - bool validate; -} daffodil_parse; - -// Declare our "daffodil unparse" CLI options - -extern struct daffodil_unparse_cli -{ - const char *infoset_converter; - const char *infile; - const char *outfile; -} daffodil_unparse; + bool validate; +} daffodil_pu; // Parse our command line interface diff --git a/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libcli/daffodil_main.c b/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libcli/daffodil_main.c index bd9c66aa5a..8603e437f6 100644 --- a/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libcli/daffodil_main.c +++ b/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libcli/daffodil_main.c @@ -15,14 +15,15 @@ * limitations under the License. */ +// auto-maintained by iwyu // clang-format off #include // for bool, true #include // for NULL, FILE, perror, fclose, fopen, stdin, stdout #include // for strcmp #include "cli_errors.h" // for CLI_DIAGNOSTICS, CLI_FILE_CLOSE, CLI_FILE_OPEN -#include "daffodil_getopt.h" // for daffodil_cli, daffodil_parse, daffodil_parse_cli, parse_daffodil_cli, daffodil_unparse, daffodil_unparse_cli, DAFFODIL_PARSE, DAFFODIL_UNPARSE -#include "errors.h" // for continue_or_exit, print_diagnostics, Error, Diagnostics, Error::(anonymous) -#include "infoset.h" // for get_infoset, walk_infoset, PState, UState, parse_data, unparse_infoset, InfosetBase, VisitEventHandler +#include "daffodil_getopt.h" // for daffodil_cli, daffodil_pu, daffodil_pu_cli, DAFFODIL_PARSE, DAFFODIL_UNPARSE +#include "errors.h" // for continue_or_exit, Diagnostics, print_diagnostics, Error +#include "infoset.h" // for ParserOrUnparserState, PState, UState, get_infoset, walk_infoset, parse_data, unparse_infoset, InfosetBase, VisitEventHandler #include "xml_reader.h" // for xmlReaderMethods, XMLReader #include "xml_writer.h" // for xmlWriterMethods, XMLWriter // clang-format on @@ -75,16 +76,16 @@ main(int argc, char *argv[]) if (daffodil_cli.subcommand == DAFFODIL_PARSE) { // Open our input and output files if given as arguments - input = fopen_or_exit(input, daffodil_parse.infile, "r"); - output = fopen_or_exit(output, daffodil_parse.outfile, "w"); + input = fopen_or_exit(input, daffodil_pu.infile, "r"); + output = fopen_or_exit(output, daffodil_pu.outfile, "w"); // Parse the input file into our infoset - const bool CLEAR_INFOSET = true; + const bool CLEAR_INFOSET = true; InfosetBase *infoset = get_infoset(CLEAR_INFOSET); - PState pstate = {input, 0, NULL, NULL, 0, 0}; + PState pstate = {{input, 0, NULL, NULL}, 0, 0}; parse_data(infoset, &pstate); - print_diagnostics(pstate.diagnostics); - continue_or_exit(pstate.error); + print_diagnostics(pstate.pu.diagnostics); + continue_or_exit(pstate.pu.error); // Visit the infoset and print XML from it XMLWriter xmlWriter = {xmlWriterMethods, output, {NULL, NULL, 0}}; @@ -92,32 +93,37 @@ main(int argc, char *argv[]) continue_or_exit(error); // Any diagnostics will fail the parse if validate mode is on - if (daffodil_parse.validate && pstate.diagnostics) + if (daffodil_pu.validate && pstate.pu.diagnostics) { - static Error error = {CLI_DIAGNOSTICS, {0}}; - - error.arg.d64 = pstate.diagnostics->length; + const Error error = {CLI_DIAGNOSTICS, {.d64 = pstate.pu.diagnostics->length}}; continue_or_exit(&error); } } else if (daffodil_cli.subcommand == DAFFODIL_UNPARSE) { // Open our input and output files if given as arguments - input = fopen_or_exit(input, daffodil_unparse.infile, "r"); - output = fopen_or_exit(output, daffodil_unparse.outfile, "w"); + input = fopen_or_exit(input, daffodil_pu.infile, "r"); + output = fopen_or_exit(output, daffodil_pu.outfile, "w"); // Initialize our infoset's values from the XML data - const bool CLEAR_INFOSET = true; + const bool CLEAR_INFOSET = true; InfosetBase *infoset = get_infoset(CLEAR_INFOSET); - XMLReader xmlReader = {xmlReaderMethods, input, NULL, NULL}; + XMLReader xmlReader = {xmlReaderMethods, input, NULL, NULL}; error = walk_infoset((VisitEventHandler *)&xmlReader, infoset); continue_or_exit(error); // Unparse our infoset to the output file - UState ustate = {output, 0, NULL, NULL, 0, 0}; + UState ustate = {{output, 0, NULL, NULL}, 0, 0}; unparse_infoset(infoset, &ustate); - print_diagnostics(ustate.diagnostics); - continue_or_exit(ustate.error); + print_diagnostics(ustate.pu.diagnostics); + continue_or_exit(ustate.pu.error); + + // Any diagnostics will fail the unparse if validate mode is on + if (daffodil_pu.validate && ustate.pu.diagnostics) + { + const Error error = {CLI_DIAGNOSTICS, {.d64 = ustate.pu.diagnostics->length}}; + continue_or_exit(&error); + } } // Close our input and out files if we opened them diff --git a/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libcli/stack.c b/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libcli/stack.c index 6ec9fae44b..56d4a114d8 100644 --- a/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libcli/stack.c +++ b/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libcli/stack.c @@ -15,6 +15,7 @@ * limitations under the License. */ +// auto-maintained by iwyu // clang-format off #include "stack.h" #include // for bool diff --git a/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libcli/stack.h b/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libcli/stack.h index 550182ca2c..d2d312109b 100644 --- a/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libcli/stack.h +++ b/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libcli/stack.h @@ -18,6 +18,7 @@ #ifndef STACK_H #define STACK_H +// auto-maintained by iwyu // clang-format off #include // for mxml_node_t #include // for bool @@ -32,9 +33,9 @@ typedef mxml_node_t *stack_item_t; typedef struct { - stack_item_t *p_after; // Pointer to one past top element - stack_item_t *p_array; // Pointer to stack's array - ptrdiff_t capacity; // Size of stack's array + stack_item_t *p_after; // Pointer to one past top element + stack_item_t *p_array; // Pointer to stack's array + ptrdiff_t capacity; // Size of stack's array } c_stack_t; // Initialize stack with preallocated array diff --git a/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libcli/xml_reader.c b/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libcli/xml_reader.c index 8ba5f7765a..73eee7abc7 100644 --- a/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libcli/xml_reader.c +++ b/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libcli/xml_reader.c @@ -15,6 +15,7 @@ * limitations under the License. */ +// auto-maintained by iwyu // clang-format off #include "xml_reader.h" #include // for assert @@ -272,7 +273,7 @@ strtohexbinary(const char *text, HexBinary *hexBinary) memset(hexBinary->array, 0, hexBinary->lengthInBytes); for (size_t i = 0; i < numNibbles; i++) { - char c = text[i]; + char c = text[i]; uint8_t value = 0; // Check whether c is valid hexadecimal character @@ -441,8 +442,8 @@ xmlSimpleElem(XMLReader *reader, const ERD *erd, void *valueptr) { // Check for any errors calling strtonum or strtounum const Error *error = NULL; - intmax_t num = 0; - uintmax_t unum = 0; + intmax_t num = 0; + uintmax_t unum = 0; // Handle various types of values const enum TypeCode typeCode = erd->typeCode; diff --git a/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libcli/xml_reader.h b/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libcli/xml_reader.h index c90f93d93e..a40238e84c 100644 --- a/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libcli/xml_reader.h +++ b/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libcli/xml_reader.h @@ -18,6 +18,7 @@ #ifndef XML_READER_H #define XML_READER_H +// auto-maintained by iwyu // clang-format off #include // for mxml_node_t #include // for FILE @@ -29,9 +30,9 @@ typedef struct XMLReader { const VisitEventHandler handler; - FILE * stream; - mxml_node_t * xml; - mxml_node_t * node; + FILE *stream; + mxml_node_t *xml; + mxml_node_t *node; } XMLReader; // XMLReader methods to pass to walk_infoset method diff --git a/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libcli/xml_writer.c b/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libcli/xml_writer.c index 78a67c5470..469945f6d3 100644 --- a/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libcli/xml_writer.c +++ b/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libcli/xml_writer.c @@ -15,6 +15,7 @@ * limitations under the License. */ +// auto-maintained by iwyu // clang-format off #include "xml_writer.h" #include // for assert @@ -82,7 +83,7 @@ fixNumberIfNeeded(const char *text) static const char * binaryToHex(HexBinary hexBinary, bool freeMemory) { - static char * text = NULL; + static char *text = NULL; static size_t capacity = 256; // Call with freeMemory true when finished @@ -177,8 +178,8 @@ xmlStartComplex(XMLWriter *writer, const InfosetBase *base) { mxml_node_t *parent = stack_top(&writer->stack); - const char * name = get_erd_name(base->erd); - const char * xmlns = get_erd_xmlns(base->erd); + const char *name = get_erd_name(base->erd); + const char *xmlns = get_erd_xmlns(base->erd); mxml_node_t *complex = mxmlNewElement(parent, name); if (xmlns) { @@ -212,7 +213,7 @@ static const Error * xmlSimpleElem(XMLWriter *writer, const ERD *erd, const void *valueptr) { mxml_node_t *parent = stack_top(&writer->stack); - const char * name = get_erd_name(erd); + const char *name = get_erd_name(erd); mxml_node_t *simple = mxmlNewElement(parent, name); // Set namespace declaration if necessary @@ -224,7 +225,7 @@ xmlSimpleElem(XMLWriter *writer, const ERD *erd, const void *valueptr) } // Format various types of values as XML element's text - mxml_node_t * text = NULL; + mxml_node_t *text = NULL; const enum TypeCode typeCode = erd->typeCode; switch (typeCode) { diff --git a/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libcli/xml_writer.h b/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libcli/xml_writer.h index 3fa5d57378..daec0ac639 100644 --- a/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libcli/xml_writer.h +++ b/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libcli/xml_writer.h @@ -18,6 +18,7 @@ #ifndef XML_WRITER_H #define XML_WRITER_H +// auto-maintained by iwyu // clang-format off #include // for FILE #include "infoset.h" // for VisitEventHandler @@ -29,8 +30,8 @@ typedef struct XMLWriter { const VisitEventHandler handler; - FILE * stream; - c_stack_t stack; + FILE *stream; + c_stack_t stack; } XMLWriter; // XMLWriter methods to pass to walk_infoset method diff --git a/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libruntime/errors.c b/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libruntime/errors.c index 608b1b577a..550e1a5758 100644 --- a/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libruntime/errors.c +++ b/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libruntime/errors.c @@ -15,6 +15,7 @@ * limitations under the License. */ +// auto-maintained by iwyu // clang-format off #include "errors.h" #include // for assert @@ -77,18 +78,20 @@ add_diagnostic(Diagnostics *diagnostics, const Error *error) static const ErrorLookup * error_lookup(uint8_t code) { - static const ErrorLookup table[ERR_ZZZ] = { + static const ErrorLookup table[ERR__NUM_CODES] = { {ERR_ARRAY_BOUNDS, "%s count out of bounds\n", FIELD_S}, {ERR_CHOICE_KEY, "no match between choice dispatch key %" PRId64 " and any branch key\n", FIELD_D64}, - {ERR_FIXED_VALUE, "value of element '%s' does not match value of its 'fixed' attribute\n", FIELD_S}, {ERR_HEXBINARY_ALLOC, "error allocating hexBinary memory -- %" PRId64 " bytes\n", FIELD_D64}, {ERR_LEFTOVER_DATA, "Left over data, at least %i bit(s) remaining after end of parse\n", FIELD_C}, {ERR_PARSE_BOOL, "error parsing binary value %" PRId64 " as either true or false\n", FIELD_D64}, - {ERR_STREAM_EOF, "EOF in stream, stopping program\n", FIELD_ZZZ}, - {ERR_STREAM_ERROR, "error in stream, stopping program\n", FIELD_ZZZ}, + {ERR_RESTR_ENUM, "value of element '%s' does not match any of its enumerations\n", FIELD_S}, + {ERR_RESTR_FIXED, "value of element '%s' does not match value of its 'fixed' attribute\n", FIELD_S}, + {ERR_RESTR_RANGE, "value of element '%s' is outside its allowed range\n", FIELD_S}, + {ERR_STREAM_EOF, "EOF in stream, stopping program\n", FIELD__NO_ARGS}, + {ERR_STREAM_ERROR, "error in stream, stopping program\n", FIELD__NO_ARGS}, }; - if (code < ERR_ZZZ) + if (code < ERR__NUM_CODES) { const ErrorLookup *lookup = &table[code]; @@ -130,7 +133,7 @@ print_maybe_stop(const Error *error, int status) fprintf(stdout, lookup->message, error->arg.s); exit(EXIT_SUCCESS); break; - case FIELD_ZZZ: + case FIELD__NO_ARGS: default: fprintf(stderr, "%s", lookup->message); break; diff --git a/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libruntime/errors.h b/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libruntime/errors.h index 6a22a2af96..62b52c51bd 100644 --- a/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libruntime/errors.h +++ b/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libruntime/errors.h @@ -18,6 +18,7 @@ #ifndef ERRORS_H #define ERRORS_H +// auto-maintained by iwyu // clang-format off #include // for bool #include // for size_t @@ -31,13 +32,15 @@ enum ErrorCode { ERR_ARRAY_BOUNDS, ERR_CHOICE_KEY, - ERR_FIXED_VALUE, ERR_HEXBINARY_ALLOC, ERR_LEFTOVER_DATA, ERR_PARSE_BOOL, + ERR_RESTR_ENUM, + ERR_RESTR_FIXED, + ERR_RESTR_RANGE, ERR_STREAM_EOF, ERR_STREAM_ERROR, - ERR_ZZZ, + ERR__NUM_CODES, }; // ErrorField - identifiers of Error fields @@ -48,15 +51,15 @@ enum ErrorField FIELD_D64, FIELD_S, FIELD_S_ON_STDOUT, - FIELD_ZZZ, + FIELD__NO_ARGS, }; // ErrorLookup - structure of an error lookup table row typedef struct ErrorLookup { - uint8_t code; - const char * message; + uint8_t code; + const char *message; enum ErrorField field; } ErrorLookup; @@ -67,9 +70,9 @@ typedef struct Error uint8_t code; union { - int c; // for %c - int64_t d64; // for %d64 - const char *s; // for %s + int c; // for %c + int64_t d64; // for %d64 + const char *s; // for %s } arg; } Error; @@ -85,7 +88,7 @@ enum Limits typedef struct Diagnostics { - Error array[LIMIT_DIAGNOSTICS]; + Error array[LIMIT_DIAGNOSTICS]; size_t length; } Diagnostics; @@ -114,8 +117,4 @@ extern void continue_or_exit(const Error *error); typedef const ErrorLookup *cli_error_lookup_t(uint8_t code); extern cli_error_lookup_t *cli_error_lookup; -// daffodil_program_version - declare our program's name and version - -extern const char *daffodil_program_version; - #endif // ERRORS_H diff --git a/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libruntime/infoset.c b/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libruntime/infoset.c index 0aa1235924..3b1aaddb2a 100644 --- a/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libruntime/infoset.c +++ b/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libruntime/infoset.c @@ -15,6 +15,7 @@ * limitations under the License. */ +// auto-maintained by iwyu // clang-format off #include "infoset.h" #include // for memccpy @@ -80,8 +81,8 @@ get_erd_xmlns(const ERD *erd) if (erd->namedQName.ns) { static char xmlns[LIMIT_NAME_LENGTH]; - char * next = xmlns; - char * last = xmlns + sizeof(xmlns) - 1; + char *next = xmlns; + char *last = xmlns + sizeof(xmlns) - 1; next = memccpy(next, "xmlns", 0, last - next); if (next) @@ -154,9 +155,9 @@ walk_infoset(const VisitEventHandler *handler, const InfosetBase *infoNode) if (!error) { - const ERD * childERD = infoNode->erd; + const ERD *childERD = infoNode->erd; const size_t childOffset = childERD->numChildren ? 0 : childERD->childrenOffsets[0]; - const void * child = (const void *)((const char *)infoNode + childOffset); + const void *child = (const void *)((const char *)infoNode + childOffset); error = walk_infoset_node(handler, infoNode, childERD, child); } @@ -176,8 +177,8 @@ walk_infoset_node(const VisitEventHandler *handler, const InfosetBase *infoNode, const void *child) { // Get the type of child to walk - const Error * error = NULL; - const InfosetBase * childNode = (const InfosetBase *)child; + const Error *error = NULL; + const InfosetBase *childNode = (const InfosetBase *)child; const enum TypeCode typeCode = childERD->typeCode; // Walk the child appropriately depending on its type @@ -225,7 +226,7 @@ walk_array(const VisitEventHandler *handler, const InfosetBase *infoNode, const // Get the array's size, type of its elements, and offset between its elements const Error *error = NULL; const size_t arraySize = arrayERD->getArraySize(infoNode); - const ERD * childERD = arrayERD->childrenERDs[0]; + const ERD *childERD = arrayERD->childrenERDs[0]; const size_t childOffset = arrayERD->childrenOffsets[0]; // Walk each element of the array @@ -247,9 +248,9 @@ walk_array(const VisitEventHandler *handler, const InfosetBase *infoNode, const static const Error * walk_infoset_node_children(const VisitEventHandler *handler, const InfosetBase *infoNode) { - const size_t numChildren = infoNode->erd->numChildren; + const size_t numChildren = infoNode->erd->numChildren; const ERD *const *const childrenERDs = infoNode->erd->childrenERDs; - const size_t * childrenOffsets = infoNode->erd->childrenOffsets; + const size_t *childrenOffsets = infoNode->erd->childrenOffsets; // Start visiting the node const Error *error = handler->visitStartComplex(handler, infoNode); @@ -257,9 +258,9 @@ walk_infoset_node_children(const VisitEventHandler *handler, const InfosetBase * // Walk each child of the node for (size_t i = 0; i < numChildren && !error; i++) { - const ERD * childERD = childrenERDs[i]; + const ERD *childERD = childrenERDs[i]; const size_t childOffset = childrenOffsets[i]; - const void * child = (const void *)((const char *)infoNode + childOffset); + const void *child = (const void *)((const char *)infoNode + childOffset); error = walk_infoset_node(handler, infoNode, childERD, child); } diff --git a/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libruntime/infoset.h b/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libruntime/infoset.h index 73374c3398..48239a2df1 100644 --- a/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libruntime/infoset.h +++ b/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libruntime/infoset.h @@ -18,6 +18,7 @@ #ifndef INFOSET_H #define INFOSET_H +// auto-maintained by iwyu // clang-format off #include // for bool #include // for size_t @@ -42,9 +43,9 @@ typedef size_t (*GetArraySize)(const struct InfosetBase *infoNode); typedef const Error *(*VisitStartDocument)(const struct VisitEventHandler *handler); typedef const Error *(*VisitEndDocument)(const struct VisitEventHandler *handler); typedef const Error *(*VisitStartComplex)(const struct VisitEventHandler *handler, - const struct InfosetBase * base); + const struct InfosetBase *base); typedef const Error *(*VisitEndComplex)(const struct VisitEventHandler *handler, - const struct InfosetBase * base); + const struct InfosetBase *base); typedef const Error *(*VisitSimpleElem)(const struct VisitEventHandler *handler, const struct ERD *erd, const void *number); @@ -82,13 +83,13 @@ enum TypeCode typedef struct ERD { - const NamedQName namedQName; - const enum TypeCode typeCode; - const size_t numChildren; - const size_t * childrenOffsets; + const NamedQName namedQName; + const enum TypeCode typeCode; + const size_t numChildren; + const size_t *childrenOffsets; const struct ERD *const *childrenERDs; - const ERDParseSelf parseSelf; + const ERDParseSelf parseSelf; const ERDUnparseSelf unparseSelf; // Save space since typeCode won't be both ARRAY and CHOICE union @@ -102,41 +103,45 @@ typedef struct ERD typedef struct HexBinary { - uint8_t *array; // pointer to data in byte array - size_t lengthInBytes; // length of data in bytes - bool dynamic; // true if byte array was malloc'ed + uint8_t *array; // pointer to data in byte array + size_t lengthInBytes; // length of data in bytes + bool dynamic; // true if byte array was malloc'ed } HexBinary; // InfosetBase - metadata of an infoset element typedef struct InfosetBase { - const ERD * erd; + const ERD *erd; const struct InfosetBase *parent; } InfosetBase; +// ParserOrUnparserState - common mutable state while validating parser or unparser data + +typedef struct ParserOrUnparserState +{ + FILE *stream; // stream to read from / write to + size_t bitPos0b; // 0-based position after last read/write (1-bit granularity) + Diagnostics *diagnostics; // any validation diagnostics + const Error *error; // any error which stops parser/unparser +} ParserOrUnparserState; + // PState - mutable state while parsing data typedef struct PState { - FILE * stream; // stream to read data from (input) - size_t bitPos0b; // 0-based position of last successful parse (1-bit granularity) - Diagnostics *diagnostics; // any validation diagnostics - const Error *error; // any error which stops parse - uint8_t unreadBits; // any buffered bits not read yet - uint8_t numUnreadBits; // number of buffered bits not read yet + ParserOrUnparserState pu; // common mutable state + uint8_t unreadBits; // any buffered bits not read yet + uint8_t numUnreadBits; // number of buffered bits not read yet } PState; // UState - mutable state while unparsing infoset typedef struct UState { - FILE * stream; // stream to write data to (output) - size_t bitPos0b; // 0-based position of last successful write (1-bit granularity) - Diagnostics *diagnostics; // any validation diagnostics - const Error *error; // any error which stops unparse - uint8_t unwritBits; // any buffered bits not written yet - uint8_t numUnwritBits; // number of buffered bits not written yet + ParserOrUnparserState pu; // common mutable state + uint8_t unwritBits; // any buffered bits not written yet + uint8_t numUnwritBits; // number of buffered bits not written yet } UState; // VisitEventHandler - methods to be called when walking an infoset @@ -144,10 +149,10 @@ typedef struct UState typedef struct VisitEventHandler { const VisitStartDocument visitStartDocument; - const VisitEndDocument visitEndDocument; - const VisitStartComplex visitStartComplex; - const VisitEndComplex visitEndComplex; - const VisitSimpleElem visitSimpleElem; + const VisitEndDocument visitEndDocument; + const VisitStartComplex visitStartComplex; + const VisitEndComplex visitEndComplex; + const VisitSimpleElem visitSimpleElem; } VisitEventHandler; // get_erd_name, get_erd_xmlns, get_erd_ns - get name and xmlns diff --git a/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libruntime/parsers.c b/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libruntime/parsers.c index f39739582b..43e89b63bc 100644 --- a/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libruntime/parsers.c +++ b/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libruntime/parsers.c @@ -15,13 +15,14 @@ * limitations under the License. */ +// auto-maintained by iwyu // clang-format off #include "parsers.h" #include // for assert #include // for bool, false, true #include // for fread, fgetc, EOF #include // for free, malloc -#include "errors.h" // for Error, eof_or_error, Error::(anonymous), ERR_LEFTOVER_DATA, add_diagnostic, get_diagnostics, ERR_ARRAY_BOUNDS, ERR_FIXED_VALUE, ERR_HEXBINARY_ALLOC, ERR_PARSE_BOOL, Diagnostics +#include "errors.h" // for Error, eof_or_error, ERR_LEFTOVER_DATA, Error::(anonymous), ERR_HEXBINARY_ALLOC, ERR_PARSE_BOOL #include "p_endian.h" // for be64toh, le64toh, be32toh, le32toh // clang-format on @@ -38,8 +39,8 @@ // remaining bits not yet read within a fragment byte; returns last // bits of last byte already shifted to left end -// Note callers must check pstate->error after calling read_bits and -// update pstate->bitPos0b themselves after successful parses +// Note callers must check pstate->pu.error after calling read_bits and +// update pstate->pu.bitPos0b themselves after successful parses static void read_bits(uint8_t *bytes, size_t num_bits, PState *pstate) @@ -51,10 +52,10 @@ read_bits(uint8_t *bytes, size_t num_bits, PState *pstate) size_t num_bytes = num_bits / BYTE_WIDTH; if (num_bytes) { - size_t count = fread(bytes, 1, num_bytes, pstate->stream); + size_t count = fread(bytes, 1, num_bytes, pstate->pu.stream); if (count < num_bytes) { - pstate->error = eof_or_error(pstate->stream); + pstate->pu.error = eof_or_error(pstate->pu.stream); return; } num_bits -= count * BYTE_WIDTH; @@ -67,10 +68,10 @@ read_bits(uint8_t *bytes, size_t num_bits, PState *pstate) { // Copy one whole byte from stream to temporary storage size_t whole_byte = 0; - size_t count = fread(&whole_byte, 1, 1, pstate->stream); + size_t count = fread(&whole_byte, 1, 1, pstate->pu.stream); if (count < 1) { - pstate->error = eof_or_error(pstate->stream); + pstate->pu.error = eof_or_error(pstate->pu.stream); return; } @@ -126,15 +127,15 @@ parse_endian_double(bool big_endian_data, double *number, size_t num_bits, PStat // Parse all doubles in ths helper function union { - uint8_t bytes[sizeof(double)]; - double number; + uint8_t bytes[sizeof(double)]; + double number; uint64_t integer; } buffer; // Read data bits buffer.integer = 0; read_bits(buffer.bytes, num_bits, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; // Convert data endianness to host endianness if (big_endian_data) @@ -153,7 +154,7 @@ parse_endian_double(bool big_endian_data, double *number, size_t num_bits, PStat // Return successfully parsed number and update our last // successful parse position *number = buffer.number; - pstate->bitPos0b += num_bits; + pstate->pu.bitPos0b += num_bits; } // Helper method to read floats depending on data endianness; note @@ -165,15 +166,15 @@ parse_endian_float(bool big_endian_data, float *number, size_t num_bits, PState // Parse all floats in this helper function union { - uint8_t bytes[sizeof(float)]; - float number; + uint8_t bytes[sizeof(float)]; + float number; uint32_t integer; } buffer; // Read data bits buffer.integer = 0; read_bits(buffer.bytes, num_bits, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; // Convert data endianness to host endianness if (big_endian_data) @@ -192,7 +193,7 @@ parse_endian_float(bool big_endian_data, float *number, size_t num_bits, PState // Return successfully parsed number and update our last // successful parse position *number = buffer.number; - pstate->bitPos0b += num_bits; + pstate->pu.bitPos0b += num_bits; } // Helper method to read signed integers using fragment byte shifts @@ -216,7 +217,7 @@ parse_endian_int64(bool big_endian_data, int64_t *number, size_t num_bits, PStat // Read data bits buffer.integer = 0; read_bits(buffer.bytes, num_bits, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; // Shift data bits differently on endianness if (big_endian_data) @@ -253,7 +254,7 @@ parse_endian_int64(bool big_endian_data, int64_t *number, size_t num_bits, PStat // Return successfully parsed number and update our last // successful parse position *number = buffer.integer; - pstate->bitPos0b += num_bits; + pstate->pu.bitPos0b += num_bits; } // Helper method to read unsigned integers using fragment byte shifts @@ -266,14 +267,14 @@ parse_endian_uint64(bool big_endian_data, uint64_t *number, size_t num_bits, PSt // Parse all unsigned integers in this helper function union { - uint8_t bytes[sizeof(uint64_t)]; + uint8_t bytes[sizeof(uint64_t)]; uint64_t integer; } buffer; // Read data bits buffer.integer = 0; read_bits(buffer.bytes, num_bits, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; // Shift data bits differently on endianness if (big_endian_data) @@ -299,7 +300,7 @@ parse_endian_uint64(bool big_endian_data, uint64_t *number, size_t num_bits, PSt // Return successfully parsed number and update our last // successful parse position *number = buffer.integer; - pstate->bitPos0b += num_bits; + pstate->pu.bitPos0b += num_bits; } // Helper method to read booleans depending on data endianness; @@ -310,8 +311,8 @@ parse_endian_bool(bool big_endian_data, bool *number, size_t num_bits, int64_t t PState *pstate) { // Parse all booleans in this helper function - const size_t last_successful_parse = pstate->bitPos0b; - uint64_t integer; + const size_t last_successful_parse = pstate->pu.bitPos0b; + uint64_t integer; // Booleans are limited to 32 bits in the DFDL spec, but we read // all unsigned integers with parse_endian_uint64 using num_bits @@ -319,7 +320,7 @@ parse_endian_bool(bool big_endian_data, bool *number, size_t num_bits, int64_t t // parse_endian_uint64 will change position of last successful parse parse_endian_uint64(big_endian_data, &integer, num_bits, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; // Recognize true or false representation and assign boolean value // negative true_rep means it is absent and only false_rep needs @@ -341,10 +342,10 @@ parse_endian_bool(bool big_endian_data, bool *number, size_t num_bits, int64_t t { static Error error = {ERR_PARSE_BOOL, {0}}; error.arg.d64 = (int64_t)integer; - pstate->error = &error; + pstate->pu.error = &error; // Restore original position of last successful parse - pstate->bitPos0b = last_successful_parse; + pstate->pu.bitPos0b = last_successful_parse; } } @@ -507,34 +508,6 @@ parse_le_uint8(uint8_t *number, size_t num_bits, PState *pstate) *number = (uint8_t)integer; } -// Parse fill bits up to alignmentInBits or end_bitPos0b - -void -parse_align(size_t alignmentInBits, PState *pstate) -{ - size_t end_bitPos0b = ((pstate->bitPos0b + alignmentInBits - 1) / alignmentInBits) * alignmentInBits; - parse_fill_bits(end_bitPos0b, pstate); -} - -void -parse_fill_bits(size_t end_bitPos0b, PState *pstate) -{ - assert(pstate->bitPos0b <= end_bitPos0b); - - size_t fill_bits = end_bitPos0b - pstate->bitPos0b; - uint8_t bytes[1]; - while (fill_bits) - { - size_t num_bits = (fill_bits >= BYTE_WIDTH) ? BYTE_WIDTH : fill_bits; - read_bits(bytes, num_bits, pstate); - if (pstate->error) return; - fill_bits -= num_bits; - } - - // If we got all the way here, update our last successful parse position - pstate->bitPos0b = end_bitPos0b; -} - // Allocate memory for hexBinary array void @@ -553,7 +526,7 @@ alloc_hexBinary(HexBinary *hexBinary, size_t num_bytes, PState *pstate) { static Error error = {ERR_HEXBINARY_ALLOC, {0}}; error.arg.d64 = (int64_t)num_bytes; - pstate->error = &error; + pstate->pu.error = &error; } } @@ -563,36 +536,36 @@ void parse_hexBinary(HexBinary *hexBinary, PState *pstate) { read_bits(hexBinary->array, hexBinary->lengthInBytes * BYTE_WIDTH, pstate); - if (pstate->error) return; - pstate->bitPos0b += hexBinary->lengthInBytes * BYTE_WIDTH; + if (pstate->pu.error) return; + pstate->pu.bitPos0b += hexBinary->lengthInBytes * BYTE_WIDTH; } -// Validate element's value matches its fixed attribute +// Parse alignment bits up to alignmentInBits or end_bitPos0b void -parse_validate_fixed(bool same, const char *element, PState *pstate) +parse_align_to(size_t alignmentInBits, PState *pstate) { - if (!same) - { - Diagnostics *diagnostics = get_diagnostics(); - const Error error = {ERR_FIXED_VALUE, {.s = element}}; - - add_diagnostic(diagnostics, &error); - pstate->diagnostics = diagnostics; - } + size_t end_bitPos0b = ((pstate->pu.bitPos0b + alignmentInBits - 1) / alignmentInBits) * alignmentInBits; + parse_alignment_bits(end_bitPos0b, pstate); } -// Check array count is within bounds - void -parse_check_bounds(const char *name, size_t count, size_t minOccurs, size_t maxOccurs, PState *pstate) +parse_alignment_bits(size_t end_bitPos0b, PState *pstate) { - if (count < minOccurs || count > maxOccurs) + assert(pstate->pu.bitPos0b <= end_bitPos0b); + + size_t fill_bits = end_bitPos0b - pstate->pu.bitPos0b; + uint8_t bytes[1]; + while (fill_bits) { - static Error error = {ERR_ARRAY_BOUNDS, {0}}; - error.arg.s = name; - pstate->error = &error; + size_t num_bits = (fill_bits >= BYTE_WIDTH) ? BYTE_WIDTH : fill_bits; + read_bits(bytes, num_bits, pstate); + if (pstate->pu.error) return; + fill_bits -= num_bits; } + + // If we got all the way here, update our last successful parse position + pstate->pu.bitPos0b = end_bitPos0b; } // Check for any data left over after end of parse @@ -601,7 +574,7 @@ void no_leftover_data(PState *pstate) { // Skip the check if we already have an error - if (!pstate->error) + if (!pstate->pu.error) { // Check for any unread bits left in pstate's fragment byte if (pstate->numUnreadBits) @@ -609,18 +582,18 @@ no_leftover_data(PState *pstate) // We have some unread bits remaining, so report leftover data static Error error = {ERR_LEFTOVER_DATA, {0}}; error.arg.c = pstate->numUnreadBits; - pstate->error = &error; + pstate->pu.error = &error; } else { // Check for any unread bytes left in input stream - int c = fgetc(pstate->stream); + int c = fgetc(pstate->pu.stream); if (c != EOF) { // We have some unread bytes remaining, so report leftover data static Error error = {ERR_LEFTOVER_DATA, {0}}; error.arg.c = BYTE_WIDTH; - pstate->error = &error; + pstate->pu.error = &error; } } } diff --git a/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libruntime/parsers.h b/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libruntime/parsers.h index 4428c07c95..a1cbdf50de 100644 --- a/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libruntime/parsers.h +++ b/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libruntime/parsers.h @@ -18,6 +18,7 @@ #ifndef PARSERS_H #define PARSERS_H +// auto-maintained by iwyu // clang-format off #include // for bool #include // for size_t @@ -57,11 +58,6 @@ extern void parse_le_uint32(uint32_t *number, size_t num_bits, PState *pstate); extern void parse_le_uint64(uint64_t *number, size_t num_bits, PState *pstate); extern void parse_le_uint8(uint8_t *number, size_t num_bits, PState *pstate); -// Parse fill bits up to alignmentInBits or end_bitPos0b - -extern void parse_align(size_t alignmentInBits, PState *pstate); -extern void parse_fill_bits(size_t end_bitPos0b, PState *pstate); - // Allocate memory for hexBinary array extern void alloc_hexBinary(HexBinary *hexBinary, size_t num_bytes, PState *pstate); @@ -70,14 +66,10 @@ extern void alloc_hexBinary(HexBinary *hexBinary, size_t num_bytes, PState *psta extern void parse_hexBinary(HexBinary *hexBinary, PState *pstate); -// Validate parsed number is same as fixed value - -extern void parse_validate_fixed(bool same, const char *element, PState *pstate); - -// Check array count is within bounds +// Parse alignment bits up to alignmentInBits or end_bitPos0b -extern void parse_check_bounds(const char *name, size_t count, size_t minOccurs, size_t maxOccurs, - PState *pstate); +extern void parse_align_to(size_t alignmentInBits, PState *pstate); +extern void parse_alignment_bits(size_t end_bitPos0b, PState *pstate); // Check for any data left over after end of parse diff --git a/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libruntime/unparsers.c b/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libruntime/unparsers.c index 9614b39fc2..04bc8f0246 100644 --- a/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libruntime/unparsers.c +++ b/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libruntime/unparsers.c @@ -15,12 +15,13 @@ * limitations under the License. */ +// auto-maintained by iwyu // clang-format off #include "unparsers.h" #include // for assert #include // for bool #include // for fwrite -#include "errors.h" // for eof_or_error, Error, add_diagnostic, get_diagnostics, ERR_ARRAY_BOUNDS, ERR_FIXED_VALUE, Diagnostics, Error::(anonymous) +#include "errors.h" // for eof_or_error #include "p_endian.h" // for htobe64, htole64, htobe32, htole32 // clang-format on @@ -37,8 +38,8 @@ // remaining bits not yet written within a fragment byte; expects last // bits of last byte to be already shifted to left end -// Note callers must check ustate->error after calling write_bits and -// update ustate->bitPos0b themselves after successful unparses +// Note callers must check ustate->pu.error after calling write_bits and +// update ustate->pu.bitPos0b themselves after successful unparses static void write_bits(const uint8_t *bytes, size_t num_bits, UState *ustate) @@ -50,10 +51,10 @@ write_bits(const uint8_t *bytes, size_t num_bits, UState *ustate) size_t num_bytes = num_bits / BYTE_WIDTH; if (num_bytes) { - size_t count = fwrite(bytes, 1, num_bytes, ustate->stream); + size_t count = fwrite(bytes, 1, num_bytes, ustate->pu.stream); if (count < num_bytes) { - ustate->error = eof_or_error(ustate->stream); + ustate->pu.error = eof_or_error(ustate->pu.stream); return; } num_bits -= count * BYTE_WIDTH; @@ -66,7 +67,7 @@ write_bits(const uint8_t *bytes, size_t num_bits, UState *ustate) { // Fill the fragment byte uint8_t whole_byte = bytes[ix_bytes++]; - size_t num_bits_fill = BYTE_WIDTH - ustate->numUnwritBits; + size_t num_bits_fill = BYTE_WIDTH - ustate->numUnwritBits; ustate->unwritBits <<= num_bits_fill; ustate->unwritBits |= HIGH_BITS(whole_byte, num_bits_fill); ustate->numUnwritBits += num_bits_fill; @@ -75,10 +76,10 @@ write_bits(const uint8_t *bytes, size_t num_bits, UState *ustate) // Copy the fragment byte to stream size_t num_bits_write = BYTE_WIDTH; - size_t count = fwrite(&ustate->unwritBits, 1, 1, ustate->stream); + size_t count = fwrite(&ustate->unwritBits, 1, 1, ustate->pu.stream); if (count < 1) { - ustate->error = eof_or_error(ustate->stream); + ustate->pu.error = eof_or_error(ustate->pu.stream); return; } ustate->numUnwritBits -= num_bits_write; @@ -116,8 +117,8 @@ unparse_endian_double(bool big_endian_data, double number, size_t num_bits, USta // Unparse all doubles in ths helper function union { - uint8_t bytes[sizeof(double)]; - double number; + uint8_t bytes[sizeof(double)]; + double number; uint64_t integer; } buffer; @@ -138,8 +139,8 @@ unparse_endian_double(bool big_endian_data, double number, size_t num_bits, USta // Write data bits and update our last successful write position write_bits(buffer.bytes, num_bits, ustate); - if (ustate->error) return; - ustate->bitPos0b += num_bits; + if (ustate->pu.error) return; + ustate->pu.bitPos0b += num_bits; } // Helper method to write floats depending on data endianness; @@ -151,8 +152,8 @@ unparse_endian_float(bool big_endian_data, float number, size_t num_bits, UState // Unparse all floats in this helper function union { - uint8_t bytes[sizeof(float)]; - float number; + uint8_t bytes[sizeof(float)]; + float number; uint32_t integer; } buffer; @@ -173,8 +174,8 @@ unparse_endian_float(bool big_endian_data, float number, size_t num_bits, UState // Write data bits and update our last successful write position write_bits(buffer.bytes, num_bits, ustate); - if (ustate->error) return; - ustate->bitPos0b += num_bits; + if (ustate->pu.error) return; + ustate->pu.bitPos0b += num_bits; } // Helper method to write signed integers using fragment byte shifts @@ -215,8 +216,8 @@ unparse_endian_int64(bool big_endian_data, int64_t number, size_t num_bits, USta // Write data bits and update our last successful write position write_bits(buffer.bytes, num_bits, ustate); - if (ustate->error) return; - ustate->bitPos0b += num_bits; + if (ustate->pu.error) return; + ustate->pu.bitPos0b += num_bits; } // Helper method to write unsigned integers using fragment byte shifts @@ -232,7 +233,7 @@ unparse_endian_uint64(bool big_endian_data, uint64_t number, size_t num_bits, US // Unparse all unsigned integers in this helper function union { - uint8_t bytes[sizeof(uint64_t)]; + uint8_t bytes[sizeof(uint64_t)]; uint64_t integer; } buffer; @@ -260,8 +261,8 @@ unparse_endian_uint64(bool big_endian_data, uint64_t number, size_t num_bits, US // Write data bits and update our last successful write position write_bits(buffer.bytes, num_bits, ustate); - if (ustate->error) return; - ustate->bitPos0b += num_bits; + if (ustate->pu.error) return; + ustate->pu.bitPos0b += num_bits; } // Unparse all binary booleans, real numbers, and integers in helper @@ -401,69 +402,41 @@ unparse_le_uint8(uint8_t number, size_t num_bits, UState *ustate) unparse_endian_uint64(LITTLE_ENDIAN_DATA, number, num_bits, ustate); } -// Unparse fill bits up to alignmentInBits or end_bitPos0b - -void -unparse_align(size_t alignmentInBits, const uint8_t fill_byte, UState *ustate) -{ - size_t end_bitPos0b = ((ustate->bitPos0b + alignmentInBits - 1) / alignmentInBits) * alignmentInBits; - unparse_fill_bits(end_bitPos0b, fill_byte, ustate); -} - -void -unparse_fill_bits(size_t end_bitPos0b, const uint8_t fill_byte, UState *ustate) -{ - assert(ustate->bitPos0b <= end_bitPos0b); - - size_t fill_bits = end_bitPos0b - ustate->bitPos0b; - while (fill_bits) - { - size_t num_bits = (fill_bits >= BYTE_WIDTH) ? BYTE_WIDTH : fill_bits; - write_bits(&fill_byte, num_bits, ustate); - if (ustate->error) return; - fill_bits -= num_bits; - } - - // If we got all the way here, update our last successful write position - ustate->bitPos0b = end_bitPos0b; -} - // Unparse opaque bytes from hexBinary field void unparse_hexBinary(HexBinary hexBinary, UState *ustate) { write_bits(hexBinary.array, hexBinary.lengthInBytes * BYTE_WIDTH, ustate); - if (ustate->error) return; - ustate->bitPos0b += hexBinary.lengthInBytes * BYTE_WIDTH; + if (ustate->pu.error) return; + ustate->pu.bitPos0b += hexBinary.lengthInBytes * BYTE_WIDTH; } -// Validate element's value matches its fixed attribute +// Unparse alignment bits up to alignmentInBits or end_bitPos0b void -unparse_validate_fixed(bool same, const char *element, UState *ustate) +unparse_align_to(size_t alignmentInBits, const uint8_t fill_byte, UState *ustate) { - if (!same) - { - Diagnostics *diagnostics = get_diagnostics(); - const Error error = {ERR_FIXED_VALUE, {.s = element}}; - - add_diagnostic(diagnostics, &error); - ustate->diagnostics = diagnostics; - } + size_t end_bitPos0b = ((ustate->pu.bitPos0b + alignmentInBits - 1) / alignmentInBits) * alignmentInBits; + unparse_alignment_bits(end_bitPos0b, fill_byte, ustate); } -// Check array count is within bounds - void -unparse_check_bounds(const char *name, size_t count, size_t minOccurs, size_t maxOccurs, UState *ustate) +unparse_alignment_bits(size_t end_bitPos0b, const uint8_t fill_byte, UState *ustate) { - if (count < minOccurs || count > maxOccurs) + assert(ustate->pu.bitPos0b <= end_bitPos0b); + + size_t fill_bits = end_bitPos0b - ustate->pu.bitPos0b; + while (fill_bits) { - static Error error = {ERR_ARRAY_BOUNDS, {0}}; - error.arg.s = name; - ustate->error = &error; + size_t num_bits = (fill_bits >= BYTE_WIDTH) ? BYTE_WIDTH : fill_bits; + write_bits(&fill_byte, num_bits, ustate); + if (ustate->pu.error) return; + fill_bits -= num_bits; } + + // If we got all the way here, update our last successful write position + ustate->pu.bitPos0b = end_bitPos0b; } // Flush the fragment byte if not done yet @@ -472,7 +445,7 @@ void flush_fragment_byte(const uint8_t fill_byte, UState *ustate) { // Skip the flush if we already have an error - if (!ustate->error) + if (!ustate->pu.error) { // Do we have any unwritten bits left in the fragment byte? if (ustate->numUnwritBits) @@ -484,14 +457,14 @@ flush_fragment_byte(const uint8_t fill_byte, UState *ustate) // Flush the fragment byte size_t num_bits_write = ustate->numUnwritBits; - size_t count = fwrite(&ustate->unwritBits, 1, 1, ustate->stream); + size_t count = fwrite(&ustate->unwritBits, 1, 1, ustate->pu.stream); if (count < 1) { - ustate->error = eof_or_error(ustate->stream); + ustate->pu.error = eof_or_error(ustate->pu.stream); num_bits_write = 0; } ustate->numUnwritBits -= num_bits_write; - ustate->bitPos0b += num_bits_write; + ustate->pu.bitPos0b += num_bits_write; } } } diff --git a/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libruntime/unparsers.h b/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libruntime/unparsers.h index af5b9c8ea7..0c7f6500cb 100644 --- a/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libruntime/unparsers.h +++ b/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libruntime/unparsers.h @@ -18,6 +18,7 @@ #ifndef UNPARSERS_H #define UNPARSERS_H +// auto-maintained by iwyu // clang-format off #include // for bool #include // for size_t @@ -57,23 +58,14 @@ extern void unparse_le_uint32(uint32_t number, size_t num_bits, UState *ustate); extern void unparse_le_uint64(uint64_t number, size_t num_bits, UState *ustate); extern void unparse_le_uint8(uint8_t number, size_t num_bits, UState *ustate); -// Unparse fill bits up to alignmentInBits or end_bitPos0b - -extern void unparse_align(size_t alignmentInBits, const uint8_t fill_byte, UState *ustate); -extern void unparse_fill_bits(size_t end_bitPos0b, const uint8_t fill_byte, UState *ustate); - // Unparse opaque bytes from hexBinary field extern void unparse_hexBinary(HexBinary hexBinary, UState *ustate); -// Validate unparsed number is same as fixed value - -extern void unparse_validate_fixed(bool same, const char *element, UState *ustate); - -// Check array count is within bounds +// Unparse alignment bits up to alignmentInBits or end_bitPos0b -extern void unparse_check_bounds(const char *name, size_t count, size_t minOccurs, size_t maxOccurs, - UState *ustate); +extern void unparse_align_to(size_t alignmentInBits, const uint8_t fill_byte, UState *ustate); +extern void unparse_alignment_bits(size_t end_bitPos0b, const uint8_t fill_byte, UState *ustate); // Flush the fragment byte if not done yet diff --git a/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libruntime/validators.c b/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libruntime/validators.c new file mode 100644 index 0000000000..69f388741e --- /dev/null +++ b/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libruntime/validators.c @@ -0,0 +1,149 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// auto-maintained by iwyu +// clang-format off +#include "validators.h" +#include // for bool, false, true +#include // for memcmp +#include "errors.h" // for add_diagnostic, get_diagnostics, Error, Diagnostics, ERR_RESTR_ENUM, ERR_ARRAY_BOUNDS, ERR_RESTR_FIXED, ERR_RESTR_RANGE, Error::(anonymous) +// clang-format on + +// Validate element's array count is within its array bounds + +void +validate_array_bounds(const char *name, size_t count, size_t minOccurs, size_t maxOccurs, + ParserOrUnparserState *pu) +{ + if (count < minOccurs || count > maxOccurs) + { + // Array count is not within bounds, so report error + static Error error = {ERR_ARRAY_BOUNDS, {0}}; + error.arg.s = name; + pu->error = &error; + } +} + +// Validate element's value is same as its fixed attribute + +void +validate_fixed_attribute(bool same, const char *element, ParserOrUnparserState *pu) +{ + if (!same) + { + // Element is not same as its fixed attribute, so diagnose problem + Diagnostics *diagnostics = get_diagnostics(); + const Error error = {ERR_RESTR_FIXED, {.s = element}}; + + add_diagnostic(diagnostics, &error); + pu->diagnostics = diagnostics; + } +} + +// Validate element's value matches a floating point enumeration + +void +validate_floatpt_enumeration(double number, size_t num_enums, double enums[], const char *element, + ParserOrUnparserState *pu) +{ + bool match_found = false; + for (size_t i = 0; !match_found && i < num_enums; i++) + { + if (number == enums[i]) + { + match_found = true; + } + } + if (!match_found) + { + // Number does not match any enumeration, so diagnose problem + Diagnostics *diagnostics = get_diagnostics(); + const Error error = {ERR_RESTR_ENUM, {.s = element}}; + + add_diagnostic(diagnostics, &error); + pu->diagnostics = diagnostics; + } +} + +// Validate element's value matches a hexBinary enumeration +// (matches the actual data bytes, not the hex digit characters) + +void +validate_hexbinary_enumeration(const HexBinary *hexBinary, size_t num_enums, HexBinary enums[], + const char *element, ParserOrUnparserState *pu) +{ + bool match_found = false; + for (size_t i = 0; !match_found && i < num_enums; i++) + { + bool same_lengths = hexBinary->lengthInBytes == enums[i].lengthInBytes; + if (same_lengths && memcmp(hexBinary->array, enums[i].array, hexBinary->lengthInBytes) == 0) + { + match_found = true; + } + } + if (!match_found) + { + // HexBinary does not match any enumeration value, so report error + Diagnostics *diagnostics = get_diagnostics(); + const Error error = {ERR_RESTR_ENUM, {.s = element}}; + + add_diagnostic(diagnostics, &error); + pu->diagnostics = diagnostics; + } +} + +// Validate element's value matches an integer enumeration + +void +validate_integer_enumeration(int64_t number, size_t num_enums, int64_t enums[], const char *element, + ParserOrUnparserState *pu) +{ + bool match_found = false; + for (size_t i = 0; !match_found && i < num_enums; i++) + { + if (number == enums[i]) + { + match_found = true; + } + } + if (!match_found) + { + // Number does not match any enumeration value, so report error + Diagnostics *diagnostics = get_diagnostics(); + const Error error = {ERR_RESTR_ENUM, {.s = element}}; + + add_diagnostic(diagnostics, &error); + pu->diagnostics = diagnostics; + } +} + +// Validate element's value fits within its schema's allowed range + +void +validate_schema_range(bool within_range, const char *element, ParserOrUnparserState *pu) +{ + // The boolean expression that determines if a field is in range + // is evaluated in generated code and the result passed here + if (!within_range) + { + Diagnostics *diagnostics = get_diagnostics(); + const Error error = {ERR_RESTR_RANGE, {.s = element}}; + + add_diagnostic(diagnostics, &error); + pu->diagnostics = diagnostics; + } +} diff --git a/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libruntime/validators.h b/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libruntime/validators.h new file mode 100644 index 0000000000..fb9cca8d63 --- /dev/null +++ b/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/libruntime/validators.h @@ -0,0 +1,57 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef VALIDATORS_H +#define VALIDATORS_H + +// auto-maintained by iwyu +// clang-format off +#include // for bool +#include // for size_t +#include // for int64_t +#include "infoset.h" // for ParserOrUnparserState +// clang-format on + +// Validate element's array count is within its array bounds + +extern void validate_array_bounds(const char *name, size_t count, size_t minOccurs, size_t maxOccurs, + ParserOrUnparserState *pu); + +// Validate element's value is same as its fixed attribute + +extern void validate_fixed_attribute(bool same, const char *element, ParserOrUnparserState *pu); + +// Validate element's value matches a floating point enumeration + +extern void validate_floatpt_enumeration(double number, size_t num_enums, double enums[], const char *element, + ParserOrUnparserState *pu); + +// Validate element's value matches a hexBinary enumeration + +extern void validate_hexbinary_enumeration(const HexBinary *hexBinary, size_t num_enums, HexBinary enums[], + const char *element, ParserOrUnparserState *pu); + +// Validate element's value matches an integer enumeration + +extern void validate_integer_enumeration(int64_t number, size_t num_enums, int64_t enums[], + const char *element, ParserOrUnparserState *pu); + +// Validate element's value fits within its schema's allowed range + +extern void validate_schema_range(bool within_range, const char *element, ParserOrUnparserState *pu); + +#endif // VALIDATORS_H diff --git a/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/tests/bits.c b/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/tests/bits.c index 6bdde99283..3b04fb2e16 100644 --- a/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/tests/bits.c +++ b/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/tests/bits.c @@ -15,15 +15,16 @@ * limitations under the License. */ +// auto-maintained by iwyu // clang-format off #include // for CR_VA_HEAD_HELPER_2, CRI_IF_DEFINED_NODEFER_2, CR_VA_TAIL_HELPER_2, CRITERION_APPLY_3, CRI_IF_DEFINED_2, CR_VA_HEAD_HELPER_1, Test #include // for CRI_ASSERT_OP_VAR_TAGGED, CRI_ASSERT_OP_TYPE_TAGGED, CRI_ASSERT_OP_VAL_TAGGED, CRI_ASSERT_TEST_TAG_u8, cr_user_u8_tostr, CRI_ASSERT_OP_MKNODE_TAGGED, CRI_ASSERT_OP_NAME_TAGGED, CRI_ASSERT_TEST_TAG_sz, cr_user_sz_tostr, CRI_ASSERT_TYPE_TAG_ID_u8, cr_user_u8_eq, CRI_ASSERT_TEST_TAG_ptr, cr_user_ptr_tostr, CRI_ASSERT_MKLIST_2, CRI_ASSERT_SPECIFIER_OP1, CRI_ASSERT_SPECIFIER_eq, CRI_ASSERT_SPEC_OPLEN_2, CRI_ASSERT_TEST_SPECIFIER_eq, cr_expect, CRI_ASSERT_TYPE_TAG_u8, CRI_ASSERT_TYPE_TAG_ID_sz, cr_user_sz_eq, CRI_ASSERT_TYPE_TAG_sz, CRI_ASSERT_TYPE_TAG_ID_ptr, cr_user_ptr_eq, CRI_ASSERT_TEST_TAGC_u8, CRI_ASSERT_TEST_TAG_int, cr_user_int_tostr, CRI_ASSERT_TYPE_TAG_ptr, CRI_ASSERT_TEST_TAGC_sz, CRI_ASSERT_TYPE_TAG_ID_int, cr_user_int_eq, CRI_ASSERT_TEST_TAGC_ptr, CRI_ASSERT_TYPE_TAG_int, CRI_ASSERT_TEST_TAG_i16, CRI_ASSERT_TEST_TAG_i32, CRI_ASSERT_TEST_TAG_i64, CRI_ASSERT_TEST_TAG_i8, CRI_ASSERT_TEST_TAG_u16, CRI_ASSERT_TEST_TAG_u32, CRI_ASSERT_TE... #include // for CR_CHECKERROR_1 #include // for false, true, bool -#include // for uint32_t, int16_t, int32_t, int64_t, int8_t, uint16_t, uint64_t, uint8_t +#include // for uint32_t, uint8_t, int16_t, int32_t, int64_t, int8_t, uint16_t, uint64_t #include // for fclose, NULL, fflush, fmemopen, open_memstream, FILE, size_t #include // for free -#include "infoset.h" // for UState, PState +#include "infoset.h" // for UState, PState, ParserOrUnparserState #include "parsers.h" // for parse_be_bool, parse_le_bool, parse_be_int16, parse_be_int32, parse_be_int64, parse_be_int8, parse_be_uint16, parse_be_uint32, parse_be_uint64, parse_be_uint8, parse_le_int16, parse_le_int32, parse_le_int64, parse_le_int8, parse_le_uint16, parse_le_uint32, parse_le_uint64, parse_le_uint8 #include "unparsers.h" // for unparse_be_bool, unparse_le_bool, flush_fragment_byte, unparse_be_int16, unparse_be_int32, unparse_be_int64, unparse_be_int8, unparse_be_uint16, unparse_be_uint32, unparse_be_uint64, unparse_be_uint8, unparse_le_int16, unparse_le_int32, unparse_le_int64, unparse_le_int8, unparse_le_uint16, unparse_le_uint32, unparse_le_uint64, unparse_le_uint8 // clang-format on @@ -31,18 +32,18 @@ Test(bits, be_bool_24) { // Open memory stream for writing to dynamic buffer - char * buffer = NULL; - size_t size = 0; - FILE * stream = open_memstream(&buffer, &size); - UState ustate = {stream, 0, NULL, NULL, 0, 0}; + char *buffer = NULL; + size_t size = 0; + FILE *stream = open_memstream(&buffer, &size); + UState ustate = {{stream, 0, NULL, NULL}, 0, 0}; uint32_t true24_rep = 076543210; // 0b_111_110_101_100_011_010_001_000 uint32_t false_rep = 0; // Verify that ustate writes 11111010, 11000110, 10001000 unparse_be_bool(true, 24, true24_rep, false_rep, &ustate); fflush(stream); - cr_expect(eq(ptr, (void *)ustate.error, 0), "ustate should have no error"); - cr_expect(eq(sz, ustate.bitPos0b, 24), "ustate should advance 24 bits"); + cr_expect(eq(ptr, (void *)ustate.pu.error, 0), "ustate should have no error"); + cr_expect(eq(sz, ustate.pu.bitPos0b, 24), "ustate should advance 24 bits"); cr_expect(eq(u8, ustate.unwritBits, 0), "ustate should hold nothing"); cr_expect(eq(u8, ustate.numUnwritBits, 0), "ustate should be empty"); cr_expect(eq(sz, size, 3), "stream should hold 3 bytes"); @@ -53,14 +54,14 @@ Test(bits, be_bool_24) // Reopen stream for reading from same dynamic buffer fclose(stream); stream = fmemopen(buffer, size, "r"); - PState pstate = {stream, 0, NULL, NULL, 0, 0}; + PState pstate = {{stream, 0, NULL, NULL}, 0, 0}; // Verify that pstate reads true_rep correctly bool number = false; parse_be_bool(&number, 24, true24_rep, false_rep, &pstate); cr_expect(eq(int, number, true), "boolean number should be true"); - cr_expect(eq(ptr, (void *)pstate.error, 0), "pstate should have no error"); - cr_expect(eq(sz, pstate.bitPos0b, 24), "pstate should advance 24 bits"); + cr_expect(eq(ptr, (void *)pstate.pu.error, 0), "pstate should have no error"); + cr_expect(eq(sz, pstate.pu.bitPos0b, 24), "pstate should advance 24 bits"); cr_expect(eq(u8, pstate.unreadBits, 0), "pstate should hold nothing"); cr_expect(eq(u8, pstate.numUnreadBits, 0), "pstate should be empty"); @@ -72,25 +73,25 @@ Test(bits, be_bool_24) Test(bits, be_bool_4) { // Open memory stream for writing to dynamic buffer - char * buffer = NULL; - size_t size = 0; - FILE * stream = open_memstream(&buffer, &size); - UState ustate = {stream, 0, NULL, NULL, 0, 0}; + char *buffer = NULL; + size_t size = 0; + FILE *stream = open_memstream(&buffer, &size); + UState ustate = {{stream, 0, NULL, NULL}, 0, 0}; uint32_t true4_rep = 012; // 0b_1_010 uint32_t false_rep = 015; // 0b_1_101 // Verify that ustate writes 10101101 unparse_be_bool(true, 4, true4_rep, false_rep, &ustate); fflush(stream); - cr_expect(eq(ptr, (void *)ustate.error, 0), "ustate should have no error"); - cr_expect(eq(sz, ustate.bitPos0b, 4), "ustate should advance 4 bits"); + cr_expect(eq(ptr, (void *)ustate.pu.error, 0), "ustate should have no error"); + cr_expect(eq(sz, ustate.pu.bitPos0b, 4), "ustate should advance 4 bits"); cr_expect(eq(u8, ustate.unwritBits, 012), "ustate should hold 0b_1_010"); cr_expect(eq(u8, ustate.numUnwritBits, 4), "ustate should buffer 4 bits"); cr_expect(eq(sz, size, 0), "stream should be empty"); unparse_be_bool(false, 4, true4_rep, false_rep, &ustate); fflush(stream); - cr_expect(eq(ptr, (void *)ustate.error, 0), "ustate should have no error"); - cr_expect(eq(sz, ustate.bitPos0b, 8), "ustate should advance 4 bits"); + cr_expect(eq(ptr, (void *)ustate.pu.error, 0), "ustate should have no error"); + cr_expect(eq(sz, ustate.pu.bitPos0b, 8), "ustate should advance 4 bits"); cr_expect(eq(u8, ustate.unwritBits, 0255), "ustate should hold 0b_10_101_101"); cr_expect(eq(u8, ustate.numUnwritBits, 0), "ustate should be empty"); cr_expect(eq(sz, size, 1), "stream should have 1 byte"); @@ -99,21 +100,21 @@ Test(bits, be_bool_4) // Reopen stream for reading from same dynamic buffer fclose(stream); stream = fmemopen(buffer, size, "r"); - PState pstate = {stream, 0, NULL, NULL, 0, 0}; + PState pstate = {{stream, 0, NULL, NULL}, 0, 0}; // Verify that pstate reads true4_rep, false_rep correctly bool number = false; parse_be_bool(&number, 4, true4_rep, false_rep, &pstate); cr_expect(eq(int, number, true), "boolean number should be true"); - cr_expect(eq(ptr, (void *)pstate.error, 0), "pstate should have no error"); - cr_expect(eq(sz, pstate.bitPos0b, 4), "pstate should advance 4 bits"); + cr_expect(eq(ptr, (void *)pstate.pu.error, 0), "pstate should have no error"); + cr_expect(eq(sz, pstate.pu.bitPos0b, 4), "pstate should advance 4 bits"); cr_expect(eq(u8, pstate.unreadBits, 0255), "pstate should hold 0b_10_101_101"); cr_expect(eq(u8, pstate.numUnreadBits, 4), "pstate should buffer 4 bits"); number = false; parse_be_bool(&number, 4, true4_rep, false_rep, &pstate); cr_expect(eq(int, number, false), "boolean number should be false"); - cr_expect(eq(ptr, (void *)pstate.error, 0), "pstate should have no error"); - cr_expect(eq(sz, pstate.bitPos0b, 8), "pstate should advance 4 bits"); + cr_expect(eq(ptr, (void *)pstate.pu.error, 0), "pstate should have no error"); + cr_expect(eq(sz, pstate.pu.bitPos0b, 8), "pstate should advance 4 bits"); cr_expect(eq(u8, pstate.unreadBits, 015), "pstate should hold 0b_1_101"); cr_expect(eq(u8, pstate.numUnreadBits, 0), "pstate should be empty"); @@ -125,25 +126,25 @@ Test(bits, be_bool_4) Test(bits, be_bool_7_7) { // Open memory stream for writing to dynamic buffer - char * buffer = NULL; - size_t size = 0; - FILE * stream = open_memstream(&buffer, &size); - UState ustate = {stream, 0, NULL, NULL, 0, 0}; + char *buffer = NULL; + size_t size = 0; + FILE *stream = open_memstream(&buffer, &size); + UState ustate = {{stream, 0, NULL, NULL}, 0, 0}; uint32_t true7_rep = 0146; // 0b_1_100_110 uint32_t false_rep = 0157; // 0b_1_101_111 // Verify that ustate writes 11001101 unparse_be_bool(true, 7, true7_rep, false_rep, &ustate); fflush(stream); - cr_expect(eq(ptr, (void *)ustate.error, 0), "ustate should have no error"); - cr_expect(eq(sz, ustate.bitPos0b, 7), "ustate should advance 7 bits"); + cr_expect(eq(ptr, (void *)ustate.pu.error, 0), "ustate should have no error"); + cr_expect(eq(sz, ustate.pu.bitPos0b, 7), "ustate should advance 7 bits"); cr_expect(eq(u8, ustate.unwritBits, 0146), "ustate should hold 0b_01_100_110"); cr_expect(eq(u8, ustate.numUnwritBits, 7), "ustate should buffer 7 bits"); cr_expect(eq(sz, size, 0), "stream should be empty"); unparse_be_bool(false, 7, true7_rep, false_rep, &ustate); fflush(stream); - cr_expect(eq(ptr, (void *)ustate.error, 0), "ustate should have no error"); - cr_expect(eq(sz, ustate.bitPos0b, 14), "ustate should advance 7 bits"); + cr_expect(eq(ptr, (void *)ustate.pu.error, 0), "ustate should have no error"); + cr_expect(eq(sz, ustate.pu.bitPos0b, 14), "ustate should advance 7 bits"); cr_expect(eq(u8, ustate.unwritBits, 0157), "ustate should hold 0b_01_101_111"); cr_expect(eq(u8, ustate.numUnwritBits, 6), "ustate should buffer 6 bits"); cr_expect(eq(sz, size, 1), "stream should have 1 byte"); @@ -153,8 +154,8 @@ Test(bits, be_bool_7_7) const uint8_t fill_byte = '\0'; flush_fragment_byte(fill_byte, &ustate); fflush(stream); - cr_expect(eq(ptr, (void *)ustate.error, 0), "ustate should have no error"); - cr_expect(eq(sz, ustate.bitPos0b, 20), "ustate should advance 6 bits"); + cr_expect(eq(ptr, (void *)ustate.pu.error, 0), "ustate should have no error"); + cr_expect(eq(sz, ustate.pu.bitPos0b, 20), "ustate should advance 6 bits"); cr_expect(eq(u8, ustate.unwritBits, 0274), "ustate should hold 0b_10_111_100"); cr_expect(eq(u8, ustate.numUnwritBits, 0), "ustate should be empty"); cr_expect(eq(sz, size, 2), "stream should have 2 bytes"); @@ -163,21 +164,21 @@ Test(bits, be_bool_7_7) // Reopen stream for reading from same dynamic buffer fclose(stream); stream = fmemopen(buffer, size, "r"); - PState pstate = {stream, 0, NULL, NULL, 0, 0}; + PState pstate = {{stream, 0, NULL, NULL}, 0, 0}; // Verify that pstate reads true7_rep, false_rep correctly bool number = false; parse_be_bool(&number, 7, true7_rep, false_rep, &pstate); cr_expect(eq(int, number, true), "boolean number should be true"); - cr_expect(eq(ptr, (void *)pstate.error, 0), "pstate should have no error"); - cr_expect(eq(sz, pstate.bitPos0b, 7), "pstate should advance 7 bits"); + cr_expect(eq(ptr, (void *)pstate.pu.error, 0), "pstate should have no error"); + cr_expect(eq(sz, pstate.pu.bitPos0b, 7), "pstate should advance 7 bits"); cr_expect(eq(u8, pstate.unreadBits, 0315), "pstate should hold 0b_11_001_101"); cr_expect(eq(u8, pstate.numUnreadBits, 1), "pstate should buffer 1 bit"); number = false; parse_be_bool(&number, 7, true7_rep, false_rep, &pstate); cr_expect(eq(int, number, false), "boolean number should be false"); - cr_expect(eq(ptr, (void *)pstate.error, 0), "pstate should have no error"); - cr_expect(eq(sz, pstate.bitPos0b, 14), "pstate should advance 7 bits"); + cr_expect(eq(ptr, (void *)pstate.pu.error, 0), "pstate should have no error"); + cr_expect(eq(sz, pstate.pu.bitPos0b, 14), "pstate should advance 7 bits"); cr_expect(eq(u8, pstate.unreadBits, 0274), "pstate should hold 0b_10_111_100"); cr_expect(eq(u8, pstate.numUnreadBits, 2), "pstate should buffer 2 bits"); @@ -189,10 +190,10 @@ Test(bits, be_bool_7_7) Test(bits, be_bool_9_7) { // Open memory stream for writing to dynamic buffer - char * buffer = NULL; - size_t size = 0; - FILE * stream = open_memstream(&buffer, &size); - UState ustate = {stream, 0, NULL, NULL, 0, 0}; + char *buffer = NULL; + size_t size = 0; + FILE *stream = open_memstream(&buffer, &size); + UState ustate = {{stream, 0, NULL, NULL}, 0, 0}; uint32_t true9_rep = 0747; // 0b_111_100_111 uint32_t true7_rep = 0167; // 0b_1_110_111 uint32_t false_rep = 0; @@ -200,16 +201,16 @@ Test(bits, be_bool_9_7) // Verify that ustate writes 11110011,1 1110111 unparse_be_bool(true, 9, true9_rep, false_rep, &ustate); fflush(stream); - cr_expect(eq(ptr, (void *)ustate.error, 0), "ustate should have no error"); - cr_expect(eq(sz, ustate.bitPos0b, 9), "ustate should advance 9 bits"); + cr_expect(eq(ptr, (void *)ustate.pu.error, 0), "ustate should have no error"); + cr_expect(eq(sz, ustate.pu.bitPos0b, 9), "ustate should advance 9 bits"); cr_expect(eq(u8, ustate.unwritBits, 01), "ustate should hold 0b_00_000_001"); cr_expect(eq(u8, ustate.numUnwritBits, 1), "ustate should buffer 1 bit"); cr_expect(eq(sz, size, 1), "stream should hold 1 byte"); cr_expect(eq(u8, buffer[0], 0363), "stream should hold 0b_11_110_011"); unparse_be_bool(true, 7, true7_rep, false_rep, &ustate); fflush(stream); - cr_expect(eq(ptr, (void *)ustate.error, 0), "ustate should have no error"); - cr_expect(eq(sz, ustate.bitPos0b, 16), "ustate should advance 7 bits"); + cr_expect(eq(ptr, (void *)ustate.pu.error, 0), "ustate should have no error"); + cr_expect(eq(sz, ustate.pu.bitPos0b, 16), "ustate should advance 7 bits"); cr_expect(eq(u8, ustate.unwritBits, 0367), "ustate should hold 0b_11_110_111"); cr_expect(eq(u8, ustate.numUnwritBits, 0), "ustate should be empty"); cr_expect(eq(sz, size, 2), "stream should hold 2 bytes"); @@ -218,21 +219,21 @@ Test(bits, be_bool_9_7) // Reopen stream for reading from same dynamic buffer fclose(stream); stream = fmemopen(buffer, size, "r"); - PState pstate = {stream, 0, NULL, NULL, 0, 0}; + PState pstate = {{stream, 0, NULL, NULL}, 0, 0}; // Verify that pstate reads true9_rep, true7_rep correctly bool number = false; parse_be_bool(&number, 9, true9_rep, false_rep, &pstate); cr_expect(eq(int, number, true), "boolean number should be true"); - cr_expect(eq(ptr, (void *)pstate.error, 0), "pstate should have no error"); - cr_expect(eq(sz, pstate.bitPos0b, 9), "pstate should advance 9 bits"); + cr_expect(eq(ptr, (void *)pstate.pu.error, 0), "pstate should have no error"); + cr_expect(eq(sz, pstate.pu.bitPos0b, 9), "pstate should advance 9 bits"); cr_expect(eq(u8, pstate.unreadBits, 0367), "pstate should hold 0b_11_110_111"); cr_expect(eq(u8, pstate.numUnreadBits, 7), "pstate should buffer 7 bits"); number = false; parse_be_bool(&number, 7, true7_rep, false_rep, &pstate); cr_expect(eq(int, number, true), "boolean number should be true"); - cr_expect(eq(ptr, (void *)pstate.error, 0), "pstate should have no error"); - cr_expect(eq(sz, pstate.bitPos0b, 16), "pstate should advance 7 bits"); + cr_expect(eq(ptr, (void *)pstate.pu.error, 0), "pstate should have no error"); + cr_expect(eq(sz, pstate.pu.bitPos0b, 16), "pstate should advance 7 bits"); cr_expect(eq(u8, pstate.unreadBits, 0167), "pstate should hold 0b_01_110_111"); cr_expect(eq(u8, pstate.numUnreadBits, 0), "pstate should be empty"); @@ -244,14 +245,14 @@ Test(bits, be_bool_9_7) Test(bits, be_signed_integers) { // Open memory stream for writing to dynamic buffer - char * buffer = NULL; + char *buffer = NULL; size_t size = 0; - FILE * stream = open_memstream(&buffer, &size); - UState ustate = {stream, 0, NULL, NULL, 0, 0}; + FILE *stream = open_memstream(&buffer, &size); + UState ustate = {{stream, 0, NULL, NULL}, 0, 0}; // Write 8 integers to the buffer - const int8_t int1 = -1; // a signed 1-bit integer can only be 0 or -1 - const int8_t int7 = 7; + const int8_t int1 = -1; // a signed 1-bit integer can only be 0 or -1 + const int8_t int7 = 7; const int16_t int9 = -9; const int16_t int15 = 15; const int32_t int17 = -17; @@ -269,19 +270,19 @@ Test(bits, be_signed_integers) fflush(stream); // Verify that these 8 integers were packed into 176 bits as expected - cr_expect(eq(ptr, (void *)ustate.error, 0), "ustate should have no error"); - cr_expect(eq(sz, ustate.bitPos0b, 176), "ustate should advance 176 bits"); + cr_expect(eq(ptr, (void *)ustate.pu.error, 0), "ustate should have no error"); + cr_expect(eq(sz, ustate.pu.bitPos0b, 176), "ustate should advance 176 bits"); cr_expect(eq(sz, size, 22), "stream should have 22 bytes"); cr_expect(eq(u8, ustate.numUnwritBits, 0), "ustate should be empty"); // Reopen stream for reading from same dynamic buffer fclose(stream); stream = fmemopen(buffer, size, "r"); - PState pstate = {stream, 0, NULL, NULL, 0, 0}; + PState pstate = {{stream, 0, NULL, NULL}, 0, 0}; // Read 8 integers from the buffer - int8_t tin1 = 0; - int8_t tin7 = 0; + int8_t tin1 = 0; + int8_t tin7 = 0; int16_t tin9 = 0; int16_t tin15 = 0; int32_t tin17 = 0; @@ -296,8 +297,8 @@ Test(bits, be_signed_integers) parse_be_int32(&tin31, 31, &pstate); parse_be_int64(&tin33, 33, &pstate); parse_be_int64(&tin63, 63, &pstate); - cr_expect(eq(ptr, (void *)pstate.error, 0), "pstate should have no error"); - cr_expect(eq(sz, pstate.bitPos0b, 176), "pstate should advance 176 bits"); + cr_expect(eq(ptr, (void *)pstate.pu.error, 0), "pstate should have no error"); + cr_expect(eq(sz, pstate.pu.bitPos0b, 176), "pstate should advance 176 bits"); cr_expect(eq(u8, pstate.numUnreadBits, 0), "pstate should be empty"); // Verify that these 8 integers are the same integers originally written @@ -318,14 +319,14 @@ Test(bits, be_signed_integers) Test(bits, be_unsigned_integers) { // Open memory stream for writing to dynamic buffer - char * buffer = NULL; + char *buffer = NULL; size_t size = 0; - FILE * stream = open_memstream(&buffer, &size); - UState ustate = {stream, 0, NULL, NULL, 0, 0}; + FILE *stream = open_memstream(&buffer, &size); + UState ustate = {{stream, 0, NULL, NULL}, 0, 0}; // Write 8 integers to the buffer - const uint8_t uint1 = 1; // an unsigned 1-bit integer can only be 0 or 1 - const uint8_t uint7 = 7; + const uint8_t uint1 = 1; // an unsigned 1-bit integer can only be 0 or 1 + const uint8_t uint7 = 7; const uint16_t uint9 = 9; const uint16_t uint15 = 15; const uint32_t uint17 = 17; @@ -343,19 +344,19 @@ Test(bits, be_unsigned_integers) fflush(stream); // Verify that these 8 integers were packed into 176 bits as expected - cr_expect(eq(ptr, (void *)ustate.error, 0), "ustate should have no error"); - cr_expect(eq(sz, ustate.bitPos0b, 176), "ustate should advance 176 bits"); + cr_expect(eq(ptr, (void *)ustate.pu.error, 0), "ustate should have no error"); + cr_expect(eq(sz, ustate.pu.bitPos0b, 176), "ustate should advance 176 bits"); cr_expect(eq(sz, size, 22), "stream should have 22 bytes"); cr_expect(eq(u8, ustate.numUnwritBits, 0), "ustate should be empty"); // Reopen stream for reading from same dynamic buffer fclose(stream); stream = fmemopen(buffer, size, "r"); - PState pstate = {stream, 0, NULL, NULL, 0, 0}; + PState pstate = {{stream, 0, NULL, NULL}, 0, 0}; // Read 8 integers from the buffer - uint8_t tniu1 = 0; - uint8_t tniu7 = 0; + uint8_t tniu1 = 0; + uint8_t tniu7 = 0; uint16_t tniu9 = 0; uint16_t tniu15 = 0; uint32_t tniu17 = 0; @@ -370,8 +371,8 @@ Test(bits, be_unsigned_integers) parse_be_uint32(&tniu31, 31, &pstate); parse_be_uint64(&tniu33, 33, &pstate); parse_be_uint64(&tniu63, 63, &pstate); - cr_expect(eq(ptr, (void *)pstate.error, 0), "pstate should have no error"); - cr_expect(eq(sz, pstate.bitPos0b, 176), "pstate should advance 176 bits"); + cr_expect(eq(ptr, (void *)pstate.pu.error, 0), "pstate should have no error"); + cr_expect(eq(sz, pstate.pu.bitPos0b, 176), "pstate should advance 176 bits"); cr_expect(eq(u8, pstate.numUnreadBits, 0), "pstate should be empty"); // Verify that these 8 integers are the same integers originally written @@ -392,18 +393,18 @@ Test(bits, be_unsigned_integers) Test(bits, le_bool_24) { // Open memory stream for writing to dynamic buffer - char * buffer = NULL; - size_t size = 0; - FILE * stream = open_memstream(&buffer, &size); - UState ustate = {stream, 0, NULL, NULL, 0, 0}; + char *buffer = NULL; + size_t size = 0; + FILE *stream = open_memstream(&buffer, &size); + UState ustate = {{stream, 0, NULL, NULL}, 0, 0}; uint32_t true24_rep = 076543210; // 0b_111_110_101_100_011_010_001_000 uint32_t false_rep = 0; // Verify that ustate writes 10001000, 11000110, 11111010 unparse_le_bool(true, 24, true24_rep, false_rep, &ustate); fflush(stream); - cr_expect(eq(ptr, (void *)ustate.error, 0), "ustate should have no error"); - cr_expect(eq(sz, ustate.bitPos0b, 24), "ustate should advance 24 bits"); + cr_expect(eq(ptr, (void *)ustate.pu.error, 0), "ustate should have no error"); + cr_expect(eq(sz, ustate.pu.bitPos0b, 24), "ustate should advance 24 bits"); cr_expect(eq(u8, ustate.unwritBits, 0), "ustate should hold nothing"); cr_expect(eq(u8, ustate.numUnwritBits, 0), "ustate should be empty"); cr_expect(eq(sz, size, 3), "stream should hold 3 bytes"); @@ -414,14 +415,14 @@ Test(bits, le_bool_24) // Reopen stream for reading from same dynamic buffer fclose(stream); stream = fmemopen(buffer, size, "r"); - PState pstate = {stream, 0, NULL, NULL, 0, 0}; + PState pstate = {{stream, 0, NULL, NULL}, 0, 0}; // Verify that pstate reads 11111010, 11000100, 11001000 bool number = false; parse_le_bool(&number, 24, true24_rep, false_rep, &pstate); cr_expect(eq(int, number, true), "boolean number should be true"); - cr_expect(eq(ptr, (void *)pstate.error, 0), "pstate should have no error"); - cr_expect(eq(sz, pstate.bitPos0b, 24), "pstate should advance 24 bits"); + cr_expect(eq(ptr, (void *)pstate.pu.error, 0), "pstate should have no error"); + cr_expect(eq(sz, pstate.pu.bitPos0b, 24), "pstate should advance 24 bits"); cr_expect(eq(u8, pstate.unreadBits, 0), "pstate should hold nothing"); cr_expect(eq(u8, pstate.numUnreadBits, 0), "pstate should be empty"); @@ -433,25 +434,25 @@ Test(bits, le_bool_24) Test(bits, le_bool_4_4) { // Open memory stream for writing to dynamic buffer - char * buffer = NULL; - size_t size = 0; - FILE * stream = open_memstream(&buffer, &size); - UState ustate = {stream, 0, NULL, NULL, 0, 0}; + char *buffer = NULL; + size_t size = 0; + FILE *stream = open_memstream(&buffer, &size); + UState ustate = {{stream, 0, NULL, NULL}, 0, 0}; uint32_t true4_rep = 012; // 0b_1_010 uint32_t false_rep = 015; // 0b_1_101 // Verify that ustate writes 10101101 unparse_le_bool(true, 4, true4_rep, false_rep, &ustate); fflush(stream); - cr_expect(eq(ptr, (void *)ustate.error, 0), "ustate should have no error"); - cr_expect(eq(sz, ustate.bitPos0b, 4), "ustate should advance 4 bits"); + cr_expect(eq(ptr, (void *)ustate.pu.error, 0), "ustate should have no error"); + cr_expect(eq(sz, ustate.pu.bitPos0b, 4), "ustate should advance 4 bits"); cr_expect(eq(u8, ustate.unwritBits, 012), "ustate should hold 0b_1_010"); cr_expect(eq(u8, ustate.numUnwritBits, 4), "ustate should buffer 4 bits"); cr_expect(eq(sz, size, 0), "stream should be empty"); unparse_le_bool(false, 4, true4_rep, false_rep, &ustate); fflush(stream); - cr_expect(eq(ptr, (void *)ustate.error, 0), "ustate should have no error"); - cr_expect(eq(sz, ustate.bitPos0b, 8), "ustate should advance 4 bits"); + cr_expect(eq(ptr, (void *)ustate.pu.error, 0), "ustate should have no error"); + cr_expect(eq(sz, ustate.pu.bitPos0b, 8), "ustate should advance 4 bits"); cr_expect(eq(u8, ustate.unwritBits, 0255), "ustate should hold 0b_10_101_101"); cr_expect(eq(u8, ustate.numUnwritBits, 0), "ustate should be empty"); cr_expect(eq(sz, size, 1), "stream should have 1 byte"); @@ -460,21 +461,21 @@ Test(bits, le_bool_4_4) // Reopen stream for reading from same dynamic buffer fclose(stream); stream = fmemopen(buffer, size, "r"); - PState pstate = {stream, 0, NULL, NULL, 0, 0}; + PState pstate = {{stream, 0, NULL, NULL}, 0, 0}; // Verify that pstate reads true4_rep, false_rep correctly bool number = false; parse_le_bool(&number, 4, true4_rep, false_rep, &pstate); cr_expect(eq(int, number, true), "boolean number should be true"); - cr_expect(eq(ptr, (void *)pstate.error, 0), "pstate should have no error"); - cr_expect(eq(sz, pstate.bitPos0b, 4), "pstate should advance 4 bits"); + cr_expect(eq(ptr, (void *)pstate.pu.error, 0), "pstate should have no error"); + cr_expect(eq(sz, pstate.pu.bitPos0b, 4), "pstate should advance 4 bits"); cr_expect(eq(u8, pstate.unreadBits, 0255), "pstate should hold 0b_10_101_101"); cr_expect(eq(u8, pstate.numUnreadBits, 4), "pstate should buffer 4 bits"); number = false; parse_le_bool(&number, 4, true4_rep, false_rep, &pstate); cr_expect(eq(int, number, false), "boolean number should be false"); - cr_expect(eq(ptr, (void *)pstate.error, 0), "pstate should have no error"); - cr_expect(eq(sz, pstate.bitPos0b, 8), "pstate should advance 4 bits"); + cr_expect(eq(ptr, (void *)pstate.pu.error, 0), "pstate should have no error"); + cr_expect(eq(sz, pstate.pu.bitPos0b, 8), "pstate should advance 4 bits"); cr_expect(eq(u8, pstate.unreadBits, 015), "pstate should hold 0b_1_101"); cr_expect(eq(u8, pstate.numUnreadBits, 0), "pstate should be empty"); @@ -486,25 +487,25 @@ Test(bits, le_bool_4_4) Test(bits, le_bool_7_7) { // Open memory stream for writing to dynamic buffer - char * buffer = NULL; - size_t size = 0; - FILE * stream = open_memstream(&buffer, &size); - UState ustate = {stream, 0, NULL, NULL, 0, 0}; + char *buffer = NULL; + size_t size = 0; + FILE *stream = open_memstream(&buffer, &size); + UState ustate = {{stream, 0, NULL, NULL}, 0, 0}; uint32_t true7_rep = 0146; // 0b_1_100_110 uint32_t false_rep = 0157; // 0b_1_101_111 // Verify that ustate writes 11001101 unparse_le_bool(true, 7, true7_rep, false_rep, &ustate); fflush(stream); - cr_expect(eq(ptr, (void *)ustate.error, 0), "ustate should have no error"); - cr_expect(eq(sz, ustate.bitPos0b, 7), "ustate should advance 7 bits"); + cr_expect(eq(ptr, (void *)ustate.pu.error, 0), "ustate should have no error"); + cr_expect(eq(sz, ustate.pu.bitPos0b, 7), "ustate should advance 7 bits"); cr_expect(eq(u8, ustate.unwritBits, 0146), "ustate should hold 0b_01_100_110"); cr_expect(eq(u8, ustate.numUnwritBits, 7), "ustate should buffer 7 bits"); cr_expect(eq(sz, size, 0), "stream should be empty"); unparse_le_bool(false, 7, true7_rep, false_rep, &ustate); fflush(stream); - cr_expect(eq(ptr, (void *)ustate.error, 0), "ustate should have no error"); - cr_expect(eq(sz, ustate.bitPos0b, 14), "ustate should advance 7 bits"); + cr_expect(eq(ptr, (void *)ustate.pu.error, 0), "ustate should have no error"); + cr_expect(eq(sz, ustate.pu.bitPos0b, 14), "ustate should advance 7 bits"); cr_expect(eq(u8, ustate.unwritBits, 0157), "ustate should hold 0b_01_101_111"); cr_expect(eq(u8, ustate.numUnwritBits, 6), "ustate should buffer 6 bits"); cr_expect(eq(sz, size, 1), "stream should have 1 byte"); @@ -514,8 +515,8 @@ Test(bits, le_bool_7_7) const uint8_t fill_byte = '\0'; flush_fragment_byte(fill_byte, &ustate); fflush(stream); - cr_expect(eq(ptr, (void *)ustate.error, 0), "ustate should have no error"); - cr_expect(eq(sz, ustate.bitPos0b, 20), "ustate should advance 6 bits"); + cr_expect(eq(ptr, (void *)ustate.pu.error, 0), "ustate should have no error"); + cr_expect(eq(sz, ustate.pu.bitPos0b, 20), "ustate should advance 6 bits"); cr_expect(eq(u8, ustate.unwritBits, 0274), "ustate should hold 0b_10_111_100"); cr_expect(eq(u8, ustate.numUnwritBits, 0), "ustate should be empty"); cr_expect(eq(sz, size, 2), "stream should have 2 bytes"); @@ -524,21 +525,21 @@ Test(bits, le_bool_7_7) // Reopen stream for reading from same dynamic buffer fclose(stream); stream = fmemopen(buffer, size, "r"); - PState pstate = {stream, 0, NULL, NULL, 0, 0}; + PState pstate = {{stream, 0, NULL, NULL}, 0, 0}; // Verify that pstate reads true7_rep, false_rep correctly bool number = false; parse_le_bool(&number, 7, true7_rep, false_rep, &pstate); cr_expect(eq(int, number, true), "boolean number should be true"); - cr_expect(eq(ptr, (void *)pstate.error, 0), "pstate should have no error"); - cr_expect(eq(sz, pstate.bitPos0b, 7), "pstate should advance 7 bits"); + cr_expect(eq(ptr, (void *)pstate.pu.error, 0), "pstate should have no error"); + cr_expect(eq(sz, pstate.pu.bitPos0b, 7), "pstate should advance 7 bits"); cr_expect(eq(u8, pstate.unreadBits, 0315), "pstate should hold 0b_11_001_101"); cr_expect(eq(u8, pstate.numUnreadBits, 1), "pstate should buffer 1 bit"); number = false; parse_le_bool(&number, 7, true7_rep, false_rep, &pstate); cr_expect(eq(int, number, false), "boolean number should be false"); - cr_expect(eq(ptr, (void *)pstate.error, 0), "pstate should have no error"); - cr_expect(eq(sz, pstate.bitPos0b, 14), "pstate should advance 7 bits"); + cr_expect(eq(ptr, (void *)pstate.pu.error, 0), "pstate should have no error"); + cr_expect(eq(sz, pstate.pu.bitPos0b, 14), "pstate should advance 7 bits"); cr_expect(eq(u8, pstate.unreadBits, 0274), "pstate should hold 0b_10_111_100"); cr_expect(eq(u8, pstate.numUnreadBits, 2), "pstate should buffer 2 bits"); @@ -550,10 +551,10 @@ Test(bits, le_bool_7_7) Test(bits, le_bool_9_7) { // Open memory stream for writing to dynamic buffer - char * buffer = NULL; - size_t size = 0; - FILE * stream = open_memstream(&buffer, &size); - UState ustate = {stream, 0, NULL, NULL, 0, 0}; + char *buffer = NULL; + size_t size = 0; + FILE *stream = open_memstream(&buffer, &size); + UState ustate = {{stream, 0, NULL, NULL}, 0, 0}; uint32_t true9_rep = 0747; // 0b_111_100_111 uint32_t true7_rep = 0167; // 0b_1_110_111 uint32_t false_rep = 0; @@ -561,16 +562,16 @@ Test(bits, le_bool_9_7) // Verify that ustate writes 11100111,1 1110111 unparse_le_bool(true, 9, true9_rep, false_rep, &ustate); fflush(stream); - cr_expect(eq(ptr, (void *)ustate.error, 0), "ustate should have no error"); - cr_expect(eq(sz, ustate.bitPos0b, 9), "ustate should advance 9 bits"); + cr_expect(eq(ptr, (void *)ustate.pu.error, 0), "ustate should have no error"); + cr_expect(eq(sz, ustate.pu.bitPos0b, 9), "ustate should advance 9 bits"); cr_expect(eq(u8, ustate.unwritBits, 01), "ustate should hold 0b_00_000_001"); cr_expect(eq(u8, ustate.numUnwritBits, 1), "ustate should buffer 1 bit"); cr_expect(eq(sz, size, 1), "stream should hold 1 byte"); cr_expect(eq(u8, buffer[0], 0347), "stream should hold 0b_11_100_111"); unparse_le_bool(true, 7, true7_rep, false_rep, &ustate); fflush(stream); - cr_expect(eq(ptr, (void *)ustate.error, 0), "ustate should have no error"); - cr_expect(eq(sz, ustate.bitPos0b, 16), "ustate should advance 7 bits"); + cr_expect(eq(ptr, (void *)ustate.pu.error, 0), "ustate should have no error"); + cr_expect(eq(sz, ustate.pu.bitPos0b, 16), "ustate should advance 7 bits"); cr_expect(eq(u8, ustate.unwritBits, 0367), "ustate should hold 0b_11_110_111"); cr_expect(eq(u8, ustate.numUnwritBits, 0), "ustate should be empty"); cr_expect(eq(sz, size, 2), "stream should hold 2 bytes"); @@ -579,21 +580,21 @@ Test(bits, le_bool_9_7) // Reopen stream for reading from same dynamic buffer fclose(stream); stream = fmemopen(buffer, size, "r"); - PState pstate = {stream, 0, NULL, NULL, 0, 0}; + PState pstate = {{stream, 0, NULL, NULL}, 0, 0}; // Verify that pstate reads true9_rep, true7_rep correctly bool number = false; parse_le_bool(&number, 9, true9_rep, false_rep, &pstate); cr_expect(eq(int, number, true), "boolean number should be true"); - cr_expect(eq(ptr, (void *)pstate.error, 0), "pstate should have no error"); - cr_expect(eq(sz, pstate.bitPos0b, 9), "pstate should advance 9 bits"); + cr_expect(eq(ptr, (void *)pstate.pu.error, 0), "pstate should have no error"); + cr_expect(eq(sz, pstate.pu.bitPos0b, 9), "pstate should advance 9 bits"); cr_expect(eq(u8, pstate.unreadBits, 0367), "pstate should hold 0b_11_110_111"); cr_expect(eq(u8, pstate.numUnreadBits, 7), "pstate should buffer 7 bits"); number = false; parse_le_bool(&number, 7, true7_rep, false_rep, &pstate); cr_expect(eq(int, number, true), "boolean number should be true"); - cr_expect(eq(ptr, (void *)pstate.error, 0), "pstate should have no error"); - cr_expect(eq(sz, pstate.bitPos0b, 16), "pstate should advance 7 bits"); + cr_expect(eq(ptr, (void *)pstate.pu.error, 0), "pstate should have no error"); + cr_expect(eq(sz, pstate.pu.bitPos0b, 16), "pstate should advance 7 bits"); cr_expect(eq(u8, pstate.unreadBits, 0167), "pstate should hold 0b_01_110_111"); cr_expect(eq(u8, pstate.numUnreadBits, 0), "pstate should be empty"); @@ -605,14 +606,14 @@ Test(bits, le_bool_9_7) Test(bits, le_signed_integers) { // Open memory stream for writing to dynamic buffer - char * buffer = NULL; + char *buffer = NULL; size_t size = 0; - FILE * stream = open_memstream(&buffer, &size); - UState ustate = {stream, 0, NULL, NULL, 0, 0}; + FILE *stream = open_memstream(&buffer, &size); + UState ustate = {{stream, 0, NULL, NULL}, 0, 0}; // Write 8 integers to the buffer - const int8_t int1 = -1; // a signed 1-bit integer can only be 0 or -1 - const int8_t int7 = 7; + const int8_t int1 = -1; // a signed 1-bit integer can only be 0 or -1 + const int8_t int7 = 7; const int16_t int9 = -9; const int16_t int15 = 15; const int32_t int17 = -17; @@ -630,19 +631,19 @@ Test(bits, le_signed_integers) fflush(stream); // Verify that these 8 integers were packed into 176 bits as expected - cr_expect(eq(ptr, (void *)ustate.error, 0), "ustate should have no error"); - cr_expect(eq(sz, ustate.bitPos0b, 176), "ustate should advance 176 bits"); + cr_expect(eq(ptr, (void *)ustate.pu.error, 0), "ustate should have no error"); + cr_expect(eq(sz, ustate.pu.bitPos0b, 176), "ustate should advance 176 bits"); cr_expect(eq(sz, size, 22), "stream should have 22 bytes"); cr_expect(eq(u8, ustate.numUnwritBits, 0), "ustate should be empty"); // Reopen stream for reading from same dynamic buffer fclose(stream); stream = fmemopen(buffer, size, "r"); - PState pstate = {stream, 0, NULL, NULL, 0, 0}; + PState pstate = {{stream, 0, NULL, NULL}, 0, 0}; // Read 8 integers from the buffer - int8_t tin1 = 0; - int8_t tin7 = 0; + int8_t tin1 = 0; + int8_t tin7 = 0; int16_t tin9 = 0; int16_t tin15 = 0; int32_t tin17 = 0; @@ -657,8 +658,8 @@ Test(bits, le_signed_integers) parse_le_int32(&tin31, 31, &pstate); parse_le_int64(&tin33, 33, &pstate); parse_le_int64(&tin63, 63, &pstate); - cr_expect(eq(ptr, (void *)pstate.error, 0), "pstate should have no error"); - cr_expect(eq(sz, pstate.bitPos0b, 176), "pstate should advance 176 bits"); + cr_expect(eq(ptr, (void *)pstate.pu.error, 0), "pstate should have no error"); + cr_expect(eq(sz, pstate.pu.bitPos0b, 176), "pstate should advance 176 bits"); cr_expect(eq(u8, pstate.numUnreadBits, 0), "pstate should be empty"); // Verify that these 8 integers are the same integers originally written @@ -679,14 +680,14 @@ Test(bits, le_signed_integers) Test(bits, le_unsigned_integers) { // Open memory stream for writing to dynamic buffer - char * buffer = NULL; + char *buffer = NULL; size_t size = 0; - FILE * stream = open_memstream(&buffer, &size); - UState ustate = {stream, 0, NULL, NULL, 0, 0}; + FILE *stream = open_memstream(&buffer, &size); + UState ustate = {{stream, 0, NULL, NULL}, 0, 0}; // Write 8 integers to the buffer - const uint8_t uint1 = 1; // an unsigned 1-bit integer can only be 0 or 1 - const uint8_t uint7 = 7; + const uint8_t uint1 = 1; // an unsigned 1-bit integer can only be 0 or 1 + const uint8_t uint7 = 7; const uint16_t uint9 = 9; const uint16_t uint15 = 15; const uint32_t uint17 = 17; @@ -704,19 +705,19 @@ Test(bits, le_unsigned_integers) fflush(stream); // Verify that these 8 integers were packed into 176 bits as expected - cr_expect(eq(ptr, (void *)ustate.error, 0), "ustate should have no error"); - cr_expect(eq(sz, ustate.bitPos0b, 176), "ustate should advance 176 bits"); + cr_expect(eq(ptr, (void *)ustate.pu.error, 0), "ustate should have no error"); + cr_expect(eq(sz, ustate.pu.bitPos0b, 176), "ustate should advance 176 bits"); cr_expect(eq(sz, size, 22), "stream should have 22 bytes"); cr_expect(eq(u8, ustate.numUnwritBits, 0), "ustate should be empty"); // Reopen stream for reading from same dynamic buffer fclose(stream); stream = fmemopen(buffer, size, "r"); - PState pstate = {stream, 0, NULL, NULL, 0, 0}; + PState pstate = {{stream, 0, NULL, NULL}, 0, 0}; // Read 8 integers from the buffer - uint8_t tniu1 = 0; - uint8_t tniu7 = 0; + uint8_t tniu1 = 0; + uint8_t tniu7 = 0; uint16_t tniu9 = 0; uint16_t tniu15 = 0; uint32_t tniu17 = 0; @@ -731,8 +732,8 @@ Test(bits, le_unsigned_integers) parse_le_uint32(&tniu31, 31, &pstate); parse_le_uint64(&tniu33, 33, &pstate); parse_le_uint64(&tniu63, 63, &pstate); - cr_expect(eq(ptr, (void *)pstate.error, 0), "pstate should have no error"); - cr_expect(eq(sz, pstate.bitPos0b, 176), "pstate should advance 176 bits"); + cr_expect(eq(ptr, (void *)pstate.pu.error, 0), "pstate should have no error"); + cr_expect(eq(sz, pstate.pu.bitPos0b, 176), "pstate should advance 176 bits"); cr_expect(eq(u8, pstate.numUnreadBits, 0), "pstate should be empty"); // Verify that these 8 integers are the same integers originally written diff --git a/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/tests/extras.c b/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/tests/extras.c index d7b69e689f..bf1b3abdd6 100644 --- a/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/tests/extras.c +++ b/daffodil-codegen-c/src/main/resources/org/apache/daffodil/codegen/c/files/tests/extras.c @@ -15,10 +15,11 @@ * limitations under the License. */ +// auto-maintained by iwyu // clang-format off #include // for bool #include // for NULL -#include "infoset.h" // for InfosetBase, get_infoset +#include "infoset.h" // for InfosetBase, UNUSED, get_infoset // clang-format on // Define `get_infoset' to avoid an undefined reference when linking. diff --git a/daffodil-codegen-c/src/main/scala/org/apache/daffodil/codegen/c/DaffodilCCodeGenerator.scala b/daffodil-codegen-c/src/main/scala/org/apache/daffodil/codegen/c/DaffodilCCodeGenerator.scala index fc92e11342..c5308172b0 100644 --- a/daffodil-codegen-c/src/main/scala/org/apache/daffodil/codegen/c/DaffodilCCodeGenerator.scala +++ b/daffodil-codegen-c/src/main/scala/org/apache/daffodil/codegen/c/DaffodilCCodeGenerator.scala @@ -35,6 +35,7 @@ import org.apache.daffodil.core.grammar.Gram import org.apache.daffodil.core.grammar.Prod import org.apache.daffodil.core.grammar.SeqComp import org.apache.daffodil.core.grammar.primitives.AlignmentFill +import org.apache.daffodil.core.grammar.primitives.AssertBooleanPrim import org.apache.daffodil.core.grammar.primitives.BinaryBoolean import org.apache.daffodil.core.grammar.primitives.BinaryDouble import org.apache.daffodil.core.grammar.primitives.BinaryFloat @@ -259,6 +260,7 @@ object DaffodilCCodeGenerator def generateCode(gram: Gram, cgState: CodeGeneratorState): Unit = { gram match { case g: AlignmentFill => alignmentFillGenerateCode(g, cgState) + case g: AssertBooleanPrim => noop(g) case g: BinaryBoolean => binaryBooleanGenerateCode(g.e, cgState) case g: BinaryDouble => binaryFloatGenerateCode(g.e, lengthInBits = 64, cgState) case g: BinaryFloat => binaryFloatGenerateCode(g.e, lengthInBits = 32, cgState) diff --git a/daffodil-codegen-c/src/main/scala/org/apache/daffodil/codegen/c/DaffodilCExamplesGenerator.scala b/daffodil-codegen-c/src/main/scala/org/apache/daffodil/codegen/c/DaffodilCExamplesGenerator.scala index 9ba25ef724..7ac70655ff 100644 --- a/daffodil-codegen-c/src/main/scala/org/apache/daffodil/codegen/c/DaffodilCExamplesGenerator.scala +++ b/daffodil-codegen-c/src/main/scala/org/apache/daffodil/codegen/c/DaffodilCExamplesGenerator.scala @@ -72,15 +72,16 @@ object DaffodilCExamplesGenerator { // Get paths to our sample schemas and their corresponding example directories val rootDir = if (os.exists(os.pwd / "src")) os.pwd / os.up else os.pwd - val schemaDir = rootDir / "daffodil-codegen-c" / "src" / "test" / "resources" / "org" / "apache" / "daffodil" / "codegen" / "c" val examplesDir = os.Path(args(0)) + + // Update each example of generated C code val examples = Array( (schemaDir / "ex_nums.dfdl.xsd", None, examplesDir / "ex_nums"), (schemaDir / "nested.dfdl.xsd", Some("NestedUnion"), examplesDir / "NestedUnion"), (schemaDir / "padtest.dfdl.xsd", None, examplesDir / "padtest"), - (schemaDir / "simple.dfdl.xsd", Some("simple-byte"), examplesDir / "simple"), + (schemaDir / "simple.dfdl.xsd", Some("simple"), examplesDir / "simple"), ( schemaDir / "variablelen.dfdl.xsd", Some("expressionElement"), diff --git a/daffodil-codegen-c/src/main/scala/org/apache/daffodil/codegen/c/generators/AlignmentFillCodeGenerator.scala b/daffodil-codegen-c/src/main/scala/org/apache/daffodil/codegen/c/generators/AlignmentFillCodeGenerator.scala index 0809994106..ac6f919af8 100644 --- a/daffodil-codegen-c/src/main/scala/org/apache/daffodil/codegen/c/generators/AlignmentFillCodeGenerator.scala +++ b/daffodil-codegen-c/src/main/scala/org/apache/daffodil/codegen/c/generators/AlignmentFillCodeGenerator.scala @@ -21,7 +21,8 @@ import org.apache.daffodil.core.grammar.primitives.AlignmentFill trait AlignmentFillCodeGenerator { - // Generate C code to parse/unparse fill bytes until an alignment boundary is reached + // Generate C code to parse/unparse alignment fill bytes until an + // alignment boundary is reached def alignmentFillGenerateCode(g: AlignmentFill, cgState: CodeGeneratorState): Unit = { val indent1 = if (cgState.hasChoice) INDENT else NO_INDENT val indent2 = if (cgState.hasArray) INDENT else NO_INDENT @@ -29,13 +30,13 @@ trait AlignmentFillCodeGenerator { val octalFillByte = g.e.fillByteEv.constValue.toByte.toOctalString val initERDStatement = "" val parseStatement = - s"""$indent1$indent2 // Fill to closest alignment - |$indent1$indent2 parse_align($alignmentInBits, pstate); - |$indent1$indent2 if (pstate->error) return;""".stripMargin + s"""$indent1$indent2 // Align to closest alignment + |$indent1$indent2 parse_align_to($alignmentInBits, pstate); + |$indent1$indent2 if (pstate->pu.error) return;""".stripMargin val unparseStatement = - s"""$indent1$indent2 // Fill to closest alignment - |$indent1$indent2 unparse_align($alignmentInBits, '\\$octalFillByte', ustate); - |$indent1$indent2 if (ustate->error) return;""".stripMargin + s"""$indent1$indent2 // Align to closest alignment + |$indent1$indent2 unparse_align_to($alignmentInBits, '\\$octalFillByte', ustate); + |$indent1$indent2 if (ustate->pu.error) return;""".stripMargin cgState.addSimpleTypeStatements(initERDStatement, parseStatement, unparseStatement) } } diff --git a/daffodil-codegen-c/src/main/scala/org/apache/daffodil/codegen/c/generators/BinaryBooleanCodeGenerator.scala b/daffodil-codegen-c/src/main/scala/org/apache/daffodil/codegen/c/generators/BinaryBooleanCodeGenerator.scala index 81222215d7..9b42fb83f8 100644 --- a/daffodil-codegen-c/src/main/scala/org/apache/daffodil/codegen/c/generators/BinaryBooleanCodeGenerator.scala +++ b/daffodil-codegen-c/src/main/scala/org/apache/daffodil/codegen/c/generators/BinaryBooleanCodeGenerator.scala @@ -63,10 +63,10 @@ trait BinaryBooleanCodeGenerator extends BinaryValueCodeGenerator { val initERDStatement = "" val parseStatement = s"""$indent1$indent2 parse_$function(&$field, $lengthInBits, $trueRep, $falseRep, pstate); - |$indent1$indent2 if (pstate->error) return;""".stripMargin + |$indent1$indent2 if (pstate->pu.error) return;""".stripMargin val unparseStatement = s"""$indent1$indent2 unparse_$function($field, $lengthInBits, $unparseTrueRep, $falseRep, ustate); - |$indent1$indent2 if (ustate->error) return;""".stripMargin + |$indent1$indent2 if (ustate->pu.error) return;""".stripMargin cgState.addSimpleTypeStatements(initERDStatement, parseStatement, unparseStatement) } } diff --git a/daffodil-codegen-c/src/main/scala/org/apache/daffodil/codegen/c/generators/BinaryValueCodeGenerator.scala b/daffodil-codegen-c/src/main/scala/org/apache/daffodil/codegen/c/generators/BinaryValueCodeGenerator.scala index 3d6bd871ac..d747b1cd0b 100644 --- a/daffodil-codegen-c/src/main/scala/org/apache/daffodil/codegen/c/generators/BinaryValueCodeGenerator.scala +++ b/daffodil-codegen-c/src/main/scala/org/apache/daffodil/codegen/c/generators/BinaryValueCodeGenerator.scala @@ -21,6 +21,7 @@ import org.apache.daffodil.core.dsom.ElementBase import org.apache.daffodil.lib.schema.annotation.props.gen.BitOrder import org.apache.daffodil.lib.schema.annotation.props.gen.ByteOrder import org.apache.daffodil.lib.util.Maybe.Nope +import org.apache.daffodil.runtime1.dpath.NodeInfo.PrimType // Base trait which provides common code to generate C code for primitive value elements trait BinaryValueCodeGenerator { @@ -50,6 +51,51 @@ trait BinaryValueCodeGenerator { if (e.hasFixedValue && e.fixedValueAsString.nonEmpty) { validateFixed(deref) } + + // Check if the element's value is restricted to a set of enumerations + if (e.typeDef.optRestriction.exists(_.hasEnumeration)) { + // Get the raw enumeration values to be inserted into the C code + val enums = e.typeDef.optRestriction.get.enumerations.map(_.enumValueRaw) + valueValidateEnumeration(e, deref, enums, cgState) + } + + // Check if the element's value is restricted to a range (we will need to handle any + // combination of inclusive and exclusive endpoints when generating our C expression) + val hasMinExclusive = e.typeDef.optRestriction.exists(_.hasMinExclusive) + val hasMinInclusive = e.typeDef.optRestriction.exists(_.hasMinInclusive) + val hasMaxExclusive = e.typeDef.optRestriction.exists(_.hasMaxExclusive) + val hasMaxInclusive = e.typeDef.optRestriction.exists(_.hasMaxInclusive) + if (hasMinExclusive || hasMinInclusive || hasMaxExclusive || hasMaxInclusive) { + // Generate the minimum endpoint comparison + val minEndpoint = if (hasMinExclusive) { + val endpoint = e.typeDef.optRestriction.map(_.minExclusiveValue).get + s"""> $endpoint""" + } else if (hasMinInclusive) { + val endpoint = e.typeDef.optRestriction.map(_.minInclusiveValue).get + e.optPrimType.get match { + case PrimType.UnsignedByte | PrimType.UnsignedShort | PrimType.UnsignedInt | + PrimType.UnsignedLong | PrimType.NonNegativeInteger if endpoint.toString == "0" => + // Avoid unsigned >= 0 comparisons (causes gcc warnings) + "" + case _ => + s""">= $endpoint""" + } + } else { + "" + } + // Generate the maximum endpoint comparison + val maxEndpoint = if (hasMaxExclusive) { + val endpoint = e.typeDef.optRestriction.map(_.maxExclusiveValue).get + s"""< $endpoint""" + } else if (hasMaxInclusive) { + val endpoint = e.typeDef.optRestriction.map(_.maxInclusiveValue).get + s"""<= $endpoint""" + } else { + "" + } + // Call another function which can be redefined differently if necessary + valueValidateRange(e, deref, minEndpoint, maxEndpoint, cgState) + } } // Generate C code to parse and unparse a primitive value element. Will be replaced by @@ -71,10 +117,10 @@ trait BinaryValueCodeGenerator { val initERDStatement = "" val parseStatement = s"""$indent1$indent2 parse_$function(&$field, $lengthInBits, pstate); - |$indent1$indent2 if (pstate->error) return;""".stripMargin + |$indent1$indent2 if (pstate->pu.error) return;""".stripMargin val unparseStatement = s"""$indent1$indent2 unparse_$function($field, $lengthInBits, ustate); - |$indent1$indent2 if (ustate->error) return;""".stripMargin + |$indent1$indent2 if (ustate->pu.error) return;""".stripMargin cgState.addSimpleTypeStatements(initERDStatement, parseStatement, unparseStatement) } @@ -93,11 +139,93 @@ trait BinaryValueCodeGenerator { val initERDStatement = "" val parseStatement = - s"""$indent1$indent2 parse_validate_fixed($field == $fixed, "$localName", pstate); - |$indent1$indent2 if (pstate->error) return;""".stripMargin + s"""$indent1$indent2 validate_fixed_attribute($field == $fixed, "$localName", &pstate->pu); + |$indent1$indent2 if (pstate->pu.error) return;""".stripMargin + val unparseStatement = + s"""$indent1$indent2 validate_fixed_attribute($field == $fixed, "$localName", &ustate->pu); + |$indent1$indent2 if (ustate->pu.error) return;""".stripMargin + cgState.addSimpleTypeStatements(initERDStatement, parseStatement, unparseStatement) + } + + // Generate C code to validate a primitive element matches one of its enumeration values. + private def valueValidateEnumeration( + e: ElementBase, + deref: String, + enums: Seq[String], + cgState: CodeGeneratorState, + ): Unit = { + val indent1 = if (cgState.hasChoice) INDENT else NO_INDENT + val indent2 = if (deref.nonEmpty) INDENT else NO_INDENT + val localName = cgState.cName(e) + val field = s"instance->$localName$deref" + + // Extra initialization only for hexBinary enumerations + val arraysName = s"arrays_$localName" + val hexEnums = enums.zipWithIndex.map { case (s, index) => + s"{$arraysName[$index], ${s.length / 2}, false}" + } + val hexEnumsInit = enums.map(_.grouped(2).map("0x" + _).mkString("{", ", ", "}")) + val hexEnumsLenMax = enums.map(_.length / 2).max + val arraysInit = + s"""$indent1$indent2 uint8_t $arraysName[][$hexEnumsLenMax] = ${hexEnumsInit.mkString( + "{", + ", ", + "}", + )};\n""" + + val (enumsInit, extraInit, primType, valType, fieldArg) = e.optPrimType.get match { + case PrimType.Double | PrimType.Float => (enums, "", "double", "floatpt", field) + case PrimType.HexBinary => (hexEnums, arraysInit, "HexBinary", "hexbinary", s"&$field") + case _ => (enums, "", "int64_t", "integer", field) + } + + // Initialization for all enumerations + val enumsArray = s"enums_$localName" + val varsInit = extraInit + + s"""$indent1$indent2 $primType $enumsArray[] = ${enumsInit.mkString( + "{", + ", ", + "}", + )};\n""" + + val initERDStatement = "" + val parseStatement = varsInit + + s"""$indent1$indent2 validate_${valType}_enumeration($fieldArg, ${enums.length}, $enumsArray, "$localName", &pstate->pu); + |$indent1$indent2 if (pstate->pu.error) return;""".stripMargin + val unparseStatement = varsInit + + s"""$indent1$indent2 validate_${valType}_enumeration($fieldArg, ${enums.length}, $enumsArray, "$localName", &ustate->pu); + |$indent1$indent2 if (ustate->pu.error) return;""".stripMargin + cgState.addSimpleTypeStatements(initERDStatement, parseStatement, unparseStatement) + } + + // Generate C code to validate a primitive element fits within its schema facets' range. + // May be replaced by a more specialized function in another trait for some types. + private def valueValidateRange( + e: ElementBase, + deref: String, + minEndpoint: String, + maxEndpoint: String, + cgState: CodeGeneratorState, + ): Unit = { + val indent1 = if (cgState.hasChoice) INDENT else NO_INDENT + val indent2 = if (deref.nonEmpty) INDENT else NO_INDENT + val localName = cgState.cName(e) + val field = s"instance->$localName$deref" + val comparison = + if (minEndpoint.nonEmpty && maxEndpoint.nonEmpty) + s"""$field $minEndpoint && $field $maxEndpoint""" + else if (minEndpoint.nonEmpty) + s"""$field $minEndpoint""" + else + s"""$field $maxEndpoint""" + + val initERDStatement = "" + val parseStatement = + s"""$indent1$indent2 validate_schema_range($comparison, "$localName", &pstate->pu); + |$indent1$indent2 if (pstate->pu.error) return;""".stripMargin val unparseStatement = - s"""$indent1$indent2 unparse_validate_fixed($field == $fixed, "$localName", ustate); - |$indent1$indent2 if (ustate->error) return;""".stripMargin + s"""$indent1$indent2 validate_schema_range($comparison, "$localName", &ustate->pu); + |$indent1$indent2 if (ustate->pu.error) return;""".stripMargin cgState.addSimpleTypeStatements(initERDStatement, parseStatement, unparseStatement) } } diff --git a/daffodil-codegen-c/src/main/scala/org/apache/daffodil/codegen/c/generators/CodeGeneratorState.scala b/daffodil-codegen-c/src/main/scala/org/apache/daffodil/codegen/c/generators/CodeGeneratorState.scala index ed7e6ec00c..70ac0ca217 100644 --- a/daffodil-codegen-c/src/main/scala/org/apache/daffodil/codegen/c/generators/CodeGeneratorState.scala +++ b/daffodil-codegen-c/src/main/scala/org/apache/daffodil/codegen/c/generators/CodeGeneratorState.scala @@ -18,7 +18,6 @@ package org.apache.daffodil.codegen.c.generators import java.util.regex.Pattern -import scala.collection.immutable import scala.collection.mutable import org.apache.daffodil.core.dsom.Choice @@ -78,8 +77,8 @@ class CodeGeneratorState(private val root: ElementBase) { context.isComplexType && context.maybeFixedLengthInBits.isDefined && context.maybeFixedLengthInBits.get > 0 ) { val lengthInBits = context.maybeFixedLengthInBits.get - structs.top.parserStatements += s" const size_t end_bitPos0b = pstate->bitPos0b + $lengthInBits;" - structs.top.unparserStatements += s" const size_t end_bitPos0b = ustate->bitPos0b + $lengthInBits;" + structs.top.parserStatements += s" const size_t end_bitPos0b = pstate->pu.bitPos0b + $lengthInBits;" + structs.top.unparserStatements += s" const size_t end_bitPos0b = ustate->pu.bitPos0b + $lengthInBits;" } } } @@ -90,12 +89,12 @@ class CodeGeneratorState(private val root: ElementBase) { // Calculate padding if complex element has an explicit length if (context.maybeFixedLengthInBits.isDefined && context.maybeFixedLengthInBits.get > 0) { structs.top.parserStatements += - s""" parse_fill_bits(end_bitPos0b, pstate); - | if (pstate->error) return;""".stripMargin + s""" parse_alignment_bits(end_bitPos0b, pstate); + | if (pstate->pu.error) return;""".stripMargin val octalFillByte = context.fillByteEv.constValue.toByte.toOctalString structs.top.unparserStatements += - s""" unparse_fill_bits(end_bitPos0b, '\\$octalFillByte', ustate); - | if (ustate->error) return;""".stripMargin + s""" unparse_alignment_bits(end_bitPos0b, '\\$octalFillByte', ustate); + | if (ustate->pu.error) return;""".stripMargin } // Finish generating the complex element's definition @@ -119,10 +118,10 @@ class CodeGeneratorState(private val root: ElementBase) { structs.top.initERDStatements += s"$indent2 ${C}_initERD(&instance->$e$deref, (InfosetBase *)instance);" structs.top.parserStatements += s"""$indent1$indent2 ${C}_parseSelf(&instance->$e$deref, pstate); - |$indent1$indent2 if (pstate->error) return;""".stripMargin + |$indent1$indent2 if (pstate->pu.error) return;""".stripMargin structs.top.unparserStatements += s"""$indent1$indent2 ${C}_unparseSelf(&instance->$e$deref, ustate); - |$indent1$indent2 if (ustate->error) return;""".stripMargin + |$indent1$indent2 if (ustate->pu.error) return;""".stripMargin } else if (context == root) { // Treat a simple type root element as a hybrid of simple and complex types addFieldDeclaration(context) // struct member for element @@ -186,10 +185,10 @@ class CodeGeneratorState(private val root: ElementBase) { structs.top.initERDStatements += s"$indent ${arrayName}_initERD(instance, parent);" structs.top.parserStatements += s"""$indent ${arrayName}_parseSelf(instance, pstate); - |$indent if (pstate->error) return;""".stripMargin + |$indent if (pstate->pu.error) return;""".stripMargin structs.top.unparserStatements += s"""$indent ${arrayName}_unparseSelf(instance, ustate); - |$indent if (ustate->error) return;""".stripMargin + |$indent if (ustate->pu.error) return;""".stripMargin } // Generates choice member/ERD and switch statements for a choice group @@ -226,16 +225,16 @@ class CodeGeneratorState(private val root: ElementBase) { val parseStatement = s""" static Error error = {ERR_CHOICE_KEY, {0}}; | - | pstate->error = instance->_base.erd->initChoice(&instance->_base); - | if (pstate->error) return; + | pstate->pu.error = instance->_base.erd->initChoice(&instance->_base); + | if (pstate->pu.error) return; | | switch (instance->_choice) | {""".stripMargin val unparseStatement = s""" static Error error = {ERR_CHOICE_KEY, {0}}; | - | ustate->error = instance->_base.erd->initChoice(&instance->_base); - | if (ustate->error) return; + | ustate->pu.error = instance->_base.erd->initChoice(&instance->_base); + | if (ustate->pu.error) return; | | switch (instance->_choice) | {""".stripMargin @@ -268,14 +267,14 @@ class CodeGeneratorState(private val root: ElementBase) { s""" default: | // Should never happen because initChoice would return an error first | error.arg.d64 = (int64_t)instance->_choice; - | pstate->error = &error; + | pstate->pu.error = &error; | return; | }""".stripMargin val unparseStatement = s""" default: | // Should never happen because initChoice would return an error first | error.arg.d64 = (int64_t)instance->_choice; - | ustate->error = &error; + | ustate->pu.error = &error; | return; | }""".stripMargin @@ -324,6 +323,7 @@ class CodeGeneratorState(private val root: ElementBase) { s"""#ifndef GENERATED_CODE_H |#define GENERATED_CODE_H | + |// auto-maintained by iwyu |// clang-format off |#include // for bool |#include // for size_t @@ -331,6 +331,10 @@ class CodeGeneratorState(private val root: ElementBase) { |#include "infoset.h" // for InfosetBase, HexBinary |// clang-format on | + |// Define schema version (will be empty if schema did not define any version string) + | + |extern const char *schema_version; + | |// Define infoset structures | |$structs @@ -342,24 +346,31 @@ class CodeGeneratorState(private val root: ElementBase) { // Generates a C source file to implement the generated code def generateCodeFile: String = { val rootName = cStructName(root) + val version = root.schemaDocument.version val prototypes = this.prototypes.mkString("\n") val erds = this.erds.mkString("\n") val finalImplementation = this.finalImplementation.mkString("\n") val code = - s"""// clang-format off + s"""// auto-maintained by iwyu + |// clang-format off |#include "generated_code.h" |#include // for false, bool, true |#include // for NULL, size_t |#include // for memcmp, memset |#include "errors.h" // for Error, PState, UState, ERR_CHOICE_KEY, Error::(anonymous), UNUSED - |#include "parsers.h" // for alloc_hexBinary, parse_hexBinary, parse_be_float, parse_be_int16, parse_validate_fixed, parse_be_bool32, parse_be_bool16, parse_be_int32, parse_be_uint16, parse_be_uint32, parse_le_bool32, parse_le_int64, parse_le_uint16, parse_le_uint8, parse_be_bool8, parse_be_double, parse_be_int64, parse_be_int8, parse_be_uint64, parse_be_uint8, parse_le_bool16, parse_le_bool8, parse_le_double, parse_le_float, parse_le_int16, parse_le_int32, parse_le_int8, parse_le_uint32, parse_le_uint64 - |#include "unparsers.h" // for unparse_hexBinary, unparse_be_float, unparse_be_int16, unparse_validate_fixed, unparse_be_bool32, unparse_be_bool16, unparse_be_int32, unparse_be_uint16, unparse_be_uint32, unparse_le_bool32, unparse_le_int64, unparse_le_uint16, unparse_le_uint8, unparse_be_bool8, unparse_be_double, unparse_be_int64, unparse_be_int8, unparse_be_uint64, unparse_be_uint8, unparse_le_bool16, unparse_le_bool8, unparse_le_double, unparse_le_float, unparse_le_int16, unparse_le_int32, unparse_le_int8, unparse_le_uint32, unparse_le_uint64 + |#include "parsers.h" // for alloc_hexBinary, parse_hexBinary, parse_be_float, parse_be_int16, parse_be_bool32, parse_be_bool16, parse_be_int32, parse_be_uint16, parse_be_uint32, parse_le_bool32, parse_le_int64, parse_le_uint16, parse_le_uint8, parse_be_bool8, parse_be_double, parse_be_int64, parse_be_int8, parse_be_uint64, parse_be_uint8, parse_le_bool16, parse_le_bool8, parse_le_double, parse_le_float, parse_le_int16, parse_le_int32, parse_le_int8, parse_le_uint32, parse_le_uint64 + |#include "unparsers.h" // for unparse_hexBinary, unparse_be_float, unparse_be_int16, unparse_be_bool32, unparse_be_bool16, unparse_be_int32, unparse_be_uint16, unparse_be_uint32, unparse_le_bool32, unparse_le_int64, unparse_le_uint16, unparse_le_uint8, unparse_be_bool8, unparse_be_double, unparse_be_int64, unparse_be_int8, unparse_be_uint64, unparse_be_uint8, unparse_le_bool16, unparse_le_bool8, unparse_le_double, unparse_le_float, unparse_le_int16, unparse_le_int32, unparse_le_int8, unparse_le_uint32, unparse_le_uint64 + |#include "validators.h" // for validate_array_bounds, validate_fixed_attribute, validate_floatpt_enumeration, validate_integer_enumeration, validate_schema_range |// clang-format on | |// Declare prototypes for easier compilation | |$prototypes | + |// Define schema version (will be empty if schema did not define any version string) + | + |const char *schema_version = "$version"; + | |// Define metadata for the infoset | |$erds @@ -741,8 +752,8 @@ class CodeGeneratorState(private val root: ElementBase) { | UNUSED(parent);""".stripMargin val parserStatements = s""" const size_t arraySize = ${arrayName}_getArraySize(instance); - | parse_check_bounds("$arrayName", arraySize, $minOccurs, $maxOccurs, pstate); - | if (pstate->error) return; + | validate_array_bounds("$arrayName", arraySize, $minOccurs, $maxOccurs, &pstate->pu); + | if (pstate->pu.error) return; | | for (size_t i = 0; i < arraySize; i++) | { @@ -750,8 +761,8 @@ class CodeGeneratorState(private val root: ElementBase) { | }""".stripMargin val unparserStatements = s""" const size_t arraySize = ${arrayName}_getArraySize(instance); - | unparse_check_bounds("$arrayName", arraySize, $minOccurs, $maxOccurs, ustate); - | if (ustate->error) return; + | validate_array_bounds("$arrayName", arraySize, $minOccurs, $maxOccurs, &ustate->pu); + | if (ustate->pu.error) return; | | for (size_t i = 0; i < arraySize; i++) | { @@ -837,7 +848,7 @@ class CodeGeneratorState(private val root: ElementBase) { if (matcher.lookingAt()) sb.replace(matcher.start, matcher.end, "") // Replace illegal characters with '_' to form a legal C name - lazy val legalCharsForC: immutable.Set[Char] = + lazy val legalCharsForC: Set[Char] = Set('_') ++ ('a' to 'z') ++ ('A' to 'Z') ++ ('0' to '9') for (i <- sb.indices) { if (!legalCharsForC.contains(sb.charAt(i))) { @@ -983,14 +994,10 @@ class CodeGeneratorState(private val root: ElementBase) { // - we can convert a relative path without any up dirs to an instance-> indirection // - we can convert slashes in the path to dots in a C struct field access notation private def cStructFieldAccess(expr: String): String = { - // If expr is an absolute path, strip the root element's local name - val rootName = root.namedQName.local - val exprWOPrefix = expr.stripPrefix(s"/$rootName") - // Turn all DFDL local names into legal C names val localName = """([\p{L}_][\p{L}:_\-.0-9]*)""".r val exprWithFields = localName.replaceAllIn( - exprWOPrefix, + expr, m => { // Make each DFDL local name a legal C name val sb = new StringBuilder(m.group(1)) @@ -1001,9 +1008,12 @@ class CodeGeneratorState(private val root: ElementBase) { // Convert exprPath to the appropriate field access indirection val fieldAccess = if (exprWithFields.startsWith("/")) { - // Convert exprPath to a get_infoset()-> indirection + // Strip the root element's name from exprWithFields + val rootName = root.namedQName.local + val exprWORoot = exprWithFields.stripPrefix(s"/$rootName/") + // Convert exprWORoot to a get_infoset()-> indirection val C = cStructName(root) - s"""(($C *)get_infoset(false))->${exprWithFields.stripPrefix("/")}""" + s"""(($C *)get_infoset(false))->$exprWORoot""" } else if (exprWithFields.startsWith("../")) { // Split exprPath into the up dirs and after the up dirs val afterUpDirs = exprWithFields.split("\\.\\./").mkString diff --git a/daffodil-codegen-c/src/main/scala/org/apache/daffodil/codegen/c/generators/HexBinaryCodeGenerator.scala b/daffodil-codegen-c/src/main/scala/org/apache/daffodil/codegen/c/generators/HexBinaryCodeGenerator.scala index 44d521a6b5..e81080ff68 100644 --- a/daffodil-codegen-c/src/main/scala/org/apache/daffodil/codegen/c/generators/HexBinaryCodeGenerator.scala +++ b/daffodil-codegen-c/src/main/scala/org/apache/daffodil/codegen/c/generators/HexBinaryCodeGenerator.scala @@ -74,16 +74,16 @@ trait HexBinaryCodeGenerator extends BinaryValueCodeGenerator { val parseStatement = s"""$indent1$indent2 ${primType}_t $lenVar; |$indent1$indent2 parse_$function(&$lenVar, $intLen, pstate); - |$indent1$indent2 if (pstate->error) return; + |$indent1$indent2 if (pstate->pu.error) return; |$indent1$indent2 alloc_hexBinary(&$field, $lenVar, pstate); - |$indent1$indent2 if (pstate->error) return; + |$indent1$indent2 if (pstate->pu.error) return; |$indent1$indent2 parse_hexBinary(&$field, pstate); - |$indent1$indent2 if (pstate->error) return;""".stripMargin + |$indent1$indent2 if (pstate->pu.error) return;""".stripMargin val unparseStatement = s"""$indent1$indent2 unparse_$function($field.lengthInBytes, $intLen, ustate); - |$indent1$indent2 if (ustate->error) return; + |$indent1$indent2 if (ustate->pu.error) return; |$indent1$indent2 unparse_hexBinary($field, ustate); - |$indent1$indent2 if (ustate->error) return;""".stripMargin + |$indent1$indent2 if (ustate->pu.error) return;""".stripMargin cgState.addSimpleTypeStatements(initERDStatement, parseStatement, unparseStatement) } @@ -134,16 +134,16 @@ trait HexBinaryCodeGenerator extends BinaryValueCodeGenerator { val parseStatement = if (specifiedLength >= 0) s"""$indent1$indent2 parse_hexBinary(&$field, pstate); - |$indent1$indent2 if (pstate->error) return;""".stripMargin + |$indent1$indent2 if (pstate->pu.error) return;""".stripMargin else s"""$indent1$indent2 $primType $lenVar = $expression; |$indent1$indent2 alloc_hexBinary(&$field, $lenVar, pstate); - |$indent1$indent2 if (pstate->error) return; + |$indent1$indent2 if (pstate->pu.error) return; |$indent1$indent2 parse_hexBinary(&$field, pstate); - |$indent1$indent2 if (pstate->error) return;""".stripMargin + |$indent1$indent2 if (pstate->pu.error) return;""".stripMargin val unparseStatement = s"""$indent1$indent2 unparse_hexBinary($field, ustate); - |$indent1$indent2 if (ustate->error) return;""".stripMargin + |$indent1$indent2 if (ustate->pu.error) return;""".stripMargin cgState.addSimpleTypeStatements(initERDStatement, parseStatement, unparseStatement) } @@ -163,12 +163,12 @@ trait HexBinaryCodeGenerator extends BinaryValueCodeGenerator { val initERDStatement = "" val parseStatement = s"""$indent1$indent2 uint8_t $fixed[] = {$array}; - |$indent1$indent2 parse_validate_fixed(memcmp($field.array, $fixed, sizeof($fixed)) == 0, "$localName", pstate); - |$indent1$indent2 if (pstate->error) return;""".stripMargin + |$indent1$indent2 validate_fixed_attribute(memcmp($field.array, $fixed, sizeof($fixed)) == 0, "$localName", &pstate->pu); + |$indent1$indent2 if (pstate->pu.error) return;""".stripMargin val unparseStatement = s"""$indent1$indent2 uint8_t $fixed[] = {$array}; - |$indent1$indent2 unparse_validate_fixed(memcmp($field.array, $fixed, sizeof($fixed)) == 0, "$localName", ustate); - |$indent1$indent2 if (ustate->error) return;""".stripMargin + |$indent1$indent2 validate_fixed_attribute(memcmp($field.array, $fixed, sizeof($fixed)) == 0, "$localName", &ustate->pu); + |$indent1$indent2 if (ustate->pu.error) return;""".stripMargin cgState.addSimpleTypeStatements(initERDStatement, parseStatement, unparseStatement) } } diff --git a/daffodil-codegen-c/src/test/examples/NestedUnion/generated_code.c b/daffodil-codegen-c/src/test/examples/NestedUnion/generated_code.c index a8ecfe8d8d..3633e16966 100644 --- a/daffodil-codegen-c/src/test/examples/NestedUnion/generated_code.c +++ b/daffodil-codegen-c/src/test/examples/NestedUnion/generated_code.c @@ -1,11 +1,13 @@ +// auto-maintained by iwyu // clang-format off #include "generated_code.h" #include // for false, bool, true #include // for NULL, size_t #include // for memcmp, memset #include "errors.h" // for Error, PState, UState, ERR_CHOICE_KEY, Error::(anonymous), UNUSED -#include "parsers.h" // for alloc_hexBinary, parse_hexBinary, parse_be_float, parse_be_int16, parse_validate_fixed, parse_be_bool32, parse_be_bool16, parse_be_int32, parse_be_uint16, parse_be_uint32, parse_le_bool32, parse_le_int64, parse_le_uint16, parse_le_uint8, parse_be_bool8, parse_be_double, parse_be_int64, parse_be_int8, parse_be_uint64, parse_be_uint8, parse_le_bool16, parse_le_bool8, parse_le_double, parse_le_float, parse_le_int16, parse_le_int32, parse_le_int8, parse_le_uint32, parse_le_uint64 -#include "unparsers.h" // for unparse_hexBinary, unparse_be_float, unparse_be_int16, unparse_validate_fixed, unparse_be_bool32, unparse_be_bool16, unparse_be_int32, unparse_be_uint16, unparse_be_uint32, unparse_le_bool32, unparse_le_int64, unparse_le_uint16, unparse_le_uint8, unparse_be_bool8, unparse_be_double, unparse_be_int64, unparse_be_int8, unparse_be_uint64, unparse_be_uint8, unparse_le_bool16, unparse_le_bool8, unparse_le_double, unparse_le_float, unparse_le_int16, unparse_le_int32, unparse_le_int8, unparse_le_uint32, unparse_le_uint64 +#include "parsers.h" // for alloc_hexBinary, parse_hexBinary, parse_be_float, parse_be_int16, parse_be_bool32, parse_be_bool16, parse_be_int32, parse_be_uint16, parse_be_uint32, parse_le_bool32, parse_le_int64, parse_le_uint16, parse_le_uint8, parse_be_bool8, parse_be_double, parse_be_int64, parse_be_int8, parse_be_uint64, parse_be_uint8, parse_le_bool16, parse_le_bool8, parse_le_double, parse_le_float, parse_le_int16, parse_le_int32, parse_le_int8, parse_le_uint32, parse_le_uint64 +#include "unparsers.h" // for unparse_hexBinary, unparse_be_float, unparse_be_int16, unparse_be_bool32, unparse_be_bool16, unparse_be_int32, unparse_be_uint16, unparse_be_uint32, unparse_le_bool32, unparse_le_int64, unparse_le_uint16, unparse_le_uint8, unparse_be_bool8, unparse_be_double, unparse_be_int64, unparse_be_int8, unparse_be_uint64, unparse_be_uint8, unparse_le_bool16, unparse_le_bool8, unparse_le_double, unparse_le_float, unparse_le_int16, unparse_le_int32, unparse_le_int8, unparse_le_uint32, unparse_le_uint64 +#include "validators.h" // for validate_array_bounds, validate_fixed_attribute, validate_floatpt_enumeration, validate_integer_enumeration, validate_schema_range // clang-format on // Declare prototypes for easier compilation @@ -20,6 +22,10 @@ static void data_NestedUnionType__unparseSelf(const data_NestedUnionType_ *insta static void NestedUnion__parseSelf(NestedUnion_ *instance, PState *pstate); static void NestedUnion__unparseSelf(const NestedUnion_ *instance, UState *ustate); +// Define schema version (will be empty if schema did not define any version string) + +const char *schema_version = ""; + // Define metadata for the infoset static const ERD tag_NestedUnionType_ERD = { @@ -229,22 +235,22 @@ static void foo_data_NestedUnionType__parseSelf(foo_data_NestedUnionType_ *instance, PState *pstate) { parse_be_int32(&instance->a, 32, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; parse_be_int32(&instance->b, 32, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; parse_be_int32(&instance->c, 32, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; } static void foo_data_NestedUnionType__unparseSelf(const foo_data_NestedUnionType_ *instance, UState *ustate) { unparse_be_int32(instance->a, 32, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; unparse_be_int32(instance->b, 32, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; unparse_be_int32(instance->c, 32, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; } static void @@ -258,22 +264,22 @@ static void bar_data_NestedUnionType__parseSelf(bar_data_NestedUnionType_ *instance, PState *pstate) { parse_be_double(&instance->x, 64, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; parse_be_double(&instance->y, 64, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; parse_be_double(&instance->z, 64, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; } static void bar_data_NestedUnionType__unparseSelf(const bar_data_NestedUnionType_ *instance, UState *ustate) { unparse_be_double(instance->x, 64, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; unparse_be_double(instance->y, 64, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; unparse_be_double(instance->z, 64, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; } static void @@ -314,23 +320,23 @@ data_NestedUnionType__parseSelf(data_NestedUnionType_ *instance, PState *pstate) { static Error error = {ERR_CHOICE_KEY, {0}}; - pstate->error = instance->_base.erd->initChoice(&instance->_base); - if (pstate->error) return; + pstate->pu.error = instance->_base.erd->initChoice(&instance->_base); + if (pstate->pu.error) return; switch (instance->_choice) { case 1: foo_data_NestedUnionType__parseSelf(&instance->foo, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; break; case 2: bar_data_NestedUnionType__parseSelf(&instance->bar, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; break; default: // Should never happen because initChoice would return an error first error.arg.d64 = (int64_t)instance->_choice; - pstate->error = &error; + pstate->pu.error = &error; return; } } @@ -340,23 +346,23 @@ data_NestedUnionType__unparseSelf(const data_NestedUnionType_ *instance, UState { static Error error = {ERR_CHOICE_KEY, {0}}; - ustate->error = instance->_base.erd->initChoice(&instance->_base); - if (ustate->error) return; + ustate->pu.error = instance->_base.erd->initChoice(&instance->_base); + if (ustate->pu.error) return; switch (instance->_choice) { case 1: foo_data_NestedUnionType__unparseSelf(&instance->foo, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; break; case 2: bar_data_NestedUnionType__unparseSelf(&instance->bar, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; break; default: // Should never happen because initChoice would return an error first error.arg.d64 = (int64_t)instance->_choice; - ustate->error = &error; + ustate->pu.error = &error; return; } } @@ -373,18 +379,18 @@ static void NestedUnion__parseSelf(NestedUnion_ *instance, PState *pstate) { parse_be_int32(&instance->tag, 32, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; data_NestedUnionType__parseSelf(&instance->data, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; } static void NestedUnion__unparseSelf(const NestedUnion_ *instance, UState *ustate) { unparse_be_int32(instance->tag, 32, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; data_NestedUnionType__unparseSelf(&instance->data, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; } // Get an infoset (optionally clearing it first) for parsing/walking diff --git a/daffodil-codegen-c/src/test/examples/NestedUnion/generated_code.h b/daffodil-codegen-c/src/test/examples/NestedUnion/generated_code.h index 40e99d37b3..cee03a0799 100644 --- a/daffodil-codegen-c/src/test/examples/NestedUnion/generated_code.h +++ b/daffodil-codegen-c/src/test/examples/NestedUnion/generated_code.h @@ -1,6 +1,7 @@ #ifndef GENERATED_CODE_H #define GENERATED_CODE_H +// auto-maintained by iwyu // clang-format off #include // for bool #include // for size_t @@ -8,6 +9,10 @@ #include "infoset.h" // for InfosetBase, HexBinary // clang-format on +// Define schema version (will be empty if schema did not define any version string) + +extern const char *schema_version; + // Define infoset structures typedef struct foo_data_NestedUnionType_ diff --git a/daffodil-codegen-c/src/test/examples/ex_nums/generated_code.c b/daffodil-codegen-c/src/test/examples/ex_nums/generated_code.c index 29228f2672..dc5ec71b04 100644 --- a/daffodil-codegen-c/src/test/examples/ex_nums/generated_code.c +++ b/daffodil-codegen-c/src/test/examples/ex_nums/generated_code.c @@ -1,11 +1,13 @@ +// auto-maintained by iwyu // clang-format off #include "generated_code.h" #include // for false, bool, true #include // for NULL, size_t #include // for memcmp, memset #include "errors.h" // for Error, PState, UState, ERR_CHOICE_KEY, Error::(anonymous), UNUSED -#include "parsers.h" // for alloc_hexBinary, parse_hexBinary, parse_be_float, parse_be_int16, parse_validate_fixed, parse_be_bool32, parse_be_bool16, parse_be_int32, parse_be_uint16, parse_be_uint32, parse_le_bool32, parse_le_int64, parse_le_uint16, parse_le_uint8, parse_be_bool8, parse_be_double, parse_be_int64, parse_be_int8, parse_be_uint64, parse_be_uint8, parse_le_bool16, parse_le_bool8, parse_le_double, parse_le_float, parse_le_int16, parse_le_int32, parse_le_int8, parse_le_uint32, parse_le_uint64 -#include "unparsers.h" // for unparse_hexBinary, unparse_be_float, unparse_be_int16, unparse_validate_fixed, unparse_be_bool32, unparse_be_bool16, unparse_be_int32, unparse_be_uint16, unparse_be_uint32, unparse_le_bool32, unparse_le_int64, unparse_le_uint16, unparse_le_uint8, unparse_be_bool8, unparse_be_double, unparse_be_int64, unparse_be_int8, unparse_be_uint64, unparse_be_uint8, unparse_le_bool16, unparse_le_bool8, unparse_le_double, unparse_le_float, unparse_le_int16, unparse_le_int32, unparse_le_int8, unparse_le_uint32, unparse_le_uint64 +#include "parsers.h" // for alloc_hexBinary, parse_hexBinary, parse_be_float, parse_be_int16, parse_be_bool32, parse_be_bool16, parse_be_int32, parse_be_uint16, parse_be_uint32, parse_le_bool32, parse_le_int64, parse_le_uint16, parse_le_uint8, parse_be_bool8, parse_be_double, parse_be_int64, parse_be_int8, parse_be_uint64, parse_be_uint8, parse_le_bool16, parse_le_bool8, parse_le_double, parse_le_float, parse_le_int16, parse_le_int32, parse_le_int8, parse_le_uint32, parse_le_uint64 +#include "unparsers.h" // for unparse_hexBinary, unparse_be_float, unparse_be_int16, unparse_be_bool32, unparse_be_bool16, unparse_be_int32, unparse_be_uint16, unparse_be_uint32, unparse_le_bool32, unparse_le_int64, unparse_le_uint16, unparse_le_uint8, unparse_be_bool8, unparse_be_double, unparse_be_int64, unparse_be_int8, unparse_be_uint64, unparse_be_uint8, unparse_le_bool16, unparse_le_bool8, unparse_le_double, unparse_le_float, unparse_le_int16, unparse_le_int32, unparse_le_int8, unparse_le_uint32, unparse_le_uint64 +#include "validators.h" // for validate_array_bounds, validate_fixed_attribute, validate_floatpt_enumeration, validate_integer_enumeration, validate_schema_range // clang-format on // Declare prototypes for easier compilation @@ -36,6 +38,10 @@ static void fixed_ex_nums__unparseSelf(const fixed_ex_nums_ *instance, UState *u static void ex_nums__parseSelf(ex_nums_ *instance, PState *pstate); static void ex_nums__unparseSelf(const ex_nums_ *instance, UState *ustate); +// Define schema version (will be empty if schema did not define any version string) + +const char *schema_version = "1.2.3"; + // Define metadata for the infoset static const ERD be_boolean_array_ex_nums_ERD = { @@ -839,13 +845,13 @@ static void array_be_boolean_array_ex_nums_array_ex_nums__parseSelf(array_ex_nums_ *instance, PState *pstate) { const size_t arraySize = array_be_boolean_array_ex_nums_array_ex_nums__getArraySize(instance); - parse_check_bounds("array_be_boolean_array_ex_nums_array_ex_nums_", arraySize, 2, 2, pstate); - if (pstate->error) return; + validate_array_bounds("array_be_boolean_array_ex_nums_array_ex_nums_", arraySize, 2, 2, &pstate->pu); + if (pstate->pu.error) return; for (size_t i = 0; i < arraySize; i++) { parse_be_bool(&instance->be_boolean[i], 32, -1, 0, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; } } @@ -853,13 +859,13 @@ static void array_be_boolean_array_ex_nums_array_ex_nums__unparseSelf(const array_ex_nums_ *instance, UState *ustate) { const size_t arraySize = array_be_boolean_array_ex_nums_array_ex_nums__getArraySize(instance); - unparse_check_bounds("array_be_boolean_array_ex_nums_array_ex_nums_", arraySize, 2, 2, ustate); - if (ustate->error) return; + validate_array_bounds("array_be_boolean_array_ex_nums_array_ex_nums_", arraySize, 2, 2, &ustate->pu); + if (ustate->pu.error) return; for (size_t i = 0; i < arraySize; i++) { unparse_be_bool(instance->be_boolean[i], 32, ~0, 0, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; } } @@ -881,13 +887,13 @@ static void array_be_float_array_ex_nums_array_ex_nums__parseSelf(array_ex_nums_ *instance, PState *pstate) { const size_t arraySize = array_be_float_array_ex_nums_array_ex_nums__getArraySize(instance); - parse_check_bounds("array_be_float_array_ex_nums_array_ex_nums_", arraySize, 3, 3, pstate); - if (pstate->error) return; + validate_array_bounds("array_be_float_array_ex_nums_array_ex_nums_", arraySize, 3, 3, &pstate->pu); + if (pstate->pu.error) return; for (size_t i = 0; i < arraySize; i++) { parse_be_float(&instance->be_float[i], 32, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; } } @@ -895,13 +901,13 @@ static void array_be_float_array_ex_nums_array_ex_nums__unparseSelf(const array_ex_nums_ *instance, UState *ustate) { const size_t arraySize = array_be_float_array_ex_nums_array_ex_nums__getArraySize(instance); - unparse_check_bounds("array_be_float_array_ex_nums_array_ex_nums_", arraySize, 3, 3, ustate); - if (ustate->error) return; + validate_array_bounds("array_be_float_array_ex_nums_array_ex_nums_", arraySize, 3, 3, &ustate->pu); + if (ustate->pu.error) return; for (size_t i = 0; i < arraySize; i++) { unparse_be_float(instance->be_float[i], 32, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; } } @@ -923,13 +929,13 @@ static void array_be_int16_array_ex_nums_array_ex_nums__parseSelf(array_ex_nums_ *instance, PState *pstate) { const size_t arraySize = array_be_int16_array_ex_nums_array_ex_nums__getArraySize(instance); - parse_check_bounds("array_be_int16_array_ex_nums_array_ex_nums_", arraySize, 3, 3, pstate); - if (pstate->error) return; + validate_array_bounds("array_be_int16_array_ex_nums_array_ex_nums_", arraySize, 3, 3, &pstate->pu); + if (pstate->pu.error) return; for (size_t i = 0; i < arraySize; i++) { parse_be_int16(&instance->be_int16[i], 16, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; } } @@ -937,13 +943,13 @@ static void array_be_int16_array_ex_nums_array_ex_nums__unparseSelf(const array_ex_nums_ *instance, UState *ustate) { const size_t arraySize = array_be_int16_array_ex_nums_array_ex_nums__getArraySize(instance); - unparse_check_bounds("array_be_int16_array_ex_nums_array_ex_nums_", arraySize, 3, 3, ustate); - if (ustate->error) return; + validate_array_bounds("array_be_int16_array_ex_nums_array_ex_nums_", arraySize, 3, 3, &ustate->pu); + if (ustate->pu.error) return; for (size_t i = 0; i < arraySize; i++) { unparse_be_int16(instance->be_int16[i], 16, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; } } @@ -970,13 +976,13 @@ static void array_hexBinary2_array_ex_nums_array_ex_nums__parseSelf(array_ex_nums_ *instance, PState *pstate) { const size_t arraySize = array_hexBinary2_array_ex_nums_array_ex_nums__getArraySize(instance); - parse_check_bounds("array_hexBinary2_array_ex_nums_array_ex_nums_", arraySize, 3, 3, pstate); - if (pstate->error) return; + validate_array_bounds("array_hexBinary2_array_ex_nums_array_ex_nums_", arraySize, 3, 3, &pstate->pu); + if (pstate->pu.error) return; for (size_t i = 0; i < arraySize; i++) { parse_hexBinary(&instance->hexBinary2[i], pstate); - if (pstate->error) return; + if (pstate->pu.error) return; } } @@ -984,13 +990,13 @@ static void array_hexBinary2_array_ex_nums_array_ex_nums__unparseSelf(const array_ex_nums_ *instance, UState *ustate) { const size_t arraySize = array_hexBinary2_array_ex_nums_array_ex_nums__getArraySize(instance); - unparse_check_bounds("array_hexBinary2_array_ex_nums_array_ex_nums_", arraySize, 3, 3, ustate); - if (ustate->error) return; + validate_array_bounds("array_hexBinary2_array_ex_nums_array_ex_nums_", arraySize, 3, 3, &ustate->pu); + if (ustate->pu.error) return; for (size_t i = 0; i < arraySize; i++) { unparse_hexBinary(instance->hexBinary2[i], ustate); - if (ustate->error) return; + if (ustate->pu.error) return; } } @@ -1015,18 +1021,18 @@ static void array_hexBinaryPrefixed_array_ex_nums_array_ex_nums__parseSelf(array_ex_nums_ *instance, PState *pstate) { const size_t arraySize = array_hexBinaryPrefixed_array_ex_nums_array_ex_nums__getArraySize(instance); - parse_check_bounds("array_hexBinaryPrefixed_array_ex_nums_array_ex_nums_", arraySize, 3, 3, pstate); - if (pstate->error) return; + validate_array_bounds("array_hexBinaryPrefixed_array_ex_nums_array_ex_nums_", arraySize, 3, 3, &pstate->pu); + if (pstate->pu.error) return; for (size_t i = 0; i < arraySize; i++) { uint16_t _l_hexBinaryPrefixed; parse_be_uint16(&_l_hexBinaryPrefixed, 16, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; alloc_hexBinary(&instance->hexBinaryPrefixed[i], _l_hexBinaryPrefixed, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; parse_hexBinary(&instance->hexBinaryPrefixed[i], pstate); - if (pstate->error) return; + if (pstate->pu.error) return; } } @@ -1034,15 +1040,15 @@ static void array_hexBinaryPrefixed_array_ex_nums_array_ex_nums__unparseSelf(const array_ex_nums_ *instance, UState *ustate) { const size_t arraySize = array_hexBinaryPrefixed_array_ex_nums_array_ex_nums__getArraySize(instance); - unparse_check_bounds("array_hexBinaryPrefixed_array_ex_nums_array_ex_nums_", arraySize, 3, 3, ustate); - if (ustate->error) return; + validate_array_bounds("array_hexBinaryPrefixed_array_ex_nums_array_ex_nums_", arraySize, 3, 3, &ustate->pu); + if (ustate->pu.error) return; for (size_t i = 0; i < arraySize; i++) { unparse_be_uint16(instance->hexBinaryPrefixed[i].lengthInBytes, 16, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; unparse_hexBinary(instance->hexBinaryPrefixed[i], ustate); - if (ustate->error) return; + if (ustate->pu.error) return; } } @@ -1069,30 +1075,30 @@ static void array_ex_nums__parseSelf(array_ex_nums_ *instance, PState *pstate) { array_be_boolean_array_ex_nums_array_ex_nums__parseSelf(instance, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; array_be_float_array_ex_nums_array_ex_nums__parseSelf(instance, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; array_be_int16_array_ex_nums_array_ex_nums__parseSelf(instance, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; array_hexBinary2_array_ex_nums_array_ex_nums__parseSelf(instance, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; array_hexBinaryPrefixed_array_ex_nums_array_ex_nums__parseSelf(instance, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; } static void array_ex_nums__unparseSelf(const array_ex_nums_ *instance, UState *ustate) { array_be_boolean_array_ex_nums_array_ex_nums__unparseSelf(instance, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; array_be_float_array_ex_nums_array_ex_nums__unparseSelf(instance, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; array_be_int16_array_ex_nums_array_ex_nums__unparseSelf(instance, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; array_hexBinary2_array_ex_nums_array_ex_nums__unparseSelf(instance, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; array_hexBinaryPrefixed_array_ex_nums_array_ex_nums__unparseSelf(instance, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; } static void @@ -1110,81 +1116,81 @@ static void bigEndian_ex_nums__parseSelf(bigEndian_ex_nums_ *instance, PState *pstate) { parse_be_bool(&instance->be_bool16, 16, 16, 0, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; parse_be_bool(&instance->be_boolean, 32, -1, 0, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; parse_be_double(&instance->be_double, 64, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; parse_be_float(&instance->be_float, 32, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; parse_be_int16(&instance->be_int16, 16, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; parse_be_int32(&instance->be_int32, 32, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; parse_be_int64(&instance->be_int64, 64, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; parse_be_int8(&instance->be_int8, 8, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; parse_be_int32(&instance->be_int17, 17, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; parse_be_uint16(&instance->be_uint16, 16, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; parse_be_uint32(&instance->be_uint32, 32, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; parse_be_uint64(&instance->be_uint64, 64, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; parse_be_uint8(&instance->be_uint8, 8, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; parse_be_uint32(&instance->be_uint31, 31, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; parse_hexBinary(&instance->hexBinary4, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; uint16_t _l_hexBinaryPrefixed; parse_be_uint16(&_l_hexBinaryPrefixed, 16, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; alloc_hexBinary(&instance->hexBinaryPrefixed, _l_hexBinaryPrefixed, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; parse_hexBinary(&instance->hexBinaryPrefixed, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; } static void bigEndian_ex_nums__unparseSelf(const bigEndian_ex_nums_ *instance, UState *ustate) { unparse_be_bool(instance->be_bool16, 16, 16, 0, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; unparse_be_bool(instance->be_boolean, 32, ~0, 0, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; unparse_be_double(instance->be_double, 64, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; unparse_be_float(instance->be_float, 32, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; unparse_be_int16(instance->be_int16, 16, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; unparse_be_int32(instance->be_int32, 32, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; unparse_be_int64(instance->be_int64, 64, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; unparse_be_int8(instance->be_int8, 8, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; unparse_be_int32(instance->be_int17, 17, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; unparse_be_uint16(instance->be_uint16, 16, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; unparse_be_uint32(instance->be_uint32, 32, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; unparse_be_uint64(instance->be_uint64, 64, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; unparse_be_uint8(instance->be_uint8, 8, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; unparse_be_uint32(instance->be_uint31, 31, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; unparse_hexBinary(instance->hexBinary4, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; unparse_be_uint16(instance->hexBinaryPrefixed.lengthInBytes, 16, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; unparse_hexBinary(instance->hexBinaryPrefixed, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; } static void @@ -1202,81 +1208,81 @@ static void littleEndian_ex_nums__parseSelf(littleEndian_ex_nums_ *instance, PState *pstate) { parse_le_bool(&instance->le_bool16, 16, 16, 0, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; parse_le_bool(&instance->le_boolean, 32, -1, 0, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; parse_le_double(&instance->le_double, 64, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; parse_le_float(&instance->le_float, 32, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; parse_le_int16(&instance->le_int16, 16, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; parse_le_int32(&instance->le_int32, 32, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; parse_le_int64(&instance->le_int64, 64, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; parse_le_int8(&instance->le_int8, 8, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; parse_le_int64(&instance->le_int46, 46, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; parse_le_uint16(&instance->le_uint16, 16, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; parse_le_uint32(&instance->le_uint32, 32, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; parse_le_uint64(&instance->le_uint64, 64, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; parse_le_uint8(&instance->le_uint8, 8, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; parse_le_uint16(&instance->le_uint10, 10, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; parse_hexBinary(&instance->hexBinary0, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; uint16_t _l_hexBinaryPrefixed; parse_le_uint16(&_l_hexBinaryPrefixed, 16, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; alloc_hexBinary(&instance->hexBinaryPrefixed, _l_hexBinaryPrefixed, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; parse_hexBinary(&instance->hexBinaryPrefixed, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; } static void littleEndian_ex_nums__unparseSelf(const littleEndian_ex_nums_ *instance, UState *ustate) { unparse_le_bool(instance->le_bool16, 16, 16, 0, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; unparse_le_bool(instance->le_boolean, 32, ~0, 0, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; unparse_le_double(instance->le_double, 64, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; unparse_le_float(instance->le_float, 32, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; unparse_le_int16(instance->le_int16, 16, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; unparse_le_int32(instance->le_int32, 32, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; unparse_le_int64(instance->le_int64, 64, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; unparse_le_int8(instance->le_int8, 8, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; unparse_le_int64(instance->le_int46, 46, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; unparse_le_uint16(instance->le_uint16, 16, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; unparse_le_uint32(instance->le_uint32, 32, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; unparse_le_uint64(instance->le_uint64, 64, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; unparse_le_uint8(instance->le_uint8, 8, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; unparse_le_uint16(instance->le_uint10, 10, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; unparse_hexBinary(instance->hexBinary0, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; unparse_le_uint16(instance->hexBinaryPrefixed.lengthInBytes, 16, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; unparse_hexBinary(instance->hexBinaryPrefixed, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; } static void @@ -1297,81 +1303,81 @@ static void fixed_ex_nums__parseSelf(fixed_ex_nums_ *instance, PState *pstate) { parse_be_bool(&instance->boolean_false, 32, -1, 0, pstate); - if (pstate->error) return; - parse_validate_fixed(instance->boolean_false == false, "boolean_false", pstate); - if (pstate->error) return; + if (pstate->pu.error) return; + validate_fixed_attribute(instance->boolean_false == false, "boolean_false", &pstate->pu); + if (pstate->pu.error) return; parse_be_bool(&instance->boolean_true, 32, -1, 0, pstate); - if (pstate->error) return; - parse_validate_fixed(instance->boolean_true == true, "boolean_true", pstate); - if (pstate->error) return; + if (pstate->pu.error) return; + validate_fixed_attribute(instance->boolean_true == true, "boolean_true", &pstate->pu); + if (pstate->pu.error) return; parse_be_double(&instance->double_3, 64, pstate); - if (pstate->error) return; - parse_validate_fixed(instance->double_3 == 3.0, "double_3", pstate); - if (pstate->error) return; + if (pstate->pu.error) return; + validate_fixed_attribute(instance->double_3 == 3.0, "double_3", &pstate->pu); + if (pstate->pu.error) return; parse_be_float(&instance->float_1_5, 32, pstate); - if (pstate->error) return; - parse_validate_fixed(instance->float_1_5 == 1.5, "float_1_5", pstate); - if (pstate->error) return; + if (pstate->pu.error) return; + validate_fixed_attribute(instance->float_1_5 == 1.5, "float_1_5", &pstate->pu); + if (pstate->pu.error) return; parse_be_int32(&instance->int_32, 32, pstate); - if (pstate->error) return; - parse_validate_fixed(instance->int_32 == 32, "int_32", pstate); - if (pstate->error) return; + if (pstate->pu.error) return; + validate_fixed_attribute(instance->int_32 == 32, "int_32", &pstate->pu); + if (pstate->pu.error) return; parse_hexBinary(&instance->hexBinary_deadbeef, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; uint8_t hexBinary_deadbeef_fixed[] = {0xDE, 0xAD, 0xBE, 0xEF}; - parse_validate_fixed(memcmp(instance->hexBinary_deadbeef.array, hexBinary_deadbeef_fixed, sizeof(hexBinary_deadbeef_fixed)) == 0, "hexBinary_deadbeef", pstate); - if (pstate->error) return; + validate_fixed_attribute(memcmp(instance->hexBinary_deadbeef.array, hexBinary_deadbeef_fixed, sizeof(hexBinary_deadbeef_fixed)) == 0, "hexBinary_deadbeef", &pstate->pu); + if (pstate->pu.error) return; parse_hexBinary(&instance->hexBinary0, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; int8_t _l_hexBinaryPrefixed_ab; parse_be_int8(&_l_hexBinaryPrefixed_ab, 8, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; alloc_hexBinary(&instance->hexBinaryPrefixed_ab, _l_hexBinaryPrefixed_ab, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; parse_hexBinary(&instance->hexBinaryPrefixed_ab, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; uint8_t hexBinaryPrefixed_ab_fixed[] = {0xAB}; - parse_validate_fixed(memcmp(instance->hexBinaryPrefixed_ab.array, hexBinaryPrefixed_ab_fixed, sizeof(hexBinaryPrefixed_ab_fixed)) == 0, "hexBinaryPrefixed_ab", pstate); - if (pstate->error) return; + validate_fixed_attribute(memcmp(instance->hexBinaryPrefixed_ab.array, hexBinaryPrefixed_ab_fixed, sizeof(hexBinaryPrefixed_ab_fixed)) == 0, "hexBinaryPrefixed_ab", &pstate->pu); + if (pstate->pu.error) return; } static void fixed_ex_nums__unparseSelf(const fixed_ex_nums_ *instance, UState *ustate) { unparse_be_bool(instance->boolean_false, 32, ~0, 0, ustate); - if (ustate->error) return; - unparse_validate_fixed(instance->boolean_false == false, "boolean_false", ustate); - if (ustate->error) return; + if (ustate->pu.error) return; + validate_fixed_attribute(instance->boolean_false == false, "boolean_false", &ustate->pu); + if (ustate->pu.error) return; unparse_be_bool(instance->boolean_true, 32, ~0, 0, ustate); - if (ustate->error) return; - unparse_validate_fixed(instance->boolean_true == true, "boolean_true", ustate); - if (ustate->error) return; + if (ustate->pu.error) return; + validate_fixed_attribute(instance->boolean_true == true, "boolean_true", &ustate->pu); + if (ustate->pu.error) return; unparse_be_double(instance->double_3, 64, ustate); - if (ustate->error) return; - unparse_validate_fixed(instance->double_3 == 3.0, "double_3", ustate); - if (ustate->error) return; + if (ustate->pu.error) return; + validate_fixed_attribute(instance->double_3 == 3.0, "double_3", &ustate->pu); + if (ustate->pu.error) return; unparse_be_float(instance->float_1_5, 32, ustate); - if (ustate->error) return; - unparse_validate_fixed(instance->float_1_5 == 1.5, "float_1_5", ustate); - if (ustate->error) return; + if (ustate->pu.error) return; + validate_fixed_attribute(instance->float_1_5 == 1.5, "float_1_5", &ustate->pu); + if (ustate->pu.error) return; unparse_be_int32(instance->int_32, 32, ustate); - if (ustate->error) return; - unparse_validate_fixed(instance->int_32 == 32, "int_32", ustate); - if (ustate->error) return; + if (ustate->pu.error) return; + validate_fixed_attribute(instance->int_32 == 32, "int_32", &ustate->pu); + if (ustate->pu.error) return; unparse_hexBinary(instance->hexBinary_deadbeef, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; uint8_t hexBinary_deadbeef_fixed[] = {0xDE, 0xAD, 0xBE, 0xEF}; - unparse_validate_fixed(memcmp(instance->hexBinary_deadbeef.array, hexBinary_deadbeef_fixed, sizeof(hexBinary_deadbeef_fixed)) == 0, "hexBinary_deadbeef", ustate); - if (ustate->error) return; + validate_fixed_attribute(memcmp(instance->hexBinary_deadbeef.array, hexBinary_deadbeef_fixed, sizeof(hexBinary_deadbeef_fixed)) == 0, "hexBinary_deadbeef", &ustate->pu); + if (ustate->pu.error) return; unparse_hexBinary(instance->hexBinary0, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; unparse_be_int8(instance->hexBinaryPrefixed_ab.lengthInBytes, 8, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; unparse_hexBinary(instance->hexBinaryPrefixed_ab, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; uint8_t hexBinaryPrefixed_ab_fixed[] = {0xAB}; - unparse_validate_fixed(memcmp(instance->hexBinaryPrefixed_ab.array, hexBinaryPrefixed_ab_fixed, sizeof(hexBinaryPrefixed_ab_fixed)) == 0, "hexBinaryPrefixed_ab", ustate); - if (ustate->error) return; + validate_fixed_attribute(memcmp(instance->hexBinaryPrefixed_ab.array, hexBinaryPrefixed_ab_fixed, sizeof(hexBinaryPrefixed_ab_fixed)) == 0, "hexBinaryPrefixed_ab", &ustate->pu); + if (ustate->pu.error) return; } static void @@ -1389,26 +1395,26 @@ static void ex_nums__parseSelf(ex_nums_ *instance, PState *pstate) { array_ex_nums__parseSelf(&instance->array, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; bigEndian_ex_nums__parseSelf(&instance->bigEndian, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; littleEndian_ex_nums__parseSelf(&instance->littleEndian, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; fixed_ex_nums__parseSelf(&instance->fixed, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; } static void ex_nums__unparseSelf(const ex_nums_ *instance, UState *ustate) { array_ex_nums__unparseSelf(&instance->array, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; bigEndian_ex_nums__unparseSelf(&instance->bigEndian, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; littleEndian_ex_nums__unparseSelf(&instance->littleEndian, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; fixed_ex_nums__unparseSelf(&instance->fixed, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; } // Get an infoset (optionally clearing it first) for parsing/walking diff --git a/daffodil-codegen-c/src/test/examples/ex_nums/generated_code.h b/daffodil-codegen-c/src/test/examples/ex_nums/generated_code.h index 364b5fb33d..b462b4a6db 100644 --- a/daffodil-codegen-c/src/test/examples/ex_nums/generated_code.h +++ b/daffodil-codegen-c/src/test/examples/ex_nums/generated_code.h @@ -1,6 +1,7 @@ #ifndef GENERATED_CODE_H #define GENERATED_CODE_H +// auto-maintained by iwyu // clang-format off #include // for bool #include // for size_t @@ -8,6 +9,10 @@ #include "infoset.h" // for InfosetBase, HexBinary // clang-format on +// Define schema version (will be empty if schema did not define any version string) + +extern const char *schema_version; + // Define infoset structures typedef struct array_ex_nums_ diff --git a/daffodil-codegen-c/src/test/examples/padtest/generated_code.c b/daffodil-codegen-c/src/test/examples/padtest/generated_code.c index 7f859e89f2..2bb8459d6f 100644 --- a/daffodil-codegen-c/src/test/examples/padtest/generated_code.c +++ b/daffodil-codegen-c/src/test/examples/padtest/generated_code.c @@ -1,11 +1,13 @@ +// auto-maintained by iwyu // clang-format off #include "generated_code.h" #include // for false, bool, true #include // for NULL, size_t #include // for memcmp, memset #include "errors.h" // for Error, PState, UState, ERR_CHOICE_KEY, Error::(anonymous), UNUSED -#include "parsers.h" // for alloc_hexBinary, parse_hexBinary, parse_be_float, parse_be_int16, parse_validate_fixed, parse_be_bool32, parse_be_bool16, parse_be_int32, parse_be_uint16, parse_be_uint32, parse_le_bool32, parse_le_int64, parse_le_uint16, parse_le_uint8, parse_be_bool8, parse_be_double, parse_be_int64, parse_be_int8, parse_be_uint64, parse_be_uint8, parse_le_bool16, parse_le_bool8, parse_le_double, parse_le_float, parse_le_int16, parse_le_int32, parse_le_int8, parse_le_uint32, parse_le_uint64 -#include "unparsers.h" // for unparse_hexBinary, unparse_be_float, unparse_be_int16, unparse_validate_fixed, unparse_be_bool32, unparse_be_bool16, unparse_be_int32, unparse_be_uint16, unparse_be_uint32, unparse_le_bool32, unparse_le_int64, unparse_le_uint16, unparse_le_uint8, unparse_be_bool8, unparse_be_double, unparse_be_int64, unparse_be_int8, unparse_be_uint64, unparse_be_uint8, unparse_le_bool16, unparse_le_bool8, unparse_le_double, unparse_le_float, unparse_le_int16, unparse_le_int32, unparse_le_int8, unparse_le_uint32, unparse_le_uint64 +#include "parsers.h" // for alloc_hexBinary, parse_hexBinary, parse_be_float, parse_be_int16, parse_be_bool32, parse_be_bool16, parse_be_int32, parse_be_uint16, parse_be_uint32, parse_le_bool32, parse_le_int64, parse_le_uint16, parse_le_uint8, parse_be_bool8, parse_be_double, parse_be_int64, parse_be_int8, parse_be_uint64, parse_be_uint8, parse_le_bool16, parse_le_bool8, parse_le_double, parse_le_float, parse_le_int16, parse_le_int32, parse_le_int8, parse_le_uint32, parse_le_uint64 +#include "unparsers.h" // for unparse_hexBinary, unparse_be_float, unparse_be_int16, unparse_be_bool32, unparse_be_bool16, unparse_be_int32, unparse_be_uint16, unparse_be_uint32, unparse_le_bool32, unparse_le_int64, unparse_le_uint16, unparse_le_uint8, unparse_be_bool8, unparse_be_double, unparse_be_int64, unparse_be_int8, unparse_be_uint64, unparse_be_uint8, unparse_le_bool16, unparse_le_bool8, unparse_le_double, unparse_le_float, unparse_le_int16, unparse_le_int32, unparse_le_int8, unparse_le_uint32, unparse_le_uint64 +#include "validators.h" // for validate_array_bounds, validate_fixed_attribute, validate_floatpt_enumeration, validate_integer_enumeration, validate_schema_range // clang-format on // Declare prototypes for easier compilation @@ -15,6 +17,10 @@ static void padhexbinary_padtest__unparseSelf(const padhexbinary_padtest_ *insta static void padtest__parseSelf(padtest_ *instance, PState *pstate); static void padtest__unparseSelf(const padtest_ *instance, UState *ustate); +// Define schema version (will be empty if schema did not define any version string) + +const char *schema_version = "3"; + // Define metadata for the infoset static const ERD opaque_padhexbinary_ERD = { @@ -104,26 +110,26 @@ padhexbinary_padtest__parseSelf(padhexbinary_padtest_ *instance, PState *pstate) { uint32_t _l_opaque; parse_be_uint32(&_l_opaque, 32, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; alloc_hexBinary(&instance->opaque, _l_opaque, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; parse_hexBinary(&instance->opaque, pstate); - if (pstate->error) return; - // Fill to closest alignment - parse_align(32, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; + // Align to closest alignment + parse_align_to(32, pstate); + if (pstate->pu.error) return; } static void padhexbinary_padtest__unparseSelf(const padhexbinary_padtest_ *instance, UState *ustate) { unparse_be_uint32(instance->opaque.lengthInBytes, 32, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; unparse_hexBinary(instance->opaque, ustate); - if (ustate->error) return; - // Fill to closest alignment - unparse_align(32, '\0', ustate); - if (ustate->error) return; + if (ustate->pu.error) return; + // Align to closest alignment + unparse_align_to(32, '\0', ustate); + if (ustate->pu.error) return; } static void @@ -138,18 +144,18 @@ static void padtest__parseSelf(padtest_ *instance, PState *pstate) { padhexbinary_padtest__parseSelf(&instance->padhexbinary, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; parse_be_uint32(&instance->after, 32, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; } static void padtest__unparseSelf(const padtest_ *instance, UState *ustate) { padhexbinary_padtest__unparseSelf(&instance->padhexbinary, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; unparse_be_uint32(instance->after, 32, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; } // Get an infoset (optionally clearing it first) for parsing/walking diff --git a/daffodil-codegen-c/src/test/examples/padtest/generated_code.h b/daffodil-codegen-c/src/test/examples/padtest/generated_code.h index 345db283dc..28b8b73b91 100644 --- a/daffodil-codegen-c/src/test/examples/padtest/generated_code.h +++ b/daffodil-codegen-c/src/test/examples/padtest/generated_code.h @@ -1,6 +1,7 @@ #ifndef GENERATED_CODE_H #define GENERATED_CODE_H +// auto-maintained by iwyu // clang-format off #include // for bool #include // for size_t @@ -8,6 +9,10 @@ #include "infoset.h" // for InfosetBase, HexBinary // clang-format on +// Define schema version (will be empty if schema did not define any version string) + +extern const char *schema_version; + // Define infoset structures typedef struct padhexbinary_padtest_ diff --git a/daffodil-codegen-c/src/test/examples/simple/generated_code.c b/daffodil-codegen-c/src/test/examples/simple/generated_code.c index 5bde5a4cbc..314b7966d6 100644 --- a/daffodil-codegen-c/src/test/examples/simple/generated_code.c +++ b/daffodil-codegen-c/src/test/examples/simple/generated_code.c @@ -1,62 +1,880 @@ +// auto-maintained by iwyu // clang-format off #include "generated_code.h" #include // for false, bool, true #include // for NULL, size_t #include // for memcmp, memset #include "errors.h" // for Error, PState, UState, ERR_CHOICE_KEY, Error::(anonymous), UNUSED -#include "parsers.h" // for alloc_hexBinary, parse_hexBinary, parse_be_float, parse_be_int16, parse_validate_fixed, parse_be_bool32, parse_be_bool16, parse_be_int32, parse_be_uint16, parse_be_uint32, parse_le_bool32, parse_le_int64, parse_le_uint16, parse_le_uint8, parse_be_bool8, parse_be_double, parse_be_int64, parse_be_int8, parse_be_uint64, parse_be_uint8, parse_le_bool16, parse_le_bool8, parse_le_double, parse_le_float, parse_le_int16, parse_le_int32, parse_le_int8, parse_le_uint32, parse_le_uint64 -#include "unparsers.h" // for unparse_hexBinary, unparse_be_float, unparse_be_int16, unparse_validate_fixed, unparse_be_bool32, unparse_be_bool16, unparse_be_int32, unparse_be_uint16, unparse_be_uint32, unparse_le_bool32, unparse_le_int64, unparse_le_uint16, unparse_le_uint8, unparse_be_bool8, unparse_be_double, unparse_be_int64, unparse_be_int8, unparse_be_uint64, unparse_be_uint8, unparse_le_bool16, unparse_le_bool8, unparse_le_double, unparse_le_float, unparse_le_int16, unparse_le_int32, unparse_le_int8, unparse_le_uint32, unparse_le_uint64 +#include "parsers.h" // for alloc_hexBinary, parse_hexBinary, parse_be_float, parse_be_int16, parse_be_bool32, parse_be_bool16, parse_be_int32, parse_be_uint16, parse_be_uint32, parse_le_bool32, parse_le_int64, parse_le_uint16, parse_le_uint8, parse_be_bool8, parse_be_double, parse_be_int64, parse_be_int8, parse_be_uint64, parse_be_uint8, parse_le_bool16, parse_le_bool8, parse_le_double, parse_le_float, parse_le_int16, parse_le_int32, parse_le_int8, parse_le_uint32, parse_le_uint64 +#include "unparsers.h" // for unparse_hexBinary, unparse_be_float, unparse_be_int16, unparse_be_bool32, unparse_be_bool16, unparse_be_int32, unparse_be_uint16, unparse_be_uint32, unparse_le_bool32, unparse_le_int64, unparse_le_uint16, unparse_le_uint8, unparse_be_bool8, unparse_be_double, unparse_be_int64, unparse_be_int8, unparse_be_uint64, unparse_be_uint8, unparse_le_bool16, unparse_le_bool8, unparse_le_double, unparse_le_float, unparse_le_int16, unparse_le_int32, unparse_le_int8, unparse_le_uint32, unparse_le_uint64 +#include "validators.h" // for validate_array_bounds, validate_fixed_attribute, validate_floatpt_enumeration, validate_integer_enumeration, validate_schema_range // clang-format on // Declare prototypes for easier compilation -static void simple_byte__parseSelf(simple_byte_ *instance, PState *pstate); -static void simple_byte__unparseSelf(const simple_byte_ *instance, UState *ustate); +static void simple__parseSelf(simple_ *instance, PState *pstate); +static void simple__unparseSelf(const simple_ *instance, UState *ustate); + +// Define schema version (will be empty if schema did not define any version string) + +const char *schema_version = "2.1.1"; // Define metadata for the infoset -static const simple_byte_ simple_byte__compute_offsets; +static const ERD simple_boolean_simple_ERD = { + { + NULL, // namedQName.prefix + "simple-boolean", // namedQName.local + NULL, // namedQName.ns + }, + PRIMITIVE_BOOLEAN, // typeCode + 0, NULL, NULL, NULL, NULL, {NULL} +}; + +static const ERD simple_byte_simple_ERD = { + { + NULL, // namedQName.prefix + "simple-byte", // namedQName.local + NULL, // namedQName.ns + }, + PRIMITIVE_INT8, // typeCode + 0, NULL, NULL, NULL, NULL, {NULL} +}; + +static const ERD simple_double_simple_ERD = { + { + NULL, // namedQName.prefix + "simple-double", // namedQName.local + NULL, // namedQName.ns + }, + PRIMITIVE_DOUBLE, // typeCode + 0, NULL, NULL, NULL, NULL, {NULL} +}; + +static const ERD simple_float_simple_ERD = { + { + NULL, // namedQName.prefix + "simple-float", // namedQName.local + NULL, // namedQName.ns + }, + PRIMITIVE_FLOAT, // typeCode + 0, NULL, NULL, NULL, NULL, {NULL} +}; + +static const ERD simple_hexBinary_simple_ERD = { + { + NULL, // namedQName.prefix + "simple-hexBinary", // namedQName.local + NULL, // namedQName.ns + }, + PRIMITIVE_HEXBINARY, // typeCode + 0, NULL, NULL, NULL, NULL, {NULL} +}; + +static const ERD simple_hexBinaryPrefixed_simple_ERD = { + { + NULL, // namedQName.prefix + "simple-hexBinaryPrefixed", // namedQName.local + NULL, // namedQName.ns + }, + PRIMITIVE_HEXBINARY, // typeCode + 0, NULL, NULL, NULL, NULL, {NULL} +}; + +static const ERD simple_int_simple_ERD = { + { + NULL, // namedQName.prefix + "simple-int", // namedQName.local + NULL, // namedQName.ns + }, + PRIMITIVE_INT32, // typeCode + 0, NULL, NULL, NULL, NULL, {NULL} +}; + +static const ERD simple_integer_simple_ERD = { + { + NULL, // namedQName.prefix + "simple-integer", // namedQName.local + NULL, // namedQName.ns + }, + PRIMITIVE_INT32, // typeCode + 0, NULL, NULL, NULL, NULL, {NULL} +}; + +static const ERD simple_long_simple_ERD = { + { + NULL, // namedQName.prefix + "simple-long", // namedQName.local + NULL, // namedQName.ns + }, + PRIMITIVE_INT64, // typeCode + 0, NULL, NULL, NULL, NULL, {NULL} +}; + +static const ERD simple_nonNegativeInteger_simple_ERD = { + { + NULL, // namedQName.prefix + "simple-nonNegativeInteger", // namedQName.local + NULL, // namedQName.ns + }, + PRIMITIVE_UINT32, // typeCode + 0, NULL, NULL, NULL, NULL, {NULL} +}; + +static const ERD simple_short_simple_ERD = { + { + NULL, // namedQName.prefix + "simple-short", // namedQName.local + NULL, // namedQName.ns + }, + PRIMITIVE_INT16, // typeCode + 0, NULL, NULL, NULL, NULL, {NULL} +}; + +static const ERD simple_unsignedByte_simple_ERD = { + { + NULL, // namedQName.prefix + "simple-unsignedByte", // namedQName.local + NULL, // namedQName.ns + }, + PRIMITIVE_UINT8, // typeCode + 0, NULL, NULL, NULL, NULL, {NULL} +}; + +static const ERD simple_unsignedInt_simple_ERD = { + { + NULL, // namedQName.prefix + "simple-unsignedInt", // namedQName.local + NULL, // namedQName.ns + }, + PRIMITIVE_UINT32, // typeCode + 0, NULL, NULL, NULL, NULL, {NULL} +}; + +static const ERD simple_unsignedLong_simple_ERD = { + { + NULL, // namedQName.prefix + "simple-unsignedLong", // namedQName.local + NULL, // namedQName.ns + }, + PRIMITIVE_UINT64, // typeCode + 0, NULL, NULL, NULL, NULL, {NULL} +}; + +static const ERD simple_unsignedShort_simple_ERD = { + { + NULL, // namedQName.prefix + "simple-unsignedShort", // namedQName.local + NULL, // namedQName.ns + }, + PRIMITIVE_UINT16, // typeCode + 0, NULL, NULL, NULL, NULL, {NULL} +}; + +static const ERD enum_byte_simple_ERD = { + { + NULL, // namedQName.prefix + "enum-byte", // namedQName.local + NULL, // namedQName.ns + }, + PRIMITIVE_INT8, // typeCode + 0, NULL, NULL, NULL, NULL, {NULL} +}; + +static const ERD enum_double_simple_ERD = { + { + NULL, // namedQName.prefix + "enum-double", // namedQName.local + NULL, // namedQName.ns + }, + PRIMITIVE_DOUBLE, // typeCode + 0, NULL, NULL, NULL, NULL, {NULL} +}; + +static const ERD enum_float_simple_ERD = { + { + NULL, // namedQName.prefix + "enum-float", // namedQName.local + NULL, // namedQName.ns + }, + PRIMITIVE_FLOAT, // typeCode + 0, NULL, NULL, NULL, NULL, {NULL} +}; + +static const ERD enum_hexBinary_simple_ERD = { + { + NULL, // namedQName.prefix + "enum-hexBinary", // namedQName.local + NULL, // namedQName.ns + }, + PRIMITIVE_HEXBINARY, // typeCode + 0, NULL, NULL, NULL, NULL, {NULL} +}; + +static const ERD enum_hexBinaryPrefixed_simple_ERD = { + { + NULL, // namedQName.prefix + "enum-hexBinaryPrefixed", // namedQName.local + NULL, // namedQName.ns + }, + PRIMITIVE_HEXBINARY, // typeCode + 0, NULL, NULL, NULL, NULL, {NULL} +}; + +static const ERD enum_int_simple_ERD = { + { + NULL, // namedQName.prefix + "enum-int", // namedQName.local + NULL, // namedQName.ns + }, + PRIMITIVE_INT32, // typeCode + 0, NULL, NULL, NULL, NULL, {NULL} +}; + +static const ERD enum_integer_simple_ERD = { + { + NULL, // namedQName.prefix + "enum-integer", // namedQName.local + NULL, // namedQName.ns + }, + PRIMITIVE_INT32, // typeCode + 0, NULL, NULL, NULL, NULL, {NULL} +}; + +static const ERD enum_long_simple_ERD = { + { + NULL, // namedQName.prefix + "enum-long", // namedQName.local + NULL, // namedQName.ns + }, + PRIMITIVE_INT64, // typeCode + 0, NULL, NULL, NULL, NULL, {NULL} +}; + +static const ERD enum_nonNegativeInteger_simple_ERD = { + { + NULL, // namedQName.prefix + "enum-nonNegativeInteger", // namedQName.local + NULL, // namedQName.ns + }, + PRIMITIVE_UINT32, // typeCode + 0, NULL, NULL, NULL, NULL, {NULL} +}; + +static const ERD enum_short_simple_ERD = { + { + NULL, // namedQName.prefix + "enum-short", // namedQName.local + NULL, // namedQName.ns + }, + PRIMITIVE_INT16, // typeCode + 0, NULL, NULL, NULL, NULL, {NULL} +}; + +static const ERD enum_unsignedByte_simple_ERD = { + { + NULL, // namedQName.prefix + "enum-unsignedByte", // namedQName.local + NULL, // namedQName.ns + }, + PRIMITIVE_UINT8, // typeCode + 0, NULL, NULL, NULL, NULL, {NULL} +}; + +static const ERD enum_unsignedInt_simple_ERD = { + { + NULL, // namedQName.prefix + "enum-unsignedInt", // namedQName.local + NULL, // namedQName.ns + }, + PRIMITIVE_UINT32, // typeCode + 0, NULL, NULL, NULL, NULL, {NULL} +}; + +static const ERD enum_unsignedLong_simple_ERD = { + { + NULL, // namedQName.prefix + "enum-unsignedLong", // namedQName.local + NULL, // namedQName.ns + }, + PRIMITIVE_UINT64, // typeCode + 0, NULL, NULL, NULL, NULL, {NULL} +}; + +static const ERD enum_unsignedShort_simple_ERD = { + { + NULL, // namedQName.prefix + "enum-unsignedShort", // namedQName.local + NULL, // namedQName.ns + }, + PRIMITIVE_UINT16, // typeCode + 0, NULL, NULL, NULL, NULL, {NULL} +}; -static const size_t simple_byte__childrenOffsets[1] = { - (const char *)&simple_byte__compute_offsets.simple_byte - (const char *)&simple_byte__compute_offsets +static const ERD range_byte_simple_ERD = { + { + NULL, // namedQName.prefix + "range-byte", // namedQName.local + NULL, // namedQName.ns + }, + PRIMITIVE_INT8, // typeCode + 0, NULL, NULL, NULL, NULL, {NULL} +}; + +static const ERD range_double_simple_ERD = { + { + NULL, // namedQName.prefix + "range-double", // namedQName.local + NULL, // namedQName.ns + }, + PRIMITIVE_DOUBLE, // typeCode + 0, NULL, NULL, NULL, NULL, {NULL} +}; + +static const ERD range_float_simple_ERD = { + { + NULL, // namedQName.prefix + "range-float", // namedQName.local + NULL, // namedQName.ns + }, + PRIMITIVE_FLOAT, // typeCode + 0, NULL, NULL, NULL, NULL, {NULL} }; -static const ERD simple_byte_ERD = { +static const ERD range_int_simple_ERD = { + { + NULL, // namedQName.prefix + "range-int", // namedQName.local + NULL, // namedQName.ns + }, + PRIMITIVE_INT32, // typeCode + 0, NULL, NULL, NULL, NULL, {NULL} +}; + +static const ERD range_integer_simple_ERD = { + { + NULL, // namedQName.prefix + "range-integer", // namedQName.local + NULL, // namedQName.ns + }, + PRIMITIVE_INT32, // typeCode + 0, NULL, NULL, NULL, NULL, {NULL} +}; + +static const ERD range_long_simple_ERD = { + { + NULL, // namedQName.prefix + "range-long", // namedQName.local + NULL, // namedQName.ns + }, + PRIMITIVE_INT64, // typeCode + 0, NULL, NULL, NULL, NULL, {NULL} +}; + +static const ERD range_nonNegativeInteger_simple_ERD = { + { + NULL, // namedQName.prefix + "range-nonNegativeInteger", // namedQName.local + NULL, // namedQName.ns + }, + PRIMITIVE_UINT32, // typeCode + 0, NULL, NULL, NULL, NULL, {NULL} +}; + +static const ERD range_short_simple_ERD = { + { + NULL, // namedQName.prefix + "range-short", // namedQName.local + NULL, // namedQName.ns + }, + PRIMITIVE_INT16, // typeCode + 0, NULL, NULL, NULL, NULL, {NULL} +}; + +static const ERD range_unsignedByte_simple_ERD = { + { + NULL, // namedQName.prefix + "range-unsignedByte", // namedQName.local + NULL, // namedQName.ns + }, + PRIMITIVE_UINT8, // typeCode + 0, NULL, NULL, NULL, NULL, {NULL} +}; + +static const ERD range_unsignedInt_simple_ERD = { + { + NULL, // namedQName.prefix + "range-unsignedInt", // namedQName.local + NULL, // namedQName.ns + }, + PRIMITIVE_UINT32, // typeCode + 0, NULL, NULL, NULL, NULL, {NULL} +}; + +static const ERD range_unsignedLong_simple_ERD = { + { + NULL, // namedQName.prefix + "range-unsignedLong", // namedQName.local + NULL, // namedQName.ns + }, + PRIMITIVE_UINT64, // typeCode + 0, NULL, NULL, NULL, NULL, {NULL} +}; + +static const ERD range_unsignedShort_simple_ERD = { + { + NULL, // namedQName.prefix + "range-unsignedShort", // namedQName.local + NULL, // namedQName.ns + }, + PRIMITIVE_UINT16, // typeCode + 0, NULL, NULL, NULL, NULL, {NULL} +}; + +static const simple_ simple__compute_offsets; + +static const size_t simple__childrenOffsets[41] = { + (const char *)&simple__compute_offsets.simple_boolean - (const char *)&simple__compute_offsets, + (const char *)&simple__compute_offsets.simple_byte - (const char *)&simple__compute_offsets, + (const char *)&simple__compute_offsets.simple_double - (const char *)&simple__compute_offsets, + (const char *)&simple__compute_offsets.simple_float - (const char *)&simple__compute_offsets, + (const char *)&simple__compute_offsets.simple_hexBinary - (const char *)&simple__compute_offsets, + (const char *)&simple__compute_offsets.simple_hexBinaryPrefixed - (const char *)&simple__compute_offsets, + (const char *)&simple__compute_offsets.simple_int - (const char *)&simple__compute_offsets, + (const char *)&simple__compute_offsets.simple_integer - (const char *)&simple__compute_offsets, + (const char *)&simple__compute_offsets.simple_long - (const char *)&simple__compute_offsets, + (const char *)&simple__compute_offsets.simple_nonNegativeInteger - (const char *)&simple__compute_offsets, + (const char *)&simple__compute_offsets.simple_short - (const char *)&simple__compute_offsets, + (const char *)&simple__compute_offsets.simple_unsignedByte - (const char *)&simple__compute_offsets, + (const char *)&simple__compute_offsets.simple_unsignedInt - (const char *)&simple__compute_offsets, + (const char *)&simple__compute_offsets.simple_unsignedLong - (const char *)&simple__compute_offsets, + (const char *)&simple__compute_offsets.simple_unsignedShort - (const char *)&simple__compute_offsets, + (const char *)&simple__compute_offsets.enum_byte - (const char *)&simple__compute_offsets, + (const char *)&simple__compute_offsets.enum_double - (const char *)&simple__compute_offsets, + (const char *)&simple__compute_offsets.enum_float - (const char *)&simple__compute_offsets, + (const char *)&simple__compute_offsets.enum_hexBinary - (const char *)&simple__compute_offsets, + (const char *)&simple__compute_offsets.enum_hexBinaryPrefixed - (const char *)&simple__compute_offsets, + (const char *)&simple__compute_offsets.enum_int - (const char *)&simple__compute_offsets, + (const char *)&simple__compute_offsets.enum_integer - (const char *)&simple__compute_offsets, + (const char *)&simple__compute_offsets.enum_long - (const char *)&simple__compute_offsets, + (const char *)&simple__compute_offsets.enum_nonNegativeInteger - (const char *)&simple__compute_offsets, + (const char *)&simple__compute_offsets.enum_short - (const char *)&simple__compute_offsets, + (const char *)&simple__compute_offsets.enum_unsignedByte - (const char *)&simple__compute_offsets, + (const char *)&simple__compute_offsets.enum_unsignedInt - (const char *)&simple__compute_offsets, + (const char *)&simple__compute_offsets.enum_unsignedLong - (const char *)&simple__compute_offsets, + (const char *)&simple__compute_offsets.enum_unsignedShort - (const char *)&simple__compute_offsets, + (const char *)&simple__compute_offsets.range_byte - (const char *)&simple__compute_offsets, + (const char *)&simple__compute_offsets.range_double - (const char *)&simple__compute_offsets, + (const char *)&simple__compute_offsets.range_float - (const char *)&simple__compute_offsets, + (const char *)&simple__compute_offsets.range_int - (const char *)&simple__compute_offsets, + (const char *)&simple__compute_offsets.range_integer - (const char *)&simple__compute_offsets, + (const char *)&simple__compute_offsets.range_long - (const char *)&simple__compute_offsets, + (const char *)&simple__compute_offsets.range_nonNegativeInteger - (const char *)&simple__compute_offsets, + (const char *)&simple__compute_offsets.range_short - (const char *)&simple__compute_offsets, + (const char *)&simple__compute_offsets.range_unsignedByte - (const char *)&simple__compute_offsets, + (const char *)&simple__compute_offsets.range_unsignedInt - (const char *)&simple__compute_offsets, + (const char *)&simple__compute_offsets.range_unsignedLong - (const char *)&simple__compute_offsets, + (const char *)&simple__compute_offsets.range_unsignedShort - (const char *)&simple__compute_offsets +}; + +static const ERD *const simple__childrenERDs[41] = { + &simple_boolean_simple_ERD, + &simple_byte_simple_ERD, + &simple_double_simple_ERD, + &simple_float_simple_ERD, + &simple_hexBinary_simple_ERD, + &simple_hexBinaryPrefixed_simple_ERD, + &simple_int_simple_ERD, + &simple_integer_simple_ERD, + &simple_long_simple_ERD, + &simple_nonNegativeInteger_simple_ERD, + &simple_short_simple_ERD, + &simple_unsignedByte_simple_ERD, + &simple_unsignedInt_simple_ERD, + &simple_unsignedLong_simple_ERD, + &simple_unsignedShort_simple_ERD, + &enum_byte_simple_ERD, + &enum_double_simple_ERD, + &enum_float_simple_ERD, + &enum_hexBinary_simple_ERD, + &enum_hexBinaryPrefixed_simple_ERD, + &enum_int_simple_ERD, + &enum_integer_simple_ERD, + &enum_long_simple_ERD, + &enum_nonNegativeInteger_simple_ERD, + &enum_short_simple_ERD, + &enum_unsignedByte_simple_ERD, + &enum_unsignedInt_simple_ERD, + &enum_unsignedLong_simple_ERD, + &enum_unsignedShort_simple_ERD, + &range_byte_simple_ERD, + &range_double_simple_ERD, + &range_float_simple_ERD, + &range_int_simple_ERD, + &range_integer_simple_ERD, + &range_long_simple_ERD, + &range_nonNegativeInteger_simple_ERD, + &range_short_simple_ERD, + &range_unsignedByte_simple_ERD, + &range_unsignedInt_simple_ERD, + &range_unsignedLong_simple_ERD, + &range_unsignedShort_simple_ERD +}; + +static const ERD simple_ERD = { { "si", // namedQName.prefix - "simple-byte", // namedQName.local + "simple", // namedQName.local "urn:simple", // namedQName.ns }, - PRIMITIVE_INT8, // typeCode - 0, // numChildren - simple_byte__childrenOffsets, - NULL, // childrenERDs - (ERDParseSelf)&simple_byte__parseSelf, - (ERDUnparseSelf)&simple_byte__unparseSelf, + COMPLEX, // typeCode + 41, // numChildren + simple__childrenOffsets, + simple__childrenERDs, + (ERDParseSelf)&simple__parseSelf, + (ERDUnparseSelf)&simple__unparseSelf, {.initChoice = NULL} }; // Initialize, parse, and unparse nodes of the infoset static void -simple_byte__initERD(simple_byte_ *instance, InfosetBase *parent) +simple__initERD(simple_ *instance, InfosetBase *parent) { - instance->_base.erd = &simple_byte_ERD; + instance->_base.erd = &simple_ERD; instance->_base.parent = parent; + instance->simple_hexBinary.array = instance->_a_simple_hexBinary; + instance->simple_hexBinary.lengthInBytes = sizeof(instance->_a_simple_hexBinary); + instance->simple_hexBinary.dynamic = false; + instance->simple_hexBinaryPrefixed.dynamic = true; + instance->enum_hexBinary.array = instance->_a_enum_hexBinary; + instance->enum_hexBinary.lengthInBytes = sizeof(instance->_a_enum_hexBinary); + instance->enum_hexBinary.dynamic = false; + instance->enum_hexBinaryPrefixed.dynamic = true; } static void -simple_byte__parseSelf(simple_byte_ *instance, PState *pstate) +simple__parseSelf(simple_ *instance, PState *pstate) { + parse_be_bool(&instance->simple_boolean, 32, 1, 0, pstate); + if (pstate->pu.error) return; parse_be_int8(&instance->simple_byte, 8, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; + parse_be_double(&instance->simple_double, 64, pstate); + if (pstate->pu.error) return; + parse_be_float(&instance->simple_float, 32, pstate); + if (pstate->pu.error) return; + parse_hexBinary(&instance->simple_hexBinary, pstate); + if (pstate->pu.error) return; + uint16_t _l_simple_hexBinaryPrefixed; + parse_be_uint16(&_l_simple_hexBinaryPrefixed, 16, pstate); + if (pstate->pu.error) return; + alloc_hexBinary(&instance->simple_hexBinaryPrefixed, _l_simple_hexBinaryPrefixed, pstate); + if (pstate->pu.error) return; + parse_hexBinary(&instance->simple_hexBinaryPrefixed, pstate); + if (pstate->pu.error) return; + parse_be_int32(&instance->simple_int, 32, pstate); + if (pstate->pu.error) return; + parse_be_int32(&instance->simple_integer, 32, pstate); + if (pstate->pu.error) return; + parse_be_int64(&instance->simple_long, 64, pstate); + if (pstate->pu.error) return; + parse_be_uint32(&instance->simple_nonNegativeInteger, 32, pstate); + if (pstate->pu.error) return; + parse_be_int16(&instance->simple_short, 16, pstate); + if (pstate->pu.error) return; + parse_be_uint8(&instance->simple_unsignedByte, 8, pstate); + if (pstate->pu.error) return; + parse_be_uint32(&instance->simple_unsignedInt, 32, pstate); + if (pstate->pu.error) return; + parse_be_uint64(&instance->simple_unsignedLong, 64, pstate); + if (pstate->pu.error) return; + parse_be_uint16(&instance->simple_unsignedShort, 16, pstate); + if (pstate->pu.error) return; + parse_be_int8(&instance->enum_byte, 8, pstate); + if (pstate->pu.error) return; + int64_t enums_enum_byte[] = {1, 2}; + validate_integer_enumeration(instance->enum_byte, 2, enums_enum_byte, "enum_byte", &pstate->pu); + if (pstate->pu.error) return; + parse_be_double(&instance->enum_double, 64, pstate); + if (pstate->pu.error) return; + double enums_enum_double[] = {1.0, 2.0}; + validate_floatpt_enumeration(instance->enum_double, 2, enums_enum_double, "enum_double", &pstate->pu); + if (pstate->pu.error) return; + parse_be_float(&instance->enum_float, 32, pstate); + if (pstate->pu.error) return; + double enums_enum_float[] = {1.0, 2.0}; + validate_floatpt_enumeration(instance->enum_float, 2, enums_enum_float, "enum_float", &pstate->pu); + if (pstate->pu.error) return; + parse_hexBinary(&instance->enum_hexBinary, pstate); + if (pstate->pu.error) return; + uint8_t arrays_enum_hexBinary[][4] = {{0x11, 0x22, 0x33, 0x44}, {0xAA, 0xBB, 0xCC, 0xDD}}; + HexBinary enums_enum_hexBinary[] = {{arrays_enum_hexBinary[0], 4, false}, {arrays_enum_hexBinary[1], 4, false}}; + validate_hexbinary_enumeration(&instance->enum_hexBinary, 2, enums_enum_hexBinary, "enum_hexBinary", &pstate->pu); + if (pstate->pu.error) return; + uint16_t _l_enum_hexBinaryPrefixed; + parse_be_uint16(&_l_enum_hexBinaryPrefixed, 16, pstate); + if (pstate->pu.error) return; + alloc_hexBinary(&instance->enum_hexBinaryPrefixed, _l_enum_hexBinaryPrefixed, pstate); + if (pstate->pu.error) return; + parse_hexBinary(&instance->enum_hexBinaryPrefixed, pstate); + if (pstate->pu.error) return; + uint8_t arrays_enum_hexBinaryPrefixed[][4] = {{0x11, 0x22, 0x33, 0x44}, {0xAA, 0xBB, 0xCC, 0xDD}}; + HexBinary enums_enum_hexBinaryPrefixed[] = {{arrays_enum_hexBinaryPrefixed[0], 4, false}, {arrays_enum_hexBinaryPrefixed[1], 4, false}}; + validate_hexbinary_enumeration(&instance->enum_hexBinaryPrefixed, 2, enums_enum_hexBinaryPrefixed, "enum_hexBinaryPrefixed", &pstate->pu); + if (pstate->pu.error) return; + parse_be_int32(&instance->enum_int, 32, pstate); + if (pstate->pu.error) return; + int64_t enums_enum_int[] = {1, 2}; + validate_integer_enumeration(instance->enum_int, 2, enums_enum_int, "enum_int", &pstate->pu); + if (pstate->pu.error) return; + parse_be_int32(&instance->enum_integer, 32, pstate); + if (pstate->pu.error) return; + int64_t enums_enum_integer[] = {1, 2}; + validate_integer_enumeration(instance->enum_integer, 2, enums_enum_integer, "enum_integer", &pstate->pu); + if (pstate->pu.error) return; + parse_be_int64(&instance->enum_long, 64, pstate); + if (pstate->pu.error) return; + int64_t enums_enum_long[] = {1, 2}; + validate_integer_enumeration(instance->enum_long, 2, enums_enum_long, "enum_long", &pstate->pu); + if (pstate->pu.error) return; + parse_be_uint32(&instance->enum_nonNegativeInteger, 32, pstate); + if (pstate->pu.error) return; + int64_t enums_enum_nonNegativeInteger[] = {1, 2}; + validate_integer_enumeration(instance->enum_nonNegativeInteger, 2, enums_enum_nonNegativeInteger, "enum_nonNegativeInteger", &pstate->pu); + if (pstate->pu.error) return; + parse_be_int16(&instance->enum_short, 16, pstate); + if (pstate->pu.error) return; + int64_t enums_enum_short[] = {1, 2}; + validate_integer_enumeration(instance->enum_short, 2, enums_enum_short, "enum_short", &pstate->pu); + if (pstate->pu.error) return; + parse_be_uint8(&instance->enum_unsignedByte, 8, pstate); + if (pstate->pu.error) return; + int64_t enums_enum_unsignedByte[] = {1, 2}; + validate_integer_enumeration(instance->enum_unsignedByte, 2, enums_enum_unsignedByte, "enum_unsignedByte", &pstate->pu); + if (pstate->pu.error) return; + parse_be_uint32(&instance->enum_unsignedInt, 32, pstate); + if (pstate->pu.error) return; + int64_t enums_enum_unsignedInt[] = {1, 2}; + validate_integer_enumeration(instance->enum_unsignedInt, 2, enums_enum_unsignedInt, "enum_unsignedInt", &pstate->pu); + if (pstate->pu.error) return; + parse_be_uint64(&instance->enum_unsignedLong, 64, pstate); + if (pstate->pu.error) return; + int64_t enums_enum_unsignedLong[] = {1, 2}; + validate_integer_enumeration(instance->enum_unsignedLong, 2, enums_enum_unsignedLong, "enum_unsignedLong", &pstate->pu); + if (pstate->pu.error) return; + parse_be_uint16(&instance->enum_unsignedShort, 16, pstate); + if (pstate->pu.error) return; + int64_t enums_enum_unsignedShort[] = {1, 2}; + validate_integer_enumeration(instance->enum_unsignedShort, 2, enums_enum_unsignedShort, "enum_unsignedShort", &pstate->pu); + if (pstate->pu.error) return; + parse_be_int8(&instance->range_byte, 8, pstate); + if (pstate->pu.error) return; + validate_schema_range(instance->range_byte > 0 && instance->range_byte <= 100, "range_byte", &pstate->pu); + if (pstate->pu.error) return; + parse_be_double(&instance->range_double, 64, pstate); + if (pstate->pu.error) return; + validate_schema_range(instance->range_double >= 1.0 && instance->range_double <= 2.0, "range_double", &pstate->pu); + if (pstate->pu.error) return; + parse_be_float(&instance->range_float, 32, pstate); + if (pstate->pu.error) return; + validate_schema_range(instance->range_float > 0.0 && instance->range_float < 1.0, "range_float", &pstate->pu); + if (pstate->pu.error) return; + parse_be_int32(&instance->range_int, 32, pstate); + if (pstate->pu.error) return; + validate_schema_range(instance->range_int >= 1 && instance->range_int < 2, "range_int", &pstate->pu); + if (pstate->pu.error) return; + parse_be_int32(&instance->range_integer, 32, pstate); + if (pstate->pu.error) return; + validate_schema_range(instance->range_integer >= 1 && instance->range_integer <= 2, "range_integer", &pstate->pu); + if (pstate->pu.error) return; + parse_be_int64(&instance->range_long, 64, pstate); + if (pstate->pu.error) return; + validate_schema_range(instance->range_long >= 1 && instance->range_long <= 2, "range_long", &pstate->pu); + if (pstate->pu.error) return; + parse_be_uint32(&instance->range_nonNegativeInteger, 32, pstate); + if (pstate->pu.error) return; + validate_schema_range(instance->range_nonNegativeInteger >= 1 && instance->range_nonNegativeInteger <= 2, "range_nonNegativeInteger", &pstate->pu); + if (pstate->pu.error) return; + parse_be_int16(&instance->range_short, 16, pstate); + if (pstate->pu.error) return; + validate_schema_range(instance->range_short >= 1 && instance->range_short <= 2, "range_short", &pstate->pu); + if (pstate->pu.error) return; + parse_be_uint8(&instance->range_unsignedByte, 8, pstate); + if (pstate->pu.error) return; + validate_schema_range(instance->range_unsignedByte >= 1 && instance->range_unsignedByte <= 2, "range_unsignedByte", &pstate->pu); + if (pstate->pu.error) return; + parse_be_uint32(&instance->range_unsignedInt, 32, pstate); + if (pstate->pu.error) return; + validate_schema_range(instance->range_unsignedInt >= 1 && instance->range_unsignedInt <= 2, "range_unsignedInt", &pstate->pu); + if (pstate->pu.error) return; + parse_be_uint64(&instance->range_unsignedLong, 64, pstate); + if (pstate->pu.error) return; + validate_schema_range(instance->range_unsignedLong >= 1 && instance->range_unsignedLong <= 2, "range_unsignedLong", &pstate->pu); + if (pstate->pu.error) return; + parse_be_uint16(&instance->range_unsignedShort, 16, pstate); + if (pstate->pu.error) return; + validate_schema_range(instance->range_unsignedShort >= 1 && instance->range_unsignedShort <= 2, "range_unsignedShort", &pstate->pu); + if (pstate->pu.error) return; } static void -simple_byte__unparseSelf(const simple_byte_ *instance, UState *ustate) +simple__unparseSelf(const simple_ *instance, UState *ustate) { + unparse_be_bool(instance->simple_boolean, 32, 1, 0, ustate); + if (ustate->pu.error) return; unparse_be_int8(instance->simple_byte, 8, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; + unparse_be_double(instance->simple_double, 64, ustate); + if (ustate->pu.error) return; + unparse_be_float(instance->simple_float, 32, ustate); + if (ustate->pu.error) return; + unparse_hexBinary(instance->simple_hexBinary, ustate); + if (ustate->pu.error) return; + unparse_be_uint16(instance->simple_hexBinaryPrefixed.lengthInBytes, 16, ustate); + if (ustate->pu.error) return; + unparse_hexBinary(instance->simple_hexBinaryPrefixed, ustate); + if (ustate->pu.error) return; + unparse_be_int32(instance->simple_int, 32, ustate); + if (ustate->pu.error) return; + unparse_be_int32(instance->simple_integer, 32, ustate); + if (ustate->pu.error) return; + unparse_be_int64(instance->simple_long, 64, ustate); + if (ustate->pu.error) return; + unparse_be_uint32(instance->simple_nonNegativeInteger, 32, ustate); + if (ustate->pu.error) return; + unparse_be_int16(instance->simple_short, 16, ustate); + if (ustate->pu.error) return; + unparse_be_uint8(instance->simple_unsignedByte, 8, ustate); + if (ustate->pu.error) return; + unparse_be_uint32(instance->simple_unsignedInt, 32, ustate); + if (ustate->pu.error) return; + unparse_be_uint64(instance->simple_unsignedLong, 64, ustate); + if (ustate->pu.error) return; + unparse_be_uint16(instance->simple_unsignedShort, 16, ustate); + if (ustate->pu.error) return; + unparse_be_int8(instance->enum_byte, 8, ustate); + if (ustate->pu.error) return; + int64_t enums_enum_byte[] = {1, 2}; + validate_integer_enumeration(instance->enum_byte, 2, enums_enum_byte, "enum_byte", &ustate->pu); + if (ustate->pu.error) return; + unparse_be_double(instance->enum_double, 64, ustate); + if (ustate->pu.error) return; + double enums_enum_double[] = {1.0, 2.0}; + validate_floatpt_enumeration(instance->enum_double, 2, enums_enum_double, "enum_double", &ustate->pu); + if (ustate->pu.error) return; + unparse_be_float(instance->enum_float, 32, ustate); + if (ustate->pu.error) return; + double enums_enum_float[] = {1.0, 2.0}; + validate_floatpt_enumeration(instance->enum_float, 2, enums_enum_float, "enum_float", &ustate->pu); + if (ustate->pu.error) return; + unparse_hexBinary(instance->enum_hexBinary, ustate); + if (ustate->pu.error) return; + uint8_t arrays_enum_hexBinary[][4] = {{0x11, 0x22, 0x33, 0x44}, {0xAA, 0xBB, 0xCC, 0xDD}}; + HexBinary enums_enum_hexBinary[] = {{arrays_enum_hexBinary[0], 4, false}, {arrays_enum_hexBinary[1], 4, false}}; + validate_hexbinary_enumeration(&instance->enum_hexBinary, 2, enums_enum_hexBinary, "enum_hexBinary", &ustate->pu); + if (ustate->pu.error) return; + unparse_be_uint16(instance->enum_hexBinaryPrefixed.lengthInBytes, 16, ustate); + if (ustate->pu.error) return; + unparse_hexBinary(instance->enum_hexBinaryPrefixed, ustate); + if (ustate->pu.error) return; + uint8_t arrays_enum_hexBinaryPrefixed[][4] = {{0x11, 0x22, 0x33, 0x44}, {0xAA, 0xBB, 0xCC, 0xDD}}; + HexBinary enums_enum_hexBinaryPrefixed[] = {{arrays_enum_hexBinaryPrefixed[0], 4, false}, {arrays_enum_hexBinaryPrefixed[1], 4, false}}; + validate_hexbinary_enumeration(&instance->enum_hexBinaryPrefixed, 2, enums_enum_hexBinaryPrefixed, "enum_hexBinaryPrefixed", &ustate->pu); + if (ustate->pu.error) return; + unparse_be_int32(instance->enum_int, 32, ustate); + if (ustate->pu.error) return; + int64_t enums_enum_int[] = {1, 2}; + validate_integer_enumeration(instance->enum_int, 2, enums_enum_int, "enum_int", &ustate->pu); + if (ustate->pu.error) return; + unparse_be_int32(instance->enum_integer, 32, ustate); + if (ustate->pu.error) return; + int64_t enums_enum_integer[] = {1, 2}; + validate_integer_enumeration(instance->enum_integer, 2, enums_enum_integer, "enum_integer", &ustate->pu); + if (ustate->pu.error) return; + unparse_be_int64(instance->enum_long, 64, ustate); + if (ustate->pu.error) return; + int64_t enums_enum_long[] = {1, 2}; + validate_integer_enumeration(instance->enum_long, 2, enums_enum_long, "enum_long", &ustate->pu); + if (ustate->pu.error) return; + unparse_be_uint32(instance->enum_nonNegativeInteger, 32, ustate); + if (ustate->pu.error) return; + int64_t enums_enum_nonNegativeInteger[] = {1, 2}; + validate_integer_enumeration(instance->enum_nonNegativeInteger, 2, enums_enum_nonNegativeInteger, "enum_nonNegativeInteger", &ustate->pu); + if (ustate->pu.error) return; + unparse_be_int16(instance->enum_short, 16, ustate); + if (ustate->pu.error) return; + int64_t enums_enum_short[] = {1, 2}; + validate_integer_enumeration(instance->enum_short, 2, enums_enum_short, "enum_short", &ustate->pu); + if (ustate->pu.error) return; + unparse_be_uint8(instance->enum_unsignedByte, 8, ustate); + if (ustate->pu.error) return; + int64_t enums_enum_unsignedByte[] = {1, 2}; + validate_integer_enumeration(instance->enum_unsignedByte, 2, enums_enum_unsignedByte, "enum_unsignedByte", &ustate->pu); + if (ustate->pu.error) return; + unparse_be_uint32(instance->enum_unsignedInt, 32, ustate); + if (ustate->pu.error) return; + int64_t enums_enum_unsignedInt[] = {1, 2}; + validate_integer_enumeration(instance->enum_unsignedInt, 2, enums_enum_unsignedInt, "enum_unsignedInt", &ustate->pu); + if (ustate->pu.error) return; + unparse_be_uint64(instance->enum_unsignedLong, 64, ustate); + if (ustate->pu.error) return; + int64_t enums_enum_unsignedLong[] = {1, 2}; + validate_integer_enumeration(instance->enum_unsignedLong, 2, enums_enum_unsignedLong, "enum_unsignedLong", &ustate->pu); + if (ustate->pu.error) return; + unparse_be_uint16(instance->enum_unsignedShort, 16, ustate); + if (ustate->pu.error) return; + int64_t enums_enum_unsignedShort[] = {1, 2}; + validate_integer_enumeration(instance->enum_unsignedShort, 2, enums_enum_unsignedShort, "enum_unsignedShort", &ustate->pu); + if (ustate->pu.error) return; + unparse_be_int8(instance->range_byte, 8, ustate); + if (ustate->pu.error) return; + validate_schema_range(instance->range_byte > 0 && instance->range_byte <= 100, "range_byte", &ustate->pu); + if (ustate->pu.error) return; + unparse_be_double(instance->range_double, 64, ustate); + if (ustate->pu.error) return; + validate_schema_range(instance->range_double >= 1.0 && instance->range_double <= 2.0, "range_double", &ustate->pu); + if (ustate->pu.error) return; + unparse_be_float(instance->range_float, 32, ustate); + if (ustate->pu.error) return; + validate_schema_range(instance->range_float > 0.0 && instance->range_float < 1.0, "range_float", &ustate->pu); + if (ustate->pu.error) return; + unparse_be_int32(instance->range_int, 32, ustate); + if (ustate->pu.error) return; + validate_schema_range(instance->range_int >= 1 && instance->range_int < 2, "range_int", &ustate->pu); + if (ustate->pu.error) return; + unparse_be_int32(instance->range_integer, 32, ustate); + if (ustate->pu.error) return; + validate_schema_range(instance->range_integer >= 1 && instance->range_integer <= 2, "range_integer", &ustate->pu); + if (ustate->pu.error) return; + unparse_be_int64(instance->range_long, 64, ustate); + if (ustate->pu.error) return; + validate_schema_range(instance->range_long >= 1 && instance->range_long <= 2, "range_long", &ustate->pu); + if (ustate->pu.error) return; + unparse_be_uint32(instance->range_nonNegativeInteger, 32, ustate); + if (ustate->pu.error) return; + validate_schema_range(instance->range_nonNegativeInteger >= 1 && instance->range_nonNegativeInteger <= 2, "range_nonNegativeInteger", &ustate->pu); + if (ustate->pu.error) return; + unparse_be_int16(instance->range_short, 16, ustate); + if (ustate->pu.error) return; + validate_schema_range(instance->range_short >= 1 && instance->range_short <= 2, "range_short", &ustate->pu); + if (ustate->pu.error) return; + unparse_be_uint8(instance->range_unsignedByte, 8, ustate); + if (ustate->pu.error) return; + validate_schema_range(instance->range_unsignedByte >= 1 && instance->range_unsignedByte <= 2, "range_unsignedByte", &ustate->pu); + if (ustate->pu.error) return; + unparse_be_uint32(instance->range_unsignedInt, 32, ustate); + if (ustate->pu.error) return; + validate_schema_range(instance->range_unsignedInt >= 1 && instance->range_unsignedInt <= 2, "range_unsignedInt", &ustate->pu); + if (ustate->pu.error) return; + unparse_be_uint64(instance->range_unsignedLong, 64, ustate); + if (ustate->pu.error) return; + validate_schema_range(instance->range_unsignedLong >= 1 && instance->range_unsignedLong <= 2, "range_unsignedLong", &ustate->pu); + if (ustate->pu.error) return; + unparse_be_uint16(instance->range_unsignedShort, 16, ustate); + if (ustate->pu.error) return; + validate_schema_range(instance->range_unsignedShort >= 1 && instance->range_unsignedShort <= 2, "range_unsignedShort", &ustate->pu); + if (ustate->pu.error) return; } // Get an infoset (optionally clearing it first) for parsing/walking @@ -64,7 +882,7 @@ simple_byte__unparseSelf(const simple_byte_ *instance, UState *ustate) InfosetBase * get_infoset(bool clear_infoset) { - static simple_byte_ infoset; + static simple_ infoset; if (clear_infoset) { @@ -72,7 +890,7 @@ get_infoset(bool clear_infoset) // you may want to walk infoset first to free their malloc'ed // storage - we are not handling that case for now... memset(&infoset, 0, sizeof(infoset)); - simple_byte__initERD(&infoset, (InfosetBase *)&infoset); + simple__initERD(&infoset, (InfosetBase *)&infoset); } return &infoset._base; diff --git a/daffodil-codegen-c/src/test/examples/simple/generated_code.h b/daffodil-codegen-c/src/test/examples/simple/generated_code.h index 5f0ebb5c5d..e4453fca26 100644 --- a/daffodil-codegen-c/src/test/examples/simple/generated_code.h +++ b/daffodil-codegen-c/src/test/examples/simple/generated_code.h @@ -1,6 +1,7 @@ #ifndef GENERATED_CODE_H #define GENERATED_CODE_H +// auto-maintained by iwyu // clang-format off #include // for bool #include // for size_t @@ -8,12 +9,58 @@ #include "infoset.h" // for InfosetBase, HexBinary // clang-format on +// Define schema version (will be empty if schema did not define any version string) + +extern const char *schema_version; + // Define infoset structures -typedef struct simple_byte_ +typedef struct simple_ { InfosetBase _base; + bool simple_boolean; int8_t simple_byte; -} simple_byte_; + double simple_double; + float simple_float; + HexBinary simple_hexBinary; + uint8_t _a_simple_hexBinary[4]; + HexBinary simple_hexBinaryPrefixed; + int32_t simple_int; + int32_t simple_integer; + int64_t simple_long; + uint32_t simple_nonNegativeInteger; + int16_t simple_short; + uint8_t simple_unsignedByte; + uint32_t simple_unsignedInt; + uint64_t simple_unsignedLong; + uint16_t simple_unsignedShort; + int8_t enum_byte; + double enum_double; + float enum_float; + HexBinary enum_hexBinary; + uint8_t _a_enum_hexBinary[4]; + HexBinary enum_hexBinaryPrefixed; + int32_t enum_int; + int32_t enum_integer; + int64_t enum_long; + uint32_t enum_nonNegativeInteger; + int16_t enum_short; + uint8_t enum_unsignedByte; + uint32_t enum_unsignedInt; + uint64_t enum_unsignedLong; + uint16_t enum_unsignedShort; + int8_t range_byte; + double range_double; + float range_float; + int32_t range_int; + int32_t range_integer; + int64_t range_long; + uint32_t range_nonNegativeInteger; + int16_t range_short; + uint8_t range_unsignedByte; + uint32_t range_unsignedInt; + uint64_t range_unsignedLong; + uint16_t range_unsignedShort; +} simple_; #endif // GENERATED_CODE_H diff --git a/daffodil-codegen-c/src/test/examples/variablelen/generated_code.c b/daffodil-codegen-c/src/test/examples/variablelen/generated_code.c index 2e87beb130..d3ea2e6062 100644 --- a/daffodil-codegen-c/src/test/examples/variablelen/generated_code.c +++ b/daffodil-codegen-c/src/test/examples/variablelen/generated_code.c @@ -1,11 +1,13 @@ +// auto-maintained by iwyu // clang-format off #include "generated_code.h" #include // for false, bool, true #include // for NULL, size_t #include // for memcmp, memset #include "errors.h" // for Error, PState, UState, ERR_CHOICE_KEY, Error::(anonymous), UNUSED -#include "parsers.h" // for alloc_hexBinary, parse_hexBinary, parse_be_float, parse_be_int16, parse_validate_fixed, parse_be_bool32, parse_be_bool16, parse_be_int32, parse_be_uint16, parse_be_uint32, parse_le_bool32, parse_le_int64, parse_le_uint16, parse_le_uint8, parse_be_bool8, parse_be_double, parse_be_int64, parse_be_int8, parse_be_uint64, parse_be_uint8, parse_le_bool16, parse_le_bool8, parse_le_double, parse_le_float, parse_le_int16, parse_le_int32, parse_le_int8, parse_le_uint32, parse_le_uint64 -#include "unparsers.h" // for unparse_hexBinary, unparse_be_float, unparse_be_int16, unparse_validate_fixed, unparse_be_bool32, unparse_be_bool16, unparse_be_int32, unparse_be_uint16, unparse_be_uint32, unparse_le_bool32, unparse_le_int64, unparse_le_uint16, unparse_le_uint8, unparse_be_bool8, unparse_be_double, unparse_be_int64, unparse_be_int8, unparse_be_uint64, unparse_be_uint8, unparse_le_bool16, unparse_le_bool8, unparse_le_double, unparse_le_float, unparse_le_int16, unparse_le_int32, unparse_le_int8, unparse_le_uint32, unparse_le_uint64 +#include "parsers.h" // for alloc_hexBinary, parse_hexBinary, parse_be_float, parse_be_int16, parse_be_bool32, parse_be_bool16, parse_be_int32, parse_be_uint16, parse_be_uint32, parse_le_bool32, parse_le_int64, parse_le_uint16, parse_le_uint8, parse_be_bool8, parse_be_double, parse_be_int64, parse_be_int8, parse_be_uint64, parse_be_uint8, parse_le_bool16, parse_le_bool8, parse_le_double, parse_le_float, parse_le_int16, parse_le_int32, parse_le_int8, parse_le_uint32, parse_le_uint64 +#include "unparsers.h" // for unparse_hexBinary, unparse_be_float, unparse_be_int16, unparse_be_bool32, unparse_be_bool16, unparse_be_int32, unparse_be_uint16, unparse_be_uint32, unparse_le_bool32, unparse_le_int64, unparse_le_uint16, unparse_le_uint8, unparse_be_bool8, unparse_be_double, unparse_be_int64, unparse_be_int8, unparse_be_uint64, unparse_be_uint8, unparse_le_bool16, unparse_le_bool8, unparse_le_double, unparse_le_float, unparse_le_int16, unparse_le_int32, unparse_le_int8, unparse_le_uint32, unparse_le_uint64 +#include "validators.h" // for validate_array_bounds, validate_fixed_attribute, validate_floatpt_enumeration, validate_integer_enumeration, validate_schema_range // clang-format on // Declare prototypes for easier compilation @@ -19,6 +21,10 @@ static size_t array_after_expressionType_expressionElement__getArraySize(const e static void expressionElement__parseSelf(expressionElement_ *instance, PState *pstate); static void expressionElement__unparseSelf(const expressionElement_ *instance, UState *ustate); +// Define schema version (will be empty if schema did not define any version string) + +const char *schema_version = ""; + // Define metadata for the infoset static const ERD before_expressionType_ERD = { @@ -155,13 +161,13 @@ static void array_variablelen_expressionType_expressionElement__parseSelf(expressionElement_ *instance, PState *pstate) { const size_t arraySize = array_variablelen_expressionType_expressionElement__getArraySize(instance); - parse_check_bounds("array_variablelen_expressionType_expressionElement_", arraySize, 0, 16, pstate); - if (pstate->error) return; + validate_array_bounds("array_variablelen_expressionType_expressionElement_", arraySize, 0, 16, &pstate->pu); + if (pstate->pu.error) return; for (size_t i = 0; i < arraySize; i++) { parse_be_uint32(&instance->variablelen[i], 32, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; } } @@ -169,13 +175,13 @@ static void array_variablelen_expressionType_expressionElement__unparseSelf(const expressionElement_ *instance, UState *ustate) { const size_t arraySize = array_variablelen_expressionType_expressionElement__getArraySize(instance); - unparse_check_bounds("array_variablelen_expressionType_expressionElement_", arraySize, 0, 16, ustate); - if (ustate->error) return; + validate_array_bounds("array_variablelen_expressionType_expressionElement_", arraySize, 0, 16, &ustate->pu); + if (ustate->pu.error) return; for (size_t i = 0; i < arraySize; i++) { unparse_be_uint32(instance->variablelen[i], 32, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; } } @@ -196,13 +202,13 @@ static void array_after_expressionType_expressionElement__parseSelf(expressionElement_ *instance, PState *pstate) { const size_t arraySize = array_after_expressionType_expressionElement__getArraySize(instance); - parse_check_bounds("array_after_expressionType_expressionElement_", arraySize, 2, 2, pstate); - if (pstate->error) return; + validate_array_bounds("array_after_expressionType_expressionElement_", arraySize, 2, 2, &pstate->pu); + if (pstate->pu.error) return; for (size_t i = 0; i < arraySize; i++) { parse_be_uint32(&instance->after[i], 32, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; } } @@ -210,13 +216,13 @@ static void array_after_expressionType_expressionElement__unparseSelf(const expressionElement_ *instance, UState *ustate) { const size_t arraySize = array_after_expressionType_expressionElement__getArraySize(instance); - unparse_check_bounds("array_after_expressionType_expressionElement_", arraySize, 2, 2, ustate); - if (ustate->error) return; + validate_array_bounds("array_after_expressionType_expressionElement_", arraySize, 2, 2, &ustate->pu); + if (ustate->pu.error) return; for (size_t i = 0; i < arraySize; i++) { unparse_be_uint32(instance->after[i], 32, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; } } @@ -240,26 +246,26 @@ static void expressionElement__parseSelf(expressionElement_ *instance, PState *pstate) { parse_be_uint32(&instance->before, 32, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; parse_be_uint32(&instance->variablelen_size, 32, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; array_variablelen_expressionType_expressionElement__parseSelf(instance, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; array_after_expressionType_expressionElement__parseSelf(instance, pstate); - if (pstate->error) return; + if (pstate->pu.error) return; } static void expressionElement__unparseSelf(const expressionElement_ *instance, UState *ustate) { unparse_be_uint32(instance->before, 32, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; unparse_be_uint32(instance->variablelen_size, 32, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; array_variablelen_expressionType_expressionElement__unparseSelf(instance, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; array_after_expressionType_expressionElement__unparseSelf(instance, ustate); - if (ustate->error) return; + if (ustate->pu.error) return; } // Get an infoset (optionally clearing it first) for parsing/walking diff --git a/daffodil-codegen-c/src/test/examples/variablelen/generated_code.h b/daffodil-codegen-c/src/test/examples/variablelen/generated_code.h index 0cb79c3d03..d2d2239206 100644 --- a/daffodil-codegen-c/src/test/examples/variablelen/generated_code.h +++ b/daffodil-codegen-c/src/test/examples/variablelen/generated_code.h @@ -1,6 +1,7 @@ #ifndef GENERATED_CODE_H #define GENERATED_CODE_H +// auto-maintained by iwyu // clang-format off #include // for bool #include // for size_t @@ -8,6 +9,10 @@ #include "infoset.h" // for InfosetBase, HexBinary // clang-format on +// Define schema version (will be empty if schema did not define any version string) + +extern const char *schema_version; + // Define infoset structures typedef struct expressionElement_ diff --git a/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/data/simple-boolean.dat b/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/data/simple-boolean.dat deleted file mode 100644 index 593f4708db84ac8fd0f5cc47c634f38c013fe9e4..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 4 LcmZQzU|;|M00aO5 diff --git a/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/data/simple-byte.dat b/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/data/simple-byte.dat deleted file mode 100644 index f76dd238ade08917e6712764a16a22005a50573d..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1 IcmZPo000310RR91 diff --git a/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/data/simple-double.dat b/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/data/simple-double.dat deleted file mode 100644 index 1b1cb4d44c57c2d7a5122870fa6ac3e62ff7e94e..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 8 KcmZQzfB*mh2mk>9 diff --git a/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/data/simple-float.dat b/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/data/simple-float.dat deleted file mode 100644 index 593f4708db84ac8fd0f5cc47c634f38c013fe9e4..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 4 LcmZQzU|;|M00aO5 diff --git a/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/data/simple-hexBinary.dat b/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/data/simple-hexBinary.dat deleted file mode 100644 index 593f4708db84ac8fd0f5cc47c634f38c013fe9e4..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 4 LcmZQzU|;|M00aO5 diff --git a/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/data/simple-hexBinaryPrefixed.dat b/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/data/simple-hexBinaryPrefixed.dat deleted file mode 100644 index 68782e0efe7bf82cc06f57886bbb4dce488262ee..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6 LcmZQzVE_UE02%-V diff --git a/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/data/simple-int.dat b/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/data/simple-int.dat deleted file mode 100644 index 593f4708db84ac8fd0f5cc47c634f38c013fe9e4..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 4 LcmZQzU|;|M00aO5 diff --git a/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/data/simple-integer.dat b/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/data/simple-integer.dat deleted file mode 100644 index 593f4708db84ac8fd0f5cc47c634f38c013fe9e4..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 4 LcmZQzU|;|M00aO5 diff --git a/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/data/simple-long.dat b/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/data/simple-long.dat deleted file mode 100644 index 1b1cb4d44c57c2d7a5122870fa6ac3e62ff7e94e..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 8 KcmZQzfB*mh2mk>9 diff --git a/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/data/simple-nonNegativeInteger.dat b/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/data/simple-nonNegativeInteger.dat deleted file mode 100644 index 593f4708db84ac8fd0f5cc47c634f38c013fe9e4..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 4 LcmZQzU|;|M00aO5 diff --git a/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/data/simple-short.dat b/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/data/simple-short.dat deleted file mode 100644 index 09f370e38f498a462e1ca0faa724559b6630c04f..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 2 JcmZQz0000200961 diff --git a/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/data/simple-unsignedByte.dat b/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/data/simple-unsignedByte.dat deleted file mode 100644 index f76dd238ade08917e6712764a16a22005a50573d..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1 IcmZPo000310RR91 diff --git a/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/data/simple-unsignedInt.dat b/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/data/simple-unsignedInt.dat deleted file mode 100644 index 593f4708db84ac8fd0f5cc47c634f38c013fe9e4..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 4 LcmZQzU|;|M00aO5 diff --git a/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/data/simple-unsignedLong.dat b/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/data/simple-unsignedLong.dat deleted file mode 100644 index 1b1cb4d44c57c2d7a5122870fa6ac3e62ff7e94e..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 8 KcmZQzfB*mh2mk>9 diff --git a/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/data/simple-unsignedShort.dat b/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/data/simple-unsignedShort.dat deleted file mode 100644 index 09f370e38f498a462e1ca0faa724559b6630c04f..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 2 JcmZQz0000200961 diff --git a/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/ex_nums.dfdl.xsd b/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/ex_nums.dfdl.xsd index 33d3af54df..56c911b4b6 100644 --- a/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/ex_nums.dfdl.xsd +++ b/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/ex_nums.dfdl.xsd @@ -17,184 +17,197 @@ --> + elementFormDefault="qualified" + targetNamespace="http://example.com" + version="1.2.3" + xmlns:dfdl="http://www.ogf.org/dfdl/dfdl-1.0/" + xmlns:net="urn:network/format" + xmlns:xs="http://www.w3.org/2001/XMLSchema" + xmlns="http://example.com"> - + - - - - - - + + + + + + - + - - - - - - - - - + + + + + + + + + - + - - + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/infosets/simple-boolean.dat.xml b/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/infosets/simple-boolean.dat.xml deleted file mode 100644 index 38dab67905..0000000000 --- a/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/infosets/simple-boolean.dat.xml +++ /dev/null @@ -1,19 +0,0 @@ - - - -false diff --git a/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/infosets/simple-byte.dat.xml b/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/infosets/simple-byte.dat.xml deleted file mode 100644 index 93d82871f8..0000000000 --- a/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/infosets/simple-byte.dat.xml +++ /dev/null @@ -1,19 +0,0 @@ - - - -0 diff --git a/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/infosets/simple-double.dat.xml b/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/infosets/simple-double.dat.xml deleted file mode 100644 index 7955ed0a8c..0000000000 --- a/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/infosets/simple-double.dat.xml +++ /dev/null @@ -1,19 +0,0 @@ - - - -0.0 diff --git a/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/infosets/simple-float.dat.xml b/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/infosets/simple-float.dat.xml deleted file mode 100644 index c33e2b0575..0000000000 --- a/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/infosets/simple-float.dat.xml +++ /dev/null @@ -1,19 +0,0 @@ - - - -0.0 diff --git a/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/infosets/simple-hexBinary.dat.xml b/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/infosets/simple-hexBinary.dat.xml deleted file mode 100644 index 1aade36087..0000000000 --- a/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/infosets/simple-hexBinary.dat.xml +++ /dev/null @@ -1,19 +0,0 @@ - - - -00000000 diff --git a/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/infosets/simple-hexBinaryPrefixed.dat.xml b/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/infosets/simple-hexBinaryPrefixed.dat.xml deleted file mode 100644 index e38aff8970..0000000000 --- a/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/infosets/simple-hexBinaryPrefixed.dat.xml +++ /dev/null @@ -1,19 +0,0 @@ - - - -00000000 diff --git a/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/infosets/simple-int.dat.xml b/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/infosets/simple-int.dat.xml deleted file mode 100644 index 4fd827083f..0000000000 --- a/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/infosets/simple-int.dat.xml +++ /dev/null @@ -1,19 +0,0 @@ - - - -0 diff --git a/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/infosets/simple-integer.dat.xml b/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/infosets/simple-integer.dat.xml deleted file mode 100644 index 55dfc6be82..0000000000 --- a/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/infosets/simple-integer.dat.xml +++ /dev/null @@ -1,19 +0,0 @@ - - - -0 diff --git a/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/infosets/simple-long.dat.xml b/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/infosets/simple-long.dat.xml deleted file mode 100644 index 955de4ed5e..0000000000 --- a/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/infosets/simple-long.dat.xml +++ /dev/null @@ -1,19 +0,0 @@ - - - -0 diff --git a/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/infosets/simple-nonNegativeInteger.dat.xml b/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/infosets/simple-nonNegativeInteger.dat.xml deleted file mode 100644 index 571189f65e..0000000000 --- a/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/infosets/simple-nonNegativeInteger.dat.xml +++ /dev/null @@ -1,19 +0,0 @@ - - - -0 diff --git a/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/infosets/simple-short.dat.xml b/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/infosets/simple-short.dat.xml deleted file mode 100644 index ebc3c4e016..0000000000 --- a/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/infosets/simple-short.dat.xml +++ /dev/null @@ -1,19 +0,0 @@ - - - -0 diff --git a/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/infosets/simple-unsignedByte.dat.xml b/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/infosets/simple-unsignedByte.dat.xml deleted file mode 100644 index 0188c9491f..0000000000 --- a/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/infosets/simple-unsignedByte.dat.xml +++ /dev/null @@ -1,19 +0,0 @@ - - - -0 diff --git a/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/infosets/simple-unsignedInt.dat.xml b/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/infosets/simple-unsignedInt.dat.xml deleted file mode 100644 index 137308bfdc..0000000000 --- a/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/infosets/simple-unsignedInt.dat.xml +++ /dev/null @@ -1,19 +0,0 @@ - - - -0 diff --git a/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/infosets/simple-unsignedLong.dat.xml b/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/infosets/simple-unsignedLong.dat.xml deleted file mode 100644 index 0430c75f13..0000000000 --- a/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/infosets/simple-unsignedLong.dat.xml +++ /dev/null @@ -1,19 +0,0 @@ - - - -0 diff --git a/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/infosets/simple-unsignedShort.dat.xml b/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/infosets/simple-unsignedShort.dat.xml deleted file mode 100644 index 6bdc4688cf..0000000000 --- a/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/infosets/simple-unsignedShort.dat.xml +++ /dev/null @@ -1,19 +0,0 @@ - - - -0 diff --git a/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/nested.dfdl.xsd b/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/nested.dfdl.xsd index af54c1a932..a5536ae37c 100644 --- a/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/nested.dfdl.xsd +++ b/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/nested.dfdl.xsd @@ -19,20 +19,16 @@ - + - + - + diff --git a/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/network/format.dfdl.xsd b/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/network/format.dfdl.xsd new file mode 100644 index 0000000000..e06f8b0d38 --- /dev/null +++ b/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/network/format.dfdl.xsd @@ -0,0 +1,52 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/padtest.dfdl.xsd b/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/padtest.dfdl.xsd index 2b7885d53a..aeadce200d 100644 --- a/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/padtest.dfdl.xsd +++ b/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/padtest.dfdl.xsd @@ -18,23 +18,19 @@ - + - + - + @@ -42,7 +38,7 @@ - + diff --git a/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/simple.dfdl.xsd b/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/simple.dfdl.xsd index 3f75517bed..dc0d1ad709 100644 --- a/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/simple.dfdl.xsd +++ b/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/simple.dfdl.xsd @@ -18,45 +18,351 @@ - + - + - + - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/simple.tdml b/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/simple.tdml index 3732a2621d..f2423f6a4f 100644 --- a/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/simple.tdml +++ b/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/simple.tdml @@ -18,312 +18,266 @@ - data/simple-boolean.dat + 00 00 00 00 - infosets/simple-boolean.dat.xml + + false + - data/simple-byte.dat + 00 - infosets/simple-byte.dat.xml + + 0 + - data/simple-double.dat + 00 00 00 00 00 00 00 00 - infosets/simple-double.dat.xml + + 0.0 + - data/simple-float.dat + 00 00 00 00 - infosets/simple-float.dat.xml + + 0.0 + - data/simple-hexBinary.dat + 00 00 00 00 - infosets/simple-hexBinary.dat.xml + + 00000000 + - data/simple-hexBinaryPrefixed.dat + 00 04 00 00 00 00 - infosets/simple-hexBinaryPrefixed.dat.xml + + 00000000 + - data/simple-int.dat + 00 00 00 00 - infosets/simple-int.dat.xml + + 0 + - data/simple-integer.dat + 00 00 00 00 - infosets/simple-integer.dat.xml + + 0 + - data/simple-long.dat + 00 00 00 00 00 00 00 00 - infosets/simple-long.dat.xml + + 0 + - data/simple-nonNegativeInteger.dat + 00 00 00 00 - infosets/simple-nonNegativeInteger.dat.xml + + 0 + - data/simple-short.dat + 00 00 - infosets/simple-short.dat.xml + + 0 + - data/simple-unsignedByte.dat + 00 - infosets/simple-unsignedByte.dat.xml + + 0 + - data/simple-unsignedInt.dat + 00 00 00 00 - infosets/simple-unsignedInt.dat.xml + + 0 + - data/simple-unsignedLong.dat + 00 00 00 00 00 00 00 00 - infosets/simple-unsignedLong.dat.xml + + 0 + - data/simple-unsignedShort.dat + 00 00 - infosets/simple-unsignedShort.dat.xml + + 0 + diff --git a/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/simple_errors.tdml b/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/simple_errors.tdml index 10b45721a5..7aeb84c822 100644 --- a/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/simple_errors.tdml +++ b/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/simple_errors.tdml @@ -18,22 +18,23 @@ - 0000002a + 00 00 00 2a parse @@ -43,14 +44,14 @@ - 0102 + 01 02 8 bit @@ -59,14 +60,14 @@ - 01020304 + 01 02 03 04 64 bit @@ -75,14 +76,14 @@ - 7fc00000 + 7f c0 00 00 @@ -99,7 +100,7 @@ name="simple-hexBinary-5b" root="simple-hexBinary"> - 0102030405 + 01 02 03 04 05 8 bit @@ -108,14 +109,14 @@ - 01020304 + 01 02 03 04 2064 bit @@ -124,7 +125,7 @@ - 01020304 + 01 02 03 04 16 bit @@ -155,4 +156,576 @@ + + + + 00 + + + + 0 + + + + enum + byte + + + + + + + 00 00 00 00 00 00 00 00 + + + + 0.0 + + + + enum + double + + + + + + + 00 00 00 00 + + + + 0.0 + + + + enum + float + + + + + + + 00 00 00 00 + + + + 00000000 + + + + enum + hexBinary + + + + + + + 00 04 00 00 00 00 + + + + 00000000 + + + + enum + hexBinaryPrefixed + + + + + + + 00 00 00 00 + + + + 0 + + + + enum + int + + + + + + + 00 00 00 00 + + + + 0 + + + + enum + integer + + + + + + + 00 00 00 00 00 00 00 00 + + + + 0 + + + + enum + long + + + + + + + 00 00 00 00 + + + + 0 + + + + enum + nonNegativeInteger + + + + + + + 00 00 + + + + 0 + + + + enum + short + + + + + + + 00 + + + + 0 + + + + enum + unsignedByte + + + + + + + 00 00 00 00 + + + + 0 + + + + enum + unsignedInt + + + + + + + 00 00 00 00 00 00 00 00 + + + + 0 + + + + enum + unsignedLong + + + + + + + 00 00 + + + + 0 + + + + enum + unsignedShort + + + + + + + 00 + + + + 0 + + + + range + byte + + + + + + + 00 00 00 00 00 00 00 00 + + + + 0.0 + + + + range + double + + + + + + + 00 00 00 00 + + + + 0.0 + + + + range + float + + + + + + + 00 00 00 00 + + + + 0 + + + + range + int + + + + + + + 00 00 00 00 + + + + 0 + + + + range + integer + + + + + + + 00 00 00 00 00 00 00 00 + + + + 0 + + + + range + long + + + + + + + 00 00 00 00 + + + + 0 + + + + range + nonNegativeInteger + + + + + + + 00 00 + + + + 0 + + + + range + short + + + + + + + 00 + + + + 0 + + + + range + unsignedByte + + + + + + + 00 00 00 00 + + + + 0 + + + + range + unsignedInt + + + + + + + 00 00 00 00 00 00 00 00 + + + + 0 + + + + range + unsignedLong + + + + + + + 00 00 + + + + 0 + + + + range + unsignedShort + + + diff --git a/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/variablelen.dfdl.xsd b/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/variablelen.dfdl.xsd index 39f59a70da..d78c5914b1 100644 --- a/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/variablelen.dfdl.xsd +++ b/daffodil-codegen-c/src/test/resources/org/apache/daffodil/codegen/c/variablelen.dfdl.xsd @@ -19,22 +19,17 @@ - + - + - + diff --git a/daffodil-core/src/main/scala/org/apache/daffodil/core/dsom/SchemaDocument.scala b/daffodil-core/src/main/scala/org/apache/daffodil/core/dsom/SchemaDocument.scala index b576cbdf7e..13cd4bae48 100644 --- a/daffodil-core/src/main/scala/org/apache/daffodil/core/dsom/SchemaDocument.scala +++ b/daffodil-core/src/main/scala/org/apache/daffodil/core/dsom/SchemaDocument.scala @@ -212,6 +212,8 @@ final class SchemaDocument private (xmlSDoc: XMLSchemaDocument) final override lazy val optLexicalParent = Some(xmlSDoc) final override lazy val optXMLSchemaDocument = Some(xmlSDoc) + final lazy val version = (xml \ "@version").text + override lazy val optReferredToComponent = None override lazy val optSchemaDocument = Some(this) diff --git a/daffodil-tdml-processor/src/test/scala/org/apache/daffodil/processor/tdml/TestDaffodilC.scala b/daffodil-tdml-processor/src/test/scala/org/apache/daffodil/processor/tdml/TestDaffodilC.scala index a460978825..8acea41ac0 100644 --- a/daffodil-tdml-processor/src/test/scala/org/apache/daffodil/processor/tdml/TestDaffodilC.scala +++ b/daffodil-tdml-processor/src/test/scala/org/apache/daffodil/processor/tdml/TestDaffodilC.scala @@ -67,6 +67,15 @@ class TestDaffodilC { , ) + // Checks that test schema compiles successfully without warnings + @Test def test_compileNode(): Unit = { + val pf = Compiler().compileNode(testSchema) + assert( + !pf.isError && pf.getDiagnostics.isEmpty, + pf.getDiagnostics.map(_.getMessage()).mkString("\n"), + ) + } + // Checks that processorFactory.forLanguage("c") succeeds @Test def test_forLanguage_success(): Unit = { // Create a ProcessorFactory from the test schema @@ -191,11 +200,11 @@ class TestDaffodilC { val executable = cg.compileCode(codeDir) // Create a DaffodilCTDMLDFDLProcessor and unparse a binary int32 number unsuccessfully - val dp = new DaffodilCTDMLDFDLProcessor(executable) + val tdp = new DaffodilCTDMLDFDLProcessor(executable) val infosetXML = FAIL val output = new ByteArrayOutputStream() - val ur = dp.unparse(infosetXML, output) - assert(ur.isProcessingError, "expected ur.isError to be true") + val ur = tdp.unparse(infosetXML, output) + assert(ur.isProcessingError, "expected ur.isProcessingError to be true") assert(ur.getDiagnostics.nonEmpty, "expected ur.getDiagnostics to be non-empty") } diff --git a/daffodil-test/src/test/scala/org/apache/daffodil/codegen/c/TestSimpleErrors.scala b/daffodil-test/src/test/scala/org/apache/daffodil/codegen/c/TestSimpleErrors.scala index 942e3dc18a..38d89d290b 100644 --- a/daffodil-test/src/test/scala/org/apache/daffodil/codegen/c/TestSimpleErrors.scala +++ b/daffodil-test/src/test/scala/org/apache/daffodil/codegen/c/TestSimpleErrors.scala @@ -43,4 +43,38 @@ class TestSimpleErrors { } @Test def simple_int_1b(): Unit = { runner.runOneTest("simple-int-1b") } @Test def simple_unsignedShort_4b(): Unit = { runner.runOneTest("simple-unsignedShort-4b") } + + @Test def enum_byte_0(): Unit = { runner.runOneTest("enum-byte-0") } + @Test def enum_double_0_0(): Unit = { runner.runOneTest("enum-double-0.0") } + @Test def enum_float_0_0(): Unit = { runner.runOneTest("enum-float-0.0") } + @Test def enum_hexBinary_00000000(): Unit = { runner.runOneTest("enum-hexBinary-00000000") } + @Test def enum_hexBinaryPrefixed_00000000(): Unit = { + runner.runOneTest("enum-hexBinaryPrefixed-00000000") + } + @Test def enum_int_0(): Unit = { runner.runOneTest("enum-int-0") } + @Test def enum_integer_0(): Unit = { runner.runOneTest("enum-integer-0") } + @Test def enum_long_0(): Unit = { runner.runOneTest("enum-long-0") } + @Test def enum_nonNegativeInteger_0(): Unit = { + runner.runOneTest("enum-nonNegativeInteger-0") + } + @Test def enum_short_0(): Unit = { runner.runOneTest("enum-short-0") } + @Test def enum_unsignedByte_0(): Unit = { runner.runOneTest("enum-unsignedByte-0") } + @Test def enum_unsignedInt_0(): Unit = { runner.runOneTest("enum-unsignedInt-0") } + @Test def enum_unsignedLong_0(): Unit = { runner.runOneTest("enum-unsignedLong-0") } + @Test def enum_unsignedShort_0(): Unit = { runner.runOneTest("enum-unsignedShort-0") } + + @Test def range_byte_0(): Unit = { runner.runOneTest("range-byte-0") } + @Test def range_double_0_0(): Unit = { runner.runOneTest("range-double-0.0") } + @Test def range_float_0_0(): Unit = { runner.runOneTest("range-float-0.0") } + @Test def range_int_0(): Unit = { runner.runOneTest("range-int-0") } + @Test def range_integer_0(): Unit = { runner.runOneTest("range-integer-0") } + @Test def range_long_0(): Unit = { runner.runOneTest("range-long-0") } + @Test def range_nonNegativeInteger_0(): Unit = { + runner.runOneTest("range-nonNegativeInteger-0") + } + @Test def range_short_0(): Unit = { runner.runOneTest("range-short-0") } + @Test def range_unsignedByte_0(): Unit = { runner.runOneTest("range-unsignedByte-0") } + @Test def range_unsignedInt_0(): Unit = { runner.runOneTest("range-unsignedInt-0") } + @Test def range_unsignedLong_0(): Unit = { runner.runOneTest("range-unsignedLong-0") } + @Test def range_unsignedShort_0(): Unit = { runner.runOneTest("range-unsignedShort-0") } }