[libc-commits] [libc] [libc] Clean up sysconf implementation and tests (PR #204130)

via libc-commits libc-commits at lists.llvm.org
Tue Jun 16 05:00:48 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libc

Author: Jeff Bailey (kaladron)

<details>
<summary>Changes</summary>

Refactored sysconf.cpp to use a switch-case block instead of an if-else chain. Delegated the logic for existing options to helper functions (get_page_size, get_nprocessors_conf, get_nprocessors_onln) in an anonymous namespace to reduce cognitive complexity.

Updated file headers in sysconf.cpp and sysconf_test.cpp to the standard three-section format.

Modernized suffix literals in sysconf_test.cpp.

Assisted-by: Automated tooling, human reviewed.

---
Full diff: https://github.com/llvm/llvm-project/pull/204130.diff


2 Files Affected:

- (modified) libc/src/unistd/linux/sysconf.cpp (+38-20) 
- (modified) libc/test/src/unistd/sysconf_test.cpp (+9-4) 


``````````diff
diff --git a/libc/src/unistd/linux/sysconf.cpp b/libc/src/unistd/linux/sysconf.cpp
index dff64cb8140d3..6b808150ab83d 100644
--- a/libc/src/unistd/linux/sysconf.cpp
+++ b/libc/src/unistd/linux/sysconf.cpp
@@ -1,10 +1,15 @@
-//===-- Linux implementation of sysconf -----------------------------------===//
+//===----------------------------------------------------------------------===//
 //
 // 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
 //
 //===----------------------------------------------------------------------===//
+///
+/// \file
+/// Linux implementation of sysconf
+///
+//===----------------------------------------------------------------------===//
 
 #include "src/unistd/sysconf.h"
 
@@ -19,29 +24,42 @@
 
 namespace LIBC_NAMESPACE_DECL {
 
-LLVM_LIBC_FUNCTION(long, sysconf, (int name)) {
-  if (name == _SC_PAGESIZE) {
-    cpp::optional<unsigned long> page_size = auxv::get(AT_PAGESZ);
-    if (page_size)
-      return static_cast<long>(*page_size);
-    libc_errno = EINVAL;
-    return -1;
-  }
+namespace { // Anonymous namespace for internal helpers
+
+long get_page_size() {
+  cpp::optional<unsigned long> page_size = auxv::get(AT_PAGESZ);
+  if (page_size)
+    return static_cast<long>(*page_size);
+  libc_errno = EINVAL;
+  return -1;
+}
 
-  if (name == _SC_NPROCESSORS_CONF)
-    return static_cast<long>(
-        sysinfo::parse_nproc_with_fallback_from(sysinfo::POSSIBLE_NPROC_PATH));
+long get_nprocessors_conf() {
+  return static_cast<long>(
+      sysinfo::parse_nproc_with_fallback_from(sysinfo::POSSIBLE_NPROC_PATH));
+}
 
-  if (name == _SC_NPROCESSORS_ONLN)
-    return static_cast<long>(
-        sysinfo::parse_nproc_with_fallback_from(sysinfo::ONLINE_NPROC_PATH));
+long get_nprocessors_onln() {
+  return static_cast<long>(
+      sysinfo::parse_nproc_with_fallback_from(sysinfo::ONLINE_NPROC_PATH));
+}
 
-  if (name == _SC_THREADS)
-    return _POSIX_THREADS;
+} // anonymous namespace
 
-  // TODO: Complete the rest of the sysconf options.
-  libc_errno = EINVAL;
-  return -1;
+LLVM_LIBC_FUNCTION(long, sysconf, (int name)) {
+  switch (name) {
+  case _SC_PAGESIZE:
+    return get_page_size();
+  case _SC_NPROCESSORS_CONF:
+    return get_nprocessors_conf();
+  case _SC_NPROCESSORS_ONLN:
+    return get_nprocessors_onln();
+  case _SC_THREADS:
+    return _POSIX_THREADS;
+  default:
+    libc_errno = EINVAL;
+    return -1;
+  }
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/src/unistd/sysconf_test.cpp b/libc/test/src/unistd/sysconf_test.cpp
index 0c37eef13b283..16882714cdf1c 100644
--- a/libc/test/src/unistd/sysconf_test.cpp
+++ b/libc/test/src/unistd/sysconf_test.cpp
@@ -1,10 +1,15 @@
-//===-- Unittests for sysconf ---------------------------------------------===//
+//===----------------------------------------------------------------------===//
 //
 // 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
 //
 //===----------------------------------------------------------------------===//
+///
+/// \file
+/// Unittests for sysconf
+///
+//===----------------------------------------------------------------------===//
 
 #include "src/unistd/sysconf.h"
 #include "test/UnitTest/Test.h"
@@ -13,17 +18,17 @@
 
 TEST(LlvmLibcSysconfTest, PagesizeTest) {
   long pagesize = LIBC_NAMESPACE::sysconf(_SC_PAGESIZE);
-  ASSERT_GT(pagesize, 0l);
+  ASSERT_GT(pagesize, 0L);
 }
 
 TEST(LlvmLibcSysconfTest, NprocessorsConfTest) {
   long sysconf_count = LIBC_NAMESPACE::sysconf(_SC_NPROCESSORS_CONF);
-  ASSERT_GT(sysconf_count, 0l);
+  ASSERT_GT(sysconf_count, 0L);
 }
 
 TEST(LlvmLibcSysconfTest, NprocessorsOnlnTest) {
   long sysconf_count = LIBC_NAMESPACE::sysconf(_SC_NPROCESSORS_ONLN);
-  ASSERT_GT(sysconf_count, 0l);
+  ASSERT_GT(sysconf_count, 0L);
 }
 
 TEST(LlvmLibcSysconfTest, ThreadsTest) {

``````````

</details>


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


More information about the libc-commits mailing list