[PATCH rtems-tools v1 01/10] CoverageWriter: Convert to C++

Ryan Long ryan.long at oarcorp.com
Thu Sep 9 13:50:22 UTC 2021


Got this fixed when I tested on MacOS. I'll change that "\n" to std::endl.

-----Original Message-----
From: devel <devel-bounces at rtems.org> On Behalf Of Chris Johns
Sent: Wednesday, September 8, 2021 5:44 PM
To: devel at rtems.org
Subject: Re: [PATCH rtems-tools v1 01/10] CoverageWriter: Convert to C++



On 9/9/21 2:44 am, Ryan Long wrote:
> ---
>  tester/covoar/CoverageWriterBase.h    |  4 +++-
>  tester/covoar/CoverageWriterRTEMS.cc  | 26 +++++++++++++++-----------
>  tester/covoar/CoverageWriterRTEMS.h   |  4 +++-
>  tester/covoar/CoverageWriterSkyeye.cc | 33 
> ++++++++++++++++++---------------  tester/covoar/CoverageWriterSkyeye.h  |  4 +++-
>  tester/covoar/CoverageWriterTSIM.cc   | 23 +++++++++++------------
>  tester/covoar/CoverageWriterTSIM.h    |  4 +++-
>  7 files changed, 56 insertions(+), 42 deletions(-)
> 
> diff --git a/tester/covoar/CoverageWriterBase.h 
> b/tester/covoar/CoverageWriterBase.h
> index e43a45b..fca501a 100644
> --- a/tester/covoar/CoverageWriterBase.h
> +++ b/tester/covoar/CoverageWriterBase.h
> @@ -9,6 +9,8 @@
>  
>  #include <stdint.h>
>  
> +#include <string>
> +
>  #include "CoverageMapBase.h"
>  
>  namespace Coverage {
> @@ -46,7 +48,7 @@ namespace Coverage {
>       *  @return Returns TRUE if the method succeeded and FALSE if it failed.
>       */
>      virtual void writeFile(
> -      const char* const file,
> +      const std::string& file,
>        CoverageMapBase*  coverage,
>        uint32_t          lowAddress,
>        uint32_t          highAddress
> diff --git a/tester/covoar/CoverageWriterRTEMS.cc 
> b/tester/covoar/CoverageWriterRTEMS.cc
> index 48e8fd1..3332cd4 100644
> --- a/tester/covoar/CoverageWriterRTEMS.cc
> +++ b/tester/covoar/CoverageWriterRTEMS.cc
> @@ -10,6 +10,7 @@
>  #include <string.h>
>  
>  #include <iostream>
> +#include <fstream>
>  #include <iomanip>
>  
>  #include <rld.h>
> @@ -28,23 +29,23 @@ namespace Coverage {
>    }
>  
>    void CoverageWriterRTEMS::writeFile(
> -    const char* const file,
> +    const std::string& file,
>      CoverageMapBase*  coverage,
>      uint32_t          lowAddress,
>      uint32_t          highAddress
>    )
>    {
> -    FILE*                       coverageFile;
> +    std::ofstream               coverageFile;
>      uint32_t                    a;
> -    int                         status;
>      uint8_t                     cover;
>      rtems_coverage_map_header_t header;
> +    long int                    bytes_before;

This need to change. Please review the fixes I added for the places this happened.

Building this on MacOS and FreeBSD should show the error.

>      /*
>       *  read the file and update the coverage map passed in
>       */
> -    coverageFile = ::fopen( file, "w" );
> -    if ( !coverageFile ) {
> +    coverageFile.open( file );
> +    if ( !coverageFile.is_open() ) {
>        std::ostringstream what;
>        what << "Unable to open " << file;
>        throw rld::error( what, "CoverageWriterRTEMS::writeFile" ); @@ 
> -58,9 +59,10 @@ namespace Coverage {
>      header.end           = highAddress;
>      strcpy( header.desc, "RTEMS Coverage Data" );
>  
> -    status = ::fwrite(&header, 1, sizeof(header), coverageFile);
> -    if (status != sizeof(header)) {
> -      ::fclose( coverageFile );
> +    bytes_before = coverageFile.tellp();
> +
> +    coverageFile.write( (char *) &header, sizeof( header ) );
> +    if ( coverageFile.tellp() - bytes_before != sizeof( header ) ) {

clang does not like this line

>        std::ostringstream what;
>        what << "Unable to write header to " << file;
>        throw rld::error( what, "CoverageWriterRTEMS::writeFile" ); @@ 
> -68,8 +70,11 @@ namespace Coverage {
>  
>      for ( a=lowAddress ; a < highAddress ; a++ ) {
>        cover  = ((coverage->wasExecuted( a ))     ? 0x01 : 0);
> -      status = fwrite(&cover, 1, sizeof(cover), coverageFile);
> -      if (status != sizeof(cover)) {
> +
> +      bytes_before = coverageFile.tellp();
> +
> +      coverageFile.write( (char *) &cover, sizeof( cover ) );
> +      if ( coverageFile.tellp() - bytes_before != sizeof( cover ) ) {
>          std::cerr << "CoverageWriterRTEMS::writeFile - write to "
>                    << file
>                    << " at address 0x%"
> @@ -81,6 +86,5 @@ namespace Coverage {
>        }
>      }
>  
> -    ::fclose( coverageFile );
>    }
>  }
> diff --git a/tester/covoar/CoverageWriterRTEMS.h 
> b/tester/covoar/CoverageWriterRTEMS.h
> index b66d2fa..c760936 100644
> --- a/tester/covoar/CoverageWriterRTEMS.h
> +++ b/tester/covoar/CoverageWriterRTEMS.h
> @@ -7,6 +7,8 @@
>  #ifndef __COVERAGE_WRITER_RTEMS_H__
>  #define __COVERAGE_WRITER_RTEMS_H__
>  
> +#include <string>
> +
>  #include "CoverageMapBase.h"
>  #include "CoverageWriterBase.h"
>  
> @@ -33,7 +35,7 @@ namespace Coverage {
>  
>      /* Inherit documentation from base class. */
>      void writeFile(
> -      const char* const file,
> +      const std::string& file,
>        CoverageMapBase*  coverage,
>        uint32_t          lowAddress,
>        uint32_t          highAddress
> diff --git a/tester/covoar/CoverageWriterSkyeye.cc 
> b/tester/covoar/CoverageWriterSkyeye.cc
> index 7f78644..5225406 100644
> --- a/tester/covoar/CoverageWriterSkyeye.cc
> +++ b/tester/covoar/CoverageWriterSkyeye.cc
> @@ -12,6 +12,7 @@
>  #include <string.h>
>  
>  #include <iostream>
> +#include <fstream>
>  #include <iomanip>
>  
>  #include <rld.h>
> @@ -30,23 +31,23 @@ namespace Coverage {
>    }
>  
>    void CoverageWriterSkyeye::writeFile(
> -    const char* const file,
> -    CoverageMapBase*  coverage,
> -    uint32_t          lowAddress,
> -    uint32_t          highAddress
> +    const std::string&  file,
> +    CoverageMapBase*    coverage,
> +    uint32_t            lowAddress,
> +    uint32_t            highAddress
>    )
>    {
>      uint32_t      a;
>      uint8_t       cover;
> -    FILE*         coverageFile;
> +    std::ofstream coverageFile;
>      prof_header_t header;
> -    int           status;
> +    long int      bytes_before;

Again here

>  
>      /*
>       *  read the file and update the coverage map passed in
>       */
> -    coverageFile = ::fopen( file, "w" );
> -    if ( !coverageFile ) {
> +    coverageFile.open( file );
> +    if ( !coverageFile.is_open() ) {
>        std::ostringstream what;
>        what << "Unable to open " << file;
>        throw rld::error( what, "CoverageWriterSkyeye::writeFile" ); @@ 
> -60,9 +61,10 @@ namespace Coverage {
>      header.prof_end      = highAddress;
>      strcpy( header.desc, "Skyeye Coverage Data" );
>  
> -    status = ::fwrite(&header, 1, sizeof(header), coverageFile);
> -    if (status != sizeof(header)) {
> -      ::fclose( coverageFile );
> +    bytes_before = coverageFile.tellp();
> +
> +    coverageFile.write( (char *) &header, sizeof( header ) );
> +    if ( coverageFile.tellp() - bytes_before != sizeof( header ) ) {
>        std::ostringstream what;
>        what << "Unable to write header to " << file;
>        throw rld::error( what, "CoverageWriterSkyeye::writeFile" ); @@ 
> -71,9 +73,11 @@ namespace Coverage {
>      for ( a = lowAddress; a < highAddress; a += 8 ) {
>        cover  = ((coverage->wasExecuted( a ))     ? 0x01 : 0);
>        cover |= ((coverage->wasExecuted( a + 4 )) ? 0x10 : 0);
> -      status = fwrite(&cover, 1, sizeof(cover), coverageFile);
> -      if (status != sizeof(cover)) {
> -        ::fclose( coverageFile );
> +
> +      bytes_before = coverageFile.tellp();
> +
> +      coverageFile.write( (char *) &cover, sizeof( cover ) );
> +      if ( coverageFile.tellp() - bytes_before != sizeof( cover ) ) {
>          std::ostringstream what;
>          what << "write to " << file
>               << " at address 0x"
> @@ -85,6 +89,5 @@ namespace Coverage {
>        }
>      }
>  
> -    ::fclose( coverageFile );
>    }
>  }
> diff --git a/tester/covoar/CoverageWriterSkyeye.h 
> b/tester/covoar/CoverageWriterSkyeye.h
> index 8a74f55..8f11959 100644
> --- a/tester/covoar/CoverageWriterSkyeye.h
> +++ b/tester/covoar/CoverageWriterSkyeye.h
> @@ -7,6 +7,8 @@
>  #ifndef __COVERAGE_WRITER_Skyeye_H__
>  #define __COVERAGE_WRITER_Skyeye_H__
>  
> +#include <string>
> +
>  #include "CoverageMapBase.h"
>  #include "CoverageWriterBase.h"
>  
> @@ -33,7 +35,7 @@ namespace Coverage {
>  
>      /* Inherit documentation from base class. */
>      void writeFile(
> -      const char* const file,
> +      const std::string& file,
>        CoverageMapBase*  coverage,
>        uint32_t          lowAddress,
>        uint32_t          highAddress
> diff --git a/tester/covoar/CoverageWriterTSIM.cc 
> b/tester/covoar/CoverageWriterTSIM.cc
> index 62a1f7d..bd15c9d 100644
> --- a/tester/covoar/CoverageWriterTSIM.cc
> +++ b/tester/covoar/CoverageWriterTSIM.cc
> @@ -9,6 +9,7 @@
>  #include <stdlib.h>
>  
>  #include <iostream>
> +#include <fstream>
>  #include <iomanip>
>  
>  #include <rld.h>
> @@ -27,7 +28,7 @@ namespace Coverage {
>  
>  
>    void CoverageWriterTSIM::writeFile(
> -    const char* const file,
> +    const std::string& file,
>      CoverageMapBase*  coverage,
>      uint32_t          lowAddress,
>      uint32_t          highAddress
> @@ -35,30 +36,29 @@ namespace Coverage {
>    {
>      uint32_t a;
>      int      cover;
> -    FILE*    coverageFile;
> +    std::ofstream coverageFile;
>      int      i;
> -    int      status;
>  
>      /*
>       *  read the file and update the coverage map passed in
>       */
> -    coverageFile = ::fopen( file, "w" );
> -    if ( !coverageFile ) {
> +    coverageFile.open( file );
> +    if ( !coverageFile.is_open() ) {
>        std::ostringstream what;
>        what << "Unable to open " << file;
>        throw rld::error( what, "CoverageWriterTSIM::writeFile" );
>      }
>  
>      for ( a = lowAddress; a < highAddress; a += 0x80 ) {
> -      status = fprintf( coverageFile, "%x : ", a );
> -      if ( status == EOF || status == 0 ) {
> +      coverageFile << std::hex << a << " : " << std::dec;
> +      if ( coverageFile.fail() ) {
>          break;
>        }
>        for ( i = 0; i < 0x80; i += 4 ) {
>          cover = ((coverage->wasExecuted( a + i )) ? 1 : 0);
> -        status = ::fprintf( coverageFile, "%d ", cover );
> -        if ( status == EOF || status == 0 ) {
> -          ::fclose( coverageFile );
> +        coverageFile << cover << " ";
> +
> +        if ( coverageFile.fail() ) {
>            std::ostringstream what;
>            what << "write to " << file
>                 << " at address 0x"
> @@ -69,9 +69,8 @@ namespace Coverage {
>            throw rld::error( what, "CoverageWriterTSIM::writeFile" );
>          }
>        }
> -      ::fprintf( coverageFile, "\n" );
> +      coverageFile << "\n";

Should this be std::endl ?

Chris

>      }
>  
> -    ::fclose( coverageFile );
>    }
>  }
> diff --git a/tester/covoar/CoverageWriterTSIM.h 
> b/tester/covoar/CoverageWriterTSIM.h
> index b43e1c6..5ae57f8 100644
> --- a/tester/covoar/CoverageWriterTSIM.h
> +++ b/tester/covoar/CoverageWriterTSIM.h
> @@ -7,6 +7,8 @@
>  #ifndef __COVERAGE_WRITER_TSIM_H__
>  #define __COVERAGE_WRITER_TSIM_H__
>  
> +#include <string>
> +
>  #include "CoverageMapBase.h"
>  #include "CoverageWriterBase.h"
>  
> @@ -33,7 +35,7 @@ namespace Coverage {
>  
>      /* Inherit documentation from base class. */
>      void writeFile(
> -      const char* const file,
> +      const std::string& file,
>        CoverageMapBase*  coverage,
>        uint32_t          lowAddress,
>        uint32_t          highAddress
> 
_______________________________________________
devel mailing list
devel at rtems.org
http://lists.rtems.org/mailman/listinfo/devel


More information about the devel mailing list