[libc-commits] [libc] [libc] Export configs for `scanf` support (PR #98066)

via libc-commits libc-commits at lists.llvm.org
Mon Jul 8 12:10:16 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libc

Author: Joseph Huber (jhuber6)

<details>
<summary>Changes</summary>

Summary:
This patch adds the options for configuring floating point and index
mode for scanf just like `printf`. Not enabling it on the GPU yet, need
to fix something else first.


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


4 Files Affected:

- (modified) libc/config/config.json (+10) 
- (modified) libc/config/gpu/config.json (+8) 
- (modified) libc/docs/configure.rst (+3) 
- (modified) libc/src/stdio/scanf_core/CMakeLists.txt (+18) 


``````````diff
diff --git a/libc/config/config.json b/libc/config/config.json
index cf80f9993e595..e8feab20175f4 100644
--- a/libc/config/config.json
+++ b/libc/config/config.json
@@ -21,6 +21,16 @@
       "doc": "Disable printing fixed point values in printf and friends."
     }
   },
+  "scanf": {
+    "LIBC_CONF_SCANF_DISABLE_FLOAT": {
+      "value": false,
+      "doc": "Disable parsing floating point values in scanf and friends."
+    },
+    "LIBC_CONF_SCANF_DISABLE_INDEX_MODE": {
+      "value": false,
+      "doc": "Disable index mode in the scanf format string."
+    }
+  },
   "string": {
     "LIBC_CONF_STRING_UNSAFE_WIDE_READ": {
       "value": false,
diff --git a/libc/config/gpu/config.json b/libc/config/gpu/config.json
index 73bf810efbac6..71107d26ea7ab 100644
--- a/libc/config/gpu/config.json
+++ b/libc/config/gpu/config.json
@@ -13,6 +13,14 @@
       "value": false
     }
   },
+  "scanf": {
+    "LIBC_CONF_SCANF_DISABLE_FLOAT": {
+      "value": true
+    },
+    "LIBC_CONF_SCANF_DISABLE_INDEX_MODE": {
+      "value": true
+    }
+  },
   "math": {
     "LIBC_CONF_MATH_OPTIMIZATIONS": {
       "value": "(LIBC_MATH_SKIP_ACCURATE_PASS | LIBC_MATH_SMALL_TABLES | LIBC_MATH_NO_ERRNO | LIBC_MATH_NO_EXCEPT)"
diff --git a/libc/docs/configure.rst b/libc/docs/configure.rst
index d309d5b772038..9c641ef94570f 100644
--- a/libc/docs/configure.rst
+++ b/libc/docs/configure.rst
@@ -42,6 +42,9 @@ to learn about the defaults for your platform and target.
     - ``LIBC_CONF_RAW_MUTEX_DEFAULT_SPIN_COUNT``: Default number of spins before blocking if a mutex is in contention (default to 100).
     - ``LIBC_CONF_RWLOCK_DEFAULT_SPIN_COUNT``: Default number of spins before blocking if a rwlock is in contention (default to 100).
     - ``LIBC_CONF_TIMEOUT_ENSURE_MONOTONICITY``: Automatically adjust timeout to CLOCK_MONOTONIC (default to true). POSIX API may require CLOCK_REALTIME, which can be unstable and leading to unexpected behavior. This option will convert the real-time timestamp to monotonic timestamp relative to the time of call.
+* **"scanf" options**
+    - ``LIBC_CONF_SCANF_DISABLE_FLOAT``: Disable parsing floating point values in scanf and friends.
+    - ``LIBC_CONF_SCANF_DISABLE_INDEX_MODE``: Disable index mode in the scanf format string.
 * **"string" options**
     - ``LIBC_CONF_MEMSET_X86_USE_SOFTWARE_PREFETCHING``: Inserts prefetch for write instructions (PREFETCHW) for memset on x86 to recover performance when hardware prefetcher is disabled.
     - ``LIBC_CONF_STRING_UNSAFE_WIDE_READ``: Read more than a byte at a time to perform byte-string operations like strlen.
diff --git a/libc/src/stdio/scanf_core/CMakeLists.txt b/libc/src/stdio/scanf_core/CMakeLists.txt
index b3445300059fa..e2b49e0c91528 100644
--- a/libc/src/stdio/scanf_core/CMakeLists.txt
+++ b/libc/src/stdio/scanf_core/CMakeLists.txt
@@ -1,8 +1,26 @@
+if(LIBC_CONF_SCANF_DISABLE_FLOAT)
+  list(APPEND scanf_config_copts "-DLIBC_COPT_SCANF_DISABLE_FLOAT")
+endif()
+if(LIBC_CONF_SCANF_DISABLE_INDEX_MODE)
+  list(APPEND scanf_config_copts "-DLIBC_COPT_SCANF_DISABLE_INDEX_MODE")
+endif()
+if(scanf_config_copts)
+  list(PREPEND scanf_config_copts "COMPILE_OPTIONS")
+endif()
+
+add_header_library(
+  scanf_config
+  HDRS
+    scanf_config.h
+  ${scanf_config_copts}
+)
+
 add_header_library(
   core_structs
   HDRS
     core_structs.h
   DEPENDS
+    .scanf_config
     libc.src.__support.CPP.string_view
     libc.src.__support.CPP.bitset
     libc.src.__support.FPUtil.fp_bits

``````````

</details>


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


More information about the libc-commits mailing list