Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[libc] Add implementations of ftell.
Reviewed By: michaelrj, lntue Differential Revision: https://reviews.llvm.org/D137395
- Loading branch information
Siva Chandra Reddy
committed
Nov 7, 2022
1 parent
d8233b5
commit 43e52ad
Showing
13 changed files
with
191 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
//===-- Implementation of ftell -------------------------------------------===// | ||
// | ||
// 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/ftell.h" | ||
#include "src/__support/File/file.h" | ||
|
||
#include <stdio.h> | ||
|
||
namespace __llvm_libc { | ||
|
||
LLVM_LIBC_FUNCTION(long, ftell, (::FILE * stream)) { | ||
return reinterpret_cast<__llvm_libc::File *>(stream)->tell(); | ||
} | ||
|
||
} // namespace __llvm_libc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
//===-- Implementation header of ftell --------------------------*- 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_FTELL_H | ||
#define LLVM_LIBC_SRC_STDIO_FTELL_H | ||
|
||
#include <stdio.h> | ||
|
||
namespace __llvm_libc { | ||
|
||
long ftell(::FILE *f); | ||
|
||
} // namespace __llvm_libc | ||
|
||
#endif // LLVM_LIBC_SRC_STDIO_FTELL_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
//===-- Unittests for ftell -----------------------------------------------===// | ||
// | ||
// 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/fclose.h" | ||
#include "src/stdio/fflush.h" | ||
#include "src/stdio/fopen.h" | ||
#include "src/stdio/fread.h" | ||
#include "src/stdio/fseek.h" | ||
#include "src/stdio/ftell.h" | ||
#include "src/stdio/fwrite.h" | ||
#include "src/stdio/setvbuf.h" | ||
#include "utils/UnitTest/Test.h" | ||
|
||
#include <stdio.h> | ||
|
||
class LlvmLibcFTellTest : public __llvm_libc::testing::Test { | ||
protected: | ||
void test_with_bufmode(int bufmode) { | ||
constexpr char FILENAME[] = "testdata/ftell.test"; | ||
// We will set a special buffer to the file so that we guarantee buffering. | ||
constexpr size_t BUFFER_SIZE = 1024; | ||
char buffer[BUFFER_SIZE]; | ||
::FILE *file = __llvm_libc::fopen(FILENAME, "w+"); | ||
ASSERT_FALSE(file == nullptr); | ||
ASSERT_EQ(__llvm_libc::setvbuf(file, buffer, bufmode, BUFFER_SIZE), 0); | ||
|
||
// Include few '\n' chars to test when |bufmode| is _IOLBF. | ||
constexpr char CONTENT[] = "12\n345\n6789"; | ||
constexpr size_t WRITE_SIZE = sizeof(CONTENT) - 1; | ||
ASSERT_EQ(WRITE_SIZE, __llvm_libc::fwrite(CONTENT, 1, WRITE_SIZE, file)); | ||
// The above write should have buffered the written data and not have | ||
// trasferred it to the underlying stream. But, ftell operation should | ||
// still return the correct effective offset. | ||
ASSERT_EQ(size_t(__llvm_libc::ftell(file)), WRITE_SIZE); | ||
|
||
long offset = 5; | ||
ASSERT_EQ(0, __llvm_libc::fseek(file, offset, SEEK_SET)); | ||
ASSERT_EQ(__llvm_libc::ftell(file), offset); | ||
ASSERT_EQ(0, __llvm_libc::fseek(file, -offset, SEEK_END)); | ||
ASSERT_EQ(size_t(__llvm_libc::ftell(file)), size_t(WRITE_SIZE - offset)); | ||
|
||
ASSERT_EQ(0, __llvm_libc::fseek(file, 0, SEEK_SET)); | ||
constexpr size_t READ_SIZE = WRITE_SIZE / 2; | ||
char data[READ_SIZE]; | ||
// Reading a small amount will actually read out much more data and | ||
// buffer it. But, ftell should return the correct effective offset. | ||
ASSERT_EQ(READ_SIZE, __llvm_libc::fread(data, 1, READ_SIZE, file)); | ||
ASSERT_EQ(size_t(__llvm_libc::ftell(file)), READ_SIZE); | ||
|
||
ASSERT_EQ(0, __llvm_libc::fclose(file)); | ||
} | ||
}; | ||
|
||
TEST_F(LlvmLibcFTellTest, TellWithFBF) { test_with_bufmode(_IOFBF); } | ||
|
||
TEST_F(LlvmLibcFTellTest, TellWithNBF) { test_with_bufmode(_IONBF); } | ||
|
||
TEST_F(LlvmLibcFTellTest, TellWithLBF) { test_with_bufmode(_IOLBF); } |