Skip to content

Refactor parser.c for better testability - #754

Merged
brawner merged 9 commits into
masterfrom
brawner/rcl_yaml-refactor-srcs
Sep 1, 2020
Merged

Refactor parser.c for better testability#754
brawner merged 9 commits into
masterfrom
brawner/rcl_yaml-refactor-srcs

Conversation

@brawner

@brawner brawner commented Aug 19, 2020

Copy link
Copy Markdown
Contributor

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

brawner commented Aug 19, 2020

Copy link
Copy Markdown
Contributor Author

Testing --packages-select rcl_yaml_param_parser

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

@wjwwood

wjwwood commented Aug 24, 2020

Copy link
Copy Markdown
Member

@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.

@hidmic hidmic left a comment

Copy link
Copy Markdown
Contributor

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
Copy Markdown
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
Copy Markdown
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

Comment thread rcl_yaml_param_parser/src/parse.c Outdated

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

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

Copy link
Copy Markdown
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
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Removed

Comment thread rcl_yaml_param_parser/src/parse.c Outdated
ret = RCUTILS_RET_ERROR;
break;
case DATA_TYPE_BOOL:
if (false == is_seq) {

Copy link
Copy Markdown
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
Copy Markdown
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

Comment thread rcl_yaml_param_parser/src/parse.c Outdated
break;
}

memmove(param_name, parameter_ns, params_ns_len);

Copy link
Copy Markdown
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
Copy Markdown
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

brawner commented Aug 29, 2020

Copy link
Copy Markdown
Contributor Author
  • 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
Comment thread rcl_yaml_param_parser/QUALITY_DECLARATION.md Outdated
@brawner

brawner commented Aug 31, 2020

Copy link
Copy Markdown
Contributor Author

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

Comment thread rcl_yaml_param_parser/src/parser.c Outdated
@brawner
brawner force-pushed the brawner/rcl_yaml-refactor-srcs branch from 28a8568 to 3ff206f Compare August 31, 2020 21:01
@brawner

brawner commented Aug 31, 2020

Copy link
Copy Markdown
Contributor Author

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

brawner commented Aug 31, 2020

Copy link
Copy Markdown
Contributor Author
  • Linux Build Status
  • Linux-aarch64 Build Status
  • macOS Build Status
  • Windows Build Status

@hidmic hidmic left a comment

Copy link
Copy Markdown
Contributor

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
Copy Markdown
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
Copy Markdown
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.

Comment thread rcl_yaml_param_parser/src/add_to_arrays.c
Comment thread rcl_yaml_param_parser/src/namespace.c Outdated

tot_len = ns_len + sep_len + name_len + 1U;

cur_ns = allocator.reallocate(cur_ns, tot_len, allocator.state);

Copy link
Copy Markdown
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
Copy Markdown
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

Comment thread rcl_yaml_param_parser/src/namespace.c Outdated
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
Copy Markdown
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
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done

Comment thread rcl_yaml_param_parser/src/namespace.c Outdated
}
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
Copy Markdown
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
Copy Markdown
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

brawner commented Sep 1, 2020

Copy link
Copy Markdown
Contributor Author
  • Linux Build Status
  • Linux-aarch64 Build Status
  • macOS Build Status
  • Windows Build Status

Signed-off-by: Stephen Brawner <brawner@gmail.com>
Signed-off-by: Stephen Brawner <brawner@gmail.com>

squash! Reorder parser.c to match parser.h
Signed-off-by: Stephen Brawner <brawner@gmail.com>
Signed-off-by: Stephen Brawner <brawner@gmail.com>
Signed-off-by: Stephen Brawner <brawner@gmail.com>
Signed-off-by: Stephen Brawner <brawner@gmail.com>
Signed-off-by: Stephen Brawner <brawner@gmail.com>
Signed-off-by: Stephen Brawner <brawner@gmail.com>
Signed-off-by: Stephen Brawner <brawner@gmail.com>
@brawner
brawner force-pushed the brawner/rcl_yaml-refactor-srcs branch from b062da9 to 8d72dc4 Compare September 1, 2020 22:11
@brawner

brawner commented Sep 1, 2020

Copy link
Copy Markdown
Contributor Author

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

brawner commented Sep 1, 2020

Copy link
Copy Markdown
Contributor Author

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

@wjwwood wjwwood left a comment

Copy link
Copy Markdown
Member

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 <Bingtao.Du@sony.com>

Correct code style

Signed-off-by: Ada-King <Bingtao.Du@sony.com>

Modify as suggested

