KiCad PCB EDA Suite
pcb_parser_tool.cpp File Reference
#include <qa_utils/utility_registry.h>
#include <cstdio>
#include <string>
#include <common.h>
#include <profile.h>
#include <wx/cmdline.h>
#include <class_board_item.h>
#include <kicad_plugin.h>
#include <pcb_parser.h>
#include <richio.h>
#include <qa_utils/stdstream_line_reader.h>

Go to the source code of this file.

Typedefs

using PARSE_DURATION = std::chrono::microseconds
 

Enumerations

enum  PARSER_RET_CODES { PARSE_FAILED = KI_TEST::RET_CODES::TOOL_SPECIFIC, PARSE_FAILED = KI_TEST::RET_CODES::TOOL_SPECIFIC }
 

Functions

bool parse (std::istream &aStream, bool aVerbose)
 Parse a PCB or footprint file from the given input stream. More...
 
int pcb_parser_main_func (int argc, char **argv)
 

Variables

static const wxCmdLineEntryDesc g_cmdLineDesc []
 
static bool registered
 

Typedef Documentation

◆ PARSE_DURATION

using PARSE_DURATION = std::chrono::microseconds

Definition at line 45 of file pcb_parser_tool.cpp.

Enumeration Type Documentation

◆ PARSER_RET_CODES

Enumerator
PARSE_FAILED 
PARSE_FAILED 

Definition at line 98 of file pcb_parser_tool.cpp.

99 {
101 };
Tools can define their own statuses from here onwards.

Function Documentation

◆ parse()

bool parse ( std::istream &  aStream,
bool  aVerbose 
)

Parse a PCB or footprint file from the given input stream.

Parameters
aStreamthe input stream to read from
Returns
success, duration (in us)

Definition at line 54 of file pcb_parser_tool.cpp.

55 {
56  // Take input from stdin
58  reader.SetStream( aStream );
59 
60  PCB_PARSER parser;
61 
62  parser.SetLineReader( &reader );
63 
64  BOARD_ITEM* board = nullptr;
65 
66  PARSE_DURATION duration{};
67 
68  try
69  {
70  PROF_COUNTER timer;
71  board = parser.Parse();
72 
73  duration = timer.SinceStart<PARSE_DURATION>();
74  }
75  catch( const IO_ERROR& )
76  {
77  }
78 
79  if( aVerbose )
80  {
81  std::cout << "Took: " << duration.count() << "us" << std::endl;
82  }
83 
84  return board != nullptr;
85 }
BOARD_ITEM is a base class for any item which can be embedded within the BOARD container class,...
PCB_PARSER reads a Pcbnew s-expression formatted LINE_READER object and returns the appropriate BOARD...
Definition: pcb_parser.h:70
The class PROF_COUNTER is a small class to help profiling.
Definition: profile.h:44
void SetStream(std::istream &aStream)
Set the stream for this line reader.
LINE_READER that wraps a given std::istream instance.
BOARD_ITEM * Parse()
Definition: pcb_parser.cpp:484
std::chrono::microseconds PARSE_DURATION
LINE_READER * SetLineReader(LINE_READER *aReader)
Function SetLineReader sets aLineReader into the parser, and returns the previous one,...
Definition: pcb_parser.h:361
DURATION SinceStart(bool aSinceLast=false)
Definition: profile.h:129
Struct IO_ERROR is a class used to hold an error message and may be used when throwing exceptions con...
Definition: ki_exception.h:76

References PCB_PARSER::Parse(), PCB_PARSER::SetLineReader(), STDISTREAM_LINE_READER::SetStream(), and PROF_COUNTER::SinceStart().

Referenced by JSON_SETTINGS::LoadFromFile(), pcb_parser_main_func(), and GERBER_JOBFILE_READER::ReadGerberJobFile().

◆ pcb_parser_main_func()

int pcb_parser_main_func ( int  argc,
char **  argv 
)

Definition at line 104 of file pcb_parser_tool.cpp.

105 {
106 #ifdef __AFL_COMPILER
107  __AFL_INIT();
108 #endif
109 
110  wxMessageOutput::Set( new wxMessageOutputStderr );
111  wxCmdLineParser cl_parser( argc, argv );
112  cl_parser.SetDesc( g_cmdLineDesc );
113  cl_parser.AddUsageText(
114  _( "This program parses PCB files, either from the "
115  "stdin stream or from the given filenames. This can be used either for "
116  "standalone testing of the parser or for fuzz testing." ) );
117 
118  int cmd_parsed_ok = cl_parser.Parse();
119  if( cmd_parsed_ok != 0 )
120  {
121  // Help and invalid input both stop here
122  return ( cmd_parsed_ok == -1 ) ? KI_TEST::RET_CODES::OK : KI_TEST::RET_CODES::BAD_CMDLINE;
123  }
124 
125  const bool verbose = cl_parser.Found( "verbose" );
126 
127  bool ok = true;
128 
129  const auto file_count = cl_parser.GetParamCount();
130 
131  if( file_count == 0 )
132  {
133  // Parse the file provided on stdin - used by AFL to drive the
134  // program
135  // while (__AFL_LOOP(2))
136  {
137  ok = parse( std::cin, verbose );
138  }
139  }
140  else
141  {
142  // Parse 'n' files given on the command line
143  // (this is useful for input minimisation (e.g. afl-tmin) as
144  // well as manual testing
145  for( unsigned i = 0; i < file_count; i++ )
146  {
147  const auto filename = cl_parser.GetParam( i ).ToStdString();
148 
149  if( verbose )
150  std::cout << "Parsing: " << filename << std::endl;
151 
152  std::ifstream fin;
153  fin.open( filename );
154 
155  ok = ok && parse( fin, verbose );
156  }
157  }
158 
159  if( !ok )
161 
162  return KI_TEST::RET_CODES::OK;
163 }
#define OK
bool parse(std::istream &aStream, bool aVerbose)
Parse a PCB or footprint file from the given input stream.
The command line was not correct for the tool.
static const wxCmdLineEntryDesc g_cmdLineDesc[]
#define _(s)
Definition: 3d_actions.cpp:33

References _, KI_TEST::BAD_CMDLINE, g_cmdLineDesc, OK, parse(), and PARSE_FAILED.

Variable Documentation

◆ g_cmdLineDesc

const wxCmdLineEntryDesc g_cmdLineDesc[]
static
Initial value:
= {
{ wxCMD_LINE_SWITCH, "h", "help", _( "displays help on the command line parameters" ).mb_str(),
wxCMD_LINE_VAL_NONE, wxCMD_LINE_OPTION_HELP },
{ wxCMD_LINE_SWITCH, "v", "verbose", _( "print parsing information" ).mb_str() },
{ wxCMD_LINE_PARAM, nullptr, nullptr, _( "input file" ).mb_str(), wxCMD_LINE_VAL_STRING,
wxCMD_LINE_PARAM_OPTIONAL | wxCMD_LINE_PARAM_MULTIPLE },
{ wxCMD_LINE_NONE }
}
#define _(s)
Definition: 3d_actions.cpp:33

Definition at line 88 of file pcb_parser_tool.cpp.

Referenced by pcb_parser_main_func().

◆ registered

bool registered
static
Initial value:
{ "pcb_parser", "Parse a KiCad PCB file", pcb_parser_main_func } )
static bool Register(const KI_TEST::UTILITY_PROGRAM &aProgInfo)
Register a utility program factory function against an ID string.
int pcb_parser_main_func(int argc, char **argv)

Definition at line 166 of file pcb_parser_tool.cpp.