Skip to content

[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

Merged
merged 4 commits into from
Jun 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libc/config/linux/aarch64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -972,6 +972,7 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.stdio.getc_unlocked
libc.src.stdio.getchar
libc.src.stdio.getchar_unlocked
libc.src.stdio.perror
libc.src.stdio.putc
libc.src.stdio.putchar
libc.src.stdio.puts
Expand Down
1 change: 1 addition & 0 deletions libc/config/linux/riscv/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1098,6 +1098,7 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.stdio.getc_unlocked
libc.src.stdio.getchar
libc.src.stdio.getchar_unlocked
libc.src.stdio.perror
libc.src.stdio.putc
libc.src.stdio.putchar
libc.src.stdio.puts
Expand Down
1 change: 1 addition & 0 deletions libc/config/linux/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1116,6 +1116,7 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.stdio.getc_unlocked
libc.src.stdio.getchar
libc.src.stdio.getchar_unlocked
libc.src.stdio.perror
libc.src.stdio.putc
libc.src.stdio.putchar
libc.src.stdio.puts
Expand Down
6 changes: 6 additions & 0 deletions libc/include/stdio.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,12 @@ functions:
- POSIX
return_type: int
arguments: []
- name: perror
standards:
- stdc
return_type: void
arguments:
- type: const char *
- name: printf
standards:
- stdc
Expand Down
1 change: 1 addition & 0 deletions libc/src/stdio/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ add_stdio_entrypoint_object(fopen)
add_stdio_entrypoint_object(fclose)
add_stdio_entrypoint_object(fread_unlocked)
add_stdio_entrypoint_object(fread)
add_stdio_entrypoint_object(perror)
add_stdio_entrypoint_object(puts)
add_stdio_entrypoint_object(fputs)
add_stdio_entrypoint_object(fwrite_unlocked)
Expand Down
15 changes: 15 additions & 0 deletions libc/src/stdio/generic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,21 @@ add_generic_entrypoint_object(
libc.src.__support.File.platform_file
)

add_generic_entrypoint_object(
perror
SRCS
perror.cpp
HDRS
../perror.h
DEPENDS
libc.src.errno.errno
libc.src.__support.StringUtil.error_to_string
libc.src.__support.CPP.string_view
libc.src.__support.File.file
libc.src.__support.File.platform_file
libc.src.__support.File.platform_stderr
)

add_generic_entrypoint_object(
fputs
SRCS
Expand Down
81 changes: 81 additions & 0 deletions libc/src/stdio/generic/perror.cpp
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
20 changes: 20 additions & 0 deletions libc/src/stdio/perror.h
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
12 changes: 12 additions & 0 deletions libc/test/src/stdio/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,18 @@ add_libc_test(
libc.src.stdio.puts
)

add_libc_test(
perror_test
HERMETIC_TEST_ONLY # writes to libc's stderr
SUITE
libc_stdio_unittests
SRCS
perror_test.cpp
DEPENDS
libc.src.stdio.perror
libc.src.errno.errno
)

add_libc_test(
fputs_test
HERMETIC_TEST_ONLY # writes to libc's stdout and stderr
Expand Down
32 changes: 32 additions & 0 deletions libc/test/src/stdio/perror_test.cpp
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);
}
Loading