-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[libc] Implement perror #143624
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
[libc] Implement perror #143624
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
//===-- Implementation of perror ------------------------------------------===// | ||
// | ||
// 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/perror.h" | ||
#include "src/__support/CPP/string_view.h" | ||
#include "src/__support/File/file.h" | ||
#include "src/__support/StringUtil/error_to_string.h" | ||
#include "src/__support/libc_errno.h" | ||
#include "src/__support/macros/config.h" | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
static int write_out(cpp::string_view str_view, File *f) { | ||
if (str_view.size() > 0) { | ||
auto result = f->write_unlocked(str_view.data(), str_view.size()); | ||
if (result.has_error()) | ||
return result.error; | ||
} | ||
return 0; | ||
} | ||
|
||
// separate function so that we can return early on error but still get the | ||
// unlock. This function sets errno and should not be called elsewhere. | ||
static void write_sequence(cpp::string_view str_view, | ||
cpp::string_view err_str) { | ||
int write_err; | ||
// TODO: this seems like there should be some sort of queue system to | ||
// deduplicate this code. | ||
|
||
// FORMAT: | ||
// if str != nullptr and doesn't start with a null byte: | ||
// "[str]: [strerror(errno)]\n" | ||
// else | ||
// "[strerror(errno)]\n" | ||
if (str_view.size() > 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not to be addressed in this PR, but probably combining the strings into a single write to stderr is better, especially when stderr is unbuffered. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That'd be difficult since the strings are of unknown length. We could probably collapse it down to 2 writes since we can check the length of the strerror strings but the user could input anything. |
||
write_err = write_out(str_view, LIBC_NAMESPACE::stderr); | ||
if (write_err != 0) { | ||
libc_errno = write_err; | ||
return; | ||
} | ||
|
||
write_err = write_out(": ", LIBC_NAMESPACE::stderr); | ||
if (write_err != 0) { | ||
libc_errno = write_err; | ||
return; | ||
} | ||
} | ||
|
||
write_err = write_out(err_str, LIBC_NAMESPACE::stderr); | ||
if (write_err != 0) { | ||
libc_errno = write_err; | ||
return; | ||
} | ||
|
||
write_err = write_out("\n", LIBC_NAMESPACE::stderr); | ||
if (write_err != 0) { | ||
libc_errno = write_err; | ||
return; | ||
} | ||
} | ||
|
||
LLVM_LIBC_FUNCTION(void, perror, (const char *str)) { | ||
const char empty_str[1] = {'\0'}; | ||
if (str == nullptr) | ||
str = empty_str; | ||
cpp::string_view str_view(str); | ||
|
||
cpp::string_view err_str = get_error_string(libc_errno); | ||
|
||
// We need to lock the stream to ensure the newline is always appended. | ||
LIBC_NAMESPACE::stderr->lock(); | ||
write_sequence(str_view, err_str); | ||
LIBC_NAMESPACE::stderr->unlock(); | ||
} | ||
|
||
} // namespace LIBC_NAMESPACE_DECL |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
//===-- Implementation header of perror -------------------------*- 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_PERROR_H | ||
#define LLVM_LIBC_SRC_STDIO_PERROR_H | ||
|
||
#include "src/__support/macros/config.h" | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
void perror(const char *s); | ||
|
||
} // namespace LIBC_NAMESPACE_DECL | ||
|
||
#endif // LLVM_LIBC_SRC_STDIO_PERROR_H |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
//===-- Unittests for perror ---------------------------------------------===// | ||
// | ||
// 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/perror.h" | ||
|
||
#include "src/__support/libc_errno.h" | ||
#include "test/UnitTest/Test.h" | ||
|
||
// The standard says perror prints directly to stderr and returns nothing. This | ||
// makes it rather difficult to test automatically. | ||
|
||
// TODO: figure out redirecting stderr so this test can check correctness. | ||
TEST(LlvmLibcPerrorTest, PrintOut) { | ||
LIBC_NAMESPACE::libc_errno = 0; | ||
constexpr char simple[] = "A simple string"; | ||
LIBC_NAMESPACE::perror(simple); | ||
|
||
// stick to stdc errno values, specifically 0, EDOM, ERANGE, and EILSEQ. | ||
LIBC_NAMESPACE::libc_errno = EDOM; | ||
LIBC_NAMESPACE::perror("Print this and an error"); | ||
|
||
LIBC_NAMESPACE::libc_errno = EILSEQ; | ||
LIBC_NAMESPACE::perror("\0 shouldn't print this."); | ||
|
||
LIBC_NAMESPACE::libc_errno = ERANGE; | ||
LIBC_NAMESPACE::perror(nullptr); | ||
} |
Uh oh!
There was an error while loading. Please reload this page.