Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
98bfd7f
commit 36991d8
Showing
15 changed files
with
415 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.