[libc] Add stdin definition.

Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D136398
This commit is contained in:
Siva Chandra Reddy 2022-10-20 23:44:13 +00:00
parent be4e425758
commit 2a038f9138
8 changed files with 48 additions and 0 deletions

View File

@ -149,6 +149,7 @@ def StringAPI : PublicAPI<"string.h"> {
def StdIOAPI : PublicAPI<"stdio.h"> { def StdIOAPI : PublicAPI<"stdio.h"> {
let Macros = [ let Macros = [
SimpleMacroDef<"stderr", "stderr">, SimpleMacroDef<"stderr", "stderr">,
SimpleMacroDef<"stdin", "stdin">,
SimpleMacroDef<"stdout", "stdout">, SimpleMacroDef<"stdout", "stdout">,
SimpleMacroDef<"_IOFBF", "0">, SimpleMacroDef<"_IOFBF", "0">,
SimpleMacroDef<"_IOLBF", "1">, SimpleMacroDef<"_IOLBF", "1">,

View File

@ -387,6 +387,7 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.stdio.putchar libc.src.stdio.putchar
libc.src.stdio.puts libc.src.stdio.puts
libc.src.stdio.stderr libc.src.stdio.stderr
libc.src.stdio.stdin
libc.src.stdio.stdout libc.src.stdio.stdout
# stdlib.h entrypoints # stdlib.h entrypoints

View File

@ -491,6 +491,7 @@ def StdC : StandardSpec<"stdc"> {
HeaderSpec StdIO = HeaderSpec< HeaderSpec StdIO = HeaderSpec<
"stdio.h", "stdio.h",
[ [
Macro<"stdin">,
Macro<"stderr">, Macro<"stderr">,
Macro<"stdout">, Macro<"stdout">,
Macro<"_IOFBF">, Macro<"_IOFBF">,
@ -621,6 +622,10 @@ def StdC : StandardSpec<"stdc"> {
>, >,
], ],
[ [
ObjectSpec<
"stdin",
"FILE *"
>,
ObjectSpec< ObjectSpec<
"stdout", "stdout",
"FILE *" "FILE *"

View File

@ -233,6 +233,7 @@ private:
// library. // library.
File *openfile(const char *path, const char *mode); File *openfile(const char *path, const char *mode);
extern File *stdin;
extern File *stdout; extern File *stdout;
extern File *stderr; extern File *stderr;

View File

@ -164,6 +164,12 @@ File *openfile(const char *path, const char *mode) {
return file; 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; constexpr size_t STDOUT_BUFFER_SIZE = 1024;
char stdout_buffer[STDOUT_BUFFER_SIZE]; char stdout_buffer[STDOUT_BUFFER_SIZE];
static LinuxFile StdOut(1, stdout_buffer, STDOUT_BUFFER_SIZE, _IOLBF, false, static LinuxFile StdOut(1, stdout_buffer, STDOUT_BUFFER_SIZE, _IOLBF, false,

View File

@ -268,6 +268,18 @@ add_entrypoint_object(
libc.src.__support.File.file 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( add_entrypoint_object(
stdout stdout
SRCS SRCS

13
libc/src/stdio/stdin.cpp Normal file
View File

@ -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);

9
libc/src/stdio/stdin.h Normal file
View File

@ -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."