[rtems-tools commit] GcovData: Convert to C++

Joel Sherrill joel at rtems.org
Tue Aug 3 21:00:44 UTC 2021


Module:    rtems-tools
Branch:    master
Commit:    c3762cd29cde6e4dd75629711851effa8201281f
Changeset: http://git.rtems.org/rtems-tools/commit/?id=c3762cd29cde6e4dd75629711851effa8201281f

Author:    Ryan Long <ryan.long at oarcorp.com>
Date:      Mon Jul 12 16:06:00 2021 -0400

GcovData: Convert to C++

Change C-style code to C++

---

 tester/covoar/GcovData.cc         | 317 ++++++++++++++++++++------------------
 tester/covoar/GcovData.h          |  32 ++--
 tester/covoar/GcovFunctionData.cc | 298 ++++++++++++++++-------------------
 tester/covoar/GcovFunctionData.h  |  42 +++--
 4 files changed, 334 insertions(+), 355 deletions(-)

diff --git a/tester/covoar/GcovData.cc b/tester/covoar/GcovData.cc
index 11c26ce..59963b6 100644
--- a/tester/covoar/GcovData.cc
+++ b/tester/covoar/GcovData.cc
@@ -1,9 +1,3 @@
-/*
- *  TODO: use strings instead of cstrings for reliability and saving memory
- *  TODO: use global buffers
- *
- */
-
 /*! @file GcovData.cc
  *  @brief GcovData Implementation
  *
@@ -35,62 +29,64 @@ namespace Gcov {
   {
   }
 
-  bool GcovData::readGcnoFile( const char* const  fileName )
+  bool GcovData::readGcnoFile( const std::string& fileName )
   {
-    int        status;
-    FILE*      gcovFile;
-    char*      tempString;
-    char*      tempString2;
-    char*      tempString3;
-
-    if ( strlen(fileName) >= FILE_NAME_LENGTH ){
-      fprintf(
-        stderr,
-        "ERROR: File name is too long to be correctly stored: %u\n",
-        (unsigned int) strlen(fileName)
-      );
+    int           status;
+    std::ifstream gcovFile;
+    std::string   tempString;
+    std::string   tempString2;
+    std::string   tempString3;
+    size_t        index;
+
+    if ( fileName.length() >= FILE_NAME_LENGTH ) {
+      std::cerr << "ERROR: File name is too long to be correctly stored: "
+                << fileName.length() << std::endl;
       return false;
     }
-    strcpy( gcnoFileName, fileName );
-    strcpy( gcdaFileName, fileName );
-    strcpy( textFileName, fileName );
-    strcpy( cFileName, fileName );
-    tempString = strstr( gcdaFileName,".gcno" );
-    tempString2 = strstr( textFileName,".gcno" );
-    tempString3 = strstr( cFileName,".gcno" );
-
-    if ( (tempString == NULL) && (tempString2 == NULL) ){
-      fprintf(stderr, "ERROR: incorrect name of *.gcno file\n");
-    }
-    else
-    {
-      strcpy( tempString, ".gcda");             // construct gcda file name
-      strcpy( tempString2, ".txt");             // construct report file name
-      strcpy( tempString3, ".c");               // construct source file name
+
+    gcnoFileName = fileName;
+    gcdaFileName = fileName;
+    textFileName = fileName;
+    cFileName    = fileName;
+    tempString   = gcdaFileName;
+    tempString2  = textFileName;
+    tempString3  = cFileName;
+
+    index = tempString.find( ".gcno" );
+    if ( index == std::string::npos ) {
+      std::cerr << "ERROR: incorrect name of *.gcno file" << std::endl;
+      return false;
+    } else {
+      // construct gcda file name
+      tempString = tempString.replace( index, strlen( ".gcno" ), ".gcda" );
+
+      // construct report file name
+      tempString2 = tempString2.replace( index, strlen( ".gcno" ), ".txt" );
+
+      // construct source file name
+      tempString3 = tempString3.replace( index, strlen( ".gcno" ), ".c" );
     }
 
     // Debug message
-    // fprintf( stderr, "Readning file: %s\n",  gcnoFileName);
+    // std::cerr << "Reading file: " << gcnoFileName << std::endl;
 
     // Open the notes file.
-    gcovFile = fopen( gcnoFileName, "r" );
+    gcovFile.open( gcnoFileName );
     if ( !gcovFile ) {
-      fprintf( stderr, "Unable to open %s\n", gcnoFileName );
+      std::cerr << "Unable to open " << gcnoFileName << std::endl;
       return false;
     }
 
     // Read and validate the gcnoPreamble (magic, version, timestamp) from the file
     status = readFilePreamble( &gcnoPreamble, gcovFile, GCNO_MAGIC );
     if ( status <= 0 ){
-      fprintf( stderr, "Unable to read %s\n", gcnoFileName );
-      fclose( gcovFile );
+      std::cerr << "Unable to read " << gcnoFileName << std::endl;
       return false;
     }
 
     //Read all remaining frames from file
     while( readFrame(gcovFile) ){}
 
-    fclose( gcovFile );
     return true;
   }
 
@@ -99,7 +95,7 @@ namespace Gcov {
   {
     gcov_preamble         preamble;
     gcov_frame_header     header;
-    FILE*                 gcdaFile;
+    std::ofstream         gcdaFile;
     functions_iterator_t  currentFunction;
     arcs_iterator_t       currentArc;
     uint32_t              buffer;
@@ -110,10 +106,10 @@ namespace Gcov {
     uint64_t              llBuffer[4096];    // TODO: Use common buffer
     gcov_statistics       objectStats;
     gcov_statistics       programStats;
-    size_t                status;
+    long int              bytes_before;
 
     // Debug message
-    // fprintf( stderr, "Writing file: %s\n",  gcdaFileName);
+    //std::cerr << "Writing file: " <<  gcdaFileName << std::endl;
 
     // Lets clear counters sumators
     countersSum     = 0;
@@ -121,9 +117,9 @@ namespace Gcov {
     countersFoundSum   = 0;
 
     // Open the data file.
-    gcdaFile = fopen( gcdaFileName, "w" );
+    gcdaFile.open( gcdaFileName );
     if ( !gcdaFile ) {
-      fprintf( stderr, "Unable to create %s\n", gcdaFileName );
+      std::cerr << "Unable to create " << gcdaFileName << std::endl;
       return false;
     }
 
@@ -133,9 +129,11 @@ namespace Gcov {
     preamble.timestamp  = gcnoPreamble.timestamp;
 
     //Write preamble
-    status = fwrite (&preamble , sizeof( preamble ), 1 , gcdaFile );
-    if ( status != 1 )
-      fprintf( stderr, "Error while writing gcda preamble to a file %s\n", gcdaFileName );
+    gcdaFile.write( (char *) &preamble , 4 * sizeof( preamble ) );
+    if ( gcdaFile.fail() ) {
+      std::cerr << "Error while writing gcda preamble to a file "
+                << gcdaFileName << std::endl;
+    }
 
     //Write function info and counter counts
     for (
@@ -147,21 +145,27 @@ namespace Gcov {
       //Write function announcement frame header (length always equals 2)
       header.tag = GCOV_TAG_FUNCTION;
       header.length = 2;
-      status = fwrite (&header, sizeof(header), 1, gcdaFile );
-      if ( status != 1 )
-        fprintf( stderr, "Error while writing function announcement to a file %s\n", gcdaFileName );
+      gcdaFile.write( (char *) &header, sizeof( header ) );
+      if ( gcdaFile.fail() ) {
+        std::cerr << "Error while writing function announcement to a file "
+                  << gcdaFileName << std::endl;
+      }
 
       //Write function id
       buffer = (*currentFunction).getId();
-      status = fwrite (&buffer, sizeof( buffer ), 1, gcdaFile );
-      if ( status != 1 )
-        fprintf( stderr, "Error while writing function id to a file %s\n", gcdaFileName );
+      gcdaFile.write( (char *) &buffer, sizeof( buffer ) );
+      if ( gcdaFile.fail() ) {
+        std::cerr << "Error while writing function id to a file "
+                  << gcdaFileName << std::endl;
+      }
 
       //Write function checksum
       buffer = (*currentFunction).getChecksum();
-      status = fwrite (&buffer, sizeof( buffer ), 1, gcdaFile );
-      if ( status != 1 )
-        fprintf( stderr, "Error while writing function checksum to a file %s\n", gcdaFileName );
+      gcdaFile.write( (char *) &buffer, sizeof( buffer ) );
+      if ( gcdaFile.fail() ) {
+        std::cerr << "Error while writing function checksum to a file "
+                  << gcdaFileName << std::endl;
+      }
 
       // Determine how many counters there are
       // and store their counts in buffer
@@ -172,13 +176,19 @@ namespace Gcov {
       //Write info about counters
       header.tag = GCOV_TAG_COUNTER;
       header.length = countersFound * 2;
-      status = fwrite (&header, sizeof( header ), 1, gcdaFile );
-      if ( status != 1 )
-        fprintf( stderr, "Error while writing counter header to a file %s\n", gcdaFileName );
+      gcdaFile.write( (char *) &header, sizeof( header ) );
+      if ( gcdaFile.fail() ) {
+        std::cerr << "Error while writing counter header to a file "
+                  << gcdaFileName << std::endl;
+      }
+
+      bytes_before = gcdaFile.tellp();
 
-      status = fwrite (llBuffer, sizeof( uint64_t ), countersFound , gcdaFile );
-      if ( status != countersFound )
-        fprintf( stderr, "Error while writing counter data to a file %s\n", gcdaFileName );
+      gcdaFile.write( (char *) llBuffer, sizeof( uint64_t ) * countersFound );
+      if ( gcdaFile.tellp() - bytes_before != countersFound ) {
+        std::cerr << "Error while writing counter data to a file "
+                  << gcdaFileName << std::endl;
+      }
     }
 
     // Prepare frame with object file statistics
@@ -192,12 +202,17 @@ namespace Gcov {
     objectStats.sumMax = countersMax;    // we have no clue
 
     // Write data
-    status = fwrite (&header, sizeof( header ), 1, gcdaFile );
-    if ( status != 1 )
-      fprintf( stderr, "Error while writing stats header to a file %s\n", gcdaFileName );
-    status = fwrite (&objectStats, sizeof( objectStats ), 1, gcdaFile );
-    if ( status != 1 )
-      fprintf( stderr, "Error while writing object stats to a file %s\n", gcdaFileName );
+    gcdaFile.write( (char *) &header, sizeof( header ) );
+    if ( gcdaFile.fail() ) {
+      std::cerr << "Error while writing stats header to a file "
+                << gcdaFileName << std::endl;
+    }
+
+    gcdaFile.write( (char *) &objectStats, sizeof( objectStats ) );
+    if ( gcdaFile.fail() ) {
+      std::cerr << "Error while writing object stats to a file "
+                << gcdaFileName << std::endl;
+    }
 
 
     // Prepare frame with program statistics
@@ -211,25 +226,26 @@ namespace Gcov {
     programStats.sumMax = countersMax;    // we have no clue
 
     // Write data
-    status = fwrite (&header, sizeof( header ), 1, gcdaFile );
-    if ( status != 1 )
-      fprintf( stderr, "Error while writing stats header to a file %s\n", gcdaFileName );
-    status = fwrite (&programStats, sizeof( programStats ), 1, gcdaFile );
-    if ( status != 1 )
-      fprintf( stderr, "Error while writing program stats to a file %s\n", gcdaFileName );
+    gcdaFile.write( (char *) &header, sizeof( header ) );
+    if ( gcdaFile.fail() ) {
+      std::cerr << "Error while writing stats header to a file "
+                << gcdaFileName << std::endl;
+    }
 
-    fclose( gcdaFile );
+    gcdaFile.write( (char *) &programStats, sizeof( programStats ) );
+    if ( gcdaFile.fail() ) {
+      std::cerr << "Error while writing program stats to a file "
+                << gcdaFileName << std::endl;
+    }
 
     return true;
   }
 
-  bool GcovData::readFrame(
-         FILE*         gcovFile
-  )
+  bool GcovData::readFrame( std::ifstream& gcovFile )
   {
     gcov_frame_header   header;
     char                buffer[512];
-    uint32_t            intBuffer[4096];
+    char                intBuffer[16384];
     uint32_t            tempBlockId;
     blocks_iterator_t   tempBlockIterator;
     int                 status;
@@ -251,7 +267,8 @@ namespace Gcov {
           GcovFunctionData newFunction;
 
           if ( !readFunctionFrame(header, gcovFile, &newFunction) ){
-            fprintf( stderr, "Error while reading FUNCTION from gcov file...\n" );
+            std::cerr << "Error while reading FUNCTION from gcov file..."
+                      << std::endl;
             return false;
           }
 
@@ -262,14 +279,13 @@ namespace Gcov {
 
       case GCOV_TAG_BLOCKS:
 
-        status = fread( &intBuffer, 4, header.length, gcovFile );
-        if ( status != (int) header.length){
-          fprintf(
-            stderr, "Error while reading BLOCKS from gcov file...\n"
-            "Header lenght is %u instead of %u\n",
-            header.length,
-            status
-          );
+        gcovFile.read( intBuffer, header.length );
+        if ( gcovFile.gcount() != (int) header.length ) {
+          std::cerr << "Error while reading BLOCKS from gcov file..."
+                    << std::endl
+                    << "Header length is " << header.length
+                    << " instead of " << gcovFile.gcount()
+                    << std::endl;
           return false;
         }
 
@@ -280,8 +296,8 @@ namespace Gcov {
 
       case GCOV_TAG_ARCS:
 
-        status = fread( &intBuffer, 4, header.length, gcovFile );
-        if (status != (int) header.length){
+        gcovFile.read( intBuffer, header.length );
+        if ( gcovFile.gcount() != (int) header.length ) {
           return false;
         }
 
@@ -292,12 +308,10 @@ namespace Gcov {
 
       case GCOV_TAG_LINES:
 
-        status = fread( &intBuffer, 4, 2, gcovFile );
-        if (status != 2 || intBuffer[1] != 0){
-          fprintf(
-            stderr,
-            "Error while reading block id for LINES from gcov file..."
-          );
+        gcovFile.read( intBuffer, 2 );
+        if ( gcovFile.gcount() != 2 || intBuffer[1] != 0 ) {
+          std::cerr << "Error while reading block id for LINES from gcov "
+                    << "file..." << std::endl;
           return false;
         }
         tempBlockId = intBuffer[0];
@@ -309,9 +323,10 @@ namespace Gcov {
         header.length -= readString(buffer, gcovFile);
         functions.back().setBlockFileName( tempBlockIterator, buffer );
 
-        status = fread( &intBuffer, 4, header.length, gcovFile );
-        if (status != (int) header.length){
-          fprintf( stderr, "Error while reading LINES from gcov file..." );
+        gcovFile.read( intBuffer, header.length );
+        if ( gcovFile.gcount() != (int) header.length ) {
+          std::cerr << "Error while reading LINES from gcov file..."
+                    << std::endl;
           return false;
         }
 
@@ -323,30 +338,30 @@ namespace Gcov {
 
       default:
 
-        fprintf( stderr, "\n\nERROR - encountered unknown *.gcno tag : 0x%x\n", header.tag );
+        std::cerr << std::endl << std::endl
+                  << "ERROR - encountered unknown *.gcno tag : 0x"
+                  << std::hex << header.tag << std::dec << std::endl;
         break;
       }
 
       return true;
   }
 
-  int GcovData::readString(
-    char*     buffer,   //TODO: use global buffer here
-    FILE*     gcovFile
-  )
+  int GcovData::readString( char* buffer, std::ifstream& gcovFile )
   {
-    int          status;
     int          length;
 
-    status = fread( &length, sizeof(int), 1, gcovFile );
-    if (status != 1){
-      fprintf( stderr, "ERROR: Unable to read string length from gcov file\n" );
+    gcovFile.read( (char *) &length, sizeof( int ) );
+    if ( gcovFile.gcount() != sizeof( int ) ) {
+      std::cerr << "ERROR: Unable to read string length from gcov file"
+                << std::endl;
       return -1;
     }
 
-    status = fread( buffer, length * 4 , 1, gcovFile );
-    if (status != 1){
-      fprintf( stderr, "ERROR: Unable to read string from gcov file\n" );
+    gcovFile.read( buffer, length * 4 );
+    if ( gcovFile.gcount() != length * 4 ) {
+      std::cerr << "ERROR: Unable to read string from gcov file"
+                << std::endl;
       return -1;
     }
 
@@ -357,16 +372,16 @@ namespace Gcov {
 
   int GcovData::readFrameHeader(
     gcov_frame_header*   header,
-    FILE*             gcovFile
+    std::ifstream&       gcovFile
   )
   {
-    int          status;
     int          length;
 
     length = sizeof(gcov_frame_header);
-    status = fread( header, length, 1, gcovFile );
-    if (status != 1){
-      //fprintf( stderr, "ERROR: Unable to read frame header from gcov file\n" );
+    gcovFile.read( (char *) header, length );
+    if ( gcovFile.gcount() != length ) {
+      std::cerr << "ERROR: Unable to read frame header from gcov file"
+                << std::endl;
       return -1;
     }
 
@@ -375,22 +390,23 @@ namespace Gcov {
 
   int GcovData::readFilePreamble(
      gcov_preamble*       preamble,
-     FILE*             gcovFile,
+     std::ifstream&    gcovFile,
      uint32_t        desiredMagic
   )
   {
-    int          status;
     int          length;
 
     length = sizeof( gcov_preamble );
-    status = fread( preamble, sizeof( gcov_preamble), 1, gcovFile );
-    if (status <= 0) {
-      fprintf( stderr, "Error while reading file preamble\n" );
+    gcovFile.read( (char *) &preamble, 4 * sizeof( gcov_preamble ) );
+    if ( gcovFile.gcount() != 4 * sizeof( gcov_preamble ) ) {
+      std::cerr << "Error while reading file preamble" << std::endl;
       return -1;
     }
 
     if ( preamble->magic != GCNO_MAGIC ) {
-      fprintf( stderr, "File is not a valid *.gcno output (magic: 0x%4x)\n", preamble->magic );
+      std::cerr << "File is not a valid *.gcno output (magic: 0x"
+                << std::hex << std::setw( 4 ) << preamble->magic
+                << ")" << std::endl;
       return -1;
     }
 
@@ -399,17 +415,16 @@ namespace Gcov {
 
   bool GcovData::readFunctionFrame(
     gcov_frame_header   header,
-    FILE*           gcovFile,
+    std::ifstream&     gcovFile,
     GcovFunctionData*  function
   )
   {
     char         buffer[512];    //TODO: use common buffers
-    uint32_t     intBuffer[4096];
-    int          status;
+    char         intBuffer[16384];
 
-    status = fread( &intBuffer, 8, 1, gcovFile );
-    if (status != 1){
-      fprintf( stderr, "ERROR: Unable to read Function ID & checksum\n" );
+    gcovFile.read( (char *) &intBuffer, 8 );
+    if ( gcovFile.gcount() != 8 ) {
+      std::cerr << "ERROR: Unable to read Function ID & checksum" << std::endl;
       return false;
     }
     header.length -= 2;
@@ -420,9 +435,10 @@ namespace Gcov {
     function->setFunctionName( buffer, symbolsToAnalyze_m );
     header.length -= readString( buffer, gcovFile );
     function->setFileName( buffer );
-    status = fread( &intBuffer, 4, header.length, gcovFile );
-    if (status <= 0){
-    fprintf( stderr, "ERROR: Unable to read Function starting line number\n" );
+    gcovFile.read( (char*) &intBuffer, 4 * header.length );
+    if (gcovFile.gcount() != 4 * header.length ) {
+      std::cerr << "ERROR: Unable to read Function starting line number"
+                << std::endl;
       return false;
     }
     function->setFirstLineNumber( intBuffer[0] );
@@ -434,15 +450,15 @@ namespace Gcov {
   {
     functions_iterator_t   currentFunction;
     uint32_t               i = 1;          //iterator
-    FILE*                  textFile;
+    std::ofstream          textFile;
 
     // Debug message
-    // fprintf( stderr, "Writing file: %s\n",  textFileName);
+    // std::cerr << "Writing file: " << textFileName << std::endl;
 
     // Open the data file.
-    textFile = fopen( textFileName, "w" );
-    if ( !textFile ) {
-      fprintf( stderr, "Unable to create %s\n", textFileName );
+    textFile.open( textFileName );
+    if ( !textFile.is_open() ) {
+      std::cerr << "Unable to create " << textFileName << std::endl;
       return false;
     }
 
@@ -459,25 +475,20 @@ namespace Gcov {
       i++;
     }
 
-    fclose ( textFile );
     return true;
   }
 
-  void GcovData::printGcnoFileInfo( FILE * textFile )
+  void GcovData::printGcnoFileInfo( std::ofstream& textFile )
   {
-    fprintf(
-      textFile,
-      "\nFILE:\t\t\t%s\n"
-      "magic:\t\t\t%x\n"
-      "version:\t\t%x\n"
-      "timestamp:\t\t%x\n"
-      "functions found: \t%u\n\n",
-      gcnoFileName,
-      gcnoPreamble.magic,
-      gcnoPreamble.version,
-      gcnoPreamble.timestamp,
-      numberOfFunctions
-    );
+    textFile << std::endl << "FILE:\t\t\t" << gcnoFileName
+             << std::endl << std::hex
+             << "magic:\t\t\t" << gcnoPreamble.magic << std::endl
+             << "version:\t\t" << gcnoPreamble.version << std::endl
+             << "timestamp:\t\t" << gcnoPreamble.timestamp << std::endl
+             << std::dec
+             << "functions found: \t"
+             << std::endl << std::endl << gcnoPreamble.timestamp
+             << std::endl << std::endl;
   }
 
   void GcovData::writeGcovFile( )
diff --git a/tester/covoar/GcovData.h b/tester/covoar/GcovData.h
index b00b27a..e135ffe 100644
--- a/tester/covoar/GcovData.h
+++ b/tester/covoar/GcovData.h
@@ -10,6 +10,8 @@
 #include <stdint.h>
 #include <list>
 #include <iostream>
+#include <iomanip>
+#include <fstream>
 #include "GcovFunctionData.h"
 #include "DesiredSymbols.h"
 
@@ -86,7 +88,7 @@ class DesiredSymbols;
      *
      *  @return Returns TRUE if the method succeeded and FALSE if it failed.
      */
