From patchwork Wed Jan 11 19:01:37 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nathan Sidwell X-Patchwork-Id: 135468 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 761E1B6EEC for ; Thu, 12 Jan 2012 06:01:59 +1100 (EST) Comment: DKIM? See http://www.dkim.org DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=gcc.gnu.org; s=default; x=1326913320; h=Comment: DomainKey-Signature:Received:Received:Received:Received:Received: Received:Received:Message-ID:Date:From:User-Agent:MIME-Version: To:Subject:Content-Type:Mailing-List:Precedence:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:Sender: Delivered-To; bh=jKmErQtHKw8+T6kNuQv6aoLj4mg=; b=H3ED6ggH3LV/kdl cI3ZAQaKikM5PC1M6PtrfCV1AdzzQivdgKKheSvMcAyW3/Et8fzRb2cHL1apsT7n 4BhX9Nd9AXOJAtoYqd2kfVD+6xga63OLVpwdXGxPr/CHcR1Gh59w7W6mo7tS6bU1 T8xDUcE7tpM6CYVkQBV1c8heJiPI= Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=default; d=gcc.gnu.org; h=Received:Received:X-SWARE-Spam-Status:X-Spam-Check-By:Received:Received:Received:Received:Received:Message-ID:Date:From:User-Agent:MIME-Version:To:Subject:Content-Type:X-IsSubscribed:Mailing-List:Precedence:List-Id:List-Unsubscribe:List-Archive:List-Post:List-Help:Sender:Delivered-To; b=UlocfiBTNfWmCgx0x7iTH2wd3OH90x12lyh7oQeheYqIxr0yHyjkSFTYkj+C4l Zfj6D6fH7BVWY6TDFquuZ02QBo2lS51QOAWCeu8xOh1amMia7F3S70n9jxH103Nc Lww2SOJEFu0CxB+DWfeZ3JusjhyHyVvUZolcS/8ZseXXk=; Received: (qmail 10938 invoked by alias); 11 Jan 2012 19:01:55 -0000 Received: (qmail 10929 invoked by uid 22791); 11 Jan 2012 19:01:54 -0000 X-SWARE-Spam-Status: No, hits=-2.4 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_LOW, TW_CP X-Spam-Check-By: sourceware.org Received: from mail-wi0-f175.google.com (HELO mail-wi0-f175.google.com) (209.85.212.175) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Wed, 11 Jan 2012 19:01:40 +0000 Received: by wibhm17 with SMTP id hm17so754600wib.20 for ; Wed, 11 Jan 2012 11:01:39 -0800 (PST) Received: by 10.180.91.42 with SMTP id cb10mr13131778wib.15.1326308499244; Wed, 11 Jan 2012 11:01:39 -0800 (PST) Received: by 10.180.91.42 with SMTP id cb10mr13131767wib.15.1326308499161; Wed, 11 Jan 2012 11:01:39 -0800 (PST) Received: from [192.168.44.102] (5ac3c85d.bb.sky.com. [90.195.200.93]) by mx.google.com with ESMTPS id f19sm2803234wbo.13.2012.01.11.11.01.38 (version=SSLv3 cipher=OTHER); Wed, 11 Jan 2012 11:01:38 -0800 (PST) Message-ID: <4F0DDC91.5060902@acm.org> Date: Wed, 11 Jan 2012 19:01:37 +0000 From: Nathan Sidwell User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.24) Gecko/20111108 Lightning/1.0b2 Thunderbird/3.1.16 MIME-Version: 1.0 To: GCC Patches Subject: [gcov] Erase stale annotated source X-IsSubscribed: yes 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 I've committed this gcov patch, which does 2 things: *) if a source file contains no code, erase any annotation file for it. Such a file will be misleading as it will refer to a previous instrumented program. *) dynamically allocate the source line buffer, rather than use a fixed length buffer. nathan 2012-01-11 Nathan Sidwell * gcov.c (STRING_SIZE): Remove. (generate_results): Erase annotations for source files with no coverage information. (read_line): New. (output_lines): Use it. Index: gcov.c =================================================================== --- gcov.c (revision 182755) +++ gcov.c (working copy) @@ -64,8 +64,6 @@ along with Gcov; see the file COPYING3. /* This is the size of the buffer used to read in source file lines. */ -#define STRING_SIZE 200 - struct function_info; struct block_info; struct source_info; @@ -708,24 +706,33 @@ generate_results (const char *file_name) function_summary (&src->coverage, "File"); total_lines += src->coverage.lines; total_executed += src->coverage.lines_executed; - if (flag_gcov_file && src->coverage.lines) + if (flag_gcov_file) { char *gcov_file_name = make_gcov_file_name (file_name, src->coverage.name); - FILE *gcov_file = fopen (gcov_file_name, "w"); - if (gcov_file) + if (src->coverage.lines) { - fnotice (stdout, "Creating '%s'\n", gcov_file_name); - output_lines (gcov_file, src); - if (ferror (gcov_file)) + FILE *gcov_file = fopen (gcov_file_name, "w"); + + if (gcov_file) + { + fnotice (stdout, "Creating '%s'\n", gcov_file_name); + output_lines (gcov_file, src); + if (ferror (gcov_file)) fnotice (stderr, "Error writing output file '%s'\n", gcov_file_name); - fclose (gcov_file); + fclose (gcov_file); + } + else + fnotice (stderr, "Could not open output file '%s'\n", + gcov_file_name); } else - fnotice (stderr, "Could not open output file '%s'\n", - gcov_file_name); + { + unlink (gcov_file_name); + fnotice (stdout, "Removing '%s'\n", gcov_file_name); + } free (gcov_file_name); } fnotice (stdout, "\n"); @@ -2188,6 +2195,44 @@ output_branch_count (FILE *gcov_file, in } +static const char * +read_line (FILE *file) +{ + static char *string; + static size_t string_len; + size_t pos = 0; + char *ptr; + + if (!string_len) + { + string_len = 200; + string = XNEWVEC (char, string_len); + } + + while ((ptr = fgets (string + pos, string_len - pos, file))) + { + size_t len = strlen (string + pos); + + if (string[pos + len - 1] == '\n') + { + string[pos + len - 1] = 0; + return string; + } + pos += len; + ptr = XNEWVEC (char, string_len * 2); + if (ptr) + { + memcpy (ptr, string, pos); + string = ptr; + string_len += 2; + } + else + pos = 0; + } + + return pos ? string : NULL; +} + /* Read in the source file one line at a time, and output that line to the gcov file preceded by its execution count and other information. */ @@ -2198,8 +2243,7 @@ output_lines (FILE *gcov_file, const sou FILE *source_file; unsigned line_num; /* current line number. */ const line_t *line; /* current line info ptr. */ - char string[STRING_SIZE]; /* line buffer. */ - char const *retval = ""; /* status of source file reading. */ + const char *retval = ""; /* status of source file reading. */ function_t *fn = NULL; fprintf (gcov_file, "%9s:%5d:Source:%s\n", "-", 0, src->coverage.name); @@ -2246,31 +2290,20 @@ output_lines (FILE *gcov_file, const sou fprintf (gcov_file, "\n"); } + if (retval) + retval = read_line (source_file); + /* For lines which don't exist in the .bb file, print '-' before the source line. For lines which exist but were never - executed, print '#####' before the source line. Otherwise, - print the execution count before the source line. There are - 16 spaces of indentation added before the source line so that - tabs won't be messed up. */ - fprintf (gcov_file, "%9s:%5u:", + executed, print '#####' or '=====' before the source line. + Otherwise, print the execution count before the source line. + There are 16 spaces of indentation added before the source + line so that tabs won't be messed up. */ + fprintf (gcov_file, "%9s:%5u:%s\n", !line->exists ? "-" : line->count ? format_gcov (line->count, 0, -1) - : line->unexceptional ? "#####" : "=====", line_num); - - if (retval) - { - /* Copy source line. */ - do - { - retval = fgets (string, STRING_SIZE, source_file); - if (!retval) - break; - fputs (retval, gcov_file); - } - while (!retval[0] || retval[strlen (retval) - 1] != '\n'); - } - if (!retval) - fputs ("/*EOF*/\n", gcov_file); + : line->unexceptional ? "#####" : "=====", line_num, + retval ? retval : "/*EOF*/"); if (flag_all_blocks) { @@ -2306,18 +2339,8 @@ output_lines (FILE *gcov_file, const sou last line of code. */ if (retval) { - for (; (retval = fgets (string, STRING_SIZE, source_file)); line_num++) - { - fprintf (gcov_file, "%9s:%5u:%s", "-", line_num, retval); - - while (!retval[0] || retval[strlen (retval) - 1] != '\n') - { - retval = fgets (string, STRING_SIZE, source_file); - if (!retval) - break; - fputs (retval, gcov_file); - } - } + for (; (retval = read_line (source_file)); line_num++) + fprintf (gcov_file, "%9s:%5u:%s\n", "-", line_num, retval); } if (source_file)