Skip to content

Commit 4e6789a

Browse files
committed
[libc++][print] Moves is_terminal to the dylib.
Having the test in the header requires including unistd.h on POSIX platforms. This header has other declarations which may conflict with code that uses named declarations provided by this header. For example code using "int pipe;" would conflict with the function pipe in this header. Moving the code to the dylib means std::print would not be available on Apple backdeployment targets. On POSIX platforms there is no transcoding required so a not Standard conforming implementation is still a useful and the observable differences are minimal. This behaviour has been done for print before #76293. Note questions have been raised in LWG4044 "Confusing requirements for std::print on POSIX platforms", whether or not the isatty check on POSIX platforms is required. When this LWG issue is resolved the backdeployment targets could become Standard compliant. This patch is intended to be backported to the LLVM-18 branch. Fixes: #79782
1 parent 1437a83 commit 4e6789a

File tree

3 files changed

+23
-15
lines changed

3 files changed

+23
-15
lines changed

libcxx/include/print

+6-6
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ namespace std {
3232
*/
3333

3434
#include <__assert> // all public C++ headers provide the assertion handler
35+
#include <__availability>
3536
#include <__concepts/same_as.h>
3637
#include <__config>
3738
#include <__system_error/system_error.h>
@@ -43,10 +44,6 @@ namespace std {
4344
#include <string_view>
4445
#include <version>
4546

46-
#if __has_include(<unistd.h>)
47-
# include <unistd.h>
48-
#endif
49-
5047
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
5148
# pragma GCC system_header
5249
#endif
@@ -68,7 +65,8 @@ _LIBCPP_EXPORTED_FROM_ABI bool __is_windows_terminal(FILE* __stream);
6865
// Note the function is only implemented on the Windows platform.
6966
_LIBCPP_EXPORTED_FROM_ABI void __write_to_windows_console(FILE* __stream, wstring_view __view);
7067
# endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
71-
68+
#elif __has_include(<unistd.h>)
69+
_LIBCPP_EXPORTED_FROM_ABI bool __is_posix_terminal(FILE* __stream);
7270
#endif // _LIBCPP_WIN32API
7371

7472
#if _LIBCPP_STD_VER >= 23
@@ -200,10 +198,12 @@ _LIBCPP_HIDE_FROM_ABI inline bool __is_terminal(FILE* __stream) {
200198
// the behavior in the test. This is not part of the public API.
201199
# ifdef _LIBCPP_TESTING_PRINT_IS_TERMINAL
202200
return _LIBCPP_TESTING_PRINT_IS_TERMINAL(__stream);
201+
# elif _LIBCPP_AVAILABILITY_HAS_PRINT == 0
202+
return false;
203203
# elif defined(_LIBCPP_WIN32API)
204204
return std::__is_windows_terminal(__stream);
205205
# elif __has_include(<unistd.h>)
206-
return isatty(fileno(__stream));
206+
return std::__is_posix_terminal(__stream);
207207
# else
208208
# error "Provide a way to determine whether a FILE* is a terminal"
209209
# endif

libcxx/lib/abi/x86_64-unknown-linux-gnu.libcxxabi.v1.stable.exceptions.nonew.abilist

+1
Original file line numberDiff line numberDiff line change
@@ -1188,6 +1188,7 @@
11881188
{'is_defined': True, 'name': '_ZNSt3__118shared_timed_mutex8try_lockEv', 'type': 'FUNC'}
11891189
{'is_defined': True, 'name': '_ZNSt3__118shared_timed_mutexC1Ev', 'type': 'FUNC'}
11901190
{'is_defined': True, 'name': '_ZNSt3__118shared_timed_mutexC2Ev', 'type': 'FUNC'}
1191+
{'is_defined': True, 'name': '_ZNSt3__119__is_posix_terminalEP8_IO_FILE', 'type': 'FUNC'}
11911192
{'is_defined': True, 'name': '_ZNSt3__119__shared_mutex_base11lock_sharedEv', 'type': 'FUNC'}
11921193
{'is_defined': True, 'name': '_ZNSt3__119__shared_mutex_base13unlock_sharedEv', 'type': 'FUNC'}
11931194
{'is_defined': True, 'name': '_ZNSt3__119__shared_mutex_base15try_lock_sharedEv', 'type': 'FUNC'}

libcxx/src/print.cpp

+16-9
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,26 @@
88

99
#include <__config>
1010

11-
#if defined(_LIBCPP_WIN32API)
11+
#include <cstdlib>
12+
#include <print>
13+
14+
#include <__system_error/system_error.h>
1215

13-
# include <cstdlib>
14-
# include <print>
16+
#include "filesystem/error.h"
1517

18+
#if defined(_LIBCPP_WIN32API)
1619
# define WIN32_LEAN_AND_MEAN
1720
# define NOMINMAX
1821
# include <io.h>
1922
# include <windows.h>
20-
21-
# include <__system_error/system_error.h>
22-
23-
# include "filesystem/error.h"
23+
#elif __has_include(<unistd.h>)
24+
# include <unistd.h>
25+
#endif
2426

2527
_LIBCPP_BEGIN_NAMESPACE_STD
2628

29+
#if defined(_LIBCPP_WIN32API)
30+
2731
_LIBCPP_EXPORTED_FROM_ABI bool __is_windows_terminal(FILE* __stream) {
2832
// Note the Standard does this in one call, but it's unclear whether
2933
// an invalid handle is allowed when calling GetConsoleMode.
@@ -52,6 +56,9 @@ __write_to_windows_console([[maybe_unused]] FILE* __stream, [[maybe_unused]] wst
5256
}
5357
# endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
5458

55-
_LIBCPP_END_NAMESPACE_STD
59+
#elif __has_include(<unistd.h>) // !_LIBCPP_WIN32API
5660

57-
#endif // !_LIBCPP_WIN32API
61+
_LIBCPP_EXPORTED_FROM_ABI bool __is_posix_terminal(FILE* __stream) { return isatty(fileno(__stream)); }
62+
#endif
63+
64+
_LIBCPP_END_NAMESPACE_STD

0 commit comments

Comments
 (0)