2020-06-13 23:28:08 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2004-2019 Jean-Pierre Charras, jp.charras at wanadoo.fr
|
|
|
|
* Copyright (C) 2014 Dick Hollenbeck, dick@softplc.com
|
|
|
|
* Copyright (C) 2017-2020 KiCad Developers, see change_log.txt for contributors.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
2020-06-18 16:55:22 +00:00
|
|
|
// fixme - way too much includes
|
2020-06-13 23:28:08 +00:00
|
|
|
#include <fctsys.h>
|
|
|
|
#include <pcbnew.h>
|
2020-06-18 16:55:22 +00:00
|
|
|
|
|
|
|
#include <reporter.h>
|
|
|
|
#include <widgets/progress_reporter.h>
|
2020-06-13 23:28:08 +00:00
|
|
|
|
|
|
|
#include <drc_proto/drc_engine.h>
|
|
|
|
#include <drc_proto/drc_rule_parser.h>
|
|
|
|
#include <drc_proto/drc_rule.h>
|
|
|
|
#include <drc_proto/drc_item.h>
|
|
|
|
#include <drc_proto/drc_test_provider.h>
|
|
|
|
|
|
|
|
|
2020-08-12 22:19:34 +00:00
|
|
|
void drcPrintDebugMessage( int level, wxString msg, const char *function, int line )
|
|
|
|
{
|
|
|
|
wxString valueStr;
|
|
|
|
if( wxGetEnv( "DRC_DEBUG", &valueStr ) )
|
|
|
|
{
|
|
|
|
int setLevel = wxAtoi( valueStr );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-18 14:17:16 +00:00
|
|
|
|
|
|
|
test::DRC_ENGINE::DRC_ENGINE( BOARD* aBoard, BOARD_DESIGN_SETTINGS *aSettings ) :
|
2020-06-13 23:28:08 +00:00
|
|
|
m_board( aBoard ),
|
2020-06-19 21:34:19 +00:00
|
|
|
m_designSettings ( aSettings ),
|
|
|
|
m_reporter( nullptr ),
|
|
|
|
m_progressReporter( nullptr )
|
2020-08-18 14:17:16 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
2020-06-13 23:28:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
test::DRC_ENGINE::~DRC_ENGINE()
|
|
|
|
{
|
2020-08-11 22:15:50 +00:00
|
|
|
}
|
|
|
|
|
2020-08-18 14:17:16 +00:00
|
|
|
|
2020-08-11 22:15:50 +00:00
|
|
|
test::DRC_REPORT::~DRC_REPORT()
|
|
|
|
{
|
|
|
|
for( auto item : m_entries )
|
|
|
|
{
|
|
|
|
if ( item.m_marker )
|
|
|
|
delete item.m_marker;
|
|
|
|
}
|
2020-06-13 23:28:08 +00:00
|
|
|
}
|
|
|
|
|
2020-08-18 14:17:16 +00:00
|
|
|
|
2020-06-13 23:28:08 +00:00
|
|
|
/*void test::DRC_ENGINE::AddMarker( MARKER_PCB* aMarker )
|
|
|
|
{
|
|
|
|
if( m_designSettings->Ignore( aMarker->GetRCItem()->GetErrorCode() ) )
|
|
|
|
{
|
|
|
|
delete aMarker;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_markers.push_back( aMarker );
|
|
|
|
}*/
|
|
|
|
|
2020-08-18 14:17:16 +00:00
|
|
|
|
2020-06-13 23:28:08 +00:00
|
|
|
bool test::DRC_ENGINE::LoadRules( wxFileName aPath )
|
|
|
|
{
|
2020-08-18 14:17:16 +00:00
|
|
|
|
2020-06-13 23:28:08 +00:00
|
|
|
if( aPath.FileExists() )
|
|
|
|
{
|
|
|
|
m_ruleConditions.clear();
|
|
|
|
m_rules.clear();
|
|
|
|
|
|
|
|
FILE* fp = wxFopen( aPath.GetFullPath(), wxT( "rt" ) );
|
|
|
|
|
|
|
|
if( fp )
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
DRC_RULES_PARSER parser( m_board, fp, aPath.GetFullPath() );
|
2020-08-11 22:17:43 +00:00
|
|
|
parser.Parse( m_rules, m_reporter );
|
2020-06-13 23:28:08 +00:00
|
|
|
}
|
|
|
|
catch( PARSE_ERROR& pe )
|
|
|
|
{
|
|
|
|
// Don't leave possibly malformed stuff around for us to trip over
|
|
|
|
m_ruleConditions.clear();
|
|
|
|
m_rules.clear();
|
|
|
|
|
|
|
|
//wxSafeYield( m_editFrame );
|
|
|
|
//m_editFrame->ShowBoardSetupDialog( _( "Rules" ), pe.What(), ID_RULES_EDITOR,
|
2020-07-30 11:24:29 +00:00
|
|
|
// pe.lineNumber, pe.byteIndex );
|
2020-06-13 23:28:08 +00:00
|
|
|
|
|
|
|
throw;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
test::DRC_TEST_PROVIDER *drcCreateClearanceTestProvider( test::DRC_ENGINE *engine );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void test::DRC_ENGINE::inferImplicitRules()
|
|
|
|
{
|
2020-06-18 16:55:22 +00:00
|
|
|
ReportAux( wxString::Format( "Inferring implicit rules (per-item/class overrides, etc...)" ) );
|
2020-06-13 23:28:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool test::DRC_ENGINE::CompileRules()
|
|
|
|
{
|
2020-06-19 21:34:19 +00:00
|
|
|
ReportAux( wxString::Format( "Compiling Rules (%d rules, %d conditions): ", m_rules.size(), m_ruleConditions.size() ) );
|
2020-06-13 23:28:08 +00:00
|
|
|
|
|
|
|
for( auto provider : m_testProviders )
|
|
|
|
{
|
2020-06-18 16:55:22 +00:00
|
|
|
ReportAux( wxString::Format( "- Provider: '%s': ", provider->GetName() ) );
|
2020-08-12 22:19:34 +00:00
|
|
|
drc_dbg(11, "do prov %s", provider->GetName() );
|
2020-06-18 16:55:22 +00:00
|
|
|
|
2020-08-11 22:17:43 +00:00
|
|
|
for ( auto id : provider->GetMatchingConstraintIds() )
|
2020-06-13 23:28:08 +00:00
|
|
|
{
|
2020-08-12 22:19:34 +00:00
|
|
|
drc_dbg(11, "do id %d", id);
|
|
|
|
if( m_constraintMap.find(id) == m_constraintMap.end() )
|
|
|
|
m_constraintMap[id] = new CONSTRAINT_SET;
|
2020-08-11 22:17:43 +00:00
|
|
|
|
2020-08-12 22:19:34 +00:00
|
|
|
m_constraintMap[ id ]->provider = provider;
|
2020-06-13 23:28:08 +00:00
|
|
|
|
|
|
|
for( auto rule : m_rules )
|
2020-08-11 22:17:43 +00:00
|
|
|
{
|
2020-08-12 22:19:34 +00:00
|
|
|
drc_dbg(11, "Scan provider %s, rule %s", provider->GetName(), rule->GetName() );
|
2020-08-11 22:17:43 +00:00
|
|
|
|
|
|
|
if( ! rule->IsEnabled() )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for( auto& constraint : rule->Constraints() )
|
2020-06-13 23:28:08 +00:00
|
|
|
{
|
2020-08-12 22:19:34 +00:00
|
|
|
drc_dbg(11, "scan constraint id %d\n", constraint.GetType() );
|
2020-08-11 22:17:43 +00:00
|
|
|
if( constraint.GetType() != id )
|
|
|
|
continue;
|
|
|
|
|
2020-08-12 22:19:34 +00:00
|
|
|
ReportAux( wxString::Format( " |- Rule: '%s' ", rule->GetName() ) );
|
2020-06-18 16:55:22 +00:00
|
|
|
|
2020-08-12 22:19:34 +00:00
|
|
|
auto rcons = new CONSTRAINT_WITH_CONDITIONS;
|
2020-06-13 23:28:08 +00:00
|
|
|
|
2020-08-11 22:17:43 +00:00
|
|
|
if( rule->IsConditional() )
|
2020-06-17 22:36:54 +00:00
|
|
|
{
|
2020-08-11 22:17:43 +00:00
|
|
|
test::DRC_RULE_CONDITION* condition = rule->Condition();
|
|
|
|
rcons->conditions.push_back( condition );
|
|
|
|
|
|
|
|
bool compileOk = condition->Compile( nullptr, 0, 0 ); // fixme
|
|
|
|
|
|
|
|
ReportAux( wxString::Format( " |- condition: '%s' compile: %s", condition->GetExpression(), compileOk ? "OK" : "ERROR") );
|
2020-06-17 22:36:54 +00:00
|
|
|
}
|
|
|
|
|
2020-08-12 22:19:34 +00:00
|
|
|
rcons->constraint = constraint;
|
|
|
|
rcons->parentRule = rule;
|
|
|
|
m_constraintMap[ id ]->sortedConstraints.push_back( rcons );
|
2020-08-11 22:17:43 +00:00
|
|
|
|
2020-06-13 23:28:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void test::DRC_ENGINE::RunTests( )
|
|
|
|
{
|
|
|
|
//m_largestClearance = m_designSettings->GetBiggestClearanceValue();
|
|
|
|
|
2020-08-11 22:15:50 +00:00
|
|
|
m_drcReport.reset( new test::DRC_REPORT );
|
2020-06-17 22:36:54 +00:00
|
|
|
m_testProviders = DRC_TEST_PROVIDER_REGISTRY::Instance().GetTestProviders();
|
|
|
|
|
|
|
|
for( auto provider : m_testProviders )
|
2020-06-18 16:55:22 +00:00
|
|
|
{
|
|
|
|
ReportAux( wxString::Format( "Create DRC provider: '%s'", provider->GetName() ) );
|
2020-06-17 22:36:54 +00:00
|
|
|
provider->SetDRCEngine( this );
|
2020-06-18 16:55:22 +00:00
|
|
|
}
|
|
|
|
|
2020-06-17 22:36:54 +00:00
|
|
|
|
2020-06-13 23:28:08 +00:00
|
|
|
inferImplicitRules();
|
|
|
|
CompileRules();
|
2020-07-23 14:22:45 +00:00
|
|
|
|
2020-06-17 22:36:54 +00:00
|
|
|
for( auto provider : m_testProviders )
|
|
|
|
{
|
2020-07-23 14:22:45 +00:00
|
|
|
bool skipProvider = false;
|
|
|
|
|
2020-08-11 22:17:43 +00:00
|
|
|
for( auto ruleID : provider->GetMatchingConstraintIds() )
|
2020-07-23 14:22:45 +00:00
|
|
|
{
|
|
|
|
if( !HasCorrectRulesForId( ruleID ) )
|
|
|
|
{
|
|
|
|
ReportAux( wxString::Format( "DRC provider '%s' has no rules provided. Skipping run.", provider->GetName() ) );
|
|
|
|
skipProvider = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( skipProvider )
|
|
|
|
continue;
|
|
|
|
|
2020-08-11 22:17:43 +00:00
|
|
|
drc_dbg(0, "Running test provider: '%s'\n", provider->GetName() );
|
2020-06-18 16:55:22 +00:00
|
|
|
ReportAux( wxString::Format( "Run DRC provider: '%s'", provider->GetName() ) );
|
2020-06-17 22:36:54 +00:00
|
|
|
provider->Run();
|
|
|
|
}
|
2020-06-13 23:28:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-11 22:17:43 +00:00
|
|
|
const test::DRC_CONSTRAINT& test::DRC_ENGINE::EvalRulesForItems( test::DRC_CONSTRAINT_TYPE_T aConstraintId, BOARD_ITEM* a, BOARD_ITEM* b, PCB_LAYER_ID aLayer )
|
2020-06-13 23:28:08 +00:00
|
|
|
{
|
2020-06-17 22:36:54 +00:00
|
|
|
test::DRC_RULE* rv;
|
2020-08-12 22:19:34 +00:00
|
|
|
auto ruleset = m_constraintMap[ aConstraintId ];
|
2020-06-13 23:28:08 +00:00
|
|
|
|
2020-08-12 22:19:34 +00:00
|
|
|
for( auto rcond : ruleset->sortedConstraints )
|
2020-06-17 22:36:54 +00:00
|
|
|
{
|
2020-08-12 22:19:34 +00:00
|
|
|
if( rcond->conditions.size() == 0 ) // uconditional
|
|
|
|
{
|
|
|
|
drc_dbg( 8, " -> rule '%s' matches (unconditional)\n",
|
2020-08-18 14:17:16 +00:00
|
|
|
rcond->constraint.GetParentRule()->GetName()
|
2020-08-12 22:19:34 +00:00
|
|
|
);
|
|
|
|
return rcond->constraint;
|
|
|
|
}
|
2020-06-17 22:36:54 +00:00
|
|
|
for( auto condition : rcond->conditions )
|
|
|
|
{
|
2020-07-31 17:37:24 +00:00
|
|
|
drc_dbg( 8, " -> check condition '%s'\n",
|
2020-08-11 22:17:43 +00:00
|
|
|
condition->GetExpression() );
|
2020-07-27 13:33:06 +00:00
|
|
|
|
2020-08-11 22:17:43 +00:00
|
|
|
bool result = condition->EvaluateFor( a, b, aLayer ); // FIXME: need the actual layer
|
2020-06-17 22:36:54 +00:00
|
|
|
if( result )
|
|
|
|
{
|
2020-07-31 17:37:24 +00:00
|
|
|
drc_dbg( 8, " -> rule '%s' matches, triggered by condition '%s'\n",
|
2020-08-12 22:19:34 +00:00
|
|
|
rcond->constraint.GetParentRule()->GetName(),
|
2020-08-11 22:17:43 +00:00
|
|
|
condition->GetExpression() );
|
2020-08-12 22:19:34 +00:00
|
|
|
|
|
|
|
return rcond->constraint;
|
2020-06-17 22:36:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(false); // should never hapen
|
2020-06-13 23:28:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-11 22:15:50 +00:00
|
|
|
void test::DRC_ENGINE::Report( std::shared_ptr<DRC_ITEM> aItem, ::MARKER_PCB *aMarker )
|
2020-06-13 23:28:08 +00:00
|
|
|
{
|
2020-08-11 22:15:50 +00:00
|
|
|
m_drcReport->AddItem( aItem, aMarker );
|
2020-08-18 14:17:16 +00:00
|
|
|
|
2020-06-18 16:55:22 +00:00
|
|
|
if( m_reporter )
|
|
|
|
{
|
2020-08-18 14:17:16 +00:00
|
|
|
m_reporter->Report ( wxString::Format( "Test '%s': violation of rule '%s' : %s (code %d)",
|
2020-08-11 22:15:50 +00:00
|
|
|
aItem->GetViolatingTest()->GetName(),
|
|
|
|
aItem->GetViolatingRule()->GetName(),
|
|
|
|
aItem->GetErrorMessage(),
|
|
|
|
aItem->GetErrorCode() ), RPT_SEVERITY_ERROR /* fixme */ );
|
|
|
|
|
|
|
|
wxString violatingItemsStr = "Violating items: ";
|
|
|
|
|
|
|
|
if( aMarker )
|
|
|
|
{
|
|
|
|
m_reporter->Report( wxString::Format( " |- violating position (%d, %d)", aMarker->GetPos().x, aMarker->GetPos().y ),
|
|
|
|
RPT_SEVERITY_ERROR /* fixme */ );
|
|
|
|
}
|
2020-06-13 23:28:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-18 16:55:22 +00:00
|
|
|
void test::DRC_ENGINE::ReportAux ( const wxString& aStr )
|
2020-06-13 23:28:08 +00:00
|
|
|
{
|
2020-06-18 16:55:22 +00:00
|
|
|
if( !m_reporter )
|
2020-06-13 23:28:08 +00:00
|
|
|
return;
|
|
|
|
|
2020-06-18 16:55:22 +00:00
|
|
|
m_reporter->Report( aStr, RPT_SEVERITY_INFO );
|
2020-06-13 23:28:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-18 16:55:22 +00:00
|
|
|
void test::DRC_ENGINE::ReportProgress( double aProgress )
|
2020-06-13 23:28:08 +00:00
|
|
|
{
|
2020-06-18 16:55:22 +00:00
|
|
|
if( !m_progressReporter )
|
2020-06-13 23:28:08 +00:00
|
|
|
return;
|
|
|
|
|
2020-06-18 16:55:22 +00:00
|
|
|
m_progressReporter->SetCurrentProgress( aProgress );
|
2020-06-13 23:28:08 +00:00
|
|
|
}
|
|
|
|
|
2020-08-18 14:17:16 +00:00
|
|
|
|
2020-06-18 16:55:22 +00:00
|
|
|
void test::DRC_ENGINE::ReportStage ( const wxString& aStageName, int index, int total )
|
2020-06-13 23:28:08 +00:00
|
|
|
{
|
2020-06-18 16:55:22 +00:00
|
|
|
if( !m_progressReporter )
|
|
|
|
return;
|
2020-06-13 23:28:08 +00:00
|
|
|
|
2020-08-18 14:17:16 +00:00
|
|
|
m_progressReporter->BeginPhase( index ); // fixme: coalesce all stages/test providers
|
2020-06-13 23:28:08 +00:00
|
|
|
}
|
|
|
|
|
2020-08-18 14:17:16 +00:00
|
|
|
|
2020-06-30 08:44:12 +00:00
|
|
|
#if 0
|
2020-08-11 22:17:43 +00:00
|
|
|
test::DRC_CONSTRAINT test::DRC_ENGINE::GetWorstGlobalConstraint( test::DRC_CONSTRAINT_TYPE_T ruleID )
|
2020-06-30 08:44:12 +00:00
|
|
|
{
|
|
|
|
DRC_CONSTRAINT rv;
|
|
|
|
|
|
|
|
rv.m_Value.SetMin( std::numeric_limits<int>::max() );
|
|
|
|
rv.m_Value.SetMax( std::numeric_limits<int>::min() );
|
|
|
|
for( auto rule : QueryRulesById( ruleID ) )
|
|
|
|
{
|
|
|
|
auto mm = rule->GetConstraint().m_Value;
|
|
|
|
if( mm.HasMax() )
|
|
|
|
rv.m_Value.SetMax( std::max( mm.Max(), rv.m_Value.Max() ) );
|
|
|
|
if( mm.HasMin() )
|
|
|
|
rv.m_Value.SetMin( std::min( mm.Min(), rv.m_Value.Min() ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-08-18 14:17:16 +00:00
|
|
|
|
2020-08-11 22:17:43 +00:00
|
|
|
std::vector<test::DRC_CONSTRAINT> test::DRC_ENGINE::QueryConstraintsById( test::DRC_CONSTRAINT_TYPE_T constraintID )
|
2020-06-30 08:44:12 +00:00
|
|
|
{
|
2020-08-11 22:17:43 +00:00
|
|
|
std::vector<test::DRC_CONSTRAINT> rv;
|
2020-08-12 22:19:34 +00:00
|
|
|
for ( auto c : m_constraintMap[constraintID]->sortedConstraints )
|
|
|
|
rv.push_back(c->constraint);
|
2020-06-30 08:44:12 +00:00
|
|
|
return rv;
|
|
|
|
}
|
2020-06-13 23:28:08 +00:00
|
|
|
|
2020-07-23 14:22:45 +00:00
|
|
|
|
2020-08-12 22:19:34 +00:00
|
|
|
bool test::DRC_ENGINE::HasCorrectRulesForId( test::DRC_CONSTRAINT_TYPE_T constraintID )
|
2020-07-23 14:22:45 +00:00
|
|
|
{
|
2020-08-12 22:19:34 +00:00
|
|
|
//drc_dbg(10,"hascorrect id %d size %d\n", ruleID, m_ruleMap[ruleID]->sortedRules.size( ) );
|
|
|
|
return m_constraintMap[constraintID]->sortedConstraints.size() != 0;
|
2020-07-23 14:22:45 +00:00
|
|
|
}
|
|
|
|
|
2020-08-11 22:15:50 +00:00
|
|
|
|
2020-08-11 22:17:43 +00:00
|
|
|
bool test::DRC_ENGINE::QueryWorstConstraint( test::DRC_CONSTRAINT_TYPE_T aConstraintId, test::DRC_CONSTRAINT& aConstraint, test::DRC_CONSTRAINT_QUERY_T aQueryType )
|
2020-08-11 22:15:50 +00:00
|
|
|
{
|
|
|
|
if( aQueryType == DRCCQ_LARGEST_MINIMUM )
|
|
|
|
{
|
|
|
|
int worst = 0;
|
2020-08-11 22:17:43 +00:00
|
|
|
for( const auto constraint : QueryConstraintsById( aConstraintId ) )
|
2020-08-11 22:15:50 +00:00
|
|
|
{
|
2020-08-11 22:17:43 +00:00
|
|
|
if( constraint.GetValue().HasMin() )
|
2020-08-11 22:15:50 +00:00
|
|
|
{
|
2020-08-11 22:17:43 +00:00
|
|
|
int current = constraint.GetValue().Min();
|
2020-08-11 22:15:50 +00:00
|
|
|
|
|
|
|
if( current > worst )
|
|
|
|
{
|
|
|
|
worst = current;
|
2020-08-11 22:17:43 +00:00
|
|
|
aConstraint = constraint;
|
2020-08-11 22:15:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return worst > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|