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

Variable name in macro shadows other variable #285

Closed
felixendres opened this issue Aug 27, 2020 · 3 comments
Closed

Variable name in macro shadows other variable #285

felixendres opened this issue Aug 27, 2020 · 3 comments
Labels
help wanted Extra attention is needed

Comments

@felixendres
Copy link
Contributor

Hi,

The logging code here triggers a lot of warnings on building. It is because a macro uses the very simple variable name ret, which is also used by the rclcpp function borrow_serialized_message (on my system in /opt/ros/dashing/include/rclcpp/message_memory_strategy.hpp L103) which uses that macro.

I would suggest to skip the assignment to the variable, as it is only used once. Alternatively, use a more unique name that is not likely to be used outside of the macro.

The full warning in question:

/opt/ros/dashing/include/rclcpp/message_memory_strategy.hpp:100:16: warning: declaration of ‘ret’ shadows a previous local [-Wshadow]
           auto ret = rmw_serialized_message_fini(msg);
                ^~~
/opt/ros/dashing/include/rclcpp/message_memory_strategy.hpp:93:10: note: shadowed declaration is here
     auto ret = rmw_serialized_message_init(msg, capacity, &rcutils_allocator_);
          ^~~
In file included from /opt/ros/dashing/include/rmw/types.h:28:0,
                 from /opt/ros/dashing/include/rcl/types.h:18,
                 from /opt/ros/dashing/include/rcl/arguments.h:20,
                 from /opt/ros/dashing/include/rcl/context.h:26,
                 from /opt/ros/dashing/include/rcl/guard_condition.h:24,
                 from /opt/ros/dashing/include/rclcpp/executor.hpp:29,
                 from /opt/ros/dashing/include/rclcpp/executors/multi_threaded_executor.hpp:24,
                 from /opt/ros/dashing/include/rclcpp/executors.hpp:21,
                 from /opt/ros/dashing/include/rclcpp/rclcpp.hpp:144,
                 from ../../../../../xxx.h:12,
                 from ../../../../../xxx.cpp:5:
/opt/ros/dashing/include/rcutils/logging.h:543:19: warning: declaration of ‘ret’ shadows a previous local [-Wshadow]
     rcutils_ret_t ret = rcutils_logging_initialize(); \
                   ^
/opt/ros/dashing/include/rcutils/logging_macros.h:68:5: note: in expansion of macro ‘RCUTILS_LOGGING_AUTOINIT’
     RCUTILS_LOGGING_AUTOINIT \
     ^~~~~~~~~~~~~~~~~~~~~~~~
/opt/ros/dashing/include/rcutils/logging_macros.h:994:3: note: in expansion of macro ‘RCUTILS_LOG_COND_NAMED’
   RCUTILS_LOG_COND_NAMED( \
   ^~~~~~~~~~~~~~~~~~~~~~
/opt/ros/dashing/include/rclcpp/message_memory_strategy.hpp:103:13: note: in expansion of macro ‘RCUTILS_LOG_ERROR_NAMED’
             RCUTILS_LOG_ERROR_NAMED(
             ^~~~~~~~~~~~~~~~~~~~~~~
In file included from /opt/ros/dashing/include/rclcpp/subscription.hpp:40:0,
                 from /opt/ros/dashing/include/rclcpp/callback_group.hpp:26,
                 from /opt/ros/dashing/include/rclcpp/any_executable.hpp:20,
                 from /opt/ros/dashing/include/rclcpp/memory_strategy.hpp:24,
                 from /opt/ros/dashing/include/rclcpp/memory_strategies.hpp:18,
                 from /opt/ros/dashing/include/rclcpp/executor.hpp:33,
                 from /opt/ros/dashing/include/rclcpp/executors/multi_threaded_executor.hpp:24,
                 from /opt/ros/dashing/include/rclcpp/executors.hpp:21,
                 from /opt/ros/dashing/include/rclcpp/rclcpp.hpp:144,
                 from ../../../../../xxx.h:12,
                 from ../../../../../xxx.cpp:5:
/opt/ros/dashing/include/rclcpp/message_memory_strategy.hpp:100:16: note: shadowed declaration is here
           auto ret = rmw_serialized_message_fini(msg);
                ^~~

@clalancette
Copy link
Contributor

I would suggest to skip the assignment to the variable, as it is only used once.

Yep, that seems totally reasonable to me. We should also consider wrapping the whole thing in a do..while(0) loop to avoid other macro badness. Can you submit a pull request to do that? Thanks.

@clalancette clalancette added the help wanted Extra attention is needed label Aug 31, 2020
felixendres added a commit to felixendres/rcutils that referenced this issue Sep 11, 2020
Fix issue ros2#285:
Remove the temporary variable in a macro, because the name of the variable may shadow a variable of the macro user.
As suggested by @clalancette, also wrapped it into a do-while wrapping.
felixendres added a commit to felixendres/rcutils that referenced this issue Sep 11, 2020
Fix issue ros2#285:
Remove the temporary variable in a macro, because the name of the variable may shadow a variable of the macro user.
As suggested by @clalancette, also wrapped it into a do-while wrapping.

Signed-off-by: Felix Endres <felix-endres-github@256bit.org>
clalancette pushed a commit to felixendres/rcutils that referenced this issue Oct 1, 2020
Fix issue ros2#285:
Remove the temporary variable in a macro, because the name of the variable may shadow a variable of the macro user.
As suggested by @clalancette, also wrapped it into a do-while wrapping.

Signed-off-by: Felix Endres <felix-endres-github@256bit.org>
clalancette pushed a commit that referenced this issue Oct 5, 2020
Fix issue #285:
Remove the temporary variable in a macro, because the name of the variable may shadow a variable of the macro user.
As suggested by @clalancette, also wrapped it into a do-while wrapping.

While we are in here, also remove the semicolon and make it a true C-statement.

Signed-off-by: Felix Endres <felix-endres-github@256bit.org>
Signed-off-by: Chris Lalancette <clalancette@openrobotics.org>
@fujitatomoya
Copy link
Collaborator

@clalancette I think you can close this.

@clalancette
Copy link
Contributor

@fujitatomoya Good call, fixed by #290, closing. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants