[PATCH v1 02/10] CoverageReader: Convert to C++

Ryan Long ryan.long at oarcorp.com
Thu Sep 2 19:24:31 UTC 2021


---
 tester/covoar/CoverageFactory.cc      | 10 +++++-----
 tester/covoar/CoverageFactory.h       |  2 +-
 tester/covoar/CoverageReaderBase.h    |  2 +-
 tester/covoar/CoverageReaderQEMU.cc   | 35 ++++++++++-------------------------
 tester/covoar/CoverageReaderQEMU.h    |  4 +++-
 tester/covoar/CoverageReaderRTEMS.cc  | 20 +++++++++-----------
 tester/covoar/CoverageReaderRTEMS.h   |  2 +-
 tester/covoar/CoverageReaderSkyeye.cc | 20 +++++++++-----------
 tester/covoar/CoverageReaderSkyeye.h  |  2 +-
 tester/covoar/CoverageReaderTSIM.cc   | 21 +++++++++++----------
 tester/covoar/CoverageReaderTSIM.h    |  4 +++-
 11 files changed, 54 insertions(+), 68 deletions(-)

diff --git a/tester/covoar/CoverageFactory.cc b/tester/covoar/CoverageFactory.cc
index 991ba8f..e6c3774 100644
--- a/tester/covoar/CoverageFactory.cc
+++ b/tester/covoar/CoverageFactory.cc
@@ -22,19 +22,19 @@
 #include "CoverageWriterTSIM.h"
 
 Coverage::CoverageFormats_t Coverage::CoverageFormatToEnum(
-  const char* const format
+  const std::string& format
 )
 {
-  if (!strcmp( format, "QEMU" ))
+  if (format == "QEMU")
     return COVERAGE_FORMAT_QEMU;
 
-  if (!strcmp( format, "RTEMS" ))
+  if (format == "RTEMS")
     return COVERAGE_FORMAT_RTEMS;
 
-  if (!strcmp( format, "Skyeye" ))
+  if (format == "Skyeye")
     return COVERAGE_FORMAT_SKYEYE;
 
-  if (!strcmp( format, "TSIM" ))
+  if (format == "TSIM")
     return COVERAGE_FORMAT_TSIM;
 
   std::ostringstream what;
diff --git a/tester/covoar/CoverageFactory.h b/tester/covoar/CoverageFactory.h
index 4770d78..f71860d 100644
--- a/tester/covoar/CoverageFactory.h
+++ b/tester/covoar/CoverageFactory.h
@@ -33,7 +33,7 @@ namespace Coverage {
    *  @return Returns a coverage file format.
    */
   CoverageFormats_t CoverageFormatToEnum(
-    const char* const format
+    const std::string& format
   );
 
   /*!
diff --git a/tester/covoar/CoverageReaderBase.h b/tester/covoar/CoverageReaderBase.h
index 332fc4d..7064b8d 100644
--- a/tester/covoar/CoverageReaderBase.h
+++ b/tester/covoar/CoverageReaderBase.h
@@ -39,7 +39,7 @@ namespace Coverage {
      *             associated executable
      */
     virtual void processFile(
-      const char* const     file,
+      const std::string&    file,
       ExecutableInfo* const executableInformation
     ) = 0;
 
diff --git a/tester/covoar/CoverageReaderQEMU.cc b/tester/covoar/CoverageReaderQEMU.cc
index a44097d..cef09b3 100644
--- a/tester/covoar/CoverageReaderQEMU.cc
+++ b/tester/covoar/CoverageReaderQEMU.cc
@@ -20,12 +20,6 @@
 
 #include "qemu-traces.h"
 
-#if HAVE_OPEN64
-#define OPEN fopen64
-#else
-#define OPEN fopen
-#endif
-
 namespace Coverage {
 
   CoverageReaderQEMU::CoverageReaderQEMU()
@@ -38,14 +32,13 @@ namespace Coverage {
   }
 
   void CoverageReaderQEMU::processFile(
-    const char* const     file,
+    const std::string&    file,
     ExecutableInfo* const executableInformation
   )
   {
     struct trace_header header;
     uintptr_t           i;
-    int                 status;
-    FILE*               traceFile;
+    std::ifstream       traceFile;
     uint8_t             taken;
     uint8_t             notTaken;
     uint8_t             branchInfo;
@@ -57,16 +50,15 @@ namespace Coverage {
     //
     // Open the coverage file and read the header.
     //
-    traceFile = ::OPEN( file, "r" );
-    if (!traceFile) {
+    traceFile.open( file );
+    if ( !traceFile.is_open() ) {
       std::ostringstream what;
       what << "Unable to open " << file;
       throw rld::error( what, "CoverageReaderQEMU::processFile" );
     }
 
-    status = ::fread( &header, sizeof(trace_header), 1, traceFile );
-    if (status != 1) {
-      ::fclose( traceFile );
+    traceFile.read( (char *) &header, sizeof( trace_header ) );
+    if ( traceFile.fail() || traceFile.gcount() != sizeof( trace_header ) ) {
       std::ostringstream what;
       what << "Unable to read header from " << file;
       throw rld::error( what, "CoverageReaderQEMU::processFile" );
@@ -80,22 +72,16 @@ namespace Coverage {
       CoverageMapBase     *aCoverageMap = NULL;
       struct trace_entry  entries[ENTRIES];
       struct trace_entry  *entry;
-      int                 num_entries;
-
 
       // Read and process each line of the coverage file.
-      num_entries = ::fread(
-        entries,
-        sizeof(struct trace_entry),
-        ENTRIES,
-        traceFile
-      );
-      if (num_entries == 0)
+      traceFile.read( (char *) entries, sizeof( struct trace_entry ) );
+      if ( traceFile.gcount() == 0 ) {
         break;
+      }
 
       // Get the coverage map for each entry.  Note that the map is
       // the same for each entry in the coverage map
-      for (int count=0; count<num_entries; count++) {
+      for ( int count = 0; count < traceFile.gcount(); count++ ) {
 
         entry = &entries[count];
 
@@ -134,6 +120,5 @@ namespace Coverage {
         }
       }
     }
-    ::fclose( traceFile );
   }
 }
diff --git a/tester/covoar/CoverageReaderQEMU.h b/tester/covoar/CoverageReaderQEMU.h
index ec87ff0..11ad720 100644
--- a/tester/covoar/CoverageReaderQEMU.h
+++ b/tester/covoar/CoverageReaderQEMU.h
@@ -7,6 +7,8 @@
 #ifndef __COVERAGE_READER_QEMU_H__
 #define __COVERAGE_READER_QEMU_H__
 
+#include <fstream>
+
 #include "CoverageReaderBase.h"
 #include "ExecutableInfo.h"
 
@@ -37,7 +39,7 @@ TBD
 
     /* Inherit documentation from base class. */
     void processFile(
-      const char* const     file,
+      const std::string&    file,
       ExecutableInfo* const executableInformation
     );
   };
diff --git a/tester/covoar/CoverageReaderRTEMS.cc b/tester/covoar/CoverageReaderRTEMS.cc
index a9e8ae8..e869fc2 100644
--- a/tester/covoar/CoverageReaderRTEMS.cc
+++ b/tester/covoar/CoverageReaderRTEMS.cc
@@ -10,6 +10,7 @@
 #include <sys/stat.h>
 
 #include <iostream>
+#include <fstream>
 #include <iomanip>
 
 #include <rld.h>
@@ -30,32 +31,30 @@ namespace Coverage {
   }
 
   void CoverageReaderRTEMS::processFile(
-    const char* const     file,
+    const std::string&    file,
     ExecutableInfo* const executableInformation
   )
   {
     CoverageMapBase*             aCoverageMap = NULL;
     uintptr_t                    baseAddress;
     uint8_t                      cover;
-    FILE*                        coverageFile;
+    std::ifstream                coverageFile;
     rtems_coverage_map_header_t  header;
     uintptr_t                    i;
     uintptr_t                    length;
-    int                          status;
 
     //
     // Open the coverage file and read the header.
     //
-    coverageFile = ::fopen( file, "r" );
-    if (!coverageFile) {
+    coverageFile.open( file );
+    if ( !coverageFile.is_open() ) {
       std::ostringstream what;
       what << "Unable to open " << file;
       throw rld::error( what, "CoverageReaderRTEMS::processFile" );
     }
 
-    status = ::fread( &header, sizeof(header), 1, coverageFile );
-    if (status != 1) {
-      ::fclose( coverageFile );
+    coverageFile.read( (char *) &header, sizeof( header ) );
+    if ( coverageFile.fail() ) {
       std::ostringstream what;
       what << "Unable to read header from " << file;
       throw rld::error( what, "CoverageReaderRTEMS::processFile" );
@@ -68,8 +67,8 @@ namespace Coverage {
     // Read and process each line of the coverage file.
     //
     for (i = 0; i < length; i++) {
-      status = ::fread( &cover, sizeof(uint8_t), 1, coverageFile );
-      if (status != 1) {
+      coverageFile.read( (char *) &cover, sizeof( uint8_t ) );
+      if ( coverageFile.fail() ) {
         std::cerr << "breaking after 0x"
                   << std::hex << std::setfill('0')
                   << std::setw(8) << i
@@ -90,6 +89,5 @@ namespace Coverage {
       }
     }
 
-    ::fclose( coverageFile );
   }
 }
diff --git a/tester/covoar/CoverageReaderRTEMS.h b/tester/covoar/CoverageReaderRTEMS.h
index 0b30285..52103c7 100644
--- a/tester/covoar/CoverageReaderRTEMS.h
+++ b/tester/covoar/CoverageReaderRTEMS.h
@@ -35,7 +35,7 @@ TBD
 
     /* Inherit documentation from base class. */
     void processFile(
-      const char* const     file,
+      const std::string&    file,
       ExecutableInfo* const executableInformation
     );
   };
diff --git a/tester/covoar/CoverageReaderSkyeye.cc b/tester/covoar/CoverageReaderSkyeye.cc
index 0fe33d7..de0d4ad 100644
--- a/tester/covoar/CoverageReaderSkyeye.cc
+++ b/tester/covoar/CoverageReaderSkyeye.cc
@@ -10,6 +10,7 @@
 #include <sys/stat.h>
 
 #include <iostream>
+#include <fstream>
 #include <iomanip>
 
 #include "CoverageReaderSkyeye.h"
@@ -28,32 +29,30 @@ namespace Coverage {
   }
 
   void CoverageReaderSkyeye::processFile(
-    const char* const     file,
+    const std::string&    file,
     ExecutableInfo* const executableInformation
   )
   {
     CoverageMapBase* aCoverageMap = NULL;
     uintptr_t        baseAddress;
     uint8_t          cover;
-    FILE*            coverageFile;
+    std::ifstream    coverageFile;
     prof_header_t    header;
     uintptr_t        i;
     uintptr_t        length;
-    int              status;
 
     //
     // Open the coverage file and read the header.
     //
-    coverageFile = ::fopen( file, "r" );
-    if (!coverageFile) {
+    coverageFile.open( file );
+    if ( !coverageFile ) {
       std::ostringstream what;
       what << "Unable to open " << file;
       throw rld::error( what, "CoverageReaderSkyeye::processFile" );
     }
 
-    status = ::fread( &header, sizeof(header), 1, coverageFile );
-    if (status != 1) {
-      ::fclose( coverageFile );
+    coverageFile.read( (char *) &header, sizeof( header ) );
+    if ( coverageFile.fail() ) {
       std::ostringstream what;
       what << "Unable to read header from " << file;
       throw rld::error( what, "CoverageReaderSkyeye::processFile" );
@@ -66,8 +65,8 @@ namespace Coverage {
     // Read and process each line of the coverage file.
     //
     for (i = 0; i < length; i += 8) {
-      status = ::fread( &cover, sizeof(uint8_t), 1, coverageFile );
-      if (status != 1) {
+      coverageFile.read( (char *) &cover, sizeof( uint8_t ) );
+      if ( coverageFile.gcount() != sizeof( uint8_t ) ) {
         std::cerr << "CoverageReaderSkyeye::ProcessFile - breaking after 0x"
                   << std::hex << std::setfill('0')
                   << std::setw(8) << i
@@ -106,6 +105,5 @@ namespace Coverage {
       }
     }
 
-    ::fclose( coverageFile );
   }
 }
diff --git a/tester/covoar/CoverageReaderSkyeye.h b/tester/covoar/CoverageReaderSkyeye.h
index 156d170..dc85cf7 100644
--- a/tester/covoar/CoverageReaderSkyeye.h
+++ b/tester/covoar/CoverageReaderSkyeye.h
@@ -35,7 +35,7 @@ TBD
 
     /* Inherit documentation from base class. */
     void processFile(
-      const char* const     file,
+      const std::string&    file,
       ExecutableInfo* const executableInformation
     );
   };
diff --git a/tester/covoar/CoverageReaderTSIM.cc b/tester/covoar/CoverageReaderTSIM.cc
index d4b037d..9c6ff7d 100644
--- a/tester/covoar/CoverageReaderTSIM.cc
+++ b/tester/covoar/CoverageReaderTSIM.cc
@@ -10,6 +10,7 @@
 #include <sys/stat.h>
 
 #include <iostream>
+#include <fstream>
 #include <iomanip>
 
 #include <rld.h>
@@ -30,22 +31,22 @@ namespace Coverage {
   }
 
   void CoverageReaderTSIM::processFile(
-    const char* const     file,
+    const std::string&    file,
     ExecutableInfo* const executableInformation
   )
   {
     CoverageMapBase* aCoverageMap = NULL;
     int              baseAddress;
     int              cover;
-    FILE*            coverageFile;
+    std::ifstream    coverageFile;
     int              i;
-    int              status;
+    char             ignore;
 
     //
     // Open the coverage file.
     //
-    coverageFile = ::fopen( file, "r" );
-    if (!coverageFile) {
+    coverageFile.open( file );
+    if ( !coverageFile.is_open() ) {
       std::ostringstream what;
       what << "Unable to open " << file;
       throw rld::error( what, "CoverageReaderTSIM::processFile" );
@@ -55,15 +56,16 @@ namespace Coverage {
     // Read and process each line of the coverage file.
     //
     while ( true ) {
-      status = ::fscanf( coverageFile, "%x : ", &baseAddress );
-      if (status == EOF || status == 0) {
+      coverageFile >> std::hex >> baseAddress >> ignore >> std::dec;
+      if ( coverageFile.gcount() == 0 || coverageFile.fail() ) {
         break;
       }
 
       for (i = 0; i < 0x80; i += 4) {
         unsigned int a;
-        status = ::fscanf( coverageFile, "%x", &cover );
-        if (status == EOF || status == 0) {
+
+        coverageFile >> std::hex >> cover >> std::dec;
+        if ( coverageFile.fail() || coverageFile.gcount() == 0 ) {
           std::cerr << "CoverageReaderTSIM: WARNING! Short line in "
                     << file
                     << " at address 0x"
@@ -99,6 +101,5 @@ namespace Coverage {
       }
     }
 
-    ::fclose( coverageFile );
   }
 }
diff --git a/tester/covoar/CoverageReaderTSIM.h b/tester/covoar/CoverageReaderTSIM.h
index e4ae849..4980981 100644
--- a/tester/covoar/CoverageReaderTSIM.h
+++ b/tester/covoar/CoverageReaderTSIM.h
@@ -7,6 +7,8 @@
 #ifndef __COVERAGE_READER_TSIM_H__
 #define __COVERAGE_READER_TSIM_H__
 
+#include <string>
+
 #include "CoverageReaderBase.h"
 #include "ExecutableInfo.h"
 
@@ -36,7 +38,7 @@ namespace Coverage {
 
     /* Inherit documentation from base class. */
     void processFile(
-      const char* const     file,
+      const std::string&    file,
       ExecutableInfo* const executableInformation
     );
   };
-- 
1.8.3.1



More information about the devel mailing list