Skip to content

[libc] Add vsscanf function #97529

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 2 commits into from

Conversation

izaakschroeder
Copy link
Contributor

@izaakschroeder izaakschroeder commented Jul 3, 2024

@llvmbot llvmbot added the libc label Jul 3, 2024
@izaakschroeder izaakschroeder mentioned this pull request Jul 3, 2024
39 tasks
@llvmbot
Copy link
Member

llvmbot commented Jul 3, 2024

@llvm/pr-subscribers-libc

Author: Izaak Schroeder (izaakschroeder)

Changes

Still needs proper implementation, but stubs are here.


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

4 Files Affected:

  • (modified) libc/spec/stdc.td (+7)
  • (modified) libc/src/stdio/CMakeLists.txt (+10)
  • (added) libc/src/stdio/vsscanf.cpp (+28)
  • (added) libc/src/stdio/vsscanf.h (+21)
diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index 9ff40bf76700c..0d35a1e3e6203 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -968,6 +968,13 @@ def StdC : StandardSpec<"stdc"> {
               RetValSpec<IntType>,
               [ArgSpec<IntType>, ArgSpec<FILEPtr>]
           >,
+          FunctionSpec<
+              "vsscanf",
+              RetValSpec<IntType>,
+              [ArgSpec<ConstCharPtr>,
+               ArgSpec<ConstCharPtr>,
+               ArgSpec<VaListType>]
+          >,
       ],
       [
           ObjectSpec<
diff --git a/libc/src/stdio/CMakeLists.txt b/libc/src/stdio/CMakeLists.txt
index a659d9e847a9e..3f1d97cad6afd 100644
--- a/libc/src/stdio/CMakeLists.txt
+++ b/libc/src/stdio/CMakeLists.txt
@@ -203,6 +203,16 @@ add_entrypoint_object(
     libc.src.stdio.printf_core.vfprintf_internal
 )
 
+add_entrypoint_object(
+  vsscanf
+  SRCS
+    vsscanf.cpp
+  HDRS
+    vsscanf.h
+  DEPENDS
+    libc.src.__support.arg_list
+)
+
 add_stdio_entrypoint_object(
   fileno
   SRCS
diff --git a/libc/src/stdio/vsscanf.cpp b/libc/src/stdio/vsscanf.cpp
new file mode 100644
index 0000000000000..b5c9173387ea0
--- /dev/null
+++ b/libc/src/stdio/vsscanf.cpp
@@ -0,0 +1,28 @@
+//===-- Implementation of vsscanf -------------------------------*- C++ -*-===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/stdio/vsscanf.h"
+
+#include "src/__support/arg_list.h"
+
+#include <stdarg.h>
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(int, vsscanf,
+                   (const char *, const char *,
+                    va_list vlist)) {
+  internal::ArgList args(vlist); // This holder class allows for easier copying
+                                 // and pointer semantics, as well as handling
+                                 // destruction automatically.
+
+  
+  return -1;
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/stdio/vsscanf.h b/libc/src/stdio/vsscanf.h
new file mode 100644
index 0000000000000..5f32f7ed351b1
--- /dev/null
+++ b/libc/src/stdio/vsscanf.h
@@ -0,0 +1,21 @@
+//===-- Implementation header of vsscanf ------------------------*- C++ -*-===//
+//
+// 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_SRC_STDIO_VSSCANF_H
+#define LLVM_LIBC_SRC_STDIO_VSSCANF_H
+
+#include <stdarg.h>
+
+namespace LIBC_NAMESPACE {
+
+int vsscanf(const char * s, const char * format,
+             va_list vlist);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_STDIO_VSSCANF_H

@izaakschroeder izaakschroeder changed the title [libc]: Add vsscanf function [libc] Add vsscanf function Jul 3, 2024
Copy link

github-actions bot commented Jul 3, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

Copy link
Contributor

@jhuber6 jhuber6 left a comment

Choose a reason for hiding this comment

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

The implementation is trivial here, can you go ahead and do it? All you need to do is copy the code from sscanf sans the va_list creation.

@izaakschroeder
Copy link
Contributor Author

@jhuber6 did my best! 😅

Copy link
Contributor

@jhuber6 jhuber6 left a comment

Choose a reason for hiding this comment

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

Thanks, now we just need a test. I think you should add this to every config/*/entrypoints.txt that already contains sscanf. The test should then add vsscanf as a dependency in the CMakeLists.txt. Add a single test that calls vsscanf using an existing va_list.

@@ -968,6 +968,13 @@ def StdC : StandardSpec<"stdc"> {
RetValSpec<IntType>,
[ArgSpec<IntType>, ArgSpec<FILEPtr>]
>,
FunctionSpec<
Copy link
Contributor

Choose a reason for hiding this comment

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

you should also add vsscanf to entrypoints.txt for the relevant platforms

Copy link
Contributor

@jhuber6 jhuber6 left a comment

Choose a reason for hiding this comment

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

Looks fine as is, but I'd change the invocations like

auto fn = [](const char *str, const char *fmt, ...) -> int { ... };
fn("Not an Integer", %d, &result);

@jhuber6
Copy link
Contributor

jhuber6 commented Jul 12, 2024

Can you make the above suggestions and add this to the entrypoints.txt for every target that already contains sscanf?

jhuber6 added a commit to jhuber6/llvm-project that referenced this pull request Jul 31, 2024
Summary:
Adds support for the `vsscanf` function similar to `sscanf`.
Based off of llvm#97529.
jhuber6 added a commit that referenced this pull request Jul 31, 2024
Summary:
Adds support for the `vsscanf` function similar to `sscanf`.
Based off of #97529.
@jhuber6
Copy link
Contributor

jhuber6 commented Jul 31, 2024

merged in #101402, thanks for the initial patch.

@jhuber6 jhuber6 closed this Jul 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants