Skip to content

Commit

Permalink
Update guard against common Windows preprocessor definitions (#401)
Browse files Browse the repository at this point in the history
* 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 <jacob@openrobotics.org>

* Guard against DELETE being predefined on Windows

Motivated by build errors encountered building
visualization_msgs/msg/Marker.msg.

Signed-off-by: Jacob Perron <jacob@openrobotics.org>
  • Loading branch information
jacobperron authored Aug 30, 2019
1 parent 36bc907 commit c488b81
Showing 1 changed file with 33 additions and 11 deletions.
44 changes: 33 additions & 11 deletions rosidl_generator_cpp/resource/msg__struct.hpp.em
Original file line number Diff line number Diff line change
@@ -1,15 +1,4 @@
@# 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.
#if defined(_WIN32)
#if defined(ERROR)
#undef ERROR
#endif
#if defined(NO_ERROR)
#undef NO_ERROR
#endif
#endif
@
@{
from rosidl_generator_cpp import create_init_alloc_and_member_lists
from rosidl_generator_cpp import escape_string
Expand All @@ -31,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 = ('DELETE', 'ERROR', 'NO_ERROR')
}@
@
@#<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Expand Down Expand Up @@ -260,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)]@
Expand All @@ -275,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
Expand Down Expand Up @@ -339,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<typename ContainerAllocator>
const @(MSG_TYPE_TO_CPP['string'])
Expand All @@ -351,6 +367,12 @@ const @(MSG_TYPE_TO_CPP['wstring'])
template<typename ContainerAllocator>
constexpr @(MSG_TYPE_TO_CPP[c.type.typename]) @(message.structure.namespaced_type.name)_<ContainerAllocator>::@(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)]@
Expand Down

0 comments on commit c488b81

Please sign in to comment.