[libc-commits] [libc] 0480b45 - [libc] Add implementation of fgetc.
Siva Chandra Reddy via libc-commits
libc-commits at lists.llvm.org
Fri Oct 21 20:34:54 PDT 2022
Author: Siva Chandra Reddy
Date: 2022-10-22T03:34:44Z
New Revision: 0480b45e9eabe4df1c58c1d3f1aa49c75defcbe5
URL: https://github.com/llvm/llvm-project/commit/0480b45e9eabe4df1c58c1d3f1aa49c75defcbe5
DIFF: https://github.com/llvm/llvm-project/commit/0480b45e9eabe4df1c58c1d3f1aa49c75defcbe5.diff
LOG: [libc] Add implementation of fgetc.
Reviewed By: michaelrj
Differential Revision: https://reviews.llvm.org/D136421
Added:
libc/src/stdio/fgetc.cpp
libc/src/stdio/fgetc.h
libc/test/src/stdio/fgetc_test.cpp
Modified:
libc/config/linux/x86_64/entrypoints.txt
libc/spec/stdc.td
libc/src/stdio/CMakeLists.txt
libc/test/src/stdio/CMakeLists.txt
Removed:
################################################################################
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 987e9b209a1cf..b2041d7ad3876 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -370,6 +370,7 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.stdio.feof_unlocked
libc.src.stdio.ferror
libc.src.stdio.ferror_unlocked
+ libc.src.stdio.fgetc
libc.src.stdio.fflush
libc.src.stdio.fopen
libc.src.stdio.fputc
diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index 310887217f444..a1327f9769700 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -525,6 +525,11 @@ def StdC : StandardSpec<"stdc"> {
RetValSpec<IntType>,
[ArgSpec<FILEPtr>]
>,
+ FunctionSpec<
+ "fgetc",
+ RetValSpec<IntType>,
+ [ArgSpec<FILEPtr>]
+ >,
FunctionSpec<
"fflush",
RetValSpec<IntType>,
diff --git a/libc/src/stdio/CMakeLists.txt b/libc/src/stdio/CMakeLists.txt
index ea46d6b4ac121..7be2d93ba40bd 100644
--- a/libc/src/stdio/CMakeLists.txt
+++ b/libc/src/stdio/CMakeLists.txt
@@ -100,6 +100,18 @@ add_entrypoint_object(
libc.src.__support.File.platform_file
)
+add_entrypoint_object(
+ fgetc
+ SRCS
+ fgetc.cpp
+ HDRS
+ fgetc.h
+ DEPENDS
+ libc.include.stdio
+ libc.src.__support.File.file
+ libc.src.__support.File.platform_file
+)
+
add_entrypoint_object(
fflush
SRCS
diff --git a/libc/src/stdio/fgetc.cpp b/libc/src/stdio/fgetc.cpp
new file mode 100644
index 0000000000000..b94cec541e85b
--- /dev/null
+++ b/libc/src/stdio/fgetc.cpp
@@ -0,0 +1,24 @@
+//===-- Implementation of fgetc -------------------------------------------===//
+//
+// 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/fgetc.h"
+#include "src/__support/File/file.h"
+
+#include <stdio.h>
+
+namespace __llvm_libc {
+
+LLVM_LIBC_FUNCTION(int, fgetc, (::FILE * stream)) {
+ unsigned char c;
+ size_t r = reinterpret_cast<__llvm_libc::File *>(stream)->read(&c, 1);
+ if (r != 1)
+ return EOF;
+ return c;
+}
+
+} // namespace __llvm_libc
diff --git a/libc/src/stdio/fgetc.h b/libc/src/stdio/fgetc.h
new file mode 100644
index 0000000000000..13d22ded7f3f8
--- /dev/null
+++ b/libc/src/stdio/fgetc.h
@@ -0,0 +1,20 @@
+//===-- Implementation header of fgetc --------------------------*- 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_FGETC_H
+#define LLVM_LIBC_SRC_STDIO_FGETC_H
+
+#include <stdio.h>
+
+namespace __llvm_libc {
+
+int fgetc(::FILE *f);
+
+} // namespace __llvm_libc
+
+#endif // LLVM_LIBC_SRC_STDIO_FGETC_H
diff --git a/libc/test/src/stdio/CMakeLists.txt b/libc/test/src/stdio/CMakeLists.txt
index 1a2a2f2ddfe04..3bd3ec575f530 100644
--- a/libc/test/src/stdio/CMakeLists.txt
+++ b/libc/test/src/stdio/CMakeLists.txt
@@ -153,5 +153,22 @@ if(${LIBC_TARGET_OS} STREQUAL "linux")
)
endif()
+add_libc_unittest(
+ fgetc_test
+ SUITE
+ libc_stdio_unittests
+ SRCS
+ fgetc_test.cpp
+ DEPENDS
+ libc.include.errno
+ libc.include.stdio
+ libc.src.stdio.fclose
+ libc.src.stdio.feof
+ libc.src.stdio.ferror
+ libc.src.stdio.fgetc
+ libc.src.stdio.fopen
+ libc.src.stdio.fwrite
+)
+
add_subdirectory(printf_core)
add_subdirectory(testdata)
diff --git a/libc/test/src/stdio/fgetc_test.cpp b/libc/test/src/stdio/fgetc_test.cpp
new file mode 100644
index 0000000000000..50d1780840de4
--- /dev/null
+++ b/libc/test/src/stdio/fgetc_test.cpp
@@ -0,0 +1,50 @@
+//===-- Unittests for fgetc -----------------------------------------------===//
+//
+// 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/clearerr.h"
+#include "src/stdio/fclose.h"
+#include "src/stdio/feof.h"
+#include "src/stdio/ferror.h"
+#include "src/stdio/fgetc.h"
+#include "src/stdio/fopen.h"
+#include "src/stdio/fwrite.h"
+#include "utils/UnitTest/Test.h"
+
+#include <errno.h>
+#include <stdio.h>
+
+TEST(LlvmLibcFGetCTest, WriteAndReadCharacters) {
+ constexpr char FILENAME[] = "testdata/fgetc.test";
+ ::FILE *file = __llvm_libc::fopen(FILENAME, "w");
+ ASSERT_FALSE(file == nullptr);
+ constexpr char CONTENT[] = "123456789";
+ constexpr size_t WRITE_SIZE = sizeof(CONTENT) - 1;
+ ASSERT_EQ(WRITE_SIZE, __llvm_libc::fwrite(CONTENT, 1, WRITE_SIZE, file));
+ // This is a write-only file so reads should fail.
+ ASSERT_EQ(__llvm_libc::fgetc(file), EOF);
+ // This is an error and not a real EOF.
+ ASSERT_EQ(__llvm_libc::feof(file), 0);
+ ASSERT_NE(__llvm_libc::ferror(file), 0);
+ errno = 0;
+
+ ASSERT_EQ(0, __llvm_libc::fclose(file));
+
+ file = __llvm_libc::fopen(FILENAME, "r");
+ ASSERT_FALSE(file == nullptr);
+
+ for (size_t i = 0; i < WRITE_SIZE; ++i) {
+ int c = __llvm_libc::fgetc(file);
+ ASSERT_EQ(c, int('1' + i));
+ }
+ // Reading more should return EOF but not set error.
+ ASSERT_EQ(__llvm_libc::fgetc(file), EOF);
+ ASSERT_NE(__llvm_libc::feof(file), 0);
+ ASSERT_EQ(__llvm_libc::ferror(file), 0);
+
+ ASSERT_EQ(0, __llvm_libc::fclose(file));
+}
More information about the libc-commits
mailing list