-    bool readGcnoFile( const char* const  fileName );
+    bool readGcnoFile( const std::string& fileName );
 
     /*!
      *  This method writes the *.gcda file. It also produces and stores
@@ -118,10 +120,10 @@ class DesiredSymbols;
 
     uint32_t                            numberOfFunctions;
     gcov_preamble                       gcnoPreamble;
-    char                                gcnoFileName[FILE_NAME_LENGTH];
-    char                                gcdaFileName[FILE_NAME_LENGTH];
-    char                                textFileName[FILE_NAME_LENGTH];
-    char                                cFileName[FILE_NAME_LENGTH];
+    std::string                         gcnoFileName;
+    std::string                         gcdaFileName;
+    std::string                         textFileName;
+    std::string                         cFileName;
     functions_t                         functions;
 
 
@@ -132,9 +134,7 @@ class DesiredSymbols;
      *
      *  @return true if read was succesfull, false otherwise
      */
-    bool readFrame(
-      FILE*       gcovFile
-    );
+    bool readFrame( std::ifstream& gcovFile );
 
     /*!
      *  This method reads a string from gcov file
@@ -144,10 +144,7 @@ class DesiredSymbols;
      *
      *  @return Returns length of words read (word = 32bit) or -1 if error ocurred
      */
-    int readString(
-      char*       buffer,
-      FILE*       gcovFile
-    );
+    int readString( char* buffer, std::ifstream& gcovFile );
 
     /*!
      *  This method reads a frame header from gcov file
@@ -158,10 +155,7 @@ class DesiredSymbols;
      *  @return Returns length of words read (word = 32bit)
      *  or -1 if error ocurred
      */
