Skip to content

Commit

Permalink
[libc] add scanf entrypoints
Browse files Browse the repository at this point in the history
This patch adds scanf, sscanf, and fscanf entrypoints. It also adds unit
tests for sscanf and a basic test to fscanf. The scanf function is
basically impossible to test in an automated fashion due to it recieving
user input.

Reviewed By: sivachandra, lntue

Differential Revision: https://reviews.llvm.org/D138076
  • Loading branch information
michaelrj-google committed Nov 17, 2022
1 parent 98bfd7f commit 36991d8
Show file tree
Hide file tree
Showing 15 changed files with 415 additions and 0 deletions.
3 changes: 3 additions & 0 deletions libc/config/linux/x86_64/entrypoints.txt
Expand Up @@ -394,6 +394,9 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.stdio.getc
libc.src.stdio.getc_unlocked
libc.src.stdio.printf
libc.src.stdio.sscanf
libc.src.stdio.scanf
libc.src.stdio.fscanf
libc.src.stdio.putc
libc.src.stdio.putchar
libc.src.stdio.puts
Expand Down
20 changes: 20 additions & 0 deletions libc/spec/stdc.td
Expand Up @@ -633,6 +633,26 @@ def StdC : StandardSpec<"stdc"> {
RetValSpec<IntType>,
[ArgSpec<FILERestrictedPtr>, ArgSpec<CharRestrictedPtr>, ArgSpec<IntType>, ArgSpec<SizeTType>]
>,
FunctionSpec<
"sscanf",
RetValSpec<IntType>,
[ArgSpec<ConstCharRestrictedPtr>,
ArgSpec<ConstCharRestrictedPtr>,
ArgSpec<VarArgType>]
>,
FunctionSpec<
"scanf",
RetValSpec<IntType>,
[ArgSpec<ConstCharRestrictedPtr>,
ArgSpec<VarArgType>]
>,
FunctionSpec<
"fscanf",
RetValSpec<IntType>,
[ArgSpec<FILERestrictedPtr>,
ArgSpec<ConstCharRestrictedPtr>,
ArgSpec<VarArgType>]
>,
FunctionSpec<
"sprintf",
RetValSpec<IntType>,
Expand Down
35 changes: 35 additions & 0 deletions libc/src/stdio/CMakeLists.txt
Expand Up @@ -403,6 +403,41 @@ add_entrypoint_object(
libc.src.__support.File.platform_file
)

add_entrypoint_object(
sscanf
SRCS
sscanf.cpp
HDRS
sscanf.h
DEPENDS
libc.src.__support.arg_list
libc.src.stdio.scanf_core.string_reader
libc.src.stdio.scanf_core.reader
libc.src.stdio.scanf_core.scanf_main
)

add_entrypoint_object(
fscanf
SRCS
fscanf.cpp
HDRS
fscanf.h
DEPENDS
libc.src.__support.arg_list
libc.src.stdio.scanf_core.vfscanf_internal
)

add_entrypoint_object(
scanf
SRCS
scanf.cpp
HDRS
scanf.h
DEPENDS
libc.src.__support.arg_list
libc.src.stdio.scanf_core.vfscanf_internal
)

add_entrypoint_object(
sprintf
SRCS
Expand Down
35 changes: 35 additions & 0 deletions libc/src/stdio/fscanf.cpp
@@ -0,0 +1,35 @@
//===-- Implementation of fscanf --------------------------------*- 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/fscanf.h"

#include "src/__support/File/file.h"
#include "src/__support/arg_list.h"
#include "src/stdio/scanf_core/vfscanf_internal.h"

#include <stdarg.h>
#include <stdio.h>

namespace __llvm_libc {

LLVM_LIBC_FUNCTION(int, fscanf,
(::FILE *__restrict stream, const char *__restrict format,
...)) {
va_list vlist;
va_start(vlist, format);
internal::ArgList args(vlist); // This holder class allows for easier copying
// and pointer semantics, as well as handling
// destruction automatically.
va_end(vlist);
int ret_val = scanf_core::vfscanf_internal(stream, format, args);
// This is done to avoid including stdio.h in the internals. On most systems
// EOF is -1, so this will be transformed into just "return ret_val".
return (ret_val == -1) ? EOF : ret_val;
}

} // namespace __llvm_libc
20 changes: 20 additions & 0 deletions libc/src/stdio/fscanf.h
@@ -0,0 +1,20 @@
//===-- Implementation header of fscanf -------------------------*- 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_FSCANF_H
#define LLVM_LIBC_SRC_STDIO_FSCANF_H

#include <stdio.h>

namespace __llvm_libc {

int fscanf(::FILE *__restrict stream, const char *__restrict format, ...);

} // namespace __llvm_libc

#endif // LLVM_LIBC_SRC_STDIO_FSCANF_H
34 changes: 34 additions & 0 deletions libc/src/stdio/scanf.cpp
@@ -0,0 +1,34 @@
//===-- Implementation of scanf ---------------------------------*- 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/scanf.h"

#include "src/__support/File/file.h"
#include "src/__support/arg_list.h"
#include "src/stdio/scanf_core/vfscanf_internal.h"

#include <stdarg.h>
#include <stdio.h>

namespace __llvm_libc {

LLVM_LIBC_FUNCTION(int, scanf, (const char *__restrict format, ...)) {
va_list vlist;
va_start(vlist, format);
internal::ArgList args(vlist); // This holder class allows for easier copying
// and pointer semantics, as well as handling
// destruction automatically.
va_end(vlist);
int ret_val = scanf_core::vfscanf_internal(
reinterpret_cast<::FILE *>(__llvm_libc::stdin), format, args);
// This is done to avoid including stdio.h in the internals. On most systems
// EOF is -1, so this will be transformed into just "return ret_val".
return (ret_val == -1) ? EOF : ret_val;
}

} // namespace __llvm_libc
18 changes: 18 additions & 0 deletions libc/src/stdio/scanf.h
@@ -0,0 +1,18 @@
//===-- Implementation header of scanf --------------------------*- 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_SCANF_H
#define LLVM_LIBC_SRC_STDIO_SCANF_H

namespace __llvm_libc {

int scanf(const char *__restrict format, ...);

} // namespace __llvm_libc

#endif // LLVM_LIBC_SRC_STDIO_SCANF_H
13 changes: 13 additions & 0 deletions libc/src/stdio/scanf_core/CMakeLists.txt
Expand Up @@ -89,3 +89,16 @@ add_object_library(
libc.src.__support.CPP.string_view
libc.src.__support.CPP.limits
)

add_object_library(
vfscanf_internal
SRCS
vfscanf_internal.cpp
HDRS
vfscanf_internal.h
DEPENDS
.reader
.file_reader
.scanf_main
libc.src.__support.arg_list
)
29 changes: 29 additions & 0 deletions libc/src/stdio/scanf_core/vfscanf_internal.cpp
@@ -0,0 +1,29 @@
//===-- Internal implementation of vfscanf ---------------------*- 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/scanf_core/vfscanf_internal.h"

#include "src/__support/arg_list.h"
#include "src/stdio/scanf_core/file_reader.h"
#include "src/stdio/scanf_core/reader.h"
#include "src/stdio/scanf_core/scanf_main.h"

#include <stdio.h>

namespace __llvm_libc {
namespace scanf_core {

int vfscanf_internal(::FILE *__restrict stream, const char *__restrict format,
internal::ArgList &args) {
FileReader file_reader(stream);
scanf_core::Reader reader(&file_reader);
return scanf_core::scanf_main(&reader, format, args);
}

} // namespace scanf_core
} // namespace __llvm_libc
24 changes: 24 additions & 0 deletions libc/src/stdio/scanf_core/vfscanf_internal.h
@@ -0,0 +1,24 @@
//===-- Internal implementation header of vfscanf ---------------*- 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_SCANF_CORE_VFSCANF_INTERNAL_H
#define LLVM_LIBC_SRC_STDIO_SCANF_CORE_VFSCANF_INTERNAL_H

#include "src/__support/arg_list.h"

#include <stdio.h>

namespace __llvm_libc {
namespace scanf_core {

int vfscanf_internal(::FILE *__restrict stream, const char *__restrict format,
internal::ArgList &args);
} // namespace scanf_core
} // namespace __llvm_libc

#endif // LLVM_LIBC_SRC_STDIO_SCANF_CORE_VFSCANF_INTERNAL_H
38 changes: 38 additions & 0 deletions libc/src/stdio/sscanf.cpp
@@ -0,0 +1,38 @@
//===-- Implementation of sscanf --------------------------------*- 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/sscanf.h"

#include "src/__support/arg_list.h"
#include "src/stdio/scanf_core/reader.h"
#include "src/stdio/scanf_core/scanf_main.h"
#include "src/stdio/scanf_core/string_reader.h"

#include <stdarg.h>
#include <stdio.h>

namespace __llvm_libc {

LLVM_LIBC_FUNCTION(int, sscanf,
(const char *__restrict buffer,
const char *__restrict format, ...)) {
va_list vlist;
va_start(vlist, format);
internal::ArgList args(vlist); // This holder class allows for easier copying
// and pointer semantics, as well as handling
// destruction automatically.
va_end(vlist);
scanf_core::StringReader string_reader(buffer);
scanf_core::Reader reader(&string_reader);
int ret_val = scanf_core::scanf_main(&reader, format, args);
// This is done to avoid including stdio.h in the internals. On most systems
// EOF is -1, so this will be transformed into just "return ret_val".
return (ret_val == -1) ? EOF : ret_val;
}

} // namespace __llvm_libc
18 changes: 18 additions & 0 deletions libc/src/stdio/sscanf.h
@@ -0,0 +1,18 @@
//===-- Implementation header of sscanf -------------------------*- 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_SSCANF_H
#define LLVM_LIBC_SRC_STDIO_SSCANF_H

namespace __llvm_libc {

int sscanf(const char *__restrict buffer, const char *__restrict format, ...);

} // namespace __llvm_libc

#endif // LLVM_LIBC_SRC_STDIO_SSCANF_H
25 changes: 25 additions & 0 deletions libc/test/src/stdio/CMakeLists.txt
Expand Up @@ -159,6 +159,31 @@ add_libc_unittest(
libc.src.stdio.printf
)

add_libc_unittest(
fscanf_test
SUITE
libc_stdio_unittests
SRCS
fscanf_test.cpp
DEPENDS
libc.src.stdio.fscanf
libc.src.stdio.fclose
libc.src.stdio.ferror
libc.src.stdio.fopen
libc.src.stdio.fwrite
libc.src.__support.CPP.string_view
)

add_libc_unittest(
sscanf_test
SUITE
libc_stdio_unittests
SRCS
sscanf_test.cpp
DEPENDS
libc.src.stdio.sscanf
)

add_libc_unittest(
puts_test
SUITE
Expand Down

0 comments on commit 36991d8

Please sign in to comment.