Message ID | 20230516164947.86543-2-adobriyan@gmail.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp580756vqo; Tue, 16 May 2023 10:11:00 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5zIWkErcOGIw9jFbt1Lbev+7iuRDWGRmult2+FNk211sHwQ7bzzEbR1X6FzQLlfo6RthZf X-Received: by 2002:a05:6a21:329c:b0:102:8f0a:5acf with SMTP id yt28-20020a056a21329c00b001028f0a5acfmr29108293pzb.62.1684257060484; Tue, 16 May 2023 10:11:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684257060; cv=none; d=google.com; s=arc-20160816; b=yS9FGbXa9GC4hA/tIYP4FlZyOKMPmVmDnCcmN9cFGW8JZeL+n8yHJzjyOhStYFmzsJ bCP2eIUhQ04rDqzKVhgEEXk1D+sje/8SSh7tf+xJwm9apPuusvlFngze5A8stKXadg1H r7nadv8i2Rxq0ADZ/ZDn/kAph2T/Od+x3k+s++2wzsEHxkIT29m9hIFA9IUeQI6N+mY0 kHpu6tZQU5I4u0P9wYcjGzdr6m9WcmfRyB2qkGo2Ck8yzgciYpzeB1GifiRyNT1Is7og gwSnYP9isAkoTv2FuAh2P06Hqep7VkatNevrpVYDrlXHKZn3GA6cQ0aPQp3rEiJ8PE8F alLg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Y4qcI2UTgZBwmxBO2YPA1Xt+uX1yMcShj7SW095AgSs=; b=uDWGSWJrbs35ZLGxNMUE6FNWSRjmnBpdsRLxe1wJkrLWSaiRuJpZWgczpQg/ADRLdx p11WXzhvl/PZRRRcphGeKS6IdbIeZebwDtHmITor3txY8MlF35UrgaP0pYaugv/soK1h lIADd2XPa35TGmUIdZJPbejkuJnEOVzCuJVj5roH1EhDYHUWDE5todpE1U1glu22dX88 vtFEmC9uLay1Xh/h2XTNGn7PkEKKc3CLjRRXdWJP1dFGdtasUXfJb/TvM963Q4c9JCep Ka/Hx5pcTT0uUHg40tqdtxbnJdYG1CKu7w8EicRxt7IsDkfRZg3bM9Vyuey16J1vF4V2 8Mwg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=eIo3pQUe; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l185-20020a6388c2000000b005342480fcd2si4468415pgd.282.2023.05.16.10.10.44; Tue, 16 May 2023 10:11:00 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=eIo3pQUe; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231690AbjEPQuR (ORCPT <rfc822;peekingduck44@gmail.com> + 99 others); Tue, 16 May 2023 12:50:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54776 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231607AbjEPQuL (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Tue, 16 May 2023 12:50:11 -0400 Received: from mail-wr1-x431.google.com (mail-wr1-x431.google.com [IPv6:2a00:1450:4864:20::431]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 63A877EE1 for <linux-kernel@vger.kernel.org>; Tue, 16 May 2023 09:50:06 -0700 (PDT) Received: by mail-wr1-x431.google.com with SMTP id ffacd0b85a97d-3078c092056so7815292f8f.1 for <linux-kernel@vger.kernel.org>; Tue, 16 May 2023 09:50:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1684255805; x=1686847805; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Y4qcI2UTgZBwmxBO2YPA1Xt+uX1yMcShj7SW095AgSs=; b=eIo3pQUeGiiTOYwGxpxeVm6OwmeYB3uuGYEtG2v1WCR+hnsIrCDHG+XTmCHl0NFWD6 njUQgZ++1AiorFA3iZjn0WqtlqZALAR7ckqO0jrOreNresiY+15oo749U6wRfhpXb1m7 IraRKkPZ0qU3x9S9t6C676Ig1BY6/CZIh33cqIvEChr456j58p7Dj2hJejGghagBURlI qisIcT+L8xEMfOhRZccCzQihJtU3Z2IHhGCtTiCP06OqKOgQgrSrGo/7yQHZCsOdB/9s G+zwnIFMVBGPbUnXtBnghxM5tkaRhsztKKE96qu7Lq6cvHuhynUEUZELIiWxZ8wQSanT uxXA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684255805; x=1686847805; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Y4qcI2UTgZBwmxBO2YPA1Xt+uX1yMcShj7SW095AgSs=; b=cSn/rO/HtLGQeutpuD9W1IQBKvmtPS5+FcvDzxG/8gu9mb8ncPbeztICBazzNXLYz/ n+KtXToHrTh4PHeC9ciN8oSOUQqP6v40VdP6i/Lrm7/LghuiGX2Zt8nhaL6Lww6VYk7A 679EUS4wvwvdQU3Vkg3ioR4WhKhzvbxK0GJ0FGTVbcI+LelTjDfXgiQlg8E4jcLuNnKz 1dnkSXcToL5BcINsh4ut5EmvGZxRbFePlpq04mZS6QUSR9rvXMYvDAMsbdYUUPoaSW7w o8twV1OSzR1xr2pgBKn9dOHzmGltqQ/l0Vlza3iRIZyU8BJmDgM28Xlyl0udXF3xSgsD P8Xw== X-Gm-Message-State: AC+VfDxRzRCAULMHBAcWYMgT4I2xnrpD70GX11Ek+bsQesvf4kOuXCXb 186RhDG48er4vlOa11iJOJRe88n/tQ== X-Received: by 2002:a5d:540d:0:b0:309:38f4:fb45 with SMTP id g13-20020a5d540d000000b0030938f4fb45mr1753616wrv.46.1684255804677; Tue, 16 May 2023 09:50:04 -0700 (PDT) Received: from localhost.localdomain ([46.53.250.37]) by smtp.gmail.com with ESMTPSA id d10-20020adffd8a000000b002f22c44e974sm3094789wrr.102.2023.05.16.09.50.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 May 2023 09:50:04 -0700 (PDT) From: Alexey Dobriyan <adobriyan@gmail.com> To: akpm@linux-foundation.org Cc: linux-kernel@vger.kernel.org, adobriyan@gmail.com Subject: [PATCH 2/3] auto: add "auto" keyword as alias for __auto_type Date: Tue, 16 May 2023 19:49:46 +0300 Message-Id: <20230516164947.86543-2-adobriyan@gmail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230516164947.86543-1-adobriyan@gmail.com> References: <20230516164947.86543-1-adobriyan@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,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 lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1766071531325426921?= X-GMAIL-MSGID: =?utf-8?q?1766071531325426921?= |
Series |
[1/3] auto, kbuild: flatten KBUILD_CFLAGS
|
|
Commit Message
Alexey Dobriyan
May 16, 2023, 4:49 p.m. UTC
It has similar semantics to "auto" keyword from a language
which can not be named on this mailing list, in particular:
{
int a;
const auto b = a; // const char b = a;
b = 1; // compile error
}
{
char a;
auto b = a; // char b = a;
// no integer promotions
static_assert(sizeof(b) == 1);
}
{
int a;
const auto p = &a; // int *const p = &a;
*p = 1; // works because const is applied only to top-level
}
It can be used to save on macroexpansion inside macro forests which
use typeof() somewhere deep enough. It is cool regardless.
Use "auto" in your code today!
gcc 5.1 supports __auto_type.
Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com>
---
Makefile | 1 +
1 file changed, 1 insertion(+)
Comments
On Tue, 16 May 2023 19:49:46 +0300 Alexey Dobriyan <adobriyan@gmail.com> wrote: > It has similar semantics to "auto" keyword from a language > which can not be named on this mailing list, in particular: Huh, 20 years ago C's auto meant "automatic variable". ie, stored on the stack. It was never needed because that's the default, unless you used `register', which gave the false impression that you're being faster. > { > int a; > const auto b = a; // const char b = a; I assume that should be "const int b = a". > b = 1; // compile error > } > { > char a; > auto b = a; // char b = a; > // no integer promotions > static_assert(sizeof(b) == 1); > } > { > int a; > const auto p = &a; // int *const p = &a; > *p = 1; // works because const is applied only to top-level > } > > It can be used to save on macroexpansion inside macro forests which > use typeof() somewhere deep enough. It is cool regardless. > > Use "auto" in your code today! > > gcc 5.1 supports __auto_type. > > Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com> > --- > Makefile | 1 + > 1 file changed, 1 insertion(+) > > diff --git a/Makefile b/Makefile > index 10fcc64fcd1f..d316924a466a 100644 > --- a/Makefile > +++ b/Makefile > @@ -570,6 +570,7 @@ KBUILD_CFLAGS += -Werror=return-type > KBUILD_CFLAGS += -Werror=strict-prototypes > KBUILD_CFLAGS += -Wno-format-security > KBUILD_CFLAGS += -Wno-trigraphs > +KBUILD_CFLAGS += -Dauto=__auto_type > > KBUILD_CPPFLAGS := -D__KERNEL__ > KBUILD_RUSTFLAGS := $(rust_common_flags) \ Examples at https://lkml.kernel.org/r/20230516164947.86543-3-adobriyan@gmail.com It is pretty cool and could get used a lot. Cc Linus for his thoughts?
On Tue, May 16, 2023 at 2:39 PM Andrew Morton <akpm@linux-foundation.org> wrote: > > It is pretty cool and could get used a lot. Cc Linus for his thoughts? I'm not against it, although I'm also not convinced we need / want to convert existing users of typeof(). The reason we use typeof is that that has always worked in gcc, and __auto_type is relatively "new" in contrast. But we require at least gcc-5.1 anyway, so it should be fine. Note that mindless conversions can be dangerous: using "typeof(x)" in macros may end up feeling a bit verbose, and "auto" can appear nicer, but the auto use needs to be *very* careful about integer promotions. For example, in #define WRAPPER(c) do { \ typeof(c) __c = (c); ... it is very obvious what the type is. But while using #define WRAPPER(c) do { \ auto __c = (c); gives you the same result with less redundancy (no need to state 'c' twice), if you *ever* then happen to make that an integer expression that is not *just* 'c' - even a trivial one - suddenly 'var' goes from 'char' to 'int' because of the integer expression. So __auto_type (and I agree that if we use it, we should probably just wrap it in an 'auto' #define, since the legacy 'auto' keyword is useless) can result in simpler and more obvious code, but it can also lead to subtle type issues that are easy to then overlook. The above is not an argument against 'auto', but it's one reason I'm not convinced some mindless "convert existing uses of __typeof__" is a good idea even if it might make some of them more legible. But I have nothing against people starting to use it in new code. And no, I don't think we should do that KBUILD_CFLAGS += -Dauto=__auto_type in the Makefile as Alexey suggests. I think this is a 'compiler_types.h' kind of thing, and goes along with all the other "simplied syntax" things we do (ie we redefine 'inline', we add "__weak" etc etc etc). Linus
On Tue, May 16, 2023 at 04:18:59PM -0700, Linus Torvalds wrote: > On Tue, May 16, 2023 at 2:39 PM Andrew Morton <akpm@linux-foundation.org> wrote: > > > > It is pretty cool and could get used a lot. Cc Linus for his thoughts? > > I'm not against it, although I'm also not convinced we need / want to > convert existing users of typeof(). > > The reason we use typeof is that that has always worked in gcc, and > __auto_type is relatively "new" in contrast. > > But we require at least gcc-5.1 anyway, so it should be fine. > > Note that mindless conversions can be dangerous: using "typeof(x)" in > macros may end up feeling a bit verbose, and "auto" can appear nicer, > but the auto use needs to be *very* careful about integer promotions. > > For example, in > > #define WRAPPER(c) do { \ > typeof(c) __c = (c); > ... > > it is very obvious what the type is. > > But while using > > #define WRAPPER(c) do { \ > auto __c = (c); > > gives you the same result with less redundancy (no need to state 'c' > twice), if you *ever* then happen to make that an integer expression > that is not *just* 'c' - even a trivial one - suddenly 'var' goes from > 'char' to 'int' because of the integer expression. > > So __auto_type (and I agree that if we use it, we should probably just > wrap it in an 'auto' #define, since the legacy 'auto' keyword is > useless) can result in simpler and more obvious code, but it can also > lead to subtle type issues that are easy to then overlook. > > The above is not an argument against 'auto', but it's one reason I'm > not convinced some mindless "convert existing uses of __typeof__" is a > good idea even if it might make some of them more legible. > > But I have nothing against people starting to use it in new code. > > And no, I don't think we should do that > > KBUILD_CFLAGS += -Dauto=__auto_type > > in the Makefile as Alexey suggests. > > I think this is a 'compiler_types.h' kind of thing, and goes along > with all the other "simplied syntax" things we do (ie we redefine > 'inline', we add "__weak" etc etc etc). Sure. I thought about where to put it in the filesystem under nice name but somehow completely forgot about compiler_types.h I'll probably go through core headers at least and start conversion myself. Expect version 2 soon.
diff --git a/Makefile b/Makefile index 10fcc64fcd1f..d316924a466a 100644 --- a/Makefile +++ b/Makefile @@ -570,6 +570,7 @@ KBUILD_CFLAGS += -Werror=return-type KBUILD_CFLAGS += -Werror=strict-prototypes KBUILD_CFLAGS += -Wno-format-security KBUILD_CFLAGS += -Wno-trigraphs +KBUILD_CFLAGS += -Dauto=__auto_type KBUILD_CPPFLAGS := -D__KERNEL__ KBUILD_RUSTFLAGS := $(rust_common_flags) \