[libc-commits] [libc] 2a038f9 - [libc] Add stdin definition.

Siva Chandra Reddy via libc-commits libc-commits at lists.llvm.org
Fri Oct 21 20:27:41 PDT 2022


Author: Siva Chandra Reddy
Date: 2022-10-22T03:27:31Z
New Revision: 2a038f91387c7fc476db83eb04a614c3a1e94e15

URL: https://github.com/llvm/llvm-project/commit/2a038f91387c7fc476db83eb04a614c3a1e94e15
DIFF: https://github.com/llvm/llvm-project/commit/2a038f91387c7fc476db83eb04a614c3a1e94e15.diff

LOG: [libc] Add stdin definition.

Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D136398

Added: 
    libc/src/stdio/stdin.cpp
    libc/src/stdio/stdin.h

Modified: 
    libc/config/linux/api.td
    libc/config/linux/x86_64/entrypoints.txt
    libc/spec/stdc.td
    libc/src/__support/File/file.h
    libc/src/__support/File/linux_file.cpp
    libc/src/stdio/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/libc/config/linux/api.td b/libc/config/linux/api.td
index 8d2e64fb00fb0..abccc0f48ed2b 100644
--- a/libc/config/linux/api.td
+++ b/libc/config/linux/api.td
@@ -149,6 +149,7 @@ def StringAPI : PublicAPI<"string.h"> {
 def StdIOAPI : PublicAPI<"stdio.h"> {
   let Macros = [
     SimpleMacroDef<"stderr", "stderr">,
+    SimpleMacroDef<"stdin", "stdin">,
     SimpleMacroDef<"stdout", "stdout">,
     SimpleMacroDef<"_IOFBF", "0">,
     SimpleMacroDef<"_IOLBF", "1">,

diff  --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 2637faf9be01d..987e9b209a1cf 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -387,6 +387,7 @@ if(LLVM_LIBC_FULL_BUILD)
     libc.src.stdio.putchar
     libc.src.stdio.puts
     libc.src.stdio.stderr
+    libc.src.stdio.stdin
     libc.src.stdio.stdout
 
     # stdlib.h entrypoints

diff  --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index 4d6c0b1b7c8ce..310887217f444 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -491,6 +491,7 @@ def StdC : StandardSpec<"stdc"> {
   HeaderSpec StdIO = HeaderSpec<
       "stdio.h",
       [
+          Macro<"stdin">,
           Macro<"stderr">,
           Macro<"stdout">,
           Macro<"_IOFBF">,
@@ -621,6 +622,10 @@ def StdC : StandardSpec<"stdc"> {
           >,
       ],
       [
+          ObjectSpec<
+              "stdin",
+              "FILE *"
+          >,
           ObjectSpec<
               "stdout",
               "FILE *"

diff  --git a/libc/src/__support/File/file.h b/libc/src/__support/File/file.h
index 40956ed2b7433..74655b1301b85 100644
--- a/libc/src/__support/File/file.h
+++ b/libc/src/__support/File/file.h
@@ -233,6 +233,7 @@ class File {
 // library.
 File *openfile(const char *path, const char *mode);
 
+extern File *stdin;
 extern File *stdout;
 extern File *stderr;
 

diff  --git a/libc/src/__support/File/linux_file.cpp b/libc/src/__support/File/linux_file.cpp
index 782cee48e4715..c6c93c8ef5086 100644
--- a/libc/src/__support/File/linux_file.cpp
+++ b/libc/src/__support/File/linux_file.cpp
@@ -164,6 +164,12 @@ File *openfile(const char *path, const char *mode) {
   return file;
 }
 
+constexpr size_t STDIN_BUFFER_SIZE = 512;
+char stdin_buffer[STDIN_BUFFER_SIZE];
+static LinuxFile StdIn(0, stdin_buffer, STDIN_BUFFER_SIZE, _IOFBF, false,
+                       File::ModeFlags(File::OpenMode::READ));
+File *stdin = &StdIn;
+
 constexpr size_t STDOUT_BUFFER_SIZE = 1024;
 char stdout_buffer[STDOUT_BUFFER_SIZE];
 static LinuxFile StdOut(1, stdout_buffer, STDOUT_BUFFER_SIZE, _IOLBF, false,

diff  --git a/libc/src/stdio/CMakeLists.txt b/libc/src/stdio/CMakeLists.txt
index 68fdb9a94d9d9..ea46d6b4ac121 100644
--- a/libc/src/stdio/CMakeLists.txt
+++ b/libc/src/stdio/CMakeLists.txt
@@ -268,6 +268,18 @@ add_entrypoint_object(
     libc.src.__support.File.file
 )
 
+add_entrypoint_object(
+  stdin
+  SRCS
+    stdin.cpp
+  HDRS
+    stdin.h
+  DEPENDS
+    libc.include.stdio
+    libc.src.__support.File.file
+    libc.src.__support.File.platform_file
+)
+
 add_entrypoint_object(
   stdout
   SRCS

diff  --git a/libc/src/stdio/stdin.cpp b/libc/src/stdio/stdin.cpp
new file mode 100644
index 0000000000000..cd34189c4b115
--- /dev/null
+++ b/libc/src/stdio/stdin.cpp
@@ -0,0 +1,13 @@
+//===-- Definition of the global stdin object -----------------------------===//
+//
+// 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/__support/File/file.h"
+
+#include <stdio.h>
+
+extern "C" FILE *stdin = reinterpret_cast<FILE *>(__llvm_libc::stdin);

diff  --git a/libc/src/stdio/stdin.h b/libc/src/stdio/stdin.h
new file mode 100644
index 0000000000000..ee4e6bfb6a2b0
--- /dev/null
+++ b/libc/src/stdio/stdin.h
@@ -0,0 +1,9 @@
+//===------------------------------------------------------------*- 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
+//
+//===----------------------------------------------------------------------===//
+
+#error "Do not include this file. Instead include __support/File/file.h."


        


More information about the libc-commits mailing list