Skip to content

Commit

Permalink
meta/pp: fix preprocessor UB in tests
Browse files Browse the repository at this point in the history
Usually I would've amended the previous commit, but it's useful to keep
it separate in this case as it makes easier to deal with this problem in
the future. Previously I didn't use a macro, instead I used
static_assert directly. However, using #if inside a function-like macro
invocation is UB for no good reason and Visual C++'s preprocessor
refuses to work correctly.

warning: C5101: use of preprocessor directive in function-like macro argument list is undefined behavior
  • Loading branch information
Hedede committed Mar 26, 2024
1 parent 3fe06a5 commit 5597131
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
7 changes: 5 additions & 2 deletions meta/tests/pp/foldr.c++
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@ ASSERT_EQUAL(
(AW_FOLD_RIGHT(AW_IDENTITY, AW_TO_STR, a, b, c)),
("a", "b", "c"));

#if AW_COMPILER == AW_COMPILER_GCC
ASSERT_EQUAL(
(AW_FOLD_RIGHT(AW_SEP_SEMI, AW_SEP_SPACE AW_DEFER, (int,a), (float,b), (char,c) );),
#if AW_COMPILER == AW_COMPILER_GCC
(int a ; float b ; char c ;)
);
#else
ASSERT_EQUAL(
(AW_FOLD_RIGHT(AW_SEP_SEMI, AW_SEP_SPACE AW_DEFER, (int,a), (float,b), (char,c) );),
(int a; float b; char c;)
#endif
);
#endif

ASSERT_EQUAL(
(AW_FOREACH(AW_TO_STR, a, b, c)),
Expand Down
21 changes: 15 additions & 6 deletions meta/tests/pp/struct.c++
Original file line number Diff line number Diff line change
Expand Up @@ -6,52 +6,61 @@

// TODO: strip spaces to eliminate differences in the preprocessor behaviour

#if AW_COMPILER == AW_COMPILER_GCC
ASSERT_EQUAL(
(AW_LIST_MEMBER_TYPES(NAME, AW_TUPLE(int, x), AW_TUPLE(int, y))),
#if AW_COMPILER == AW_COMPILER_GCC
(int , int)
);
#else
ASSERT_EQUAL(
(AW_LIST_MEMBER_TYPES(NAME, AW_TUPLE(int, x), AW_TUPLE(int, y))),
(int, int)
#endif
);
#endif

#if AW_COMPILER == AW_COMPILER_GCC
ASSERT_EQUAL(
(AW_DEFINE_STRUCT(s, AW_TUPLE(int, x), AW_TUPLE(int, y))),
#if AW_COMPILER == AW_COMPILER_GCC
(struct s {
::aw::type_t<int> x ;
::aw::type_t<int> y ;
using tuple_type = std::tuple<int , int >;
constexpr operator tuple_type() { return { x , y }; }
})
);
#else
ASSERT_EQUAL(
(AW_DEFINE_STRUCT(s, AW_TUPLE(int, x), AW_TUPLE(int, y))),
(struct s {
::aw::type_t<int> x;
::aw::type_t<int> y;
using tuple_type = std::tuple<int, int>;
constexpr operator tuple_type() { return { x, y }; }
})
#endif
);
#endif

#if AW_COMPILER == AW_COMPILER_GCC
ASSERT_EQUAL(
(AW_DEFINE_STRUCT( str, (int, x), (int, y) )),
#if AW_COMPILER == AW_COMPILER_GCC
(struct str {
::aw::type_t<int> x ;
::aw::type_t<int> y ;
using tuple_type = std::tuple<int , int >;
constexpr operator tuple_type() { return { x , y }; }
})
);
#else
ASSERT_EQUAL(
(AW_DEFINE_STRUCT( str, (int, x), (int, y) )),
(struct str {
::aw::type_t<int> x;
::aw::type_t<int> y;
using tuple_type = std::tuple<int, int>;
constexpr operator tuple_type() { return { x, y }; }
})
#endif
);
#endif

AW_DEFINE_STRUCT(s, AW_TUPLE(int, x), AW_TUPLE(int, y));

Expand Down

0 comments on commit 5597131

Please sign in to comment.