-    int readFrameHeader(
-      gcov_frame_header*  header,
-      FILE*               gcovFile
-    );
+    int readFrameHeader( gcov_frame_header* header, std::ifstream& gcovFile );
 
     /*!
      *  This method reads a frame header from gcov file
@@ -175,7 +169,7 @@ class DesiredSymbols;
      */
     int readFilePreamble(
       gcov_preamble*      preamble,
-      FILE*               gcovFile,
+      std::ifstream&      gcovFile,
       const uint32_t      desiredMagic
     );
 
@@ -190,7 +184,7 @@ class DesiredSymbols;
      */
     bool readFunctionFrame(
       gcov_frame_header   header,
-      FILE*               gcovFile,
+      std::ifstream&      gcovFile,
       GcovFunctionData*   function
     );
 
@@ -198,7 +192,7 @@ class DesiredSymbols;
      *  This method prints info about previously read *.gcno file
      *  to a specified report file
      */
-    void printGcnoFileInfo( FILE * textFile );
+    void printGcnoFileInfo( std::ofstream& textFile );
 
     /*!
      * This member variable contains the symbols to be analyzed
diff --git a/tester/covoar/GcovFunctionData.cc b/tester/covoar/GcovFunctionData.cc
index ff0e515..34af677 100644
--- a/tester/covoar/GcovFunctionData.cc
+++ b/tester/covoar/GcovFunctionData.cc
@@ -45,7 +45,7 @@ namespace Gcov {
   }
 
   bool GcovFunctionData::setFunctionName(
-    const char*               fcnName,
+    const std::string&        fcnName,
     Coverage::DesiredSymbols& symbolsToAnalyze
   )
   {
@@ -53,16 +53,13 @@ namespace Gcov {
 
     symbolName = fcnName;
 
-    if ( strlen(fcnName) >= FUNCTION_NAME_LENGTH ) {
-      fprintf(
-        stderr,
-        "ERROR: Function name is too long to be correctly stored: %u\n",
-        (unsigned int) strlen(fcnName)
-      );
+    if ( fcnName.length() >= FUNCTION_NAME_LENGTH ) {
+      std::cerr << "ERROR: Function name is too long to be correctly stored: "
+                << fcnName.length() << std::endl;
       return false;
     }
 
-    strcpy (functionName, fcnName);
+    functionName = fcnName;
 
     // Tie function to its coverage map
     symbolInfo = symbolsToAnalyze.find( symbolName );
@@ -70,34 +67,26 @@ namespace Gcov {
       coverageMap = symbolInfo->unifiedCoverageMap;
 
 #if 0
-    if ( coverageMap == NULL) {
-      fprintf(
-        stderr,
-        "ERROR: Could not find coverage map for: %s\n",
-        symbolName.c_str()
-      );
+    if ( coverageMap == NULL ) {
+      std::cerr << "ERROR: Could not find coverage map for: " << symbolName
+                << std::endl;
     } else {
-      fprintf(
-        stderr,
-        "SUCCESS: Hound coverage map for: %s\n",
-        symbolName.c_str()
-      );
-   }
+      std::cerr << "SUCCESS: Found coverage map for: " << symbolName
+                << std::endl;
+    }
 #endif
 
     return true;
   }
 
-  bool GcovFunctionData::setFileName( const char* fileName ) {
-    if ( strlen(fileName) >= FILE_NAME_LENGTH ){
-      fprintf(
-        stderr,
-        "ERROR: File name is too long to be correctly stored: %u\n",
-        (unsigned int) strlen(fileName)
-      );
+  bool GcovFunctionData::setFileName( const std::string& fileName ) {
+    if ( fileName.length() >= FILE_NAME_LENGTH ) {
+      std::cerr << "ERROR: File name is too long to be correctly stored: "
+                << fileName.length() << std::endl;
       return false;
     }
-    strcpy (sourceFileName, fileName);
+
+    sourceFileName = fileName;
     return true;
   }
 
@@ -172,9 +161,9 @@ namespace Gcov {
   }
 
   void GcovFunctionData::addBlock(
-    const uint32_t  id,
-    const uint32_t  flags,
-    const char *    sourceFileName
+    const uint32_t     id,
+    const uint32_t     flags,
+    const std::string& sourceFileName
   )
   {
     gcov_block_info block;
@@ -184,44 +173,35 @@ namespace Gcov {
     block.flags = flags;
     block.numberOfLines = 0;
     block.counter = 0;
-    strcpy (block.sourceFileName, sourceFileName);
+    block.sourceFileName = sourceFileName;
     blocks.push_back(block);
   }
 
   void GcovFunctionData::printFunctionInfo(
-    FILE * textFile,
-    uint32_t function_number
+    std::ofstream& textFile,
+    uint32_t       function_number
   )
   {
     blocks_iterator_t  currentBlock;
     arcs_iterator_t    currentArc;
 
-    fprintf(
-      textFile,
-      "\n\n=========================="
-      "FUNCTION %3d "
-      "==========================\n\n",
-      function_number
-    );
-    fprintf(
-      textFile,
-      "Name:      %s\n"
-      "File:      %s\n"
-      "Line:      %u\n"
-      "Id:        %u\n"
-      "Checksum:  0x%x\n\n",
-      functionName,
-      sourceFileName,
-      firstLineNumber,
-      id,
-      checksum
-    );
+    textFile << std::endl << std::endl
+             << "=========================="
+             << "FUNCTION  " << std::setw( 3 ) << function_number
+             << "=========================="
+             << std::endl << std::endl
+             << "Name:      " << functionName << std::endl
+             << "File:      " << sourceFileName << std::endl
+             << "Line:      " << firstLineNumber << std::endl
+             << "Id:        " << id << std::endl
+             << "Checksum:  0x" << std::hex << checksum << std::dec
+             << std::endl << std::endl;
 
     // Print arcs info
     for ( currentArc = arcs.begin(); currentArc != arcs.end(); currentArc++ ) {
       printArcInfo( textFile, currentArc );
     }
-    fprintf( textFile, "\n");
+    textFile << std::endl;
 
     // Print blocks info
     for ( currentBlock = blocks.begin();
@@ -233,8 +213,8 @@ namespace Gcov {
   }
 
   void GcovFunctionData::printCoverageInfo(
-    FILE     *textFile,
-    uint32_t  function_number
+    std::ofstream& textFile,
+    uint32_t       function_number
   )
   {
     uint32_t        baseAddress = 0;
@@ -254,12 +234,12 @@ namespace Gcov {
       }
       baseSize   = coverageMap->getSize();
 
-      fprintf(
-        textFile,
-        "\nInstructions (Base address: 0x%08x, Size: %4u): \n\n",
-        baseAddress,
-        baseSize
-      );
+      textFile << std::endl << "Instructions (Base address: 0x"
+               << std::setfill( '0' ) << std::setw( 8 )
+               << std::hex << baseAddress << std::dec << std::setfill( ' ' )
+               << ", Size: " << std::setw( 4 ) << baseSize
+               << "):" << std::endl << std::endl;
+
       for ( instruction = symbolInfo->instructions.begin();
             instruction != symbolInfo->instructions.end();
             instruction++
@@ -267,27 +247,30 @@ namespace Gcov {
       {
         if ( instruction->isInstruction ) {
           currentAddress = instruction->address - baseAddress;
-          fprintf( textFile, "0x%-70s ", instruction->line.c_str() );
-          fprintf( textFile, "| 0x%08x ",   currentAddress );
-          fprintf( textFile, "*");
-          fprintf( textFile,
-                    "| exec: %4u ",
-                    coverageMap->getWasExecuted( currentAddress )
-          );
-          fprintf( textFile, "| taken/not: %4u/%4u ",
-                    coverageMap->getWasTaken( currentAddress ),
-                    coverageMap->getWasNotTaken( currentAddress )
-          );
+
+          textFile << std::left << "0x" << std::setw( 70 )
+                   << instruction->line.c_str() << " " << std::right
+                   << "| 0x" << std::hex << std::setfill( '0' )
+                   << std::setw( 8 ) << currentAddress << " "
+                   << std::dec << std::setfill( ' ' )
+                   << "*| exec: " << std::setw( 4 )
+                   << coverageMap->getWasExecuted( currentAddress )
+                   << " | taken/not: " << std::setw( 4 )
+                   << coverageMap->getWasTaken( currentAddress )
+                   << "/" << std::setw( 4 )
+                   << coverageMap->getWasNotTaken( currentAddress )
+                   << " ";
 
           if ( instruction->isBranch )
-            fprintf( textFile, "| Branch " );
+            textFile << "| Branch ";
           else
-            fprintf( textFile, "         " );
+            textFile << "         ";
 
           if ( instruction->isNop )
-            fprintf( textFile, "| NOP(%3u) \n", instruction->nopSize );
+            textFile << "| NOP(" << std::setw( 3 ) << instruction->nopSize
+                     << ") " << std::endl;
           else
-            fprintf( textFile, "           \n" );
+            textFile << "           " << std::endl;
         }
       }
     }
@@ -295,10 +278,10 @@ namespace Gcov {
 
   void GcovFunctionData::setBlockFileName(
     const blocks_iterator_t  block,
-    const char               *fileName
+    const std::string&       fileName
   )
   {
-    strcpy(block->sourceFileName, fileName);
+    block->sourceFileName = fileName;
   }
 
   void GcovFunctionData::addBlockLine(
@@ -310,9 +293,7 @@ namespace Gcov {
     (block->numberOfLines)++;
   }
 
-  blocks_iterator_t GcovFunctionData::findBlockById(
-    const uint32_t    id
-  )
+  blocks_iterator_t GcovFunctionData::findBlockById( const uint32_t id )
   {
     blocks_iterator_t blockIterator;
 
@@ -324,78 +305,70 @@ namespace Gcov {
         blockIterator++;
       }
     } else {
-      fprintf(
-        stderr,
-        "ERROR: GcovFunctionData::findBlockById() failed, no blocks present\n"
-      );
+      std::cerr << "ERROR: GcovFunctionData::findBlockById() failed"
+                << ", no blocks present" << std::endl;
     }
     return blockIterator;
   }
 
   void GcovFunctionData::printArcInfo(
-                FILE * textFile, arcs_iterator_t arc
+    std::ofstream&  textFile,
+    arcs_iterator_t arc
   )
   {
-    fprintf(
-      textFile,
-      " > ARC %3u -> %3u ",
-      arc->sourceBlock,
-      arc->destinationBlock
-    );
-
-    fprintf( textFile, "\tFLAGS: ");
-    switch ( arc->flags ){
+    textFile << " > ARC " << std::setw( 3 ) << arc->sourceBlock
+             << " -> " << arc->destinationBlock << " ";
+
+    textFile << "\tFLAGS: ";
+    switch ( arc->flags ) {
       case 0:
-        fprintf( textFile, "( ___________ ____ _______ )");
+        textFile << "( ___________ ____ _______ )";
         break;
       case 1:
-        fprintf( textFile, "( ___________ ____ ON_TREE )");
+        textFile << "( ___________ ____ ON_TREE )";
         break;
       case 2:
-        fprintf( textFile, "( ___________ FAKE _______ )");
+        textFile << "( ___________ FAKE _______ )";
         break;
       case 3:
-        fprintf( textFile, "( ___________ FAKE ON_TREE )");
+        textFile << "( ___________ FAKE ON_TREE )";
         break;
       case 4:
-        fprintf( textFile, "( FALLTHROUGH ____ _______ )");
+        textFile << "( FALLTHROUGH ____ _______ )";
         break;
       case 5:
-        fprintf( textFile, "( FALLTHROUGH ____ ON_TREE )");
+        textFile << "( FALLTHROUGH ____ ON_TREE )";
         break;
       default:
-        fprintf( textFile, "( =======FLAGS_ERROR====== )");
-        fprintf( stderr,
-                " ERROR: Unknown arc flag: 0x%x\n",
-                arcs.back().flags
-        );
+        textFile  << "( =======FLAGS_ERROR====== )";
+        std::cerr << " ERROR: Unknown arc flag: 0x"
+                  << std::hex << arcs.back().flags << std::endl
+                  << std::dec;
         break;
     }
-    fprintf( textFile, "\tTaken: %5" PRIu64 "\n", (uint64_t) arc->counter );
+
+    textFile << "\tTaken: " << std::setw( 5 ) << arc->counter << std::endl;
   }
 
   void GcovFunctionData::printBlockInfo(
-    FILE * textFile,
+    std::ofstream&    textFile,
     blocks_iterator_t block
   )
   {
     std::list<uint32_t>::iterator  line;
 
-    fprintf(
-      textFile,
-      " > BLOCK %3u from %s\n"
-      "    -counter: %5" PRIu64 "\n"
-      "    -flags: 0x%" PRIx32 "\n"
-      "    -lines: ",
-      block->id,
-      block->sourceFileName,
-      (uint64_t) block->counter,
-      block->flags
-    );
-    if ( !block->lines.empty( ) )
-      for ( line = block->lines.begin() ; line != block->lines.end(); line++ )
-        fprintf ( textFile, "%u, ", *line);
-    fprintf ( textFile, "\n");
+    textFile << " > BLOCK " << std::setw( 3 ) << block->id
+             << " from " << block->sourceFileName << std::endl
+             << "    -counter: " << std::setw( 5 ) << block->counter << std::endl
+             << "    -flags: 0x" << std::hex << block->flags << std::endl
+             << "    -lines: ";
+
+    if ( !block->lines.empty() )
+      for ( line = block->lines.begin(); line != block->lines.end(); line++ ) {
+        textFile << *line << ", ";
+      }
+
+    textFile << std::endl;
   }
 
   bool GcovFunctionData::processFunctionCounters( void ) {
@@ -410,14 +383,12 @@ namespace Gcov {
     std::list<uint64_t>    taken;       // List of taken counts for branches
     std::list<uint64_t>    notTaken;    // List of not taken counts for branches
 
-    //fprintf( stderr, "DEBUG: Processing counters for file: %s\n", sourceFileName  );
-    if ( blocks.empty() || arcs.empty() || coverageMap == NULL || symbolInfo->instructions.empty())
-    {
-      //fprintf( stderr,
-      //          "DEBUG: sanity check returned false for function: %s from file: %s\n",
-      //          functionName,
-      //          sourceFileName
-      //);
+    //std::cerr << "DEBUG: Processing counters for file: " << sourceFileName
+    //          << std::endl;
+    if ( blocks.empty() || arcs.empty() || coverageMap == NULL || symbolInfo->instructions.empty())     {
+      //std::cerr << "DEBUG: sanity check returned false for function: "
+      //          << functionName << " from file: " << sourceFileName
+      //          << std::endl;
       return false;
     }
 
@@ -433,20 +404,19 @@ namespace Gcov {
     // Find taken/not taken values for branches
     if ( !processBranches( &taken , &notTaken ) )
     {
-      //fprintf( stderr,
-      //          "ERROR: Failed to process branches for function: %s from file: %s\n",
-      //          functionName,
-      //          sourceFileName
-      //);
+      //std::cerr << "ERROR: Failed to process branches for function: "
+      //          << functionName << " from file: " << sourceFileName
+      //          << std::endl;
       return false;
     };
 
     // Process the branching arcs
     while ( blockIterator != blocks.end() ) {
-      //fprintf( stderr, "DEBUG: Processing branches\n" );
+      //std::cerr << "DEBUG: Processing branches" << std::endl;
       while ( arcIterator->sourceBlock != blockIterator->id ) {
         if ( arcIterator == arcs.end() ) {
-          //fprintf( stderr, "ERROR: Unexpectedly runned out of arcs to analyze\n" );
+          //std::cerr << "ERROR: Unexpectedly runned out of arcs to analyze"
+          //          << std::endl;
           return false;
         }
         arcIterator++;
@@ -464,15 +434,16 @@ namespace Gcov {
         !( arcIterator2->flags & FAKE_ARC_FLAG )
       ) {
         if ( taken.empty() || notTaken.empty() ) {
-          fprintf(
-            stderr,
-            "ERROR: Branchess missing for function: %s from file: %s\n",
-            functionName,
-            sourceFileName
-          );
+          std::cerr << "ERROR: Branches missing for function: "
+                    << functionName << " from file: " << sourceFileName
+                    << std::endl;
           return false;
         }
-        //fprintf( stderr, "DEBUG: Found true branching arc %3u -> %3u\n", arcIterator->sourceBlock, arcIterator->destinationBlock );
+
+        //std::cerr << "DEBUG: Found true branching arc "
+        //          << std::setw( 3 ) << arcIterator->sourceBlock << " -> "
+        //          << std::setw( 3 ) << arcIteratior->destinationBlock
+        //          << std::endl;
         if ( arcIterator->flags & FALLTHROUGH_ARC_FLAG ) {
           arcIterator->counter = notTaken.front();
           notTaken.pop_front();
@@ -513,7 +484,8 @@ namespace Gcov {
     while ( blockIterator != blocks.end() ) {
       while ( arcIterator->sourceBlock != blockIterator->id ) {
         if ( arcIterator == arcs.end() ) {
-          fprintf( stderr, "ERROR: Unexpectedly runned out of arcs to analyze\n" );
+          std::cerr << "ERROR: Unexpectedly runned out of arcs to analyze"
+                    << std::endl;
           return false;
         }
         arcIterator++;
@@ -522,11 +494,9 @@ namespace Gcov {
 
       // If this is the last arc, propagate counter and exit
       if ( arcIterator2 == arcs.end() ) {
-        //fprintf( stderr,
-        //        "DEBUG: Found last arc %3u -> %3u\n",
-        //        arcIterator->sourceBlock,
-        //        arcIterator->destinationBlock
-        //);
+        //std::cerr << "DEBUG: Found last arc " << std::setw( 3 )
+        //          << arcIterator->sourceBlock << " -> " << std::setw( 3 )
+        //          << arcIterator->destinationBlock << std::endl;
         arcIterator->counter = blockIterator->counter;
         blockIterator2 =  blocks.begin();
         while ( arcIterator->destinationBlock != blockIterator2->id)  //TODO: ADD FAILSAFE
@@ -537,7 +507,9 @@ namespace Gcov {
 
       // If this is not a branch, propagate counter and continue
       if ( arcIterator->sourceBlock != arcIterator2->sourceBlock ) {
-        //fprintf( stderr, "DEBUG: Found simple arc %3u -> %3u\n", arcIterator->sourceBlock, arcIterator->destinationBlock );
+        //std::cerr << "DEBUG: Found simple arc " << std::setw( 3 )
+        //          << arcIterator->sourceBlock << " -> " << std::setw( 3 )
+        //          << arcIterator->destinationBlock << std::endl;
         arcIterator->counter = blockIterator->counter;
         blockIterator2 =  blocks.begin();;
         while ( arcIterator->destinationBlock != blockIterator2->id) //TODO: ADD FAILSAFE
@@ -548,7 +520,9 @@ namespace Gcov {
       // If this is  a branch with FAKE arc
       else if ( (arcIterator->sourceBlock == arcIterator2->sourceBlock ) && ( arcIterator2->flags & FAKE_ARC_FLAG ))
       {
-        //fprintf( stderr, "DEBUG: Found fake branching arc %3u -> %3u\n", arcIterator->sourceBlock, arcIterator->destinationBlock );
+        //std::cerr << "DEBUG: Found fake branching arc " << std::setw( 3 )
+        //          << arcIterator->sourceBlock << " -> " << std::setw( 3 )
+        //          << arcIterator->destinationBlock << std::endl;
         arcIterator->counter = blockIterator->counter;
         blockIterator2 =  blocks.begin();
         while ( arcIterator->destinationBlock != blockIterator2->id) //TODO: ADD FAILSAFE
@@ -582,7 +556,8 @@ namespace Gcov {
         break;
       }
 
-    //fprintf( stderr, "DEBUG: Processing instructions in search of branches\n" );
+    // std::cerr << "DEBUG: Processing instructions in search of branches"
+    //           << std::endl;
     for (instruction = symbolInfo->instructions.begin(); instruction != symbolInfo->instructions.end(); instruction++)
     {
       if ( instruction->isInstruction) {
@@ -590,11 +565,12 @@ namespace Gcov {
         if ( instruction->isBranch ) {
           taken->push_back ( (uint64_t) coverageMap->getWasTaken( currentAddress  ) );
           notTaken->push_back ( (uint64_t) coverageMap->getWasNotTaken( currentAddress ) );
-          //fprintf( stderr,
-          //          "Added branch to list taken/not: %4u/%4u\n",
-          //          coverageMap->getWasTaken( currentAddress ),
-          //          coverageMap->getWasNotTaken( currentAddress )
-          //);
+
+          //std::cerr << "Added branch to list taken/not: " << std::setw( 4 )
+          //          << coverageMap->getWasTaken( currentAddress )
+          //          << "/" << std::setw( 4 )
+          //          << coverageMap->getWasNotTaken( currentAddress )
+          //          << std::endl;
         }
       }
     }
diff --git a/tester/covoar/GcovFunctionData.h b/tester/covoar/GcovFunctionData.h
index 7d87b55..e1ad431 100644
--- a/tester/covoar/GcovFunctionData.h
+++ b/tester/covoar/GcovFunctionData.h
@@ -9,6 +9,8 @@
 
 #include <stdint.h>
 #include <list>
+#include <fstream>
+#include <iomanip>
 #include "CoverageMapBase.h"
 #include "DesiredSymbols.h"
 
@@ -35,7 +37,7 @@ struct gcov_block_info
   uint32_t                    flags;
   uint32_t                    numberOfLines;
   uint64_t                    counter;
-  char                        sourceFileName[FILE_NAME_LENGTH];
+  std::string                 sourceFileName;
   std::list<uint32_t>         lines;
 };
 
@@ -101,7 +103,7 @@ class DesiredSymbols;
      *  @return Returns TRUE if the method succeeded and FALSE if it failed.
      */
     bool setFunctionName(
-      const char*               fcnName,
+      const std::string&        fcnName,
       Coverage::DesiredSymbols& symbolsToAnalyze
     );
 
