From patchwork Mon Feb 20 13:53:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 65263 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 15033383FE11 for ; Mon, 20 Feb 2023 13:53:47 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 15033383FE11 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1676901227; bh=QVHIkSRElSMOPmwDAsZLgruQMzwIPsFhJ/QAytCT64M=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=xBnhXTgPGKEpCStRS5W6nZd4vaAgKEwbK9EbEX/swF5/U5kyBJwjlX+TAg/Rkm24r X6RdieTFof0TEM2wDO3ygYu+uMeWYiJ/1jH2ySJ4gulmFuAjEdIcLV/YU1tMozzN+S euJCrh7n93obeLwOCSr+nb4uPzEYDAxtK3NcXTXI= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id ED3233841820 for ; Mon, 20 Feb 2023 13:53:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org ED3233841820 Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-64-chxYlnD5NSe7P9IJCIfvYw-1; Mon, 20 Feb 2023 08:53:20 -0500 X-MC-Unique: chxYlnD5NSe7P9IJCIfvYw-1 Received: by mail-wm1-f70.google.com with SMTP id bg26-20020a05600c3c9a00b003e21a3f4e84so233851wmb.8 for ; Mon, 20 Feb 2023 05:53:20 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=QVHIkSRElSMOPmwDAsZLgruQMzwIPsFhJ/QAytCT64M=; b=Xe2JMgjTJUHo0lfwaWjVlhRJvu5yvlzKnzfipMSME/t2fESv0xbHhw20CjiK0zdhZr ZhokdvWOn3yRj9fy3oi9q4Whf+axA8uLAeCasi7q+BRDZ4apWzlrQCGanC2j2D1G2ieS UhJRIPbKNOmyyz43dnjMcGruaxaeqYUC77jA2yfGRsPhRlkCYh7efJEVe8HDSgvrRPaL MhkjzH3cu8zr/qFSFirxqWPhapGbazyN8LgsR3vVaX9eilt3+1gQF03bac/XP++IJXEC TDS1XqYPuN7Y+xJLbXSpZLu8s81d//V6BORuXGVYjQOy00VJ74YF1rpuUbQwRwad5QIx YNqg== X-Gm-Message-State: AO0yUKWa6DKRfi0aAJ2vRGWNA8SdcYi76V3StUHjqgYenv0c9RidkEES idJjz1AIAQuNZOqZutIpxna8X52ie2e6sG2rcOj0XFD+7+S92YIOhzNLErN8yZLGjFDizvNd/Vz oblOckGnoFSaLcBY2AqnFnaNszy9zcU7y5GcH5NHV0wNXpgIfT9tMQd2JpoIPO1aQ1DzECmH746 P4JDY= X-Received: by 2002:a5d:53c1:0:b0:2c5:5234:882c with SMTP id a1-20020a5d53c1000000b002c55234882cmr649829wrw.7.1676901199219; Mon, 20 Feb 2023 05:53:19 -0800 (PST) X-Google-Smtp-Source: AK7set8vxky3yMx5Omqt7fBPL2iJt58EIfeVqZA6QKwv9VII9qRB4jRt3Z+io+FgUlhhl86Cw3wJYA== X-Received: by 2002:a5d:53c1:0:b0:2c5:5234:882c with SMTP id a1-20020a5d53c1000000b002c55234882cmr649816wrw.7.1676901198855; Mon, 20 Feb 2023 05:53:18 -0800 (PST) Received: from localhost (95.72.115.87.dyn.plus.net. [87.115.72.95]) by smtp.gmail.com with ESMTPSA id t18-20020a5d42d2000000b002c6e84cadcbsm3971756wrr.72.2023.02.20.05.53.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Feb 2023 05:53:18 -0800 (PST) To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCH] gdb: don't treat empty enums as flag enums Date: Mon, 20 Feb 2023 13:53:16 +0000 Message-Id: <92a46a82cbc43033fc03942674b80979cc91e572.1676901184.git.aburgess@redhat.com> X-Mailer: git-send-email 2.25.4 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.6 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Andrew Burgess via Gdb-patches From: Andrew Burgess Reply-To: Andrew Burgess Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" In C++ it is possible to use an empty enum as a strong typedef. For example, a user could write: enum class my_type : unsigned char {}; Now my_type can be used like 'unsigned char' except the compiler will not allow implicit conversion too and from the native 'unsigned char' type. This is used in the standard library for things like std::byte. Currently, when GDB prints a value of type my_type, it looks like this: (gdb) print my_var $1 = (unknown: 0x4) Which isn't great. This gets worse when we consider something like: std::vector vec; When using a pretty-printer, this could look like this: std::vector of length 2, capacity 2 = {(unknown: 0x2), (unknown: 0x4)} Clearly not great. This is described in PR gdb/30148. The problem here is in dwarf2/read.c, we assume all enums are flag enums unless we find an enumerator with a non-flag like value. Clearly an empty enum contains no non-flag values, so we assume the enum is a flag enum. I propose adding an extra check here; that is, an empty enum should never be a flag enum. With this the above cases look more like: (gdb) print my_var $1 = (unknown: 0x4) and: std::vector of length 2, capacity 2 = {2, 4} Which look much better. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30148 Reviewed-By: Tom Tromey --- gdb/dwarf2/read.c | 2 ++ gdb/testsuite/gdb.cp/empty-enum.cc | 31 +++++++++++++++++++ gdb/testsuite/gdb.cp/empty-enum.exp | 48 +++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+) create mode 100644 gdb/testsuite/gdb.cp/empty-enum.cc create mode 100644 gdb/testsuite/gdb.cp/empty-enum.exp base-commit: 733da2ced8f948b299bdd10b7ff40146095f43f9 diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c index eb37c776989..df7f2da555c 100644 --- a/gdb/dwarf2/read.c +++ b/gdb/dwarf2/read.c @@ -13178,6 +13178,8 @@ update_enumeration_type_from_children (struct die_info *die, memcpy (type->fields (), fields.data (), sizeof (struct field) * fields.size ()); } + else + flag_enum = 0; if (unsigned_enum) type->set_is_unsigned (true); diff --git a/gdb/testsuite/gdb.cp/empty-enum.cc b/gdb/testsuite/gdb.cp/empty-enum.cc new file mode 100644 index 00000000000..1a0eeced710 --- /dev/null +++ b/gdb/testsuite/gdb.cp/empty-enum.cc @@ -0,0 +1,31 @@ +/* Copyright 2023 Free Software Foundation, Inc. + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +enum enum1 {}; + +enum class enum2 : unsigned char {}; + +void +breakpt (enum1 arg1, enum2 arg2) +{ + /* Nothing. */ +} + +int +main () +{ + breakpt ((enum1) 8, (enum2) 4); + + return 0; +} diff --git a/gdb/testsuite/gdb.cp/empty-enum.exp b/gdb/testsuite/gdb.cp/empty-enum.exp new file mode 100644 index 00000000000..83cb8cb3973 --- /dev/null +++ b/gdb/testsuite/gdb.cp/empty-enum.exp @@ -0,0 +1,48 @@ +# Copyright 2023 Free Software Foundation, Inc. +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +# Test how GDB displays empty enums. At one point an enum with no +# enumeration values would be considered a flag enum, and, as a +# consequence any value with that type would display like: +# +# (gdb) print enum_var +# $1 = (unknown: 0x8) +# +# Which resulted in a lot of noise. Now GDB treats empty enums as a +# non-flag enum, and should print them like this: +# +# (gdb) print enum_var +# $1 = 8 +# +# This test checks this behaviour. + +standard_testfile .cc + +if {[prepare_for_testing "failed to prepare" $testfile $srcfile]} { + return -1 +} + +if {![runto_main]} { + return -1 +} + +gdb_breakpoint "breakpt" +gdb_continue_to_breakpoint "stop in breakpt" + + +gdb_test "print arg1" " = 8" +gdb_test "print arg2" " = 4" + +gdb_test "ptype arg1" "type = enum enum1 : unsigned int \\{\\}" +gdb_test "ptype arg2" "type = enum class enum2 : unsigned char \\{\\}"