diff options
author | Michael Jones <michaelrj@google.com> | 2023-04-14 15:36:39 -0700 |
---|---|---|
committer | Michael Jones <michaelrj@google.com> | 2023-04-14 15:40:05 -0700 |
commit | 1c261e360f558a914b9eafb22423f893f5dc54de (patch) | |
tree | 93e6b7ffbb207a78c2c46b0eb591a96f9d659dc5 /libc | |
parent | 5e53e1bbc34fe563b740364d7329ca0bd123f9ff (diff) | |
download | llvm-1c261e360f558a914b9eafb22423f893f5dc54de.zip llvm-1c261e360f558a914b9eafb22423f893f5dc54de.tar.gz llvm-1c261e360f558a914b9eafb22423f893f5dc54de.tar.bz2 |
[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
Diffstat (limited to 'libc')
-rw-r--r-- | libc/config/linux/aarch64/entrypoints.txt | 2 | ||||
-rw-r--r-- | libc/config/linux/riscv64/entrypoints.txt | 2 | ||||
-rw-r--r-- | libc/config/linux/x86_64/entrypoints.txt | 2 | ||||
-rw-r--r-- | libc/docs/stdio.rst | 2 | ||||
-rw-r--r-- | libc/spec/posix.td | 5 | ||||
-rw-r--r-- | libc/spec/stdc.td | 5 | ||||
-rw-r--r-- | libc/src/stdio/CMakeLists.txt | 26 | ||||
-rw-r--r-- | libc/src/stdio/getchar.cpp | 28 | ||||
-rw-r--r-- | libc/src/stdio/getchar.h | 18 | ||||
-rw-r--r-- | libc/src/stdio/getchar_unlocked.cpp | 28 | ||||
-rw-r--r-- | libc/src/stdio/getchar_unlocked.h | 18 |
11 files changed, 135 insertions, 1 deletions
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt index 166114f..104c5df 100644 --- a/libc/config/linux/aarch64/entrypoints.txt +++ b/libc/config/linux/aarch64/entrypoints.txt @@ -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 diff --git a/libc/config/linux/riscv64/entrypoints.txt b/libc/config/linux/riscv64/entrypoints.txt index 47827bd..255c526 100644 --- a/libc/config/linux/riscv64/entrypoints.txt +++ b/libc/config/linux/riscv64/entrypoints.txt @@ -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 diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt index b4e6d51..c5093db 100644 --- a/libc/config/linux/x86_64/entrypoints.txt +++ b/libc/config/linux/x86_64/entrypoints.txt @@ -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 diff --git a/libc/docs/stdio.rst b/libc/docs/stdio.rst index fdb7965..1063aa24 100644 --- a/libc/docs/stdio.rst +++ b/libc/docs/stdio.rst @@ -83,7 +83,7 @@ Function Name Available ============= ========= (f)getc |check| fgets |check| -getchar +getchar |check| fread |check| (f)putc |check| (f)puts |check| diff --git a/libc/spec/posix.td b/libc/spec/posix.td index de16096..f59891c0 100644 --- a/libc/spec/posix.td +++ b/libc/spec/posix.td @@ -1077,6 +1077,11 @@ def POSIX : StandardSpec<"POSIX"> { RetValSpec<IntType>, [ArgSpec<FILEPtr>] >, + FunctionSpec< + "getchar_unlocked", + RetValSpec<IntType>, + [ArgSpec<VoidType>] + >, ] >; diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td index 56ee9a6..ad29bb8 100644 --- a/libc/spec/stdc.td +++ b/libc/spec/stdc.td @@ -581,6 +581,11 @@ def StdC : StandardSpec<"stdc"> { [ArgSpec<FILEPtr>] >, FunctionSpec< + "getchar", + RetValSpec<IntType>, + [ArgSpec<VoidType>] + >, + FunctionSpec< "putc", RetValSpec<IntType>, [ArgSpec<IntType>, diff --git a/libc/src/stdio/CMakeLists.txt b/libc/src/stdio/CMakeLists.txt index 7ccbf9a..4747b5d 100644 --- a/libc/src/stdio/CMakeLists.txt +++ b/libc/src/stdio/CMakeLists.txt @@ -155,6 +155,32 @@ add_entrypoint_object( ) 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 fgets.cpp diff --git a/libc/src/stdio/getchar.cpp b/libc/src/stdio/getchar.cpp new file mode 100644 index 0000000..91e01e3 --- /dev/null +++ b/libc/src/stdio/getchar.cpp @@ -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 diff --git a/libc/src/stdio/getchar.h b/libc/src/stdio/getchar.h new file mode 100644 index 0000000..51f2f7a --- /dev/null +++ b/libc/src/stdio/getchar.h @@ -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 diff --git a/libc/src/stdio/getchar_unlocked.cpp b/libc/src/stdio/getchar_unlocked.cpp new file mode 100644 index 0000000..1a9a44e --- /dev/null +++ b/libc/src/stdio/getchar_unlocked.cpp @@ -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 diff --git a/libc/src/stdio/getchar_unlocked.h b/libc/src/stdio/getchar_unlocked.h new file mode 100644 index 0000000..6bbd49a --- /dev/null +++ b/libc/src/stdio/getchar_unlocked.h @@ -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 |