Skip to content

Commit 22f9dca

Browse files
author
Siva Chandra Reddy
committed
[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
1 parent bff8356 commit 22f9dca

File tree

8 files changed

+82
-0
lines changed

8 files changed

+82
-0
lines changed

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ if(LLVM_LIBC_FULL_BUILD)
253253
# stdio.h entrypoints
254254
libc.src.stdio.fclose
255255
libc.src.stdio.flockfile
256+
libc.src.stdio.fflush
256257
libc.src.stdio.fopen
257258
libc.src.stdio.fread
258259
libc.src.stdio.fread_unlocked

libc/spec/stdc.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,11 @@ def StdC : StandardSpec<"stdc"> {
483483
RetValSpec<IntType>,
484484
[ArgSpec<FILEPtr>]
485485
>,
486+
FunctionSpec<
487+
"fflush",
488+
RetValSpec<IntType>,
489+
[ArgSpec<FILEPtr>]
490+
>,
486491
FunctionSpec<
487492
"fopen",
488493
RetValSpec<FILEPtr>,

libc/src/__support/File/file.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ int File::flush() {
165165
pos = 0;
166166
return platform_flush(this);
167167
}
168+
// TODO: Add POSIX behavior for input streams.
168169
return 0;
169170
}
170171

libc/src/stdio/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@ add_entrypoint_object(
2424
libc.src.__support.File.platform_file
2525
)
2626

27+
add_entrypoint_object(
28+
fflush
29+
SRCS
30+
fflush.cpp
31+
HDRS
32+
fflush.h
33+
DEPENDS
34+
libc.include.stdio
35+
libc.src.__support.File.file
36+
libc.src.__support.File.platform_file
37+
)
38+
2739
add_entrypoint_object(
2840
flockfile
2941
SRCS

libc/src/stdio/fflush.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation of fflush ------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/stdio/fflush.h"
10+
#include "src/__support/File/file.h"
11+
12+
#include <stdio.h>
13+
14+
namespace __llvm_libc {
15+
16+
LLVM_LIBC_FUNCTION(int, fflush, (::FILE * stream)) {
17+
return reinterpret_cast<__llvm_libc::File *>(stream)->flush();
18+
}
19+
20+
} // namespace __llvm_libc

libc/src/stdio/fflush.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation header of fflush -------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_STDIO_FFLUSH_H
10+
#define LLVM_LIBC_SRC_STDIO_FFLUSH_H
11+
12+
#include <stdio.h>
13+
14+
namespace __llvm_libc {
15+
16+
int fflush(::FILE *stream);
17+
18+
} // namespace __llvm_libc
19+
20+
#endif // LLVM_LIBC_SRC_STDIO_FFLUSH_H

libc/test/src/stdio/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ add_libc_unittest(
88
fileop_test.cpp
99
DEPENDS
1010
libc.src.stdio.fclose
11+
libc.src.stdio.fflush
1112
libc.src.stdio.fopen
1213
libc.src.stdio.fread
1314
libc.src.stdio.fseek

libc/test/src/stdio/fileop_test.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "src/stdio/fclose.h"
10+
#include "src/stdio/fflush.h"
1011
#include "src/stdio/fopen.h"
1112
#include "src/stdio/fread.h"
1213
#include "src/stdio/fseek.h"
@@ -41,3 +42,24 @@ TEST(LlvmLibcStdio, SimpleOperations) {
4142

4243
ASSERT_EQ(__llvm_libc::fclose(file), 0);
4344
}
45+
46+
TEST(LlvmLibcFILE, FFlushTest) {
47+
constexpr char FILENAME[] = "testdata/fflush.test";
48+
::FILE *file = __llvm_libc::fopen(FILENAME, "w+");
49+
ASSERT_FALSE(file == nullptr);
50+
constexpr char CONTENT[] = "1234567890987654321";
51+
ASSERT_EQ(sizeof(CONTENT),
52+
__llvm_libc::fwrite(CONTENT, 1, sizeof(CONTENT), file));
53+
54+
// Flushing at this point should write the data to disk. So, we should be
55+
// able to read it back.
56+
ASSERT_EQ(0, __llvm_libc::fflush(file));
57+
58+
char data[sizeof(CONTENT)];
59+
ASSERT_EQ(__llvm_libc::fseek(file, 0, SEEK_SET), 0);
60+
ASSERT_EQ(__llvm_libc::fread(data, 1, sizeof(CONTENT), file),
61+
sizeof(CONTENT));
62+
ASSERT_STREQ(data, CONTENT);
63+
64+
ASSERT_EQ(__llvm_libc::fclose(file), 0);
65+
}

0 commit comments

Comments
 (0)