KiCad PCB EDA Suite
Loading...
Searching...
No Matches
erc_settings.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2024 CERN
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 * @author Jon Evans <[email protected]>
7 *
8 * This program is free software: you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation, either version 3 of the License, or (at your
11 * option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#include <algorithm>
23#include <erc/erc_item.h>
24#include <erc/erc_settings.h>
25#include <schematic.h>
26#include <sch_marker.h>
27#include <sch_screen.h>
29#include <settings/parameters.h>
30
31
33
34
35
36#define OK PIN_ERROR::OK
37#define ERR PIN_ERROR::PP_ERROR
38#define WAR PIN_ERROR::WARNING
39
44{
45/* I, O, Bi, 3S, Pas, NIC, UnS, PwrI, PwrO, OC, OE, NC */
46/* I */ { OK, OK, OK, OK, OK, OK, WAR, OK, OK, OK, OK, ERR },
47/* O */ { OK, ERR, OK, WAR, OK, OK, WAR, OK, ERR, ERR, ERR, ERR },
48/* Bi */ { OK, OK, OK, OK, OK, OK, WAR, OK, WAR, OK, WAR, ERR },
49/* 3S */ { OK, WAR, OK, OK, OK, OK, WAR, WAR, ERR, WAR, WAR, ERR },
50/*Pas */ { OK, OK, OK, OK, OK, OK, WAR, OK, OK, OK, OK, ERR },
51/*NIC */ { OK, OK, OK, OK, OK, OK, OK, OK, OK, OK, OK, ERR },
52/*UnS */ { WAR, WAR, WAR, WAR, WAR, OK, WAR, WAR, WAR, WAR, WAR, ERR },
53/*PwrI*/ { OK, OK, OK, WAR, OK, OK, WAR, OK, OK, OK, OK, ERR },
54/*PwrO*/ { OK, ERR, WAR, ERR, OK, OK, WAR, OK, ERR, ERR, ERR, ERR },
55/* OC */ { OK, ERR, OK, WAR, OK, OK, WAR, OK, ERR, OK, OK, ERR },
56/* OE */ { OK, ERR, WAR, WAR, OK, OK, WAR, OK, ERR, OK, OK, ERR },
57/* NC */ { ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR }
58};
59
60
72{
73/* I, O, Bi, 3S, Pas, NIC, UnS, PwrI, PwrO, OC, OE, NC */
74/* I */ { NOD, DRV, DRV, DRV, DRV, NOD, DRV, NOD, DRV, DRV, DRV, NPI },
75/* O */ { DRV, DRV, DRV, DRV, DRV, NOD, DRV, DRV, DRV, DRV, DRV, NPI },
76/* Bi */ { DRV, DRV, DRV, DRV, DRV, NOD, DRV, NOD, DRV, DRV, DRV, NPI },
77/* 3S */ { DRV, DRV, DRV, DRV, DRV, NOD, DRV, NOD, DRV, DRV, DRV, NPI },
78/*Pas */ { DRV, DRV, DRV, DRV, DRV, NOD, DRV, NOD, DRV, DRV, DRV, NPI },
79/*NIC */ { NOD, NOD, NOD, NOD, NOD, NOD, NOD, NOD, NOD, NOD, NOD, NPI },
80/*UnS */ { DRV, DRV, DRV, DRV, DRV, NOD, DRV, NOD, DRV, DRV, DRV, NPI },
81/*PwrI*/ { NOD, DRV, NOD, NOD, NOD, NOD, NOD, NOD, DRV, NOD, NOD, NPI },
82/*PwrO*/ { DRV, DRV, DRV, DRV, DRV, NOD, DRV, DRV, DRV, DRV, DRV, NPI },
83/* OC */ { DRV, DRV, DRV, DRV, DRV, NOD, DRV, NOD, DRV, DRV, DRV, NPI },
84/* OE */ { DRV, DRV, DRV, DRV, DRV, NOD, DRV, NOD, DRV, DRV, DRV, NPI },
85/* NC */ { NPI, NPI, NPI, NPI, NPI, NPI, NPI, NPI, NPI, NPI, NPI, NPI }
86};
87
88
89ERC_SETTINGS::ERC_SETTINGS( JSON_SETTINGS* aParent, const std::string& aPath ) :
90 NESTED_SETTINGS( "erc", ercSettingsSchemaVersion, aParent, aPath )
91{
93
94 for( int i = ERCE_FIRST; i <= ERCE_LAST; ++i )
96
97 // Error is the default setting so set non-error priorities here.
126
127 m_params.emplace_back( new PARAM_LAMBDA<nlohmann::json>( "rule_severities",
128 [&]() -> nlohmann::json
129 {
130 nlohmann::json ret = {};
131
132 for( const RC_ITEM& item : ERC_ITEM::GetItemsWithSeverities() )
133 {
134 wxString name = item.GetSettingsKey();
135 int code = item.GetErrorCode();
136
137 if( name.IsEmpty() || m_ERCSeverities.count( code ) == 0 )
138 continue;
139
140 ret[std::string( name.ToUTF8() )] = SeverityToString( m_ERCSeverities[code] );
141 }
142
143 return ret;
144 },
145 [&]( const nlohmann::json& aJson )
146 {
147 if( !aJson.is_object() )
148 return;
149
150 for( const RC_ITEM& item : ERC_ITEM::GetItemsWithSeverities() )
151 {
152 int code = item.GetErrorCode();
153 wxString name = item.GetSettingsKey();
154
155 std::string key( name.ToUTF8() );
156
157 if( aJson.contains( key ) )
158 m_ERCSeverities[code] = SeverityFromString( aJson[key] );
159 }
160 },
161 {} ) );
162
163 m_params.emplace_back( new PARAM_LAMBDA<nlohmann::json>( "erc_exclusions",
164 [&]() -> nlohmann::json
165 {
166 nlohmann::json js = nlohmann::json::array();
167
168 for( const wxString& entry : m_ErcExclusions )
169 js.push_back( { entry, m_ErcExclusionComments[ entry ] } );
170
171 return js;
172 },
173 [&]( const nlohmann::json& aObj )
174 {
175 m_ErcExclusions.clear();
176
177 if( !aObj.is_array() )
178 return;
179
180 for( const nlohmann::json& entry : aObj )
181 {
182 if( entry.is_array() )
183 {
184 wxString serialized = entry[0].get<wxString>();
185 m_ErcExclusions.insert( serialized );
186 m_ErcExclusionComments[ serialized ] = entry[1].get<wxString>();
187 }
188 else if( entry.is_string() )
189 {
190 m_ErcExclusions.insert( entry.get<wxString>() );
191 }
192 }
193 },
194 {} ) );
195
196 m_params.emplace_back( new PARAM_LAMBDA<nlohmann::json>( "pin_map",
197 [&]() -> nlohmann::json
198 {
199 nlohmann::json ret = nlohmann::json::array();
200
201 for( int i = 0; i < ELECTRICAL_PINTYPES_TOTAL; i++ )
202 {
203 nlohmann::json inner = nlohmann::json::array();
204
205 for( int j = 0; j < ELECTRICAL_PINTYPES_TOTAL; j++ )
206 inner.push_back( static_cast<int>( GetPinMapValue( i, j ) ) );
207
208 ret.push_back( inner );
209 }
210
211 return ret;
212 },
213 [&]( const nlohmann::json& aJson )
214 {
215 if( !aJson.is_array() || aJson.size() != ELECTRICAL_PINTYPES_TOTAL )
216 return;
217
218 for( size_t i = 0; i < ELECTRICAL_PINTYPES_TOTAL; i++ )
219 {
220 if( i > aJson.size() - 1 )
221 break;
222
223 nlohmann::json inner = aJson[i];
224
225 if( !inner.is_array() || inner.size() != ELECTRICAL_PINTYPES_TOTAL )
226 return;
227
228 for( size_t j = 0; j < ELECTRICAL_PINTYPES_TOTAL; j++ )
229 {
230 if( inner[j].is_number_integer() )
231 {
232 int val = inner[j].get<int>();
233
234 if( val >= 0 && val <= static_cast<int>( PIN_ERROR::UNCONNECTED ) )
235 SetPinMapValue( i, j, static_cast<PIN_ERROR>( val ) );
236 }
237 }
238 }
239 },
240 {} ) );
241
242 // Pin weights used for sorting. Take care, sorting is descending!
255
257}
258
259
261{
262 if( m_parent )
263 {
264 m_parent->ReleaseNestedSettings( this );
265 m_parent = nullptr;
266 }
267}
268
269
270SEVERITY ERC_SETTINGS::GetSeverity( int aErrorCode ) const
271{
272 // Special-case duplicate pin error. Multiple pins with the same number are allowed
273 // if they share the same net, but having them on different nets is always an error.
274 if( aErrorCode == ERCE_DUPLICATE_PIN_ERROR )
275 {
276 return RPT_SEVERITY_ERROR;
277 }
278 // Special-case pin-to-pin errors:
279 // Ignore-or-not is controlled by ERCE_PIN_TO_PIN_WARNING (for both)
280 // Warning-or-error is controlled by which errorCode it is
281 else if( aErrorCode == ERCE_PIN_TO_PIN_ERROR )
282 {
283 wxASSERT( m_ERCSeverities.count( ERCE_PIN_TO_PIN_WARNING ) );
284
286 return RPT_SEVERITY_IGNORE;
287 else
288 return RPT_SEVERITY_ERROR;
289 }
290 else if( aErrorCode == ERCE_PIN_TO_PIN_WARNING )
291 {
292 wxASSERT( m_ERCSeverities.count( ERCE_PIN_TO_PIN_WARNING ) );
293
295 return RPT_SEVERITY_IGNORE;
296 else
298 }
299 else if( aErrorCode == ERCE_GENERIC_WARNING )
300 {
302 }
303 else if( aErrorCode == ERCE_GENERIC_ERROR )
304 {
305 return RPT_SEVERITY_ERROR;
306 }
307
308 wxCHECK_MSG( m_ERCSeverities.count( aErrorCode ), RPT_SEVERITY_IGNORE,
309 wxS( "Missing severity from map in ERC_SETTINGS!" ) );
310
311 return m_ERCSeverities.at( aErrorCode );
312}
313
314
315void ERC_SETTINGS::SetSeverity( int aErrorCode, SEVERITY aSeverity )
316{
317 m_ERCSeverities[ aErrorCode ] = aSeverity;
318}
319
320
322{
323 memcpy( m_PinMap, m_defaultPinMap, sizeof( m_PinMap ) );
324}
325
326
328{
329 bool operator()( const SCH_MARKER* item1, const SCH_MARKER* item2 ) const
330 {
331 wxCHECK( item1 && item2, false );
332
333 if( item1->GetPosition() == item2->GetPosition() )
334 return item1->SerializeToString() < item2->SerializeToString();
335
336 return item1->GetPosition() < item2->GetPosition();
337 }
338};
339
340
341void SHEETLIST_ERC_ITEMS_PROVIDER::visitMarkers( std::function<void( SCH_MARKER* )> aVisitor ) const
342{
343 std::set<SCH_SCREEN*> seenScreens;
344
345 for( const SCH_SHEET_PATH& sheet : m_schematic->BuildUnorderedSheetList() )
346 {
347 bool firstTime = seenScreens.count( sheet.LastScreen() ) == 0;
348
349 if( firstTime )
350 seenScreens.insert( sheet.LastScreen() );
351
352 std::set<SCH_MARKER*, CompareMarkers> orderedMarkers;
353
354 for( SCH_ITEM* item : sheet.LastScreen()->Items().OfType( SCH_MARKER_T ) )
355 orderedMarkers.insert( static_cast<SCH_MARKER*>( item ) );
356
357 for( SCH_ITEM* item : orderedMarkers )
358 {
359 SCH_MARKER* marker = static_cast<SCH_MARKER*>( item );
360
361 if( marker->GetMarkerType() != MARKER_BASE::MARKER_ERC )
362 continue;
363
364 std::shared_ptr<const ERC_ITEM> ercItem =
365 std::static_pointer_cast<const ERC_ITEM>( marker->GetRCItem() );
366
367 // Only show sheet-specific markers on the owning sheet
368 if( ercItem->IsSheetSpecific() )
369 {
370 if( ercItem->GetSpecificSheetPath() != sheet )
371 continue;
372 }
373
374 // Don't show non-specific markers more than once
375 if( !firstTime && !ercItem->IsSheetSpecific() )
376 continue;
377
378 aVisitor( marker );
379 }
380 }
381}
382
383
385{
386 m_severities = aSeverities;
387
388 m_filteredMarkers.clear();
389
390 ERC_SETTINGS& settings = m_schematic->ErcSettings();
391
393 [&]( SCH_MARKER* aMarker )
394 {
395 SEVERITY markerSeverity;
396
397 if( aMarker->IsExcluded() )
398 markerSeverity = RPT_SEVERITY_EXCLUSION;
399 else
400 markerSeverity = settings.GetSeverity( aMarker->GetRCItem()->GetErrorCode() );
401
402 if( markerSeverity & m_severities )
403 m_filteredMarkers.push_back( aMarker );
404 } );
405
406 // Sort markers so that errors appear before warnings
407 std::stable_sort( m_filteredMarkers.begin(), m_filteredMarkers.end(),
408 []( const SCH_MARKER* a, const SCH_MARKER* b )
409 {
410 return a->GetSeverity() > b->GetSeverity();
411 } );
412}
413
414
419
420
422{
423 if( aSeverity < 0 )
424 return m_filteredMarkers.size();
425
426 int count = 0;
427
428 const ERC_SETTINGS& settings = m_schematic->ErcSettings();
429
431 [&]( SCH_MARKER* aMarker )
432 {
433 SEVERITY markerSeverity;
434
435 if( aMarker->IsExcluded() )
436 markerSeverity = RPT_SEVERITY_EXCLUSION;
437 else
438 markerSeverity = settings.GetSeverity( aMarker->GetRCItem()->GetErrorCode() );
439
440 if( ( markerSeverity & aSeverity ) > 0 )
441 count++;
442 } );
443
444 return count;
445}
446
447
448std::shared_ptr<ERC_ITEM> SHEETLIST_ERC_ITEMS_PROVIDER::GetERCItem( int aIndex ) const
449{
450 SCH_MARKER* marker = m_filteredMarkers[ aIndex ];
451
452 return marker ? std::static_pointer_cast<ERC_ITEM>( marker->GetRCItem() ) : nullptr;
453}
454
455
456std::shared_ptr<RC_ITEM> SHEETLIST_ERC_ITEMS_PROVIDER::GetItem( int aIndex ) const
457{
458 return GetERCItem( aIndex );
459}
460
461
462void SHEETLIST_ERC_ITEMS_PROVIDER::DeleteItem( int aIndex, bool aDeep )
463{
464 SCH_MARKER* marker = m_filteredMarkers[ aIndex ];
465 m_filteredMarkers.erase( m_filteredMarkers.begin() + aIndex );
466
467 if( aDeep )
468 {
469 SCH_SCREENS screens( m_schematic->Root() );
470 screens.DeleteMarker( marker );
471 }
472}
473
const char * name
static std::vector< std::reference_wrapper< RC_ITEM > > GetItemsWithSeverities()
Definition erc_item.h:76
Container for ERC settings.
ERC_PIN_SORTING_METRIC m_ERCSortingMetric
The type of sorting used by the ERC checker to resolve multi-pin errors.
std::map< wxString, wxString > m_ErcExclusionComments
std::map< ELECTRICAL_PINTYPE, int > m_PinTypeWeights
Weights for electrical pins used in ERC to decide which pin gets the marker in case of a multi-pin er...
static PIN_ERROR m_defaultPinMap[ELECTRICAL_PINTYPES_TOTAL][ELECTRICAL_PINTYPES_TOTAL]
Default Look up table which gives the ERC error level for a pair of connected pins.
SEVERITY GetSeverity(int aErrorCode) const
ERC_SETTINGS(JSON_SETTINGS *aParent, const std::string &aPath)
void SetSeverity(int aErrorCode, SEVERITY aSeverity)
virtual ~ERC_SETTINGS()
PIN_ERROR GetPinMapValue(int aFirstType, int aSecondType) const
std::map< int, SEVERITY > m_ERCSeverities
std::set< wxString > m_ErcExclusions
static int m_PinMinDrive[ELECTRICAL_PINTYPES_TOTAL][ELECTRICAL_PINTYPES_TOTAL]
Look up table which gives the minimal drive for a pair of connected pins on a net.
void SetPinMapValue(int aFirstType, int aSecondType, PIN_ERROR aValue)
PIN_ERROR m_PinMap[ELECTRICAL_PINTYPES_TOTAL][ELECTRICAL_PINTYPES_TOTAL]
std::vector< PARAM_BASE * > m_params
The list of parameters (owned by this object)
JSON_SETTINGS(const wxString &aFilename, SETTINGS_LOC aLocation, int aSchemaVersion)
bool IsExcluded() const
Definition marker_base.h:93
std::shared_ptr< RC_ITEM > GetRCItem() const
enum MARKER_T GetMarkerType() const
Definition marker_base.h:91
JSON_SETTINGS * m_parent
A pointer to the parent object to load and store from.
NESTED_SETTINGS(const std::string &aName, int aSchemaVersion, JSON_SETTINGS *aParent, const std::string &aPath, bool aLoadFromFile=true)
Like a normal param, but with custom getter and setter functions.
Definition parameters.h:297
A holder for a rule check item, DRC in Pcbnew or ERC in Eeschema.
Definition rc_item.h:84
int GetErrorCode() const
Definition rc_item.h:161
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:168
wxString SerializeToString() const
VECTOR2I GetPosition() const override
Definition sch_marker.h:102
Container class that holds multiple SCH_SCREEN objects in a hierarchy.
Definition sch_screen.h:749
void DeleteMarker(SCH_MARKER *aMarker)
Delete a specific marker.
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
int GetCount(int aSeverity=-1) const override
void SetSeverities(int aSeverities) override
void visitMarkers(std::function< void(SCH_MARKER *)> aVisitor) const
void DeleteItem(int aIndex, bool aDeep) override
Remove (and optionally deletes) the indexed item from the list.
std::shared_ptr< ERC_ITEM > GetERCItem(int aIndex) const
std::vector< SCH_MARKER * > m_filteredMarkers
std::shared_ptr< RC_ITEM > GetItem(int aIndex) const override
Retrieve a RC_ITEM by index.
int GetSeverities() const override
const int ercSettingsSchemaVersion
#define ERR
#define WAR
#define OK
#define DRV
@ ERCE_UNSPECIFIED
@ ERCE_DRIVER_CONFLICT
Conflicting drivers (labels, etc) on a subgraph.
@ ERCE_SIMILAR_POWER
2 power pins are equal for case insensitive comparisons.
@ ERCE_UNCONNECTED_WIRE_ENDPOINT
A label is connected to more than one wire.
@ ERCE_GROUND_PIN_NOT_GROUND
A ground-labeled pin is not on a ground net while another pin is.
@ ERCE_SIMILAR_LABELS
2 labels are equal for case insensitive comparisons.
@ ERCE_STACKED_PIN_SYNTAX
Pin name resembles stacked pin notation.
@ ERCE_FIRST
@ ERCE_ENDPOINT_OFF_GRID
Pin or wire-end off grid.
@ ERCE_SAME_LOCAL_GLOBAL_LABEL
2 labels are equal for case insensitive comparisons.
@ ERCE_LAST
@ ERCE_SIMILAR_LABEL_AND_POWER
label and pin are equal for case insensitive comparisons.
@ ERCE_SAME_LOCAL_GLOBAL_POWER
Local power port and global power port have the same name.
@ ERCE_LABEL_SINGLE_PIN
A label is connected only to a single pin.
@ ERCE_BUS_ENTRY_CONFLICT
A wire connected to a bus doesn't match the bus.
@ ERCE_FOOTPRINT_LINK_ISSUES
The footprint link is invalid, or points to a missing (or inactive) footprint or library.
@ ERCE_DUPLICATE_PIN_ERROR
@ ERCE_NOCONNECT_NOT_CONNECTED
A no connect symbol is not connected to anything.
@ ERCE_FOUR_WAY_JUNCTION
A four-way junction was found.
@ ERCE_SIMULATION_MODEL
An error was found in the simulation model.
@ ERCE_LIB_SYMBOL_MISMATCH
Symbol doesn't match copy in library.
@ ERCE_GENERIC_ERROR
@ ERCE_NOCONNECT_CONNECTED
A no connect symbol is connected to more than 1 pin.
@ ERCE_PIN_TO_PIN_WARNING
@ ERCE_MISSING_INPUT_PIN
Symbol has input pins that are not placed.
@ ERCE_MISSING_UNIT
Symbol has units that are not placed on the schematic.
@ ERCE_FIELD_NAME_WHITESPACE
Field name has leading or trailing whitespace.
@ ERCE_MISSING_BIDI_PIN
Symbol has bi-directional pins that are not placed.
@ ERCE_LIB_SYMBOL_ISSUES
Symbol not found in active libraries.
@ ERCE_FOOTPRINT_FILTERS
The assigned footprint doesn't match the footprint filters.
@ ERCE_GENERIC_WARNING
@ ERCE_SINGLE_GLOBAL_LABEL
A label only exists once in the schematic.
@ ERCE_LABEL_MULTIPLE_WIRES
A label is connected to more than one wire.
@ ERCE_PIN_TO_PIN_ERROR
PIN_ERROR
The values a pin-to-pin entry in the pin matrix can take on.
#define NOD
#define NPI
Types of drive on a net (used for legacy ERC)
@ PT_INPUT
usual pin input: must be connected
Definition pin_type.h:37
@ PT_NC
not connected (must be left open)
Definition pin_type.h:50
@ PT_OUTPUT
usual output
Definition pin_type.h:38
@ PT_TRISTATE
tri state bus pin
Definition pin_type.h:40
@ PT_NIC
not internally connected (may be connected to anything)
Definition pin_type.h:44
@ PT_BIDI
input or output (like port for a microprocessor)
Definition pin_type.h:39
@ PT_OPENEMITTER
pin type open emitter
Definition pin_type.h:49
@ PT_POWER_OUT
output of a regulator: intended to be connected to power input pins
Definition pin_type.h:47
@ PT_OPENCOLLECTOR
pin type open collector
Definition pin_type.h:48
@ PT_POWER_IN
power input (GND, VCC for ICs). Must be connected to a power output.
Definition pin_type.h:46
@ PT_UNSPECIFIED
unknown electrical properties: creates always a warning when connected
Definition pin_type.h:45
@ PT_PASSIVE
pin for passive symbols: must be connected, and can be connected to any pin.
Definition pin_type.h:43
#define ELECTRICAL_PINTYPES_TOTAL
Definition pin_type.h:56
SEVERITY
@ RPT_SEVERITY_WARNING
@ RPT_SEVERITY_ERROR
@ RPT_SEVERITY_UNDEFINED
@ RPT_SEVERITY_EXCLUSION
@ RPT_SEVERITY_IGNORE
bool operator()(const SCH_MARKER *item1, const SCH_MARKER *item2) const
@ SCH_MARKER_T
Definition typeinfo.h:159
SEVERITY SeverityFromString(const wxString &aSeverity)
Definition ui_common.cpp:56
wxString SeverityToString(const SEVERITY &aSeverity)
Definition ui_common.cpp:67