From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 15382 invoked by alias); 25 Sep 2013 07:09:35 -0000 Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org Received: (qmail 15368 invoked by uid 89); 25 Sep 2013 07:09:34 -0000 Received: from mail-oa0-f44.google.com (HELO mail-oa0-f44.google.com) (209.85.219.44) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-SHA encrypted) ESMTPS; Wed, 25 Sep 2013 07:09:34 +0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-0.9 required=5.0 tests=AWL,BAYES_00,FREEMAIL_FROM,NO_RELAYS autolearn=ham version=3.3.2 X-HELO: mail-oa0-f44.google.com Received: by mail-oa0-f44.google.com with SMTP id l10so578418oag.17 for ; Wed, 25 Sep 2013 00:09:32 -0700 (PDT) MIME-Version: 1.0 X-Received: by 10.60.142.8 with SMTP id rs8mr11931409oeb.34.1380092972268; Wed, 25 Sep 2013 00:09:32 -0700 (PDT) Received: by 10.182.31.18 with HTTP; Wed, 25 Sep 2013 00:09:32 -0700 (PDT) Date: Wed, 25 Sep 2013 07:09:00 -0000 Message-ID: Subject: Making gcc warn about dead code pertaining to out of scope variable. From: vijay nag To: "gcc-help@gcc.gnu.org" Content-Type: text/plain; charset=ISO-8859-1 X-IsSubscribed: yes X-SW-Source: 2013-09/txt/msg00170.txt.bz2 Hello, I recently upgraded to GCC-4.7.2 and it has unabashedly exposed various bugs in the code base which the old gcc compiler simply couldn't. I'm little concerned about breakage/regressions caused by subtle GCC optimizations. Let me describe the problem that I have been observing. I have pasted the code snippet at the end of this mail so that it can be easily reproduced. The below macro declares/defines two local block variables local_addr and remote_addr. At higher optimization level i.e. at -O2, gcc seems to be eliminating the call to memset(dead-code) for it knows for sure that __remote_addr has reached the end of its scope, however at optimization level -O0 I do see a call to memcpy. The code below is surely buggy, however I was wondering if there is any option to make GCC emit warnings on encountering such code. That way it will help us identify and fix several such incantations in our large code base. #define FILL_TRANSPORT_INFO(_params) \ do { \ _ip_addr_t _local_addr; \ _ip_addr_t _remote_addr; \ _ZERO_VARIABLE(_local_addr); \ _ZERO_VARIABLE(_remote_addr); \ (_local_addr).ip_ver = SNX_IPV4_VERSION; \ (_local_addr)._ip_addr_t_u.ipv4 = 283725;\ memcpy(&(_remote_addr), &((_params)->addr), sizeof(_ip_addr_t));\ (_params)->local_addr = &_local_addr;\ (_params)->remote_addr = &_remote_addr;\ } while(0); #include #include typedef unsigned int u_int; #define _MAX_IP_LEN 50 #define _IPV4_VERSION 4 #define _IPV6_VERSION 6 #define _IPV4V6_VERSION 10 #define _IPV6_BYTES 16 typedef struct { int ip_ver; union { u_int ipv4; u_int ipv6[4]; } _ip_addr_t_u; }_ip_addr_t; #define SN_ZERO_VARIABLE(var) \ memset(&(var), 0, sizeof(var)); typedef struct sn_ip_params { _ip_addr_t addr; _ip_addr_t *local_addr; _ip_addr_t *remote_addr; } _ip_params; #define FILL_TRANSPORT_INFO(_params) \ do { \ _ip_addr_t _local_addr; \ _ip_addr_t _remote_addr; \ _ZERO_VARIABLE(_local_addr); \ _ZERO_VARIABLE(_remote_addr); \ (_local_addr).ip_ver = _IPV4_VERSION; \ (_local_addr)._ip_addr_t_u.ipv4 = 283725;\ memcpy(&(_remote_addr), &((_params)->addr), sizeof(_ip_addr_t));\ (_params)->local_addr = &_local_addr;\ (_params)->remote_addr = &_remote_addr;\ } while(0); int main() { _ip_params params; params.addr.ip_ver = _IPV4_VERSION; params.addr._ip_addr_t_u.ipv4 = 283725; FILL_TRANSPORT_INFO(¶ms); if (_IPV4_VERSION != params.local_addr->ip_ver) { fprintf(stderr, "Initialization failed\n"); } else { fprintf(stderr, "IPV4 value = %d\n", params.local_addr->_ip_addr_t_u.ipv4); } return 0; } Binary at -O2 : i686-pc-linux-gnu -g -O2 gcc_test.c -std=c99 -pedantic -Wall -Werror -o gcc_test ./gcc_test Initialization failed Binary at -O0: i686-pc-linux-gnu -g -OO gcc_test.c -std=c99 -pedantic -Wall -Werror -o gcc_test ./gcc_test IPV4 value = 283725