Skip to content

Commit 3eb1e6d

Browse files
[libc] Move libc_errno inside of LIBC_NAMESPACE (#80774)
Having libc_errno outside of the namespace causes versioning issues when trying to link the tests against LLVM-libc. Most of this patch is just moving libc_errno inside the namespace in tests. This isn't necessary in the function implementations since those are already inside the namespace.
1 parent 0473e32 commit 3eb1e6d

File tree

123 files changed

+330
-312
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

123 files changed

+330
-312
lines changed

libc/src/errno/libc_errno.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,7 @@ LIBC_NAMESPACE::Errno::operator int() { return errno; }
4444

4545
#endif // LIBC_FULL_BUILD
4646

47+
namespace LIBC_NAMESPACE {
4748
// Define the global `libc_errno` instance.
4849
LIBC_NAMESPACE::Errno libc_errno;
50+
} // namespace LIBC_NAMESPACE

libc/src/errno/libc_errno.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ struct Errno {
3939
void operator=(int);
4040
operator int();
4141
};
42-
} // namespace LIBC_NAMESPACE
4342

4443
extern LIBC_NAMESPACE::Errno libc_errno;
4544

45+
} // namespace LIBC_NAMESPACE
46+
4647
#endif // LLVM_LIBC_SRC_ERRNO_LIBC_ERRNO_H

libc/test/IntegrationTest/test.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,12 @@
6868
////////////////////////////////////////////////////////////////////////////////
6969
// Errno checks.
7070

71-
#define ASSERT_ERRNO_EQ(VAL) ASSERT_EQ(VAL, static_cast<int>(libc_errno))
72-
#define ASSERT_ERRNO_SUCCESS() ASSERT_EQ(0, static_cast<int>(libc_errno))
73-
#define ASSERT_ERRNO_FAILURE() ASSERT_NE(0, static_cast<int>(libc_errno))
71+
#define ASSERT_ERRNO_EQ(VAL) \
72+
ASSERT_EQ(VAL, static_cast<int>(LIBC_NAMESPACE::libc_errno))
73+
#define ASSERT_ERRNO_SUCCESS() \
74+
ASSERT_EQ(0, static_cast<int>(LIBC_NAMESPACE::libc_errno))
75+
#define ASSERT_ERRNO_FAILURE() \
76+
ASSERT_NE(0, static_cast<int>(LIBC_NAMESPACE::libc_errno))
7477

7578
// Integration tests are compiled with -ffreestanding which stops treating
7679
// the main function as a non-overloadable special function. Hence, we use a

