[libc-commits] [libc] 1c261e3 - [libc] Add implementation of getchar

Michael Jones via libc-commits libc-commits at lists.llvm.org
Fri Apr 14 15:40:10 PDT 2023


Author: Michael Jones
Date: 2023-04-14T15:40:05-07:00
New Revision: 1c261e360f558a914b9eafb22423f893f5dc54de

URL: https://github.com/llvm/llvm-project/commit/1c261e360f558a914b9eafb22423f893f5dc54de
DIFF: https://github.com/llvm/llvm-project/commit/1c261e360f558a914b9eafb22423f893f5dc54de.diff

LOG: [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

Added: 
    libc/src/stdio/getchar.cpp
    libc/src/stdio/getchar.h
    libc/src/stdio/getchar_unlocked.cpp
    libc/src/stdio/getchar_unlocked.h

Modified: 
    libc/config/linux/aarch64/entrypoints.txt
    libc/config/linux/riscv64/entrypoints.txt
    libc/config/linux/x86_64/entrypoints.txt
    libc/docs/stdio.rst
    libc/spec/posix.td
    libc/spec/stdc.td
    libc/src/stdio/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 166114f08b76d..104c5dfa95904 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 47827bd4efba2..255c5263a53e8 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 b4e6d51322e57..c5093dbb615c2 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 fdb796513d8fc..1063aa2432af9 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 de160960ab6ac..f59891c00c3a9 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 56ee9a60d10ae..ad29bb8b4be10 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -580,6 +580,11 @@ def StdC : StandardSpec<"stdc"> {
               RetValSpec<IntType>,
               [ArgSpec<FILEPtr>]
           >,
+          FunctionSpec<
+              "getchar",
+              RetValSpec<IntType>,
+              [ArgSpec<VoidType>]
+          >,
           FunctionSpec<
               "putc",
               RetValSpec<IntType>,

diff  --git a/libc/src/stdio/CMakeLists.txt b/libc/src/stdio/CMakeLists.txt
index 7ccbf9aa28c4c..4747b5daee9e6 100644
--- a/libc/src/stdio/CMakeLists.txt
+++ b/libc/src/stdio/CMakeLists.txt
@@ -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

diff  --git a/libc/src/stdio/getchar.cpp b/libc/src/stdio/getchar.cpp
new file mode 100644
index 0000000000000..91e01e37125f2
--- /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 0000000000000..51f2f7afa1945
--- /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 0000000000000..1a9a44ea4279e
--- /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 0000000000000..6bbd49a76a62b
--- /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


        


More information about the libc-commits mailing list