Signed-off-by: Ada-King <Bingtao.Du@sony.com>

Improve test

Signed-off-by: Ada-King <Bingtao.Du@sony.com>

Fix minor flaw

Signed-off-by: Ada-King <Bingtao.Du@sony.com>

Fix minor flaw again

Signed-off-by: Ada-King <Bingtao.Du@sony.com>

Satisfy windows CI

Signed-off-by: Ada-King <Bingtao.Du@sony.com>

Change the match rule for special float

Signed-off-by: Ada-King <Bingtao.Du@sony.com>

Distinguish +.inf and -.inf

Signed-off-by: Ada-King <Bingtao.Du@sony.com>

* Remove unnecessary #include change.

Signed-off-by: Chris Lalancette <clalancette@openrobotics.org>

* Add in two more necessary includes.

Signed-off-by: Chris Lalancette <clalancette@openrobotics.org>

Co-authored-by: Ada-King <Bingtao.Du@sony.com>
Co-authored-by: Chris Lalancette <clalancette@openrobotics.org>
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 <brawner@gmail.com>

* Reorder parser.c to match parser.h

Signed-off-by: Stephen Brawner <brawner@gmail.com>

squash! Reorder parser.c to match parser.h

* Refactor yaml_variant.c for deduplication

Signed-off-by: Stephen Brawner <brawner@gmail.com>

* ADD_VALUE_TO_SIMPLE_ARRAY for deduplication

Signed-off-by: Stephen Brawner <brawner@gmail.com>

* Remove fprintf

Signed-off-by: Stephen Brawner <brawner@gmail.com>

* PR Fixup

Signed-off-by: Stephen Brawner <brawner@gmail.com>

* Move headers to src directory

Signed-off-by: Stephen Brawner <brawner@gmail.com>

* PR Fixup

Signed-off-by: Stephen Brawner <brawner@gmail.com>

* Rebase #780

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

Signed-off-by: Ada-King <Bingtao.Du@sony.com>

Correct code style

Signed-off-by: Ada-King <Bingtao.Du@sony.com>

Modify as suggested

Signed-off-by: Ada-King <Bingtao.Du@sony.com>

Improve test

Signed-off-by: Ada-King <Bingtao.Du@sony.com>

Fix minor flaw

Signed-off-by: Ada-King <Bingtao.Du@sony.com>

Fix minor flaw again

Signed-off-by: Ada-King <Bingtao.Du@sony.com>

Satisfy windows CI

Signed-off-by: Ada-King <Bingtao.Du@sony.com>

Change the match rule for special float

Signed-off-by: Ada-King <Bingtao.Du@sony.com>

Distinguish +.inf and -.inf

Signed-off-by: Ada-King <Bingtao.Du@sony.com>

* Remove unnecessary #include change.

Signed-off-by: Chris Lalancette <clalancette@openrobotics.org>

* Add in two more necessary includes.

Signed-off-by: Chris Lalancette <clalancette@openrobotics.org>

Co-authored-by: Ada-King <Bingtao.Du@sony.com>
Co-authored-by: Chris Lalancette <clalancette@openrobotics.org>
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 <brawner@gmail.com>

* Reorder parser.c to match parser.h

Signed-off-by: Stephen Brawner <brawner@gmail.com>

squash! Reorder parser.c to match parser.h

* Refactor yaml_variant.c for deduplication

Signed-off-by: Stephen Brawner <brawner@gmail.com>

* ADD_VALUE_TO_SIMPLE_ARRAY for deduplication

Signed-off-by: Stephen Brawner <brawner@gmail.com>

* Remove fprintf

Signed-off-by: Stephen Brawner <brawner@gmail.com>

* PR Fixup

Signed-off-by: Stephen Brawner <brawner@gmail.com>

* Move headers to src directory

Signed-off-by: Stephen Brawner <brawner@gmail.com>

* PR Fixup

Signed-off-by: Stephen Brawner <brawner@gmail.com>

* Rebase #780

Signed-off-by: Stephen Brawner <brawner@gmail.com>
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 <brawner@gmail.com>

* Reorder parser.c to match parser.h

Signed-off-by: Stephen Brawner <brawner@gmail.com>

squash! Reorder parser.c to match parser.h

* Refactor yaml_variant.c for deduplication

Signed-off-by: Stephen Brawner <brawner@gmail.com>

* ADD_VALUE_TO_SIMPLE_ARRAY for deduplication

Signed-off-by: Stephen Brawner <brawner@gmail.com>

* Remove fprintf

Signed-off-by: Stephen Brawner <brawner@gmail.com>

