Skip to content

Commit 691b021

Browse files
committed
Check for name collisions with system header macros.
Fix places where using `I` for an identifier was causing conflicts with complex.h. Fixes NVIDIA#1244.
1 parent ff00c81 commit 691b021

File tree

6 files changed

+79
-15
lines changed

6 files changed

+79
-15
lines changed

cmake/ThrustHeaderTesting.cmake

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@
44
# .inl files are not globbed for, because they are not supposed to be used as public
55
# entrypoints.
66

7+
# Meta target for all configs' header builds:
8+
add_custom_target(thrust.all.headers)
9+
710
foreach(thrust_target IN LISTS THRUST_TARGETS)
811
thrust_get_target_property(config_host ${thrust_target} HOST)
912
thrust_get_target_property(config_device ${thrust_target} DEVICE)
1013
thrust_get_target_property(config_prefix ${thrust_target} PREFIX)
14+
set(config_systems ${config_host} ${config_device})
1115

1216
string(TOLOWER "${config_host}" host_lower)
1317
string(TOLOWER "${config_device}" device_lower)
@@ -115,5 +119,14 @@ foreach(thrust_target IN LISTS THRUST_TARGETS)
115119
target_link_libraries(${headertest_target} PUBLIC ${thrust_target})
116120
thrust_clone_target_properties(${headertest_target} ${thrust_target})
117121

122+
# Disable macro checks on TBB; the TBB atomic implementation uses `I` and
123+
# our checks will issue false errors.
124+
if ("TBB" IN_LIST config_systems)
125+
target_compile_definitions(${headertest_target}
126+
PRIVATE THRUST_IGNORE_MACRO_CHECKS
127+
)
128+
endif()
129+
130+
add_dependencies(thrust.all.headers ${headertest_target})
118131
add_dependencies(${config_prefix}.all ${headertest_target})
119132
endforeach()