@@ -112,9 +114,7 @@ class DesiredSymbols;
      *
      *  @return Returns TRUE if the method succeeded and FALSE if it failed.
      */
-    bool setFileName(
-      const char*                 fileName
-    );
+    bool setFileName( const std::string& fileName );
 
     /*!
      *  This method stores name of the source file where block is located
@@ -126,7 +126,7 @@ class DesiredSymbols;
      */
     void setBlockFileName(
       const blocks_iterator_t             block,
-      const char*                         fileName
+      const std::string&                  fileName
     );
 
     /*!
@@ -194,9 +194,7 @@ class DesiredSymbols;
      *
      *  @return Returns iterator to a matching block or NULL for error.
      */
-    blocks_iterator_t findBlockById(
-      const uint32_t              id
-    );
+    blocks_iterator_t findBlockById( const uint32_t id );
 
     /*!
      *  This method adds new block to block list
@@ -208,18 +206,24 @@ class DesiredSymbols;
     void addBlock(
       const uint32_t              id,
       const uint32_t              flags,
-      const char *                sourceFileName
+      const std::string&          sourceFileName
     );
 
     /*!
      *  This method prints info about function
      */
-    void printFunctionInfo( FILE * textFile, uint32_t function_number );
+    void printFunctionInfo(
+      std::ofstream& textFile,
+      uint32_t       function_number
+    );
 
     /*!
      *  This method prints info about coverage of this function
      */
