[libcxx-commits] [libcxx] [libc++] Fix unnecessary fflush() in __vprint_unicode() on POSIX (PR #70321)

Dimitrij Mijoski via libcxx-commits libcxx-commits at lists.llvm.org
Tue Dec 19 02:54:01 PST 2023


https://github.com/dimztimz updated https://github.com/llvm/llvm-project/pull/70321

>From 16de38596598fcd8346efb01a5e63d3db8d8ac1d Mon Sep 17 00:00:00 2001
From: Dimitrij Mijoski <dmjpp at hotmail.com>
Date: Thu, 26 Oct 2023 14:03:57 +0200
Subject: [PATCH] [libc++] Fix unnecessary fflush() in __vprint_unicode() on
 POSIX

Fixes #70142
---
 libcxx/include/print                          | 11 +--
 .../print.fun/vprint_unicode_posix.pass.cpp   | 77 -------------------
 2 files changed, 1 insertion(+), 87 deletions(-)
 delete mode 100644 libcxx/test/libcxx/input.output/iostream.format/print.fun/vprint_unicode_posix.pass.cpp

diff --git a/libcxx/include/print b/libcxx/include/print
index d119c8bda74976..29d417310e1058 100644
--- a/libcxx/include/print
+++ b/libcxx/include/print
@@ -230,15 +230,6 @@ __vprint_nonunicode(FILE* __stream, string_view __fmt, format_args __args, bool
 // terminal when the output is redirected. Typically during testing the
 // output is redirected to be able to capture it. This makes it hard to
 // test this code path.
-template <class = void> // TODO PRINT template or availability markup fires too eagerly (http://llvm.org/PR61563).
-_LIBCPP_HIDE_FROM_ABI inline void
-__vprint_unicode_posix(FILE* __stream, string_view __fmt, format_args __args, bool __write_nl, bool __is_terminal) {
-  // TODO PRINT Should flush errors throw too?
-  if (__is_terminal)
-    std::fflush(__stream);
-
-  __print::__vprint_nonunicode(__stream, __fmt, __args, __write_nl);
-}
 
 #    ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
 template <class = void> // TODO PRINT template or availability markup fires too eagerly (http://llvm.org/PR61563).
@@ -310,7 +301,7 @@ __vprint_unicode([[maybe_unused]] FILE* __stream,
   // Windows there is a different API. This API requires transcoding.
 
 #    ifndef _WIN32
-  __print::__vprint_unicode_posix(__stream, __fmt, __args, __write_nl, __print::__is_terminal(__stream));
+  __print::__vprint_nonunicode(__stream, __fmt, __args, __write_nl);
 #    elif !defined(_LIBCPP_HAS_NO_WIDE_CHARACTERS)
   __print::__vprint_unicode_windows(__stream, __fmt, __args, __write_nl, __print::__is_terminal(__stream));
 #    else
diff --git a/libcxx/test/libcxx/input.output/iostream.format/print.fun/vprint_unicode_posix.pass.cpp b/libcxx/test/libcxx/input.output/iostream.format/print.fun/vprint_unicode_posix.pass.cpp
deleted file mode 100644
index 9a50770d97dbcb..00000000000000
--- a/libcxx/test/libcxx/input.output/iostream.format/print.fun/vprint_unicode_posix.pass.cpp
+++ /dev/null
@@ -1,77 +0,0 @@
-//===----------------------------------------------------------------------===//
-// 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
-//
-//===----------------------------------------------------------------------===//
-
-// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
-// UNSUPPORTED: no-filesystem
-// UNSUPPORTED: GCC-ALWAYS_INLINE-FIXME
-
-// XFAIL: availability-fp_to_chars-missing
-
-// fmemopen is available starting in Android M (API 23)
-// XFAIL: target={{.+}}-android{{(eabi)?(21|22)}}
-
-// REQUIRES: has-unix-headers
-
-// <print>
-
-// Tests the implementation of
-//   void __print::__vprint_unicode_posix(FILE* __stream, string_view __fmt,
-//                                        format_args __args, bool __write_nl,
-//                                        bool __is_terminal);
-//
-// In the library when the stdout is redirected to a file it is no
-// longer considered a terminal and the special terminal handling is no
-// longer executed. By testing this function we can "force" emulate a
-// terminal.
-// Note __write_nl is tested by the public API.
-
-#include <algorithm>
-#include <array>
-#include <cassert>
-#include <cstdio>
-#include <print>
-
-#include "test_macros.h"
-
-int main(int, char**) {
-  std::array<char, 100> buffer;
-  std::ranges::fill(buffer, '*');
-
-  FILE* file = fmemopen(buffer.data(), buffer.size(), "wb");
-  assert(file);
-
-  // Test the file is buffered.
-  std::fprintf(file, "Hello");
-  assert(std::ftell(file) == 5);
-#if defined(TEST_HAS_GLIBC) &&                                                                                         \
-    !(__has_feature(address_sanitizer) || __has_feature(thread_sanitizer) || __has_feature(memory_sanitizer))
-  assert(std::ranges::all_of(buffer, [](char c) { return c == '*'; }));
-#endif
-
-  // Test writing to a "non-terminal" stream does not flush.
-  std::__print::__vprint_unicode_posix(file, " world", std::make_format_args(), false, false);
-  assert(std::ftell(file) == 11);
-#if defined(TEST_HAS_GLIBC) &&                                                                                         \
-    !(__has_feature(address_sanitizer) || __has_feature(thread_sanitizer) || __has_feature(memory_sanitizer))
-  assert(std::ranges::all_of(buffer, [](char c) { return c == '*'; }));
-#endif
-
-  // Test writing to a "terminal" stream flushes before writing.
-  std::__print::__vprint_unicode_posix(file, "!", std::make_format_args(), false, true);
-  assert(std::ftell(file) == 12);
-  assert(std::string_view(buffer.data(), buffer.data() + 11) == "Hello world");
-#if defined(TEST_HAS_GLIBC)
-  // glibc does not flush after a write.
-  assert(buffer[11] != '!');
-#endif
-
-  // Test everything is written when closing the stream.
-  std::fclose(file);
-  assert(std::string_view(buffer.data(), buffer.data() + 12) == "Hello world!");
-
-  return 0;
-}



More information about the libcxx-commits mailing list