Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor parser.c for better testability #754

Merged
merged 9 commits into from
Sep 1, 2020

Conversation

brawner
Copy link
Contributor

@brawner brawner commented Aug 19, 2020

This PR separates out the parser.c functionality into separate source files with their own headers so additional unit tests can be added to target specific functions. I still need to create the additional unit tests, but this current refactor passes the tests that currently exist, which has about 88% coverage.

Because this PR is so large, I've separated the changes into logical commits. Reviewing each commit individually might be the easiest way to scan through this PR as they illustrate the copy-paste changes I did. They could be their own PRs if that's desired by the reviewers.

Main commits

  • 8872020 Shuffles functions into their own separate files, but doesn't change any actual logic
  • 70e0c99 Reorders parser.c to match parser.h
  • bbb23d2 Creates a couple of macros and reduces some of the duplicate code when copying rcl_yaml_variant_t
  • 5635514 Creates some more macros for deduplicating the add_value_to_array type functions.

Addressing PR Feedback commits

  • e741afa Removes a debug fprintf
  • e617716 Various PR feedback
  • 3ff206f Moves impl headers into src directory

@brawner brawner requested review from anup-pem and wjwwood August 19, 2020 00:48
@brawner
Copy link
Contributor Author

brawner commented Aug 19, 2020

Testing --packages-select rcl_yaml_param_parser

  • Linux Build Status
  • Linux-aarch64 Build Status
  • macOS Build Status
  • Windows Build Status

@wjwwood
Copy link
Member

wjwwood commented Aug 24, 2020

@anup-pem do you have time to review this pull request since you originally wrote this? It would help us make sure it doesn’t change the code in a way that you didn’t intend or that might cause apex issues in the future.

Copy link
Contributor

@hidmic hidmic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall LGTM, though I didn't try to mix and match new and old lines. I'll try again next week, but I guess our best chance of not introducing regressions lies with tests.

RCUTILS_SAFE_FWRITE_TO_STDERR("Error allocating mem\n"); \
return RCUTILS_RET_BAD_ALLOC; \
} \
memmove(val_array->values, tmp_arr, (val_array->size * sizeof(value_type))); \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@brawner perhaps unrelated to this PR, but we can use memcpy instead of memmove. There's no way val_array->values and tmp_arr can overlap.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switched to memcpy


rcl_variant_t * param_value = &(params_st->params[node_idx].parameter_values[parameter_idx]);

// param_value->string_value = rcutils_strdup(value, allocator);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@brawner why keep this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed

ret = RCUTILS_RET_ERROR;
break;
case DATA_TYPE_BOOL:
if (false == is_seq) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@brawner probably preexistent, but:

