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 <board_item.h>
#include <plugins/kicad/pcb_plugin.h>
#include <plugins/kicad/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 40 of file pcb_parser_tool.cpp.

Enumeration Type Documentation

◆ PARSER_RET_CODES

Enumerator
PARSE_FAILED 
PARSE_FAILED 

Definition at line 90 of file pcb_parser_tool.cpp.

91{
93};
@ TOOL_SPECIFIC
Tools can define their own statuses from here onwards.
@ PARSE_FAILED

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 49 of file pcb_parser_tool.cpp.

50{
51 // Take input from stdin
53 reader.SetStream( aStream );
54
55 PCB_PARSER parser( &reader, nullptr, nullptr );
56 BOARD_ITEM* board = nullptr;
57
58 PARSE_DURATION duration{};
59
60 try
61 {
62 PROF_TIMER timer;
63 board = parser.Parse();
64
65 duration = timer.SinceStart<PARSE_DURATION>();
66 }
67 catch( const IO_ERROR& )
68 {
69 }
70
71 if( aVerbose )
72 {
73 std::cout << "Took: " << duration.count() << "us" << std::endl;
74 }
75
76 return board != nullptr;
77}
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:58
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:76
Read a Pcbnew s-expression formatted LINE_READER object and returns the appropriate BOARD_ITEM object...
Definition: pcb_parser.h:74
A small class to help profiling.
Definition: profile.h:47
DURATION SinceStart(bool aSinceLast=false)
Definition: profile.h:133
LINE_READER that wraps a given std::istream instance.
void SetStream(std::istream &aStream)
Set the stream for this line reader.
std::chrono::microseconds PARSE_DURATION

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

Referenced by PLUGIN_CONTENT_MANAGER::fetchPackages(), SIM_SERDE::InferTypeFromRefAndValue(), PCM_TASK_MANAGER::InstallFromFile(), SPICE_GENERATOR_SOURCE::ItemLine(), JSON_SETTINGS::LoadFromFile(), JSON_SETTINGS::LoadFromRawFile(), SIM_MODEL_RAW_SPICE::parseLegacyPinsField(), SIM_SERDE::ParseParams(), SIM_SERDE::ParsePins(), SIM_SERDE::ParseValue(), pcb_parser_main_func(), NETLIST_EXPORTER_SPICE::ReadDirectives(), SPICE_LIBRARY_PARSER::ReadFile(), GERBER_JOBFILE_READER::ReadGerberJobFile(), SPICE_MODEL_PARSER::ReadModel(), SPICE_MODEL_PARSER_SUBCKT::ReadModel(), SPICE_MODEL_PARSER::ReadType(), and GERBV::IFACE::SaveFileAs().

◆ pcb_parser_main_func()

int pcb_parser_main_func ( int  argc,
char **  argv 
)

Definition at line 96 of file pcb_parser_tool.cpp.

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

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 }
}

Definition at line 80 of file pcb_parser_tool.cpp.

Referenced by pcb_parser_main_func().

◆ registered

bool registered
static
Initial value:
= UTILITY_REGISTRY::Register( { "pcb_parser",
"Parse a KiCad PCB file",
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 155 of file pcb_parser_tool.cpp.