* PR Fixup

Signed-off-by: Stephen Brawner <brawner@gmail.com>

* Move headers to src directory

Signed-off-by: Stephen Brawner <brawner@gmail.com>

* PR Fixup

Signed-off-by: Stephen Brawner <brawner@gmail.com>

* Rebase #780

Signed-off-by: Stephen Brawner <brawner@gmail.com>

* Missing includes

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

Signed-off-by: Ada-King <Bingtao.Du@sony.com>

Correct code style

Signed-off-by: Ada-King <Bingtao.Du@sony.com>

Modify as suggested

Signed-off-by: Ada-King <Bingtao.Du@sony.com>

Improve test

Signed-off-by: Ada-King <Bingtao.Du@sony.com>

Fix minor flaw

Signed-off-by: Ada-King <Bingtao.Du@sony.com>

Fix minor flaw again

Signed-off-by: Ada-King <Bingtao.Du@sony.com>

Satisfy windows CI

Signed-off-by: Ada-King <Bingtao.Du@sony.com>

Change the match rule for special float

Signed-off-by: Ada-King <Bingtao.Du@sony.com>

Distinguish +.inf and -.inf

Signed-off-by: Ada-King <Bingtao.Du@sony.com>

* Remove unnecessary #include change.

Signed-off-by: Chris Lalancette <clalancette@openrobotics.org>

* Add in two more necessary includes.

Signed-off-by: Chris Lalancette <clalancette@openrobotics.org>

Co-authored-by: Ada-King <Bingtao.Du@sony.com>
Co-authored-by: Chris Lalancette <clalancette@openrobotics.org>
brawner added a commit that referenced this pull request Oct 6, 2020
* Fix yaml parser error when meets .nan

Signed-off-by: Ada-King <Bingtao.Du@sony.com>

Correct code style

Signed-off-by: Ada-King <Bingtao.Du@sony.com>

Modify as suggested

Signed-off-by: Ada-King <Bingtao.Du@sony.com>

Improve test

Signed-off-by: Ada-King <Bingtao.Du@sony.com>

Fix minor flaw

Signed-off-by: Ada-King <Bingtao.Du@sony.com>

Fix minor flaw again

Signed-off-by: Ada-King <Bingtao.Du@sony.com>

Satisfy windows CI

Signed-off-by: Ada-King <Bingtao.Du@sony.com>

Change the match rule for special float

Signed-off-by: Ada-King <Bingtao.Du@sony.com>

Distinguish +.inf and -.inf

Signed-off-by: Ada-King <Bingtao.Du@sony.com>

* Remove unnecessary #include change.

Signed-off-by: Chris Lalancette <clalancette@openrobotics.org>

* Add in two more necessary includes.

Signed-off-by: Chris Lalancette <clalancette@openrobotics.org>

Co-authored-by: Ada-King <Bingtao.Du@sony.com>
Co-authored-by: Chris Lalancette <clalancette@openrobotics.org>
Signed-off-by: Stephen Brawner <brawner@gmail.com>
brawner added a commit that referenced this pull request Oct 6, 2020
* Fix yaml parser error when meets .nan

Signed-off-by: Ada-King <Bingtao.Du@sony.com>

Correct code style

Signed-off-by: Ada-King <Bingtao.Du@sony.com>

Modify as suggested

Signed-off-by: Ada-King <Bingtao.Du@sony.com>

Improve test

Signed-off-by: Ada-King <Bingtao.Du@sony.com>

Fix minor flaw

Signed-off-by: Ada-King <Bingtao.Du@sony.com>

Fix minor flaw again

Signed-off-by: Ada-King <Bingtao.Du@sony.com>

Satisfy windows CI

Signed-off-by: Ada-King <Bingtao.Du@sony.com>

Change the match rule for special float

Signed-off-by: Ada-King <Bingtao.Du@sony.com>

Distinguish +.inf and -.inf

Signed-off-by: Ada-King <Bingtao.Du@sony.com>

* Remove unnecessary #include change.

Signed-off-by: Chris Lalancette <clalancette@openrobotics.org>

* Add in two more necessary includes.

Signed-off-by: Chris Lalancette <clalancette@openrobotics.org>

Co-authored-by: Ada-King <Bingtao.Du@sony.com>
Co-authored-by: Chris Lalancette <clalancette@openrobotics.org>
Signed-off-by: Stephen Brawner <brawner@gmail.com>

Co-authored-by: Ada-King <Bingtao.Du@sony.com>
Co-authored-by: Chris Lalancette <clalancette@openrobotics.org>
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