Repository navigation
Enforce parent tag type when creating a pbf_builder submessage
#159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kkaefer
wants to merge
1
commit into
kk/fix-ci-images
Choose a base branch
from
kk/strict-builder-parent-tag
base: kk/fix-ci-images
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| #----------------------------------------------------------------------------- | ||
| # | ||
| # CMake config | ||
| # | ||
| # protozero compile failure tests | ||
| # | ||
| # Each source file in here must fail to compile with the expected error | ||
| # message. This is used to test that misuse of the API is caught at compile | ||
| # time, for instance by a static_assert. | ||
| # | ||
| # To add a test, create a new source file and add its name (without the | ||
| # .cpp suffix) to COMPILE_FAIL_TESTS below. | ||
| # | ||
| #----------------------------------------------------------------------------- | ||
|
|
||
| set(COMPILE_FAIL_TESTS builder_parent_wrong_enum | ||
| builder_parent_integer_tag) | ||
|
|
||
| foreach(_test IN LISTS COMPILE_FAIL_TESTS) | ||
| # Compile-only target (no main() needed), excluded from the normal build. | ||
| add_library(compile_fail_${_test} OBJECT EXCLUDE_FROM_ALL ${_test}.cpp) | ||
| set_target_properties(compile_fail_${_test} PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD ON) | ||
|
|
||
| # The test builds the target above, so its output is the compiler output. | ||
| add_test(NAME compile_fail_${_test} | ||
| COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} | ||
| --target compile_fail_${_test} | ||
| --config $<CONFIG>) | ||
|
|
||
| # Pass only if the expected error shows up (the exit code is ignored). | ||
| # Unlike WILL_FAIL, this doesn't accept unrelated compile errors. | ||
| set_tests_properties(compile_fail_${_test} PROPERTIES | ||
| PASS_REGULAR_EXPRESSION "tag must be of the parent builder's enum type") | ||
| endforeach() | ||
|
|
||
|
|
||
| #----------------------------------------------------------------------------- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
|
|
||
| // This file must not compile: A pbf_builder parent requires a tag of its enum | ||
| // type, not a plain integer. | ||
|
|
||
| #include <protozero/pbf_builder.hpp> | ||
|
|
||
| #include <string> | ||
|
|
||
| enum class Outer : protozero::pbf_tag_type { | ||
| sub = 1 | ||
| }; | ||
|
|
||
| enum class Inner : protozero::pbf_tag_type { | ||
| value = 2 | ||
| }; | ||
|
|
||
| void test() { | ||
| std::string buffer; | ||
| protozero::pbf_builder<Outer> outer{buffer}; | ||
| protozero::pbf_builder<Inner> inner{outer, 1}; | ||
| } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
|
|
||
| // This file must not compile: The tag used to open a submessage must have the | ||
| // enum type of the parent pbf_builder. | ||
|
|
||
| #include <protozero/pbf_builder.hpp> | ||
|
|
||
| #include <string> | ||
|
|
||
| enum class Outer : protozero::pbf_tag_type { | ||
| sub = 1 | ||
| }; | ||
|
|
||
| enum class Inner : protozero::pbf_tag_type { | ||
| value = 2 | ||
| }; | ||
|
|
||
| void test() { | ||
| std::string buffer; | ||
| protozero::pbf_builder<Outer> outer{buffer}; | ||
| protozero::pbf_builder<Inner> inner{outer, Inner::value}; | ||
| } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ | |
| set(UNIT_TESTS data_view | ||
| basic | ||
| buffer | ||
| builder | ||
| endian | ||
| exceptions | ||
| iterators | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
|
|
||
| #include <test.hpp> | ||
|
|
||
| #include <string> | ||
|
|
||
| namespace { | ||
|
|
||
| enum class Outer : protozero::pbf_tag_type { | ||
| sub = 1 | ||
| }; | ||
|
|
||
| enum class Inner : protozero::pbf_tag_type { | ||
| value = 2 | ||
| }; | ||
|
|
||
| const std::string expected{"\x0a\x02\x10\x2a"}; | ||
|
|
||
| } // anonymous namespace | ||
|
|
||
| TEST_CASE("pbf_builder submessage from pbf_builder parent with matching tag type") { | ||
| std::string buffer; | ||
| { | ||
| protozero::pbf_builder<Outer> outer{buffer}; | ||
| protozero::pbf_builder<Inner> inner{outer, Outer::sub}; | ||
| inner.add_uint32(Inner::value, 42); | ||
| } | ||
| REQUIRE(buffer == expected); | ||
| } | ||
|
|
||
| TEST_CASE("pbf_builder submessage from pbf_writer parent with integer tag") { | ||
| std::string buffer; | ||
| { | ||
| protozero::pbf_writer outer{buffer}; | ||
| protozero::pbf_builder<Inner> inner{outer, 1}; | ||
| inner.add_uint32(Inner::value, 42); | ||
| } | ||
| REQUIRE(buffer == expected); | ||
| } | ||
|
|
||
| TEST_CASE("pbf_builder submessage from pbf_builder parent cast to pbf_writer") { | ||
| std::string buffer; | ||
| { | ||
| protozero::pbf_builder<Outer> outer{buffer}; | ||
| protozero::pbf_builder<Inner> inner{static_cast<protozero::pbf_writer&>(outer), 1}; | ||
| inner.add_uint32(Inner::value, 42); | ||
| } | ||
| REQUIRE(buffer == expected); | ||
| } | ||
|
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We now have an explicit
basic_pbf_buildertemplate overload, so we don't have implicit casts anymore.