From patchwork Mon Dec 1 16:39:41 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?TWFudWVsIEzDs3Blei1JYsOhw7Fleg==?= X-Patchwork-Id: 416509 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 D159D140168 for ; Tue, 2 Dec 2014 03:40:14 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :mime-version:from:date:message-id:subject:to:content-type; q= dns; s=default; b=nHChRADkYtwQS6I2jqV/arxV7bmxnV1nAUATvxRAtt1LHM nSu5nzSf4ZhpL0LuilKmcdZJ5OuOAOI9rzM5MpY24XROAuno6lFvALpzN0i5tUJg 69zRpdQpEzdYt1/ayKcq5lbm7IXzIJ6xdoV+SLzuD3bfAEKUugOLPf00sA3+g= 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 :mime-version:from:date:message-id:subject:to:content-type; s= default; bh=AIjFChbqAyU8G/uJtDnRpISEFnk=; b=LssT47Tvi7oc+b4G6FQS Z3Tq+yiAH07pwY0kFkkRH/NuvncgJV5lLPYCxV410cWk0GyW0Q4y+p8PXxDZQICF i/+DR3qQe2hEoVhl5kInfyg5eNM7hfUeDAibQQ+tsuDcL4l4J3qtL/pJXQrngPTQ xDPfM7AHkaadcENXmDcDA5s= Received: (qmail 12916 invoked by alias); 1 Dec 2014 16:40:07 -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 12905 invoked by uid 89); 1 Dec 2014 16:40:06 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.3 required=5.0 tests=AWL, BAYES_00, FREEMAIL_FROM, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham version=3.3.2 X-HELO: mail-wg0-f47.google.com Received: from mail-wg0-f47.google.com (HELO mail-wg0-f47.google.com) (74.125.82.47) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-SHA encrypted) ESMTPS; Mon, 01 Dec 2014 16:40:04 +0000 Received: by mail-wg0-f47.google.com with SMTP id n12so14707252wgh.20 for ; Mon, 01 Dec 2014 08:40:01 -0800 (PST) X-Received: by 10.180.107.193 with SMTP id he1mr83876372wib.27.1417452001752; Mon, 01 Dec 2014 08:40:01 -0800 (PST) MIME-Version: 1.0 Received: by 10.217.141.72 with HTTP; Mon, 1 Dec 2014 08:39:41 -0800 (PST) From: =?ISO-8859-1?Q?Manuel_L=F3pez=2DIb=E1=F1ez?= Date: Mon, 1 Dec 2014 17:39:41 +0100 Message-ID: Subject: [PATCH linemap] Make some asserts fail gracefully To: Gcc Patch List , Dodji Seketeli , "Joseph S. Myers" The following patch adds linemap_assert_fails(), which is an assert meant to be used in conditions such as: if (linemap_assert_fails(EXPR)) handle_error(); This is useful for ICEs that we would like to detect during the development phase but that could be handled gracefully. In the case of linemap_position_for_loc_and_offset (its only user so far), there are a few conditions that show something went wrong, but it would be bad to ICE on users when we can simply return the original location without column offset, which is only a minor diagnostic output degradation. Bootstrapped & regression tested. I'm happy to use a different name. I tried linemap_soft_assert, but it doesn't read as nice together with "if". The alternative is to just not use assert but a simple 'if'. We could resort to that if some condition triggers too often or we cannot find an immediate fix. For now, one has to use special Fortran testcases to trigger this, thus I would like to fix the testcases rather than disable the asserts completely. OK? libcpp/ChangeLog: 2014-12-01 Manuel López-Ibáñez * include/line-map.h (linemap_assert_fails): Declare. * line-map.c (linemap_position_for_loc_and_offset): Use it. Index: libcpp/line-map.c =================================================================== --- libcpp/line-map.c (revision 218192) +++ libcpp/line-map.c (working copy) @@ -643,11 +643,13 @@ linemap_position_for_loc_and_offset (str unsigned int offset) { const struct line_map * map = NULL; /* This function does not support virtual locations yet. */ - linemap_assert (!linemap_location_from_macro_expansion_p (set, loc)); + if (linemap_assert_fails + (!linemap_location_from_macro_expansion_p (set, loc))) + return loc; if (offset == 0 /* Adding an offset to a reserved location (like UNKNOWN_LOCATION for the C/C++ FEs) does not really make sense. So let's leave the location intact in that case. */ @@ -656,26 +658,31 @@ linemap_position_for_loc_and_offset (str /* We find the real location and shift it. */ loc = linemap_resolve_location (set, loc, LRK_SPELLING_LOCATION, &map); /* The new location (loc + offset) should be higher than the first location encoded by MAP. */ - linemap_assert (MAP_START_LOCATION (map) < loc + offset); + if (linemap_assert_fails (MAP_START_LOCATION (map) < loc + offset)) + return loc; /* If MAP is not the last line map of its set, then the new location (loc + offset) should be less than the first location encoded by the next line map of the set. */ if (map != LINEMAPS_LAST_ORDINARY_MAP (set)) - linemap_assert (loc + offset < MAP_START_LOCATION (&map[1])); + if (linemap_assert_fails (loc + offset < MAP_START_LOCATION (&map[1]))) + return loc; offset += SOURCE_COLUMN (map, loc); - linemap_assert (offset < (1u << map->d.ordinary.column_bits)); + if (linemap_assert_fails (offset < (1u << map->d.ordinary.column_bits))) + return loc; source_location r = linemap_position_for_line_and_column (map, SOURCE_LINE (map, loc), offset); - linemap_assert (map == linemap_lookup (set, r)); + if (linemap_assert_fails (map == linemap_lookup (set, r))) + return loc; + return r; } /* Given a virtual source location yielded by a map (either an ordinary or a macro map), returns that map. */ Index: libcpp/include/line-map.h =================================================================== --- libcpp/include/line-map.h (revision 218192) +++ libcpp/include/line-map.h (working copy) @@ -578,18 +578,27 @@ bool linemap_location_from_macro_expansi do { \ if (! (EXPR)) \ abort (); \ } while (0) +/* Assert that becomes a conditional expression when not checking. + Use this for conditions that should not happen but if they happen, it + is better to handle them gracefully than ICE. Usage: + + if (linemap_assert_fails(EXPR)) handle_error(); */ +#define linemap_assert_fails(EXPR) __extension__ \ + ({linemap_assert (EXPR); false;}) + /* Assert that MAP encodes locations of tokens that are not part of the replacement-list of a macro expansion. */ #define linemap_check_ordinary(LINE_MAP) __extension__ \ ({linemap_assert (!linemap_macro_expansion_map_p (LINE_MAP)); \ (LINE_MAP);}) #else /* Include EXPR, so that unused variable warnings do not occur. */ #define linemap_assert(EXPR) ((void)(0 && (EXPR))) +#define linemap_assert_fails(EXPR) (! (EXPR)) #define linemap_check_ordinary(LINE_MAP) (LINE_MAP) #endif /* Encode and return a source_location from a column number. The source line considered is the last source line used to call