[libc-commits] [libc] 22f9dca - [libc] Add the implementation of the fflush function.
Siva Chandra Reddy via libc-commits
libc-commits at lists.llvm.org
Wed Apr 20 12:43:59 PDT 2022
Author: Siva Chandra Reddy
Date: 2022-04-20T19:43:39Z
New Revision: 22f9dca1137afe61e8facbd1d5b7edaa83a1220d
URL: https://github.com/llvm/llvm-project/commit/22f9dca1137afe61e8facbd1d5b7edaa83a1220d
DIFF: https://github.com/llvm/llvm-project/commit/22f9dca1137afe61e8facbd1d5b7edaa83a1220d.diff
LOG: [libc] Add the implementation of the fflush function.
Note that the underlying flush implementation does not yet fully implement
the POSIX standard. It is complete with respect to the C standard
however. A future change will add the POSIX behavior. It should not affect
the implementation of the fflush function however as the POSIX behavior
will be added in a lower layer.
Reviewed By: lntue
Differential Revision: https://reviews.llvm.org/D124073
Added:
libc/src/stdio/fflush.cpp
libc/src/stdio/fflush.h
Modified:
libc/config/linux/x86_64/entrypoints.txt
libc/spec/stdc.td
libc/src/__support/File/file.cpp
libc/src/stdio/CMakeLists.txt
libc/test/src/stdio/CMakeLists.txt
libc/test/src/stdio/fileop_test.cpp
Removed:
################################################################################
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index c7af459c36381..5803468acf001 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -253,6 +253,7 @@ if(LLVM_LIBC_FULL_BUILD)
# stdio.h entrypoints
libc.src.stdio.fclose
libc.src.stdio.flockfile
+ libc.src.stdio.fflush
libc.src.stdio.fopen
libc.src.stdio.fread
libc.src.stdio.fread_unlocked
diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index 10a980bf98df3..b5d95369e2a6b 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -483,6 +483,11 @@ def StdC : StandardSpec<"stdc"> {
RetValSpec<IntType>,
[ArgSpec<FILEPtr>]
>,
+ FunctionSpec<
+ "fflush",
+ RetValSpec<IntType>,
+ [ArgSpec<FILEPtr>]
+ >,
FunctionSpec<
"fopen",
RetValSpec<FILEPtr>,
diff --git a/libc/src/__support/File/file.cpp b/libc/src/__support/File/file.cpp
index 95f464d1df8df..31b960bc00a9e 100644
--- a/libc/src/__support/File/file.cpp
+++ b/libc/src/__support/File/file.cpp
@@ -165,6 +165,7 @@ int File::flush() {
pos = 0;
return platform_flush(this);
}
+ // TODO: Add POSIX behavior for input streams.
return 0;
}
diff --git a/libc/src/stdio/CMakeLists.txt b/libc/src/stdio/CMakeLists.txt
index 493244257850d..048e46b91db3f 100644
--- a/libc/src/stdio/CMakeLists.txt
+++ b/libc/src/stdio/CMakeLists.txt
@@ -24,6 +24,18 @@ add_entrypoint_object(
libc.src.__support.File.platform_file
)
+add_entrypoint_object(
+ fflush
+ SRCS
+ fflush.cpp
+ HDRS
+ fflush.h
+ DEPENDS
+ libc.include.stdio
+ libc.src.__support.File.file
+ libc.src.__support.File.platform_file
+)
+
add_entrypoint_object(
flockfile
SRCS
diff --git a/libc/src/stdio/fflush.cpp b/libc/src/stdio/fflush.cpp
new file mode 100644
index 0000000000000..2dc78e3e72e25
--- /dev/null
+++ b/libc/src/stdio/fflush.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of fflush ------------------------------------------===//
+//
+// 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/fflush.h"
+#include "src/__support/File/file.h"
+
+#include <stdio.h>
+
+namespace __llvm_libc {
+
+LLVM_LIBC_FUNCTION(int, fflush, (::FILE * stream)) {
+ return reinterpret_cast<__llvm_libc::File *>(stream)->flush();
+}
+
+} // namespace __llvm_libc
diff --git a/libc/src/stdio/fflush.h b/libc/src/stdio/fflush.h
new file mode 100644
index 0000000000000..f2adbe17e8ef2
--- /dev/null
+++ b/libc/src/stdio/fflush.h
@@ -0,0 +1,20 @@
+//===-- Implementation header of fflush -------------------------*- 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_FFLUSH_H
+#define LLVM_LIBC_SRC_STDIO_FFLUSH_H
+
+#include <stdio.h>
+
+namespace __llvm_libc {
+
+int fflush(::FILE *stream);
+
+} // namespace __llvm_libc
+
+#endif // LLVM_LIBC_SRC_STDIO_FFLUSH_H
diff --git a/libc/test/src/stdio/CMakeLists.txt b/libc/test/src/stdio/CMakeLists.txt
index 6a955ba742c70..b2e3813a74c7b 100644
--- a/libc/test/src/stdio/CMakeLists.txt
+++ b/libc/test/src/stdio/CMakeLists.txt
@@ -8,6 +8,7 @@ add_libc_unittest(
fileop_test.cpp
DEPENDS
libc.src.stdio.fclose
+ libc.src.stdio.fflush
libc.src.stdio.fopen
libc.src.stdio.fread
libc.src.stdio.fseek
diff --git a/libc/test/src/stdio/fileop_test.cpp b/libc/test/src/stdio/fileop_test.cpp
index e515a62a5e768..b0cc66cbe72bd 100644
--- a/libc/test/src/stdio/fileop_test.cpp
+++ b/libc/test/src/stdio/fileop_test.cpp
@@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//
#include "src/stdio/fclose.h"
+#include "src/stdio/fflush.h"
#include "src/stdio/fopen.h"
#include "src/stdio/fread.h"
#include "src/stdio/fseek.h"
@@ -41,3 +42,24 @@ TEST(LlvmLibcStdio, SimpleOperations) {
ASSERT_EQ(__llvm_libc::fclose(file), 0);
}
+
+TEST(LlvmLibcFILE, FFlushTest) {
+ constexpr char FILENAME[] = "testdata/fflush.test";
+ ::FILE *file = __llvm_libc::fopen(FILENAME, "w+");
+ ASSERT_FALSE(file == nullptr);
+ constexpr char CONTENT[] = "1234567890987654321";
+ ASSERT_EQ(sizeof(CONTENT),
+ __llvm_libc::fwrite(CONTENT, 1, sizeof(CONTENT), file));
+
+ // Flushing at this point should write the data to disk. So, we should be
+ // able to read it back.
+ ASSERT_EQ(0, __llvm_libc::fflush(file));
+
+ char data[sizeof(CONTENT)];
+ ASSERT_EQ(__llvm_libc::fseek(file, 0, SEEK_SET), 0);
+ ASSERT_EQ(__llvm_libc::fread(data, 1, sizeof(CONTENT), file),
+ sizeof(CONTENT));
+ ASSERT_STREQ(data, CONTENT);
+
+ ASSERT_EQ(__llvm_libc::fclose(file), 0);
+}
More information about the libc-commits
mailing list