From patchwork Wed Jan 5 14:11:20 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 77607 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]) by ozlabs.org (Postfix) with SMTP id 1470FB7063 for ; Thu, 6 Jan 2011 01:11:32 +1100 (EST) Received: (qmail 28434 invoked by alias); 5 Jan 2011 14:11:31 -0000 Received: (qmail 28423 invoked by uid 22791); 5 Jan 2011 14:11:29 -0000 X-SWARE-Spam-Status: No, hits=-6.9 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_HI, SPF_HELO_PASS, T_RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Wed, 05 Jan 2011 14:11:23 +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.13.8/8.13.8) with ESMTP id p05EBLab020055 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Wed, 5 Jan 2011 09:11:22 -0500 Received: from ns3.rdu.redhat.com (ns3.rdu.redhat.com [10.11.255.199]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id p05EBL9W027363; Wed, 5 Jan 2011 09:11:21 -0500 Received: from opsy.redhat.com (ovpn01.gateway.prod.ext.phx2.redhat.com [10.5.9.1]) by ns3.rdu.redhat.com (8.13.8/8.13.8) with ESMTP id p05EBK7Y017975; Wed, 5 Jan 2011 09:11:20 -0500 Received: by opsy.redhat.com (Postfix, from userid 500) id 2C10037848D; Wed, 5 Jan 2011 07:11:20 -0700 (MST) From: Tom Tromey To: gcc-patches@gcc.gnu.org Subject: Patch: fix location in lvalue error Date: Wed, 05 Jan 2011 07:11:20 -0700 Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2 (gnu/linux) MIME-Version: 1.0 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 While playing with a refactoring hack, I noticed that locations in lvalue errors are sometimes incorrect. E.g., consider this file: int q(int x) { (x + 0) = 5; (x + 0) = 7; } One error is reported against the '=', but the other is not: opsy. gcc --syntax-only q.c q.c: In function ‘q’: q.c:3:3: error: lvalue required as left operand of assignment q.c:5:5: error: lvalue required as left operand of assignment I think it makes sense to report the error using the '=' token. That is what this patch does. The new output is: opsy. gcc --syntax-only q.c q.c: In function ‘q’: q.c:3:11: error: lvalue required as left operand of assignment q.c:5:5: error: lvalue required as left operand of assignment I did not look at what the C++ compiler emits here. The patch does not make it any worse, though. Built and regtested on x86-64 (compile farm). Ok? Tom 2011-01-05 Tom Tromey * c-parser.c (c_parser_omp_atomic): Pass location of assignment operator to c_finish_omp_atomic. * c-typeck.c (lvalue_or_else): Add 'loc' argument. (build_unary_op): Update. (build_modify_expr): Update. (build_asm_expr): Update. 2011-01-05 Tom Tromey * typeck.c (cp_build_addr_expr_1): Update call to lvalue_error. (lvalue_or_else): Likewise. 2011-01-05 Tom Tromey * c-common.h (lvalue_error): Update. * c-common.c (lvalue_error): Add 'loc' argument. Call error_at, not error. Index: c-parser.c =================================================================== --- c-parser.c (revision 168451) +++ c-parser.c (working copy) @@ -1,6 +1,6 @@ /* Parser for C and Objective-C. Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998, - 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010 + 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc. Parser actions based on the old Bison parser; structure somewhat @@ -9104,6 +9104,9 @@ goto saw_error; } + /* Arrange to pass the location of the assignment operator to + c_finish_omp_atomic. */ + loc = c_parser_peek_token (parser)->location; c_parser_consume_token (parser); { location_t rhs_loc = c_parser_peek_token (parser)->location; Index: c-typeck.c =================================================================== --- c-typeck.c (revision 168451) +++ c-typeck.c (working copy) @@ -1,6 +1,6 @@ /* Build expressions with type checking for C compiler. Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, - 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 + 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc. This file is part of GCC. @@ -98,7 +98,7 @@ static void set_nonincremental_init_from_string (tree, struct obstack *); static tree find_init_member (tree, struct obstack *); static void readonly_warning (tree, enum lvalue_use); -static int lvalue_or_else (const_tree, enum lvalue_use); +static int lvalue_or_else (location_t, const_tree, enum lvalue_use); static void record_maybe_used_decl (tree); static int comptypes_internal (const_tree, const_tree, bool *, bool *); @@ -3564,7 +3564,8 @@ /* Complain about anything that is not a true lvalue. In Objective-C, skip this check for property_refs. */ if (!objc_is_property_ref (arg) - && !lvalue_or_else (arg, ((code == PREINCREMENT_EXPR + && !lvalue_or_else (location, + arg, ((code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR) ? lv_increment : lv_decrement))) @@ -3747,7 +3748,7 @@ /* Anything not already handled and not a true memory reference or a non-lvalue array is an error. */ else if (typecode != FUNCTION_TYPE && !flag - && !lvalue_or_else (arg, lv_addressof)) + && !lvalue_or_else (location, arg, lv_addressof)) return error_mark_node; /* Move address operations inside C_MAYBE_CONST_EXPR to simplify @@ -3905,15 +3906,16 @@ /* Return nonzero if REF is an lvalue valid for this language; otherwise, print an error message and return zero. USE says - how the lvalue is being used and so selects the error message. */ + how the lvalue is being used and so selects the error message. + LOCATION is the location at which any error should be reported. */ static int -lvalue_or_else (const_tree ref, enum lvalue_use use) +lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use) { int win = lvalue_p (ref); if (!win) - lvalue_error (use); + lvalue_error (loc, use); return win; } @@ -4801,7 +4803,7 @@ return error_mark_node; /* For ObjC properties, defer this check. */ - if (!objc_is_property_ref (lhs) && !lvalue_or_else (lhs, lv_assign)) + if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign)) return error_mark_node; if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR) @@ -4851,7 +4853,7 @@ return result; /* Else, do the check that we postponed for Objective-C. */ - if (!lvalue_or_else (lhs, lv_assign)) + if (!lvalue_or_else (location, lhs, lv_assign)) return error_mark_node; } @@ -8479,7 +8481,7 @@ get an error. Gross, but ... */ STRIP_NOPS (output); - if (!lvalue_or_else (output, lv_asm)) + if (!lvalue_or_else (loc, output, lv_asm)) output = error_mark_node; if (output != error_mark_node Index: cp/typeck.c =================================================================== --- cp/typeck.c (revision 168451) +++ cp/typeck.c (working copy) @@ -1,6 +1,6 @@ /* Build expressions with type checking for C++ compiler. Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998, - 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 + 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc. Hacked by Michael Tiemann (tiemann@cygnus.com) @@ -4756,7 +4756,7 @@ if (kind == clk_none) { if (complain & tf_error) - lvalue_error (lv_addressof); + lvalue_error (input_location, lv_addressof); return error_mark_node; } if (strict_lvalue && (kind & (clk_rvalueref|clk_class))) @@ -8219,7 +8219,7 @@ if (kind == clk_none) { if (complain & tf_error) - lvalue_error (use); + lvalue_error (input_location, use); return 0; } else if (kind & (clk_rvalueref|clk_class)) Index: c-family/c-common.c =================================================================== --- c-family/c-common.c (revision 168451) +++ c-family/c-common.c (working copy) @@ -1,6 +1,6 @@ /* Subroutines shared by all languages that are variants of C. Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, - 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 + 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc. This file is part of GCC. @@ -8631,27 +8631,28 @@ } /* Print an error message for an invalid lvalue. USE says - how the lvalue is being used and so selects the error message. */ + how the lvalue is being used and so selects the error message. LOC + is the location for the error. */ void -lvalue_error (enum lvalue_use use) +lvalue_error (location_t loc, enum lvalue_use use) { switch (use) { case lv_assign: - error ("lvalue required as left operand of assignment"); + error_at (loc, "lvalue required as left operand of assignment"); break; case lv_increment: - error ("lvalue required as increment operand"); + error_at (loc, "lvalue required as increment operand"); break; case lv_decrement: - error ("lvalue required as decrement operand"); + error_at (loc, "lvalue required as decrement operand"); break; case lv_addressof: - error ("lvalue required as unary %<&%> operand"); + error_at (loc, "lvalue required as unary %<&%> operand"); break; case lv_asm: - error ("lvalue required in asm statement"); + error_at (loc, "lvalue required in asm statement"); break; default: gcc_unreachable (); Index: c-family/c-common.h =================================================================== --- c-family/c-common.h (revision 168451) +++ c-family/c-common.h (working copy) @@ -1,6 +1,6 @@ /* Definitions for c-common.c. Copyright (C) 1987, 1993, 1994, 1995, 1997, 1998, - 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010 + 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc. This file is part of GCC. @@ -929,7 +929,7 @@ }; extern void readonly_error (tree, enum lvalue_use); -extern void lvalue_error (enum lvalue_use); +extern void lvalue_error (location_t, enum lvalue_use); extern void invalid_indirection_error (location_t, tree, ref_operator); extern int complete_array_type (tree *, tree, bool);