[PATCH 1/5] GcovData: Convert to C++

Ryan Long ryan.long at oarcorp.com
Thu Jul 29 17:45:29 UTC 2021


Change C-style code to C++
---
 tester/covoar/GcovData.cc         | 317 ++++++++++++++++++++------------------
 tester/covoar/GcovData.h          |  32 ++--
 tester/covoar/GcovFunctionData.cc | 287 ++++++++++++++++------------------
 tester/covoar/GcovFunctionData.h  |  44 +++---
 4 files changed, 327 insertions(+), 353 deletions(-)

diff --git a/tester/covoar/GcovData.cc b/tester/covoar/GcovData.cc
index e8b8573..800edae 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
  *
@@ -34,62 +28,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;
   }
 
@@ -98,7 +94,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;
@@ -109,10 +105,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;
@@ -120,9 +116,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;
     }
 
@@ -132,9 +128,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 (
@@ -146,21 +144,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
@@ -171,13 +175,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
@@ -191,12 +201,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
@@ -210,25 +225,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;
@@ -250,7 +266,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;
           }
 
@@ -261,14 +278,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;
         }
 
@@ -279,8 +295,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;
         }
 
@@ -291,12 +307,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];
@@ -308,9 +322,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;
         }
 
@@ -322,30 +337,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;
     }
 
@@ -356,16 +371,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;
     }
 
@@ -374,22 +389,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;
     }
 
@@ -398,17 +414,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;
@@ -419,9 +434,10 @@ namespace Gcov {
     function->setFunctionName( buffer );
     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] );
@@ -433,15 +449,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;
     }
 
@@ -458,25 +474,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 0e74b02..a0f98ac 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"
 
 namespace Gcov {
@@ -81,7 +83,7 @@ struct gcov_statistics
      *
      *  @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
@@ -113,10 +115,10 @@ struct gcov_statistics
 
     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;
 
 
@@ -127,9 +129,7 @@ struct gcov_statistics
      *
      *  @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
@@ -139,10 +139,7 @@ struct gcov_statistics
      *
      *  @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
@@ -153,10 +150,7 @@ struct gcov_statistics
      *  @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
@@ -170,7 +164,7 @@ struct gcov_statistics
      */
     int readFilePreamble(
       gcov_preamble*      preamble,
-      FILE*               gcovFile,
+      std::ifstream&      gcovFile,
       const uint32_t      desiredMagic
     );
 
@@ -185,7 +179,7 @@ struct gcov_statistics
      */
     bool readFunctionFrame(
       gcov_frame_header   header,
-      FILE*               gcovFile,
+      std::ifstream&      gcovFile,
       GcovFunctionData*   function
     );
 
@@ -193,7 +187,7 @@ struct gcov_statistics
      *  This method prints info about previously read *.gcno file
      *  to a specified report file
      */
-    void printGcnoFileInfo( FILE * textFile );
+    void printGcnoFileInfo( std::ofstream& textFile );
   };
 }
 #endif
diff --git a/tester/covoar/GcovFunctionData.cc b/tester/covoar/GcovFunctionData.cc
index 90b1be0..ca4afd6 100644
--- a/tester/covoar/GcovFunctionData.cc
+++ b/tester/covoar/GcovFunctionData.cc
@@ -44,22 +44,19 @@ namespace Gcov {
     firstLineNumber = lineNo;
   }
 
-  bool GcovFunctionData::setFunctionName( const char* fcnName )
+  bool GcovFunctionData::setFunctionName( const std::string& fcnName )
   {
     std::string   symbolName;
 
     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 );
@@ -67,34 +64,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;
   }
 
@@ -171,7 +160,7 @@ namespace Gcov {
   void GcovFunctionData::addBlock(
     const uint32_t  id,
     const uint32_t  flags,
-    const char *    sourceFileName
+    const std::string& sourceFileName
   )
   {
     gcov_block_info block;
@@ -181,44 +170,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,
+    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();
@@ -230,7 +210,7 @@ namespace Gcov {
   }
 
   void GcovFunctionData::printCoverageInfo(
-    FILE     *textFile,
+    std::ofstream& textFile,
     uint32_t  function_number
   )
   {
@@ -251,12 +231,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++
@@ -264,27 +244,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;
         }
       }
     }
@@ -292,10 +275,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(
@@ -307,9 +290,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;
 
@@ -321,78 +302,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 ) {
@@ -407,14 +380,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;
     }
 
@@ -430,20 +401,18 @@ 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\n";
       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\n";
           return false;
         }
         arcIterator++;
@@ -461,15 +430,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();
@@ -510,7 +480,7 @@ 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\n";
           return false;
         }
         arcIterator++;
@@ -519,11 +489,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
@@ -534,7 +502,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
@@ -545,7 +515,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
@@ -579,7 +551,7 @@ namespace Gcov {
         break;
       }
 
-    //fprintf( stderr, "DEBUG: Processing instructions in search of branches\n" );
+    // std::cerr << "DEBUG: Processing instructions in search of branches\n";
     for (instruction = symbolInfo->instructions.begin(); instruction != symbolInfo->instructions.end(); instruction++)
     {
       if ( instruction->isInstruction) {
@@ -587,11 +559,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 812b45c..5e15c43 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;
 };
 
@@ -97,9 +99,7 @@ typedef std::list<gcov_block_info>::iterator    blocks_iterator_t;
      *
      *  @return Returns TRUE if the method succeeded and FALSE if it failed.
      */
-    bool setFunctionName(
-      const char*                 fcnName
-    );
+    bool setFunctionName( const std::string& fcnName );
 
     /*!
      *  This method stores name of the source file where function is located
@@ -108,9 +108,7 @@ typedef std::list<gcov_block_info>::iterator    blocks_iterator_t;
      *
      *  @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
@@ -122,7 +120,7 @@ typedef std::list<gcov_block_info>::iterator    blocks_iterator_t;
      */
     void setBlockFileName(
       const blocks_iterator_t             block,
-      const char*                         fileName
+      const std::string&                  fileName
     );
 
     /*!
@@ -190,9 +188,7 @@ typedef std::list<gcov_block_info>::iterator    blocks_iterator_t;
      *
      *  @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
@@ -204,18 +200,24 @@ typedef std::list<gcov_block_info>::iterator    blocks_iterator_t;
     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
@@ -223,20 +225,14 @@ typedef std::list<gcov_block_info>::iterator    blocks_iterator_t;
      *  @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
@@ -252,8 +248,8 @@ typedef std::list<gcov_block_info>::iterator    blocks_iterator_t;
     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
-- 
1.8.3.1



More information about the devel mailing list