Skip to content

[libc][NFC] Fix -Wdangling-else when compiling libc with gcc >= 7 #67833

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions libc/test/UnitTest/FPMatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,17 +156,21 @@ template <TestCond C, typename T> FPMatcher<T, C> getMatcher(T expectedValue) {
do { \
using namespace LIBC_NAMESPACE::fputil::testing; \
ForceRoundingMode __r1(RoundingMode::Nearest); \
if (__r1.success) \
if (__r1.success) { \
EXPECT_FP_EQ((expected), (actual)); \
} \
ForceRoundingMode __r2(RoundingMode::Upward); \
if (__r2.success) \
if (__r2.success) { \
EXPECT_FP_EQ((expected), (actual)); \
} \
ForceRoundingMode __r3(RoundingMode::Downward); \
if (__r3.success) \
if (__r3.success) { \
EXPECT_FP_EQ((expected), (actual)); \
} \
ForceRoundingMode __r4(RoundingMode::TowardZero); \
if (__r4.success) \
if (__r4.success) { \
EXPECT_FP_EQ((expected), (actual)); \
} \
} while (0)

#endif // LLVM_LIBC_UTILS_UNITTEST_FPMATCHER_H
14 changes: 0 additions & 14 deletions libc/test/UnitTest/LibcTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -378,19 +378,6 @@ CString libc_make_test_file_path_func(const char *file_name);
SuiteClass##_##TestName SuiteClass##_##TestName##_Instance; \
void SuiteClass##_##TestName::Run()

// The GNU compiler emits a warning if nested "if" statements are followed by
// an "else" statement and braces are not used to explicitly disambiguate the
// "else" binding. This leads to problems with code like:
//
// if (gate)
// ASSERT_*(condition) << "Some message";
//
// The "switch (0) case 0:" idiom is used to suppress this.
#define LIBC_AMBIGUOUS_ELSE_BLOCKER_ \
switch (0) \
case 0: \
default:

// If RET_OR_EMPTY is the 'return' keyword we perform an early return which
// corresponds to an assert. If it is empty the execution continues, this
// corresponds to an expect.
Expand All @@ -402,7 +389,6 @@ CString libc_make_test_file_path_func(const char *file_name);
// returning a boolean. This expression is responsible for logging the
// diagnostic in case of failure.
#define LIBC_TEST_SCAFFOLDING_(TEST, RET_OR_EMPTY) \
LIBC_AMBIGUOUS_ELSE_BLOCKER_ \
if (TEST) \
; \
else \
Expand Down
60 changes: 40 additions & 20 deletions libc/test/src/__support/FPUtil/rounding_mode_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,118 +19,138 @@ TEST(LlvmLibcFEnvImplTest, QuickRoundingUpTest) {
using LIBC_NAMESPACE::fputil::fenv_is_round_up;
{
ForceRoundingMode __r(RoundingMode::Upward);
if (__r.success)
if (__r.success) {
ASSERT_TRUE(fenv_is_round_up());
}
}
{
ForceRoundingMode __r(RoundingMode::Downward);
if (__r.success)
if (__r.success) {
ASSERT_FALSE(fenv_is_round_up());
}
}
{
ForceRoundingMode __r(RoundingMode::Nearest);
if (__r.success)
if (__r.success) {
ASSERT_FALSE(fenv_is_round_up());
}
}
{
ForceRoundingMode __r(RoundingMode::TowardZero);
if (__r.success)
if (__r.success) {
ASSERT_FALSE(fenv_is_round_up());
}
}
}

TEST(LlvmLibcFEnvImplTest, QuickRoundingDownTest) {
using LIBC_NAMESPACE::fputil::fenv_is_round_down;
{
ForceRoundingMode __r(RoundingMode::Upward);
if (__r.success)
if (__r.success) {
ASSERT_FALSE(fenv_is_round_down());
}
}
{
ForceRoundingMode __r(RoundingMode::Downward);
if (__r.success)
if (__r.success) {
ASSERT_TRUE(fenv_is_round_down());
}
}
{
ForceRoundingMode __r(RoundingMode::Nearest);
if (__r.success)
if (__r.success) {
ASSERT_FALSE(fenv_is_round_down());
}
}
{
ForceRoundingMode __r(RoundingMode::TowardZero);
if (__r.success)
if (__r.success) {
ASSERT_FALSE(fenv_is_round_down());
}
}
}

TEST(LlvmLibcFEnvImplTest, QuickRoundingNearestTest) {
using LIBC_NAMESPACE::fputil::fenv_is_round_to_nearest;
{
ForceRoundingMode __r(RoundingMode::Upward);
if (__r.success)
if (__r.success) {
ASSERT_FALSE(fenv_is_round_to_nearest());
}
}
{
ForceRoundingMode __r(RoundingMode::Downward);
if (__r.success)
if (__r.success) {
ASSERT_FALSE(fenv_is_round_to_nearest());
}
}
{
ForceRoundingMode __r(RoundingMode::Nearest);
if (__r.success)
if (__r.success) {
ASSERT_TRUE(fenv_is_round_to_nearest());
}
}
{
ForceRoundingMode __r(RoundingMode::TowardZero);
if (__r.success)
if (__r.success) {
ASSERT_FALSE(fenv_is_round_to_nearest());
}
}
}

TEST(LlvmLibcFEnvImplTest, QuickRoundingTowardZeroTest) {
using LIBC_NAMESPACE::fputil::fenv_is_round_to_zero;
{
ForceRoundingMode __r(RoundingMode::Upward);
if (__r.success)
if (__r.success) {
ASSERT_FALSE(fenv_is_round_to_zero());
}
}
{
ForceRoundingMode __r(RoundingMode::Downward);
if (__r.success)
if (__r.success) {
ASSERT_FALSE(fenv_is_round_to_zero());
}
}
{
ForceRoundingMode __r(RoundingMode::Nearest);
if (__r.success)
if (__r.success) {
ASSERT_FALSE(fenv_is_round_to_zero());
}
}
{
ForceRoundingMode __r(RoundingMode::TowardZero);
if (__r.success)
if (__r.success) {
ASSERT_TRUE(fenv_is_round_to_zero());
}
}
}

TEST(LlvmLibcFEnvImplTest, QuickGetRoundTest) {
using LIBC_NAMESPACE::fputil::quick_get_round;
{
ForceRoundingMode __r(RoundingMode::Upward);
if (__r.success)
if (__r.success) {
ASSERT_EQ(quick_get_round(), FE_UPWARD);
}
}
{
ForceRoundingMode __r(RoundingMode::Downward);
if (__r.success)
if (__r.success) {
ASSERT_EQ(quick_get_round(), FE_DOWNWARD);
}
}
{
ForceRoundingMode __r(RoundingMode::Nearest);
if (__r.success)
if (__r.success) {
ASSERT_EQ(quick_get_round(), FE_TONEAREST);
}
}
{
ForceRoundingMode __r(RoundingMode::TowardZero);
if (__r.success)
if (__r.success) {
ASSERT_EQ(quick_get_round(), FE_TOWARDZERO);
}
}
}
9 changes: 6 additions & 3 deletions libc/test/src/spawn/posix_spawn_file_actions_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,15 @@ TEST(LlvmLibcPosixSpawnFileActionsTest, AddActions) {
int action_count = 0;
while (act != nullptr) {
++action_count;
if (action_count == 1)
if (action_count == 1) {
ASSERT_EQ(act->type, LIBC_NAMESPACE::BaseSpawnFileAction::CLOSE);
if (action_count == 2)
}
if (action_count == 2) {
ASSERT_EQ(act->type, LIBC_NAMESPACE::BaseSpawnFileAction::DUP2);
if (action_count == 3)
}
if (action_count == 3) {
ASSERT_EQ(act->type, LIBC_NAMESPACE::BaseSpawnFileAction::OPEN);
}
act = act->next;
}
ASSERT_EQ(action_count, 3);
Expand Down
24 changes: 16 additions & 8 deletions libc/utils/MPFRWrapper/MPFRUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -356,21 +356,25 @@ template <typename T> bool round_to_long(T x, RoundingMode mode, long &result);
{ \
namespace mpfr = LIBC_NAMESPACE::testing::mpfr; \
mpfr::ForceRoundingMode __r1(mpfr::RoundingMode::Nearest); \
if (__r1.success) \
if (__r1.success) { \
EXPECT_MPFR_MATCH(op, input, match_value, ulp_tolerance, \
mpfr::RoundingMode::Nearest); \
} \
mpfr::ForceRoundingMode __r2(mpfr::RoundingMode::Upward); \
if (__r2.success) \
if (__r2.success) { \
EXPECT_MPFR_MATCH(op, input, match_value, ulp_tolerance, \
mpfr::RoundingMode::Upward); \
} \
mpfr::ForceRoundingMode __r3(mpfr::RoundingMode::Downward); \
if (__r3.success) \
if (__r3.success) { \
EXPECT_MPFR_MATCH(op, input, match_value, ulp_tolerance, \
mpfr::RoundingMode::Downward); \
} \
mpfr::ForceRoundingMode __r4(mpfr::RoundingMode::TowardZero); \
if (__r4.success) \
if (__r4.success) { \
EXPECT_MPFR_MATCH(op, input, match_value, ulp_tolerance, \
mpfr::RoundingMode::TowardZero); \
} \
}

#define TEST_MPFR_MATCH_ROUNDING_SILENTLY(op, input, match_value, \
Expand Down Expand Up @@ -400,21 +404,25 @@ template <typename T> bool round_to_long(T x, RoundingMode mode, long &result);
{ \
namespace mpfr = LIBC_NAMESPACE::testing::mpfr; \
mpfr::ForceRoundingMode __r1(mpfr::RoundingMode::Nearest); \
if (__r1.success) \
if (__r1.success) { \
ASSERT_MPFR_MATCH(op, input, match_value, ulp_tolerance, \
mpfr::RoundingMode::Nearest); \
} \
mpfr::ForceRoundingMode __r2(mpfr::RoundingMode::Upward); \
if (__r2.success) \
if (__r2.success) { \
ASSERT_MPFR_MATCH(op, input, match_value, ulp_tolerance, \
mpfr::RoundingMode::Upward); \
} \
mpfr::ForceRoundingMode __r3(mpfr::RoundingMode::Downward); \
if (__r3.success) \
if (__r3.success) { \
ASSERT_MPFR_MATCH(op, input, match_value, ulp_tolerance, \
mpfr::RoundingMode::Downward); \
} \
mpfr::ForceRoundingMode __r4(mpfr::RoundingMode::TowardZero); \
if (__r4.success) \
if (__r4.success) { \
ASSERT_MPFR_MATCH(op, input, match_value, ulp_tolerance, \
mpfr::RoundingMode::TowardZero); \
} \
}

#endif // LLVM_LIBC_UTILS_TESTUTILS_MPFRUTILS_H