Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp3953667ybb; Tue, 31 Mar 2020 15:38:10 -0700 (PDT) X-Google-Smtp-Source: APiQypK0TtO9HpyUGdFb/YJZhhGig8Eee9PP6nS/Vo0iFfEqL1X7zrZIKMJ+eHVbZrcVbVB9JG7i X-Received: by 2002:aca:ad88:: with SMTP id w130mr890994oie.82.1585694290416; Tue, 31 Mar 2020 15:38:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1585694290; cv=none; d=google.com; s=arc-20160816; b=S9tsYfNMkQKToXmdzWa61ayoVJQWzgegkB3fa/SHUIafPOaLUEO7VRTLXBs0GInjUp FnO7eWX03CgSRCfyf8FZczOkLRLHZrrsnR9FkkFWwPMj72c1EV5dfpACUqJhMw6tXHVF fkG59XN/vcipC4UaLiiPEdFP+UWEta/BKkgxbxMCo/YR9RzQtHbQErsFpjkBIHuSvwLH PUKpohNkrVse1iPptg+alUFSGunWPrb7tbVHmOi61DRqarKM5EfyokMFCFL+cuqFIChM TNl81A08awY0q3eocclXnJUiDjcs731x6efxO97SEVTKB+81ytEadgPWtOnT7zLF58yb eEHQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-transfer-encoding:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=PCkuNYX5Y1DiFeU+I1dLaQDU7lZ3r1TK5BjT1r/iMHA=; b=JygDLCFvtELoMbqNpnzgDx2J0BHygOhLkxKwgi3j2baGim8a2FqrKAGU1F6do3Tpbn oZ2DYCYv6ojMB1JveiWQqduUwix7/LszQGXOUMQyG+Q7B7AP+EH1/7MxQGQ+ZGVHCtj3 FQyncv90a2QbKKkUgJPTYmNJseyoqHaCz/9lKSaLuMCVqOGRpJiYVwxXSyN9KKsVcEqY 8L9uXi+C/WyLNKqo49/tf02afmtBFS4+5NkbEeTz9gkUzMA5Ybj3CU8EmTeMJWsdyUFI syAoBnX5W0eBjahP6h/ZGIeG6DPE21p/oQ/zIUiyKS1iGrYCC7sS3yYQn4b26gCq5zhm muSg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=BFGazVTm; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d21si8084511oof.2.2020.03.31.15.37.57; Tue, 31 Mar 2020 15:38:10 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=BFGazVTm; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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 S1730556AbgCaWgl (ORCPT + 99 others); Tue, 31 Mar 2020 18:36:41 -0400 Received: from mail-oi1-f195.google.com ([209.85.167.195]:36155 "EHLO mail-oi1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727955AbgCaWgl (ORCPT ); Tue, 31 Mar 2020 18:36:41 -0400 Received: by mail-oi1-f195.google.com with SMTP id k18so20492061oib.3 for ; Tue, 31 Mar 2020 15:36:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:content-transfer-encoding:in-reply-to :user-agent; bh=PCkuNYX5Y1DiFeU+I1dLaQDU7lZ3r1TK5BjT1r/iMHA=; b=BFGazVTmhE2AtUwT4yvqeN2PkTKCLR16uj8T19NoHr9d3oTOvsU7wd3A3Oz3u1C9Ix FsT53ZGHB/wvM0wwzr9e/+TUoJA3769FCKnzYAAZBYjxbwPNbiGkn0cysXs7g66si3WH AD3WJXMNbzwMiGkpPiaqKZQ7sM7iwFAPEuzUBwU59jxbPfjo0ocOjCFPyxNWFLtwTlrT ldwavlsc3T2IniWNkoL1K+XMc4EyJJeIdyYxxZvPgwzIY+3GKMccNN0D0NwBxrjGtxhl vZfTsObBeOcDO/dHIPZitDlhUYRXYgvWYJ9tATUIlojZbBOUsWMYrxBNWq7i0kVnloCj QNTQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:content-transfer-encoding :in-reply-to:user-agent; bh=PCkuNYX5Y1DiFeU+I1dLaQDU7lZ3r1TK5BjT1r/iMHA=; b=WwrUvjJ1AS8OzdX8HZaAFeOnjQOYj0hcC+90rr/DyM8I+A3KI8Z28rBwtAZi+r/78s H5BtjdDGDiPMlIqhVy+Cant/P6sBB5hbRmDpLr5uwaIu3aGK0zKewTq2x1QanRjW8pWY 3Whky4j8jqOX5gybacI39dwDaVHRcITnc6103jqYcTdqx8dLKTOiBWNx4ncDPKurOJHV 5erNvFywsPD+ymJt7+V3rYEuZRZK7sFbjgtupxZsjA8LUbfi8k/vJmrLd2SPESChBv7T SngSd+aIA3MofPKxmhglKtgYYItt/X1QDbxKcF49BCPaW6RXN61cdxGnaJCOnkx0umP0 xwkw== X-Gm-Message-State: AGi0PuaYgoslRvqtK8gqgutao4bSJbtLm5jlqKOkO7dWiOoWdtZgeGjA VlNc0aVBSnjnwqFtbcO9194= X-Received: by 2002:aca:d68a:: with SMTP id n132mr892459oig.40.1585694200626; Tue, 31 Mar 2020 15:36:40 -0700 (PDT) Received: from ubuntu-m2-xlarge-x86 ([2604:1380:4111:8b00::1]) by smtp.gmail.com with ESMTPSA id o1sm28285otl.49.2020.03.31.15.36.39 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 31 Mar 2020 15:36:40 -0700 (PDT) Date: Tue, 31 Mar 2020 15:36:38 -0700 From: Nathan Chancellor To: Daniel Santos Cc: Vegard Nossum , Andrew Morton , linux-kernel@vger.kernel.org, Rasmus Villemoes , Masahiro Yamada , Ian Abbott Subject: Re: [PATCH] compiler.h: fix error in BUILD_BUG_ON() reporting Message-ID: <20200331223638.GA53668@ubuntu-m2-xlarge-x86> References: <20200331112637.25047-1-vegard.nossum@oracle.com> <91730318-da0e-c992-f154-a74044b26650@pobox.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <91730318-da0e-c992-f154-a74044b26650@pobox.com> User-Agent: Mutt/1.9.4 (2018-02-28) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Mar 31, 2020 at 05:25:38PM -0500, Daniel Santos wrote: > On 3/31/20 6:26 AM, Vegard Nossum wrote: > > compiletime_assert() uses __LINE__ to create a unique function name. > > This means that if you have more than one BUILD_BUG_ON() in the same > > source line (which can happen if they appear e.g. in a macro), then > > the error message from the compiler might output the wrong condition. > > > > For this source file: > > > > #include > > > > #define macro() \ > > BUILD_BUG_ON(1); \ > > BUILD_BUG_ON(0); > > > > void foo() > > { > > macro(); > > } > > > > gcc would output: > > > > ./include/linux/compiler.h:350:38: error: call to ‘__compiletime_assert_9’ declared with attribute error: BUILD_BUG_ON failed: 0 > > _compiletime_assert(condition, msg, __compiletime_assert_, __LINE__) > > > > However, it was not the BUILD_BUG_ON(0) that failed, so it should say 1 > > instead of 0. With this patch, we use __COUNTER__ instead of __LINE__, so > > each BUILD_BUG_ON() gets a different function name and the correct > > condition is printed: > > > > ./include/linux/compiler.h:350:38: error: call to ‘__compiletime_assert_0’ declared with attribute error: BUILD_BUG_ON failed: 1 > > _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__) > > > > Cc: Daniel Santos > > Cc: Rasmus Villemoes > > Cc: Masahiro Yamada > > Cc: Ian Abbott > > Signed-off-by: Vegard Nossum > > --- > > include/linux/compiler.h | 2 +- > > 1 file changed, 1 insertion(+), 1 deletion(-) > > > > diff --git a/include/linux/compiler.h b/include/linux/compiler.h > > index 5e88e7e33abec..034b0a644efcc 100644 > > --- a/include/linux/compiler.h > > +++ b/include/linux/compiler.h > > @@ -347,7 +347,7 @@ static inline void *offset_to_ptr(const int *off) > > * compiler has support to do so. > > */ > > #define compiletime_assert(condition, msg) \ > > - _compiletime_assert(condition, msg, __compiletime_assert_, __LINE__) > > + _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__) > > > > #define compiletime_assert_atomic_type(t) \ > > compiletime_assert(__native_word(t), \ > > This will break builds using gcc 4.2 and earlier and the expectation was > that you don't put two of them on the same line -- not helpful in macros > where it all must be on the same line.  Is gcc 4.2 still supported?  If > so, I recommend using another macro for the unique number that uses > __COUNTER__ if available and __LINE__ otherwise.  This was the decision > for using __LINE__ when I wrote the original anyway. > > Also note that this construct: > > BUILD_BUG_ON_MSG(0, "I like chicken"); BUILD_BUG_ON_MSG(1, "I don't like > chicken"); > > will incorrectly claim that I like chicken.  This is because of how > __attribute__((error)) works -- gcc will use the first declaration to > define the error message. > > I couple of years ago, I almost wrote a gcc extension to get rid of this > whole mess and just __builtin_const_assert(cond, msg).  Maybe I'll > finish that this year. > > Daniel No, GCC 4.6 is the minimum required version and it is highly likely that the minimum version of GCC will be raised to 4.8 soon: https://lore.kernel.org/lkml/20200123153341.19947-10-will@kernel.org/ https://git.kernel.org/peterz/queue/c/0e75b883b400ac4b1dafafe3cbd2e0a39b714232 Cheers, Nathan