Suggested change
if (false == is_seq) {
if (!is_seq) {

? Same below.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely preexistent. Switched

break;
}

memmove(param_name, parameter_ns, params_ns_len);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@brawner perhaps unrelated, we can use memcpy instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switched to memcpy

@brawner
Copy link
Contributor Author

brawner commented Aug 29, 2020

  • Linux Build Status
  • Linux-aarch64 Build Status
  • macOS Build Status
  • Windows Build Status

@brawner brawner closed this Aug 29, 2020
@brawner brawner reopened this Aug 29, 2020
@brawner
Copy link
Contributor Author

brawner commented Aug 31, 2020

This will need to be rebased after #765 and #775 are merged

@brawner brawner force-pushed the brawner/rcl_yaml-refactor-srcs branch from 28a8568 to 3ff206f Compare August 31, 2020 21:01
@brawner
Copy link
Contributor Author

brawner commented Aug 31, 2020

Rebased this onto master after recent PR merges. I updated the commits in the description at the top of this PR if the reviews would rather step through the changes that way. They make it easier to see what changes and what doesn't.

@brawner
Copy link
Contributor Author

brawner commented Aug 31, 2020

  • Linux Build Status
  • Linux-aarch64 Build Status
  • macOS Build Status
  • Windows Build Status

Copy link
Contributor

@hidmic hidmic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, comments are minor or can be addressed in another PR.

if (NULL == val_array->values) { \
val_array->values = tmp_arr; \
RCUTILS_SAFE_FWRITE_TO_STDERR("Error allocating mem\n"); \
return RCUTILS_RET_BAD_ALLOC; \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@brawner nit: if ADD_VALUE_TO_SIMPLE_ARRAY took ownership of value and deallocated it in failure cases, add_val_to_* API callers wouldn't have to add a trailing nullity check and deallocation every time one of those functions fails.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The callers already have to handle deallocation of value in several instances. The logic is complicated and this change will lead to larger changes, which I think are out of scope for just adding coverage to this package.


tot_len = ns_len + sep_len + name_len + 1U;

cur_ns = allocator.reallocate(cur_ns, tot_len, allocator.state);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@brawner if allocator.reallocate fails, cur_ns will leak.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving to #780

return RCUTILS_RET_BAD_ALLOC;
}
memmove((cur_ns + ns_len), sep_str, sep_len);
memmove((cur_ns + ns_len + sep_len), name, name_len);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@brawner same about memcpy being cheaper.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

}
if (NULL != last_idx) {
tot_len = ((size_t)(last_idx - cur_ns) + 1U);
cur_ns = allocator.reallocate(cur_ns, tot_len, allocator.state);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@brawner same about cur_ns leaking on allocator.reallocate failure.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@brawner brawner force-pushed the brawner/rcl_yaml-refactor-srcs branch 2 times, most recently from 9a09bd2 to b062da9 Compare September 1, 2020 21:52
@brawner
Copy link
Contributor Author

brawner commented Sep 1, 2020

  • Linux Build Status
  • Linux-aarch64 Build Status
  • macOS Build Status
  • Windows Build Status

Signed-off-by: Stephen Brawner <[email protected]>

squash! Reorder parser.c to match parser.h
Signed-off-by: Stephen Brawner <[email protected]>
Signed-off-by: Stephen Brawner <[email protected]>
Signed-off-by: Stephen Brawner <[email protected]>
Signed-off-by: Stephen Brawner <[email protected]>
Signed-off-by: Stephen Brawner <[email protected]>
@brawner brawner force-pushed the brawner/rcl_yaml-refactor-srcs branch from b062da9 to 8d72dc4 Compare September 1, 2020 22:11
@brawner
Copy link
Contributor Author

brawner commented Sep 1, 2020

Rebasing onto master after merging #780. Testing one last time just because it changes a lot of stuff

  • Linux Build Status
  • Linux-aarch64 Build Status
  • macOS Build Status
  • Windows Build Status

@brawner
Copy link
Contributor Author

brawner commented Sep 1, 2020

All ci.ros2.org tests pass, and tests pass with rpr job. The failures in the Rpr job are unrelated to this PR and have already been addressed in PRs to rcl I believe.

@brawner brawner merged commit 5663f24 into master Sep 1, 2020
@delete-merged-branch delete-merged-branch bot deleted the brawner/rcl_yaml-refactor-srcs branch September 1, 2020 22:36
Copy link
Member

@wjwwood wjwwood left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm (post merge)

brawner added a commit that referenced this pull request Sep 2, 2020
* Fix yaml parser error when meets .nan

Signed-off-by: Ada-King <[email protected]>

Correct code style

Signed-off-by: Ada-King <[email protected]>

Modify as suggested

Signed-off-by: Ada-King <[email protected]>

Improve test

Signed-off-by: Ada-King <[email protected]>

Fix minor flaw

Signed-off-by: Ada-King <[email protected]>

Fix minor flaw again

Signed-off-by: Ada-King <[email protected]>

Satisfy windows CI

Signed-off-by: Ada-King <[email protected]>

Change the match rule for special float

Signed-off-by: Ada-King <[email protected]>

Distinguish +.inf and -.inf

Signed-off-by: Ada-King <[email protected]>

* Remove unnecessary #include change.

Signed-off-by: Chris Lalancette <[email protected]>

* Add in two more necessary includes.

Signed-off-by: Chris Lalancette <[email protected]>

Co-authored-by: Ada-King <[email protected]>
Co-authored-by: Chris Lalancette <[email protected]>
brawner added a commit that referenced this pull request Sep 2, 2020
* Refactor rcl_yaml_param_parser for better testability

Signed-off-by: Stephen Brawner <[email protected]>

* Reorder parser.c to match parser.h

Signed-off-by: Stephen Brawner <[email protected]>

squash! Reorder parser.c to match parser.h

* Refactor yaml_variant.c for deduplication

Signed-off-by: Stephen Brawner <[email protected]>

* ADD_VALUE_TO_SIMPLE_ARRAY for deduplication

Signed-off-by: Stephen Brawner <[email protected]>

* Remove fprintf

Signed-off-by: Stephen Brawner <[email protected]>

* PR Fixup

Signed-off-by: Stephen Brawner <[email protected]>

* Move headers to src directory

Signed-off-by: Stephen Brawner <[email protected]>

* PR Fixup

Signed-off-by: Stephen Brawner <[email protected]>

* Rebase #780

Signed-off-by: Stephen Brawner <[email protected]>
brawner added a commit that referenced this pull request Sep 2, 2020
* Fix yaml parser error when meets .nan

Signed-off-by: Ada-King <[email protected]>

Correct code style

Signed-off-by: Ada-King <[email protected]>

Modify as suggested

Signed-off-by: Ada-King <[email protected]>

Improve test

Signed-off-by: Ada-King <[email protected]>

Fix minor flaw

Signed-off-by: Ada-King <[email protected]>

Fix minor flaw again

Signed-off-by: Ada-King <[email protected]>

Satisfy windows CI

Signed-off-by: Ada-King <[email protected]>

Change the match rule for special float

Signed-off-by: Ada-King <[email protected]>

Distinguish +.inf and -.inf

Signed-off-by: Ada-King <[email protected]>

* Remove unnecessary #include change.

Signed-off-by: Chris Lalancette <[email protected]>

* Add in two more necessary includes.

Signed-off-by: Chris Lalancette <[email protected]>

Co-authored-by: Ada-King <[email protected]>
Co-authored-by: Chris Lalancette <[email protected]>
brawner added a commit that referenced this pull request Oct 1, 2020
* Refactor rcl_yaml_param_parser for better testability

Signed-off-by: Stephen Brawner <[email protected]>

* Reorder parser.c to match parser.h

Signed-off-by: Stephen Brawner <[email protected]>

squash! Reorder parser.c to match parser.h

* Refactor yaml_variant.c for deduplication

Signed-off-by: Stephen Brawner <[email protected]>

* ADD_VALUE_TO_SIMPLE_ARRAY for deduplication

Signed-off-by: Stephen Brawner <[email protected]>

* Remove fprintf

Signed-off-by: Stephen Brawner <[email protected]>

* PR Fixup

Signed-off-by: Stephen Brawner <[email protected]>

* Move headers to src directory

Signed-off-by: Stephen Brawner <[email protected]>

* PR Fixup

Signed-off-by: Stephen Brawner <[email protected]>

* Rebase #780

Signed-off-by: Stephen Brawner <[email protected]>
brawner added a commit that referenced this pull request Oct 6, 2020
* Refactor parser.c for better testability (#754)

* Refactor rcl_yaml_param_parser for better testability

Signed-off-by: Stephen Brawner <[email protected]>

* Reorder parser.c to match parser.h

Signed-off-by: Stephen Brawner <[email protected]>

squash! Reorder parser.c to match parser.h

* Refactor yaml_variant.c for deduplication

Signed-off-by: Stephen Brawner <[email protected]>

* ADD_VALUE_TO_SIMPLE_ARRAY for deduplication

Signed-off-by: Stephen Brawner <[email protected]>

* Remove fprintf

Signed-off-by: Stephen Brawner <[email protected]>

* PR Fixup

Signed-off-by: Stephen Brawner <[email protected]>

* Move headers to src directory

Signed-off-by: Stephen Brawner <[email protected]>

* PR Fixup

Signed-off-by: Stephen Brawner <[email protected]>

* Rebase #780

Signed-off-by: Stephen Brawner <[email protected]>

* Missing includes

Signed-off-by: Stephen Brawner <[email protected]>
brawner added a commit that referenced this pull request Oct 6, 2020
* Fix yaml parser error when meets .nan

Signed-off-by: Ada-King <[email protected]>

Correct code style

Signed-off-by: Ada-King <[email protected]>

Modify as suggested

Signed-off-by: Ada-King <[email protected]>

Improve test

Signed-off-by: Ada-King <[email protected]>

Fix minor flaw

Signed-off-by: Ada-King <[email protected]>

Fix minor flaw again

Signed-off-by: Ada-King <[email protected]>

Satisfy windows CI

Signed-off-by: Ada-King <[email protected]>

Change the match rule for special float

Signed-off-by: Ada-King <[email protected]>

Distinguish +.inf and -.inf

Signed-off-by: Ada-King <[email protected]>

* Remove unnecessary #include change.

Signed-off-by: Chris Lalancette <[email protected]>

* Add in two more necessary includes.

Signed-off-by: Chris Lalancette <[email protected]>

Co-authored-by: Ada-King <[email protected]>
Co-authored-by: Chris Lalancette <[email protected]>
brawner added a commit that referenced this pull request Oct 6, 2020
* Fix yaml parser error when meets .nan

Signed-off-by: Ada-King <[email protected]>

Correct code style

Signed-off-by: Ada-King <[email protected]>

Modify as suggested

Signed-off-by: Ada-King <[email protected]>

Improve test

Signed-off-by: Ada-King <[email protected]>

Fix minor flaw

Signed-off-by: Ada-King <[email protected]>

Fix minor flaw again

Signed-off-by: Ada-King <[email protected]>

Satisfy windows CI

Signed-off-by: Ada-King <[email protected]>

Change the match rule for special float

Signed-off-by: Ada-King <[email protected]>

Distinguish +.inf and -.inf

Signed-off-by: Ada-King <[email protected]>

* Remove unnecessary #include change.

Signed-off-by: Chris Lalancette <[email protected]>

* Add in two more necessary includes.

Signed-off-by: Chris Lalancette <[email protected]>

Co-authored-by: Ada-King <[email protected]>
Co-authored-by: Chris Lalancette <[email protected]>
Signed-off-by: Stephen Brawner <[email protected]>
brawner added a commit that referenced this pull request Oct 6, 2020
* Fix yaml parser error when meets .nan

Signed-off-by: Ada-King <[email protected]>

Correct code style

Signed-off-by: Ada-King <[email protected]>

Modify as suggested

Signed-off-by: Ada-King <[email protected]>

Improve test

Signed-off-by: Ada-King <[email protected]>

Fix minor flaw

Signed-off-by: Ada-King <[email protected]>

Fix minor flaw again

Signed-off-by: Ada-King <[email protected]>

Satisfy windows CI

Signed-off-by: Ada-King <[email protected]>

Change the match rule for special float

Signed-off-by: Ada-King <[email protected]>

Distinguish +.inf and -.inf

Signed-off-by: Ada-King <[email protected]>

* Remove unnecessary #include change.

Signed-off-by: Chris Lalancette <[email protected]>

* Add in two more necessary includes.

Signed-off-by: Chris Lalancette <[email protected]>

Co-authored-by: Ada-King <[email protected]>
Co-authored-by: Chris Lalancette <[email protected]>
Signed-off-by: Stephen Brawner <[email protected]>

Co-authored-by: Ada-King <[email protected]>
Co-authored-by: Chris Lalancette <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants