Skip to content

Commit

Permalink
[libc] Make libc_errno point to internal errno for non-public builds.
Browse files Browse the repository at this point in the history
The macro llvmlibc_errno has also been removed. This change completes
the switch to using a hermetic errno for unit tests.

Fixes #61037

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D146005
  • Loading branch information
Siva Chandra Reddy committed Mar 14, 2023
1 parent 55612b8 commit 547e345
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 27 deletions.
1 change: 0 additions & 1 deletion libc/src/errno/CMakeLists.txt
Expand Up @@ -4,7 +4,6 @@ add_entrypoint_object(
libc_errno.cpp
HDRS
libc_errno.h # Include this
llvmlibc_errno.h # DEPRECATED: Will be removed soon
DEPENDS
libc.include.errno
)
4 changes: 3 additions & 1 deletion libc/src/errno/libc_errno.cpp
Expand Up @@ -9,17 +9,19 @@
namespace __llvm_libc {

extern "C" {
#ifdef LIBC_COPT_PUBLIC_PACKAGING
// TODO: Declare __llvmlibc_errno only under LIBC_COPT_PUBLIC_PACKAGING and
// __llvmlibc_internal_errno otherwise.
//
// In overlay mode, this will be an unused thread local variable as libc_errno
// will resolve to errno from the system libc's errno.h. In full build mode
// however, libc_errno will resolve to this thread local variable via the errno
// macro defined in LLVM libc's public errno.h header file.
// TODO: Use a macro to distinguish full build and overlay build which can be
// used to exclude __llvmlibc_errno under overlay build.
thread_local int __llvmlibc_errno;
#else
thread_local int __llvmlibc_internal_errno;
#endif
} // extern "C"

} // namespace __llvm_libc
2 changes: 1 addition & 1 deletion libc/src/errno/libc_errno.h
Expand Up @@ -29,7 +29,7 @@ extern thread_local int __llvmlibc_internal_errno;
// libc_errno, this header file will be "shipped" via an add_entrypoint_object
// target. At which point libc_errno, should point to __llvmlibc_internal_errno
// if LIBC_COPT_PUBLIC_PACKAGING is not defined.
#define libc_errno errno
#define libc_errno __llvm_libc::__llvmlibc_internal_errno

} // namespace __llvm_libc
#endif
Expand Down
21 changes: 0 additions & 21 deletions libc/src/errno/llvmlibc_errno.h

This file was deleted.

2 changes: 2 additions & 0 deletions libc/test/CMakeLists.txt
Expand Up @@ -9,6 +9,8 @@ add_header_library(
errno_setter_matcher
HDRS
ErrnoSetterMatcher.h
DEPENDS
libc.src.errno.errno
)

add_custom_target(check-libc)
Expand Down
6 changes: 3 additions & 3 deletions libc/test/ErrnoSetterMatcher.h
Expand Up @@ -9,9 +9,9 @@
#ifndef LLVM_LIBC_TEST_ERRNOSETTERMATCHER_H
#define LLVM_LIBC_TEST_ERRNOSETTERMATCHER_H

#include "src/errno/libc_errno.h"
#include "test/UnitTest/Test.h"

#include <errno.h>
#include <string.h>

namespace __llvm_libc {
Expand Down Expand Up @@ -42,8 +42,8 @@ template <typename T> class ErrnoSetterMatcher : public Matcher<T> {

bool match(T Got) {
ActualReturn = Got;
ActualErrno = errno;
errno = 0;
ActualErrno = libc_errno;
libc_errno = 0;
return Got == ExpectedReturn && ActualErrno == ExpectedErrno;
}
};
Expand Down

0 comments on commit 547e345

Please sign in to comment.