Skip to content

Commit c488b81

Browse files
authored
Update guard against common Windows preprocessor definitions (#401)
* 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 <[email protected]> * Guard against DELETE being predefined on Windows Motivated by build errors encountered building visualization_msgs/msg/Marker.msg. Signed-off-by: Jacob Perron <[email protected]>
1 parent 36bc907 commit c488b81

File tree

1 file changed

+33
-11
lines changed

1 file changed

+33
-11
lines changed

rosidl_generator_cpp/resource/msg__struct.hpp.em

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,4 @@
11
@# Included from rosidl_generator_cpp/resource/idl__struct.hpp.em
2-
// Protect against ERROR being predefined on Windows, in case somebody defines a
3-
// constant by that name.
4-
#if defined(_WIN32)
5-
#if defined(ERROR)
6-
#undef ERROR
7-
#endif
8-
#if defined(NO_ERROR)
9-
#undef NO_ERROR
10-
#endif
11-
#endif
12-
@
132
@{
143
from rosidl_generator_cpp import create_init_alloc_and_member_lists
154
from rosidl_generator_cpp import escape_string
@@ -31,6 +20,9 @@ from rosidl_parser.definition import OCTET_TYPE
3120
from rosidl_parser.definition import UNSIGNED_INTEGER_TYPES
3221

3322
message_typename = '::'.join(message.structure.namespaced_type.namespaced_name())
23+
24+
# Common Windows macros that may interfere with user defined constants
25+
msvc_common_macros = ('DELETE', 'ERROR', 'NO_ERROR')
3426
}@
3527
@
3628
@#<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
@@ -260,6 +252,15 @@ non_defaulted_zero_initialized_members = [
260252

261253
// constant declarations
262254
@[for constant in message.constants]@
255+
@[ if constant.name in msvc_common_macros]@
256+
// guard against '@(constant.name)' being predefined by MSVC by temporarily undefining it
257+
#if defined(_WIN32)
258+
# if defined(@(constant.name))
259+
# pragma push_macro("@(constant.name)")
260+
# undef @(constant.name)
261+
# endif
262+
#endif
263+
@[ end if]@
263264
@[ if isinstance(constant.type, AbstractString)]@
264265
static const @(MSG_TYPE_TO_CPP['string']) @(constant.name);
265266
@[ elif isinstance(constant.type, AbstractWString)]@
@@ -275,6 +276,12 @@ u@
275276
@(constant.value);
276277
@[ end if]@
277278
@[ end if]@
279+
@[ if constant.name in msvc_common_macros]@
280+
#if defined(_WIN32)
281+
# pragma warning(suppress : 4602)
282+
# pragma pop_macro("@(constant.name)")
283+
#endif
284+
@[ end if]@
278285
@[end for]@
279286

280287
// pointer types
@@ -339,6 +346,15 @@ using @(message.structure.namespaced_type.name) =
339346

340347
// constant definitions
341348
@[for c in message.constants]@
349+
@[ if c.name in msvc_common_macros]@
350+
// guard against '@(c.name)' being predefined by MSVC by temporarily undefining it
351+
#if defined(_WIN32)
352+
# if defined(@(c.name))
353+
# pragma push_macro("@(c.name)")
354+
# undef @(c.name)
355+
# endif
356+
#endif
357+
@[ end if]@
342358
@[ if isinstance(c.type, AbstractString)]@
343359
template<typename ContainerAllocator>
344360
const @(MSG_TYPE_TO_CPP['string'])
@@ -351,6 +367,12 @@ const @(MSG_TYPE_TO_CPP['wstring'])
351367
template<typename ContainerAllocator>
352368
constexpr @(MSG_TYPE_TO_CPP[c.type.typename]) @(message.structure.namespaced_type.name)_<ContainerAllocator>::@(c.name);
353369
@[ end if]@
370+
@[ if c.name in msvc_common_macros]@
371+
#if defined(_WIN32)
372+
# pragma warning(suppress : 4602)
373+
# pragma pop_macro("@(c.name)")
374+
#endif
375+
@[ end if]@
354376
@[end for]@
355377
@
356378
@[for ns in reversed(message.structure.namespaced_type.namespaces)]@

0 commit comments

Comments
 (0)