From patchwork Tue Jun 1 14:09:18 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Martin_Li=C5=A1ka?= X-Patchwork-Id: 1486001 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=suse.cz header.i=@suse.cz header.a=rsa-sha256 header.s=susede2_rsa header.b=FmJHwIPz; dkim=pass header.d=suse.cz header.i=@suse.cz header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=NgXkWdGS; dkim=pass (1024-bit key) header.d=suse.cz header.i=@suse.cz header.a=rsa-sha256 header.s=susede2_rsa header.b=FmJHwIPz; dkim=neutral header.d=suse.cz header.i=@suse.cz header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=NgXkWdGS; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4FvYv64P0wz9sVb for ; Wed, 2 Jun 2021 00:09:37 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3DB9A39CD03C for ; Tue, 1 Jun 2021 14:09:35 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by sourceware.org (Postfix) with ESMTPS id BB4B539B3451 for ; Tue, 1 Jun 2021 14:09:21 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org BB4B539B3451 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=suse.cz Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.cz Received: from imap.suse.de (imap-alt.suse-dmz.suse.de [192.168.254.47]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 8269621915 for ; Tue, 1 Jun 2021 14:09:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1622556560; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=7gL1/XzIl1kHFSdxvVGDnM+Y7dcXZvz0hWf0sxVurAI=; b=FmJHwIPzuaTSwOOvgD+BXjE6HwP4XabMLvSqBMWz5fxwhxo0h1PGRatnnLA/40/IIIakkI Jd2KQDz3thVlavdIXI+naXRgO1Ys/F//7QnuHjeBw6CNV1ccRNQus08glxw00iNBAmA44c L9zkRjYTzH/snw1Nxg9jcJBLo9LfFuU= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1622556560; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=7gL1/XzIl1kHFSdxvVGDnM+Y7dcXZvz0hWf0sxVurAI=; b=NgXkWdGS1AfoZJ5CYW0ua/gobtd8a3HccThRFgp07P16EhU9eYE4OvO9Wm0OZ3QADqia7n t8vql/z5QFVHSlDA== Received: from imap3-int (imap-alt.suse-dmz.suse.de [192.168.254.47]) by imap.suse.de (Postfix) with ESMTP id 47FE3118DD for ; Tue, 1 Jun 2021 14:09:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1622556560; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=7gL1/XzIl1kHFSdxvVGDnM+Y7dcXZvz0hWf0sxVurAI=; b=FmJHwIPzuaTSwOOvgD+BXjE6HwP4XabMLvSqBMWz5fxwhxo0h1PGRatnnLA/40/IIIakkI Jd2KQDz3thVlavdIXI+naXRgO1Ys/F//7QnuHjeBw6CNV1ccRNQus08glxw00iNBAmA44c L9zkRjYTzH/snw1Nxg9jcJBLo9LfFuU= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1622556560; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=7gL1/XzIl1kHFSdxvVGDnM+Y7dcXZvz0hWf0sxVurAI=; b=NgXkWdGS1AfoZJ5CYW0ua/gobtd8a3HccThRFgp07P16EhU9eYE4OvO9Wm0OZ3QADqia7n t8vql/z5QFVHSlDA== Received: from director2.suse.de ([192.168.254.72]) by imap3-int with ESMTPSA id YkWrEJA/tmCGHwAALh3uQQ (envelope-from ) for ; Tue, 01 Jun 2021 14:09:20 +0000 From: =?utf-8?q?Martin_Li=C5=A1ka?= Subject: [PATCH] Introduce -Wcoverage-invalid-line-number To: gcc-patches@gcc.gnu.org Message-ID: <22f1ed9b-d967-8e97-0e2a-4d069c0c2f26@suse.cz> Date: Tue, 1 Jun 2021 16:09:18 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.10.2 MIME-Version: 1.0 Content-Language: en-US X-Spam-Status: No, score=-11.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" Hello. As seen in the PR, one can easily corrupt line number information and we can end up with a function that ends before it starts ;) I'm adding a new warning for that instead of the ICE. Patch can bootstrap on x86_64-linux-gnu and survives regression tests. Ready to be installed? Thanks, Martin PR gcov-profile/100788 gcc/ChangeLog: * common.opt: Add new option. * coverage.c (coverage_begin_function): Emit warning instead on the internal compiler error. * doc/invoke.texi: Document the option. * toplev.c (process_options): Enable it by default. gcc/testsuite/ChangeLog: * gcc.dg/pr100788.c: New test. --- gcc/common.opt | 4 ++++ gcc/coverage.c | 31 ++++++++++++++++++++----------- gcc/doc/invoke.texi | 11 +++++++++++ gcc/testsuite/gcc.dg/pr100788.c | 13 +++++++++++++ gcc/toplev.c | 19 +++++++++++++------ 5 files changed, 61 insertions(+), 17 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/pr100788.c diff --git a/gcc/common.opt b/gcc/common.opt index ffb968d90f8..509937da24f 100644 --- a/gcc/common.opt +++ b/gcc/common.opt @@ -826,6 +826,10 @@ Wcoverage-mismatch Common Var(warn_coverage_mismatch) Init(1) Warning Warn in case profiles in -fprofile-use do not match. +Wcoverage-invalid-line-number +Common Var(warn_coverage_invalid_linenum) Init(1) Warning +Warn in case a function ends earlier than it begins due to an invalid linenum macros. + Wmissing-profile Common Var(warn_missing_profile) Init(1) Warning Warn in case profiles in -fprofile-use do not exist. diff --git a/gcc/coverage.c b/gcc/coverage.c index 5a344cdfc17..dfc8108d5d8 100644 --- a/gcc/coverage.c +++ b/gcc/coverage.c @@ -622,18 +622,16 @@ coverage_compute_cfg_checksum (struct function *fn) int coverage_begin_function (unsigned lineno_checksum, unsigned cfg_checksum) { - expanded_location xloc; - unsigned long offset; - /* We don't need to output .gcno file unless we're under -ftest-coverage (e.g. -fprofile-arcs/generate/use don't need .gcno to work). */ if (no_coverage || !bbg_file_name) return 0; - xloc = expand_location (DECL_SOURCE_LOCATION (current_function_decl)); + expanded_location startloc + = expand_location (DECL_SOURCE_LOCATION (current_function_decl)); /* Announce function */ - offset = gcov_write_tag (GCOV_TAG_FUNCTION); + unsigned long offset = gcov_write_tag (GCOV_TAG_FUNCTION); if (param_profile_func_internal_id) gcov_write_unsigned (current_function_funcdef_no + 1); else @@ -650,16 +648,27 @@ coverage_begin_function (unsigned lineno_checksum, unsigned cfg_checksum) gcov_write_unsigned (DECL_ARTIFICIAL (current_function_decl) && !DECL_FUNCTION_VERSIONED (current_function_decl) && !DECL_LAMBDA_FUNCTION_P (current_function_decl)); - gcov_write_filename (xloc.file); - gcov_write_unsigned (xloc.line); - gcov_write_unsigned (xloc.column); + gcov_write_filename (startloc.file); + gcov_write_unsigned (startloc.line); + gcov_write_unsigned (startloc.column); expanded_location endloc = expand_location (cfun->function_end_locus); /* Function can start in a single file and end in another one. */ - int end_line = endloc.file == xloc.file ? endloc.line : xloc.line; - int end_column = endloc.file == xloc.file ? endloc.column: xloc.column; - gcc_assert (xloc.line <= end_line); + int end_line + = endloc.file == startloc.file ? endloc.line : startloc.line; + int end_column + = endloc.file == startloc.file ? endloc.column: startloc.column; + + if (startloc.line > end_line) + { + warning_at (DECL_SOURCE_LOCATION (current_function_decl), + OPT_Wcoverage_invalid_line_number, + "function starts on a higher line number than it ends"); + end_line = startloc.line; + end_column = startloc.column; + } + gcov_write_unsigned (end_line); gcov_write_unsigned (end_column); gcov_write_length (offset); diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index 08c3206b719..e91680ab329 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -5795,6 +5795,17 @@ poorly optimized code and is useful only in the case of very minor changes such as bug fixes to an existing code-base. Completely disabling the warning is not recommended. +@item -Wno-coverage-invalid-line-number +@opindex Wno-coverage-invalid-line-number +@opindex Wcoverage-invalid-line-number +Warn in case a function ends earlier than it begins due +to an invalid linenum macros. The warning is emitted only +with @option{--coverage} enabled. + By default, this warning is enabled and is treated as an +error. @option{-Wno-coverage-invalid-line-number} can be used to disable the +warning or @option{-Wno-error=coverage-invalid-line-number} can be used to +disable the error. + @item -Wno-cpp @r{(C, Objective-C, C++, Objective-C++ and Fortran only)} @opindex Wno-cpp diff --git a/gcc/testsuite/gcc.dg/pr100788.c b/gcc/testsuite/gcc.dg/pr100788.c new file mode 100644 index 00000000000..6f510ecf57c --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr100788.c @@ -0,0 +1,13 @@ +/* { dg-do compile } */ +/* { dg-options "--coverage -Wno-error=coverage-invalid-line-number" } */ + +void +foo() // { dg-warning "function starts on a higher line number than it ends" } +{ +#line 1 +} + +int main() +{ + foo (); +} diff --git a/gcc/toplev.c b/gcc/toplev.c index 6a6ebe9bb8c..55e7550151f 100644 --- a/gcc/toplev.c +++ b/gcc/toplev.c @@ -1744,12 +1744,19 @@ process_options (void) /* Enable -Werror=coverage-mismatch when -Werror and -Wno-error have not been set. */ - if (!global_options_set.x_warnings_are_errors - && warn_coverage_mismatch - && (global_dc->classify_diagnostic[OPT_Wcoverage_mismatch] == - DK_UNSPECIFIED)) - diagnostic_classify_diagnostic (global_dc, OPT_Wcoverage_mismatch, - DK_ERROR, UNKNOWN_LOCATION); + if (!global_options_set.x_warnings_are_errors) + { + if (warn_coverage_mismatch + && (global_dc->classify_diagnostic[OPT_Wcoverage_mismatch] == + DK_UNSPECIFIED)) + diagnostic_classify_diagnostic (global_dc, OPT_Wcoverage_mismatch, + DK_ERROR, UNKNOWN_LOCATION); + if (warn_coverage_invalid_linenum + && (global_dc->classify_diagnostic[OPT_Wcoverage_invalid_line_number] == + DK_UNSPECIFIED)) + diagnostic_classify_diagnostic (global_dc, OPT_Wcoverage_invalid_line_number, + DK_ERROR, UNKNOWN_LOCATION); + } /* Save the current optimization options. */ optimization_default_node