libc/test/UnitTest/ErrnoSetterMatcher.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ template <typename T> class ErrnoSetterMatcher : public Matcher<T> {
109109

110110
bool match(T got) {
111111
actual_return = got;
112-
actual_errno = libc_errno;
113-
libc_errno = 0;
112+
actual_errno = LIBC_NAMESPACE::libc_errno;
113+
LIBC_NAMESPACE::libc_errno = 0;
114114
if constexpr (ignore_errno())
115115
return return_cmp.compare(actual_return);
116116
else

libc/test/UnitTest/FPMatcher.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,17 +132,17 @@ template <typename T> struct FPTest : public Test {
132132
#define EXPECT_MATH_ERRNO(expected) \
133133
do { \
134134
if (math_errhandling & MATH_ERRNO) { \
135-
int actual = libc_errno; \
136-
libc_errno = 0; \
135+
int actual = LIBC_NAMESPACE::libc_errno; \
136+
LIBC_NAMESPACE::libc_errno = 0; \
137137
EXPECT_EQ(actual, expected); \
138138
} \
139139
} while (0)
140140

141141
#define ASSERT_MATH_ERRNO(expected) \
142142
do { \
143143
if (math_errhandling & MATH_ERRNO) { \
144-
int actual = libc_errno; \
145-
libc_errno = 0; \
144+
int actual = LIBC_NAMESPACE::libc_errno; \
145+
LIBC_NAMESPACE::libc_errno = 0; \
146146
ASSERT_EQ(actual, expected); \
147147
} \
148148
} while (0)

libc/test/UnitTest/FuchsiaTest.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@
1414
#define WITH_SIGNAL(X) #X
1515

1616
// These macros are used in string unittests.
17-
#define ASSERT_ERRNO_EQ(VAL) ASSERT_EQ(VAL, static_cast<int>(libc_errno))
18-
#define ASSERT_ERRNO_SUCCESS() ASSERT_EQ(0, static_cast<int>(libc_errno))
19-
#define ASSERT_ERRNO_FAILURE() ASSERT_NE(0, static_cast<int>(libc_errno))
17+
#define ASSERT_ERRNO_EQ(VAL) \
18+
ASSERT_EQ(VAL, static_cast<int>(LIBC_NAMESPACE::libc_errno))
19+
#define ASSERT_ERRNO_SUCCESS() \
20+
ASSERT_EQ(0, static_cast<int>(LIBC_NAMESPACE::libc_errno))
21+
#define ASSERT_ERRNO_FAILURE() \
22+
ASSERT_NE(0, static_cast<int>(LIBC_NAMESPACE::libc_errno))
2023

2124
#ifndef EXPECT_DEATH
2225
// Since zxtest has ASSERT_DEATH but not EXPECT_DEATH, wrap calling it

libc/test/UnitTest/LibcTest.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -446,9 +446,12 @@ CString libc_make_test_file_path_func(const char *file_name);
446446
////////////////////////////////////////////////////////////////////////////////
447447
// Errno checks.
448448

449-
#define ASSERT_ERRNO_EQ(VAL) ASSERT_EQ(VAL, static_cast<int>(libc_errno))
450-
#define ASSERT_ERRNO_SUCCESS() ASSERT_EQ(0, static_cast<int>(libc_errno))
451-
#define ASSERT_ERRNO_FAILURE() ASSERT_NE(0, static_cast<int>(libc_errno))
449+
#define ASSERT_ERRNO_EQ(VAL) \
450+
ASSERT_EQ(VAL, static_cast<int>(LIBC_NAMESPACE::libc_errno))
451+
#define ASSERT_ERRNO_SUCCESS() \
452+
ASSERT_EQ(0, static_cast<int>(LIBC_NAMESPACE::libc_errno))
453+
#define ASSERT_ERRNO_FAILURE() \
454+
ASSERT_NE(0, static_cast<int>(LIBC_NAMESPACE::libc_errno))
452455

453456
////////////////////////////////////////////////////////////////////////////////
454457
// Subprocess checks.

libc/test/integration/src/pthread/pthread_create_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ static void run_failure_tests() {
332332
}
333333

334334
TEST_MAIN() {
335-
libc_errno = 0;
335+
LIBC_NAMESPACE::libc_errno = 0;
336336
run_success_tests();
337337
run_failure_tests();
338338
return 0;

libc/test/integration/src/pthread/pthread_join_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ static void nullJoinTest() {
2525
}
2626

2727
TEST_MAIN() {
28-
libc_errno = 0;
28+
LIBC_NAMESPACE::libc_errno = 0;
2929
nullJoinTest();
3030
return 0;
3131
}

libc/test/integration/src/unistd/getcwd_test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ TEST_MAIN(int argc, char **argv, char **envp) {
3131
cwd = LIBC_NAMESPACE::getcwd(buffer, 0);
3232
ASSERT_TRUE(cwd == nullptr);
3333
ASSERT_ERRNO_EQ(EINVAL);
34-
libc_errno = 0;
34+
LIBC_NAMESPACE::libc_errno = 0;
3535

3636
// Insufficient size
3737
cwd = LIBC_NAMESPACE::getcwd(buffer, 2);
3838
ASSERT_TRUE(cwd == nullptr);
39-
int err = libc_errno;
39+
int err = LIBC_NAMESPACE::libc_errno;
4040
ASSERT_EQ(err, ERANGE);
4141

4242
return 0;

libc/test/src/__support/str_to_double_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ TEST(LlvmLibcStrToDblTest, SimpleDecimalConversionExtraTypes) {
9090
uint64_t double_output_mantissa = 0;
9191
uint32_t output_exp2 = 0;
9292

93-
libc_errno = 0;
93+
LIBC_NAMESPACE::libc_errno = 0;
9494
auto double_result =
9595
internal::simple_decimal_conversion<double>("123456789012345678900");
9696

libc/test/src/__support/str_to_float_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ TEST(LlvmLibcStrToFltTest, SimpleDecimalConversionExtraTypes) {
4646
uint32_t float_output_mantissa = 0;
4747
uint32_t output_exp2 = 0;
4848

49-
libc_errno = 0;
49+
LIBC_NAMESPACE::libc_errno = 0;
5050
auto float_result =
5151
internal::simple_decimal_conversion<float>("123456789012345678900");
5252
float_output_mantissa = float_result.num.mantissa;

libc/test/src/__support/str_to_fp_test.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ template <typename T> struct LlvmLibcStrToFloatTest : public testing::Test {
6666
const int expectedErrno = 0) {
6767
StorageType actual_output_mantissa = 0;
6868
uint32_t actual_output_exp2 = 0;
69-
libc_errno = 0;
69+
LIBC_NAMESPACE::libc_errno = 0;
7070

7171
auto result = internal::simple_decimal_conversion<T>(numStart);
7272

libc/test/src/dirent/dirent_test.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,17 @@ TEST(LlvmLibcDirentTest, SimpleOpenAndRead) {
5555
}
5656

5757
TEST(LlvmLibcDirentTest, OpenNonExistentDir) {
58-
libc_errno = 0;
58+
LIBC_NAMESPACE::libc_errno = 0;
5959
::DIR *dir = LIBC_NAMESPACE::opendir("___xyz123__.non_existent__");
6060
ASSERT_TRUE(dir == nullptr);
6161
ASSERT_ERRNO_EQ(ENOENT);
62-
libc_errno = 0;
62+
LIBC_NAMESPACE::libc_errno = 0;
6363
}
6464

6565
TEST(LlvmLibcDirentTest, OpenFile) {
66-
libc_errno = 0;
66+
LIBC_NAMESPACE::libc_errno = 0;
6767
::DIR *dir = LIBC_NAMESPACE::opendir("testdata/file1.txt");
6868
ASSERT_TRUE(dir == nullptr);
6969
ASSERT_ERRNO_EQ(ENOTDIR);
70-
libc_errno = 0;
70+
LIBC_NAMESPACE::libc_errno = 0;
7171
}

libc/test/src/errno/errno_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@
1111

1212
TEST(LlvmLibcErrnoTest, Basic) {
1313
int test_val = 123;
14-
libc_errno = test_val;
14+
LIBC_NAMESPACE::libc_errno = test_val;
1515
ASSERT_ERRNO_EQ(test_val);
1616
}

libc/test/src/math/RoundToIntegerTest.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class RoundToIntegerTestTemplate : public LIBC_NAMESPACE::testing::Test {
5151

5252
void test_one_input(RoundToIntegerFunc func, F input, I expected,
5353
bool expectError) {
54-
libc_errno = 0;
54+
LIBC_NAMESPACE::libc_errno = 0;
5555
LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT);
5656

5757
ASSERT_EQ(func(input), expected);

libc/test/src/math/acosf_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
2222
using LlvmLibcAcosfTest = LIBC_NAMESPACE::testing::FPTest<float>;
2323

2424
TEST_F(LlvmLibcAcosfTest, SpecialNumbers) {
25-
libc_errno = 0;
25+
LIBC_NAMESPACE::libc_errno = 0;
2626

2727
EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::acosf(aNaN));
2828
EXPECT_MATH_ERRNO(0);

libc/test/src/math/acoshf_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ using LlvmLibcAcoshfTest = LIBC_NAMESPACE::testing::FPTest<float>;
2222
namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
2323

2424
TEST_F(LlvmLibcAcoshfTest, SpecialNumbers) {
25-
libc_errno = 0;
25+
LIBC_NAMESPACE::libc_errno = 0;
2626

2727
EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::acoshf(aNaN));
2828
EXPECT_MATH_ERRNO(0);

libc/test/src/math/asinf_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ using LlvmLibcAsinfTest = LIBC_NAMESPACE::testing::FPTest<float>;
2323
namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
2424

2525
TEST_F(LlvmLibcAsinfTest, SpecialNumbers) {
26-
libc_errno = 0;
26+
LIBC_NAMESPACE::libc_errno = 0;
2727

2828
EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::asinf(aNaN));
2929
EXPECT_MATH_ERRNO(0);

libc/test/src/math/asinhf_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ using LlvmLibcAsinhfTest = LIBC_NAMESPACE::testing::FPTest<float>;
2222
namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
2323

2424
TEST_F(LlvmLibcAsinhfTest, SpecialNumbers) {
25-
libc_errno = 0;
25+
LIBC_NAMESPACE::libc_errno = 0;
2626

2727
EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::asinhf(aNaN));
2828
EXPECT_MATH_ERRNO(0);

libc/test/src/math/atanf_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ using LlvmLibcAtanfTest = LIBC_NAMESPACE::testing::FPTest<float>;
2222
namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
2323

2424
TEST_F(LlvmLibcAtanfTest, SpecialNumbers) {
25-
libc_errno = 0;
25+
LIBC_NAMESPACE::libc_errno = 0;
2626
LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT);
2727
EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::atanf(aNaN));
2828
EXPECT_FP_EXCEPTION(0);

libc/test/src/math/atanhf_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
2323

2424
TEST_F(LlvmLibcAtanhfTest, SpecialNumbers) {
2525
using Sign = LIBC_NAMESPACE::fputil::Sign;
26-
libc_errno = 0;
26+
LIBC_NAMESPACE::libc_errno = 0;
2727
LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT);
2828
EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::atanhf(aNaN));
2929
EXPECT_FP_EXCEPTION(0);

libc/test/src/math/cosf_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ using LlvmLibcCosfTest = LIBC_NAMESPACE::testing::FPTest<float>;
2424
namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
2525

2626
TEST_F(LlvmLibcCosfTest, SpecialNumbers) {
27-
libc_errno = 0;
27+
LIBC_NAMESPACE::libc_errno = 0;
2828

2929
EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::cosf(aNaN));
3030
EXPECT_MATH_ERRNO(0);

libc/test/src/math/coshf_test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ using LlvmLibcCoshfTest = LIBC_NAMESPACE::testing::FPTest<float>;
2323
namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
2424

2525
TEST_F(LlvmLibcCoshfTest, SpecialNumbers) {
26-
libc_errno = 0;
26+
LIBC_NAMESPACE::libc_errno = 0;
2727

2828
EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::coshf(aNaN));
2929
EXPECT_MATH_ERRNO(0);
@@ -42,7 +42,7 @@ TEST_F(LlvmLibcCoshfTest, SpecialNumbers) {
4242
}
4343

4444
TEST_F(LlvmLibcCoshfTest, Overflow) {
45-
libc_errno = 0;
45+
LIBC_NAMESPACE::libc_errno = 0;
4646
EXPECT_FP_EQ_WITH_EXCEPTION(
4747
inf, LIBC_NAMESPACE::coshf(FPBits(0x7f7fffffU).get_val()), FE_OVERFLOW);
4848
EXPECT_MATH_ERRNO(ERANGE);

libc/test/src/math/exp10_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ TEST_F(LlvmLibcExp10Test, InDoubleRange) {
106106
double x = FPBits(v).get_val();
107107
if (isnan(x) || isinf(x) || x < 0.0)
108108
continue;
109-
libc_errno = 0;
109+
LIBC_NAMESPACE::libc_errno = 0;
110110
double result = LIBC_NAMESPACE::exp10(x);
111111
++cc;
112112
if (isnan(result) || isinf(result))

libc/test/src/math/exp10f_test.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ using LlvmLibcExp10fTest = LIBC_NAMESPACE::testing::FPTest<float>;
2121
namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
2222

2323
TEST_F(LlvmLibcExp10fTest, SpecialNumbers) {
24-
libc_errno = 0;
24+
LIBC_NAMESPACE::libc_errno = 0;
2525

2626
EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::exp10f(aNaN));
2727
EXPECT_MATH_ERRNO(0);
@@ -40,7 +40,7 @@ TEST_F(LlvmLibcExp10fTest, SpecialNumbers) {
4040
}
4141

4242
TEST_F(LlvmLibcExp10fTest, Overflow) {
43-
libc_errno = 0;
43+
LIBC_NAMESPACE::libc_errno = 0;
4444
EXPECT_FP_EQ_WITH_EXCEPTION(
4545
inf, LIBC_NAMESPACE::exp10f(FPBits(0x7f7fffffU).get_val()), FE_OVERFLOW);
4646
EXPECT_MATH_ERRNO(ERANGE);
@@ -55,7 +55,7 @@ TEST_F(LlvmLibcExp10fTest, Overflow) {
5555
}
5656

5757
TEST_F(LlvmLibcExp10fTest, Underflow) {
58-
libc_errno = 0;
58+
LIBC_NAMESPACE::libc_errno = 0;
5959
EXPECT_FP_EQ_WITH_EXCEPTION(
6060
0.0f, LIBC_NAMESPACE::exp10f(FPBits(0xff7fffffU).get_val()),
6161
FE_UNDERFLOW);
@@ -97,7 +97,7 @@ TEST_F(LlvmLibcExp10fTest, TrickyInputs) {
9797
0x41200000, // x = 10.0f
9898
};
9999
for (int i = 0; i < N; ++i) {
100-
libc_errno = 0;
100+
LIBC_NAMESPACE::libc_errno = 0;
101101
float x = FPBits(INPUTS[i]).get_val();
102102
EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Exp10, x,
103103
LIBC_NAMESPACE::exp10f(x), 0.5);
@@ -113,14 +113,14 @@ TEST_F(LlvmLibcExp10fTest, InFloatRange) {
113113
float x = FPBits(v).get_val();
114114
if (isnan(x) || isinf(x))
115115
continue;
116-
libc_errno = 0;
116+
LIBC_NAMESPACE::libc_errno = 0;
117117
float result = LIBC_NAMESPACE::exp10f(x);
118118

119119
// If the computation resulted in an error or did not produce valid result
120120
// in the single-precision floating point range, then ignore comparing with
121121
// MPFR result as MPFR can still produce valid results because of its
122122
// wider precision.
123-
if (isnan(result) || isinf(result) || libc_errno != 0)
123+
if (isnan(result) || isinf(result) || LIBC_NAMESPACE::libc_errno != 0)
124124
continue;
125125
ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Exp10, x,
126126
LIBC_NAMESPACE::exp10f(x), 0.5);

libc/test/src/math/exp2_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ TEST_F(LlvmLibcExp2Test, InDoubleRange) {
8181
double x = FPBits(v).get_val();
8282
if (isnan(x) || isinf(x) || x < 0.0)
8383
continue;
84-
libc_errno = 0;
84+
LIBC_NAMESPACE::libc_errno = 0;
8585
double result = LIBC_NAMESPACE::exp2(x);
8686
++cc;
8787
if (isnan(result) || isinf(result))

0 commit comments

Comments
 (0)