Skip to content

[libc] force GCC limits.h to not include_next limits.h #79211

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 17 additions & 0 deletions libc/src/__support/CPP/limits.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,25 @@
#include "src/__support/CPP/type_traits/is_signed.h"
#include "src/__support/macros/attributes.h" // LIBC_INLINE

// Coax GCC to NOT attempt to include_next limits.h from glibc. llvmlibc should
// include this file rather than <limits.h> directy for this reason.
#ifndef __clang__
#define _LIBC_LIMITS_H_
#endif
#include <limits.h> // CHAR_BIT

// GCC's limits.h only defines these for __STDC_VERSION__ >= 199901L, i.e. not
// C++.
#ifndef LLONG_MAX
#define LLONG_MAX __LONG_LONG_MAX__
#endif
#ifndef LLONG_MIN
#define LLONG_MIN (-LONG_MAX - 1LL)
#endif
#ifndef ULLONG_MAX
#define ULLONG_MAX (~0ULL)
#endif

namespace LIBC_NAMESPACE {
namespace cpp {

Expand Down
2 changes: 1 addition & 1 deletion libc/src/__support/FPUtil/ManipulationFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
#include "NormalFloat.h"

#include "src/__support/CPP/bit.h"
#include "src/__support/CPP/limits.h" // INT_MAX, INT_MIN
#include "src/__support/CPP/type_traits.h"
#include "src/__support/FPUtil/FEnvImpl.h"
#include "src/__support/macros/attributes.h"
#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY

#include <limits.h>
#include <math.h>

namespace LIBC_NAMESPACE {
Expand Down
3 changes: 1 addition & 2 deletions libc/src/__support/math_extras.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@
#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_EXTRAS_H
#define LLVM_LIBC_SRC___SUPPORT_MATH_EXTRAS_H

#include "src/__support/CPP/limits.h" // CHAR_BIT
#include "src/__support/CPP/type_traits.h" // is_unsigned_v
#include "src/__support/macros/attributes.h" // LIBC_INLINE
#include "src/__support/macros/config.h" // LIBC_HAS_BUILTIN

#include <limits.h> // CHAR_BIT

namespace LIBC_NAMESPACE {

// Create a bitmask with the count right-most bits set to 1, and all other bits
Expand Down
1 change: 0 additions & 1 deletion libc/src/__support/str_to_integer.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include "src/__support/ctype_utils.h"
#include "src/__support/str_to_num_result.h"
#include "src/errno/libc_errno.h" // For ERANGE
#include <limits.h>

namespace LIBC_NAMESPACE {
namespace internal {
Expand Down
2 changes: 1 addition & 1 deletion libc/src/__support/threads/linux/callonce.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
#include "futex_word.h"

#include "src/__support/CPP/atomic.h"
#include "src/__support/CPP/limits.h"
#include "src/__support/OSUtil/syscall.h" // For syscall functions.
#include "src/__support/threads/callonce.h"

#include <limits.h>
#include <linux/futex.h>
#include <sys/syscall.h> // For syscall numbers.

Expand Down
2 changes: 1 addition & 1 deletion libc/src/threads/linux/call_once.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
#include "Futex.h"

#include "src/__support/CPP/atomic.h"
#include "src/__support/CPP/limits.h"
#include "src/__support/OSUtil/syscall.h" // For syscall functions.
#include "src/__support/common.h"
#include "src/threads/call_once.h"
#include "src/threads/linux/Futex.h"

#include <limits.h>
#include <linux/futex.h>
#include <sys/syscall.h> // For syscall numbers.
#include <threads.h> // For call_once related type definition.
Expand Down
3 changes: 1 addition & 2 deletions libc/src/time/mktime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
//===----------------------------------------------------------------------===//

#include "src/time/mktime.h"
#include "src/__support/CPP/limits.h"
#include "src/__support/common.h"
#include "src/time/time_utils.h"

#include <limits.h>

namespace LIBC_NAMESPACE {

using LIBC_NAMESPACE::time_utils::TimeConstants;
Expand Down
3 changes: 1 addition & 2 deletions libc/src/time/time_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
//===----------------------------------------------------------------------===//

#include "src/time/time_utils.h"
#include "src/__support/CPP/limits.h"
#include "src/__support/common.h"

#include <limits.h>

namespace LIBC_NAMESPACE {
namespace time_utils {

Expand Down
3 changes: 1 addition & 2 deletions libc/test/src/math/ILogbTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@
#ifndef LLVM_LIBC_TEST_SRC_MATH_ILOGBTEST_H
#define LLVM_LIBC_TEST_SRC_MATH_ILOGBTEST_H

#include "src/__support/CPP/limits.h"
#include "src/__support/FPUtil/FPBits.h"
#include "src/__support/FPUtil/ManipulationFunctions.h"
#include "test/UnitTest/Test.h"
#include <math.h>

#include <limits.h>

class LlvmLibcILogbTest : public LIBC_NAMESPACE::testing::Test {
public:
template <typename T> struct ILogbFunc {
Expand Down
2 changes: 1 addition & 1 deletion libc/test/src/math/LdExpTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
#ifndef LLVM_LIBC_TEST_SRC_MATH_LDEXPTEST_H
#define LLVM_LIBC_TEST_SRC_MATH_LDEXPTEST_H

#include "src/__support/CPP/limits.h"
#include "src/__support/FPUtil/FPBits.h"
#include "src/__support/FPUtil/NormalFloat.h"
#include "test/UnitTest/FPMatcher.h"
#include "test/UnitTest/Test.h"

#include <limits.h>
#include <math.h>
#include <stdint.h>

Expand Down
3 changes: 1 addition & 2 deletions libc/test/src/math/smoke/ILogbTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@
#ifndef LLVM_LIBC_TEST_SRC_MATH_ILOGBTEST_H
#define LLVM_LIBC_TEST_SRC_MATH_ILOGBTEST_H

#include "src/__support/CPP/limits.h" // INT_MAX
#include "src/__support/FPUtil/FPBits.h"
#include "src/__support/FPUtil/ManipulationFunctions.h"
#include "test/UnitTest/Test.h"
#include <math.h>

#include <limits.h>

class LlvmLibcILogbTest : public LIBC_NAMESPACE::testing::Test {
public:
template <typename T> struct ILogbFunc {
Expand Down
2 changes: 1 addition & 1 deletion libc/test/src/math/smoke/LdExpTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
#ifndef LLVM_LIBC_TEST_SRC_MATH_LDEXPTEST_H
#define LLVM_LIBC_TEST_SRC_MATH_LDEXPTEST_H

#include "src/__support/CPP/limits.h" // INT_MAX
#include "src/__support/FPUtil/FPBits.h"
#include "src/__support/FPUtil/NormalFloat.h"
#include "test/UnitTest/FPMatcher.h"
#include "test/UnitTest/Test.h"

#include <limits.h>
#include <math.h>
#include <stdint.h>

Expand Down
3 changes: 1 addition & 2 deletions libc/test/src/stdlib/AtoiTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
//
//===----------------------------------------------------------------------===//

#include "src/__support/CPP/limits.h"
#include "src/__support/CPP/type_traits.h"
#include "test/UnitTest/Test.h"

#include <limits.h>

using LIBC_NAMESPACE::cpp::is_same_v;

template <typename ReturnT>
Expand Down
1 change: 0 additions & 1 deletion libc/test/src/stdlib/StrtolTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include "src/errno/libc_errno.h"
#include "test/UnitTest/Test.h"

#include <limits.h>
#include <stddef.h>

using LIBC_NAMESPACE::cpp::is_signed_v;
Expand Down
2 changes: 1 addition & 1 deletion libc/test/src/stdlib/atof_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
//
//===----------------------------------------------------------------------===//

#include "src/__support/CPP/limits.h"
#include "src/__support/FPUtil/FPBits.h"
#include "src/errno/libc_errno.h"
#include "src/stdlib/atof.h"

#include "test/UnitTest/ErrnoSetterMatcher.h"
#include "test/UnitTest/Test.h"

#include <limits.h>
#include <stddef.h>

using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
Expand Down
2 changes: 1 addition & 1 deletion libc/test/src/stdlib/strtod_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//
//===----------------------------------------------------------------------===//

#include "src/__support/CPP/limits.h"
#include "src/__support/FPUtil/FPBits.h"
#include "src/errno/libc_errno.h"
#include "src/stdlib/strtod.h"
Expand All @@ -14,7 +15,6 @@
#include "test/UnitTest/RoundingModeUtils.h"
#include "test/UnitTest/Test.h"

#include <limits.h>
#include <stddef.h>

using LIBC_NAMESPACE::fputil::testing::ForceRoundingModeTest;
Expand Down
2 changes: 1 addition & 1 deletion libc/test/src/stdlib/strtof_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//
//===----------------------------------------------------------------------===//

#include "src/__support/CPP/limits.h"
#include "src/__support/FPUtil/FPBits.h"
#include "src/errno/libc_errno.h"
#include "src/stdlib/strtof.h"
Expand All @@ -14,7 +15,6 @@
#include "test/UnitTest/RoundingModeUtils.h"
#include "test/UnitTest/Test.h"

#include <limits.h>
#include <stddef.h>

using LIBC_NAMESPACE::fputil::testing::ForceRoundingModeTest;
Expand Down
2 changes: 1 addition & 1 deletion libc/test/src/stdlib/strtold_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
//
//===----------------------------------------------------------------------===//

#include "src/__support/CPP/limits.h"
#include "src/__support/FPUtil/FPBits.h"
#include "src/__support/UInt128.h"
#include "src/errno/libc_errno.h"
#include "src/stdlib/strtold.h"

#include "test/UnitTest/Test.h"

#include <limits.h>
#include <stddef.h>

#if defined(LIBC_LONG_DOUBLE_IS_FLOAT64)
Expand Down
5 changes: 4 additions & 1 deletion libc/test/src/time/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ add_libc_unittest(
TmMatcher.h
DEPENDS
libc.src.time.gmtime
libc.src.__support.CPP.limits
)

add_libc_unittest(
Expand Down Expand Up @@ -98,6 +99,7 @@ add_libc_unittest(
20
DEPENDS
libc.src.time.mktime
libc.src.__support.CPP.limits
)

# Sleeping is not supported on older NVPTX architectures.
Expand Down Expand Up @@ -136,6 +138,7 @@ add_libc_test(
clock_test.cpp
DEPENDS
libc.include.time
libc.src.time.clock
libc.src.__support.CPP.limits
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's unclear to me when we need to add these DEPENDS vs not. I didn't for many changes, and the build succeeds. Is that expected? Do we have some convention here that perhaps I'm unaware of?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DEPENDS primarily tells cmake which object files need to be linked. For header files it's technically unnecessary, though we should still do it to make sure that cmake properly rebuilds targets that depend on those header files.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I change this line in particular s/limits/asdfasdfasdf/ literally nothing happens to the build...that doesn't instill confidence that this change is even doing anything...

Copy link
Contributor

@SchrodingerZhu SchrodingerZhu Jan 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After an "unknown" change sometime ago, if you miss the generated header dependency in full build, the header may not be generated in time. This results in most of the full build failures that we have been fixing.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason the build doesn't fail when you add an invalid dependency is because the cmake checks the dependencies when it's generating the targets. If you turn on verbose logging you should see something like -- Skipping unittest libc.test.src.time.clock_test.__unit__ as it has missing deps: libc.src.asdfasdfasdf. when you rerun the cmake, and also if you specifically run that test with ninja libc.test.src.time.clock_test.__unit__ it will say it can't find the target.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that a feature or a bug?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a feature, so that we skip the tests if its entrypoints are not enabled instead of erroring out, or something like that.

libc.src.errno.errno
libc.src.time.clock
)
2 changes: 1 addition & 1 deletion libc/test/src/time/clock_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
//
//===----------------------------------------------------------------------===//

#include "src/__support/CPP/limits.h"
#include "src/time/clock.h"
#include "test/UnitTest/Test.h"

#include <limits.h>
#include <time.h>

TEST(LlvmLibcClockTest, SmokeTest) {
Expand Down
3 changes: 1 addition & 2 deletions libc/test/src/time/gmtime_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
//
//===----------------------------------------------------------------------===//

#include "src/__support/CPP/limits.h"
#include "src/errno/libc_errno.h"
#include "src/time/gmtime.h"
#include "src/time/time_utils.h"
#include "test/UnitTest/ErrnoSetterMatcher.h"
#include "test/UnitTest/Test.h"
#include "test/src/time/TmMatcher.h"

#include <limits.h>

using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
using LIBC_NAMESPACE::time_utils::TimeConstants;
Expand Down
3 changes: 1 addition & 2 deletions libc/test/src/time/mktime_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
//
//===----------------------------------------------------------------------===//

#include "src/__support/CPP/limits.h"
#include "src/time/mktime.h"
#include "src/time/time_utils.h"
#include "test/UnitTest/ErrnoSetterMatcher.h"
#include "test/UnitTest/Test.h"
#include "test/src/time/TmHelper.h"
#include "test/src/time/TmMatcher.h"

#include <limits.h>

using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
using LIBC_NAMESPACE::time_utils::Month;
Expand Down
1 change: 0 additions & 1 deletion libc/test/src/time/time_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include "src/time/time_func.h"
#include "test/UnitTest/Test.h"

#include <limits.h>
#include <time.h>

TEST(LlvmLibcTimeTest, SmokeTest) {
Expand Down