cmake/header_test.in

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,55 @@
1+
// This source file checks that:
2+
// 1) Header <thrust/${header}> compiles without error.
3+
// 2) Common macro collisions with platform/system headers are avoided.
4+
5+
// Turn off failures for certain configurations:
16
#define THRUST_CPP11_REQUIRED_NO_ERROR
27
#define THRUST_CPP14_REQUIRED_NO_ERROR
38
#define THRUST_MODERN_GCC_REQUIRED_NO_ERROR
9+
10+
#ifndef THRUST_IGNORE_MACRO_CHECKS
11+
12+
// Define THRUST_MACRO_CHECK(macro, header), which emits a diagnostic indicating
13+
// a potential macro collision and halts.
14+
//
15+
// Hacky way to build a string, but it works on all tested platforms.
16+
#define THRUST_MACRO_CHECK(MACRO, HEADER) \
17+
THRUST_MACRO_CHECK_IMPL(Identifier MACRO should not be used from Thrust \
18+
headers due to conflicts with HEADER.)
19+
20+
// Use raw platform checks instead of the THRUST_HOST_COMPILER macros since we
21+
// don't want to #include any headers other than the one being tested.
22+
//
23+
// This is only implemented for MSVC/GCC/Clang.
24+
#if defined(_MSC_VER) // MSVC
25+
26+
// Fake up an error for MSVC
27+
#define THRUST_MACRO_CHECK_IMPL(msg) \
28+
/* Print message that looks like an error: */ \
29+
__pragma(message(__FILE__ ":" THRUST_MACRO_CHECK_IMPL0(__LINE__) \
30+
": error: " #msg)) \
31+
/* abort compilation due to static_assert or syntax error: */ \
32+
static_assert(false, #msg);
33+
#define THRUST_MACRO_CHECK_IMPL0(x) THRUST_MACRO_CHECK_IMPL1(x)
34+
#define THRUST_MACRO_CHECK_IMPL1(x) #x
35+
36+
#elif defined(__clang__) || defined(__GNUC__)
37+
38+
// GCC/clang are easy:
39+
#define THRUST_MACRO_CHECK_IMPL(msg) THRUST_MACRO_CHECK_IMPL0(GCC error #msg)
40+
#define THRUST_MACRO_CHECK_IMPL0(expr) _Pragma(#expr)
41+
42+
#endif
43+
44+
// complex.h conflicts
45+
#define I THRUST_MACRO_CHECK('I', complex.h)
46+
47+
// windows.h conflicts
48+
// Disabling for now; we use min/max in many places, but since most
49+
// projects build with NOMINMAX this doesn't seem to be high priority to fix.
50+
//#define min(...) THRUST_MACRO_CHECK('min', windows.h)
51+
//#define max(...) THRUST_MACRO_CHECK('max', windows.h)
52+
53+
#endif // THRUST_IGNORE_MACRO_CHECKS
54+
455
#include <thrust/${header}>

dependencies/cub

Submodule cub updated from 2442f44 to 8e09201

thrust/iterator/constant_iterator.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,11 +217,11 @@ template<typename Value,
217217
*
218218
* \see constant_iterator
219219
*/
220-
template<typename V, typename I>
220+
template<typename ValueT, typename IndexT>
221221
inline __host__ __device__
222-
constant_iterator<V,I> make_constant_iterator(V x, I i = int())
222+
constant_iterator<ValueT, IndexT> make_constant_iterator(ValueT x, IndexT i = int())
223223
{
224-
return constant_iterator<V,I>(x, i);
224+
return constant_iterator<ValueT, IndexT>(x, i);
225225
} // end make_constant_iterator()
226226

227227

thrust/system/cuda/detail/sort.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -312,18 +312,18 @@ namespace __merge_sort {
312312
item_type (&items)[ITEMS_PER_THREAD])
313313
{
314314
#pragma unroll
315-
for (int I = 0; I < ITEMS_PER_THREAD; ++I)
315+
for (int i = 0; i < ITEMS_PER_THREAD; ++i)
316316
{
317317
#pragma unroll
318-
for (int J = 1 & I; J < ITEMS_PER_THREAD - 1; J += 2)
318+
for (int j = 1 & i; j < ITEMS_PER_THREAD - 1; j += 2)
319319
{
320-
if (compare_op(keys[J + 1], keys[J]))
320+
if (compare_op(keys[j + 1], keys[j]))
321321
{
322322
using thrust::swap;
323-
swap(keys[J], keys[J + 1]);
323+
swap(keys[j], keys[j + 1]);
324324
if (SORT_ITEMS::value)
325325
{
326-
swap(items[J], items[J + 1]);
326+
swap(items[j], items[j + 1]);
327327
}
328328
}
329329
} // inner loop

thrust/type_traits/integer_sequence.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,11 @@ template <typename T, std::size_t N>
167167
struct make_reversed_integer_sequence_impl;
168168

169169
// Add a new element to the front of an integer_sequence<>.
170-
template <typename T, T I, typename Sequence>
170+
template <typename T, T Value, typename Sequence>
171171
struct integer_sequence_push_front_impl;
172172

173173
// Add a new element to the back of an integer_sequence<>.
174-
template <typename T, T I, typename Sequence>
174+
template <typename T, T Value, typename Sequence>
175175
struct integer_sequence_push_back_impl;
176176

177177
}
@@ -189,14 +189,14 @@ using make_reversed_index_sequence =
189189
make_reversed_integer_sequence<std::size_t, N>;
190190

191191
// Add a new element to the front of an integer_sequence<>.
192-
template <typename T, T I, typename Sequence>
192+
template <typename T, T Value, typename Sequence>
193193
using integer_sequence_push_front =
194-
typename detail::integer_sequence_push_front_impl<T, I, Sequence>::type;
194+
typename detail::integer_sequence_push_front_impl<T, Value, Sequence>::type;
195195

196196
// Add a new element to the back of an integer_sequence<>.
197-
template <typename T, T I, typename Sequence>
197+
template <typename T, T Value, typename Sequence>
198198
using integer_sequence_push_back =
199-
typename detail::integer_sequence_push_back_impl<T, I, Sequence>::type;
199+
typename detail::integer_sequence_push_back_impl<T, Value, Sequence>::type;
200200

201201
///////////////////////////////////////////////////////////////////////////////
202202

0 commit comments

Comments
 (0)