From patchwork Wed Jun 4 06:46:52 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 355726 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id AEC7014007E for ; Wed, 4 Jun 2014 16:47:05 +1000 (EST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type; q=dns; s= default; b=rh2yIAX6sRBJFwqN36Pzgz7uHjUFr5Z9PPyIXyO0wsGHiYvWo0/YJ 3eJmEstvj1RPHjBeV2gDaNNg5PrNKT3i5BJXTgEaj9SSXPO3KZgR+418qKcD8M1H Ix3I7xX85kHC2CLt28xAaEtVISUJYNj/GIDfz9rsiV6eJeoH1sywJs= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type; s= default; bh=2W6pxLugaGeN6SkVNuU/FakoEOw=; b=JfBLyx/MJ8IMaLtq3BOp eqDNDDJqnHP9psua1QHTtj+xy7c1DL7xZNpvD/jiiXAkq5U2MlCeoZ84SoL2yS11 mN5hsb9rIxMjM1+fjQUUkuCj/J4A6lWLK+VlrYheXB7NBr+wxI1cfixHNzbvWnB1 R2kKuQx+h81fbwk/sVlQYkw= Received: (qmail 6892 invoked by alias); 4 Jun 2014 06:46:58 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 6883 invoked by uid 89); 4 Jun 2014 06:46:58 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-3.1 required=5.0 tests=AWL, BAYES_00, RP_MATCHES_RCVD, SPF_HELO_PASS, SPF_PASS autolearn=ham version=3.3.2 X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 04 Jun 2014 06:46:56 +0000 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id s546ktke015025 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Wed, 4 Jun 2014 02:46:55 -0400 Received: from redhat.com (ovpn-116-106.ams2.redhat.com [10.36.116.106]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id s546kqhC031528 (version=TLSv1/SSLv3 cipher=AES128-GCM-SHA256 bits=128 verify=NO) for ; Wed, 4 Jun 2014 02:46:54 -0400 Date: Wed, 4 Jun 2014 08:46:52 +0200 From: Marek Polacek To: GCC Patches Subject: [C PATCH] Better location for switch warnings (PR c/30020) Message-ID: <20140604064652.GG29196@redhat.com> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.23 (2014-03-12) It is trivial to pass the location from c_add_case_label down to check_case_bounds, so do that. With it, we instead of i.c:4:3: warning: case label value is less than minimum value for type switch (c) { case 42: case -1: return -1; }; ^ output i.c:4:25: warning: case label value is less than minimum value for type switch (c) { case 42: case -1: return -1; }; ^ which is better. Tested x86_64, ok for trunk? 2014-06-04 Marek Polacek PR c/30020 * c-common.c (check_case_bounds): Add location parameter. Use it. (c_add_case_label): Pass loc to check_case_bounds. * c-c++-common/pr30020.c: New test. Marek diff --git gcc/c-family/c-common.c gcc/c-family/c-common.c index 6ec14fc..07a1798 100644 --- gcc/c-family/c-common.c +++ gcc/c-family/c-common.c @@ -301,7 +301,7 @@ struct visibility_flags visibility_options; static tree c_fully_fold_internal (tree expr, bool, bool *, bool *); static tree check_case_value (tree); -static bool check_case_bounds (tree, tree, tree *, tree *); +static bool check_case_bounds (location_t, tree, tree, tree *, tree *); static tree handle_packed_attribute (tree *, tree, tree, int, bool *); static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *); @@ -3355,7 +3355,7 @@ check_case_value (tree value) untouched) or false if the label is out of range. */ static bool -check_case_bounds (tree type, tree orig_type, +check_case_bounds (location_t loc, tree type, tree orig_type, tree *case_low_p, tree *case_high_p) { tree min_value, max_value; @@ -3373,7 +3373,8 @@ check_case_bounds (tree type, tree orig_type, if (tree_int_cst_compare (case_low, min_value) < 0 && tree_int_cst_compare (case_high, min_value) < 0) { - warning (0, "case label value is less than minimum value for type"); + warning_at (loc, 0, "case label value is less than minimum value " + "for type"); return false; } @@ -3381,7 +3382,7 @@ check_case_bounds (tree type, tree orig_type, if (tree_int_cst_compare (case_low, max_value) > 0 && tree_int_cst_compare (case_high, max_value) > 0) { - warning (0, "case label value exceeds maximum value for type"); + warning_at (loc, 0, "case label value exceeds maximum value for type"); return false; } @@ -3389,8 +3390,8 @@ check_case_bounds (tree type, tree orig_type, if (tree_int_cst_compare (case_high, min_value) >= 0 && tree_int_cst_compare (case_low, min_value) < 0) { - warning (0, "lower value in case label range" - " less than minimum value for type"); + warning_at (loc, 0, "lower value in case label range" + " less than minimum value for type"); case_low = min_value; } @@ -3398,8 +3399,8 @@ check_case_bounds (tree type, tree orig_type, if (tree_int_cst_compare (case_low, max_value) <= 0 && tree_int_cst_compare (case_high, max_value) > 0) { - warning (0, "upper value in case label range" - " exceeds maximum value for type"); + warning_at (loc, 0, "upper value in case label range" + " exceeds maximum value for type"); case_high = max_value; } @@ -6014,7 +6015,7 @@ c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type, expression. If both low_value and high_value are out of range, don't insert the case label and return NULL_TREE. */ if (low_value - && !check_case_bounds (type, orig_type, + && !check_case_bounds (loc, type, orig_type, &low_value, high_value ? &high_value : NULL)) return NULL_TREE; diff --git gcc/testsuite/c-c++-common/pr30020.c gcc/testsuite/c-c++-common/pr30020.c index e69de29..b8082cd 100644 --- gcc/testsuite/c-c++-common/pr30020.c +++ gcc/testsuite/c-c++-common/pr30020.c @@ -0,0 +1,12 @@ +/* PR c/30020 */ +/* { dg-do compile } */ + +int +foo (unsigned char c) +{ + switch (c) { case 42: case -1: return -1; }; /* { dg-warning "25:case label value" } */ + switch (c) { case 42: case 300: return -1; }; /* { dg-warning "25:case label value" } */ + switch (c) { case 42: case -1 ... 2: return -1; }; /* { dg-warning "25:lower value in case" } */ + switch (c) { case 42: case 250 ... 300: return -1; }; /* { dg-warning "25:upper value in case" } */ + return 0; +}