LCOV - code coverage report
Current view: top level - OdbDesignLib/FileModel/Design - NetlistFile.cpp (source / functions) Coverage Total Hit
Test: OdbDesign Test Coverage Lines: 68.9 % 254 175
Test Date: 2026-11-21 03:51:50 Functions: 57.1 % 21 12
Legend: Lines: hit not hit

            Line data    Source code
       1              : #include "NetlistFile.h"
       2              : #include "NetlistFile.h"
       3              : #include <Logger.h>
       4              : #include "../parse_error.h"
       5              : #include "../invalid_odb_error.h"
       6              : #include <climits>
       7              : #include <filesystem>
       8              : #include <ostream>
       9              : #include "../../Constants.h"
      10              : #include <exception>
      11              : #include <cctype>
      12              : #include <iosfwd>
      13              : #include <memory>
      14              : #include <string>
      15              : #include "../parse_info.h"
      16              : #include "netlistfile.pb.h"
      17              : #include "../../Text/Utf8Sanitizer.h"
      18              : 
      19              : using namespace std::filesystem;
      20              : 
      21              : namespace Odb::Lib::FileModel::Design
      22              : {
      23           99 :         NetlistFile::NetlistFile(std::filesystem::path path)
      24           99 :                 : m_path(path)
      25           99 :                 , m_optimized(false)
      26           99 :                 , m_staggered(Staggered::Unknown)
      27              :         {
      28           99 :         }
      29              : 
      30           81 :         NetlistFile::~NetlistFile()
      31              :         {
      32           81 :                 m_netRecords.clear();
      33           81 :                 m_netRecordsByName.clear();
      34           81 :                 m_netPointRecords.clear();
      35           81 :         }
      36              : 
      37            0 :         std::filesystem::path NetlistFile::GetPath() const
      38              :         {
      39            0 :                 return m_path;
      40              :         }
      41              : 
      42           96 :         std::string NetlistFile::GetName() const
      43              :         {
      44           96 :                 return m_name;
      45              :         }
      46              : 
      47            0 :         std::string NetlistFile::GetUnits() const
      48              :         {
      49            0 :                 return m_units;
      50              :         }
      51              : 
      52            0 :         bool NetlistFile::GetOptimized() const
      53              :         {
      54            0 :                 return m_optimized;
      55              :         }
      56              : 
      57            0 :         NetlistFile::Staggered NetlistFile::GetStaggered() const
      58              :         {
      59            0 :                 return m_staggered;
      60              :         }       
      61              : 
      62           84 :         const NetlistFile::NetRecord::Vector& NetlistFile::GetNetRecords() const
      63              :         {
      64           84 :                 return m_netRecords;
      65              :         }
      66              : 
      67            0 :         const NetlistFile::NetRecord::StringMap& NetlistFile::GetNetRecordsByName() const
      68              :         {
      69            0 :                 return m_netRecordsByName;
      70              :         }
      71              : 
      72           82 :         const NetlistFile::NetPointRecord::Vector& NetlistFile::GetNetPointRecords() const
      73              :         {
      74           82 :                 return m_netPointRecords;
      75              :         }
      76              : 
      77           94 :         bool NetlistFile::Parse()
      78              :         {
      79           94 :                 std::ifstream netlistFile;
      80           94 :                 int lineNumber = 0;
      81           94 :                 std::string line;
      82              : 
      83              :                 try
      84              :                 {
      85           94 :                         m_name = std::filesystem::path(m_path).filename().string();
      86              : 
      87          188 :                         loginfo("Parsing netlist: " + m_name + "...");
      88              : 
      89           94 :                         auto netlistFilePath = m_path / "netlist";
      90              : 
      91           94 :                         if (!std::filesystem::exists(netlistFilePath))
      92              :                         {
      93            0 :                                 auto message = "netlist file does not exist: [" + m_path.string() + "]";
      94            0 :                                 throw invalid_odb_error(message.c_str());
      95            0 :                         }
      96              : 
      97           94 :                         else if (!std::filesystem::is_regular_file(netlistFilePath))
      98              :                         {
      99            0 :                                 auto message = "netlist file is not a file: [" + m_path.string() + "]";
     100            0 :                                 throw invalid_odb_error(message.c_str());
     101            0 :                         }
     102              :                         
     103           94 :                         netlistFile.open(netlistFilePath.string(), std::ios::in);
     104           94 :                         if (!netlistFile.is_open())
     105              :                         {
     106            0 :                                 auto message = "unable to open netlist file: [" + m_path.string() + "]";
     107            0 :                                 throw invalid_odb_error(message.c_str());
     108            0 :                         }
     109              : 
     110       601842 :                         while (std::getline(netlistFile, line))
     111              :                         {
     112       601748 :                                 lineNumber++;
     113       601748 :                                 if (!line.empty())
     114              :                                 {
     115       601748 :                                         std::stringstream lineStream(line);
     116              : 
     117       601748 :                                         if (line.find(Constants::COMMENT_TOKEN) == 0)
     118              :                                         {
     119              :                                                 // comment line
     120              :                                         }
     121       601466 :                                         else if (line.find(Constants::UNITS_TOKEN) == 0)
     122              :                                         {
     123              :                                                 // units line
     124           94 :                                                 std::string token;
     125           94 :                                                 if (!std::getline(lineStream, token, '='))
     126              :                                                 {
     127            0 :                                                         throw_parse_error(m_path, line, token, lineNumber);
     128              :                                                 }
     129           94 :                                                 else if (!std::getline(lineStream, token, '='))
     130              :                                                 {
     131            0 :                                                         throw_parse_error(m_path, line, token, lineNumber);
     132              :                                                 }
     133           94 :                                                 m_units = token;
     134           94 :                                         }
     135       601372 :                                         else if (line.find(HEADER_TOKEN) == 0)
     136              :                                         {
     137              :                                                 // header line
     138           94 :                                                 std::string token;
     139              : 
     140              :                                                 // H (throw away
     141           94 :                                                 lineStream >> token;
     142              : 
     143              :                                                 // optimize(d)
     144           94 :                                                 lineStream >> token;
     145           94 :                                                 if (token != "optimize")
     146              :                                                 {
     147            0 :                                                         throw_parse_error(m_path, line, token, lineNumber);
     148              :                                                 }
     149              : 
     150              :                                                 char optimized;                                         
     151           94 :                                                 if (!(lineStream >> optimized))
     152              :                                                 {
     153            0 :                                                         throw_parse_error(m_path, line, token, lineNumber);
     154              :                                                 }
     155              : 
     156           94 :                                                 if (optimized == 'Y' || std::tolower(optimized) == 'y')
     157              :                                                 {
     158            0 :                                                         m_optimized = true;
     159              :                                                 }
     160           94 :                                                 else if (optimized == 'N' || std::tolower(optimized) == 'n')
     161              :                                                 {
     162           94 :                                                         m_optimized = false;
     163              :                                                 }
     164              :                                                 else
     165              :                                                 {
     166            0 :                                                         throw_parse_error(m_path, line, token, lineNumber);
     167              :                                                 }
     168              : 
     169              :                                                 // staggered (optional)
     170           94 :                                                 if (lineStream >> token && token == STAGGERED_KEY)
     171              :                                                 {
     172              :                                                         char staggered;
     173           94 :                                                         if (!(lineStream >> staggered))
     174              :                                                         {
     175            0 :                                                                 throw_parse_error(m_path, line, token, lineNumber);
     176              :                                                         }
     177              :                                                         
     178           94 :                                                         if (staggered == 'Y' || std::tolower(staggered) == 'y')
     179              :                                                         {
     180            0 :                                                                 m_staggered = Staggered::Yes;
     181              :                                                         }
     182           94 :                                                         else if (staggered == 'N' || std::tolower(staggered) == 'n')
     183              :                                                         {
     184           94 :                                                                 m_staggered = Staggered::No;
     185              :                                                         }
     186              :                                                         else
     187              :                                                         {
     188            0 :                                                                 throw_parse_error(m_path, line, token, lineNumber);
     189              :                                                         }
     190              :                                                 }
     191              :                                                 else
     192              :                                                 {
     193            0 :                                                         m_staggered = Staggered::Unknown;
     194              :                                                 }
     195           94 :                                         }
     196       601278 :                                         else if (line.find(NetRecord::FIELD_TOKEN) == 0)
     197              :                                         {
     198              :                                                 // net record line                                              
     199              : 
     200              :                                                 // net serial number
     201        61724 :                                                 std::string token;
     202        61724 :                                                 if (!(lineStream >> token))
     203              :                                                 {
     204            0 :                                                         throw_parse_error(m_path, line, token, lineNumber);
     205              :                                                 }
     206              : 
     207        61724 :                                                 auto strSerialNumber = token;
     208        61724 :                                                 strSerialNumber.erase(0, 1); // remove leading '$'
     209        61724 :                                                 auto ulSerialNumber = std::stoul(strSerialNumber);
     210        61724 :                                                 if (ulSerialNumber > UINT_MAX)
     211              :                                                 {
     212            0 :                                                         throw_parse_error(m_path, line, token, lineNumber);
     213              :                                                 }                       
     214              : 
     215        61724 :                                                 auto unSerialNumber = static_cast<unsigned int>(ulSerialNumber);
     216              : 
     217              :                                                 // net name
     218        61724 :                                                 if (!(lineStream >> token))
     219              :                                                 {
     220            0 :                                                         throw_parse_error(m_path, line, token, lineNumber);
     221              :                                                 }
     222              : 
     223        61724 :                                                 auto& netName = token;
     224              : 
     225        61724 :                                                 auto pNetRecord = std::make_shared<NetRecord>();
     226        61724 :                                                 pNetRecord->serialNumber = unSerialNumber;
     227        61724 :                                                 pNetRecord->netName = netName;
     228        61724 :                                                 m_netRecords.push_back(pNetRecord);
     229              :                                                 //m_netRecordsByName[pNetRecord->netName] = pNetRecord;                                              
     230        61724 :                                         }
     231              :                                         else // NetPointRecord (starts with an unsigned int, netNumber)
     232              :                                         {
     233       539554 :                                                 std::string token;
     234       539554 :                                                 if (!(lineStream >> token))
     235              :                                                 {
     236            0 :                                                         throw_parse_error(m_path, line, token, lineNumber);
     237              :                                                 }
     238              : 
     239       539554 :                                                 auto ulNetNumber = std::stoul(token);
     240       539554 :                                                 if (ulNetNumber >= UINT_MAX)
     241              :                                                 {
     242            0 :                                                         throw_parse_error(m_path, line, token, lineNumber);
     243              :                                                 }
     244              : 
     245       539554 :                                                 auto unNetNumber = static_cast<unsigned int>(ulNetNumber);
     246              : 
     247       539554 :                                                 auto pNetPointRecord = std::make_shared<NetPointRecord>();
     248       539554 :                                                 pNetPointRecord->netNumber = unNetNumber;
     249              : 
     250       539554 :                                                 if (!(lineStream >> pNetPointRecord->radius))
     251              :                                                 {
     252            0 :                                                         throw_parse_error(m_path, line, token, lineNumber);
     253              :                                                 }
     254              : 
     255       539554 :                                                 if (!(lineStream >> pNetPointRecord->x))
     256              :                                                 {
     257            0 :                                                         throw_parse_error(m_path, line, token, lineNumber);
     258              :                                                 }
     259              : 
     260       539554 :                                                 if (!(lineStream >> pNetPointRecord->y))
     261              :                                                 {
     262            0 :                                                         throw_parse_error(m_path, line, token, lineNumber);
     263              :                                                 }
     264              : 
     265              :                                                 // TODO: parse the rest of the NetPointRecord fields
     266              : 
     267       539554 :                                                 m_netPointRecords.push_back(pNetPointRecord);                                                                                                                                                                                                                   
     268       539554 :                                         }
     269       601748 :                                 }
     270              :                         }
     271              : 
     272          188 :                         loginfo("Parsing netlist: " + m_name + " complete");
     273              : 
     274           94 :                         netlistFile.close();
     275           94 :                 }
     276            0 :                 catch (parse_error& pe)
     277              :                 {
     278            0 :                         auto m = pe.toString("Parse Error:");
     279            0 :                         logerror(m);
     280              :                         // cleanup file
     281            0 :                         netlistFile.close();
     282            0 :                         throw pe;
     283              : 
     284            0 :                 }
     285            0 :                 catch (std::exception& e)
     286              :                 {
     287            0 :                         parse_info pi(m_path, line, lineNumber);
     288            0 :                         const auto m = pi.toString();
     289            0 :                         logexception_msg(e, m);
     290            0 :                         netlistFile.close();
     291            0 :                         throw e;
     292            0 :                 }
     293              : 
     294           94 :                 return true;
     295           94 :         }
     296              : 
     297           91 :         std::unique_ptr<Protobuf::NetlistFile> NetlistFile::to_protobuf() const
     298              :         {
     299           91 :                 std::unique_ptr<Protobuf::NetlistFile> pNetlistFileMessage(new Protobuf::NetlistFile);
     300          182 :                 pNetlistFileMessage->set_units(Odb::Lib::Text::ToUtf8(m_units));
     301          182 :                 pNetlistFileMessage->set_path(Odb::Lib::Text::ToUtf8(m_path.string()));
     302          182 :                 pNetlistFileMessage->set_name(Odb::Lib::Text::ToUtf8(m_name));
     303           91 :                 pNetlistFileMessage->set_optimized(m_optimized);
     304           91 :                 pNetlistFileMessage->set_staggered(static_cast<Protobuf::NetlistFile::Staggered>(m_staggered));
     305              : 
     306        59289 :                 for (const auto& pNetRecord : m_netRecords)
     307              :                 {
     308        59198 :                         auto pNetRecordMessage = pNetRecord->to_protobuf();
     309        59198 :                         pNetlistFileMessage->add_netrecordss()->CopyFrom(*pNetRecordMessage);
     310        59198 :                 }
     311              : 
     312           91 :                 for (const auto& kvNetRecord : m_netRecordsByName)
     313              :                 {
     314            0 :                         (*pNetlistFileMessage->mutable_netrecordsbyname())[Odb::Lib::Text::ToUtf8(kvNetRecord.first)] = *kvNetRecord.second->to_protobuf();
     315              :                 }
     316              : 
     317       490880 :                 for (const auto& pNetPointRecord : m_netPointRecords)
     318              :                 {
     319       490789 :                         auto pNetPointRecordMessage = pNetPointRecord->to_protobuf();
     320       490789 :                         pNetlistFileMessage->add_netpointrecords()->CopyFrom(*pNetPointRecordMessage);
     321       490789 :                 }
     322              : 
     323           91 :                 return pNetlistFileMessage;
     324            0 :         }
     325              : 
     326            5 :         void NetlistFile::from_protobuf(const Protobuf::NetlistFile& message)
     327              :         {
     328            5 :                 m_name = message.name();
     329            5 :                 m_path = message.path();
     330            5 :                 m_units = message.units();
     331            5 :                 m_optimized = message.optimized();
     332            5 :                 m_staggered = static_cast<Staggered>(message.staggered());
     333              : 
     334         3291 :                 for (const auto& netRecordMessage : message.netrecordss())
     335              :                 {
     336         3286 :                         auto pNetRecord = std::make_shared<NetRecord>();
     337         3286 :                         pNetRecord->from_protobuf(netRecordMessage);
     338         3286 :                         m_netRecords.push_back(pNetRecord);
     339         3286 :                 }
     340              : 
     341            5 :                 for (const auto& kvNetRecord : message.netrecordsbyname())
     342              :                 {
     343            0 :                         auto pNetRecord = std::make_shared<NetRecord>();
     344            0 :                         pNetRecord->from_protobuf(kvNetRecord.second);                       
     345            0 :                         m_netRecordsByName[pNetRecord->netName] = pNetRecord;
     346            0 :                 }
     347              : 
     348        28864 :                 for (const auto& netPointRecordMessage : message.netpointrecords())
     349              :                 {
     350        28859 :                         auto pNetPointRecord = std::make_shared<NetPointRecord>();
     351        28859 :                         pNetPointRecord->from_protobuf(netPointRecordMessage);
     352        28859 :                         m_netPointRecords.push_back(pNetPointRecord);
     353        28859 :                 }
     354            5 :         }
     355              : 
     356            0 :         bool NetlistFile::Save(std::ostream& os)
     357              :         {
     358            0 :                 os << 'H' << " optimize " << (m_optimized ? 'Y' : 'N') << "staggered " << (m_staggered == Staggered::Yes ? 'Y' : 'N') << std::endl;
     359            0 :                 os << Constants::UNITS_TOKEN << " = " << m_units << std::endl;
     360              : 
     361            0 :                 for (const auto& netRecord : m_netRecords)
     362              :                 {
     363            0 :                         os << NetRecord::FIELD_TOKEN << netRecord->serialNumber << " " << netRecord->netName << std::endl;
     364              :                 }
     365              : 
     366            0 :                 for (const auto& netPointRecord : m_netPointRecords)
     367              :                 {
     368            0 :                         netPointRecord->Save(os);
     369            0 :                         os << std::endl;
     370              :                 }
     371              : 
     372            0 :                 return true;
     373              :         }
     374              : 
     375            0 :         bool NetlistFile::Save(const std::filesystem::path& directory)
     376              :         {
     377            0 :                 auto netlistDir = directory / m_name;
     378            0 :                 if (!create_directory(netlistDir)) return false;
     379              : 
     380            0 :                 std::ofstream netlistFile(netlistDir / "netlist");
     381            0 :                 if (!netlistFile.is_open()) return false;
     382            0 :                 if (! Save(netlistFile)) return false;
     383            0 :                 netlistFile.close();
     384              : 
     385            0 :                 return true;
     386            0 :         }
     387              : 
     388        59198 :         std::unique_ptr<Protobuf::NetlistFile::NetRecord> NetlistFile::NetRecord::to_protobuf() const
     389              :         {
     390        59198 :                 std::unique_ptr<Protobuf::NetlistFile::NetRecord> pNetRecordMessage(new Protobuf::NetlistFile::NetRecord);
     391        59198 :                 pNetRecordMessage->set_serialnumber(serialNumber);
     392       118396 :                 pNetRecordMessage->set_netname(Odb::Lib::Text::ToUtf8(netName));
     393        59198 :                 return pNetRecordMessage;
     394            0 :         }
     395              : 
     396         3286 :         void NetlistFile::NetRecord::from_protobuf(const Protobuf::NetlistFile::NetRecord& message)
     397              :         {
     398         3286 :                 serialNumber = message.serialnumber();
     399         3286 :                 netName = message.netname();
     400         3286 :         }
     401              : 
     402       490789 :         std::unique_ptr<Protobuf::NetlistFile::NetPointRecord> NetlistFile::NetPointRecord::to_protobuf() const
     403              :         {
     404       490789 :                 std::unique_ptr<Protobuf::NetlistFile::NetPointRecord> pNetPointRecordMessage(new Protobuf::NetlistFile::NetPointRecord);
     405       490789 :                 pNetPointRecordMessage->set_netnumber(netNumber);
     406       490789 :                 pNetPointRecordMessage->set_radius(radius);
     407       490789 :                 pNetPointRecordMessage->set_x(x);
     408       490789 :                 pNetPointRecordMessage->set_y(y);
     409       981578 :                 pNetPointRecordMessage->set_epoint(std::to_string(epoint));
     410       490789 :                 pNetPointRecordMessage->set_width(width);
     411       981578 :                 pNetPointRecordMessage->set_exp(std::to_string(exp));
     412       490789 :                 pNetPointRecordMessage->set_fiducialpoint(fiducialPoint);
     413       490789 :                 pNetPointRecordMessage->set_height(height);
     414       490789 :                 pNetPointRecordMessage->set_commentpoint(commentPoint);
     415       490789 :                 pNetPointRecordMessage->set_netnumber(netNumber);
     416       490789 :                 pNetPointRecordMessage->set_side(static_cast<Protobuf::NetlistFile::NetPointRecord::AccessSide>(side));
     417       490789 :                 pNetPointRecordMessage->set_staggeredradius(staggeredRadius);
     418       490789 :                 pNetPointRecordMessage->set_staggeredx(staggeredX);
     419       490789 :                 pNetPointRecordMessage->set_staggeredy(staggeredY);
     420       981578 :                 pNetPointRecordMessage->set_testexecutionside(std::to_string(testExecutionSide));
     421       490789 :                 pNetPointRecordMessage->set_testpoint(testPoint);
     422       490789 :                 pNetPointRecordMessage->set_viapoint(viaPoint);
     423       490789 :                 pNetPointRecordMessage->set_fiducialpoint(fiducialPoint);
     424       490789 :                 return pNetPointRecordMessage;
     425            0 :         }
     426              : 
     427        28859 :         void NetlistFile::NetPointRecord::from_protobuf(const Protobuf::NetlistFile::NetPointRecord& message)
     428              :         {
     429        28859 :                 netNumber = message.netnumber();
     430        28859 :                 radius = message.radius();
     431        28859 :                 x = message.x();
     432        28859 :                 y = message.y();                
     433        28859 :                 if (message.epoint().empty())
     434              :                 {
     435            0 :                         epoint = message.epoint()[0];
     436              :                 }
     437        28859 :                 width = message.width();
     438        28859 :                 if (!message.exp().empty())
     439              :                 {
     440        28859 :                         exp = message.exp()[0];
     441              :                 }
     442        28859 :                 fiducialPoint = message.fiducialpoint();
     443        28859 :                 height = message.height();
     444        28859 :                 commentPoint = message.commentpoint();
     445        28859 :                 netNumber = message.netnumber();
     446        28859 :                 side = static_cast<AccessSide>(message.side());
     447        28859 :                 staggeredRadius = message.staggeredradius();
     448        28859 :                 staggeredX = message.staggeredx();
     449        28859 :                 staggeredY = message.staggeredy();
     450        28859 :                 if (!message.testexecutionside().empty())
     451              :                 {
     452        28859 :                         testExecutionSide = message.testexecutionside()[0];
     453              :                 }
     454        28859 :                 testPoint = message.testpoint();
     455        28859 :                 viaPoint = message.viapoint();
     456        28859 :                 fiducialPoint = message.fiducialpoint();
     457        28859 :         }
     458              : 
     459            0 :         bool NetlistFile::NetPointRecord::Save(std::ostream& os)
     460              :         {
     461            0 :                 return false;
     462              :         }
     463              :         
     464              : } // namespace Odb::Lib::FileModel::Design
        

Generated by: LCOV version 2.0-1