From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753830AbcIDPG3 (ORCPT ); Sun, 4 Sep 2016 11:06:29 -0400 Received: from smtprelay0209.hostedemail.com ([216.40.44.209]:56430 "EHLO smtprelay.hostedemail.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1753239AbcIDPG2 (ORCPT ); Sun, 4 Sep 2016 11:06:28 -0400 X-Session-Marker: 6A6F6540706572636865732E636F6D X-Spam-Summary: 50,0,0,,d41d8cd98f00b204,joe@perches.com,:::::,RULES_HIT:41:355:379:541:599:960:967:973:988:989:1260:1277:1311:1313:1314:1345:1359:1373:1437:1515:1516:1518:1534:1541:1593:1594:1711:1730:1747:1777:1792:2194:2199:2393:2525:2553:2560:2563:2682:2685:2828:2859:2933:2937:2939:2942:2945:2947:2951:2954:3022:3138:3139:3140:3141:3142:3353:3622:3653:3865:3867:3868:3870:3871:3872:3934:3936:3938:3941:3944:3947:3950:3953:3956:3959:4321:4362:5007:6691:7903:7904:8985:9025:10004:10400:10848:11232:11658:11783:11914:12043:12740:13069:13153:13161:13228:13229:13311:13357:13439:13894:14181:14659:14721:21080:21451:30012:30054:30090:30091,0,RBL:none,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fn,MSBL:0,DNSBL:none,Custom_rules:0:0:0,LFtime:1,LUA_SUMMARY:none X-HE-Tag: shade18_54c22f981ad61 X-Filterd-Recvd-Size: 2272 Message-ID: <1473001581.5018.37.camel@perches.com> Subject: Re: Possible code defects: macros and precedence From: Joe Perches To: Julia Lawall Cc: Dan Carpenter , LKML Date: Sun, 04 Sep 2016 08:06:21 -0700 In-Reply-To: References: <1472927739.5018.13.camel@perches.com> Content-Type: text/plain; charset="ISO-8859-1" X-Mailer: Evolution 3.18.5.2-0ubuntu3 Mime-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sun, 2016-09-04 at 18:10 +0800, Julia Lawall wrote: > On Sat, 3 Sep 2016, Joe Perches wrote: > > There are many nominally incorrect macro definitions > > in linux-kernel source where parentheses are not used > > for various macros arguments with calculations. > > > > Does coccinelle or smatch have the ability to detect > > potential macro misuse where arguments passed to the > > macro are not correctly parenthesized by the macro? > > > > Something like: > > > >       #define A 1 > >       #define B 2 > >       #define shift(val) (val << 1) > > > > where a use is: > > > >       int c = shift(A | B) > > > > where the actual result is 5 but the expected result is 6? > > > > Can either tool suggest changing the macro to > > > >       #define shift(val) ((val) << 1) > > Coccinelle could do this.  It is possible to match macro parameters, and > it is possible to match binary operators generically.  I can look into it. Thanks Julia. It is not just binary operators though, it is all operations including dereference where precedence and associativity operations on the macro argument might cause an unexpected result. The possible regex checkpatch rule I sent for this https://lkml.org/lkml/2016/9/3/271 is _way_ too noisy and stupid. The $Operator test there includes a comma which makes the possible macro argument precedence test output silly.  More work is necessary to make the checkpatch test more reasonable.