-
Notifications
You must be signed in to change notification settings - Fork 14.8k
[libc] Migrate from baremetal stdio.h to generic stdio.h #152748
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
saturn691
wants to merge
5
commits into
llvm:main
Choose a base branch
from
saturn691:libc-stdio
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
66e8d61
[libc] Migrate from baremetal stdio.h to generic stdio.h
saturn691 45aeb22
Some files were not tracked, commit these
saturn691 9f1846f
Apply suggestions from @mysterymath
saturn691 7b3dd93
Add suggestion from @michaelrj-google
saturn691 4988ae4
Merge branch 'main' into libc-stdio
saturn691 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,32 @@ | ||
add_object_library( | ||
stdout | ||
SRCS | ||
stdout.cpp | ||
DEPENDS | ||
libc.hdr.stdio_macros | ||
libc.src.__support.File.cookie_file | ||
libc.src.__support.File.file | ||
libc.src.__support.OSUtil.osutil | ||
) | ||
|
||
add_object_library( | ||
stdin | ||
SRCS | ||
stdin.cpp | ||
DEPENDS | ||
libc.hdr.stdio_macros | ||
libc.src.__support.File.cookie_file | ||
libc.src.__support.File.file | ||
libc.src.__support.OSUtil.osutil | ||
) | ||
|
||
add_object_library( | ||
stderr | ||
SRCS | ||
stderr.cpp | ||
DEPENDS | ||
libc.hdr.stdio_macros | ||
libc.src.__support.File.cookie_file | ||
libc.src.__support.File.file | ||
libc.src.__support.OSUtil.osutil | ||
) |
This file contains hidden or 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,34 @@ | ||
//===--- Definition of baremetal stderr -----------------------------------===// | ||
// | ||
// 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 "hdr/stdio_macros.h" | ||
#include "src/__support/File/cookie_file.h" | ||
#include "src/__support/File/file.h" | ||
#include "src/__support/OSUtil/baremetal/io.h" | ||
#include "src/__support/macros/config.h" | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
cookie_io_functions_t io_func = {.read = nullptr, | ||
.write = __llvm_libc_stdio_write, | ||
.seek = nullptr, | ||
.close = nullptr}; | ||
#pragma clang diagnostic push | ||
#pragma clang diagnostic ignored "-Wglobal-constructors" | ||
// Buffering is implementation defined. Therefore to save RAM, we use no | ||
// buffering | ||
static CookieFile StdErr(&__llvm_libc_stderr_cookie, io_func, nullptr, 0, | ||
_IONBF, File::mode_flags("w")); | ||
#pragma clang diagnostic pop | ||
File *stderr = &StdErr; | ||
|
||
} // namespace LIBC_NAMESPACE_DECL | ||
|
||
extern "C" { | ||
FILE *stderr = reinterpret_cast<FILE *>(&LIBC_NAMESPACE::StdErr); | ||
} |
This file contains hidden or 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,34 @@ | ||
//===--- Definition of baremetal stdin ------------------------------------===// | ||
// | ||
// 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 "hdr/stdio_macros.h" | ||
#include "src/__support/File/cookie_file.h" | ||
#include "src/__support/File/file.h" | ||
#include "src/__support/OSUtil/baremetal/io.h" | ||
#include "src/__support/macros/config.h" | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
cookie_io_functions_t io_func = {.read = __llvm_libc_stdio_read, | ||
.write = nullptr, | ||
.seek = nullptr, | ||
.close = nullptr}; | ||
#pragma clang diagnostic push | ||
#pragma clang diagnostic ignored "-Wglobal-constructors" | ||
// Buffering is implementation defined. Therefore to save RAM, we use no | ||
// buffering | ||
static CookieFile StdIn(&__llvm_libc_stdin_cookie, io_func, nullptr, 0, _IONBF, | ||
File::mode_flags("r")); | ||
#pragma clang diagnostic pop | ||
File *stdin = &StdIn; | ||
|
||
} // namespace LIBC_NAMESPACE_DECL | ||
|
||
extern "C" { | ||
FILE *stdin = reinterpret_cast<FILE *>(&LIBC_NAMESPACE::StdIn); | ||
} |
This file contains hidden or 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,34 @@ | ||
//===--- Definition of baremetal stdout -----------------------------------===// | ||
// | ||
// 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 "hdr/stdio_macros.h" | ||
#include "src/__support/File/cookie_file.h" | ||
#include "src/__support/File/file.h" | ||
#include "src/__support/OSUtil/baremetal/io.h" | ||
#include "src/__support/macros/config.h" | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
cookie_io_functions_t io_func = {.read = nullptr, | ||
.write = __llvm_libc_stdio_write, | ||
.seek = nullptr, | ||
.close = nullptr}; | ||
#pragma clang diagnostic push | ||
#pragma clang diagnostic ignored "-Wglobal-constructors" | ||
// Buffering is implementation defined. Therefore to save RAM, we use no | ||
// buffering | ||
static CookieFile StdOut(&__llvm_libc_stdout_cookie, io_func, nullptr, 0, | ||
_IONBF, File::mode_flags("w")); | ||
#pragma clang diagnostic pop | ||
File *stdout = &StdOut; | ||
|
||
} // namespace LIBC_NAMESPACE_DECL | ||
|
||
extern "C" { | ||
FILE *stdout = reinterpret_cast<FILE *>(&LIBC_NAMESPACE::StdOut); | ||
} |
This file contains hidden or 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,80 @@ | ||
//===--- A platform independent cookie file class -------------------------===// | ||
// | ||
// 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 "hdr/errno_macros.h" | ||
#include "hdr/types/cookie_io_functions_t.h" | ||
#include "hdr/types/off_t.h" | ||
#include "src/__support/CPP/new.h" | ||
#include "src/__support/File/file.h" | ||
#include "src/__support/macros/config.h" | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
class CookieFile : public LIBC_NAMESPACE::File { | ||
void *cookie; | ||
cookie_io_functions_t ops; | ||
|
||
LIBC_INLINE static FileIOResult cookie_write(File *f, const void *data, | ||
size_t size); | ||
LIBC_INLINE static FileIOResult cookie_read(File *f, void *data, size_t size); | ||
LIBC_INLINE static ErrorOr<off_t> cookie_seek(File *f, off_t offset, | ||
int whence); | ||
LIBC_INLINE static int cookie_close(File *f); | ||
|
||
public: | ||
LIBC_INLINE CookieFile(void *c, cookie_io_functions_t cops, uint8_t *buffer, | ||
size_t bufsize, int buffer_mode, File::ModeFlags mode) | ||
: File(&cookie_write, &cookie_read, &CookieFile::cookie_seek, | ||
&cookie_close, buffer, bufsize, buffer_mode, | ||
true /* File owns buffer */, mode), | ||
cookie(c), ops(cops) {} | ||
}; | ||
|
||
LIBC_INLINE FileIOResult CookieFile::cookie_write(File *f, const void *data, | ||
size_t size) { | ||
auto cookie_file = reinterpret_cast<CookieFile *>(f); | ||
if (cookie_file->ops.write == nullptr) | ||
return 0; | ||
return static_cast<size_t>(cookie_file->ops.write( | ||
cookie_file->cookie, reinterpret_cast<const char *>(data), size)); | ||
} | ||
|
||
LIBC_INLINE FileIOResult CookieFile::cookie_read(File *f, void *data, | ||
size_t size) { | ||
auto cookie_file = reinterpret_cast<CookieFile *>(f); | ||
if (cookie_file->ops.read == nullptr) | ||
return 0; | ||
return static_cast<size_t>(cookie_file->ops.read( | ||
cookie_file->cookie, reinterpret_cast<char *>(data), size)); | ||
} | ||
|
||
LIBC_INLINE ErrorOr<off_t> CookieFile::cookie_seek(File *f, off_t offset, | ||
int whence) { | ||
auto cookie_file = reinterpret_cast<CookieFile *>(f); | ||
if (cookie_file->ops.seek == nullptr) { | ||
return Error(EINVAL); | ||
} | ||
off64_t offset64 = offset; | ||
int result = cookie_file->ops.seek(cookie_file->cookie, &offset64, whence); | ||
if (result == 0) | ||
return offset64; | ||
return -1; | ||
} | ||
|
||
LIBC_INLINE int CookieFile::cookie_close(File *f) { | ||
auto cookie_file = reinterpret_cast<CookieFile *>(f); | ||
if (cookie_file->ops.close == nullptr) | ||
return 0; | ||
int retval = cookie_file->ops.close(cookie_file->cookie); | ||
if (retval != 0) | ||
return retval; | ||
delete cookie_file; | ||
return 0; | ||
} | ||
|
||
} // namespace LIBC_NAMESPACE_DECL |
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.