[libc-commits] [libc] 0fb3066 - [libc] Add limits.h

Sam James via libc-commits libc-commits at lists.llvm.org
Sun Aug 13 17:37:29 PDT 2023


Author: Alfred Persson Forsberg
Date: 2023-08-14T01:35:44+01:00
New Revision: 0fb3066873fdfbe28dbb6409c58a8bff7cf208c6

URL: https://github.com/llvm/llvm-project/commit/0fb3066873fdfbe28dbb6409c58a8bff7cf208c6
DIFF: https://github.com/llvm/llvm-project/commit/0fb3066873fdfbe28dbb6409c58a8bff7cf208c6.diff

LOG: [libc] Add limits.h

This header contains implementation specific constants.

The compiler already provides its own limits.h with numerical limits
conforming to freestanding ISO C. But it is missing extensions like
POSIX, and does for example not include <linux/limits.h> which is
expected on a Linux system, therefore, an LLVM libc implementation of
limits.h is needed for hosted (__STDC_HOSTED__) environments.

Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D156961

Added: 
    libc/include/limits.h.def
    libc/include/llvm-libc-macros/limits-macros.h

Modified: 
    libc/config/linux/x86_64/headers.txt
    libc/include/CMakeLists.txt
    libc/include/llvm-libc-macros/CMakeLists.txt
    libc/spec/posix.td
    libc/spec/stdc.td

Removed: 
    


################################################################################
diff  --git a/libc/config/linux/x86_64/headers.txt b/libc/config/linux/x86_64/headers.txt
index aaa75a9dd08cbd7..9201d271c41372d 100644
--- a/libc/config/linux/x86_64/headers.txt
+++ b/libc/config/linux/x86_64/headers.txt
@@ -6,6 +6,7 @@ set(TARGET_PUBLIC_HEADERS
     libc.include.fcntl
     libc.include.fenv
     libc.include.inttypes
+    libc.include.limits
     libc.include.math
     libc.include.pthread
     libc.include.sched

diff  --git a/libc/include/CMakeLists.txt b/libc/include/CMakeLists.txt
index ab04b8d2e5ca8c1..6da512d62721712 100644
--- a/libc/include/CMakeLists.txt
+++ b/libc/include/CMakeLists.txt
@@ -66,6 +66,15 @@ add_gen_header(
     .llvm-libc-types.imaxdiv_t
 )
 
+add_gen_header(
+  limits
+  DEF_FILE limits.h.def
+  GEN_HDR limits.h
+  DEPENDS
+    .llvm_libc_common_h
+    .llvm-libc-macros.limits_macros
+)
+
 add_gen_header(
   math
   DEF_FILE math.h.def

diff  --git a/libc/include/limits.h.def b/libc/include/limits.h.def
new file mode 100644
index 000000000000000..56415377a15ba99
--- /dev/null
+++ b/libc/include/limits.h.def
@@ -0,0 +1,17 @@
+//===-- C standard library header 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_LIMITS_H
+#define LLVM_LIBC_LIMITS_H
+
+#include <__llvm-libc-common.h>
+#include <llvm-libc-macros/limits-macros.h>
+
+%%public_api()
+
+#endif // LLVM_LIBC_LIMITS_H

diff  --git a/libc/include/llvm-libc-macros/CMakeLists.txt b/libc/include/llvm-libc-macros/CMakeLists.txt
index 58bbf5e2e431712..323fa3866e602e4 100644
--- a/libc/include/llvm-libc-macros/CMakeLists.txt
+++ b/libc/include/llvm-libc-macros/CMakeLists.txt
@@ -172,3 +172,9 @@ add_header(
   HDR
     wchar-macros.h
 )
+
+add_header(
+  limits_macros
+  HDR
+    limits-macros.h
+)

diff  --git a/libc/include/llvm-libc-macros/limits-macros.h b/libc/include/llvm-libc-macros/limits-macros.h
new file mode 100644
index 000000000000000..01ca850a34b9e83
--- /dev/null
+++ b/libc/include/llvm-libc-macros/limits-macros.h
@@ -0,0 +1,12 @@
+#ifndef __LLVM_LIBC_MACROS_LIMITS_MACROS_H
+#define __LLVM_LIBC_MACROS_LIMITS_MACROS_H
+
+#ifdef __linux__
+#include <linux/limits.h>
+#endif
+
+#ifndef SSIZE_MAX
+#define SSIZE_MAX __LONG_MAX__
+#endif
+
+#endif // __LLVM_LIBC_MACROS_LIMITS_MACROS_H

diff  --git a/libc/spec/posix.td b/libc/spec/posix.td
index d09c7228549dfc7..53146530b102b59 100644
--- a/libc/spec/posix.td
+++ b/libc/spec/posix.td
@@ -225,6 +225,16 @@ def POSIX : StandardSpec<"POSIX"> {
     ]
   >;
 
+  HeaderSpec Limits = HeaderSpec<
+    "limits.h",
+    [
+        Macro<"SSIZE_MAX">,
+    ],
+    [], // Types
+    [], // Enumerations
+    [] // Functions
+  >;
+
   HeaderSpec SysMMan = HeaderSpec<
       "sys/mman.h",
       [
@@ -1395,6 +1405,7 @@ def POSIX : StandardSpec<"POSIX"> {
     Dirent,
     Errno,
     FCntl,
+    Limits,
     PThread,
     Sched,
     Signal,

diff  --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index 96668ccb5558b5b..f012e112ee5dc76 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -821,6 +821,16 @@ def StdC : StandardSpec<"stdc"> {
       ]
   >;
 
+  HeaderSpec Limits = HeaderSpec<
+      "limits.h",
+      [
+        Macro<"LONG_MAX">,
+      ],
+      [], // Types
+      [], // Enumerations
+      [] // Functions
+  >;
+
   HeaderSpec Errno = HeaderSpec<
       "errno.h",
       [
@@ -1138,6 +1148,7 @@ def StdC : StandardSpec<"stdc"> {
     StdIO,
     StdLib,
     IntTypes,
+    Limits,
     SetJmp,
     Signal,
     Threads,


        


More information about the libc-commits mailing list