From patchwork Tue Oct 15 09:47:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 1997287 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=huTk+W6W; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4XSTn30Mxrz1xv6 for ; Tue, 15 Oct 2024 20:48:22 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C35C63858C42 for ; Tue, 15 Oct 2024 09:48:20 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTP id 06C3D3858D20 for ; Tue, 15 Oct 2024 09:47:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 06C3D3858D20 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 06C3D3858D20 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1728985682; cv=none; b=iwerNZA3mfhLz0K7e40goFi4grRH3fSAJ7X2l07Dsu0q3drYjJDaLNMupzkL4C6Xu+JSI+NB+vRLUTpkPXCdES1mrlrhbK2ROkah7G6nQPguUWvHBuzrvK+hG0nCdmKqsQmLx9lwLzngGZdsd6puibYbnvpgGUNN0tSv3tM7tuY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1728985682; c=relaxed/simple; bh=/KAHiZNdh+mH/S3y8sXZcMd1i5qm5g5dKlvfwukuFqI=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=D9OwEEXioleUXlKFg+9YhGSFy55t+kXLgq5QNoe5EiCm68ucEEhlw4/2oFS3mnICUGS9qHP//JAk8O+WyZ4qz9hSl/+mo6SlKWou/EMlshr3jdFAS0iX6OUtU5olg+/UPIxD0o8DtMIV4lRJ7QMN0EHOnET2kMBV4LR2MCkcslQ= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1728985671; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type; bh=yOxmleBIEQ9DH5d72QL4tlZ8GNaafJEKXuwi7laDm2A=; b=huTk+W6WSmytyD/ENY0NdbydJfeBF5SqW4pwOQ+yhSVCQK4UM161B1hCXnF0srDkRzNWtl 7yGZBeMSycrcp98+m/UFd0BmiNUdVaCQfHyyEgMzesinKYE7Y8PZtJGfcdJnq19VhmejFk AD3GtaATGGkdRLrtXTnKBzOBgf4IvfM= Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-496-8aoa1i-pMvCabrngSM4dlQ-1; Tue, 15 Oct 2024 05:47:50 -0400 X-MC-Unique: 8aoa1i-pMvCabrngSM4dlQ-1 Received: from mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.15]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id A87C41955F41; Tue, 15 Oct 2024 09:47:49 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.45.224.16]) by mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id B3E9B1956089; Tue, 15 Oct 2024 09:47:48 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 49F9lje62884061 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Tue, 15 Oct 2024 11:47:45 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 49F9ljYl2884060; Tue, 15 Oct 2024 11:47:45 +0200 Date: Tue, 15 Oct 2024 11:47:45 +0200 From: Jakub Jelinek To: Richard Biener , David Malcolm Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] genmatch: Add selftests to genmatch for diag_vfprintf Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.15 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-0.9 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, RCVD_IN_SBL_CSS, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org Hi! The following patch adds selftests to genmatch to verify the new printing routine there. So that I can rely on HAVE_DECL_FMEMOPEN (host test), the tests are done solely in stage2+ where we link the host libcpp etc. to genmatch. The tests have been adjusted from pretty-print.cc (test_pp_format), and I've added to that function two new tests because I've noticed nothing was testing the %M$.*N$s etc. format specifiers. Tested on x86_64-linux, ok for trunk? 2024-10-15 Jakub Jelinek * configure.ac (gcc_AC_CHECK_DECLS): Add fmemopen. * configure: Regenerate. * config.in: Regenerate. * Makefile.in (build/genmatch.o): Add -DGENMATCH_SELFTESTS to BUILD_CPPFLAGS for stage2+ genmatch. * genmatch.cc (test_diag_vfprintf, genmatch_diag_selftests): New functions. (main): Call genmatch_diag_selftests. * pretty-print.cc (test_pp_format): Add two tests, one for %M$.*N$s and one for %M$.Ns. Jakub --- gcc/configure.ac.jj 2024-09-24 15:14:54.044163018 +0200 +++ gcc/configure.ac 2024-10-15 11:06:04.568842228 +0200 @@ -1629,7 +1629,7 @@ gcc_AC_CHECK_DECLS(getenv atol atoll asp madvise stpcpy strnlen strsignal strverscmp \ strtol strtoul strtoll strtoull setenv unsetenv \ errno snprintf vsnprintf vasprintf malloc realloc calloc \ - free getopt clock getpagesize ffs gcc_UNLOCKED_FUNCS, , ,[ + free getopt clock getpagesize ffs fmemopen gcc_UNLOCKED_FUNCS, , ,[ #include "ansidecl.h" #include "system.h"]) --- gcc/configure.jj 2024-09-24 15:14:54.043163031 +0200 +++ gcc/configure 2024-10-15 11:06:10.606756527 +0200 @@ -12084,7 +12084,7 @@ for ac_func in getenv atol atoll asprint madvise stpcpy strnlen strsignal strverscmp \ strtol strtoul strtoll strtoull setenv unsetenv \ errno snprintf vsnprintf vasprintf malloc realloc calloc \ - free getopt clock getpagesize ffs clearerr_unlocked feof_unlocked ferror_unlocked fflush_unlocked fgetc_unlocked fgets_unlocked fileno_unlocked fprintf_unlocked fputc_unlocked fputs_unlocked fread_unlocked fwrite_unlocked getchar_unlocked getc_unlocked putchar_unlocked putc_unlocked + free getopt clock getpagesize ffs fmemopen clearerr_unlocked feof_unlocked ferror_unlocked fflush_unlocked fgetc_unlocked fgets_unlocked fileno_unlocked fprintf_unlocked fputc_unlocked fputs_unlocked fread_unlocked fwrite_unlocked getchar_unlocked getc_unlocked putchar_unlocked putc_unlocked do ac_tr_decl=`$as_echo "HAVE_DECL_$ac_func" | $as_tr_cpp` { $as_echo "$as_me:${as_lineno-$LINENO}: checking whether $ac_func is declared" >&5 --- gcc/config.in.jj 2024-07-31 21:47:22.637999164 +0200 +++ gcc/config.in 2024-10-15 11:06:13.153720379 +0200 @@ -1018,6 +1018,13 @@ #endif +/* Define to 1 if we found a declaration for 'fmemopen', otherwise define to + 0. */ +#ifndef USED_FOR_TARGET +#undef HAVE_DECL_FMEMOPEN +#endif + + /* Define to 1 if we found a declaration for 'fprintf_unlocked', otherwise define to 0. */ #ifndef USED_FOR_TARGET --- gcc/Makefile.in.jj 2024-10-14 19:40:46.989958038 +0200 +++ gcc/Makefile.in 2024-10-15 10:56:52.027684998 +0200 @@ -3143,6 +3143,7 @@ else BUILD_CPPLIB = $(CPPLIB) $(LIBIBERTY) build/genmatch$(build_exeext): BUILD_LIBDEPS += $(LIBINTL_DEP) $(LIBICONV_DEP) build/genmatch$(build_exeext): BUILD_LIBS += $(LIBINTL) $(LIBICONV) +build/genmatch.o: BUILD_CPPFLAGS += -DGENMATCH_SELFTESTS endif build/genmatch$(build_exeext) : $(BUILD_CPPLIB) \ --- gcc/genmatch.cc.jj 2024-10-14 19:40:46.990958024 +0200 +++ gcc/genmatch.cc 2024-10-15 11:33:54.521110736 +0200 @@ -584,6 +584,138 @@ diag_vfprintf (FILE *f, int err_no, cons fprintf (f, "%s", q); } +#if defined(GENMATCH_SELFTESTS) && defined(HAVE_DECL_FMEMOPEN) +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wsuggest-attribute=format" + +static void +test_diag_vfprintf (const char *expected, const char *msg, ...) +{ + char buf[256]; + va_list ap; + FILE *f = fmemopen (buf, 256, "w"); + gcc_assert (f != NULL); + va_start (ap, msg); + diag_vfprintf (f, 0, msg, &ap); + va_end (ap); + fclose (f); + gcc_assert (strcmp (buf, expected) == 0); +} + +#pragma GCC diagnostic pop + +static void +genmatch_diag_selftests (void) +{ + /* Verify that plain text is passed through unchanged. */ + test_diag_vfprintf ("unformatted", "unformatted"); + + /* Verify various individual format codes, in the order listed in the + comment for pp_format above. For each code, we append a second + argument with a known bit pattern (0x12345678), to ensure that we + are consuming arguments correctly. */ + test_diag_vfprintf ("-27 12345678", "%d %x", -27, 0x12345678); + test_diag_vfprintf ("-5 12345678", "%i %x", -5, 0x12345678); + test_diag_vfprintf ("10 12345678", "%u %x", 10, 0x12345678); + test_diag_vfprintf ("17 12345678", "%o %x", 15, 0x12345678); + test_diag_vfprintf ("cafebabe 12345678", "%x %x", 0xcafebabe, 0x12345678); + test_diag_vfprintf ("-27 12345678", "%ld %x", (long)-27, 0x12345678); + test_diag_vfprintf ("-5 12345678", "%li %x", (long)-5, 0x12345678); + test_diag_vfprintf ("10 12345678", "%lu %x", (long)10, 0x12345678); + test_diag_vfprintf ("17 12345678", "%lo %x", (long)15, 0x12345678); + test_diag_vfprintf ("cafebabe 12345678", "%lx %x", (long)0xcafebabe, + 0x12345678); + test_diag_vfprintf ("-27 12345678", "%lld %x", (long long)-27, 0x12345678); + test_diag_vfprintf ("-5 12345678", "%lli %x", (long long)-5, 0x12345678); + test_diag_vfprintf ("10 12345678", "%llu %x", (long long)10, 0x12345678); + test_diag_vfprintf ("17 12345678", "%llo %x", (long long)15, 0x12345678); + test_diag_vfprintf ("cafebabe 12345678", "%llx %x", (long long)0xcafebabe, + 0x12345678); + test_diag_vfprintf ("-27 12345678", "%wd %x", HOST_WIDE_INT_C (-27), + 0x12345678); + test_diag_vfprintf ("-5 12345678", "%wi %x", HOST_WIDE_INT_C (-5), + 0x12345678); + test_diag_vfprintf ("10 12345678", "%wu %x", HOST_WIDE_INT_UC (10), + 0x12345678); + test_diag_vfprintf ("17 12345678", "%wo %x", HOST_WIDE_INT_C (15), + 0x12345678); + test_diag_vfprintf ("0xcafebabe 12345678", "%wx %x", + HOST_WIDE_INT_C (0xcafebabe), 0x12345678); + test_diag_vfprintf ("-27 12345678", "%zd %x", (ssize_t)-27, 0x12345678); + test_diag_vfprintf ("-5 12345678", "%zi %x", (ssize_t)-5, 0x12345678); + test_diag_vfprintf ("10 12345678", "%zu %x", (size_t)10, 0x12345678); + test_diag_vfprintf ("17 12345678", "%zo %x", (size_t)15, 0x12345678); + test_diag_vfprintf ("cafebabe 12345678", "%zx %x", (size_t)0xcafebabe, + 0x12345678); + test_diag_vfprintf ("-27 12345678", "%td %x", (ptrdiff_t)-27, 0x12345678); + test_diag_vfprintf ("-5 12345678", "%ti %x", (ptrdiff_t)-5, 0x12345678); + test_diag_vfprintf ("10 12345678", "%tu %x", (ptrdiff_t)10, 0x12345678); + test_diag_vfprintf ("17 12345678", "%to %x", (ptrdiff_t)15, 0x12345678); + test_diag_vfprintf ("1afebabe 12345678", "%tx %x", (ptrdiff_t)0x1afebabe, + 0x12345678); + test_diag_vfprintf ("1.000000 12345678", "%f %x", 1.0, 0x12345678); + test_diag_vfprintf ("A 12345678", "%c %x", 'A', 0x12345678); + test_diag_vfprintf ("hello world 12345678", "%s %x", "hello world", + 0x12345678); + + /* Not nul-terminated. */ + char arr[5] = { '1', '2', '3', '4', '5' }; + test_diag_vfprintf ("123 12345678", "%.*s %x", 3, arr, 0x12345678); + test_diag_vfprintf ("1234 12345678", "%.*s %x", -1, "1234", 0x12345678); + test_diag_vfprintf ("12345 12345678", "%.*s %x", 7, "12345", 0x12345678); + + /* We can't test for %p; the pointer is printed in an implementation-defined + manner. */ + test_diag_vfprintf ("normal colored normal 12345678", + "normal %rcolored%R normal %x", + "error", 0x12345678); + + /* TODO: + %m: strerror(text->err_no) - does not consume a value *ap. */ + test_diag_vfprintf ("% 12345678", "%% %x", 0x12345678); + test_diag_vfprintf ("' 12345678", "%< %x", 0x12345678); + test_diag_vfprintf ("' 12345678", "%> %x", 0x12345678); + test_diag_vfprintf ("' 12345678", "%' %x", 0x12345678); + test_diag_vfprintf ("abc 12345678", "%.*s %x", 3, "abcdef", 0x12345678); + test_diag_vfprintf ("abc 12345678", "%.3s %x", "abcdef", 0x12345678); + + /* Verify flag 'q'. */ + test_diag_vfprintf ("'foo' 12345678", "%qs %x", "foo", 0x12345678); + + /* Verify %Z. */ + int v[] = { 1, 2, 3 }; + test_diag_vfprintf ("1, 2, 3 12345678", "%Z %x", v, 3, 0x12345678); + + int v2[] = { 0 }; + test_diag_vfprintf ("0 12345678", "%Z %x", v2, 1, 0x12345678); + + /* Verify that combinations work, along with unformatted text. */ + test_diag_vfprintf ("the quick brown fox jumps over the lazy dog", + "the %s %s %s jumps over the %s %s", + "quick", "brown", "fox", "lazy", "dog"); + test_diag_vfprintf ("item 3 of 7", "item %i of %i", 3, 7); + test_diag_vfprintf ("problem with 'bar' at line 10", + "problem with %qs at line %i", "bar", 10); + + /* Verified numbered args. */ + test_diag_vfprintf ("foo: second bar: first", + "foo: %2$s bar: %1$s", "first", "second"); + test_diag_vfprintf ("foo: 1066 bar: 1776", + "foo: %2$i bar: %1$i", 1776, 1066); + test_diag_vfprintf ("foo: second bar: 1776", + "foo: %2$s bar: %1$i", 1776, "second"); + test_diag_vfprintf ("foo: sec bar: 3360", + "foo: %3$.*2$s bar: %1$o", 1776, 3, "second"); + test_diag_vfprintf ("foo: seco bar: 3360", + "foo: %2$.4s bar: %1$o", 1776, "second"); +} +#else +static void +genmatch_diag_selftests (void) +{ +} +#endif + static bool #if GCC_VERSION >= 4001 __attribute__((format (gcc_diag, 5, 0))) @@ -6091,6 +6223,8 @@ main (int argc, char **argv) return 1; } + genmatch_diag_selftests (); + if (!s_include_file) s_include_file = s_header_file; --- gcc/pretty-print.cc.jj 2024-10-09 13:21:15.868012931 +0200 +++ gcc/pretty-print.cc 2024-10-15 11:37:02.322440466 +0200 @@ -3368,6 +3368,14 @@ test_pp_format () "foo: second bar: 1776", "foo: %2$s bar: %1$i", 1776, "second"); + assert_pp_format (SELFTEST_LOCATION, + "foo: sec bar: 3360", + "foo: %3$.*2$s bar: %1$o", + 1776, 3, "second"); + assert_pp_format (SELFTEST_LOCATION, + "foo: seco bar: 3360", + "foo: %2$.4s bar: %1$o", + 1776, "second"); } static void