-    void printCoverageInfo( FILE * textFile, uint32_t function_number );
+    void printCoverageInfo(
+      std::ofstream& textFile,
+      uint32_t       function_number
+    );
 
     /*!
      *  This method prints info about chosen arc in arcs list
@@ -227,20 +231,14 @@ class DesiredSymbols;
      *  @param[in] textFile specifies output file
      *  @param[in] arc passes iterator identifying arc
      */
-    void printArcInfo(
-      FILE * textFile,
-      arcs_iterator_t arc
-    );
+    void printArcInfo( std::ofstream& textFile, arcs_iterator_t arc );
 
     /*!
      *  This method prints info about chosen block in blocks list
      *
      *  @param[in] block passes iterator identifying block
      */
-    void printBlockInfo(
-      FILE * textFile,
-      blocks_iterator_t block
-    );
+    void printBlockInfo( std::ofstream& textFile, blocks_iterator_t block );
 
     /*!
      *  This method calculates values of arc counters
@@ -256,8 +254,8 @@ class DesiredSymbols;
     uint32_t            numberOfArcs;
     arcs_t              arcs;
     blocks_t            blocks;
-    char                functionName[FUNCTION_NAME_LENGTH];
-    char                sourceFileName[FILE_NAME_LENGTH];
+    std::string         functionName;
+    std::string         sourceFileName;
 
     /*!
      *  This member contains the unified or merged coverage map



More information about the vc mailing list