From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 098AE3858C50 for ; Wed, 22 Nov 2023 18:27:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 098AE3858C50 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 098AE3858C50 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700677636; cv=none; b=OwuYNmojgG8mlXZjxQ9sR+MrLSocacKCVDLUecDC7IDh0U5VGBn6G5cNfNmFA57saehj8c/UllfOdkvp5NQzAnzKon6GNGUQAU7GcPlu91bnbKnpfno/TFBulg+kjCskfH1pre19FzIRgFCAyFfaayvD3BNJk428WEtb2hOC+do= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700677636; c=relaxed/simple; bh=FaDXc0+TTMRst+mf9DlzZYJLN0DYpF4j8H7sf8f/7K0=; h=DKIM-Signature:Message-ID:Date:MIME-Version:Subject:From:To; b=gBdNDaqgwiP7qvSNhPlTd+u5j8ff27F1uOAWqEQCQr3qNM2CByd5ig5gWPHT+5uYy7NnQPiWyt7b7bVbxEPS9XRwJQQL13jZeC5XcNHq7XfAGPuuIFFmgn9NFWsK/YiLoQp44IXZC5eAs7pJHLOO406HAJLUV15x52sZbwyEg20= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1700677623; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=W+YmQ180Zdc3DaJ5H8xI2rLSKh0fsUtmWrk3qEIRsK8=; b=AoxNlSmyGsxpeX0HPZM/ilBTE7KQ4DE60evSwlyEt0KXLkt/RvrI9u4oOzlm4jzoAlQuc2 7xBzerawaoH0CABLTJZ7c7rJpO0abp0ZU/Zluzoacp0kQZJ9Vvvkw0ZptIMLIiHvBTs0up sFiJQ10omNxlhOGDTC0jNmsrw8RWk30= Received: from mail-qv1-f71.google.com (mail-qv1-f71.google.com [209.85.219.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-199-od6JObwOPz6bCbm8OZ6SdQ-1; Wed, 22 Nov 2023 13:27:00 -0500 X-MC-Unique: od6JObwOPz6bCbm8OZ6SdQ-1 Received: by mail-qv1-f71.google.com with SMTP id 6a1803df08f44-67049a2c8b3so1454506d6.0 for ; Wed, 22 Nov 2023 10:27:00 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700677620; x=1701282420; h=content-transfer-encoding:in-reply-to:references:cc:to:from :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=W+YmQ180Zdc3DaJ5H8xI2rLSKh0fsUtmWrk3qEIRsK8=; b=pyEg5FTsbMFJl6owULQbaWntM+E+3hoWUT1kSc04cekBIE4KzY9wUcV2WrYv0AfIX6 SMvQH5qlVjL17oiOYzFofwnRQyRTKb1ti7ur3+SdMRtiSmAX7nc/oJLaOt0ZIiOGtL1h tmdjAg0yj1aUi9b+ncfgXKCd5lb8db84/rd+hAkcF4+5AkteHu1v98Pdv51s9KY3W0mA Vg6NYrRwzv+xOt8PTSKivuqdFs5eRMY+nhYE+Bfle2S57Qp+zu6RiodEe842ebTPomjG B0gBeHmIgqytyFfB8uGK7DAUe5QXZiySMcfW5ofCIqjMtmrvDKIiVAZLwyKXbmyQMF3k jZAA== X-Gm-Message-State: AOJu0YxpxqgEsv4DyB4Yhdnp75pD2j+xGxkNdZ5vX44p8bavxsI4ExJ3 6+WU8Vm4P7sLYAtIft6HEHue2qiF9HEkf8MFXlMzWZtZKR6zGky3/nYmqHasqwuRHwrPyrcFPPe bX6fJTOgFYXixyL5mnpGagiztUQ== X-Received: by 2002:a05:6214:1c0c:b0:679:d36e:7570 with SMTP id u12-20020a0562141c0c00b00679d36e7570mr4158472qvc.40.1700677620042; Wed, 22 Nov 2023 10:27:00 -0800 (PST) X-Google-Smtp-Source: AGHT+IFTVu1gpZpWniWYX9ccX8Uq7Dm2SJiV7+1fNfoYfxWtqrcmxqzOUkTj7LLJ3lt6tz1ygE8PpQ== X-Received: by 2002:a05:6214:1c0c:b0:679:d36e:7570 with SMTP id u12-20020a0562141c0c00b00679d36e7570mr4158451qvc.40.1700677619764; Wed, 22 Nov 2023 10:26:59 -0800 (PST) Received: from [192.168.1.145] (130-44-146-16.s12558.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.146.16]) by smtp.gmail.com with ESMTPSA id z13-20020a0cfc0d000000b00677fb605c8csm4622046qvo.55.2023.11.22.10.26.58 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 22 Nov 2023 10:26:59 -0800 (PST) Message-ID: <9f88b17b-4202-454c-abbd-6d9c999ceee8@redhat.com> Date: Wed, 22 Nov 2023 13:26:58 -0500 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH #2/4] c++: mark short-enums as packed From: Jason Merrill To: Alexandre Oliva Cc: gcc-patches@gcc.gnu.org, David Malcolm , Rainer Orth , Mike Stump , Nathan Sidwell , "H.J. Lu" References: <78e7499c-2278-44ff-8455-503e3690e3fd@redhat.com> <9d922575-6f5a-4458-b3dc-a25dce128e24@redhat.com> In-Reply-To: <9d922575-6f5a-4458-b3dc-a25dce128e24@redhat.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-6.1 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_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On 11/22/23 13:12, Jason Merrill wrote: > On 11/22/23 03:17, Alexandre Oliva wrote: >> On Nov 20, 2023, Jason Merrill wrote: >> >>> I think the warning is wrong here. >> >> Interesting...  Yeah, your analysis makes perfect sense. >> >> Still, we're left with a divergence WRT the TYPE_PACKED status of enum >> types between C and C++. >> >> It sort of kind of makes sense to mark short enums as packed, because, >> well, they are. > > The enum is conceptually packed into a smaller integer type, sure. > >> Even enum types with explicit attribute packed, that IIUC uses the same >> underlying type selection as -fshort-enums, IIRC are not be marked with >> TYPE_PACKED in C++, at least not at the place where I proposed to set >> it.  Do you consider that behavior correct? > > Since attribute ((packed)) has this meaning, it seems reasonable to set > TYPE_PACKED to express it. > >> Even if the warning happens to be buggy in this regard, it is at best >> (or worst) accessory to this patch, in that it makes that difference >> between languages apparent, and I worry that there might be other middle >> end tests involving TYPE_PACKED that would get things different in C vs >> C++.  (admittedly, I haven't searched for occurrences of TYPE_PACKED in >> the tree, but I could, to alleviate my concerns, in case there's a >> decision to keep them different) > > The middle-end doesn't seem to use TYPE_PACKED for anything other than > structure layout. > >>> In the analyzer testcase, we have a cast from an >>> enum pointer that we don't know what it points to, and even if it did >>> point to the obj_type member of struct connection, that wouldn't be a >>> problem because it's at offset 0. >> >> Maybe I misunderstand the point of the warning, but ISTM that the >> circumstance it's warning about is real: the member is not as aligned as >> the enclosing struct, so the cast is risky.  Now, I suppose the idiom of >> finding the enclosing struct given a member is common enough that we >> don't want to warn about it in general.  I'm not sure what makes packed >> structs special in this regard, though.  I don't really see much >> difference, more laxly-aligned fields seem equally warn-worthy, whether >> the enclosing struct is packed or not, but what do I know? > > Exactly.  If we want to warn about casting from pointer to less-aligned > type to pointer to more-aligned type, that's already > -Wcast-align=strict; whether the lower alignment is due to TYPE_PACKED > seems irrelevant. > > The observation that the type-based warning is a subset of > -Wcast-align=strict was previously made in the discussion of the patch > for PR88928. > > And the motivating testcase for the warning was about converting from > unaligned int* to aligned int*, not to a different type at all.  And > that warning doesn't involve TYPE_PACKED. > > The clang -Waddress-of-packed-member doesn't seem to include the > type-based warning. > >>> Also, -fshort-enums has nothing to do with structure packing >> >> *nod*, it's about packing of the enum type itself.  It is some sort of a >> degenerated aggregate type ;-) But yeah, I guess it doesn't fit the >> circumstance the warning was meant to catch, and the fact that in C is >> does is a consequence of marking C short enums as TYPE_PACKED. >> >> Which might be a bug in C. >> >> But wouldn't it be a bug in C++ if an enum with attribute packed weren't >> markd as TYPE_PACKED?  Or is TYPE_PACKED really meant to say something >> about the enclosing struct rather than about the enclosed type itself? >> (am I getting too philosophical here? :-) > > I'm coming to the conclusion that your C++ patch is fine but we should > remove the TYPE_PACKED warning from > check_address_or_pointer_of_packed_member.  And maybe add > -Wcast-align=strict to -Wextra. Since I seem to have opinions, I'm preparing a patch for this. Jason