Skip to content

Commit 110ee16

Browse files
author
Siva Chandra Reddy
committed
[libc][NFC] Refactor internal errno.
This is in preparation for the transition to a solution to make libc tests hermetic with respect to their use of errno. The implementation of strdup has been switched over to libc_errno as an example of what the code looks like in the new way. See #61037 for more information. Reviewed By: lntue Differential Revision: https://reviews.llvm.org/D144928
1 parent acc30a1 commit 110ee16

File tree

9 files changed

+77
-42
lines changed

9 files changed

+77
-42
lines changed

libc/src/errno/CMakeLists.txt

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
1-
if (LLVM_LIBC_FULL_BUILD)
21
add_object_library(
32
errno
43
SRCS
5-
errno.cpp
4+
libc_errno.cpp
65
HDRS
7-
llvmlibc_errno.h
6+
libc_errno.h # Include this
7+
llvmlibc_errno.h # DEPRECATED: Will be removed soon
8+
DEPENDS
9+
libc.include.errno
810
)
9-
else()
10-
add_object_library(
11-
errno
12-
SRCS
13-
dummy_errno.cpp
14-
HDRS
15-
dummy_errno.h
16-
)
17-
endif()

libc/src/errno/dummy_errno.cpp

Lines changed: 0 additions & 7 deletions
This file was deleted.

libc/src/errno/dummy_errno.h

Lines changed: 0 additions & 7 deletions
This file was deleted.

libc/src/errno/errno.cpp

Lines changed: 0 additions & 9 deletions
This file was deleted.

libc/src/errno/libc_errno.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//===-- Implementation of errno -------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
namespace __llvm_libc {
10+
11+
extern "C" {
12+
// TODO: Declare __llvmlibc_errno only under LIBC_COPT_PUBLIC_PACKAGING and
13+
// __llvmlibc_internal_errno otherwise.
14+
//
15+
// In overlay mode, this will be an unused thread local variable as libc_errno
16+
// will resolve to errno from the system libc's errno.h. In full build mode
17+
// however, libc_errno will resolve to this thread local variable via the errno
18+
// macro defined in LLVM libc's public errno.h header file.
19+
// TODO: Use a macro to distinguish full build and overlay build which can be
20+
// used to exclude __llvmlibc_errno under overlay build.
21+
thread_local int __llvmlibc_errno;
22+
thread_local int __llvmlibc_internal_errno;
23+
} // extern "C"
24+
25+
} // namespace __llvm_libc

libc/src/errno/libc_errno.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//===-- Implementation header for errno -------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_ERRNO_LLVMLIBC_ERRNO_H
10+
#define LLVM_LIBC_SRC_ERRNO_LLVMLIBC_ERRNO_H
11+
12+
#include <errno.h>
13+
14+
// All of the libc runtime and test code should use the "libc_errno" macro. They
15+
// should not refer to the "errno" macro directly.
16+
#ifdef LIBC_COPT_PUBLIC_PACKAGING
17+
// This macro will resolve to errno from the errno.h file included above. Under
18+
// full build, this will be LLVM libc's errno. In overlay build, it will be
19+
// system libc's errno.
20+
#define libc_errno errno
21+
#else
22+
namespace __llvm_libc {
23+
24+
extern "C" {
25+
extern thread_local int __llvmlibc_internal_errno;
26+
} // extern "C"
27+
28+
// TODO: After all of libc/src and libc/test are switched over to use
29+
// libc_errno, this header file will be "shipped" via an add_entrypoint_object
30+
// target. At which point libc_errno, should point to __llvmlibc_internal_errno
31+
// if LIBC_COPT_PUBLIC_PACKAGING is not defined.
32+
#define libc_errno errno
33+
34+
} // namespace __llvm_libc
35+
#endif
36+
37+
#endif // LLVM_LIBC_SRC_ERRNO_LLVMLIBC_ERRNO_H

libc/src/errno/llvmlibc_errno.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@
99
#ifndef LLVM_LIBC_SRC_ERRNO_LLVMLIBC_ERRNO_H
1010
#define LLVM_LIBC_SRC_ERRNO_LLVMLIBC_ERRNO_H
1111

12-
// Internal code should use this and not use the errno macro from the
13-
// public header.
14-
extern thread_local int __llvmlibc_errno;
15-
#define llvmlibc_errno __llvmlibc_errno
12+
#include <errno.h>
13+
14+
// DEPRECATED: Use libc_errno from libc_errno.h instead. This macro is only
15+
// present to facilitate gradual transition (as in, in multiple simple patches)
16+
// to libc_errno.
17+
// TODO: After all of libc/src and libc/test is switched over to use libc_errno,
18+
// remove this macro and header file.
19+
#define llvmlibc_errno errno
1620

1721
#endif // LLVM_LIBC_SRC_ERRNO_LLVMLIBC_ERRNO_H

libc/src/string/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,6 @@ add_entrypoint_object(
176176
DEPENDS
177177
.memory_utils.memcpy_implementation
178178
.string_utils
179-
libc.include.errno
180179
libc.include.stdlib
181180
libc.src.errno.errno
182181
)

libc/src/string/strdup.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "src/string/strdup.h"
10+
#include "src/errno/libc_errno.h"
1011
#include "src/string/allocating_string_utils.h"
1112
#include "src/string/memory_utils/memcpy_implementations.h"
1213

1314
#include "src/__support/common.h"
1415

15-
#include <errno.h>
1616
#include <stdlib.h>
1717

1818
namespace __llvm_libc {
@@ -22,7 +22,7 @@ LLVM_LIBC_FUNCTION(char *, strdup, (const char *src)) {
2222
if (dup)
2323
return *dup;
2424
if (src != nullptr)
25-
errno = ENOMEM;
25+
libc_errno = ENOMEM;
2626
return nullptr;
2727
}
2828

0 commit comments

Comments
 (0)