forked from OSchip/llvm-project
[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
This commit is contained in:
parent
bff8356b19
commit
22f9dca113
|
@ -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
|
||||
|
|
|
@ -483,6 +483,11 @@ def StdC : StandardSpec<"stdc"> {
|
|||
RetValSpec<IntType>,
|
||||
[ArgSpec<FILEPtr>]
|
||||
>,
|
||||
FunctionSpec<
|
||||
"fflush",
|
||||
RetValSpec<IntType>,
|
||||
[ArgSpec<FILEPtr>]
|
||||
>,
|
||||
FunctionSpec<
|
||||
"fopen",
|
||||
RetValSpec<FILEPtr>,
|
||||
|
|
|
@ -165,6 +165,7 @@ int File::flush() {
|
|||
pos = 0;
|
||||
return platform_flush(this);
|
||||
}
|
||||
// TODO: Add POSIX behavior for input streams.
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
|
@ -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
|
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue