libstdc++: Address '-Wunused-function' for 'print_raw' (was: [committed] libstdc++: Simplify print_raw function for debug assertions)
Checks
Commit Message
Hi!
On 2022-10-14T15:36:02+0100, Jonathan Wakely via Gcc-patches <gcc-patches@gcc.gnu.org> wrote:
> Tested powerpc64le-linux. Pushed to trunk.
>
> -- >8 --
>
> Replace two uses of print_raw where it's clearer to just use fprintf
> directly. Then the only remaining use of print_raw is as the print_func
> argument of pretty_print.
OK to push the attached
"libstdc++: Address '-Wunused-function' for 'print_raw'",
or should this be addressed differently?
Grüße
Thomas
> When called by pretty_print the count is
> either a positive integer or -1, so we can simplify print_raw itself.
>
> Remove the default argument, because it's never used. Remove the check
> for nbc == 0, which never happens (but would be harmless if it did).
> Replace the conditional expression with a single call to fprintf, using
> INT_MAX as the maximum length.
>
> libstdc++-v3/ChangeLog:
>
> * src/c++11/debug.cc (print_raw): Simplify.
> (print_word): Print indentation by calling fprintf directly.
> (_Error_formatter::_M_error): Print unindented string by calling
> fprintf directly.
> ---
> libstdc++-v3/src/c++11/debug.cc | 21 ++++++++-------------
> 1 file changed, 8 insertions(+), 13 deletions(-)
>
> diff --git a/libstdc++-v3/src/c++11/debug.cc b/libstdc++-v3/src/c++11/debug.cc
> index abc4124c01e..f2b25fbefce 100644
> --- a/libstdc++-v3/src/c++11/debug.cc
> +++ b/libstdc++-v3/src/c++11/debug.cc
> @@ -37,6 +37,7 @@
> #include <cstdlib> // for std::abort
> #include <cctype> // for std::isspace.
> #include <cstring> // for std::strstr.
> +#include <climits> // for INT_MAX
>
> #include <algorithm> // for std::min.
>
> @@ -609,14 +610,11 @@ namespace
> { print_word(ctx, word, Length - 1); }
>
> void
> - print_raw(PrintContext& ctx, const char* str, ptrdiff_t nbc = -1)
> + print_raw(PrintContext& ctx, const char* str, ptrdiff_t nbc)
> {
> - if (nbc != 0)
> - {
> - ctx._M_column += (nbc > 0)
> - ? fprintf(stderr, "%.*s", (int)nbc, str)
> - : fprintf(stderr, "%s", str);
> - }
> + if (nbc == -1)
> + nbc = INT_MAX;
> + ctx._M_column += fprintf(stderr, "%.*s", (int)nbc, str);
> }
>
> void
> @@ -645,12 +643,9 @@ namespace
> || (ctx._M_column + visual_length < ctx._M_max_length)
> || (visual_length >= ctx._M_max_length && ctx._M_column == 1))
> {
> - // If this isn't the first line, indent
> + // If this isn't the first line, indent.
> if (ctx._M_column == 1 && !ctx._M_first_line)
> - {
> - const char spacing[PrintContext::_S_indent + 1] = " ";
> - print_raw(ctx, spacing, PrintContext::_S_indent);
> - }
> + ctx._M_column += fprintf(stderr, "%*c", PrintContext::_S_indent, ' ');
>
> int written = fprintf(stderr, "%.*s", (int)length, word);
>
> @@ -1166,7 +1161,7 @@ namespace __gnu_debug
> PrintContext ctx;
> if (_M_file)
> {
> - print_raw(ctx, _M_file);
> + ctx._M_column += fprintf(stderr, "%s", _M_file);
> print_literal(ctx, ":");
> go_to_next_line = true;
> }
-----------------
Siemens Electronic Design Automation GmbH; Anschrift: Arnulfstraße 201, 80634 München; Gesellschaft mit beschränkter Haftung; Geschäftsführer: Thomas Heurung, Frank Thürauf; Sitz der Gesellschaft: München; Registergericht München, HRB 106955
Comments
On Sat, 15 Oct 2022 at 11:52, Thomas Schwinge <thomas@codesourcery.com> wrote:
>
> Hi!
>
> On 2022-10-14T15:36:02+0100, Jonathan Wakely via Gcc-patches <gcc-patches@gcc.gnu.org> wrote:
> > Tested powerpc64le-linux. Pushed to trunk.
> >
> > -- >8 --
> >
> > Replace two uses of print_raw where it's clearer to just use fprintf
> > directly. Then the only remaining use of print_raw is as the print_func
> > argument of pretty_print.
>
> OK to push the attached
> "libstdc++: Address '-Wunused-function' for 'print_raw'",
> or should this be addressed differently?
Oh yes, I didn't notice it's only used within the conditional block,
because I only tested with stacktrace enabled.
I think it would be a little better to move print_raw down to where
it's actually needed:
--- a/libstdc++-v3/src/c++11/debug.cc
+++ b/libstdc++-v3/src/c++11/debug.cc
@@ -609,14 +609,6 @@ namespace
print_literal(PrintContext& ctx, const char(&word)[Length])
{ print_word(ctx, word, Length - 1); }
- void
- print_raw(PrintContext& ctx, const char* str, ptrdiff_t nbc)
- {
- if (nbc == -1)
- nbc = INT_MAX;
- ctx._M_column += fprintf(stderr, "%.*s", (int)nbc, str);
- }
-
void
print_word(PrintContext& ctx, const char* word, ptrdiff_t nbc = -1)
{
@@ -1092,6 +1084,14 @@ namespace
{ print_string(ctx, str, nbc, nullptr, 0); }
#if _GLIBCXX_HAVE_STACKTRACE
+ void
+ print_raw(PrintContext& ctx, const char* str, ptrdiff_t nbc)
+ {
+ if (nbc == -1)
+ nbc = INT_MAX;
+ ctx._M_column += fprintf(stderr, "%.*s", (int)nbc, str);
+ }
+
int
print_backtrace(void* data, __UINTPTR_TYPE__ pc, const char* filename,
int lineno, const char* function)
I'll push that later today, or feel free to do it yourself if you want
the warning to go away :-)
On Sat, 15 Oct 2022 at 14:30, Jonathan Wakely <jwakely@redhat.com> wrote:
>
> On Sat, 15 Oct 2022 at 11:52, Thomas Schwinge <thomas@codesourcery.com> wrote:
> >
> > Hi!
> >
> > On 2022-10-14T15:36:02+0100, Jonathan Wakely via Gcc-patches <gcc-patches@gcc.gnu.org> wrote:
> > > Tested powerpc64le-linux. Pushed to trunk.
> > >
> > > -- >8 --
> > >
> > > Replace two uses of print_raw where it's clearer to just use fprintf
> > > directly. Then the only remaining use of print_raw is as the print_func
> > > argument of pretty_print.
> >
> > OK to push the attached
> > "libstdc++: Address '-Wunused-function' for 'print_raw'",
> > or should this be addressed differently?
>
> Oh yes, I didn't notice it's only used within the conditional block,
> because I only tested with stacktrace enabled.
>
> I think it would be a little better to move print_raw down to where
> it's actually needed:
>
> --- a/libstdc++-v3/src/c++11/debug.cc
> +++ b/libstdc++-v3/src/c++11/debug.cc
> @@ -609,14 +609,6 @@ namespace
> print_literal(PrintContext& ctx, const char(&word)[Length])
> { print_word(ctx, word, Length - 1); }
>
> - void
> - print_raw(PrintContext& ctx, const char* str, ptrdiff_t nbc)
> - {
> - if (nbc == -1)
> - nbc = INT_MAX;
> - ctx._M_column += fprintf(stderr, "%.*s", (int)nbc, str);
> - }
> -
> void
> print_word(PrintContext& ctx, const char* word, ptrdiff_t nbc = -1)
> {
> @@ -1092,6 +1084,14 @@ namespace
> { print_string(ctx, str, nbc, nullptr, 0); }
>
> #if _GLIBCXX_HAVE_STACKTRACE
> + void
> + print_raw(PrintContext& ctx, const char* str, ptrdiff_t nbc)
> + {
> + if (nbc == -1)
> + nbc = INT_MAX;
> + ctx._M_column += fprintf(stderr, "%.*s", (int)nbc, str);
> + }
> +
> int
> print_backtrace(void* data, __UINTPTR_TYPE__ pc, const char* filename,
> int lineno, const char* function)
>
>
> I'll push that later today, or feel free to do it yourself if you want
> the warning to go away :-)
Now pushed as r13-3314-g030a08c8572049
Thanks for pointing out the warning.
From 9ba7d0e6026ef4c3d095b0a57f9b88a87df403ea Mon Sep 17 00:00:00 2001
From: Thomas Schwinge <thomas@codesourcery.com>
Date: Sat, 15 Oct 2022 12:15:58 +0200
Subject: [PATCH] libstdc++: Address '-Wunused-function' for 'print_raw'
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Per recent
commit r13-3303-gcf0b7e9787c3686c47219a725f2cbcaa19faaaca
"libstdc++: Simplify print_raw function for debug assertions":
> Replace two uses of print_raw where it's clearer to just use fprintf
> directly. Then the only remaining use of print_raw is as the print_func
> argument of pretty_print.
But that one is only compiled '#if _GLIBCXX_HAVE_STACKTRACE'.
In a '--enable-werror' build I've thus run into:
[...]/source-gcc/libstdc++-v3/src/c++11/debug.cc:613:3: error: ‘void {anonymous}::print_raw(PrintContext&, const char*, ptrdiff_t)’ defined but not used [-Werror=unused-function]
613 | print_raw(PrintContext& ctx, const char* str, ptrdiff_t nbc)
| ^~~~~~~~~
cc1plus: all warnings being treated as errors
libstdc++-v3/
* src/c++11/debug.cc (print_raw): Only '#if _GLIBCXX_HAVE_STACKTRACE'.
---
libstdc++-v3/src/c++11/debug.cc | 2 ++
1 file changed, 2 insertions(+)
@@ -609,6 +609,7 @@ namespace
print_literal(PrintContext& ctx, const char(&word)[Length])
{ print_word(ctx, word, Length - 1); }
+#if _GLIBCXX_HAVE_STACKTRACE
void
print_raw(PrintContext& ctx, const char* str, ptrdiff_t nbc)
{
@@ -616,6 +617,7 @@ namespace
nbc = INT_MAX;
ctx._M_column += fprintf(stderr, "%.*s", (int)nbc, str);
}
+#endif
void
print_word(PrintContext& ctx, const char* word, ptrdiff_t nbc = -1)
--
2.35.1