[libc] Add implementation of getchar

added getchar and getchar_unlocked which are just wrappers getc and getc_unlocked respectively.

Reviewed By: sivachandra, lntue, michaelrj

Differential Revision: https://reviews.llvm.org/D147919
This commit is contained in:
Michael Jones 2023-04-14 15:36:39 -07:00
parent 5e53e1bbc3
commit 1c261e360f
11 changed files with 135 additions and 1 deletions

View File

@ -396,6 +396,8 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.stdio.fwrite
libc.src.stdio.fwrite_unlocked
libc.src.stdio.fprintf
libc.src.stdio.getchar
libc.src.stdio.getchar_unlocked
libc.src.stdio.printf
libc.src.stdio.putc
libc.src.stdio.putchar

View File

@ -409,6 +409,8 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.stdio.fprintf
libc.src.stdio.getc
libc.src.stdio.getc_unlocked
libc.src.stdio.getchar
libc.src.stdio.getchar_unlocked
libc.src.stdio.printf
libc.src.stdio.sscanf
libc.src.stdio.scanf

View File

@ -423,6 +423,8 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.stdio.fwrite_unlocked
libc.src.stdio.getc
libc.src.stdio.getc_unlocked
libc.src.stdio.getchar
libc.src.stdio.getchar_unlocked
libc.src.stdio.sscanf
libc.src.stdio.scanf
libc.src.stdio.fscanf

View File

@ -83,7 +83,7 @@ Function Name Available
============= =========
(f)getc |check|
fgets |check|
getchar
getchar |check|
fread |check|
(f)putc |check|
(f)puts |check|

View File

@ -1077,6 +1077,11 @@ def POSIX : StandardSpec<"POSIX"> {
RetValSpec<IntType>,
[ArgSpec<FILEPtr>]
>,
FunctionSpec<
"getchar_unlocked",
RetValSpec<IntType>,
[ArgSpec<VoidType>]
>,
]
>;

View File

@ -580,6 +580,11 @@ def StdC : StandardSpec<"stdc"> {
RetValSpec<IntType>,
[ArgSpec<FILEPtr>]
>,
FunctionSpec<
"getchar",
RetValSpec<IntType>,
[ArgSpec<VoidType>]
>,
FunctionSpec<
"putc",
RetValSpec<IntType>,

View File

@ -154,6 +154,32 @@ add_entrypoint_object(
libc.src.__support.File.platform_file
)
add_entrypoint_object(
getchar
SRCS
getchar.cpp
HDRS
getchar.h
DEPENDS
libc.src.errno.errno
libc.include.stdio
libc.src.__support.File.file
libc.src.__support.File.platform_file
)
add_entrypoint_object(
getchar_unlocked
SRCS
getc_unlocked.cpp
HDRS
getc_unlocked.h
DEPENDS
libc.src.errno.errno
libc.include.stdio
libc.src.__support.File.file
libc.src.__support.File.platform_file
)
add_entrypoint_object(
fgets
SRCS

View File

@ -0,0 +1,28 @@
//===-- Implementation of getchar -----------------------------------------===//
//
// 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/getchar.h"
#include "src/__support/File/file.h"
#include "src/errno/libc_errno.h"
#include <stdio.h>
namespace __llvm_libc {
LLVM_LIBC_FUNCTION(int, getchar, ()) {
unsigned char c;
auto result = stdin->read(&c, 1);
if (result.has_error())
libc_errno = result.error;
if (result.value != 1)
return EOF;
return c;
}
} // namespace __llvm_libc

18
libc/src/stdio/getchar.h Normal file
View File

@ -0,0 +1,18 @@
//===-- Implementation header of getchar ------------------------*- 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_GETCHAR_H
#define LLVM_LIBC_SRC_STDIO_GETCHAR_H
namespace __llvm_libc {
int getchar();
} // namespace __llvm_libc
#endif // LLVM_LIBC_SRC_STDIO_GETCHAR_H

View File

@ -0,0 +1,28 @@
//===-- Implementation of getchar_unlocked --------------------------------===//
//
// 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/getchar_unlocked.h"
#include "src/__support/File/file.h"
#include "src/errno/libc_errno.h"
#include <stdio.h>
namespace __llvm_libc {
LLVM_LIBC_FUNCTION(int, getchar_unlocked, ()) {
unsigned char c;
auto result = stdin->read_unlocked(&c, 1);
if (result.has_error())
libc_errno = result.error;
if (result.value != 1)
return EOF;
return c;
}
} // namespace __llvm_libc

View File

@ -0,0 +1,18 @@
//===-- Implementation header of getchar_unlocked ---------------*- 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_GETCHAR_UNLOCKED_H
#define LLVM_LIBC_SRC_STDIO_GETCHAR_UNLOCKED_H
namespace __llvm_libc {
int getchar_unlocked();
} // namespace __llvm_libc
#endif // LLVM_LIBC_SRC_STDIO_GETCHAR_UNLOCKED_H