[libc-commits] [libc] [libc] Add limits.h header. (PR #78887)

Nick Desaulniers via libc-commits libc-commits at lists.llvm.org
Tue Jan 23 08:29:54 PST 2024


================
@@ -0,0 +1,247 @@
+//===-- Definition of macros from limits.h --------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef __LLVM_LIBC_MACROS_LIMITS_MACROS_H
+#define __LLVM_LIBC_MACROS_LIMITS_MACROS_H
+
+#if __has_include_next(<limits.h>)
+
+// Suppress `#include_next is a language extension` warnings.
+#ifdef __clang__
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wgnu-include-next"
+#elif defined(__GNUC__) // gcc
+#if !defined _GCC_LIMITS_H_
+#define _GCC_LIMITS_H_
+#endif
+#pragma GCC system_header
+#endif // __clang__, __GNUC__
+
+// Include compiler's header
+#include_next <limits.h>
+
+#ifdef __clang__
+#pragma clang diagnostic pop
+#endif // __clang__
+
+#endif // __has_include_next(<limits.h>)
+
+// Supplement missing macros.
+
----------------
nickdesaulniers wrote:

> Besides some old versions of gcc do not have LLONG_MIN, LLONG_MAX, ULLONG_MAX as in glibc's limits.h comments

Ah, but do we have to support any of those versions of GCC?  If not, I don't think we should add these.

> there are also C23 new macros *_WIDTH that are only added to both clang and gcc recently.

Ok, those are fair to retain with a comment //why//.

> The rest are for completeness.

Right, but we don't //need// them; they should come from the compiler's resource headers which we should always expect to be able to rely on.  If they don't, then I think it would be better to:
1. allow the build to fail loudly because such preprocessor defines are missing.
2. add the bare minimum to get the build back to green, with comments/commit messages explaining why.

The thing I worry most about overriding the compiler's resource headers is that we risk getting these wrong, which may lead to hard to diagnose issues, particularly on platforms other than the host machine.

https://github.com/llvm/llvm-project/pull/78887


More information about the libc-commits mailing list