From 828006dd04e61f759d932da4916975f9d81b80b2 Mon Sep 17 00:00:00 2001 From: Jacob Perron Date: Thu, 22 Aug 2019 16:31:20 -0700 Subject: [PATCH 1/3] Guard against DELETE being predefined on Windows Motivated by build errors encountered building visualization_msgs/msg/Marker.msg. Signed-off-by: Jacob Perron --- rosidl_generator_cpp/resource/msg__struct.hpp.em | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/rosidl_generator_cpp/resource/msg__struct.hpp.em b/rosidl_generator_cpp/resource/msg__struct.hpp.em index 2a880f45b..28f37b96c 100644 --- a/rosidl_generator_cpp/resource/msg__struct.hpp.em +++ b/rosidl_generator_cpp/resource/msg__struct.hpp.em @@ -1,6 +1,6 @@ @# Included from rosidl_generator_cpp/resource/idl__struct.hpp.em -// Protect against ERROR being predefined on Windows, in case somebody defines a -// constant by that name. +// Protect against common names being predefined on Windows, in case somebody defines +// constants by these names. #if defined(_WIN32) #if defined(ERROR) #undef ERROR @@ -8,6 +8,9 @@ #if defined(NO_ERROR) #undef NO_ERROR #endif + #if defined(DELETE) + #undef DELETE + #endif #endif @ @{ From f5be05245e95f4e7ee08265b7b01e2f00e6202cf Mon Sep 17 00:00:00 2001 From: Jacob Perron Date: Thu, 29 Aug 2019 13:49:05 -0700 Subject: [PATCH 2/3] Update guard against common Windows preprocessor definitions Rather than undefining unconditionally at the top of the file, only undefine if a constant by one of the common names is defined. Also, redefine the macro after the constant has been declared using the pragma push_macro and pop_macro. Suppress a potential warning for popping a macro that was not previously pushed. Signed-off-by: Jacob Perron --- .../resource/msg__struct.hpp.em | 47 +++++++++++++------ 1 file changed, 33 insertions(+), 14 deletions(-) diff --git a/rosidl_generator_cpp/resource/msg__struct.hpp.em b/rosidl_generator_cpp/resource/msg__struct.hpp.em index 28f37b96c..eac0e6e06 100644 --- a/rosidl_generator_cpp/resource/msg__struct.hpp.em +++ b/rosidl_generator_cpp/resource/msg__struct.hpp.em @@ -1,18 +1,4 @@ @# Included from rosidl_generator_cpp/resource/idl__struct.hpp.em -// Protect against common names being predefined on Windows, in case somebody defines -// constants by these names. -#if defined(_WIN32) - #if defined(ERROR) - #undef ERROR - #endif - #if defined(NO_ERROR) - #undef NO_ERROR - #endif - #if defined(DELETE) - #undef DELETE - #endif -#endif -@ @{ from rosidl_generator_cpp import create_init_alloc_and_member_lists from rosidl_generator_cpp import escape_string @@ -34,6 +20,9 @@ from rosidl_parser.definition import OCTET_TYPE from rosidl_parser.definition import UNSIGNED_INTEGER_TYPES message_typename = '::'.join(message.structure.namespaced_type.namespaced_name()) + +# Common Windows macros that may interfere with user defined constants +msvc_common_macros = ('ERROR', 'NO_ERROR', 'DELETE') }@ @ @#<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @@ -263,6 +252,15 @@ non_defaulted_zero_initialized_members = [ // constant declarations @[for constant in message.constants]@ +@[ if constant.name in msvc_common_macros]@ + // guard against '@(constant.name)' being predefined by MSVC by temporarily undefining it +#if defined(_WIN32) +# if defined(@(constant.name)) +# pragma push_macro("@(constant.name)") +# undef @(constant.name) +# endif +#endif +@[ end if]@ @[ if isinstance(constant.type, AbstractString)]@ static const @(MSG_TYPE_TO_CPP['string']) @(constant.name); @[ elif isinstance(constant.type, AbstractWString)]@ @@ -278,6 +276,12 @@ u@ @(constant.value); @[ end if]@ @[ end if]@ +@[ if constant.name in msvc_common_macros]@ +#if defined(_WIN32) +# pragma warning(suppress : 4602) +# pragma pop_macro("@(constant.name)") +#endif +@[ end if]@ @[end for]@ // pointer types @@ -342,6 +346,15 @@ using @(message.structure.namespaced_type.name) = // constant definitions @[for c in message.constants]@ +@[ if c.name in msvc_common_macros]@ +// guard against '@(c.name)' being predefined by MSVC by temporarily undefining it +#if defined(_WIN32) +# if defined(@(c.name)) +# pragma push_macro("@(c.name)") +# undef @(c.name) +# endif +#endif +@[ end if]@ @[ if isinstance(c.type, AbstractString)]@ template const @(MSG_TYPE_TO_CPP['string']) @@ -354,6 +367,12 @@ const @(MSG_TYPE_TO_CPP['wstring']) template constexpr @(MSG_TYPE_TO_CPP[c.type.typename]) @(message.structure.namespaced_type.name)_::@(c.name); @[ end if]@ +@[ if c.name in msvc_common_macros]@ +#if defined(_WIN32) +# pragma warning(suppress : 4602) +# pragma pop_macro("@(c.name)") +#endif +@[ end if]@ @[end for]@ @ @[for ns in reversed(message.structure.namespaced_type.namespaces)]@ From 9b902a8d6b714eee132b2a217d7d5aa0d19b9580 Mon Sep 17 00:00:00 2001 From: Jacob Perron Date: Thu, 29 Aug 2019 18:06:52 -0700 Subject: [PATCH 3/3] Alphabetize Signed-off-by: Jacob Perron --- rosidl_generator_cpp/resource/msg__struct.hpp.em | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rosidl_generator_cpp/resource/msg__struct.hpp.em b/rosidl_generator_cpp/resource/msg__struct.hpp.em index eac0e6e06..2e563fb34 100644 --- a/rosidl_generator_cpp/resource/msg__struct.hpp.em +++ b/rosidl_generator_cpp/resource/msg__struct.hpp.em @@ -22,7 +22,7 @@ from rosidl_parser.definition import UNSIGNED_INTEGER_TYPES message_typename = '::'.join(message.structure.namespaced_type.namespaced_name()) # Common Windows macros that may interfere with user defined constants -msvc_common_macros = ('ERROR', 'NO_ERROR', 'DELETE') +msvc_common_macros = ('DELETE', 'ERROR', 'NO_ERROR') }@ @ @#<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<