From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr1-f66.google.com (mail-wr1-f66.google.com [209.85.221.66]) by sourceware.org (Postfix) with ESMTPS id 0E38D3840C36 for ; Thu, 27 Aug 2020 19:12:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 0E38D3840C36 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=palves.net Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=alves.ped@gmail.com Received: by mail-wr1-f66.google.com with SMTP id c15so6367131wrs.11 for ; Thu, 27 Aug 2020 12:12:24 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=xBIN2wjsajVO6Wnot1X5edCD6ismXSFRmj3rae5SO0o=; b=HdT6ew04nqnQMC5jaS4UbCJRsC6WP2+bEio1mbjLcKExKPwF95mEaDCZQaq5lWmdko gbWyF57K0Ld6ToBWpor1Nthr11dthhaRGkQgFjN3GBD6vlorRj2mLzdKRViLhBG4hbnm py0MiMlKeYOEImzokodiI9m63UnCgaSlchXxe9R+1Alfb3YIZEvzyDF5TjVclFONN4vS 8QnVC/s88k4mE/kHxM5q46uOvhF4fDAGlOiTuxbX7d1YXBR/SdJ4WJmBGsU7kF3hAc1R YdJDG+F3059MJ5CTFt9Lj4u/OPKlCcHZ/cNKrRFS+Qpe9vZpCOqyZWX5Fr19jh5Tug0O bR0A== X-Gm-Message-State: AOAM5328T5+ywtonkHRY6N/yyvhyjVaxwzasEPylqxAouhKzeVMHrPux HWlHNEVd7I+L8ME01QyJ95KqRgmRUjKfWw== X-Google-Smtp-Source: ABdhPJyDZ8Ba/dk41UJjlOSyM+ckkdtBoNozXKiQG/8bwUce65saJ70XayM2ddANl5oaqBbUW5WiFw== X-Received: by 2002:adf:e50e:: with SMTP id j14mr20562976wrm.238.1598555542203; Thu, 27 Aug 2020 12:12:22 -0700 (PDT) Received: from ?IPv6:2001:8a0:f905:5600:56ee:75ff:fe8d:232b? ([2001:8a0:f905:5600:56ee:75ff:fe8d:232b]) by smtp.gmail.com with ESMTPSA id y24sm6528259wmi.17.2020.08.27.12.12.20 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 27 Aug 2020 12:12:21 -0700 (PDT) Subject: Re: [PATCH] Reject ambiguous C++ field accesses To: Luis Machado , gdb-patches@sourceware.org References: <20200827180251.20244-1-pedro@palves.net> From: Pedro Alves Message-ID: <30604ffc-365f-d580-18fb-3596c74913e8@palves.net> Date: Thu, 27 Aug 2020 20:12:16 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.2.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3.0 required=5.0 tests=BAYES_00, BODY_8BITS, FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS, KAM_DMARC_STATUS, NICE_REPLY_A, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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-List-Received-Date: Thu, 27 Aug 2020 19:12:25 -0000 On 8/27/20 7:45 PM, Luis Machado wrote: > On 8/27/20 3:02 PM, Pedro Alves wrote: >> The gdb.cp/ambiguous.exp testcase had been disabled for many years, >> but recently it was re-enabled.  However, it is failing everywhere. >> That is because it is testing an old feature that is gone from GDB. >> >> The testcase is expecting to see an ambiguous field warning, like: >> >>   # X is derived from A1 and A2; both A1 and A2 have a member 'x' >>   send_gdb "print x.x\n" >>   gdb_expect { >>      -re "warning: x ambiguous; using X::A2::x. Use a cast to disambiguate.\r\n\\$\[0-9\]* = \[-\]*\[0-9\]*\r\n$gdb_prompt $" { >>     pass "print x.x" >>      } >>      -re "warning: x ambiguous; using X::A1::x. Use a cast to disambiguate.\r\n\\$\[0-9\]* = \[-\]*\[0-9\]*\r\n$gdb_prompt $" { >>     pass "print x.x" >>      } >>      -re ".*$gdb_prompt $" { fail "print x.x" } >>      timeout { fail "(timeout) print x.x" } >>    } >> >> However, GDB just accesses one of the candidates without warning or >> error: >> >>   print x.x >>   $1 = 1431655296 >>   (gdb) FAIL: gdb.cp/ambiguous.exp: print x.x >> >> (The weird number is because the testcase does not initialize the >> variables.) >> >> The testcase come in originally with the big HP merge: >> >>   +Sun Jan 10 23:44:11 1999  David Taylor  >>   + >>   + >>   +       The following files are part of the HP merge; some had longer >>   +       names at HP, but have been renamed to be no more than 14 >>   +       characters in length. >> >> Looking at the tree back then, we find that warning: >> >>   /* Helper function used by value_struct_elt to recurse through baseclasses. >>      Look for a field NAME in ARG1. Adjust the address of ARG1 by OFFSET bytes, >>      and search in it assuming it has (class) type TYPE. >>      If found, return value, else return NULL. >> >>      If LOOKING_FOR_BASECLASS, then instead of looking for struct fields, >>      look for a baseclass named NAME.  */ >> >>   static value_ptr >>   search_struct_field (name, arg1, offset, type, looking_for_baseclass) >>        char *name; >>        register value_ptr arg1; >>        int offset; >>        register struct type *type; >>        int looking_for_baseclass; >>   { >>     int found = 0; >>     char found_class[1024]; >>     value_ptr v; >>     struct type *vbase = NULL; >> >>     found_class[0] = '\000'; >> >>     v = search_struct_field_aux (name, arg1, offset, type, looking_for_baseclass, &found, found_class, &vbase); >>     if (found > 1) >>       warning ("%s ambiguous; using %s::%s. Use a cast to disambiguate.", >>                name, found_class, name); >> >>     return v; >>   } >> >> >> However, in current GDB, search_struct_field does not handle the >> ambiguous field case, nor is that warning found anywhere.  Somehow it >> got lost over the years.  That seems like a regression, because the >> compiler (as per language rules) rejects the ambiguous accesses as >> well.  E.g.: >> >>   gdb.cp/ambiguous.cc:98:5: error: request for member 'x' is ambiguous >>      98 |   x.x = 1; >>         |     ^ >>   gdb.cp/ambiguous.cc:10:7: note: candidates are: 'int A2::x' >>      10 |   int x; >>         |       ^ >>   gdb.cp/ambiguous.cc:4:7: note:                 'int A1::x' >>       4 |   int x; >>         |       ^ >> >> >> This patch restores the feature, though implemented differently and >> with better user experience, IMHO.  An ambiguous access is now an >> error instead of a warning, and also GDB shows you the all candidates, >> like: >> >>   (gdb) print x.x >>   Request for member 'x' is ambiguous in type 'X'. Candidates are: >>     'int A1::x' (X -> A1) >>     'int A2::x' (X -> A2) >>   (gdb) print j.x >>   Request for member 'x' is ambiguous in type 'J'. Candidates are: >>     'int A1::x' (J -> K -> A1) >>     'int A1::x' (J -> L -> A1) > > Would it make sense to spare the users the touble of having to re-type/cast things and, instead, display all the possible values with an indication of what the fields are for each value? > > That would be more intuitive to me, given the debugger is only inspecting things and not enforcing a particular syntax for a source file. > > Users tend to be lazy. They just want to see values, not be syntactically correct. :-) > No, that would not work with not-as-simple expressions. Like "p foo(x.x + 2)".