Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/llvm/llvm-project.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/libc
diff options
context:
space:
mode:
authorSiva Chandra Reddy <sivachandra@google.com>2022-04-20 11:29:22 +0300
committerSiva Chandra Reddy <sivachandra@google.com>2022-04-20 22:43:39 +0300
commit22f9dca1137afe61e8facbd1d5b7edaa83a1220d (patch)
tree7df47824586e3ef1b304c36636137aedece9d3a6 /libc
parentbff8356b1969d2edd02e22c73d1c3d386f862937 (diff)
[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
Diffstat (limited to 'libc')
-rw-r--r--libc/config/linux/x86_64/entrypoints.txt1
-rw-r--r--libc/spec/stdc.td5
-rw-r--r--libc/src/__support/File/file.cpp1
-rw-r--r--libc/src/stdio/CMakeLists.txt12
-rw-r--r--libc/src/stdio/fflush.cpp20
-rw-r--r--libc/src/stdio/fflush.h20
-rw-r--r--libc/test/src/stdio/CMakeLists.txt1
-rw-r--r--libc/test/src/stdio/fileop_test.cpp22
8 files changed, 82 insertions, 0 deletions
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index c7af459c3638..5803468acf00 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 10a980bf98df..b5d95369e2a6 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -484,6 +484,11 @@ def StdC : StandardSpec<"stdc"> {
[ArgSpec<FILEPtr>]
>,
FunctionSpec<
+ "fflush",
+ RetValSpec<IntType>,
+ [ArgSpec<FILEPtr>]
+ >,
+ FunctionSpec<
"fopen",
RetValSpec<FILEPtr>,
[ArgSpec<ConstCharPtr>,
diff --git a/libc/src/__support/File/file.cpp b/libc/src/__support/File/file.cpp
index 95f464d1df8d..31b960bc00a9 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 493244257850..048e46b91db3 100644
--- a/libc/src/stdio/CMakeLists.txt
+++ b/libc/src/stdio/CMakeLists.txt
@@ -25,6 +25,18 @@ add_entrypoint_object(
)
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
flockfile.cpp
diff --git a/libc/src/stdio/fflush.cpp b/libc/src/stdio/fflush.cpp
new file mode 100644
index 000000000000..2dc78e3e72e2
--- /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 000000000000..f2adbe17e8ef
--- /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 6a955ba742c7..b2e3813a74c7 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 e515a62a5e76..b0cc66cbe72b 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);
+}